:root {
  --bg: #0d1117;
  --panel: #161b22;
  --panel-2: #1c2230;
  --border: #2a313c;
  --text: #e6edf3;
  --muted: #8b949e;
  --accent: #ffb020;
  --accent-2: #2f81f7;
  --fav: #ff5c8a;
  --footage: #2f6feb;
  --footage-dim: #1d3a6e;
  --ok: #3fb950;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.4 -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  overflow: hidden;
}

#app {
  display: grid;
  grid-template-columns: 1fr 320px;
  grid-template-rows: 48px 1fr;
  grid-template-areas:
    "top    top"
    "stage  side";
  /* dvh, not vh: on mobile browsers vh includes the collapsing URL bar, which
     leaves the controls row cut off below the fold until the user scrolls. */
  height: 100dvh;
}

/* ---------- Top bar ---------- */
#topbar {
  grid-area: top;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; letter-spacing: .3px; display: flex; align-items: center; gap: 8px; }
/* Also the restricted-mode switch: no pointer cursor and no tooltip, so there is
   nothing about it to notice. Turns amber while unlocked. */
.brand .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--ok); box-shadow: 0 0 8px var(--ok); user-select: none; }
body.unlocked .brand .dot { background: var(--accent); box-shadow: 0 0 8px var(--accent); }
/* Master tier gets its own colour, so which password is in play is obvious at a
   glance to anyone who already knows the dot means something. */
body.master .brand .dot { background: var(--fav); box-shadow: 0 0 8px var(--fav); }
#clock { margin-left: auto; text-align: right; font-variant-numeric: tabular-nums; }
#clockTime { font-size: 16px; font-weight: 600; }
#clockMeta { color: var(--muted); margin-left: 8px; font-size: 12px; }
.status { color: var(--muted); font-size: 12px; min-width: 90px; text-align: right; }
.status.err { color: var(--fav); }

/* ---------- Stage ---------- */
#stage {
  grid-area: stage;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  padding: 12px;
  gap: 10px;
}
#videoWrap {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  background: #000;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid var(--border);
}
.vid {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #000;
  opacity: 0;
  transition: opacity .12s linear;
  pointer-events: none;
}
.vid.show { opacity: 1; z-index: 2; }

/* ---------- cursor-anchored video zoom ---------- */
/* The transform sits on the video elements only, so the overlays above them (clock, spinner,
   codec help, play hint) stay put and legible while the footage scales underneath.
   transform-origin 0 0 keeps the maths trivial: screen = pan + scale * content. */
.vid { transform-origin: 0 0; }
#videoWrap.vz-on { cursor: grab; }
#videoWrap.vz-panning { cursor: grabbing; }
#vzBadge {
  position: absolute; right: 12px; bottom: 12px; z-index: 5;
  padding: 3px 9px; border-radius: 6px;
  background: rgba(0,0,0,.62); color: #fff;
  font-size: 12px; font-variant-numeric: tabular-nums;
  pointer-events: none; opacity: 0; transition: opacity .18s linear;
}
#videoWrap.vz-on #vzBadge { opacity: 1; }

.big-clock {
  position: absolute;
  left: 12px; bottom: 12px;
  z-index: 5;
  font-variant-numeric: tabular-nums;
  font-size: 15px;
  font-weight: 600;
  color: #fff;
  background: rgba(0,0,0,.55);
  padding: 4px 10px;
  border-radius: 6px;
  pointer-events: none;
  text-shadow: 0 1px 2px #000;
}

.center-hint {
  position: absolute; inset: 0; z-index: 6;
  display: flex; align-items: center; justify-content: center;
}
.center-hint.hidden { display: none; }
#bigPlay {
  width: 88px; height: 88px; border-radius: 50%;
  font-size: 34px; color: #fff; cursor: pointer;
  background: rgba(20,25,35,.7); border: 1px solid rgba(255,255,255,.25);
}
#bigPlay:hover { background: rgba(40,50,70,.85); }

.spinner {
  position: absolute; top: 50%; left: 50%; z-index: 6;
  width: 46px; height: 46px; margin: -23px;
  border: 4px solid rgba(255,255,255,.2);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
.spinner.hidden { display: none; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Codec error overlay ---------- */
.codec-help {
  position: absolute; inset: 0; z-index: 20;
  display: flex; align-items: center; justify-content: center;
  background: rgba(6, 9, 14, 0.82); backdrop-filter: blur(2px);
}
.codec-help.hidden { display: none; }
.codec-help-box {
  max-width: 460px; margin: 16px; padding: 20px 24px;
  background: var(--panel); border: 1px solid var(--border); border-radius: 10px;
  color: var(--text); box-shadow: 0 8px 30px rgba(0,0,0,0.5);
}
.codec-help-box h3 { margin: 0 0 10px; color: #ff5c5c; font-size: 17px; }
.codec-help-box p { margin: 8px 0; line-height: 1.5; }
.codec-help-box ul { margin: 8px 0; padding-left: 20px; line-height: 1.6; }
.codec-help-box .muted { color: var(--muted); font-size: 13px; }
.codec-help-box .primary { margin-top: 12px; }

/* ---------- Timeline ---------- */
#timelineSection { flex: 0 0 auto; }
#timelineWrap {
  position: relative;
  height: 54px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  cursor: pointer;
}
#tlBase, #tlCursor { position: absolute; inset: 0; width: 100%; height: 100%; }
#tlCursor { pointer-events: none; }
.tl-tooltip {
  position: absolute; top: -28px; transform: translateX(-50%);
  background: #000; color: #fff; padding: 2px 7px; border-radius: 4px;
  font-size: 12px; white-space: nowrap; pointer-events: none; z-index: 10;
  border: 1px solid var(--border);
}
.tl-tooltip.hidden { display: none; }
/* Hover preview thumbnail (fixed-positioned so the timeline's overflow can't clip it) */
.tl-thumb {
  position: fixed; z-index: 60; transform: translate(-50%, -100%);
  background: #000; border: 1px solid var(--border); border-radius: 6px;
  padding: 4px; pointer-events: none; box-shadow: 0 6px 22px rgba(0, 0, 0, 0.65);
}
.tl-thumb.hidden { display: none; }
.tl-thumb img { display: block; width: 240px; height: 90px; object-fit: cover; border-radius: 3px; background: #15171c; }
.tl-thumb img.hidden { display: none; }
.tl-thumb #tlThumbCap { color: #fff; font-size: 11px; text-align: center; margin-top: 3px; font-variant-numeric: tabular-nums; white-space: nowrap; }
.tl-thumb.no-clip img { display: none; }
.tl-thumb.no-clip #tlThumbCap::after { content: " · no footage"; color: var(--muted); }
.tl-labels {
  display: flex; justify-content: space-between; align-items: center;
  color: var(--muted); font-size: 11px; margin-top: 3px; padding: 0 2px;
}
.tl-zoom { font-variant-numeric: tabular-nums; opacity: 0.8; }
.tl-zoom.active {
  cursor: pointer; color: var(--accent-2, #ffb020);
  border: 1px solid var(--border); border-radius: 4px; padding: 1px 6px; opacity: 1;
}
.tl-zoom.active:hover { background: var(--panel-2); }
#timelineWrap.grabbing { cursor: grabbing; }

/* ---------- Controls ---------- */
#controls {
  flex: 0 0 auto;
  display: flex; align-items: center; gap: 14px; flex-wrap: wrap;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 12px;
}
.ctl {
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 6px;
  padding: 6px 10px; cursor: pointer; font-size: 15px; line-height: 1;
}
.ctl:hover { border-color: var(--accent-2); }
.ctl-play { font-size: 16px; min-width: 44px; }
.ctl-group { display: flex; align-items: center; gap: 6px; }
.ctl-group label { color: var(--muted); font-size: 12px; }
select, input[type="number"] {
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 6px; padding: 5px 7px;
}
.vol-group { min-width: 220px; }
#volume { width: 130px; accent-color: var(--accent); }
.vol-label { color: var(--muted); width: 42px; font-variant-numeric: tabular-nums; }
.speed-note { color: var(--accent-2, #ffb020); font-size: 11px; min-width: 52px; }

/* Cast: the button goes solid while a receiver is actually connected. */
body.casting #btnCast { background: var(--accent-2); border-color: var(--accent-2); color: #fff; }
#castNote { min-width: 60px; }

/* ---------- Sidebar ---------- */
#sidebar {
  grid-area: side;
  background: var(--panel);
  border-left: 1px solid var(--border);
  overflow-y: auto;
  padding: 12px;
}
.panel { margin-bottom: 18px; }
.panel h2 {
  font-size: 12px; text-transform: uppercase; letter-spacing: .8px;
  color: var(--muted); margin: 0 0 10px; font-weight: 600;
}
.jump-grid { display: grid; grid-template-columns: 1.6fr 1fr 1fr 1fr; gap: 8px; margin-bottom: 8px; }
.jump-grid label { display: flex; flex-direction: column; gap: 4px; font-size: 11px; color: var(--muted); }
.jump-grid select { width: 100%; }
button.primary {
  background: var(--accent-2); color: #fff; border: none;
  border-radius: 6px; padding: 8px 12px; cursor: pointer; font-weight: 600;
}
button.primary:hover { filter: brightness(1.1); }
.full { width: 100%; }
.hint { color: var(--muted); font-size: 12px; margin-top: 6px; }

.fav-mark { display: flex; gap: 6px; }
.chip {
  flex: 1; background: var(--panel-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 6px; padding: 7px 4px;
  cursor: pointer; font-size: 12px;
}
.chip:hover { border-color: var(--accent-2); }
.chip-accent { background: var(--accent); color: #1a1300; border-color: var(--accent); font-weight: 700; }
.chip:disabled { opacity: .45; cursor: not-allowed; }
.chip.armed { outline: 2px solid var(--accent); }

.fav-list { list-style: none; margin: 12px 0 0; padding: 0; }
.fav-item {
  background: var(--panel-2); border: 1px solid var(--border);
  border-radius: 7px; padding: 8px 10px; margin-bottom: 8px;
  display: flex; flex-direction: column; gap: 3px;
}
.fav-item.current {
  border-color: #ffd60a;
  box-shadow: 0 0 0 1px #ffd60a, 0 0 10px rgba(255, 214, 10, 0.35);
}
.fav-item .fav-name { font-weight: 600; display: flex; align-items: center; gap: 6px; }
.fav-item .fav-name::before { content: "★"; color: var(--fav-color, var(--fav)); }
/* Sensitive entries only ever reach an unlocked browser, so marking them is safe. */
.fav-item.sensitive { border-color: var(--accent); }
.fav-item.sensitive .fav-name::after { content: "🔒"; font-size: 11px; opacity: .85; }
.fav-item .fav-note { font-size: 12px; color: var(--text); font-style: italic; }
.fav-item .fav-note { cursor: pointer; }
.fav-colors { display: flex; gap: 8px; margin-top: 2px; }
.fav-colors .swatch {
  width: 18px; height: 18px; border-radius: 50%; padding: 0; cursor: pointer;
  border: 2px solid transparent; transition: transform 0.08s ease;
}
.fav-colors .swatch:hover { transform: scale(1.18); }
.fav-colors .swatch.active { border-color: #fff; box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.5); }

/* ---------- Modal (favorite editor) ---------- */
.modal {
  position: fixed; inset: 0; z-index: 50;
  display: flex; align-items: center; justify-content: center;
  background: rgba(0, 0, 0, 0.62);
}
.modal.hidden { display: none; }
.modal-box {
  width: 360px; max-width: 92vw; padding: 18px 20px;
  background: var(--panel); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.55);
}
.modal-box h3 { margin: 0 0 10px; }
.modal-meta { color: var(--muted); font-size: 12px; margin-bottom: 14px; }
.modal-field { display: block; color: var(--muted); font-size: 12px; margin-bottom: 16px; }
.modal-field input {
  width: 100%; margin-top: 6px; box-sizing: border-box;
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 6px; padding: 8px 10px; font-size: 14px;
}
.modal-check { display: flex; align-items: center; gap: 8px; color: var(--text); }
.modal-check input { width: auto; margin: 0; accent-color: var(--accent); }
.modal-box .fav-colors { margin-top: 8px; }
.modal-box .fav-colors .swatch { width: 24px; height: 24px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; }
.fav-item .fav-note.empty { color: var(--muted); font-style: normal; cursor: default; opacity: 0.7; }
.fav-item .fav-range { color: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.fav-sort { margin-top: 10px; font-size: 11px; color: var(--muted); display: flex; align-items: center; gap: 8px; }
.fav-sort label { display: flex; align-items: center; gap: 6px; flex: 1; }
.fav-sort select { flex: 1; }
.fav-sort .sort-dir { padding: 5px 9px; font-size: 11px; white-space: nowrap; }
.fav-item .fav-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 4px; }
.fav-item .fav-actions button {
  background: transparent; color: var(--accent-2); border: 1px solid var(--border);
  border-radius: 5px; padding: 3px 8px; cursor: pointer; font-size: 11px;
}
.fav-item .fav-actions button.dl { color: var(--ok); border-color: var(--ok); }
.fav-item .fav-actions button.del { color: var(--fav); }
.fav-item .fav-actions button.event { color: var(--accent-2); border-color: var(--accent-2); }
.fav-item .fav-actions button:hover { background: var(--panel); }

/* Admin audio toggle + "disabled" indicator in the controls row */
.admin-group { margin-left: auto; }
#btnAudioAdmin.audio-off { color: var(--fav); border-color: var(--fav); }
#btnExperimental.exp-on { color: var(--accent); border-color: var(--accent); }
.audio-off-note { color: var(--fav); font-size: 12px; white-space: nowrap; }

.clip-info { color: var(--muted); font-size: 12px; word-break: break-all; line-height: 1.6; }
.clip-info b { color: var(--text); font-weight: 600; }

.hidden { display: none !important; }

/* scrollbar */
#sidebar::-webkit-scrollbar { width: 9px; }
#sidebar::-webkit-scrollbar-thumb { background: #2a313c; border-radius: 6px; }

/* ==========================================================================
   Mobile / touch
   ========================================================================== */

/* The timeline owns its gestures (drag to pan, pinch to zoom). Without this the
   browser scrolls or page-zooms instead and the timeline never sees the touch. */
#timelineWrap { touch-action: none; }

#videoWrap:fullscreen { border: none; border-radius: 0; aspect-ratio: auto; }
#videoWrap:-webkit-full-screen { border: none; border-radius: 0; }

/* --- Narrow screens: stop being a two-column app and stack into one scroll --- */
@media (max-width: 900px) {
  body { overflow: auto; }
  #app {
    display: flex;
    flex-direction: column;
    height: auto;
    min-height: 100dvh;
  }
  #topbar { position: sticky; top: 0; z-index: 30; gap: 10px; padding: 0 10px; flex: 0 0 48px; }
  #clockTime { font-size: 14px; }
  #clockMeta { display: none; }        /* the date is already on the big clock */
  .status { min-width: 0; flex: 1 1 auto; }

  #stage { padding: 8px; gap: 8px; }
  /* 8/3 is the native clip aspect (4608x1728), so the frame fills the box with
     no letterboxing instead of guessing at 16/9. */
  #videoWrap { flex: 0 0 auto; aspect-ratio: 8 / 3; }
  #controls { gap: 10px 12px; padding: 8px; }

  #sidebar {
    border-left: none;
    border-top: 1px solid var(--border);
    overflow: visible;
  }
}

/* --- Landscape phone: the video is the point, give it the screen --- */
@media (max-width: 900px) and (orientation: landscape) {
  #videoWrap { aspect-ratio: auto; height: 62dvh; }
}

/* --- Touch input: 9px dots and 11px buttons are not finger targets --- */
@media (hover: none) {
  /* The unlock switch stays a 9px dot visually, but gets a ~37px hit area.
     Still no cursor and no tooltip, so there is nothing about it to notice. */
  .brand .dot { position: relative; }
  .brand .dot::before { content: ""; position: absolute; inset: -14px; }

  .ctl { padding: 10px 14px; font-size: 16px; }
  .ctl-play { min-width: 56px; }
  .chip { padding: 11px 6px; font-size: 13px; }
  button.primary { padding: 12px 14px; }
  .fav-item .fav-actions { gap: 8px; }
  .fav-item .fav-actions button { padding: 9px 12px; font-size: 13px; }
  .fav-colors .swatch { width: 28px; height: 28px; }
  #timelineWrap { height: 76px; }
  /* iOS zooms the whole page when focusing an input under 16px. */
  select, input[type="number"], .modal-field input { font-size: 16px; }
  /* Hover previews cannot work without a pointer; the drag tooltip replaces them. */
  .tl-thumb { display: none !important; }
}

/* --- Modals become bottom sheets, which is where a thumb already is --- */
@media (max-width: 600px) {
  .modal { align-items: flex-end; }
  .modal-box {
    width: 100%;
    max-width: 100%;
    border-radius: 14px 14px 0 0;
    padding: 18px 16px calc(18px + env(safe-area-inset-bottom));
    max-height: 88dvh;
    overflow-y: auto;
  }
  .modal-actions button { flex: 1; padding: 13px 12px; }
  .jump-grid { grid-template-columns: 1fr 1fr; }
}
