:root {
  --bg: #ffffff;
  --bg-elevated: #f6f7f9;
  --text: #16181d;
  --text-dim: #55606e;
  --border: #e4e7eb;
  --accent: #a6402f;
  --accent-bg: #fbeeec;
  --code-bg: #f1f2f4;
  --sidebar-bg: #fafafa;
  --shadow: 0 8px 24px rgba(20, 20, 30, 0.10);
  --max-w: 780px;
  color-scheme: light;
}

:root[data-theme="dark"] {
  --bg: #14161b;
  --bg-elevated: #1b1e25;
  --text: #e8e9ec;
  --text-dim: #a2a9b4;
  --border: #2b2f38;
  --accent: #e8a598;
  --accent-bg: #2a1c1a;
  --code-bg: #20232b;
  --sidebar-bg: #17191f;
  --shadow: 0 8px 28px rgba(0, 0, 0, 0.45);
  color-scheme: dark;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14161b;
    --bg-elevated: #1b1e25;
    --text: #e8e9ec;
    --text-dim: #a2a9b4;
    --border: #2b2f38;
    --accent: #e8a598;
    --accent-bg: #2a1c1a;
    --code-bg: #20232b;
    --sidebar-bg: #17191f;
    --shadow: 0 8px 28px rgba(0, 0, 0, 0.45);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  line-height: 1.65;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

/* ---------- Topbar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  gap: 10px;
  height: 56px;
  padding: 0 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(180%) blur(10px);
  border-bottom: 1px solid var(--border);
}
.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  border: none;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  flex: none;
}
.icon-btn:hover { background: var(--bg-elevated); }
.brand {
  flex: 1;
  min-width: 0;
  font-weight: 700;
  font-size: 15px;
  color: var(--text);
  text-decoration: none;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.brand-sub {
  font-weight: 400;
  color: var(--text-dim);
  font-size: 13px;
}
@media (max-width: 480px) {
  .brand-sub { display: none; }
}

/* ---------- Layout shell ---------- */
.shell {
  display: flex;
  align-items: flex-start;
  max-width: 1180px;
  margin: 0 auto;
}

.sidebar {
  width: 280px;
  flex: none;
  position: sticky;
  top: 56px;
  height: calc(100vh - 56px);
  overflow-y: auto;
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
  padding: 14px 10px 40px;
}
.sidebar-search { padding: 4px 6px 12px; }
#searchInput {
  width: 100%;
  padding: 9px 12px;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  outline: none;
}
#searchInput:focus { border-color: var(--accent); }

.nav-toc-link {
  display: block;
  padding: 9px 12px;
  margin: 2px 4px 10px;
  border-radius: 8px;
  color: var(--text);
  text-decoration: none;
  font-weight: 600;
  font-size: 14px;
  border: 1px solid var(--border);
}
.nav-toc-link:hover { background: var(--bg-elevated); }

.nav-part { margin: 14px 4px 6px; padding: 0 8px; font-size: 11.5px; text-transform: uppercase; letter-spacing: .04em; color: var(--text-dim); font-weight: 700; }
.nav-item {
  display: flex;
  gap: 8px;
  align-items: baseline;
  padding: 8px 12px;
  margin: 1px 4px;
  border-radius: 8px;
  color: var(--text);
  text-decoration: none;
  font-size: 13.5px;
  line-height: 1.4;
}
.nav-item .n { color: var(--text-dim); font-variant-numeric: tabular-nums; flex: none; }
.nav-item:hover { background: var(--bg-elevated); }
.nav-item.active { background: var(--accent-bg); color: var(--accent); font-weight: 600; }
.nav-item.hidden { display: none; }

.content {
  flex: 1;
  min-width: 0;
  padding: 28px 28px 80px;
  max-width: var(--max-w);
  margin: 0 auto;
}
.content:focus { outline: none; }

/* ---------- Prose ---------- */
.content h1 { font-size: 26px; margin: 0 0 6px; line-height: 1.3; }
.content h2 { font-size: 19px; margin: 34px 0 12px; padding-top: 6px; border-top: 1px solid var(--border); }
.content h2:first-of-type { border-top: none; padding-top: 0; }
.content h3 { font-size: 16px; margin: 22px 0 8px; color: var(--text); }
.content p { margin: 0 0 14px; color: var(--text); }
.content strong { color: var(--text); }
.content ul, .content ol { padding-left: 22px; margin: 0 0 14px; }
.content li { margin-bottom: 6px; }
.content a { color: var(--accent); text-decoration: underline; text-decoration-color: color-mix(in srgb, var(--accent) 40%, transparent); text-underline-offset: 2px; }
.content code { background: var(--code-bg); padding: .15em .4em; border-radius: 5px; font-size: .92em; }
.content hr { border: none; border-top: 1px solid var(--border); margin: 28px 0; }
.content blockquote {
  margin: 0 0 14px;
  padding: 4px 14px;
  border-left: 3px solid var(--accent);
  color: var(--text-dim);
  background: var(--bg-elevated);
  border-radius: 0 8px 8px 0;
}

.table-wrap { overflow-x: auto; margin: 0 0 18px; border: 1px solid var(--border); border-radius: 10px; }
.content table { border-collapse: collapse; width: 100%; font-size: 13.5px; min-width: 420px; }
.content th, .content td { padding: 9px 12px; border-bottom: 1px solid var(--border); text-align: left; white-space: normal; }
.content th { background: var(--bg-elevated); font-weight: 700; white-space: nowrap; }
.content tr:last-child td { border-bottom: none; }

/* Chapter meta / pager */
.chapter-head-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 6px;
}
.chapter-eyebrow {
  display: inline-block;
  font-size: 12.5px;
  font-weight: 700;
  color: var(--accent);
  background: var(--accent-bg);
  padding: 4px 10px;
  border-radius: 999px;
}
.source-link {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-dim);
  text-decoration: none;
  border: 1px solid var(--border);
  padding: 4px 10px;
  border-radius: 999px;
}
.source-link:hover { color: var(--accent); border-color: var(--accent); }

/* ---------- Diagrams ---------- */
.diagram {
  margin: 8px 0 22px;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--bg-elevated);
  overflow-x: auto;
}
.diagram-title {
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--text-dim);
  margin-bottom: 12px;
}
.flow-row {
  display: flex;
  align-items: stretch;
  gap: 8px;
  flex-wrap: nowrap;
  min-width: max-content;
}
.flow-row.wrap { flex-wrap: wrap; min-width: 0; }
.flow-box {
  flex: 1;
  min-width: 108px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 13px;
  line-height: 1.35;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
}
.flow-box.accent { border-color: var(--accent); background: var(--accent-bg); color: var(--accent); font-weight: 700; }
.flow-arrow {
  flex: none;
  display: flex;
  align-items: center;
  color: var(--text-dim);
  font-size: 18px;
  padding: 0 2px;
}
.flow-stage {
  margin-bottom: 14px;
}
.flow-stage:last-child { margin-bottom: 0; }
.flow-stage-label {
  font-size: 12px;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 6px;
}
.bar-3 {
  display: flex;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--border);
  font-size: 12.5px;
  text-align: center;
  margin: 6px 0;
}
.bar-3 > div { padding: 10px 6px; flex: 1; }
.bar-3 .lo { background: var(--bg); color: var(--text-dim); }
.bar-3 .hi { background: var(--accent-bg); color: var(--accent); font-weight: 700; }
@media (max-width: 480px) {
  .flow-row:not(.wrap) { flex-direction: column; }
  .flow-row:not(.wrap) .flow-arrow { transform: rotate(90deg); align-self: center; }
}
.pager {
  display: flex;
  gap: 12px;
  margin-top: 44px;
  padding-top: 20px;
  border-top: 1px solid var(--border);
}
.pager a {
  flex: 1;
  min-width: 0;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  text-decoration: none;
  color: var(--text);
}
.pager a:hover { background: var(--bg-elevated); border-color: var(--accent); }
.pager .dir { font-size: 11.5px; color: var(--text-dim); text-transform: uppercase; letter-spacing: .03em; }
.pager .ttl { display: block; margin-top: 3px; font-size: 13.5px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pager .next { text-align: right; }

/* Home / TOC page */
.home-hero { margin-bottom: 26px; }
.home-hero p { color: var(--text-dim); }
.home-note {
  font-size: 13px;
  color: var(--text-dim);
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  margin-top: 26px;
}
.part-block { margin-bottom: 26px; }
.part-title { font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--text-dim); font-weight: 700; margin-bottom: 8px; }
.chapter-card-list { display: flex; flex-direction: column; gap: 8px; }
.chapter-card {
  display: flex;
  gap: 12px;
  align-items: baseline;
  padding: 13px 15px;
  border: 1px solid var(--border);
  border-radius: 12px;
  text-decoration: none;
  color: var(--text);
}
.chapter-card:hover { border-color: var(--accent); background: var(--bg-elevated); }
.chapter-card .num { font-weight: 700; color: var(--accent); flex: none; font-variant-numeric: tabular-nums; }

/* ---------- Mobile ---------- */
.scrim { display: none; }
@media (max-width: 860px) {
  .sidebar {
    position: fixed;
    top: 56px;
    left: 0;
    height: calc(100vh - 56px);
    width: min(86vw, 320px);
    z-index: 50;
    transform: translateX(-100%);
    transition: transform .22s ease;
    box-shadow: var(--shadow);
  }
  .sidebar.open { transform: translateX(0); }
  .scrim {
    display: block;
    position: fixed;
    inset: 56px 0 0 0;
    background: rgba(0,0,0,.4);
    z-index: 45;
    opacity: 0;
    pointer-events: none;
    transition: opacity .22s ease;
  }
  .scrim.open { opacity: 1; pointer-events: auto; }
  .content { padding: 20px 16px 64px; }
  .content h1 { font-size: 22px; }
  .content h2 { font-size: 17.5px; }
  .pager { flex-direction: column; }
}

@media (min-width: 861px) {
  #menuBtn { display: none; }
}

::selection { background: var(--accent-bg); }

/* ---------- Slide presentation mode ---------- */
.slide-overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: var(--bg);
  color: var(--text);
  display: none;
  flex-direction: column;
}
.slide-overlay.open { display: flex; }
body.slides-lock { overflow: hidden; }

.slide-topbar {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
}
.slide-counter { font-size: 13px; color: var(--text-dim); font-variant-numeric: tabular-nums; }
.slide-icon-btn {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  border-radius: 8px;
  padding: 7px 14px;
  font-size: 13px;
  cursor: pointer;
}
.slide-icon-btn:hover { background: var(--bg-elevated); border-color: var(--accent); }

.slide-stage {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 6vh 24px;
}
.slide {
  max-width: 860px;
  width: 100%;
  margin: 0 auto;
}
.slide.slide-title {
  text-align: center;
  min-height: 72vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
}
.slide h1 { font-size: clamp(24px, 4.5vw, 40px); line-height: 1.25; margin: 0 0 16px; }
.slide h2 { font-size: clamp(20px, 3.4vw, 28px); line-height: 1.3; margin: 0 0 20px; border: none; padding: 0; }
.slide h3 { font-size: clamp(16px, 2.4vw, 20px); margin: 18px 0 8px; }
.slide p { font-size: clamp(15px, 2vw, 19px); line-height: 1.65; margin: 0 0 16px; }
.slide-subtitle { font-size: clamp(14px, 1.8vw, 19px); color: var(--text-dim); margin: 0 0 14px; }
.slide ul, .slide ol { font-size: clamp(14px, 1.9vw, 18px); line-height: 1.6; padding-left: 24px; margin: 0 0 14px; }
.slide li { margin-bottom: 8px; }
.slide strong { color: var(--accent); }
.slide a { color: var(--accent); }
.slide code { background: var(--code-bg); padding: .15em .4em; border-radius: 5px; }
.slide blockquote {
  margin: 0 0 14px; padding: 4px 14px; border-left: 3px solid var(--accent);
  color: var(--text-dim); background: var(--bg-elevated); border-radius: 0 8px 8px 0;
  font-size: clamp(14px, 1.8vw, 18px);
}
.slide hr { display: none; }
.slide .diagram { margin: 6px 0 4px; }
.slide table { border-collapse: collapse; width: 100%; font-size: clamp(12.5px, 1.6vw, 15px); min-width: 380px; }
.slide th, .slide td { padding: 8px 10px; border-bottom: 1px solid var(--border); text-align: left; }
.slide th { background: var(--bg-elevated); font-weight: 700; }
.slide .table-wrap { max-height: 50vh; }
.slide .part-block { text-align: left; }
.slide .part-title { font-size: 13px; }
.slide .chapter-card { font-size: clamp(13.5px, 1.8vw, 16px); }

.slide-controls {
  flex: none;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 12px 20px calc(12px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
}
.slide-nav-btn {
  width: 42px;
  height: 42px;
  flex: none;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
}
.slide-nav-btn:hover:not(:disabled) { background: var(--bg-elevated); border-color: var(--accent); color: var(--accent); }
.slide-nav-btn:disabled { opacity: .3; cursor: default; }
.slide-progress { flex: 1; height: 4px; background: var(--border); border-radius: 999px; overflow: hidden; }
.slide-progress-bar { height: 100%; width: 0%; background: var(--accent); transition: width .2s ease; }
