:root {
  --bg: #eef4f5;
  --surface: #ffffff;
  --surface-2: #e3edef;
  --ink: #0d2a33;
  --muted: #5b7680;
  --line: #cfdde1;
  --accent: #0f8f8a;
  --accent-ink: #ffffff;
  --good: #1b9e5a;
  --warn: #c88a12;
  --bad: #c8463c;
  --stage: #031017;
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #06141c; --surface: #0c222c; --surface-2: #123140; --ink: #e2f1f3;
    --muted: #87a6b0; --line: #1d4250; --accent: #38c6c2; --accent-ink: #032024;
    --good: #46d18a; --warn: #e8b04a; --bad: #ef7a70; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #06141c; --surface: #0c222c; --surface-2: #123140; --ink: #e2f1f3;
  --muted: #87a6b0; --line: #1d4250; --accent: #38c6c2; --accent-ink: #032024;
  --good: #46d18a; --warn: #e8b04a; --bad: #ef7a70; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--ink);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; cursor: pointer; }
svg { width: 1.25em; height: 1.25em; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

.bar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 16px;
  padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top));
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: blur(10px); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 650; min-width: 0; }
.brand svg { fill: var(--accent); stroke: none; width: 26px; height: 26px; flex: none; }
.brand span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tabs { display: flex; gap: 4px; background: var(--surface-2); padding: 3px; border-radius: 10px; margin-left: auto; }
.tabs button { border: 0; background: none; padding: 6px 12px; border-radius: 8px; color: var(--muted); }
.tabs button[aria-selected="true"] { background: var(--surface); color: var(--ink); box-shadow: 0 1px 2px #0002; }
.conn { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); white-space: nowrap; }
.conn i { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.conn[data-state="live"] i { background: var(--good); box-shadow: 0 0 0 3px color-mix(in srgb, var(--good) 25%, transparent); }
.conn[data-state="wait"] i { background: var(--warn); animation: pulse 1.2s infinite; }
.conn[data-state="down"] i { background: var(--bad); }
@keyframes pulse { 50% { opacity: .35; } }

main { max-width: 1180px; margin: 0 auto; padding: 16px; padding-bottom: max(24px, env(safe-area-inset-bottom)); }
.panel[hidden] { display: none; }

.stage {
  position: relative; background: var(--stage); border-radius: var(--radius); overflow: hidden;
  aspect-ratio: 16 / 9; box-shadow: 0 10px 30px #0003;
}
.stage video, .stage img { width: 100%; height: 100%; object-fit: contain; display: block; }
.stage:fullscreen { border-radius: 0; aspect-ratio: auto; }
.overlay {
  position: absolute; inset: 0; display: grid; place-content: center; justify-items: center; gap: 12px;
  color: #cfe7ea; text-align: center; padding: 16px;
  background: radial-gradient(circle at 50% 40%, #0b3a4566, #031017ee);
}
.overlay[hidden] { display: none; }
.overlay p { margin: 0; max-width: 34ch; }
.spinner { width: 34px; height: 34px; border-radius: 50%; border: 3px solid #ffffff22; border-top-color: #38c6c2; animation: spin 0.9s linear infinite; }
.overlay.err .spinner { display: none; }
@keyframes spin { to { transform: rotate(360deg); } }
.hud { position: absolute; left: 12px; top: 10px; right: 12px; display: flex; gap: 8px; align-items: center; color: #fff; font-size: 13px; text-shadow: 0 1px 3px #000a; pointer-events: none; font-variant-numeric: tabular-nums; }
.pill { background: #0008; padding: 2px 8px; border-radius: 99px; text-shadow: none; }
.pill:empty { display: none; }
.stagebtns { position: absolute; right: 10px; bottom: 10px; display: flex; gap: 8px; }
.icon { width: 40px; height: 40px; border-radius: 50%; border: 0; background: #000a; color: #fff; display: grid; place-items: center; }
.icon .on { display: none; }
.icon[aria-pressed="true"] .on { display: inline; }
.icon[aria-pressed="true"] .off { display: none; }

.controls { display: grid; gap: 16px; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); margin-top: 16px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.card h2 { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 0 0 10px; font-weight: 600; }
.card h2:not(:first-child) { margin-top: 18px; }
.hint { color: var(--muted); font-size: 13px; margin: 8px 0 0; text-align: center; }

.dpad { display: grid; grid-template-columns: repeat(3, 60px); grid-template-rows: repeat(3, 60px); gap: 6px; justify-content: center; touch-action: none; user-select: none; -webkit-user-select: none; }
.dpad button { border: 1px solid var(--line); background: var(--surface-2); border-radius: 14px; display: grid; place-items: center; }
.dpad button svg { width: 26px; height: 26px; }
.dpad button.active { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.dpad [data-dir="up"] { grid-area: 1 / 2; }
.dpad [data-dir="left"] { grid-area: 2 / 1; }
.dpad .hub { grid-area: 2 / 2; border-radius: 50%; margin: 14px; background: var(--line); }
.dpad [data-dir="right"] { grid-area: 2 / 3; }
.dpad [data-dir="down"] { grid-area: 3 / 2; }

.seg { display: flex; background: var(--surface-2); border-radius: 10px; padding: 3px; gap: 3px; }
.seg button { flex: 1; border: 0; background: none; padding: 8px 10px; border-radius: 8px; color: var(--muted); }
.seg button[aria-pressed="true"] { background: var(--surface); color: var(--ink); box-shadow: 0 1px 2px #0002; font-weight: 600; }

.switch { display: flex; align-items: center; gap: 10px; padding: 6px 0; cursor: pointer; }
.switch input { position: absolute; opacity: 0; }
.switch span { width: 40px; height: 24px; border-radius: 99px; background: var(--line); position: relative; transition: background .15s; flex: none; }
.switch span::after { content: ""; position: absolute; left: 3px; top: 3px; width: 18px; height: 18px; border-radius: 50%; background: #fff; transition: transform .15s; box-shadow: 0 1px 2px #0004; }
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::after { transform: translateX(16px); }
.switch input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

.primary, .ghost { width: 100%; display: flex; align-items: center; justify-content: center; gap: 8px; padding: 11px 14px; border-radius: 10px; font-weight: 600; }
.primary { background: var(--accent); color: var(--accent-ink); border: 0; }
.ghost { background: none; border: 1px solid var(--line); }
.overlay .ghost { width: auto; color: #fff; border-color: #ffffff55; }
button:disabled { opacity: .5; cursor: default; }

.lapsehead { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 12px; align-items: center; }
.lapsehead select { flex: 1 1 160px; padding: 9px 10px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); color: var(--ink); font: inherit; }
.lapsehead .seg { flex: 1 1 220px; }
.lapsehead .primary { width: auto; flex: 0 0 110px; }
#scrub { width: 100%; margin: 12px 0; accent-color: var(--accent); }
.strip { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 6px; scroll-snap-type: x proximity; }
.strip img { height: 64px; aspect-ratio: 16/9; object-fit: cover; border-radius: 6px; cursor: pointer; opacity: .6; scroll-snap-align: center; border: 2px solid transparent; }
.strip img.cur { opacity: 1; border-color: var(--accent); }
.empty { color: var(--muted); text-align: center; padding: 24px; }

dialog { border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); color: var(--ink); padding: 24px; width: min(340px, calc(100vw - 32px)); }
dialog::backdrop { background: #031017cc; }
dialog form { display: grid; gap: 12px; }
dialog h2 { margin: 0; }
dialog input { padding: 10px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); color: var(--ink); font: inherit; }
.err { color: var(--bad); margin: 0; min-height: 1.2em; font-size: 13px; }

#toast { position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 10px; opacity: 0; transition: .2s; pointer-events: none; max-width: calc(100vw - 32px); }
#toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 560px) {
  .bar { gap: 10px; flex-wrap: wrap; }
  .conn span { display: none; }
  main { padding: 12px 16px; }
  .stage { border-radius: 10px; }
}
