/* ---------- Tokens ---------- */
:root {
  --bg: #fff8f1;
  --bg-2: #ffeedd;
  --surface: #ffffff;
  --surface-2: #fff4ea;
  --text: #2b2118;
  --muted: #7a6a5c;
  --border: #f0dccb;
  --primary: #c4410f;
  --primary-2: #ff8f5e;
  --primary-ink: #ffffff;
  --good: #0b7a4f;
  --good-bg: #dcf7ea;
  --bad: #c0283c;
  --bad-bg: #fde2e6;
  --accent: #5b3fd6;
  --accent-ink: #ffffff;
  --accent-bg: #ece6ff;
  --gold: #f5b301;
  --track: #f3e5d8;
  --shadow: 0 6px 0 rgba(120, 70, 30, .12), 0 12px 30px rgba(120, 70, 30, .10);
  --shadow-sm: 0 3px 0 rgba(120, 70, 30, .12);
  --radius: 20px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1a1411;
    --bg-2: #221a15;
    --surface: #2a211b;
    --surface-2: #332821;
    --text: #fbefe4;
    --muted: #bba898;
    --border: #45372d;
    --primary: #ff7a47;
    --primary-2: #ff9a6e;
    --primary-ink: #1a1411;
    --accent-ink: #15102e;
    --good: #3ad296;
    --good-bg: #173a2c;
    --bad: #ff6b7d;
    --bad-bg: #3f1d24;
    --accent: #a08bff;
    --accent-bg: #2e2650;
    --track: #3b2f27;
    --shadow: 0 6px 0 rgba(0, 0, 0, .35), 0 12px 30px rgba(0, 0, 0, .35);
    --shadow-sm: 0 3px 0 rgba(0, 0, 0, .35);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #1a1411;
  --bg-2: #221a15;
  --surface: #2a211b;
  --surface-2: #332821;
  --text: #fbefe4;
  --muted: #bba898;
  --border: #45372d;
  --primary: #ff7a47;
  --primary-2: #ff9a6e;
  --primary-ink: #1a1411;
  --accent-ink: #15102e;
  --good: #3ad296;
  --good-bg: #173a2c;
  --bad: #ff6b7d;
  --bad-bg: #3f1d24;
  --accent: #a08bff;
  --accent-bg: #2e2650;
  --track: #3b2f27;
  --shadow: 0 6px 0 rgba(0, 0, 0, .35), 0 12px 30px rgba(0, 0, 0, .35);
  --shadow-sm: 0 3px 0 rgba(0, 0, 0, .35);
  color-scheme: dark;
}

/* ---------- Base ---------- */
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  font-family: 'Nunito', system-ui, -apple-system, 'Segoe UI', sans-serif;
  background: radial-gradient(1200px 600px at 10% -10%, var(--bg-2), transparent 60%), var(--bg);
  background-color: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, h3 { line-height: 1.15; margin: 0 0 .4em; font-weight: 900; }
p { margin: 0 0 .7em; }
small, .muted { color: var(--muted); }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; border-radius: 8px; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 10px 16px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.brand {
  background: none; border: 0; font-weight: 900; font-size: 1.25rem; padding: 4px 6px;
  display: flex; align-items: center; gap: 6px;
}
.tabs { display: flex; gap: 4px; flex: 1; justify-content: center; }
.tabs button {
  background: none; border: 0; padding: 8px 14px; border-radius: 999px; font-weight: 800; color: var(--muted);
}
.tabs button:hover { background: var(--surface-2); color: var(--text); }
.tabs button.active { background: var(--text); color: var(--bg); }
.hud { display: flex; align-items: center; gap: 10px; font-weight: 800; font-size: .9rem; }
.hud .lvl { background: var(--accent); color: var(--accent-ink); border-radius: 999px; padding: 3px 10px; white-space: nowrap; }
.hud .xpbar { width: 90px; height: 10px; border-radius: 99px; background: var(--track); overflow: hidden; }
.hud .xpbar > div { height: 100%; background: linear-gradient(90deg, var(--accent), var(--primary)); border-radius: 99px; transition: width .6s ease; }
.hud .streak { white-space: nowrap; }
body.onboarding .tabs, body.onboarding .hud { visibility: hidden; }

@media (max-width: 720px) {
  .topbar { padding: 8px 16px; }
  .tabs { order: 3; flex-basis: 100%; justify-content: space-between; }
  .tabs button { padding: 6px 10px; font-size: .92rem; }
  .brand-text { font-size: 1.05rem; }
  .hud { margin-left: auto; }
  .hud .xpbar { width: 60px; }
}

/* ---------- Layout ---------- */
main { width: 100%; max-width: 920px; margin: 0 auto; padding: 20px 16px 40px; flex: 1; outline: none; }
.footer { text-align: center; color: var(--muted); font-size: .8rem; padding: 18px 16px 26px; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 22px; margin-bottom: 18px;
}
.grid { display: grid; gap: 14px; }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
@media (max-width: 640px) { .grid-2, .grid-3 { grid-template-columns: minmax(0, 1fr); } }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
.fade-in { animation: fadeUp .35s ease both; }
@keyframes fadeUp { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: none; } }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  border: 0; border-radius: 16px; padding: 12px 22px; font-weight: 900; font-size: 1.05rem;
  background: var(--surface-2); color: var(--text); box-shadow: var(--shadow-sm);
  transition: transform .08s ease, filter .15s ease;
}
.btn:hover { filter: brightness(1.04); }
.btn:active { transform: translateY(2px); box-shadow: none; }
.btn.primary { background: var(--primary); color: var(--primary-ink); box-shadow: 0 4px 0 color-mix(in srgb, var(--primary) 60%, #000); }
.btn.big { font-size: 1.25rem; padding: 16px 30px; border-radius: 20px; }
.btn.ghost { background: transparent; box-shadow: none; }
.btn.danger { color: var(--bad); }
.btn[disabled] { opacity: .5; cursor: default; }

/* ---------- Home ---------- */
.hero {
  display: grid; grid-template-columns: 1fr auto; gap: 18px; align-items: center;
  background: linear-gradient(135deg, #b8380b, #c2410c 55%, #a14a06);
  color: #fff; border: 0;
}
.hero h1 { font-size: clamp(1.6rem, 4vw, 2.3rem); }
.hero p { opacity: .95; font-weight: 600; }
.hero .btn { background: #fff; color: #9a3412; box-shadow: 0 5px 0 rgba(0, 0, 0, .15); }
.hero .big-emoji { font-size: 4.6rem; line-height: 1; filter: drop-shadow(0 6px 10px rgba(0, 0, 0, .2)); animation: bob 3s ease-in-out infinite; }
@keyframes bob { 50% { transform: translateY(-8px) rotate(-4deg); } }
.hero-xp { margin: 12px 0 16px; }
.hero-xp .track { height: 14px; background: rgba(255, 255, 255, .35); border-radius: 99px; overflow: hidden; }
.hero-xp .track > div { height: 100%; background: #fff; border-radius: 99px; }
.hero-xp small { color: #fff; opacity: .9; font-weight: 700; }
@media (max-width: 560px) { .hero { grid-template-columns: 1fr; } .hero .big-emoji { display: none; } }

.anchor { display: flex; gap: 12px; align-items: center; background: var(--surface-2); border-radius: 16px; padding: 12px 14px; }
.anchor .em { font-size: 2rem; }
.anchor b { font-size: 1.1rem; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { display: inline-flex; gap: 6px; align-items: center; border-radius: 999px; padding: 6px 12px; font-weight: 800; font-size: .88rem; background: var(--accent-bg); color: var(--accent); }
.chip.locked { background: var(--track); color: var(--muted); }
.fact { border-left: 5px solid var(--gold); }

/* ---------- Quiz ---------- */
.quiz-top { display: flex; align-items: center; gap: 12px; margin-bottom: 14px; }
.qprogress { flex: 1; height: 16px; background: var(--track); border-radius: 99px; overflow: hidden; }
.qprogress > div { height: 100%; background: linear-gradient(90deg, var(--good), #6be0a8); border-radius: 99px; transition: width .4s ease; }
.combo { font-weight: 900; min-width: 64px; text-align: right; color: var(--primary); }
.combo.hot { animation: pulse .5s ease; }
@keyframes pulse { 50% { transform: scale(1.35); } }
.qcard { text-align: center; }
.kicker { text-transform: uppercase; letter-spacing: .08em; font-size: .78rem; font-weight: 900; color: var(--accent); margin-bottom: 6px; }
.diff { display: inline-block; margin-left: 6px; color: var(--gold); letter-spacing: 0; }
.q-emoji { font-size: 4.2rem; line-height: 1.1; margin: 4px 0 8px; animation: popIn .4s cubic-bezier(.2, 1.6, .4, 1) both; }
@keyframes popIn { from { transform: scale(.3); opacity: 0; } to { transform: none; opacity: 1; } }
.q-prompt { font-size: clamp(1.2rem, 3.2vw, 1.6rem); max-width: 640px; margin: 0 auto .35em; }
.q-sub { color: var(--muted); font-weight: 600; max-width: 600px; margin: 0 auto 1em; }
.options { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; margin-top: 16px; }
.options.tiles { grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); }
@media (max-width: 420px) { .options { grid-template-columns: minmax(0, 1fr); } .options.tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
.opt {
  position: relative; border: 2px solid var(--border); background: var(--surface); border-radius: 18px;
  padding: 16px 14px 16px 44px; font-weight: 900; font-size: 1.15rem; text-align: left;
  box-shadow: 0 4px 0 var(--border); transition: transform .08s ease, border-color .15s ease, background .15s ease;
}
.opt:hover:not([disabled]) { border-color: var(--primary-2); transform: translateY(-2px); }
.opt:active:not([disabled]) { transform: translateY(2px); box-shadow: none; }
.opt .key {
  position: absolute; left: 12px; top: 50%; transform: translateY(-50%);
  width: 22px; height: 22px; border-radius: 7px; font-size: .75rem; display: grid; place-items: center;
  background: var(--track); color: var(--muted);
}
.options.tiles .opt { text-align: center; padding: 16px 10px 14px; display: flex; flex-direction: column; align-items: center; gap: 4px; }
.options.tiles .opt .key { top: 10px; left: 10px; transform: none; }
.opt .tile-emoji { font-size: 2.6rem; line-height: 1.1; }
.opt .tile-label { font-size: 1rem; }
.opt .tile-sub { font-size: .78rem; font-weight: 700; color: var(--muted); }
.opt .reveal { font-size: .9rem; color: var(--muted); font-weight: 800; }
.opt[disabled] { cursor: default; }
.opt.correct { border-color: var(--good); background: var(--good-bg); box-shadow: 0 4px 0 var(--good); animation: popIn .35s ease; }
.opt.wrong { border-color: var(--bad); background: var(--bad-bg); box-shadow: 0 4px 0 var(--bad); animation: shake .4s ease; }
.opt.dim { opacity: .55; }
@keyframes shake { 20%, 60% { transform: translateX(-6px); } 40%, 80% { transform: translateX(6px); } }

.feedback { text-align: left; margin-top: 18px; border-radius: 18px; padding: 18px; animation: fadeUp .3s ease both; }
.feedback.ok { background: var(--good-bg); }
.feedback.no { background: var(--bad-bg); }
.feedback h3 { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; font-size: 1.3rem; }
.feedback.ok h3 { color: var(--good); }
.feedback.no h3 { color: var(--bad); }
.xp-pop { font-size: .95rem; background: var(--accent); color: var(--accent-ink); border-radius: 99px; padding: 2px 10px; }
.feedback .next-row { display: flex; justify-content: flex-end; margin-top: 12px; gap: 10px; align-items: center; }
.hook { background: var(--surface); border-radius: 14px; padding: 10px 14px; margin-top: 10px; font-weight: 700; }
.hook::before { content: '💡 '; }

/* ---------- Visuals ---------- */
.bars { display: grid; gap: 8px; margin: 12px 0 4px; }
.bar-row { display: grid; grid-template-columns: minmax(90px, 38%) 1fr auto; gap: 10px; align-items: center; font-weight: 700; font-size: .92rem; }
.bar-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-track { height: 14px; background: var(--track); border-radius: 99px; overflow: hidden; }
.bar-fill { height: 100%; width: var(--w); background: var(--muted); border-radius: 99px; animation: grow .8s cubic-bezier(.2, .9, .3, 1) both; }
.bar-row.hi .bar-fill { background: var(--good); }
.bar-row.lo .bar-fill { background: var(--bad); }
.bar-row.ac .bar-fill { background: var(--accent); }
.bar-row.pr .bar-fill { background: var(--primary); }
.bar-val { font-variant-numeric: tabular-nums; white-space: nowrap; }
@keyframes grow { from { width: 0; } }

.equiv {
  display: grid; grid-template-columns: auto auto 1fr; gap: 12px; align-items: center;
  background: var(--surface); border-radius: 16px; padding: 12px 14px; margin-top: 10px;
}
.eq-side { display: flex; align-items: center; gap: 10px; font-weight: 700; }
.eq-big { font-size: 2.6rem; line-height: 1; }
.eq-sign { font-size: 1.8rem; font-weight: 900; color: var(--muted); }
.eq-right { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.icons { display: flex; flex-wrap: wrap; gap: 2px; font-size: 1.45rem; line-height: 1.2; }
.icons .ic { display: inline-block; animation: popIn .3s ease both; }
.icons .ic.part { clip-path: inset(0 calc((1 - var(--p)) * 100%) 0 0); }
@media (max-width: 520px) { .equiv { grid-template-columns: 1fr; text-align: left; } .eq-sign { display: none; } }

/* ---------- Summary ---------- */
.summary { text-align: center; }
.stars { font-size: 3.2rem; letter-spacing: 6px; margin: 6px 0; }
.stars .off { filter: grayscale(1); opacity: .3; }
.stars span { display: inline-block; animation: popIn .5s cubic-bezier(.2, 1.6, .4, 1) both; }
.stars span:nth-child(2) { animation-delay: .15s; }
.stars span:nth-child(3) { animation-delay: .3s; }
.recap { text-align: left; list-style: none; padding: 0; margin: 14px 0; display: grid; gap: 6px; }
.recap li { background: var(--surface-2); border-radius: 12px; padding: 8px 12px; font-weight: 700; }
.recap li.ok::before { content: '✅ '; }
.recap li.no::before { content: '🔁 '; }

/* ---------- Stats & progress ---------- */
.stat { background: var(--surface-2); border-radius: 16px; padding: 14px; text-align: center; }
.stat b { display: block; font-size: 1.7rem; font-weight: 900; }
.stat span { color: var(--muted); font-weight: 700; font-size: .85rem; }
.badges { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 10px; }
.badge { background: var(--surface-2); border-radius: 16px; padding: 12px; text-align: center; }
.badge .em { font-size: 2.2rem; }
.badge b { display: block; }
.badge small { display: block; font-size: .78rem; }
.badge.locked .em { filter: grayscale(1); opacity: .35; }
.badge.locked b { color: var(--muted); }
.mastery { display: grid; grid-template-columns: repeat(auto-fill, minmax(46px, 1fr)); gap: 6px; }
.tile { aspect-ratio: 1; border-radius: 12px; display: grid; place-items: center; font-size: 1.5rem; background: var(--track); border: 2px solid transparent; }
.tile.b0 { background: var(--track); opacity: .45; filter: grayscale(.8); }
.tile.b1, .tile.b2 { background: var(--bad-bg); border-color: color-mix(in srgb, var(--bad) 40%, transparent); }
.tile.b3 { background: color-mix(in srgb, var(--gold) 25%, var(--surface)); border-color: color-mix(in srgb, var(--gold) 60%, transparent); }
.tile.b4, .tile.b5, .tile.b6 { background: var(--good-bg); border-color: var(--good); }
.tile.locked { opacity: .2; filter: grayscale(1); }
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: .85rem; font-weight: 700; color: var(--muted); margin-top: 10px; }
.legend i { display: inline-block; width: 14px; height: 14px; border-radius: 4px; vertical-align: -2px; margin-right: 4px; }

/* ---------- Forms ---------- */
.form { display: grid; gap: 14px; }
.field label { display: block; font-weight: 800; margin-bottom: 6px; }
.field .hint { font-size: .82rem; color: var(--muted); margin-top: 4px; }
.input-row { display: flex; gap: 8px; }
input[type="number"], input[type="search"], select {
  width: 100%; padding: 12px 14px; border-radius: 14px; border: 2px solid var(--border); background: var(--surface);
  font-weight: 700; min-width: 0;
}
input:focus, select:focus { border-color: var(--accent); outline: none; }
select.unit { width: auto; flex: 0 0 auto; }
.seg { display: flex; background: var(--track); border-radius: 14px; padding: 4px; gap: 4px; }
.seg label { flex: 1; text-align: center; padding: 9px 8px; border-radius: 10px; font-weight: 800; cursor: pointer; margin: 0; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg input:focus-visible + span { outline: 3px solid var(--accent); outline-offset: 1px; }
.seg input:checked + span { background: var(--surface); box-shadow: var(--shadow-sm); }
.seg label span { display: block; border-radius: 10px; padding: 6px 4px; }
.seg label { padding: 0; }
.error { color: var(--bad); font-weight: 800; }
input[type="range"] { width: 100%; accent-color: var(--primary); }

.table { width: 100%; border-collapse: collapse; font-size: .92rem; }
.table td { padding: 7px 6px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table td.num { text-align: right; font-weight: 800; white-space: nowrap; font-variant-numeric: tabular-nums; }
.table .mini { height: 8px; background: var(--track); border-radius: 99px; overflow: hidden; min-width: 50px; }
.table .mini > div { height: 100%; background: var(--primary); border-radius: 99px; }
.table-wrap { max-height: 460px; overflow: auto; }
.eq-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 10px; margin-top: 12px; }
.eq-tile { background: var(--surface-2); border-radius: 14px; padding: 10px; text-align: center; font-weight: 700; }
.eq-tile .em { font-size: 1.8rem; }
.eq-tile b { display: block; font-size: 1.2rem; }

/* ---------- Toasts & confetti ---------- */
.toasts { position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); display: grid; gap: 8px; z-index: 50; width: min(420px, calc(100% - 32px)); }
.toast {
  background: var(--text); color: var(--bg); border-radius: 16px; padding: 12px 16px; font-weight: 800;
  box-shadow: var(--shadow); animation: toastIn .35s cubic-bezier(.2, 1.4, .4, 1) both; display: flex; gap: 10px; align-items: center;
}
.toast .em { font-size: 1.6rem; }
.toast.out { animation: toastOut .3s ease forwards; }
@keyframes toastIn { from { transform: translateY(20px) scale(.9); opacity: 0; } }
@keyframes toastOut { to { transform: translateY(20px); opacity: 0; } }
.confetti { position: fixed; inset: 0; pointer-events: none; overflow: hidden; z-index: 60; }
.confetti i { position: absolute; top: -20px; width: 10px; height: 14px; border-radius: 3px; animation: fall linear forwards; }
@keyframes fall { to { transform: translateY(110vh) rotate(720deg); } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
  .confetti { display: none; }
}
