:root {
  --bg: #faf8f3;
  --surface: #ffffff;
  --ink: #1c1917;
  --muted: #6b6560;
  --line: #e7e2d9;
  --edge: #a8a29e;
  --accent: #b91c1c;
  --band: rgba(0, 0, 0, 0.025);
  --shadow: 0 8px 30px rgba(28, 25, 23, 0.12);
  --serif: "Source Serif 4", Georgia, serif;
  --sans: "Inter", system-ui, -apple-system, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14130f;
    --surface: #1e1d19;
    --ink: #ece8e1;
    --muted: #a39d94;
    --line: #34312b;
    --edge: #6b655d;
    --accent: #f87171;
    --band: rgba(255, 255, 255, 0.03);
    --shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #14130f;
  --surface: #1e1d19;
  --ink: #ece8e1;
  --muted: #a39d94;
  --line: #34312b;
  --edge: #6b655d;
  --accent: #f87171;
  --band: rgba(255, 255, 255, 0.03);
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 14px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  padding: 12px 20px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
  z-index: 3;
}
.brand h1 { font-family: var(--serif); font-weight: 600; font-size: 22px; margin: 0; letter-spacing: -0.01em; }
.tagline { margin: 2px 0 0; color: var(--muted); font-size: 13px; }
.controls { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.controls input[type="search"], .controls button {
  font: inherit;
  color: var(--ink);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 6px 10px;
}
.controls input[type="search"] { width: 200px; }
.controls button { cursor: pointer; }
.controls button:hover { border-color: var(--edge); }
.toggle { display: flex; align-items: center; gap: 6px; color: var(--muted); cursor: pointer; user-select: none; }

.stage { position: relative; flex: 1; min-height: 0; }
#chart { position: absolute; inset: 0; }
#chart svg { display: block; width: 100%; height: 100%; cursor: grab; }
#chart svg:active { cursor: grabbing; }

/* Chart */
.band { fill: var(--band); }
.band-label { font-family: var(--serif); font-size: 13px; font-weight: 600; letter-spacing: 0.04em; text-transform: uppercase; }
.band-label-bg { fill: var(--bg); opacity: 0.85; }
.axis-bg { fill: var(--surface); }
.axis-line { stroke: var(--line); }
.tick line { stroke: var(--line); }
.tick text { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.gridline { stroke: var(--line); stroke-dasharray: 2 4; }

.edge { fill: none; stroke-opacity: 0.45; transition: stroke-opacity 0.15s, stroke-width 0.15s; }
.edge.inherits { stroke-width: 2.2; }
.edge.merges { stroke-width: 1.3; }
.edge.convergent { stroke: var(--accent); stroke-width: 1.6; stroke-dasharray: 2 4; stroke-linecap: round; stroke-opacity: 0.8; }

.node { cursor: pointer; }
.node circle.dot { stroke: var(--bg); stroke-width: 2; }
.node.dormant circle.dot { fill: var(--bg) !important; stroke-width: 2; stroke-dasharray: 3 2; }
.node circle.ring { fill: none; stroke-width: 1.3; }
.node text {
  font-size: 12px;
  fill: var(--ink);
  paint-order: stroke;
  stroke: var(--bg);
  stroke-width: 4px;
  stroke-linejoin: round;
}
.node text .yr { fill: var(--muted); font-size: 10.5px; }
.node:hover text { font-weight: 600; }

/* Selection state */
.has-selection .edge { stroke-opacity: 0.07; }
.has-selection .node { opacity: 0.22; }
.has-selection .node.lit { opacity: 1; }
.has-selection .edge.lit-up { stroke-opacity: 0.9; stroke-width: 2.4; }
.has-selection .edge.lit-down { stroke-opacity: 0.55; stroke-dasharray: 5 3; }
.has-selection .edge.convergent.lit-conv { stroke-opacity: 1; }
.node.selected circle.dot { stroke: var(--ink); stroke-width: 2.5; }
.node.selected text { font-weight: 600; }
.hide-convergent .edge.convergent { display: none; }

/* Legend */
.legend {
  position: absolute;
  right: 16px;
  bottom: 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 12px;
  color: var(--muted);
  display: grid;
  gap: 4px;
  z-index: 2;
}
.legend-row { display: flex; align-items: center; gap: 8px; }
.legend-hint { margin-top: 4px; padding-top: 6px; border-top: 1px solid var(--line); font-size: 11.5px; }
.lg-inherits { stroke: var(--edge); stroke-width: 2.4; }
.lg-merges { stroke: var(--edge); stroke-width: 1.3; }
.lg-convergent { stroke: var(--accent); stroke-width: 1.8; stroke-dasharray: 2 4; stroke-linecap: round; }
.lg-dormant { fill: var(--bg); stroke: var(--muted); stroke-width: 2; stroke-dasharray: 3 2; }
.lg-revived { fill: var(--muted); }
.lg-revived-ring { fill: none; stroke: var(--muted); stroke-width: 1.2; }

/* Panels */
.panel, .about {
  position: absolute;
  top: 16px;
  right: 16px;
  width: 360px;
  max-height: calc(100% - 32px);
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: var(--shadow);
  padding: 20px 22px;
  z-index: 4;
}
.panel[hidden], .about[hidden] { display: none; }
.panel-close {
  position: absolute;
  top: 8px;
  right: 10px;
  border: 0;
  background: none;
  color: var(--muted);
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  padding: 4px 6px;
}
.panel-close:hover { color: var(--ink); }
.panel h2, .about h2 { font-family: var(--serif); font-weight: 600; font-size: 22px; margin: 0 24px 4px 0; line-height: 1.2; }
.meta { color: var(--muted); font-size: 12.5px; display: flex; flex-wrap: wrap; gap: 4px 10px; margin-bottom: 12px; }
.chip { display: inline-flex; align-items: center; gap: 5px; }
.chip .sw { width: 9px; height: 9px; border-radius: 50%; display: inline-block; }
.status { color: var(--accent); font-weight: 500; }
.panel p, .about p { font-family: var(--serif); font-size: 15.5px; line-height: 1.55; margin: 0 0 12px; }
.panel h3 { font-size: 11.5px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin: 16px 0 6px; font-weight: 600; }
.panel ul { margin: 0; padding-left: 18px; line-height: 1.5; }
.links { display: flex; flex-wrap: wrap; gap: 6px; }
.links button {
  font: inherit;
  font-size: 12.5px;
  color: var(--ink);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 3px 10px 3px 8px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.links button:hover { border-color: var(--edge); }
.links .sw { width: 8px; height: 8px; border-radius: 50%; }
.links .kind { color: var(--muted); font-size: 11px; }
.panel ul.papers { list-style: none; padding: 0; display: grid; gap: 8px; }
.papers a { color: var(--ink); text-decoration: underline; text-decoration-color: var(--edge); text-underline-offset: 2px; line-height: 1.35; }
.papers a:hover { text-decoration-color: var(--ink); }
.paper-meta { display: block; color: var(--muted); font-size: 12px; margin-top: 2px; }
.paper-meta .kind { text-transform: uppercase; letter-spacing: 0.05em; font-size: 10.5px; }
.badge {
  display: inline-block;
  margin-left: 6px;
  padding: 1px 6px;
  border-radius: 4px;
  border: 1px solid var(--line);
  color: var(--muted);
  font-size: 10.5px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  vertical-align: 1px;
}
.badge.ai { color: var(--accent); border-color: currentColor; margin-left: 0; }
.paper-meta .badge.ai { margin-left: 0; }
.lean-summary, .lean-intro { font-size: 14.5px !important; }
.lean-summary code { font-size: 12px; }
.lean-summary a, .lean-intro a { color: var(--ink); text-decoration-color: var(--edge); text-underline-offset: 2px; }
.panel .links, .about .links { margin-top: 4px; }
.feed-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; flex-wrap: wrap; }
.feed-head .toggle { text-transform: none; letter-spacing: 0; font-weight: 400; font-size: 12px; }
.about h3 { font-size: 11.5px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin: 16px 0 6px; font-weight: 600; }
.about ul.papers { list-style: none; padding: 0; display: grid; gap: 8px; margin: 0; }
.papers > li { min-width: 0; overflow-wrap: anywhere; }
.links .more { color: var(--muted); font-size: 12px; align-self: center; }
/* Key papers: a folded "bundle" that opens into a branch of paper cards. */
.nest {
  margin: 16px 0 4px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  overflow: hidden;
}
.nest summary {
  list-style: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border-left: 3px solid var(--clade, var(--edge));
}
.nest summary::-webkit-details-marker { display: none; }
.nest summary:hover { background: var(--surface); }
.nest summary:focus-visible { outline: 2px solid var(--edge); outline-offset: -2px; }
.nest-icon svg { width: 22px; height: 22px; fill: none; stroke: var(--clade, var(--muted)); stroke-width: 1.4; stroke-linejoin: round; display: block; }
.nest-text { display: grid; flex: 1; min-width: 0; }
.nest-label { font-size: 11.5px; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase; color: var(--ink); }
.nest-sub { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.nest-spines { display: flex; align-items: flex-end; gap: 3px; height: 22px; transition: opacity 0.2s; }
.spine { width: 6px; height: 22px; border-radius: 2px; background: var(--clade, var(--edge)); }
.spine:nth-child(2n) { height: 18px; }
.spine:nth-child(3n) { height: 20px; }
.spine.k-about { opacity: 0.6; }
.spine.k-search { background: transparent; border: 1.5px dashed var(--clade, var(--edge)); }
.nest[open] .nest-spines { opacity: 0; }
.nest-chev {
  width: 8px; height: 8px;
  border-right: 2px solid var(--muted);
  border-bottom: 2px solid var(--muted);
  transform: rotate(-45deg);
  transition: transform 0.2s;
  margin-right: 4px;
}
.nest[open] .nest-chev { transform: rotate(45deg); }
.nest[open] .paper-tree { animation: branch-in 0.25s ease; }
@keyframes branch-in { from { opacity: 0; transform: translateY(-4px); } to { opacity: 1; transform: none; } }

/* The branch: a rail in the clade color with an elbow to each paper. */
.panel ol.paper-tree { list-style: none; margin: 0; min-width: 0; grid-template-columns: minmax(0, 1fr); padding: 4px 12px 12px 14px; display: grid; gap: 10px; position: relative; }
.paper-tree::before {
  content: "";
  position: absolute;
  left: 14px; top: 0; bottom: 30px;
  border-left: 2px solid var(--clade, var(--edge));
  opacity: 0.5;
}
.paper-branch { position: relative; display: grid; grid-template-columns: 56px 1fr; align-items: start; min-width: 0; }
.paper-branch::before {
  content: "";
  position: absolute;
  left: 0; top: 13px;
  width: 12px;
  border-top: 2px solid var(--clade, var(--edge));
  opacity: 0.5;
}
.paper-year {
  margin-left: 14px;
  margin-top: 5px;
  font-size: 11px;
  font-weight: 600;
  color: var(--clade, var(--muted));
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
}
.paper-year small { display: block; font-size: 9px; letter-spacing: 0.05em; }
.paper-card {
  min-width: 0;
  overflow-wrap: anywhere;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  transition: border-color 0.15s, transform 0.15s;
}
.paper-branch.k-search .paper-card { border-style: dashed; }
.paper-card:hover { border-color: var(--clade, var(--edge)); transform: translateX(2px); }
.paper-card a { color: var(--ink); text-decoration: none; font-weight: 500; line-height: 1.35; }
.paper-card a:hover { text-decoration: underline; text-underline-offset: 2px; }
.paper-card .paper-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 8px; margin-top: 4px; }
.kind-pill {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  padding: 1px 6px;
  border-radius: 999px;
  border: 1px solid var(--line);
  color: var(--muted);
}
.paper-branch.k-direct .kind-pill { color: var(--clade, var(--ink)); border-color: currentColor; }
.paper-branch.k-search .kind-pill { border-style: dashed; }
@media (prefers-reduced-motion: reduce) {
  .nest-chev, .nest-spines, .paper-card { transition: none; }
  .nest[open] .paper-tree { animation: none; }
  .paper-card:hover { transform: none; }
}
.counts { color: var(--muted); font-size: 12.5px; margin-top: 14px; border-top: 1px solid var(--line); padding-top: 10px; }
.about code { font-size: 13px; }

@media (max-width: 720px) {
  .topbar { padding: 10px 16px; }
  .tagline { display: none; }
  .controls input[type="search"] { width: 140px; }
  .legend { display: none; }
  .panel, .about {
    top: auto;
    left: 0;
    right: 0;
    bottom: 0;
    width: auto;
    max-height: 55%;
    border-radius: 14px 14px 0 0;
    padding: 18px 16px;
  }
}
