:root {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-2: #eef2fa;
  --text: #16213a;
  --muted: #5b6684;
  --line: #d9e0ee;
  --accent: #1b4fa0;
  --accent-ink: #ffffff;
  --accent-soft: #e3ecfb;
  --good: #1f8f5a;
  --good-soft: #dff4ea;
  --warn: #c97a00;
  --warn-soft: #fff1d6;
  --bad: #c43d3d;
  --bad-soft: #fde3e3;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(20, 30, 60, .06), 0 8px 24px rgba(20, 30, 60, .06);
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, Inter, system-ui, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1b33; --surface: #172540; --surface-2: #1f2f4f; --text: #eaf0ff; --muted: #a7b4d1; --line: #2c3d61;
    --accent: #6fa0ff; --accent-ink: #0b1630; --accent-soft: #223a66;
    --good: #4cc98a; --good-soft: #163b2c; --warn: #f0b24a; --warn-soft: #3d2e12; --bad: #ff7b7b; --bad-soft: #4a1f1f;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 8px 24px rgba(0,0,0,.35);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1b33; --surface: #172540; --surface-2: #1f2f4f; --text: #eaf0ff; --muted: #a7b4d1; --line: #2c3d61;
  --accent: #6fa0ff; --accent-ink: #0b1630; --accent-soft: #223a66;
  --good: #4cc98a; --good-soft: #163b2c; --warn: #f0b24a; --warn-soft: #3d2e12; --bad: #ff7b7b; --bad-soft: #4a1f1f;
  --shadow: 0 1px 2px rgba(0,0,0,.4), 0 8px 24px rgba(0,0,0,.35);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg); color: var(--text); font-family: var(--font); font-size: 16px; line-height: 1.5;
  -webkit-text-size-adjust: 100%; min-height: 100vh;
  padding-bottom: env(safe-area-inset-bottom);
}
a { color: var(--accent); text-decoration: none; }
button { font: inherit; }
h1, h2, h3 { line-height: 1.2; margin: 0 0 .5rem; }
h1 { font-size: 1.6rem; } h2 { font-size: 1.25rem; } h3 { font-size: 1.05rem; }
p { margin: 0 0 .75rem; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }
.hidden { display: none !important; }

.topbar {
  position: sticky; top: 0; z-index: 10; background: var(--surface); border-bottom: 1px solid var(--line);
  padding: calc(env(safe-area-inset-top) + 8px) 16px 0; display: flex; flex-wrap: wrap; align-items: center; gap: 8px 16px;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; color: var(--text); padding: 6px 0 10px; }
.brand-mark { width: 28px; height: 28px; border-radius: 8px; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; font-weight: 800; }
.tabs { display: flex; gap: 4px; margin-left: auto; overflow-x: auto; -webkit-overflow-scrolling: touch; }
.tabs a { padding: 8px 12px 12px; color: var(--muted); font-weight: 600; border-bottom: 3px solid transparent; white-space: nowrap; }
.tabs a.active { color: var(--accent); border-bottom-color: var(--accent); }
@media (max-width: 560px) {
  .topbar { padding-bottom: 0; }
  .brand span:last-child { display: none; }
  .tabs { margin-left: 0; width: 100%; justify-content: space-between; }
  .tabs a { padding: 6px 8px 10px; font-size: .9rem; }
}

.view { max-width: 860px; margin: 0 auto; padding: 20px 16px 48px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); margin-bottom: 14px; }
.card.flat { box-shadow: none; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); }
.stack > * + * { margin-top: 10px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; border: 1px solid var(--line); background: var(--surface);
  color: var(--text); padding: 10px 16px; border-radius: 10px; font-weight: 600; cursor: pointer; min-height: 44px; transition: transform .05s ease;
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.ghost { background: transparent; }
.btn.small { min-height: 36px; padding: 6px 12px; font-size: .9rem; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; cursor: default; }
.btn.good { background: var(--good); border-color: var(--good); color: #fff; }

.chip { display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px; border-radius: 999px; background: var(--surface-2); color: var(--muted); font-size: .8rem; font-weight: 600; white-space: nowrap; }
.chip.accent { background: var(--accent-soft); color: var(--accent); }
.chip.good { background: var(--good-soft); color: var(--good); }
.chip.warn { background: var(--warn-soft); color: var(--warn); }
.chip.bad { background: var(--bad-soft); color: var(--bad); }

.bar { height: 8px; background: var(--surface-2); border-radius: 999px; overflow: hidden; }
.bar > span { display: block; height: 100%; background: var(--accent); border-radius: 999px; transition: width .3s ease; }
.bar.good > span { background: var(--good); }
.bar.warn > span { background: var(--warn); }

.stat { text-align: center; padding: 10px 6px; }
.stat b { display: block; font-size: 1.6rem; line-height: 1.1; }
.stat span { color: var(--muted); font-size: .8rem; }

.list { list-style: none; margin: 0; padding: 0; }
.list li { padding: 12px 0; border-top: 1px solid var(--line); }
.list li:first-child { border-top: 0; padding-top: 0; }
.list li:last-child { padding-bottom: 0; }
.list .row { gap: 8px; }

.course-tile { display: block; color: inherit; cursor: pointer; }
.course-tile h3 { margin-bottom: 4px; }
.course-tile .bar { margin-top: 10px; }

.dots { display: flex; flex-wrap: wrap; gap: 4px; }
.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--line); }
.dot.l1 { background: var(--bad); } .dot.l2 { background: var(--warn); } .dot.l3 { background: var(--accent); } .dot.l4 { background: var(--good); }

.question { font-size: 1.1rem; }
.question .prompt { white-space: pre-wrap; margin: 8px 0 16px; }
.question .prompt img { max-width: 100%; }
.options { display: grid; gap: 8px; }
.opt { display: flex; gap: 10px; align-items: flex-start; text-align: left; padding: 12px 14px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); cursor: pointer; min-height: 44px; color: var(--text); }
.opt .key { font-weight: 800; color: var(--muted); min-width: 1.2em; }
.opt:hover { border-color: var(--accent); }
.opt.selected { border-color: var(--accent); background: var(--accent-soft); }
.opt.correct { border-color: var(--good); background: var(--good-soft); }
.opt.wrong { border-color: var(--bad); background: var(--bad-soft); }
.opt:disabled { cursor: default; }
input[type="text"], input[type="password"], input[type="number"], textarea, select {
  width: 100%; font: inherit; padding: 12px 14px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); color: var(--text); min-height: 44px;
}
textarea { min-height: 140px; resize: vertical; line-height: 1.5; }
textarea.writing { min-height: 320px; font-size: 1.05rem; }
label.field { display: block; margin-bottom: 10px; }
label.field > span { display: block; font-size: .85rem; color: var(--muted); margin-bottom: 4px; font-weight: 600; }
.check { display: flex; gap: 10px; align-items: center; padding: 8px 0; }
.check input { width: 20px; height: 20px; }

.feedback { border-radius: 10px; padding: 12px 14px; margin-top: 14px; border: 1px solid var(--line); }
.feedback.good { background: var(--good-soft); border-color: var(--good); }
.feedback.bad { background: var(--bad-soft); border-color: var(--bad); }
.feedback.partial { background: var(--warn-soft); border-color: var(--warn); }
.feedback h3 { margin-bottom: 6px; }
.feedback .explanation { white-space: pre-wrap; }
details summary { cursor: pointer; font-weight: 600; color: var(--accent); }
details[open] summary { margin-bottom: 8px; }

.progress-steps { display: flex; gap: 4px; margin-bottom: 12px; }
.progress-steps span { flex: 1; height: 6px; border-radius: 3px; background: var(--line); }
.progress-steps span.done { background: var(--good); }
.progress-steps span.miss { background: var(--bad); }
.progress-steps span.cur { background: var(--accent); }

.loader { display: inline-block; width: 18px; height: 18px; border: 3px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; vertical-align: middle; }
@keyframes spin { to { transform: rotate(360deg); } }
.center { text-align: center; }
.pad { padding: 28px 12px; }

.toast { position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-weight: 600; z-index: 50; max-width: 90vw; box-shadow: var(--shadow); }

.rubric { width: 100%; border-collapse: collapse; margin-top: 8px; font-size: .95rem; }
.rubric td, .rubric th { text-align: left; padding: 6px 8px; border-top: 1px solid var(--line); vertical-align: top; }
.rubric th { color: var(--muted); font-weight: 600; border-top: 0; }
.rubric .score { white-space: nowrap; font-weight: 700; }

.timer { font-variant-numeric: tabular-nums; font-weight: 700; font-size: 1.1rem; }
.timer.low { color: var(--bad); }

.kbd { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .85em; background: var(--surface-2); padding: 1px 6px; border-radius: 6px; }
.katex { font-size: 1.05em; }

.reaction { margin: 6px 0 10px; }
.reaction img { display: block; max-width: 100%; width: auto; height: auto; max-height: 200px; border-radius: 10px; }
.reaction .attribution { margin-top: 4px; font-size: .7rem; }
.emoji-burst { font-size: 2.2rem; line-height: 1; padding: 6px 0; }
.emoji-burst span { display: inline-block; animation: pop .7s cubic-bezier(.2,1.6,.4,1) both; }
.emoji-burst.miss span { animation-name: wobble; }
@keyframes pop { 0% { transform: scale(0) rotate(-30deg); opacity: 0; } 100% { transform: scale(1) rotate(0); opacity: 1; } }
@keyframes wobble { 0% { transform: translateY(-14px) rotate(-12deg); opacity: 0; } 60% { transform: translateY(2px) rotate(8deg); opacity: 1; } 100% { transform: none; } }
.confetti { position: fixed; inset: 0; width: 100vw; height: 100vh; pointer-events: none; z-index: 40; }
@media (prefers-reduced-motion: reduce) { .emoji-burst span { animation: none; } .confetti { display: none; } }
