:root {
  --bg: #faf9f7; --card: #ffffff; --ink: #262322; --muted: #6a6561; --line: #e4e0db;
  --accent: #8f0838; --accent-ink: #ffffff; --good: #138a4b; --good-bg: #e3f6ec; --bad: #b42318; --bad-bg: #fdecea;
  --hl: #ffd84d; --hl-ink: #1b2233; --shadow: 0 2px 10px rgba(20, 30, 60, .08);
  --metal: #3577c9; --nonmetal: #c85f34; --metalloid: #6a4bc4; --multi: #2b8aa8; --poly: #ad3767;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #11151f; --card: #1b2130; --ink: #eef1f8; --muted: #a3acc2; --line: #2d3548;
    --accent: #d0436f; --accent-ink: #ffffff; --good: #4fd18b; --good-bg: #173326; --bad: #ff8a7a; --bad-bg: #3a1d1a;
    --hl: #ffd84d; --hl-ink: #11151f; --shadow: 0 2px 10px rgba(0, 0, 0, .35);
  }
}
:root[data-theme="dark"] {
  --bg: #11151f; --card: #1b2130; --ink: #eef1f8; --muted: #a3acc2; --line: #2d3548;
  --accent: #d0436f; --accent-ink: #ffffff; --good: #4fd18b; --good-bg: #173326; --bad: #ff8a7a; --bad-bg: #3a1d1a;
  --hl: #ffd84d; --hl-ink: #11151f; --shadow: 0 2px 10px rgba(0, 0, 0, .35);
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--ink); }
body { font: 17px/1.45 "IBM Plex Sans", system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; -webkit-text-size-adjust: 100%; min-height: 100dvh; }
button { font: inherit; color: inherit; cursor: pointer; -webkit-tap-highlight-color: transparent; touch-action: manipulation; }
input { font: inherit; font-size: 18px; } /* ≥16px stops iOS zooming */
.wrap { max-width: 620px; margin: 0 auto; padding: 12px 16px 40px; }
.hidden { display: none !important; }
h1 { font-family: "DM Serif Display", Georgia, serif; font-weight: 400; font-size: 1.9rem; margin: .2em 0 .1em; letter-spacing: -.01em; }
h2 { font-size: 1.2rem; margin: 0 0 .4em; }
.sub { color: var(--muted); margin: 0 0 1em; }
.card { background: var(--card); border-radius: 18px; box-shadow: var(--shadow); padding: 18px; margin: 12px 0; border: 1px solid var(--line); }
.btn { display: block; width: 100%; border: 0; border-radius: 14px; padding: 14px 16px; font-weight: 700; font-size: 1.05rem; background: var(--accent); color: var(--accent-ink); min-height: 52px; }
.btn.secondary { background: transparent; color: var(--accent); border: 2px solid var(--accent); }
.btn.ghost { background: transparent; color: var(--muted); font-weight: 600; min-height: 40px; padding: 8px; }
.btn:disabled { opacity: .45; }
.row { display: flex; gap: 10px; }
.row > * { flex: 1; }
label.field { display: block; font-weight: 600; margin: 10px 0 4px; }
.text { width: 100%; padding: 12px 14px; border-radius: 12px; border: 2px solid var(--line); background: var(--bg); color: var(--ink); }
.text:focus { outline: none; border-color: var(--accent); }
.msg { min-height: 1.4em; color: var(--bad); font-size: .95rem; margin: 8px 0; }
.brand a { color: inherit; text-decoration: none; }
.brand { color: var(--muted); font-size: .85rem; text-transform: uppercase; letter-spacing: .08em; font-weight: 700; }

/* Menu */
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 4px; }
.who { font-weight: 700; }
.who small { color: var(--muted); font-weight: 500; }
.section-btn { width: 100%; text-align: left; border: 0; background: none; padding: 0; display: flex; align-items: center; gap: 12px; }
.section-btn .dot { width: 44px; height: 44px; border-radius: 12px; display: grid; place-items: center; font-weight: 800; color: #fff; flex: none; font-size: 1.2rem; }
.section-btn h2 { margin: 0; }
.section-btn p { margin: 0; color: var(--muted); font-size: .92rem; }
.game-list { list-style: none; margin: 12px 0 0; padding: 0; }
.game-list li + li { border-top: 1px solid var(--line); }
.game-row { width: 100%; text-align: left; background: none; border: 0; padding: 12px 4px; display: flex; gap: 12px; align-items: center; min-height: 52px; }
.game-row .num { font-weight: 800; color: var(--accent); min-width: 2.4em; }
.game-row .pr { margin-left: auto; color: var(--muted); font-size: .85rem; white-space: nowrap; }

/* Sheet */
.sheet-bg { position: fixed; inset: 0; background: rgba(0, 0, 0, .45); display: flex; align-items: flex-end; justify-content: center; z-index: 20; }
.sheet { background: var(--card); width: 100%; max-width: 620px; border-radius: 22px 22px 0 0; padding: 20px 16px calc(20px + env(safe-area-inset-bottom)); }
@media (min-width: 700px) { .sheet-bg { align-items: center; } .sheet { border-radius: 22px; } }
.sheet .btn + .btn { margin-top: 10px; }
.check { display: flex; gap: 10px; align-items: center; margin: 12px 0; color: var(--muted); }
.check input { width: 22px; height: 22px; }

/* Play */
.playbar { display: flex; align-items: center; gap: 10px; min-height: 48px; }
.icon-btn { border: 0; background: var(--card); border-radius: 12px; width: 44px; height: 44px; font-size: 1.3rem; box-shadow: var(--shadow); flex: none; }
.playbar .title { font-weight: 700; font-size: .95rem; flex: 1; line-height: 1.2; }
.score { font-variant-numeric: tabular-nums; font-weight: 700; color: var(--muted); }
.timer { font-variant-numeric: tabular-nums; font-weight: 800; display: flex; align-items: center; gap: 6px; }
.timer svg { width: 28px; height: 28px; transform: rotate(-90deg); }
.streak-zone { height: 42px; display: flex; justify-content: center; align-items: center; gap: 10px; }
.streak { background: var(--good); color: #fff; font-weight: 900; border-radius: 999px; padding: 4px 14px; font-size: 1.15rem; animation: bump .35s ease-out; }
:root[data-theme="dark"] .streak, .streak { color: #fff; }
@media (prefers-color-scheme: dark) { .streak { background: #1f9d5c; } }
.streak-time { color: var(--good); font-weight: 700; font-variant-numeric: tabular-nums; }
@keyframes bump { 0% { transform: scale(.6); } 60% { transform: scale(1.15); } 100% { transform: scale(1); } }

.qcard { position: relative; overflow: hidden; text-align: center; padding: 22px 16px; min-height: 180px; display: flex; flex-direction: column; justify-content: center; }
.q-label { font-size: 1.1rem; color: var(--muted); }
.q-label b { color: var(--ink); }
.q-main { font-size: 2.6rem; font-weight: 800; margin-top: 6px; line-height: 1.25; word-break: break-word; }
.q-namebig { font-size: 1.7rem; }
.q-context { font-size: 2rem; font-weight: 800; margin-top: 4px; }
.big-name { font-size: 2.1rem; }
.formula sub, .formula sup, sup.charge { font-size: .62em; line-height: 0; }
.formula sub { vertical-align: -0.25em; }
.formula sup, sup.charge { vertical-align: 0.75em; }
.hl { background: var(--hl); color: var(--hl-ink); border-radius: 4px; box-shadow: 0 0 0 2px var(--hl); }
.hl-text { background: var(--hl); color: var(--hl-ink); border-radius: 4px; padding: 0 .25em; }
.charge-stage { font-size: 3.4rem; margin-top: 12px; }
sup.charge.q { color: var(--muted); }
sup.charge.ans { color: var(--good); display: inline-block; }
sup.charge.pop { animation: pop .35s cubic-bezier(.2, .9, .3, 1.4); }
sup.charge.reveal { color: var(--bad); display: inline-block; }
@keyframes pop { 0% { transform: scale(.4); opacity: .3; } 55% { transform: scale(1.6); opacity: 1; } 100% { transform: scale(1); } }

.answers { margin-top: 14px; }
.choices { display: grid; gap: 12px; }
.choices.two { grid-template-columns: 1fr 1fr; }
.choices.many { grid-template-columns: 1fr 1fr; }
.choices.many .choice:last-child:nth-child(odd) { grid-column: 1 / -1; }
.choice { border: 0; border-radius: 18px; padding: 16px 10px; min-height: 72px; font-weight: 800; font-size: 1.15rem; color: #fff; box-shadow: 0 4px 0 rgba(0, 0, 0, .18); position: relative; overflow-wrap: anywhere; min-width: 0; }
.choices.two .choice { min-height: 110px; font-size: 1.35rem; }
.choice:active { transform: translateY(2px); box-shadow: 0 2px 0 rgba(0, 0, 0, .18); }
.choice.c0 { background: var(--metal); } .choice.c1 { background: var(--nonmetal); } .choice.c2 { background: var(--metalloid); }
.choice.c3 { background: var(--multi); } .choice.c4 { background: var(--poly); }
.choice.right, .ch-btn.right, .key.right { outline: 4px solid var(--good); outline-offset: 3px; }
.choice.wrong, .ch-btn.wrong { opacity: .5; }
.choice .mark, .ch-btn .mark { position: absolute; top: 4px; right: 8px; font-size: 1rem; }

.charges { display: grid; grid-template-columns: repeat(9, 1fr); gap: 6px; }
.ch-btn { border: 0; border-radius: 12px; min-height: 58px; font-weight: 900; font-size: 1.1rem; color: #fff; text-shadow: 0 1px 2px rgba(0, 0, 0, .35); position: relative; box-shadow: 0 3px 0 rgba(0, 0, 0, .18); }
@media (max-width: 480px) {
  .charges { grid-template-columns: repeat(4, 1fr); }
  .charges .zero { grid-column: 1 / -1; min-height: 46px; }
}

.answer-box { display: flex; gap: 8px; }
.answer-box .text { flex: 1; font-size: 1.25rem; font-weight: 700; }
.keypad { display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px; margin-top: 8px; }
.key { border: 1px solid var(--line); background: var(--card); border-radius: 10px; min-height: 46px; font-size: 1.25rem; font-weight: 700; box-shadow: 0 2px 0 var(--line); }
.key.wide { grid-column: span 2; }
.key.subkey { font-size: 2rem; line-height: 1; }
.key .tiny { font-size: .8rem; font-weight: 700; }
.key.on { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.key small { font-size: .7rem; font-weight: 600; }
.numpad { grid-template-columns: repeat(3, 1fr); max-width: 340px; margin: 10px auto 0; }
.numpad .key { min-height: 56px; font-size: 1.4rem; }
.num-display { text-align: center; font-size: 2rem; font-weight: 800; min-height: 1.5em; border-bottom: 3px solid var(--line); max-width: 180px; margin: 0 auto; font-variant-numeric: tabular-nums; }
.check-btn { margin-top: 10px; }

.feedback { border-radius: 14px; padding: 12px 14px; margin-top: 12px; font-size: 1rem; text-align: left; }
.feedback.good { background: var(--good-bg); color: var(--good); font-weight: 700; text-align: center; }
.feedback.bad { background: var(--bad-bg); color: var(--ink); }
.feedback .label { color: var(--bad); font-weight: 800; }
.feedback.good.hold .label { color: var(--good); }
.feedback.good.hold .why { font-weight: 400; }
.feedback .correct { font-size: 1.4rem; font-weight: 800; margin: 4px 0; color: var(--ink); }
.feedback .hint { font-weight: 600; }
.feedback .why { color: var(--muted); font-size: .95rem; margin-top: 4px; }
.feedback .btn { margin-top: 10px; }

/* Personal record: rainbow glow */
.pr-pop { position: fixed; left: 50%; top: 30%; transform: translate(-50%, -50%); z-index: 30; text-align: center; pointer-events: none; background: rgba(12, 10, 24, .92); border-radius: 24px; padding: 18px 28px; box-shadow: 0 0 30px rgba(255, 200, 80, .55), 0 0 0 3px rgba(255, 255, 255, .15); white-space: nowrap; animation: prIn 2.4s ease-out forwards; }
.pr-pop .t1 { font-weight: 900; font-size: 1.3rem; letter-spacing: .12em; }
.pr-pop .t2 { font-weight: 900; font-size: 3.4rem; font-variant-numeric: tabular-nums; }
.pr-pop .t1, .pr-pop .t2 {
  background: linear-gradient(90deg, #ff3b3b, #ff9f1a, #ffe11a, #3bd16f, #1aa3ff, #8b5cff, #ff3bd0, #ff3b3b);
  background-size: 200% 100%; -webkit-background-clip: text; background-clip: text; color: transparent;
  animation: rainbow .6s linear infinite; filter: drop-shadow(0 0 10px rgba(255, 255, 255, .7)) drop-shadow(0 0 18px rgba(255, 180, 0, .6));
}
@keyframes rainbow { to { background-position: 200% 0; } }
@keyframes prIn { 0% { opacity: 0; transform: translate(-50%, -50%) scale(.5); } 12% { opacity: 1; transform: translate(-50%, -50%) scale(1.1); } 20% { transform: translate(-50%, -50%) scale(1); } 85% { opacity: 1; } 100% { opacity: 0; } }

/* Summary */
.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.stat { background: var(--bg); border-radius: 14px; padding: 12px; text-align: center; }
.stat b { display: block; font-size: 1.6rem; }
.stat span { color: var(--muted); font-size: .85rem; }
.missed { list-style: none; padding: 0; margin: 0; }
.missed li { padding: 10px 0; border-top: 1px solid var(--line); display: flex; justify-content: space-between; gap: 10px; align-items: baseline; flex-wrap: wrap; }
.missed .q { font-weight: 700; }
.missed .a { color: var(--good); font-weight: 700; }
.foot { text-align: center; color: var(--muted); font-size: .8rem; margin-top: 24px; }
.foot a { color: inherit; }
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation-duration: .01ms !important; } .pr-pop { animation: none !important; } }

/* Correct answer: the question card glows green briefly, with a small check in its corner */
.qcard.ok-glow { animation: okGlow .7s ease-out; }
@keyframes okGlow { 0% { box-shadow: 0 0 0 0 var(--good); background: var(--card); }
  20% { box-shadow: 0 0 0 4px var(--good); background: color-mix(in srgb, var(--good-bg) 60%, var(--card)); }
  100% { box-shadow: var(--shadow); background: var(--card); } }
.flash-ok { position: absolute; top: 10px; right: 12px; width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center;
  background: var(--good); color: #fff; font-size: 1.1rem; font-weight: 700; pointer-events: none; animation: okBadge .25s ease-out; }
@keyframes okBadge { from { opacity: 0; transform: scale(.5); } to { opacity: 1; transform: scale(1); } }

/* About section (for search engines and curious visitors) */
.about { margin-top: 72px; padding-top: 40px; border-top: 1px solid var(--line); position: relative; color: var(--muted); font-size: .93rem; }
.about::before { content: "About this game"; position: absolute; top: 0; left: 50%; transform: translate(-50%, -50%); background: var(--bg); padding: 0 14px;
  color: var(--muted); font-size: .72rem; font-weight: 700; letter-spacing: .14em; text-transform: uppercase; white-space: nowrap; }
.about h2 { color: var(--ink); font-family: "DM Serif Display", Georgia, serif; font-weight: 400; font-size: 1.3rem; margin-bottom: .5em; }
.about h3 { color: var(--ink); font-size: 1.02rem; margin: 1.2em 0 .3em; }
.about ul { padding-left: 1.2em; margin: 0; }
.about li { margin: .2em 0; }
.about .cta { margin-top: 1.6em; padding: 16px; border-radius: 16px; border: 1px solid var(--line); background: var(--card); }
.about .cta p { margin: 0 0 10px; color: var(--ink); }
.about .cta .btn { text-decoration: none; text-align: center; }
.foot a { color: var(--accent); }
.aq { font-size: .5em; font-weight: 600; color: var(--muted); margin-left: .1em; }
/* Worked example shown before a level starts (games that provide one) */
.sheet { max-height: 92vh; overflow-y: auto; }
.intro { border: 1px solid var(--line); border-radius: 14px; padding: 10px 14px; margin: 0 0 14px; background: var(--bg); }
.intro-h { font-size: .75rem; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; color: var(--accent); margin-bottom: 4px; }
.intro p { margin: 4px 0; }
.intro p:last-child { color: var(--muted); font-size: .92rem; }
/* Mastered tick on the game menu (90% or better on the last 20 first tries, the teacher console's rule). */
.game-row .mastered { margin-left: auto; color: #fff; background: #1f9d5c; border-radius: 999px; width: 24px; height: 24px; display: grid; place-items: center; font-size: .8rem; font-weight: 800; flex: none; }
.game-row .mastered + .pr { margin-left: 8px; }
