:root {
  --bg: #0d1117;
  --panel: #161b22;
  --panel-2: #1c2230;
  --line: #2a313c;
  --text: #e6edf3;
  --muted: #8b949e;
  --accent: #2f81f7;
  --live: #3fb950;
  --warn: #d29922;
  --bad: #f85149;
  --radius: 8px;
}

* { box-sizing: border-box; }

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

/* ------------------------------------------------------------------ toolbar */

.bar {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 8px 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}

.brand { display: flex; align-items: center; gap: 8px; }
.brand strong { letter-spacing: .2px; }

.dot {
  width: 9px; height: 9px; border-radius: 50%;
  background: var(--muted);
  box-shadow: 0 0 0 3px rgb(139 148 158 / .15);
}
.dot.ok { background: var(--live); box-shadow: 0 0 0 3px rgb(63 185 80 / .18); }
.dot.bad { background: var(--bad); box-shadow: 0 0 0 3px rgb(248 81 73 / .18); }

.group { display: flex; align-items: center; gap: 6px; }
.spacer { flex: 1; }
.lbl { color: var(--muted); font-size: 12px; white-space: nowrap; }
.chk { color: var(--muted); font-size: 12px; display: flex; align-items: center; gap: 5px; cursor: pointer; }

.btn {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 5px 10px;
  font: inherit;
  cursor: pointer;
  transition: background .12s, border-color .12s;
}
.btn:hover { background: #22293a; border-color: #3a4352; }
.btn.is-active { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.1); }
.btn.ghost { background: transparent; }
.btn.layout { min-width: 32px; padding: 5px 7px; }
.btn.danger { color: var(--bad); border-color: #40262a; }
.btn.danger:hover { background: #2d1a1c; }

.select, input[type=text], input:not([type]), input[type=number] {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 5px 8px;
  font: inherit;
}

/* --------------------------------------------------------------------- grid */

.grid {
  height: calc(100vh - 53px);
  display: grid;
  gap: 4px;
  padding: 4px;
  overflow: hidden;
}
.grid[data-cols="1"]  { grid-template-columns: 1fr; }
.grid[data-cols="2"]  { grid-template-columns: repeat(2, 1fr); }
.grid[data-cols="3"]  { grid-template-columns: repeat(3, 1fr); }
.grid[data-cols="4"]  { grid-template-columns: repeat(4, 1fr); }
.grid[data-cols="6"]  { grid-template-columns: repeat(3, 1fr); }
.grid[data-cols="9"]  { grid-template-columns: repeat(3, 1fr); }
.grid[data-cols="16"] { grid-template-columns: repeat(4, 1fr); }
.grid.autosize[data-cols="6"]  { grid-template-rows: repeat(2, 1fr); }
.grid.autosize[data-cols="9"]  { grid-template-rows: repeat(3, 1fr); }
.grid.autosize[data-cols="16"] { grid-template-rows: repeat(4, 1fr); }

.tile {
  position: relative;
  background: #000;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  cursor: pointer;
  min-height: 0;
}
.tile img {
  width: 100%; height: 100%;
  object-fit: contain;
  display: block;
  background: #000;
}
.grid.autosize .tile img { object-fit: cover; }

.tile .meta {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  display: flex; align-items: center; gap: 6px;
  padding: 4px 7px;
  background: linear-gradient(transparent, rgb(0 0 0 / .78));
  font-size: 12px;
  pointer-events: none;
}
.tile .name { font-weight: 600; text-shadow: 0 1px 3px #000; }
.tile .spacer { flex: 1; }
.tile .stat { color: var(--muted); font-variant-numeric: tabular-nums; text-shadow: 0 1px 3px #000; }

.badge {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: 11px; padding: 1px 6px; border-radius: 999px;
  background: rgb(0 0 0 / .55); border: 1px solid var(--line);
}
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge.live { color: var(--live); }
.badge.starting { color: var(--warn); }
.badge.offline, .badge.error { color: var(--bad); }
.badge.idle { color: var(--muted); }

.tile .overlay {
  position: absolute; inset: 0;
  display: none;
  align-items: center; justify-content: center;
  text-align: center;
  padding: 12px;
  background: rgb(8 10 14 / .82);
  color: var(--muted);
  font-size: 12px;
}
.tile[data-state="offline"] .overlay,
.tile[data-state="error"] .overlay,
.tile[data-state="starting"] .overlay,
.tile[data-state="idle"] .overlay { display: flex; }
.tile .overlay b { color: var(--bad); display: block; margin-bottom: 4px; font-size: 13px; }
.tile[data-state="starting"] .overlay b { color: var(--warn); }
.tile[data-state="idle"] .overlay b { color: var(--muted); }

.tile .tools {
  position: absolute; top: 6px; right: 6px;
  display: flex; gap: 4px;
  opacity: 0; transition: opacity .12s;
}
.tile:hover .tools { opacity: 1; }
.tile .tools button {
  background: rgb(0 0 0 / .6);
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 5px;
  width: 24px; height: 24px;
  cursor: pointer; font-size: 12px; line-height: 1;
}
.tile .tools button:hover { background: #2b3446; }

/* --------------------------------------------------------------- focus mode */

body.focused .bar { display: none; }
body.focused .grid { height: 100vh; grid-template-columns: 1fr; }
body.focused .grid .tile { display: none; }
body.focused .grid .tile.focus-target { display: block; }

.focus-bar {
  position: fixed; top: 10px; left: 50%; transform: translateX(-50%);
  z-index: 40;
  display: flex; align-items: center; gap: 8px;
  padding: 6px 10px;
  background: rgb(13 17 23 / .85);
  border: 1px solid var(--line);
  border-radius: 999px;
  backdrop-filter: blur(6px);
  font-weight: 600;
}

/* ------------------------------------------------------------------- dialog */

.dialog {
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel);
  color: var(--text);
  padding: 0;
  width: min(720px, 92vw);
  max-height: 86vh;
  overflow: auto;
}
.dialog::backdrop { background: rgb(0 0 0 / .6); }

.dialog-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 14px; border-bottom: 1px solid var(--line);
  position: sticky; top: 0; background: var(--panel); z-index: 1;
}
.dialog-head h3 { margin: 0; font-size: 15px; }
.dialog-foot { padding: 10px 14px; border-top: 1px solid var(--line); }
/* ------------------------------------------------------------ auth dialogs */

.auth-dialog { max-width: 380px; }
.auth-form { display: grid; gap: 12px; padding: 20px; }
.auth-form h3 { margin: 0; }
.auth-form label { display: grid; gap: 4px; }
.auth-form input {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #101216;
  color: var(--fg);
  font: inherit;
}
.auth-error {
  margin: 0;
  min-height: 1.2em;
  color: var(--bad);
  font-size: 13px;
}

/* ------------------------------------------------------------- access tab */

h4 {
  margin: 22px 0 6px;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
}
h4:first-child { margin-top: 0; }

tr.is-disabled td { opacity: 0.5; }
.mono {
  font-family: ui-monospace, "Cascadia Mono", Consolas, monospace;
  font-size: 12px;
}
.row-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.row-actions .btn { padding: 4px 9px; font-size: 12px; }
.warn-box {
  margin: 0 0 8px;
  padding: 8px 10px;
  border: 1px solid #5a4520;
  border-left: 3px solid #d9a441;
  border-radius: 6px;
  background: #1f1a10;
  color: #e8cf9a;
  font-size: 13px;
  line-height: 1.45;
}
.warn-box.hidden { display: none; }
.btn.danger { color: var(--bad); border-color: #5a2b2b; }
.btn.danger:hover { background: #2a1616; }

/* -------------------------------------------------------- camera discovery */

.scan-results { display: grid; gap: 10px; margin-top: 14px; }
.scan-row {
  display: grid;
  gap: 6px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  grid-template-columns: 1fr auto;
  align-items: start;
}
.scan-main { display: grid; gap: 2px; }
.scan-main b { font-size: 14px; }
.scan-url {
  grid-column: 1 / -1;
  display: block;
  padding: 6px 8px;
  border-radius: 5px;
  background: #0b0d10;
  color: var(--muted);
  font-size: 12px;
  overflow-wrap: anywhere;
  user-select: all;
}
.scan-row .hint { grid-column: 1 / -1; margin: 0; }
.scan-row .btn { grid-row: 1; grid-column: 2; }

.diag { color: var(--muted); font-size: 12px; }

/* --------------------------------------------------------------------- tabs */

.tabs {
  display: flex;
  gap: 2px;
  padding: 0 10px;
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 47px;
  background: var(--panel);
  z-index: 1;
}
.tab {
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--muted);
  padding: 9px 12px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.tab:hover { color: var(--text); }
.tab.is-active { color: var(--text); border-bottom-color: var(--accent); }

.panel { display: none; }
.panel.is-active { display: block; }

/* -------------------------------------------------------------- record form */

.rec-form { display: grid; gap: 14px; padding: 14px; }
.rec-group {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px 12px;
  margin: 0;
  display: grid;
  gap: 8px;
}
.rec-group legend { padding: 0 6px; color: var(--muted); font-size: 12px; font-weight: 600; }
.rec-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.rec-form label { display: grid; gap: 4px; font-size: 12px; color: var(--muted); }
.rec-form select, .rec-form input[type=text], .rec-form input:not([type]) { width: 100%; }

label.switch, label.radio {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text);
  font-size: 13px;
  cursor: pointer;
}
label.switch input, label.radio input { width: auto; }

.cam-picker {
  display: grid;
  gap: 4px;
  max-height: 150px;
  overflow: auto;
  padding: 8px;
  border: 1px dashed var(--line);
  border-radius: 6px;
}
.cam-picker:empty::after {
  content: "No cameras yet - add one on the Cameras tab.";
  color: var(--muted);
  font-size: 12px;
}
.cam-picker label { display: flex; align-items: center; gap: 7px; color: var(--text); font-size: 13px; }
.cam-picker input { width: auto; }

.path-row { display: flex; gap: 6px; }
.path-row input { flex: 1; }

.notice {
  margin: 0;
  padding: 9px 11px;
  border-radius: 6px;
  background: #2a2416;
  border: 1px solid #4a3d1c;
  color: #e3cf94;
  font-size: 12px;
}

.estimate {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 8px;
}
.estimate .cell {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px 10px;
}
.estimate .k { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .4px; }
.estimate .v { font-size: 15px; font-weight: 600; font-variant-numeric: tabular-nums; }
.estimate .v.warn { color: var(--warn); }
.estimate .v.bad { color: var(--bad); }

/* --------------------------------------------------------- folder browser */

.dir-dialog { width: min(560px, 92vw); }
.dir-path { display: flex; align-items: center; gap: 8px; padding: 10px 14px; }
.dir-path code {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  direction: rtl;
  text-align: left;
  color: var(--muted);
  font-size: 12px;
}
.dir-list { max-height: 320px; overflow: auto; padding: 0 8px 8px; }
.dir-list button {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  border-radius: 5px;
  color: var(--text);
  padding: 6px 9px;
  font: inherit;
  cursor: pointer;
}
.dir-list button:hover { background: var(--panel-2); }
.dir-list .ro { color: var(--muted); font-size: 11px; }

.cam-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.cam-table th, .cam-table td { padding: 7px 10px; text-align: left; border-bottom: 1px solid var(--line); }
.cam-table th { color: var(--muted); font-weight: 600; font-size: 12px; }
.cam-table td.src { color: var(--muted); font-family: ui-monospace, monospace; font-size: 11px; max-width: 260px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cam-table .row-off td { opacity: .5; }

.add-block { padding: 12px 14px; }
.add-block summary { cursor: pointer; color: var(--accent); font-weight: 600; }
.cam-form { display: grid; gap: 10px; margin-top: 12px; }
.cam-form label { display: grid; gap: 4px; font-size: 12px; color: var(--muted); }
.cam-form input, .cam-form select { width: 100%; }
.sub { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.form-actions { display: flex; justify-content: flex-end; gap: 8px; }
.hint { color: var(--muted); font-size: 12px; min-height: 1em; }

/* ------------------------------------------------------------------ states */

.empty {
  position: fixed; inset: 53px 0 0 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 6px; text-align: center; padding: 20px;
}
.empty h2 { margin: 0; }
.empty p { color: var(--muted); margin: 0 0 10px; max-width: 46ch; }
.empty-actions { display: flex; gap: 8px; }
.hidden { display: none !important; }

.toast {
  position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%);
  background: var(--panel-2); border: 1px solid var(--line);
  padding: 9px 14px; border-radius: 8px; z-index: 60;
  box-shadow: 0 6px 20px rgb(0 0 0 / .45);
}
.toast.bad { border-color: #5a2b2b; color: #ffb4b4; }
