:root {
  color-scheme: dark;
  --bg: #0b0d11; --bar: rgb(11 13 17 / .86); --fg: #e8eaed; --muted: #8f96a0; --line: #232831;
  --accent: #5aa9ff; --bad: #ff6b6b; --ok: #4cd37a; --card: #161a21; --card-hi: #1e232c;
  --radius: 14px; --header: 52px;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html { background: var(--bg); }
/* No rubber-band bounce or pull-to-refresh: the app does not move under the finger. */
html, body { overscroll-behavior: none; }
body {
  margin: 0; background: var(--bg); color: var(--fg);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased; text-size-adjust: 100%;
  padding: 0 env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* Header: fixed height, the back button keeps its place when hidden so the title never jumps. */
header {
  display: flex; align-items: center; gap: 4px; height: calc(var(--header) + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) 4px 0; position: sticky; top: 0; z-index: 10;
  background: var(--bar); backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--line); user-select: none; -webkit-user-select: none;
}
header #back[hidden] { display: grid !important; visibility: hidden; }
header h1 { flex: 1; font-size: 17px; font-weight: 600; margin: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

button {
  font: inherit; color: var(--fg); background: var(--card); border: 0; border-radius: 10px; padding: 10px 14px;
  touch-action: manipulation; cursor: pointer; transition: background-color .15s, transform .1s, opacity .15s;
}
button:active { transform: scale(.97); background: var(--card-hi); }
button.icon { display: grid; place-items: center; width: 44px; height: 44px; font-size: 24px; line-height: 1; padding: 0; background: none; border-radius: 22px; }
button.icon:active { background: var(--card); }
button.danger { color: var(--bad); }

/* Status: a floating pill under the header, never part of the layout, so messages do not push the
   page around. Success fades away by itself; problems stay until tapped or resolved. */
#status {
  position: fixed; z-index: 20; left: 50%; top: calc(var(--header) + env(safe-area-inset-top) + 8px);
  transform: translate(-50%, 0); max-width: min(92vw, 480px); width: max-content;
  padding: 7px 14px 7px 30px; border-radius: 18px; font-size: 13px; line-height: 1.35;
  color: var(--fg); background: rgb(30 35 44 / .94); border: 1px solid var(--line);
  box-shadow: 0 6px 24px rgb(0 0 0 / .4); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  transition: opacity .25s, transform .25s; pointer-events: auto;
}
#status::before {
  content: ""; position: absolute; left: 12px; top: 50%; width: 8px; height: 8px; margin-top: -4px;
  border-radius: 50%; background: var(--accent); animation: pulse 1.2s ease-in-out infinite;
}
#status.ok::before { background: var(--ok); animation: none; }
#status.bad::before { background: var(--bad); animation: none; }
#status.bad { cursor: pointer; }
#status.gone, #status:empty { opacity: 0; transform: translate(-50%, -8px); pointer-events: none; }
@keyframes pulse { 50% { opacity: .35; } }

main { padding: 12px 16px 32px; }
main > section:not([hidden]) { animation: enter .18s ease-out; }
@keyframes enter { from { opacity: 0; } }
@media (prefers-reduced-motion: reduce) { *, *::before { animation: none !important; transition: none !important; } }

.paste { display: flex; gap: 8px; }
input, select { font: inherit; font-size: 16px; /* under 16 px iOS zooms the page on focus */ }
.paste input { flex: 1; min-width: 0; color: var(--fg); background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; }
.install { display: flex; align-items: center; gap: 8px; margin: 12px 16px 0; padding: 8px 8px 8px 14px; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); font-size: 14px; }
.install span { flex: 1; }
.install #install-btn { color: var(--accent); padding: 6px 12px; background: var(--card-hi); }
.install .icon { width: 36px; height: 36px; font-size: 20px; }
.lead { font-size: 18px; }
.muted { color: var(--muted); font-size: 14px; }
.code { font: 600 44px/1.2 ui-monospace, monospace; letter-spacing: 6px; text-align: center; margin: 16px 0; font-variant-numeric: tabular-nums; }

/* Tabs: a segmented control. */
#tabs { display: flex; padding: 3px; margin-bottom: 14px; background: var(--card); border-radius: 12px; user-select: none; -webkit-user-select: none; }
#tabs .tab { flex: 1; background: none; padding: 8px; border-radius: 9px; color: var(--muted); font-size: 15px; font-weight: 500; }
#tabs .tab:active { transform: none; }
#tabs .tab.active { background: var(--card-hi); color: var(--fg); box-shadow: 0 1px 3px rgb(0 0 0 / .35); }

/* Camera cards */
#camera-list { list-style: none; padding: 0; margin: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
#camera-list li {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; cursor: pointer;
  user-select: none; -webkit-user-select: none; transition: transform .1s; contain: layout paint;
}
#camera-list li:active { transform: scale(.98); }
#camera-list li.off { opacity: .55; }
#camera-list .snap { display: block; width: 100%; aspect-ratio: 16 / 9; object-fit: cover; }
#camera-list .caption { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; padding: 8px 10px; font-size: 14px; font-weight: 500; }
#camera-list .caption span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#camera-list .meta, #event-list .meta { color: var(--muted); font-size: 12px; font-weight: 400; white-space: nowrap; font-variant-numeric: tabular-nums; }

/* Picture placeholder until the first snapshot: a quiet shimmer instead of a black hole that pops. */
.snap, .set-grid .tile img, #event-list .thumb {
  background: linear-gradient(100deg, #12151b 30%, #1a1f27 50%, #12151b 70%) 0 0 / 300% 100%;
  animation: shimmer 1.6s linear infinite;
}
.snap.loaded, .set-grid .tile img.loaded, #event-list .thumb.loaded { animation: none; background: #000; }
img:not(.loaded) { color: transparent; }
@keyframes shimmer { to { background-position: -150% 0; } }

/* A desktop camera set, cell for cell: the grid keeps 16:9 overall like the desktop's wall. */
.set-grid { display: grid; gap: 2px; margin: 0 -16px; background: #000; aspect-ratio: 16 / 9; user-select: none; -webkit-user-select: none; }
.set-grid .tile { position: relative; background: #000; overflow: hidden; cursor: pointer; min-width: 0; min-height: 0; }
.set-grid .tile:active { opacity: .8; }
.set-grid .tile.empty { background: var(--card); opacity: .35; cursor: default; }
.set-grid .tile.off { opacity: .55; }
.set-grid .tile img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.set-grid .tile .name { position: absolute; left: 0; right: 0; bottom: 0; padding: 10px 6px 3px; font-size: 11px; font-weight: 500; background: linear-gradient(transparent, rgb(0 0 0 / .75)); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.filters { display: flex; gap: 8px; margin-bottom: 12px; }
.filters select, .quality-bar select {
  flex: 1; min-width: 0; color: var(--fg); background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px;
}

/* Events */
#event-list { list-style: none; padding: 0; margin: 0; }
#event-list li { display: flex; gap: 12px; align-items: center; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 8px; margin-bottom: 8px; transition: transform .1s; }
#event-list li.clip { cursor: pointer; }
#event-list li.clip:active { transform: scale(.985); }
#event-list .thumb { width: 112px; aspect-ratio: 16 / 9; object-fit: cover; border-radius: 8px; flex: none; }
#event-list .text { min-width: 0; }
#event-list .what { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 500; text-transform: capitalize; }
#events-note { min-height: 1.5em; text-align: center; }
#more { width: 100%; }

/* Live view and clip player: the box has its size before the first frame (the camera's own aspect,
   16:9 until known), so nothing below it moves when the picture arrives. */
.video-wrap { margin: 0 -16px; background: #000; position: relative; aspect-ratio: 16 / 9; max-height: 80vh; max-height: 80svh; overflow: hidden; }
.video-wrap video { position: absolute; inset: 0; width: 100%; height: 100%; display: block; background: #000; object-fit: contain; }
#clip-wrap { aspect-ratio: auto; }
#clip-wrap video { position: static; height: auto; max-height: 80vh; }
/* A replacement live element stays invisible until it has a frame; the old one shows until then. */
.video-wrap video.pending { opacity: 0; }
#overlay, #live-overlay { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 1; }
.video-wrap.loading::after {
  content: ""; position: absolute; left: 50%; top: 50%; width: 34px; height: 34px; margin: -17px 0 0 -17px;
  border: 3px solid rgb(255 255 255 / .18); border-top-color: #fff; border-radius: 50%; animation: spin .8s linear infinite; z-index: 2;
}
@keyframes spin { to { transform: rotate(1turn); } }
.fs-button {
  position: absolute; right: 8px; bottom: 8px; width: 44px; height: 44px; padding: 0; z-index: 3;
  display: grid; place-items: center; border: 0; border-radius: 22px;
  background: rgb(0 0 0 / 0.5); color: #fff; cursor: pointer;
}
.video-wrap:fullscreen { margin: 0; width: 100vw; height: 100vh; max-height: none; aspect-ratio: auto; }
.video-wrap:fullscreen .fs-button { right: 16px; bottom: 16px; }

.quality-bar { display: flex; align-items: center; gap: 10px; margin-top: 12px; min-height: 40px; }
.quality-bar select { flex: 0 1 auto; }
#quality-info { font-variant-numeric: tabular-nums; white-space: nowrap; }
.boxes-toggle { margin-left: auto; display: flex; align-items: center; gap: 6px; font-size: 14px; user-select: none; -webkit-user-select: none; }
.boxes-toggle input { width: 18px; height: 18px; accent-color: var(--accent); }
#live-note { min-height: 1.45em; }
#live-info { font-size: 11px; font-family: ui-monospace, monospace; white-space: pre-wrap; }

/* Settings */
#settings .check { display: flex; align-items: center; gap: 10px; margin: 16px 0; }
#settings .check input { width: 18px; height: 18px; accent-color: var(--accent); }
