/* ============ 主题变量 ============ */
:root {
  --bg: #f7faf7;
  --bg-elevated: #ffffff;
  --bg-subtle: #eef4ee;
  --border: #dde7dd;
  --border-strong: #c3d6c3;
  --text: #1f2a22;
  --text-secondary: #5b6b5f;
  --text-muted: #84948a;
  --accent: #2f9e63;
  --accent-strong: #237a4c;
  --accent-soft: #e3f2e9;
  --accent-border: #bfe3cd;
  --code-bg: #f2f6f2;
  --code-inline-bg: #eef4ee;
  --link: #1f7a4d;
  --hover: #f0f7f1;
  --shadow: 0 1px 3px rgba(31, 60, 40, 0.08);
  --focus-ring: rgba(47, 158, 99, 0.35);
}

[data-theme="dark"] {
  --bg: #101612;
  --bg-elevated: #161e19;
  --bg-subtle: #1c261f;
  --border: #26332a;
  --border-strong: #35463b;
  --text: #dce6de;
  --text-secondary: #9fb0a3;
  --text-muted: #74847a;
  --accent: #4cb783;
  --accent-strong: #6ecf9c;
  --accent-soft: #1c3025;
  --accent-border: #2c4636;
  --code-bg: #141b17;
  --code-inline-bg: #1b251f;
  --link: #6ecf9c;
  --hover: #1c241f;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
  --focus-ring: rgba(76, 183, 131, 0.4);
}

/* ============ 基础 ============ */
* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
}
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", sans-serif;
  font-size: 16px;
  line-height: 1.75;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}
a { color: var(--link); text-decoration: none; }
a:hover { text-decoration: underline; }
img { max-width: 100%; height: auto; display: block; }

/* ============ 顶部栏 ============ */
.topbar {
  position: fixed;
  inset: 0 0 auto 0;
  height: 52px;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 20px;
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
  z-index: 100;
}
.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  font-size: 17px;
  color: var(--text);
  white-space: nowrap;
}
.brand:hover { text-decoration: none; }
.brand-mark { color: var(--accent); }
.topbar-nav { display: flex; gap: 6px; margin-left: 8px; }
.switch-btn {
  padding: 5px 14px;
  border-radius: 999px;
  font-size: 13px;
  color: var(--text-secondary);
  border: 1px solid transparent;
}
.switch-btn:hover { color: var(--accent); background: var(--accent-soft); text-decoration: none; }
.switch-btn[data-active="true"] {
  color: var(--accent-strong);
  background: var(--accent-soft);
  border-color: var(--accent-border);
  font-weight: 600;
}
.topbar-actions { margin-left: auto; display: flex; align-items: center; gap: 4px; }
.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border-radius: 8px;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
}
.icon-btn:hover { background: var(--hover); color: var(--accent); text-decoration: none; }
.icon-moon { display: none; }
[data-theme="dark"] .icon-sun { display: none; }
[data-theme="dark"] .icon-moon { display: block; }

/* ============ 布局 ============ */
.shell {
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr) 260px;
  max-width: 1440px;
  margin: 0 auto;
  padding-top: 52px;
  min-height: calc(100vh - 52px);
}

/* ============ 左侧导航 ============ */
.sidebar {
  border-right: 1px solid var(--border);
  padding: 16px 12px 40px;
  overflow-y: auto;
  position: sticky;
  top: 52px;
  height: calc(100vh - 52px);
  background: var(--bg);
}
.nav-tree { display: flex; flex-direction: column; gap: 2px; }
.nav-group { margin-bottom: 4px; }
.nav-group-header {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 7px 10px;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.04em;
  cursor: pointer;
  border-radius: 8px;
}
.nav-group-header:hover { background: var(--hover); color: var(--accent); }
.nav-group-arrow {
  font-size: 10px;
  transition: transform 0.15s ease;
  color: var(--text-muted);
}
.nav-group[data-collapsed="true"] .nav-group-arrow { transform: rotate(-90deg); }
.nav-group[data-collapsed="true"] .nav-group-links { display: none; }
.nav-group-links { display: flex; flex-direction: column; }
.nav-link {
  display: flex;
  align-items: baseline;
  gap: 8px;
  padding: 6px 10px 6px 16px;
  border-radius: 8px;
  font-size: 14px;
  color: var(--text-secondary);
  line-height: 1.5;
}
.nav-link .nav-num { font-size: 11px; color: var(--text-muted); font-variant-numeric: tabular-nums; }
.nav-link:hover { background: var(--hover); color: var(--accent); text-decoration: none; }
.nav-link[data-current="true"] {
  background: var(--accent-soft);
  color: var(--accent-strong);
  font-weight: 600;
}
.nav-link[data-current="true"] .nav-num { color: var(--accent); }

/* ============ 正文 ============ */
.main {
  min-width: 0;
  padding: 32px 48px 64px;
  position: relative;
}
.breadcrumb {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 20px;
}
.breadcrumb a { color: var(--text-muted); }
.breadcrumb a:hover { color: var(--accent); }
.article { max-width: 820px; margin: 0 auto; }
.article-content h1 {
  font-size: 30px;
  line-height: 1.35;
  margin: 0 0 20px;
  letter-spacing: -0.01em;
}
.article-content h2 {
  font-size: 23px;
  margin: 44px 0 16px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border);
  scroll-margin-top: 70px;
  letter-spacing: -0.01em;
}
.article-content h3 {
  font-size: 18px;
  margin: 32px 0 12px;
  scroll-margin-top: 70px;
}
.article-content h2:first-child { margin-top: 0; }
.article-content p { margin: 14px 0; }
.article-content ul, .article-content ol { margin: 14px 0; padding-left: 26px; }
.article-content li { margin: 6px 0; }
.article-content li::marker { color: var(--accent); }
.article-content a { text-decoration: underline; text-underline-offset: 3px; text-decoration-color: var(--accent-border); }
.article-content a:hover { color: var(--accent-strong); text-decoration-color: var(--accent); }

.article-content strong { color: var(--text); font-weight: 650; }
.article-content blockquote {
  margin: 18px 0;
  padding: 14px 18px;
  border-left: 4px solid var(--accent);
  background: var(--bg-subtle);
  border-radius: 0 10px 10px 0;
  color: var(--text-secondary);
}
.article-content blockquote p { margin: 6px 0; }

.article-content table {
  border-collapse: collapse;
  width: 100%;
  margin: 20px 0;
  font-size: 14px;
  display: block;
  overflow-x: auto;
}
.article-content th, .article-content td {
  border: 1px solid var(--border);
  padding: 9px 14px;
  text-align: left;
  vertical-align: top;
}
.article-content th { background: var(--bg-subtle); font-weight: 650; white-space: nowrap; }
.article-content tr:nth-child(even) td { background: var(--bg-subtle); }
.first-col-nowrap td:first-child { white-space: nowrap; }

.article-content pre {
  position: relative;
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px 18px;
  overflow-x: auto;
  font-size: 13.5px;
  line-height: 1.65;
}
.article-content code {
  font-family: "SF Mono", "JetBrains Mono", Menlo, Consolas, "Courier New", monospace;
  font-size: 0.92em;
}
.article-content p code, .article-content li code, .article-content td code {
  background: var(--code-inline-bg);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 1px 6px;
  font-size: 0.88em;
  color: var(--accent-strong);
}
.article-content pre code {
  background: none;
  border: none;
  padding: 0;
  color: var(--text);
  font-size: inherit;
}
.copy-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  padding: 3px 10px;
  font-size: 12px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text-muted);
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.15s ease;
}
.article-content pre:hover .copy-btn { opacity: 1; }
.copy-btn:hover { color: var(--accent); border-color: var(--accent-border); }

.article-content hr { border: none; border-top: 1px solid var(--border); margin: 32px 0; }
.article-content kbd {
  font-family: "SF Mono", Menlo, Consolas, monospace;
  font-size: 0.85em;
  background: var(--bg-elevated);
  border: 1px solid var(--border-strong);
  border-bottom-width: 2px;
  border-radius: 5px;
  padding: 1px 7px;
  color: var(--text-secondary);
}
.article-content img { margin: 20px auto; border-radius: 10px; }
.article-content img + em, .article-content p em { color: var(--text-muted); font-size: 13px; }

/* ============ 分页 ============ */
.pager {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
  margin-top: 48px;
  padding-top: 24px;
  border-top: 1px solid var(--border);
}
.pager-link {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 14px 16px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--bg-elevated);
  transition: border-color 0.15s ease, background 0.15s ease;
}
.pager-link:hover { border-color: var(--accent-border); background: var(--accent-soft); text-decoration: none; }
.pager-next { text-align: right; align-items: flex-end; }
.pager-dir { font-size: 12px; color: var(--text-muted); }
.pager-title { font-size: 14.5px; color: var(--text); font-weight: 550; line-height: 1.45; }

/* ============ 返回顶部 ============ */
.back-top {
  position: fixed;
  right: 28px;
  bottom: 28px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text-secondary);
  cursor: pointer;
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
  z-index: 90;
}
.back-top[data-visible="true"] { opacity: 1; pointer-events: auto; }
.back-top:hover { color: var(--accent); }

/* ============ 右侧目录 ============ */
.toc {
  border-left: 1px solid var(--border);
  padding: 24px 18px 40px;
  position: sticky;
  top: 52px;
  height: calc(100vh - 52px);
  overflow-y: auto;
  font-size: 13px;
}
.toc-title { font-size: 12px; font-weight: 700; color: var(--text-muted); letter-spacing: 0.04em; margin-bottom: 12px; }
.toc-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 1px; }
.toc-link {
  display: block;
  padding: 4px 10px;
  border-left: 2px solid transparent;
  color: var(--text-secondary);
  line-height: 1.5;
  border-radius: 0 6px 6px 0;
}
.toc-link[data-level="3"] { padding-left: 24px; font-size: 12.5px; }
.toc-link:hover { color: var(--accent); text-decoration: none; background: var(--hover); }
.toc-link[data-active="true"] {
  color: var(--accent-strong);
  border-left-color: var(--accent);
  background: var(--accent-soft);
  font-weight: 600;
}

/* ============ 搜索 ============ */
.search-trigger {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 5px 14px;
  font-size: 13px;
  color: var(--text-muted);
  background: var(--bg-subtle);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 6px;
}
.search-trigger:hover { border-color: var(--accent-border); color: var(--accent); }
.search-overlay {
  position: fixed;
  inset: 0;
  background: rgba(16, 22, 18, 0.55);
  z-index: 200;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding-top: 12vh;
}
.search-overlay[hidden] { display: none; }
.search-box {
  width: min(620px, 92vw);
  background: var(--bg-elevated);
  border-radius: 14px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.25);
  overflow: hidden;
  border: 1px solid var(--border);
}
.search-input-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  color: var(--text-muted);
}
.search-input-row input {
  flex: 1;
  border: none;
  outline: none;
  font-size: 16px;
  background: transparent;
  color: var(--text);
}
.search-results { list-style: none; margin: 0; padding: 6px 0; max-height: 50vh; overflow-y: auto; }
.search-result { display: block; padding: 10px 16px; border-radius: 8px; margin: 2px 8px; }
.search-result:hover, .search-result[data-active="true"] { background: var(--accent-soft); text-decoration: none; }
.search-result-title { font-size: 14px; font-weight: 600; color: var(--text); }
.search-result-meta { font-size: 12px; color: var(--text-muted); margin-top: 2px; }
.search-result-snippet { font-size: 12.5px; color: var(--text-secondary); margin-top: 3px; line-height: 1.5; }
.search-result-snippet mark { background: var(--accent-soft); color: var(--accent-strong); border-radius: 3px; }
.search-status { padding: 14px 16px; color: var(--text-muted); font-size: 13px; border-top: 1px solid var(--border); }

/* ============ 专注模式 ============ */
body[data-focus="true"] .sidebar,
body[data-focus="true"] .toc,
body[data-focus="true"] .pager { display: none; }
body[data-focus="true"] .shell {
  grid-template-columns: minmax(0, 1fr);
  max-width: 860px;
}
body[data-focus="true"] .main { padding: 28px 24px 64px; }

/* ============ 首页 ============ */
.home-main { padding-top: 52px; max-width: 1000px; margin: 0 auto; padding-left: 20px; padding-right: 20px; }
.hero { text-align: center; padding: 64px 16px 40px; }
.hero-title { font-size: 40px; margin: 0 0 14px; letter-spacing: -0.02em; color: var(--text); }
.hero-sub { font-size: 17px; color: var(--text-secondary); margin: 0 0 18px; }
.hero-stats {
  display: inline-block;
  font-size: 13px;
  color: var(--accent-strong);
  background: var(--accent-soft);
  border: 1px solid var(--accent-border);
  border-radius: 999px;
  padding: 5px 16px;
  font-weight: 600;
}
.handbook-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 24px;
  padding: 16px 0 64px;
}
.handbook-card {
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 26px 26px 20px;
  background: var(--bg-elevated);
  transition: border-color 0.15s ease, transform 0.15s ease;
}
.handbook-card:hover { border-color: var(--accent-border); transform: translateY(-2px); text-decoration: none; }
.handbook-card-title { font-size: 22px; font-weight: 700; color: var(--text); margin: 0 0 8px; }
.handbook-card-sub { font-size: 14px; color: var(--text-secondary); margin: 0 0 18px; line-height: 1.6; }
.handbook-card-count { font-size: 12.5px; color: var(--accent-strong); font-weight: 600; margin-bottom: 14px; }
.handbook-card-groups { display: flex; flex-wrap: wrap; gap: 6px; }
.group-chip {
  font-size: 12px;
  color: var(--text-muted);
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 3px 10px;
}
.group-chip .chip-range { color: var(--accent); font-weight: 600; margin-right: 4px; }
.card-cta { margin-top: 20px; font-size: 14px; color: var(--accent); font-weight: 600; }

/* ============ 404 ============ */
.notfound { text-align: center; padding: 120px 20px; }
.notfound h1 { font-size: 72px; margin: 0 0 10px; color: var(--accent); }
.notfound p { color: var(--text-secondary); margin: 0 0 28px; }
.btn-primary {
  display: inline-block;
  background: var(--accent);
  color: #fff;
  padding: 10px 26px;
  border-radius: 999px;
  font-weight: 600;
}
.btn-primary:hover { background: var(--accent-strong); text-decoration: none; }

/* ============ 移动端 ============ */
.menu-btn { display: none; }
@media (max-width: 1080px) {
  .shell { grid-template-columns: 240px minmax(0, 1fr); }
  .toc { display: none; }
}
@media (max-width: 760px) {
  .topbar { padding: 0 14px; gap: 8px; }
  .topbar-nav { display: none; }
  .shell { grid-template-columns: minmax(0, 1fr); padding-top: 52px; }
  .sidebar {
    position: fixed;
    top: 52px;
    left: 0;
    bottom: 0;
    width: min(300px, 84vw);
    z-index: 120;
    background: var(--bg-elevated);
    box-shadow: 4px 0 24px rgba(0, 0, 0, 0.18);
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    border-right: none;
  }
  body[data-nav-open="true"] .sidebar { transform: translateX(0); }
  .nav-scrim {
    position: fixed;
    inset: 52px 0 0 0;
    background: rgba(16, 22, 18, 0.5);
    z-index: 110;
  }
  .menu-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 34px;
    height: 34px;
    border-radius: 8px;
    border: none;
    background: transparent;
    color: var(--text-secondary);
    cursor: pointer;
  }
  .menu-btn:hover { background: var(--hover); color: var(--accent); }
  .main { padding: 20px 16px 48px; }
  .article { max-width: none; }
  .article-content h1 { font-size: 24px; }
  .article-content h2 { font-size: 20px; }
  .pager { grid-template-columns: 1fr; }
  .handbook-grid { grid-template-columns: 1fr; }
  .hero-title { font-size: 30px; }
  .copy-btn { opacity: 1; }
}

/* ============ 无障碍 ============ */
a:focus-visible, button:focus-visible, input:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 6px;
}
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}
