:root {
  --sky-top: #bdeaff;
  --sky-bot: #fef6d8;
  --ink: #2b2440;
  --cream: #fff8ec;
  --grass: #6fcf57;
  --apple: #ff5d5d;
  --egg: #ffe6a8;
  --star: #ffce3a;
  --btn: #ff8a3d;
  --btn-dark: #e96a1d;
  --jump: #4cc9f0;
  --roar: #ff5d8f;
  --ui-font: "Trebuchet MS", "Avenir Next", "Segoe UI", system-ui, sans-serif;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #8fd3ff;
  font-family: var(--ui-font);
  color: var(--ink);
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  touch-action: none;
  overscroll-behavior: none;
}

#stage {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
}

#c {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
}

.hidden { display: none !important; }

/* ---------- HUD ---------- */
#hud {
  position: absolute;
  top: max(12px, env(safe-area-inset-top));
  left: max(12px, env(safe-area-inset-left));
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  /* keep the tally row clear of the pause + mute corner buttons on phones;
     overflow wraps to a second row instead of sliding under them */
  max-width: calc(100vw - 132px);
  pointer-events: none;
}
.tally {
  display: flex;
  align-items: center;
  gap: 6px;
  background: rgba(255, 255, 255, 0.82);
  border: 3px solid #fff;
  border-radius: 999px;
  padding: 4px 12px 4px 6px;
  font-weight: 800;
  font-size: clamp(15px, 2.4vw, 22px);
  box-shadow: 0 4px 0 rgba(43, 36, 64, 0.15);
}
.ico {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  display: inline-block;
}
.ico.apple { background: radial-gradient(circle at 35% 30%, #ff8f8f, var(--apple)); }
.ico.egg { background: radial-gradient(circle at 35% 30%, #fff6df, var(--egg)); border-radius: 50% 50% 48% 48%; }
.ico.star {
  background: var(--star);
  clip-path: polygon(50% 0, 61% 35%, 98% 35%, 68% 57%, 79% 91%, 50% 70%, 21% 91%, 32% 57%, 2% 35%, 39% 35%);
  border-radius: 0;
}

/* ---------- corner button ---------- */
.corner-btn {
  position: absolute;
  top: max(12px, env(safe-area-inset-top));
  right: max(12px, env(safe-area-inset-right));
  width: 50px;
  height: 50px;
  border: 3px solid #fff;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.82);
  font-size: 22px;
  cursor: pointer;
  box-shadow: 0 4px 0 rgba(43, 36, 64, 0.15);
}
.corner-btn:active { transform: translateY(2px); box-shadow: 0 2px 0 rgba(43, 36, 64, 0.15); }

/* ---------- screens ---------- */
.screen {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  overflow-y: auto;
  background: linear-gradient(180deg, rgba(189, 234, 255, 0.6), rgba(254, 246, 216, 0.6));
}
.card {
  width: min(92vw, 460px);
  max-height: calc(100% - 16px);
  background: var(--cream);
  border: 6px solid #fff;
  border-radius: 32px;
  padding: clamp(20px, 5vw, 40px);
  text-align: center;
  box-shadow: 0 14px 0 rgba(43, 36, 64, 0.12), 0 24px 50px rgba(43, 36, 64, 0.25);
}
.card h1 {
  font-size: clamp(30px, 7vw, 50px);
  line-height: 1.02;
  color: var(--ink);
  text-shadow: 0 3px 0 #ffd98a;
  letter-spacing: 0.5px;
}
.sub {
  margin: 14px 0 22px;
  font-size: clamp(15px, 3.4vw, 19px);
  color: #6a627d;
  font-weight: 600;
}
.big-btn {
  font-family: var(--ui-font);
  font-weight: 900;
  font-size: clamp(22px, 5vw, 30px);
  color: #fff;
  background: var(--btn);
  border: none;
  border-radius: 999px;
  padding: 16px 40px;
  cursor: pointer;
  box-shadow: 0 7px 0 var(--btn-dark);
  letter-spacing: 1px;
}
.big-btn:active { transform: translateY(4px); box-shadow: 0 3px 0 var(--btn-dark); }

.legend { margin-top: 24px; font-size: 14px; color: #6a627d; }
.legend .hint { font-weight: 700; color: #6a627d; margin-bottom: 10px; }
.legend .keys { display: flex; flex-wrap: wrap; gap: 10px 18px; justify-content: center; color: #6a627d; font-weight: 600; }
.legend .keys span { display: inline-flex; align-items: center; gap: 5px; }
kbd {
  display: inline-block;
  background: #fff;
  border: 2px solid #e3dccb;
  border-bottom-width: 4px;
  border-radius: 8px;
  padding: 2px 8px;
  font-family: var(--ui-font);
  font-weight: 800;
  font-size: 13px;
  color: var(--ink);
}

/* legend swaps to mobile control chips on touch devices */
.touch-only { display: none; }
body.is-touch .key-only { display: none; }
body.is-touch .touch-only { display: flex; }
.chip {
  display: inline-block;
  color: #fff;
  background: #4caf43;
  border-radius: 8px;
  padding: 3px 9px;
  font-weight: 900;
  font-size: 12px;
  letter-spacing: 0.5px;
}
.chip.jump { background: #18a0cf; }
.chip.roar { background: #e23a78; }

.results { display: flex; gap: 22px; justify-content: center; margin: 8px 0 26px; }
.res { display: flex; align-items: center; gap: 8px; font-size: clamp(22px, 5vw, 30px); font-weight: 900; }
.res .ico { width: 30px; height: 30px; }
.baby-line {
  margin: -8px 0 20px;
  font-size: clamp(15px, 3.6vw, 19px);
  font-weight: 800;
  color: #5aa84a;
}

/* ---------- touch controls ---------- */
#touch {
  position: absolute;
  inset: auto 0 auto 0;
  bottom: max(18px, env(safe-area-inset-bottom));
  display: flex;
  justify-content: space-between;
  padding: 0 max(12px, env(safe-area-inset-right)) 0 max(12px, env(safe-area-inset-left));
  pointer-events: none;
}
.pad-left { display: flex; gap: clamp(10px, 3vw, 20px); align-items: flex-end; pointer-events: none; }
.pad-right { display: flex; gap: clamp(14px, 4vw, 26px); align-items: flex-end; pointer-events: none; }
.tbtn {
  pointer-events: auto;
  width: clamp(62px, 17vw, 100px);
  height: clamp(62px, 17vw, 100px);
  border-radius: 50%;
  border: 4px solid rgba(255, 255, 255, 0.95);
  color: #fff;
  font-family: var(--ui-font);
  font-weight: 900;
  font-size: clamp(16px, 3.6vw, 21px);
  box-shadow: 0 5px 0 rgba(43, 36, 64, 0.32);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  text-shadow: 0 2px 3px rgba(0, 0, 0, 0.35);
  touch-action: none;
}
/* move = green (Pip's colour) so it clearly reads as "walk the dino" */
.tbtn.move {
  font-size: clamp(30px, 7vw, 42px);
  background: #4caf43;
  box-shadow: 0 5px 0 #2f8a2c;
}
/* jump = big, bright, the primary action; gentle idle pulse draws the eye */
.tbtn.jump {
  background: var(--jump);
  box-shadow: 0 6px 0 #2f93bd;
  width: clamp(70px, 19.5vw, 116px);
  height: clamp(70px, 19.5vw, 116px);
  animation: jumpPulse 1.5s ease-in-out infinite;
}
.tbtn.roar { background: var(--roar); box-shadow: 0 5px 0 #d23a6e; }
.tbtn.is-down { transform: translateY(4px) scale(0.95); box-shadow: 0 1px 0 rgba(43, 36, 64, 0.32); animation: none; }

@keyframes jumpPulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.06); }
}

/* first-run "how to move" hint — sits clear above the tallest button */
#touchHint {
  position: absolute;
  left: 50%;
  bottom: max(158px, calc(env(safe-area-inset-bottom) + 158px));
  transform: translate(-50%, 8px);
  max-width: 92vw;
  text-align: center;
  background: rgba(43, 36, 64, 0.88);
  color: #fff;
  font-weight: 800;
  font-size: clamp(14px, 3.6vw, 17px);
  padding: 10px 18px;
  border-radius: 999px;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.4s ease, transform 0.4s ease;
}
#touchHint.show { opacity: 1; transform: translate(-50%, 0); }

/* transient toast (power-ups, baby rescue) */
#toast {
  position: absolute;
  left: 50%;
  top: max(74px, calc(env(safe-area-inset-top) + 74px));
  transform: translate(-50%, -8px);
  background: rgba(43, 36, 64, 0.9);
  color: #fff;
  font-weight: 900;
  font-size: clamp(16px, 4vw, 20px);
  padding: 9px 20px;
  border-radius: 999px;
  white-space: nowrap;
  pointer-events: none;
  z-index: 40;
  opacity: 0;
  transition: opacity 0.25s ease, transform 0.25s ease;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* short landscape: shrink controls so they don't swallow the screen */
@media (orientation: landscape) and (max-height: 470px) {
  .tbtn { width: clamp(60px, 15vh, 84px); height: clamp(60px, 15vh, 84px); }
  .tbtn.jump { width: clamp(68px, 18vh, 98px); height: clamp(68px, 18vh, 98px); }
  #touchHint { bottom: max(96px, calc(env(safe-area-inset-bottom) + 96px)); }
}

@media (prefers-reduced-motion: reduce) {
  .big-btn:active, .tbtn.is-down, .corner-btn:active { transition: none; }
  .tbtn.jump { animation: none; }
  #touchHint { transition: opacity 0.3s ease; }
}

/* ---------- forced landscape (iOS fallback) ----------
   iOS Safari can't lock orientation or fullscreen a div, so on those devices
   we rotate the whole stage into landscape with CSS. Only applies on touch
   devices that lack a real lock (body.force-landscape) AND only while the
   phone is physically portrait; in landscape this is a no-op and the game
   renders normally. Inputs are unaffected: the canvas tap is position-free
   and the buttons are DOM elements that rotate with the stage. */
@media (orientation: portrait) {
  body.force-landscape #stage {
    top: 0;
    left: 100vw;
    left: 100dvw;
    right: auto;
    bottom: auto;
    width: 100vh;
    width: 100dvh;
    height: 100vw;
    height: 100dvw;
    transform-origin: top left;
    transform: rotate(90deg);
  }
}

/* ---------- rotate-to-landscape nudge ---------- */
#rotateNudge {
  position: absolute;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: linear-gradient(180deg, #bdeaff, #fef6d8);
}
.rotate-card { text-align: center; max-width: 320px; }
.rotate-card h2 {
  margin: 20px 0 6px;
  font-size: clamp(22px, 6vw, 30px);
  color: var(--ink);
  text-shadow: 0 2px 0 #ffd98a;
}
.rotate-card p {
  font-size: clamp(15px, 4vw, 18px);
  color: #6a627d;
  font-weight: 600;
  margin-bottom: 24px;
}
.phone-rot { width: 110px; height: 110px; margin: 0 auto; display: flex; align-items: center; justify-content: center; }
.phone {
  width: 48px;
  height: 84px;
  border-radius: 13px;
  background: var(--jump);
  border: 4px solid var(--ink);
  box-shadow: inset 0 0 0 3px #fff, 0 6px 0 rgba(43, 36, 64, 0.18);
  display: block;
  transform-origin: 50% 50%;
  animation: phoneRotate 2.6s ease-in-out infinite;
}
@keyframes phoneRotate {
  0%, 18% { transform: rotate(0deg); }
  45%, 72% { transform: rotate(-90deg); }
  95%, 100% { transform: rotate(0deg); }
}
.small-btn {
  font-family: var(--ui-font);
  font-weight: 800;
  font-size: 16px;
  color: var(--ink);
  background: #fff;
  border: 3px solid #ffd98a;
  border-radius: 999px;
  padding: 11px 24px;
  cursor: pointer;
  box-shadow: 0 4px 0 rgba(43, 36, 64, 0.12);
}
.small-btn:active { transform: translateY(3px); box-shadow: 0 1px 0 rgba(43, 36, 64, 0.12); }

/* keyboard focus rings (the global reset hides default outlines) */
.big-btn:focus-visible,
.small-btn:focus-visible,
.corner-btn:focus-visible { outline: 4px solid var(--jump); outline-offset: 3px; }

@media (prefers-reduced-motion: reduce) {
  .phone { animation: none; transform: rotate(-90deg); }
}

/* ---------- level select map ---------- */
.map-card { width: min(94vw, 540px); }
.map {
  position: relative;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 6px;
  margin: 18px 0 22px;
  padding: 8px 6px 0;
}
.map::before {
  content: "";
  position: absolute;
  top: 42px;
  left: 14%;
  right: 14%;
  border-top: 5px dotted #ffd98a;
  z-index: 0;
}
.node {
  position: relative;
  z-index: 1;
  flex: 1 1 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  background: none;
  border: none;
  cursor: pointer;
  font-family: var(--ui-font);
  padding: 0 2px;
}
.node-dot {
  width: clamp(64px, 18vw, 88px);
  height: clamp(64px, 18vw, 88px);
  border-radius: 50%;
  border: 5px solid #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(26px, 7vw, 38px);
  box-shadow: 0 6px 0 rgba(43, 36, 64, 0.18);
  transition: transform 0.12s ease;
}
.node-dot.meadow { background: radial-gradient(circle at 35% 30%, #c7ecaf, #6fcf57); }
.node-dot.beach { background: radial-gradient(circle at 35% 30%, #ffd9a0, #ff9e7d); }
.node-dot.night { background: radial-gradient(circle at 35% 30%, #3d5a8a, #10183a); }
.node-dot.snow { background: radial-gradient(circle at 35% 30%, #ffffff, #aacfe8); }
.node-name { font-weight: 800; font-size: clamp(12px, 3vw, 15px); color: #6a627d; text-align: center; }
.node-badge {
  position: absolute;
  top: -6px;
  right: 6%;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  font-size: 18px;
  line-height: 30px;
  text-align: center;
  pointer-events: none;
}
.node.is-done .node-badge { background: var(--star); box-shadow: 0 3px 0 #d8a300; }
.node.is-done .node-badge::after { content: "★"; color: #fff; }
.node.is-locked { cursor: not-allowed; }
.node.is-locked .node-dot { filter: grayscale(0.85) brightness(0.85); box-shadow: 0 4px 0 rgba(43, 36, 64, 0.12); }
.node.is-locked .node-badge { background: #cfc8bd; box-shadow: 0 2px 0 rgba(43, 36, 64, 0.12); }
.node.is-locked .node-badge::after { content: "🔒"; font-size: 14px; }
.node:not(.is-locked):active .node-dot { transform: translateY(4px) scale(0.96); box-shadow: 0 2px 0 rgba(43, 36, 64, 0.18); }
.node:not(.is-locked):focus-visible .node-dot { outline: 4px solid var(--jump); outline-offset: 3px; }

.win-actions { display: flex; flex-direction: column; align-items: center; gap: 12px; }

/* ---------- two stacked corner buttons (pause + mute) ---------- */
#muteBtn { right: max(70px, calc(env(safe-area-inset-right) + 70px)); }
#pauseBtn { right: max(12px, env(safe-area-inset-right)); }
#pauseOverlay, #settings, #tutorial { z-index: 45; }
.pause-card .win-actions, .settings-card .actions { margin-top: 6px; }

/* ---------- settings ---------- */
.settings-card { width: min(92vw, 440px); }
.gear-btn { margin-top: 14px; }
.toggles { display: flex; flex-direction: column; gap: 12px; margin: 22px 0 8px; }
.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  background: #fff;
  border: 3px solid #ffe6b0;
  border-radius: 18px;
  padding: 12px 16px;
  cursor: pointer;
  font-family: var(--ui-font);
  box-shadow: 0 4px 0 rgba(43, 36, 64, 0.1);
}
.toggle-row:active { transform: translateY(3px); box-shadow: 0 1px 0 rgba(43, 36, 64, 0.1); }
.toggle-label { font-weight: 800; font-size: clamp(16px, 4vw, 20px); color: var(--ink); }
.toggle-pill {
  width: 56px; height: 30px; border-radius: 999px;
  background: #d9d2c4; flex: 0 0 auto; position: relative;
  transition: background 0.18s ease;
}
.toggle-knob {
  position: absolute; top: 3px; left: 3px;
  width: 24px; height: 24px; border-radius: 50%;
  background: #fff; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  transition: transform 0.18s ease;
}
.toggle-row[aria-checked="true"] .toggle-pill { background: var(--grass); }
.toggle-row[aria-checked="true"] .toggle-knob { transform: translateX(26px); }
.toggle-row:focus-visible { outline: 4px solid var(--jump); outline-offset: 3px; }

/* ---------- win stars + per-node stars ---------- */
.win-stars {
  font-size: clamp(38px, 11vw, 64px);
  letter-spacing: 6px;
  color: var(--star);
  text-shadow: 0 3px 0 #d8a300;
  margin: 6px 0 14px;
  line-height: 1;
}
.win-stars .dim { color: #e3dccb; text-shadow: none; }
.node-stars {
  font-size: clamp(12px, 3.2vw, 16px);
  letter-spacing: 1px;
  color: var(--star);
  line-height: 1;
  min-height: 1em;
}
.node-stars .dim { color: #d9d2c4; }

/* ---------- level transition fade ---------- */
.fade {
  position: absolute;
  inset: 0;
  z-index: 60;
  background: #fff7e6;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}
.fade.show { opacity: 1; }

/* ---------- tutorial ---------- */
.tut-card { width: min(94vw, 480px); }
.tut-rows { list-style: none; text-align: left; margin: 18px 0 24px; display: flex; flex-direction: column; gap: 12px; }
.tut-rows li { display: flex; align-items: center; gap: 14px; font-weight: 700; font-size: clamp(15px, 3.6vw, 18px); color: #5a5270; }
.tut-ico {
  flex: 0 0 auto;
  width: 52px; height: 52px;
  display: flex; align-items: center; justify-content: center;
  background: #fff; border: 3px solid #ffe6b0; border-radius: 14px;
  font-size: clamp(18px, 4.4vw, 24px); font-weight: 900; color: var(--ink);
  box-shadow: 0 3px 0 rgba(43, 36, 64, 0.1);
}

/* ---------- cosmetics closet ---------- */
.closet { margin: 14px auto 4px; max-width: 560px; text-align: center; }
.closet-title { font: 800 1rem var(--ui-font); color: var(--ink); margin: 0 0 8px; }
.closet-stars { color: var(--star); font-weight: 900; }
.closet-row { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.swatch {
  position: relative; width: 64px; height: 64px; border-radius: 14px;
  border: 3px solid #e7d9c2; background: #fff7ea; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  font: 800 0.62rem var(--ui-font); color: #6b5a40;
  transition: transform 0.15s ease, border-color 0.15s ease;
}
.swatch canvas { pointer-events: none; }
.swatch:hover, .swatch:focus-visible { transform: translateY(-3px); border-color: #ffb14d; outline: none; }
.swatch.is-equipped { border-color: #ff5d8f; box-shadow: 0 0 0 3px rgba(255, 93, 143, 0.25); }
.swatch.is-locked { cursor: not-allowed; opacity: 0.55; filter: grayscale(0.6); }
.swatch .cost { position: absolute; bottom: 2px; right: 5px; font-size: 0.6rem; color: var(--star); }
.swatch .lock { position: absolute; top: 2px; right: 4px; font-size: 0.7rem; }

@media (prefers-reduced-motion: reduce) {
  .node-dot { transition: none; }
  .toggle-pill, .toggle-knob, .swatch { transition: none; }
  .fade { transition: opacity 0.08s linear; }
}

/* ---------- v4: coin HUD ---------- */
.ico.coin { background: radial-gradient(circle at 35% 30%, #ffe27a, #f4b400); border-radius: 50%; }

/* ---------- v4: colorblind-safe (Okabe-Ito) + letter differentiators ---------- */
body.colorblind { --apple: #d55e00; --egg: #f0e442; --star: #0072b2; }
body.colorblind .ico { position: relative; }
body.colorblind .ico::after {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 900; color: #fff; text-shadow: 0 1px 1px rgba(0, 0, 0, 0.55); line-height: 1;
}
body.colorblind .ico.apple::after { content: "A"; }
body.colorblind .ico.egg::after { content: "E"; color: #6a5a00; text-shadow: none; }
body.colorblind .ico.star::after { content: "S"; }

/* ---------- v4: picture mode (hide prose for pre-readers, keep icons) ---------- */
body.icon-only .sub,
body.icon-only .legend,
body.icon-only .node-name,
body.icon-only .closet-title { display: none; }

/* ---------- v4: learning-mode selector ---------- */
.select-row { cursor: pointer; }
.select-value {
  font-weight: 900; font-size: clamp(15px, 3.6vw, 18px); color: var(--btn-dark);
  background: #fff3d4; border-radius: 999px; padding: 4px 14px; min-width: 64px; text-align: center;
}

/* ---------- v4: found tracker ---------- */
.found-line { margin: -10px 0 16px; font-size: clamp(14px, 3.4vw, 18px); font-weight: 800; color: #6a627d; }
.node-found {
  display: inline-block; margin-top: 2px; font-size: clamp(11px, 2.8vw, 14px); font-weight: 800;
  color: #fff; background: #6fcf57; border-radius: 999px; padding: 1px 8px;
}

/* ---------- v4: map nav + badges/album grids ---------- */
.map-nav { display: flex; gap: 10px; justify-content: center; margin: 4px 0 10px; }
.grid-card { width: min(94vw, 540px); }
.badge-grid, .album-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(86px, 1fr));
  gap: 12px; margin: 18px 0 22px; max-height: 56vh; overflow-y: auto;
}
.badge-cell, .album-cell { display: flex; flex-direction: column; align-items: center; text-align: center; gap: 4px; }
.badge-cell canvas, .album-cell canvas { width: 78px; height: 78px; }
.badge-cell .badge-name, .album-cell .label { font-size: 12px; font-weight: 800; color: #6a627d; line-height: 1.1; }
.badge-cell.is-locked .badge-name, .album-cell.locked .label { opacity: 0.55; }
.album-count { color: #6a627d; font-weight: 700; margin-top: -6px; }

/* ---------- v4: seasonal map accents ---------- */
#levelSelect.season-spring { --season-accent: #ffb7d5; }
#levelSelect.season-summer { --season-accent: #ffd24a; }
#levelSelect.season-autumn { --season-accent: #ff9d52; }
#levelSelect.season-winter { --season-accent: #9fd4ff; }
#levelSelect.season-holiday { --season-accent: #ff6b6b; }
#levelSelect[class*="season-"] .map-card { box-shadow: 0 14px 0 rgba(43, 36, 64, 0.12), 0 0 0 5px var(--season-accent, #fff); }
