:root,
[data-theme="default"] {
  color-scheme: light;
  --bg: #f4f1ea;
  --ink: #1d1916;
  --muted: #5f574f;
  --line: #ddd4c8;
  --accent: #8f3d32;
  --link: #8f3d32;
  --paper: #fbf8f3;
  --code: #efe8de;
  --font: "PingFang SC", "Hiragino Sans GB", "Noto Sans SC", "Microsoft YaHei", "WenQuanYi Micro Hei", sans-serif;
  --size: 17px;
  --lh: 1.75;
  --measure: 72rem;
  --gap: 1.15rem;
  --cell: 0.8rem 0.85rem;
  --h1: 1.85rem;
}

[data-theme="serif"] {
  color-scheme: light;
  --bg: #f7f1e4;
  --ink: #241c14;
  --muted: #5c5146;
  --line: #e0d3bf;
  --accent: #6e4428;
  --link: #6e4428;
  --paper: #fbf6ec;
  --code: #efe4d2;
  --font: "Iowan Old Style", Palatino, "Palatino Linotype", "Songti SC", "Noto Serif SC", "Source Han Serif SC", "STSong", PMingLiU, serif;
  --size: 19px;
  --lh: 1.9;
  --measure: 68rem;
  --gap: 1.25rem;
  --h1: 2.05rem;
}

[data-theme="compact"] {
  color-scheme: light;
  --bg: #f7f6f3;
  --ink: #1c1b19;
  --muted: #5c5854;
  --line: #e1ddd6;
  --accent: #3f4d3a;
  --link: #3f4d3a;
  --paper: #ffffff;
  --code: #f0eeea;
  --font: "PingFang SC", "Hiragino Sans GB", "Noto Sans SC", "Microsoft YaHei", "WenQuanYi Micro Hei", sans-serif;
  --size: 15px;
  --lh: 1.45;
  --measure: 78rem;
  --gap: 0.65rem;
  --cell: 0.4rem 0.55rem;
  --h1: 1.45rem;
}

[data-theme="dark"] {
  color-scheme: dark;
  --bg: #171614;
  --ink: #ece6dc;
  --muted: #b7aea3;
  --line: #3a342e;
  --accent: #e4b5a2;
  --link: #f0c4b2;
  --paper: #221f1c;
  --code: #2c2824;
  --font: "PingFang SC", "Hiragino Sans GB", "Noto Sans SC", "Microsoft YaHei", "WenQuanYi Micro Hei", sans-serif;
  --size: 17px;
  --lh: 1.75;
  --measure: 72rem;
  --gap: 1.15rem;
  --cell: 0.8rem 0.85rem;
  --h1: 1.85rem;
}

* { box-sizing: border-box; }

html {
  background: var(--bg);
  scroll-padding-top: 4.5rem;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  font-size: var(--size);
  line-height: var(--lh);
}

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

a:hover { text-decoration-thickness: 2px; }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.top {
  position: sticky;
  top: 0;
  z-index: 2;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem 1.25rem;
  max-width: var(--measure);
  margin: 0 auto;
  padding: 0.85rem 1.25rem 0.75rem;
  background: var(--bg);
  border-top: 4px solid var(--accent);
  border-bottom: 1px solid var(--line);
}

.brand {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 650;
  letter-spacing: 0.04em;
  line-height: 1.3;
}

.brand a {
  color: var(--ink);
  text-decoration: none;
}

.themes {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 0.35rem;
  margin-left: auto;
}

.themes button {
  border: 1px solid var(--line);
  background: var(--paper);
  color: var(--muted);
  font: inherit;
  font-size: 0.8rem;
  line-height: 1.3;
  padding: 0.28rem 0.62rem;
  border-radius: 999px;
  cursor: pointer;
}

.themes button[aria-pressed="true"] {
  background: var(--ink);
  border-color: var(--ink);
  color: var(--bg);
}

main {
  max-width: var(--measure);
  margin: 0 auto;
  padding: 1.4rem 1.25rem 3.5rem;
}

.lede {
  margin: 0.2rem 0 1.4rem;
  color: var(--ink);
}

.table-wrap { overflow-x: auto; }

.catalog {
  width: 100%;
  border-collapse: collapse;
  background: var(--paper);
}

.catalog th,
.catalog td {
  padding: var(--cell);
  text-align: left;
  vertical-align: top;
  border-bottom: 1px solid var(--line);
  white-space: nowrap;
}

.catalog th:last-child,
.catalog td:last-child { white-space: normal; }

.catalog th {
  font-size: 0.78em;
  font-weight: 650;
  letter-spacing: 0.06em;
  color: var(--muted);
}

.catalog tbody tr { cursor: pointer; }

.catalog tbody tr:hover { background: color-mix(in srgb, var(--accent) 8%, var(--paper)); }

.catalog a { font-weight: 650; }

.back { margin: 0.15rem 0 0.4rem; }

.foot,
.note { color: var(--muted); }

.foot { margin: 1rem 0 0; font-size: 0.92em; }

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

.notes h1 {
  margin: 0.2rem 0 var(--gap);
  font-size: var(--h1);
  font-weight: 650;
  line-height: 1.25;
  letter-spacing: 0.02em;
}

.notes h2 {
  margin: 1.6em 0 0.45em;
  font-size: 1.15em;
  line-height: 1.35;
}

.notes h3 {
  margin: 1.25em 0 0.4em;
  font-size: 1.05em;
  line-height: 1.4;
}

.notes h1,
.notes h2,
.notes h3 {
  scroll-margin-top: 4.5rem;
}

.notes p,
.notes ul,
.notes ol,
.notes blockquote,
.notes pre { margin: 0 0 var(--gap); }

.notes ul,
.notes ol { padding-left: 1.3em; }

.notes blockquote {
  margin-left: 0;
  padding: 0.15rem 0 0.15rem 0.9rem;
  border-left: 3px solid var(--accent);
  color: var(--muted);
}

.notes code {
  font-family: "Iowan Old Style", Palatino, "Songti SC", ui-monospace, monospace;
  font-size: 0.92em;
  background: var(--code);
  padding: 0.05em 0.28em;
}

.notes pre {
  overflow-x: auto;
  background: var(--code);
  padding: 0.8rem 0.9rem;
}

.notes pre code { background: transparent; padding: 0; }

.notes table {
  width: 100%;
  border-collapse: collapse;
  margin: 0 0 var(--gap);
  font-size: 0.95em;
}

.notes th,
.notes td {
  padding: var(--cell);
  text-align: left;
  border-bottom: 1px solid var(--line);
}

@media (max-width: 640px) {
  .themes { width: 100%; }
  .themes button { flex: 1 1 auto; }
}


/* pending cells on catalog */
.catalog .pending {
  color: var(--muted, #888);
  font-size: 0.92em;
}


/* reader layout + floating TOC */
.reader {
  max-width: calc(var(--measure) + 17rem);
}

.reader-layout {
  display: grid;
  grid-template-columns: 15rem minmax(0, 1fr);
  gap: 1.5rem 1.75rem;
  align-items: start;
}

.toc-aside {
  position: sticky;
  top: 4.25rem;
  z-index: 1;
  width: 15rem;
  max-height: calc(100vh - 5rem);
  overflow: auto;
  padding: 0.65rem 0.75rem 0.85rem;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 0.5rem;
}

.toc-aside[hidden] {
  display: none !important;
}

.toc-toggle {
  display: none;
  width: 100%;
  margin: 0 0 0.5rem;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--ink);
  font: inherit;
  font-size: 0.88rem;
  font-weight: 650;
  line-height: 1.3;
  padding: 0.4rem 0.65rem;
  border-radius: 0.4rem;
  cursor: pointer;
  text-align: left;
}

.toc {
  font-size: 0.86rem;
  line-height: 1.4;
}

.toc-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.toc-item {
  margin: 0;
}

.toc-item + .toc-item {
  margin-top: 0.28rem;
}

.toc-item a {
  display: block;
  color: var(--muted);
  text-decoration: none;
  border-radius: 0.25rem;
  padding: 0.15rem 0.2rem;
}

.toc-item a:hover {
  color: var(--link);
  background: color-mix(in srgb, var(--accent) 8%, transparent);
}

.toc-item a.is-active {
  color: var(--accent);
  background: color-mix(in srgb, var(--accent) 12%, transparent);
  font-weight: 650;
}

.toc-level-1 a.is-active {
  color: var(--accent);
}

.toc-level-1 a {
  font-weight: 650;
  color: var(--ink);
}

.toc-level-2 {
  padding-left: 0.75rem;
}

.toc-level-3 {
  padding-left: 1.45rem;
  font-size: 0.95em;
}

.reader-layout .notes {
  min-width: 0;
}

@media (max-width: 900px) {
  .reader {
    max-width: var(--measure);
  }

  .reader-layout {
    grid-template-columns: 1fr;
  }

  /* 手机：固定在顶栏下方，下滑半屏展开，上滑收起；大纲内部可滚 */
  .toc-aside {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 5;
    width: auto;
    max-width: none;
    margin: 0;
    padding: 0;
    border: none;
    border-radius: 0;
    background: var(--paper);
    box-shadow: 0 8px 24px color-mix(in srgb, var(--ink) 12%, transparent);
    /* top 由 JS 按顶栏高度写入 --toc-top */
    top: var(--toc-top, 3.5rem);
    max-height: 0;
    overflow: hidden;
    opacity: 0;
    pointer-events: none;
    transition: max-height 0.22s ease, opacity 0.18s ease;
  }

  .toc-aside.toc-open {
    max-height: 50vh;
    opacity: 1;
    pointer-events: auto;
    border-bottom: 1px solid var(--line);
  }

  .toc-aside .toc {
    display: block;
    max-height: 50vh;
    overflow: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    padding: 0.55rem 0.85rem 0.85rem;
  }

  .toc-toggle {
    display: none; /* 改由下滑/上滑控制 */
  }

  .toc-aside:not(.toc-open) .toc {
    display: block; /* 收起靠 max-height，不靠 display */
  }
}


/* Mermaid diagrams in notes */
.notes .mermaid {
  margin: 1.25rem 0;
  overflow-x: auto;
  text-align: center;
  background: transparent;
}
.notes .mermaid svg {
  max-width: 100%;
  height: auto;
}
