:root {
  --bg: #07090d;
  --bg-2: #0d1117;
  --panel: #10151d;
  --panel-2: #161d27;
  --line: #243041;
  --text: #dfe7f1;
  --muted: #8a98ab;
  --accent: #5ee0c1;
  --accent-2: #ff6fae;
  --warn: #ffb454;
  --good: #7ee787;
  --radius: 10px;
  --sidebar-w: 248px;
  --panel-w: 340px;
  --top-h: 52px;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --sans: Inter, "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font: 14px/1.45 var(--sans); }
body { overflow: hidden; }
button, input, select { font: inherit; color: inherit; }
a { color: var(--accent); }
.mono { font-family: var(--mono); font-size: 12px; }

#app {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr var(--panel-w);
  grid-template-rows: var(--top-h) 1fr;
  grid-template-areas: "top top top" "side view panel";
  height: 100vh;
  height: 100dvh;
}

/* ---------------------------------------------------------------- topbar */
#topbar {
  grid-area: top;
  display: flex; align-items: center; gap: 14px;
  padding: 0 12px;
  background: linear-gradient(180deg, #0f141c, #0b0f15);
  border-bottom: 1px solid var(--line);
  min-width: 0;
}
.brand { display: flex; align-items: center; gap: 9px; font-weight: 650; letter-spacing: .2px; white-space: nowrap; }
.brand-mark {
  width: 22px; height: 22px; border-radius: 50%;
  background: radial-gradient(circle at 38% 36%, #b6fff0 0 12%, #37c9a8 13% 42%, #13493f 43% 58%, transparent 59%),
              radial-gradient(circle at 50% 50%, #ff6fae55 0 70%, transparent 71%);
  box-shadow: 0 0 12px #5ee0c155;
}
#scene-heading { display: flex; align-items: center; gap: 10px; min-width: 0; flex: 1; }
#scene-heading h1 { font-size: 15px; font-weight: 600; margin: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chip {
  font-size: 11px; font-weight: 600; letter-spacing: .3px; text-transform: uppercase;
  padding: 3px 8px; border-radius: 999px; border: 1px solid var(--line); color: var(--accent);
  white-space: nowrap;
}
.chip:empty { display: none; }
.toolbar { display: flex; align-items: center; gap: 8px; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg button { background: transparent; border: 0; padding: 6px 11px; cursor: pointer; color: var(--muted); }
.seg button + button { border-left: 1px solid var(--line); }
.seg button.active { background: var(--panel-2); color: var(--text); box-shadow: inset 0 -2px 0 var(--accent); }
.icon-btn {
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px;
  min-width: 34px; height: 32px; padding: 0 8px; cursor: pointer; line-height: 1;
}
.icon-btn:hover { border-color: #3a4a61; }
#nav-toggle, #panel-toggle { display: none; }

/* --------------------------------------------------------------- sidebar */
#sidebar {
  grid-area: side; overflow-y: auto; background: var(--bg-2);
  border-right: 1px solid var(--line); padding: 10px 8px 24px;
}
.nav-group { margin: 10px 4px 4px; font-size: 11px; letter-spacing: .6px; text-transform: uppercase; color: var(--muted); }
.nav-item {
  display: block; width: 100%; text-align: left; background: transparent; border: 1px solid transparent;
  border-radius: 8px; padding: 7px 9px; cursor: pointer; color: var(--text);
}
.nav-item small { display: block; color: var(--muted); font-size: 11.5px; margin-top: 1px; }
.nav-item:hover { background: var(--panel); }
.nav-item.active { background: var(--panel-2); border-color: var(--line); box-shadow: inset 3px 0 0 var(--accent); }

/* -------------------------------------------------------------- viewport */
#viewport { grid-area: view; position: relative; min-width: 0; min-height: 0; background: #000; }
#gl { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }
#hud { position: absolute; inset: 0; pointer-events: none; }
#hud-legend {
  position: absolute; left: 12px; top: 12px; display: flex; flex-direction: column; gap: 4px;
  font-size: 12px; background: #0a0e14cc; border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px;
  max-width: min(320px, 60%);
}
#hud-legend:empty { display: none; }
.legend-row { display: flex; align-items: center; gap: 7px; }
.legend-sw { width: 11px; height: 11px; border-radius: 3px; flex: none; }
#hud-tip {
  position: absolute; padding: 7px 9px; font-size: 12px; background: #0a0e14ee; border: 1px solid var(--line);
  border-radius: 8px; max-width: 280px; transform: translate(12px, 12px); white-space: pre-line;
}
#transport {
  position: absolute; left: 50%; bottom: 12px; transform: translateX(-50%);
  display: flex; align-items: center; gap: 10px; padding: 6px 10px;
  background: #0a0e14dd; border: 1px solid var(--line); border-radius: 12px; backdrop-filter: blur(6px);
  max-width: calc(100% - 24px); flex-wrap: wrap; justify-content: center;
}
#transport label { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); font-size: 12px; }
#transport input[type=range] { width: 96px; }
.loading {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; gap: 12px;
  background: #000a; color: var(--muted); font-size: 13px;
}
.loading[hidden] { display: none; }
.spinner { width: 22px; height: 22px; border-radius: 50%; border: 2px solid var(--line); border-top-color: var(--accent); animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ----------------------------------------------------------------- panel */
#panel { grid-area: panel; overflow-y: auto; background: var(--bg-2); border-left: 1px solid var(--line); padding: 12px 12px 40px; }
#scene-about h2 { font-size: 13px; margin: 0 0 6px; color: var(--accent); text-transform: uppercase; letter-spacing: .5px; }
#scene-about p { margin: 0 0 8px; color: #c4cfdc; }
#scene-about .paper-ref { font-size: 12px; color: var(--muted); border-left: 2px solid var(--accent-2); padding: 2px 0 2px 8px; margin: 8px 0; }
.ctl-section { border-top: 1px solid var(--line); padding-top: 10px; margin-top: 12px; }
.ctl-section > h3 { font-size: 12px; margin: 0 0 8px; color: var(--muted); text-transform: uppercase; letter-spacing: .5px; font-weight: 600; }
.ctl { margin: 8px 0; }
.ctl-row { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; font-size: 12.5px; }
.ctl-row .val { font-family: var(--mono); font-size: 11.5px; color: var(--accent); }
.ctl input[type=range] { width: 100%; margin: 4px 0 0; accent-color: var(--accent); }
.ctl select { width: 100%; background: var(--panel-2); border: 1px solid var(--line); border-radius: 7px; padding: 5px 7px; }
.ctl-toggle { display: flex; align-items: center; gap: 8px; font-size: 12.5px; cursor: pointer; }
.ctl-toggle input { accent-color: var(--accent); }
.btn {
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px; cursor: pointer;
}
.btn:hover { border-color: #3a4a61; }
.btn.primary { border-color: #2f7f6d; background: #123a33; }
.btn-row { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0; }
.readouts { display: grid; grid-template-columns: 1fr auto; gap: 3px 10px; font-size: 12.5px; }
.readouts .k { color: var(--muted); }
.readouts .v { font-family: var(--mono); font-size: 12px; text-align: right; }
.chart { width: 100%; height: 150px; display: block; background: #0a0e14; border: 1px solid var(--line); border-radius: 8px; }
.chart-wrap { margin: 8px 0; }
.chart-title { font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.eq {
  font-family: var(--mono); font-size: 12px; background: #0a0e14; border: 1px solid var(--line);
  border-radius: 8px; padding: 8px 10px; white-space: pre-wrap; color: #cfe9ff; overflow-x: auto;
}
.note { font-size: 12px; color: var(--muted); }
.tag { display: inline-block; font-size: 11px; padding: 1px 6px; border-radius: 6px; border: 1px solid var(--line); margin: 0 4px 4px 0; color: #c4cfdc; }
table.data { width: 100%; border-collapse: collapse; font-size: 12px; }
table.data td, table.data th { border-bottom: 1px solid var(--line); padding: 4px 4px; text-align: left; }
table.data th { color: var(--muted); font-weight: 600; }
table.data td.num { text-align: right; font-family: var(--mono); }

/* ---------------------------------------------------------- responsive */
@media (max-width: 1100px) {
  :root { --panel-w: 300px; --sidebar-w: 220px; }
}
@media (max-width: 860px) {
  #app { grid-template-columns: 1fr; grid-template-areas: "top" "view"; }
  #nav-toggle, #panel-toggle { display: inline-block; }
  .brand-name { display: none; }
  #sidebar, #panel {
    position: fixed; top: var(--top-h); bottom: 0; z-index: 20; width: min(86vw, 340px);
    transition: transform .2s ease; box-shadow: 0 0 30px #000;
  }
  #sidebar { left: 0; transform: translateX(-105%); }
  #panel { right: 0; transform: translateX(105%); }
  body.nav-open #sidebar { transform: none; }
  body.panel-open #panel { transform: none; }
  #transport { bottom: 8px; gap: 6px; }
  #scene-order { display: none; }
  #hud-legend { font-size: 11px; max-height: 30vh; overflow-y: auto; padding: 6px 8px; max-width: 70%; }
  #transport input[type=range] { width: 70px; }
  .seg button { padding: 6px 8px; }
}
