:root {
  --bg: #f6f2ea;
  --surface: #fffdf8;
  --surface-2: #efe7d9;
  --text: #201b16;
  --muted: #746a5f;
  --accent: #9a3e2b;
  --accent-soft: rgba(154, 62, 43, 0.12);
  --border: #e2d8c8;
  --code-bg: #0d1117;
  --code-fg: #e6edf3;
  --sidebar-w: 304px;
  --base-font-size: 18px;
  --topbar-h: 60px;
  --shadow: 0 18px 50px rgba(31, 24, 16, 0.10);
}

[data-theme="dark"] {
  --bg: #0f1215;
  --surface: #171b20;
  --surface-2: #20262d;
  --text: #e9e3d8;
  --muted: #a49b8e;
  --accent: #e2a06c;
  --accent-soft: rgba(226, 160, 108, 0.16);
  --border: #2b323a;
  --code-bg: #090c10;
  --code-fg: #dbe6f0;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.38);
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Charter, "Iowan Old Style", Georgia, "Times New Roman", serif;
  font-size: var(--base-font-size);
  line-height: 1.7;
  transition: background .25s ease, color .25s ease;
}

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

img {
  max-width: 100%;
  height: auto;
  border-radius: 8px;
}

button { font: inherit; color: inherit; }

/* ------------------------------------------------------------------ topbar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 900;
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 0 18px;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--border);
}
.brand {
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--text);
  text-decoration: none;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.top-actions { margin-left: auto; display: flex; align-items: center; gap: 8px; }
.font-controls { display: inline-flex; gap: 2px; }

.icon-btn {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  min-width: 38px;
  height: 38px;
  padding: 0 10px;
  border-radius: 10px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background .2s ease, border-color .2s ease;
}
.icon-btn:hover { background: var(--surface-2); border-color: var(--accent); }

/* --------------------------------------------------------------- progress */
#progress-bar {
  position: fixed;
  top: 0;
  left: 0;
  height: 3px;
  width: 0;
  background: var(--accent);
  z-index: 1200;
  transition: width 70ms linear;
}

/* ---------------------------------------------------------------- sidebar */
.sidebar {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  width: min(var(--sidebar-w), 88vw);
  background: var(--surface);
  border-right: 1px solid var(--border);
  z-index: 1000;
  transform: translateX(-102%);
  transition: transform .28s cubic-bezier(.4, 0, .2, 1);
  display: flex;
  flex-direction: column;
}
.sidebar.open { transform: translateX(0); box-shadow: var(--shadow); }
.sidebar-head {
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 18px;
  border-bottom: 1px solid var(--border);
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-weight: 700;
  letter-spacing: -0.01em;
}
.sidebar-title { color: var(--text); }
.sidebar-close { display: inline-flex; }
.toc-nav { overflow-y: auto; padding: 18px 14px 42px; }
.nav-section {
  margin: 14px 8px 8px;
  color: var(--muted);
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: .72rem;
  font-weight: 800;
  letter-spacing: .09em;
  text-transform: uppercase;
}
.chapter-list { list-style: none; margin: 0; padding: 0; }
.chapter-list li { margin: 0 0 4px; }
.nav-link {
  display: flex;
  gap: 10px;
  align-items: baseline;
  padding: 9px 10px;
  border-radius: 9px;
  color: var(--text);
  text-decoration: none;
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: .88rem;
  line-height: 1.35;
}
.nav-link:hover { background: var(--surface-2); }
.nav-link.active { background: var(--accent-soft); color: var(--accent); }
.nav-number { font-variant-numeric: tabular-nums; color: var(--muted); min-width: 1.7em; }
.nav-title { flex: 1; }

.backdrop {
  position: fixed;
  inset: 0;
  background: rgba(10, 8, 5, 0.45);
  z-index: 990;
  opacity: 0;
  pointer-events: none;
  transition: opacity .25s ease;
}
.backdrop.show { opacity: 1; pointer-events: auto; }

/* ------------------------------------------------------------------- page */
.page {
  min-height: 100vh;
  padding: 42px 22px 70px;
}
.reader {
  max-width: 800px;
  margin: 0 auto;
  font-size: var(--base-font-size);
}
.home {
  max-width: 1050px;
  margin: 0 auto;
}

/* ------------------------------------------------------------------ hero */
.hero {
  display: grid;
  grid-template-columns: 230px 1fr;
  gap: 38px;
  align-items: center;
  margin: 20px 0 52px;
}
.hero-cover {
  width: 100%;
  max-width: 260px;
  border-radius: 16px;
  box-shadow: 10px 18px 50px rgba(31, 24, 16, 0.22);
}
.hero-copy h1 {
  font-size: clamp(2rem, 5vw, 3.8rem);
  line-height: 1.05;
  letter-spacing: -0.045em;
  margin: 0 0 16px;
}
.hero-subtitle {
  color: var(--muted);
  font-size: 1.08rem;
  max-width: 640px;
  margin: 0 0 22px;
}
.eyebrow {
  color: var(--muted);
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: .72rem;
  font-weight: 800;
  letter-spacing: .12em;
  text-transform: uppercase;
  margin: 0 0 10px;
}

.continue-card {
  display: inline-flex;
  flex-direction: column;
  gap: 4px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 4px solid var(--accent);
  border-radius: 12px;
  padding: 14px 18px;
  text-decoration: none;
  color: var(--text);
  margin-bottom: 10px;
  box-shadow: 0 8px 30px rgba(31, 24, 16, 0.08);
  transition: transform .2s ease, box-shadow .2s ease;
}
.continue-card:hover { transform: translateY(-2px); box-shadow: var(--shadow); }
.continue-kicker {
  color: var(--accent);
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: .7rem;
  font-weight: 800;
  letter-spacing: .1em;
  text-transform: uppercase;
}
.continue-card strong {
  font-size: 1.05rem;
  font-weight: 700;
}
.continue-progress { color: var(--muted); font-size: .85rem; }

/* -------------------------------------------------------------- contents */
.contents h2 {
  font-size: 1.7rem;
  letter-spacing: -0.02em;
  margin: 0 0 24px;
}
.section-group { display: grid; gap: 36px; }
.section-heading {
  color: var(--muted);
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: .74rem;
  font-weight: 800;
  letter-spacing: .1em;
  text-transform: uppercase;
  margin: 0 0 12px;
}
.toc-list { list-style: none; margin: 0; padding: 0; }
.toc-link {
  display: grid;
  grid-template-columns: 44px 1fr auto;
  gap: 12px;
  align-items: center;
  padding: 14px 16px;
  border-radius: 12px;
  color: var(--text);
  text-decoration: none;
  border: 1px solid transparent;
  transition: background .18s ease, border-color .18s ease, transform .18s ease;
}
.toc-link:hover {
  background: var(--surface);
  border-color: var(--border);
  transform: translateX(4px);
}
.toc-number {
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: .82rem;
}
.toc-title {
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: .99rem;
  font-weight: 650;
  letter-spacing: -0.01em;
}
.toc-arrow { color: var(--accent); opacity: 0; transform: translateX(-4px); transition: opacity .18s ease, transform .18s ease; }
.toc-link:hover .toc-arrow { opacity: 1; transform: translateX(0); }

/* ------------------------------------------------------------ chapter body */
.chapter-header { margin-bottom: 30px; }
.chapter-header h1 {
  font-size: clamp(1.8rem, 4.5vw, 3rem);
  line-height: 1.12;
  letter-spacing: -0.035em;
  margin: 0 0 24px;
}
.reader-body {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: clamp(22px, 4vw, 52px);
  box-shadow: var(--shadow);
}
.reader-body > :first-child { margin-top: 0; }
.reader-body h1,
.reader-body h2,
.reader-body h3,
.reader-body h4 {
  line-height: 1.22;
  letter-spacing: -0.02em;
  margin: 1.8em 0 .6em;
}
.reader-body h1 { font-size: 1.75em; }
.reader-body h2 { font-size: 1.45em; }
.reader-body h3 { font-size: 1.2em; }
.reader-body p { margin: 0 0 1.1em; }
.reader-body strong { font-weight: 700; }
.reader-body blockquote {
  border-left: 4px solid var(--accent);
  margin: 1.6em 0;
  padding: .25em 1.3em;
  color: var(--muted);
  background: var(--accent-soft);
  border-radius: 0 10px 10px 0;
}
.reader-body blockquote p:last-child { margin-bottom: 0; }
.reader-body ul,
.reader-body ol { padding-left: 1.45em; margin: 0 0 1.2em; }
.reader-body li { margin: .25em 0; }
.reader-body table {
  width: 100%;
  border-collapse: collapse;
  margin: 1.5em 0;
  font-size: .88em;
  display: block;
  overflow-x: auto;
  white-space: nowrap;
}
.reader-body th,
.reader-body td {
  border: 1px solid var(--border);
  padding: 8px 10px;
  text-align: left;
  vertical-align: top;
}
.reader-body th { background: var(--surface-2); font-weight: 700; }
.reader-body figure { margin: 1.6em auto; text-align: center; }
.reader-body figcaption { color: var(--muted); font-size: .85em; margin-top: .6em; }
.reader-body hr { border: 0; border-top: 1px solid var(--border); margin: 2em 0; }

/* Code: always dark, always scrollable horizontally. */
.reader-body pre {
  background: var(--code-bg) !important;
  color: var(--code-fg);
  padding: 18px 20px;
  border-radius: 12px;
  overflow-x: auto;
  line-height: 1.52;
  font-size: .84em;
}
.reader-body .codehilite {
  background: var(--code-bg) !important;
  margin: 1.5em 0;
  border-radius: 12px;
  overflow: hidden;
}
.reader-body .codehilite pre {
  margin: 0;
  border-radius: 0;
  overflow-x: auto;
}
.reader-body code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
}
.reader-body :not(pre) > code {
  background: var(--surface-2);
  border-radius: 5px;
  padding: .12em .35em;
  font-size: .86em;
}
.reader-body .task-list { list-style: none; padding-left: .1em; }
.reader-body .task-list-item { display: flex; gap: .55em; align-items: flex-start; }
.reader-body .task-list-item input { margin-top: .28em; }

/* ----------------------------------------------------------- chapter nav */
.chapter-nav {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin: 26px 0 0;
}
.chapter-nav.bottom-nav { grid-template-columns: 1fr auto 1fr; margin-top: 26px; }
.nav-arrow {
  display: flex;
  flex-direction: column;
  gap: 3px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  text-decoration: none;
  color: var(--text);
  min-width: 0;
  transition: border-color .18s ease, transform .18s ease;
}
.nav-arrow:hover { border-color: var(--accent); transform: translateY(-2px); }
.nav-arrow.next { text-align: right; align-items: flex-end; }
.nav-arrow.toc { align-items: center; text-align: center; }
.nav-arrow-label {
  color: var(--accent);
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: .72rem;
  font-weight: 800;
  letter-spacing: .07em;
  text-transform: uppercase;
}
.nav-arrow-title {
  font-size: .9rem;
  line-height: 1.3;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

/* ------------------------------------------------------------- responsive */
@media (min-width: 960px) {
  .page { margin-left: var(--sidebar-w); padding: 46px 46px 90px; }
  .sidebar { transform: translateX(0); }
  .sidebar-close, #menu-btn, #backdrop { display: none; }
  .topbar { margin-left: var(--sidebar-w); }
}

@media (max-width: 959px) {
  .hero { grid-template-columns: 1fr; gap: 24px; }
  .hero-cover { max-width: 200px; }
  .page { padding-top: 28px; }
  .chapter-nav { grid-template-columns: 1fr; }
  .chapter-nav.bottom-nav { grid-template-columns: 1fr; }
  .nav-arrow.next { text-align: left; align-items: flex-start; }
  .nav-arrow.toc { align-items: flex-start; text-align: left; }
}

@media (max-width: 520px) {
  :root { --base-font-size: 17px; }
  .brand { max-width: 150px; }
  .font-controls { display: none; }
  .reader-body { padding: 18px 16px; border-radius: 14px; }
  .toc-link { grid-template-columns: 34px 1fr auto; padding: 12px 10px; }
}
