:root {
  --bg: #f1edf7;
  --surface: #fbfaff;
  --surface-2: #efeaf7;
  --text: #241f33;
  --muted: #6f6685;
  --border: #ddd5ea;
  --accent: #7c3aed;        /* violet */
  --accent-2: #c4b5fd;      /* soft lilac highlight */
  --correct: #1f9d63;
  --correct-bg: #e2f4ea;
  --correct-border: #9ad9bb;
  --wrong: #d23b57;
  --wrong-bg: #fbe6ea;
  --wrong-border: #eeaab6;
  --shadow: 0 18px 40px rgba(60, 30, 110, 0.18);
  --gold-bg: rgba(196, 181, 253, 0.32);
  --gold-border: #a78bfa;
  --gold-text: #5b3aa6;
  /* Difficulty bands: Low green, Medium yellow, High red, Extreme purple */
  --diff-l-fg: #1f7a4d; --diff-l-bg: #e2f4ea; --diff-l-bd: #9ad9bb;
  --diff-m-fg: #946400; --diff-m-bg: #fbf0d2; --diff-m-bd: #eccf86;
  --diff-h-fg: #c0263f; --diff-h-bg: #fbe6ea; --diff-h-bd: #eeaab6;
  /* Extreme: solid vibrant purple fill, white text — same in both themes */
  --diff-e-fg: #ffffff; --diff-e-bg: #6d28d9; --diff-e-bd: #5b21b6;
}
[data-theme="dark"] {
  --bg: #161320;
  --surface: #211c30;
  --surface-2: #2b2440;
  --text: #ece8f6;
  --muted: #a99fc2;
  --border: #3a3252;
  --accent: #a78bfa;
  --accent-2: #d6c9ff;
  --correct: #45c98a;
  --correct-bg: #18352a;
  --correct-border: #2f6b50;
  --wrong: #f0788d;
  --wrong-bg: #3a1f28;
  --wrong-border: #7a3f4d;
  --shadow: 0 18px 40px rgba(0, 0, 0, 0.5);
  --gold-bg: rgba(167, 139, 250, 0.22);
  --gold-border: #8b6fd6;
  --gold-text: #d6c9ff;
  --diff-l-fg: #5fd39b; --diff-l-bg: #163027; --diff-l-bd: #2f6b50;
  --diff-m-fg: #f1cf6e; --diff-m-bg: #352b10; --diff-m-bd: #6f5a1e;
  --diff-h-fg: #f0788d; --diff-h-bg: #371d25; --diff-h-bd: #7a3f4d;
  --diff-e-fg: #ffffff; --diff-e-bg: #6d28d9; --diff-e-bd: #5b21b6;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  min-height: 100vh;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  transition: background 0.25s ease, color 0.25s ease;
}

.hidden { display: none !important; }

/* Top bar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 22px;
  max-width: 1000px;
  margin: 0 auto;
}
.brand { display: flex; align-items: center; gap: 10px; font-size: 1.15rem; }
.logo {
  display: inline-grid; place-items: center;
  width: 34px; height: 34px; border-radius: 9px;
  background: var(--accent); color: var(--accent-2);
  font-size: 1.2rem; font-weight: 800;
}
.brand-text { letter-spacing: 0.2px; font-weight: 400; }
.brand-text strong { font-weight: 800; }
.theme-toggle {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 10px;
  width: 40px; height: 40px;
  font-size: 1.1rem; cursor: pointer;
}
.theme-toggle:hover { border-color: var(--accent); }

.app { max-width: 1000px; margin: 0 auto; padding: 8px 22px 60px; }

/* Cards */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  box-shadow: var(--shadow);
}

/* Start screen */
.start-card { max-width: 720px; margin: 4vh auto 0; padding: 34px; }
.start-card h1 { margin: 0 0 8px; font-size: 1.7rem; line-height: 1.2; }
.lead { color: var(--muted); margin: 0 0 22px; line-height: 1.5; }
.start-form label { font-size: 0.82rem; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); }
.start-row { display: flex; gap: 10px; margin-top: 8px; }
#nameInput {
  flex: 1;
  padding: 13px 15px; font-size: 1.05rem;
  border-radius: 12px; border: 2px solid var(--border);
  background: var(--surface-2); color: var(--text); outline: none;
}
#nameInput:focus { border-color: var(--accent); }

/* Question-count chooser — tap a circle to start. Circle AREA is proportional to
   the number (so diameter scales with sqrt of the value). Deepening purples. */
.count-label { display: block; margin-top: 22px; text-align: center; }
.count-choose {
  display: flex; gap: 26px; margin-top: 16px;
  justify-content: center;
  align-items: center;
}
.count-btn {
  border-radius: 50%;
  border: none;
  color: #f4f1fb;
  font-weight: 800;
  cursor: pointer;
  display: grid; place-items: center;
  box-shadow: 0 8px 22px rgba(50, 20, 90, 0.3);
  transition: transform 0.1s ease, filter 0.15s ease;
}
.count-btn:hover { transform: translateY(-3px); filter: brightness(1.08); }
.count-btn:active { transform: translateY(0); }

/* sqrt(n) * 22 -> diameters ~54 / 76 / 108, areas in ratio 1 : 2 : 4 */
.count-btn[data-count="6"]  { width: 54px;  height: 54px;  font-size: 1.05rem; background: #9b8bc4; }
.count-btn[data-count="12"] { width: 76px;  height: 76px;  font-size: 1.5rem;  background: #6d4c9f; }
.count-btn[data-count="24"] { width: 108px; height: 108px; font-size: 2.1rem;  background: #4c2c70; }

/* Global leaderboard */
.leaderboard { margin-top: 22px; border-top: 1px solid var(--border); padding-top: 16px; }
.lb-head { font-weight: 700; display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.lb-sub { font-size: 0.78rem; color: var(--muted); font-weight: 400; }
.lb-cols { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-top: 14px; }
.lb-col { min-width: 0; }
.lb-colhead { display: flex; justify-content: center; margin-bottom: 8px; }
.lb-badge {
  width: 34px; height: 34px; border-radius: 50%;
  display: grid; place-items: center; color: #fff; font-weight: 800; font-size: 0.95rem;
}
.lb-badge.c6 { background: #9b8bc4; }
.lb-badge.c12 { background: #6d4c9f; }
.lb-badge.c24 { background: #4c2c70; }
.lb-list { list-style: none; margin: 0; padding: 0; }
.lb-list li {
  display: flex; align-items: center; gap: 6px;
  padding: 5px 7px; border-radius: 7px; background: var(--surface-2);
  margin-bottom: 4px; font-size: 0.8rem; min-width: 0;
}
.lb-rank { flex: 0 0 auto; width: 1.5em; text-align: center; font-weight: 800; font-variant-numeric: tabular-nums; }
.lb-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lb-score { flex: 0 0 auto; font-variant-numeric: tabular-nums; font-weight: 700; color: var(--accent); font-size: 0.76rem; }
.lb-list li.me { background: var(--gold-bg); border: 1px solid var(--gold-border); font-weight: 800; }
.lb-list li.lb-gap { height: 8px; padding: 0; margin: 0; background: none; }
.lb-list li.lb-ellipsis { justify-content: center; color: var(--muted); background: none; padding: 0 0 2px; letter-spacing: 2px; }
.lb-empty { justify-content: center; color: var(--muted); background: none; }
.lb-note { margin: 12px 0 0; font-size: 0.78rem; color: var(--muted); text-align: center; min-height: 1em; }

@media (max-width: 560px) { .lb-cols { grid-template-columns: 1fr; gap: 16px; } }

/* Quiz header */
.quiz-header {
  display: flex; align-items: center; justify-content: space-between;
  margin: 6px 0 12px; gap: 12px;
}
.player { font-size: 1.4rem; font-weight: 700; }
.scoreline { display: flex; align-items: center; gap: 12px; }
.topic-chip {
  text-transform: uppercase; letter-spacing: 1px; font-size: 0.7rem; font-weight: 800;
  padding: 5px 11px; border-radius: 99px;
  background: var(--surface-2); color: var(--accent); border: 1px solid var(--border);
}
/* Difficulty band chip: coloured label (Low/Medium/High/Extreme) */
.diff-chip {
  text-transform: uppercase; letter-spacing: 1px; font-size: 0.7rem; font-weight: 800;
  padding: 5px 11px; border-radius: 99px;
  background: var(--surface-2); color: var(--muted); border: 1px solid var(--border);
}
.diff-chip:empty { display: none; }
.diff-chip.L { color: var(--diff-l-fg); background: var(--diff-l-bg); border-color: var(--diff-l-bd); }
.diff-chip.M { color: var(--diff-m-fg); background: var(--diff-m-bg); border-color: var(--diff-m-bd); }
.diff-chip.H { color: var(--diff-h-fg); background: var(--diff-h-bg); border-color: var(--diff-h-bd); }
.diff-chip.E { color: var(--diff-e-fg); background: var(--diff-e-bg); border-color: var(--diff-e-bd); }
.score {
  font-size: 1.6rem; font-weight: 800; font-variant-numeric: tabular-nums;
  color: var(--accent); min-width: 1.4em; text-align: right;
}
.progress { height: 6px; background: var(--surface-2); border-radius: 99px; overflow: hidden; margin-bottom: 8px; }
.progress-bar { height: 100%; width: 0%; background: var(--accent); transition: width 0.3s ease; }
.qpos { font-size: 0.8rem; color: var(--muted); margin-bottom: 12px; }

/* Question stage (one question at a time) */
.qstage { display: block; }
.qcard {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 22px 24px;
}
.qstem { font-size: 1.12rem; line-height: 1.5; margin: 0 0 8px; }
.qstem sub, .qstem sup { font-size: 0.74em; }

/* Rich content inside a stem or option */
.visual { margin: 14px 0 4px; display: flex; justify-content: center; }
.struct { max-width: 100%; height: auto; color: var(--text); }
.qtable {
  border-collapse: collapse; margin: 14px auto 4px; font-size: 0.96rem;
  font-variant-numeric: tabular-nums;
}
.qtable th, .qtable td {
  border: 1px solid var(--border); padding: 7px 14px; text-align: center;
}
.qtable th { background: var(--surface-2); font-weight: 700; }
.eqn { text-align: center; font-size: 1.08rem; margin: 14px 0 4px; line-height: 1.6; }

/* Question diagram (inline SVG, baked into q.visual). Self-contained: the SVG
   uses currentColor for structure and var(--accent) for highlights, so it
   re-colours with the theme. Stimulus only — never reveals the answer. */
.kc-figure {
  margin: 16px 0 4px; border: 1px solid var(--border); background: var(--surface-2);
  border-radius: 14px; padding: 14px 16px 10px; color: var(--text);
}
.kc-figure svg { display: block; width: 100%; height: auto; max-width: 440px; margin: 0 auto; }
.kc-figure svg text { fill: currentColor; font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
.kc-figure figcaption { margin-top: 9px; font-size: 0.8rem; color: var(--muted); text-align: center; line-height: 1.45; }

/* Options */
.opts { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; margin-top: 18px; }
@media (max-width: 620px) { .opts { grid-template-columns: 1fr; } }
.opt {
  display: flex; align-items: center; gap: 12px;
  text-align: left; width: 100%;
  background: var(--surface-2); color: var(--text);
  border: 2px solid var(--border); border-radius: 12px;
  padding: 13px 15px; font-size: 1.02rem; cursor: pointer;
  transition: border-color 0.15s ease, background 0.15s ease, transform 0.08s ease;
}
.opt:hover:not(:disabled) { border-color: var(--accent); transform: translateY(-1px); }
.opt:disabled { cursor: default; }
.opt .opt-key {
  flex: 0 0 auto; width: 27px; height: 27px; border-radius: 7px;
  display: grid; place-items: center; font-size: 0.85rem; font-weight: 800;
  background: var(--surface); color: var(--muted); border: 1px solid var(--border);
}
.opt .opt-body { flex: 1; min-width: 0; }
.opt .opt-body sub, .opt .opt-body sup { font-size: 0.74em; }
.opt .opt-mark { flex: 0 0 auto; width: 20px; text-align: center; font-size: 1.2rem; font-weight: 800; }

.opt.correct {
  background: var(--correct-bg); border-color: var(--correct-border); color: var(--correct);
}
.opt.correct .opt-key { background: var(--correct); color: #fff; border-color: var(--correct); }
.opt.correct .opt-mark { color: var(--correct); }
.opt.wrong {
  background: var(--wrong-bg); border-color: var(--wrong-border); color: var(--wrong);
}
.opt.wrong .opt-key { background: var(--wrong); color: #fff; border-color: var(--wrong); }
.opt.wrong .opt-mark { color: var(--wrong); }
.opt.locked:not(.correct):not(.wrong) { opacity: 0.55; }

/* Footer: explanation + next */
.qfoot { margin-top: 16px; display: flex; align-items: center; justify-content: space-between; gap: 14px; }
.explain {
  flex: 1; margin: 0; font-size: 0.95rem; line-height: 1.5; color: var(--muted);
  min-height: 1em;
}
.explain.show { color: var(--text); }
.explain b { color: var(--accent); }
.next-btn {
  flex: 0 0 auto;
  padding: 12px 26px; font-size: 1.02rem; font-weight: 700;
  border: none; border-radius: 12px; cursor: pointer;
  background: var(--accent); color: #fff;
}
.next-btn:hover { filter: brightness(1.08); }

/* Result overlay */
.overlay {
  position: fixed; inset: 0; z-index: 30;
  display: grid; place-items: center;
  background: rgba(30, 18, 55, 0.55);
  backdrop-filter: blur(3px);
  padding: 20px;
}
.result-card { max-width: 420px; width: 100%; padding: 32px; text-align: center; }
.result-badge { display: flex; justify-content: center; margin-bottom: 10px; }
.count-btn.is-badge { cursor: default; }
.count-btn.is-badge:hover { transform: none; filter: none; }
.result-card h2 { margin: 6px 0 2px; font-size: 1.5rem; }
.result-score {
  font-size: 2.8rem; font-weight: 800; color: var(--accent);
  font-variant-numeric: tabular-nums; margin: 6px 0;
}
.result-meta { color: var(--muted); line-height: 1.6; margin-bottom: 6px; }
.result-meta .new-best { color: var(--accent); font-weight: 700; }
.share-hint {
  background: var(--surface-2); border-radius: 10px; padding: 10px;
  margin: 14px 0 18px; font-size: 0.95rem;
}
.save-status { min-height: 1em; margin: 0 0 12px; font-size: 0.82rem; color: var(--muted); }
.save-status.ok { color: var(--correct); }
.save-status.warn { color: #d98b2b; }

.again-btn {
  width: 100%; padding: 13px; font-size: 1.02rem; font-weight: 700;
  border: none; border-radius: 12px; cursor: pointer;
  background: var(--accent); color: #fff;
}
.again-btn:hover { filter: brightness(1.07); }

/* Stars */
.stars { position: fixed; inset: 0; z-index: 25; pointer-events: none; overflow: hidden; }
.star {
  position: absolute; top: -8%; font-size: 1.6rem;
  animation: fall linear forwards;
  will-change: transform, opacity;
}
@keyframes fall {
  0% { transform: translateY(-10vh) rotate(0deg); opacity: 0; }
  10% { opacity: 1; }
  100% { transform: translateY(110vh) rotate(540deg); opacity: 0.9; }
}
