/* ---------- 主题变量 ---------- */
:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #e4e7ec;
  --text: #1f2430;
  --text-soft: #5b6577;
  --text-faint: #8b95a7;
  --accent: #3b6ef0;
  --accent-soft: #eaf0ff;
  --code-bg: #f4f6f9;
  --shadow: 0 1px 2px rgba(16, 24, 40, .04), 0 8px 24px rgba(16, 24, 40, .06);
  --sidebar-w: 300px;
  --toc-w: 240px;
  --radius: 12px;
}

html[data-theme="dark"] {
  --bg: #14171c;
  --surface: #1b1f26;
  --surface-2: #232830;
  --border: #2c323c;
  --text: #dfe4ec;
  --text-soft: #a4adbd;
  --text-faint: #737d8d;
  --accent: #6b93ff;
  --accent-soft: #1e2739;
  --code-bg: #1a1f27;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 24px rgba(0, 0, 0, .25);
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB",
               "Microsoft YaHei", "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  font-size: 15px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

/* ---------- 顶部栏 ---------- */
#progress {
  position: fixed;
  top: 0; left: 0;
  height: 2px; width: 0;
  background: linear-gradient(90deg, var(--accent), #8b5cf6);
  z-index: 100;
  transition: width .1s linear;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  gap: 14px;
  height: 56px;
  padding: 0 18px;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid var(--border);
}

.brand { display: flex; align-items: center; gap: 8px; min-width: 0; }
.brand-mark { color: var(--accent); font-size: 18px; }
.brand-text {
  font-weight: 650;
  font-size: 15px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.search-wrap { flex: 1; display: flex; justify-content: center; }
#search {
  width: 100%;
  max-width: 420px;
  height: 34px;
  padding: 0 14px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text);
  font-size: 13.5px;
  outline: none;
  transition: border-color .15s, box-shadow .15s;
}
#search:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
  background: var(--surface);
}

.icon-btn {
  height: 34px; min-width: 34px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text-soft);
  border-radius: 9px;
  cursor: pointer;
  font-size: 14px;
  transition: .15s;
}
.icon-btn:hover { color: var(--accent); border-color: var(--accent); }

#menu-btn { display: none; }

.switch {
  display: flex; align-items: center; gap: 5px;
  font-size: 12.5px; color: var(--text-faint);
  cursor: pointer; user-select: none; white-space: nowrap;
}
.switch input { accent-color: var(--accent); cursor: pointer; }

/* ---------- 布局 ---------- */
.layout {
  display: grid;
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr) var(--toc-w);
  align-items: start;
  max-width: 1680px;
  margin: 0 auto;
}

.sidebar {
  position: sticky;
  top: 56px;
  height: calc(100vh - 56px);
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border);
  background: var(--surface);
}

/* ---------- 目录树 ---------- */
.tree {
  flex: 1;
  overflow-y: auto;
  padding: 12px 10px 24px;
  font-size: 13.5px;
}
.tree::-webkit-scrollbar, .toc-list::-webkit-scrollbar, .article pre::-webkit-scrollbar { width: 8px; height: 8px; }
.tree::-webkit-scrollbar-thumb, .toc-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

.group { margin-bottom: 2px; }
.group > summary {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 10px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 600;
  color: var(--text);
  list-style: none;
}
.group > summary::-webkit-details-marker { display: none; }
.group > summary:hover { background: var(--surface-2); }
.group > summary::before {
  content: "▸";
  font-size: 10px;
  color: var(--text-faint);
  transition: transform .15s;
  flex: none;
}
.group[open] > summary::before { transform: rotate(90deg); }

.num-badge {
  flex: none;
  font-size: 11px;
  font-weight: 700;
  padding: 1px 6px;
  border-radius: 5px;
  background: var(--accent-soft);
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}
.group-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.items { padding: 2px 0 6px 6px; }

.item {
  display: flex;
  gap: 8px;
  align-items: flex-start;
  padding: 6px 9px 6px 12px;
  border-radius: 7px;
  color: var(--text-soft);
  text-decoration: none;
  cursor: pointer;
  border-left: 2px solid transparent;
  transition: .13s;
}
.item:hover { background: var(--surface-2); color: var(--text); }
.item.active {
  background: var(--accent-soft);
  color: var(--accent);
  border-left-color: var(--accent);
  font-weight: 600;
}
.item .label {
  flex: 1;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  line-height: 1.45;
}
.dup-tag {
  flex: none;
  font-size: 10px;
  color: var(--text-faint);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0 4px;
  line-height: 15px;
  margin-top: 2px;
}

.group-hint {
  flex: none;
  font-size: 11px;
  color: var(--text-faint);
  background: var(--surface-2);
  border-radius: 4px;
  padding: 1px 6px;
  margin-top: 2px;
  max-width: 90px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sidebar-foot {
  padding: 9px 16px;
  border-top: 1px solid var(--border);
  font-size: 12px;
  color: var(--text-faint);
}

.empty-hint { padding: 16px 12px; color: var(--text-faint); font-size: 13px; }

/* ---------- 正文 ---------- */
.main { padding: 32px 40px 80px; min-width: 0; }

.article {
  max-width: 820px;
  margin: 0 auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 44px 52px 56px;
  animation: fade .25s ease;
}
@keyframes fade { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

.article h1 {
  font-size: 27px;
  line-height: 1.4;
  margin: 0 0 22px;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--border);
  letter-spacing: -.3px;
}
.article h2 {
  font-size: 20px;
  margin: 40px 0 14px;
  padding-left: 12px;
  border-left: 4px solid var(--accent);
  line-height: 1.45;
}
.article h3 { font-size: 17px; margin: 30px 0 10px; }
.article h4 { font-size: 15.5px; margin: 24px 0 8px; color: var(--text-soft); }
.article h1, .article h2, .article h3, .article h4 { scroll-margin-top: 76px; }

.article p { margin: 14px 0; }
.article ul, .article ol { padding-left: 26px; margin: 14px 0; }
.article li { margin: 6px 0; }
.article li::marker { color: var(--text-faint); }

.article a { color: var(--accent); text-decoration: none; border-bottom: 1px solid transparent; }
.article a:hover { border-bottom-color: var(--accent); }

.article strong { font-weight: 650; }

.article blockquote {
  margin: 18px 0;
  padding: 4px 18px;
  border-left: 3px solid var(--border);
  background: var(--surface-2);
  border-radius: 0 8px 8px 0;
  color: var(--text-soft);
}

.article hr { border: none; border-top: 1px solid var(--border); margin: 34px 0; }

.article img {
  max-width: 100%;
  height: auto;
  display: block;
  margin: 20px auto;
  border-radius: 8px;
  border: 1px solid var(--border);
}

.article audio { width: 100%; margin: 14px 0; height: 38px; }

.article code {
  font-family: "SF Mono", ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  font-size: .875em;
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 1.5px 5px;
  word-break: break-word;
}

.article pre {
  position: relative;
  margin: 18px 0;
  padding: 16px 18px;
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow-x: auto;
  line-height: 1.62;
}
.article pre code {
  background: none;
  border: none;
  padding: 0;
  font-size: 13px;
  display: block;
}
.article pre code.hljs { background: transparent; padding: 0; }

.copy-btn {
  position: absolute;
  top: 8px; right: 8px;
  font-size: 11.5px;
  padding: 3px 9px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-faint);
  cursor: pointer;
  opacity: 0;
  transition: .15s;
}
.article pre:hover .copy-btn { opacity: 1; }
.copy-btn:hover { color: var(--accent); border-color: var(--accent); }
.copy-btn.done { color: #16a34a; border-color: #16a34a; }

.article table {
  width: 100%;
  border-collapse: collapse;
  margin: 18px 0;
  font-size: 13.5px;
  display: block;
  overflow-x: auto;
}
.article th, .article td { border: 1px solid var(--border); padding: 8px 12px; text-align: left; }
.article th { background: var(--surface-2); font-weight: 650; }

/* 上一篇 / 下一篇 */
.pager {
  max-width: 820px;
  margin: 22px auto 0;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}
.pager a {
  display: block;
  padding: 13px 18px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  text-decoration: none;
  color: var(--text);
  font-size: 13.5px;
  transition: .15s;
  overflow: hidden;
}
.pager a:hover { border-color: var(--accent); box-shadow: var(--shadow); }
.pager a .dir { display: block; font-size: 11.5px; color: var(--text-faint); margin-bottom: 3px; }
.pager a .t { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.pager .next { text-align: right; }
.pager .placeholder { visibility: hidden; }

/* ---------- 右侧 TOC ---------- */
.toc {
  position: sticky;
  top: 56px;
  height: calc(100vh - 56px);
  overflow-y: auto;
  padding: 28px 20px 40px 8px;
  font-size: 12.5px;
}
.toc-title {
  font-weight: 650;
  color: var(--text-faint);
  font-size: 11.5px;
  letter-spacing: .08em;
  text-transform: uppercase;
  margin-bottom: 12px;
  padding-left: 11px;
}
.toc-list a {
  display: block;
  padding: 4px 10px;
  border-left: 2px solid var(--border);
  color: var(--text-soft);
  text-decoration: none;
  line-height: 1.5;
  transition: .13s;
}
.toc-list a:hover { color: var(--text); border-left-color: var(--text-faint); }
.toc-list a.lv3 { padding-left: 22px; font-size: 12px; }
.toc-list a.active { color: var(--accent); border-left-color: var(--accent); font-weight: 600; }

/* ---------- 响应式 ---------- */
@media (max-width: 1280px) {
  .layout { grid-template-columns: var(--sidebar-w) minmax(0, 1fr); }
  .toc { display: none; }
}
@media (max-width: 900px) {
  #menu-btn { display: block; }
  .layout { grid-template-columns: minmax(0, 1fr); }
  .sidebar {
    position: fixed;
    top: 56px; left: 0; bottom: 0;
    width: var(--sidebar-w);
    z-index: 40;
    transform: translateX(-100%);
    transition: transform .22s ease;
    box-shadow: var(--shadow);
  }
  .sidebar.open { transform: none; }
  .main { padding: 20px 14px 60px; }
  .article { padding: 26px 20px 40px; border-radius: 10px; }
  .article h1 { font-size: 22px; }
  .search-wrap { justify-content: flex-end; }
  #search { max-width: 100%; }
  .brand-text { display: none; }
  .pager { grid-template-columns: 1fr; }
}
