:root {
  --ground: #EAEDEF;
  --surface: #FFFFFF;
  --surface-2: #F2F4F5;
  --ink: #171B20;
  --ink-muted: #5B6570;
  --line: #D7DBDE;
  --accent: #A9762A;
  --accent-ink: #1b1508;
  --live: #1C8C6C;
  --record: #B93A2B;
  --shadow: 0 1px 2px rgba(23,27,32,0.05), 0 10px 28px rgba(23,27,32,0.07);
  --font-ui: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --font-mono: ui-monospace, "SF Mono", "Cascadia Mono", "Roboto Mono", "Courier New", monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --ground: #12161B;
    --surface: #1A2129;
    --surface-2: #212A33;
    --ink: #E7EAEE;
    --ink-muted: #8A93A0;
    --line: #2A323C;
    --accent: #D6A24C;
    --accent-ink: #1b1508;
    --live: #3FBF93;
    --record: #E9604F;
    --shadow: 0 1px 2px rgba(0,0,0,0.4), 0 16px 40px rgba(0,0,0,0.4);
  }
}
:root[data-theme="dark"] {
  --ground: #12161B; --surface: #1A2129; --surface-2: #212A33;
  --ink: #E7EAEE; --ink-muted: #8A93A0; --line: #2A323C;
  --accent: #D6A24C; --accent-ink: #1b1508;
  --live: #3FBF93; --record: #E9604F;
  --shadow: 0 1px 2px rgba(0,0,0,0.4), 0 16px 40px rgba(0,0,0,0.4);
}
:root[data-theme="light"] {
  --ground: #EAEDEF; --surface: #FFFFFF; --surface-2: #F2F4F5;
  --ink: #171B20; --ink-muted: #5B6570; --line: #D7DBDE;
  --accent: #A9762A; --accent-ink: #1b1508;
  --live: #1C8C6C; --record: #B93A2B;
  --shadow: 0 1px 2px rgba(23,27,32,0.05), 0 10px 28px rgba(23,27,32,0.07);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
/* Author `display` rules below (e.g. .login-shell { display: flex }) would
   otherwise beat the UA stylesheet's [hidden] { display: none }, since
   author-origin rules always win over user-agent-origin ones regardless
   of specificity — so `hidden` needs an explicit, unconditional override. */
[hidden] { display: none !important; }
body {
  background: var(--ground);
  color: var(--ink);
  font-family: var(--font-ui);
  -webkit-font-smoothing: antialiased;
}
button { font-family: inherit; }
a { color: var(--accent); }

/* ---------- Login ---------- */
.login-shell {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.login-card {
  width: 100%;
  max-width: 340px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: var(--shadow);
  padding: 28px 24px;
}
.login-card .wordmark {
  font-family: var(--font-mono);
  font-size: 20px;
  font-weight: 600;
  letter-spacing: 0.06em;
  margin: 0 0 20px;
}
.field { margin-bottom: 14px; }
.field label {
  display: block;
  font-size: 11px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--ink-muted);
  margin-bottom: 6px;
}
.field input {
  width: 100%;
  padding: 10px 12px;
  border-radius: 7px;
  border: 1px solid var(--line);
  background: var(--surface-2);
  color: var(--ink);
  font-size: 14px;
  font-family: var(--font-ui);
}
.field input:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
.login-error {
  color: var(--record);
  font-size: 13px;
  min-height: 18px;
  margin-bottom: 8px;
}

/* ---------- Buttons ---------- */
.btn {
  font-family: var(--font-ui);
  font-size: 13px;
  font-weight: 600;
  padding: 9px 14px;
  border-radius: 7px;
  border: 1px solid var(--line);
  background: var(--surface-2);
  color: var(--ink);
  cursor: pointer;
}
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn--accent { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn--full { width: 100%; }
.btn--small { padding: 6px 10px; font-size: 12px; }

/* ---------- App shell ---------- */
.app {
  min-height: 100vh;
  background: var(--surface);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 16px 20px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
  flex-wrap: wrap;
}
.brand { display: flex; flex-direction: column; gap: 2px; }
.brand .wordmark {
  font-family: var(--font-mono);
  font-size: 19px;
  font-weight: 600;
  letter-spacing: 0.06em;
}
.topbar__right { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
.clock {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  font-size: 13px;
  color: var(--ink-muted);
}
.role-pill {
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: 0.08em;
  padding: 5px 9px;
  border-radius: 6px;
  border: 1px solid var(--line);
  color: var(--ink-muted);
}

.access {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 20px;
  font-size: 12.5px;
  border-bottom: 1px solid var(--line);
  background: var(--surface-2);
  color: var(--ink-muted);
  flex-wrap: wrap;
}
.access strong { color: var(--ink); font-weight: 600; }
.access .dot {
  display: inline-block; width: 6px; height: 6px; border-radius: 50%;
  background: var(--live); margin-right: 6px; position: relative; top: -1px;
}
.access.is-closed .dot { background: var(--ink-muted); }
.access.is-disabled .dot { background: var(--accent); }
.access.is-disabled strong { color: var(--accent); }

/* ---------- Camera grid ---------- */
.grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1px;
  background: var(--line);
}
@media (max-width: 700px) {
  .grid { grid-template-columns: 1fr; }
}

.tile { background: var(--surface); padding: 16px 16px 14px; }
.tile__frame {
  position: relative;
  aspect-ratio: 16 / 9;
  border-radius: 4px;
  overflow: hidden;
  background: #0e1114;
  outline: 1px solid rgba(255,255,255,0.06);
}
.tile__frame video {
  width: 100%; height: 100%; object-fit: cover; display: block;
  background: #0e1114;
}

/* Access closed (guest outside 08:00-17:00): plain black, no feed
   attempted at all — not an error state, a deliberate one. */
.tile__frame.is-closed { background: #000; }
.tile__frame.is-closed video { display: none; }
.tile__closed-label {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  font-family: var(--font-mono); letter-spacing: 0.12em; font-size: 12.5px;
  color: #5a5f66; text-transform: uppercase;
}

.corner {
  position: absolute;
  width: 16px; height: 16px;
  border-color: rgba(255,255,255,0.55);
  border-style: solid;
  border-width: 0;
  pointer-events: none;
}
.corner.tl { top: 8px; left: 8px; border-top-width: 2px; border-left-width: 2px; }
.corner.tr { top: 8px; right: 8px; border-top-width: 2px; border-right-width: 2px; }
.corner.bl { bottom: 8px; left: 8px; border-bottom-width: 2px; border-left-width: 2px; }
.corner.br { bottom: 8px; right: 8px; border-bottom-width: 2px; border-right-width: 2px; }

.tile__status {
  position: absolute; top: 8px; left: 32px; right: 32px;
  display: flex; gap: 6px; flex-wrap: wrap;
  pointer-events: none;
}
.tile__tools {
  position: absolute; top: 8px; right: 32px;
  display: flex; gap: 4px;
}
.chip {
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 0.08em;
  padding: 3px 6px;
  border-radius: 3px;
  background: rgba(0,0,0,0.45);
  color: #D8DCE1;
}
.chip--offline { color: #9aa2ab; }
.chip--live { color: #bdf2dd; }
.chip--rec { color: #ffd2ca; }
.chip--live .dot, .chip--rec .dot {
  display: inline-block; width: 5px; height: 5px; border-radius: 50%;
  margin-right: 4px; position: relative; top: -1px;
}
.chip--live .dot { background: var(--live); }
.chip--rec .dot { background: var(--record); }
@media (prefers-reduced-motion: no-preference) {
  .chip--live .dot { animation: pulse 2.2s ease-in-out infinite; }
}
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.35; } }

.tile__icon-btn {
  width: 22px; height: 22px;
  display: flex; align-items: center; justify-content: center;
  padding: 0;
  border: none;
  border-radius: 3px;
  background: rgba(0,0,0,0.45);
  color: #D8DCE1;
  cursor: pointer;
}
.tile__icon-btn:hover { background: rgba(0,0,0,0.65); }
.tile__icon-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.tile__icon-btn svg { width: 13px; height: 13px; }

/* Fullscreen: a CSS-class-based "fake fullscreen" (fixed positioning),
   not the native Fullscreen API — iOS Safari doesn't support
   requestFullscreen() on arbitrary elements (only <video>), so this is
   the one approach that behaves identically on every device. The whole
   .tile (frame + controls) fills the screen so Live/Go-back/scrub stay
   reachable, not just the bare video. */
body.has-pseudo-fullscreen { overflow: hidden; }
.tile.is-pseudo-fullscreen {
  /* inset: 0 on a fixed element fills the true visual viewport on its own —
     adding explicit 100vw/100vh here used to fight mobile Safari, where vh
     is sized off the largest-viewport state (address bar hidden) rather
     than what's actually visible, cropping the tile so only part of the
     video showed once the toolbar was on screen. */
  position: fixed; inset: 0; z-index: 1000;
  display: flex;
  flex-direction: column;
  justify-content: center;
  background: #000;
  padding: 0;
}
.tile.is-pseudo-fullscreen .tile__frame {
  flex: 1;
  aspect-ratio: unset;
  border-radius: 0;
  outline: none;
}
.tile.is-pseudo-fullscreen .tile__controls {
  position: absolute;
  bottom: 0; left: 0; right: 0;
  margin: 0;
  padding: 10px 16px;
  background: rgba(0,0,0,0.55);
  border-top: none;
}
.tile.is-pseudo-fullscreen .scrub {
  position: absolute;
  bottom: 46px; left: 0; right: 0;
  margin: 0;
  padding: 8px 16px;
  background: rgba(0,0,0,0.55);
}

.tile__center {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 8px; color: #6b727b; text-align: center; padding: 0 24px;
  pointer-events: none;
}
.tile__center p { margin: 0; font-size: 12.5px; line-height: 1.5; color: #8a919a; max-width: 220px; }

.tile__label {
  position: absolute; left: 10px; bottom: 8px;
  font-family: var(--font-mono); font-size: 10.5px; letter-spacing: 0.08em;
  color: #cfd3d8; text-shadow: 0 1px 2px rgba(0,0,0,0.6);
  pointer-events: none;
}
.tile__ts {
  position: absolute; right: 10px; bottom: 8px;
  font-family: var(--font-mono); font-variant-numeric: tabular-nums;
  font-size: 10.5px; color: #cfd3d8; text-shadow: 0 1px 2px rgba(0,0,0,0.6);
  pointer-events: none;
}

.tile__controls {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  margin-top: 10px; flex-wrap: wrap;
}
.tile__controls .hint { font-size: 12px; color: var(--ink-muted); }
.scrub {
  display: flex; align-items: center; gap: 8px; margin-top: 8px; flex-wrap: wrap;
}
.scrub input[type="range"] { flex: 1; min-width: 120px; accent-color: var(--accent); }
.scrub .scrub-time {
  font-family: var(--font-mono); font-variant-numeric: tabular-nums; font-size: 12px;
  color: var(--ink-muted); min-width: 48px; text-align: right;
}
.scrub-end {
  font-family: var(--font-mono); font-size: 10px; letter-spacing: 0.06em;
  color: var(--ink-muted); text-transform: uppercase; white-space: nowrap;
}
.scrub-end--live { color: var(--live); font-weight: 600; }

/* ---------- Control rail ---------- */
.rail {
  padding: 16px 20px; border-top: 1px solid var(--line);
}
.rail h2 {
  font-size: 11px; letter-spacing: 0.1em; text-transform: uppercase;
  color: var(--ink-muted); font-weight: 600; margin: 0 0 10px;
}

/* ---------- Recordings list ---------- */
.plist { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.plist li {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 10px 12px; border: 1px solid var(--line); border-radius: 7px;
  background: var(--surface-2); font-size: 13px; flex-wrap: wrap;
}
.plist .day { display: flex; flex-direction: column; gap: 2px; }
.plist .day b { font-weight: 600; }
.plist .day span { font-size: 11.5px; color: var(--ink-muted); font-family: var(--font-mono); }
.plist .actions { display: flex; gap: 8px; align-items: center; }
.tag {
  font-family: var(--font-mono); font-size: 10.5px; letter-spacing: 0.06em;
  padding: 3px 8px; border-radius: 20px; border: 1px solid var(--line);
  color: var(--ink-muted); background: var(--surface);
}

.range-row {
  display: flex; align-items: center; gap: 8px;
  flex-basis: 100%; margin-top: 4px;
}
.range-row input[type="time"] {
  font-family: var(--font-mono); font-size: 12px; padding: 4px 6px;
  border: 1px solid var(--line); border-radius: 5px;
  background: var(--surface); color: var(--ink);
}
.range-row .hint { color: var(--record); }

.empty-hint { font-size: 13px; color: var(--ink-muted); padding: 4px 0; }
