/* Paper Reading Notes — modern reading library
   Light/dark tokens follow the confirmed ink-blue + indigo palette. */

:root {
  color-scheme: light dark;
  --bg: #FBFAF7;
  --bg-soft: #F4F1EA;
  --surface: #FFFFFF;
  --surface-2: #F5F3FF;
  --text: #1E293B;
  --muted: #64748B;
  --faint: #94A3B8;
  --border: #E5E7EB;
  --border-strong: #CBD5E1;
  --primary: #4F46E5;
  --primary-soft: #EEF2FF;
  --accent: #7C3AED;
  --accent-soft: #F5F3FF;
  --success: #0F766E;
  --code-bg: #F8FAFC;
  --code-border: #E2E8F0;
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.04), 0 10px 30px rgba(15, 23, 42, 0.05);
  --shadow-lg: 0 18px 60px rgba(79, 70, 229, 0.14);
  --ring: 0 0 0 3px rgba(79, 70, 229, 0.22);
  --radius: 16px;
  --radius-sm: 10px;
  --content: 760px;
  --toc: 240px;
  --ui: "Noto Sans CJK SC", "PingFang SC", "Microsoft YaHei", -apple-system,
    BlinkMacSystemFont, "Segoe UI", sans-serif;
  --reading: "Noto Serif CJK SC", "Songti SC", "STSong", "SimSun", Georgia,
    "Times New Roman", serif;
  --mono: "Noto Sans Mono CJK SC", ui-monospace, SFMono-Regular, "SF Mono",
    Menlo, Consolas, "Liberation Mono", monospace;
}

:root[data-theme="dark"] {
  --bg: #0F172A;
  --bg-soft: #0B1220;
  --surface: #111827;
  --surface-2: #1E293B;
  --text: #E2E8F0;
  --muted: #94A3B8;
  --faint: #64748B;
  --border: #334155;
  --border-strong: #475569;
  --primary: #818CF8;
  --primary-soft: #1E1B4B;
  --accent: #A78BFA;
  --accent-soft: #2E1065;
  --success: #5EEAD4;
  --code-bg: #0B1220;
  --code-border: #334155;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.2), 0 14px 40px rgba(0, 0, 0, 0.22);
  --shadow-lg: 0 22px 70px rgba(0, 0, 0, 0.4);
  --ring: 0 0 0 3px rgba(129, 140, 248, 0.3);
}

* { box-sizing: border-box; }

html {
  scroll-behavior: smooth;
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: var(--ui);
  font-size: 16px;
  line-height: 1.65;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  z-index: -1;
  pointer-events: none;
  background:
    radial-gradient(900px 520px at 12% -8%, rgba(79, 70, 229, 0.10), transparent 62%),
    radial-gradient(760px 460px at 92% 4%, rgba(124, 58, 237, 0.08), transparent 60%),
    linear-gradient(180deg, transparent 0, var(--bg) 680px);
}

:root[data-theme="dark"] body::before {
  background:
    radial-gradient(900px 520px at 12% -8%, rgba(129, 140, 248, 0.13), transparent 62%),
    radial-gradient(760px 460px at 92% 4%, rgba(167, 139, 250, 0.10), transparent 60%),
    linear-gradient(180deg, transparent 0, var(--bg) 680px);
}

::selection { background: rgba(124, 58, 237, 0.20); }

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; text-underline-offset: 3px; }

button, input, select { font: inherit; }
button { color: inherit; }

:focus-visible {
  outline: none;
  box-shadow: var(--ring);
  border-radius: 6px;
}

[hidden] { display: none !important; }

.skip-link {
  position: fixed;
  top: -60px;
  left: 16px;
  z-index: 100;
  padding: 10px 14px;
  border-radius: 10px;
  background: var(--primary);
  color: #fff;
  transition: top 0.15s ease;
}
.skip-link:focus { top: 12px; }

/* ---------- top bar ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  gap: 18px;
  min-height: 64px;
  padding: 10px clamp(16px, 4vw, 40px);
  border-bottom: 1px solid color-mix(in srgb, var(--border) 82%, transparent);
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: blur(16px) saturate(1.3);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: var(--text);
  font-weight: 700;
  font-size: 14px;
  letter-spacing: 0.01em;
  white-space: nowrap;
}
.brand:hover { text-decoration: none; color: var(--primary); }
.brand-mark {
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border-radius: 9px;
  color: #fff;
  font-family: var(--reading);
  font-size: 17px;
  font-weight: 700;
  background: linear-gradient(135deg, var(--primary), var(--accent));
  box-shadow: 0 6px 18px rgba(79, 70, 229, 0.28);
}
.brand-text em { font-style: normal; color: var(--primary); }

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

.topbar-link,
.theme-toggle,
.nav-action {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 36px;
  padding: 6px 10px;
  border: 1px solid transparent;
  border-radius: 10px;
  color: var(--muted);
  background: transparent;
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
}
.topbar-link:hover,
.theme-toggle:hover,
.nav-action:hover {
  color: var(--primary);
  background: var(--primary-soft);
  border-color: color-mix(in srgb, var(--primary) 18%, transparent);
  text-decoration: none;
}
.theme-icon { font-size: 15px; line-height: 1; }
.nav-action.pdf-action { color: var(--primary); border-color: var(--border); }

/* ---------- home hero ---------- */

main { width: 100%; }

.hero {
  position: relative;
  max-width: 1180px;
  margin: 0 auto;
  padding: clamp(52px, 9vw, 104px) clamp(20px, 5vw, 56px) 42px;
  overflow: hidden;
}
.hero-glow {
  position: absolute;
  top: -220px;
  right: -120px;
  width: 520px;
  height: 520px;
  border-radius: 50%;
  pointer-events: none;
  background: radial-gradient(circle, rgba(124, 58, 237, 0.16), transparent 66%);
  filter: blur(10px);
}
.eyebrow {
  margin: 0 0 14px;
  color: var(--primary);
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.18em;
}
.hero h1 {
  max-width: 820px;
  margin: 0;
  font-family: var(--reading);
  font-size: clamp(2.5rem, 6.2vw, 4.5rem);
  line-height: 1.08;
  letter-spacing: -0.035em;
  font-weight: 700;
}
.hero-lede {
  max-width: 640px;
  margin: 22px 0 0;
  color: var(--muted);
  font-size: clamp(1rem, 1.5vw, 1.14rem);
  line-height: 1.8;
}
.hero-stats {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 24px;
  margin-top: 28px;
  color: var(--muted);
  font-size: 13px;
}
.hero-stats span { display: inline-flex; align-items: baseline; gap: 6px; }
.hero-stats strong {
  color: var(--text);
  font-family: var(--mono);
  font-size: 18px;
  font-weight: 700;
}

.hero-tools {
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
  margin-top: 32px;
}
.search-box {
  position: relative;
  display: flex;
  align-items: center;
  flex: 1 1 340px;
  max-width: 520px;
  min-height: 48px;
  padding: 0 12px 0 42px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  box-shadow: var(--shadow);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.search-box:focus-within {
  border-color: var(--primary);
  box-shadow: var(--shadow), var(--ring);
}
.search-icon {
  position: absolute;
  left: 15px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--faint);
  display: inline-flex;
}
.search-box input {
  width: 100%;
  min-width: 0;
  padding: 11px 0;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--text);
  font-size: 14px;
}
.search-box input::placeholder { color: var(--faint); }
.search-box input::-webkit-search-cancel-button { filter: grayscale(1); }
.search-kbd {
  display: inline-grid;
  place-items: center;
  min-width: 24px;
  height: 24px;
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 6px;
  color: var(--faint);
  background: var(--bg-soft);
  font-family: var(--mono);
  font-size: 11px;
}
.index-link {
  color: var(--muted);
  font-size: 13px;
  white-space: nowrap;
}
.index-link:hover { color: var(--primary); }

/* ---------- library ---------- */

.library {
  max-width: 1180px;
  margin: 0 auto;
  padding: 18px clamp(20px, 5vw, 56px) 84px;
}
.library-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  padding: 12px 0 22px;
  border-bottom: 1px solid var(--border);
}
.filters { display: flex; flex-wrap: wrap; gap: 6px; }
.filter {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-height: 36px;
  padding: 6px 12px;
  border: 1px solid transparent;
  border-radius: 999px;
  color: var(--muted);
  background: transparent;
  font-size: 13px;
  cursor: pointer;
  transition: 0.15s ease;
}
.filter:hover { color: var(--primary); background: var(--primary-soft); }
.filter.is-active {
  color: var(--primary);
  background: var(--primary-soft);
  border-color: color-mix(in srgb, var(--primary) 22%, transparent);
  font-weight: 700;
}
.filter-count {
  min-width: 20px;
  padding: 1px 6px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--primary) 10%, transparent);
  font-family: var(--mono);
  font-size: 11px;
}
.sort-control {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 12px;
}
.sort-control select {
  min-height: 34px;
  padding: 4px 30px 4px 10px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--surface);
  color: var(--text);
  font-size: 13px;
  cursor: pointer;
}

/* ---------- featured summary ---------- */

.featured { display: grid; gap: 16px; margin: 24px 0 0; }
.featured-card {
  position: relative;
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 28px;
  padding: clamp(22px, 3.4vw, 34px);
  border: 1px solid transparent;
  border-radius: calc(var(--radius) + 4px);
  background:
    linear-gradient(var(--surface), var(--surface)) padding-box,
    linear-gradient(120deg, var(--primary), var(--accent)) border-box;
  box-shadow: var(--shadow-lg);
  overflow: hidden;
}
.featured-card::after {
  content: "";
  position: absolute;
  right: -90px;
  bottom: -140px;
  width: 320px;
  height: 320px;
  border-radius: 50%;
  pointer-events: none;
  background: radial-gradient(circle, rgba(124, 58, 237, 0.14), transparent 68%);
}
.featured-main { position: relative; z-index: 1; max-width: 780px; }
.featured-main h2 {
  margin: 14px 0 12px;
  font-family: var(--reading);
  font-size: clamp(1.45rem, 2.8vw, 2.1rem);
  line-height: 1.3;
  letter-spacing: -0.02em;
}
.featured-main h2 a { color: var(--text); }
.featured-main h2 a:hover { color: var(--primary); text-decoration: none; }
.featured-main > p {
  margin: 0 0 16px;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.8;
}
.featured-actions {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 8px;
  min-width: 132px;
}

/* ---------- result line + cards ---------- */

.result-line {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 14px;
  margin: 30px 0 14px;
  color: var(--muted);
  font-size: 12px;
}
#result-count { color: var(--text); font-weight: 700; }
.result-hint { color: var(--faint); }

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 16px;
}
.card {
  min-width: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
  transition: transform 0.18s ease, border-color 0.18s ease, box-shadow 0.18s ease;
}
.card:hover {
  transform: translateY(-3px);
  border-color: color-mix(in srgb, var(--primary) 32%, var(--border));
  box-shadow: var(--shadow-lg);
}
.card-link {
  position: relative;
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 236px;
  padding: 20px;
  color: inherit;
}
.card-link:hover { text-decoration: none; }
.card-top {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 22px;
}
.card-id {
  color: var(--primary);
  font-family: var(--mono);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.04em;
}
.card-kind {
  padding: 2px 7px;
  border-radius: 999px;
  background: var(--primary-soft);
  color: var(--primary);
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.02em;
}
.card h3 {
  margin: 12px 0 8px;
  font-family: var(--reading);
  font-size: 19px;
  line-height: 1.45;
  letter-spacing: -0.01em;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.card-paper {
  margin: 0 0 8px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.55;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.card-excerpt {
  margin: 0 0 16px;
  color: var(--muted);
  font-size: 12.5px;
  line-height: 1.7;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.card-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 5px 10px;
  margin-top: auto;
  color: var(--faint);
  font-size: 11px;
}
.card-meta span + span::before {
  content: "·";
  margin-right: 8px;
  color: var(--border-strong);
}
.card-arrow {
  position: absolute;
  right: 18px;
  bottom: 16px;
  color: var(--primary);
  font-size: 17px;
  opacity: 0;
  transform: translateX(-4px);
  transition: 0.18s ease;
}
.card:hover .card-arrow { opacity: 1; transform: translateX(0); }

.empty-state {
  margin: 20px 0;
  padding: 48px 24px;
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius);
  text-align: center;
  color: var(--muted);
}
.empty-title { margin: 0 0 6px; color: var(--text); font-weight: 700; }
.empty-state p { margin: 0 0 16px; font-size: 13px; }

/* ---------- buttons ---------- */

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  min-height: 38px;
  padding: 8px 15px;
  border: 1px solid var(--primary);
  border-radius: 10px;
  background: var(--primary);
  color: #fff;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
  white-space: nowrap;
}
.button:hover { background: color-mix(in srgb, var(--primary) 88%, black); text-decoration: none; }
.button-ghost {
  background: var(--surface);
  color: var(--primary);
  border-color: var(--border);
}
.button-ghost:hover { background: var(--primary-soft); color: var(--primary); }

/* ---------- footer ---------- */

.site-footer {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 28px;
  max-width: 1180px;
  margin: 0 auto;
  padding: 28px clamp(20px, 5vw, 56px) 48px;
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 12px;
}
.site-footer strong { color: var(--text); }
.site-footer p { margin: 6px 0 0; }
.footer-links { display: flex; gap: 16px; white-space: nowrap; }
.nojs {
  max-width: 1180px;
  margin: 0 auto;
  padding: 12px clamp(20px, 5vw, 56px) 0;
  color: var(--muted);
  font-size: 12px;
}

/* ---------- document page ---------- */

.reading-progress {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 50;
  width: 0;
  height: 3px;
  background: linear-gradient(90deg, var(--primary), var(--accent));
  box-shadow: 0 0 12px rgba(124, 58, 237, 0.5);
  transition: width 0.08s linear;
}
.topbar-doc { gap: 16px; }
.doc-breadcrumb {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--faint);
  font-size: 12px;
}
.doc-breadcrumb a { color: var(--muted); }
.doc-breadcrumb span[aria-hidden] { color: var(--border-strong); }

.doc-layout {
  display: grid;
  grid-template-columns: minmax(0, var(--content)) var(--toc);
  justify-content: center;
  gap: clamp(28px, 4vw, 56px);
  max-width: 1120px;
  margin: 0 auto;
  padding: 46px clamp(20px, 5vw, 56px) 84px;
}
.doc { min-width: 0; }
.doc-header {
  margin-bottom: 30px;
  padding-bottom: 26px;
  border-bottom: 1px solid var(--border);
}
.doc-kicker {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px 12px;
  color: var(--faint);
  font-size: 12px;
}
.doc-id { font-family: var(--mono); color: var(--primary); font-weight: 700; }
.kind-badge {
  padding: 3px 8px;
  border-radius: 999px;
  background: var(--primary-soft);
  color: var(--primary);
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.03em;
}
.kind-blog { background: var(--accent-soft); color: var(--accent); }
.kind-summary { background: var(--primary-soft); color: var(--primary); }
.doc-header h1 {
  margin: 16px 0 10px;
  font-family: var(--reading);
  font-size: clamp(1.85rem, 4vw, 2.7rem);
  line-height: 1.3;
  letter-spacing: -0.025em;
}
.doc-paper-title {
  margin: 0 0 14px;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.65;
}
.doc-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 12px;
  margin-top: 16px;
  color: var(--muted);
  font-size: 12px;
}
.doc-meta span + span::before {
  content: "·";
  margin-right: 10px;
  color: var(--border-strong);
}
.doc-links {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  margin-top: 16px;
  font-size: 12px;
}
.doc-link { color: var(--muted); }
.doc-link:hover { color: var(--primary); }

/* ---------- prose ---------- */

.prose {
  font-family: var(--reading);
  font-size: 17px;
  line-height: 1.95;
  color: var(--text);
  overflow-wrap: break-word;
}
.prose > :first-child { margin-top: 0; }
.prose p { margin: 0 0 1.15em; }
.prose h2,
.prose h3,
.prose h4 {
  font-family: var(--ui);
  line-height: 1.4;
  letter-spacing: -0.015em;
  scroll-margin-top: 86px;
}
.prose h2 {
  position: relative;
  margin: 2.2em 0 0.85em;
  padding-left: 14px;
  font-size: 1.42em;
  font-weight: 800;
}
.prose h2::before {
  content: "";
  position: absolute;
  left: 0;
  top: 0.22em;
  bottom: 0.18em;
  width: 4px;
  border-radius: 99px;
  background: linear-gradient(180deg, var(--primary), var(--accent));
}
.prose h3 {
  margin: 1.7em 0 0.6em;
  font-size: 1.13em;
  font-weight: 700;
}
.prose h4 {
  margin: 1.4em 0 0.5em;
  font-size: 1em;
  font-weight: 700;
  color: var(--muted);
}
.prose a { color: var(--primary); text-decoration: underline; text-decoration-thickness: 1px; text-underline-offset: 3px; }
.prose a:hover { color: var(--accent); }
.prose strong { font-weight: 700; color: var(--text); }
.prose ul,
.prose ol { margin: 0 0 1.15em; padding-left: 1.55em; }
.prose li { margin: 0.28em 0; }
.prose li > p { margin: 0.3em 0; }
.prose blockquote {
  margin: 1.5em 0;
  padding: 0.35em 0 0.35em 1.1em;
  border-left: 3px solid var(--primary);
  color: var(--muted);
  background: linear-gradient(90deg, var(--primary-soft), transparent 70%);
  border-radius: 0 8px 8px 0;
}
.prose blockquote p:last-child { margin-bottom: 0; }
.prose hr {
  margin: 2.4em 0;
  border: 0;
  border-top: 1px solid var(--border);
}
.prose code {
  padding: 0.12em 0.36em;
  border: 1px solid var(--code-border);
  border-radius: 5px;
  background: var(--code-bg);
  font-family: var(--mono);
  font-size: 0.82em;
}
.prose pre {
  position: relative;
  margin: 1.4em 0;
  padding: 16px 18px;
  overflow: auto;
  border: 1px solid var(--code-border);
  border-radius: 12px;
  background: var(--code-bg);
  font-family: var(--mono);
  font-size: 13px;
  line-height: 1.65;
}
.prose pre code {
  padding: 0;
  border: 0;
  background: transparent;
  font-size: inherit;
  white-space: pre;
}
.code-copy {
  position: absolute;
  top: 8px;
  right: 8px;
  padding: 4px 8px;
  border: 1px solid var(--code-border);
  border-radius: 7px;
  background: var(--surface);
  color: var(--muted);
  font-family: var(--ui);
  font-size: 11px;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.15s ease;
}
.prose pre:hover .code-copy { opacity: 1; }
.code-copy:hover { color: var(--primary); border-color: var(--primary); }

.table-wrap {
  margin: 1.4em 0;
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: 12px;
}
.prose table {
  width: 100%;
  border-collapse: collapse;
  font-family: var(--ui);
  font-size: 13px;
  line-height: 1.6;
}
.prose th,
.prose td {
  padding: 9px 11px;
  border-bottom: 1px solid var(--border);
  border-right: 1px solid var(--border);
  text-align: left;
  vertical-align: top;
}
.prose th:last-child,
.prose td:last-child { border-right: 0; }
.prose tr:last-child td { border-bottom: 0; }
.prose th {
  position: sticky;
  top: 0;
  background: var(--surface-2);
  color: var(--text);
  font-weight: 700;
  white-space: nowrap;
}
.prose tbody tr:nth-child(even) td { background: color-mix(in srgb, var(--surface) 70%, var(--bg-soft)); }

.prose img {
  display: block;
  max-width: 100%;
  height: auto;
  margin: 0 auto;
  border-radius: 10px;
}
.prose figure {
  margin: 1.8em 0;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
}
.prose figure img { cursor: zoom-in; }
.prose figcaption {
  margin-top: 9px;
  color: var(--muted);
  font-family: var(--ui);
  font-size: 12px;
  line-height: 1.6;
  text-align: center;
}

.math-display {
  margin: 1.5em 0;
  padding: 6px 0;
  overflow-x: auto;
  text-align: center;
}
mjx-container[jax="SVG"][display="true"] {
  max-width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
}
mjx-container[jax="SVG"][display="true"] > svg { max-width: 100%; height: auto; }
.mermaid {
  position: relative;
  margin: 1.7em 0;
  overflow-x: auto;
  text-align: center;
}
.mermaid svg { max-width: 100%; height: auto; }
.mermaid > svg { cursor: zoom-in; }
/* Firefox clips Mermaid HTML-label glyphs at the foreignObject box; let the ink paint out. */
.mermaid foreignObject,
.diagram-stage foreignObject { overflow: visible; }
.mermaid-zoom {
  display: flex;
  width: max-content;
  align-items: center;
  justify-content: center;
  min-height: 26px;
  margin: 0 0 6px auto;
  padding: 2px 9px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  color: var(--muted);
  font-family: var(--ui);
  font-size: 12px;
  font-weight: 700;
  line-height: 1.4;
  cursor: zoom-in;
  transition: color 0.15s ease, border-color 0.15s ease;
}
.mermaid-zoom:hover {
  border-color: var(--primary);
  color: var(--primary);
}

.loading {
  padding: 28px 0;
  color: var(--muted);
  font-family: var(--ui);
  font-size: 14px;
}
.render-error {
  padding: 14px 16px;
  border: 1px solid #D4A72C;
  border-radius: 10px;
  background: #FFF8C5;
  color: #7A4F00;
  font-family: var(--ui);
  font-size: 13px;
  white-space: pre-wrap;
}
:root[data-theme="dark"] .render-error { background: #3B2F0B; color: #FDE68A; border-color: #A16207; }
.noscript-note {
  margin: 18px 0;
  padding: 14px 16px;
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--muted);
  font-family: var(--ui);
  font-size: 13px;
}

/* ---------- TOC ---------- */

.toc {
  position: sticky;
  top: 88px;
  align-self: start;
  max-height: calc(100vh - 112px);
  overflow: auto;
  padding: 2px 0 16px 16px;
  border-left: 1px solid var(--border);
  scrollbar-width: thin;
}
.toc-title {
  margin: 0 0 10px;
  color: var(--text);
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.12em;
}
.toc nav { display: flex; flex-direction: column; gap: 3px; }
.toc a {
  display: block;
  padding: 4px 8px;
  border-left: 2px solid transparent;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.5;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.toc a:hover { color: var(--primary); text-decoration: none; }
.toc a.is-active {
  border-left-color: var(--primary);
  background: var(--primary-soft);
  color: var(--primary);
  font-weight: 700;
}
.mobile-toc {
  display: none;
  margin: 0 20px 40px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
}
.mobile-toc summary {
  padding: 12px 14px;
  cursor: pointer;
  color: var(--text);
  font-size: 13px;
  font-weight: 700;
}
.mobile-toc nav { display: flex; flex-direction: column; padding: 0 14px 12px; }
.mobile-toc a { padding: 5px 0; color: var(--muted); font-size: 13px; }

/* ---------- doc footer / nav ---------- */

.page-navs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-top: 44px;
  padding-top: 24px;
  border-top: 1px solid var(--border);
}
.page-nav {
  display: flex;
  flex-direction: column;
  gap: 5px;
  padding: 14px 16px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  font-size: 13px;
}
.page-nav:hover { border-color: var(--primary); text-decoration: none; }
.page-nav span { color: var(--faint); font-size: 11px; }
.page-nav strong { font-weight: 600; line-height: 1.5; }
.page-nav.next { text-align: right; }
.page-nav.next strong { margin-left: auto; }
.doc-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-top: 26px;
  color: var(--faint);
  font-size: 11px;
}
.doc-footer a { color: var(--muted); }

.to-top {
  position: fixed;
  right: 22px;
  bottom: 22px;
  z-index: 20;
  width: 40px;
  height: 40px;
  border: 1px solid var(--border);
  border-radius: 50%;
  background: var(--surface);
  color: var(--primary);
  font-size: 17px;
  cursor: pointer;
  box-shadow: var(--shadow);
}
.to-top:hover { border-color: var(--primary); }

/* ---------- lightbox ---------- */

.lightbox {
  width: min(92vw, 1100px);
  max-height: 92vh;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  box-shadow: var(--shadow-lg);
}
.lightbox::backdrop { background: rgba(2, 6, 23, 0.82); backdrop-filter: blur(4px); }
.lightbox img {
  display: block;
  max-width: 100%;
  max-height: 80vh;
  margin: 0 auto;
  object-fit: contain;
}
.lightbox p {
  margin: 0;
  padding: 12px 18px 16px;
  color: var(--muted);
  font-family: var(--ui);
  font-size: 12px;
  text-align: center;
}
.lightbox-close {
  position: absolute;
  top: 8px;
  right: 8px;
  z-index: 2;
  width: 34px;
  height: 34px;
  border: 0;
  border-radius: 50%;
  background: rgba(15, 23, 42, 0.62);
  color: #fff;
  font-size: 20px;
  cursor: pointer;
}

.lightbox-diagram {
  width: min(96vw, 1400px);
  height: min(88vh, 900px);
  height: min(88dvh, 900px);
  max-height: 90vh;
  overflow: hidden;
}
.lightbox-diagram[open] {
  display: flex;
  flex-direction: column;
}
.diagram-toolbar {
  display: flex;
  flex: none;
  align-items: center;
  justify-content: flex-end;
  gap: 4px;
  padding: 8px 50px 8px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--surface-2);
}
.diagram-toolbar button {
  min-width: 34px;
  height: 30px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font-family: var(--ui);
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
}
.diagram-toolbar button:hover:not(:disabled) {
  border-color: var(--primary);
  color: var(--primary);
}
.diagram-toolbar button:disabled { opacity: 0.45; cursor: default; }
[data-diagram-percent] { font-variant-numeric: tabular-nums; }
.diagram-stage {
  display: flex;
  flex: 1 1 auto;
  min-height: 0;
  overflow: auto;
  padding: 16px;
  background: var(--bg);
  overscroll-behavior: contain;
}
.diagram-stage > svg { flex: none; margin: auto; }
.diagram-stage:focus-visible {
  border-radius: 0;
  outline: 2px solid var(--primary);
  outline-offset: -2px;
  box-shadow: none;
}
.diagram-caption {
  flex: none;
  margin: 0;
  padding: 9px 16px 12px;
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-family: var(--ui);
  font-size: 12px;
  text-align: center;
}

/* ---------- responsive ---------- */

@media (max-width: 1080px) {
  .doc-layout { grid-template-columns: minmax(0, var(--content)); }
  .toc { display: none; }
  .mobile-toc { display: block; }
}

@media (max-width: 760px) {
  .topbar { min-height: 58px; gap: 10px; }
  .brand-text { display: none; }
  .doc-breadcrumb { display: none; }
  .topbar-link span { display: none; }
  .search-jump { display: inline-flex; }
  .hero { padding-top: 48px; }
  .hero h1 { font-size: clamp(2.2rem, 11vw, 3.2rem); }
  .hero-tools { align-items: stretch; }
  .index-link { padding-top: 4px; }
  .featured-card { flex-direction: column; align-items: stretch; }
  .featured-actions { flex-direction: row; justify-content: flex-end; }
  .cards { grid-template-columns: 1fr; }
  .card-link { min-height: 0; }
  .site-footer { flex-direction: column; }
  .doc-layout { padding-top: 30px; }
  .doc-header h1 { font-size: clamp(1.65rem, 8vw, 2.2rem); }
  .prose { font-size: 16px; line-height: 1.9; }
  .prose h2 { font-size: 1.3em; }
  .page-navs { grid-template-columns: 1fr; }
  .page-nav.next { text-align: left; }
  .page-nav.next strong { margin-left: 0; }
  .doc-footer { flex-direction: column; align-items: flex-start; }
}

@media (max-width: 520px) {
  .topbar { padding: 9px 14px; }
  .topbar-actions { gap: 2px; }
  .theme-label { display: none; }
  .search-kbd { display: none; }
  .search-box { flex-basis: 100%; }
  .library-toolbar { align-items: flex-start; }
  .sort-control { width: 100%; justify-content: flex-end; }
  .filter { min-height: 34px; padding: 5px 9px; }
  .doc-layout { padding-left: 16px; padding-right: 16px; }
  .mobile-toc { margin-left: 16px; margin-right: 16px; }
}

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

/* ---------- print ---------- */

@media print {
  :root,
  :root[data-theme="dark"] {
    color-scheme: light;
    --bg: #FFFFFF;
    --bg-soft: #FFFFFF;
    --surface: #FFFFFF;
    --surface-2: #F8FAFC;
    --text: #111827;
    --muted: #4B5563;
    --faint: #6B7280;
    --border: #D1D5DB;
    --border-strong: #9CA3AF;
    --primary: #1F2937;
    --primary-soft: #F3F4F6;
    --accent: #374151;
    --accent-soft: #F3F4F6;
    --code-bg: #F8FAFC;
    --code-border: #D1D5DB;
    --shadow: none;
    --shadow-lg: none;
  }

  @page { size: A4; margin: 16mm 14mm 16mm 14mm; }

  html, body { background: #FFFFFF !important; }
  body { font-size: 10.5pt; line-height: 1.7; }
  body::before,
  .topbar,
  .reading-progress,
  .toc,
  .mobile-toc,
  .to-top,
  .page-navs,
  .doc-footer,
  .lightbox,
  .skip-link,
  .hero-glow,
  .site-footer,
  .nojs { display: none !important; }

  .doc-layout {
    display: block;
    max-width: none;
    margin: 0;
    padding: 0;
  }
  .doc { max-width: none; }
  .doc-header {
    margin-bottom: 18pt;
    padding-bottom: 12pt;
    border-bottom: 1pt solid #D1D5DB;
  }
  .doc-header h1 { font-size: 22pt; line-height: 1.25; }
  .doc-paper-title { font-size: 10pt; }
  .doc-meta, .doc-links { font-size: 9pt; }
  .doc-link { color: #4B5563; }

  .prose {
    max-width: none;
    font-size: 10.5pt;
    line-height: 1.75;
    color: #111827;
  }
  .prose h2, .prose h3, .prose h4 { break-after: avoid; }
  .prose h2 { font-size: 15pt; }
  .prose h3 { font-size: 12pt; }
  .prose a { color: inherit; text-decoration: none; }
  .prose pre {
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    overflow: visible;
  }
  .prose pre code { white-space: pre-wrap; }
  .code-copy { display: none !important; }
  .mermaid-zoom { display: none !important; }

  .table-wrap {
    overflow: visible;
    border: 1pt solid #D1D5DB;
    break-inside: auto;
  }
  .prose table {
    display: table;
    width: 100%;
    font-size: 9pt;
  }
  .prose thead { display: table-header-group; }
  .prose th { position: static; background: #F3F4F6 !important; }
  .prose tr, .prose img, .math-display, .mermaid { break-inside: avoid; }
  .prose td, .prose th { padding: 4pt 5pt; }

  .math-display { overflow: visible; }
  mjx-container[jax="SVG"][display="true"] { overflow: visible; }
  mjx-container[jax="SVG"][display="true"] > svg { max-width: 100%; height: auto; }
  .mermaid { overflow: visible; }
  .mermaid svg {
    max-width: 100% !important;
    max-height: 250mm !important;
    width: auto !important;
    height: auto !important;
  }
  .prose img { max-width: 100% !important; height: auto !important; }
  .prose figure { break-inside: avoid; border-color: #D1D5DB; }
  .render-error { display: block; }
}
