/* =========================================================================
   Interview-Prep Study App — styles
   Mobile-first, minimalist, reading-first. Tokens per DESIGN.md §3.
   ========================================================================= */

/* ============================================================
   Design tokens — CRED-inspired: matte black, chunky cards,
   tactile press feedback, single bright accent, mono numerals.
   Dark-first; "Light" is the opt-in inversion.
   ============================================================ */
:root {
  /* Matte-black palette (default) */
  --bg: #0a0a0b;                   /* near-pure black; CRED-style matte */
  --surface: #141416;              /* card surface */
  --surface-2: #1f1f23;            /* hover / chip background */
  --text: #f2efe9;                 /* warm off-white; not pure white */
  --text-muted: #8a8a93;
  --border: #25252b;               /* subtle hairlines */
  --accent: #ff7a45;               /* bright tactile coral */
  --accent-weak: #2b1810;          /* dim coral-tinted fill */
  --mark: #4a3517;                 /* highlight bg (search) */
  --good: #4ade80;                 /* electric green */
  --bad:  #f87171;                 /* signal red */
  --on-accent: #0a0a0b;            /* dark ink on bright accent fills */

  /* Decorative tokens (kept for backward-compat with paper-era classes) */
  --gilt: #ffb84a;
  --rule: #25252b;
  --paper-edge: transparent;       /* no paper edge in CRED style */
  --page-shadow: 0 24px 60px -20px rgba(0, 0, 0, .8);

  /* Spacing — slightly more generous (CRED-style negative space) */
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;
  --space-8: 48px;

  /* Chunky rounded corners — the CRED signature */
  --radius: 20px;                  /* cards */
  --radius-sm: 14px;               /* chips, small buttons */
  --shadow: 0 2px 6px rgba(0, 0, 0, .35), 0 8px 24px -8px rgba(0, 0, 0, .45);

  /* Typography — bold geometric sans + true mono for numerals */
  --font: "Inter", "SF Pro Display", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --font-serif: "Inter", "SF Pro Display", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --mono: "JetBrains Mono", "SF Mono", ui-monospace, Menlo, Consolas, monospace;

  --content-max: 760px;
  --reader-max: 820px;
  --measure: 68ch;
  --tabbar-h: 64px;                /* chunkier than before */

  /* Motion — springier, tactile */
  --dur-fast: .14s;
  --dur-mid: .26s;
  --dur-page: .36s;
  --ease-soft: cubic-bezier(.2, .8, .2, 1);     /* slightly springy */
  --ease-page: cubic-bezier(.4, 0, .2, 1);
}

/* Inter via system fallback chain — gracefully degrades.
   We deliberately use only system stacks (no @import) so the app
   stays 100% offline-capable per the original design goal. */

/* ---- "Light" theme — opt-in only (system dark stays the default) ---- */
:root.theme-light {
  --bg: #f5f5f4;                    /* warm off-white */
  --surface: #ffffff;
  --surface-2: #ececeb;
  --text: #0a0a0b;
  --text-muted: #6b6b73;
  --border: #e0e0dd;
  --accent: #ff5c2e;                /* slightly deeper coral for AA on light */
  --accent-weak: #ffe9dc;
  --mark: #ffd699;
  --good: #16a34a;
  --bad:  #dc2626;
  --on-accent: #ffffff;
  --gilt: #c2922e;
  --rule: #e0e0dd;
  --paper-edge: transparent;
  --page-shadow: 0 24px 60px -20px rgba(0, 0, 0, .12);
  --shadow: 0 2px 6px rgba(0, 0, 0, .06), 0 8px 24px -8px rgba(0, 0, 0, .08);
}

/* Mono numerals where it reads tactile (counts, chips, scores) */
.count-chip,
.tag,
.meta time,
.score,
.tab__label {
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum" 1, "cv11" 1;
}

/* ---- Reset / base ---- */
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  font-size: 1rem;
  line-height: 1.65;
  color: var(--text);
  background: var(--bg);
  /* Bottom padding so content clears the fixed mobile tab bar. */
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom, 0px));
}
a { color: var(--accent); }
button { font-family: inherit; }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 4px;
}
#app:focus { outline: none; }

/* ---- Top nav (desktop) ---- */
.topnav {
  position: sticky;
  top: 0;
  z-index: 20;
  display: none; /* hidden on mobile; shown >=768px */
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}
.topnav__inner {
  max-width: var(--content-max);
  margin: 0 auto;
  padding: var(--space-3) var(--space-4);
  display: flex;
  align-items: center;
  gap: var(--space-4);
}
.topnav__title {
  font-weight: 800;
  font-size: 1.15rem;
  letter-spacing: -.01em;
  text-decoration: none;
  color: var(--text);
  margin-right: auto;
  text-transform: uppercase;       /* CRED-style bold uppercase wordmark */
}
.topnav__links {
  display: flex;
  align-items: center;
  gap: var(--space-4);
}
.navlink {
  text-decoration: none;
  color: var(--text-muted);
  font-weight: 500;
  padding: var(--space-2) 0;
  border-bottom: 2px solid transparent;
}
.navlink:hover { color: var(--text); }
.navlink.is-active {
  color: var(--accent);
  border-bottom-color: var(--accent);
  font-weight: 600;
}
.theme-toggle {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: var(--radius-sm);
  width: 36px;
  height: 36px;
  cursor: pointer;
  font-size: 1.1rem;
  line-height: 1;
}
.theme-toggle:hover { background: var(--border); }

/* ---- Marked-missing banner ---- */
.banner {
  background: var(--accent-weak);
  color: var(--text);
  border-bottom: 1px solid var(--border);
  padding: var(--space-2) var(--space-4);
  font-size: 0.9rem;
  display: flex;
  align-items: center;
  gap: var(--space-3);
  justify-content: center;
}
.banner__close {
  background: none;
  border: none;
  color: var(--text);
  font-size: 1.2rem;
  cursor: pointer;
  line-height: 1;
  padding: 0 var(--space-1);
}

/* ---- Main content column ---- */
#app {
  max-width: var(--content-max);
  margin: 0 auto;
  padding: var(--space-5) var(--space-4) var(--space-8);
}

/* ---- Bottom tab bar (mobile) ---- */
.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 30;
  display: flex;
  background: var(--bg);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom, 0px);
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom, 0px));
}
.tab {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-height: 44px;
  text-decoration: none;
  color: var(--text-muted);
  font-size: 0.72rem;
}
.tab__icon { font-size: 1.25rem; line-height: 1; }
.tab.is-active { color: var(--accent); }
.tab.is-active .tab__label { font-weight: 700; }

/* ---- Generic page bits ---- */
.page-title {
  font-size: clamp(1.75rem, 5vw, 2.25rem);
  font-weight: 800;
  letter-spacing: -.02em;          /* tighter, CRED-style */
  margin: 0 0 var(--space-4);
  line-height: 1.1;
}
.page-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--space-3);
  flex-wrap: wrap;
  margin-bottom: var(--space-4);
}
.page-head .page-title { margin-bottom: 0; }
.meta { color: var(--text-muted); font-size: 0.85rem; }
.empty {
  text-align: center;
  color: var(--text-muted);
  padding: var(--space-8) var(--space-4);
}
.empty a { font-weight: 600; }

/* ---- Cards — chunky, CRED-style tactile press ---- */
.card {
  display: block;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: var(--space-5) var(--space-4);
  text-decoration: none;
  color: var(--text);
  box-shadow: var(--shadow);
  transition:
    transform var(--dur-fast) var(--ease-soft),
    box-shadow var(--dur-fast) var(--ease-soft),
    background-color var(--dur-fast) var(--ease-soft);
  will-change: transform;
}
.card:hover {
  background: var(--surface-2);
  transform: translateY(-2px);
  box-shadow: 0 4px 10px rgba(0, 0, 0, .35), 0 14px 36px -10px rgba(0, 0, 0, .55);
}
.card:active {
  /* Tactile press — scale down + shadow collapses (the CRED feel) */
  transform: scale(.975) translateY(0);
  box-shadow: 0 1px 2px rgba(0, 0, 0, .3);
  background: var(--surface-2);
  transition-duration: .06s;
}
@media (prefers-reduced-motion: reduce) {
  .card { transition: none; }
  .card:hover, .card:active { transform: none; }
}
.card__title { font-weight: 600; display: block; }
.card__path {
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: var(--text-muted);
  font-size: 0.85rem;
  margin-top: var(--space-1);
}
.card__chev { color: var(--text-muted); }

/* ---- Library ---- */
.cat-group { margin-bottom: var(--space-6); }
.cat-head {
  display: flex;
  align-items: baseline;
  gap: var(--space-2);
  margin: 0 0 var(--space-3);
  font-size: 1.15rem;
  font-weight: 700;
}
.count-chip {
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--text-muted);
  background: var(--surface-2);
  border-radius: 999px;
  padding: 1px 8px;
}
.card-list { display: grid; gap: var(--space-3); }
.continue {
  border-left: 3px solid var(--accent);
  margin-bottom: var(--space-6);
}
.continue .eyebrow {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--accent);
  font-weight: 700;
}

/* ---- Chips (interactive filter) ---- */
.chip-row {
  display: flex;
  gap: var(--space-2);
  overflow-x: auto;
  padding-bottom: var(--space-2);
  margin-bottom: var(--space-4);
  -webkit-overflow-scrolling: touch;
}
.chip {
  white-space: nowrap;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  border-radius: 999px;
  padding: 8px 16px;
  font-size: 0.875rem;
  font-weight: 600;
  cursor: pointer;
  min-height: 36px;
  transition:
    transform var(--dur-fast) var(--ease-soft),
    background-color var(--dur-fast) var(--ease-soft);
}
.chip:hover { background: var(--border); transform: translateY(-1px); }
.chip:active { transform: scale(.94); transition-duration: .05s; }
.chip.is-active {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--on-accent);
  font-weight: 700;
  box-shadow: 0 4px 12px -2px rgba(255, 122, 69, .3);
}
@media (prefers-reduced-motion: reduce) {
  .chip { transition: none; }
  .chip:hover, .chip:active { transform: none; }
}
/* Non-interactive display tag (e.g. topic on a question) */
.tag {
  display: inline-block;
  background: var(--surface-2);
  color: var(--text-muted);
  border-radius: 999px;
  padding: 2px 10px;
  font-size: 0.78rem;
  white-space: nowrap;
}

/* ---- Buttons — chunky pills with tactile press ---- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-height: 48px;                /* slightly chunkier than 44 */
  padding: 0 var(--space-5);
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-size: 1rem;
  font-weight: 700;                /* bolder for tactile presence */
  letter-spacing: .01em;
  cursor: pointer;
  text-decoration: none;
  transition:
    transform var(--dur-fast) var(--ease-soft),
    background-color var(--dur-fast) var(--ease-soft),
    box-shadow var(--dur-fast) var(--ease-soft);
  will-change: transform;
}
.btn:hover { background: var(--border); transform: translateY(-1px); }
.btn:active {
  transform: scale(.96);
  transition-duration: .05s;
}
.btn--primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--on-accent);
  box-shadow: 0 4px 14px -2px rgba(255, 122, 69, .35);
}
.btn--primary:hover { filter: brightness(1.08); transform: translateY(-1px); }
.btn--primary:active { transform: scale(.96); box-shadow: 0 1px 3px rgba(255, 122, 69, .25); }
.btn--good { background: var(--good); border-color: var(--good); color: var(--on-accent); }
.btn--bad  { background: var(--bad);  border-color: var(--bad);  color: var(--on-accent); }
.btn--block { width: 100%; }
.btn--ghost {
  background: none;
  border: none;
  color: var(--accent);
  padding: var(--space-2) 0;
  min-height: 0;
  box-shadow: none;
}
.btn--ghost:hover { background: none; transform: none; text-decoration: underline; }
@media (prefers-reduced-motion: reduce) {
  .btn { transition: none; }
  .btn:hover, .btn:active { transform: none; }
}

/* ---- Reader ---- */
.reader-bar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  padding: var(--space-3) 0;
  margin-bottom: var(--space-4);
}
.back-btn {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  padding: var(--space-2) var(--space-2) var(--space-2) 0;
  min-height: 44px;
}
.reader-cat {
  background: var(--surface-2);
  color: var(--text-muted);
  border-radius: 999px;
  padding: 4px 12px;
  font-size: 0.8rem;
  text-decoration: none;
}
.next-pill {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  margin-top: var(--space-6);
  padding: var(--space-4);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  text-decoration: none;
  color: var(--text);
}
.next-pill:hover { background: var(--surface-2); }
.next-pill .eyebrow {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--text-muted);
  display: block;
}

/* ---- Markdown body (book prose: serif, generous measure) ---- */
.markdown {
  max-width: var(--measure);
  font-family: var(--font-serif);
  font-size: 1rem;
  line-height: 1.6;
  text-rendering: optimizeLegibility;
  font-feature-settings: "kern" 1, "liga" 1;
}
.markdown > pre.raw-fallback {
  white-space: pre-wrap;
  word-wrap: break-word;
  font-family: var(--mono);
  font-size: 0.9rem;
}
.markdown h1 { font-size: 1.5rem; }
.markdown h2 { font-size: 1.22rem; }
.markdown h3 { font-size: 1.05rem; }
.markdown h4, .markdown h5, .markdown h6 { font-size: 0.95rem; }
.markdown h1, .markdown h2, .markdown h3,
.markdown h4, .markdown h5, .markdown h6 {
  font-weight: 700;
  line-height: 1.3;
  margin: var(--space-5) 0 var(--space-2);
}
.markdown h1:first-child { margin-top: 0; }
.markdown p, .markdown ul, .markdown ol { margin: 0 0 var(--space-4); }
.markdown li { margin-bottom: var(--space-1); }
.markdown li > ul, .markdown li > ol { margin-top: var(--space-1); }
.markdown a { color: var(--accent); text-decoration: underline; }
.markdown img { max-width: 100%; height: auto; }
.markdown hr { border: none; border-top: 1px solid var(--border); margin: var(--space-6) 0; }

.markdown code {
  font-family: var(--mono);
  font-size: 0.9em;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 5px;
}
.markdown pre {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: var(--space-4);
  overflow-x: auto;
}
.markdown pre code {
  background: none;
  border: none;
  padding: 0;
  font-size: 0.9rem;
}
.markdown blockquote {
  margin: 0 0 var(--space-4);
  padding: var(--space-1) var(--space-4);
  border-left: 3px solid var(--accent);
  color: var(--text-muted);
}
/* Tables: scroll-wrapped to avoid full-page horizontal scroll on mobile */
.table-wrap { overflow-x: auto; margin: 0 0 var(--space-4); -webkit-overflow-scrolling: touch; }
.markdown table { border-collapse: collapse; width: 100%; font-size: 0.95rem; }
.markdown th, .markdown td {
  border: 1px solid var(--border);
  padding: var(--space-2) var(--space-3);
  text-align: left;
}
.markdown thead th { background: var(--surface-2); }

/* Search highlight + transient flash */
mark, .markdown mark {
  background: var(--mark);
  color: var(--text);
  border-radius: 2px;
  padding: 0 2px;
}
mark.flash { animation: flash 1.4s ease; }
@keyframes flash {
  0% { box-shadow: 0 0 0 4px var(--accent-weak); }
  100% { box-shadow: 0 0 0 0 transparent; }
}
@media (prefers-reduced-motion: reduce) {
  mark.flash { animation: none; }
}

/* ---- Search ---- */
.search-input {
  width: 100%;
  font-size: 16px; /* >=16px avoids iOS zoom-on-focus */
  padding: var(--space-3) var(--space-4);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  margin-bottom: var(--space-4);
}
.results-group { margin-bottom: var(--space-6); }
.results-group h2 {
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--text-muted);
  margin: 0 0 var(--space-3);
}
.result-snippet {
  display: block;
  color: var(--text-muted);
  font-size: 0.9rem;
  margin-top: var(--space-1);
}
.result-meta {
  display: block;
  color: var(--text-muted);
  font-size: 0.8rem;
  margin-top: var(--space-1);
}
.show-more { margin-top: var(--space-2); }

/* ---- Q&A accordion ---- */
.qa-list { display: grid; gap: var(--space-3); }
.qa-item {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  overflow: hidden;
}
.qa-trigger {
  width: 100%;
  display: flex;
  align-items: center;
  gap: var(--space-3);
  text-align: left;
  background: none;
  border: none;
  color: var(--text);
  padding: var(--space-4);
  cursor: pointer;
  min-height: 44px;
  font-size: 1rem;
}
.qa-trigger:hover { background: var(--surface); }
.qa-q { flex: 1; font-weight: 500; }
.qa-chev {
  color: var(--text-muted);
  transition: transform .18s ease;
  flex-shrink: 0;
}
.qa-trigger[aria-expanded="true"] .qa-chev { transform: rotate(90deg); }
@media (prefers-reduced-motion: reduce) { .qa-chev { transition: none; } }
/* Animated reveal via grid-template-rows 0fr->1fr (no JS height measurement). */
.qa-panel {
  display: grid;
  grid-template-rows: 0fr;
  transition: grid-template-rows var(--dur-mid) var(--ease-soft);
}
.qa-item.is-open .qa-panel {
  grid-template-rows: 1fr;
  border-top: 1px solid var(--border);
  background: var(--surface);
}
.qa-inner { overflow: hidden; padding: 0 var(--space-4); }
.qa-item.is-open .qa-inner { padding-bottom: var(--space-4); }
@media (prefers-reduced-motion: reduce) { .qa-panel { transition: none; } }
.qa-answer { margin: var(--space-3) 0; }
.qa-answer p:last-child { margin-bottom: 0; }
.qa-source { font-size: 0.85rem; font-weight: 600; }

/* ---- Flashcards ---- */
.fc-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  flex-wrap: wrap;
  margin-bottom: var(--space-3);
}
.fc-stats { display: flex; gap: var(--space-4); font-size: 0.95rem; }
.fc-stats .stat { display: inline-flex; align-items: center; gap: 4px; }
.fc-progress-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  margin-bottom: var(--space-3);
}
.progress-track {
  flex: 1;
  height: 6px;
  background: var(--surface-2);
  border-radius: 999px;
  overflow: hidden;
}
.progress-fill { height: 100%; background: var(--accent); transition: width .2s ease; }
@media (prefers-reduced-motion: reduce) { .progress-fill { transition: none; } }

.flashcard {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: var(--space-6) var(--space-5);
  margin: var(--space-4) 0;
  min-height: 220px;
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
}
.fc-question { font-size: 1.2rem; font-weight: 600; line-height: 1.4; }
.fc-divider { border: none; border-top: 1px solid var(--border); margin: 0; }
.fc-answer { font-size: 1.0625rem; line-height: 1.6; }
.fc-answer p:last-child { margin-bottom: 0; }
.fc-actions { display: flex; gap: var(--space-3); margin-top: auto; }
.fc-actions .btn { flex: 1; }
.fc-controls {
  display: flex;
  justify-content: center;
  gap: var(--space-4);
  margin-top: var(--space-2);
}
.fc-study { margin-top: var(--space-3); }

/* Deck-complete summary */
.summary { text-align: center; }
.summary .big { font-size: 2rem; font-weight: 700; margin: var(--space-3) 0; }
.summary-actions {
  display: flex;
  gap: var(--space-3);
  justify-content: center;
  flex-wrap: wrap;
  margin: var(--space-5) 0;
}
.missed-list { display: grid; gap: var(--space-2); text-align: left; margin-top: var(--space-4); }
.missed-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: var(--space-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
}
.missed-row span { flex: 1; font-size: 0.95rem; }

/* ---- Responsive: desktop ---- */
@media (min-width: 768px) {
  body { padding-bottom: 0; }
  .topnav { display: block; }
  .tabbar { display: none; }
  #app { padding-top: var(--space-6); }
  .page-title { font-size: 1.9rem; }
}

/* ---- Difficulty badges ---- */
.diff-badge {
  display: inline-block;
  font-size: .68rem;
  font-weight: 700;
  line-height: 1;
  letter-spacing: .02em;
  padding: .28em .6em;
  border-radius: 999px;
  border: 1px solid currentColor;
  white-space: nowrap;
  flex: none;
}
.diff-badge.diff-easy { color: var(--good); }
.diff-badge.diff-medium { color: var(--accent); }
.diff-badge.diff-hard { color: var(--bad); }

/* Flashcard meta row (difficulty + topic above the question) */
.fc-meta {
  display: flex;
  gap: var(--space-2, .5rem);
  align-items: center;
  flex-wrap: wrap;
  margin-bottom: var(--space-3, .75rem);
}

/* ---- Collapsible / filterable Library categories ---- */
.cat-head-btn {
  width: 100%;
  display: flex;
  align-items: center;
  gap: var(--space-2);
  background: none;
  border: none;
  color: var(--text);
  cursor: pointer;
  text-align: left;
  padding: var(--space-2) 0;
  margin-bottom: var(--space-2);
}
.cat-head-btn .cat-name { font-weight: 700; font-size: 1.15rem; }
.cat-chev { color: var(--text-muted); transition: transform .18s ease; }
.cat-head-btn.is-collapsed .cat-chev { transform: rotate(-90deg); }
@media (prefers-reduced-motion: reduce) { .cat-chev { transition: none; } }
.cat-body[hidden] { display: none; }
.cat-filter {
  width: 100%;
  font-size: 16px; /* >=16px avoids iOS zoom-on-focus */
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  margin-bottom: var(--space-3);
}

/* ---- Grouped, collapsible topic picker (Q&A + Flashcards) ---- */
.topic-picker { margin-bottom: var(--space-4); }
.tp-toprow {
  display: flex;
  gap: var(--space-2);
  align-items: center;
  flex-wrap: wrap;
  margin-bottom: var(--space-2);
}
.tp-filter {
  flex: 1;
  min-width: 140px;
  font-size: 16px;
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
}
.tp-group { border-bottom: 1px solid var(--border); }
.tp-group-btn {
  width: 100%;
  display: flex;
  align-items: center;
  gap: var(--space-2);
  background: none;
  border: none;
  color: var(--text);
  cursor: pointer;
  text-align: left;
  padding: var(--space-3) var(--space-1);
  font-size: 0.95rem;
  min-height: 44px;
}
.tp-group-btn:hover { color: var(--accent); }
.tp-group-btn .tp-name { font-weight: 600; flex: 1; }
.tp-group-btn.has-active .tp-name { color: var(--accent); }
.tp-chev { color: var(--text-muted); transition: transform .18s ease; }
.tp-group-btn.is-open .tp-chev { transform: rotate(90deg); }
@media (prefers-reduced-motion: reduce) { .tp-chev { transition: none; } }
.tp-panel {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  padding: 0 0 var(--space-3) var(--space-4);
}
.tp-panel[hidden] { display: none; }
.tp-chip { font-size: 0.82rem; }

/* ---- Q&A expand/collapse toolbar ---- */
.qa-toolbar { display: flex; justify-content: flex-end; margin-bottom: var(--space-2); }

/* ---- Reader prev/next nav row ---- */
.reader-nav {
  display: flex;
  gap: var(--space-3);
  margin-top: var(--space-6);
  flex-wrap: wrap;
}
.reader-nav .next-pill { margin-top: 0; flex: 1; min-width: 180px; }
.reader-nav .rn-label { display: flex; flex-direction: column; }
.prev-pill { justify-content: flex-start; }
.rn-chev { color: var(--text-muted); }

/* ---- Flashcard deck-builder landing ---- */
.fc-landing { text-align: center; padding: var(--space-5) 0; }
.fc-landing-actions {
  display: flex;
  gap: var(--space-3);
  justify-content: center;
  flex-wrap: wrap;
  margin-top: var(--space-3);
}

/* =========================================================================
   BOOK THEME — paper leaf, chapter opener, TOC, progress, flip, transitions.
   Additive; all motion gated behind prefers-reduced-motion.
   ========================================================================= */

/* Reader gets a wider column to make room for the margin TOC. */
#app.is-reader { max-width: var(--reader-max); perspective: 1500px; }

/* Two-column reader layout: margin TOC + page leaf (desktop only). */
.reader-layout { display: block; }
@media (min-width: 920px) {
  .reader-layout.has-toc {
    display: grid;
    grid-template-columns: 190px minmax(0, 1fr);
    gap: var(--space-6);
    align-items: start;
  }
}

/* The paper leaf the chapter is printed on. */
.leaf {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--page-shadow);
  padding: var(--space-6) var(--space-5);
}
.leaf::before {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: var(--radius);
  box-shadow: inset 0 0 0 1px var(--paper-edge);
  pointer-events: none;
}
@media (min-width: 768px) { .leaf { padding: var(--space-8) var(--space-6); } }

/* Chapter opener. */
.chapter-opener { margin-bottom: var(--space-5); }
.chapter-eyebrow {
  font-family: var(--font);
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: .14em;
  color: var(--text-muted);
  display: block;
  margin-bottom: var(--space-2);
}
.chapter-eyebrow .chapter-no { color: var(--text); font-weight: 700; }
.chapter-title {
  font-family: var(--font-serif);
  font-size: clamp(1.45rem, 1.1rem + 1.4vw, 1.95rem);
  font-weight: 700;
  line-height: 1.18;
  margin: 0 0 var(--space-2);
}
.chapter-meta { font-family: var(--font); font-size: 0.8rem; color: var(--text-muted); }
.gilt-rule {
  border: none;
  height: 2px;
  background: linear-gradient(90deg, var(--gilt), transparent);
  margin: var(--space-4) 0 var(--space-5);
}

/* Drop cap on the first paragraph; hide the duplicate H1 title. */
.markdown.has-opener > h1:first-child { display: none; }
.markdown.has-opener > p:first-of-type::first-letter {
  float: left;
  font-family: var(--font-serif);
  font-weight: 700;
  font-size: 2.7em;
  line-height: .8;
  padding: .02em .08em 0 0;
  color: var(--text);
}

/* Booky blockquote + ornamental rule. */
.markdown blockquote { font-style: italic; }
.markdown hr {
  height: 1px;
  background: var(--rule);
  border: none;
  width: 60%;
  margin: var(--space-6) auto;
}

/* In-page Table of Contents. */
.toc { font-family: var(--font); }
.toc-title {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: .12em;
  color: var(--text-muted);
  margin: 0 0 var(--space-2);
}
.toc ul { list-style: none; margin: 0; padding: 0; }
.toc-link {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  border-left: 2px solid var(--border);
  color: var(--text-muted);
  font-size: 0.85rem;
  line-height: 1.35;
  padding: 5px var(--space-3);
  cursor: pointer;
}
.toc-link:hover { color: var(--accent); border-left-color: var(--accent); }
.toc-link.lvl-3 { padding-left: var(--space-5); font-size: 0.8rem; }
.toc-link.is-current { color: var(--accent); border-left-color: var(--accent); font-weight: 700; }
@media (min-width: 920px) {
  .toc-desktop { position: sticky; top: 76px; max-height: calc(100vh - 100px); overflow: auto; }
  .toc-mobile { display: none; }
}
@media (max-width: 919px) {
  .toc-desktop { display: none; }
  .toc-mobile {
    margin-bottom: var(--space-4);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--surface);
    padding: var(--space-2) var(--space-3);
  }
  .toc-mobile > summary {
    cursor: pointer;
    font-family: var(--font);
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-muted);
    min-height: 32px;
    display: flex;
    align-items: center;
  }
  .toc-mobile[open] > summary { margin-bottom: var(--space-2); }
}

/* Top reading-progress ribbon (lives on <body>, outside #app). */
.read-progress {
  position: fixed;
  top: 0; left: 0;
  height: 3px; width: 100%;
  background: var(--gilt);
  transform: scaleX(var(--progress, 0));
  transform-origin: left;
  z-index: 100;
  pointer-events: none;
}
.read-progress[hidden] { display: none; }

/* Page-turn pills lift on hover. */
.next-pill { transition: transform var(--dur-fast) var(--ease-soft), background var(--dur-fast) ease; }
.next-pill:hover { transform: translateY(-1px); }
@media (prefers-reduced-motion: reduce) {
  .next-pill { transition: none; }
  .next-pill:hover { transform: none; }
}

/* View enter + page-turn transitions. */
.view-root { animation: viewIn var(--dur-mid) var(--ease-soft) both; }
@keyframes viewIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
.view-root.page-fwd  { animation: pageFwd  var(--dur-page) var(--ease-page) both; transform-origin: left center; }
.view-root.page-back { animation: pageBack var(--dur-page) var(--ease-page) both; transform-origin: right center; }
@keyframes pageFwd  { from { opacity: 0; transform: translateX(4%) rotateY(6deg); }   to { opacity: 1; transform: none; } }
@keyframes pageBack { from { opacity: 0; transform: translateX(-4%) rotateY(-6deg); } to { opacity: 1; transform: none; } }
@media (prefers-reduced-motion: reduce) {
  .view-root, .view-root.page-fwd, .view-root.page-back { animation: none; }
}

/* ---- Library as a table of contents ---- */
.cat-name .part-label {
  font-size: 0.68rem;
  letter-spacing: .14em;
  text-transform: uppercase;
  color: var(--text-muted);
  margin-right: var(--space-2);
  font-weight: 700;
}
.chapter-num {
  display: block;
  font-family: var(--font);
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  font-size: 0.72rem;
  letter-spacing: .06em;
  color: var(--text-muted);
  margin-bottom: 2px;
}

/* ---- Contents drawer (hover-reveal Parts → Chapters) ---- */
.contents {
  position: fixed;
  top: 0; bottom: 0; left: 0;
  z-index: 40;
  width: min(300px, 84vw);
  background: var(--surface);
  border-right: 1px solid var(--border);
  box-shadow: var(--page-shadow);
  transform: translateX(-100%);
  transition: transform var(--dur-mid) var(--ease-soft);
  display: flex;
  flex-direction: column;
}
.contents:hover,
.contents:focus-within,
.contents.is-open { transform: translateX(0); }
@media (prefers-reduced-motion: reduce) { .contents { transition: none; } }

/* The little tab that pokes out at the screen's left edge when collapsed. */
.contents__handle {
  position: absolute;
  left: 100%;
  top: 88px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  background: var(--accent);
  color: var(--on-accent);
  border: none;
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  padding: var(--space-3) 6px;
  cursor: pointer;
  box-shadow: var(--shadow);
}
.contents__handle-icon { font-size: 1.05rem; line-height: 1; }
.contents__handle-text {
  writing-mode: vertical-rl;
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: .12em;
  text-transform: uppercase;
}

.contents__nav { overflow-y: auto; padding: var(--space-5) var(--space-4) var(--space-8); -webkit-overflow-scrolling: touch; }
.contents__title {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: .12em;
  color: var(--text-muted);
  margin: 0 0 var(--space-4);
}
.contents__part { margin-bottom: var(--space-4); }
.contents__part-name {
  font-family: var(--font);
  font-weight: 700;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: .07em;
  color: var(--text-muted);
  margin: 0 0 var(--space-2);
}
.contents__list { list-style: none; margin: 0 0 var(--space-2); padding: 0; }
.contents__link {
  display: block;
  padding: 6px var(--space-3);
  border-left: 2px solid transparent;
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  color: var(--text);
  text-decoration: none;
  font-size: 0.9rem;
  line-height: 1.3;
}
.contents__link:hover { background: var(--surface-2); }
.contents__link.is-current {
  border-left-color: var(--accent);
  color: var(--accent);
  font-weight: 600;
  background: var(--accent-weak);
}
.contents__backdrop { position: fixed; inset: 0; z-index: 39; background: rgba(0, 0, 0, .4); }
.contents__backdrop[hidden] { display: none; }

/* ---- Library landing: Parts overview tiles ---- */
.lib-hint { color: var(--text-muted); font-size: 0.9rem; margin: 0 0 var(--space-5); }
.parts-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: var(--space-3);
}
.part-tile {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  padding: var(--space-4);
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 3px solid var(--gilt);
  border-radius: var(--radius);
  text-decoration: none;
  color: var(--text);
  box-shadow: var(--shadow);
  transition: transform var(--dur-fast) var(--ease-soft), background var(--dur-fast) ease;
}
.part-tile:hover { transform: translateY(-1px); background: var(--surface-2); }
@media (prefers-reduced-motion: reduce) { .part-tile { transition: none; } .part-tile:hover { transform: none; } }
.part-tile__eyebrow {
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: .12em;
  color: var(--text-muted);
  font-weight: 700;
}
.part-tile__name { font-family: var(--font-serif); font-size: 1.15rem; font-weight: 700; }
.part-tile__count { font-size: 0.82rem; color: var(--text-muted); }

/* ---- Flashcard 3D flip ---- */
.fc-scene { perspective: 1500px; margin: var(--space-4) 0; }
.fc-card {
  position: relative;
  display: grid;
  transition: transform var(--dur-page) var(--ease-page);
  transform-style: preserve-3d;
}
.fc-card .fc-face {
  grid-area: 1 / 1;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--page-shadow);
  padding: var(--space-6) var(--space-5);
  min-height: 240px;
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
}
.fc-face--back { transform: rotateY(180deg); }
.fc-card.is-flipped { transform: rotateY(180deg); }
@media (prefers-reduced-motion: reduce) {
  .fc-card { transition: none; transform-style: flat; }
  .fc-card .fc-face { backface-visibility: visible; -webkit-backface-visibility: visible; }
  .fc-face--back { transform: none; display: none; }
  .fc-card.is-flipped .fc-face--front { display: none; }
  .fc-card.is-flipped .fc-face--back { display: flex; }
}
