:root {
  --bg: #f5f3ef;
  --surface: #fdfcfa;
  --surface-2: #efebe5;
  --text: #2a2825;
  --muted: #6e6960;
  --border: #e2ddd5;
  --accent: #7d705f;
  --accent-soft: #e9e3da;
  --correct: #5c7a5a;
  --correct-soft: #e5ece3;
  --wrong: #9a5b52;
  --wrong-soft: #f2e4e1;
  --shadow: 0 1px 2px rgba(40, 35, 28, 0.05), 0 4px 16px rgba(40, 35, 28, 0.04);
  --radius: 12px;
  --serif: "Source Serif 4", Georgia, "Times New Roman", serif;
  --sans: "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1b1a18;
    --surface: #23211f;
    --surface-2: #2c2a27;
    --text: #ece8e1;
    --muted: #a39d93;
    --border: #3a3733;
    --accent: #c2b5a2;
    --accent-soft: #34302b;
    --correct: #9cbc98;
    --correct-soft: #26302a;
    --wrong: #d49a90;
    --wrong-soft: #362826;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  --bg: #1b1a18;
  --surface: #23211f;
  --surface-2: #2c2a27;
  --text: #ece8e1;
  --muted: #a39d93;
  --border: #3a3733;
  --accent: #c2b5a2;
  --accent-soft: #34302b;
  --correct: #9cbc98;
  --correct-soft: #26302a;
  --wrong: #d49a90;
  --wrong-soft: #362826;
  --shadow: none;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.6 var(--sans);
}
h1, h2, h3 { font-family: var(--serif); font-weight: 600; line-height: 1.25; margin: 0 0 0.5em; }
h1 { font-size: clamp(1.9rem, 4vw, 2.6rem); letter-spacing: -0.01em; }
h2 { font-size: 1.5rem; }
h3 { font-size: 1.15rem; }
p { margin: 0 0 1em; }
a { color: inherit; }
button { font: inherit; color: inherit; cursor: pointer; }

/* ---------- Header ---------- */
.site-header {
  position: sticky; top: 0; z-index: 10;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.header-inner {
  max-width: 1040px; margin: 0 auto; padding: 0 16px;
  display: flex; align-items: center; gap: 16px; height: 60px;
}
.brand { font-family: var(--serif); font-weight: 600; font-size: 1.1rem; text-decoration: none; white-space: nowrap; }
.brand span { color: var(--muted); font-weight: 400; }
nav { display: flex; gap: 4px; margin-left: auto; overflow-x: auto; scrollbar-width: none; }
nav::-webkit-scrollbar { display: none; }
nav a {
  text-decoration: none; padding: 6px 12px; border-radius: 8px;
  color: var(--muted); font-size: 0.93rem; white-space: nowrap;
}
nav a:hover { color: var(--text); background: var(--surface-2); }
nav a.active { color: var(--text); background: var(--accent-soft); }
.theme-toggle {
  border: 1px solid var(--border); background: var(--surface); border-radius: 8px;
  width: 36px; height: 36px; flex-shrink: 0; display: grid; place-items: center;
}

/* ---------- Layout ---------- */
main { max-width: 1040px; margin: 0 auto; padding: 40px 16px 80px; }
.lead { color: var(--muted); font-size: 1.08rem; max-width: 640px; }
.eyebrow { text-transform: uppercase; letter-spacing: 0.08em; font-size: 0.75rem; color: var(--muted); font-weight: 600; margin-bottom: 8px; }
.section { margin-top: 48px; }
.row { display: flex; gap: 12px; flex-wrap: wrap; align-items: center; }
.spacer { flex: 1; }
.muted { color: var(--muted); }
.small { font-size: 0.88rem; }

.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow);
}
.grid { display: grid; gap: 16px; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }
.grid-4 { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 10px 18px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface); text-decoration: none; font-weight: 500; font-size: 0.95rem;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--text); color: var(--bg); border-color: var(--text); }
.btn.primary:hover { opacity: 0.9; }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }

.hero { padding: 24px 0 8px; }
.hero .row { margin-top: 24px; }

/* ---------- Unit cards ---------- */
.unit-card { display: flex; flex-direction: column; gap: 8px; text-decoration: none; transition: border-color 0.15s; }
.unit-card:hover { border-color: var(--accent); }
.unit-num { font-family: var(--serif); font-size: 0.9rem; color: var(--muted); }
.unit-card h3 { margin: 0; }
.meta { display: flex; gap: 8px; flex-wrap: wrap; font-size: 0.82rem; color: var(--muted); }
.pill { background: var(--surface-2); border-radius: 999px; padding: 2px 10px; font-size: 0.8rem; color: var(--muted); }
.progress { height: 6px; background: var(--surface-2); border-radius: 999px; overflow: hidden; margin-top: auto; }
.progress > div { height: 100%; background: var(--accent); border-radius: 999px; transition: width 0.3s; }

.concepts { list-style: none; padding: 0; margin: 0; display: grid; gap: 10px; }
.concepts li { padding: 12px 16px; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; }

/* ---------- Quiz ---------- */
.setup label { display: block; font-weight: 500; margin-bottom: 8px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  border: 1px solid var(--border); background: var(--surface); border-radius: 999px;
  padding: 6px 14px; font-size: 0.9rem;
}
.chip[aria-pressed="true"] { background: var(--text); color: var(--bg); border-color: var(--text); }

.quiz { max-width: 720px; margin: 0 auto; }
.quiz-top { display: flex; justify-content: space-between; color: var(--muted); font-size: 0.9rem; margin-bottom: 12px; }
.question { font-family: var(--serif); font-size: 1.3rem; line-height: 1.4; margin-bottom: 20px; }
.options { display: grid; gap: 10px; }
.option {
  text-align: left; padding: 14px 16px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface);
  display: flex; gap: 12px; align-items: flex-start; line-height: 1.45;
}
.option:hover:not(:disabled) { border-color: var(--accent); }
.option .letter {
  flex-shrink: 0; width: 26px; height: 26px; border-radius: 50%;
  display: grid; place-items: center; font-size: 0.8rem; font-weight: 600;
  background: var(--surface-2); color: var(--muted);
}
.option.correct { background: var(--correct-soft); border-color: var(--correct); }
.option.correct .letter { background: var(--correct); color: var(--surface); }
.option.wrong { background: var(--wrong-soft); border-color: var(--wrong); }
.option.wrong .letter { background: var(--wrong); color: var(--surface); }
.option:disabled { cursor: default; }
.explain { margin-top: 16px; padding: 14px 16px; border-left: 3px solid var(--accent); background: var(--surface-2); border-radius: 0 10px 10px 0; }
.quiz-nav { display: flex; justify-content: flex-end; margin-top: 20px; }

.score-big { font-family: var(--serif); font-size: 3.5rem; line-height: 1; margin: 8px 0; }
.review-item { padding: 14px 0; border-top: 1px solid var(--border); }
.review-item:first-child { border-top: 0; }
.tag-right { color: var(--correct); font-weight: 600; }
.tag-wrong { color: var(--wrong); font-weight: 600; }

/* ---------- Flashcards ---------- */
.flash-wrap { max-width: 560px; margin: 24px auto 0; }
.flashcard { perspective: 1200px; height: 280px; cursor: pointer; }
.flash-inner { position: relative; width: 100%; height: 100%; transition: transform 0.5s; transform-style: preserve-3d; }
.flashcard.flipped .flash-inner { transform: rotateY(180deg); }
.flash-face {
  position: absolute; inset: 0; backface-visibility: hidden;
  display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center;
  padding: 28px; border-radius: var(--radius); border: 1px solid var(--border); background: var(--surface); box-shadow: var(--shadow);
}
.flash-face.back { transform: rotateY(180deg); background: var(--surface-2); }
.flash-term { font-family: var(--serif); font-size: 1.8rem; }
.flash-def { font-size: 1.08rem; line-height: 1.55; }
.flash-hint { position: absolute; bottom: 14px; font-size: 0.78rem; color: var(--muted); }
.flash-controls { display: flex; justify-content: space-between; align-items: center; margin-top: 16px; }

/* ---------- Writing ---------- */
table { width: 100%; border-collapse: collapse; font-size: 0.93rem; }
th, td { text-align: left; padding: 10px 8px; border-top: 1px solid var(--border); vertical-align: top; }
th { font-weight: 600; color: var(--muted); font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.05em; border-top: 0; }
td:nth-child(2) { white-space: nowrap; color: var(--muted); }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 20px; }
.tab { border: 0; background: none; padding: 10px 14px; color: var(--muted); border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tab[aria-selected="true"] { color: var(--text); border-bottom-color: var(--text); }
.tips { margin: 16px 0 0; padding-left: 20px; color: var(--muted); }
.prompt-box { font-family: var(--serif); font-size: 1.2rem; line-height: 1.5; margin: 12px 0 20px; }
.timer { font-variant-numeric: tabular-nums; font-family: var(--serif); font-size: 2rem; }
textarea {
  width: 100%; min-height: 260px; padding: 14px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font: 1rem/1.6 var(--sans); resize: vertical;
}
textarea:focus, .btn:focus-visible, .chip:focus-visible, .option:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.stat { text-align: left; }
.stat .num { font-family: var(--serif); font-size: 2rem; line-height: 1.1; }
.stat .label { color: var(--muted); font-size: 0.85rem; }

footer { border-top: 1px solid var(--border); padding: 24px 16px; text-align: center; color: var(--muted); font-size: 0.85rem; }

@media (max-width: 640px) {
  main { padding-top: 24px; }
  .brand span { display: none; }
  .question { font-size: 1.15rem; }
  .flashcard { height: 240px; }
  td:nth-child(2) { white-space: normal; }
}

/* ---------- Shared additions ---------- */
[hidden] { display: none !important; }
.btn-sm { padding: 6px 12px; font-size: 0.85rem; border-radius: 8px; }
.btn.wide { width: 100%; }
.center { text-align: center; }
.chip-sm { padding: 2px 10px; }
a.chip { text-decoration: none; }
.grid-3 { display: grid; gap: 12px; grid-template-columns: repeat(3, minmax(0, 1fr)); }
.notice { margin-top: 16px; padding: 12px 16px; border-radius: 10px; background: var(--correct-soft); border: 1px solid var(--correct); }
.pill-good { background: var(--correct-soft); color: var(--correct); font-weight: 600; }
.linkish { border: 0; background: none; padding: 0; color: var(--text); text-decoration: underline; text-underline-offset: 2px; font-size: inherit; }

/* ---------- Header account ---------- */
.acct { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }
.acct-name { font-size: 0.88rem; color: var(--muted); }

.booting { min-height: 60vh; display: grid; place-items: center; color: var(--muted); }

/* ---------- Auth ---------- */
.auth {
  max-width: 1040px; margin: 0 auto; padding: 72px 16px;
  display: grid; gap: 48px; grid-template-columns: 1.1fr 1fr; align-items: center; min-height: 100vh;
}
.auth-points { list-style: none; padding: 0; margin: 24px 0 0; display: grid; gap: 10px; color: var(--muted); }
.auth-points li { padding-left: 22px; position: relative; }
.auth-points li::before { content: ""; position: absolute; left: 0; top: 0.6em; width: 10px; height: 2px; background: var(--accent); }
.auth-card { padding: 28px; }
.auth-card form { display: grid; gap: 14px; margin-top: 20px; }
.field { display: grid; gap: 6px; font-size: 0.88rem; font-weight: 500; }
.field small { font-weight: 400; color: var(--muted); }
.field-row { display: grid; gap: 12px; grid-template-columns: 1fr 1fr; }
.field input {
  font: 1rem var(--sans); color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: 10px; padding: 11px 12px; width: 100%;
}
.field input:focus { outline: 2px solid var(--accent); outline-offset: 1px; border-color: transparent; }
.auth-msg { min-height: 1.2em; margin: 14px 0 0; font-size: 0.9rem; }
.auth-msg.bad { color: var(--wrong); }
.auth-msg.good { color: var(--correct); }

/* Segmented control (auth tabs, mastery/accuracy) */
.seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; background: var(--surface-2); padding: 4px; border-radius: 10px; gap: 4px; }
.seg-inline { display: inline-grid; }
.seg button { border: 0; background: none; padding: 8px 16px; border-radius: 7px; color: var(--muted); font-weight: 500; }
.seg button[aria-selected="true"] { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08); }

/* ---------- Sources (College Board–style stimulus) ---------- */
.source { margin: 0 0 20px; padding: 18px 20px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.source-kind { font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); font-weight: 600; margin-bottom: 8px; }
.source-text { font-family: var(--serif); font-size: 1.05rem; line-height: 1.6; margin: 0; }
.source figcaption { margin-top: 10px; font-size: 0.85rem; color: var(--muted); }
.source figcaption::before { content: "Source: "; font-weight: 600; }
.data-table caption { text-align: left; font-weight: 600; padding-bottom: 6px; font-size: 0.92rem; }
.data-table td:nth-child(2) { white-space: nowrap; color: var(--text); font-variant-numeric: tabular-nums; }

/* ---------- Textbook chapter tabs ---------- */
.book { display: grid; grid-template-columns: 300px minmax(0, 1fr); gap: 20px; align-items: start; }
.chapter-tabs {
  display: flex; flex-direction: column; gap: 2px; max-height: 70vh; overflow-y: auto;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 6px;
  position: sticky; top: 76px;
}
.chapter-tab {
  display: grid; grid-template-columns: 28px minmax(0, 1fr) auto; gap: 8px; align-items: center;
  text-align: left; border: 0; background: none; padding: 9px 10px; border-radius: 8px; font-size: 0.88rem; line-height: 1.3;
}
.chapter-tab:hover { background: var(--surface-2); }
.chapter-tab[aria-selected="true"] { background: var(--accent-soft); }
.chapter-tab.is-locked .ct-title, .chapter-tab.is-locked .ct-num { color: var(--muted); }
.ct-num { font-family: var(--serif); font-weight: 600; color: var(--accent); }
.ct-meta { font-size: 0.78rem; color: var(--muted); display: flex; }
.chapter-panel { min-height: 300px; }
.locked-box { display: flex; gap: 12px; align-items: flex-start; margin-top: 20px; padding: 16px; border-radius: 10px; background: var(--surface-2); color: var(--text); }
.locked-box svg { margin-top: 3px; flex-shrink: 0; color: var(--muted); }
.btn svg { flex-shrink: 0; }
.lock-inline { color: var(--muted); vertical-align: -2px; }

/* ---------- Progress meters ---------- */
.meter-row { display: grid; grid-template-columns: minmax(0, 1.4fr) minmax(0, 1fr) 84px; gap: 16px; align-items: center; padding: 12px 0; border-top: 1px solid var(--border); }
.meter-label a { text-decoration: none; font-weight: 500; }
.meter-label a:hover { text-decoration: underline; }
.meter { height: 8px; background: var(--surface-2); border-radius: 999px; overflow: hidden; }
.meter span { display: block; height: 100%; background: var(--accent); border-radius: 999px; }
.meter.acc span { background: var(--correct); }
.meter-val { text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; font-size: 0.92rem; }
.report { margin: 14px 0 0; padding: 14px; background: var(--surface-2); border-radius: 10px; font: 0.8rem/1.5 ui-monospace, SFMono-Regular, Menlo, monospace; overflow-x: auto; white-space: pre; }

@media (max-width: 860px) {
  .auth { grid-template-columns: 1fr; gap: 28px; padding-top: 40px; min-height: 0; }
  .book { grid-template-columns: 1fr; }
  .chapter-tabs { flex-direction: row; overflow-x: auto; max-height: none; position: static; }
  .chapter-tab { grid-template-columns: auto auto auto; white-space: nowrap; flex-shrink: 0; }
}
@media (max-width: 640px) {
  .acct-name { display: none; }
  .header-inner { gap: 8px; }
  .field-row { grid-template-columns: 1fr; }
  .grid-3 { grid-template-columns: 1fr 1fr 1fr; }
  .meter-row { grid-template-columns: 1fr 64px; }
  .meter-row .meter { grid-column: 1 / -1; grid-row: 2; }
  .source { padding: 14px 16px; }
}

@media (max-width: 760px) {
  .header-inner { flex-wrap: wrap; height: auto; padding-top: 10px; padding-bottom: 6px; row-gap: 6px; }
  .acct { margin-left: auto; }
  nav { order: 3; width: 100%; margin-left: -8px; }
}

.deck-pick { display: flex; align-items: center; gap: 10px; margin-top: 16px; font-weight: 500; font-size: 0.9rem; }
.deck-pick select {
  font: 0.95rem var(--sans); color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 10px; padding: 9px 12px; max-width: 100%; min-width: 0; flex: 1 1 auto;
}
@media (min-width: 641px) { .deck-pick select { flex: 0 1 420px; } }

/* ---------- Writing practice ---------- */
.pick-grid { display: grid; gap: 12px; grid-template-columns: 2fr 1fr 1.4fr; margin-top: 12px; }
.pick-grid select, .score-row select {
  font: 0.95rem var(--sans); color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; width: 100%; min-width: 0;
}
.official { display: flex; gap: 16px; align-items: center; justify-content: space-between; flex-wrap: wrap; margin-top: 18px; padding: 16px; border-radius: 10px; background: var(--surface-2); }
.score-row { display: grid; grid-template-columns: minmax(0, 1fr) 110px; gap: 16px; align-items: center; padding: 12px 0; border-top: 1px solid var(--border); }
.score-total { font-family: var(--serif); font-size: 1.6rem; }
@media (max-width: 760px) { .pick-grid { grid-template-columns: 1fr; } }
