/* Clean dark/light NVR theme inspired by Frigate & brick-live. */
:root {
  color-scheme: dark;
  --bg: hsl(0 0% 0%);
  --bg-alt: hsl(0 0% 9%);
  --fg: hsl(0 0% 100%);
  --card: hsl(0 0% 12%);
  --secondary: hsl(0 0% 15%);
  --secondary-fg: hsl(0 0% 64%);
  --secondary-hover: hsl(0 0% 25%);
  --muted-fg: hsl(0 0% 55%);
  --border: hsl(0 0% 20%);
  --input: hsl(0 0% 25%);
  --primary: hsl(0 0% 91%);
  --primary-fg: hsl(0 0% 9%);
  --selected: hsl(228 89% 63%);
  --selected-bg: hsl(226 71% 40% / 0.6);
  --rec: hsl(0 84% 58%);
  --motion: hsl(35 95% 54%);
  --motion-bg: hsl(35 95% 54% / 0.22);
  --recorded: hsl(205 72% 45% / 0.55);
  --danger: hsl(0 100% 83%);
  --radius: 0.5rem;
  --sidebar: 3.25rem;
  --tile-min: 20rem;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
}
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    color-scheme: light;
    --bg: hsl(0 0% 100%);
    --bg-alt: hsl(0 0% 98.5%);
    --fg: hsl(222.2 84% 4.9%);
    --card: hsl(0 0% 100%);
    --secondary: hsl(210 20% 94.1%);
    --secondary-fg: hsl(222.2 17.4% 36.2%);
    --secondary-hover: hsl(210 17% 88%);
    --muted-fg: hsl(215.4 6.3% 46.9%);
    --border: hsl(214.3 31.8% 91.4%);
    --input: hsl(0 0% 85%);
    --primary: hsl(222.2 47.4% 11.2%);
    --primary-fg: hsl(210 40% 98%);
    --selected-bg: hsl(226 71% 40% / 0.15);
    --selected: hsl(228 80% 50%);
    --recorded: hsl(205 78% 48% / 0.45);
    --danger: hsl(0 72% 45%);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; background: var(--bg); color: var(--fg); }
html { overscroll-behavior: none; -webkit-text-size-adjust: 100%; }
body {
  font: 14px/1.4 Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent;
}
svg { width: 1.25rem; height: 1.25rem; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
button { font: inherit; color: inherit; cursor: pointer; }
:focus-visible { outline: 2px solid var(--selected); outline-offset: 2px; }
.muted { color: var(--muted-fg); font-size: 0.8rem; }

.btn {
  display: inline-flex; align-items: center; gap: 0.4rem;
  height: 2.25rem; padding: 0 0.85rem; border: 0; border-radius: var(--radius);
  background: var(--secondary); color: var(--fg); white-space: nowrap;
}
.btn:hover { background: var(--secondary-hover); }
.btn.small { height: 1.85rem; padding: 0 0.6rem; font-size: 0.8rem; }
.btn.icon { width: 2.25rem; padding: 0; justify-content: center; }
.btn.icon.small { width: 1.85rem; }
.btn.primary { background: var(--selected); color: #fff; justify-content: center; }
.btn.primary:hover { filter: brightness(1.1); }
.btn:disabled { opacity: 0.55; cursor: default; }

.select, .date-input {
  height: 2.1rem; padding: 0 0.6rem; border: 1px solid var(--input); border-radius: var(--radius);
  background: var(--secondary); color: var(--fg); font: inherit; font-size: 0.85rem;
}
.pill {
  padding: 0.25rem 0.6rem; border-radius: 999px; background: var(--secondary);
  color: var(--secondary-fg); font-size: 0.75rem; white-space: nowrap;
}

/* Login */
.login {
  min-height: 100dvh; display: grid; place-items: center;
  padding: calc(2rem + var(--safe-top)) calc(1rem + var(--safe-right)) calc(2rem + var(--safe-bottom)) calc(1rem + var(--safe-left));
}
.login-form { width: min(100%, 350px); display: flex; flex-direction: column; gap: 1rem; }
.login-logo { width: 2.5rem; height: 2.5rem; margin: 0 auto; color: var(--selected); }
.login-form h1 { margin: 0 0 0.5rem; text-align: center; font-size: 1.25rem; font-weight: 600; }
.login-form label { display: flex; flex-direction: column; gap: 0.4rem; font-weight: 500; }
.login-form input {
  height: 2.5rem; padding: 0 0.75rem; border: 1px solid var(--input); border-radius: var(--radius);
  background: transparent; color: var(--fg); font: inherit;
}
.login-error { min-height: 1.2em; margin: 0; color: var(--danger); font-size: 0.85rem; }

/* Shell */
.sidebar {
  position: fixed; inset: 0 auto 0 0; width: calc(var(--sidebar) + var(--safe-left)); z-index: 10;
  display: flex; flex-direction: column; align-items: center; gap: 0.75rem;
  padding: calc(0.75rem + var(--safe-top)) 0 calc(0.75rem + var(--safe-bottom)) var(--safe-left);
  background: var(--bg-alt); border-right: 1px solid var(--border);
}
.brand { width: 1.6rem; height: 1.6rem; color: var(--selected); margin-bottom: 0.25rem; }
.groups { flex: 1; min-height: 0; overflow-y: auto; scrollbar-width: none; display: flex; flex-direction: column; align-items: center; gap: 0.5rem; padding: 2px; }
.nav-btn {
  position: relative; display: grid; place-items: center; width: 2.25rem; height: 2.25rem; flex: none;
  border: 0; border-radius: var(--radius); background: var(--secondary); color: var(--secondary-fg);
  font-size: 0.75rem; font-weight: 600; letter-spacing: 0.02em;
}
.nav-btn:hover { background: var(--secondary-hover); color: var(--fg); }
.nav-btn[aria-pressed="true"] { background: var(--selected-bg); color: var(--selected); }
main { margin-left: calc(var(--sidebar) + var(--safe-left)); min-height: 100dvh; padding-right: var(--safe-right); }

.toolbar {
  position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: 0.65rem;
  padding: calc(0.75rem + var(--safe-top)) 1rem 0.75rem; background: var(--bg);
}
.view-title { font-size: 1rem; font-weight: 600; margin-right: 0.25rem; }
.search {
  display: flex; align-items: center; gap: 0.5rem; flex: 0 1 22rem; min-width: 0;
  height: 2.25rem; padding: 0 0.4rem 0 0.75rem; border: 1px solid var(--input); border-radius: var(--radius);
  color: var(--muted-fg);
}
.search:focus-within { border-color: var(--selected); }
.search svg { width: 1rem; height: 1rem; }
.search input { flex: 1; min-width: 0; border: 0; outline: 0; background: transparent; color: var(--fg); font: inherit; }
.search input::-webkit-search-cancel-button { display: none; }
.heading { margin-left: auto; color: var(--muted-fg); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* Camera grid */
.grid {
  display: grid; gap: 0.75rem; padding: 0 1rem calc(1rem + var(--safe-bottom)); align-items: start;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, var(--tile-min)), 1fr));
}
.tile {
  position: relative; display: block; overflow: hidden; border-radius: var(--radius);
  background: #000; aspect-ratio: 16 / 9; color: #fff; text-decoration: none;
  outline: 1px solid var(--border); outline-offset: -1px;
  -webkit-touch-callout: none; user-select: none;
}
.tile:hover { outline: 2px solid var(--selected); outline-offset: -2px; }
.tile.has-motion { outline: 2px solid var(--motion); outline-offset: -2px; }
.tile-badges {
  position: absolute; top: 0.5rem; right: 0.5rem; display: flex; gap: 0.35rem; pointer-events: none;
}
.rec-badge, .motion-badge {
  display: inline-flex; align-items: center; gap: 0.3rem;
  padding: 0.12rem 0.45rem; border-radius: 999px; font-size: 0.68rem; font-weight: 700; letter-spacing: 0.04em;
}
.rec-badge { background: rgb(0 0 0 / 0.65); color: #fff; }
.rec-badge::before { content: ""; width: 0.45rem; height: 0.45rem; border-radius: 50%; background: var(--rec); }
.motion-badge { background: var(--motion); color: #000; }
.sleep-badge {
  display: inline-flex; align-items: center; gap: 0.3rem;
  padding: 0.12rem 0.5rem; border-radius: 999px; font-size: 0.68rem; font-weight: 600;
  background: rgb(0 0 0 / 0.68); color: rgb(255 255 255 / 0.85);
}
.ondemand-poster {
  position: relative; width: 100%; height: 100%;
  display: grid; place-items: center;
  background: radial-gradient(circle at center, hsl(225 18% 15%), hsl(0 0% 5%));
  color: rgb(255 255 255 / 0.78); font-size: 0.85rem; text-align: center;
}
.ondemand-poster img {
  position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; opacity: 0.55;
}
.ondemand-poster .wake-cta {
  position: relative; z-index: 1; padding: 0.35rem 0.75rem; border-radius: 999px;
  background: rgb(0 0 0 / 0.7); border: 1px solid rgb(255 255 255 / 0.18);
  font-size: 0.8rem; font-weight: 500;
}
.label {
  position: absolute; left: 0.5rem; bottom: 0.5rem; max-width: calc(100% - 1rem);
  display: flex; flex-direction: column; padding: 0.2rem 0.45rem; border-radius: 0.375rem;
  background: rgb(0 0 0 / 0.6); pointer-events: none; font-size: 0.8rem; line-height: 1.25;
}
.label strong { font-weight: 600; overflow-wrap: anywhere; }
.label span { color: rgb(255 255 255 / 0.7); font-size: 0.7rem; }
.empty { padding: 2rem 1rem; color: var(--muted-fg); text-align: center; }

/* Live video element */
live-video { position: relative; display: block; width: 100%; height: 100%; background: #000; }
live-video video { object-fit: contain; }
live-video::after {
  position: absolute; inset: 0; display: grid; place-items: center; pointer-events: none;
  color: rgb(255 255 255 / 0.75); font-size: 0.8rem; text-align: center; padding: 1rem;
}
live-video[data-state="loading"]::after {
  content: ""; inset: calc(50% - 0.9rem); width: 1.8rem; height: 1.8rem; padding: 0;
  border: 3px solid rgb(255 255 255 / 0.2); border-top-color: #fff; border-radius: 50%;
  animation: spin 0.9s linear infinite;
}
live-video[data-state="error"]::after { content: "No signal. Reconnecting…"; }
live-video[data-state="unsupported"]::after { content: "This browser cannot decode this camera's video format."; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Events Feed */
.events-grid {
  display: grid; gap: 0.75rem; padding: 0 1rem calc(1.25rem + var(--safe-bottom));
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 17.5rem), 1fr));
}
.event-card {
  display: flex; flex-direction: column; overflow: hidden; border-radius: var(--radius);
  background: var(--card); border: 1px solid var(--border); cursor: pointer; text-align: left;
  transition: border-color 0.15s;
}
.event-card:hover { border-color: var(--selected); }
.event-thumb-wrap {
  position: relative; aspect-ratio: 16 / 9; background: #000; overflow: hidden;
}
.event-thumb { width: 100%; height: 100%; object-fit: cover; display: block; }
.event-thumb-empty {
  width: 100%; height: 100%; display: grid; place-items: center; color: var(--muted-fg); font-size: 0.8rem;
}
.event-cam-pill {
  position: absolute; left: 0.45rem; top: 0.45rem;
  padding: 0.15rem 0.45rem; border-radius: 0.3rem; background: rgb(0 0 0 / 0.7);
  color: #fff; font-size: 0.72rem; font-weight: 600;
}
.event-dur-pill {
  position: absolute; right: 0.45rem; bottom: 0.45rem;
  padding: 0.12rem 0.4rem; border-radius: 0.3rem; background: rgb(0 0 0 / 0.75);
  color: #fff; font-size: 0.72rem; font-variant-numeric: tabular-nums;
}
.event-meta {
  display: flex; align-items: center; justify-content: space-between; gap: 0.5rem;
  padding: 0.55rem 0.7rem;
}
.event-time { font-weight: 600; font-size: 0.85rem; font-variant-numeric: tabular-nums; }
.event-sub { color: var(--muted-fg); font-size: 0.75rem; }
.event-actions { display: flex; gap: 0.35rem; }

/* Single camera view */
.camera-view { min-height: 100dvh; display: flex; flex-direction: column; }
.camera-view.live-mode { height: 100dvh; }
.camera-title { display: flex; flex-direction: column; min-width: 0; margin-right: auto; }
.title-row { display: flex; align-items: center; gap: 0.45rem; min-width: 0; }
.camera-title strong { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.camera-title > span { color: var(--muted-fg); font-size: 0.78rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.segmented { display: flex; padding: 0.2rem; gap: 0.2rem; border-radius: var(--radius); background: var(--secondary); }
.segmented button { height: 1.85rem; padding: 0 0.7rem; border: 0; border-radius: 0.375rem; background: transparent; color: var(--secondary-fg); white-space: nowrap; font-size: 0.85rem; }
.segmented button[aria-pressed="true"] { background: var(--selected-bg); color: var(--selected); font-weight: 600; }
#sound[aria-pressed="true"] { background: var(--selected-bg); color: var(--selected); }

.stage {
  position: relative; flex: 1; min-height: 16rem;
  margin: 0 1rem calc(0.75rem + var(--safe-bottom)); border-radius: var(--radius);
  overflow: hidden; background: #000;
}
.camera-view:not(.live-mode) .stage {
  flex: 0 0 auto; aspect-ratio: 16 / 9; max-height: 58dvh; margin-bottom: 0.75rem;
}
.stage:fullscreen { margin: 0; border-radius: 0; max-height: none; }
.playback-video { width: 100%; height: 100%; background: #000; object-fit: contain; display: block; }
.stage-empty {
  position: absolute; inset: 0; display: grid; place-items: center;
  color: rgb(255 255 255 / 0.75); font-size: 0.9rem; text-align: center; padding: 1.5rem;
}

/* Playback timeline panel */
.playback-panel {
  display: flex; flex-direction: column; gap: 0.75rem;
  margin: 0 1rem calc(1.25rem + var(--safe-bottom));
  padding: 0.85rem 1rem; border-radius: var(--radius);
  background: var(--bg-alt); border: 1px solid var(--border);
}
.timeline-controls {
  display: flex; flex-wrap: wrap; align-items: center; gap: 0.65rem;
}
.date-nav { display: flex; align-items: center; gap: 0.35rem; }
.tl-clock {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.95rem; font-weight: 600; font-variant-numeric: tabular-nums;
  padding: 0.2rem 0.55rem; border-radius: 0.35rem; background: var(--secondary);
}
.tl-info { color: var(--muted-fg); font-size: 0.8rem; margin-right: auto; }

.timeline { user-select: none; touch-action: none; cursor: pointer; }
.tl-track {
  position: relative; height: 2.75rem; border-radius: 0.4rem;
  background: var(--secondary); border: 1px solid var(--border); overflow: hidden;
}
.tl-layer { position: absolute; inset: 0; pointer-events: none; }
.tl-seg {
  position: absolute; top: 0; bottom: 0; background: var(--recorded);
}
.tl-ev {
  position: absolute; top: 0; bottom: 0; background: var(--motion); opacity: 0.88;
  min-width: 3px; border-radius: 1px;
}
.tl-playhead {
  position: absolute; top: 0; bottom: 0; width: 2px; background: #fff;
  box-shadow: 0 0 6px rgb(0 0 0 / 0.8); pointer-events: none; z-index: 3;
}
.tl-hover {
  position: absolute; top: 0.2rem; transform: translateX(-50%);
  padding: 0.1rem 0.35rem; border-radius: 0.25rem; background: rgb(0 0 0 / 0.85); color: #fff;
  font-size: 0.7rem; font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  pointer-events: none; z-index: 4; white-space: nowrap;
}
.tl-ticks {
  position: relative; height: 1.25rem; margin-top: 0.25rem;
  color: var(--muted-fg); font-size: 0.7rem; font-variant-numeric: tabular-nums;
}
.tl-tick {
  position: absolute; top: 0; transform: translateX(-50%); white-space: nowrap;
}
.tl-tick:first-child { transform: none; }
.tl-tick:last-child { transform: translateX(-100%); }

/* Day motion events strip */
.day-events-wrap { display: flex; flex-direction: column; gap: 0.5rem; padding-top: 0.35rem; border-top: 1px solid var(--border); }
.day-events-head { display: flex; align-items: center; justify-content: space-between; }
.day-events {
  display: flex; gap: 0.6rem; overflow-x: auto; padding-bottom: 0.35rem;
  scrollbar-width: thin;
}
.day-ev-chip {
  flex: 0 0 10.5rem; display: flex; flex-direction: column; overflow: hidden;
  border-radius: 0.45rem; background: var(--card); border: 1px solid var(--border);
  padding: 0; text-align: left;
}
.day-ev-chip:hover, .day-ev-chip.active { border-color: var(--motion); }
.day-ev-chip img { width: 100%; aspect-ratio: 16 / 9; object-fit: cover; background: #000; display: block; }
.day-ev-chip .chip-meta {
  display: flex; justify-content: space-between; align-items: center;
  padding: 0.35rem 0.5rem; font-size: 0.75rem; font-variant-numeric: tabular-nums;
}

/* Mobile */
@media (max-width: 640px) {
  :root { --tile-min: 100%; }
  .sidebar {
    position: sticky; top: 0; inset: auto; width: auto; height: calc(3.25rem + var(--safe-top));
    flex-direction: row; padding: var(--safe-top) calc(0.75rem + var(--safe-right)) 0 calc(0.75rem + var(--safe-left));
    border-right: 0; border-bottom: 1px solid var(--border);
  }
  .groups { flex-direction: row; overflow-x: auto; overflow-y: hidden; height: 100%; }
  main { margin-left: 0; padding: 0 var(--safe-right) 0 var(--safe-left); }
  .toolbar { top: calc(3.25rem + var(--safe-top)); padding: 0.6rem 0.75rem; flex-wrap: wrap; }
  .heading, .pill { display: none; }
  .search { flex: 1 1 100%; }
  .grid, .events-grid { padding: 0 0.75rem calc(0.75rem + var(--safe-bottom)); }
  .camera-view.live-mode { height: calc(100dvh - 3.25rem - var(--safe-top)); }
  .camera-view .toolbar { top: 0; }
  .btn span { display: none; }
  #back { width: 2.25rem; padding: 0; justify-content: center; }
  .stage { margin: 0 0 0.5rem; border-radius: 0; }
  .playback-panel { margin: 0 0.5rem calc(1rem + var(--safe-bottom)); padding: 0.7rem; }
}
