:root {
  --ink: #30254d;
  --cream: #fffaf0;
  --plum: #7452b8;
  --brass: #c08a2e;
  --brass-dark: #8a5a14;
  --star: #ffd84d;
  --star-edge: #a86100;
  --control: 56px;
  --gap: 12px;
  --edge: 12px;
  --tint: #fffaf0;
  --ease-pop: cubic-bezier(.2, .9, .25, 1.2);
  --round-font: ui-rounded, "SF Pro Rounded", "Arial Rounded MT Bold", system-ui, sans-serif;
}

* { box-sizing: border-box; }
html,
body {
  width: 100%;
  min-height: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
  background: #2b2140;
  color: var(--ink);
  font-family: var(--round-font);
  font-weight: 800;
}
button,
a { font: inherit; }

#game-shell {
  position: relative;
  width: 100%;
  height: 100dvh;
  min-height: 100dvh;
  overflow: hidden;
  isolation: isolate;
  touch-action: manipulation;
  background: radial-gradient(circle at 50% 50%, #4a3220 0, #2b1d12 46%, #160f0a 100%);
}
#game-shell[data-room="shelf"] { --tint: #fff4dc; }
#game-shell[data-room="garden"] { --tint: #eef0ff; }
#game-shell[data-room="workshop"] { --tint: #fff0dc; }

/* The spyglass rim: a brass lens frame drawn around the whole view. */
#game-shell::after {
  content: "";
  position: absolute;
  z-index: 3;
  inset: 0;
  border-radius: 28px;
  box-shadow: inset 0 0 0 5px var(--brass), inset 0 0 0 8px rgba(48, 37, 77, .55), inset 0 0 38px rgba(36, 20, 6, .35);
  pointer-events: none;
}

#game-canvas {
  position: absolute;
  z-index: 1;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  cursor: pointer;
  touch-action: none;
  outline: none;
  border-radius: 28px;
  transition: clip-path 350ms cubic-bezier(.45, 0, .2, 1);
}
#game-canvas:focus, #game-canvas:focus-visible { outline: none; }
#game-canvas.keyboard-focus { outline: 4px solid var(--plum); outline-offset: -12px; }

.iris-eye {
  position: absolute;
  z-index: 0;
  left: 50%;
  top: 50%;
  width: 150px;
  transform: translate(-50%, -50%);
  pointer-events: none;
}
.iris-eye svg { display: block; width: 100%; }
.eye-fur { fill: #f08a2c; stroke: var(--ink); stroke-width: 4; }
.eye-white { fill: #fff6dc; }
.eye-iris { fill: #ffb43a; stroke: #a86100; stroke-width: 3; }
.eye-pupil { fill: var(--ink); }
.eye-shine { fill: #fff; }

.iris-ring {
  position: absolute;
  z-index: 2;
  left: 50%;
  top: 50%;
  width: var(--iris-size, 200vmax);
  height: var(--iris-size, 200vmax);
  transform: translate(-50%, -50%) scale(1);
  transition: transform 350ms cubic-bezier(.45, 0, .2, 1);
  pointer-events: none;
}
.iris-ring svg { display: block; width: 100%; height: 100%; overflow: visible; }
.iris-ring circle { fill: none; vector-effect: non-scaling-stroke; }
.ring-brass { stroke: var(--brass); stroke-width: 16; }
.ring-edge { stroke: var(--ink); stroke-width: 3; }

/* ---------- HUD ---------- */

.hud {
  position: absolute;
  z-index: 12;
  inset: 0 0 auto 0;
  pointer-events: none;
}

.actions {
  position: absolute;
  top: max(var(--edge), env(safe-area-inset-top));
  right: max(var(--edge), env(safe-area-inset-right));
  display: flex;
  gap: var(--gap);
  pointer-events: none;
}

.round-control {
  width: var(--control);
  height: var(--control);
  min-width: 56px;
  min-height: 56px;
  padding: 0;
  display: grid;
  place-items: center;
  border: 3px solid var(--ink);
  border-radius: 18px;
  background: var(--tint);
  color: var(--ink);
  box-shadow: 0 4px 0 var(--ink);
  cursor: pointer;
  pointer-events: auto;
  -webkit-tap-highlight-color: transparent;
  transition: transform 160ms var(--ease-pop), box-shadow 160ms var(--ease-pop);
}
.round-control:active { transform: translateY(2px) scale(.94); box-shadow: 0 2px 0 var(--ink); transition-duration: 80ms; }
.round-control:focus-visible,
.big-button:focus-visible { outline: 4px solid #fff; outline-offset: 2px; box-shadow: 0 0 0 7px var(--plum), 0 4px 0 var(--ink); }
.round-control svg { width: 26px; height: 26px; fill: none; stroke: currentColor; stroke-linecap: round; stroke-linejoin: round; stroke-width: 2.3; }
.sound-off { display: none; }
#sound-button[aria-pressed="false"] .sound-on { display: none; }
#sound-button[aria-pressed="false"] .sound-off { display: block; }

.prompt-card {
  position: absolute;
  top: max(var(--edge), env(safe-area-inset-top));
  left: 50%;
  width: max-content;
  max-width: min(560px, calc(100vw - 2 * (var(--edge) + 3 * var(--control) + 2 * var(--gap) + 16px)));
  min-height: 64px;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 7px 18px 7px 8px;
  border: 3px solid var(--ink);
  border-radius: 24px;
  background: var(--tint);
  box-shadow: 0 5px 0 var(--ink), 0 12px 24px rgba(30, 20, 50, .22);
  transform: translateX(-50%);
  pointer-events: none;
  user-select: none;
}
.goal-icon {
  flex: 0 0 auto;
  width: 48px;
  height: 48px;
  display: grid;
  place-items: center;
  border: 3px solid var(--ink);
  border-radius: 16px;
  background: #fff;
  transition: transform 260ms var(--ease-pop);
}
.goal-icon svg { width: 38px; height: 38px; overflow: visible; stroke: var(--ink); stroke-width: 1.5; stroke-linejoin: round; stroke-linecap: round; }
.prompt-card.hint-strong .goal-icon { transform: scale(1.15); animation: icon-pulse 1.1s ease-in-out infinite; }
.prompt-card.solved .goal-icon { background: #fff4c2; }
@keyframes icon-pulse { 50% { transform: scale(1.22); } }

.prompt-copy { min-width: 0; display: grid; gap: 4px; }
#prompt-text {
  display: block;
  overflow: hidden;
  font-size: 22px;
  font-weight: 900;
  line-height: 1.1;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.prompt-meta { display: flex; align-items: center; gap: 10px; min-height: 18px; }
.riddle-progress { display: flex; align-items: center; gap: 6px; min-height: 18px; }
/* Mixed mode: which language this level speaks, drawn as a small lettered tag (no flags). */
.lang-badge {
  display: inline-grid;
  place-items: center;
  min-width: 34px;
  height: 22px;
  padding: 0 6px;
  border: 2px solid var(--ink);
  border-radius: 8px;
  background: var(--plum);
  color: #fff;
  font-size: 16px;
  font-weight: 900;
  line-height: 1;
  letter-spacing: .04em;
}
.lang-badge[hidden] { display: none; }
.lang-badge[data-lang="en"] { background: #2f7d6d; }
.lang-badge[data-lang="et"] { background: #3f6fc4; }

/* The word for the toy just touched, in the level language. */
.name-pop {
  position: absolute;
  z-index: 11;
  left: 0;
  top: 0;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  max-width: calc(100% - 28px);
  padding: 6px 14px;
  border: 3px solid var(--ink);
  border-radius: 18px;
  background: var(--cream);
  box-shadow: 0 4px 0 var(--ink);
  font-size: 20px;
  font-weight: 900;
  line-height: 1.1;
  white-space: nowrap;
  pointer-events: none;
  user-select: none;
}
.name-pop[hidden] { display: none; }
.name-pop.pop { animation: name-pop 1700ms var(--ease-pop) forwards; }
.name-swatch { display: none; width: 18px; height: 18px; flex: 0 0 auto; border: 2px solid var(--ink); border-radius: 50%; background: var(--swatch, transparent); }
.name-pop.with-colour .name-swatch { display: block; }
@keyframes name-pop {
  0% { opacity: 1; transform: translateY(6px) scale(.85); }
  14% { opacity: 1; transform: translateY(0) scale(1.04); }
  22%, 82% { opacity: 1; transform: none; }
  100% { opacity: 0; transform: translateY(-6px); }
}
.riddle-dot { width: 16px; height: 16px; border: 3px solid var(--ink); border-radius: 50%; background: #fff; }
.riddle-dot.done { background: var(--star); }
.riddle-dot.current { background: var(--plum); }
.riddle-dot svg { display: block; width: 100%; height: 100%; fill: none; stroke: var(--ink); stroke-width: 3.2; stroke-linecap: round; stroke-linejoin: round; }
.wake-eye { width: 28px; height: 20px; }
.wake-eye svg { display: block; width: 100%; height: 100%; fill: none; stroke: var(--ink); stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
.wake-eye .open { display: none; }
.wake-eye.awake .open { display: inline; }
.wake-eye.awake .closed { display: none; }
.wake-eye.awake { animation: eye-open 300ms var(--ease-pop); }
@keyframes eye-open { from { transform: scale(.6); } }

.room-progress {
  position: absolute;
  z-index: 11;
  left: max(var(--edge), env(safe-area-inset-left));
  bottom: max(var(--edge), env(safe-area-inset-bottom));
  display: flex;
  gap: 8px;
  padding: 7px 10px 6px;
  border: 3px solid var(--ink);
  border-radius: 22px;
  background: var(--tint);
  box-shadow: 0 4px 0 var(--ink);
  pointer-events: none;
}
.medallion { display: grid; justify-items: center; gap: 4px; }
.medallion svg {
  width: 40px;
  height: 40px;
  padding: 5px;
  border: 3px solid var(--ink);
  border-radius: 50%;
  background: #efe6d2;
  fill: none;
  stroke: var(--ink);
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.medallion.current svg { background: var(--star); }
.medallion.done svg { background: #ffe9a6; }
.level-dots { display: flex; gap: 3px; }
.level-dots i { width: 9px; height: 9px; border: 2px solid var(--ink); border-radius: 50%; background: #fff; }
.level-dots i.done { background: var(--star-edge); }
.level-dots i.current { background: var(--plum); }

.fox-guide {
  position: absolute;
  z-index: 11;
  right: max(var(--edge), env(safe-area-inset-right));
  bottom: max(var(--edge), env(safe-area-inset-bottom));
  width: 96px;
  height: 96px;
  pointer-events: none;
}
.fox-guide svg,
.celebration-fox svg { display: block; width: 100%; height: 100%; overflow: visible; }
.fox-rim { fill: var(--brass); stroke: var(--ink); stroke-width: 3; }
.fox-lens { fill: #fff6dd; stroke: var(--brass-dark); stroke-width: 2; }
.fox-ear { fill: #f08a2c; stroke: var(--ink); stroke-width: 3; stroke-linejoin: round; }
.fox-ear-inner { fill: #5a3424; }
.fox-face { fill: #f08a2c; stroke: var(--ink); stroke-width: 3; stroke-linejoin: round; }
.fox-muzzle { fill: #fff1d6; }
.fox-eyes { fill: var(--ink); transition: transform 260ms var(--ease-pop); transform: translate(var(--glance-x, 0px), var(--glance-y, 0px)); }
.fox-nose { fill: var(--ink); }
.fox-smile { fill: none; stroke: var(--ink); stroke-width: 2.5; stroke-linecap: round; }
.fox-head { transform-origin: 48px 70px; }
.fox-guide.nod .fox-head { animation: fox-nod 520ms var(--ease-pop); }
@keyframes fox-nod { 30% { transform: rotate(-6deg) translateY(3px); } 65% { transform: rotate(4deg); } }

.title-plate {
  position: absolute;
  z-index: 14;
  left: 50%;
  top: 50%;
  padding: 18px 30px;
  border: 4px solid var(--ink);
  border-radius: 26px;
  background: var(--cream);
  box-shadow: 0 0 0 8px var(--brass), 0 0 0 11px var(--ink), 0 18px 40px rgba(0, 0, 0, .35);
  transform: translate(-50%, -50%);
  transition: opacity 300ms ease-out, transform 300ms var(--ease-pop);
  pointer-events: none;
}
.title-plate strong { display: block; font-size: 30px; font-weight: 950; white-space: nowrap; }
.title-plate.gone { opacity: 0; transform: translate(-50%, -50%) scale(.9); }

.visually-hidden {
  position: absolute !important;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

/* ---------- Celebration (platform grammar) ---------- */

.celebration {
  position: absolute;
  z-index: 30;
  inset: 0;
  display: grid;
  place-items: center;
  padding: max(84px, calc(env(safe-area-inset-top) + 76px)) 16px max(124px, calc(env(safe-area-inset-bottom) + 124px));
  background: rgba(30, 20, 50, .34);
}
.celebration[hidden] { display: none; }
.celebration-card {
  width: min(460px, 100%);
  display: grid;
  justify-items: center;
  gap: 14px;
  padding: 18px 18px 20px;
  border: 4px solid var(--ink);
  border-radius: 30px;
  background: var(--cream);
  box-shadow: 0 7px 0 var(--ink), 0 22px 50px rgba(0, 0, 0, .35);
  animation: card-in 380ms var(--ease-pop);
}
@keyframes card-in { from { opacity: 0; transform: scale(.84); } }
.celebration-art { position: relative; width: 132px; height: 104px; }
.celebration-star { position: absolute; left: 0; top: 0; width: 104px; height: 104px; fill: var(--star); stroke: var(--star-edge); stroke-width: 1.3; stroke-linejoin: round; animation: star-in 520ms var(--ease-pop); }
@keyframes star-in { from { transform: scale(.3) rotate(-30deg); } }
.celebration-fox { position: absolute; right: -6px; bottom: -4px; width: 60px; height: 60px; }
.celebration-phrase { font-size: 32px; font-weight: 950; line-height: 1.05; text-align: center; }
.celebration-actions { display: flex; flex-wrap: nowrap; gap: 12px; justify-content: center; }
.big-button {
  min-width: 64px;
  min-height: 64px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 8px 16px;
  border: 3px solid var(--ink);
  border-radius: 20px;
  background: #fff;
  color: var(--ink);
  box-shadow: 0 5px 0 var(--ink);
  font-size: 18px;
  font-weight: 900;
  text-decoration: none;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: transform 160ms var(--ease-pop), box-shadow 160ms var(--ease-pop);
}
.big-button[hidden] { display: none; }
.big-button span { white-space: nowrap; }
.big-button.primary { background: var(--star); }
.big-button:active { transform: translateY(3px) scale(.94); box-shadow: 0 2px 0 var(--ink); transition-duration: 80ms; }
.big-button svg { width: 26px; height: 26px; flex: 0 0 auto; fill: none; stroke: currentColor; stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; }

.error-card {
  position: absolute;
  z-index: 40;
  inset: 50% auto auto 50%;
  width: min(390px, calc(100vw - 32px));
  padding: 25px;
  border: 3px solid var(--ink);
  border-radius: 24px;
  background: var(--cream);
  text-align: center;
  transform: translate(-50%, -50%);
}
.error-card strong,
.error-card span { display: block; }
.error-card strong { font-size: 24px; }
.error-card span { margin-top: 8px; font-size: 18px; }

/* ---------- Narrower screens: the task panel takes its own row ---------- */

@media (max-width: 820px) {
  .prompt-card {
    top: var(--panel-top, calc(max(var(--edge), env(safe-area-inset-top)) + var(--control) + 12px));
    max-width: min(560px, calc(100vw - 2 * var(--edge)));
  }
}

@media (max-width: 560px) {
  :root { --control: 48px; }
  .round-control { min-width: 48px; min-height: 48px; border-radius: 16px; }
  .round-control svg { width: 23px; height: 23px; }
  .prompt-card { min-height: 60px; gap: 10px; padding: 6px 14px 6px 7px; }
  .goal-icon { width: 44px; height: 44px; border-radius: 14px; }
  .goal-icon svg { width: 34px; height: 34px; }
  #prompt-text { font-size: 19px; }
  .name-pop { font-size: 18px; padding: 5px 12px; }
  .fox-guide { width: 72px; height: 72px; }
  .medallion svg { width: 36px; height: 36px; }
  .title-plate strong { font-size: 28px; }
  .celebration-actions { gap: 10px; }
  .big-button { flex-direction: column; gap: 2px; min-width: 92px; padding: 6px 8px; font-size: 16px; }
  .celebration-phrase { font-size: 28px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 1ms !important; animation-iteration-count: 1 !important; transition-duration: 1ms !important; }
  .prompt-card.hint-strong .goal-icon { animation: none; transform: scale(1.15); }
  .name-pop.pop { animation: none; }
}
