/* ---------- Tokens ---------- */
:root {
  --text-xs: clamp(0.75rem, 0.7rem + 0.25vw, 0.875rem);
  --text-sm: clamp(0.875rem, 0.8rem + 0.35vw, 1rem);
  --text-base: clamp(1rem, 0.95rem + 0.25vw, 1.125rem);
  --text-lg: clamp(1.125rem, 1rem + 0.75vw, 1.5rem);
  --text-xl: clamp(1.5rem, 1.2rem + 1.25vw, 2.25rem);
  --space-1: 0.25rem; --space-2: 0.5rem; --space-3: 0.75rem; --space-4: 1rem;
  --space-5: 1.25rem; --space-6: 1.5rem; --space-8: 2rem; --space-10: 2.5rem;
  --radius-md: 0.75rem; --radius-lg: 1.1rem; --radius-xl: 1.6rem; --radius-full: 9999px;
  --transition-interactive: 180ms cubic-bezier(0.16, 1, 0.3, 1);
  --font-display: 'Chillax', 'Nunito', sans-serif;
  --font-body: 'Nunito', 'Trebuchet MS', sans-serif;
}

:root, [data-theme='light'] {
  --color-bg: #fdf3e3;
  --color-surface: #fffaf0;
  --color-border: #ecd9bf;
  --color-text: #4a3728;
  --color-text-muted: #99806a;
  --color-primary: #e8746f;
  --color-primary-hover: #d95f5a;
  --color-primary-soft: #fbdcd3;
  --board-line: #d9bfa0;
  --board-bg: #fffaf0;
  --paw: #8a6f58;
  --sparkle: #e8a03c;
  --conflict: #e05b5b;
  --shadow-md: 0 6px 18px rgba(120, 80, 40, 0.12);
  --shadow-lg: 0 16px 44px rgba(120, 80, 40, 0.18);
  --r1: #ffd9c9; --r2: #cdebd8; --r3: #cfe4f7; --r4: #e7d9f5;
  --r5: #fbeec3; --r6: #f9d3e0; --r7: #d2eee9;
}
[data-theme='dark'] {
  --color-bg: #211d2b;
  --color-surface: #2b2637;
  --color-border: #443c55;
  --color-text: #f0e8dc;
  --color-text-muted: #a396b4;
  --color-primary: #f28d88;
  --color-primary-hover: #f6a5a1;
  --color-primary-soft: #4d3341;
  --board-line: #55496b;
  --board-bg: #2b2637;
  --paw: #baa88f;
  --sparkle: #f2c14e;
  --conflict: #ff8080;
  --shadow-md: 0 6px 18px rgba(0, 0, 0, 0.35);
  --shadow-lg: 0 16px 44px rgba(0, 0, 0, 0.5);
  --r1: #6b4a3d; --r2: #3d5c4a; --r3: #3c5069; --r4: #544768;
  --r5: #6b5d38; --r6: #63404f; --r7: #3a5c56;
}

/* ---------- Base ---------- */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { -webkit-font-smoothing: antialiased; text-rendering: optimizeLegibility; }
body {
  min-height: 100dvh;
  font-family: var(--font-body);
  font-size: var(--text-base);
  line-height: 1.55;
  color: var(--color-text);
  background:
    radial-gradient(1100px 500px at 15% -8%, color-mix(in oklab, var(--r6) 32%, transparent), transparent 60%),
    radial-gradient(900px 480px at 90% 0%, color-mix(in oklab, var(--r3) 30%, transparent), transparent 55%),
    var(--color-bg);
  transition: background 300ms ease, color 300ms ease;
}
button { cursor: pointer; background: none; border: none; font: inherit; color: inherit; }
button, input { transition: color var(--transition-interactive), background var(--transition-interactive),
  border-color var(--transition-interactive), box-shadow var(--transition-interactive), transform var(--transition-interactive); }
:focus-visible { outline: 2px solid var(--color-primary); outline-offset: 3px; border-radius: var(--radius-md); }
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; transition-duration: 0.01ms !important; }
}

/* ---------- Header ---------- */
.top {
  display: flex; align-items: center; justify-content: space-between; gap: var(--space-3);
  padding: var(--space-4) clamp(var(--space-4), 4vw, var(--space-10));
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: var(--space-3); color: var(--color-primary); }
.logo { width: 40px; height: 40px; }
.title { font-family: var(--font-display); font-weight: 600; font-size: var(--text-xl); color: var(--color-text); letter-spacing: 0.01em; }
.hud { display: flex; align-items: center; gap: var(--space-2); flex-wrap: wrap; }
.hud-chip {
  font-size: var(--text-sm); font-weight: 700; font-variant-numeric: tabular-nums;
  background: var(--color-surface); border: 1.5px solid var(--color-border);
  border-radius: var(--radius-full); padding: var(--space-1) var(--space-3);
}
.icon-btn {
  width: 38px; height: 38px; display: grid; place-items: center;
  font-size: var(--text-sm); font-weight: 800;
  background: var(--color-surface); border: 1.5px solid var(--color-border); border-radius: var(--radius-full);
}
.icon-btn:hover { transform: translateY(-2px); box-shadow: var(--shadow-md); }
.icon-btn.off { opacity: 0.45; filter: grayscale(1); }

/* ---------- Stage ---------- */
.stage { display: flex; flex-direction: column; align-items: center; gap: var(--space-4); padding: 0 var(--space-4) var(--space-10); }
.whisper { color: var(--color-text-muted); font-size: var(--text-sm); font-weight: 600; text-align: center; min-height: 1.5em; }

.board-wrap {
  background: var(--board-bg);
  border: 2px solid var(--board-line);
  border-radius: var(--radius-xl);
  padding: clamp(6px, 1.4vw, 10px);
  box-shadow: var(--shadow-lg);
}
.board {
  display: grid;
  grid-auto-rows: 1fr;
  width: min(92vw, 500px);
  aspect-ratio: 1;
  border-radius: calc(var(--radius-xl) - 6px);
  overflow: hidden;
  touch-action: manipulation;
}
.cell {
  position: relative;
  border: 1px solid color-mix(in oklab, var(--board-line) 45%, transparent);
  display: grid; place-items: center;
  padding: 6%;
  -webkit-tap-highlight-color: transparent;
}
.cell:hover { filter: brightness(1.05); }
.cell svg { width: 100%; height: 100%; display: block; }
.cell .paw-mark { width: 46%; height: 46%; color: var(--paw); opacity: 0.75; animation: pop-in 140ms ease-out; }
.cell .paw-mark.ghost { opacity: 0.34; }
.cell .critter { animation: tuck-in 180ms ease-out; transform-origin: 50% 85%; }
.cell.bob .critter { animation: tuck-in 180ms ease-out, bob 3.2s ease-in-out 1s infinite; }
.cell.conflict { box-shadow: inset 0 0 0 3.5px var(--conflict); }
.cell.conflict::after {
  content: ''; position: absolute; inset: 0;
  background: color-mix(in oklab, var(--conflict) 16%, transparent);
  pointer-events: none;
}
.cell.conflict .critter { animation: wiggle 450ms ease-in-out infinite; }
.cell.sparkle { box-shadow: inset 0 0 0 3px var(--sparkle); animation: glow 900ms ease-in-out infinite alternate; }
.cell.asleep { cursor: pointer; }
.zzz {
  position: absolute; top: 2%; right: 4%;
  font-family: var(--font-display); font-weight: 700; font-size: clamp(10px, 2.4vw, 15px);
  color: var(--color-text-muted); pointer-events: none;
  animation: floatz 2.4s ease-in-out infinite;
}

@keyframes pop-in { from { transform: scale(0.8); opacity: 0; } to { transform: scale(1); opacity: 1; } }
@keyframes tuck-in { 0% { transform: scale(0.82) translateY(8%); opacity: 0.6; } 100% { transform: scale(1) translateY(0); opacity: 1; } }
@keyframes bob { 0%, 100% { transform: translateY(0) rotate(0deg); } 50% { transform: translateY(-3%) rotate(-2deg); } }
@keyframes wiggle { 0%, 100% { transform: rotate(-6deg); } 50% { transform: rotate(6deg); } }
@keyframes glow { from { filter: brightness(1); } to { filter: brightness(1.14); } }
@keyframes floatz { 0% { transform: translateY(0); opacity: 0.4; } 50% { transform: translateY(-30%); opacity: 1; } 100% { transform: translateY(-60%); opacity: 0; } }

/* ---------- Toolbar ---------- */
.toolbar { display: flex; flex-direction: column; align-items: center; gap: var(--space-3); width: min(92vw, 520px); }
.seg { display: flex; background: var(--color-surface); border: 1.5px solid var(--color-border); border-radius: var(--radius-full); padding: 3px; gap: 3px; }
.seg button { font-size: var(--text-sm); font-weight: 700; padding: var(--space-1) var(--space-4); border-radius: var(--radius-full); color: var(--color-text-muted); }
.seg button.on { background: var(--color-primary); color: #fff; }
.seg button:not(.on):hover { background: var(--color-primary-soft); color: var(--color-text); }
.row2 { display: flex; align-items: center; gap: var(--space-4); flex-wrap: wrap; justify-content: center; }
.critters { display: flex; gap: var(--space-2); }
.critters button {
  width: 52px; height: 52px; padding: 7px;
  background: var(--color-surface); border: 2px solid var(--color-border); border-radius: var(--radius-lg);
}
.critters.big button { width: 74px; height: 74px; padding: 10px; }
.critters button.on { border-color: var(--color-primary); background: var(--color-primary-soft); transform: translateY(-2px); box-shadow: var(--shadow-md); }
.critters button:hover { transform: translateY(-2px) rotate(-3deg); }
.critters svg { width: 100%; height: 100%; }
.helper-toggle { display: flex; align-items: center; gap: var(--space-2); font-size: var(--text-sm); font-weight: 700; color: var(--color-text-muted); cursor: pointer; user-select: none; }
.helper-toggle input { accent-color: var(--color-primary); width: 18px; height: 18px; cursor: pointer; }
.actions { display: flex; gap: var(--space-2); flex-wrap: wrap; justify-content: center; }
.btn {
  font-weight: 800; font-size: var(--text-sm);
  padding: var(--space-2) var(--space-5); border-radius: var(--radius-full);
}
.btn-main { background: var(--color-primary); color: #fff; box-shadow: var(--shadow-md); }
.btn-main:hover { background: var(--color-primary-hover); transform: translateY(-2px); }
.btn-soft { background: var(--color-surface); border: 1.5px solid var(--color-border); }
.btn-soft:hover { background: var(--color-primary-soft); transform: translateY(-2px); }
.btn.big { font-size: var(--text-base); padding: var(--space-3) var(--space-8); }
.btn:disabled { opacity: 0.4; cursor: default; transform: none; }

/* ---------- Modals ---------- */
.modal-veil {
  position: fixed; inset: 0; z-index: 50;
  background: color-mix(in oklab, var(--color-bg) 55%, rgba(30, 15, 40, 0.45));
  backdrop-filter: blur(6px);
  display: grid; place-items: center; padding: var(--space-4);
  animation: veil-in 260ms ease;
}
.modal-veil.hidden { display: none; }
.modal {
  background: var(--color-surface); border: 2px solid var(--color-border);
  border-radius: var(--radius-xl); box-shadow: var(--shadow-lg);
  padding: var(--space-8) var(--space-6); max-width: 430px; width: 100%;
  text-align: center; display: flex; flex-direction: column; gap: var(--space-4); align-items: center;
  animation: modal-in 320ms cubic-bezier(0.34, 1.56, 0.64, 1);
  max-height: 92dvh; overflow: auto;
}
.modal h2 { font-family: var(--font-display); font-weight: 600; font-size: var(--text-xl); }
.modal-sub { color: var(--color-text-muted); font-weight: 600; font-size: var(--text-sm); }
.rules { list-style: none; text-align: left; display: flex; flex-direction: column; gap: var(--space-2); font-size: var(--text-sm); }
.rules li { padding-left: var(--space-5); position: relative; }
.rules li::before { content: '🐾'; position: absolute; left: 0; font-size: 0.8em; top: 0.2em; }
.pick-label { font-size: var(--text-sm); font-weight: 800; }
.win-art { display: flex; gap: var(--space-2); justify-content: center; }
.win-art svg { width: 64px; height: 64px; animation: bob 3s ease-in-out infinite; }
.win-art svg:nth-child(2) { animation-delay: 0.4s; }
.win-art svg:nth-child(3) { animation-delay: 0.8s; }
.pet-hint { font-size: var(--text-xs); color: var(--color-text-muted); font-weight: 700; }
.win-actions { display: flex; gap: var(--space-2); flex-wrap: wrap; justify-content: center; }
.btn.spent { opacity: 0.45; filter: saturate(0.5); }
.tut { max-width: 460px; }
.tut-step { display: none; flex-direction: column; gap: var(--space-3); align-items: center; }
.tut-step.on { display: flex; animation: tut-in 240ms ease; }
.tut-step svg { max-width: 100%; height: auto; }
.tut-text { font-size: var(--text-sm); font-weight: 600; max-width: 36ch; }
.tut-text b { color: var(--color-primary); }
.tut-hearts { font-size: 2rem; letter-spacing: 4px; }
.tut-dots { display: flex; gap: 8px; }
.tut-dots .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--color-border); transition: transform 160ms ease, background 160ms ease; }
.tut-dots .dot.on { background: var(--color-primary); transform: scale(1.3); }
.tut-nav { display: flex; gap: var(--space-2); justify-content: center; }
@keyframes tut-in { from { opacity: 0; transform: translateX(10px); } to { opacity: 1; transform: none; } }
@keyframes veil-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes modal-in { from { transform: scale(0.85) translateY(16px); opacity: 0; } to { transform: scale(1); opacity: 1; } }

/* ---------- Toasts ---------- */
.toasts { position: fixed; bottom: var(--space-6); left: 50%; transform: translateX(-50%); z-index: 60; display: flex; flex-direction: column; gap: var(--space-2); align-items: center; pointer-events: none; }
.toast {
  background: var(--color-surface); border: 1.5px solid var(--color-border); border-radius: var(--radius-full);
  padding: var(--space-2) var(--space-5); font-size: var(--text-sm); font-weight: 800;
  box-shadow: var(--shadow-md); animation: toast-in 300ms cubic-bezier(0.34, 1.56, 0.64, 1), toast-out 300ms ease 2.2s forwards;
}
@keyframes toast-in { from { transform: translateY(20px) scale(0.8); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes toast-out { to { transform: translateY(10px); opacity: 0; } }

/* ---------- Hearts / confetti ---------- */
.heart {
  position: fixed; z-index: 70; pointer-events: none; font-size: 18px;
  animation: heart-float 1.3s ease-out forwards;
}
@keyframes heart-float {
  0% { transform: translate(0, 0) scale(0.5) rotate(0deg); opacity: 1; }
  100% { transform: translate(var(--dx), var(--dy)) scale(1.15) rotate(var(--rot)); opacity: 0; }
}
.confetto {
  position: fixed; z-index: 45; pointer-events: none; top: -30px; font-size: 20px;
  animation: confetto-fall var(--dur) linear forwards;
}
@keyframes confetto-fall {
  0% { transform: translateY(0) rotate(0deg); opacity: 1; }
  90% { opacity: 1; }
  100% { transform: translateY(110vh) rotate(var(--rot)); opacity: 0; }
}

/* ---------- FPS badge ---------- */
.fps {
  position: fixed; left: 8px; bottom: 8px; z-index: 80;
  font: 700 10px/1.6 monospace; color: var(--color-text-muted);
  background: color-mix(in oklab, var(--color-surface) 80%, transparent);
  border-radius: 6px; padding: 0 6px; pointer-events: none; opacity: 0.75;
}

@media (max-width: 560px) {
  .top { justify-content: center; }
  .title { font-size: var(--text-lg); }
  .critters button { width: 46px; height: 46px; }
}
