:root {
  --bg: #ffffff;
  --bg-soft: #f7f8fa;
  --bg-elev: #ffffff;
  --text: #1f2328;
  --text-soft: #57606a;
  --border: #e5e7eb;
  --accent: #2563eb;
  --accent-soft: #eff4ff;
  --code-bg: #f6f8fa;
  --shadow: 0 1px 3px rgba(0,0,0,.08);
  --scrollbar: rgba(31,35,40,.2);
  --scrollbar-hover: rgba(31,35,40,.35);
  --sidebar-w: 290px;
  --toc-w: 240px;
  --header-h: 60px;
  --radius: 8px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  --mono: SFMono-Regular, Consolas, "Liberation Mono", Menlo, monospace;
}

[data-theme="dark"] {
  --bg: #0d1117;
  --bg-soft: #161b22;
  --bg-elev: #161b22;
  --text: #e6edf3;
  --text-soft: #9198a1;
  --border: #30363d;
  --accent: #4493f8;
  --accent-soft: #15233b;
  --code-bg: #161b22;
  --shadow: 0 1px 3px rgba(0,0,0,.4);
  --scrollbar: rgba(230,237,243,.22);
  --scrollbar-hover: rgba(230,237,243,.4);
}

* { box-sizing: border-box; }

html { scroll-behavior: smooth; }
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  * { scroll-behavior: auto !important; }
}
html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.7;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ---- 顶栏 ---- */
.header {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: var(--header-h);
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 20px;
  background: var(--bg-elev);
  border-bottom: 1px solid var(--border);
  z-index: 1000;
}
.header .brand {
  font-weight: 700;
  font-size: 1.05rem;
  color: var(--text);
  white-space: nowrap;
}
.header .brand small {
  font-weight: 400;
  color: var(--text-soft);
  font-size: .8rem;
  margin-left: 6px;
}
.header .spacer { flex: 1; }

.search-box {
  position: relative;
  width: 280px;
  max-width: 40vw;
}
.search-box input {
  width: 100%;
  height: 36px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-soft);
  color: var(--text);
  font-size: .9rem;
  outline: none;
}
.search-box input:focus { border-color: var(--accent); }
#search-results {
  position: absolute;
  top: 42px; left: 0; right: 0;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  max-height: 60vh;
  overflow-y: auto;
  display: none;
  z-index: 1100;
}
.search-hit {
  display: block;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  color: var(--text);
}
.search-hit:last-child { border-bottom: none; }
.search-hit:hover { background: var(--accent-soft); text-decoration: none; }
.search-hit-title { display: block; font-weight: 600; font-size: .9rem; }
.search-hit-desc { display: block; color: var(--text-soft); font-size: .8rem; }

.btn-icon {
  height: 36px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-soft);
  color: var(--text);
  cursor: pointer;
  font-size: .85rem;
  white-space: nowrap;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.btn-icon:hover { border-color: var(--accent); }

.menu-toggle { display: none; }

/* ---- 布局 ---- */
.layout {
  display: flex;
  margin-top: var(--header-h);
  align-items: flex-start;
}

/* ---- 侧栏 ---- */
.sidebar {
  position: sticky;
  top: var(--header-h);
  width: var(--sidebar-w);
  flex: 0 0 var(--sidebar-w);
  height: calc(100vh - var(--header-h));
  overflow-y: auto;
  border-right: 1px solid var(--border);
  background: var(--bg-soft);
  padding: 16px 12px;
}
.sidebar ul { list-style: none; margin: 0; padding: 0; }
.sidebar > ul > li { margin: 2px 0; }

/* 分组：统一可折叠；展开标识符（箭头 chevron）固定在标题最右侧 */
.nav-group > summary {
  display: flex;
  align-items: center;
  cursor: pointer;
  font-weight: 600;
  padding: 6px 8px;
  border-radius: 6px;
  color: var(--text);
  user-select: none;
  list-style: none;                 /* Firefox：去掉默认左侧三角 */
}
.nav-group > summary::-webkit-details-marker { display: none; }   /* Safari/Chrome：去掉左侧默认三角 */
.nav-group > summary:hover { background: var(--accent-soft); }

/* 标题文字 / 链接：占满左侧空间，箭头被推到最右 */
.nav-group-label,
.nav-group-link {
  flex: 1 1 auto;
  min-width: 0;
  color: inherit;
  text-decoration: none;
}
.nav-group-link.active { color: var(--accent); font-weight: 600; }

/* 右侧箭头：折叠时朝右 ›，展开时朝下 ⌄ */
.nav-group > summary::after {
  content: "";
  flex: 0 0 auto;
  margin-left: auto;
  width: 7px;
  height: 7px;
  border-right: 2px solid currentColor;
  border-bottom: 2px solid currentColor;
  opacity: .55;
  transform: rotate(-45deg);        /* 朝右 */
  transition: transform .15s ease;
}
.nav-group[open] > summary::after { transform: rotate(45deg); }    /* 朝下 */
.nav-group-list { margin: 2px 0 6px; padding-left: 12px !important; border-left: 1px solid var(--border); }

.nav-link {
  display: block;
  padding: 6px 10px;
  border-radius: 6px;
  color: var(--text-soft);
  font-size: .92rem;
}
.nav-link:hover { background: var(--accent-soft); color: var(--text); text-decoration: none; }
.nav-link.active { background: var(--accent); color: #fff; font-weight: 600; }
.nav-sublist { padding-left: 10px !important; border-left: 1px solid var(--border); margin: 2px 0; }

/* 侧栏 / TOC 滚动条（6px 细条、透明轨道、滑块=前景色 20% 透明、圆角 3px、hover 加深） */
.sidebar, .toc-wrap {
  scrollbar-width: thin;
  scrollbar-color: var(--scrollbar) transparent;
}
.sidebar::-webkit-scrollbar, .toc-wrap::-webkit-scrollbar { width: 6px; height: 6px; }
.sidebar::-webkit-scrollbar-track, .toc-wrap::-webkit-scrollbar-track { background: transparent; }
.sidebar::-webkit-scrollbar-thumb, .toc-wrap::-webkit-scrollbar-thumb {
  background: var(--scrollbar);
  border-radius: 3px;
}
.sidebar::-webkit-scrollbar-thumb:hover, .toc-wrap::-webkit-scrollbar-thumb:hover {
  background: var(--scrollbar-hover);
}

/* ---- 主内容 ---- */
.main {
  flex: 1 1 auto;
  min-width: 0;
  padding: 32px 40px 80px;
}
#content {
  max-width: 760px;
  margin: 0 auto;
}
#content > :first-child { margin-top: 0; }

/* 标题层级 + 锚点跳转不被固定顶栏遮挡 */
#content h1, #content h2, #content h3, #content h4 {
  line-height: 1.3;
  scroll-margin-top: calc(var(--header-h) + 16px);
}
#content h1 { font-size: 2rem; font-weight: 700; margin: 0 0 1.2rem; padding-bottom: .4em; border-bottom: 1px solid var(--border); }
#content h2 { font-size: 1.5rem; font-weight: 650; margin: 2.4rem 0 1rem; padding-bottom: .35em; border-bottom: 1px solid var(--border); }
#content h3 { font-size: 1.2rem; font-weight: 600; margin: 1.8rem 0 .8rem; }
#content h4 { font-size: 1.02rem; font-weight: 600; margin: 1.4rem 0 .6rem; }

#content p { margin: 1rem 0; line-height: 1.8; }
#content ul, #content ol { padding-left: 1.6em; margin: 1rem 0; line-height: 1.8; }
#content li { margin: .35rem 0; }
#content a { color: var(--accent); text-decoration: none; border-bottom: 1px solid transparent; }
#content a:hover { text-decoration: none; border-bottom-color: currentColor; }

#content code {
  padding: .15em .4em;
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 5px;
  font-family: var(--mono);
  font-size: .88em;
}
#content pre {
  margin: 1.4rem 0;
  padding: 1rem 1.1rem;
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: auto;
  font-size: .875rem;
  line-height: 1.6;
}
#content pre code { padding: 0; background: transparent; border: none; font-size: 1em; }

#content blockquote {
  margin: 1.4rem 0;
  padding: .7rem 1.1rem;
  border-left: 3px solid var(--accent);
  border-radius: 0 var(--radius) var(--radius) 0;
  background: var(--bg-soft);
  color: var(--text-soft);
}
#content blockquote p { margin: .4rem 0; }

/* 图片：锁宽 + 锁高（竖屏截图不再被拉成巨幅）、居中、描边阴影 */
#content img {
  display: block;
  max-width: 100%;
  max-height: 70vh;
  width: auto;
  height: auto;
  margin: 1.6rem auto;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
}

#content table {
  border-collapse: collapse;
  margin: 1.4rem 0;
  width: 100%;
  font-size: .92rem;
  display: block;
  overflow-x: auto;
}
#content th, #content td { border: 1px solid var(--border); padding: .6em .9em; text-align: left; }
#content th { background: var(--bg-soft); font-weight: 600; }
#content tr:nth-child(2n) td { background: var(--bg-soft); }

#content hr { border: none; border-top: 1px solid var(--border); margin: 2.4rem 0; }

.error-box {
  max-width: 820px; margin: 40px auto;
  padding: 16px 20px; border: 1px solid var(--border);
  border-radius: var(--radius); background: var(--bg-soft); color: var(--text-soft);
}

/* ---- 右侧 TOC ---- */
.toc-wrap {
  position: sticky;
  top: var(--header-h);
  width: var(--toc-w);
  flex: 0 0 var(--toc-w);
  height: calc(100vh - var(--header-h));
  overflow-y: auto;
  padding: 32px 16px;
}
.toc-title { font-size: .8rem; text-transform: uppercase; letter-spacing: .05em; color: var(--text-soft); margin-bottom: 10px; }
#toc { list-style: none; margin: 0; padding: 0; border-left: 2px solid var(--border); }
#toc li { margin: 0; }
#toc a {
  display: block;
  padding: 4px 10px;
  font-size: .85rem;
  color: var(--text-soft);
  border-left: 2px solid transparent;
  margin-left: -2px;
}
#toc a:hover { color: var(--accent); text-decoration: none; }
.toc-h3 { padding-left: 22px !important; }
.toc-h4 { padding-left: 34px !important; }

/* ---- 移动端 ---- */
@media (max-width: 960px) {
  .toc-wrap { display: none; }
}
@media (max-width: 768px) {
  .menu-toggle { display: inline-block; }
  .search-box { width: 160px; }
  .sidebar {
    position: fixed;
    top: var(--header-h);
    left: 0;
    transform: translateX(-100%);
    transition: transform .25s ease;
    z-index: 900;
    box-shadow: var(--shadow);
  }
  body.sidebar-open .sidebar { transform: translateX(0); }
  .main { padding: 20px 16px 60px; }
}
