:root {
  color-scheme: light dark;
  --soft: #e9ecf1;
  --bg: #f6f7f9;
  --surface: #ffffff;
  --text: #1b1f24;
  --muted: #667085;
  --line: #e4e7ec;
  --accent: #4f46e5;
  --accent-soft: rgba(79, 70, 229, 0.1);
  --on-accent: #ffffff;
  --danger: #d92d20;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.05), 0 6px 20px rgba(16, 24, 40, 0.08);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  --tok-kw: #7c3aed;
  --tok-ty: #0f766e;
  --tok-fn: #2563eb;
  --tok-str: #b45309;
  --tok-num: #be185d;
  --tok-com: #6b7280;
}

@media (prefers-color-scheme: dark) {
  :root {
    --soft: #2a2f3a;
    --bg: #0e1015;
    --surface: #171a21;
    --text: #e8eaef;
    --muted: #98a2b3;
    --line: #2a2f3a;
    --accent: #8b8df7;
    --accent-soft: rgba(139, 141, 247, 0.16);
    --on-accent: #0e1015;
    --danger: #f97066;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.5), 0 8px 24px rgba(0, 0, 0, 0.4);
    --tok-kw: #c4b5fd;
    --tok-ty: #5eead4;
    --tok-fn: #93c5fd;
    --tok-str: #fcd34d;
    --tok-num: #f9a8d4;
    --tok-com: #8b95a7;
  }
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); }
h1, h2, h3 { margin: 0; line-height: 1.25; }
.muted { color: var(--muted); }
.error { color: var(--danger); }
.empty { color: var(--muted); padding: 0.5rem 0; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---- shell ---------------------------------------------------------------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  padding-top: env(safe-area-inset-top);
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.topbar-inner { max-width: 56rem; margin: 0 auto; padding: 0.7rem 1rem; display: flex; align-items: center; justify-content: space-between; }

.brand {
  display: inline-flex; align-items: center; gap: 0.55rem;
  font-weight: 700; letter-spacing: -0.01em; color: var(--text); text-decoration: none;
}
.logo {
  width: 0.95rem; height: 0.95rem; border-radius: 50%;
  background: var(--accent); box-shadow: 0 0 0 4px var(--accent-soft);
}
.page {
  max-width: 56rem;
  margin: 0 auto;
  padding: 1.25rem 1rem calc(4rem + env(safe-area-inset-bottom));
}
.page-title { font-size: 1.6rem; letter-spacing: -0.02em; margin-bottom: 0.25rem; }

/* ---- controls --------------------------------------------------------------- */

button {
  font: inherit; font-size: 0.9rem; color: var(--text);
  background: var(--surface); border: 1px solid var(--line); border-radius: 0.6rem;
  padding: 0.4rem 0.8rem; cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.05s;
}
button:hover { background: var(--accent-soft); border-color: var(--accent); }
button:active { transform: translateY(1px); }
button.primary { background: var(--accent); color: var(--on-accent); border-color: var(--accent); }
button.primary:hover { filter: brightness(1.08); background: var(--accent); }
button.small { font-size: 0.78rem; padding: 0.15rem 0.55rem; }
button.danger { color: var(--danger); }
button.danger:hover { border-color: var(--danger); background: transparent; }
button.icon-btn {
  border: 0; background: transparent; color: var(--muted);
  padding: 0.1rem 0.45rem; font-size: 1.15rem; line-height: 1;
}
button.icon-btn:hover { color: var(--danger); background: transparent; }
/* pill = link kind, drawn like its line on the map */
button.pill {
  border-radius: 999px; font-size: 0.72rem; padding: 0.1rem 0.65rem;
  background: var(--accent-soft); border-color: transparent; color: var(--accent);
}
button.pill.deeper { background: transparent; border: 1px dotted var(--muted); color: var(--muted); }
button.pill.sidetrack { background: transparent; border: 1px dashed var(--muted); color: var(--muted); }

.picker-actions { display: flex; flex-wrap: wrap; gap: 0.4rem; }
.picker p kbd { font-size: 0.7rem; }

/* ---- home ---------------------------------------------------------------------- */

.new-outline { display: flex; gap: 0.5rem; margin: 1rem 0 1.25rem; }
.new-outline input {
  flex: 1; font: inherit; color: inherit; padding: 0.6rem 0.8rem;
  border: 1px solid var(--line); border-radius: 0.7rem; background: var(--surface);
}
.new-outline input:focus { outline: 2px solid var(--accent); border-color: transparent; }

ul.cards {
  list-style: none; padding: 0; margin: 0;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(15rem, 1fr)); gap: 0.75rem;
}
.card {
  position: relative; display: flex; flex-direction: column; gap: 0.6rem;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 0.9rem 1rem;
  transition: transform 0.12s, border-color 0.12s;
}
.card:hover { transform: translateY(-1px); border-color: var(--accent); }
.card-main { display: flex; flex-direction: column; gap: 0.15rem; color: inherit; text-decoration: none; }
.card-main::after { content: ""; position: absolute; inset: 0; border-radius: var(--radius); } /* whole card is the link */
.card-title { font-weight: 650; font-size: 1.05rem; }
.card-meta { font-size: 0.82rem; color: var(--muted); }
.card-actions { display: flex; justify-content: flex-end; position: relative; z-index: 1; }

/* ---- outline header ---------------------------------------------------------------- */

nav.crumbs { display: flex; flex-wrap: wrap; align-items: center; gap: 0.4rem; font-size: 0.85rem; color: var(--muted); margin-bottom: 0.75rem; }
nav.crumbs a { color: var(--muted); text-decoration: none; }
nav.crumbs a:hover { color: var(--accent); }

.tabs {
  display: inline-flex; gap: 0.2rem; padding: 0.2rem; margin-bottom: 1rem;
  background: var(--surface); border: 1px solid var(--line); border-radius: 0.75rem;
}
.tab { padding: 0.3rem 0.95rem; border-radius: 0.55rem; font-size: 0.9rem; font-weight: 550; color: var(--muted); text-decoration: none; }
.tab:hover { color: var(--text); }
.tab.active { background: var(--accent); color: var(--on-accent); }

input.title {
  display: block; width: 100%; margin: 0 0 0.75rem; padding: 0.2rem 0;
  font: inherit; font-size: 1.75rem; font-weight: 750; letter-spacing: -0.02em; color: inherit;
  background: transparent; border: 0; border-bottom: 2px solid transparent;
}
input.title:focus { outline: none; border-bottom-color: var(--accent); }

/* ---- node tree ------------------------------------------------------------------------ */

ul.nodes { list-style: none; margin: 0; padding: 0; }
ul.nodes ul.nodes { margin-left: 0.6rem; padding-left: 0.9rem; border-left: 1px solid var(--line); }
ul.nodes li { position: relative; }
ul.nodes li:not(.refitem)::before {
  content: ""; position: absolute; left: 0.45rem; top: 0.95rem;
  width: 0.38rem; height: 0.38rem; border-radius: 50%; background: var(--muted); opacity: 0.7;
}

/* a note's title row: input + content toggle */
ul.nodes .row { display: flex; align-items: center; gap: 0.25rem; }
ul.nodes .row > input[type="text"] { flex: 1; min-width: 0; padding: 0.38rem 0.5rem 0.38rem 1.35rem; }
ul.nodes .row > input[type="text"],
ul.nodes .ref > input.blurb {
  font: inherit; color: inherit; background: transparent; border: 0; border-radius: 0.5rem;
}
ul.nodes .row > input[type="text"]:hover { background: var(--accent-soft); }
ul.nodes .row > input[type="text"]:focus { outline: none; background: var(--surface); box-shadow: 0 0 0 1px var(--accent); }

button.body-btn {
  border: 0; background: transparent; color: var(--muted);
  padding: 0.1rem 0.45rem; font-size: 1.05rem; line-height: 1;
}
button.body-btn:hover { color: var(--accent); background: transparent; }
button.body-btn.filled { color: var(--accent); }

textarea.body {
  display: block; width: calc(100% - 1.35rem); min-height: 5rem; resize: vertical;
  margin: 0.1rem 0 0.4rem 1.35rem; padding: 0.5rem 0.65rem;
  font: inherit; font-size: 0.92rem; color: inherit; background: var(--surface);
  border: 1px solid var(--line); border-radius: 0.6rem;
}
textarea.body:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }

ul.nodes .ref {
  display: flex; align-items: center; gap: 0.6rem; margin: 0.3rem 0; padding: 0.35rem 0.6rem;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: 0 1px 2px rgba(16, 24, 40, 0.04);
}
ul.nodes .ref:focus-within { border-color: var(--accent); }
a.ref-link { font-weight: 650; text-decoration: none; white-space: nowrap; }
a.ref-link::after { content: " →"; opacity: 0.6; }
a.ref-link:hover { text-decoration: underline; }
ul.nodes .ref > input.blurb { flex: 1; min-width: 0; padding: 0.2rem 0.4rem; font-size: 0.9rem; color: var(--muted); }
ul.nodes .ref > input.blurb:focus { color: var(--text); outline: none; }

.toolbar { display: flex; flex-wrap: wrap; gap: 0.5rem; margin-top: 1rem; }
.hint { color: var(--muted); font-size: 0.8rem; margin-top: 1.25rem; }
kbd {
  font: 0.75rem ui-monospace, SFMono-Regular, Menlo, monospace;
  background: var(--surface); border: 1px solid var(--line); border-bottom-width: 2px;
  border-radius: 0.35rem; padding: 0 0.35rem;
}

@media (max-width: 36rem) {
  ul.nodes .ref { flex-wrap: wrap; }
  ul.nodes .ref > input.blurb { flex-basis: 100%; order: 5; }
}

/* ---- link picker ------------------------------------------------------------------------ */

.picker {
  background: var(--surface); border: 1px solid var(--accent); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 0.75rem; margin: 0.5rem 0 1rem;
}
.picker p { margin: 0 0 0.5rem; font-size: 0.85rem; color: var(--muted); }
.picker input {
  width: 100%; font: inherit; color: inherit; padding: 0.5rem 0.7rem;
  background: var(--bg); border: 1px solid var(--line); border-radius: 0.6rem;
}
.picker ul { list-style: none; padding: 0; margin: 0.5rem 0; max-height: 16rem; overflow: auto; }
.picker li { padding: 0.4rem 0.6rem; border-radius: 0.5rem; cursor: pointer; }
.picker li:hover { background: var(--accent-soft); }
.picker li.active { background: var(--accent); color: var(--on-accent); }
.picker li.blocked { opacity: 0.55; cursor: default; }
.picker li.blocked:hover { background: transparent; }

/* ---- map: the route through one outline ------------------------------------------------ */

/* notes on the map are clickable */
.mnode.note { cursor: pointer; }
.mnode.note:hover .mlabel { border-color: var(--accent); }
.mnode.note:focus-visible { outline: 2px solid var(--accent); outline-offset: 4px; border-radius: 6px; }

/* a small badge marks notes that have content */
.mlabel.has-body::after {
  content: "≡"; position: absolute; top: -9px; right: -9px;
  width: 18px; height: 18px; border-radius: 50%;
  display: grid; place-items: center; font-size: 11px; line-height: 1;
  background: var(--text); color: var(--surface);
}
.mnode.left .mlabel.has-body::after { right: auto; left: -9px; }

/* reading panel */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 50; display: grid; place-items: center;
  padding: 1rem; background: rgba(0, 0, 0, 0.4);
}
.modal {
  width: min(36rem, 100%); max-height: 80vh; overflow: auto;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 1.1rem 1.25rem;
}
.modal-title { font-size: 1.2rem; margin-bottom: 0.6rem; }
.modal-actions { display: flex; gap: 0.5rem; margin-top: 1rem; }

.map-scroll {
  overflow: auto; max-height: 80vh;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
}
.map-canvas { position: relative; margin: 0 auto; }
.map-links { position: absolute; inset: 0; pointer-events: none; overflow: visible; }

.link { fill: none; stroke: var(--text); stroke-linecap: round; stroke-linejoin: round; }
.link.main { stroke-width: 2.25; }
.link.nested { stroke-width: 1.8; }
.link.sidetrack { stroke-width: 1.8; stroke-dasharray: 7 6; }
.link.deeper { stroke-width: 2; stroke-dasharray: 0.1 6; marker-end: url(#map-arrow); }
.arrow-head { fill: var(--text); }

.map-title {
  position: absolute; left: 24px; top: 22px; margin: 0; max-width: 560px;
  font-size: 1.5rem; font-weight: 750; letter-spacing: -0.02em; text-wrap: balance;
}

.glyph { flex: none; overflow: visible; color: var(--text); }
.g-bg { fill: var(--surface); }
.g-dot { fill: currentColor; }
.g-soft { fill: var(--soft); stroke: currentColor; stroke-width: 1; }
.g-ring { fill: none; stroke: currentColor; stroke-width: 2; }
.g-ring.sidetrack { stroke-dasharray: 6 3.42; }
.g-ring.deeper { stroke-dasharray: 0.1 4.14; stroke-linecap: round; stroke-width: 2.2; }
.g-port { fill: var(--surface); }

.mnode { position: absolute; display: flex; align-items: center; gap: 10px; color: var(--text); text-decoration: none; }
.mnode.left, .mnode.right { transform: translateY(-50%); }
.mnode.left { flex-direction: row-reverse; }
.mnode.left .mlabel { align-items: flex-end; text-align: right; }
/* nested items: label to the right, hanging a little below the circle */
.mnode.hang { align-items: flex-start; gap: 14px; }
.mnode.hang .mlabel { margin-top: 16px; }

.mlabel {
  display: flex; flex-direction: column; min-width: 0; max-width: var(--label-max);
  position: relative;
  line-height: 1.25;
  padding: 2px 8px;
  background: var(--surface);
  border: 1.5px solid var(--text);
  border-radius: 6px;
}
a.mnode:hover .mlabel { border-color: var(--accent); }
.mt {
  font-size: 0.95rem; overflow: hidden;
  display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; line-clamp: 2;
}
.mnode.sub .mt { font-weight: 600; -webkit-line-clamp: 1; line-clamp: 1; }
.mnode.hang .mt { font-size: 0.9rem; }
.mb { font-size: 0.8rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
a.mnode:hover .mt { color: var(--accent); text-decoration: underline; }
a.mnode:hover .glyph { color: var(--accent); }

.empty-card {
  display: flex; flex-direction: column; align-items: center; gap: 0.6rem;
  padding: 3rem 1rem; text-align: center; color: var(--muted);
  border: 1px dashed var(--line); border-radius: var(--radius);
}

.legend { display: flex; flex-wrap: wrap; gap: 0.4rem 1.2rem; color: var(--muted); font-size: 0.8rem; margin-top: 0.75rem; }
.lg { display: inline-flex; align-items: center; gap: 0.4rem; }
.legend .glyph { width: 22px; height: 22px; }

/* rendered Markdown */
.md { overflow-wrap: anywhere; }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md h1, .md h2, .md h3, .md h4 { margin: 1rem 0 0.4rem; font-size: 1.05rem; line-height: 1.3; }
.md h1 { font-size: 1.25rem; }
.md h2 { font-size: 1.15rem; }
.md p, .md ul, .md ol, .md blockquote, .md pre, .md table { margin: 0.5rem 0; }
.md ul, .md ol { padding-left: 1.4rem; }
.md blockquote { padding: 0.1rem 0.9rem; border-left: 3px solid var(--line); color: var(--muted); }
.md code { font: 0.88em ui-monospace, SFMono-Regular, Menlo, monospace; background: var(--soft); padding: 0.1em 0.35em; border-radius: 0.3rem; }
.md pre { background: var(--soft); padding: 0.7rem 0.9rem; border-radius: 0.6rem; overflow: auto; }
.md pre code { background: none; padding: 0; }
.md table { border-collapse: collapse; }
.md th, .md td { border: 1px solid var(--line); padding: 0.3rem 0.6rem; }
.md img { max-width: 100%; height: auto; border-radius: 0.5rem; }
.md hr { border: 0; border-top: 1px solid var(--line); }

/* syntax highlighting */
.tok-kw { color: var(--tok-kw); font-weight: 600; }
.tok-ty { color: var(--tok-ty); }
.tok-fn { color: var(--tok-fn); }
.tok-str { color: var(--tok-str); }
.tok-num { color: var(--tok-num); }
.tok-com { color: var(--tok-com); font-style: italic; }

/* math (MathML is drawn by the browser) */
math { font-family: "Latin Modern Math", "STIX Two Math", "Cambria Math", "Noto Sans Math", math; font-size: 1.05em; }
.math-display { display: block; text-align: center; overflow-x: auto; margin: 0.6rem 0; }
.math-error { color: var(--danger); }

/* the editor's content box tools */
.body-tools { display: flex; flex-wrap: wrap; align-items: center; gap: 0.5rem; margin: 0 0 0.4rem 1.35rem; font-size: 0.8rem; }
label.file-btn {
  cursor: pointer; font-size: 0.78rem; padding: 0.15rem 0.55rem;
  background: var(--surface); border: 1px solid var(--line); border-radius: 0.6rem;
}
label.file-btn:hover { background: var(--accent-soft); border-color: var(--accent); }
button.small.active { background: var(--accent); color: var(--on-accent); border-color: var(--accent); }
.preview { margin: 0 0 0.5rem 1.35rem; padding: 0.6rem 0.8rem; border: 1px dashed var(--line); border-radius: 0.6rem; }

.toplink { font-size: 0.9rem; color: var(--muted); text-decoration: none; }
.toplink:hover { color: var(--accent); }

/* citations and reference lists */
.cite { color: var(--accent); cursor: help; border-bottom: 1px dotted var(--accent); }
.cite-missing { color: var(--danger); border-bottom-color: var(--danger); }
.references { margin-top: 1.2rem; padding-top: 0.6rem; border-top: 1px solid var(--line); font-size: 0.92rem; }
.references h4 { margin: 0 0 0.4rem; font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); }
.references ul { list-style: none; padding: 0; margin: 0; }
.references li { padding-left: 1.5rem; text-indent: -1.5rem; margin: 0.35rem 0; } /* hanging indent */

/* the references page */
.bib-actions { margin: 1rem 0 0.6rem; }
.bib-paste {
  display: block; width: 100%; min-height: 6rem; resize: vertical; margin-bottom: 0.5rem;
  font: 0.85rem ui-monospace, SFMono-Regular, Menlo, monospace; color: inherit;
  background: var(--surface); border: 1px solid var(--line); border-radius: 0.6rem; padding: 0.5rem 0.65rem;
}
.bib-filter {
  display: block; width: 100%; margin: 1rem 0 0.75rem; font: inherit; color: inherit;
  padding: 0.5rem 0.8rem; background: var(--surface); border: 1px solid var(--line); border-radius: 0.7rem;
}
.notice { color: var(--muted); font-size: 0.9rem; }
.bib-list { list-style: none; padding: 0; margin: 0; }
.bib-item {
  display: grid; grid-template-columns: 1fr auto; align-items: start; gap: 0.25rem 0.75rem;
  padding: 0.6rem 0.8rem; margin-bottom: 0.5rem;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
}
.bib-key { grid-column: 1 / -1; user-select: all; color: var(--accent); font-size: 0.8rem; }
.bib-ref { overflow-wrap: anywhere; font-size: 0.92rem; }

.toplinks { display: flex; align-items: center; gap: 1rem; }
.dot { display: inline-block; width: 0.55rem; height: 0.55rem; margin-right: 0.35rem; border-radius: 50%; background: var(--muted); }
.dot.off { background: var(--line); }
.dot.ok { background: #16a34a; }
.dot.busy { background: #d97706; }
.dot.err { background: var(--danger); }

.form { display: grid; gap: 0.9rem; margin: 1rem 0; max-width: 32rem; }
.form label { display: grid; gap: 0.3rem; font-size: 0.85rem; color: var(--muted); }
.form input {
  font: inherit; color: var(--text); padding: 0.55rem 0.75rem;
  background: var(--surface); border: 1px solid var(--line); border-radius: 0.6rem;
}
.form-actions { display: flex; flex-wrap: wrap; gap: 0.5rem; }
