:root {
  --bg: #0b0d10;
  --panel: #13161b;
  --panel-2: #1a1e25;
  --line: #262b34;
  --line-2: #333a45;
  --text: #e8eaee;
  --muted: #8a919d;
  --accent: #f2b544;
  --accent-ink: #1d1503;
  --live: #3ddc84;
  --warn: #f2b544;
  --bad: #ff5d5d;
  --radius: 10px;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font: 15px/1.45 var(--sans); -webkit-font-smoothing: antialiased; }
body { overflow: hidden; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.mono { font-family: var(--mono); font-size: 12.5px; }

/* ---------- brand ---------- */
.brand { display: flex; align-items: center; gap: 10px; font-weight: 650; letter-spacing: .2px; white-space: nowrap; }
.brand.big { font-size: 22px; justify-content: center; margin-bottom: 18px; }
.brand-mark { width: 14px; height: 14px; border-radius: 4px; background: var(--accent); box-shadow: 0 0 0 3px rgba(242,181,68,.18); position: relative; }
.brand-mark::after { content: ""; position: absolute; inset: 4px; border-radius: 50%; background: var(--accent-ink); }

/* ---------- layout ---------- */
.screen { position: fixed; inset: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: calc(10px + var(--safe-t)) calc(14px + var(--safe-r)) 10px calc(14px + var(--safe-l));
  border-bottom: 1px solid var(--line); background: rgba(11,13,16,.92); backdrop-filter: blur(8px);
  flex: none; z-index: 5;
}
.topbar-mid { display: flex; align-items: center; gap: 10px; color: var(--muted); }
.clock { font-family: var(--mono); font-size: 13px; letter-spacing: .5px; }
.engine-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.engine-dot.ok { background: var(--live); box-shadow: 0 0 8px rgba(61,220,132,.6); }
.engine-dot.bad { background: var(--bad); }
.topbar-actions { display: flex; align-items: center; gap: 8px; }

.icon-btn { width: 38px; height: 38px; display: grid; place-items: center; border-radius: 9px; border: 1px solid var(--line); background: var(--panel); }
.icon-btn:hover { background: var(--panel-2); border-color: var(--line-2); }
.icon-btn.ghost { background: rgba(0,0,0,.35); border-color: rgba(255,255,255,.12); backdrop-filter: blur(6px); }

.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 9px; overflow: hidden; background: var(--panel); }
.seg button { border: 0; background: transparent; padding: 0 12px; height: 36px; color: var(--muted); font-size: 13px; font-weight: 550; }
.seg button + button { border-left: 1px solid var(--line); }
.seg button.on { background: var(--panel-2); color: var(--text); box-shadow: inset 0 -2px 0 var(--accent); }
.seg.full { display: flex; width: 100%; }
.seg.full button { flex: 1; height: 40px; }

/* ---------- grid ---------- */
.grid {
  flex: 1; min-height: 0; display: grid; gap: 6px; padding: 6px calc(6px + var(--safe-r)) calc(6px + var(--safe-b)) calc(6px + var(--safe-l));
  align-content: center; justify-content: center; overflow: auto;
}
.grid.scroll { align-content: start; }
.tile {
  position: relative; background: #000; border-radius: 8px; overflow: hidden; cursor: pointer;
  border: 1px solid var(--line); aspect-ratio: 16 / 9; min-width: 0; outline: none;
}
.tile:focus-visible { border-color: var(--accent); }
.tile:hover .tile-label { background: rgba(0,0,0,.7); }
.tile cam-player, .single-stage cam-player { position: absolute; inset: 0; display: block; }
.tile video, .single-stage video { width: 100%; height: 100%; object-fit: contain; background: #000; display: block; }
.grid.fill .tile video { object-fit: cover; }
.tile-label {
  position: absolute; left: 8px; top: 8px; display: flex; align-items: center; gap: 8px;
  padding: 4px 9px 4px 7px; border-radius: 6px; background: rgba(0,0,0,.55); backdrop-filter: blur(4px);
  font-size: 13px; font-weight: 600; pointer-events: none; max-width: calc(100% - 16px);
}
.tile-label .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cam-index { font-family: var(--mono); font-size: 11px; color: var(--accent); letter-spacing: .5px; }
.status-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--warn); flex: none; animation: pulse 1.4s infinite; }
.status-dot.live { background: var(--live); animation: none; }
.status-dot.error { background: var(--bad); animation: none; }
@keyframes pulse { 50% { opacity: .35; } }
.tile-msg {
  position: absolute; inset: 0; display: grid; place-items: center; text-align: center; padding: 20px;
  color: var(--muted); font-size: 13px; pointer-events: none; overflow-wrap: anywhere;
}
.tile-msg > div { max-width: 100%; }
.tile-msg .spinner { width: 22px; height: 22px; border-radius: 50%; border: 2px solid var(--line-2); border-top-color: var(--accent); animation: spin .9s linear infinite; margin: 0 auto 8px; }
@keyframes spin { to { transform: rotate(360deg); } }
.tile-time { position: absolute; right: 8px; bottom: 8px; font-family: var(--mono); font-size: 11px; color: rgba(255,255,255,.75); text-shadow: 0 1px 2px #000; pointer-events: none; }

/* ---------- empty ---------- */
.empty { position: absolute; inset: 60px 0 0; display: grid; place-items: center; padding: 20px; }
.empty-card { max-width: 380px; text-align: center; }
.empty-icon { width: 64px; height: 64px; margin: 0 auto 14px; border-radius: 16px; display: grid; place-items: center; background: var(--panel); border: 1px solid var(--line); color: var(--accent); }
.empty-icon svg { width: 30px; height: 30px; }
.empty h2 { margin: 0 0 6px; font-size: 20px; }
.empty p { color: var(--muted); margin: 0 0 18px; }

/* ---------- single view ---------- */
.single { position: fixed; inset: 0; background: #000; z-index: 20; }
.single-stage { position: absolute; inset: 0; }
.single-top, .single-bottom { position: absolute; left: 0; right: 0; display: flex; align-items: center; gap: 10px; padding: 12px calc(14px + var(--safe-r)) 12px calc(14px + var(--safe-l)); transition: opacity .25s; z-index: 2; }
.single-top { top: 0; padding-top: calc(12px + var(--safe-t)); background: linear-gradient(rgba(0,0,0,.65), transparent); }
.single-bottom { bottom: 0; justify-content: center; padding-bottom: calc(16px + var(--safe-b)); background: linear-gradient(transparent, rgba(0,0,0,.65)); }
.single.idle .single-top, .single.idle .single-bottom { opacity: 0; pointer-events: none; }
.single.idle { cursor: none; }
.single-title { flex: 1; display: flex; align-items: center; gap: 10px; font-weight: 650; font-size: 17px; min-width: 0; }
.single-title #singleName { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.single-actions { display: flex; gap: 8px; }
.live-pill { font-size: 11px; font-weight: 700; letter-spacing: .8px; text-transform: uppercase; padding: 3px 8px; border-radius: 5px; background: rgba(242,181,68,.18); color: var(--warn); }
.live-pill.live { background: rgba(61,220,132,.18); color: var(--live); }
.live-pill.error { background: rgba(255,93,93,.18); color: var(--bad); }
.pill-btn { display: inline-flex; align-items: center; gap: 8px; height: 42px; padding: 0 16px; border-radius: 21px; border: 1px solid rgba(255,255,255,.14); background: rgba(20,22,26,.7); backdrop-filter: blur(8px); font-size: 14px; font-weight: 550; }
.pill-btn:hover { background: rgba(40,44,52,.85); }
.pill-btn.on { border-color: var(--accent); color: var(--accent); }
#muteBtn .ico-sound { display: none; }
#muteBtn.on .ico-sound { display: block; }
#muteBtn.on .ico-muted { display: none; }

/* ---------- drawer ---------- */
.scrim { position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 30; }
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; width: min(420px, 100vw); z-index: 31;
  background: var(--panel); border-left: 1px solid var(--line); display: flex; flex-direction: column;
  transform: translateX(100%); transition: transform .22s ease; padding-top: var(--safe-t); padding-right: var(--safe-r);
}
.drawer.open { transform: none; box-shadow: -20px 0 50px rgba(0,0,0,.5); }
.drawer-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px; border-bottom: 1px solid var(--line); }
.drawer-head h2 { margin: 0; font-size: 17px; }
.drawer-body { flex: 1; overflow: auto; padding: 16px; padding-bottom: calc(16px + var(--safe-b)); display: flex; flex-direction: column; gap: 14px; }

.cam-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.cam-row { display: flex; align-items: center; gap: 10px; padding: 10px 10px 10px 12px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel-2); }
.cam-row .meta { flex: 1; min-width: 0; }
.cam-row .meta b { display: block; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cam-row .meta span { font-family: var(--mono); font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; display: block; }
.cam-row.disabled .meta b { color: var(--muted); }
.cam-row .row-btns { display: flex; gap: 4px; }
.cam-row .row-btns button { width: 32px; height: 32px; border-radius: 7px; border: 1px solid transparent; background: transparent; display: grid; place-items: center; color: var(--muted); }
.cam-row .row-btns button:hover { border-color: var(--line-2); color: var(--text); }
.cam-row .row-btns button:disabled { opacity: .25; pointer-events: none; }
.cam-row .row-btns svg { width: 17px; height: 17px; }

label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); font-weight: 550; }
.field-label { font-size: 13px; color: var(--muted); font-weight: 550; margin-bottom: -6px; }
input:not([type=checkbox]) {
  font: inherit; font-size: 15px; color: var(--text); background: var(--bg); border: 1px solid var(--line-2);
  border-radius: 8px; height: 42px; padding: 0 12px; outline: none; width: 100%;
}
select {
  font: inherit; font-size: 15px; color: var(--text); background: var(--bg); border: 1px solid var(--line-2);
  border-radius: 8px; height: 42px; padding: 0 10px; outline: none; width: 100%;
}
input:focus, select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(242,181,68,.15); }
label.check { flex-direction: row; align-items: center; gap: 10px; color: var(--text); font-weight: 500; font-size: 14px; }
label.check input { width: 18px; height: 18px; accent-color: var(--accent); }
[data-for] { display: flex; flex-direction: column; gap: 14px; }
.hint { margin: -4px 0 0; font-size: 12.5px; color: var(--muted); line-height: 1.5; }
details.adv summary { font-size: 13px; color: var(--muted); cursor: pointer; }
details.adv[open] summary { margin-bottom: 10px; }

.btn { height: 42px; padding: 0 16px; border-radius: 9px; border: 1px solid var(--line-2); background: var(--panel-2); font-weight: 600; font-size: 14px; }
.btn:hover { border-color: #46505e; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.06); }
.btn.subtle { background: transparent; }
.btn.danger { background: transparent; color: var(--bad); border-color: rgba(255,93,93,.35); }
.btn.block { width: 100%; }
.btn:disabled { opacity: .55; pointer-events: none; }
.form-actions { display: flex; gap: 10px; }
.form-actions .btn { flex: 1; }
.form-error { margin: 0; color: var(--bad); font-size: 13px; min-height: 0; }
.form-error:empty { display: none; }

.test-box { border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; background: var(--bg); }
.test-box img { display: block; width: 100%; aspect-ratio: 16/9; object-fit: cover; background: #000; }
.test-box img[src=""], .test-box img:not([src]) { display: none; }
.test-box p { margin: 0; padding: 10px 12px; font-size: 13px; }
.test-box.ok p { color: var(--live); }
.test-box.bad p { color: var(--bad); }
.test-box.wait p { color: var(--muted); }

.drawer-foot { margin-top: auto; border-top: 1px solid var(--line); padding-top: 14px; display: flex; flex-direction: column; gap: 10px; }
.foot-row { display: flex; justify-content: space-between; gap: 10px; font-size: 13px; color: var(--muted); }
.foot-row span:last-child { color: var(--text); text-align: right; }

/* ---------- login ---------- */
#login { align-items: center; justify-content: center; padding: 20px; }
.login-card { width: min(340px, 100%); display: flex; flex-direction: column; gap: 14px; padding: 26px; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; }

/* ---------- toast ---------- */
.toast { position: fixed; left: 50%; bottom: calc(24px + var(--safe-b)); transform: translateX(-50%); background: var(--panel-2); border: 1px solid var(--line-2); padding: 10px 16px; border-radius: 10px; z-index: 50; font-size: 14px; box-shadow: 0 10px 30px rgba(0,0,0,.4); }

/* ---------- small screens ---------- */
@media (max-width: 640px) {
  .seg#layoutSeg button[data-cols="3"], .seg#layoutSeg button[data-cols="4"] { display: none; }
  .clock { display: none; }
  .pill-btn span { display: none; }
  .pill-btn { width: 46px; padding: 0; justify-content: center; }
}
@media (max-width: 420px) {
  .brand { font-size: 0; gap: 0; }
}

/* keep form rows from collapsing when the drawer content is taller than the screen */
.drawer-body > *, [data-for] > * { flex-shrink: 0; }
details.adv { display: flex; flex-direction: column; }
details.adv[open] > * + * { margin-top: 12px; }
details.adv .hint { margin-top: 6px; }

/* accounts */
.card-title { margin: 0; font-size: 18px; }
.hint.center { text-align: center; margin: 0; }
.login-card p { margin: 0; }
.drawer .tabs { margin: 12px 16px 0; width: auto; flex-shrink: 0; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.stack > * { flex-shrink: 0; }
.cam-checks { display: flex; flex-direction: column; gap: 8px; margin-top: 10px; padding-left: 4px; }
.cam-checks label.check { font-size: 14px; }
.cam-checks.off { opacity: .45; pointer-events: none; }
#camAccess { display: flex; flex-direction: column; gap: 10px; }
.badge { font-family: var(--mono); font-size: 10.5px; text-transform: uppercase; letter-spacing: .6px; padding: 2px 6px; border-radius: 4px; background: var(--panel); border: 1px solid var(--line-2); color: var(--muted); margin-left: 6px; vertical-align: 1px; }
.badge.admin { color: var(--accent); border-color: rgba(242,181,68,.4); }
.badge.off { color: var(--bad); border-color: rgba(255,93,93,.4); }
