:root {
  color-scheme: dark;
  --bg: #0c1022;
  --panel: #131a30;
  --panel2: #0f1528;
  --accent: #2fd3c4;
  --gold: #f2c14e;
  --text: #e8eefc;
  --muted: #9fb6c9;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  overflow-x: hidden; /* timelines scroll internally; the page never does */
}

#app {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 14px 12px 40px;
}

/* ---- controls ---- */
#controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px 14px;
  width: 100%;
  max-width: 1200px;
  justify-content: center;
}
.ctl { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }
.ctl.chk { gap: 5px; }
.ctl select {
  background: var(--panel); color: var(--text); border: 1px solid #27324f;
  border-radius: 8px; padding: 6px 8px; font-size: 13px;
}
.ctl input[type="checkbox"] { accent-color: var(--accent); width: 16px; height: 16px; }
.note { flex-basis: 100%; text-align: center; font-size: 11px; opacity: 0.6; }

button {
  background: var(--accent); color: #06201d; border: 0; border-radius: 10px;
  padding: 9px 18px; font-size: 14px; font-weight: 700; cursor: pointer;
}
button.ghost { background: transparent; color: var(--accent); border: 1px solid #27423f; }

/* ---- stage / panels ---- */
#stageWrap { position: relative; width: 100%; max-width: 1200px; display: flex; justify-content: center; }
#stage {
  display: flex; gap: 10px; width: 100%; justify-content: center;
}
.panel { position: relative; flex: 1 1 0; min-width: 0; }
.panel canvas {
  width: 100%; aspect-ratio: 16 / 9; border-radius: 14px; background: #0c1022; display: block;
}
#stage.side .panel canvas { aspect-ratio: 356 / 640; }

.panel-label {
  position: absolute; top: 8px; left: 8px;
  font-size: 12px; font-weight: 700; letter-spacing: 0.03em; color: var(--accent);
  background: rgba(0, 0, 0, 0.4); padding: 3px 8px; border-radius: 6px;
}
.tok-id {
  position: absolute; top: 8px; right: 8px;
  font-size: 11px; font-weight: 700; font-variant-numeric: tabular-nums;
  color: var(--gold); background: rgba(0, 0, 0, 0.4); padding: 3px 7px; border-radius: 6px;
}
.time-readout {
  position: absolute; bottom: 8px; left: 50%; transform: translateX(-50%);
  font-size: 12px; font-variant-numeric: tabular-nums; color: var(--text);
  background: rgba(0, 0, 0, 0.45); padding: 3px 10px; border-radius: 6px;
}

/* Narrow side-by-side panels: compact label + stack the token badge beneath it
   (both pinned top-left) so '1 Rhubarb (9)' never collides with the token id. */
@media (max-width: 560px) {
  #stage.side .panel-label { font-size: 9px; padding: 2px 5px; left: 5px; top: 5px; }
  #stage.side .tok-id {
    top: 24px; left: 5px; right: auto; font-size: 10px; padding: 2px 5px;
  }
}

/* ---- timeline zoom/scroll toolbar ---- */
.tlbar {
  width: 100%; max-width: 1200px; display: flex; align-items: center; gap: 6px;
  font-size: 12px; color: var(--muted);
}
.tlbar:empty { display: none; }
.tlbtn {
  background: var(--panel); color: var(--text); border: 1px solid #27324f;
  border-radius: 8px; padding: 4px 10px; font-size: 14px; font-weight: 700;
  line-height: 1; min-width: 30px;
}

/* ---- timelines ---- */
#timelines { width: 100%; max-width: 1200px; display: flex; flex-direction: column; gap: 14px; }
.tl-block { background: var(--panel2); border: 1px solid #1c263f; border-radius: 12px; padding: 8px 10px; overflow: hidden; }
.tl-label { font-size: 12px; font-weight: 700; color: var(--accent); margin-bottom: 6px; }
.timeline { display: flex; flex-direction: column; }
.tl-scroll {
  overflow-x: auto; overflow-y: hidden; width: 100%;
  scrollbar-width: thin; scrollbar-color: #2b3b58 transparent;
  overscroll-behavior-x: contain;
}
.tl-scroll::-webkit-scrollbar { height: 3px; }
.tl-scroll::-webkit-scrollbar-track { background: transparent; }
.tl-scroll::-webkit-scrollbar-thumb { background: #2b3b58; border-radius: 3px; }
.tl-content { position: relative; display: flex; flex-direction: column; gap: 3px; min-width: 100%; }
.tl-playhead {
  position: absolute; top: 0; bottom: 0; left: 0; width: 2px;
  background: var(--gold); opacity: 0.85; pointer-events: none; z-index: 5;
}
.seg-strip { display: flex; height: 40px; gap: 1px; align-items: stretch; }
.seg {
  position: relative; padding: 0; border: 0; border-radius: 4px; overflow: hidden;
  background: #0d1426; cursor: pointer; min-width: 3px;
}
.seg-thumb { width: 100%; height: 100%; display: block; object-fit: cover; }
.seg-conf { position: absolute; left: 0; bottom: 0; width: 100%; background: var(--accent); opacity: 0.8; }
.seg-id {
  position: absolute; top: 1px; left: 2px; font-size: 8px; color: #cfe; opacity: 0.75;
  font-variant-numeric: tabular-nums;
}
.phone-row { display: flex; height: 14px; gap: 1px; }
.phone {
  font-size: 9px; color: var(--muted); text-align: center; overflow: hidden; white-space: nowrap;
  border-left: 1px solid #1c263f; min-width: 2px;
}

/* ---- alternatives popover (appended to <body>, clamped into viewport) ---- */
.popover {
  position: fixed; z-index: 60; top: 0; left: 0;
  background: #0c1326; border: 1px solid #27324f; border-radius: 10px; padding: 8px;
  box-shadow: 0 12px 30px rgba(0,0,0,0.5); width: max-content; max-width: 260px;
}
/* Mobile: dock as a bottom sheet so it is always fully on-screen. */
.popover.sheet {
  top: auto; bottom: 0; left: 0; right: 0; width: auto; max-width: none;
  border-radius: 14px 14px 0 0; padding: 12px 14px calc(14px + env(safe-area-inset-bottom));
  box-shadow: 0 -12px 30px rgba(0,0,0,0.55);
}
.popover.sheet .pop-alts { flex-wrap: wrap; }
.pop-head { font-size: 11px; color: var(--muted); margin-bottom: 6px; font-variant-numeric: tabular-nums; }
.pop-alts { display: flex; gap: 5px; }
.alt { padding: 2px; background: #0d1426; border: 1px solid #1c263f; border-radius: 6px; cursor: pointer; display: flex; flex-direction: column; align-items: center; gap: 2px; }
.alt.chosen { border-color: var(--accent); }
.alt canvas { display: block; border-radius: 3px; }
.alt-score { font-size: 9px; color: var(--muted); }
.pop-lib { margin-top: 7px; width: 100%; background: transparent; color: var(--accent); border: 1px solid #27423f; font-size: 12px; padding: 6px; }

/* ---- library picker modal ---- */
.picker {
  position: fixed; inset: 0; background: rgba(4, 7, 16, 0.72);
  display: flex; align-items: center; justify-content: center; z-index: 50; padding: 20px;
}
.picker.hidden { display: none; }
.picker-grid {
  background: #0c1326; border: 1px solid #27324f; border-radius: 14px; padding: 14px;
  display: grid; grid-template-columns: repeat(auto-fill, 86px); gap: 8px;
  max-height: 80vh; overflow: auto;
}
.pick {
  padding: 4px; background: #0d1426; border: 1px solid #1c263f; border-radius: 8px;
  cursor: pointer; display: flex; flex-direction: column; align-items: center; gap: 3px;
}
.pick.current { border-color: var(--accent); }
.pick span { font-size: 10px; color: var(--muted); font-variant-numeric: tabular-nums; }

/* ---- capture: fixed 1280x720, no chrome ---- */
body.capture { background: #0c1022; }
body.capture #controls, body.capture #timelines, body.capture #audio { display: none !important; }
body.capture #app { padding: 0; }
body.capture #stageWrap { width: 1280px; height: 720px; }
body.capture #stage { width: 1280px; height: 720px; gap: 0; }
body.capture .panel canvas { width: 100%; height: 720px; border-radius: 0; aspect-ratio: auto; }
body.capture #stage.side .panel { width: 426.66px; }
body.capture #stage:not(.side) .panel canvas { width: 1280px; }
body.capture .time-readout { bottom: 12px; font-size: 14px; }
