/* ============ 主题变量 ============ */
:root {
  --bg: #ffffff;
  --bg-elev: #f6f7f9;
  --bg-sidebar: #f4f5f7;
  --text: #1f2329;
  --text-soft: #5b6470;
  --text-faint: #939ba6;
  --border: #e3e6ea;
  --accent: #3b6ef0;
  --accent-soft: #e8efff;
  --code-bg: #f3f4f6;
  --quote-bar: #d7dbe0;
  --mark: #fff3a3;
  --topbar-h: 52px;
  --reader-fs: 17px;
  --reader-lh: 1.75;
  --maxw: 72ch;
  --sidebar-w: 300px;
  --outline-w: 240px;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}
body[data-theme="dark"] {
  --bg: #1b1f27;
  --bg-elev: #232834;
  --bg-sidebar: #181c23;
  --text: #e6e9ee;
  --text-soft: #aab2bf;
  --text-faint: #727b88;
  --border: #2c323d;
  --accent: #6aa0ff;
  --accent-soft: #25324d;
  --code-bg: #232a36;
  --quote-bar: #3a4250;
  --mark: #6b5d1f;
}
@media (prefers-color-scheme: dark) {
  body[data-theme="auto"] {
    --bg: #1b1f27; --bg-elev: #232834; --bg-sidebar: #181c23;
    --text: #e6e9ee; --text-soft: #aab2bf; --text-faint: #727b88;
    --border: #2c323d; --accent: #6aa0ff; --accent-soft: #25324d;
    --code-bg: #232a36; --quote-bar: #3a4250; --mark: #6b5d1f;
  }
}
body[data-font="s"]  { --reader-fs: 15px; }
body[data-font="m"]  { --reader-fs: 17px; }
body[data-font="l"]  { --reader-fs: 19px; }
body[data-font="xl"] { --reader-fs: 22px; }

/* ============ 基础 ============ */
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}
button { font-family: inherit; cursor: pointer; }

/* ============ 顶栏 ============ */
.topbar {
  position: sticky; top: 0; z-index: 40;
  height: calc(var(--topbar-h) + var(--safe-t));
  padding-top: var(--safe-t);
  display: flex; align-items: center; gap: 6px;
  padding-left: max(12px, var(--safe-l)); padding-right: max(12px, var(--safe-r));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(160%) blur(10px);
  border-bottom: 1px solid var(--border);
}
.topbar-title {
  flex: 1; min-width: 0; font-weight: 600; font-size: 16px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  padding: 0 4px;
}
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 40px; height: 40px; border: none; border-radius: 10px;
  background: transparent; color: var(--text); font-size: 16px;
}
.icon-btn:active { background: var(--bg-elev); }

/* ============ 布局 ============ */
.layout {
  display: flex;
  height: calc(100% - var(--topbar-h) - var(--safe-t));
}
.sidebar, .outline {
  flex: 0 0 auto;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  overflow-y: auto; -webkit-overflow-scrolling: touch;
  display: flex; flex-direction: column;
}
.outline { border-right: none; border-left: 1px solid var(--border); background: var(--bg); }
.reader {
  flex: 1 1 auto; min-width: 0;
  overflow-y: auto; -webkit-overflow-scrolling: touch;
}

/* ----- 窄屏（折叠态/竖屏手机）：侧栏与大纲都是抽屉 ----- */
@media (max-width: 759px) {
  .sidebar, .outline {
    position: fixed; top: calc(var(--topbar-h) + var(--safe-t)); bottom: 0; z-index: 35;
    width: min(82vw, 340px);
    transition: transform .25s ease;
    box-shadow: 0 0 40px rgba(0,0,0,.18);
  }
  .sidebar { left: 0; transform: translateX(-102%); }
  .outline { right: 0; left: auto; transform: translateX(102%); width: min(76vw, 300px); }
  body.nav-open .sidebar { transform: translateX(0); }
  body.outline-open .outline { transform: translateX(0); }
  .scrim {
    position: fixed; inset: calc(var(--topbar-h) + var(--safe-t)) 0 0 0; z-index: 34;
    background: rgba(0,0,0,.4); opacity: 0; pointer-events: none; transition: opacity .25s;
  }
  body.nav-open .scrim, body.outline-open .scrim { opacity: 1; pointer-events: auto; }
}

/* ----- 宽屏（展开态/平板）：侧栏固定为左栏 ----- */
@media (min-width: 760px) {
  .sidebar { width: var(--sidebar-w); }
  .scrim { display: none; }
  #btn-menu { display: none; }      /* 宽屏不需要抽屉按钮 */
  .outline {
    position: fixed; top: calc(var(--topbar-h) + var(--safe-t)); bottom: 0; right: 0; z-index: 35;
    width: var(--outline-w); transform: translateX(102%); transition: transform .25s ease;
    box-shadow: 0 0 40px rgba(0,0,0,.12);
  }
  body.outline-open .outline { transform: translateX(0); }
}

/* ----- 超宽屏：大纲常驻为右栏 ----- */
@media (min-width: 1120px) {
  .outline {
    position: static; transform: none !important; box-shadow: none; z-index: auto;
    width: var(--outline-w);
  }
  #btn-outline { display: none; }
}

/* ----- 真·双屏折叠设备：按屏段排布，避开折痕 ----- */
@media (horizontal-viewport-segments: 2) {
  .sidebar { width: env(viewport-segment-width 0 0); }
  .reader  { margin-left: calc(env(viewport-segment-left 1 0) - env(viewport-segment-right 0 0)); }
  #btn-menu { display: none; }
  .scrim { display: none; }
}

/* ============ 文件树 ============ */
.sidebar-head {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 12px 14px 8px; position: sticky; top: 0; background: var(--bg-sidebar); z-index: 2;
}
.vault-name { font-weight: 700; font-size: 14px; color: var(--text-soft);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.text-btn {
  border: 1px solid var(--border); background: var(--bg); color: var(--accent);
  border-radius: 8px; padding: 5px 10px; font-size: 13px; white-space: nowrap;
}
.text-btn:active { background: var(--bg-elev); }
.tree-search { padding: 0 12px 8px; }
.tree-search input {
  width: 100%; border: 1px solid var(--border); background: var(--bg); color: var(--text);
  border-radius: 8px; padding: 7px 10px; font-size: 13px; outline: none;
}
.tree-search input:focus { border-color: var(--accent); }
.tree { padding: 0 6px 24px; font-size: 14px; }
.tree-item { display: flex; align-items: center; gap: 4px; border-radius: 8px;
  padding: 7px 8px; color: var(--text); user-select: none; line-height: 1.3; }
.tree-item:active { background: var(--bg-elev); }
.tree-item.folder { color: var(--text-soft); font-weight: 600; }
.tree-item.file.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.tree-caret { width: 16px; height: 16px; flex: 0 0 auto; transition: transform .15s;
  color: var(--text-faint); }
.tree-item.collapsed > .tree-caret { transform: rotate(-90deg); }
.tree-ico { width: 16px; height: 16px; flex: 0 0 auto; color: var(--text-faint); }
.tree-label { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tree-children.collapsed { display: none; }
.tree-children { margin-left: 12px; border-left: 1px solid var(--border); padding-left: 2px; }
.tree-empty { padding: 20px 14px; color: var(--text-faint); font-size: 13px; }

/* ============ 阅读区 ============ */
.doc {
  max-width: var(--maxw); margin: 0 auto;
  padding: 28px max(20px, var(--safe-r)) calc(64px + var(--safe-b)) max(20px, var(--safe-l));
  font-size: var(--reader-fs); line-height: var(--reader-lh); color: var(--text);
  word-wrap: break-word; overflow-wrap: anywhere;
}
.doc h1, .doc h2, .doc h3, .doc h4 { line-height: 1.3; font-weight: 700; margin: 1.6em 0 .6em; }
.doc h1 { font-size: 1.7em; margin-top: .2em; }
.doc h2 { font-size: 1.4em; padding-bottom: .25em; border-bottom: 1px solid var(--border); }
.doc h3 { font-size: 1.2em; }
.doc h4 { font-size: 1.05em; }
.doc p { margin: .85em 0; }
.doc a { color: var(--accent); text-decoration: none; }
.doc a:hover { text-decoration: underline; }
.doc ul, .doc ol { padding-left: 1.5em; margin: .7em 0; }
.doc li { margin: .3em 0; }
.doc img { max-width: 100%; height: auto; border-radius: 8px; display: block; margin: .8em 0; }
.doc hr { border: none; border-top: 1px solid var(--border); margin: 2em 0; }
.doc strong { font-weight: 700; }
.doc mark { background: var(--mark); color: inherit; padding: 0 2px; border-radius: 3px; }

/* 代码 */
.doc code { font-family: "SF Mono", ui-monospace, "Cascadia Code", Menlo, Consolas, monospace;
  font-size: .88em; background: var(--code-bg); padding: .15em .4em; border-radius: 5px; }
.doc pre { background: var(--code-bg); border: 1px solid var(--border); border-radius: 10px;
  padding: 14px 16px; overflow-x: auto; -webkit-overflow-scrolling: touch; margin: 1em 0; }
.doc pre code { background: none; padding: 0; font-size: .86em; line-height: 1.6; }

/* 引用 & Obsidian callout */
.doc blockquote { margin: 1em 0; padding: .2em 1em; border-left: 3px solid var(--quote-bar);
  color: var(--text-soft); }
.doc .callout { border: 1px solid var(--border); border-left: 4px solid var(--accent);
  background: var(--accent-soft); border-radius: 8px; padding: 12px 14px; margin: 1em 0; }
.doc .callout-title { font-weight: 700; display: flex; align-items: center; gap: 6px; margin-bottom: 4px; }
.doc .callout-body > :first-child { margin-top: 0; }
.doc .callout-body > :last-child { margin-bottom: 0; }

/* 表格 */
.doc .table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; margin: 1em 0; }
.doc table { border-collapse: collapse; width: 100%; font-size: .92em; }
.doc th, .doc td { border: 1px solid var(--border); padding: 7px 11px; text-align: left; }
.doc th { background: var(--bg-elev); font-weight: 700; }

/* 任务清单 */
.doc li.task-list-item { list-style: none; margin-left: -1.4em; }
.doc li.task-list-item input { margin-right: .5em; transform: translateY(1px); }

/* 维基双链 */
.doc a.wikilink { color: var(--accent); border-bottom: 1px dashed color-mix(in srgb, var(--accent) 50%, transparent); }
.doc a.wikilink.missing { color: var(--text-faint); border-bottom-style: dotted; cursor: help; }

/* 文末导航/未找到提示 */
.doc-missing { margin: 1.5em 0; padding: 12px 14px; border: 1px dashed var(--border);
  border-radius: 8px; color: var(--text-faint); font-size: .9em; }

/* ============ 空状态 ============ */
.empty { max-width: 460px; margin: 8vh auto 0; text-align: center; padding: 0 24px; }
.empty-icon { color: var(--text-faint); margin-bottom: 8px; }
.empty h2 { font-size: 1.3em; margin: .2em 0 .4em; }
.empty p { color: var(--text-soft); line-height: 1.7; }
.empty-actions { display: flex; flex-direction: column; gap: 10px; margin: 22px auto; max-width: 280px; }
.primary-btn { background: var(--accent); color: #fff; border: none; border-radius: 12px;
  padding: 14px; font-size: 16px; font-weight: 600; }
.primary-btn:active { filter: brightness(.94); }
.ghost-btn { background: transparent; color: var(--text-soft); border: 1px solid var(--border);
  border-radius: 12px; padding: 12px; font-size: 15px; }
.empty .hint { font-size: 13px; color: var(--text-faint); }

/* ============ 大纲 ============ */
.outline-head, .sidebar-head { font-size: 13px; }
.outline-head { padding: 14px 16px 8px; font-weight: 700; color: var(--text-soft);
  position: sticky; top: 0; background: inherit; }
.outline-list { padding: 0 8px 24px; font-size: 13.5px; }
.outline-list a { display: block; color: var(--text-soft); text-decoration: none;
  padding: 6px 8px; border-radius: 7px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.outline-list a:active { background: var(--bg-elev); }
.outline-list a.active { color: var(--accent); background: var(--accent-soft); font-weight: 600; }
.outline-list a.lvl-1 { font-weight: 600; }
.outline-list a.lvl-2 { padding-left: 18px; }
.outline-list a.lvl-3 { padding-left: 30px; }
.outline-list a.lvl-4 { padding-left: 42px; color: var(--text-faint); }
.outline-empty { padding: 16px; color: var(--text-faint); font-size: 13px; }

/* ============ 设置面板 ============ */
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 60;
  background: var(--bg); border-top-left-radius: 18px; border-top-right-radius: 18px;
  box-shadow: 0 -8px 40px rgba(0,0,0,.25); padding-bottom: calc(16px + var(--safe-b));
  animation: sheet-up .25s ease;
}
@media (min-width: 760px) {
  .sheet { left: auto; right: 16px; bottom: 16px; width: 360px; border-radius: 16px; }
}
@keyframes sheet-up { from { transform: translateY(20px); opacity: 0; } to { transform: none; opacity: 1; } }
.sheet-head { display: flex; align-items: center; justify-content: space-between;
  padding: 16px 18px; font-weight: 700; border-bottom: 1px solid var(--border); }
.sheet-body { padding: 8px 18px 4px; }
.setting-row { display: flex; align-items: center; justify-content: space-between;
  gap: 12px; padding: 14px 0; border-bottom: 1px solid var(--border); }
.setting-row:last-of-type { border-bottom: none; }
.setting-row > span { color: var(--text-soft); font-size: 14px; }
.seg { display: inline-flex; background: var(--bg-elev); border-radius: 9px; padding: 3px; gap: 2px; }
.seg button { border: none; background: transparent; color: var(--text-soft);
  padding: 7px 12px; border-radius: 7px; font-size: 13px; }
.seg button.active { background: var(--bg); color: var(--text); font-weight: 600;
  box-shadow: 0 1px 3px rgba(0,0,0,.12); }
.sheet-hint { color: var(--text-faint); font-size: 12px; padding: 4px 0 8px; }

/* 加载进度条 */
.loading-bar { position: fixed; top: calc(var(--topbar-h) + var(--safe-t)); left: 0; height: 3px;
  background: var(--accent); width: 0; z-index: 70; transition: width .2s; }

/* Toast 提示 */
#toast { position: fixed; left: 50%; bottom: calc(28px + var(--safe-b)); transform: translateX(-50%);
  background: rgba(20,22,28,.92); color: #fff; padding: 10px 16px; border-radius: 10px; font-size: 14px;
  z-index: 80; max-width: 84vw; text-align: center; opacity: 0; transition: opacity .25s; pointer-events: none; }

/* 键盘聚焦可见 */
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 6px; }

/* #标签 药丸 */
.doc .tag { display: inline-block; background: var(--accent-soft); color: var(--accent);
  font-size: .82em; padding: .05em .5em; border-radius: 999px; line-height: 1.5; white-space: nowrap; }

/* frontmatter 属性块 */
.doc .properties { margin: 0 0 1.4em; border: 1px solid var(--border); border-radius: 10px;
  background: var(--bg-elev); padding: 4px 12px; font-size: .9em; }
.doc .properties > summary { cursor: pointer; color: var(--text-soft); padding: 6px 0; list-style: none; font-weight: 600; }
.doc .properties > summary::-webkit-details-marker { display: none; }
.doc .properties > summary::before { content: '▸ '; color: var(--text-faint); }
.doc .properties[open] > summary::before { content: '▾ '; }
.doc .properties dl { display: grid; grid-template-columns: max-content 1fr; gap: 4px 14px; margin: 6px 0 10px; }
.doc .properties dt { color: var(--text-faint); }
.doc .properties dd { margin: 0; color: var(--text-soft); word-break: break-word; }

/* 嵌入笔记（![[note]] 转写） */
.doc .embed-note-resolved { border: 1px solid var(--border); border-left: 3px solid var(--accent);
  border-radius: 8px; margin: 1em 0; overflow: hidden; background: color-mix(in srgb, var(--accent-soft) 45%, transparent); }
.doc .embed-note-title { padding: 8px 14px; border-bottom: 1px solid var(--border); font-size: .9em; }
.doc .embed-note-title a { color: var(--text-soft); font-weight: 600; }
.doc .embed-note-body { padding: 4px 14px; }
.doc .embed-note-body > :first-child { margin-top: .4em; }
.doc .embed-note-body > :last-child { margin-bottom: .4em; }

/* 文件树筛选结果里的目录提示 */
.tree-dir { color: var(--text-faint); font-size: .85em; }

/* 安装到桌面浮条（固定底部，不挤压布局） */
.install-bar {
  position: fixed; left: 10px; right: 10px; bottom: calc(12px + var(--safe-b)); z-index: 55;
  display: flex; align-items: center; gap: 8px; padding: 10px 12px;
  background: var(--bg-elev); border: 1px solid var(--border); border-radius: 12px;
  box-shadow: 0 6px 24px rgba(0,0,0,.18); font-size: 13.5px; color: var(--text);
}
.install-bar[hidden] { display: none; }   /* 修：display:flex 会盖掉 hidden 属性，必须显式隐藏 */
@media (min-width: 760px) { .install-bar { left: auto; right: 16px; bottom: 16px; max-width: 420px; } }
.install-bar span { flex: 1; min-width: 0; }
.install-go { background: var(--accent); color: #fff; border: none; border-radius: 8px;
  padding: 7px 16px; font-weight: 600; font-size: 13.5px; white-space: nowrap; }
.install-x { background: transparent; border: none; color: var(--text-faint); font-size: 15px; width: 30px; height: 30px; }
.install-steps p { margin: .55em 0; color: var(--text-soft); font-size: 14px; line-height: 1.65; }
.install-steps b { color: var(--text); }
.install-steps .install-fallback { margin-top: 1em; padding-top: .8em; border-top: 1px solid var(--border); color: var(--text-faint); }

/* 宽屏：空状态按钮并排，不再显得孤立 */
@media (min-width: 760px) {
  .empty { margin-top: 12vh; max-width: 540px; }
  .empty-actions { flex-direction: row; max-width: none; justify-content: center; }
  .empty-actions button { flex: 0 1 auto; }
}
