:root {
  --bg: #0b0e14;
  --panel: #141924;
  --panel-2: #1c2333;
  --border: #2a3346;
  --text: #e6ebf5;
  --muted: #8a95a8;
  --accent: #3b82f6;
  --accent-hover: #2f6fe0;
  --live: #22c55e;
  --offline: #6b7280;
  --danger: #ef4444;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

.brand { display: flex; align-items: center; gap: 10px; }
.brand h1 { font-size: 1.15rem; margin: 0; font-weight: 650; }

.dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--muted);
}
.dot.live {
  background: var(--live);
  box-shadow: 0 0 0 0 rgba(34, 197, 94, 0.6);
  animation: pulse 2s infinite;
}
@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(34, 197, 94, 0.5); }
  70% { box-shadow: 0 0 0 8px rgba(34, 197, 94, 0); }
  100% { box-shadow: 0 0 0 0 rgba(34, 197, 94, 0); }
}

.login-body {
  display: flex; align-items: center; justify-content: center;
  min-height: 100vh; padding: 20px;
  background: radial-gradient(1200px 600px at 50% -10%, #16203a 0%, var(--bg) 60%);
}
.login-card {
  width: 100%; max-width: 380px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 32px 28px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.45);
}
.subtitle { color: var(--muted); margin: 8px 0 24px; font-size: 0.92rem; }

form { display: flex; flex-direction: column; }
label { font-size: 0.8rem; color: var(--muted); margin-bottom: 6px; margin-top: 14px; }
input {
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 12px 14px;
  border-radius: 10px;
  font-size: 0.95rem;
  outline: none;
}
input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(59,130,246,0.25);
}
button, .btn {
  margin-top: 22px;
  background: var(--accent);
  color: white;
  border: none;
  padding: 12px 16px;
  border-radius: 10px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  text-align: center;
  display: inline-block;
}
button:hover, .btn:hover { background: var(--accent-hover); }

.viewer-page .dock-btn,
.viewer-page .volume-label {
  margin-top: 0 !important;
}

.alert {
  background: rgba(239, 68, 68, 0.12);
  border: 1px solid rgba(239, 68, 68, 0.4);
  color: #fca5a5;
  padding: 10px 14px;
  border-radius: 10px;
  font-size: 0.88rem;
  margin-bottom: 4px;
}

.page-body { display: flex; flex-direction: column; min-height: 100vh; }
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.topbar-right { display: flex; align-items: center; gap: 12px; }
.topbar-right button, .topbar-right .btn {
  margin: 0;
  background: var(--panel-2);
  border: 1px solid var(--border);
  font-size: 0.85rem;
  padding: 7px 14px;
}
.topbar-right button:hover { background: var(--border); }

main { padding: 24px; max-width: 1100px; margin: 0 auto; width: 100%; }

.camera-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 16px;
}
.camera-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.camera-card h2 { margin: 0; font-size: 1rem; }
.camera-meta { color: var(--muted); font-size: 0.85rem; }
.badge {
  display: inline-block;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.4px;
  padding: 3px 8px;
  border-radius: 6px;
}
.badge.online { background: rgba(34,197,94,0.15); color: #86efac; }
.badge.offline { background: rgba(107,114,128,0.2); color: #d1d5db; }
.badge.streaming { background: rgba(59,130,246,0.15); color: #93c5fd; }
.badge.dvr { background: rgba(239,68,68,0.15); color: #fca5a5; }

.badge-row { display: flex; flex-wrap: wrap; gap: 6px; }
.card-actions { display: flex; flex-direction: column; gap: 8px; margin-top: auto; }
.card-actions .btn { margin-top: 0; }

.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  font-size: 0.88rem;
  color: var(--muted);
}
.toggle-row input[type="checkbox"] {
  width: 18px;
  height: 18px;
  accent-color: var(--accent);
  cursor: pointer;
}
.storage-hint { margin-top: -12px; font-size: 0.85rem; }

.btn-secondary {
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
}
.btn-secondary:hover { background: #252d40; }
.btn-inline {
  display: inline-block;
  margin-top: 0;
  padding: 8px 12px;
  font-size: 0.85rem;
  text-decoration: none;
  text-align: center;
}

.recordings-main {
  max-width: 900px;
  margin: 0 auto;
  padding: 0 16px 32px;
  width: 100%;
}
.recordings-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 16px;
  margin-bottom: 16px;
}
.checkbox-label {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 0.9rem;
  color: var(--muted);
  cursor: pointer;
}
.recording-list { display: flex; flex-direction: column; gap: 10px; }
.recording-row {
  display: grid;
  grid-template-columns: auto 112px 1fr auto;
  gap: 12px;
  align-items: center;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
}
.recording-thumb {
  display: block;
  width: 112px;
  height: 63px;
  border-radius: 8px;
  overflow: hidden;
  background: #0a0a0c;
  border: 1px solid var(--border);
  flex-shrink: 0;
}
.recording-thumb-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
.recording-thumb-img.recording-thumb-missing,
.recording-thumb:has(.recording-thumb-missing) {
  background: linear-gradient(135deg, #1a1a22 0%, #121218 100%);
}
.recording-thumb:has(.recording-thumb-missing)::after {
  content: "No preview";
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  font-size: 0.7rem;
  color: var(--muted);
  position: absolute;
  inset: 0;
}
.recording-thumb:has(.recording-thumb-missing) {
  position: relative;
}
.recording-thumb:has(.recording-thumb-missing) .recording-thumb-img {
  display: none;
}
.recording-title { font-size: 0.95rem; font-weight: 600; }
.recording-meta { color: var(--muted); font-size: 0.82rem; margin-top: 4px; }
.recording-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.empty-msg, .muted-text { color: var(--muted); font-size: 0.9rem; }
.is-hidden { display: none !important; }

.playback-wrap video {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #000;
  pointer-events: auto;
  z-index: 2;
}
.playback-meta { text-align: center; margin-top: 12px; }

.render-modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  background: rgba(0, 0, 0, 0.65);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.render-dialog {
  width: 100%;
  max-width: 560px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 20px;
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.5);
}
.render-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 8px;
}
.render-header h2 { margin: 0; font-size: 1.05rem; }
.render-close {
  background: transparent;
  border: none;
  color: var(--muted);
  font-size: 1.5rem;
  line-height: 1;
  cursor: pointer;
  padding: 4px 8px;
  margin: 0;
}
.render-note {
  color: var(--muted);
  font-size: 0.82rem;
  margin: 0 0 14px;
}
.render-progress-wrap {
  height: 8px;
  background: var(--panel-2);
  border-radius: 999px;
  overflow: hidden;
  margin-bottom: 12px;
}
.render-progress-bar {
  height: 100%;
  width: 0;
  background: var(--accent);
  transition: width 0.25s ease;
}
.render-status {
  font-size: 0.9rem;
  color: var(--text);
  margin: 0 0 14px;
  min-height: 2.5em;
}
.render-preview {
  width: 100%;
  max-height: 280px;
  background: #000;
  border-radius: 10px;
  margin-bottom: 14px;
}
.render-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}
.render-actions .btn { margin-top: 0; flex: 1; min-width: 120px; }

.camera-card .btn { margin-top: auto; }
.camera-card .btn.disabled {
  opacity: 0.5;
  pointer-events: none;
}

.page-body { display: flex; flex-direction: column; min-height: 100vh; }
.viewer-page {
  padding-bottom: calc(130px + env(safe-area-inset-bottom, 0));
  overflow-x: hidden;
}

.viewer-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 12px 12px 0;
  max-width: 1100px;
  margin: 0 auto;
  width: 100%;
  min-height: 0;
}

.stage {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
}
.video-wrap {
  position: relative;
  width: 100%;
  max-height: calc(100vh - 180px);
  background: #000;
  border: 1px solid var(--border);
  border-radius: 14px;
  overflow: hidden;
  aspect-ratio: 16 / 9;
}
.video-wrap:not(.playback-wrap) video,
.video-wrap:not(.playback-wrap) img,
.video-wrap .placeholder {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 0;
}
.video-wrap:not(.playback-wrap) video,
.video-wrap:not(.playback-wrap) img { object-fit: contain; display: block; background: #000; }
.playback-wrap video { object-fit: contain; display: block; background: #000; }
.stream-img.is-hidden { visibility: hidden; opacity: 0; }
.placeholder {
  color: var(--muted);
  font-size: 0.95rem;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 16px;
  z-index: 1;
}
.placeholder.is-hidden {
  display: none !important;
  visibility: hidden !important;
}

.status-badge {
  position: absolute; top: 12px; left: 12px;
  z-index: 2;
  pointer-events: none;
  background: rgba(34, 197, 94, 0.9);
  color: #04210f;
  font-weight: 700; font-size: 0.72rem; letter-spacing: 0.5px;
  padding: 4px 10px; border-radius: 6px;
}
.status-badge.waiting { background: rgba(234, 179, 8, 0.9); color: #422006; }
.status-badge.offline { background: rgba(239, 68, 68, 0.9); color: #2a0606; }

.controls { display: flex; gap: 12px; flex-wrap: wrap; justify-content: center; }
.controls button {
  margin: 0;
  background: var(--panel-2);
  border: 1px solid var(--border);
}
.controls button:hover { background: var(--border); }

.playback-bar {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
}

.player-dock {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 100;
  padding: 8px 12px calc(10px + env(safe-area-inset-bottom, 0));
  background: var(--panel);
  border-top: 1px solid var(--border);
  box-shadow: 0 -8px 32px rgba(0, 0, 0, 0.55);
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.player-dock-label {
  width: 100%;
  max-width: 1100px;
  margin: 0 auto;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}
.player-dock-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  width: 100%;
  max-width: 1100px;
  margin: 0 auto;
}
.player-dock-row-tools {
  padding-top: 2px;
  border-top: 1px solid var(--border);
}
.dock-btn {
  margin: 0 !important;
  display: inline-flex !important;
  align-items: center;
  justify-content: center;
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 14px;
  border-radius: 10px;
  font-size: 0.88rem;
  font-weight: 600;
  min-height: 44px;
  cursor: pointer;
  touch-action: manipulation;
  flex-shrink: 0;
}
.dock-btn:hover { background: var(--border); }
.dock-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}
.dock-btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  min-width: 108px;
}
.dock-btn-primary:hover { background: var(--accent-hover); }
.dock-btn-primary.is-live {
  background: rgba(34, 197, 94, 0.25);
  border-color: rgba(34, 197, 94, 0.55);
  color: #86efac;
}
.dock-btn-audio {
  min-width: 118px;
  border-color: var(--border);
}
.dock-btn-audio[aria-pressed="true"] {
  background: rgba(59, 130, 246, 0.2);
  border-color: rgba(59, 130, 246, 0.5);
  color: #93c5fd;
}
.dock-btn-audio.stat-live {
  background: rgba(34, 197, 94, 0.2);
  border-color: rgba(34, 197, 94, 0.5);
  color: #86efac;
}
.dock-btn-audio.stat-waiting {
  color: #fcd34d;
  border-color: rgba(234, 179, 8, 0.45);
}
.dock-btn-audio.stat-error {
  color: #fca5a5;
  border-color: rgba(239, 68, 68, 0.45);
}
.stat-pill {
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--muted);
  background: var(--panel-2);
  border: 1px solid var(--border);
  padding: 8px 12px;
  border-radius: 999px;
  margin-left: auto;
  white-space: nowrap;
}
.stat-pill.stat-live {
  color: #86efac;
  border-color: rgba(34, 197, 94, 0.35);
  background: rgba(34, 197, 94, 0.12);
}
.volume-label {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 0.82rem;
  color: var(--muted);
  margin: 0;
  flex-shrink: 0;
}
.volume-label input[type="range"] {
  width: min(140px, 28vw);
  accent-color: var(--accent);
}
.audio-gate {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.45);
  cursor: pointer;
}
.audio-gate[hidden] { display: none !important; }

.audio-prompt,
#enable-audio {
  margin: 0;
  background: rgba(59, 130, 246, 0.95);
  color: white;
  border: none;
  padding: 12px 22px;
  border-radius: 999px;
  font-weight: 600;
  font-size: 0.95rem;
  cursor: pointer;
  box-shadow: 0 8px 24px rgba(0,0,0,0.35);
  z-index: 11;
  pointer-events: auto;
  touch-action: manipulation;
}
.audio-prompt:hover,
#enable-audio:hover { background: var(--accent-hover); }

#enable-audio[hidden] { display: none !important; }
@keyframes audio-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(59, 130, 246, 0.5); }
  50% { box-shadow: 0 0 0 8px rgba(59, 130, 246, 0); }
}

.back-link {
  color: var(--muted);
  text-decoration: none;
  font-size: 0.9rem;
}
.back-link:hover { color: var(--text); }
