:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --surface-2: #efeee8;
  --ink: #1b1a17;
  --ink-2: #4a4740;
  --ink-3: #7c786e;
  --line: #e0ded4;
  --line-2: #cbc8bb;
  --accent: #16624f;
  --accent-soft: #e4efeA;
  --accent-ink: #0d3f33;
  --amber: #8a5a00;
  --amber-soft: #fbf0d8;
  --ok: #1a7346;
  --ok-soft: #e6f3ec;
  --bad: #a32621;
  --bad-soft: #fbeae9;
  --radius: 10px;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, "Helvetica Neue", Arial, sans-serif;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --sidebar: 268px;
}

* { box-sizing: border-box; }

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 16px;
  line-height: 1.65;
  -webkit-font-smoothing: antialiased;
}

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

code, kbd { font-family: var(--mono); font-size: 0.88em; }

/* ---------- layout ---------- */

.shell { display: flex; min-height: 100vh; }

.sidebar {
  width: var(--sidebar);
  flex: 0 0 var(--sidebar);
  border-right: 1px solid var(--line);
  background: var(--surface);
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
  padding: 22px 18px 32px;
}

.brand { display: block; text-decoration: none; color: inherit; margin-bottom: 20px; }
.brand strong { display: block; font-size: 15px; font-weight: 600; letter-spacing: -0.01em; }
.brand span { display: block; font-size: 12px; color: var(--ink-3); margin-top: 2px; }

.nav-label {
  font-size: 11px;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  color: var(--ink-3);
  margin: 18px 0 8px;
}

.nav a {
  display: flex;
  align-items: baseline;
  gap: 8px;
  padding: 7px 9px;
  border-radius: 8px;
  text-decoration: none;
  color: var(--ink-2);
  font-size: 14px;
  line-height: 1.35;
}
.nav a:hover { background: var(--surface-2); color: var(--ink); }
.nav a.active { background: var(--accent-soft); color: var(--accent-ink); font-weight: 550; }
.nav a .n { font-size: 11px; color: var(--ink-3); min-width: 15px; font-variant-numeric: tabular-nums; }
.nav a.active .n { color: var(--accent); }

.nav .pct { margin-left: auto; font-size: 11px; color: var(--ink-3); font-variant-numeric: tabular-nums; }

.main { flex: 1; min-width: 0; }
.wrap { max-width: 780px; margin: 0 auto; padding: 40px 28px 96px; }

/* ---------- topbar (mobile) ---------- */

.topbar {
  display: none;
  position: sticky;
  top: 0;
  z-index: 30;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  padding: 10px 14px;
  align-items: center;
  gap: 10px;
}
.topbar strong { font-size: 14px; }
.menu-btn {
  border: 1px solid var(--line-2);
  background: var(--surface);
  border-radius: 8px;
  padding: 5px 10px;
  font: inherit;
  font-size: 13px;
  cursor: pointer;
}

/* ---------- headings ---------- */

.eyebrow {
  font-size: 11.5px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--ink-3);
  margin: 0 0 6px;
}

h1 { font-size: 30px; line-height: 1.2; letter-spacing: -0.02em; margin: 0 0 10px; font-weight: 600; }
h2 { font-size: 20px; margin: 40px 0 12px; font-weight: 600; letter-spacing: -0.01em; scroll-margin-top: 80px; }
h3 { font-size: 16px; margin: 24px 0 8px; font-weight: 600; }

.lede { color: var(--ink-2); font-size: 16.5px; margin: 0 0 4px; }

/* ---------- tabs ---------- */

.tabs {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  gap: 4px;
  background: var(--bg);
  padding: 10px 0;
  margin: 24px 0 8px;
  border-bottom: 1px solid var(--line);
  overflow-x: auto;
  scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
  border: 0;
  background: transparent;
  font: inherit;
  font-size: 14px;
  color: var(--ink-2);
  padding: 6px 12px;
  border-radius: 999px;
  cursor: pointer;
  white-space: nowrap;
}
.tab:hover { background: var(--surface-2); }
.tab[aria-selected="true"] { background: var(--ink); color: #fff; }
.tab .cnt { opacity: 0.6; font-size: 12px; margin-left: 4px; }

.panel[hidden] { display: none; }

/* ---------- notes ---------- */

.notes p { margin: 0 0 14px; }
.notes ul, .notes ol { margin: 0 0 16px; padding-left: 22px; }
.notes li { margin: 0 0 6px; }
.notes .sec { margin-bottom: 8px; }

.key {
  border-left: 3px solid var(--accent);
  background: var(--accent-soft);
  padding: 12px 14px;
  margin: 0 0 16px;
  border-radius: 0 8px 8px 0;
}
.key > strong:first-child { display: block; font-size: 11.5px; letter-spacing: 0.08em; text-transform: uppercase; color: var(--accent-ink); margin-bottom: 4px; }
.key p strong { font-weight: 600; color: inherit; }
.key p:last-child { margin-bottom: 0; }

.warnbox {
  border-left: 3px solid var(--amber);
  background: var(--amber-soft);
  padding: 12px 14px;
  margin: 0 0 16px;
  border-radius: 0 8px 8px 0;
}
.warnbox > strong:first-child { display: block; font-size: 11.5px; letter-spacing: 0.08em; text-transform: uppercase; color: var(--amber); margin-bottom: 4px; }
.warnbox p strong { font-weight: 600; color: inherit; }
.warnbox p:last-child { margin-bottom: 0; }

pre {
  background: #1e1d1a;
  color: #f2efe6;
  padding: 14px 16px;
  border-radius: var(--radius);
  overflow-x: auto;
  margin: 0 0 16px;
  font-size: 13px;
  line-height: 1.55;
}
pre code { font-size: inherit; color: inherit; }

figure.fig {
  margin: 0 0 18px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px 16px 10px;
}
figure.fig svg { width: 100%; height: auto; display: block; font-family: var(--sans); }
figure.fig figcaption { font-size: 13px; color: var(--ink-3); margin-top: 9px; line-height: 1.5; }

/* On narrow screens a 700-wide diagram would shrink to illegible text.
   Let the figure scroll inside its own box instead — the page never scrolls sideways. */
@media (max-width: 660px) {
  figure.fig { overflow-x: auto; }
  figure.fig svg { min-width: 580px; }
  figure.fig figcaption { position: sticky; left: 0; max-width: 100%; }
}
.qa .body figure.fig { background: var(--bg); }

svg .fl { fill: var(--surface); }
svg .fm { fill: var(--surface-2); }
svg .fa { fill: var(--accent-soft); }
svg .fw { fill: var(--amber-soft); }
svg .fb { fill: var(--bad-soft); }
svg .fk { fill: var(--ok-soft); }
svg .st { stroke: var(--line-2); fill: none; }
svg .sa { stroke: var(--accent); fill: none; }
svg .sw { stroke: var(--amber); fill: none; }
svg .sd { stroke: var(--line-2); fill: none; stroke-dasharray: 4 3; }
svg text { fill: var(--ink); font-size: 12px; }
svg text.s { fill: var(--ink-2); font-size: 11px; }
svg text.t { fill: var(--ink-3); font-size: 10.5px; letter-spacing: .06em; }
svg text.a { fill: var(--accent-ink); font-size: 11.5px; }
svg text.w { fill: var(--amber); font-size: 11.5px; }
svg text.b { font-weight: 600; }

.notes table, .tbl {
  width: 100%;
  border-collapse: collapse;
  margin: 0 0 18px;
  font-size: 14.5px;
  background: var(--surface);
  border-radius: var(--radius);
  overflow: hidden;
}
.notes th, .tbl th {
  text-align: left;
  background: var(--surface-2);
  padding: 9px 12px;
  font-weight: 600;
  font-size: 13px;
  border-bottom: 1px solid var(--line);
}
.notes td, .tbl td { padding: 9px 12px; border-bottom: 1px solid var(--line); vertical-align: top; }
.notes tr:last-child td, .tbl tr:last-child td { border-bottom: 0; }

.tablewrap { overflow-x: auto; }

.formula {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px 14px;
  margin: 0 0 16px;
  font-family: var(--mono);
  font-size: 14px;
  overflow-x: auto;
  white-space: pre-wrap;
}
.formula .lbl { font-family: var(--sans); font-size: 12px; color: var(--ink-3); display: block; margin-bottom: 4px; }

/* ---------- quiz ---------- */

.quizbar {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 12px;
  margin: 0 0 18px;
  position: sticky;
  top: 52px;
  z-index: 10;
}
.score { font-size: 13.5px; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.score b { color: var(--ink); }
.bar { flex: 1 1 120px; height: 5px; background: var(--surface-2); border-radius: 999px; overflow: hidden; min-width: 80px; }
.bar i { display: block; height: 100%; background: var(--accent); width: 0; transition: width .25s; }

.btn {
  border: 1px solid var(--line-2);
  background: var(--surface);
  border-radius: 8px;
  padding: 5px 11px;
  font: inherit;
  font-size: 13px;
  color: var(--ink-2);
  cursor: pointer;
}
.btn:hover { background: var(--surface-2); color: var(--ink); }
.btn[aria-pressed="true"] { background: var(--ink); border-color: var(--ink); color: #fff; }

.q {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px 18px;
  margin: 0 0 12px;
}
.q[hidden] { display: none; }
.q .stem { display: flex; gap: 10px; margin-bottom: 12px; }
.q .num { font-size: 12px; color: var(--ink-3); font-variant-numeric: tabular-nums; padding-top: 3px; min-width: 20px; }
.q .txt { flex: 1; }

.opts { display: grid; gap: 6px; }
.opt {
  display: flex;
  gap: 9px;
  align-items: flex-start;
  text-align: left;
  width: 100%;
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 8px;
  padding: 9px 11px;
  font: inherit;
  font-size: 14.5px;
  color: var(--ink);
  cursor: pointer;
  line-height: 1.45;
}
.opt:hover:not(:disabled) { border-color: var(--line-2); background: var(--surface-2); }
.opt .ltr {
  font-size: 12px;
  font-weight: 600;
  color: var(--ink-3);
  border: 1px solid var(--line-2);
  border-radius: 5px;
  min-width: 21px;
  height: 21px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  margin-top: 1px;
}
.opt.correct { background: var(--ok-soft); border-color: var(--ok); }
.opt.correct .ltr { border-color: var(--ok); color: var(--ok); }
.opt.wrong { background: var(--bad-soft); border-color: var(--bad); }
.opt.wrong .ltr { border-color: var(--bad); color: var(--bad); }
.opt:disabled { cursor: default; }

.why {
  margin-top: 11px;
  border-top: 1px dashed var(--line-2);
  padding-top: 10px;
  font-size: 14px;
  color: var(--ink-2);
}
.why b { color: var(--ink); font-weight: 600; }
.why[hidden] { display: none; }

.tf-btns { display: flex; gap: 8px; }
.tf-btns .opt { flex: 1; justify-content: center; font-weight: 500; }

/* ---------- Q&A cards ---------- */

.qa { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); margin: 0 0 10px; overflow: hidden; }
.qa > summary {
  cursor: pointer;
  list-style: none;
  padding: 14px 18px;
  display: flex;
  gap: 10px;
  font-size: 15.5px;
  align-items: baseline;
}
.qa > summary::-webkit-details-marker { display: none; }
.qa > summary:hover { background: var(--surface-2); }
.qa > summary .num { font-size: 12px; color: var(--ink-3); min-width: 26px; font-variant-numeric: tabular-nums; }
.qa > summary::after { content: "＋"; margin-left: auto; color: var(--ink-3); font-size: 15px; }
.qa[open] > summary::after { content: "−"; }
.qa[open] > summary { border-bottom: 1px solid var(--line); }
.qa .body { padding: 14px 18px 4px 54px; }
.qa .body p { margin: 0 0 12px; }
.qa .body ul { margin: 0 0 12px; padding-left: 20px; }
.qa .body li { margin-bottom: 5px; }

/* ---------- glossary ---------- */

.gl { width: 100%; border-collapse: collapse; font-size: 15px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.gl th { text-align: left; font-size: 13px; background: var(--surface-2); padding: 9px 12px; border-bottom: 1px solid var(--line); }
.gl td { padding: 10px 12px; border-bottom: 1px solid var(--line); vertical-align: top; }
.gl tr:last-child td { border-bottom: 0; }
.gl { table-layout: fixed; }
.gl th:nth-child(1), .gl td:nth-child(1) { width: 24%; }
.gl th:nth-child(2), .gl td:nth-child(2) { width: 20%; }
.gl .ar { font-size: 15.5px; direction: rtl; text-align: right; color: var(--ink-2); }
.gl .term { font-weight: 600; }
.gl .def { color: var(--ink-2); font-size: 14.5px; }

@media (max-width: 620px) {
  .gl th:nth-child(2), .gl td:nth-child(2) { width: 26%; }
  .gl th:nth-child(1), .gl td:nth-child(1) { width: 30%; }
}

/* ---------- index page ---------- */

.hero { padding: 8px 0 8px; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 12px; margin: 20px 0 0; }
.card {
  display: block;
  text-decoration: none;
  color: inherit;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 15px 16px;
}
.card:hover { border-color: var(--line-2); }
.card .k { font-size: 11.5px; letter-spacing: 0.08em; text-transform: uppercase; color: var(--ink-3); }
.card h3 { margin: 4px 0 6px; font-size: 16.5px; }
.card p { margin: 0 0 12px; font-size: 13.5px; color: var(--ink-2); line-height: 1.5; }
.card .meta { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--ink-3); }
.card .bar { height: 4px; flex: 1; }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 10px; margin: 22px 0 0; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; }
.stat .v { font-size: 22px; font-weight: 600; letter-spacing: -0.02em; }
.stat .l { font-size: 12.5px; color: var(--ink-3); }

.note-sm { font-size: 13px; color: var(--ink-3); }

hr.sep { border: 0; border-top: 1px solid var(--line); margin: 34px 0; }

.pager { display: flex; justify-content: space-between; gap: 12px; margin-top: 40px; padding-top: 18px; border-top: 1px solid var(--line); }
.pager a { font-size: 14px; text-decoration: none; color: var(--ink-2); }
.pager a:hover { color: var(--accent); }

.scrim { display: none; }

@media (max-width: 900px) {
  .topbar { display: flex; }
  .sidebar {
    position: fixed;
    z-index: 40;
    left: 0; top: 0;
    transform: translateX(-100%);
    transition: transform .2s;
    box-shadow: 0 0 40px rgba(0,0,0,.12);
  }
  body.nav-open .sidebar { transform: none; }
  body.nav-open .scrim { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.28); z-index: 35; }
  .wrap { padding: 24px 18px 80px; }
  h1 { font-size: 25px; }
  .quizbar { top: 46px; }
  .tabs { top: 44px; }
}

@media print {
  .sidebar, .tabs, .quizbar, .topbar, .pager { display: none !important; }
  .panel[hidden] { display: block !important; }
  .q, .qa, .card { break-inside: avoid; }
}
