:root {
  --bg: #f5f5f3; --surface: #ffffff; --sunken: #ebebe8; --line: #deded9; --line-strong: #c9c9c3;
  --text: #1c1c1a; --muted: #6c6c66; --primary: #1c1c1a; --primary-ink: #ffffff;
  --ok: #2e8b4a; --warn: #b7791f; --bad: #c0412c;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --radius: 5px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #151515; --surface: #1d1d1d; --sunken: #242424; --line: #2d2d2d; --line-strong: #3d3d3d;
    --text: #ebebe8; --muted: #9b9b95; --primary: #ebebe8; --primary-ink: #151515;
    --ok: #4cbf6c; --warn: #dca43a; --bad: #ec6a55;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
  display: flex; flex-direction: column;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.meta { color: var(--muted); font: 12px/1.4 var(--mono); }
.meta.warn { color: var(--warn); } .meta.bad { color: var(--bad); }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.contents { display: contents; }
h2 { font-size: 14px; font-weight: 600; margin: 0 0 10px; }

/* Controls */
button {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--line-strong);
  border-radius: var(--radius); padding: 5px 12px; cursor: pointer; white-space: nowrap;
}
button:hover { border-color: var(--muted); }
button.primary { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); font-weight: 500; }
button.primary:hover { opacity: .88; }
button.quiet { background: none; border-color: transparent; color: var(--muted); padding: 5px 8px; }
button.quiet:hover { color: var(--text); }
button:disabled { opacity: .4; cursor: default; }
button:focus-visible, input:focus-visible, select:focus-visible { outline: 2px solid var(--muted); outline-offset: 1px; }

input[type=text], select {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--line-strong);
  border-radius: var(--radius); padding: 5px 8px; min-width: 0;
}
input.code { font: 12px var(--mono); flex: 1; min-width: 120px; }
input.code[readonly] { background: var(--sunken); color: var(--muted); }
input[type=range] { accent-color: var(--primary); flex: 1; min-width: 0; }
input[type=checkbox] { accent-color: var(--primary); margin: 0; }

/* Status: a colored dot and text. */
.status { display: inline-flex; align-items: center; gap: 7px; color: var(--muted); }
.status::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: var(--line-strong); flex: none; }
.status.ok { color: var(--text); } .status.ok::before { background: var(--ok); }
.status.warn::before { background: var(--warn); }
.status.bad { color: var(--bad); } .status.bad::before { background: var(--bad); }

/* Header */
header {
  display: flex; align-items: stretch; gap: 28px; padding: 0 24px; height: 48px; flex: none;
  border-bottom: 1px solid var(--line);
}
header h1 { font-size: 15px; font-weight: 600; margin: 0; align-self: center; }
.tabs { display: flex; gap: 20px; }
.tabs button {
  background: none; border: 0; border-bottom: 2px solid transparent; border-radius: 0;
  padding: 0 2px; color: var(--muted); margin-bottom: -1px;
}
.tabs button.on { color: var(--text); border-bottom-color: var(--text); }
.tabs button:hover { color: var(--text); }
.header-actions { margin-left: auto; display: flex; align-items: center; gap: 2px; }

main { flex: 1; min-height: 0; overflow: auto; }
.page { width: 100%; max-width: 1240px; margin: 0 auto; padding: 20px 24px 24px; }
.page.watch { height: 100%; }

/* Host */
.host { display: grid; grid-template-columns: minmax(0, calc(44vh * 16 / 9)) minmax(320px, 1fr); gap: 24px 40px; align-items: start; }
.left { display: grid; gap: 24px; align-content: start; }
.source { display: grid; gap: 10px; }
#capturePreview { width: min(100%, calc(44vh * 16 / 9)); aspect-ratio: 16/9; background: #000; border-radius: var(--radius); display: block; }
.source .info { display: grid; gap: 2px; }
.source .status { margin-left: 8px; }

.form {
  display: grid; grid-template-columns: auto minmax(0, 1fr) auto minmax(0, 1fr); gap: 8px 12px; align-items: center;
  max-width: calc(44vh * 16 / 9);
}
.form > label { color: var(--muted); }
.form > label.top { grid-column: 1; align-self: start; margin-top: 3px; }
.form .wide { grid-column: 2 / -1; }
.form select { width: 100%; }
.form .row { flex-wrap: nowrap; }
#bitrateLabel { font: 12px var(--mono); color: var(--muted); min-width: 56px; text-align: right; }
.hint { margin: 4px 0 0; font-size: 12px; color: var(--muted); }
.hint a { color: var(--text); }

.viewers { min-width: 0; }
.viewers .head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 4px; }
.viewers .head h2 { margin: 0; }
#viewerList:not(:empty) ~ .empty { display: none; }
.empty { margin: 6px 0 0; }

.viewer { padding: 12px 0; border-bottom: 1px solid var(--line); display: grid; gap: 8px; }
.viewer:last-child { border-bottom: 0; }
.viewer .top { display: flex; align-items: center; gap: 14px; }
.viewer .name { border-color: transparent; background: none; padding: 3px 6px; margin-left: -6px; width: 120px; font-weight: 500; }
.viewer .name:hover, .viewer .name:focus { border-color: var(--line-strong); background: var(--surface); }
.viewer .remove { margin-left: auto; }
.viewer .steps { display: grid; gap: 6px; }
.viewer .steps .row { flex-wrap: nowrap; }
.viewer .steps span { color: var(--muted); min-width: 38px; }
.viewer.live .steps { display: none; }

/* Watch */
.watch { height: 100%; display: flex; flex-direction: column; gap: 12px; }
.connect { display: grid; gap: 8px; max-width: 760px; }
.connect .row { flex-wrap: nowrap; }
.connect .row > span { color: var(--muted); white-space: nowrap; }
.watch .bar { display: flex; align-items: center; gap: 12px; }

#stage {
  position: relative; flex: 1; min-height: 240px; background: #000; border-radius: var(--radius);
  overflow: hidden; display: flex; align-items: center; justify-content: center;
}
#stage video { display: none; width: 100%; height: 100%; object-fit: contain; }
#stage.live video { display: block; }
#stage.live #noStreamMessage { display: none; }
#noStreamMessage { color: #8a8a85; }
#stage.actual-size { overflow: auto; display: block; }
#stage.actual-size video { width: auto; height: auto; max-width: none; margin: 0 auto; }

/* Maximized: fills the browser window, not the monitor. */
#stage.maximized { position: fixed; inset: 0; z-index: 30; border-radius: 0; min-height: 0; }
#stage:fullscreen { border-radius: 0; }

#statsOverlay {
  display: none; position: absolute; top: 10px; left: 10px; background: rgba(0,0,0,.6); color: #ddd;
  padding: 6px 9px; border-radius: 4px; font: 11px/1.45 var(--mono); pointer-events: none; white-space: pre;
}
#stage.live.stats #statsOverlay { display: block; }

.controls {
  position: absolute; right: 10px; bottom: 10px; display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 4px;
  max-width: calc(100% - 20px); opacity: 0; transition: opacity .15s;
}
#stage:hover .controls, #stage:focus-within .controls { opacity: 1; }
@media (hover: none) { .controls { opacity: 1; } }
.controls button {
  background: rgba(20,20,20,.72); color: #eee; border: 1px solid rgba(255,255,255,.14); font-size: 12px; padding: 4px 10px;
}
.controls button:hover { background: rgba(40,40,40,.9); border-color: rgba(255,255,255,.3); }
.controls .volume {
  display: flex; align-items: center; padding: 0 8px; border-radius: var(--radius);
  background: rgba(20,20,20,.72); border: 1px solid rgba(255,255,255,.14);
}
.controls .volume input { flex: none; width: 90px; accent-color: #eee; }
.controls button[aria-pressed=true] { background: rgba(235,235,232,.92); color: #151515; }

/* Dialogs */
dialog {
  padding: 0; border: 1px solid var(--line-strong); border-radius: 8px; background: var(--surface); color: var(--text);
  width: min(560px, calc(100vw - 32px)); max-height: 86vh; box-shadow: 0 12px 40px rgba(0,0,0,.25);
}
dialog[open] { display: flex; flex-direction: column; overflow: hidden; }
dialog::backdrop { background: rgba(0,0,0,.4); }
dialog .dialog-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 12px 16px; border-bottom: 1px solid var(--line); }
dialog .dialog-head h2 { margin: 0; }
dialog .dialog-body { padding: 16px; overflow: auto; flex: 1 1 auto; min-height: 0; }
dialog .dialog-body p, dialog .dialog-body li { color: var(--muted); }
dialog .dialog-body p { margin: 0 0 10px; }
dialog .dialog-body h3 { font-size: 13px; margin: 16px 0 6px; }
dialog .dialog-body h3:first-child { margin-top: 0; }
dialog .dialog-body ol, dialog .dialog-body ul { margin: 0 0 10px; padding-left: 18px; }
dialog .dialog-body li ul { margin: 4px 0 0; }
dialog .dialog-body h4 { font-size: 12px; margin: 10px 0 4px; }
dialog .dialog-body .warning { color: var(--bad); font-weight: 600; }
dialog .dialog-body li { margin-bottom: 4px; }
.stack { display: grid; gap: 12px; }
.stack label { display: grid; gap: 4px; color: var(--muted); }
.stack label.check { display: flex; align-items: center; gap: 8px; color: var(--text); }
dialog details summary { cursor: pointer; color: var(--muted); }
dialog details[open] summary { margin-bottom: 12px; }

/* Window picker (desktop app) */
dialog#sourcePicker { width: min(900px, calc(100vw - 32px)); }
.picker-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 10px; margin-bottom: 18px; }
#sourcePickerList h3 { color: var(--muted); font-weight: 500; }
.tile { display: grid; gap: 6px; padding: 6px; text-align: left; border-color: var(--line); }
.tile:hover { border-color: var(--text); }
.tile img.thumb { width: 100%; aspect-ratio: 16/10; object-fit: contain; background: #000; border-radius: 3px; }
.tile .name { display: flex; gap: 6px; align-items: center; font-size: 12px; overflow: hidden; }
.tile .name span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tile .name img { width: 16px; height: 16px; flex: none; }

/* Narrow screens */
@media (max-width: 760px) {
  header { gap: 16px; padding: 0 16px; }
  .page { padding: 16px; }
  .host { grid-template-columns: 1fr; }
  .form { grid-template-columns: auto minmax(0, 1fr); }
  .connect .row { flex-wrap: wrap; }
}
