/* ============ ms · 设计系统 ============ */
:root {
  --bg: #f4f6fa;
  --bg-soft: #eef1f7;
  --card: #ffffff;
  --card-2: #fafbfd;
  --border: #e4e8f0;
  --border-strong: #d3dae6;
  --text: #1a2233;
  --text-2: #4a5568;
  --muted: #8a94a6;
  --primary: #4f46e5;
  --primary-2: #6366f1;
  --primary-soft: #eef0ff;
  --accent: #f59e0b;
  --accent-soft: #fff5e6;
  --success: #10b981;
  --success-soft: #e7f8f1;
  --warn: #f59e0b;
  --warn-soft: #fff5e6;
  --danger: #ef4444;
  --danger-soft: #fdecec;
  --cyan: #06b6d4;
  --shadow: 0 1px 2px rgba(16, 24, 40, .04), 0 4px 16px rgba(16, 24, 40, .06);
  --shadow-lg: 0 8px 30px rgba(16, 24, 40, .12);
  --radius: 14px;
  --radius-sm: 10px;
  --sidebar-w: 264px;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro SC", "PingFang SC", "Helvetica Neue", "Microsoft YaHei", sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

html[data-theme="dark"] {
  --bg: #10141c;
  --bg-soft: #161b25;
  --card: #181e29;
  --card-2: #1c2331;
  --border: #263042;
  --border-strong: #33405a;
  --text: #e6ebf5;
  --text-2: #b3bdd0;
  --muted: #7b8798;
  --primary: #818cf8;
  --primary-2: #a5b4fc;
  --primary-soft: #232a45;
  --accent: #fbbf24;
  --accent-soft: #33290f;
  --success: #34d399;
  --success-soft: #14342a;
  --warn: #fbbf24;
  --warn-soft: #33290f;
  --danger: #f87171;
  --danger-soft: #3a1d1d;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px rgba(0, 0, 0, .25);
  --shadow-lg: 0 8px 30px rgba(0, 0, 0, .45);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }
body {
  margin: 0; font-family: var(--font); background: var(--bg); color: var(--text);
  font-size: 15px; line-height: 1.6; -webkit-font-smoothing: antialiased;
  transition: background .2s, color .2s;
}
a { color: inherit; text-decoration: none; }
button, input, select, textarea { font-family: inherit; font-size: inherit; color: inherit; }
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 8px; border: 3px solid transparent; background-clip: content-box; }
::-webkit-scrollbar-track { background: transparent; }

/* ---------- 登录 ---------- */
.login-wrap { position: fixed; inset: 0; display: grid; place-items: center; background: linear-gradient(135deg, #eef2ff 0%, #f8fafc 45%, #fff7ed 100%); z-index: 999; padding: 20px; }
html[data-theme="dark"] .login-wrap { background: linear-gradient(135deg, #131a2c 0%, #10141c 50%, #1a1410 100%); }
.login-card { width: 100%; max-width: 380px; background: var(--card); border: 1px solid var(--border); border-radius: 18px; box-shadow: var(--shadow-lg); padding: 34px 30px; }
.login-logo { width: 54px; height: 54px; border-radius: 15px; background: linear-gradient(135deg, var(--primary), var(--primary-2)); color: #fff; display: grid; place-items: center; font-size: 26px; font-weight: 700; margin-bottom: 16px; }
.login-card h1 { margin: 0 0 4px; font-size: 21px; letter-spacing: .3px; }
.login-sub { color: var(--muted); font-size: 13px; margin-bottom: 22px; }

/* ---------- 布局 ---------- */
.app { display: flex; height: 100vh; height: 100dvh; overflow: hidden; }
.sidebar {
  width: var(--sidebar-w); flex: 0 0 var(--sidebar-w); background: var(--card); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; transition: margin-left .22s ease;
}
.sidebar.collapsed { margin-left: calc(-1 * var(--sidebar-w)); }
.brand { display: flex; align-items: center; gap: 10px; padding: 16px 16px 12px; }
.brand-mark { width: 34px; height: 34px; border-radius: 10px; background: linear-gradient(135deg, var(--primary), var(--primary-2)); color: #fff; display: grid; place-items: center; font-weight: 700; font-size: 16px; flex: 0 0 34px; }
.brand-text b { display: block; font-size: 15px; line-height: 1.2; }
.brand-text span { font-size: 11px; color: var(--muted); }
.nav { padding: 6px 10px; overflow-y: auto; flex: 1; }
.nav-item { display: flex; align-items: center; gap: 10px; padding: 9px 12px; border-radius: var(--radius-sm); color: var(--text-2); cursor: pointer; font-size: 14px; margin-bottom: 2px; user-select: none; }
.nav-item:hover { background: var(--bg-soft); color: var(--text); }
.nav-item.active { background: var(--primary-soft); color: var(--primary); font-weight: 600; }
.nav-item .ic { width: 20px; text-align: center; font-size: 16px; }
.nav-item .badge { margin-left: auto; background: var(--danger); color: #fff; font-size: 11px; padding: 1px 7px; border-radius: 10px; font-weight: 600; }
.nav-sec { font-size: 11px; color: var(--muted); padding: 14px 12px 6px; text-transform: uppercase; letter-spacing: .6px; }
.sidebar-foot { padding: 12px; border-top: 1px solid var(--border); font-size: 12px; color: var(--muted); }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; overflow: hidden; }
.topbar { height: 54px; flex: 0 0 54px; display: flex; align-items: center; gap: 12px; padding: 0 16px; border-bottom: 1px solid var(--border); background: var(--card); }
.topbar h2 { margin: 0; font-size: 16px; font-weight: 650; }
.spacer { flex: 1; }
.content { flex: 1; overflow-y: auto; padding: 18px; scroll-behavior: smooth; }
.page { max-width: 1180px; margin: 0 auto; }

/* ---------- 通用组件 ---------- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 8px 14px; border-radius: var(--radius-sm); border: 1px solid var(--border-strong); background: var(--card); cursor: pointer; font-size: 14px; transition: .15s; white-space: nowrap; }
.btn:hover { border-color: var(--primary); color: var(--primary); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-2); color: #fff; }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: var(--bg-soft); }
.btn.sm { padding: 5px 10px; font-size: 13px; }
.btn.danger { color: var(--danger); border-color: var(--danger); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.icon-btn { width: 34px; height: 34px; display: grid; place-items: center; border-radius: var(--radius-sm); border: 1px solid var(--border); background: var(--card); cursor: pointer; font-size: 15px; }
.icon-btn:hover { border-color: var(--primary); color: var(--primary); }
.input, select.input, textarea.input { width: 100%; padding: 9px 12px; border-radius: var(--radius-sm); border: 1px solid var(--border-strong); background: var(--card); outline: none; transition: .15s; }
.input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
.card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-pad { padding: 16px; }
.grid { display: grid; gap: 14px; }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.g4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.row { display: flex; gap: 10px; align-items: center; }
.wrap { flex-wrap: wrap; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.tiny { font-size: 11.5px; }
.b { font-weight: 650; }
.center { text-align: center; }
.mt { margin-top: 14px; }
.mb { margin-bottom: 14px; }
.hidden { display: none !important; }
.tag { display: inline-flex; align-items: center; gap: 4px; padding: 2px 8px; border-radius: 20px; font-size: 11.5px; background: var(--bg-soft); color: var(--text-2); border: 1px solid var(--border); }
.tag.green { background: var(--success-soft); color: var(--success); border-color: transparent; }
.tag.blue { background: var(--primary-soft); color: var(--primary); border-color: transparent; }
.tag.amber { background: var(--accent-soft); color: var(--accent); border-color: transparent; }
.tag.red { background: var(--danger-soft); color: var(--danger); border-color: transparent; }
.empty { text-align: center; padding: 48px 20px; color: var(--muted); }
.empty .e-ic { font-size: 40px; opacity: .5; margin-bottom: 8px; }
.divider { height: 1px; background: var(--border); margin: 14px 0; }

/* 进度条 */
.bar { height: 7px; border-radius: 6px; background: var(--bg-soft); overflow: hidden; }
.bar > i { display: block; height: 100%; border-radius: 6px; background: linear-gradient(90deg, var(--primary), var(--primary-2)); transition: width .4s; }
.bar.slim { height: 5px; }
.bar.green > i { background: linear-gradient(90deg, #10b981, #34d399); }

/* 统计卡 */
.stat { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.stat .k { font-size: 12px; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.stat .v { font-size: 25px; font-weight: 700; line-height: 1.25; letter-spacing: -.5px; }
.stat .s { font-size: 11.5px; color: var(--muted); }
.stat.accent .v { color: var(--primary); }
.stat.ok .v { color: var(--success); }
.stat.warn .v { color: var(--accent); }

/* 列表项 */
.doc-item { display: flex; gap: 12px; align-items: flex-start; padding: 12px 14px; border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--card); cursor: pointer; transition: .15s; }
.doc-item:hover { border-color: var(--primary); transform: translateY(-1px); box-shadow: var(--shadow); }
.doc-ic { width: 34px; height: 34px; flex: 0 0 34px; border-radius: 9px; display: grid; place-items: center; font-size: 15px; background: var(--danger-soft); color: var(--danger); }
.doc-ic.img { background: var(--primary-soft); color: var(--primary); }
.doc-main { flex: 1; min-width: 0; }
.doc-title { font-weight: 600; font-size: 14.5px; line-height: 1.4; word-break: break-word; display: flex; align-items: center; gap: 6px; }
.doc-meta { font-size: 12px; color: var(--muted); margin-top: 3px; display: flex; gap: 8px; flex-wrap: wrap; }
.doc-right { display: flex; flex-direction: column; align-items: flex-end; gap: 6px; flex: 0 0 auto; }
.dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; }
.dot.new { background: var(--border-strong); }
.dot.ing { background: var(--accent); }
.dot.done { background: var(--success); }

/* 目录树 */
.tree-node { user-select: none; }
.tree-row { display: flex; align-items: center; gap: 6px; padding: 7px 10px; border-radius: 8px; cursor: pointer; font-size: 13.5px; }
.tree-row:hover { background: var(--bg-soft); }
.tree-row.open { background: var(--bg-soft); }
.tree-row.active { background: var(--primary-soft); color: var(--primary); font-weight: 600; }
.tree-caret { width: 14px; transition: transform .15s; font-size: 10px; color: var(--muted); }
.tree-caret.open { transform: rotate(90deg); }
.tree-count { margin-left: auto; font-size: 11px; color: var(--muted); }
.tree-children { padding-left: 14px; border-left: 1px dashed var(--border); margin-left: 14px; }

/* 移动底部导航 */
.tabbar { display: none; }
.mask { position: fixed; inset: 0; background: rgba(0, 0, 0, .4); z-index: 40; }

/* ---------- 阅读器 ---------- */
.reader { position: fixed; inset: 0; background: var(--bg); z-index: 60; display: flex; flex-direction: column; }
.reader-head { height: 50px; flex: 0 0 50px; display: flex; align-items: center; gap: 8px; padding: 0 12px; background: var(--card); border-bottom: 1px solid var(--border); }
.reader-title { font-weight: 650; font-size: 14.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.reader-body { flex: 1; display: flex; min-height: 0; position: relative; }
.reader-toc { width: 260px; flex: 0 0 260px; border-right: 1px solid var(--border); background: var(--card); overflow-y: auto; padding: 12px; }
.reader-stage { flex: 1; min-width: 0; overflow-y: auto; position: relative; background: var(--bg-soft); }
.reader-foot { height: 42px; flex: 0 0 42px; display: flex; align-items: center; gap: 10px; padding: 0 14px; background: var(--card); border-top: 1px solid var(--border); font-size: 12.5px; color: var(--muted); }

/* 文本模式 */
.textview { max-width: 780px; margin: 0 auto; padding: 28px 22px 120px; background: var(--card); min-height: 100%; box-shadow: var(--shadow); }
.textview .tv-page { border-top: 1px dashed var(--border); padding-top: 18px; margin-top: 22px; }
.textview .tv-page:first-child { border-top: 0; padding-top: 0; margin-top: 0; }
.textview .tv-pnum { font-size: 11px; color: var(--muted); text-align: center; margin-bottom: 10px; letter-spacing: 1px; }
.textview p { margin: 0 0 12px; }
.textview h1, .textview h2, .textview h3 { line-height: 1.45; margin: 22px 0 10px; scroll-margin-top: 20px; }
.textview h1 { font-size: 21px; } .textview h2 { font-size: 18px; } .textview h3 { font-size: 16px; }
.textview code { background: var(--bg-soft); padding: 1px 5px; border-radius: 4px; font-family: var(--mono); font-size: .9em; }
.textview pre { background: var(--bg-soft); padding: 12px; border-radius: 10px; overflow-x: auto; font-family: var(--mono); font-size: 12.5px; border: 1px solid var(--border); }
.textview .hl { background: #ffe89a; border-radius: 2px; }
html[data-theme="dark"] .textview .hl { background: #5b4a10; color: #ffe89a; }

/* PDF 模式 */
.pdfview { padding: 14px 0 120px; display: flex; flex-direction: column; align-items: center; gap: 14px; }
.pdfview canvas { background: #fff; box-shadow: var(--shadow); border-radius: 4px; max-width: 100%; }
html[data-theme="dark"] .pdfview canvas { filter: invert(.92) hue-rotate(180deg) brightness(1.02); }

/* 图片模式 */
.imgview { padding: 0 0 100px; }
.img-fit { width: 100%; display: block; }
.img-fit img { width: 100%; height: auto; display: block; }
.seg { position: relative; width: 100%; background-repeat: no-repeat; background-size: 100% auto; border-bottom: 1px solid var(--border); }
.seg-num { position: absolute; right: 8px; top: 6px; background: rgba(0, 0, 0, .35); color: #fff; font-size: 11px; padding: 1px 7px; border-radius: 10px; }
.img-zoom { overflow: auto; height: 100%; -webkit-overflow-scrolling: touch; }
.img-zoom img { display: block; transform-origin: top left; }

/* 抽屉 */
.drawer { position: fixed; top: 0; right: 0; bottom: 0; width: 380px; max-width: 92vw; background: var(--card); border-left: 1px solid var(--border); box-shadow: var(--shadow-lg); z-index: 70; display: flex; flex-direction: column; transform: translateX(100%); transition: transform .22s; }
.drawer.open { transform: none; }
.drawer-head { padding: 12px 14px; border-bottom: 1px solid var(--border); display: flex; align-items: center; gap: 8px; }
.drawer-body { flex: 1; overflow-y: auto; padding: 14px; }
.bm-item { border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 10px 12px; margin-bottom: 10px; cursor: pointer; }
.bm-item:hover { border-color: var(--primary); }
.note-area { width: 100%; min-height: 200px; resize: vertical; }

/* 热力图 */
.heat { display: grid; grid-auto-flow: column; grid-template-rows: repeat(7, 11px); gap: 3px; overflow-x: auto; padding-bottom: 4px; }
.heat i { width: 11px; height: 11px; border-radius: 2px; background: var(--bg-soft); display: block; }
.heat i[data-l="1"] { background: #c7d2fe; }
.heat i[data-l="2"] { background: #818cf8; }
.heat i[data-l="3"] { background: #6366f1; }
.heat i[data-l="4"] { background: #4338ca; }

/* 复习卡 */
.rev-card { border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; background: var(--card); box-shadow: var(--shadow); }
.rev-q { font-size: 17px; font-weight: 650; line-height: 1.5; }
.rev-meta { font-size: 12px; color: var(--muted); margin-top: 6px; }

/* toast */
.toast { position: fixed; left: 50%; bottom: 30px; transform: translateX(-50%) translateY(20px); background: #1f2937; color: #fff; padding: 10px 18px; border-radius: 10px; font-size: 14px; z-index: 300; opacity: 0; transition: .22s; pointer-events: none; box-shadow: var(--shadow-lg); }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* 弹窗 */
.modal { position: fixed; inset: 0; background: rgba(15, 23, 42, .5); z-index: 200; display: grid; place-items: center; padding: 18px; backdrop-filter: blur(2px); }
.modal-box { background: var(--card); border-radius: 16px; box-shadow: var(--shadow-lg); width: 100%; max-width: 520px; max-height: 86vh; display: flex; flex-direction: column; overflow: hidden; }
.modal-head { padding: 15px 18px; border-bottom: 1px solid var(--border); display: flex; align-items: center; font-weight: 650; }
.modal-body { padding: 18px; overflow-y: auto; }
.modal-foot { padding: 12px 18px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 10px; }
.field { margin-bottom: 14px; }
.field label { display: block; font-size: 13px; color: var(--text-2); margin-bottom: 6px; }
.chips { display: flex; flex-wrap: wrap; gap: 7px; }
.chip { padding: 5px 11px; border-radius: 20px; border: 1px solid var(--border-strong); background: var(--card); cursor: pointer; font-size: 13px; }
.chip.on { background: var(--primary-soft); border-color: var(--primary); color: var(--primary); font-weight: 600; }

/* 搜索结果 */
.sr { border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 12px 14px; background: var(--card); cursor: pointer; margin-bottom: 10px; }
.sr:hover { border-color: var(--primary); }
.sr b { color: var(--primary); }
.sr .snip { font-size: 13px; color: var(--text-2); margin-top: 5px; line-height: 1.55; word-break: break-word; }

/* ============ 移动端 ============ */
@media (max-width: 860px) {
  .sidebar { position: fixed; left: 0; top: 0; bottom: 0; z-index: 50; box-shadow: var(--shadow-lg); }
  .sidebar.collapsed { margin-left: calc(-1 * var(--sidebar-w)); }
  .content { padding: 12px; }
  .g4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .g3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .g2 { grid-template-columns: 1fr; }
  .tabbar {
    display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 45; background: var(--card);
    border-top: 1px solid var(--border); padding: 5px 0 calc(5px + env(safe-area-inset-bottom));
  }
  .tabbar .tab { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 4px 0; font-size: 10.5px; color: var(--muted); cursor: pointer; position: relative; }
  .tabbar .tab.active { color: var(--primary); font-weight: 600; }
  .tabbar .tab .ic { font-size: 19px; line-height: 1; }
  .tabbar .tab .bdg { position: absolute; top: 0; right: 22%; background: var(--danger); color: #fff; font-size: 9.5px; padding: 0 5px; border-radius: 8px; }
  .app { padding-bottom: 58px; }
  .reader-toc { position: absolute; left: 0; top: 0; bottom: 0; z-index: 5; box-shadow: var(--shadow-lg); transform: translateX(-100%); transition: transform .2s; }
  .reader-toc.open { transform: none; }
  .textview { padding: 18px 14px 100px; }
  .stat .v { font-size: 21px; }
  .modal-box { max-width: 100%; }
  .drawer { width: 100%; }
}
@media (max-width: 400px) {
  .g4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
