/* ---------- Design tokens ---------- */
:root {
  --bg: #f4f6f8;
  --panel: #ffffff;
  --ink: #14181c;
  --muted: #5b6672;
  --line: #d5dbe1;
  --accent: #0b62d6;
  --accent-ink: #ffffff;
  --ok: #1a7f37;
  --warn: #b25e09;
  --err: #b00020;
  --chip-bg: rgba(255, 255, 255, .92);
  --chip-ink: #14181c;
  --skeleton-a: #2a2f35;
  --skeleton-b: #3a4149;
  --banner-bg: #fdeaea;
  --banner-ink: #8f0019;
  --radius: 10px;
}
html[data-theme="dark"] {
  --bg: #101418;
  --panel: #1a2026;
  --ink: #e8edf2;
  --muted: #97a3ae;
  --line: #333c45;
  --accent: #4d9bff;
  --accent-ink: #0b1220;
  --ok: #4ade80;
  --warn: #fbbf24;
  --err: #ff6b7a;
  --chip-bg: rgba(16, 20, 24, .85);
  --chip-ink: #e8edf2;
  --skeleton-a: #1e242b;
  --skeleton-b: #2b333c;
  --banner-bg: #3a1418;
  --banner-ink: #ff8a96;
}

/* ---------- Base ---------- */
* { box-sizing: border-box; }
[hidden] { display: none !important; } /* ID selectors below set display; keep the hidden attribute authoritative */
body {
  margin: 0;
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
}
h1, h2 { margin: 0; font-weight: 650; }
button { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.noscript { padding: 24px; text-align: center; color: var(--muted); }

/* ---------- Header ---------- */
#bar {
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top)) calc(14px + env(safe-area-inset-right)) 10px calc(14px + env(safe-area-inset-left));
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 10;
}
#bar h1 { font-size: 17px; letter-spacing: .01em; }
.actions { display: flex; gap: 8px; }
.iconBtn {
  display: grid; place-items: center;
  width: 40px; height: 40px;
  background: none; border: 1px solid var(--line); border-radius: var(--radius);
  color: var(--muted); cursor: pointer;
}
.iconBtn:hover { color: var(--ink); border-color: var(--muted); }

/* ---------- Gateway-offline banner ---------- */
#banner {
  display: flex; align-items: center; justify-content: center; gap: 12px;
  padding: 10px 16px;
  background: var(--banner-bg); color: var(--banner-ink);
  border-bottom: 1px solid var(--line);
  font-size: 14px; font-weight: 600;
}
#banner button {
  padding: 6px 14px; border: 1px solid currentColor; border-radius: 8px;
  background: none; color: inherit; font-weight: 600; cursor: pointer;
}

/* ---------- Grid & tiles ---------- */
#grid { display: grid; gap: 12px; padding: 14px; grid-template-columns: 1fr; }
@media (min-width: 620px) { #grid { grid-template-columns: 1fr 1fr; } }
@media (min-width: 980px) { #grid { grid-template-columns: 1fr 1fr 1fr; } }

.tile {
  position: relative; padding: 0;
  border: 1px solid var(--line); border-radius: 12px;
  overflow: hidden; background: #000;
  aspect-ratio: 16/9; cursor: pointer;
}
.tile:hover { border-color: var(--muted); }
.tile video-rtc, .tile video { width: 100%; height: 100%; object-fit: cover; }
/* hide native controls inside grid tiles */
.tile video-rtc video::-webkit-media-controls { display: none !important; }

/* last-known frame behind the video; visible again when the stream drops */
.tile .poster { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.tile[data-state="offline"] .poster { opacity: .45; filter: grayscale(1); }

/* ---------- Rotation (per-camera, saved in cameras.json) ---------- */
.tile[data-rotate="180"] video-rtc,
.tile[data-rotate="180"] .poster { transform: rotate(180deg); }
/* 90/270 in a fixed 16:9 tile: size the pre-rotation box to the tile's swapped
   dimensions (width = tile height, height = tile width) so object-fit:cover fills it. */
.tile[data-rotate="90"] video-rtc, .tile[data-rotate="90"] .poster,
.tile[data-rotate="270"] video-rtc, .tile[data-rotate="270"] .poster {
  position: absolute; inset: auto; top: 50%; left: 50%;
  width: 56.25%; height: 177.778%;
}
.tile[data-rotate="90"] video-rtc, .tile[data-rotate="90"] .poster { transform: translate(-50%, -50%) rotate(90deg); }
.tile[data-rotate="270"] video-rtc, .tile[data-rotate="270"] .poster { transform: translate(-50%, -50%) rotate(270deg); }

/* Fullscreen: swap against the viewport instead of a 16:9 box. */
dialog.fs[data-rotate="180"] video-rtc { transform: rotate(180deg); }
dialog.fs[data-rotate="90"] video-rtc,
dialog.fs[data-rotate="270"] video-rtc {
  position: absolute; top: 50%; left: 50%; width: 100vh; height: 100vw;
}
dialog.fs[data-rotate="90"] video-rtc { transform: translate(-50%, -50%) rotate(90deg); }
dialog.fs[data-rotate="270"] video-rtc { transform: translate(-50%, -50%) rotate(270deg); }

/* Rotate button in the settings rows */
.renameRow .rotBtn {
  flex: 0 0 auto; display: inline-flex; align-items: center; gap: 5px;
  padding: 9px 10px; border: 1px solid var(--line); border-radius: 8px;
  background: var(--bg); color: var(--muted); font-size: 13px; font-weight: 600; cursor: pointer;
}
.renameRow .rotBtn:hover { color: var(--ink); border-color: var(--muted); }

.tile .label {
  position: absolute; left: 8px; top: 8px; z-index: 2;
  max-width: 60%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  padding: 3px 10px; border-radius: 7px;
  background: var(--chip-bg); color: var(--chip-ink);
  font-size: 13px; font-weight: 650;
}

/* status pill */
.tile .status {
  position: absolute; right: 8px; top: 8px; z-index: 2;
  display: flex; align-items: center; gap: 6px;
  padding: 3px 10px; border-radius: 7px;
  background: var(--chip-bg); color: var(--chip-ink);
  font-size: 12px; font-weight: 600;
}
.tile .status::before {
  content: ""; width: 8px; height: 8px; border-radius: 50%;
  background: var(--muted);
}
.tile[data-state="live"] .status::before { background: var(--ok); }
.tile[data-state="connecting"] .status::before,
.tile[data-state="reconnecting"] .status::before { background: var(--warn); animation: pulse 1.2s ease-in-out infinite; }
.tile[data-state="offline"] .status::before { background: var(--err); }

/* skeleton shimmer behind the video until the first frame */
.tile .skeleton {
  position: absolute; inset: 0; z-index: 1;
  background: linear-gradient(100deg, var(--skeleton-a) 40%, var(--skeleton-b) 50%, var(--skeleton-a) 60%);
  background-size: 200% 100%;
  animation: shimmer 1.4s linear infinite;
}
@keyframes shimmer { from { background-position: 150% 0; } to { background-position: -50% 0; } }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: .35; } }
@media (prefers-reduced-motion: reduce) {
  .tile .skeleton, .tile .status::before { animation: none; }
}

/* ---------- Fullscreen ---------- */
dialog.fs {
  border: none; padding: 0; background: #000;
  width: 100vw; height: 100vh; max-width: 100vw; max-height: 100vh;
}
dialog.fs::backdrop { background: #000; }
dialog.fs video-rtc, dialog.fs video { width: 100%; height: 100%; object-fit: contain; }
.fsName {
  position: fixed; top: calc(14px + env(safe-area-inset-top)); left: calc(14px + env(safe-area-inset-left));
  padding: 4px 12px; border-radius: 8px;
  background: rgba(255, 255, 255, .92); color: #14181c;
  font-size: 14px; font-weight: 650;
}
.fsActions {
  position: fixed; top: calc(14px + env(safe-area-inset-top)); right: calc(14px + env(safe-area-inset-right));
  display: flex; gap: 8px;
}
.fsBtn {
  display: grid; place-items: center;
  width: 42px; height: 42px; border: none; border-radius: 50%;
  background: rgba(255, 255, 255, .92); color: #14181c; cursor: pointer;
}
.fsBtn:hover { background: #fff; }

/* ---------- Login ---------- */
#login { display: grid; place-items: center; padding: 15vh 16px 16px; }
#login form {
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px;
  padding: 28px; width: min(360px, 92vw); display: grid; gap: 14px;
}
#login h2 { font-size: 18px; }
#login input {
  padding: 12px; border: 1px solid var(--line); border-radius: 8px;
  font-size: 16px; background: var(--bg); color: var(--ink);
}
#login input:focus { border-color: var(--accent); outline: none; }
#login button {
  padding: 12px; border: none; border-radius: 8px;
  background: var(--accent); color: var(--accent-ink);
  font-size: 16px; font-weight: 650; cursor: pointer;
}
.err { margin: 0; color: var(--err); font-size: 14px; font-weight: 600; }

/* ---------- Settings dialog ---------- */
#settings {
  border: 1px solid var(--line); border-radius: 14px;
  background: var(--panel); color: var(--ink);
  padding: 0; width: min(420px, 92vw);
}
#settings::backdrop { background: rgba(10, 14, 18, .45); }
#settings form { padding: 22px; }
#settings h2 { font-size: 17px; margin-bottom: 6px; }
#renameRows { max-height: 55vh; overflow-y: auto; }
.renameRow { display: flex; gap: 8px; align-items: center; margin: 10px 0; }
.renameRow .camId {
  flex: 0 0 auto; min-width: 52px;
  color: var(--muted); font-size: 13px; font-weight: 600;
}
.renameRow input {
  flex: 1; min-width: 0; padding: 9px 10px;
  border: 1px solid var(--line); border-radius: 8px;
  background: var(--bg); color: var(--ink); font-size: 15px;
}
.renameRow input:focus { border-color: var(--accent); outline: none; }
.renameRow .saveBtn {
  flex: 0 0 auto; padding: 9px 14px; border: none; border-radius: 8px;
  background: var(--accent); color: var(--accent-ink);
  font-size: 14px; font-weight: 650; cursor: pointer;
}
.renameRow .saveBtn.saved { background: var(--ok); }
.renameRow .saveBtn.failed { background: var(--err); }
#settings menu { display: flex; justify-content: flex-end; margin: 14px 0 0; padding: 0; }
.ghostBtn {
  padding: 10px 18px; border: 1px solid var(--line); border-radius: 8px;
  background: none; color: var(--ink); font-weight: 600; cursor: pointer;
}
.ghostBtn:hover { border-color: var(--muted); }
