/* ---------------- Tokens ---------------- */
:root {
  --accent: #2D6CDF;
  --accent-soft: rgba(45,108,223,0.10);
  --accent-soft-2: rgba(45,108,223,0.16);

  --bg: #ffffff;
  --bg-rail: #fbfbfa;
  --bg-elev: #ffffff;
  --bg-hover: #f1f1ef;
  --bg-active: var(--accent-soft);
  --bg-code: #f6f6f4;

  --tx: #32302c;
  --tx-strong: #1d1b16;
  --tx-muted: #8a877f;
  --tx-faint: #b3b0a8;

  --line: #ececea;
  --line-strong: #e0dfdb;

  --pill-bg: #f0f0ee;
  --pill-tx: #6f6c64;

  --green-bg:#e7f3ec; --green-tx:#1f7a4d;
  --amber-bg:#fbf0db; --amber-tx:#9a6a16;
  --red-bg:#fbe9e7;   --red-tx:#b3402f;

  --co-blue-bg:#eef3fc; --co-blue-br:#2D6CDF;
  --co-amber-bg:#fbf3e1; --co-amber-br:#d39a2a;
  --co-red-bg:#fbe9e7;   --co-red-br:#d35f4e;

  --font-sans: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  --font-mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;

  --rail-w: 264px;
  --right-w: 312px;
  --doc-w: 720px;
}
html[data-theme="dark"] {
  --accent: #5b92ff;
  --accent-soft: rgba(91,146,255,0.14);
  --accent-soft-2: rgba(91,146,255,0.22);

  --bg: #1a1a19;
  --bg-rail: #161615;
  --bg-elev: #1f1f1e;
  --bg-hover: #272725;
  --bg-active: var(--accent-soft);
  --bg-code: #232322;

  --tx: #d6d4cf;
  --tx-strong: #f1efe9;
  --tx-muted: #8c8a83;
  --tx-faint: #5e5c56;

  --line: #2b2b29;
  --line-strong: #353532;

  --pill-bg: #2a2a28;
  --pill-tx: #a6a39b;

  --green-bg:#16301f; --green-tx:#5cc189;
  --amber-bg:#312713; --amber-tx:#d9a948;
  --red-bg:#341d19;   --red-tx:#e08373;

  --co-blue-bg:#1c2638; --co-blue-br:#5b92ff;
  --co-amber-bg:#2e2514; --co-amber-br:#d39a2a;
  --co-red-bg:#321c18;   --co-red-br:#d35f4e;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  font-family: var(--font-sans);
  color: var(--tx);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  font-size: 15px;
  line-height: 1.6;
}
::selection { background: var(--accent-soft-2); }

.app {
  display: grid;
  grid-template-columns: var(--rail-w) 1fr var(--right-w);
  height: 100vh;
  overflow: hidden;
}

/* ---------------- Left rail ---------------- */
.rail {
  background: var(--bg-rail);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.rail-head { padding: 26px 22px 14px; }
.brand {
  display: flex; align-items: center; gap: 9px;
  font-weight: 650; font-size: 17px; color: var(--tx-strong);
  letter-spacing: -0.01em; margin: 0 0 16px;
}
.brand-mark {
  width: 22px; height: 22px; border-radius: 6px;
  background: var(--accent); color: #fff;
  display: grid; place-items: center; flex: none;
  font-size: 12px; font-weight: 700;
}
.conn-dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--tx-faint); margin-left: auto;
  transition: background .2s;
}
.conn-dot.on { background: #34c47b; box-shadow: 0 0 0 3px rgba(52,196,123,0.18); }
.conn-dot.off { background: #d35f4e; }

.search-wrap { position: relative; }
.search-wrap svg { position: absolute; left: 11px; top: 50%; transform: translateY(-50%); color: var(--tx-faint); }
#search {
  width: 100%; border: 1px solid var(--line-strong); background: var(--bg-elev);
  border-radius: 9px; padding: 9px 12px 9px 33px;
  font-family: inherit; font-size: 13.5px; color: var(--tx);
  outline: none; transition: border-color .15s, box-shadow .15s;
}
#search::placeholder { color: var(--tx-faint); }
#search:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.kbd { position: absolute; right: 10px; top: 50%; transform: translateY(-50%); font-size: 11px; color: var(--tx-faint); border: 1px solid var(--line-strong); border-radius: 5px; padding: 1px 6px; font-family: var(--font-mono); }

.rail-section { padding: 8px 12px 4px; }
.section-label {
  font-size: 11px; font-weight: 650; letter-spacing: .07em; text-transform: uppercase;
  color: var(--tx-muted); padding: 6px 10px;
}
.recent-section { padding: 0 6px 6px; }
.recent-row {
  display: flex; align-items: center; gap: 6px;
  width: 100%; text-align: left; border: 0; background: transparent;
  padding: 4px 8px; border-radius: 6px; cursor: pointer;
  font-family: inherit; font-size: 13px; color: var(--tx);
  line-height: 1.3;
}
.recent-row:hover { background: var(--bg-hover); }
.recent-row.active { background: var(--bg-active); color: var(--accent); font-weight: 550; }
.recent-row .recent-dot {
  width: 5px; height: 5px; border-radius: 50%;
  background: var(--tx-faint); flex: none;
}
.recent-row.fresh .recent-dot { background: #34c47b; }
.recent-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.recent-ago { color: var(--tx-faint); font-size: 11px; flex: none; }
.rail-section-sep { border-top: 1px solid var(--line); margin-top: 6px; padding-top: 10px; }

.explorer-scroll { flex: 1; min-height: 0; overflow-y: auto; padding: 0 6px 24px; }
#explorer-list { display: flex; flex-direction: column; }

/* Tree rows */
.tree-row {
  display: flex; align-items: center; gap: 4px;
  width: 100%; text-align: left; border: 0; background: transparent;
  padding: 3px 6px; border-radius: 6px; cursor: pointer;
  font-family: inherit; font-size: 13.5px; color: var(--tx);
  line-height: 1.35;
}
.tree-row:hover { background: var(--bg-hover); }
.tree-row.active { background: var(--bg-active); color: var(--accent); font-weight: 550; }
.tree-row.active .tree-ic { color: var(--accent); }
.tree-chev {
  width: 14px; height: 14px; flex: none; color: var(--tx-faint);
  transition: transform .14s; display: grid; place-items: center;
}
.tree-chev.open { transform: rotate(90deg); }
.tree-chev.file-spacer { visibility: hidden; }
.tree-ic { width: 14px; height: 14px; flex: none; color: var(--tx-faint); }
.tree-folder .tree-ic { color: var(--tx-muted); }
.tree-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.tree-children { display: flex; flex-direction: column; }
.tree-children.collapsed { display: none; }
.explorer-empty { padding: 12px 12px; color: var(--tx-faint); font-size: 13px; }

/* scrollbars */
.explorer-scroll::-webkit-scrollbar, #doc-scroll::-webkit-scrollbar, .right::-webkit-scrollbar { width: 9px; }
.explorer-scroll::-webkit-scrollbar-thumb, #doc-scroll::-webkit-scrollbar-thumb, .right::-webkit-scrollbar-thumb { background: var(--line-strong); border-radius: 8px; border: 2px solid transparent; background-clip: content-box; }
.explorer-scroll::-webkit-scrollbar-thumb:hover, #doc-scroll::-webkit-scrollbar-thumb:hover { background: var(--tx-faint); background-clip: content-box; }

/* ---------------- Center ---------------- */
.center {
  display: flex; flex-direction: column;
  min-width: 0; min-height: 0; height: 100%;
  background: var(--bg);
}
.topbar {
  height: 52px; min-height: 52px; flex: 0 0 52px;
  display: flex; align-items: center; gap: 10px;
  padding: 0 28px; border-bottom: 1px solid var(--line);
}
.crumbs { display: flex; align-items: center; gap: 8px; font-size: 13.5px; color: var(--tx-muted); min-width: 0; }
.crumb-btn { border: 0; background: transparent; color: var(--tx-muted); cursor: pointer; font-family: inherit; font-size: 13.5px; padding: 3px 6px; border-radius: 6px; }
.crumb-btn:hover { background: var(--bg-hover); color: var(--tx); }
#crumb-current { color: var(--tx-strong); font-weight: 550; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.crumb-sep { color: var(--tx-faint); }
.topbar-spacer { flex: 1; }
.icon-btn {
  width: 34px; height: 34px; border: 0; background: transparent; border-radius: 8px;
  display: grid; place-items: center; cursor: pointer; color: var(--tx-muted);
}
.icon-btn:hover { background: var(--bg-hover); color: var(--tx); }
.icon-btn.active { background: var(--accent-soft); color: var(--accent); }
html[data-theme="light"] .moon { display: block; }
html[data-theme="light"] .sun { display: none; }
html[data-theme="dark"] .moon { display: none; }
html[data-theme="dark"] .sun { display: block; }

#doc-scroll {
  flex: 1 1 auto;
  min-height: 0; height: 0;
  overflow-y: auto; overflow-x: hidden;
  position: relative;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}
#doc {
  max-width: var(--doc-w); margin: 0 auto; padding: 56px 32px 160px;
}

/* ---------------- Editor ---------------- */
#editor {
  display: block; width: calc(100% - 64px); max-width: var(--doc-w);
  margin: 32px auto 0; padding: 24px 28px 160px;
  background: var(--bg); color: var(--tx);
  font-family: var(--font-mono); font-size: 13.5px; line-height: 1.65;
  border: 1px solid var(--line); border-radius: 10px;
  outline: none; resize: none; min-height: 70vh;
}
#editor:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
#editor-bar {
  position: sticky; bottom: 0;
  display: flex; align-items: center; gap: 10px;
  background: var(--bg-elev); border-top: 1px solid var(--line);
  padding: 10px 28px;
}
#editor-bar button {
  border: 1px solid var(--line-strong); background: var(--bg-elev);
  color: var(--tx); padding: 6px 12px; border-radius: 8px;
  font-family: inherit; font-size: 13px; cursor: pointer;
}
#editor-bar #editor-save { background: var(--accent); color: #fff; border-color: var(--accent); }
#editor-bar #editor-save:hover { filter: brightness(1.05); }
#editor-bar button:hover { background: var(--bg-hover); }
#editor-status { color: var(--tx-muted); font-size: 13px; }
.kbd-hint { margin-left: auto; color: var(--tx-faint); font-size: 12px; }

/* ---------------- Document (Notion style) ---------------- */
#doc h1 {
  font-size: 38px; line-height: 1.18; font-weight: 700; letter-spacing: -0.02em;
  color: var(--tx-strong); margin: 0 0 4px; text-wrap: balance;
}
.doc-meta { display: flex; align-items: center; gap: 9px; color: var(--tx-muted); font-size: 13.5px; margin: 2px 0 14px; }
.doc-meta .dot { color: var(--tx-faint); }
.doc-tags { display: flex; flex-wrap: wrap; gap: 7px; margin: 0 0 8px; }
.tag { font-size: 12.5px; color: var(--accent); background: var(--accent-soft); padding: 2px 9px; border-radius: 20px; }

#doc h2 {
  font-size: 24px; font-weight: 650; letter-spacing: -0.01em; color: var(--tx-strong);
  margin: 38px 0 10px; padding-top: 4px;
}
#doc h3 { font-size: 18.5px; font-weight: 600; color: var(--tx-strong); margin: 28px 0 8px; }
#doc p { margin: 12px 0; color: var(--tx); }
#doc a:not(.wikilink) { color: var(--accent); text-decoration: none; }
#doc strong { color: var(--tx-strong); font-weight: 650; }

#doc ul, #doc ol { margin: 12px 0; padding-left: 26px; }
#doc li { margin: 5px 0; }
#doc li::marker { color: var(--tx-faint); }

#doc code {
  font-family: var(--font-mono); font-size: 0.86em; background: var(--bg-code);
  padding: 1.5px 6px; border-radius: 5px; color: var(--tx-strong);
  border: 1px solid var(--line);
}
#doc pre {
  background: var(--bg-code); border: 1px solid var(--line); border-radius: 10px;
  padding: 16px 18px; overflow-x: auto; margin: 16px 0;
}
#doc pre code { background: none; border: 0; padding: 0; font-size: 13px; line-height: 1.65; color: var(--tx); }

#doc blockquote {
  margin: 16px 0; padding: 4px 0 4px 18px; border-left: 3px solid var(--line-strong);
  color: var(--tx-muted); font-style: normal;
}

#doc hr { border: 0; border-top: 1px solid var(--line); margin: 30px 0; }

#doc table { width: 100%; border-collapse: collapse; margin: 18px 0; font-size: 14px; }
#doc th, #doc td { text-align: left; padding: 10px 14px; border: 1px solid var(--line); vertical-align: top; }
#doc th { background: var(--bg-rail); font-weight: 600; color: var(--tx-strong); font-size: 13px; }
#doc tbody tr:hover { background: var(--bg-hover); }

.wikilink {
  color: var(--accent); text-decoration: none; cursor: pointer;
  border-bottom: 1px solid var(--accent-soft-2); padding-bottom: 0.5px;
  transition: background .12s;
}
.wikilink:hover { background: var(--accent-soft); border-radius: 3px; }
.wikilink.broken { color: var(--tx-muted); border-bottom-style: dashed; cursor: default; }

/* ---------------- Right rail ---------------- */
.right {
  border-left: 1px solid var(--line); background: var(--bg-rail);
  overflow-y: auto; padding: 26px 22px 60px; min-height: 0;
}
.panel { margin-bottom: 30px; }
.panel-head {
  display: flex; align-items: center; gap: 7px; margin-bottom: 12px; cursor: pointer; user-select: none;
}
.panel-title { font-size: 13px; font-weight: 650; letter-spacing: .04em; text-transform: uppercase; color: var(--tx-muted); }
.panel-chev { width: 14px; height: 14px; color: var(--tx-faint); transition: transform .18s; margin-left: auto; }
.panel-chev.rot { transform: rotate(-90deg); }
.collapsible { overflow: hidden; transition: max-height .25s ease; max-height: 600px; }
.collapsible.collapsed { max-height: 0 !important; }

.graph-box {
  background: var(--bg-elev); border: 1px solid var(--line); border-radius: 12px;
  overflow: hidden;
}
#graph-svg { display: block; width: 100%; height: 230px; }
.g-edge { stroke: var(--line-strong); stroke-width: 1; }
.g-edge.hot { stroke: var(--accent); stroke-width: 1.5; stroke-opacity: .55; }
.g-node { fill: var(--tx-faint); cursor: pointer; transition: fill .15s, r .15s; }
.g-node.neigh { fill: var(--accent); fill-opacity: .65; }
.g-node.cur { fill: var(--accent); }
.g-node-g { cursor: pointer; }
.g-node-g .g-label {
  font-family: var(--font-sans); font-size: 8.5px; fill: var(--tx-faint);
  text-anchor: middle; opacity: 0; transition: opacity .15s; pointer-events: none;
}
.g-node-g .g-label.cur { opacity: 1; fill: var(--tx); font-weight: 600; }
.g-node-g.hover .g-label { opacity: 1; fill: var(--tx); }
.g-node-g.hover .g-node { fill: var(--accent); }

#toc { display: flex; flex-direction: column; gap: 1px; }
.toc-link {
  text-decoration: none; color: var(--tx-muted); font-size: 13px; line-height: 1.45;
  padding: 4px 10px; border-radius: 6px; border-left: 2px solid transparent;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.toc-link:hover { color: var(--tx); background: var(--bg-hover); }
.toc-h3 { padding-left: 22px; font-size: 12.5px; }
.toc-link.active { color: var(--accent); border-left-color: var(--accent); background: var(--accent-soft); font-weight: 550; }
.toc-empty, .bl-empty { color: var(--tx-faint); font-size: 13px; padding: 2px 10px; }

#backlinks { display: flex; flex-direction: column; gap: 6px; }
.backlink {
  text-align: left; border: 1px solid var(--line); background: var(--bg-elev);
  border-radius: 9px; padding: 9px 12px; cursor: pointer; font-family: inherit;
  font-size: 13px; color: var(--accent); transition: border-color .12s, background .12s;
}
.backlink:hover { border-color: var(--accent); background: var(--accent-soft); }

/* ---------------- Ask Claude pill + panel ---------------- */
#ask-pill {
  position: fixed; z-index: 50;
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--tx-strong); color: var(--bg);
  border: 0; border-radius: 999px; padding: 7px 14px;
  font-family: inherit; font-size: 13px; font-weight: 550;
  cursor: pointer; box-shadow: 0 6px 22px rgba(0,0,0,0.18);
  transition: transform .12s;
}
#ask-pill:hover { transform: translateY(-1px); }

#claude-panel {
  position: fixed; right: 18px; bottom: 18px; top: 64px;
  width: 380px; max-width: calc(100vw - 36px);
  background: var(--bg-elev); border: 1px solid var(--line);
  border-radius: 14px; box-shadow: 0 20px 60px rgba(0,0,0,0.18);
  display: flex; flex-direction: column;
  z-index: 60;
}
.cp-head {
  display: flex; align-items: center; padding: 14px 18px;
  border-bottom: 1px solid var(--line);
  font-weight: 650; color: var(--tx-strong);
}
.cp-head button {
  margin-left: auto; border: 0; background: transparent;
  color: var(--tx-muted); font-size: 22px; cursor: pointer;
}
.cp-sel {
  padding: 12px 18px; border-bottom: 1px solid var(--line); background: var(--bg-rail);
  max-height: 140px; overflow-y: auto;
}
.cp-sel-label { font-size: 11px; color: var(--tx-muted); letter-spacing: .05em; text-transform: uppercase; margin-bottom: 6px; }
#cp-sel-text { font-family: var(--font-mono); font-size: 12.5px; color: var(--tx); white-space: pre-wrap; }
#cp-question {
  margin: 14px 18px 0; padding: 10px 12px;
  border: 1px solid var(--line-strong); background: var(--bg); color: var(--tx);
  border-radius: 9px; font-family: inherit; font-size: 13.5px; resize: vertical;
  min-height: 70px; outline: none;
}
#cp-question:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
#cp-send {
  margin: 10px 18px; padding: 9px 14px; border: 0;
  background: var(--accent); color: #fff; border-radius: 9px;
  font-family: inherit; font-size: 13.5px; font-weight: 550; cursor: pointer;
}
#cp-send:hover { filter: brightness(1.05); }
#cp-send:disabled { opacity: .55; cursor: progress; }
#cp-answer {
  margin: 4px 18px 0; padding: 0 0 18px;
  font-size: 14px; color: var(--tx); white-space: pre-wrap;
  overflow-y: auto; flex: 1; min-height: 0;
}
#cp-patch {
  border-top: 1px solid var(--line); padding: 12px 18px 14px;
  background: var(--bg-rail);
}
.cp-patch-label { font-size: 11px; color: var(--tx-muted); letter-spacing: .05em; text-transform: uppercase; margin-bottom: 6px; }
#cp-patch-text {
  background: var(--bg); border: 1px solid var(--line); border-radius: 8px;
  padding: 10px 12px; font-family: var(--font-mono); font-size: 12.5px;
  color: var(--tx); margin: 0 0 10px; max-height: 160px; overflow-y: auto;
  white-space: pre-wrap;
}
.cp-actions { display: flex; gap: 8px; }
.cp-actions button {
  flex: 1; padding: 7px 10px; border-radius: 8px; font-family: inherit; font-size: 13px;
  border: 1px solid var(--line-strong); background: var(--bg-elev); color: var(--tx); cursor: pointer;
}
#cp-apply { background: var(--accent); color: #fff; border-color: var(--accent); }
#cp-apply:hover { filter: brightness(1.05); }
.cp-actions button:hover { background: var(--bg-hover); }

/* ---------------- Full-graph modal ---------------- */
.panel-icon-btn {
  margin-left: auto; width: 22px; height: 22px;
  border: 0; background: transparent; color: var(--tx-muted);
  border-radius: 5px; cursor: pointer; display: grid; place-items: center;
}
.panel-icon-btn:hover { background: var(--bg-hover); color: var(--tx); }
.panel-head .panel-chev { margin-left: 4px; }

#graph-modal {
  position: fixed; inset: 0; z-index: 80;
  background: var(--bg);
  display: flex; flex-direction: column;
}
.gm-head {
  flex: 0 0 52px; display: flex; align-items: center; gap: 12px;
  padding: 0 18px; border-bottom: 1px solid var(--line);
}
.gm-title { font-weight: 650; color: var(--tx-strong); font-size: 14px; }
.gm-status { color: var(--tx-muted); font-size: 12.5px; }
.gm-actions { margin-left: auto; display: flex; align-items: center; gap: 6px; }
.gm-actions button {
  border: 1px solid var(--line-strong); background: var(--bg-elev); color: var(--tx);
  padding: 5px 12px; border-radius: 7px; font-family: inherit; font-size: 13px; cursor: pointer;
}
.gm-actions button:hover { background: var(--bg-hover); }
#gm-close { font-size: 18px; line-height: 1; padding: 3px 10px; }
#gm-canvas {
  flex: 1 1 auto; min-height: 0; position: relative;
  background: var(--bg);
  cursor: grab; overflow: hidden;
}
#gm-canvas.dragging { cursor: grabbing; }
#gm-cv {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  display: block;
  user-select: none;
}
#gm-tooltip {
  position: absolute; pointer-events: none;
  background: var(--tx-strong); color: var(--bg);
  padding: 4px 9px; border-radius: 6px; font-size: 12px;
  transform: translate(-50%, -130%); white-space: nowrap;
  box-shadow: 0 4px 14px rgba(0,0,0,0.15);
}
.gm-foot {
  flex: 0 0 36px; display: flex; align-items: center; padding: 0 18px;
  border-top: 1px solid var(--line); color: var(--tx-muted); font-size: 12px;
}
.gm-foot b { color: var(--tx); font-weight: 600; }

/* responsive */
@media (max-width: 1180px) {
  .app { grid-template-columns: var(--rail-w) 1fr; }
  .right { display: none; }
}
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .rail { display: none; }
  #doc { padding: 40px 22px 120px; }
  #claude-panel { right: 8px; left: 8px; width: auto; }
}
