/* Animal Not Crossing: one board, a few buttons, calm colors. */

/* Pixelify Sans (SIL Open Font License 1.1, see fonts/PixelifySans-OFL.txt).
   Used only by the Valley theme; loaded locally so offline play never breaks. */
@font-face {
  font-family: "Pixelify Sans";
  src: url("../../fonts/PixelifySans.ttf") format("truetype");
  font-weight: 400 700;
  font-display: swap;
}

:root {
  color-scheme: light dark;
  --font: ui-rounded, -apple-system, "SF Pro Rounded", "Segoe UI", system-ui, sans-serif;
  --radius: 16px;
  --cell-radius: 6px;
  --s1: 4px; --s2: 8px; --s3: 12px; --s4: 16px; --s5: 24px;
  --ease: cubic-bezier(0.2, 0.8, 0.2, 1);
  --thin: 1px;
  --thick: 3px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

/* Light theme (default) */
:root, html[data-theme="light"] {
  --bg: #f6f1e7;
  --fg: #2b2622;
  --muted: #7a7066;
  --card: #fffdf8;
  --line: rgba(43, 38, 34, 0.14);
  --board-line: rgba(43, 38, 34, 0.18);
  --board-thick: #3a332d;
  --accent: #c8623a;
  --accent-fg: #fff8f2;
  --danger: #d7443e;
  --mark: rgba(43, 38, 34, 0.45);
  --hint: #f2c14e;
  --shadow: 0 10px 30px rgba(43, 38, 34, 0.12);
  --animal: #2b2622;
}

html[data-theme="dark"] {
  --bg: #1c1a17;
  --fg: #f1ebe2;
  --muted: #a39a8f;
  --card: #26231f;
  --line: rgba(241, 235, 226, 0.14);
  --board-line: rgba(241, 235, 226, 0.22);
  --board-thick: #f1ebe2;
  --accent: #e5825a;
  --accent-fg: #1c1a17;
  --danger: #ff6b63;
  --mark: rgba(241, 235, 226, 0.5);
  --hint: #f2c14e;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
  --animal: #f1ebe2;
}

@media (prefers-color-scheme: dark) {
  html[data-theme="auto"] {
    --bg: #1c1a17;
    --fg: #f1ebe2;
    --muted: #a39a8f;
    --card: #26231f;
    --line: rgba(241, 235, 226, 0.14);
    --board-line: rgba(241, 235, 226, 0.22);
    --board-thick: #f1ebe2;
    --accent: #e5825a;
    --accent-fg: #1c1a17;
    --danger: #ff6b63;
    --mark: rgba(241, 235, 226, 0.5);
    --hint: #f2c14e;
    --shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
    --animal: #f1ebe2;
  }
}

/* Region palettes: 12 colors each, set on <html data-palette>. Defined in palettes.js
   and applied as custom properties --region-0 .. --region-11. */

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font);
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

body {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

#app {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: calc(var(--safe-top) + 10px) calc(var(--safe-right) + 14px) calc(var(--safe-bottom) + 8px) calc(var(--safe-left) + 14px);
  max-width: 560px;
  width: 100%;
  margin: 0 auto;
  gap: 10px;
}

#top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 32px;
  padding: 0 2px;
}

#subtitle {
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 600;
  letter-spacing: 0.02em;
}

#status-right {
  display: flex;
  align-items: center;
  gap: 12px;
}

#timer.low { color: var(--danger); font-weight: 700; }
#timer {
  font-variant-numeric: tabular-nums;
  color: var(--muted);
  font-size: 0.95rem;
}

#mistakes {
  display: flex;
  gap: 4px;
  min-height: 18px;
}

#mistakes .paw {
  width: 18px;
  height: 18px;
  color: var(--accent);
  transition: opacity 200ms ease, transform 200ms ease;
}

#mistakes .paw.spent {
  opacity: 0.22;
  transform: scale(0.85);
}

#board-wrap {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  min-height: 0;
  padding-bottom: 4vh; /* sit a touch below centre, closer to the thumb */
}

#board {
  --n: 8;
  --cell: 40px;
  display: grid;
  grid-template-columns: repeat(var(--n), minmax(0, 1fr));
  grid-template-rows: repeat(var(--n), minmax(0, 1fr));
  width: min(100%, calc(100dvh - 250px), 520px);
  aspect-ratio: 1;
  border: var(--thick) solid var(--board-thick);
  border-radius: 12px;
  overflow: hidden;
  background: var(--board-thick);
  gap: 0;
  box-shadow: var(--shadow), 0 0 0 1px rgba(0, 0, 0, 0.04);
  touch-action: none; /* the board handles its own taps, holds and drags */
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  box-shadow: var(--shadow);
}

.cell {
  position: relative;
  appearance: none;
  border: 0;
  margin: 0;
  padding: 0;
  background: var(--region-color, #ddd);
  color: var(--animal);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  touch-action: manipulation;
  outline: none;
  box-shadow:
    inset calc(-1 * var(--thin)) 0 0 var(--board-line),
    inset 0 calc(-1 * var(--thin)) 0 var(--board-line);
}

/* Thick lines where the region changes. Right and bottom edges only, so each
   line is drawn exactly once. */
.cell.b-right { box-shadow: inset calc(-1 * var(--thick)) 0 0 var(--board-thick), inset 0 calc(-1 * var(--thin)) 0 var(--board-line); }
.cell.b-bottom { box-shadow: inset calc(-1 * var(--thin)) 0 0 var(--board-line), inset 0 calc(-1 * var(--thick)) 0 var(--board-thick); }
.cell.b-right.b-bottom { box-shadow: inset calc(-1 * var(--thick)) 0 0 var(--board-thick), inset 0 calc(-1 * var(--thick)) 0 var(--board-thick); }

.cell .icon {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}
.cell .icon svg,
.cell .icon img {
  width: 78%;
  height: 78%;
  display: block;
}
.cell .icon img { filter: drop-shadow(0 1px 1px rgba(0, 0, 0, 0.22)); pointer-events: none; -webkit-user-drag: none; }
.cell.mark .icon::after {
  content: "";
  width: 34%;
  height: 34%;
  background:
    linear-gradient(45deg, transparent 44%, var(--mark) 44%, var(--mark) 56%, transparent 56%),
    linear-gradient(-45deg, transparent 44%, var(--mark) 44%, var(--mark) 56%, transparent 56%);
}
/* Press-and-hold feedback: a ring that fills over HOLD_MS. */
.cell.pressing::after {
  content: "";
  position: absolute;
  inset: 18%;
  border-radius: 50%;
  border: 3px solid var(--fg);
  opacity: 0;
  animation: press-ring 380ms linear forwards;
  pointer-events: none;
}
@keyframes press-ring { 0% { opacity: 0; transform: scale(0.5); } 30% { opacity: 0.5; } 100% { opacity: 0.85; transform: scale(1); } }

.cell.animal.wrong { color: var(--danger); background-image: linear-gradient(rgba(215, 68, 62, 0.28), rgba(215, 68, 62, 0.28)); }
.cell.conflict::before,
.cell.wrong::before {
  content: "";
  position: absolute;
  inset: 3px;
  border: 2px solid var(--danger);
  border-radius: var(--cell-radius);
  pointer-events: none;
}
.cell.hint {
  outline: 3px solid var(--hint);
  outline-offset: -3px;
  background-image: linear-gradient(rgba(242, 193, 78, 0.18), rgba(242, 193, 78, 0.18));
}
.cell.hint-move { background-image: linear-gradient(rgba(242, 193, 78, 0.45), rgba(242, 193, 78, 0.45)); }

.cell.placed .icon svg, .cell.placed .icon img { animation: pop 200ms cubic-bezier(0.2, 0.8, 0.2, 1); }
.cell.shake { animation: shake 320ms ease-in-out; }
.cell:focus-visible { outline: 3px solid var(--accent); outline-offset: -3px; }

/* First-run tip */
#tip {
  width: min(100%, 520px);
  color: var(--muted);
  font-size: 0.85rem;
  text-align: center;
}

@keyframes pop { from { transform: scale(0.7); } 60% { transform: scale(1.08); } to { transform: scale(1); } }
@keyframes shake { 0%, 100% { transform: translateX(0); } 25% { transform: translateX(-3px); } 75% { transform: translateX(3px); } }

/* Hint banner and loading */
#hint-slot {
  width: min(100%, 520px);
  min-height: 58px;
  display: flex;
  align-items: flex-start;
}
#hint-banner {
  width: 100%;
  background: var(--card);
  border: 1px solid var(--line);
  border-left: 4px solid var(--hint);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 0.95rem;
  line-height: 1.35;
  box-shadow: var(--shadow);
}

#loading {
  color: var(--muted);
  font-size: 0.9rem;
  display: flex;
  align-items: center;
  gap: 8px;
}
.spinner {
  width: 14px; height: 14px; border-radius: 50%;
  border: 2px solid var(--line); border-top-color: var(--accent);
  animation: spin 800ms linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Bottom bar */
#bar {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 22px;
  padding: var(--s1);
  box-shadow: var(--shadow);
}

.bar-btn {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--fg);
  border-radius: 18px;
  padding: 8px 4px 6px;
  font: inherit;
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.01em;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  min-height: 56px;
  cursor: pointer;
  touch-action: manipulation;
  transition: background 120ms var(--ease), transform 120ms var(--ease), opacity 120ms var(--ease);
}
.bar-btn + .bar-btn { position: relative; }
.bar-btn + .bar-btn::before {
  content: "";
  position: absolute;
  left: 0;
  top: 14px;
  bottom: 14px;
  width: 1px;
  background: var(--line);
}
.bar-btn svg { width: 22px; height: 22px; }
.bar-btn:active { background: var(--bg); transform: scale(0.96); }
.bar-btn:disabled { opacity: 0.35; }
.bar-btn:disabled:active { transform: none; background: transparent; }
.bar-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }

/* Sheets and cards */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 10;
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
}
.sheet-backdrop[hidden] { display: none; }

.card {
  background: var(--card);
  color: var(--fg);
  border-radius: 20px;
  padding: 22px 20px calc(18px + var(--safe-bottom));
  width: min(100%, 420px);
  box-shadow: var(--shadow);
  text-align: center;
  animation: rise 220ms var(--ease);
}
@keyframes rise { from { transform: translateY(16px); opacity: 0; } to { transform: none; opacity: 1; } }

.card h2 { margin: 4px 0 10px; font-size: 1.25rem; letter-spacing: -0.01em; }
.card p { margin: 6px 0; line-height: 1.45; }
#overlay-message { font-size: 1.15rem; font-weight: 500; padding: 0 6px; }
#overlay-time { color: var(--muted); font-size: 0.85rem; margin-top: 10px; }
#overlay-stat { color: var(--muted); font-size: 0.85rem; margin-top: 2px; }
#overlay-stat:empty { display: none; }
#overlay-art { height: 96px; display: flex; align-items: center; justify-content: center; color: var(--accent); }
#overlay-art svg, #overlay-art img { width: 84px; height: 84px; animation: bounce 1.2s ease-in-out infinite; }
#overlay-art img { width: 96px; height: 96px; filter: drop-shadow(0 2px 2px rgba(0, 0, 0, 0.2)); }
@keyframes bounce { 0%, 100% { transform: translateY(0) rotate(-3deg); } 50% { transform: translateY(-8px) rotate(3deg); } }

button.primary {
  appearance: none;
  border: 0;
  background: var(--accent);
  color: var(--accent-fg);
  font: inherit;
  font-weight: 600;
  font-size: 1rem;
  padding: 12px 20px;
  border-radius: 999px;
  width: 100%;
  margin-top: 14px;
  cursor: pointer;
  touch-action: manipulation;
}
button.link {
  appearance: none;
  border: 0;
  background: none;
  color: var(--muted);
  font: inherit;
  padding: 10px;
  margin-top: 4px;
  cursor: pointer;
}

/* Menu sheet */
.sheet-backdrop:has(.sheet) { align-items: flex-end; padding: 0; }
.sheet {
  width: 100%;
  max-width: 560px;
  max-height: 88dvh;
  overflow-y: auto;
  border-radius: 20px 20px 0 0;
  text-align: left;
  animation: slide 240ms var(--ease);
}
@keyframes slide { from { transform: translateY(40px); opacity: 0; } to { transform: none; opacity: 1; } }
.sheet-handle { width: 40px; height: 4px; border-radius: 2px; background: var(--line); margin: -6px auto 10px; }
.sheet h2 { margin: 0 0 4px; font-size: 1.25rem; }
.sheet h3 { margin: 18px 0 6px 4px; font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.1em; color: var(--muted); font-weight: 700; }
.sheet section { border: 0; padding: 0; }
.sheet section > label, .sheet section > .row.gap, .sheet section > #stats, .sheet section > .note {
  background: var(--bg);
  margin: 0;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
}
.sheet section > :nth-child(2) { border-radius: 12px 12px 0 0; }
.sheet section > :last-child { border-radius: 0 0 12px 12px; border-bottom: 0; }
.sheet section > :nth-child(2):last-child { border-radius: 12px; }
.sheet label { display: flex; align-items: center; justify-content: space-between; gap: 12px; font-size: 0.95rem; min-height: 44px; }
.sheet label.row { justify-content: flex-start; }
.sheet select {
  font: inherit;
  font-size: 0.95rem;
  color: var(--fg);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 6px 10px;
  max-width: 60%;
}
.sheet input[type="checkbox"] { width: 20px; height: 20px; accent-color: var(--accent); }
.row.gap { display: flex; gap: 10px; }
.row.gap button {
  flex: 1; font: inherit; font-weight: 600; padding: 10px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--card); color: var(--fg); cursor: pointer;
}
.row.gap button:active { transform: scale(0.98); }
.note { color: var(--muted); font-size: 0.85rem; margin: 6px 0 0; }
#stats { font-size: 0.9rem; color: var(--muted); line-height: 1.45; }
#stats .stat { display: flex; flex-direction: column; padding: 4px 0; }
#stats .stat + .stat { border-top: 1px solid var(--line); }
#stats .stat b { color: var(--fg); font-weight: 600; }

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

/* Larger boards: thinner lines and a touch more icon room. */
#board[data-n="11"], #board[data-n="12"] { --thin: 1px; --thick: 2px; border-width: 2px; }
#board[data-n="11"] .cell .icon svg, #board[data-n="12"] .cell .icon svg, #board[data-n="11"] .cell .icon img, #board[data-n="12"] .cell .icon img { width: 88%; height: 88%; }
#board[data-n="11"] .cell.mark .icon::after, #board[data-n="12"] .cell.mark .icon::after { width: 40%; height: 40%; }

/* Valley: the Stardew-inspired farm look. Pixel edges, chunky lines, a wooden
   frame around the board and parchment or night-sky backgrounds. */
html[data-palette="valley"] {
  --radius: 4px;
  --cell-radius: 0;
  --thick: 3px;
  --font: "Pixelify Sans", "Trebuchet MS", "Verdana", system-ui, sans-serif;
}
html[data-palette="valley"] body { font-size: 1.05rem; }
html[data-palette="valley"] #subtitle { font-size: 1.05rem; letter-spacing: 0.03em; }
html[data-palette="valley"] .bar-btn { font-size: 0.85rem; }
html[data-palette="valley"] .card h2 { font-size: 1.5rem; }
html[data-palette="valley"] #overlay-message { font-size: 1.2rem; }
html[data-palette="valley"] body {
  background-color: var(--bg);
  background-image:
    linear-gradient(rgba(255, 255, 255, 0.04) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255, 255, 255, 0.04) 1px, transparent 1px);
  background-size: 8px 8px;
}
html[data-palette="valley"]:is([data-theme="light"], [data-theme="auto"]) body {
  --bg: #efe2c0;
  --card: #f8efd8;
  --board-thick: #5a3a1e;
  --board-line: rgba(90, 58, 30, 0.28);
  --accent: #b8502e;
  --fg: #3b2a18;
  --muted: #7d6446;
}
@media (prefers-color-scheme: dark) {
  html[data-palette="valley"][data-theme="auto"] body {
    --bg: #1b1f2e;
    --card: #262b3d;
    --board-thick: #d9c9a3;
    --board-line: rgba(217, 201, 163, 0.25);
    --accent: #e39a4a;
    --fg: #f0e8d6;
    --muted: #a99f8a;
  }
}
html[data-palette="valley"][data-theme="dark"] body {
  --bg: #1b1f2e;
  --card: #262b3d;
  --board-thick: #d9c9a3;
  --board-line: rgba(217, 201, 163, 0.25);
  --accent: #e39a4a;
  --fg: #f0e8d6;
  --muted: #a99f8a;
}
html[data-palette="valley"] #board {
  border-radius: 0;
  border-width: 4px;
  box-shadow:
    0 0 0 4px #8a5a2b,
    0 0 0 7px #5a3a1e,
    0 10px 24px rgba(0, 0, 0, 0.25);
  image-rendering: pixelated;
}
html[data-palette="valley"] .cell .icon svg { image-rendering: pixelated; width: 80%; height: 80%; }
html[data-palette="valley"] .cell.mark .icon::after {
  background:
    linear-gradient(45deg, transparent 40%, var(--mark) 40%, var(--mark) 60%, transparent 60%),
    linear-gradient(-45deg, transparent 40%, var(--mark) 40%, var(--mark) 60%, transparent 60%);
}
html[data-palette="valley"] #bar, html[data-palette="valley"] .card, html[data-palette="valley"] .sheet select {
  border-radius: 4px;
  border-width: 2px;
  box-shadow: 3px 3px 0 rgba(0, 0, 0, 0.18);
}
html[data-palette="valley"] .bar-btn { border-radius: 2px; }
html[data-palette="valley"] .sheet section > label, html[data-palette="valley"] .sheet section > .row.gap, html[data-palette="valley"] .sheet section > #stats, html[data-palette="valley"] .sheet section > .note { border-radius: 0 !important; }
html[data-palette="valley"] button.primary { border-radius: 4px; box-shadow: 3px 3px 0 rgba(0, 0, 0, 0.25); }
html[data-palette="valley"] #overlay-art svg { animation: hop 0.9s steps(2, end) infinite; }
@keyframes hop { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-10px); } }

/* Win confetti: a few falling pieces, cheap and brief. */
.confetti {
  position: fixed;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
  z-index: 9;
}
.confetti i {
  position: absolute;
  top: -12px;
  width: 8px;
  height: 12px;
  border-radius: 2px;
  opacity: 0.9;
  animation: fall 1.8s ease-in forwards;
}
html[data-palette="valley"] .confetti i { border-radius: 0; }
@keyframes fall {
  from { transform: translateY(0) rotate(0deg); }
  to { transform: translateY(105vh) rotate(540deg); opacity: 0.4; }
}

/* Advanced mode: the icon sways from its base. Outer animations (pop, bounce,
   hop) stay on the svg, so the sway lives on the inner group. */
.adv .wobble { transform-box: fill-box; transform-origin: 50% 92%; animation: sway 2.4s ease-in-out infinite; }
@keyframes sway { 0%, 100% { transform: rotate(-7deg); } 50% { transform: rotate(7deg); } }
html[data-palette="valley"] .cell .icon svg.adv { image-rendering: auto; }

/* Feedback card: shown once every ten finished games. */
#feedback-card p { color: var(--fg); }
#feedback-card .stars { font-size: 1.6rem; letter-spacing: 0.08em; color: var(--hint); margin: 2px 0 6px; }
#feedback-card button.link { display: block; width: 100%; }
