:root {
  --bg-main: #070B14;
  --bg-secondary: #0C1320;
  --bg-toolbar: #101A2A;
  --bg-panel: #111C2C;
  --border: #26364A;
  --text: #E8EEF7;
  --text-sec: #98A8BC;
  --text-muted: #718096;
  --axes: #D7E0EA;
  --grid: #526174;
  --focus: #38BDF8;
  --sel-bg: #24466B;
  --sel-text: #F8FAFC;
  --danger: #FB7185;
  --ok: #2DD4BF;
  --radius: 10px;
  --shadow: 0 8px 32px rgba(0,0,0,0.45);
  --font: "Inter", "Segoe UI", system-ui, -apple-system, sans-serif;
  --mono: "JetBrains Mono", "Fira Code", ui-monospace, monospace;
  --toolbar-h: 64px;
  --timebar-h: 72px;
  --drawer-w: 320px;
  --base-d: 230px;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  background: var(--bg-main);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.45;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

button, input, select, textarea {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--bg-panel);
  color: var(--text);
  border-radius: 8px;
  padding: 6px 12px;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}
button:hover { border-color: var(--focus); background: #152336; }
button:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}
button.active, button[aria-pressed="true"] {
  background: var(--sel-bg);
  color: var(--sel-text);
  border-color: var(--focus);
}
button:disabled { opacity: 0.4; cursor: not-allowed; }

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  height: 6px;
  border-radius: 3px;
  background: #1a2a3d;
  outline: none;
  flex: 1;
  min-width: 80px;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 16px; height: 16px;
  border-radius: 50%;
  background: var(--focus);
  border: 2px solid #fff;
  cursor: pointer;
}
input[type="range"]:focus-visible { outline: 2px solid var(--focus); outline-offset: 3px; }

select {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 5px 8px;
  color: var(--text);
}
select:focus-visible { outline: 2px solid var(--focus); }

label { color: var(--text-sec); font-size: 12px; }

/* ── App shell ── */
#app {
  display: grid;
  grid-template-rows: var(--toolbar-h) 1fr auto;
  height: 100vh;
  width: 100vw;
  position: relative;
}

/* ── Toolbar ── */
#toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 14px;
  background: var(--bg-toolbar);
  border-bottom: 1px solid var(--border);
  z-index: 40;
  flex-wrap: wrap;
  min-height: var(--toolbar-h);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  flex: 0 1 auto;
}
.brand-text { min-width: 0; }
.brand h1 {
  font-size: 15px;
  font-weight: 650;
  letter-spacing: 0.01em;
  white-space: nowrap;
}
.brand .subtitle {
  font-size: 11px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 340px;
}

.btn-icon {
  width: 34px; height: 34px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  font-weight: 600;
  font-size: 13px;
}
.btn-icon.info {
  background: #16304a;
  border-color: #2a5a80;
  color: var(--focus);
}

.mode-switch {
  display: inline-flex;
  background: #0a1420;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 3px;
  margin: 0 auto;
  flex-shrink: 0;
}
.mode-switch button {
  border: none;
  background: transparent;
  border-radius: 8px;
  padding: 7px 14px;
  font-size: 13px;
  font-weight: 550;
  color: var(--text-sec);
}
.mode-switch button[aria-pressed="true"] {
  background: var(--sel-bg);
  color: var(--sel-text);
  box-shadow: 0 0 0 1px rgba(56,189,248,0.35);
}

.toolbar-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-left: auto;
  flex-wrap: wrap;
}
.toolbar-actions button { font-size: 12px; padding: 6px 10px; white-space: nowrap; }

/* ── Main stage ── */
#stage-wrap {
  position: relative;
  min-height: 0;
  background: radial-gradient(ellipse at 50% 40%, #0e1a2c 0%, var(--bg-main) 70%);
  overflow: hidden;
}

#main-canvas {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

.hud {
  position: absolute;
  pointer-events: none;
  z-index: 5;
  font-family: var(--mono);
  font-size: 11px;
  color: var(--text-sec);
  background: rgba(12, 19, 32, 0.72);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  backdrop-filter: blur(6px);
  line-height: 1.55;
}
.hud strong { color: var(--text); font-weight: 600; }
#live-readout {
  top: 12px;
  right: 12px;
  min-width: 180px;
}
#mode-badge {
  top: 12px;
  left: 12px;
  pointer-events: none;
  font-family: var(--font);
  font-size: 12px;
  color: var(--text-sec);
}
#mode-badge .title { color: var(--text); font-weight: 600; font-size: 13px; }

.overlay-toggles {
  position: absolute;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 8px;
  z-index: 6;
  pointer-events: auto;
  flex-wrap: wrap;
  justify-content: center;
  max-width: 90%;
}
.overlay-toggles label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: rgba(17, 28, 44, 0.88);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 5px 10px;
  font-size: 12px;
  color: var(--text-sec);
  cursor: pointer;
  user-select: none;
}
.overlay-toggles input { accent-color: var(--focus); }

/* ── Base S² inset ── */
#base-inset {
  position: absolute;
  left: 14px;
  bottom: 14px;
  width: var(--base-d);
  height: calc(var(--base-d) + 48px);
  z-index: 15;
  pointer-events: auto;
  display: flex;
  flex-direction: column;
  background: rgba(12, 19, 32, 0.88);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  overflow: hidden;
}
#base-inset .base-header {
  padding: 8px 10px 4px;
  flex-shrink: 0;
}
#base-inset .base-header h2 {
  font-size: 12px;
  font-weight: 650;
  color: var(--text);
}
#base-inset .base-header p {
  font-size: 10.5px;
  color: var(--text-muted);
  margin-top: 1px;
}
#base-canvas {
  flex: 1;
  width: 100%;
  min-height: 0;
  display: block;
  touch-action: none;
  cursor: grab;
}
#base-canvas:active { cursor: grabbing; }

/* ── Time bar ── */
#timebar {
  display: none;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  background: var(--bg-toolbar);
  border-top: 1px solid var(--border);
  z-index: 30;
  min-height: var(--timebar-h);
}
#timebar.visible { display: grid; }

.time-left, .time-right {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}
.time-center {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}
#t-display {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--focus);
  min-width: 70px;
  text-align: right;
}
.seg-btn {
  width: 36px;
  padding: 6px 0;
  font-size: 11px;
  font-family: var(--mono);
}

/* Time-slice tools — compact panel on the right, clear of the main sphere */
#s3-tools {
  display: none;
  position: absolute;
  top: auto;
  bottom: 14px;
  right: 12px;
  z-index: 16;
  background: rgba(17, 28, 44, 0.92);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px;
  box-shadow: var(--shadow);
  flex-direction: column;
  gap: 5px;
  width: 132px;
  backdrop-filter: blur(6px);
}
#s3-tools.visible { display: flex; }
#s3-tools .tool-title {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  font-weight: 650;
  padding: 0 2px 2px;
}
#s3-tools > button {
  width: 100%;
  font-size: 11.5px;
  padding: 6px 8px;
  text-align: left;
}
#t-select-row {
  display: none;
  flex-direction: column;
  gap: 4px;
  padding-top: 4px;
  border-top: 1px solid var(--border);
  margin-top: 2px;
}
#t-select-row.visible { display: flex; }
#t-select-row label {
  font-size: 10px;
  color: var(--text-muted);
}
#t-select-row input[type="range"] {
  width: 100%;
  min-width: 0;
}
#t-select-row span {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--text-sec);
}

/* ── Right drawer ── */
#drawer {
  position: fixed;
  top: var(--toolbar-h);
  right: 0;
  bottom: 0;
  width: var(--drawer-w);
  max-width: 100vw;
  background: var(--bg-panel);
  border-left: 1px solid var(--border);
  z-index: 50;
  transform: translateX(100%);
  transition: transform 0.22s ease;
  display: flex;
  flex-direction: column;
  box-shadow: -8px 0 32px rgba(0,0,0,0.35);
}
#drawer.open { transform: translateX(0); }
#drawer header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
#drawer header h2 { font-size: 14px; font-weight: 650; }
#drawer .drawer-body {
  overflow-y: auto;
  padding: 12px;
  flex: 1;
  min-height: 0;
}
.section-title {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  margin: 14px 0 8px;
  font-weight: 650;
}
.section-title:first-child { margin-top: 0; }

.preset-card {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px;
  margin-bottom: 8px;
}
.preset-card h3 {
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 4px;
}
.preset-card p {
  font-size: 11.5px;
  color: var(--text-sec);
  margin-bottom: 8px;
  line-height: 1.4;
}
.preset-thumb {
  width: 100%;
  height: 48px;
  border-radius: 6px;
  margin-bottom: 8px;
  background: linear-gradient(135deg, #132033, #1a2f48);
  border: 1px solid var(--border);
  position: relative;
  overflow: hidden;
}
.preset-thumb svg { width: 100%; height: 100%; display: block; }
.preset-actions {
  display: flex;
  gap: 6px;
  align-items: center;
  flex-wrap: wrap;
}
.preset-actions button { font-size: 11px; padding: 4px 10px; }
.preset-actions .replace-add {
  display: flex;
  gap: 4px;
  margin-left: auto;
  font-size: 11px;
  color: var(--text-muted);
}
.preset-actions .replace-add label {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  cursor: pointer;
}

/* Fiber list */
.fiber-row {
  display: grid;
  grid-template-columns: 14px 1fr auto;
  gap: 6px;
  align-items: center;
  padding: 6px 8px;
  border-radius: 8px;
  border: 1px solid transparent;
  margin-bottom: 4px;
  background: var(--bg-secondary);
}
.fiber-row.selected {
  border-color: var(--focus);
  background: #152a40;
}
.fiber-row .swatch {
  width: 12px; height: 12px;
  border-radius: 50%;
  border: 1px solid rgba(255,255,255,0.35);
}
.fiber-row .meta {
  min-width: 0;
  font-size: 11px;
}
.fiber-row .meta .name { font-weight: 600; color: var(--text); }
.fiber-row .meta .coords {
  font-family: var(--mono);
  font-size: 10px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.fiber-row .actions {
  display: flex;
  gap: 3px;
}
.fiber-row .actions button {
  padding: 3px 6px;
  font-size: 10px;
  min-width: 0;
}

/* ── Info modal ── */
#info-overlay {
  position: fixed;
  inset: 0;
  background: rgba(4, 8, 16, 0.72);
  z-index: 100;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 24px;
  backdrop-filter: blur(4px);
}
#info-overlay.open { display: flex; }
#info-panel {
  width: min(720px, 100%);
  max-height: min(88vh, 900px);
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
#info-panel header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
#info-panel header h2 { font-size: 16px; }
#info-content {
  overflow-y: auto;
  padding: 8px 18px 20px;
  flex: 1;
}
.info-section {
  border-bottom: 1px solid var(--border);
}
.info-section:last-child { border-bottom: none; }
.info-section summary {
  cursor: pointer;
  padding: 12px 4px;
  font-weight: 600;
  font-size: 13.5px;
  list-style: none;
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text);
  user-select: none;
}
.info-section summary::-webkit-details-marker { display: none; }
.info-section summary::before {
  content: "▸";
  color: var(--focus);
  font-size: 12px;
  transition: transform 0.15s;
}
.info-section[open] summary::before { transform: rotate(90deg); }
.info-section .body {
  padding: 0 4px 14px 18px;
  color: var(--text-sec);
  font-size: 13px;
  line-height: 1.55;
}
.info-section .body p { margin-bottom: 8px; }
.info-section .body .callout {
  background: #152a40;
  border-left: 3px solid var(--focus);
  padding: 8px 12px;
  border-radius: 0 8px 8px 0;
  color: var(--text);
  font-weight: 550;
  margin: 10px 0;
}
.eq {
  font-family: var(--mono);
  font-size: 12px;
  background: #0a1420;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  margin: 8px 0;
  overflow-x: auto;
  color: #c8d8ea;
  white-space: pre-wrap;
  line-height: 1.5;
}
.eq .hl { color: var(--ok); }

/* ── Toast ── */
#toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: #1a3048;
  border: 1px solid var(--focus);
  color: var(--text);
  padding: 8px 16px;
  border-radius: 8px;
  font-size: 12px;
  z-index: 200;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
}
#toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ── Drawer backdrop (mobile) ── */
#drawer-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  z-index: 45;
}
#drawer-backdrop.show { display: block; }

/* ── Responsive ── */
@media (max-width: 900px) {
  :root {
    --base-d: 160px;
    --toolbar-h: auto;
  }
  #toolbar {
    padding: 8px 10px;
    gap: 8px;
  }
  .brand .subtitle { display: none; }
  .mode-switch button { padding: 6px 10px; font-size: 12px; }
  #drawer {
    top: auto;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    height: min(70vh, 520px);
    border-left: none;
    border-top: 1px solid var(--border);
    border-radius: 16px 16px 0 0;
    transform: translateY(100%);
  }
  #drawer.open { transform: translateY(0); }
  #timebar {
    grid-template-columns: 1fr;
    gap: 8px;
    padding: 8px 12px;
  }
  .time-left, .time-right, .time-center {
    width: 100%;
    justify-content: center;
  }
  #live-readout { font-size: 10px; min-width: 140px; }
  #base-inset { left: 8px; bottom: 8px; }
  #s3-tools {
    top: auto;
    bottom: 8px;
    right: 8px;
    width: 120px;
  }
}

@media (max-width: 480px) {
  :root { --base-d: 145px; }
  .toolbar-actions button .label { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}

/* utility */
.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0,0,0,0);
  border: 0;
}
.hidden { display: none !important; }

.hud-note {
  margin-top: 4px;
  font-size: 10px;
  color: var(--text-muted);
  max-width: 220px;
  font-family: var(--font);
  line-height: 1.35;
}
