:root {
  --bg: #f5f7fb;
  --surface: #ffffff;
  --surface-2: #f0f3f9;
  --text: #16202e;
  --muted: #5c6b80;
  --border: #e2e8f2;
  --brand: #2f6df6;
  --brand-ink: #ffffff;
  --good: #1f9d55;
  --good-bg: #e7f7ee;
  --bad: #e03131;
  --bad-bg: #fdeaea;
  --warn: #b7791f;
  --shadow: 0 1px 2px rgba(16, 32, 64, .06), 0 8px 24px rgba(16, 32, 64, .06);
  --radius: 14px;
}
:root[data-theme="dark"] {
  --bg: #0e1420;
  --surface: #161e2e;
  --surface-2: #1d2740;
  --text: #e8eef8;
  --muted: #9fb0c8;
  --border: #26324a;
  --brand: #5a8bff;
  --brand-ink: #0b1220;
  --good: #45c17d;
  --good-bg: #12301f;
  --bad: #ff6b6b;
  --bad-bg: #331616;
  --warn: #e0b050;
  --shadow: 0 1px 2px rgba(0,0,0,.3), 0 10px 30px rgba(0,0,0,.35);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ---- top bar ---- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
}
.brand { font-weight: 700; font-size: 1.15rem; text-decoration: none; color: var(--text); }
.topnav { display: flex; gap: 8px; align-items: center; }
.topnav a {
  text-decoration: none; color: var(--muted); padding: 6px 12px; border-radius: 8px; font-weight: 600;
}
.topnav a:hover, .topnav a.active { color: var(--text); background: var(--surface-2); }
.ghost-btn {
  background: var(--surface-2); border: 1px solid var(--border); color: var(--text);
  border-radius: 8px; padding: 6px 10px; cursor: pointer; font-size: 1rem;
}

.container { width: 100%; max-width: 900px; margin: 0 auto; padding: 24px 20px 48px; flex: 1; }
.footer { text-align: center; color: var(--muted); font-size: .85rem; padding: 18px; border-top: 1px solid var(--border); }
.loading { text-align: center; color: var(--muted); padding: 60px 0; }

/* ---- headings ---- */
h1 { font-size: 1.7rem; margin: 0 0 4px; }
h2 { font-size: 1.25rem; margin: 28px 0 12px; }
.subtitle { color: var(--muted); margin: 0 0 24px; }

/* ---- stat row ---- */
.stat-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin: 0 0 28px; }
.stat {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px; box-shadow: var(--shadow);
}
.stat .num { font-size: 1.5rem; font-weight: 700; }
.stat .lbl { color: var(--muted); font-size: .82rem; }

/* ---- category cards ---- */
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 16px; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 10px;
}
.card h3 { margin: 0; font-size: 1.1rem; }
.card .blurb { color: var(--muted); font-size: .9rem; flex: 1; }
.card .meta { display: flex; justify-content: space-between; align-items: center; font-size: .82rem; color: var(--muted); }
.progress-bar { height: 7px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.progress-bar > span { display: block; height: 100%; background: var(--brand); border-radius: 99px; }
.card .actions { display: flex; gap: 8px; }

/* "new questions only" toggle */
.toggle { display: flex; align-items: center; gap: 9px; margin-top: 14px; color: var(--muted); font-size: .92rem; cursor: pointer; user-select: none; }
.toggle input { width: 16px; height: 16px; cursor: pointer; accent-color: var(--brand); }

/* ---- buttons ---- */
.btn {
  appearance: none; border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
  padding: 9px 14px; border-radius: 10px; font-weight: 600; cursor: pointer; text-decoration: none;
  font-size: .92rem; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
}
.btn:hover { border-color: var(--brand); }
.btn.primary { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }
.btn.primary:hover { filter: brightness(1.05); }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* ---- setup panel ---- */
.panel {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 20px; box-shadow: var(--shadow); margin-bottom: 20px;
}
.field { margin-bottom: 16px; }
.field > label { display: block; font-weight: 600; margin-bottom: 8px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
  padding: 7px 13px; border-radius: 99px; cursor: pointer; font-size: .88rem; font-weight: 600;
}
.chip.active { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }

/* ---- quiz ---- */
.quiz-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; color: var(--muted); font-size: .9rem; gap: 12px; }
.q-progress { height: 6px; background: var(--surface-2); border-radius: 99px; overflow: hidden; margin-bottom: 20px; }
.q-progress > span { display: block; height: 100%; background: var(--brand); transition: width .25s; }
.q-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 22px; box-shadow: var(--shadow); }
.q-tags { display: flex; gap: 8px; margin-bottom: 12px; flex-wrap: wrap; }
.tag { font-size: .72rem; text-transform: uppercase; letter-spacing: .04em; font-weight: 700; color: var(--muted); background: var(--surface-2); padding: 3px 8px; border-radius: 6px; }
.q-directions {
  font-size: .9rem; color: var(--muted); font-style: italic;
  border-left: 3px solid var(--brand); padding: 6px 0 6px 12px; margin: 0 0 12px;
  background: var(--surface-2); border-radius: 0 8px 8px 0;
}
.q-text { font-size: 1.15rem; font-weight: 600; margin: 0 0 18px; white-space: pre-wrap; }
.emph { font-style: italic; text-decoration: underline; text-underline-offset: 3px; }
.q-image-wrap { margin: 0 0 18px; overflow-x: auto; text-align: center; }
.q-image {
  /* Scale small line-art figures up to fill the card (source images are tiny) */
  width: 100%; max-width: 680px; height: auto;
  border: 1px solid var(--border); border-radius: 10px;
  background: #fff; padding: 16px;
  image-rendering: auto;
}
.options { display: flex; flex-direction: column; gap: 10px; }
.option {
  display: flex; align-items: center; gap: 12px; text-align: left; width: 100%;
  padding: 13px 15px; border-radius: 12px; border: 1.5px solid var(--border);
  background: var(--surface); color: var(--text); cursor: pointer; font-size: 1rem;
}
.option:hover:not(:disabled) { border-color: var(--brand); }
.option .key {
  flex: 0 0 26px; height: 26px; border-radius: 6px; background: var(--surface-2);
  display: grid; place-items: center; font-weight: 700; font-size: .85rem;
}
.option.selected { border-color: var(--brand); }
.option.correct { border-color: var(--good); background: var(--good-bg); }
.option.correct .key { background: var(--good); color: #fff; }
.option.wrong { border-color: var(--bad); background: var(--bad-bg); }
.option.wrong .key { background: var(--bad); color: #fff; }
.option:disabled { cursor: default; }

/* ---- feedback ---- */
.feedback { margin-top: 18px; border-radius: 12px; padding: 15px 16px; border: 1px solid var(--border); background: var(--surface-2); }
.feedback.correct { border-color: var(--good); }
.feedback.wrong { border-color: var(--bad); }
.feedback .verdict { font-weight: 700; margin-bottom: 6px; }
.feedback.correct .verdict { color: var(--good); }
.feedback.wrong .verdict { color: var(--bad); }
.feedback .why-label { font-weight: 700; margin: 8px 0 2px; }
.feedback .explanation { color: var(--text); white-space: pre-wrap; }
.media-note { color: var(--warn); font-size: .82rem; margin-top: 8px; }
.src { font-size: .78rem; margin-top: 10px; }
.src a { color: var(--muted); }

.quiz-actions { display: flex; justify-content: space-between; gap: 10px; margin-top: 20px; }

/* ---- results ---- */
.score-hero { text-align: center; padding: 26px; }
.score-ring { font-size: 3rem; font-weight: 800; }
.score-sub { color: var(--muted); }
.review-item { border: 1px solid var(--border); border-radius: 12px; padding: 14px 16px; margin-bottom: 12px; background: var(--surface); }
.review-item .rq { font-weight: 600; margin-bottom: 8px; }
.review-line { font-size: .92rem; margin: 3px 0; }
.review-line .ok { color: var(--good); font-weight: 700; }
.review-line .no { color: var(--bad); font-weight: 700; }

.notice { background: var(--surface-2); border: 1px dashed var(--border); border-radius: 12px; padding: 16px; color: var(--muted); }

/* ---- users ---- */
.userbox { display: flex; align-items: center; gap: 8px; }
.user-chip {
  font-weight: 700; font-size: .9rem; color: var(--text);
  background: var(--surface-2); border: 1px solid var(--border);
  padding: 6px 12px; border-radius: 99px; text-transform: capitalize;
}
.login-wrap { display: flex; justify-content: center; padding-top: 24px; }
.login-card { max-width: 440px; width: 100%; }
.text-input {
  width: 100%; padding: 12px 14px; font-size: 1rem; border-radius: 10px;
  border: 1.5px solid var(--border); background: var(--surface); color: var(--text);
}
.text-input:focus { outline: none; border-color: var(--brand); }
.login-err { color: var(--bad); font-size: .88rem; margin-top: 8px; }

@media (max-width: 640px) {
  .topnav a[data-nav] { display: none; }  /* keep the top bar compact on mobile */
  .user-chip { max-width: 96px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
}

@media (max-width: 640px) {
  .stat-row { grid-template-columns: repeat(2, 1fr); }
  .quiz-actions { flex-direction: column-reverse; }
  .quiz-actions .btn { width: 100%; }
}
