:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #eef2f6;
  --text: #17212b;
  --muted: #5b6b7a;
  --line: #d8e0e7;
  --primary: #1f5f8b;
  --primary-ink: #ffffff;
  --good: #1d7a46;
  --good-bg: #e3f4ea;
  --bad: #b3261e;
  --bad-bg: #fbe7e5;
  --warn: #8a5a00;
  --warn-bg: #fff3d6;
  --d1: #1f5f8b;
  --d2: #7a4fb0;
  --d3: #b0631f;
  --radius: 12px;
  --tabbar-h: 60px;
  --shadow: 0 1px 2px rgba(0, 0, 0, .06), 0 2px 8px rgba(0, 0, 0, .05);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11161b;
    --surface: #1a2128;
    --surface-2: #232c35;
    --text: #e6edf3;
    --muted: #9aa8b5;
    --line: #2f3a45;
    --primary: #5aa7dc;
    --primary-ink: #0b1a26;
    --good: #5fd08f;
    --good-bg: #173325;
    --bad: #ff8a80;
    --bad-bg: #3a1d1b;
    --warn: #f0c060;
    --warn-bg: #3a2e12;
    --d1: #5aa7dc;
    --d2: #b393e6;
    --d3: #e3a066;
    --shadow: none;
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
}

a { color: var(--primary); }

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 16px;
  padding-top: calc(10px + env(safe-area-inset-top));
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}

.brand { font-weight: 700; color: var(--text); text-decoration: none; font-size: 1.05rem; }
.topbar-extra { font-variant-numeric: tabular-nums; font-weight: 600; }
.topbar-extra.low { color: var(--bad); }

.view { max-width: 680px; margin: 0 auto; padding: 16px; outline: none; }
.loading { color: var(--muted); text-align: center; margin-top: 40px; }

h1 { font-size: 1.4rem; margin: 4px 0 12px; line-height: 1.25; }
h2 { font-size: 1.1rem; margin: 20px 0 8px; }
h3 { font-size: 1rem; margin: 12px 0 6px; }
p { margin: 8px 0; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  margin: 12px 0;
  box-shadow: var(--shadow);
}

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.stat { text-align: center; padding: 12px; }
.stat .num { font-size: 1.6rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat .lbl { color: var(--muted); font-size: .85rem; }

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 48px;
  padding: 10px 18px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  -webkit-tap-highlight-color: transparent;
}
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn.danger { color: var(--bad); border-color: var(--bad); }
.btn.block { display: flex; width: 100%; }
.btn[disabled] { opacity: .5; cursor: default; }
.btn-row { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 12px; }
.btn-row .btn { flex: 1 1 0; }

.mode-list { display: grid; gap: 10px; }
.mode {
  display: flex;
  gap: 14px;
  align-items: center;
  padding: 14px 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  color: var(--text);
  text-decoration: none;
  box-shadow: var(--shadow);
}
.mode .ico { font-size: 1.5rem; width: 36px; text-align: center; color: var(--primary); }
.mode b { display: block; }
.mode span.desc { color: var(--muted); font-size: .9rem; }

/* Form controls */
.field { display: block; margin: 12px 0; font-weight: 600; }
.field > span { display: block; margin-bottom: 6px; }
select, input[type="date"] {
  width: 100%;
  min-height: 48px;
  padding: 8px 12px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  font: inherit;
}

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  min-height: 40px;
  padding: 6px 14px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  cursor: pointer;
}
.chip[aria-pressed="true"] { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); font-weight: 600; }

/* Question UI */
.qmeta { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin-bottom: 10px; font-size: .8rem; }
.tag {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
  font-weight: 600;
}
.tag.d1 { color: var(--d1); }
.tag.d2 { color: var(--d2); }
.tag.d3 { color: var(--d3); }
.tag.ohio { color: var(--warn); background: var(--warn-bg); }
.dots { letter-spacing: 1px; color: var(--muted); }

.stem { font-size: 1.08rem; font-weight: 600; margin: 4px 0 14px; }
.choices { display: grid; gap: 10px; }
.choice {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  width: 100%;
  min-height: 52px;
  padding: 12px 14px;
  text-align: left;
  border-radius: 10px;
  border: 1.5px solid var(--line);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.choice .letter {
  flex: 0 0 26px;
  height: 26px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--surface-2);
  font-weight: 700;
  font-size: .85rem;
}
.choice.selected { border-color: var(--primary); box-shadow: inset 0 0 0 1px var(--primary); }
.choice.selected .letter { background: var(--primary); color: var(--primary-ink); }
.choice.correct { border-color: var(--good); background: var(--good-bg); }
.choice.correct .letter { background: var(--good); color: var(--surface); }
.choice.wrong { border-color: var(--bad); background: var(--bad-bg); }
.choice.wrong .letter { background: var(--bad); color: var(--surface); }
.choice[disabled] { cursor: default; }

.feedback { margin-top: 14px; padding: 12px 14px; border-radius: 10px; border-left: 4px solid; }
.feedback.ok { border-color: var(--good); background: var(--good-bg); }
.feedback.no { border-color: var(--bad); background: var(--bad-bg); }
.feedback .verdict { font-weight: 700; }
.source { font-size: .8rem; color: var(--muted); margin-top: 6px; }

.progressline { height: 6px; background: var(--surface-2); border-radius: 999px; overflow: hidden; margin: 6px 0 14px; }
.progressline > i { display: block; height: 100%; background: var(--primary); width: 0; }

/* Flashcards */
.flashcard {
  min-height: 260px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  cursor: pointer;
  user-select: none;
}
.flashcard .answer { font-size: 1.05rem; font-weight: 700; color: var(--good); margin: 6px 0 8px; }
.flashcard .hint { color: var(--muted); font-size: .85rem; text-align: center; margin-top: 16px; }

/* Bars and tables */
.bar-row { margin: 10px 0; }
.bar-head { display: flex; justify-content: space-between; gap: 8px; font-size: .9rem; }
.bar-head > span { white-space: nowrap; }
.bar-head b { font-weight: 600; }
.bar { height: 10px; background: var(--surface-2); border-radius: 999px; overflow: hidden; margin-top: 4px; }
.bar > i { display: block; height: 100%; width: 0; background: var(--primary); }
.bar.d1 > i { background: var(--d1); }
.bar.d2 > i { background: var(--d2); }
.bar.d3 > i { background: var(--d3); }
.bar.low > i { background: var(--bad); }
.bar.mid > i { background: var(--warn); }
.bar.high > i { background: var(--good); }

table { width: 100%; border-collapse: collapse; font-size: .9rem; }
th, td { padding: 8px 6px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
th { color: var(--muted); font-weight: 600; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }

.weak-item { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.weak-item:last-child { border-bottom: 0; }
.weak-item .grow { flex: 1; min-width: 0; }
.weak-item .btn { min-height: 40px; padding: 6px 12px; }

/* Mock exam */
.palette { display: grid; grid-template-columns: repeat(auto-fill, minmax(42px, 1fr)); gap: 6px; }
.palette button {
  min-height: 42px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: .85rem;
  font-variant-numeric: tabular-nums;
  cursor: pointer;
}
.palette button.answered { background: var(--surface-2); font-weight: 700; }
.palette button.flagged { border-color: var(--warn); box-shadow: inset 0 0 0 2px var(--warn); }
.palette button.current { outline: 2px solid var(--primary); outline-offset: 1px; }
.palette button.right { background: var(--good-bg); border-color: var(--good); }
.palette button.miss { background: var(--bad-bg); border-color: var(--bad); }
.legend { display: flex; flex-wrap: wrap; gap: 12px; font-size: .8rem; color: var(--muted); margin: 8px 0; }

.flag-btn[aria-pressed="true"] { color: var(--warn); border-color: var(--warn); }

.score-big { font-size: 2.6rem; font-weight: 800; text-align: center; font-variant-numeric: tabular-nums; margin: 4px 0; }

.notice { padding: 10px 14px; border-radius: 10px; background: var(--warn-bg); color: var(--text); border-left: 4px solid var(--warn); font-size: .92rem; }

details.review { border-bottom: 1px solid var(--line); padding: 8px 0; }
details.review summary { cursor: pointer; font-weight: 600; list-style-position: outside; }
details.review .ok { color: var(--good); }
details.review .no { color: var(--bad); }

/* Tab bar */
.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 10;
  display: flex;
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface);
  border-top: 1px solid var(--line);
}
.tabbar a {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  color: var(--muted);
  text-decoration: none;
  font-size: .75rem;
  font-weight: 600;
}
.tabbar a .ico { font-size: 1.25rem; line-height: 1; }
.tabbar a[aria-current="page"] { color: var(--primary); }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--tabbar-h) + 16px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  max-width: calc(100% - 32px);
  padding: 10px 16px;
  border-radius: 10px;
  background: var(--text);
  color: var(--bg);
  font-size: .9rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s;
  z-index: 20;
}
.toast.show { opacity: 1; }

.hidden { display: none !important; }
