:root {
  --bg: #0a0b0f;
  --panel: #111319;
  --panel-2: #171a22;
  --line: #22262f;
  --line-2: #2f3441;
  --text: #eceef2;
  --muted: #8b92a2;
  --faint: #5b6273;
  --accent: #ff6a3d;
  --accent-hi: #ff8a63;
  --accent-ink: #1c0a03;
  --ok: #3ccf8e;
  --warn: #f2b544;
  --bad: #f0565e;
  --radius: 14px;
  --radius-sm: 10px;
  --topbar-h: 64px;
  --controls-h: 92px;
  --f-display: 'Bricolage Grotesque', 'Segoe UI', system-ui, sans-serif;
  --f-ui: 'Figtree', 'Segoe UI', system-ui, sans-serif;
  --f-mono: 'JetBrains Mono', ui-monospace, Consolas, monospace;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 var(--f-ui);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
[hidden] { display: none !important; }

.app {
  height: 100vh;
  display: grid;
  grid-template-rows: var(--topbar-h) minmax(0, 1fr) var(--controls-h);
}

/* ---------- top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 0 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark { width: 26px; height: 26px; fill: var(--accent); }
.brand-mark-small { fill: #ffc46b; }
.brand-name {
  font-family: var(--f-display);
  font-weight: 700;
  font-size: 20px;
  letter-spacing: -0.02em;
}
.topbar-spacer { flex: 1; }

.status {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  height: 30px;
  padding: 0 12px;
  border: 1px solid var(--line-2);
  border-radius: 999px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 500;
}
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--faint); }
.status[data-state="connecting"] .status-dot { background: var(--warn); animation: pulse 1.1s ease-in-out infinite; }
.status[data-state="live"] { color: var(--text); border-color: color-mix(in srgb, var(--ok) 45%, var(--line-2)); }
.status[data-state="live"] .status-dot { background: var(--ok); }
.status[data-state="error"] { color: var(--text); border-color: color-mix(in srgb, var(--bad) 50%, var(--line-2)); }
.status[data-state="error"] .status-dot { background: var(--bad); }

.camera-select {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  height: 36px;
  padding: 0 10px;
  background: var(--panel-2);
  border: 1px solid var(--line-2);
  border-radius: var(--radius-sm);
  color: var(--muted);
  max-width: 260px;
}
.camera-select svg { width: 18px; height: 18px; flex: none; }
.camera-select select {
  background: transparent;
  border: 0;
  color: var(--text);
  min-width: 0;
  max-width: 200px;
  outline: none;
  text-overflow: ellipsis;
}
.camera-select select option { background: var(--panel-2); }

.magic-id {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  height: 32px;
  padding: 0 10px;
  border-radius: 8px;
  border: 1px solid var(--line-2);
  background: transparent;
  font-size: 12px;
}
.magic-id span { color: var(--faint); font-weight: 700; letter-spacing: 0.08em; }
.magic-id b { font-family: var(--f-mono); font-weight: 600; letter-spacing: 0.02em; }
.magic-id:hover { border-color: var(--muted); }

.dialog {
  width: min(420px, calc(100vw - 40px));
  padding: 24px;
  border: 1px solid var(--line-2);
  border-radius: var(--radius);
  background: var(--panel);
  color: var(--text);
  box-shadow: 0 30px 80px -30px rgba(0, 0, 0, 0.9);
}
.dialog::backdrop { background: rgba(0, 0, 0, 0.55); }
.dialog h2 { margin: 0 0 14px; font: 700 20px var(--f-display); letter-spacing: -0.01em; }
.dialog p { margin: 0; color: var(--muted); }
.dialog-id {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 8px 0 16px;
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  background: var(--panel-2);
  border: 1px solid var(--line-2);
}
.dialog-id span { font: 600 22px var(--f-mono); letter-spacing: 0.04em; }
.dialog-note { line-height: 1.55; }
.dialog-actions { display: flex; justify-content: flex-end; margin-top: 20px; }

.minutes {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  line-height: 1.1;
  padding-left: 14px;
  border-left: 1px solid var(--line);
}
.minutes-value {
  font-family: var(--f-mono);
  font-weight: 600;
  font-size: 21px;
  font-variant-numeric: tabular-nums;
}
.minutes-label { font-size: 11px; color: var(--muted); letter-spacing: 0.04em; }
.minutes[data-level="low"] .minutes-value { color: var(--warn); }
.minutes[data-level="empty"] .minutes-value { color: var(--bad); }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  height: 38px;
  padding: 0 16px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  font-weight: 600;
  white-space: nowrap;
  transition: background 0.15s, border-color 0.15s, transform 0.15s;
}
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn-ghost { background: transparent; border-color: var(--line-2); }
.btn-ghost:hover { border-color: var(--muted); }
.btn-primary {
  height: 52px;
  min-width: 190px;
  padding: 0 30px;
  background: var(--accent);
  color: var(--accent-ink);
  font-family: var(--f-display);
  font-size: 17px;
  font-weight: 700;
  letter-spacing: -0.01em;
  box-shadow: 0 10px 30px -12px color-mix(in srgb, var(--accent) 80%, transparent);
}
.btn-primary:hover:not(:disabled) { background: var(--accent-hi); transform: translateY(-1px); }
.btn-stop {
  height: 52px;
  min-width: 120px;
  background: transparent;
  border-color: color-mix(in srgb, var(--bad) 55%, var(--line-2));
  color: #ffd9db;
  font-size: 15px;
}
.btn-stop:hover { background: color-mix(in srgb, var(--bad) 14%, transparent); }
.btn-spinner {
  display: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  border: 2px solid color-mix(in srgb, var(--accent-ink) 30%, transparent);
  border-top-color: var(--accent-ink);
  animation: spin 0.8s linear infinite;
}
.btn-primary.is-busy .btn-spinner { display: inline-block; }

.link-btn {
  background: none;
  border: 0;
  padding: 2px 0;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
}
.link-btn:hover { color: var(--text); }

.card-actions { display: flex; align-items: center; gap: 12px; }
.cam-toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 24px;
  padding: 0 9px;
  border-radius: 999px;
  border: 1px solid var(--line-2);
  background: var(--panel-2);
  color: var(--muted);
  font-size: 11px;
  font-weight: 600;
}
.cam-toggle:hover { color: var(--text); border-color: var(--muted); }
.cam-toggle-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--faint); }
.cam-toggle[aria-pressed="true"] { color: var(--text); }
.cam-toggle[aria-pressed="true"] .cam-toggle-dot { background: var(--ok); }
.link-btn[aria-pressed="true"] { color: var(--accent-hi); }

/* ---------- stage ---------- */
.stage {
  display: grid;
  grid-template-columns: 300px minmax(0, 1fr);
  gap: 18px;
  padding: 18px 20px;
  min-height: 0;
}
.rail { display: flex; flex-direction: column; gap: 16px; min-height: 0; }

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 0;
}
.card-head { display: flex; align-items: center; justify-content: space-between; padding: 0 2px; }
.card-head h2 {
  margin: 0;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--muted);
}

.dropzone {
  position: relative;
  display: grid;
  place-items: center;
  aspect-ratio: 4 / 5;
  max-height: 34vh;
  border: 1.5px dashed var(--line-2);
  border-radius: var(--radius-sm);
  background: var(--panel-2);
  overflow: hidden;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}
.dropzone:hover, .dropzone.is-over { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 6%, var(--panel-2)); }
.dropzone.has-image { border-style: solid; border-color: var(--line-2); }
.dropzone img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.dropzone-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 16px;
  text-align: center;
  color: var(--muted);
  font-size: 12px;
}
.dropzone-empty svg { width: 34px; height: 34px; margin-bottom: 6px; color: var(--faint); }
.dropzone-empty strong { color: var(--text); font-size: 14px; font-weight: 600; }

.cam-frame {
  position: relative;
  aspect-ratio: 16 / 9;
  border-radius: var(--radius-sm);
  background: #06070a;
  overflow: hidden;
}
.cam-frame video { width: 100%; height: 100%; object-fit: cover; display: block; }
.cam-frame video.mirrored { transform: scaleX(-1); }
.cam-empty {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 12px;
  text-align: center;
  color: var(--faint);
  font-size: 12px;
}

/* ---------- output ---------- */
.output { display: grid; place-items: center; min-height: 0; min-width: 0; }
.output-frame {
  position: relative;
  width: min(100%, calc((100vh - var(--topbar-h) - var(--controls-h) - 36px) * 16 / 9));
  aspect-ratio: 16 / 9;
  border-radius: 16px;
  background:
    radial-gradient(120% 90% at 50% 0%, #15171e 0%, #07080b 70%);
  border: 1px solid var(--line);
  overflow: hidden;
}
.output-frame.is-live { border-color: color-mix(in srgb, var(--accent) 35%, var(--line)); }
.output-frame video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; background: #000; opacity: 0; transition: opacity 0.35s; }
.output-frame.has-video video { opacity: 1; }
.output-frame .output-canvas { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; background: #000; opacity: 0; transition: opacity 0.35s; }
.output-frame.direct-output video { display: none; }
.output-frame:not(.direct-output) .output-canvas { display: none; }
.output-frame.has-video .output-canvas { opacity: 1; }
/* Once the picture is live, the "Casting the spell…" message must not sit on it. */
.output-frame.has-video .output-empty { opacity: 0; pointer-events: none; }
.output-empty {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  text-align: center;
  padding: 24px;
}
.output-empty svg { width: 40px; height: 40px; fill: var(--line-2); margin-bottom: 10px; }
.output-frame[data-phase="connecting"] .output-empty svg { fill: var(--accent); animation: twinkle 1.4s ease-in-out infinite; }
.output-empty-title {
  margin: 0;
  font-family: var(--f-display);
  font-weight: 600;
  font-size: 20px;
  letter-spacing: -0.01em;
  text-wrap: balance;
}
.output-empty-sub { margin: 0; color: var(--muted); max-width: 44ch; text-wrap: balance; }

.live-badge {
  position: absolute;
  top: 14px;
  left: 14px;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 5px 10px;
  border-radius: 6px;
  background: rgba(0, 0, 0, 0.55);
  font-family: var(--f-mono);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.12em;
  backdrop-filter: blur(6px);
}
.live-badge b { font-weight: 600; }
.live-badge span { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); animation: pulse 1.4s ease-in-out infinite; }

.sharpness {
  position: absolute;
  top: 12px;
  right: 12px;
  display: inline-flex;
  align-items: center;
  gap: 2px;
  padding: 3px;
  border-radius: 9px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  background: rgba(10, 11, 15, 0.7);
  backdrop-filter: blur(8px);
  font-size: 12px;
}
.sharpness-label { padding: 0 8px 0 6px; color: var(--muted); font-weight: 600; }
.sharpness button {
  height: 26px;
  padding: 0 10px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  font-weight: 600;
}
.sharpness button:hover { color: var(--text); }
.sharpness button[aria-checked="true"] { background: rgba(255, 255, 255, 0.12); color: var(--text); }

.boost-toggle {
  position: absolute;
  top: 8px;
  right: 8px;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  height: 24px;
  padding: 0 9px;
  border-radius: 999px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: rgba(0, 0, 0, 0.6);
  color: var(--muted);
  font-size: 11px;
  font-weight: 600;
  backdrop-filter: blur(6px);
}
.boost-toggle svg { width: 13px; height: 13px; }
.boost-toggle:hover { color: var(--text); }
.boost-toggle[aria-pressed="true"] { color: #ffd38a; border-color: rgba(255, 196, 107, 0.35); }

.light-meter {
  position: absolute;
  left: 8px;
  bottom: 8px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  max-width: calc(100% - 16px);
  padding: 4px 9px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.6);
  font-size: 11px;
  font-weight: 600;
  backdrop-filter: blur(6px);
}
.light-meter-dot { width: 7px; height: 7px; border-radius: 50%; flex: none; background: var(--ok); }
.light-meter[data-level="low"] .light-meter-dot,
.light-meter[data-level="backlit"] .light-meter-dot { background: var(--warn); }

.popout-btn {
  position: absolute;
  right: 14px;
  bottom: 14px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  height: 34px;
  padding: 0 12px;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: rgba(10, 11, 15, 0.7);
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
  backdrop-filter: blur(8px);
}
.popout-btn svg { width: 16px; height: 16px; }
.popout-btn:hover { border-color: rgba(255, 255, 255, 0.3); }

/* ---------- controls ---------- */
.controls {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 0 20px;
  background: var(--panel);
  border-top: 1px solid var(--line);
}
.scene-field { display: flex; flex-direction: column; gap: 5px; }
.scene-label { font-size: 12px; font-weight: 600; color: var(--muted); }
.segmented {
  display: inline-flex;
  height: 40px;
  padding: 3px;
  background: var(--panel-2);
  border: 1px solid var(--line-2);
  border-radius: var(--radius-sm);
}
.segmented button {
  padding: 0 14px;
  border: 0;
  border-radius: 7px;
  background: transparent;
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
}
.segmented button:hover { color: var(--text); }
.segmented button[aria-checked="true"] { background: var(--line-2); color: var(--text); }

.note-field { flex: 1; display: flex; flex-direction: column; gap: 5px; min-width: 0; max-width: 680px; }
.note-field label { font-size: 12px; font-weight: 600; color: var(--muted); }
.note-field label span { font-weight: 400; color: var(--faint); margin-left: 4px; }
.note-field input {
  height: 40px;
  padding: 0 12px;
  background: var(--panel-2);
  border: 1px solid var(--line-2);
  border-radius: var(--radius-sm);
  outline: none;
}
.note-field input:focus { border-color: var(--accent); }
.note-field input::placeholder { color: var(--faint); }
.controls .btn-primary { margin-left: auto; }
.diag-toggle {
  background: none;
  border: 0;
  color: var(--faint);
  font-size: 12px;
  font-weight: 600;
  padding: 6px 2px;
}
.diag-toggle:hover, .diag-toggle[aria-expanded="true"] { color: var(--muted); }

/* ---------- diagnostics drawer ---------- */
.diag {
  position: fixed;
  left: 20px;
  right: 20px;
  bottom: calc(var(--controls-h) + 10px);
  height: 34vh;
  display: flex;
  flex-direction: column;
  background: rgba(13, 15, 20, 0.96);
  border: 1px solid var(--line-2);
  border-radius: var(--radius);
  box-shadow: 0 24px 60px -20px rgba(0, 0, 0, 0.8);
  z-index: 20;
}
.diag-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 14px;
  border-bottom: 1px solid var(--line);
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
}
.diag pre {
  flex: 1;
  margin: 0;
  padding: 10px 14px;
  overflow: auto;
  font: 12px/1.6 var(--f-mono);
  color: #b9c0cf;
  white-space: pre-wrap;
  word-break: break-word;
}

/* ---------- toasts ---------- */
.toasts {
  position: fixed;
  right: 20px;
  bottom: calc(var(--controls-h) + 14px);
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 30;
  pointer-events: none;
}
.toast {
  max-width: 360px;
  padding: 11px 14px;
  border-radius: var(--radius-sm);
  background: var(--panel-2);
  border: 1px solid var(--line-2);
  border-left: 3px solid var(--muted);
  box-shadow: 0 16px 40px -18px rgba(0, 0, 0, 0.9);
  font-size: 13px;
  animation: toast-in 0.2s ease-out;
}
.toast.success { border-left-color: var(--ok); }
.toast.error { border-left-color: var(--bad); }
.toast.warn { border-left-color: var(--warn); }

@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.35; } }
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes twinkle { 0%, 100% { opacity: 1; transform: scale(1); } 50% { opacity: 0.5; transform: scale(0.88); } }
@keyframes toast-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

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

@media (max-width: 960px) {
  .stage { grid-template-columns: 240px minmax(0, 1fr); }
  .camera-select { display: none; }
}
