:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --surface-2: #f1f3f9;
  --border: #e3e7ef;
  --fg: #0f172a;
  --muted: #64748b;
  --accent: #4338ca;
  --accent-2: #6d28d9;
  --accent-soft: #eef0ff;
  --success: #059669;
  --warn: #d97706;
  --shadow: 0 1px 2px rgba(15,23,42,.06), 0 8px 24px rgba(15,23,42,.06);
  --radius: 14px;
}
[data-theme="dark"] {
  --bg: #0b1020;
  --surface: #121830;
  --surface-2: #0e1530;
  --border: #20294a;
  --fg: #e6eaf5;
  --muted: #93a0c0;
  --accent: #8b7cff;
  --accent-2: #a78bfa;
  --accent-soft: #1a2244;
  --shadow: 0 1px 2px rgba(0,0,0,.4), 0 10px 30px rgba(0,0,0,.35);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg); color: var(--fg); -webkit-font-smoothing: antialiased;
}
a { color: inherit; text-decoration: none; cursor: pointer; }
#app { display: grid; grid-template-columns: 268px 1fr; min-height: 100vh; }

/* Sidebar */
.sidebar {
  background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; padding: 18px 14px; gap: 14px;
  position: sticky; top: 0; height: 100vh;
}
.brand { display: flex; align-items: center; gap: 12px; padding: 6px 8px 14px; }
.brand-mark {
  width: 40px; height: 40px; border-radius: 11px; display: grid; place-items: center;
  background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: #fff;
  font-weight: 800; letter-spacing: .5px; box-shadow: var(--shadow);
}
.brand-title { font-weight: 800; font-size: 1.02rem; }
.brand-sub { font-size: .72rem; color: var(--muted); letter-spacing: .12em; text-transform: uppercase; }
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav-item {
  display: flex; align-items: center; gap: 11px; padding: 10px 12px; border-radius: 10px;
  color: var(--muted); font-weight: 600; font-size: .92rem; transition: .15s;
}
.nav-item .ni { width: 18px; text-align: center; opacity: .8; }
.nav-item:hover { background: var(--surface-2); color: var(--fg); }
.nav-item.active { background: var(--accent-soft); color: var(--accent); }
.sidebar-foot { margin-top: auto; display: flex; flex-direction: column; gap: 10px; }
.mini-stats { font-size: .76rem; color: var(--muted); line-height: 1.6; padding: 0 8px; }
.mini-stats b { color: var(--fg); }
.theme-toggle {
  background: var(--surface-2); border: 1px solid var(--border); color: var(--muted);
  padding: 9px; border-radius: 10px; cursor: pointer; font-weight: 600; font-size: .82rem;
}
.theme-toggle:hover { color: var(--fg); }

/* Main */
.main { display: flex; flex-direction: column; min-width: 0; }
.topbar {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 14px;
  padding: 14px 26px; background: color-mix(in srgb, var(--bg) 80%, transparent);
  backdrop-filter: blur(8px); border-bottom: 1px solid var(--border);
}
.hamburger { display: none; background: none; border: none; font-size: 1.3rem; color: var(--fg); cursor: pointer; }
.global-search { flex: 1; max-width: 720px; }
.global-search input {
  width: 100%; padding: 11px 16px; border-radius: 11px; border: 1px solid var(--border);
  background: var(--surface); color: var(--fg); font-size: .92rem; outline: none;
}
.global-search input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.pill { background: var(--accent-soft); color: var(--accent); padding: 6px 12px; border-radius: 999px; font-size: .78rem; font-weight: 700; }
.view { padding: 26px; max-width: 1200px; width: 100%; margin: 0 auto; }

/* Headings */
.page-head { margin: 4px 0 22px; }
.page-head h1 { font-size: 1.7rem; margin: 0 0 6px; }
.page-head p { color: var(--muted); margin: 0; max-width: 720px; }

/* Stat cards */
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 16px; }
.stat-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); }
.stat-card .label { color: var(--muted); font-size: .8rem; font-weight: 700; text-transform: uppercase; letter-spacing: .06em; }
.stat-card .value { font-size: 1.9rem; font-weight: 800; margin: 6px 0 2px; }
.stat-card .target { font-size: .78rem; color: var(--muted); }
.bar { height: 7px; background: var(--surface-2); border-radius: 999px; margin-top: 10px; overflow: hidden; }
.bar > span { display: block; height: 100%; background: linear-gradient(90deg, var(--accent), var(--accent-2)); border-radius: 999px; }

/* Generic cards grid */
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 16px; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; box-shadow: var(--shadow); transition: .15s; display: flex; flex-direction: column; gap: 8px;
}
.card:hover { transform: translateY(-2px); border-color: color-mix(in srgb, var(--accent) 40%, var(--border)); }
.book-cover {
  height: 96px; border-radius: 10px; background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff; padding: 12px 14px; display: flex; flex-direction: column; justify-content: space-between;
  position: relative; overflow: hidden;
}
.book-cover::after { content: ""; position: absolute; right: -30px; top: -30px; width: 110px; height: 110px; background: rgba(255,255,255,.12); border-radius: 50%; }
.book-cover .bc-cat { font-size: .68rem; text-transform: uppercase; letter-spacing: .1em; opacity: .85; }
.book-cover .bc-title { font-weight: 800; font-size: .96rem; line-height: 1.25; z-index: 1; }
.card .meta { font-size: .78rem; color: var(--muted); display: flex; flex-wrap: wrap; gap: 6px 12px; }
.tag { font-size: .72rem; padding: 3px 9px; border-radius: 999px; background: var(--surface-2); color: var(--muted); font-weight: 600; }
.tag.level { background: var(--accent-soft); color: var(--accent); }
.tag.pub { background: #dcfce7; color: #166534; }
[data-theme="dark"] .tag.pub { background: #0f3d2a; color: #6ee7b7; }
.card-actions { display: flex; gap: 8px; margin-top: auto; flex-wrap: wrap; }

/* Buttons */
.btn { border: 1px solid var(--border); background: var(--surface-2); color: var(--fg); padding: 8px 13px; border-radius: 9px; font-weight: 700; font-size: .82rem; cursor: pointer; transition: .15s; }
.btn:hover { border-color: var(--accent); color: var(--accent); }
.btn.primary { background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: #fff; border: none; }
.btn.primary:hover { opacity: .92; color: #fff; }
.btn.ghost { background: none; }
.btn.icon { padding: 8px 10px; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }

/* Filters */
.toolbar { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin-bottom: 18px; }
.toolbar select, .toolbar input { padding: 9px 12px; border-radius: 10px; border: 1px solid var(--border); background: var(--surface); color: var(--fg); font-size: .86rem; }
.count-note { color: var(--muted); font-size: .84rem; margin-left: auto; }

/* Book detail / viewer */
.detail-head { display: grid; grid-template-columns: 220px 1fr; gap: 24px; margin-bottom: 24px; }
.detail-cover { height: 280px; border-radius: 16px; background: linear-gradient(150deg, var(--accent), var(--accent-2)); color: #fff; padding: 22px; display: flex; flex-direction: column; justify-content: space-between; box-shadow: var(--shadow); }
.detail-cover .dc-title { font-size: 1.3rem; font-weight: 800; line-height: 1.2; }
.detail-meta h1 { font-size: 1.6rem; margin: 0 0 8px; }
.detail-meta .sub { color: var(--muted); font-size: 1rem; margin-bottom: 14px; }
.kv { display: flex; flex-wrap: wrap; gap: 8px 18px; font-size: .85rem; color: var(--muted); margin-bottom: 16px; }
.kv b { color: var(--fg); }
.section-block { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px 20px; box-shadow: var(--shadow); margin-bottom: 18px; }
.section-block h2 { margin: 0 0 12px; font-size: 1.1rem; }
.chapter-list { display: flex; flex-direction: column; }
.chapter-row { padding: 12px 8px; border-bottom: 1px solid var(--border); display: flex; gap: 12px; align-items: baseline; }
.chapter-row:last-child { border-bottom: none; }
.chapter-row .num { color: var(--accent); font-weight: 800; min-width: 28px; }
.chapter-row .ct { font-weight: 700; }
.chapter-row .cs { color: var(--muted); font-size: .84rem; }
.chip-row { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 4px; }
.chip { font-size: .7rem; padding: 2px 8px; border-radius: 6px; background: var(--surface-2); color: var(--muted); }

/* Reader */
.reader { display: grid; grid-template-columns: 250px 1fr; gap: 24px; }
.reader-toc { position: sticky; top: 82px; align-self: start; max-height: calc(100vh - 110px); overflow: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; }
.reader-toc a { display: block; padding: 6px 8px; border-radius: 7px; font-size: .82rem; color: var(--muted); }
.reader-toc a:hover, .reader-toc a.active { background: var(--accent-soft); color: var(--accent); }
.reader-body { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 30px 36px; box-shadow: var(--shadow); max-width: 820px; }
.reader-body h1 { font-size: 1.8rem; margin-top: 1.6em; border-bottom: 2px solid var(--accent); padding-bottom: .3em; }
.reader-body h2 { font-size: 1.25rem; margin-top: 1.4em; }
.reader-body h3 { font-size: 1.05rem; color: var(--muted); }
.reader-body p { line-height: 1.75; }
.reader-body pre { background: #0b1021; color: #e2e8f0; padding: 16px; border-radius: 10px; overflow: auto; font-size: .82rem; }
.reader-body pre.mermaid { background: var(--surface-2); color: var(--fg); text-align: center; border: 1px solid var(--border); }
.diagram { margin: 1.2em 0; }
.diagram-svg { text-align: center; overflow-x: auto; }
.diagram-svg svg { max-width: 100%; height: auto; }
.diagram-img { max-width: 100%; height: auto; display: block; margin: 0 auto; border: 1px solid var(--border); border-radius: 10px; }
.diagram-loading { color: var(--muted); font-size: .85rem; padding: 24px; text-align: center; border: 1px dashed var(--border); border-radius: 10px; }
.diagram-fallback { font-size: .75rem; max-height: 200px; overflow: auto; }
.diagram-preview { min-height: 120px; background: var(--surface-2); border-radius: 10px; margin-top: 10px; display: flex; align-items: center; justify-content: center; overflow: hidden; }
.diagram-preview svg { max-width: 100%; max-height: 140px; }
.reader-body ul, .reader-body ol { line-height: 1.7; }
.reader-body table { border-collapse: collapse; width: 100%; font-size: .88rem; }
.reader-body th, .reader-body td { border: 1px solid var(--border); padding: 7px 9px; text-align: left; }
.reader-body th { background: var(--surface-2); }
.qbox { background: var(--surface-2); border: 1px solid var(--border); border-left: 4px solid var(--accent); border-radius: 10px; padding: 12px 14px; margin: 10px 0; }
.qbox .opt { display: block; padding: 3px 0; color: var(--muted); }
.qbox .ans { margin-top: 6px; font-weight: 700; color: var(--success); }
.cap { color: var(--muted); font-size: .8rem; font-style: italic; }

/* Search results */
.result { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; margin-bottom: 12px; box-shadow: var(--shadow); }
.result h3 { margin: 0 0 4px; font-size: 1rem; }
.result .snippet { color: var(--muted); font-size: .86rem; }
mark { background: var(--accent-soft); color: var(--accent); padding: 0 2px; border-radius: 3px; }

/* progress */
.progress-track { height: 6px; background: var(--surface-2); border-radius: 999px; overflow: hidden; }
.progress-track > span { display: block; height: 100%; background: var(--success); }

/* paths */
.path-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px 20px; box-shadow: var(--shadow); margin-bottom: 16px; }
.path-steps { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 12px; }
.path-step { background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 9px 13px; font-size: .82rem; font-weight: 600; display: flex; align-items: center; gap: 8px; }
.path-step .n { width: 22px; height: 22px; border-radius: 50%; background: var(--accent); color: #fff; display: grid; place-items: center; font-size: .72rem; }

.empty { text-align: center; color: var(--muted); padding: 60px 20px; }
.loading { color: var(--muted); padding: 40px; text-align: center; }
.pager { display: flex; gap: 8px; justify-content: center; margin-top: 22px; align-items: center; }

@media (max-width: 900px) {
  #app { grid-template-columns: 1fr; }
  .sidebar { position: fixed; z-index: 50; width: 268px; transform: translateX(-100%); transition: .2s; }
  .sidebar.open { transform: none; }
  .hamburger { display: block; }
  .detail-head, .reader { grid-template-columns: 1fr; }
  .reader-toc { position: static; max-height: none; }
}
