:root {
  --bg: #0d0f16;
  --panel: rgba(18, 21, 32, 0.95);
  --panel-solid: #121520;
  --card: #181c2b;
  --line: #262c40;
  --text: #e6e9f5;
  --muted: #98a1c0;
  --faint: #6c7596;
  --link: #9fb0d8;
  --link-hover: #cfe0ff;
  --sat: #f2b544;
  --ok: #7fd49a;
  --warn: #f2b544;
  --radius: 10px;
  --panel-w: 440px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: 'Roboto', Arial, sans-serif;
  font-size: 15px;
  overscroll-behavior: none;
}

button { font: inherit; color: inherit; }
a { color: var(--link); }
a:hover { color: var(--link-hover); }

#stage {
  position: fixed;
  inset: 0;
  touch-action: none;
}
#stage canvas { display: block; }

/* CSS2D labels sit in their own layer above the canvas */
#stage .labels { position: absolute; inset: 0; pointer-events: none; }
.label {
  pointer-events: auto;
  cursor: pointer;
  font-size: 12px;
  line-height: 1;
  white-space: nowrap;
  color: #dfe4f7;
  padding: 2px 4px;
  border-radius: 4px;
  text-shadow: 0 0 3px #000, 0 0 6px #000, 0 0 10px rgba(0, 0, 0, .8);
  transition: opacity .25s, color .25s;
  user-select: none;
}
.label.root { font-family: 'Space Grotesk', sans-serif; font-weight: 700; font-size: 15px; color: var(--sat); }
.label.hot { color: #fff; font-weight: 500; }
.label.sel { color: #fff; font-weight: 500; font-size: 13px; background: rgba(13, 15, 22, .75); }
body.no-labels .label:not(.sel):not(.hot):not(.root) { opacity: 0 !important; pointer-events: none; }

/* ---------------------------------------------------------------- header */
#top {
  position: fixed;
  top: 16px;
  left: 22px;
  z-index: 5;
  pointer-events: none;
  max-width: 44vw;
}
#top > * { pointer-events: auto; }
#back { font-size: 0.9rem; text-decoration: none; }
#back:hover { text-decoration: underline; }
#top h1 {
  font-family: 'Space Grotesk', sans-serif;
  font-weight: 700;
  font-size: 2.1rem;
  letter-spacing: .01em;
  margin: 10px 0 2px;
}
.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  background: none;
  border: 0;
  padding: 0;
  font: inherit;
  color: inherit;
  cursor: pointer;
}
.logo { width: 34px; height: 34px; overflow: visible; }
.logo path { fill: none; stroke-width: 2.6; stroke-linecap: round; }
.logo .root { transition: filter .3s; }
.brand:hover .logo .root { filter: drop-shadow(0 0 3px rgba(242, 181, 68, .9)); }
#tagline { margin: 0; color: var(--muted); font-size: 0.92rem; }

/* -------------------------------------------------------------- controls */
#controls {
  position: fixed;
  top: 16px;
  right: 22px;
  z-index: 6;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 8px;
  transition: right .3s ease;
}
body.panel-open #controls { right: calc(var(--panel-w) + 22px); }
#controls .row { display: flex; gap: 8px; align-items: center; }

#search-wrap { position: relative; }
#search {
  width: 260px;
  padding: 8px 12px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: rgba(18, 21, 32, .9);
  color: var(--text);
  font: inherit;
  outline: none;
}
#search:focus { border-color: #4a5680; }
#search-results {
  position: absolute;
  right: 0;
  top: calc(100% + 4px);
  width: 320px;
  max-height: 50vh;
  overflow-y: auto;
  margin: 0;
  padding: 4px;
  list-style: none;
  background: var(--panel-solid);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .5);
}
#search-results li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border-radius: 6px;
  cursor: pointer;
}
#search-results li.active, #search-results li:hover { background: #20263a; }
#search-results .sub { color: var(--faint); font-size: .8rem; margin-left: auto; }

.dot { width: 9px; height: 9px; border-radius: 50%; flex: none; display: inline-block; }

.seg {
  display: flex;
  align-items: center;
  background: rgba(18, 21, 32, .9);
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
}
.seg-label { color: var(--faint); font-size: .8rem; padding: 0 6px 0 10px; }
.seg button, .toggle {
  border: 0;
  background: none;
  padding: 6px 10px;
  font-size: .85rem;
  color: var(--muted);
  cursor: pointer;
}
.seg button.on { color: var(--text); background: #262d45; }
.toggle {
  background: rgba(18, 21, 32, .9);
  border: 1px solid var(--line);
  border-radius: 8px;
}
.toggle.on { color: var(--text); }
.seg button:hover, .toggle:hover { color: var(--text); }

/* ---------------------------------------------------------------- legend */
#legend {
  position: fixed;
  left: 22px;
  bottom: 18px;
  z-index: 5;
  font-size: .82rem;
  color: var(--muted);
  max-width: 270px;
}
#legend-toggle { display: none; }
#legend ul { list-style: none; margin: 0; padding: 0; }
#legend-cats li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 2px 6px;
  margin-left: -6px;
  border-radius: 6px;
  cursor: pointer;
}
#legend-cats li:hover { color: var(--text); background: rgba(38, 45, 69, .6); }
#legend-cats li.on { color: var(--text); background: rgba(38, 45, 69, .9); }
#legend-edges { margin-top: 10px !important; }
#legend-edges li { display: flex; align-items: center; gap: 8px; padding: 1px 0; }
.lg-primary { stroke: #cfd6f2; stroke-width: 2.4; }
.lg-direct { stroke: #8a93b8; stroke-width: 1.2; }
.lg-implied { stroke: #8a93b8; stroke-width: 1; stroke-dasharray: 3 3; }
.legend-note { margin: 10px 0 0; color: var(--faint); line-height: 1.35; }
.legend-note.keys { line-height: 1.7; }
kbd {
  font-family: inherit;
  font-size: .76rem;
  color: var(--muted);
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: 4px;
  padding: 0 4px;
  margin: 0 1px;
  background: #161a28;
}
@media (hover: none) { .legend-note.keys { display: none; } }

/* ----------------------------------------------------------------- panel */
#panel {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: var(--panel-w);
  z-index: 8;
  background: var(--panel);
  border-left: 1px solid var(--line);
  backdrop-filter: blur(6px);
  overflow-y: auto;
  overscroll-behavior: contain;
  box-shadow: -12px 0 40px rgba(0, 0, 0, .45);
}
#panel[hidden] { display: none; }
#panel-close, #about-close {
  position: sticky;
  top: 10px;
  float: right;
  margin: 10px 12px 0 0;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--panel-solid);
  color: var(--muted);
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  z-index: 2;
}
#panel-close:hover, #about-close:hover { color: var(--text); }
#panel-body { padding: 18px 22px 40px; line-height: 1.5; }
#panel-body h2 {
  font-family: 'Space Grotesk', sans-serif;
  font-size: 1.55rem;
  margin: 6px 0 2px;
  line-height: 1.2;
}
#panel-body h3 {
  font-family: 'Space Grotesk', sans-serif;
  font-size: 1.02rem;
  font-weight: 500;
  color: #c9cfe6;
  margin: 22px 0 8px;
  padding-bottom: 4px;
  border-bottom: 1px solid var(--line);
}
#panel-body h4 {
  font-size: .78rem;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--faint);
  margin: 12px 0 4px;
  font-weight: 500;
}
#panel-body p { margin: 6px 0; }
#panel-body ul { margin: 4px 0; padding-left: 20px; }
#panel-body li { margin: 4px 0; }
.full { color: var(--muted); margin: 0 0 8px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 4px 0; }
.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: .78rem;
  padding: 2px 8px;
  border-radius: 999px;
  border: 1px solid var(--line);
  color: var(--muted);
  background: #161a28;
}
.chip.cat { color: var(--text); }
a.chip, button.chip { cursor: pointer; text-decoration: none; }
a.chip:hover, button.chip:hover { color: var(--text); border-color: #3a4462; }

.def {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 14px;
}
.def dt { font-size: .78rem; text-transform: uppercase; letter-spacing: .06em; color: var(--faint); margin-top: 6px; }
.def dt:first-child { margin-top: 0; }
.def dd { margin: 2px 0 0; }

.chain { display: flex; flex-wrap: wrap; align-items: center; gap: 4px; font-size: .88rem; }
.chain .arrow { color: var(--faint); }
.nlink {
  background: none;
  border: 0;
  padding: 0;
  color: var(--link);
  cursor: pointer;
  text-decoration: underline;
  text-decoration-color: rgba(159, 176, 216, .35);
  text-underline-offset: 2px;
  font: inherit;
}
.nlink:hover { color: var(--link-hover); }

details.red {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  margin: 8px 0;
}
details.red > summary {
  list-style: none;
  cursor: pointer;
  padding: 10px 12px;
  display: grid;
  grid-template-columns: 14px 1fr;
  gap: 2px 8px;
}
details.red > summary::-webkit-details-marker { display: none; }
details.red > summary::before {
  content: "\25B8";
  color: var(--faint);
  grid-row: 1;
  align-self: center;
  justify-self: center;
  line-height: 1;
  transition: transform .15s;
}
details.red[open] > summary::before { transform: rotate(90deg); }
.red-head { font-weight: 500; }
.red-head .from { color: var(--faint); font-weight: 400; }
.red-sub { grid-column: 2; color: var(--muted); font-size: .86rem; }
.red-body { padding: 0 14px 12px 34px; font-size: .93rem; }
.idea { color: #d6dbef; font-style: italic; }
.note {
  border-left: 2px solid #4a5680;
  padding: 2px 0 2px 10px;
  color: var(--muted);
  font-size: .88rem;
}
.meta { color: var(--faint); font-size: .82rem; margin-top: 8px; }
.verify {
  margin-top: 10px;
  font-size: .82rem;
  padding: 6px 10px;
  border-radius: 8px;
  background: #151a26;
  border: 1px solid var(--line);
  color: var(--muted);
}
.verify.ok { border-color: rgba(127, 212, 154, .35); color: #b9e6c6; }
.verify.ok b { color: var(--ok); }
.tech { font-size: .72rem; color: var(--faint); border: 1px solid var(--line); border-radius: 999px; padding: 0 7px; margin-left: 4px; white-space: nowrap; }
.primary-tag { font-size: .72rem; color: var(--sat); margin-left: 4px; white-space: nowrap; }

.refs { font-size: .84rem; color: var(--muted); padding-left: 18px !important; }
.refs li { margin: 5px 0; }
.empty { color: var(--faint); font-style: italic; }
.more { font-size: .86rem; }
.more summary { cursor: pointer; color: var(--link); margin: 4px 0; }

/* ----------------------------------------------------------------- about */
#about {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: rgba(5, 6, 10, .6);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
#about[hidden] { display: none; }
.about-card {
  width: min(680px, 100%);
  max-height: 86vh;
  overflow-y: auto;
  background: var(--panel-solid);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 0 26px 22px;
  line-height: 1.55;
  box-shadow: 0 20px 60px rgba(0, 0, 0, .6);
}
.about-card h2 { font-family: 'Space Grotesk', sans-serif; margin: 22px 0 8px; }
.about-card h3 { font-family: 'Space Grotesk', sans-serif; font-weight: 500; font-size: 1.05rem; margin: 18px 0 4px; color: #c9cfe6; }
.about-card p { color: #cdd2e6; margin: 8px 0; }
.nowrap { white-space: nowrap; }

/* --------------------------------------------------------------- tooltip */
#tooltip {
  position: fixed;
  z-index: 7;
  pointer-events: none;
  max-width: 300px;
  background: var(--panel-solid);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 6px 10px;
  font-size: .84rem;
  color: var(--text);
  box-shadow: 0 6px 20px rgba(0, 0, 0, .5);
}
#tooltip .sub { color: var(--muted); display: block; margin-top: 2px; }

.katex { font-size: 1.02em; }

@media (max-width: 1180px) {
  body.panel-open #tagline { display: none; }
}

/* ---------------------------------------------------------------- mobile */
@media (max-width: 760px) {
  :root { --panel-w: 100vw; }
  body { font-size: 14px; }
  #top { top: 10px; left: 16px; max-width: 60vw; }
  #top h1 { font-size: 1.5rem; margin-top: 6px; }
  .logo { width: 26px; height: 26px; }
  .brand { gap: 8px; }
  #tagline { display: none; }
  #controls { top: 10px; right: 16px; }
  #controls .row { margin-top: 14px; gap: 6px; }
  body.panel-open #controls { right: 16px; }
  .label { font-size: 10.5px; padding: 1px 3px; }
  .label.root { font-size: 13px; }
  #search { width: 150px; }
  #search-results { width: min(320px, calc(100vw - 32px)); }
  .seg-label { display: none; }
  .seg button, .toggle { padding: 6px 8px; }
  #legend { left: 16px; bottom: 12px; }
  #legend-toggle {
    display: inline-block;
    border: 1px solid var(--line);
    background: rgba(18, 21, 32, .9);
    border-radius: 8px;
    padding: 5px 10px;
    color: var(--muted);
    cursor: pointer;
  }
  #legend-body { display: none; margin-top: 8px; background: rgba(18, 21, 32, .95); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; }
  #legend.open #legend-body { display: block; }
  #panel {
    top: auto;
    left: 0;
    width: 100vw;
    height: 62vh;
    border-left: 0;
    border-top: 1px solid var(--line);
    border-radius: 16px 16px 0 0;
  }
  #panel-body { padding: 14px 16px 40px; }
  body.panel-open #legend { display: none; }
}
