: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-eyebrow {
  display: inline-block;
  font-size: 12.5px;
  font-weight: 700;
  color: var(--accent);
  background: var(--accent-bg);
  padding: 4px 10px;
  border-radius: 999px;
  margin-bottom: 10px;
}
.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); }
