:root {
  --control: 56px;
  --pad-button: 64px;
  --pad-gap: 12px;
  --ink: #30254d;
  --paper: #fffaf0;
  --plum: #7452b8;
  --star: #ffd84d;
  --star-edge: #a86100;
  --shadow: #39295e;
  --chrome-fill: #fffaf0;
  --accent: #ffcf4a;
  --ease: cubic-bezier(.2, .9, .25, 1.2);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  color-scheme: light;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { width: 100%; height: 100%; margin: 0; overflow: hidden; overscroll-behavior: none; }
body {
  background: #8fd3a8;
  color: var(--ink);
  font-family: ui-rounded, "SF Pro Rounded", "Arial Rounded MT Bold", system-ui, sans-serif;
  font-weight: 850;
  -webkit-user-select: none;
  user-select: none;
}
button, a { font: inherit; }

#game-shell {
  position: relative;
  width: 100%;
  height: 100dvh;
  min-height: 100%;
  overflow: hidden;
  isolation: isolate;
  background:
    radial-gradient(circle at 18% 12%, rgba(255, 244, 214, .95), rgba(255, 244, 214, 0) 42%),
    linear-gradient(180deg, #c4ecd2 0%, #9bd9b2 55%, #7cc99c 100%);
  transition: background 400ms var(--ease);
}
#game-shell[data-world="pond"] {
  --chrome-fill: #f6fbff;
  --accent: #ffd66b;
  background:
    radial-gradient(circle at 20% 10%, rgba(255, 246, 220, .95), rgba(255, 246, 220, 0) 40%),
    linear-gradient(180deg, #c8ecf6 0%, #98d4e8 55%, #74bfd8 100%);
}
#game-shell[data-world="moon"] {
  --chrome-fill: #f7f3ff;
  --accent: #ffe46b;
  background:
    radial-gradient(circle at 12% 18%, #fff6d8 0 1.5px, transparent 2.5px),
    radial-gradient(circle at 31% 8%, #fff6d8 0 1px, transparent 2px),
    radial-gradient(circle at 47% 22%, #ffffff 0 1.5px, transparent 2.5px),
    radial-gradient(circle at 66% 6%, #fff6d8 0 1px, transparent 2px),
    radial-gradient(circle at 88% 30%, #ffffff 0 2px, transparent 3px),
    radial-gradient(circle at 6% 52%, #fff6d8 0 1px, transparent 2px),
    radial-gradient(circle at 94% 64%, #fff6d8 0 1.5px, transparent 2.5px),
    radial-gradient(circle at 22% 88%, #ffffff 0 1px, transparent 2px),
    radial-gradient(circle at 78% 92%, #fff6d8 0 1.5px, transparent 2.5px),
    radial-gradient(circle at 80% 14%, rgba(255, 240, 200, .32), rgba(255, 240, 200, 0) 26%),
    linear-gradient(180deg, #2a2860 0%, #3b3478 55%, #4b3f8c 100%);
}

#game-canvas { position: absolute; inset: 0; display: block; width: 100%; height: 100%; touch-action: none; cursor: pointer; }
#trail-layout { position: absolute; left: 16px; top: 120px; width: 10px; height: 10px; pointer-events: none; }

/* HUD: platform home slot on the left, task panel in the centre, sound and fullscreen on the right. */
.hud {
  position: absolute;
  z-index: 20;
  top: 0;
  left: 0;
  right: 0;
  display: grid;
  grid-template-columns: minmax(86px, 1fr) minmax(250px, 460px) minmax(86px, 1fr);
  grid-template-areas: "home task actions";
  align-items: start;
  gap: 12px;
  padding: max(10px, var(--safe-top)) max(12px, var(--safe-right)) 0 max(10px, var(--safe-left));
  pointer-events: none;
}
.hud > * { pointer-events: auto; }
.hud > .home-button, .hud > .tokenville-home-slot { grid-area: home; justify-self: start; }
.home-button svg { width: 24px; height: 24px; fill: currentColor; }

.task-panel {
  grid-area: task;
  justify-self: center;
  display: grid;
  justify-items: center;
  gap: 4px;
  width: 100%;
  min-height: 56px;
  padding: 6px 16px 8px;
  border: 3px solid var(--ink);
  border-radius: 22px;
  background: var(--paper);
  box-shadow: 0 5px 0 var(--shadow), 0 10px 22px rgba(31, 24, 51, .16);
  text-align: center;
}
.task-title { font-size: 16px; font-weight: 900; line-height: 1.1; color: var(--plum); }
.task-head { display: inline-flex; align-items: center; gap: 8px; }
/* MIX only: which language this trail speaks. A drawn pill with letters, never a flag. */
.lang-badge {
  display: inline-grid;
  place-items: center;
  min-width: 34px;
  height: 22px;
  padding: 0 7px;
  border: 2.5px solid var(--ink);
  border-radius: 999px;
  background: var(--accent);
  color: var(--ink);
  font-size: 12px;
  font-weight: 950;
  letter-spacing: .04em;
  line-height: 1;
}

/* Word card: the name of the thing the child just met, in the trail language. */
.word-pop {
  position: absolute;
  z-index: 18;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  max-width: 160px;
  padding: 6px 14px;
  border: 3px solid var(--ink);
  border-radius: 18px;
  background: var(--paper);
  color: var(--ink);
  box-shadow: 0 4px 0 var(--shadow), 0 8px 16px rgba(31, 24, 51, .16);
  font-size: 22px;
  font-weight: 950;
  line-height: 1.1;
  white-space: nowrap;
  pointer-events: none;
  transform: translate(-50%, -100%);
}
.word-pop b { display: grid; place-items: center; min-width: 30px; height: 30px; border-radius: 50%; background: var(--star); border: 2.5px solid var(--ink); font-size: 18px; }
.word-pop.pop { animation: word-pop 2200ms var(--ease) both; }
@keyframes word-pop {
  0% { opacity: 0; transform: translate(-50%, -80%) scale(.6); }
  12% { opacity: 1; transform: translate(-50%, -100%) scale(1.06); }
  20%, 82% { opacity: 1; transform: translate(-50%, -100%) scale(1); }
  100% { opacity: 0; transform: translate(-50%, -120%) scale(1); }
}
.task-line { display: flex; align-items: center; justify-content: center; gap: 8px; margin: 0; min-width: 0; }
.task-line strong { font-size: 20px; font-weight: 950; line-height: 1.15; }
.task-icon { flex: none; display: grid; place-items: center; width: 34px; height: 34px; border-radius: 12px; background: var(--accent); border: 2.5px solid var(--ink); }
.task-icon svg { width: 24px; height: 24px; fill: none; stroke: var(--ink); stroke-width: 2.1; stroke-linecap: round; stroke-linejoin: round; }
.task-icon svg .fill { fill: #fff6d8; }
.task-progress { display: flex; align-items: center; justify-content: center; gap: 14px; min-height: 20px; }
.dots { display: flex; align-items: center; gap: 6px; margin: 0; padding: 0; list-style: none; }
.dots li {
  display: grid;
  place-items: center;
  width: 18px;
  height: 18px;
  border: 2.5px solid var(--ink);
  border-radius: 50%;
  background: #fff;
  transition: transform 250ms var(--ease), background 200ms ease;
}
.dots li svg { width: 12px; height: 12px; fill: none; stroke: var(--ink); stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; opacity: 0; }
.dots li.done { background: var(--star); }
.dots li.done svg { opacity: 1; }
.dots li.current { box-shadow: 0 0 0 3px var(--plum); }
.treat-dots li { border-radius: 40%; }
.treat-dots li.done { background: var(--treat, #ffd84d); transform: scale(1.12); }
.treat-dots:empty { display: none; }

.hud-actions { grid-area: actions; justify-self: end; display: flex; gap: 10px; }
.square-button {
  display: grid;
  place-items: center;
  width: var(--control);
  height: var(--control);
  min-width: var(--control);
  padding: 0;
  border: 3px solid var(--ink);
  border-radius: 18px;
  background: var(--chrome-fill);
  color: var(--ink);
  box-shadow: 0 5px 0 var(--shadow), 0 8px 18px rgba(31, 24, 51, .18);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.square-button svg { width: 26px; height: 26px; fill: none; stroke: currentColor; stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; }
.sound-off { display: none; }
#sound-button[aria-pressed="false"] .sound-on { display: none; }
#sound-button[aria-pressed="false"] .sound-off { display: block; }

/* Game controls: right column on landscape, bottom band on portrait. */
.controls {
  position: absolute;
  z-index: 22;
  right: max(16px, var(--safe-right));
  bottom: max(16px, var(--safe-bottom));
  display: grid;
  justify-items: center;
  gap: 14px;
}
.hint-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-width: 120px;
  height: 64px;
  padding: 0 18px 0 14px;
  border: 3px solid var(--ink);
  border-radius: 22px;
  background: var(--accent);
  color: var(--ink);
  box-shadow: 0 5px 0 var(--shadow), 0 8px 18px rgba(31, 24, 51, .18);
  font-size: 20px;
  font-weight: 950;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.hint-button svg { width: 30px; height: 30px; fill: none; stroke: currentColor; stroke-width: 2.3; stroke-linecap: round; stroke-linejoin: round; }

.dpad {
  display: grid;
  grid-template-columns: repeat(3, var(--pad-button));
  grid-template-rows: repeat(3, var(--pad-button));
  gap: var(--pad-gap);
  padding: 10px;
  border: 3px solid rgba(255, 255, 255, .85);
  border-radius: 36px;
  background: rgba(48, 37, 77, .22);
  box-shadow: 0 10px 24px rgba(31, 24, 51, .18);
  touch-action: none;
}
.dpad button {
  display: grid;
  place-items: center;
  width: var(--pad-button);
  height: var(--pad-button);
  padding: 0;
  border: 3px solid var(--ink);
  border-radius: 20px;
  background: var(--paper);
  color: var(--ink);
  box-shadow: 0 5px 0 var(--shadow);
  cursor: pointer;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
}
.dpad button svg { width: 32px; height: 32px; fill: none; stroke: currentColor; stroke-width: 3.2; stroke-linecap: round; stroke-linejoin: round; }
.dpad-up { grid-column: 2; grid-row: 1; }
.dpad-left { grid-column: 1; grid-row: 2; }
.dpad-center { grid-column: 2; grid-row: 2; display: grid; place-items: center; color: rgba(255, 255, 255, .92); }
.dpad-center svg { width: 34px; height: 34px; }
.dpad-center .paw { fill: currentColor; stroke: none; }
.dpad-right { grid-column: 3; grid-row: 2; }
.dpad-down { grid-column: 2; grid-row: 3; }
.dpad button.hint { background: var(--star); animation: hint-pulse 700ms var(--ease) 3; }
.dpad button.hint-strong { background: var(--star); animation: hint-pulse 900ms var(--ease) infinite; }
.hint-button.glow { animation: hint-pulse 700ms var(--ease) 2; }

/* Press grammar: 0.94 in 80 ms, release with a 1.04 overshoot. */
.square-button, .dpad button, .hint-button, .completion-actions > * {
  transition: transform 220ms var(--ease), box-shadow 120ms ease;
}
.square-button:active, .dpad button:active, .dpad button.pressed, .hint-button:active, .completion-actions > *:active {
  transform: scale(.94) translateY(2px);
  transition-duration: 80ms;
  box-shadow: 0 2px 0 var(--shadow);
}
.square-button:focus-visible, .dpad button:focus-visible, .hint-button:focus-visible, .completion-actions > *:focus-visible, #game-canvas:focus-visible {
  outline: 4px solid #fff;
  outline-offset: 3px;
  box-shadow: 0 0 0 7px var(--plum);
}

@keyframes hint-pulse { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.1); } }
@keyframes card-in { from { opacity: 0; transform: translateY(16px) scale(.96); } to { opacity: 1; transform: none; } }
@keyframes star-pop { 0% { transform: scale(.4) rotate(-12deg); } 70% { transform: scale(1.08) rotate(3deg); } 100% { transform: scale(1) rotate(0); } }

/* Completion: platform grammar. Right column on landscape (beside the board), bottom sheet on portrait. */
.completion-card {
  position: absolute;
  z-index: 30;
  right: max(16px, var(--safe-right));
  top: 50%;
  display: grid;
  justify-items: center;
  gap: 10px;
  width: 240px;
  max-height: calc(100% - 24px);
  padding: 16px 18px 18px;
  border: 3px solid var(--ink);
  border-radius: 28px;
  background: var(--paper);
  box-shadow: 0 7px 0 var(--shadow), 0 18px 40px rgba(31, 24, 51, .22);
  translate: 0 -50%;
  text-align: center;
}
.completion-card:not([hidden]) { animation: card-in 320ms var(--ease) both; }
.completion-card .big-star { width: 96px; height: 96px; fill: var(--star); stroke: var(--star-edge); stroke-width: 1.4; stroke-linejoin: round; animation: star-pop 400ms var(--ease) both; }
.completion-card strong { font-size: 24px; font-weight: 950; line-height: 1.1; }
.completion-actions { display: grid; gap: 12px; width: 100%; }
.completion-actions > * {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  min-height: 64px;
  padding: 0 16px;
  border: 3px solid var(--ink);
  border-radius: 22px;
  background: var(--paper);
  color: var(--ink);
  box-shadow: 0 5px 0 var(--shadow);
  font-size: 20px;
  font-weight: 950;
  text-decoration: none;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.completion-actions > * svg { width: 26px; height: 26px; flex: none; fill: none; stroke: currentColor; stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; }
#next-button { background: var(--star); }

#webgl-error {
  position: absolute;
  z-index: 40;
  left: 50%;
  top: 50%;
  width: min(360px, calc(100% - 32px));
  padding: 16px 20px;
  border: 3px solid var(--ink);
  border-radius: 22px;
  background: var(--paper);
  font-size: 20px;
  font-weight: 950;
  text-align: center;
  transform: translate(-50%, -50%);
}

#game-shell.celebrating .controls { visibility: hidden; }

/* The platform star toast sits over the task panel, never over the board or the card. */
body [data-tokenville-star-toast] {
  top: max(10px, var(--safe-top)) !important;
  bottom: auto !important;
}

@media (orientation: portrait), (max-width: 700px) {
  .controls {
    left: max(16px, var(--safe-left));
    grid-template-columns: auto auto;
    align-items: end;
    justify-content: space-between;
  }
  .completion-card {
    left: max(12px, var(--safe-left));
    right: max(12px, var(--safe-right));
    top: auto;
    bottom: max(12px, var(--safe-bottom));
    width: auto;
    max-width: 640px;
    margin: 0 auto;
    grid-template-columns: auto 1fr;
    align-items: center;
    translate: none;
    text-align: left;
  }
  .completion-card .big-star { width: 72px; height: 72px; }
  .completion-actions { grid-column: 1 / -1; grid-template-columns: repeat(auto-fit, minmax(0, 1fr)); }
}

@media (max-width: 700px) {
  :root { --control: 48px; --pad-button: 56px; }
  .hud {
    grid-template-columns: 1fr auto;
    grid-template-areas: "home actions" "task task";
    row-gap: 10px;
    padding-left: max(10px, var(--safe-left));
    padding-right: max(12px, var(--safe-right));
  }
  .hud-actions { gap: 8px; }
  /* Keep the platform corner (home badge and language toggle) free. */
  .task-panel { margin-left: calc(var(--tokenville-corner-right, 86px) - max(10px, var(--safe-left)) + 6px); width: auto; padding: 5px 10px 7px; border-radius: 20px; }
  .task-title { font-size: 16px; }
  .task-line strong { font-size: 18px; }
  .task-icon { width: 30px; height: 30px; border-radius: 10px; }
  .dpad { padding: 8px; border-radius: 30px; }
  .dpad button { border-radius: 18px; }
  .controls { gap: 12px; }
  .hint-button { min-width: 64px; flex-direction: column; gap: 0; height: 72px; padding: 4px 12px; font-size: 16px; }
  .completion-card { padding: 12px 14px 14px; gap: 8px; }
  .completion-card strong { font-size: 22px; }
  .completion-actions { gap: 8px; }
  .completion-actions > * { flex-direction: column; gap: 2px; padding: 4px 6px; font-size: 16px; }
  body [data-tokenville-star-toast] { top: calc(max(10px, var(--safe-top)) + 60px) !important; }
}

@media (max-height: 560px) and (orientation: landscape) {
  :root { --pad-button: 56px; }
  .dpad { padding: 6px; }
  .controls { gap: 8px; }
  .hint-button { height: 56px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation: none !important; transition: none !important; }
  .dpad button.hint, .dpad button.hint-strong { box-shadow: 0 0 0 5px var(--plum), 0 5px 0 var(--shadow); }
}
