/* KnowledgeWiki 管理台（P1）— 移动优先 */
:root {
  --bg: #f7f6f3; --panel: #ffffff; --line: #e9e9e7; --ink: #37352f; --muted: #787774;
  --brand: #b45309; --brand-dark: #8a3f06; --ok: #2e7d32; --warn: #b45309; --err: #d4442e;
  --line-strong: #dfdfde; --surface-muted: #f1f1ef; --shadow-sm: 0 1px 2px rgb(15 15 15 / 4%); --shadow-md: 0 8px 24px rgb(15 15 15 / 8%);
  /* 顶栏基准高度；全面屏安全区（刘海/挖孔）自动补偿，统一供 layout/侧栏/预览用 */
  --tb-h: 53px;
}
@supports (top: max(0px, env(safe-area-inset-top))) {
  :root { --tb-h: calc(53px + env(safe-area-inset-top)); }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body { margin: 0; font-family: system-ui, sans-serif; background: var(--bg); color: var(--ink); }
button { font: inherit; padding: 8px 12px; border-radius: 8px; border: 1px solid var(--line); background: var(--panel); color: var(--ink); cursor: pointer; min-height: 36px; }
button.primary { background: var(--brand); color: #fff; border: 0; }
button.ghost { background: transparent; }
button:disabled { opacity: .5; cursor: default; }
.spacer { flex: 1; }
.muted { color: var(--muted); font-size: 13px; }
.pad { padding: 12px; }

/* ---- 顶栏 ---- */
#topbar { position: sticky; top: 0; z-index: 30; display: flex; align-items: center; gap: 8px; padding: 8px 10px; padding-top: max(8px, env(safe-area-inset-top)); background: var(--panel); border-bottom: 1px solid var(--line); }
#tb-title { font-weight: 700; }
.badge { font-size: 12px; padding: 3px 8px; border-radius: 999px; background: #eee; color: var(--muted); }
.badge.dirty { background: #fff3e0; color: var(--warn); }
.badge.clean { background: #e8f5e9; color: var(--ok); }
#more-menu { position: absolute; right: 10px; top: 52px; display: flex; flex-direction: column; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 6px; box-shadow: 0 6px 18px rgba(0,0,0,.12); min-width: 160px; }
#more-menu button { text-align: left; border: 0; background: transparent; }

/* ---- 布局 ---- */
#layout { height: calc(100vh - var(--tb-h)); height: calc(100dvh - var(--tb-h)); display: flex; overflow: hidden; }
#sidebar { position: fixed; inset: var(--tb-h) auto 0 0; width: min(78vw, 300px); background: var(--panel); border-right: 1px solid var(--line); transform: translateX(-105%); transition: transform .18s ease; z-index: 25; display: flex; flex-direction: column; }
#sidebar.open { transform: translateX(0); }
.side-head { display: flex; align-items: center; justify-content: space-between; padding: 10px; border-bottom: 1px solid var(--line); }
#tree { overflow: auto; padding: 6px; flex: 1; }
#tree details { margin: 1px 0; }
#tree summary { cursor: pointer; padding: 6px 8px; border-radius: 6px; font-weight: 600; list-style: none; }
#tree summary::before { content: "▸ "; color: var(--muted); }
#tree details[open] > summary::before { content: "▾ "; }
#tree summary:hover, .file-row:hover { background: #eef3f9; }
.file-row { display: flex; align-items: center; gap: 6px; padding: 6px 8px 6px 22px; border-radius: 6px; cursor: pointer; font-size: 14px; }
.file-row.current { background: #e3edf8; font-weight: 600; }
.file-row .ext { color: var(--muted); font-size: 11px; }
#overlay { position: fixed; inset: var(--tb-h) 0 0 0; background: rgba(0,0,0,.28); z-index: 24; }

#main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
#toolbar { display: flex; gap: 4px; align-items: center; padding: 6px; background: var(--panel); border-bottom: 1px solid var(--line); overflow-x: auto; }
#toolbar button { min-height: 34px; padding: 4px 10px; white-space: nowrap; }

#editor-host, #editor-fallback { flex: 1; overflow: auto; background: var(--panel); }
#editor-fallback { width: 100%; border: 0; padding: 12px; font: 14px/1.6 ui-monospace, monospace; resize: none; outline: none; }
.cm-editor { height: 100%; }
.cm-scroller { font-family: ui-monospace, "Cascadia Mono", monospace; font-size: 14px; line-height: 1.6; }

#preview { position: fixed; inset: var(--tb-h) 0 0 0; background: var(--panel); z-index: 20; overflow: auto; }
/* 预览面板自身的关闭按钮：桌面分栏时 toolbar 右端的预览按钮会被预览层盖住，移动端全屏同理 */
#preview-close { position: fixed; top: calc(var(--tb-h) + 10px); right: 14px; z-index: 22; background: var(--panel); box-shadow: 0 2px 8px rgba(0,0,0,.18); }
#statusbar { display: flex; gap: 8px; align-items: center; padding: 5px 10px; border-top: 1px solid var(--line); background: var(--panel); font-size: 12px; }

/* ---- 桌面分栏 ---- */
@media (min-width: 1000px) {
  #sidebar { position: static; transform: none; width: 270px; }
  #overlay { display: none !important; }
  #btn-menu { display: none; }
  /* 分屏时 #main 真实收缩到左半：工具栏/编辑器/状态栏完整可见可交互，
     不被预览层遮挡；预览占据右半，两者无缝衔接（移动端仍全屏覆盖+关闭按钮） */
  body.preview-on #main { margin-right: 50%; }
  body.preview-on #preview { display: block; position: fixed; top: var(--tb-h); right: 0; bottom: 0; left: auto; width: 50%; border-left: 1px solid var(--line); z-index: 21; }
}

/* ---- 预览内容 ---- */
#preview-inner, .md-body { padding: 16px; max-width: 860px; margin: 0 auto; line-height: 1.75; overflow-wrap: break-word; }
.md-body pre { background: #f2f2ee; padding: 12px; border-radius: 8px; overflow: auto; font-size: 13px; }
.md-body code { background: #f2f2ee; padding: 1px 5px; border-radius: 4px; font-size: .92em; }
.md-body pre code { background: transparent; padding: 0; }
.md-body img { max-width: 100%; }
.md-body blockquote { border-left: 3px solid var(--brand); margin: 8px 0; padding: 2px 12px; color: var(--muted); background: #f7f9fc; }
.md-body .wikilink { color: var(--brand); background: #eef4fb; border-radius: 4px; padding: 0 3px; }
.md-body .circuit-svg svg { max-width: 100%; height: auto; }
.md-body .render-note { font-size: 12px; color: var(--warn); }

/* ---- 弹层 ---- */
#modal { position: fixed; inset: 0; z-index: 40; background: rgba(0,0,0,.35); display: flex; align-items: center; justify-content: center; padding: 10px; }
#modal-card { background: var(--panel); border-radius: 12px; padding: 14px; max-width: 640px; width: 100%; max-height: 82vh; max-height: 86dvh; overflow: auto; }
#toast { position: fixed; left: 50%; bottom: calc(28px + env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 50; background: #333; color: #fff; padding: 10px 16px; border-radius: 10px; font-size: 14px; max-width: 86vw; box-shadow: 0 4px 14px rgba(0,0,0,.25); }
#toast.err { background: var(--err); }

/* ---- P3：图库缩略图（预览 SVG 为透明底深色线条，垫白底才可见） ---- */
.gal-thumb { width: 96px; height: 56px; object-fit: contain; background: #fff; border: 1px solid var(--line); border-radius: 6px; flex: none; }

/* ---- P3：双链搜索弹层候选项 ---- */
.wl-item { display: block; width: 100%; text-align: left; padding: 8px 10px; margin: 2px 0; border: 1px solid var(--line); border-radius: 8px; background: var(--panel); cursor: pointer; }
.wl-item:hover { background: #eef3f9; border-color: var(--brand); }

/* ---- P3：触屏细节：工具栏/顶栏按钮加大触控目标，文件行提高可点高度（保留左缩进） ---- */
#toolbar button { min-height: 38px; }
.file-row { padding-top: 8px; padding-bottom: 8px; }
@media (max-width: 999px) {
  #topbar button { padding: 6px 10px; }
  #toolbar { padding: 6px 4px; }
}

/* ---- P4：改动审查（Q7-B/Q8-C/Q9-B）：diff 着色 + 来源徽章 + 新旧 SVG 并排 ---- */
.rv-file { border: 1px solid var(--line); border-radius: 8px; margin: 8px 0; background: var(--panel); }
.rv-file summary { display: flex; align-items: center; gap: 8px; padding: 8px 10px; cursor: pointer; flex-wrap: wrap; }
.rv-file summary code { word-break: break-all; }
.rv-file[open] summary { border-bottom: 1px solid var(--line); }
.rv-st { font: 600 11px/1 system-ui, sans-serif; padding: 3px 7px; border-radius: 999px; white-space: nowrap; }
.rv-st.add { background: #e6f4ea; color: #137333; }
.rv-st.del { background: #fdecea; color: #c5221f; }
.rv-st.mod { background: #fef7e0; color: #b06000; }
.rv-who { font: 600 12px/1 system-ui, sans-serif; padding: 3px 8px; border-radius: 999px; white-space: nowrap; }
.rv-who.agent { background: #e8f0fe; color: #1a56b8; border: 1px solid #c6dafc; }
.rv-who.human { background: #f1f3f4; color: #5f6368; }
.rv-diff { margin: 0; padding: 10px; font: 12px/1.5 ui-monospace, Consolas, monospace; overflow: auto; white-space: pre; background: #fafafa; }
.dl-add { display: inline-block; width: 100%; background: #e6f4ea; color: #0d5c2f; }
.dl-del { display: inline-block; width: 100%; background: #fdecea; color: #a50e0e; text-decoration: line-through; }
.dl-hunk { display: inline-block; width: 100%; color: #8250df; }
.rv-trunc { color: #b06000; }
.rv-circuit { margin: 6px 0; padding-left: 22px; }
.rv-circuit li { margin: 2px 0; }
.rv-compare { display: flex; gap: 12px; flex-wrap: wrap; padding: 10px; }
.rv-svg { margin: 0; flex: 1 1 260px; max-width: 50%; text-align: center; }
.rv-svg figcaption { font: 600 12px/1.6 system-ui, sans-serif; color: var(--muted, #667); }
.rv-svg img { max-width: 100%; height: auto; border: 1px solid var(--line); border-radius: 6px; background: #fff; }
