:root {
  color-scheme: dark;
  --bg: #0a0c0f;
  --surface: #12151a;
  --surface-2: #191d24;
  --border: #252a33;
  --border-strong: #353c48;
  --text: #e8eaed;
  --muted: #8b93a1;
  --accent: #5b8cff;
  --accent-strong: #4a7af0;
  --live: #e5484d;
  --warn: #f5a524;
  --ok: #30c463;
  --radius: 12px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --gutter: 16px;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; }

.icon {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex: none;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  height: 36px;
  padding: 0 14px;
  border-radius: 8px;
  border: 1px solid transparent;
  cursor: pointer;
  font-weight: 500;
}
.btn:disabled { opacity: 0.6; cursor: default; }
a.btn { text-decoration: none; }
.btn .icon { width: 16px; height: 16px; }
.btn-ghost { background: transparent; border-color: var(--border); color: var(--muted); }
.btn-ghost:hover { color: var(--text); border-color: var(--border-strong); }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--accent-strong); }

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  border: 1px solid transparent;
  background: rgba(255, 255, 255, 0.06);
  cursor: pointer;
}
.icon-btn:hover { background: rgba(255, 255, 255, 0.12); }
.icon-btn:focus-visible, .btn:focus-visible, .tile:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ------------------------------------------------------------ top bar */

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 16px;
  min-height: 56px;
  padding: max(8px, env(safe-area-inset-top)) max(var(--gutter), env(safe-area-inset-right)) 8px max(var(--gutter), env(safe-area-inset-left));
  background: rgba(10, 12, 15, 0.85);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 600; font-size: 15px; white-space: nowrap; }
.brand-mark { color: var(--accent); }
.summary { display: flex; align-items: center; gap: 8px; color: var(--muted); white-space: nowrap; }
.summary-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.summary[data-level="ok"] .summary-dot { background: var(--ok); }
.summary[data-level="warn"] .summary-dot { background: var(--warn); }
.summary[data-level="bad"] .summary-dot { background: var(--live); }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 14px; }
.clock { font-variant-numeric: tabular-nums; color: var(--muted); }
.user { color: var(--muted); }

.banner {
  margin: 12px var(--gutter) 0;
  padding: 10px 14px;
  border-radius: 8px;
  background: rgba(245, 165, 36, 0.12);
  border: 1px solid rgba(245, 165, 36, 0.35);
  color: #f7c46c;
}

/* ------------------------------------------------------------ camera grid */

.grid {
  display: grid;
  gap: 12px;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 440px), 1fr));
  max-width: 1920px;
  margin: 0 auto;
  padding: 12px max(var(--gutter), env(safe-area-inset-right)) max(24px, env(safe-area-inset-bottom)) max(var(--gutter), env(safe-area-inset-left));
}
.grid[data-count="1"] { max-width: 1280px; }

.tile {
  position: relative;
  aspect-ratio: 16 / 9;
  background: #000;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  overflow: hidden;
  cursor: pointer;
  isolation: isolate;
}
.tile:hover { border-color: var(--border-strong); }
.tile video, .focus-stage video {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #000;
  transition: opacity 0.3s, filter 0.3s;
}
.tile[data-state="offline"] video, .tile[data-state="error"] video,
.focus-stage[data-state="offline"] video, .focus-stage[data-state="error"] video { opacity: 0.3; filter: grayscale(1); }
.tile[data-state="reconnecting"] video, .focus-stage[data-state="reconnecting"] video { opacity: 0.55; }

.tile-top, .tile-bottom {
  position: absolute;
  left: 0;
  right: 0;
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 12px;
  pointer-events: none;
}
.tile-top { top: 0; align-items: flex-start; background: linear-gradient(rgba(0, 0, 0, 0.5), transparent); }
.tile-bottom { bottom: 0; align-items: flex-end; background: linear-gradient(transparent, rgba(0, 0, 0, 0.75)); padding-top: 28px; }
.tile-name { margin: 0; font-size: 15px; font-weight: 600; }
.tile-meta { margin: 0; font-size: 12px; color: rgba(255, 255, 255, 0.65); }
.tile-res, .focus-res { font-size: 11px; color: rgba(255, 255, 255, 0.6); font-variant-numeric: tabular-nums; }
.tile-hint {
  font-size: 12px;
  padding: 3px 8px;
  border-radius: 6px;
  background: rgba(0, 0, 0, 0.5);
  opacity: 0;
  transition: opacity 0.2s;
}
@media (hover: hover) {
  .tile:hover .tile-hint { opacity: 1; }
}

.stage-msg {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  max-width: min(440px, 86%);
  padding: 10px 14px;
  border-radius: 8px;
  background: rgba(10, 12, 15, 0.82);
  border: 1px solid var(--border);
  text-align: center;
  font-size: 13px;
  color: var(--text);
  pointer-events: none;
}

/* ------------------------------------------------------------ status pill */

.pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 22px;
  padding: 0 8px;
  border-radius: 6px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  background: rgba(0, 0, 0, 0.55);
  color: #d5d9e0;
  white-space: nowrap;
}
.pill::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.pill-live { background: var(--live); color: #fff; }
.pill-live::before { background: #fff; animation: pulse 1.6s ease-in-out infinite; }
.pill-connecting, .pill-reconnecting, .pill-buffering { color: var(--warn); }
.pill-connecting::before, .pill-reconnecting::before { animation: pulse 1s ease-in-out infinite; }
.pill-error { color: #ff8a8d; }
.pill-offline, .pill-paused { color: var(--muted); }
@keyframes pulse { 50% { opacity: 0.25; } }
@media (prefers-reduced-motion: reduce) {
  .pill::before { animation: none !important; }
}

/* ------------------------------------------------------------ focus view */

.focus {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  grid-template-rows: auto 1fr;
  background: #000;
}
body.focus-open { overflow: hidden; }
.focus-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: max(8px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) 8px max(12px, env(safe-area-inset-left));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.focus-title { min-width: 0; flex: 1; }
.focus-heading { display: flex; align-items: center; gap: 10px; }
.focus-heading h2 { margin: 0; font-size: 16px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.focus-meta { margin: 0; font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.focus-actions { display: flex; gap: 6px; }
#focus-mute .icon-sound { display: none; }
#focus-mute.is-on .icon-sound { display: block; }
#focus-mute.is-on .icon-muted { display: none; }
#focus-mute.is-on, #focus-controls.is-on { background: rgba(91, 140, 255, 0.22); color: #c9d8ff; }
.focus-stage { position: relative; min-height: 0; }
.focus-res { position: absolute; right: 12px; bottom: max(10px, env(safe-area-inset-bottom)); }

/* ------------------------------------------------------------ toast */

.toast {
  position: fixed;
  left: 50%;
  bottom: max(24px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 100;
  padding: 10px 16px;
  border-radius: 10px;
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
}

/* ------------------------------------------------------------ login */

.login-page {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: var(--gutter);
  background: radial-gradient(1200px 600px at 50% -10%, #16202f 0%, var(--bg) 60%);
}
.login-card {
  width: min(380px, 100%);
  padding: 32px 28px 28px;
  border-radius: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.45);
}
.login-mark { width: 32px; height: 32px; color: var(--accent); }
.login-card h1 { margin: 14px 0 4px; font-size: 22px; }
.login-sub { margin: 0 0 22px; color: var(--muted); }
.login-card form { display: grid; gap: 6px; }
.login-card label { font-size: 13px; color: var(--muted); margin-top: 8px; }
.login-card input {
  height: 42px;
  padding: 0 12px;
  border-radius: 8px;
  border: 1px solid var(--border-strong);
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: 16px; /* avoids iOS zoom-on-focus */
}
.login-card input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(91, 140, 255, 0.25); }
.login-card .btn-primary { height: 42px; margin-top: 16px; }
.login-error { margin: 8px 0 0; color: #ff8a8d; font-size: 13px; }

.btn-block { width: 100%; height: 42px; }
.offline-status { margin: 14px 0 0; font-size: 13px; }

/* ------------------------------------------------------------ pan / tilt */

.ptz {
  --ptz-size: 32px;
  display: grid;
  grid-template-columns: repeat(3, var(--ptz-size));
  grid-template-rows: repeat(3, var(--ptz-size));
  gap: 3px;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}
.ptz-lg { --ptz-size: 52px; gap: 4px; }
.ptz-btn {
  display: grid;
  place-items: center;
  padding: 0;
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.22);
  background: rgba(10, 12, 15, 0.6);
  color: #fff;
  cursor: pointer;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
}
.ptz-btn:hover { background: rgba(10, 12, 15, 0.8); border-color: rgba(255, 255, 255, 0.4); }
.ptz-btn.is-active { background: var(--accent); border-color: var(--accent); }
.ptz-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.ptz-btn .icon { width: 55%; height: 55%; stroke-width: 2.4; }
.ptz-up { grid-column: 2; grid-row: 1; }
.ptz-left { grid-column: 1; grid-row: 2; }
.ptz-right { grid-column: 3; grid-row: 2; }
.ptz-down { grid-column: 2; grid-row: 3; }

/* On grid tiles: right edge, shown on hover (always visible on touch screens). */
.tile > .ptz { position: absolute; right: 10px; top: 50%; transform: translateY(-50%); z-index: 2; transition: opacity 0.2s; }
@media (hover: hover) {
  .tile > .ptz { opacity: 0; }
  .tile:hover > .ptz, .tile > .ptz:focus-within { opacity: 1; }
}

/* In the full-screen view: bottom right, above the resolution label. */
.ptz-panel {
  position: absolute;
  right: max(16px, env(safe-area-inset-right));
  bottom: max(40px, calc(env(safe-area-inset-bottom) + 28px));
  z-index: 3;
  display: grid;
  gap: 10px;
  justify-items: center;
}
.ptz-speed {
  display: flex;
  overflow: hidden;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.22);
  background: rgba(10, 12, 15, 0.6);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
}
.ptz-speed button { border: 0; background: transparent; color: rgba(255, 255, 255, 0.7); padding: 5px 10px; font-size: 12px; cursor: pointer; }
.ptz-speed button[aria-pressed="true"] { background: rgba(91, 140, 255, 0.35); color: #fff; }

/* Clip recording button: turns red and shows the elapsed time while recording. */
#focus-rec { grid-auto-flow: column; gap: 6px; }
#focus-rec.is-rec { width: auto; padding: 0 12px; background: var(--live); color: #fff; }
#focus-rec.is-rec .icon circle:first-child { animation: pulse 1.2s ease-in-out infinite; }
.rec-time { font-size: 12px; font-weight: 600; font-variant-numeric: tabular-nums; }
#focus-pip.is-on { background: rgba(91, 140, 255, 0.22); color: #c9d8ff; }
@media (prefers-reduced-motion: reduce) {
  #focus-rec.is-rec .icon circle:first-child { animation: none; }
}

/* Saved positions: a scrolling row of buttons at the bottom left, beside the direction pad. */
.presets {
  position: absolute;
  left: max(16px, env(safe-area-inset-left));
  right: calc(max(16px, env(safe-area-inset-right)) + 190px);
  bottom: max(40px, calc(env(safe-area-inset-bottom) + 28px));
  z-index: 3;
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 4px;
  scrollbar-width: thin;
  -webkit-overflow-scrolling: touch;
}
.preset-chip {
  flex: none;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  height: 38px;
  max-width: 220px;
  padding: 0 14px 0 8px;
  border-radius: 19px;
  border: 1px solid rgba(255, 255, 255, 0.22);
  background: rgba(10, 12, 15, 0.6);
  color: #fff;
  font-size: 13px;
  cursor: pointer;
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
}
.preset-chip:hover { background: rgba(10, 12, 15, 0.8); border-color: rgba(255, 255, 255, 0.4); }
.preset-chip:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.preset-chip.is-going { background: var(--accent); border-color: var(--accent); }
.preset-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.preset-key {
  display: inline-grid;
  place-items: center;
  min-width: 22px;
  height: 22px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.14);
  font-size: 11px;
  font-variant-numeric: tabular-nums;
}
.preset-chip:not(:has(.preset-key)) { padding-left: 14px; }

/* ------------------------------------------------------------ desktop: all cameras fit the window */
/* Switched on by app.js (class "fit"), which sets the column sizes and each tile's span. */

.fit-layout {
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.fit-layout .topbar, .fit-layout .banner { flex: none; }
.grid.fit {
  flex: 1;
  min-height: 0;
  width: 100%;
  max-width: none;
  margin: 0;
  padding: 12px 16px 16px;
  justify-content: center;
  align-content: center;
  align-items: start;
  overflow: hidden;
}
.grid.fit .tile { min-width: 0; }

/* ------------------------------------------------------------ small screens */

@media (max-width: 640px) {
  .topbar { gap: 10px; }
  .user, .clock { display: none; }
  .grid { gap: 10px; padding-top: 10px; }
  .focus-meta { display: none; }
  .focus-actions { gap: 4px; }
  .icon-btn { width: 38px; height: 38px; }
  .ptz-lg { --ptz-size: 46px; }
  /* With more buttons in the full-screen bar, give them their own evenly spaced row. */
  .focus-bar { flex-wrap: wrap; row-gap: 6px; }
  .focus-title { flex: 1 1 0; }
  .focus-actions { order: 3; flex: 1 0 100%; justify-content: space-between; }
}
@media (max-width: 380px) {
  .summary span:last-child { display: none; }
}
