:root {
  --bg: #f4f7ff;
  --card: #ffffff;
  --ink: #23264a;
  --muted: #6b7099;
  --line: #e2e6f5;
  --primary: #4d7cfe;
  --primary-dark: #3560d8;
  --ok: #2fb36b;
  --ok-bg: #e5f8ee;
  --bad: #ff5d5d;
  --bad-bg: #ffecec;
  --info-bg: #fff6d9;
  --star: #ffb800;
  --race: #ff7a45;
  --radius: 20px;
  --shadow: 0 6px 20px rgba(40, 60, 140, 0.10);
  --font: "Nunito", "Segoe UI", "Trebuchet MS", system-ui, -apple-system, sans-serif;
  /* per-topic accents */
  --c-addsub: #4d7cfe;
  --c-missing: #9b5de5;
  --c-word: #00a6a6;
  --c-smallest: #f15bb5;
  --c-order: #2fb36b;
  --c-words: #ff9f1c;
  --c-compare: #e76f51;
  --c-mix: #ff7a45;
  --accent: var(--primary);
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: var(--font);
  color: var(--ink);
  background:
    radial-gradient(circle at 10% 0%, #e3ebff 0, transparent 40%),
    radial-gradient(circle at 100% 100%, #ffeef7 0, transparent 40%),
    var(--bg);
  min-height: 100vh;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; }
a { color: inherit; }
h1 { font-size: clamp(1.6rem, 4vw, 2.3rem); margin: 0.2em 0; }
h2 { font-size: 1.3rem; margin: 1.5em 0 0.6em; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }

.t-addsub { --accent: var(--c-addsub); }
.t-missing { --accent: var(--c-missing); }
.t-word { --accent: var(--c-word); }
.t-smallest { --accent: var(--c-smallest); }
.t-order { --accent: var(--c-order); }
.t-words { --accent: var(--c-words); }
.t-compare { --accent: var(--c-compare); }
.t-mix { --accent: var(--c-mix); }

/* ---------- Top bar ---------- */
.topbar, .playbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 10px clamp(12px, 3vw, 28px);
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 800; text-decoration: none; font-size: 1.1rem; }
.logo { font-size: 1.5rem; }
.top-actions { margin-left: auto; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.lang-switch { display: flex; gap: 4px; }
.chip {
  border: 2px solid var(--line); background: #fff; border-radius: 999px;
  padding: 6px 12px; font-weight: 700; cursor: pointer; text-decoration: none; font-size: 0.9rem;
  transition: transform .1s, border-color .15s;
}
.chip:hover { border-color: var(--primary); }
.chip:active { transform: scale(0.95); }
.chip.active { background: var(--primary); border-color: var(--primary); color: #fff; }
.chip.icon { padding: 6px 10px; }

/* ---------- Layout ---------- */
.page { max-width: 980px; margin: 0 auto; padding: 20px clamp(12px, 3vw, 28px) 60px; }
.hero { text-align: center; margin: 10px 0 24px; }
.hero p { color: var(--muted); font-size: 1.1rem; margin: 0; }
.back { display: inline-block; margin-bottom: 8px; color: var(--muted); text-decoration: none; font-weight: 700; }
.back:hover { color: var(--ink); }
.warning { background: var(--info-bg); padding: 10px 14px; border-radius: 12px; }

/* ---------- Topic cards ---------- */
.topic-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 16px; }
.topic-card {
  position: relative; display: flex; flex-direction: column; gap: 6px;
  padding: 18px; border-radius: var(--radius); background: var(--card);
  box-shadow: var(--shadow); text-decoration: none;
  border-top: 6px solid var(--accent);
  transition: transform .15s, box-shadow .15s;
}
.topic-card:hover { transform: translateY(-4px); box-shadow: 0 12px 28px rgba(40, 60, 140, 0.16); }
.topic-card:focus-visible, .btn:focus-visible, .chip:focus-visible, .choice:focus-visible, .tile:focus-visible, .key:focus-visible, .blank:focus-visible {
  outline: 3px solid var(--primary); outline-offset: 3px;
}
.topic-icon { font-size: 2.2rem; }
.topic-icon.big { font-size: 3.4rem; }
.topic-name { font-weight: 800; font-size: 1.15rem; }
.topic-desc { color: var(--muted); font-size: 0.92rem; flex: 1; }
.topic-card.race { background: linear-gradient(135deg, #fff3ec, #ffe1d1); }

.stars { display: inline-flex; gap: 2px; color: #cfd4ea; font-size: 1.4rem; line-height: 1; }
.stars.small { font-size: 1rem; }
.stars.big { font-size: 3rem; margin: 6px 0; }
.star.on { color: var(--star); text-shadow: 0 1px 0 #d99a00; }
.stars.big .star.on { animation: pop .5s both; }
.stars.big .star:nth-child(2) { animation-delay: .15s; }
.stars.big .star:nth-child(3) { animation-delay: .3s; }

/* ---------- Topic page ---------- */
.topic-hero { display: flex; align-items: center; gap: 16px; }
.topic-hero p { margin: 0; color: var(--muted); }
.level-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 16px; }
.level-card {
  background: var(--card); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 10px; border: 3px solid transparent;
}
.level-card:not(.locked):hover { border-color: var(--accent); }
.level-card.locked { opacity: 0.6; background: #f0f2fa; }
.level-head { display: flex; align-items: center; gap: 12px; }
.level-num {
  width: 46px; height: 46px; border-radius: 50%; display: grid; place-items: center;
  background: var(--accent); color: #fff; font-weight: 800; font-size: 1.4rem;
}
.level-title { font-weight: 800; font-size: 1.1rem; }
.level-sub, .level-best { color: var(--muted); font-size: 0.9rem; }
.level-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: auto; }
.lock { font-weight: 800; }
.hint { margin: 0; font-size: 0.85rem; color: var(--muted); }

/* ---------- Buttons ---------- */
.btn {
  border: 0; border-radius: 14px; padding: 12px 18px; font-weight: 800; cursor: pointer;
  background: #eef1fb; text-decoration: none; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  box-shadow: 0 3px 0 rgba(0, 0, 0, 0.08); transition: transform .08s, filter .15s;
}
.btn:hover { filter: brightness(0.97); }
.btn:active { transform: translateY(2px); box-shadow: none; }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn.primary { background: var(--accent); color: #fff; box-shadow: 0 4px 0 rgba(0, 0, 0, 0.18); }
.btn.race-btn { background: var(--race); color: #fff; box-shadow: 0 4px 0 rgba(0, 0, 0, 0.18); }
.btn.ghost { background: transparent; box-shadow: none; border: 2px solid var(--line); }
.btn.danger { background: var(--bad-bg); color: #b42318; margin-top: 30px; }
.btn.big { font-size: 1.2rem; padding: 14px 28px; min-width: 180px; }
.level-actions .btn { flex: 1; }

/* ---------- Play ---------- */
.playbar { gap: 10px 16px; }
.play-title { font-weight: 800; }
.score { margin-left: auto; font-weight: 800; font-size: 1.2rem; background: #fff7d6; padding: 4px 12px; border-radius: 999px; }
.dots { display: flex; gap: 5px; flex-wrap: wrap; }
.dot { width: 14px; height: 14px; border-radius: 50%; background: var(--line); }
.dot.current { background: var(--accent); box-shadow: 0 0 0 3px rgba(77, 124, 254, 0.25); }
.dot.ok { background: var(--ok); }
.dot.retry { background: var(--star); }
.dot.bad { background: var(--bad); }
.timer { display: flex; align-items: center; gap: 8px; min-width: 180px; flex: 1; max-width: 360px; }
.time-left { font-weight: 800; font-size: 1.3rem; min-width: 2ch; text-align: right; font-variant-numeric: tabular-nums; }
.time-bar { flex: 1; height: 14px; border-radius: 999px; background: var(--line); overflow: hidden; }
.time-fill { height: 100%; background: linear-gradient(90deg, #2fb36b, #8bd346); border-radius: 999px; transition: width .2s linear; }
.time-fill.hurry { background: linear-gradient(90deg, #ff5d5d, #ff9f43); animation: pulse 0.8s infinite; }

.play { display: flex; flex-direction: column; align-items: center; gap: 18px; }
.task-card {
  width: 100%; max-width: 720px; background: var(--card); border-radius: 28px; box-shadow: var(--shadow);
  padding: clamp(18px, 4vw, 34px); text-align: center; border-top: 8px solid var(--accent);
}
.task-card.enter { animation: slideIn .3s ease-out; }
.task-card.shake { animation: shake .35s; }
.prompt { font-size: 1.15rem; font-weight: 700; color: var(--muted); margin: 0 0 18px; }
.story {
  font-size: 1.25rem; line-height: 1.5; text-align: left; background: #f3fbfb; border-left: 6px solid var(--accent);
  padding: 14px 18px; border-radius: 12px; margin: 0 0 20px;
}
.task { display: flex; flex-direction: column; gap: 16px; align-items: center; }
.eq-line { display: flex; align-items: center; justify-content: center; gap: 12px; flex-wrap: wrap; }
.eq-line.sentence .eq-tokens { font-size: 1.2rem; }
.eq-label { font-weight: 800; color: var(--accent); min-width: 6em; text-align: right; }
.eq-tokens { display: flex; align-items: center; justify-content: center; gap: 8px; flex-wrap: wrap; font-size: clamp(1.8rem, 6vw, 2.8rem); font-weight: 800; }
.tok.op { color: var(--accent); }
.tok.text { font-weight: 600; }
.tok.words {
  font-size: clamp(1.4rem, 4.5vw, 2rem); background: #fff4e0; padding: 8px 18px; border-radius: 14px;
}
.blank {
  min-width: 2.4em; height: 1.6em; padding: 0 0.3em; border-radius: 14px; border: 3px dashed #b8c0e0;
  background: #f7f8ff; font-weight: 800; font-size: 1em; cursor: pointer; display: inline-grid; place-items: center;
  font-variant-numeric: tabular-nums;
}
.eq-line.sentence .blank { font-size: 1.6rem; }
.blank.op-blank { min-width: 1.6em; }
.blank.active { border-style: solid; border-color: var(--accent); background: #fff; box-shadow: 0 0 0 5px rgba(77, 124, 254, 0.18); animation: glow 1.2s infinite alternate; }
.blank.ok { border: 3px solid var(--ok); background: var(--ok-bg); color: #147a45; }
.blank.revealed { border: 3px solid var(--star); background: #fff6d9; color: #8a5a00; }
.blank:disabled { cursor: default; }
.sign-slot { min-width: 1.8em; cursor: default; }

.choices { display: flex; gap: 12px; justify-content: center; flex-wrap: wrap; }
.choices.column { flex-direction: column; align-items: stretch; width: 100%; max-width: 480px; }
.choice {
  min-width: 90px; padding: 14px 20px; border-radius: 18px; border: 3px solid var(--line); background: #fff;
  font-size: 1.8rem; font-weight: 800; cursor: pointer; display: flex; flex-direction: column; align-items: center;
  box-shadow: 0 4px 0 var(--line); transition: transform .08s, border-color .15s;
}
.choice:hover:not(:disabled) { border-color: var(--accent); transform: translateY(-2px); }
.choice:active:not(:disabled) { transform: translateY(2px); box-shadow: none; }
.choice.c-words { font-size: 1.3rem; }
.choice.c-sign { min-width: 110px; font-size: 2.4rem; }
.choice-sub { font-size: 0.8rem; color: var(--muted); font-weight: 700; }
.choice.wrong { border-color: var(--bad); background: var(--bad-bg); opacity: 0.7; text-decoration: line-through; }
.choice.right { border-color: var(--ok); background: var(--ok-bg); animation: pop .35s; }
.choice:disabled { cursor: default; }

.tile-row { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; min-height: 64px; }
.answer-row { padding: 12px; border-radius: 18px; background: #f3f5fd; width: 100%; }
.tile {
  min-width: 64px; height: 56px; padding: 0 14px; border-radius: 14px; border: 3px solid var(--line); background: #fff;
  font-size: 1.6rem; font-weight: 800; cursor: pointer; display: inline-grid; place-items: center; box-shadow: 0 4px 0 var(--line);
  animation: pop .2s;
}
.words .tile { font-size: 1.2rem; }
.tile:hover:not(:disabled) { border-color: var(--accent); }
.tile.placed { border-color: var(--accent); background: #eef3ff; }
.tile.placed.ok { border-color: var(--ok); background: var(--ok-bg); }
.tile.empty { border-style: dashed; background: transparent; box-shadow: none; cursor: default; animation: none; }
.tile.revealed { border-color: var(--star); background: #fff6d9; cursor: default; }
.solution::before { content: "💡"; font-size: 1.6rem; align-self: center; }

.feedback { min-height: 2.2em; margin: 16px 0 6px; font-size: 1.25rem; font-weight: 800; border-radius: 12px; padding: 6px; }
.feedback.ok { color: #147a45; background: var(--ok-bg); animation: pop .3s; }
.feedback.bad { color: #b42318; background: var(--bad-bg); }
.feedback.info { color: #8a5a00; background: var(--info-bg); }
.actions { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }

.keypad-wrap { display: flex; flex-direction: column; gap: 10px; width: 100%; max-width: 340px; }
.keypad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.op-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.key {
  height: 60px; border-radius: 16px; border: 0; background: #fff; font-size: 1.7rem; font-weight: 800; cursor: pointer;
  box-shadow: 0 4px 0 var(--line), var(--shadow); transition: transform .06s;
}
.key:active:not(:disabled) { transform: translateY(3px); box-shadow: none; }
.key.fn { background: #eef1fb; }
.key.op-key { background: #f3ecff; color: var(--c-missing); font-size: 2rem; }
.key.op-key:disabled { opacity: 0.45; }
.key:disabled { opacity: 0.35; cursor: default; }

.countdown-wrap { text-align: center; margin-top: 40px; }
.rules { font-size: 1.2rem; color: var(--muted); max-width: 520px; }
.countdown { font-size: 9rem; font-weight: 900; color: var(--race); animation: pop .8s infinite; }

/* ---------- Result ---------- */
.result { display: flex; justify-content: center; }
.result-card { background: var(--card); border-radius: 28px; padding: 30px; box-shadow: var(--shadow); text-align: center; max-width: 560px; width: 100%; }
.result-card.enter { animation: slideIn .4s ease-out; }
.result-icon { font-size: 4rem; }
.result-score { font-size: 1.5rem; font-weight: 800; margin: 8px 0; }
.result-msg { font-size: 1.15rem; color: var(--muted); }
.badge { display: inline-block; background: #fff1c2; padding: 8px 16px; border-radius: 999px; font-weight: 800; margin: 4px; animation: pop .5s; }
.badge.unlock { background: var(--ok-bg); }
.actions.wrap { margin-top: 16px; }

/* ---------- Stats ---------- */
.stat-tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin: 14px 0; }
.stat { background: var(--card); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); display: flex; flex-direction: column; }
.stat-val { font-size: 2rem; font-weight: 900; }
.stat-label { color: var(--muted); font-weight: 700; }
.table-wrap { overflow-x: auto; background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); }
.progress-table { width: 100%; border-collapse: collapse; }
.progress-table th, .progress-table td { padding: 10px 12px; border-bottom: 1px solid var(--line); text-align: center; }
.progress-table tbody th { text-align: left; font-weight: 700; white-space: nowrap; }
.progress-table tr:last-child th, .progress-table tr:last-child td { border-bottom: 0; }
.history { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 8px; }
.history li { background: var(--card); border-radius: 14px; padding: 10px 14px; display: flex; gap: 12px; align-items: center; flex-wrap: wrap; box-shadow: var(--shadow); }
.history li strong { margin-left: auto; }
.pill { background: #eef1fb; padding: 2px 10px; border-radius: 999px; font-size: 0.85rem; font-weight: 700; }

/* ---------- Confetti & animations ---------- */
.confetti { position: fixed; inset: 0; pointer-events: none; overflow: hidden; z-index: 50; }
.confetti span { position: absolute; top: -20px; width: 10px; height: 16px; border-radius: 3px; animation: fall linear forwards; }
@keyframes fall { to { top: 110vh; transform: rotate(720deg) translateX(40px); } }
@keyframes pop { 0% { transform: scale(0.6); opacity: 0; } 70% { transform: scale(1.1); opacity: 1; } 100% { transform: scale(1); } }
@keyframes slideIn { from { transform: translateY(16px); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes shake { 20%, 60% { transform: translateX(-8px); } 40%, 80% { transform: translateX(8px); } }
@keyframes glow { to { box-shadow: 0 0 0 9px rgba(77, 124, 254, 0.08); } }
@keyframes pulse { 50% { opacity: 0.6; } }

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

@media (max-width: 560px) {
  .topbar { justify-content: center; }
  .top-actions { margin-left: 0; justify-content: center; }
  .playbar .play-title { display: none; }
  .eq-label { min-width: 0; width: 100%; text-align: center; }
  .key { height: 54px; }
}
