:root {
  --sea: #58c4dd;
  --sea-deep: #2f9fc4;
  --sand: #f6dfa4;
  --ink: #1d3557;
  --card: #ffffff;
  --accent: #ff8c42;
  --accent-dark: #e06a1b;
  --good: #2ec27e;
  --soft-red: #f4a3a3;
  --muted: #8aa3b5;
  --tile: max(64px, 13vmin);
  --gap: 2vmin;
  --radius: 3vmin;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  font-family: ui-rounded, "SF Pro Rounded", "Arial Rounded MT Bold", system-ui, -apple-system, "Segoe UI", sans-serif;
  color: var(--ink);
  background: var(--sea);
}

#app {
  position: fixed;
  inset: 0;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  display: flex;
  flex-direction: column;
  background: linear-gradient(180deg, #bfeaf5 0%, var(--sea) 30%, var(--sea-deep) 100%);
}

svg { display: block; }
button { font: inherit; color: inherit; border: 0; padding: 0; background: none; cursor: pointer; touch-action: manipulation; }
button:disabled { cursor: default; }

/* ---------- shared pieces ---------- */

.bar {
  display: flex;
  align-items: center;
  gap: var(--gap);
  padding: 2vmin 3vmin;
  flex: none;
}
.bar .spacer { flex: 1; }
.title { font-size: 5vmin; font-weight: 800; margin: 0; }

.round-btn {
  width: max(64px, 10vmin);
  height: max(64px, 10vmin);
  border-radius: 50%;
  background: var(--card);
  box-shadow: 0 0.8vmin 0 rgba(29, 53, 87, 0.25);
  display: grid;
  place-items: center;
  flex: none;
}
.round-btn .icon { width: 55%; height: 55%; }
.round-btn:active, .pill:active, .tile:active { transform: translateY(0.4vmin); box-shadow: 0 0.3vmin 0 rgba(29, 53, 87, 0.25); }

.pill {
  min-height: max(64px, 10vmin);
  padding: 0 4vmin;
  border-radius: 99vmin;
  background: var(--card);
  box-shadow: 0 0.8vmin 0 rgba(29, 53, 87, 0.25);
  font-size: 4vmin;
  font-weight: 800;
  display: inline-flex;
  align-items: center;
  gap: 1.5vmin;
  flex: none;
}
.pill.primary { background: var(--accent); color: #fff; box-shadow: 0 0.8vmin 0 var(--accent-dark); }
.pill .icon, .pill .star { width: 5vmin; height: 5vmin; }

.star polygon { fill: #e3e9ee; stroke: #c3ced6; }
.star.on polygon { fill: #ffd23f; stroke: #f4a100; }
.stars { display: inline-flex; gap: 0.4vmin; }
.stars .star { width: 3.4vmin; height: 3.4vmin; }

.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: 0 1vmin 0 rgba(29, 53, 87, 0.18);
}

.pip { width: 16vmin; height: 16vmin; }

/* ---------- map ---------- */

.map { position: relative; flex: 1; min-height: 0; }
.map-path { position: absolute; inset: 0; width: 100%; height: 100%; }
.map-path path { fill: none; stroke: #fff; stroke-width: 0.9; stroke-dasharray: 0.4 2.2; stroke-linecap: round; opacity: 0.85; vector-effect: non-scaling-stroke; stroke-width: 6px; stroke-dasharray: 2px 16px; }
.map-path .portrait { display: none; }

.island {
  position: absolute;
  left: var(--lx);
  top: var(--ly);
  width: max(96px, 19vmin);
  transform: translate(-50%, -50%);
  display: flex;
  flex-direction: column;
  align-items: center;
}
.island-art { width: 100%; height: auto; filter: drop-shadow(0 0.6vmin 0 rgba(29, 53, 87, 0.2)); }
.island-name {
  margin-top: -1vmin;
  padding: 0.6vmin 1.6vmin;
  border-radius: 99vmin;
  background: rgba(255, 255, 255, 0.92);
  font-size: max(13px, 2.3vmin);
  font-weight: 800;
  white-space: nowrap;
}
.island-stars { font-size: max(12px, 2.1vmin); font-weight: 800; display: flex; align-items: center; gap: 0.5vmin; margin-top: 0.4vmin; color: #fff; text-shadow: 0 1px 2px rgba(29, 53, 87, 0.6); }
.island-stars .star { width: 2.6vmin; height: 2.6vmin; }
.island.locked .island-art { filter: grayscale(1) opacity(0.55); }
.island.locked .island-name { color: var(--muted); }
.island .lock { position: absolute; top: 22%; left: 50%; width: 30%; transform: translateX(-50%); color: var(--ink); }
.island.current .island-art { animation: bob 1.6s ease-in-out infinite; }
.island .pip { position: absolute; top: -18%; left: -8%; width: 46%; height: auto; pointer-events: none; }

.gear {
  position: absolute;
  right: 2vmin;
  bottom: 2vmin;
  width: 64px;
  height: 64px;
  border-radius: 50%;
  color: rgba(255, 255, 255, 0.75);
  display: grid;
  place-items: center;
  touch-action: none;
}
.gear .icon { width: 50%; height: 50%; }
.gear::after {
  content: "";
  position: absolute;
  inset: 4px;
  border-radius: 50%;
  border: 4px solid #fff;
  clip-path: inset(100% 0 0 0);
}
.gear.holding::after { clip-path: inset(0 0 0 0); transition: clip-path 3s linear; }

/* ---------- island (level picker) ---------- */

.island-screen { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 5vmin; padding: 2vmin 4vmin 6vmin; min-height: 0; }
.island-head { display: flex; align-items: center; gap: 3vmin; }
.speech { padding: 2.4vmin 3.4vmin; font-size: 4vmin; font-weight: 800; position: relative; }
.stones { display: flex; flex-wrap: wrap; justify-content: center; gap: 4vmin; }
.stone {
  width: max(84px, 17vmin);
  height: max(84px, 17vmin);
  border-radius: 50%;
  background: var(--sand);
  box-shadow: 0 1vmin 0 #d9b96a;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.6vmin;
  font-size: 6.5vmin;
  font-weight: 900;
}
.stone.locked { background: #dfe7ec; box-shadow: 0 1vmin 0 #b9c6cf; color: var(--muted); }
.stone.locked .icon { width: 40%; height: 40%; }
.stone.next { animation: bob 1.6s ease-in-out infinite; outline: 0.8vmin solid #fff; }
.stone:not(.locked):active { transform: translateY(0.5vmin); box-shadow: 0 0.4vmin 0 #d9b96a; }

/* ---------- level ---------- */

.dots-progress { display: flex; gap: 1.4vmin; align-items: center; }
.dots-progress i { width: 3vmin; height: 3vmin; border-radius: 50%; background: rgba(255, 255, 255, 0.45); }
.dots-progress i.done { background: #ffd23f; }
.dots-progress i.now { background: #fff; transform: scale(1.3); }

.stage {
  flex: 1;
  min-height: 0;
  margin: 0 3vmin 3vmin;
  padding: 2.5vmin;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2vmin;
  overflow: hidden;
}
.prompt { font-size: 5vmin; font-weight: 800; margin: 0; text-align: center; flex: none; }
.widget { flex: 1; min-height: 0; width: 100%; display: flex; flex-direction: column; align-items: center; justify-content: space-evenly; gap: 2vmin; }
.stage.locked .widget { pointer-events: none; }
.stage.wobble .widget { animation: wobble 0.4s; }
.stage.cheer .widget { animation: pop 0.45s; }

.hint { flex: none; display: flex; align-items: center; justify-content: center; gap: 3vmin; min-height: 0; font-size: 3.6vmin; font-weight: 700; }
.hint:empty { display: none; }
.hint .hint-box { background: #fff7dd; border: 0.5vmin dashed #f4a100; border-radius: 2.5vmin; padding: 1.4vmin 2.4vmin; display: flex; align-items: center; gap: 2.5vmin; }
.ok-row { flex: none; }
.help-row { flex: none; display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 1.5vmin 3vmin; max-width: 100%; }
.help-row:has(.hint:empty):has(.ok-row:empty) { display: none; }

.visual { display: flex; align-items: center; justify-content: center; gap: 3vmin; flex-wrap: wrap; min-height: 0; }
.equation { font-size: 11vmin; font-weight: 900; letter-spacing: 0.5vmin; white-space: nowrap; }
.equation .blank { display: inline-block; min-width: 11vmin; padding: 0 1.5vmin; border-radius: 2vmin; background: #fff3c4; border: 0.6vmin dashed #f4a100; color: var(--accent-dark); text-align: center; }
.groups { display: flex; align-items: center; gap: 3vmin; font-size: 8vmin; font-weight: 900; }

.objs { display: flex; flex-direction: column; gap: 1vmin; }
.obj-row { display: flex; gap: 1vmin; }
.obj-cell { position: relative; width: 8.5vmin; height: 8.5vmin; }
.objs[data-count="11"] .obj-cell, .objs[data-count="12"] .obj-cell, .objs[data-count="13"] .obj-cell, .objs[data-count="14"] .obj-cell, .objs[data-count="15"] .obj-cell,
.objs[data-count="16"] .obj-cell, .objs[data-count="17"] .obj-cell, .objs[data-count="18"] .obj-cell, .objs[data-count="19"] .obj-cell, .objs[data-count="20"] .obj-cell { width: 7vmin; height: 7vmin; }
.obj { width: 100%; height: 100%; }
.obj-cell.crossed .obj { opacity: 0.35; }
.obj-cell.crossed::after { content: ""; position: absolute; left: 0; right: 0; top: 46%; height: 0.9vmin; background: #e63946; transform: rotate(-35deg); border-radius: 1vmin; }
.obj-num { position: absolute; inset: 0; display: grid; place-items: center; font-size: 3.6vmin; font-weight: 900; color: #fff; text-shadow: 0 0 4px #1d3557, 0 0 4px #1d3557; }
.hint .obj-cell, .small .obj-cell { width: 5.5vmin; height: 5.5vmin; }

.dots { display: flex; flex-direction: column; gap: 0.7vmin; }
.dot-row { display: flex; gap: 0.7vmin; }
.dot { width: 3.6vmin; height: 3.6vmin; border-radius: 50%; background: #3a86ff; position: relative; }
.dot.second { background: var(--accent); }
.dot.crossed { background: #cfd8de; }
.dot.crossed::after { content: ""; position: absolute; left: -10%; right: -10%; top: 42%; height: 16%; background: #e63946; transform: rotate(-40deg); }

.tiles { display: flex; flex-wrap: wrap; justify-content: center; gap: var(--gap); flex: none; }
.tile {
  min-width: var(--tile);
  height: var(--tile);
  padding: 0 3vmin;
  border-radius: 2.6vmin;
  background: #eef6fb;
  border: 0.5vmin solid #cfe3ee;
  box-shadow: 0 0.8vmin 0 #b9d3e2;
  font-size: 7vmin;
  font-weight: 900;
  display: grid;
  place-items: center;
}
.tile.word { font-size: 4.4vmin; }
.tile.wrong { background: #f3f3f3; border-color: #e2e2e2; box-shadow: none; color: #b8c2c9; pointer-events: none; }
.tile.right, .tile.answer { background: #d7f5e3; border-color: var(--good); box-shadow: 0 0.8vmin 0 #1f9d57; }
.tile.selected { background: #fff3c4; border-color: #f4a100; box-shadow: 0 0.8vmin 0 #d99000; }
.tile.answer { animation: pulse 0.9s ease-in-out infinite; }
.numpad { display: grid; grid-template-columns: repeat(11, auto); gap: 1.2vmin; flex: none; }
.numpad .tile { --tile: max(64px, 7.6vmin); min-width: var(--tile); padding: 0; font-size: 5vmin; }
.numpad.to20 { grid-template-columns: repeat(7, auto); }

/* ten-frame */
.frames { display: flex; gap: 4vmin; flex-wrap: wrap; justify-content: center; }
.frame { display: grid; grid-template-columns: repeat(5, 1fr); gap: 0; border: 0.7vmin solid var(--ink); border-radius: 1.5vmin; overflow: hidden; background: #fff; }
.cell { width: max(64px, 10vmin); height: max(64px, 10vmin); border: 0.25vmin solid var(--ink); display: grid; place-items: center; }
.two-frames .cell { width: max(52px, 8vmin); height: max(52px, 8vmin); }
.counter { width: 76%; height: 76%; border-radius: 50%; background: #e63946; box-shadow: inset 0 -0.6vmin 0 rgba(0, 0, 0, 0.18); touch-action: none; }
.counter.fixed { background: #3a86ff; }
.counter.ghost { background: transparent; border: 0.6vmin dashed var(--good); box-shadow: none; }
.tray { display: flex; align-items: center; gap: 3vmin; flex: none; }
.pile { width: max(72px, 12vmin); height: max(72px, 12vmin); border-radius: 50%; background: #fde2e4; border: 0.5vmin dashed #e63946; display: grid; place-items: center; touch-action: none; }
.pile .counter { width: 64%; height: 64%; }
.count-badge { font-size: 6vmin; font-weight: 900; min-width: 9vmin; text-align: center; }

/* number line */
.line-wrap { width: 100%; padding: 0 3vmin; flex: none; }
.numberline { position: relative; width: 100%; height: 26vmin; touch-action: none; }
.numberline svg.arcs { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; }
.numberline .axis { position: absolute; left: 0; right: 0; bottom: 9vmin; height: 0.8vmin; background: var(--ink); border-radius: 1vmin; }
.tick { position: absolute; bottom: 0; width: 0; display: flex; flex-direction: column; align-items: center; }
.tick::before { content: ""; position: absolute; bottom: 7.4vmin; width: 0.7vmin; height: 4vmin; background: var(--ink); border-radius: 1vmin; }
.tick span { position: absolute; bottom: 0; transform: translateX(-50%); left: 0; font-size: 4.4vmin; font-weight: 800; width: 7vmin; text-align: center; height: 7vmin; line-height: 7vmin; border-radius: 50%; }
.to20 .tick span { font-size: 3.2vmin; width: 4.6vmin; }
.tick.start span { background: #dbeafe; }
.tick.here span { background: #ffd23f; }
.frog { position: absolute; bottom: 10.5vmin; width: 9vmin; height: 9vmin; transform: translateX(-50%); transition: left 0.18s ease-out; touch-action: none; }
.frog.dragging { transition: none; }

/* compare */
.compare-row { display: flex; align-items: center; justify-content: center; gap: 4vmin; width: 100%; }
.side { min-width: 26vmin; min-height: 26vmin; padding: 2.5vmin; border-radius: 3vmin; background: #eef6fb; border: 0.6vmin solid #cfe3ee; box-shadow: 0 0.8vmin 0 #b9d3e2; display: grid; place-items: center; font-size: 15vmin; font-weight: 900; }
div.side { box-shadow: none; }
.side.wrong { opacity: 0.45; pointer-events: none; }
.side.right, .side.answer { background: #d7f5e3; border-color: var(--good); }
.side .obj-cell { width: 5.6vmin; height: 5.6vmin; }
.sign-slot { width: 16vmin; height: 16vmin; border-radius: 3vmin; border: 0.7vmin dashed #f4a100; background: #fff3c4; display: grid; place-items: center; font-size: 12vmin; font-weight: 900; color: var(--accent-dark); flex: none; }
.sign-slot.over { background: #ffe08a; }
.sign.tile { touch-action: none; }

/* clock */
.clock-face { width: 30vmin; height: 30vmin; }
.clock-options { display: flex; gap: 4vmin; justify-content: center; flex-wrap: wrap; }
.clock-btn { padding: 1.5vmin; border-radius: 50%; background: #eef6fb; border: 0.6vmin solid #cfe3ee; box-shadow: 0 0.8vmin 0 #b9d3e2; }
.clock-btn .clock-face { width: 24vmin; height: 24vmin; }
.clock-btn.wrong { opacity: 0.4; pointer-events: none; box-shadow: none; }
.clock-btn.right, .clock-btn.answer { background: #d7f5e3; border-color: var(--good); }
.time-text { font-size: 10vmin; font-weight: 900; }

/* shapes */
.shape-art { width: 100%; height: 100%; }
.visual .shape-art { width: 26vmin; height: 26vmin; }
.shape-grid { display: grid; gap: 1.5vmin; justify-content: center; grid-template-columns: repeat(var(--cols, 3), auto); }
.shape-btn { width: max(64px, 13vmin); height: max(64px, 13vmin); padding: 1vmin; border-radius: 2.4vmin; border: 0.6vmin solid transparent; }
.shape-btn.selected { background: #fff3c4; border-color: #f4a100; }
.shape-btn.answer { background: #d7f5e3; border-color: var(--good); }

/* ---------- level complete ---------- */

.complete { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3.5vmin; padding: 4vmin; text-align: center; min-height: 0; }
.complete h1 { font-size: 8vmin; margin: 0; color: #fff; text-shadow: 0 0.6vmin 0 rgba(29, 53, 87, 0.3); }
.big-stars { display: flex; gap: 2vmin; }
.big-stars .star { width: 18vmin; height: 18vmin; }
.big-stars .star.on { animation: pop 0.5s backwards; }
.big-stars .star:nth-child(2) { animation-delay: 0.25s; }
.big-stars .star:nth-child(3) { animation-delay: 0.5s; }
.reward { display: flex; align-items: center; gap: 3vmin; padding: 2vmin 4vmin; font-size: 4vmin; font-weight: 800; }
.reward .sticker { width: 14vmin; height: 14vmin; animation: pop 0.6s 0.7s backwards; }
.actions { display: flex; gap: 3vmin; flex-wrap: wrap; justify-content: center; }

/* ---------- sticker book ---------- */

.book { flex: 1; min-height: 0; margin: 0 3vmin 3vmin; padding: 3vmin; display: grid; grid-template-columns: repeat(9, 1fr); gap: 2vmin; align-content: center; justify-items: center; }
.book .sticker { width: 100%; max-width: 13vmin; height: auto; }
.sticker.missing { filter: grayscale(1); opacity: 0.22; }

/* ---------- parent page ---------- */

[data-screen="parent"] { -webkit-user-select: text; user-select: text; }
.parent { flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; touch-action: pan-y; padding: 0 3vmin 4vmin; display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 2.5vmin; align-content: start; align-items: start; }
.panel { padding: 18px 20px; font-size: 17px; }
.panel h2 { font-size: 20px; margin: 0 0 10px; }
.panel p { margin: 6px 0; line-height: 1.4; }
.panel table { width: 100%; border-collapse: collapse; }
.panel td { padding: 5px 4px; border-top: 1px solid #e6eef3; vertical-align: middle; }
.panel td:last-child { text-align: right; white-space: nowrap; }
.panel .stars .star { width: 18px; height: 18px; }
.panel textarea { width: 100%; height: 76px; font: 13px ui-monospace, Menlo, Consolas, monospace; border: 2px solid #cfe3ee; border-radius: 10px; padding: 8px; resize: none; -webkit-user-select: text; user-select: text; }
.small-btn { min-height: 48px; padding: 0 18px; border-radius: 12px; background: #eef6fb; border: 2px solid #cfe3ee; font-size: 16px; font-weight: 700; margin: 6px 6px 0 0; }
.small-btn.danger { background: #fde2e4; border-color: #f4a3a3; color: #9c1f3d; }
.small-btn.on { background: #d7f5e3; border-color: var(--good); }
.note { color: #5b7385; font-size: 15px; }
.msg { font-weight: 700; min-height: 1.4em; }
.msg.bad { color: #c1121f; }
.msg.good { color: #1f9d57; }
.install-note { background: #fff7dd; border: 2px dashed #f4a100; }

/* ---------- motion ---------- */

@keyframes bob { 0%, 100% { translate: 0 0; } 50% { translate: 0 -1.2vmin; } }
@keyframes wobble { 0%, 100% { translate: 0; } 20% { translate: -2vmin; } 40% { translate: 2vmin; } 60% { translate: -1.2vmin; } 80% { translate: 1.2vmin; } }
@keyframes pop { 0% { scale: 0.6; opacity: 0; } 70% { scale: 1.12; opacity: 1; } 100% { scale: 1; } }
@keyframes pulse { 0%, 100% { scale: 1; } 50% { scale: 1.08; } }

@media (prefers-reduced-motion: reduce) {
  .island.current .island-art, .stone.next, .tile.answer { animation: none; }
}

/* ---------- portrait ---------- */

@media (orientation: portrait) {
  .island { left: var(--px); top: var(--py); width: max(96px, 24vmin); }
  .map-path .landscape { display: none; }
  .map-path .portrait { display: inline; }
  .numpad { grid-template-columns: repeat(6, auto); }
  .numpad.to20 { grid-template-columns: repeat(7, auto); }
  .numpad .tile { --tile: max(64px, 11vmin); }
  .book { grid-template-columns: repeat(5, 1fr); }
  .compare-row { gap: 2vmin; }
  .side { min-width: 24vmin; }
}

/* Empty slot the size of a round button, so the progress dots stay centred. */
.round-gap { width: max(64px, 10vmin); flex: none; }

/* Landscape is short: once a hint or the answer is showing, the picture and the
   pieces shrink so nothing overlaps. Portrait has room to spare. */
@media (orientation: landscape) {
  .stage.helping { gap: 1.2vmin; }
  .stage.helping .widget { gap: 1.2vmin; }
  .stage.helping .prompt { font-size: 4vmin; }
  .stage.helping .equation { font-size: 7vmin; }
  .stage.helping .equation .blank { min-width: 7vmin; }
  .stage.helping .groups { font-size: 5.5vmin; }
  .stage.helping .widget .obj-cell { width: 5.5vmin; height: 5.5vmin; }
  .stage.helping .numberline { height: 20vmin; }
  .stage.helping .cell { width: max(52px, 7vmin); height: max(52px, 7vmin); }
  .stage.helping .pile { width: 64px; height: 64px; }
  .stage.helping .hint { font-size: 3vmin; }
  .stage.helping .hint .obj-cell { width: 4.2vmin; height: 4.2vmin; }
  .stage.helping .hint .obj-num { font-size: 2.6vmin; }
  .stage.helping .hint-box { padding: 0.8vmin 1.6vmin; gap: 1.5vmin; }
  /* Four rows of objects (16 to 20) need one more step down. */
  .stage.helping .widget .objs:is([data-count="16"], [data-count="17"], [data-count="18"], [data-count="19"], [data-count="20"]) .obj-cell { width: 4vmin; height: 4vmin; }
  .stage.helping .hint .objs:is([data-count="16"], [data-count="17"], [data-count="18"], [data-count="19"], [data-count="20"]) .obj-cell { width: 3.4vmin; height: 3.4vmin; }
  .stage.helping .hint .objs:is([data-count="16"], [data-count="17"], [data-count="18"], [data-count="19"], [data-count="20"]) .obj-num { font-size: 2.2vmin; }
}
