:root {
  --bg: #0f1115;
  --bg-elev: #161a21;
  --bg-elev2: #1c222b;
  --line: #262d38;
  --text: #e6e9ef;
  --muted: #9aa4b2;
  --accent: #c8a45c;
  --accent-soft: rgba(200, 164, 92, .14);
  --ok: #4ec98a;
  --bad: #e8687a;
  --warn: #e3b341;
  --radius: 12px;
  --maxw: 880px;
  --sans: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, "Times New Roman", serif;
}
:root[data-theme="light"] {
  --bg: #f7f6f3;
  --bg-elev: #ffffff;
  --bg-elev2: #f0eee9;
  --line: #e0ddd6;
  --text: #1b1d21;
  --muted: #5f6672;
  --accent: #8a6a1f;
  --accent-soft: rgba(138, 106, 31, .10);
  --ok: #17794a;
  --bad: #b4283c;
  --warn: #9a6d09;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg); color: var(--text);
  font-family: var(--sans); font-size: 16px; line-height: 1.6;
  -webkit-text-size-adjust: 100%;
}
a { color: inherit; }
button, input, select, textarea { font: inherit; color: inherit; }

.skip { position: absolute; left: -9999px; }
.skip:focus { left: 8px; top: 8px; background: var(--accent); color: #111; padding: 8px 12px; border-radius: 8px; z-index: 100; }

/* ---------- topbar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 30;
  display: flex; align-items: center; gap: 10px;
  padding: 10px 14px; background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 9px; text-decoration: none; font-weight: 650; letter-spacing: .01em; }
.brand-mark {
  display: grid; place-items: center; width: 28px; height: 28px; border-radius: 8px;
  background: var(--accent); color: #17140c; font-family: var(--serif); font-size: 19px; font-weight: 700;
}
.topbar-spacer { flex: 1; }
.icon-btn {
  background: transparent; border: 1px solid var(--line); border-radius: 9px;
  width: 34px; height: 34px; cursor: pointer; display: grid; place-items: center; font-size: 16px;
}
.icon-btn:hover { background: var(--bg-elev2); }
.menu-toggle { display: none; }

/* ---------- layout ---------- */
.layout { display: grid; grid-template-columns: 236px minmax(0, 1fr); }
.sidebar {
  position: sticky; top: 55px; align-self: start; height: calc(100vh - 55px);
  overflow-y: auto; padding: 16px 10px 40px; border-right: 1px solid var(--line);
}
.nav-group { margin-bottom: 18px; }
.nav-label { font-size: 11px; text-transform: uppercase; letter-spacing: .09em; color: var(--muted); padding: 0 10px 6px; }
.sidebar a {
  display: flex; align-items: center; gap: 9px; padding: 7px 10px; margin: 1px 0;
  border-radius: 9px; text-decoration: none; font-size: 14.5px; color: var(--muted);
}
.sidebar a:hover { background: var(--bg-elev2); color: var(--text); }
.sidebar a.active { background: var(--accent-soft); color: var(--text); font-weight: 600; }
.ni { width: 18px; text-align: center; opacity: .85; font-size: 13px; }
.badge {
  margin-left: auto; background: var(--accent); color: #17140c; font-size: 11px; font-weight: 700;
  border-radius: 999px; padding: 1px 7px;
}
.scrim { position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 25; }
.main { min-height: calc(100vh - 55px); padding: 26px 22px 80px; }
.view { max-width: var(--maxw); margin: 0 auto; }
.loading { color: var(--muted); padding: 40px 0; text-align: center; }

/* ---------- typography ---------- */
h1 { font-size: 27px; line-height: 1.25; margin: 0 0 6px; letter-spacing: -.01em; }
h2 { font-size: 20px; margin: 30px 0 10px; letter-spacing: -.005em; }
h3 { font-size: 16.5px; margin: 20px 0 8px; }
p { margin: 0 0 12px; }
.lede { color: var(--muted); margin-bottom: 20px; }
.crumb { font-size: 12.5px; color: var(--muted); letter-spacing: .04em; text-transform: uppercase; margin-bottom: 8px; }
ul, ol { margin: 0 0 14px; padding-left: 22px; }
li { margin-bottom: 5px; }
code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .9em; background: var(--bg-elev2); padding: 1px 5px; border-radius: 5px; }
hr { border: 0; border-top: 1px solid var(--line); margin: 26px 0; }

/* ---------- cards & grid ---------- */
.card {
  background: var(--bg-elev); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px 18px; margin-bottom: 14px;
}
.grid { display: grid; gap: 12px; }
.grid.two { grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); }
.grid.three { grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); }
.tile {
  display: block; text-decoration: none; background: var(--bg-elev); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 15px 16px; transition: border-color .15s, transform .15s;
}
.tile:hover { border-color: var(--accent); transform: translateY(-1px); }
.tile h3 { margin: 0 0 4px; font-size: 15.5px; }
.tile p { margin: 0; font-size: 13.5px; color: var(--muted); }
.stat { text-align: center; padding: 14px 10px; }
.stat .num { font-size: 26px; font-weight: 700; font-variant-numeric: tabular-nums; display: block; }
.stat .lbl { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  background: var(--bg-elev2); border: 1px solid var(--line); border-radius: 10px;
  padding: 9px 16px; cursor: pointer; text-decoration: none; font-size: 14.5px; font-weight: 550;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #17140c; }
.btn.primary:hover { filter: brightness(1.07); }
.btn.ghost { background: transparent; }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-row { display: flex; flex-wrap: wrap; gap: 9px; margin: 14px 0; }
.btn.sm { padding: 6px 11px; font-size: 13px; border-radius: 8px; }

/* ---------- chips / pills ---------- */
.chips { display: flex; flex-wrap: wrap; gap: 7px; margin: 10px 0 18px; }
.chip {
  background: var(--bg-elev); border: 1px solid var(--line); border-radius: 999px;
  padding: 5px 12px; font-size: 13px; cursor: pointer; white-space: nowrap;
}
.chip.on { background: var(--accent-soft); border-color: var(--accent); color: var(--text); font-weight: 600; }
.pill {
  display: inline-block; font-size: 11.5px; letter-spacing: .04em; text-transform: uppercase;
  border: 1px solid var(--line); border-radius: 999px; padding: 2px 9px; color: var(--muted);
}
.pill.accent { border-color: var(--accent); color: var(--accent); }

/* ---------- art. references ---------- */
.artref {
  border: 0; background: var(--accent-soft); color: var(--text); border-bottom: 1px solid var(--accent);
  border-radius: 4px; padding: 0 4px; cursor: pointer; font-size: .95em; white-space: nowrap;
}
.artref:hover { background: var(--accent); color: #17140c; }

.quote {
  border-left: 3px solid var(--accent); background: var(--bg-elev); padding: 12px 15px;
  border-radius: 0 10px 10px 0; margin: 14px 0; font-family: var(--serif); font-size: 15.5px;
}
.quote .src { display: block; margin-top: 7px; font-family: var(--sans); font-size: 12.5px; color: var(--muted); }

.note { border-left: 3px solid var(--warn); background: var(--bg-elev); padding: 12px 15px; border-radius: 0 10px 10px 0; margin: 14px 0; }
.note strong:first-child { color: var(--warn); }
.note.tk { border-left-color: var(--accent); }
.note.trap { border-left-color: var(--bad); }
.note.trap strong:first-child { color: var(--bad); }

/* ---------- tables ---------- */
.tbl { width: 100%; border-collapse: collapse; margin: 14px 0; font-size: 14.5px; }
.tbl th, .tbl td { text-align: left; padding: 9px 11px; border-bottom: 1px solid var(--line); vertical-align: top; }
.tbl th { font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); font-weight: 600; }
.tbl tr:hover td { background: var(--bg-elev); }
.tbl-wrap { overflow-x: auto; }

/* ---------- search input ---------- */
.search-input {
  width: 100%; background: var(--bg-elev); border: 1px solid var(--line);
  border-radius: 10px; padding: 11px 14px; margin-bottom: 14px;
}
.search-input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

/* ---------- progress ---------- */
.bar { height: 7px; background: var(--bg-elev2); border-radius: 999px; overflow: hidden; margin: 8px 0; }
.bar > i { display: block; height: 100%; background: var(--accent); border-radius: 999px; transition: width .3s; }
.bar.ok > i { background: var(--ok); }

/* ---------- flashcards ---------- */
.fc-stage { min-height: 260px; display: grid; place-items: center; }
.fc {
  width: 100%; background: var(--bg-elev); border: 1px solid var(--line); border-radius: 16px;
  padding: 32px 26px; text-align: center; cursor: pointer; min-height: 240px;
  display: flex; flex-direction: column; justify-content: center; gap: 14px;
}
.fc .q { font-size: 20px; font-weight: 600; line-height: 1.4; }
.fc .a { font-size: 16px; color: var(--text); text-align: left; }
.fc .hint { font-size: 12.5px; color: var(--muted); }
.fc-grade { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-top: 14px; }
.fc-grade .btn { flex-direction: column; gap: 2px; padding: 10px 6px; font-size: 13px; }
.fc-grade .btn small { color: var(--muted); font-size: 11px; font-weight: 400; }

/* ---------- quiz ---------- */
.opt {
  display: flex; gap: 11px; align-items: flex-start; width: 100%; text-align: left;
  background: var(--bg-elev); border: 1px solid var(--line); border-radius: 11px;
  padding: 12px 14px; margin-bottom: 9px; cursor: pointer;
}
.opt:hover { border-color: var(--accent); }
.opt .key {
  flex: 0 0 24px; height: 24px; border-radius: 7px; background: var(--bg-elev2);
  display: grid; place-items: center; font-size: 12.5px; font-weight: 700;
}
.opt.correct { border-color: var(--ok); background: color-mix(in srgb, var(--ok) 13%, var(--bg-elev)); }
.opt.wrong { border-color: var(--bad); background: color-mix(in srgb, var(--bad) 13%, var(--bg-elev)); }
.opt.picked .key { background: var(--accent); color: #17140c; }
.opt[disabled] { cursor: default; }
.qmeta { display: flex; justify-content: space-between; align-items: center; color: var(--muted); font-size: 13px; margin-bottom: 10px; }
.explain { border-left: 3px solid var(--ok); background: var(--bg-elev); padding: 12px 15px; border-radius: 0 10px 10px 0; margin: 12px 0; }
.explain.bad { border-left-color: var(--bad); }

/* ---------- constitution ---------- */
.art { border-bottom: 1px solid var(--line); padding: 14px 0; }
.art h4 { margin: 0 0 5px; font-size: 15px; display: flex; align-items: center; gap: 9px; }
.art h4 .no { color: var(--accent); font-weight: 700; }
.art p { font-family: var(--serif); font-size: 15.5px; margin: 0; }
.art mark { background: var(--accent); color: #17140c; border-radius: 3px; }
.art-head { font-size: 12px; text-transform: uppercase; letter-spacing: .07em; color: var(--muted); margin: 22px 0 2px; }

/* ---------- modal ---------- */
.modal { position: fixed; inset: 0; z-index: 50; display: grid; place-items: center; padding: 18px; background: rgba(0,0,0,.55); }
.modal-box { background: var(--bg-elev); border: 1px solid var(--line); border-radius: 14px; max-width: 620px; width: 100%; max-height: 80vh; overflow-y: auto; padding: 20px 22px; }
.modal-box h3 { margin-top: 0; }

/* ---------- palette ---------- */
.palette { position: fixed; inset: 0; z-index: 60; background: rgba(0,0,0,.55); padding: 10vh 16px 16px; }
.palette-box { max-width: 620px; margin: 0 auto; background: var(--bg-elev); border: 1px solid var(--line); border-radius: 14px; overflow: hidden; }
.palette-box input { width: 100%; border: 0; background: transparent; padding: 15px 18px; font-size: 16px; border-bottom: 1px solid var(--line); }
.palette-box input:focus { outline: none; }
.palette-results { max-height: 52vh; overflow-y: auto; }
.pres { display: block; width: 100%; text-align: left; background: transparent; border: 0; padding: 10px 18px; cursor: pointer; border-bottom: 1px solid var(--line); }
.pres:hover, .pres.sel { background: var(--accent-soft); }
.pres .t { font-size: 14.5px; }
.pres .s { font-size: 12.5px; color: var(--muted); }
.palette-hint { padding: 8px 18px; font-size: 11.5px; color: var(--muted); border-top: 1px solid var(--line); }

/* ---------- misc ---------- */
.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }
.mono { font-variant-numeric: tabular-nums; }
.kbd { border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 5px; padding: 1px 6px; font-size: 12px; background: var(--bg-elev2); }
.acc { display: block; width: 100%; text-align: left; background: var(--bg-elev); border: 1px solid var(--line); border-radius: 10px; padding: 12px 15px; margin-bottom: 8px; cursor: pointer; font-weight: 550; }
.acc + .acc-body { padding: 4px 15px 14px; margin: -6px 0 10px; border: 1px solid var(--line); border-top: 0; border-radius: 0 0 10px 10px; }

@media (max-width: 860px) {
  .layout { grid-template-columns: 1fr; }
  .menu-toggle { display: grid; }
  .sidebar {
    position: fixed; top: 0; left: 0; bottom: 0; width: 250px; height: 100vh; z-index: 28;
    background: var(--bg-elev); transform: translateX(-100%); transition: transform .2s; padding-top: 18px;
  }
  .sidebar.open { transform: none; }
  .main { padding: 20px 16px 70px; }
  h1 { font-size: 23px; }
  .fc-grade { grid-template-columns: repeat(2, 1fr); }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
