:root {
  --bg: #f6f5fb;
  --surface: #ffffff;
  --surface-2: #f0eef8;
  --text: #1d1b2c;
  --muted: #6b6880;
  --border: #e3e0ef;
  --brand: #5b4cf0;
  --brand-2: #8a7dff;
  --brand-ink: #ffffff;
  --good: #14a46c;
  --good-bg: #e3f7ee;
  --bad: #e0475b;
  --bad-bg: #fdebee;
  --warn: #e69a12;
  --warn-bg: #fff4dd;
  --shadow: 0 1px 2px rgba(25, 20, 60, 0.06), 0 6px 20px rgba(25, 20, 60, 0.06);
  --radius: 16px;
  --m-ethics: #5b4cf0;
  --m-taxation: #e69a12;
  --m-life: #14a46c;
  --m-sickness: #e0475b;
  --m-segfunds: #1f8fd6;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121120;
    --surface: #1c1a2e;
    --surface-2: #26233d;
    --text: #eceaf6;
    --muted: #a19db8;
    --border: #322e4d;
    --brand: #8a7dff;
    --brand-2: #b2a9ff;
    --brand-ink: #14122a;
    --good: #3fd39a;
    --good-bg: #133628;
    --bad: #ff7a8c;
    --bad-bg: #3d1a22;
    --warn: #ffbf4d;
    --warn-bg: #3a2c10;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 8px 24px rgba(0, 0, 0, 0.25);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #121120;
  --surface: #1c1a2e;
  --surface-2: #26233d;
  --text: #eceaf6;
  --muted: #a19db8;
  --border: #322e4d;
  --brand: #8a7dff;
  --brand-2: #b2a9ff;
  --brand-ink: #14122a;
  --good: #3fd39a;
  --good-bg: #133628;
  --bad: #ff7a8c;
  --bad-bg: #3d1a22;
  --warn: #ffbf4d;
  --warn-bg: #3a2c10;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 8px 24px rgba(0, 0, 0, 0.25);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  padding-bottom: calc(72px + env(safe-area-inset-bottom));
  overflow-x: hidden;
}
a { color: var(--brand); }
h1, h2, h3 { line-height: 1.25; margin: 0 0 0.5rem; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }
p { margin: 0 0 0.75rem; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
[hidden] { display: none !important; }

/* ---------- Top bar & tab bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 12px;
  padding: 10px 16px; padding-top: calc(10px + env(safe-area-inset-top));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; text-decoration: none; color: var(--text); font-weight: 800; }
.brand-mark { width: 30px; height: 30px; border-radius: 9px; display: grid; place-items: center; background: linear-gradient(135deg, var(--brand), var(--brand-2)); color: #fff; }
.brand-name { display: none; }
@media (min-width: 420px) { .brand-name { display: inline; } }
.top-stats { margin-left: auto; display: flex; gap: 8px; font-weight: 700; font-size: 0.9rem; }
.chip { display: inline-flex; align-items: center; gap: 4px; padding: 4px 10px; border-radius: 999px; background: var(--surface); border: 1px solid var(--border); white-space: nowrap; }
.chip.flame.cold { filter: grayscale(1); opacity: 0.7; }
.icon-btn { border: 1px solid var(--border); background: var(--surface); color: var(--text); width: 36px; height: 36px; border-radius: 10px; font-size: 1.1rem; cursor: pointer; }

.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 10;
  display: flex; justify-content: space-around;
  background: var(--surface); border-top: 1px solid var(--border);
  padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
}
.tabbar a { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; text-decoration: none; color: var(--muted); font-size: 0.72rem; font-weight: 600; padding: 4px 0; border-radius: 10px; }
.tabbar a .ti { font-size: 1.25rem; line-height: 1; }
.tabbar a.active { color: var(--brand); background: var(--surface-2); }
body.in-quiz .tabbar { display: none; }
body.in-quiz { padding-bottom: 0; }

/* ---------- Layout ---------- */
main { max-width: 760px; margin: 0 auto; padding: 16px; outline: none; }
.loading { padding: 48px 0; text-align: center; color: var(--muted); }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); margin-bottom: 14px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
@media (max-width: 380px) { .grid3 { grid-template-columns: 1fr 1fr; } }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
.stack > * + * { margin-top: 10px; }

/* ---------- Buttons ---------- */
.btn {
  appearance: none; border: 0; border-radius: 12px; padding: 12px 18px; font: inherit; font-weight: 700; cursor: pointer;
  background: var(--brand); color: var(--brand-ink); box-shadow: 0 3px 0 color-mix(in srgb, var(--brand) 60%, #000);
  transition: transform 0.08s ease, filter 0.15s ease; text-decoration: none; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
}
.btn:hover { filter: brightness(1.05); }
.btn:active { transform: translateY(2px); box-shadow: 0 1px 0 color-mix(in srgb, var(--brand) 60%, #000); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; transform: none; }
.btn.block { width: 100%; }
.btn.ghost { background: var(--surface-2); color: var(--text); box-shadow: none; }
.btn.good { background: var(--good); color: #fff; box-shadow: 0 3px 0 color-mix(in srgb, var(--good) 60%, #000); }
.btn.big { padding: 15px 20px; font-size: 1.05rem; }
.kbd { font-size: 0.72rem; opacity: 0.7; font-weight: 600; border: 1px solid currentColor; border-radius: 5px; padding: 0 4px; }

/* ---------- Home ---------- */
.hero { display: flex; gap: 14px; align-items: center; }
.hero .big-emoji { font-size: 2.4rem; }
.level-bar { height: 10px; border-radius: 99px; background: var(--surface-2); overflow: hidden; margin-top: 6px; }
.level-bar > i { display: block; height: 100%; background: linear-gradient(90deg, var(--brand), var(--brand-2)); border-radius: 99px; transition: width 0.6s ease; }
.quick { display: grid; grid-template-columns: 1fr; gap: 10px; }
@media (min-width: 520px) { .quick { grid-template-columns: repeat(3, 1fr); } }
.quick button { text-align: left; display: block; padding: 14px; border-radius: var(--radius); border: 1px solid var(--border); background: var(--surface); color: var(--text); font: inherit; cursor: pointer; box-shadow: var(--shadow); transition: transform 0.12s ease; }
.quick button:hover { transform: translateY(-2px); }
.quick button b { display: block; font-size: 1.02rem; }
.quick button .qe { font-size: 1.6rem; }
.seg { display: flex; background: var(--surface-2); border-radius: 12px; padding: 4px; gap: 4px; }
.seg button { flex: 1; border: 0; background: transparent; color: var(--text); font: inherit; font-weight: 600; padding: 8px 6px; border-radius: 9px; cursor: pointer; font-size: 0.92rem; }
.seg button[aria-pressed="true"] { background: var(--surface); box-shadow: var(--shadow); color: var(--brand); }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.pick { border: 1.5px solid var(--border); background: var(--surface); color: var(--text); border-radius: 999px; padding: 6px 12px; font: inherit; font-size: 0.88rem; font-weight: 600; cursor: pointer; }
.pick[aria-pressed="true"] { border-color: var(--brand); background: color-mix(in srgb, var(--brand) 12%, var(--surface)); color: var(--brand); }
.pick .n { opacity: 0.6; font-weight: 500; }
label.field { display: block; font-weight: 700; font-size: 0.9rem; margin: 12px 0 6px; }
input[type="text"] { width: 100%; padding: 11px 12px; border-radius: 12px; border: 1.5px solid var(--border); background: var(--surface); color: var(--text); font: inherit; }

/* ---------- Quiz ---------- */
.quiz-head { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.progress { flex: 1; height: 12px; border-radius: 99px; background: var(--surface-2); overflow: hidden; }
.progress > i { display: block; height: 100%; background: var(--good); border-radius: 99px; transition: width 0.35s ease; }
.timer { font-variant-numeric: tabular-nums; font-weight: 800; }
.timer.low { color: var(--bad); animation: pulse 1s infinite; }
.qmeta { display: flex; gap: 8px; flex-wrap: wrap; font-size: 0.8rem; color: var(--muted); margin-bottom: 8px; }
.mtag { display: inline-flex; align-items: center; gap: 6px; font-weight: 700; }
.mtag::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--mc, var(--brand)); }
.question { font-size: 1.12rem; font-weight: 650; margin-bottom: 14px; }
.options { display: grid; gap: 10px; }
.opt {
  display: grid; grid-template-columns: 34px 1fr; gap: 10px; align-items: start; text-align: left;
  border: 2px solid var(--border); background: var(--surface); color: var(--text); border-radius: 14px;
  padding: 12px; font: inherit; cursor: pointer; transition: border-color 0.12s, background 0.12s, transform 0.08s;
  box-shadow: 0 2px 0 var(--border);
}
.opt:hover:not(:disabled) { border-color: color-mix(in srgb, var(--brand) 50%, var(--border)); }
.opt:active:not(:disabled) { transform: translateY(1px); }
.opt .lbl { width: 30px; height: 30px; border-radius: 9px; display: grid; place-items: center; font-weight: 800; background: var(--surface-2); color: var(--muted); font-size: 0.9rem; }
.opt.sel { border-color: var(--brand); background: color-mix(in srgb, var(--brand) 9%, var(--surface)); box-shadow: 0 2px 0 var(--brand); }
.opt.sel .lbl { background: var(--brand); color: var(--brand-ink); }
.opt:disabled { cursor: default; }
.opt.right { border-color: var(--good); background: var(--good-bg); box-shadow: 0 2px 0 var(--good); }
.opt.right .lbl { background: var(--good); color: #fff; }
.opt.wrong { border-color: var(--bad); background: var(--bad-bg); box-shadow: 0 2px 0 var(--bad); }
.opt.wrong .lbl { background: var(--bad); color: #fff; }
.opt.dim { opacity: 0.85; }
.why { grid-column: 2; font-size: 0.88rem; color: var(--muted); margin-top: 4px; border-top: 1px dashed var(--border); padding-top: 6px; }
.opt.right .why, .opt.wrong .why { color: var(--text); }
.why b { font-weight: 700; }
.feedback { margin-top: 14px; border-radius: 14px; padding: 14px; animation: rise 0.25s ease; }
.feedback.ok { background: var(--good-bg); }
.feedback.no { background: var(--bad-bg); }
.feedback h3 { display: flex; align-items: center; gap: 8px; font-size: 1.1rem; }
.feedback .ref { font-size: 0.82rem; color: var(--muted); margin-top: 6px; }
.quiz-foot {
  position: sticky; bottom: 0; margin: 14px -16px -16px; padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  background: color-mix(in srgb, var(--bg) 92%, transparent); backdrop-filter: blur(8px); border-top: 1px solid var(--border);
  display: flex; gap: 10px;
}
.quiz-foot .btn { flex: 1; }
.xp-pop { position: fixed; left: 50%; top: 38%; transform: translateX(-50%); font-weight: 900; font-size: 1.6rem; color: var(--brand); pointer-events: none; animation: xp 1.1s ease forwards; z-index: 30; text-shadow: 0 2px 10px color-mix(in srgb, var(--brand) 40%, transparent); }
.palette { display: grid; grid-template-columns: repeat(auto-fill, minmax(36px, 1fr)); gap: 6px; }
.palette button { height: 36px; border-radius: 9px; border: 1.5px solid var(--border); background: var(--surface); color: var(--text); font: inherit; font-weight: 700; font-size: 0.85rem; cursor: pointer; }
.palette button.done { background: color-mix(in srgb, var(--brand) 15%, var(--surface)); border-color: var(--brand); }
.palette button.cur { outline: 2px solid var(--brand); outline-offset: 1px; }
.shake { animation: shake 0.35s ease; }
.pop { animation: popin 0.3s ease; }

/* ---------- Results ---------- */
.score-ring { width: 150px; height: 150px; margin: 0 auto 8px; position: relative; }
.score-ring svg { transform: rotate(-90deg); }
.score-ring .val { position: absolute; inset: 0; display: grid; place-items: center; text-align: center; font-weight: 900; font-size: 2rem; }
.score-ring .val small { display: block; font-size: 0.8rem; font-weight: 600; color: var(--muted); }
.verdict { text-align: center; font-weight: 800; font-size: 1.2rem; }
.verdict.pass { color: var(--good); }
.verdict.fail { color: var(--warn); }
.tiles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 12px; }
.tile { background: var(--surface-2); border-radius: 12px; padding: 10px; text-align: center; }
.tile b { display: block; font-size: 1.15rem; }
.tile span { font-size: 0.75rem; color: var(--muted); }
.bars .bar { margin: 8px 0; }
.bars .bar .top { display: flex; justify-content: space-between; font-size: 0.88rem; font-weight: 600; }
.bars .bar .track { height: 10px; border-radius: 99px; background: var(--surface-2); overflow: hidden; margin-top: 4px; }
.bars .bar .track i { display: block; height: 100%; border-radius: 99px; background: var(--mc, var(--brand)); }
details.missed { border: 1px solid var(--border); border-radius: 12px; padding: 10px 12px; margin: 8px 0; background: var(--surface); }
details.missed summary { cursor: pointer; font-weight: 600; }
details.missed .opt { cursor: default; margin-top: 8px; }
.badge-new { display: flex; gap: 10px; align-items: center; padding: 10px; border-radius: 12px; background: var(--warn-bg); margin: 6px 0; animation: popin 0.4s ease; }
.badge-new .bi { font-size: 1.8rem; }

/* ---------- Progress ---------- */
.chart { width: 100%; height: auto; display: block; }
.chart text { fill: var(--muted); font-size: 11px; }
.chart .grid { stroke: var(--border); stroke-width: 1; }
.legend { display: flex; flex-wrap: wrap; gap: 10px; font-size: 0.8rem; margin-top: 6px; }
.legend span { display: inline-flex; align-items: center; gap: 5px; }
.legend i { width: 10px; height: 10px; border-radius: 3px; background: var(--mc); display: inline-block; }
.heat { display: grid; gap: 6px; }
.heat .hrow { display: grid; grid-template-columns: 92px 1fr; gap: 8px; align-items: center; }
.heat .hname { font-size: 0.8rem; font-weight: 700; }
.heat .cells { display: flex; flex-wrap: wrap; gap: 4px; }
.heat .cell { width: 34px; height: 34px; border-radius: 8px; display: grid; place-items: center; font-size: 0.72rem; font-weight: 700; background: var(--surface-2); color: var(--muted); }
.badges { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 8px; }
.badge { text-align: center; padding: 10px 6px; border-radius: 12px; background: var(--surface-2); font-size: 0.75rem; }
.badge .bi { font-size: 1.7rem; display: block; }
.badge.locked { opacity: 0.4; filter: grayscale(1); }

/* ---------- Leaderboard ---------- */
.lb-row { display: grid; grid-template-columns: 40px 1fr auto; gap: 10px; align-items: center; padding: 12px; border-radius: 14px; background: var(--surface); border: 1px solid var(--border); margin-bottom: 8px; animation: rise 0.3s ease both; }
.lb-row.me { border-color: var(--brand); box-shadow: 0 0 0 2px color-mix(in srgb, var(--brand) 20%, transparent); }
.lb-rank { font-size: 1.4rem; text-align: center; font-weight: 900; }
.lb-name { font-weight: 700; }
.lb-xp { font-weight: 900; color: var(--brand); text-align: right; }

/* ---------- Tables (admin) ---------- */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; font-size: 0.85rem; }
th, td { text-align: left; padding: 7px 8px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.03em; color: var(--muted); }
.pct-low { color: var(--bad); font-weight: 700; }
.flag { background: var(--warn-bg); border-radius: 10px; padding: 10px; margin: 6px 0; font-size: 0.88rem; }

/* ---------- Toast & confetti ---------- */
.toast { position: fixed; left: 50%; bottom: calc(86px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(20px); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-weight: 600; opacity: 0; pointer-events: none; transition: all 0.25s ease; z-index: 40; max-width: calc(100% - 32px); }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.confetti { position: fixed; top: -10px; width: 9px; height: 14px; border-radius: 2px; z-index: 50; pointer-events: none; animation: fall linear forwards; }

@keyframes rise { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
@keyframes popin { 0% { transform: scale(0.92); opacity: 0; } 70% { transform: scale(1.03); opacity: 1; } 100% { transform: scale(1); } }
@keyframes shake { 0%, 100% { transform: translateX(0); } 25% { transform: translateX(-6px); } 75% { transform: translateX(6px); } }
@keyframes pulse { 50% { opacity: 0.55; } }
@keyframes xp { 0% { opacity: 0; transform: translate(-50%, 10px) scale(0.8); } 25% { opacity: 1; transform: translate(-50%, 0) scale(1.1); } 100% { opacity: 0; transform: translate(-50%, -50px) scale(1); } }
@keyframes fall { to { transform: translateY(105vh) rotate(720deg); } }
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
