:root {
  --ink: #1e1b4b;
  --muted: #5b5880;
  --card: #ffffff;
  --brand: #4338ca;
  --brand-2: #7c3aed;
  --play: #16a34a;
  --play-dark: #15803d;
  --sel: #eef2ff;
  --sel-border: #6366f1;
  --bad: #dc2626;
  --sky-1: #38bdf8;
  --sky-2: #818cf8;
  --sky-3: #c084fc;
  --radius: 22px;
  --font: 'Fredoka', ui-rounded, 'SF Pro Rounded', 'Segoe UI', system-ui, sans-serif;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font-family: var(--font);
  color: var(--ink);
  background: linear-gradient(160deg, var(--sky-1), var(--sky-2) 55%, var(--sky-3));
  background-attachment: fixed;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

button { font: inherit; color: inherit; cursor: pointer; }
[hidden] { display: none !important; }

.screen { display: none; }
.screen.active { display: block; }

/* ---------- Home & results ---------- */
#home, #results {
  min-height: 100dvh;
  padding: calc(16px + var(--safe-t)) calc(16px + var(--safe-r)) calc(24px + var(--safe-b)) calc(16px + var(--safe-l));
}
.home-inner { max-width: 820px; margin: 0 auto; display: grid; gap: 16px; }
.back { color: #fff; font-weight: 600; text-decoration: none; justify-self: start; padding: 6px 2px; }

.brand { text-align: center; color: #fff; padding: 8px 0 0; }
.brand .logo { font-size: clamp(48px, 12vw, 80px); line-height: 1; filter: drop-shadow(0 6px 10px rgba(0,0,0,.2)); }
.brand h1 { margin: 4px 0 0; font-size: clamp(34px, 8vw, 56px); font-weight: 700; letter-spacing: .5px; text-shadow: 0 3px 0 rgba(30,27,75,.25); }
.brand p { margin: 4px 0 0; font-size: clamp(16px, 4vw, 20px); font-weight: 500; }

.card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: 0 10px 30px rgba(30, 27, 75, .18);
}
.card h2 { margin: 0 0 12px; font-size: 20px; display: flex; align-items: center; gap: 8px; }
.step {
  display: inline-grid; place-items: center; width: 28px; height: 28px; border-radius: 50%;
  background: var(--brand); color: #fff; font-size: 16px;
}

.topics { display: grid; grid-template-columns: repeat(auto-fill, minmax(94px, 1fr)); gap: 8px; }
@media (min-width: 600px) { .topics { grid-template-columns: repeat(auto-fill, minmax(124px, 1fr)); gap: 10px; } }
.topic {
  border: 3px solid #e5e7eb; background: #f8fafc; border-radius: 18px;
  padding: 8px 4px; display: grid; justify-items: center; align-content: center; gap: 2px;
  font-size: 15px; font-weight: 600; min-height: 86px; line-height: 1.15; text-align: center;
  transition: transform .12s, border-color .12s, background .12s;
}
.topic .ic { font-size: 34px; line-height: 1.1; }
.topic small { font-weight: 400; color: var(--muted); font-size: 12px; }
.topic:active { transform: scale(.96); }
.topic[aria-checked="true"] { border-color: var(--sel-border); background: var(--sel); box-shadow: 0 0 0 3px rgba(99,102,241,.2); }

.option { display: grid; gap: 6px; margin-bottom: 12px; }
.option:last-child { margin-bottom: 0; }
.option-label { font-weight: 600; color: var(--muted); font-size: 14px; }
.seg { display: flex; flex-wrap: wrap; gap: 6px; }
.seg button {
  flex: 1 1 auto; min-height: 44px; padding: 8px 12px; border-radius: 14px;
  border: 2px solid #e5e7eb; background: #f8fafc; font-weight: 500; font-size: 15px;
}
.seg button[aria-pressed="true"] { border-color: var(--sel-border); background: var(--sel); font-weight: 600; }
.level-note { margin: 2px 0 0; font-size: 14px; color: var(--muted); line-height: 1.35; }

.btn {
  border: 0; border-radius: 999px; min-height: 52px; padding: 10px 26px;
  font-weight: 700; font-size: 20px;
  transition: transform .1s, filter .1s;
}
.btn:active { transform: translateY(2px); }
.btn-play { background: var(--play); color: #fff; box-shadow: 0 5px 0 var(--play-dark); }
.btn-ghost { background: #fff; color: var(--brand); box-shadow: 0 5px 0 #c7d2fe; }
#playBtn { font-size: 28px; min-height: 68px; justify-self: center; width: min(100%, 360px); }

.privacy { margin: 0; text-align: center; color: #fff; font-size: 14px; line-height: 1.4; }
.install { display: grid; justify-items: center; gap: 6px; color: #fff; font-size: 14px; text-align: center; }
.install p { margin: 0; }

/* ---------- Game ---------- */
#game.active {
  position: fixed; inset: 0; overflow: hidden; display: block;
  background: linear-gradient(180deg, #7dd3fc, #a5b4fc 60%, #f0abfc);
  touch-action: none; user-select: none; -webkit-user-select: none;
}
#cam {
  position: absolute; inset: 0; width: 100%; height: 100%;
  object-fit: cover; transform: scaleX(-1);
  opacity: 0; transition: opacity .3s;
}
#game.camera #cam { opacity: 1; }
#game.camera::after { /* soften the video so bubbles stand out */
  content: ''; position: absolute; inset: 0; pointer-events: none;
  background: linear-gradient(180deg, rgba(30,27,75,.35), rgba(30,27,75,.05) 30%, rgba(30,27,75,.15));
}
#stage { position: absolute; inset: 0; width: 100%; height: 100%; z-index: 1; touch-action: none; }

.hud {
  position: absolute; z-index: 2; left: 0; right: 0; top: 0;
  padding: calc(10px + var(--safe-t)) calc(12px + var(--safe-r)) 0 calc(12px + var(--safe-l));
  display: grid; gap: 8px; align-items: center;
  grid-template-columns: auto 1fr auto auto auto;
  grid-template-areas: "score task hands time pause";
  pointer-events: none;
}
.pill {
  background: rgba(255,255,255,.92); border-radius: 999px; padding: 6px 14px;
  font-weight: 700; font-size: clamp(16px, 2.6vw, 24px); box-shadow: 0 4px 12px rgba(0,0,0,.15);
  white-space: nowrap;
}
.hud-score { grid-area: score; }
.hud-task {
  grid-area: task; justify-self: center; text-align: center; white-space: normal;
  background: var(--brand); color: #fff; font-size: clamp(17px, 3vw, 28px); padding: 6px 18px;
}
.hud-hands { grid-area: hands; }
.hud-time { grid-area: time; }
.hud-pause { grid-area: pause; border: 0; pointer-events: auto; min-width: 48px; min-height: 44px; }
.hud-time.low { background: #fee2e2; color: var(--bad); }

@media (max-width: 620px) {
  .hud {
    grid-template-columns: auto 1fr auto auto;
    grid-template-areas: "score hands time pause" "task task task task";
  }
  .hud-hands { justify-self: end; }
}

.toast {
  position: absolute; z-index: 3; left: 50%; bottom: calc(18px + var(--safe-b));
  transform: translate(-50%, 20px); opacity: 0; transition: opacity .2s, transform .2s;
  background: #fff; color: var(--ink); border-radius: 18px; padding: 10px 18px;
  font-size: clamp(16px, 3vw, 24px); font-weight: 600; box-shadow: 0 6px 18px rgba(0,0,0,.25);
  max-width: calc(100% - 32px); text-align: center; pointer-events: none;
  border: 3px solid var(--bad);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.overlay {
  position: absolute; inset: 0; z-index: 4; display: grid; place-items: center; padding: 16px;
  background: rgba(30, 27, 75, .35);
}
.overlay.clear { background: transparent; align-items: end; padding-bottom: calc(24px + var(--safe-b)); }
.panel {
  background: #fff; border-radius: var(--radius); padding: 20px; width: min(100%, 460px);
  text-align: center; box-shadow: 0 10px 40px rgba(0,0,0,.3);
}
.ov-icon { font-size: 52px; line-height: 1.1; }
.panel h2 { margin: 6px 0; font-size: 26px; }
.panel p { margin: 0 0 14px; color: var(--muted); font-size: 17px; line-height: 1.4; }
.buttons { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; }
.spinner {
  width: 44px; height: 44px; margin: 4px auto; border-radius: 50%;
  border: 5px solid #e0e7ff; border-top-color: var(--brand); animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Results ---------- */
.results { text-align: center; display: grid; gap: 10px; }
.results h2 { justify-content: center; font-size: 30px; margin: 0; }
.results h3 { margin: 8px 0 0; font-size: 18px; }
.results h3 small { font-weight: 400; color: var(--muted); }
.stars { font-size: clamp(48px, 13vw, 72px); letter-spacing: 4px; }
.stars .off { filter: grayscale(1); opacity: .3; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.stats div { background: var(--sel); border-radius: 16px; padding: 10px 4px; display: grid; }
.stats b { font-size: 30px; color: var(--brand); }
.stats span { font-size: 13px; color: var(--muted); }
.move { margin: 0; font-size: 17px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.chip {
  border: 2px solid #bbf7d0; background: #f0fdf4; border-radius: 999px; padding: 6px 14px;
  font-size: 18px; font-weight: 600; min-height: 44px;
}
.chips .empty { color: var(--muted); }
.oops { list-style: none; padding: 0; margin: 0; display: grid; gap: 6px; }
.oops li { background: #fef2f2; border-radius: 14px; padding: 8px 12px; font-size: 17px; }

/* ---------- Teacher dialog ---------- */
dialog {
  border: 0; border-radius: var(--radius); padding: 0; width: min(calc(100% - 32px), 520px);
  box-shadow: 0 20px 50px rgba(0,0,0,.35); color: var(--ink);
}
dialog::backdrop { background: rgba(30, 27, 75, .5); }
.custom-form { padding: 20px; display: grid; gap: 12px; }
.custom-form h2 { margin: 0; }
.custom-form .hint { margin: 0; color: var(--muted); }
.custom-form label { display: grid; gap: 4px; font-weight: 600; font-size: 15px; }
.custom-form input, .custom-form textarea {
  font: inherit; font-weight: 400; font-size: 17px; padding: 10px 12px; border-radius: 12px;
  border: 2px solid #e5e7eb; width: 100%; resize: vertical;
}
.custom-form input:focus, .custom-form textarea:focus { outline: none; border-color: var(--sel-border); }
.error { color: var(--bad); margin: 0; font-weight: 600; }

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
