:root {
  --bg: #f5f8ec;
  --ink: #42463a;
  --muted: #7f8572;
  --tile: #cfe5ad;
  --tile-edge: #e2eed0;
  --tile-hover: #bcdc92;
  --line: #dfe5cf;
  --accent: #7fae55;
  --accent-dark: #6a9744;
  --wool: #fffdf7;
  --wool-line: #cdbfa8;
  --face: #4b4142;
  --face-line: #fbeee0;
  --blush: #f2a3a3;
  --wood: #b07d4f;
  --wood-dark: #8a5c36;
  --wood-light: #d4a574;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--ink);
  display: flex;
  justify-content: center;
  -webkit-tap-highlight-color: transparent;
}

main {
  width: min(540px, 100vw);
  padding: max(12px, env(safe-area-inset-top)) 12px max(16px, env(safe-area-inset-bottom));
  text-align: center;
}

h1 { margin: 6px 0 2px; font-size: 1.6rem; letter-spacing: 0.01em; }
.intro { margin: 0 0 12px; color: var(--muted); font-size: 0.95rem; }

/* Difficulty: segmented control */
.levels {
  display: inline-flex;
  background: #e6eed6;
  border-radius: 999px;
  padding: 3px;
  gap: 2px;
}
.levels button {
  border: 0;
  background: transparent;
  color: var(--ink);
  font: inherit;
  font-size: 0.9rem;
  padding: 7px 14px;
  border-radius: 999px;
  cursor: pointer;
  min-height: 34px;
}
.levels button[aria-checked="true"] {
  background: #fff;
  box-shadow: 0 1px 3px rgba(66, 70, 58, 0.18);
  font-weight: 600;
}

/* Board */
.board-wrap { position: relative; margin-top: 10px; }
#board { width: 100%; height: auto; display: block; touch-action: manipulation; user-select: none; }

.cell { fill: var(--tile); stroke: var(--bg); stroke-width: 2.5; stroke-linejoin: round; cursor: pointer; transition: fill 120ms; }
.cell.edge { fill: var(--tile-edge); }
.cell.blocked { cursor: default; }
@media (hover: hover) {
  .cell.open:hover { fill: var(--tile-hover); }
}
.cell.open:active { fill: var(--tile-hover); }

/* Fence: posts (seen from above) and the rails that join neighbouring posts */
.rail { pointer-events: none; }
.rail-bar { stroke: var(--wood); stroke-width: 3.4; stroke-linecap: round; }
.rail.grow .rail-bar { stroke-dasharray: 1 1; animation: grow 280ms ease-out 80ms both; }
@keyframes grow {
  from { stroke-dashoffset: 1; }
  to { stroke-dashoffset: 0; }
}
.post { pointer-events: none; transform-box: view-box; transform-origin: 0 0; }
.post.pop { animation: pop 260ms cubic-bezier(.3, 1.6, .5, 1); }
.post-rim { fill: var(--wood-dark); }
.post-top { fill: var(--wood-light); }
.post-ring { fill: none; stroke: var(--wood); stroke-width: 1.1; }
@keyframes pop {
  from { transform: scale(0.2); opacity: 0.4; }
  to { transform: scale(1); opacity: 1; }
}

/* Sheep */
.piece-pos { transition: transform 240ms ease-in-out; pointer-events: none; }
.piece-pos.gone { opacity: 0; transition: transform 240ms ease-in-out, opacity 400ms ease 200ms; }
.sheep-body.hop { animation: hop 240ms ease-out; }
@keyframes hop {
  0% { transform: translateY(0); }
  45% { transform: translateY(-2px); }
  100% { transform: translateY(0); }
}
.wool-outline { fill: var(--wool); stroke: var(--wool-line); stroke-width: 1.4; }
.wool-fill { fill: var(--wool); }
.leg, .ear, .face { fill: var(--face); }
.blush { fill: var(--blush); opacity: 0.6; }
.face-line { fill: none; stroke: var(--face-line); stroke-width: 0.9; stroke-linecap: round; stroke-linejoin: round; }
.eye { fill: var(--face-line); }
.eye-white { fill: #fff; }
.pupil { fill: var(--face); }
.ear-l, .ear-r { transform-box: view-box; animation: ear-l 2.6s ease-in-out infinite; }
.ear-l { transform-origin: -4px -2px; }
.ear-r { transform-origin: 4px -2px; animation-name: ear-r; animation-delay: 0.15s; }
@keyframes ear-l { 0%, 70%, 100% { transform: rotate(0deg); } 82% { transform: rotate(-16deg); } }
@keyframes ear-r { 0%, 70%, 100% { transform: rotate(0deg); } 82% { transform: rotate(16deg); } }
.tuft { transform-box: view-box; transform-origin: 0px -6px; animation: fluff 2.6s ease-in-out infinite; }
@keyframes fluff { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.08, 0.94); } }
.mood-worried .ear-l, .mood-worried .ear-r { animation-duration: 0.8s; }
.mood-sleepy .ear-l, .mood-sleepy .ear-r, .mood-sleepy .tuft { animation: none; }
.mood-sleepy .ear-l { transform: rotate(14deg); }
.mood-sleepy .ear-r { transform: rotate(-14deg); }
.eyes { transform-box: fill-box; transform-origin: center; animation: blink 4.2s infinite; }
@keyframes blink {
  0%, 94%, 100% { transform: scaleY(1); }
  97% { transform: scaleY(0.1); }
}
.zzz { font: 700 7px system-ui, sans-serif; fill: #8796b3; animation: float 2.4s ease-in-out infinite; }
@keyframes float {
  0%, 100% { transform: translateY(0); opacity: 0.6; }
  50% { transform: translateY(-3px); opacity: 1; }
}

/* Bottom bar */
.bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
  flex-wrap: wrap;
}
.status { color: var(--muted); font-variant-numeric: tabular-nums; font-size: 0.95rem; padding-left: 4px; }
.controls { display: flex; gap: 6px; }

.btn {
  font: inherit;
  font-size: 0.95rem;
  min-height: 40px;
  padding: 0 16px;
  border-radius: 999px;
  border: 1.5px solid var(--line);
  background: #fff;
  color: var(--ink);
  cursor: pointer;
}
.btn:disabled { opacity: 0.4; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
@media (hover: hover) {
  .btn:not(:disabled):hover { border-color: #d6c8b4; }
  .btn.primary:hover { background: var(--accent-dark); border-color: var(--accent-dark); }
}
.btn:focus-visible, .levels button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Win / lose overlay */
#overlay {
  display: none;
  position: absolute;
  inset: 0;
  align-items: center;
  justify-content: center;
  padding: 14px 0;
  background: rgba(245, 248, 236, 0.35);
  border-radius: 12px;
}
/* Keep the card away from the sheep so it stays visible. */
#overlay.sheep-top { align-items: flex-end; }
#overlay.sheep-bottom { align-items: flex-start; }
#overlay.show { display: flex; animation: fade-in 260ms ease-out 260ms both; }
@keyframes fade-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: none; }
}
.panel {
  background: #fff;
  padding: 20px 24px 18px;
  border-radius: 16px;
  box-shadow: 0 6px 24px rgba(66, 70, 58, 0.18);
  max-width: 86%;
}
#overlay-text { font-size: 1.2rem; font-weight: 700; margin: 0 0 14px; }
#overlay.win #overlay-text { color: #5d8c3a; }
#overlay.lose #overlay-text { color: #c0604a; }
.panel-buttons { display: flex; gap: 8px; justify-content: center; }

@media (max-width: 420px) {
  h1 { font-size: 1.4rem; }
  .levels button { padding: 7px 11px; }
  .btn { padding: 0 13px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
