:root {
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --serif: "Merriweather", "Iowan Old Style", Georgia, serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --bg: #ffffff;
  --bg-soft: #f6f8f7;
  --border: #e4e7e5;
  --text: #222222;
  --text-muted: #5c625f;
  --text-faint: #8d9490;
  --accent: #2b8a55;
  --accent-hover: #1f6b41;
  --accent-soft: #e6f3eb;
  --graph-node: #4aa574;
  --graph-current: #1d6b42;
  --graph-ghost: #b9d9c6;
  --graph-line: rgba(43, 138, 85, 0.28);
  --graph-line-hi: rgba(43, 138, 85, 0.85);
  --danger: #c0392b;
  --left-w: 300px;
  --right-w: 300px;
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); }
a:hover { color: var(--accent-hover); }
button { font: inherit; color: inherit; }

/* ---------- layout ---------- */
.layout {
  display: grid;
  grid-template-columns: var(--left-w) minmax(0, 1fr) var(--right-w);
  min-height: 100vh;
}
.left {
  border-right: 1px solid var(--border);
  padding: 40px 24px 24px 40px;
  position: sticky; top: 0; height: 100vh; overflow-y: auto;
  display: flex; flex-direction: column;
}
.center { padding: 32px 48px 120px; min-width: 0; }
.center-inner { max-width: 720px; margin: 0 auto; }
.right {
  padding: 40px 24px 24px 8px;
  position: sticky; top: 0; height: 100vh; overflow-y: auto;
}

.site-title {
  font-size: 22px; font-weight: 700; color: var(--text); text-decoration: none;
  display: block; margin-bottom: 24px; letter-spacing: -0.01em; line-height: 1.3;
}
.site-title:hover { color: var(--text); }

/* ---------- nav ---------- */
.nav { flex: 1; font-size: 15px; }
.nav-section { margin-bottom: 4px; }
.nav-row {
  display: flex; align-items: center; gap: 4px; position: relative;
  border-radius: 6px; min-height: 32px;
}
.nav-row:hover { background: var(--bg-soft); }
.nav-toggle {
  display: flex; align-items: center; gap: 6px; flex: 1; min-width: 0;
  background: none; border: 0; padding: 4px 6px; cursor: pointer; text-align: left; color: var(--text);
}
.nav-toggle .chev { width: 14px; height: 14px; flex: none; color: var(--text-faint); transition: transform .15s; }
.nav-section.open .chev { transform: rotate(90deg); }
.nav-pages { display: none; margin: 2px 0 8px 13px; border-left: 1px solid var(--border); }
.nav-section.open .nav-pages { display: block; }
.nav-page {
  display: block; padding: 4px 8px 4px 16px; color: var(--text-muted); text-decoration: none; line-height: 1.45;
  border-left: 2px solid transparent; margin-left: -1px; flex: 1; min-width: 0;
}
.nav-page:hover { color: var(--text); }
.nav-page.active { color: var(--accent); border-left-color: var(--accent); }
.nav-pages .nav-row { border-radius: 0 6px 6px 0; }
.nav-empty { color: var(--text-faint); font-size: 13px; padding: 4px 16px; font-style: italic; }

.row-actions { display: none; gap: 2px; padding-right: 4px; flex: none; }
.nav-row:hover .row-actions, .nav-row:focus-within .row-actions, .row-actions.open { display: flex; }
.icon-btn {
  width: 26px; height: 26px; border: 0; background: none; border-radius: 5px; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; color: var(--text-muted); padding: 0;
}
.icon-btn:hover { background: var(--border); color: var(--text); }
.icon-btn svg { width: 16px; height: 16px; }

.add-section {
  margin-top: 12px; display: flex; align-items: center; gap: 6px; background: none; border: 1px dashed var(--border);
  color: var(--accent); border-radius: 8px; padding: 8px 10px; cursor: pointer; width: 100%; font-size: 14px;
}
.add-section:hover { background: var(--accent-soft); border-color: var(--accent); }
.inline-input {
  width: 100%; font: inherit; font-size: 14px; padding: 5px 8px; border: 1px solid var(--accent);
  border-radius: 6px; outline: none; margin: 2px 0; background: #fff;
}
.admin-foot {
  margin-top: 24px; padding-top: 12px; border-top: 1px solid var(--border); font-size: 13px; color: var(--text-faint);
  display: flex; flex-wrap: wrap; gap: 4px 12px; align-items: center;
}
.admin-foot button { background: none; border: 0; color: var(--text-muted); cursor: pointer; padding: 0; text-decoration: underline; font-size: 13px; }
.admin-foot .badge { background: var(--accent-soft); color: var(--accent-hover); padding: 2px 8px; border-radius: 99px; font-weight: 600; text-decoration: none; }

/* ---------- page ---------- */
.page-head { display: flex; align-items: flex-start; gap: 12px; margin-bottom: 8px; }
.page-title { font-size: 44px; line-height: 1.15; font-weight: 800; letter-spacing: -0.02em; margin: 16px 0 24px; flex: 1; min-width: 0; overflow-wrap: anywhere; }
.page-tools { display: flex; gap: 6px; margin-top: 22px; flex: none; }
.btn {
  border: 1px solid var(--border); background: #fff; border-radius: 7px; padding: 6px 12px; cursor: pointer; font-size: 14px;
  display: inline-flex; align-items: center; gap: 6px; line-height: 1.3; text-decoration: none; color: var(--text);
}
.btn:hover { background: var(--bg-soft); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-hover); }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: .5; cursor: default; }

.markdown { font-size: 17px; overflow-wrap: break-word; }
.markdown > :first-child { margin-top: 0; }
.markdown h1, .markdown h2, .markdown h3, .markdown h4, .markdown h5, .markdown h6 { line-height: 1.3; margin: 1.6em 0 0.6em; font-weight: 700; scroll-margin-top: 16px; }
.markdown h1 { font-size: 1.9em; }
.markdown h2 { font-size: 1.55em; }
.markdown h3 { font-size: 1.3em; }
.markdown h4 { font-size: 1.12em; }
.markdown p { margin: 0 0 1em; }
.markdown img { max-width: 100%; height: auto; border-radius: 6px; display: inline-block; vertical-align: top; }
.markdown blockquote { margin: 1em 0; padding: 2px 0 2px 24px; border-left: 3px solid var(--accent); color: var(--text); }
.markdown blockquote p { margin: 0.3em 0; }
.markdown hr { border: 0; border-top: 2px solid var(--border); margin: 2em 0; }
.markdown code { font-family: var(--mono); font-size: .88em; background: var(--bg-soft); padding: 1px 5px; border-radius: 4px; }
.markdown pre { background: var(--bg-soft); padding: 14px; border-radius: 8px; overflow-x: auto; }
.markdown pre code { background: none; padding: 0; }
.markdown table { border-collapse: collapse; margin: 1em 0; display: block; overflow-x: auto; }
.markdown th, .markdown td { border: 1px solid var(--border); padding: 6px 10px; }
.markdown ul, .markdown ol { padding-left: 1.6em; margin: 0 0 1em; }
.markdown li { margin: 0.2em 0; }
.markdown li::marker { color: var(--text-faint); }
.markdown a.internal { color: var(--accent); }
.markdown a.external::after {
  content: ""; display: inline-block; width: .75em; height: .75em; margin-left: 2px; vertical-align: 0;
  background: currentColor; opacity: .6;
  -webkit-mask: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="black" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round"><path d="M15 3h6v6"/><path d="M10 14 21 3"/><path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"/></svg>') center / contain no-repeat;
          mask: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="black" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round"><path d="M15 3h6v6"/><path d="M10 14 21 3"/><path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"/></svg>') center / contain no-repeat;
}
.markdown .unresolved { color: var(--accent); opacity: .65; text-decoration: underline dotted; text-underline-offset: 3px; }
.markdown .unresolved.can-create { cursor: pointer; opacity: .8; }
.markdown .missing-file { color: var(--text-faint); font-style: italic; font-size: .9em; }
.markdown .pdf-embed { width: 100%; height: 80vh; border: 1px solid var(--border); border-radius: 8px; }
.markdown .note-embed { border-left: 3px solid var(--accent-soft); padding: 4px 0 4px 18px; margin: 1em 0; }
.markdown .note-embed-title { font-weight: 700; font-size: .9em; color: var(--text-muted); margin-bottom: 6px; display: block; text-decoration: none; }
.markdown mark { background: #fff2a8; padding: 0 2px; }
.markdown .task-list-item { list-style: none; }
.markdown .task-list-item input { margin: 0 6px 0 -1.4em; }

.backlinks { margin-top: 56px; padding-top: 16px; border-top: 1px solid var(--border); font-size: 15px; }
.backlinks h4 { margin: 0 0 8px; font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--text-faint); }
.backlinks a { display: block; text-decoration: none; padding: 2px 0; }

/* ---------- right column ---------- */
.graph-card { border: 1px solid var(--border); border-radius: 10px; position: relative; height: 300px; background: #fff; overflow: hidden; }
.graph-card canvas { display: block; touch-action: none; cursor: grab; }
.graph-tools { position: absolute; top: 6px; right: 6px; display: flex; gap: 2px; z-index: 2; }
.graph-tools .icon-btn { background: rgba(255,255,255,.85); }
.graph-tools .icon-btn.on { color: var(--accent); }
.graph-mode { position: absolute; left: 10px; bottom: 6px; font-size: 11px; color: var(--text-faint); pointer-events: none; }

.toc { margin-top: 28px; font-size: 14px; }
.toc a { display: block; color: var(--text-muted); text-decoration: none; padding: 3px 0 3px 10px; border-left: 1px solid var(--border); line-height: 1.4; }
.toc a:hover { color: var(--text); }
.toc a.active { color: var(--accent); border-left-color: var(--accent); }

.graph-modal { position: fixed; inset: 0; background: rgba(20, 30, 25, .35); z-index: 50; display: flex; padding: 4vh 4vw; }
.graph-modal .graph-card { flex: 1; height: auto; box-shadow: 0 20px 60px rgba(0,0,0,.2); }

/* ---------- menus & dialogs ---------- */
.menu {
  position: fixed; z-index: 60; min-width: 220px; background: #fff; border: 1px solid var(--border); border-radius: 10px;
  box-shadow: 0 10px 30px rgba(0,0,0,.12); padding: 6px; font-size: 14px;
}
.menu button {
  display: flex; align-items: center; gap: 10px; width: 100%; text-align: left; background: none; border: 0;
  padding: 7px 10px; border-radius: 6px; cursor: pointer;
}
.menu button:hover, .menu button:focus-visible { background: var(--bg-soft); outline: none; }
.menu button.danger { color: var(--danger); }
.menu .check { width: 16px; height: 16px; border: 1.5px solid var(--text-faint); border-radius: 4px; display: inline-flex; align-items: center; justify-content: center; flex: none; }
.menu .check.on { background: var(--accent); border-color: var(--accent); }
.menu .check.on::after { content: ""; width: 8px; height: 4px; border: 2px solid #fff; border-top: 0; border-right: 0; transform: rotate(-45deg) translate(1px, -1px); }
.menu .sep { height: 1px; background: var(--border); margin: 5px 4px; }
.menu .label { color: var(--text-faint); font-size: 12px; padding: 6px 10px 2px; }

dialog.dlg { border: 0; border-radius: 12px; padding: 0; width: min(440px, calc(100vw - 32px)); box-shadow: 0 20px 60px rgba(0,0,0,.25); }
dialog.dlg::backdrop { background: rgba(20, 30, 25, .35); }
.dlg form { padding: 22px; }
.dlg h3 { margin: 0 0 8px; font-size: 18px; }
.dlg p { margin: 0 0 14px; color: var(--text-muted); font-size: 15px; }
.dlg input, .dlg select { width: 100%; font: inherit; padding: 8px 10px; border: 1px solid var(--border); border-radius: 7px; margin-bottom: 14px; }
.dlg input:focus, .dlg select:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
.dlg .actions { display: flex; justify-content: flex-end; gap: 8px; }
dialog.dlg-wide { width: min(560px, calc(100vw - 32px)); }
.dlg-body { padding: 22px; }
.dlg .admin-add { padding: 0; display: flex; gap: 8px; margin: 14px 0 6px; }
.dlg .admin-add input { margin: 0; flex: 1; min-width: 0; }
.admin-list { border: 1px solid var(--border); border-radius: 8px; }
.admin-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px 12px; border-top: 1px solid var(--border); }
.admin-row:first-child { border-top: 0; }
.admin-email { font-weight: 600; overflow-wrap: anywhere; }
.admin-email small { font-weight: 400; color: var(--text-faint); }
.admin-row .pending { color: var(--text-faint); display: block; }
.admin-note { font-size: 14px; color: var(--text-muted); margin-bottom: 12px; }
.admin-note p { margin: 0 0 6px; }
.admin-note input { margin: 0; font-size: 13px; }
.admin-note .err { color: var(--danger); }
.dlg .err { color: var(--danger); font-size: 14px; min-height: 18px; margin: -6px 0 10px; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: #1f2a24; color: #fff; padding: 9px 16px;
  border-radius: 8px; font-size: 14px; z-index: 80; box-shadow: 0 6px 20px rgba(0,0,0,.2);
}
.toast.err { background: var(--danger); }

/* ---------- login ---------- */
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; background: var(--bg-soft); }
.login { width: min(380px, 100%); background: #fff; border: 1px solid var(--border); border-radius: 14px; padding: 28px; }
.login h1 { margin: 0 0 4px; font-size: 22px; }
.login p { margin: 0 0 20px; color: var(--text-muted); font-size: 14px; }
.login label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 4px; }
.login input { width: 100%; font: inherit; padding: 9px 11px; border: 1px solid var(--border); border-radius: 8px; margin-bottom: 14px; }
.login input:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
.login .btn { width: 100%; justify-content: center; padding: 10px; }
.login .err { color: var(--danger); font-size: 14px; min-height: 20px; margin-bottom: 8px; }
.login .back { display: block; text-align: center; margin-top: 14px; font-size: 14px; }

/* ---------- editor ---------- */
.layout.editing { grid-template-columns: var(--left-w) minmax(0, 1fr); }
.layout.editing .right { display: none; }
.layout.editing .center-inner { max-width: 1400px; }
.editor-head { display: flex; gap: 10px; align-items: center; margin: 8px 0 14px; flex-wrap: wrap; }
.title-input { flex: 1; min-width: 200px; font: inherit; font-size: 30px; font-weight: 800; letter-spacing: -0.02em; border: 0; border-bottom: 1px solid transparent; padding: 4px 0; outline: none; }
.title-input:focus { border-bottom-color: var(--accent); }
.save-state { font-size: 13px; color: var(--text-faint); min-width: 90px; text-align: right; }
.toolbar { display: flex; gap: 2px; flex-wrap: wrap; padding: 6px; border: 1px solid var(--border); border-bottom: 0; border-radius: 10px 10px 0 0; background: var(--bg-soft); align-items: center; }
.toolbar .icon-btn { width: 32px; height: 30px; font-weight: 700; font-size: 14px; }
.toolbar .spacer { flex: 1; }
.toolbar .tabs { display: none; }
.editor-panes { display: grid; grid-template-columns: 1fr 1fr; border: 1px solid var(--border); border-radius: 0 0 10px 10px; min-height: 70vh; }
.editor-panes textarea {
  width: 100%; border: 0; resize: none; outline: none; padding: 18px; font-family: var(--mono); font-size: 14.5px; line-height: 1.65;
  min-height: 70vh; border-right: 1px solid var(--border); background: #fff; color: var(--text); border-radius: 0 0 0 10px;
}
.editor-panes .preview { padding: 18px 24px; overflow: auto; min-width: 0; }
.drop-hint { outline: 2px dashed var(--accent); outline-offset: -6px; }
.hint { font-size: 13px; color: var(--text-faint); margin-top: 8px; }
.hint code { font-family: var(--mono); background: var(--bg-soft); padding: 0 4px; border-radius: 3px; }

.autocomplete {
  position: fixed; z-index: 70; background: #fff; border: 1px solid var(--border); border-radius: 8px; box-shadow: 0 10px 30px rgba(0,0,0,.12);
  max-height: 260px; overflow-y: auto; min-width: 240px; max-width: 360px; font-size: 14px; padding: 4px;
}
.autocomplete div { padding: 6px 10px; border-radius: 5px; cursor: pointer; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.autocomplete div.sel { background: var(--accent-soft); color: var(--accent-hover); }
.autocomplete small { color: var(--text-faint); margin-left: 6px; }

/* ---------- mobile ---------- */
.topbar { display: none; }
@media (max-width: 1100px) {
  .layout { grid-template-columns: var(--left-w) minmax(0, 1fr); }
  .right { position: static; height: auto; grid-column: 2; padding: 0 48px 48px; }
  .right > * { max-width: 720px; margin-left: auto; margin-right: auto; }
  .layout.editing .center-inner { max-width: none; }
}
@media (max-width: 900px) {
  .editor-panes { grid-template-columns: 1fr; }
  .editor-panes textarea { border-right: 0; border-radius: 0 0 10px 10px; }
  .editor-panes.show-write .preview { display: none; }
  .editor-panes.show-preview textarea { display: none; }
  .toolbar .tabs { display: flex; gap: 4px; }
}
@media (max-width: 760px) {
  :root { --left-w: 86vw; }
  .layout, .layout.editing { display: block; }
  .topbar {
    display: flex; align-items: center; gap: 8px; padding: 10px 16px; border-bottom: 1px solid var(--border);
    position: sticky; top: 0; background: rgba(255,255,255,.95); z-index: 30;
  }
  .topbar a { font-weight: 700; color: var(--text); text-decoration: none; }
  .left {
    position: fixed; z-index: 40; left: 0; top: 0; width: var(--left-w); background: #fff; transform: translateX(-100%);
    transition: transform .2s; padding: 24px 16px; box-shadow: 0 0 40px rgba(0,0,0,.15);
  }
  .nav-open .left { transform: none; }
  .scrim { display: none; }
  .nav-open .scrim { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.25); z-index: 35; }
  .center { padding: 8px 16px 40px; }
  .right { padding: 0 16px 40px; }
  .page-title { font-size: 32px; }
  .graph-card { height: 260px; }
  .row-actions { display: flex; }
}

/* ---------- serif headings & navigation ---------- */
.site-title, .nav, .topbar a, .page-title, .title-input,
.markdown h1, .markdown h2, .markdown h3, .markdown h4, .markdown h5, .markdown h6,
.note-embed-title, .login h1, .dlg h3, .toc { font-family: var(--serif); }
.page-title, .title-input { font-weight: 700; letter-spacing: -0.01em; }
.site-title { font-weight: 700; letter-spacing: 0; }
.nav { font-size: 14.5px; }
