:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #eef1f4;
  --border: #dde2e8;
  --text: #17202a;
  --muted: #5d6b7a;
  --accent: #1f6feb;
  --accent-contrast: #ffffff;
  --danger: #d1242f;
  --ok: #1a7f37;
  --warn: #b7791f;
  --person: #d1242f;
  --animal: #1a7f37;
  --vehicle: #c26a00;
  --motion: #6e7781;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 2px 8px rgba(16, 24, 40, .06);
  --radius: 14px;
  color-scheme: light;
}

:root[data-theme="dark"] {
  --bg: #0b0f14;
  --surface: #141a21;
  --surface-2: #1c242d;
  --border: #2a333d;
  --text: #e6edf3;
  --muted: #8b98a5;
  --accent: #4c9aff;
  --accent-contrast: #0b0f14;
  --danger: #ff6b6b;
  --ok: #3fb950;
  --warn: #e3b341;
  --person: #ff6b6b;
  --animal: #3fb950;
  --vehicle: #f0a24b;
  --motion: #8b98a5;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
}
a { color: var(--accent); }
h1, h2, h3 { line-height: 1.2; margin: 0 0 .5rem; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.hidden { display: none !important; }

/* ---------- layout ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: .75rem;
  /* On an installed iPhone app the page runs under the status bar: keep clear of it. */
  padding: calc(.6rem + env(safe-area-inset-top)) calc(1rem + env(safe-area-inset-right)) .6rem calc(1rem + env(safe-area-inset-left));
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: .5rem; font-weight: 700; font-size: 1.05rem; text-decoration: none; color: var(--text); }
.brand img { width: 28px; height: 28px; border-radius: 8px; }
.nav { display: flex; gap: .25rem; margin-left: 1rem; }
.nav a {
  padding: .4rem .75rem; border-radius: 999px; text-decoration: none; color: var(--muted); font-weight: 500;
}
.nav a.active { background: var(--surface-2); color: var(--text); }
.topbar .spacer { flex: 1; }
main { max-width: 1200px; margin: 0 auto; padding: 1rem; padding-bottom: 6rem; }

.bottom-nav { display: none; }
@media (max-width: 760px) {
  .nav { display: none; }
  /* Phones: Privacy is on the Live screen, Theme and Sign out are in Settings. */
  .topbar .topbar-actions { display: none; }
  /* App shell: only <main> scrolls, so the top bar and bottom nav can't drift
     when iOS bounces or resizes the page in the installed app. */
  html, body { height: 100%; overflow: hidden; overscroll-behavior: none; }
  body { min-height: 0; }
  /* Pinned to the edges instead of 100vh/100dvh: in the installed iOS app those units
     report the Safari-sized viewport and leave a band under the nav bar. */
  #app { position: fixed; inset: 0; display: flex; flex-direction: column; }
  .topbar { position: relative; flex: none; }
  main { flex: 1; width: 100%; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior-y: contain; padding-bottom: 1.5rem; }
  #app > .auth { flex: 1; min-height: 0; overflow-y: auto; }
  .bottom-nav {
    display: flex; flex: none; z-index: 20;
    background: var(--surface); border-top: 1px solid var(--border);
    padding-bottom: env(safe-area-inset-bottom);
  }
  body .toasts { bottom: calc(4.5rem + env(safe-area-inset-bottom)); }
  .bottom-nav a {
    flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px;
    padding: .5rem 0 .4rem; font-size: .7rem; color: var(--muted); text-decoration: none;
  }
  .bottom-nav a.active { color: var(--accent); }
  .bottom-nav svg { width: 22px; height: 22px; }
}
/* Installed iPhone app: iOS 26+ has a WebKit bug (bugs.webkit.org/show_bug.cgi?id=301994)
   that makes the page shorter than the screen by the status-bar height and paints the
   leftover strip under it in the page background, which no element can reach. Give the
   nav the same background so the strip reads as part of the nav instead of a gap. */
@media (max-width: 760px) and (display-mode: standalone) {
  .bottom-nav { background: var(--bg); }
}

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 1rem;
}
.stack { display: flex; flex-direction: column; gap: 1rem; }
.row { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.grid { display: grid; gap: 1rem; }
.grid.cams { grid-template-columns: repeat(auto-fill, minmax(min(100%, 460px), 1fr)); }
.grid.events { grid-template-columns: repeat(auto-fill, minmax(min(100%, 240px), 1fr)); }
.grid.two { grid-template-columns: repeat(auto-fit, minmax(min(100%, 380px), 1fr)); align-items: start; }

/* ---------- controls ---------- */
button, .btn {
  font: inherit; cursor: pointer; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); padding: .5rem .9rem; font-weight: 500;
  display: inline-flex; align-items: center; gap: .4rem; text-decoration: none;
}
button:hover, .btn:hover { background: var(--surface-2); }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-contrast); }
button.primary:hover { filter: brightness(1.08); background: var(--accent); }
button.danger { color: var(--danger); }
button.ghost { border-color: transparent; background: transparent; }
button.icon { padding: .45rem; border-radius: 999px; }
button:disabled { opacity: .5; cursor: default; }
button svg { width: 18px; height: 18px; }

input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; padding: .55rem .7rem; width: 100%;
}
input:focus, select:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
input[type="checkbox"] { width: auto; }
input[type="range"] { padding: 0; }
label { display: block; font-size: .85rem; color: var(--muted); margin-bottom: .25rem; font-weight: 500; }
.field { margin-bottom: .8rem; }

.chips { display: flex; flex-wrap: wrap; gap: .4rem; }
.chip {
  border-radius: 999px; padding: .3rem .75rem; font-size: .85rem; border: 1px solid var(--border);
  background: var(--surface); cursor: pointer; user-select: none;
}
.chip.on { background: var(--accent); color: var(--accent-contrast); border-color: var(--accent); }

.switch { position: relative; display: inline-block; width: 42px; height: 24px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch span {
  position: absolute; inset: 0; background: var(--border); border-radius: 999px; transition: .2s; cursor: pointer;
}
.switch span::before {
  content: ""; position: absolute; width: 18px; height: 18px; left: 3px; top: 3px; border-radius: 50%;
  background: #fff; transition: .2s;
}
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::before { transform: translateX(18px); }

.badge {
  display: inline-flex; align-items: center; gap: .3rem; font-size: .75rem; font-weight: 600;
  padding: .15rem .55rem; border-radius: 999px; background: var(--surface-2); color: var(--muted);
}
.badge.person { background: color-mix(in srgb, var(--person) 16%, transparent); color: var(--person); }
.badge.animal { background: color-mix(in srgb, var(--animal) 16%, transparent); color: var(--animal); }
.badge.vehicle { background: color-mix(in srgb, var(--vehicle) 16%, transparent); color: var(--vehicle); }
.badge.ok { background: color-mix(in srgb, var(--ok) 16%, transparent); color: var(--ok); }
.badge.bad { background: color-mix(in srgb, var(--danger) 16%, transparent); color: var(--danger); }
.dot { width: 8px; height: 8px; border-radius: 50%; background: currentColor; display: inline-block; }
.rec { animation: pulse 1.2s infinite; }
@keyframes pulse { 50% { opacity: .3; } }

/* ---------- live ---------- */
.cam { overflow: hidden; padding: 0; }
.cam .frame { position: relative; background: #000; aspect-ratio: 16 / 9; }
.cam .frame img { width: 100%; height: 100%; object-fit: contain; display: block; color: transparent; }
.cam .frame .overlay {
  position: absolute; left: .6rem; top: .6rem; right: .6rem; display: flex; justify-content: space-between; gap: .5rem;
}
.cam .frame .overlay .badge { background: rgba(0, 0, 0, .55); color: #fff; backdrop-filter: blur(4px); }
.cam .frame .offline {
  position: absolute; inset: 0; display: grid; place-items: center; color: #aaa; text-align: center; padding: 1rem;
}
.cam .meta { padding: .7rem 1rem; }
.night { gap: .5rem; flex-wrap: wrap; margin-top: .5rem; }
.night select { padding: .35rem .5rem; font-size: .85rem; max-width: 100%; width: auto; }
.night .error { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 60%; }
/* Buttons over the video: fullscreen sits bottom-right; bottom-left is kept free for camera controls. */
.cam .frame .frame-actions {
  position: absolute; right: .6rem; bottom: .6rem; display: flex; gap: .4rem;
  margin: 0 env(safe-area-inset-right) env(safe-area-inset-bottom) 0;
}
.frame-btn {
  width: 2.4rem; height: 2.4rem; padding: 0; display: grid; place-items: center;
  background: rgba(0, 0, 0, .55); color: #fff; border: 1px solid rgba(255, 255, 255, .25); backdrop-filter: blur(4px);
}
.frame-btn:hover { background: rgba(0, 0, 0, .75); }
.frame-btn svg { width: 1.2rem; height: 1.2rem; }
.cam .frame:fullscreen { aspect-ratio: auto; width: 100%; height: 100%; }
.cam .frame:-webkit-full-screen { aspect-ratio: auto; width: 100%; height: 100%; }
/* Fallback where the Fullscreen API is missing (iPhone): fill the screen with CSS. */
.cam .frame.fs { position: fixed; inset: 0; z-index: 60; aspect-ratio: auto; }
body.frame-fs { overflow: hidden; }
/* Pan/tilt pad, bottom-left of the picture (the bottom-right corner holds the fullscreen button). */
.cam .frame .ptz {
  position: absolute; left: .6rem; bottom: .6rem; display: grid; gap: 2px;
  grid-template-columns: repeat(3, 34px); grid-template-rows: repeat(3, 34px);
  grid-template-areas: ". up ." "left . right" ". down .";
}
.cam .frame .ptz button {
  padding: 0; border: 0; border-radius: 50%; display: grid; place-items: center; cursor: pointer;
  background: rgba(0, 0, 0, .55); color: #fff; backdrop-filter: blur(4px); min-height: 0; width: 34px; height: 34px;
}
.cam .frame .ptz button svg { width: 18px; height: 18px; }
.cam .frame .ptz button:hover { background: rgba(0, 0, 0, .75); }
.cam .frame .ptz button:active { transform: scale(.92); }
.cam .frame .ptz.busy button { opacity: .5; cursor: progress; }
.cam .frame .ptz .up { grid-area: up; }
.cam .frame .ptz .left { grid-area: left; }
.cam .frame .ptz .right { grid-area: right; }
.cam .frame .ptz .down { grid-area: down; }
.cam .frame .ptz .step { grid-area: 2 / 2; font-size: .7rem; font-weight: 700; background: rgba(0, 0, 0, .35); }

.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: .75rem; }
@media (max-width: 560px) { .stats { grid-template-columns: repeat(2, 1fr); } }
.stat { padding: .8rem 1rem; }
.stat .n { font-size: 1.6rem; font-weight: 700; }
.stat .l { font-size: .8rem; color: var(--muted); }

/* ---------- activity ---------- */
.timeline {
  position: relative; height: 46px; border-radius: 10px; background: var(--surface-2); overflow: hidden; margin-top: .5rem;
}
.timeline .tick { position: absolute; top: 0; bottom: 0; width: 1px; background: var(--border); }
.timeline .tick span { position: absolute; bottom: 2px; left: 3px; font-size: .65rem; color: var(--muted); }
.timeline .mark {
  position: absolute; top: 6px; height: 22px; min-width: 3px; border-radius: 3px; cursor: pointer; opacity: .9;
}
.timeline .now { position: absolute; top: 0; bottom: 0; width: 2px; background: var(--accent); }
.mark.person, .legend .person { background: var(--person); }
.mark.animal, .legend .animal { background: var(--animal); }
.mark.vehicle, .legend .vehicle { background: var(--vehicle); }
.mark.motion, .legend .motion { background: var(--motion); }
.legend { display: flex; gap: .9rem; font-size: .75rem; color: var(--muted); flex-wrap: wrap; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: .3rem; vertical-align: -1px; }

.event { padding: 0; overflow: hidden; cursor: pointer; transition: transform .1s; }
.event:hover { transform: translateY(-1px); }
.event .thumb { aspect-ratio: 16 / 9; background: var(--surface-2); position: relative; }
.event .thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.event .thumb .star { position: absolute; top: .4rem; right: .5rem; color: #ffd33d; font-size: 1.2rem; text-shadow: 0 1px 3px #000; }
.event .thumb .dur { position: absolute; bottom: .4rem; right: .5rem; background: rgba(0,0,0,.65); color: #fff; font-size: .72rem; padding: .1rem .4rem; border-radius: 6px; }
.event .info { padding: .6rem .8rem; }
.event .time { font-weight: 600; }
.empty { text-align: center; padding: 3rem 1rem; color: var(--muted); }

/* ---------- modal ---------- */
.modal-bg {
  position: fixed; inset: 0; z-index: 50; background: rgba(0, 0, 0, .6); display: grid; place-items: center; padding: 1rem;
}
.modal { width: min(900px, 100%); max-height: 95vh; overflow: auto; }
.modal video { width: 100%; border-radius: 10px; background: #000; display: block; max-height: 65vh; }

/* ---------- schedules ---------- */
.rule { display: flex; gap: 1rem; align-items: center; }
.rule .body { flex: 1; min-width: 0; }
.rule .title { font-weight: 600; }

/* ---------- zones editor ---------- */
.zone-editor { position: relative; background: #000; border-radius: 10px; overflow: hidden; }
.zone-editor img { width: 100%; display: block; }
.zone-editor canvas { position: absolute; inset: 0; width: 100%; height: 100%; cursor: crosshair; touch-action: none; }

/* ---------- auth ---------- */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 1rem; }
.auth .card { width: min(400px, 100%); padding: 1.6rem; }
.auth .logo { width: 56px; height: 56px; border-radius: 14px; margin-bottom: .75rem; }
.error { color: var(--danger); font-size: .9rem; min-height: 1.2em; }

/* ---------- toasts & alarm ---------- */
.toasts { position: fixed; right: 1rem; bottom: 1rem; z-index: 60; display: flex; flex-direction: column; gap: .5rem; max-width: min(380px, calc(100vw - 2rem)); }
@media (max-width: 760px) { .toasts { bottom: 5rem; } }
.toast { display: flex; gap: .7rem; align-items: center; padding: .7rem .9rem; cursor: pointer; animation: slidein .2s ease-out; }
.toast img { width: 64px; height: 40px; object-fit: cover; border-radius: 6px; }
@keyframes slidein { from { transform: translateY(10px); opacity: 0; } }

.alarm {
  position: fixed; inset: 0; z-index: 100; display: grid; place-items: center; padding: 1rem;
  background: rgba(150, 0, 0, .85); color: #fff; animation: flash 1s infinite;
}
@keyframes flash { 50% { background: rgba(90, 0, 0, .9); } }
.alarm .box { text-align: center; max-width: 560px; }
.alarm img { width: 100%; border-radius: 12px; margin: 1rem 0; border: 3px solid #fff; }
.alarm h1 { font-size: 2rem; }
.alarm button { font-size: 1.1rem; padding: .8rem 1.4rem; }

.banner { padding: .7rem 1rem; border-radius: 10px; background: color-mix(in srgb, var(--warn) 16%, transparent); color: var(--text); display: flex; gap: .75rem; align-items: center; flex-wrap: wrap; }
table { width: 100%; border-collapse: collapse; }
td, th { text-align: left; padding: .5rem .4rem; border-bottom: 1px solid var(--border); font-size: .9rem; }
th { color: var(--muted); font-weight: 500; }
code { background: var(--surface-2); padding: .1rem .35rem; border-radius: 6px; font-size: .85em; word-break: break-all; }
.privacy-on { background: color-mix(in srgb, var(--warn) 22%, transparent); }
pre.cmd {
  margin: 0 0 .5rem; padding: .7rem .8rem; border-radius: 10px; background: var(--surface-2); border: 1px solid var(--border);
  font-size: .82rem; white-space: pre-wrap; word-break: break-all; user-select: all;
}
details summary { cursor: pointer; }
details p { margin: .5rem 0 0; }
@media (max-width: 560px) {
  .cam .frame .ptz { grid-template-columns: repeat(3, 28px); grid-template-rows: repeat(3, 28px); left: .4rem; bottom: .4rem; }
  .cam .frame .ptz button { width: 28px; height: 28px; }
  .cam .frame .ptz button svg { width: 15px; height: 15px; }
}
