:root {
  --reader-font-size: 20px;

  --bg: #f7f2e8;
  --bg-soft: #efe8db;
  --surface: #fffdf8;
  --surface-soft: rgba(255, 253, 248, 0.72);
  --topbar-bg: rgba(247, 242, 232, 0.86);
  --sidebar-bg: rgba(244, 238, 226, 0.92);
  --text: #24211c;
  --muted: #6f6a60;
  --faint: #999184;
  --border: rgba(36, 33, 28, 0.12);
  --border-strong: rgba(36, 33, 28, 0.2);
  --accent: #b84a2e;
  --accent-strong: #943619;
  --accent-soft: rgba(184, 74, 46, 0.11);
  --quote-bg: rgba(184, 74, 46, 0.055);
  --inline-code-bg: rgba(47, 52, 64, 0.08);
  --inline-code-color: #a33422;
  --table-head-bg: rgba(47, 52, 64, 0.07);
  --table-stripe: rgba(47, 52, 64, 0.03);
  --shadow-soft: 0 18px 50px rgba(66, 51, 29, 0.11);
  --shadow-card: 0 12px 34px rgba(66, 51, 29, 0.08);
  --code-bg: #1f2430;
  --code-text: #f8f8f2;
  --kbd-bg: #f3ede2;
}

:root[data-theme="dark"] {
  --bg: #131519;
  --bg-soft: #1a1c21;
  --surface: #1b1e24;
  --surface-soft: rgba(27, 30, 36, 0.8);
  --topbar-bg: rgba(19, 21, 25, 0.88);
  --sidebar-bg: rgba(20, 22, 27, 0.94);
  --text: #e9e3d8;
  --muted: #a8a29a;
  --faint: #7c786f;
  --border: rgba(233, 227, 216, 0.12);
  --border-strong: rgba(233, 227, 216, 0.2);
  --accent: #e0825f;
  --accent-strong: #f09b7c;
  --accent-soft: rgba(224, 130, 95, 0.14);
  --quote-bg: rgba(224, 130, 95, 0.08);
  --inline-code-bg: rgba(255, 255, 255, 0.09);
  --inline-code-color: #f2a086;
  --table-head-bg: rgba(255, 255, 255, 0.08);
  --table-stripe: rgba(255, 255, 255, 0.035);
  --shadow-soft: 0 22px 60px rgba(0, 0, 0, 0.42);
  --shadow-card: 0 16px 40px rgba(0, 0, 0, 0.34);
  --code-bg: #171a20;
  --code-text: #f8f8f2;
  --kbd-bg: #252931;
}

* {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
  background: var(--bg);
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", sans-serif;
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
}

a {
  color: inherit;
}

button {
  font-family: inherit;
}

:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--accent) 55%, transparent);
  outline-offset: 2px;
}

.reading-progress {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 100;
  height: 3px;
  width: 100%;
  pointer-events: none;
  background: linear-gradient(90deg, var(--accent), #e4a56f);
  transform: scaleX(0);
  transform-origin: 0 50%;
  box-shadow: 0 1px 5px rgba(184, 74, 46, 0.3);
}

body.home-page .reading-progress {
  display: none;
}

/* -------------------------------------------------- */
/* Home page                                           */
/* -------------------------------------------------- */

.home-shell {
  width: min(1120px, 100%);
  margin: 0 auto;
  padding: 72px 22px 48px;
}

.hero {
  max-width: 820px;
  margin: 0 auto 54px;
  text-align: center;
}

.hero-kicker {
  margin: 0 0 16px;
  color: var(--accent);
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.16em;
  text-transform: uppercase;
}

.hero h1 {
  margin: 0;
  font-family: Georgia, "Songti SC", "Noto Serif CJK SC", "Source Han Serif SC", "STSong", serif;
  font-size: clamp(2.25rem, 6vw, 4.8rem);
  line-height: 1.08;
  letter-spacing: -0.03em;
  color: var(--text);
  text-wrap: balance;
}

.hero-subtitle {
  max-width: 690px;
  margin: 24px auto 0;
  color: var(--muted);
  font-size: 1.04rem;
  line-height: 1.8;
}

.hero-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin-top: 26px;
  color: var(--faint);
  font-size: 0.9rem;
}

.toc-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 16px;
}

.toc-card {
  position: relative;
  display: flex;
  min-height: 156px;
  flex-direction: column;
  justify-content: space-between;
  padding: 22px;
  overflow: hidden;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 22px;
  box-shadow: var(--shadow-card);
  text-decoration: none;
  transition: transform 0.22s ease, border-color 0.22s ease, box-shadow 0.22s ease;
}

.toc-card::after {
  content: "";
  position: absolute;
  right: -32px;
  bottom: -32px;
  width: 96px;
  height: 96px;
  border-radius: 50%;
  background: var(--accent-soft);
  transition: transform 0.25s ease;
}

.toc-card:hover {
  transform: translateY(-3px);
  border-color: color-mix(in srgb, var(--accent) 46%, var(--border));
  box-shadow: var(--shadow-soft);
}

.toc-card:hover::after {
  transform: scale(1.55);
}

.toc-card-label {
  position: relative;
  z-index: 1;
  color: var(--accent);
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.08em;
}

.toc-card-title {
  position: relative;
  z-index: 1;
  margin-top: 18px;
  color: var(--text);
  font-family: Georgia, "Songti SC", "Noto Serif CJK SC", "Source Han Serif SC", "STSong", serif;
  font-size: 1.28rem;
  font-weight: 650;
  line-height: 1.45;
  text-wrap: balance;
}

.home-footer {
  margin-top: 42px;
  text-align: center;
  color: var(--faint);
  font-size: 0.9rem;
}

/* -------------------------------------------------- */
/* Reading shell                                       */
/* -------------------------------------------------- */

.app-shell {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  position: fixed;
  inset: 0 auto 0 0;
  z-index: 80;
  width: min(326px, 86vw);
  overflow-y: auto;
  padding: 26px 18px 34px;
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
  transform: translateX(-105%);
  transition: transform 0.26s ease;
}

body.sidebar-open .sidebar {
  transform: translateX(0);
  box-shadow: 20px 0 60px rgba(0, 0, 0, 0.18);
}

.sidebar-scrim {
  position: fixed;
  inset: 0;
  z-index: 70;
  display: block;
  width: 100%;
  border: 0;
  background: rgba(8, 9, 12, 0.42);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.26s ease;
  cursor: default;
}

body.sidebar-open .sidebar-scrim {
  opacity: 1;
  pointer-events: auto;
}

.sidebar-header {
  padding: 6px 10px 18px;
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 11px;
  text-decoration: none;
  color: var(--text);
}

.brand-mark {
  display: inline-grid;
  width: 38px;
  height: 38px;
  place-items: center;
  flex: 0 0 38px;
  border-radius: 12px;
  background: linear-gradient(145deg, var(--accent), #d97a4b);
  color: #fff;
  font-size: 0.8rem;
  font-weight: 800;
  letter-spacing: -0.02em;
  box-shadow: 0 7px 18px color-mix(in srgb, var(--accent) 30%, transparent);
}

.brand-text {
  font-size: 1rem;
  font-weight: 700;
  line-height: 1.35;
}

.brand-subtitle {
  margin: 8px 0 0 49px;
  color: var(--faint);
  font-size: 0.78rem;
}

.chapter-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding-top: 18px;
}

.chapter-list-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 9px;
  padding: 10px 12px;
  border-radius: 12px;
  color: var(--accent);
  font-size: 0.9rem;
  font-weight: 700;
  text-decoration: none;
  background: var(--accent-soft);
}

.chapter-link {
  display: grid;
  grid-template-columns: auto 1fr;
  column-gap: 11px;
  align-items: baseline;
  padding: 10px 12px;
  border-radius: 13px;
  text-decoration: none;
  color: var(--muted);
  transition: background 0.18s ease, color 0.18s ease;
}

.chapter-link:hover {
  background: var(--bg-soft);
  color: var(--text);
}

.chapter-link.active {
  background: var(--accent-soft);
  color: var(--accent-strong);
}

.chapter-link-label {
  min-width: 3.3em;
  color: inherit;
  font-size: 0.74rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  white-space: nowrap;
}

.chapter-link-title {
  color: inherit;
  font-size: 0.91rem;
  line-height: 1.45;
}

.main-pane {
  width: 100%;
  min-width: 0;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 50;
  display: flex;
  min-height: 62px;
  align-items: center;
  gap: 12px;
  padding: 8px max(14px, env(safe-area-inset-right)) 8px max(14px, env(safe-area-inset-left));
  background: var(--topbar-bg);
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
}

.topbar-title {
  display: flex;
  min-width: 0;
  flex: 1;
  flex-direction: column;
  gap: 2px;
}

.topbar-label {
  color: var(--accent);
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.06em;
}

.topbar-heading {
  overflow: hidden;
  color: var(--text);
  font-size: 0.96rem;
  font-weight: 650;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 7px;
  margin-left: auto;
}

.icon-button {
  display: inline-grid;
  min-width: 34px;
  height: 34px;
  place-items: center;
  padding: 0 9px;
  border: 1px solid var(--border);
  border-radius: 11px;
  background: var(--surface-soft);
  color: var(--text);
  cursor: pointer;
  font-size: 0.82rem;
  font-weight: 700;
  transition: border-color 0.18s ease, background 0.18s ease, color 0.18s ease;
}

.icon-button:hover {
  border-color: var(--accent);
  color: var(--accent-strong);
  background: var(--accent-soft);
}

.font-size-current {
  min-width: 25px;
  text-align: center;
  color: var(--muted);
  font-size: 0.78rem;
  font-variant-numeric: tabular-nums;
}

.icon-menu {
  position: relative;
  display: inline-block;
  width: 14px;
  height: 2px;
  background: currentColor;
  border-radius: 3px;
}

.icon-menu::before,
.icon-menu::after {
  content: "";
  position: absolute;
  left: 0;
  width: 14px;
  height: 2px;
  background: currentColor;
  border-radius: 3px;
}

.icon-menu::before {
  top: -5px;
}

.icon-menu::after {
  top: 5px;
}

.theme-sun {
  display: inline;
}

:root[data-theme="dark"] .theme-sun {
  display: none;
}

.theme-moon {
  display: none;
}

:root[data-theme="dark"] .theme-moon {
  display: inline;
}

.reading-main {
  width: min(860px, 100%);
  margin: 0 auto;
  padding: 42px 20px 90px;
}

.chapter-pager {
  display: flex;
  gap: 12px;
  margin: 0 0 36px;
}

.chapter-pager-bottom {
  margin: 46px 0 0;
}

.pager-link {
  display: flex;
  min-width: 0;
  flex: 1 1 50%;
  flex-direction: column;
  gap: 5px;
  padding: 13px 15px;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--surface);
  text-decoration: none;
  color: var(--text);
  transition: border-color 0.18s ease, transform 0.18s ease, box-shadow 0.18s ease;
}

.pager-link:hover {
  border-color: color-mix(in srgb, var(--accent) 45%, var(--border));
  transform: translateY(-2px);
  box-shadow: var(--shadow-card);
}

.pager-next {
  text-align: right;
  align-items: flex-end;
}

.pager-direction {
  color: var(--accent);
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.04em;
}

.pager-title {
  overflow: hidden;
  color: var(--muted);
  font-size: 0.92rem;
  line-height: 1.45;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 100%;
}

.is-empty {
  background: transparent;
  box-shadow: none;
  opacity: 0.78;
}

.chapter-article {
  color: var(--text);
  font-family: Georgia, "Songti SC", "Noto Serif CJK SC", "Source Han Serif SC", "STSong", serif;
  font-size: var(--reader-font-size);
  line-height: 1.92;
  letter-spacing: 0.005em;
  word-break: break-word;
}

.chapter-header {
  margin-bottom: 38px;
}

.chapter-eyebrow {
  margin: 0 0 10px;
  color: var(--accent);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  font-size: 0.76rem;
  font-weight: 750;
  letter-spacing: 0.12em;
}

.chapter-header h1 {
  margin: 0;
  color: var(--text);
  font-size: calc(var(--reader-font-size) * 1.72);
  line-height: 1.2;
  letter-spacing: -0.02em;
  text-wrap: balance;
}

/* -------------------------------------------------- */
/* Markdown content                                    */
/* -------------------------------------------------- */

.chapter-content > :first-child {
  margin-top: 0;
}

.chapter-content > :last-child {
  margin-bottom: 0;
}

.chapter-content h2,
.chapter-content h3,
.chapter-content h4,
.chapter-content h5,
.chapter-content h6 {
  scroll-margin-top: 78px;
  color: var(--text);
  font-family: Georgia, "Songti SC", "Noto Serif CJK SC", "Source Han Serif SC", "STSong", serif;
  line-height: 1.32;
  letter-spacing: -0.01em;
  text-wrap: balance;
}

.chapter-content h2 {
  margin: 2.15em 0 0.8em;
  font-size: 1.52em;
}

.chapter-content h3 {
  margin: 1.9em 0 0.72em;
  font-size: 1.25em;
}

.chapter-content h4 {
  margin: 1.7em 0 0.62em;
  font-size: 1.08em;
}

.chapter-content h5,
.chapter-content h6 {
  margin: 1.55em 0 0.58em;
  font-size: 1em;
}

.chapter-content p {
  margin: 0 0 1.35em;
}

.chapter-content a {
  color: var(--accent);
  text-decoration-color: color-mix(in srgb, var(--accent) 42%, transparent);
  text-decoration-thickness: 1px;
  text-underline-offset: 3px;
}

.chapter-content a:hover {
  color: var(--accent-strong);
  text-decoration-color: currentColor;
}

.chapter-content strong {
  color: var(--text);
  font-weight: 700;
}

.chapter-content em {
  font-style: italic;
}

.chapter-content ul,
.chapter-content ol {
  margin: 1.15em 0 1.4em;
  padding-left: 1.55em;
}

.chapter-content li {
  margin: 0.42em 0;
  padding-left: 0.18em;
}

.chapter-content li::marker {
  color: var(--accent);
}

.chapter-content li > ul,
.chapter-content li > ol {
  margin: 0.45em 0;
}

.chapter-content blockquote {
  margin: 1.7em 0;
  padding: 0.9em 1.15em;
  border-left: 4px solid var(--accent);
  border-radius: 4px 16px 16px 4px;
  background: var(--quote-bg);
  color: var(--muted);
}

.chapter-content blockquote p {
  margin: 0.45em 0;
}

.chapter-content blockquote p:first-child {
  margin-top: 0;
}

.chapter-content blockquote p:last-child {
  margin-bottom: 0;
}

.chapter-content hr {
  margin: 2.5em 0;
  border: 0;
  border-top: 1px solid var(--border);
}

.chapter-content img {
  display: block;
  max-width: 100%;
  height: auto;
  margin: 1.9em auto;
  border-radius: 14px;
}

:root[data-theme="dark"] .chapter-content img {
  padding: 8px;
  background: rgba(255, 255, 255, 0.92);
}

.chapter-content figure {
  margin: 1.8em 0;
}

.chapter-content figcaption {
  margin-top: 0.6em;
  color: var(--muted);
  font-size: 0.86em;
  text-align: center;
}

/* Tables */
.chapter-content table {
  display: block;
  width: 100%;
  max-width: 100%;
  overflow-x: auto;
  margin: 1.8em 0;
  border-collapse: collapse;
  border-radius: 12px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  font-size: 0.86em;
  line-height: 1.55;
  -webkit-overflow-scrolling: touch;
}

.chapter-content thead {
  background: var(--table-head-bg);
}

.chapter-content th,
.chapter-content td {
  min-width: 120px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  text-align: left;
  vertical-align: top;
}

.chapter-content th {
  color: var(--text);
  font-weight: 750;
  white-space: nowrap;
}

.chapter-content tbody tr:nth-child(even) {
  background: var(--table-stripe);
}

/* Code */
.chapter-content code,
.chapter-content kbd,
.chapter-content samp {
  font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", "Courier New", monospace;
}

.chapter-content :not(pre) > code {
  padding: 0.14em 0.42em;
  border-radius: 6px;
  background: var(--inline-code-bg);
  color: var(--inline-code-color);
  font-size: 0.85em;
  white-space: pre-wrap;
}

.chapter-content pre {
  margin: 1.65em 0;
  overflow-x: auto;
  border-radius: 14px;
  background: var(--code-bg) !important;
  color: var(--code-text);
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.16);
  -webkit-overflow-scrolling: touch;
  direction: ltr;
}

.chapter-content pre code {
  display: block;
  min-width: max-content;
  padding: 1.05em 1.15em;
  background: transparent;
  color: inherit;
  font-size: 0.86em;
  line-height: 1.62;
  white-space: pre;
}

.chapter-content .highlight {
  margin: 1.65em 0;
  overflow-x: auto;
  border-radius: 14px;
  background: var(--code-bg) !important;
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.16);
}

.chapter-content .highlight pre {
  margin: 0;
  overflow: visible;
  border-radius: 0;
  background: transparent !important;
  box-shadow: none;
}

/* Pygments emits inline spans; keep their foreground colors. */
.chapter-content .highlight code {
  min-width: max-content;
}

.katex-display {
  margin: 1.5em 0;
  overflow-x: auto;
  overflow-y: hidden;
  padding: 0.4em 0;
}

.katex {
  font-size: 1.04em;
}

/* Footnotes */
.footnote {
  margin-top: 2.5em;
  padding-top: 1.3em;
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 0.84em;
  line-height: 1.7;
}

.footnote hr {
  display: none;
}

.footnote ol {
  margin: 0;
  padding-left: 1.4em;
}

.footnote li {
  margin: 0.7em 0;
}

sup {
  line-height: 0;
}

a.footnote-ref,
a.footnote-backref {
  margin: 0 0.18em;
  color: var(--accent);
  text-decoration: none;
}

a.footnote-ref:hover,
a.footnote-backref:hover {
  text-decoration: underline;
}

/* Task lists (present for extension parity, though this book does not use them) */
.chapter-content .task-list {
  padding-left: 0;
  list-style: none;
}

.chapter-content .task-list-item {
  display: flex;
  align-items: flex-start;
  gap: 0.5em;
}

.chapter-content .task-list-item input {
  margin: 0.38em 0 0;
  accent-color: var(--accent);
}

/* -------------------------------------------------- */
/* Desktop                                             */
/* -------------------------------------------------- */

@media (min-width: 1024px) {
  .sidebar {
    position: sticky;
    top: 0;
    flex: 0 0 318px;
    width: 318px;
    height: 100vh;
    transform: none;
    box-shadow: none;
  }

  .sidebar-scrim {
    display: none;
  }

  .main-pane {
    flex: 1;
  }

  .menu-button {
    display: none;
  }

  body.sidebar-collapsed .sidebar {
    display: none;
  }

  .topbar {
    padding-left: 24px;
    padding-right: 24px;
  }

  .reading-main {
    padding: 52px 46px 110px;
  }

  .chapter-pager {
    gap: 14px;
  }
}

@media (max-width: 600px) {
  .home-shell {
    padding: 48px 16px 34px;
  }

  .toc-grid {
    grid-template-columns: 1fr;
  }

  .toc-card {
    min-height: 132px;
  }

  .topbar-heading {
    max-width: 36vw;
  }

  .topbar-actions {
    gap: 5px;
  }

  .icon-button {
    min-width: 31px;
    height: 31px;
    padding: 0 7px;
  }

  .reading-main {
    padding: 32px 16px 70px;
  }

  .chapter-header h1 {
    font-size: calc(var(--reader-font-size) * 1.45);
  }

  .chapter-pager {
    flex-direction: column;
  }

  .pager-next {
    align-items: flex-start;
    text-align: left;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
  }
}

.last-read-badge {
  position: absolute;
  top: 14px;
  right: 14px;
  z-index: 2;
  padding: 4px 8px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent-strong);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.04em;
}
