/* ============ 联图 Nexus · styles ============ */
:root {
  --bg: #f6f4f0;
  --canvas: #f2efe9;
  --surface: #fffdfa;
  --surface-2: #f2efe9;
  --ink: #1b1917;
  --ink-2: #57514a;
  --ink-3: #8e867c;
  --line: #e3ded4;
  --line-soft: #ece7de;
  --accent: #b4471f;
  --accent-ink: #ffffff;
  --accent-soft: #fbeee7;
  --edge: #b3aa9c;
  --edge-active: #b4471f;
  --shadow-s: 0 1px 2px rgba(28, 25, 23, .06);
  --shadow-m: 0 2px 6px rgba(28, 25, 23, .07), 0 14px 34px -18px rgba(28, 25, 23, .35);
  --radius: 12px;
  --panel-w: 34vw;
  --sans: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  --serif: "Iowan Old Style", "Palatino Linotype", "Songti SC", "Source Han Serif SC", Georgia, serif;
  --mono: "SF Mono", ui-monospace, "JetBrains Mono", Menlo, Consolas, monospace;
  color-scheme: light;
}

html[data-theme="dark"] {
  --bg: #0c0e11;
  --canvas: #101318;
  --surface: #15181d;
  --surface-2: #1b1f25;
  --ink: #eae6e0;
  --ink-2: #a8a29a;
  --ink-3: #716c66;
  --line: #262b32;
  --line-soft: #1e232a;
  --accent: #ff7a45;
  --accent-ink: #1a0d06;
  --accent-soft: #2a1a13;
  --edge: #4d545d;
  --edge-active: #ff7a45;
  --shadow-s: 0 1px 2px rgba(0, 0, 0, .4);
  --shadow-m: 0 2px 8px rgba(0, 0, 0, .5), 0 18px 40px -20px rgba(0, 0, 0, .8);
  color-scheme: dark;
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  font-family: var(--sans);
  background: var(--bg);
  color: var(--ink);
  font-size: 14px;
  line-height: 1.5;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }
input, textarea { font: inherit; color: inherit; }
svg { fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
::selection { background: var(--accent); color: var(--accent-ink); }

#app { display: flex; flex-direction: column; height: 100dvh; }

/* ============ 顶栏 ============ */
#topbar {
  position: relative;
  display: flex;
  align-items: center;
  gap: 18px;
  height: 56px;
  padding: 0 14px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  z-index: 30;
  flex: 0 0 auto;
}

.brand { display: flex; align-items: center; gap: 9px; user-select: none; }
.brand-mark { width: 26px; height: 26px; color: var(--accent); stroke: none; }
.brand-txt { display: flex; align-items: baseline; gap: 6px; }
.brand-txt b { font-family: var(--serif); font-size: 17px; letter-spacing: .08em; font-weight: 600; }
.brand-txt span { font-size: 10.5px; letter-spacing: .18em; text-transform: uppercase; color: var(--ink-3); }

.stats { display: flex; align-items: center; gap: 14px; font-size: 12px; color: var(--ink-3); }
.stats b { color: var(--ink); font-variant-numeric: tabular-nums; font-weight: 600; }
.stats > span { display: flex; align-items: center; gap: 5px; }
.dot-i { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); display: inline-block; }
.dot-i.line { width: 12px; height: 2px; border-radius: 1px; background: var(--ink-3); }
.stat-sync {
  padding: 2px 8px; border-radius: 999px; font-size: 11px;
  background: var(--surface-2); color: var(--ink-2); transition: .2s;
}
.stat-sync[data-state="saving"] { background: var(--accent-soft); color: var(--accent); }
.stat-sync[data-state="error"] { background: #fdeaea; color: #b42318; }
html[data-theme="dark"] .stat-sync[data-state="error"] { background: #3a1a17; color: #ff8a80; }

.tools { margin-left: auto; display: flex; align-items: center; gap: 2px; }
.tbtn {
  width: 34px; height: 34px; border-radius: 9px; display: grid; place-items: center;
  color: var(--ink-2); transition: background .16s, color .16s;
}
.tbtn svg { width: 18px; height: 18px; }
.tbtn:hover { background: var(--surface-2); color: var(--ink); }
.tbtn.on { background: var(--accent); color: var(--accent-ink); }
html[data-theme="light"] .tbtn.on { color: #fff; }

.searchbox {
  position: absolute; left: 50%; transform: translateX(-50%);
  display: flex; align-items: center; gap: 8px;
  width: min(460px, 46vw); padding: 7px 12px;
  background: var(--surface-2); border: 1px solid var(--line);
  border-radius: 10px; box-shadow: var(--shadow-s);
}
.searchbox svg { width: 15px; height: 15px; color: var(--ink-3); }
.searchbox input { flex: 1; background: none; border: 0; outline: 0; font-size: 13px; }
.search-hint { font-size: 10.5px; color: var(--ink-3); border: 1px solid var(--line); padding: 1px 5px; border-radius: 4px; }

.menu {
  position: absolute; top: 50px; right: 12px;
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 10px; box-shadow: var(--shadow-m); padding: 5px; min-width: 172px; z-index: 40;
}
.menu button { display: block; width: 100%; text-align: left; padding: 8px 11px; border-radius: 6px; font-size: 13px; color: var(--ink-2); }
.menu button:hover { background: var(--surface-2); color: var(--ink); }

/* ============ 舞台 ============ */
#stage { flex: 1; display: flex; min-height: 0; position: relative; }

#canvas-wrap {
  position: relative; flex: 1 1 auto; min-width: 0; overflow: hidden;
  background:
    radial-gradient(ellipse 70% 60% at 46% 42%, color-mix(in srgb, var(--accent) 5%, transparent), transparent 70%),
    var(--canvas);
}
html[data-theme="dark"] #canvas-wrap {
  background:
    radial-gradient(ellipse 70% 60% at 46% 42%, color-mix(in srgb, var(--accent) 9%, transparent), transparent 72%),
    var(--canvas);
}
#canvas { width: 100%; height: 100%; display: block; cursor: grab; touch-action: none; }
#canvas.dragging { cursor: grabbing; }

/* 背景网点 */
.canvas-dots { pointer-events: none; }

/* 边 */
.edge-hit { stroke: transparent; stroke-width: 16; fill: none; cursor: pointer; }
.edge-line {
  fill: none; stroke: var(--edge); stroke-width: 1.5; stroke-linecap: round;
  transition: stroke .18s, stroke-width .18s, opacity .18s;
}
.edge-arrow { fill: var(--edge); stroke: none; transition: fill .18s, opacity .18s; }
.edge-label {
  font-size: 10.5px; fill: var(--ink-3); text-anchor: middle;
  paint-order: stroke; stroke: var(--canvas); stroke-width: 3.5px; stroke-linejoin: round;
  pointer-events: none; transition: opacity .18s;
}
.edge-group.hot .edge-line { stroke: var(--edge-active); stroke-width: 2.1; }
.edge-group.hot .edge-arrow { fill: var(--edge-active); }
.edge-group.hot .edge-label { fill: var(--accent); }
.edge-group.dim { opacity: .22; }
.edge-group.dashed .edge-line { stroke-dasharray: 6 5; }
.edge-group.sel .edge-line { stroke: var(--accent); stroke-width: 2.4; }

/* 节点 */
.node { cursor: pointer; }
.node .halo { fill: var(--accent); opacity: 0; transition: opacity .2s; }
.node .disc { stroke: var(--canvas); stroke-width: 2.5; transition: transform .16s ease, opacity .2s; }
.node .ring { fill: none; stroke: var(--accent); stroke-width: 1.4; opacity: 0; transition: opacity .2s; }
.node .lbl {
  font-size: 12.5px; font-weight: 550; fill: var(--ink); text-anchor: middle;
  paint-order: stroke; stroke: var(--canvas); stroke-width: 4px; stroke-linejoin: round;
  pointer-events: none; transition: opacity .2s;
}
.node:hover .disc { transform: scale(1.06); }
.node:hover .halo { opacity: .1; }
.node.sel .halo { opacity: .16; }
.node.sel .ring { opacity: .85; }
.node.dim { opacity: .32; }
.node.dim .lbl { opacity: .5; }
.node.hit-anim .halo { animation: pulse 1.4s ease-out 2; }
@keyframes pulse {
  0% { opacity: .45; }
  100% { opacity: 0; }
}
.node.pulse .ring { animation: ringpulse 1.6s ease-out infinite; transform-origin: center; }
@keyframes ringpulse {
  0% { opacity: .8; r: 0; }
  100% { opacity: 0; }
}
.node.born .disc { animation: born .34s cubic-bezier(.2, 1.5, .5, 1); }
@keyframes born { from { transform: scale(.1); } to { transform: scale(1); } }

.canvas-hint {
  position: absolute; left: 14px; bottom: 12px;
  font-size: 11.5px; color: var(--ink-3); pointer-events: none;
  background: color-mix(in srgb, var(--surface) 72%, transparent);
  padding: 5px 10px; border-radius: 8px; border: 1px solid var(--line-soft);
  backdrop-filter: blur(6px);
}
.canvas-hint kbd {
  font-family: var(--mono); font-size: 10.5px; padding: 1px 4px;
  border: 1px solid var(--line); border-radius: 4px; background: var(--surface);
  color: var(--ink-2);
}
.link-tip {
  position: absolute; left: 50%; top: 14px; transform: translateX(-50%);
  background: var(--accent); color: #fff; font-size: 12px; padding: 6px 14px;
  border-radius: 999px; box-shadow: var(--shadow-m); z-index: 5;
}

.zoombar {
  position: absolute; right: 14px; bottom: 14px;
  display: flex; flex-direction: column; align-items: center;
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 10px; box-shadow: var(--shadow-s); overflow: hidden;
}
.zoombar button { width: 32px; height: 30px; color: var(--ink-2); font-size: 15px; }
.zoombar button:hover { background: var(--surface-2); color: var(--ink); }
.zoom-pct { font-size: 9.5px; color: var(--ink-3); padding: 3px 0 5px; font-variant-numeric: tabular-nums; }

.ctxmenu {
  position: absolute; z-index: 25; min-width: 150px; padding: 5px;
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 10px; box-shadow: var(--shadow-m);
}
.ctxmenu button { display: block; width: 100%; text-align: left; padding: 7px 10px; border-radius: 6px; font-size: 12.5px; color: var(--ink-2); }
.ctxmenu button:hover { background: var(--surface-2); color: var(--ink); }
.ctxmenu button.danger:hover { background: #fdeaea; color: #b42318; }
html[data-theme="dark"] .ctxmenu button.danger:hover { background: #3a1a17; color: #ff8a80; }

.splitter {
  flex: 0 0 5px; cursor: col-resize; background: var(--line-soft);
  transition: background .15s; position: relative; z-index: 12;
}
.splitter:hover, .splitter.drag { background: var(--accent); }

/* ============ 右侧面板 ============ */
#panel {
  flex: 0 0 var(--panel-w);
  width: var(--panel-w);
  min-width: 320px; max-width: 78vw;
  background: var(--surface);
  border-left: 1px solid var(--line);
  display: flex; flex-direction: column;
  position: relative; z-index: 10;
  animation: slidein .22s ease;
}
@keyframes slidein { from { transform: translateX(14px); opacity: .4; } to { transform: none; opacity: 1; } }

.panel-head {
  display: flex; align-items: flex-start; gap: 10px;
  padding: 14px 14px 10px; border-bottom: 1px solid var(--line-soft);
  flex: 0 0 auto;
}
.panel-title-wrap { flex: 1; min-width: 0; }
.panel-title {
  width: 100%; border: 0; background: none; outline: 0;
  font-family: var(--serif); font-size: 19px; font-weight: 600; letter-spacing: .01em;
  padding: 2px 4px; margin-left: -4px; border-radius: 6px; color: var(--ink);
}
.panel-title:hover { background: var(--surface-2); }
.panel-title:focus { background: var(--surface-2); box-shadow: inset 0 0 0 1px var(--line); }
.panel-meta { font-size: 11.5px; color: var(--ink-3); padding: 2px 0 0 0; }
.panel-actions { display: flex; gap: 4px; flex: 0 0 auto; }
.pbtn {
  display: inline-flex; align-items: center; gap: 5px; height: 30px; padding: 0 10px;
  border: 1px solid var(--line); border-radius: 8px; font-size: 12px; color: var(--ink-2);
  background: var(--surface); transition: .16s;
}
.pbtn svg { width: 14px; height: 14px; }
.pbtn:hover { background: var(--surface-2); color: var(--ink); }
.pbtn.icon-only { padding: 0; width: 30px; justify-content: center; }
.pbtn.primary { background: var(--accent); color: #fff; border-color: transparent; }
.pbtn.on { background: var(--accent-soft); color: var(--accent); border-color: color-mix(in srgb, var(--accent) 35%, transparent); }

.md-toolbar {
  display: flex; align-items: center; gap: 2px; flex-wrap: wrap;
  padding: 6px 10px; border-bottom: 1px solid var(--line-soft); background: var(--surface);
  flex: 0 0 auto;
}
.md-toolbar button {
  min-width: 28px; height: 27px; padding: 0 6px; border-radius: 6px;
  font-size: 12px; color: var(--ink-2); display: inline-flex; align-items: center; justify-content: center; gap: 4px;
}
.md-toolbar button:hover { background: var(--surface-2); color: var(--ink); }
.md-toolbar button svg { width: 15px; height: 15px; }
.md-toolbar .tb-sep { width: 1px; height: 16px; background: var(--line); margin: 0 4px; }
.md-toolbar .tb-right { margin-left: auto; font-size: 10.5px; color: var(--ink-3); padding-right: 4px; font-variant-numeric: tabular-nums; }
.tb-label { font-size: 11px; }

.panel-body { flex: 1; min-height: 0; overflow: auto; position: relative; scrollbar-width: thin; }

.md-edit {
  display: block; width: 100%; min-height: 100%; height: 100%;
  border: 0; outline: 0; resize: none; padding: 18px 18px 40px;
  font-family: var(--mono); font-size: 13px; line-height: 1.72;
  background: var(--surface); color: var(--ink); tab-size: 2;
}

.panel-foot {
  flex: 0 0 auto; display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 8px 12px; border-top: 1px solid var(--line-soft); background: var(--surface);
  font-size: 11.5px; color: var(--ink-3);
}
.upload-hint { display: flex; align-items: center; gap: 6px; }
.upload-hint svg { width: 13px; height: 13px; }
.upload-hint a { color: var(--accent); text-decoration: none; border-bottom: 1px solid color-mix(in srgb, var(--accent) 40%, transparent); }
.save-state { padding: 2px 8px; border-radius: 999px; background: var(--surface-2); }
.save-state[data-state="saving"] { color: var(--accent); }
.save-state[data-state="error"] { color: #b42318; }

.drop-mask {
  position: absolute; inset: 0; z-index: 20; display: none;
  place-items: center; background: color-mix(in srgb, var(--accent) 12%, var(--surface));
  border: 2px dashed var(--accent); border-radius: 0;
  font-size: 13px; color: var(--accent); font-weight: 600;
}
.drop-mask.show { display: grid; }

/* ============ Markdown 排版 ============ */
.markdown {
  padding: 20px 20px 60px;
  font-size: 14.5px; line-height: 1.78; color: var(--ink-2);
  word-wrap: break-word;
}
.markdown > *:first-child { margin-top: 0 !important; }
.markdown h1, .markdown h2, .markdown h3, .markdown h4, .markdown h5, .markdown h6 {
  color: var(--ink); font-family: var(--serif); font-weight: 600;
  line-height: 1.32; margin: 1.5em 0 .55em; letter-spacing: .01em;
}
.markdown h1 { font-size: 1.62em; }
.markdown h2 { font-size: 1.3em; padding-bottom: .3em; border-bottom: 1px solid var(--line-soft); }
.markdown h3 { font-size: 1.1em; }
.markdown h4 { font-size: 1em; }
.markdown h5, .markdown h6 { font-size: .92em; color: var(--ink-3); text-transform: uppercase; letter-spacing: .06em; }
.markdown p { margin: .72em 0; }
.markdown strong { color: var(--ink); font-weight: 650; }
.markdown em { font-style: italic; }
.markdown a { color: var(--accent); text-decoration: none; border-bottom: 1px solid color-mix(in srgb, var(--accent) 35%, transparent); }
.markdown a:hover { border-bottom-color: var(--accent); }
.markdown ul, .markdown ol { margin: .6em 0; padding-left: 1.4em; }
.markdown li { margin: .28em 0; }
.markdown li::marker { color: var(--ink-3); }
.markdown ul li.task-list-item { list-style: none; margin-left: -1.25em; }
.markdown .task-list-item-checkbox { margin-right: .5em; accent-color: var(--accent); width: 13px; height: 13px; }
.markdown blockquote {
  margin: .9em 0; padding: .55em 1em; border-left: 2.5px solid var(--accent);
  background: var(--accent-soft); border-radius: 0 8px 8px 0; color: var(--ink-2);
}
.markdown blockquote p { margin: .25em 0; }
.markdown code {
  font-family: var(--mono); font-size: .875em; padding: .14em .38em;
  background: var(--surface-2); border: 1px solid var(--line-soft);
  border-radius: 5px; color: var(--ink);
}
.markdown pre {
  margin: .9em 0; padding: 13px 15px; border-radius: 10px;
  background: var(--surface-2); border: 1px solid var(--line-soft);
  overflow: auto; line-height: 1.62;
}
.markdown pre code {
  background: none; border: 0; padding: 0; font-size: 12.5px; color: var(--ink);
}
html[data-theme="dark"] .markdown pre { background: #0f1115; border-color: #232833; }
.markdown table {
  width: 100%; border-collapse: collapse; margin: .95em 0; font-size: .93em;
  display: block; overflow-x: auto;
}
.markdown th, .markdown td { padding: 7px 11px; border: 1px solid var(--line); text-align: left; }
.markdown th { background: var(--surface-2); color: var(--ink); font-weight: 600; white-space: nowrap; }
.markdown tr:nth-child(even) td { background: color-mix(in srgb, var(--surface-2) 45%, transparent); }
.markdown hr { border: 0; border-top: 1px solid var(--line); margin: 1.6em 0; }
.markdown img {
  max-width: 100%; height: auto; border-radius: 9px; border: 1px solid var(--line);
  margin: .6em 0; display: block; cursor: zoom-in; background: var(--surface-2);
}
.markdown .footnotes { margin-top: 2em; padding-top: 1em; border-top: 1px solid var(--line); font-size: .86em; color: var(--ink-3); }
.markdown .footnotes hr { display: none; }
.markdown dl dt { font-weight: 600; color: var(--ink); margin-top: .6em; }
.markdown dl dd { margin-left: 1.2em; color: var(--ink-2); }
.markdown abbr { border-bottom: 1px dotted var(--ink-3); cursor: help; }
.markdown mark { background: color-mix(in srgb, var(--accent) 22%, transparent); color: var(--ink); padding: 0 2px; border-radius: 3px; }
.markdown .katex { font-size: 1.02em; }
.markdown .katex-display { overflow-x: auto; overflow-y: hidden; padding: .3em 0; }
.markdown .header-anchor { display: none; }
.markdown .empty-hint { color: var(--ink-3); font-size: 13px; }

/* 图片放大 */
.lightbox {
  position: fixed; inset: 0; z-index: 200; display: grid; place-items: center;
  background: rgba(12, 10, 8, .82); backdrop-filter: blur(3px); padding: 4vmin;
  animation: fadein .18s ease;
}
.lightbox img { max-width: 100%; max-height: 100%; border-radius: 10px; box-shadow: 0 24px 70px rgba(0, 0, 0, .5); }
@keyframes fadein { from { opacity: 0; } to { opacity: 1; } }

.toast {
  position: fixed; left: 50%; bottom: 26px; transform: translate(-50%, 12px);
  background: var(--ink); color: var(--bg); font-size: 12.5px;
  padding: 9px 16px; border-radius: 999px; z-index: 300;
  opacity: 0; pointer-events: none; transition: .24s;
  box-shadow: var(--shadow-m);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

dialog#help-dialog {
  border: 1px solid var(--line); border-radius: 14px; padding: 22px 24px;
  background: var(--surface); color: var(--ink-2); max-width: 520px;
  box-shadow: var(--shadow-m); font-size: 13.5px; line-height: 1.8;
}
dialog#help-dialog::backdrop { background: rgba(12, 10, 8, .45); backdrop-filter: blur(2px); }
dialog#help-dialog h3 { font-family: var(--serif); font-size: 19px; color: var(--ink); margin-bottom: 10px; }
dialog#help-dialog ul { padding-left: 1.1em; margin-bottom: 16px; }
dialog#help-dialog li { margin: .4em 0; }
dialog#help-dialog b { color: var(--ink); }

@media (max-width: 900px) {
  :root { --panel-w: 100vw; }
  #panel {
    position: absolute; inset: 0 0 0 auto; width: 100vw; max-width: 100vw;
    box-shadow: -12px 0 40px rgba(0, 0, 0, .18);
  }
  .splitter { display: none !important; }
  .stats { display: none; }
  .canvas-hint { display: none; }
  .brand-txt span { display: none; }
}

/* 背景网点（随缩放平移） */
.grid-bg {
  position: absolute; inset: 0; pointer-events: none; z-index: 0;
  background-image: radial-gradient(circle, var(--line) 1px, transparent 1.2px);
  background-size: 26px 26px;
  opacity: .55;
}
html[data-theme="dark"] .grid-bg { opacity: .38; }
#canvas { position: relative; z-index: 1; }
.temp-edge, .temp-edge path { pointer-events: none; }
.node.linking .disc { stroke: var(--accent); stroke-width: 3; }
.node .disc { transform-box: fill-box; transform-origin: center; }
.node .ring { transform-box: fill-box; transform-origin: center; }

/* 双链 */
.markdown a[href^="#n="] { font-weight: 550; }
.markdown a[href^="#missing="] {
  color: var(--ink-3); border-bottom-style: dashed;
  border-bottom-color: var(--line);
}
.markdown a[href^="#missing="]:hover { color: var(--accent); border-bottom-color: var(--accent); }

/* 边标签更克制 */
.edge-label { font-size: 10px; }

/* 公式块 */
.markdown div[data-katex] { text-align: center; overflow-x: auto; overflow-y: hidden; margin: .9em 0; }
.markdown div[data-katex] .katex { font-size: 1.06em; }

.node .halo { opacity: .1; }
.node.sel .halo { opacity: .14; }
.node.sel .ring { opacity: 1; stroke-width: 1.8; }
.root.zoomed-out .edge-label { opacity: 0; }
