:root {
  --bg: #0d0f12;
  --panel: #171a1f;
  --line: #2a2f37;
  --text: #e8eaed;
  --muted: #8b929c;
  --ok: #3ccf7a;
  --warn: #f2b33d;
  --bad: #ef5a5a;
  --accent: #4c8dff;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

body { display: flex; flex-direction: column; }

.bar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}
.bar h1 { font-size: 16px; margin: 0; font-weight: 600; }
.spacer { flex: 1; }
.muted { color: var(--muted); }

.pill {
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  border: 1px solid currentColor;
}
.pill-on { color: var(--ok); }
.pill-off { color: var(--warn); }
.pill-bad { color: var(--bad); }

/* Grid: columns are set from JS so every feed fits on one screen. */
.grid {
  flex: 1;
  min-height: 0;
  display: grid;
  gap: 6px;
  padding: 6px;
  grid-template-columns: repeat(var(--cols, 1), minmax(0, 1fr));
  grid-auto-rows: minmax(0, 1fr);
}

.tile {
  position: relative;
  background: #000;
  border-radius: 6px;
  overflow: hidden;
  min-height: 0;
}
.tile video {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
  background: #000;
}
.tile:fullscreen video { object-fit: contain; }

.label {
  position: absolute;
  z-index: 2;
  left: 8px;
  bottom: 8px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 10px;
  border-radius: 6px;
  background: rgba(0, 0, 0, .6);
  font-size: 12px;
  max-width: calc(100% - 16px);
}
.label .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.label .stats { font-variant-numeric: tabular-nums; white-space: nowrap; }

.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--warn); flex: none; }
.tile.live .dot { background: var(--ok); }
.tile.failed .dot { background: var(--bad); }

.tile-state {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: var(--muted);
  pointer-events: none;
}
.tile.live .tile-state { display: none; }

/* Per-tile controls: recording switch and zoom */
.controls {
  position: absolute;
  z-index: 2;
  top: 8px;
  right: 8px;
  display: flex;
  gap: 6px;
  align-items: center;
}
.controls button {
  padding: 4px 10px;
  border-radius: 6px;
  border: 1px solid transparent;
  background: rgba(0, 0, 0, .6);
  color: var(--text);
  font-size: 12px;
  font-weight: 600;
}
.controls button:hover { border-color: var(--line); }
.zoom { display: flex; gap: 2px; }
.zoom[hidden] { display: none; }
.zoom .zoom-out, .zoom .zoom-in { width: 30px; padding: 4px 0; font-size: 15px; line-height: 1; }
.zoom .zoom-val { min-width: 52px; font-variant-numeric: tabular-nums; }

.rec-btn { display: flex; align-items: center; gap: 6px; }
.rec-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.rec-btn[data-state="recording"] .rec-dot { background: var(--bad); animation: blink 1.2s steps(2, start) infinite; }
.rec-btn[data-state="recording"] { color: #fff; }
.rec-btn[data-state="connecting"] .rec-dot { background: var(--warn); }
.rec-btn[data-state="error"] .rec-dot,
.rec-btn[data-state="low-disk"] .rec-dot { background: var(--warn); }
.rec-btn[data-state="off"] { color: var(--muted); }
@keyframes blink { to { visibility: hidden; } }

a.link { color: var(--text); text-decoration: none; padding: 3px 10px; border: 1px solid var(--line); border-radius: 6px; font-size: 12px; }
a.link:hover { border-color: var(--muted); }

.center-msg {
  position: fixed;
  inset: 50px 0 0 0;
  display: grid;
  place-content: center;
  text-align: center;
  pointer-events: none;
}
.center-msg[hidden] { display: none; }
.center-msg p { margin: 4px 0; }

.overlay {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(0, 0, 0, .7);
  z-index: 10;
}
.overlay[hidden] { display: none; }

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 24px;
  width: min(360px, 90vw);
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.card h2 { margin: 0; font-size: 18px; }
.card p { margin: 0; }

input, button, select {
  font: inherit;
  padding: 9px 12px;
  border-radius: 6px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--text);
}
button {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
}
button:disabled { opacity: .5; cursor: default; }
button.secondary { background: transparent; color: var(--text); border-color: var(--line); }
.error { color: var(--bad); }

/* Recordings page */
body:has(.rec-page) { overflow: auto; }
.rec-page { padding: 16px; display: flex; flex-direction: column; gap: 16px; max-width: 1100px; width: 100%; margin: 0 auto; }
.rec-player { background: #000; border-radius: 8px; overflow: hidden; border: 1px solid var(--line); }
.rec-player[hidden] { display: none; }
.rec-player video { width: 100%; max-height: 65vh; display: block; background: #000; }
.rec-player-bar { display: flex; align-items: center; gap: 12px; padding: 8px 12px; background: var(--panel); }
.rec-table { width: 100%; border-collapse: collapse; }
.rec-table th, .rec-table td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); }
.rec-table th { color: var(--muted); font-weight: 600; font-size: 12px; }
.rec-table td.num { font-variant-numeric: tabular-nums; }
.rec-table td.actions { display: flex; gap: 6px; justify-content: flex-end; align-items: center; }
.rec-table td.actions button, .rec-table td.actions a { padding: 5px 12px; font-size: 13px; }
.rec-now { color: var(--bad); font-weight: 600; }

/* Browser test camera page */
.cam-page { padding: 16px; gap: 12px; display: flex; flex-direction: column; align-items: center; }
.cam-page video { width: min(640px, 100%); aspect-ratio: 16 / 9; background: #000; border-radius: 6px; object-fit: cover; }
.cam-page .row { display: flex; gap: 8px; flex-wrap: wrap; width: min(640px, 100%); }
.cam-page .row > * { flex: 1; min-width: 140px; }
