:root {
  --bg: #0b0f14;
  --bg-elev: #121821;
  --panel: #161d28;
  --line: #243041;
  --text: #e8eef7;
  --muted: #8b9bb0;
  --accent: #3dd6c6;
  --danger: #ff5c7a;
  --warn: #ffb020;
  --online: #3ddc84;
  --font: "DM Sans", system-ui, sans-serif;
  --mono: "JetBrains Mono", ui-monospace, monospace;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body {
  margin: 0;
  min-height: 100%;
  background: radial-gradient(1200px 600px at 10% -10%, #1a2740 0%, transparent 55%),
              radial-gradient(900px 500px at 100% 0%, #13231f 0%, transparent 45%),
              var(--bg);
  color: var(--text);
  font-family: var(--font);
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

.auth-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 2rem;
}
.auth-card {
  width: min(420px, 100%);
  background: linear-gradient(180deg, #182131, #121821);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 2rem;
  box-shadow: 0 30px 80px rgba(0,0,0,.45);
}
.brand {
  font-weight: 700;
  letter-spacing: .04em;
  font-size: 1.6rem;
  margin: 0 0 .35rem;
}
.brand span { color: var(--accent); }
.lede { color: var(--muted); margin: 0 0 1.5rem; }
.auth-card label { display: block; margin: .75rem 0 .35rem; color: var(--muted); font-size: .9rem; }
.auth-card input {
  width: 100%;
  background: #0d131c;
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 10px;
  padding: .75rem .9rem;
  font: inherit;
}
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .4rem;
  border: 0;
  border-radius: 10px;
  padding: .75rem 1rem;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  background: var(--accent);
  color: #06231f;
}
.btn.secondary { background: transparent; color: var(--text); border: 1px solid var(--line); }
.btn.block { width: 100%; margin-top: 1rem; }
.errorlist { color: var(--danger); list-style: none; padding: 0; font-size: .9rem; }

.viewer-shell {
  min-height: 100vh;
  display: grid;
  grid-template-rows: auto 1fr auto;
}
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: .85rem 1.25rem;
  border-bottom: 1px solid var(--line);
  background: rgba(11,15,20,.72);
  backdrop-filter: blur(10px);
  position: sticky;
  top: 0;
  z-index: 20;
}
.topbar nav { display: flex; gap: .85rem; align-items: center; flex-wrap: wrap; }
.topbar a { color: var(--muted); font-weight: 600; }
.topbar a.active, .topbar a:hover { color: var(--text); text-decoration: none; }

.grid {
  display: grid;
  gap: .75rem;
  padding: .75rem;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 300px), 1fr));
  align-content: start;
  align-items: stretch;
  min-height: calc(100vh - 110px);
}
.tile {
  position: relative;
  background: #000;
  border: 1px solid var(--line);
  border-radius: 12px;
  overflow: hidden;
  isolation: auto;
  contain: none;
  min-width: 0;
  min-height: 160px;
  aspect-ratio: 16 / 10;
  cursor: pointer;
  z-index: 1;
}
@supports not (aspect-ratio: 16 / 10) {
  .tile::before {
    content: "";
    display: block;
    padding-top: 62.5%;
  }
}
.tile video,
.tile .placeholder {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  background: #05070a;
  border: 0;
  max-width: 100%;
  max-height: 100%;
}
.tile video {
  z-index: 0;
  pointer-events: none;
}
.tile .placeholder {
  display: grid;
  place-items: center;
  color: var(--muted);
  font-size: .95rem;
  z-index: 1;
  pointer-events: none;
}
.tile .placeholder[hidden] {
  display: none !important;
}
/* Mobile WebKit: contain:paint / overflow+radius on the tile blanks WebRTC
   video frames while the audio track still plays. */
@supports (-webkit-touch-callout: none) {
  .tile {
    overflow: visible;
    border-radius: 12px;
  }
  .tile video {
    object-fit: contain;
    border-radius: 12px;
  }
}
.tile-noise {
  position: absolute;
  left: .55rem;
  right: .55rem;
  bottom: 2.55rem;
  z-index: 2;
  display: flex;
  align-items: center;
  gap: .45rem;
  padding: .28rem .4rem;
  border-radius: 8px;
  background: rgba(5, 8, 12, .72);
  backdrop-filter: blur(4px);
  pointer-events: none;
  font-size: .72rem;
  color: #d7e2f0;
}
.tile-noise[hidden] { display: none !important; }
.tile-noise-label {
  flex: 0 0 auto;
  min-width: 4.6rem;
  font-variant-numeric: tabular-nums;
  letter-spacing: .02em;
}
.tile-noise-bar {
  flex: 1;
  height: 6px;
  border-radius: 999px;
  background: rgba(255,255,255,.14);
  overflow: hidden;
  position: relative;
}
.tile-noise-bar > i {
  display: block;
  height: 100%;
  width: 0%;
  border-radius: inherit;
  background: linear-gradient(90deg, #3dd6c6, #7dffb3);
  transition: width .12s linear, background .2s ease;
}
.tile-noise.hot .tile-noise-bar > i {
  background: linear-gradient(90deg, #ffb020, #ff5d5d);
}
.tile-noise.missing {
  opacity: .72;
}
.tile-detect {
  position: absolute;
  inset: 0;
  z-index: 1;
  pointer-events: none;
}
.tile-detect span {
  position: absolute;
  border: 2px solid #7dffb3;
  border-radius: 4px;
  box-shadow: 0 0 0 1px rgba(0,0,0,.35);
}
.tile-detect span em {
  position: absolute;
  left: 0;
  top: -1.15rem;
  font-style: normal;
  font-size: .68rem;
  background: rgba(0,0,0,.75);
  color: #7dffb3;
  padding: .05rem .3rem;
  border-radius: 3px;
  white-space: nowrap;
}
.tile-detect-badge {
  position: absolute;
  top: .55rem;
  right: .55rem;
  z-index: 2;
  background: rgba(0,0,0,.72);
  color: #7dffb3;
  font-size: .72rem;
  padding: .2rem .45rem;
  border-radius: 999px;
  pointer-events: none;
}
.ctrl-noise {
  display: flex;
  flex-direction: column;
  gap: .25rem;
  margin: .2rem 0 .35rem;
}
.ctrl-noise .tile-noise-bar {
  height: 8px;
}
.ctrl-noise.hot .tile-noise-bar > i {
  background: linear-gradient(90deg, #ffb020, #ff5d5d);
}
.ctrl-noise[hidden] { display: none !important; }
.ai-class-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: .35rem;
  max-height: 260px;
  overflow: auto;
  padding: .5rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: rgba(0,0,0,.18);
}
.ai-class-grid label {
  display: flex;
  align-items: center;
  gap: .4rem;
  margin: 0;
  padding: .25rem .4rem;
  border-radius: 6px;
  font-size: .82rem;
  color: var(--muted);
  cursor: pointer;
}
.ai-class-grid label:hover {
  background: rgba(255,255,255,.05);
  color: var(--text);
}
.ai-class-grid label.on {
  color: var(--text);
  background: rgba(61, 214, 198, .12);
}
.ai-class-grid label.hidden {
  display: none;
}
.tile-meta {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: .5rem;
  padding: .55rem .7rem;
  background: linear-gradient(transparent, rgba(0,0,0,.8));
  font-size: .9rem;
  z-index: 2;
  pointer-events: none;
  overflow: hidden;
}
.tile-meta > span {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.tile-controls {
  position: absolute;
  left: .5rem;
  right: .5rem;
  top: .5rem;
  z-index: 3;
  display: none;
  flex-wrap: wrap;
  gap: .35rem;
  pointer-events: auto;
  max-width: calc(100% - 1rem);
}
.tile:hover .tile-controls,
.tile.fullscreen .tile-controls,
.tile.controls-open .tile-controls {
  display: flex;
}
.tile-controls button,
.tile-controls select {
  border: 1px solid var(--line);
  background: rgba(11,15,20,.88);
  color: var(--text);
  border-radius: 8px;
  padding: .35rem .55rem;
  font: inherit;
  font-size: .78rem;
  cursor: pointer;
}
.tile-controls button.on {
  border-color: var(--accent);
  color: var(--accent);
}
.tile-controls button:disabled {
  opacity: .45;
  cursor: default;
}
.dot {
  width: .55rem;
  height: .55rem;
  border-radius: 50%;
  display: inline-block;
  margin-right: .35rem;
  background: var(--muted);
  flex-shrink: 0;
}
.dot.on {
  background: var(--online);
  box-shadow: 0 0 10px var(--online);
}
.tile.motion {
  outline: 2px solid var(--danger);
  box-shadow: 0 0 0 3px rgba(255,92,122,.25);
}
.tile-sectors {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: none;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(3, 1fr);
  pointer-events: none;
  background: rgba(0, 0, 0, .18);
}
.tile-sectors.show {
  display: grid;
}
.tile-sectors span {
  display: grid;
  place-items: center;
  border: 1px solid rgba(255, 255, 255, .18);
  color: rgba(255, 255, 255, .45);
  font-size: .7rem;
  font-weight: 600;
  background: rgba(0, 0, 0, .12);
}
.tile-sectors span.hot {
  background: rgba(255, 92, 122, .45);
  border-color: #ff5c7a;
  color: #fff;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .25);
  animation: sectorPulse .7s ease-in-out 2;
}
.tile-sectors-label {
  position: absolute;
  left: .55rem;
  top: .55rem;
  z-index: 4;
  max-width: calc(100% - 7rem);
  padding: .25rem .5rem;
  border-radius: 8px;
  background: rgba(20, 8, 12, .82);
  border: 1px solid rgba(255, 92, 122, .55);
  color: #ffd7de;
  font-size: .78rem;
  font-weight: 600;
  pointer-events: none;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
@keyframes sectorPulse {
  from { filter: brightness(1); }
  50% { filter: brightness(1.35); }
  to { filter: brightness(1); }
}
.tile.fullscreen {
  position: fixed;
  inset: 0;
  z-index: 50;
  border-radius: 0;
  aspect-ratio: auto;
  min-height: 100vh;
  width: 100vw;
  height: 100vh;
  max-width: none;
}
.ctrl-drawer {
  position: fixed;
  right: 0;
  top: 0;
  bottom: 0;
  width: min(340px, 92vw);
  background: linear-gradient(180deg, #121821, #0b0f14);
  border-left: 1px solid var(--line);
  z-index: 70;
  padding: 1rem 1.1rem 1.5rem;
  transform: translateX(105%);
  transition: transform .2s ease;
  overflow: auto;
}
.ctrl-drawer.open { transform: translateX(0); }
.ctrl-drawer h3 { margin: 0 0 .35rem; }
.ctrl-drawer .muted { margin: 0 0 1rem; }
.ctrl-drawer .ctrl-row {
  display: flex;
  flex-wrap: wrap;
  gap: .5rem;
  margin-bottom: .75rem;
}
.ctrl-drawer label.ctrl-check {
  display: flex;
  align-items: center;
  gap: .45rem;
  width: 100%;
  padding: .55rem .65rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel);
  cursor: pointer;
}
.ctrl-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.45);
  z-index: 65;
  display: none;
}
.ctrl-backdrop.show { display: block; }

.toast-wrap {
  position: fixed;
  right: 1rem;
  bottom: 1rem;
  display: grid;
  gap: .5rem;
  z-index: 60;
}
.toast {
  background: #2a1218;
  border: 1px solid var(--danger);
  color: #ffd7de;
  padding: .75rem 1rem;
  border-radius: 10px;
  min-width: 240px;
  animation: pop .2s ease;
}
@keyframes pop { from { transform: translateY(8px); opacity: 0; } }

.camera-shell {
  min-height: 100vh;
  display: grid;
  grid-template-rows: auto 1fr auto;
}
.preview-wrap {
  position: relative;
  background: #000;
  min-height: 60vh;
}
.preview-wrap video,
.preview-wrap canvas:not(.ai-overlay) {
  width: 100%;
  height: min(70vh, 720px);
  object-fit: cover;
  background: #000;
  display: block;
}
.preview-wrap .ai-overlay {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 4;
  background: transparent;
}
.talkback-badge {
  position: absolute;
  left: .75rem;
  bottom: .75rem;
  z-index: 5;
  padding: .35rem .65rem;
  border-radius: 999px;
  background: rgba(6, 35, 31, .88);
  border: 1px solid var(--accent);
  color: var(--accent);
  font-size: .8rem;
  font-weight: 600;
  pointer-events: none;
}
.talkback-badge[hidden] { display: none !important; }
#remoteAudio {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}
.preview-wrap video.mirror {
  transform: scaleX(-1);
}
.preview-wrap.dual-mode #localVideo,
.preview-wrap.dual-mode #pipVideo {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}
.preview-wrap.dual-mode #composeCanvas {
  display: block;
}
.facing-pick {
  gap: .5rem;
}
.facing-pick select {
  background: #0d131c;
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 8px;
  padding: .35rem .5rem;
  font: inherit;
}
.local-panel {
  margin-top: 1rem;
  padding-top: 1rem;
  border-top: 1px solid var(--line);
}
.motion-panel {
  margin-top: 1rem;
  padding: 1rem;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #0f1520;
}
.motion-field {
  display: grid;
  gap: .35rem;
  margin-bottom: .9rem;
}
.motion-field input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}
.motion-hint {
  font-size: .8rem;
}
.motion-sector-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: .35rem;
  margin-top: .5rem;
}
.motion-sector-grid button {
  aspect-ratio: 4 / 3;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #1a2333;
  color: var(--muted);
  cursor: pointer;
  font: inherit;
  font-size: .75rem;
  padding: 0;
}
.motion-sector-grid button.on {
  background: rgba(56, 189, 185, .22);
  border-color: var(--accent);
  color: var(--text);
}
.motion-sector-grid button.hot {
  box-shadow: inset 0 0 0 2px #f59e0b;
}
.motion-days {
  display: flex;
  flex-wrap: wrap;
  gap: .35rem;
  width: 100%;
}
.motion-days button {
  min-width: 2.4rem;
  padding: .4rem .45rem;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #1a2333;
  color: var(--muted);
  cursor: pointer;
  font: inherit;
  font-size: .8rem;
}
.motion-days button.on {
  background: rgba(56, 189, 185, .22);
  border-color: var(--accent);
  color: var(--text);
}
.motion-schedule {
  margin-top: .35rem;
  padding: .75rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #0c121b;
}
.ctrl-days {
  margin: .45rem 0 .25rem;
}
.ctrl-motion {
  display: grid;
  gap: .45rem;
  margin: .35rem 0 .85rem;
  padding: .75rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #0f1520;
}
.ctrl-motion input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}
.ctrl-sectors {
  margin-top: .15rem;
}
.motion-overlay {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(3, 1fr);
  pointer-events: none;
  z-index: 3;
}
.motion-overlay span {
  border: 1px solid rgba(56, 189, 185, .35);
  background: rgba(56, 189, 185, .08);
}
.motion-overlay span.off {
  background: rgba(0, 0, 0, .45);
  border-color: rgba(255, 255, 255, .12);
}
.motion-overlay span.hot {
  background: rgba(245, 158, 11, .35);
  border-color: #f59e0b;
}
.local-player {
  width: 100%;
  max-height: 280px;
  margin-top: 1rem;
  background: #000;
  border-radius: 10px;
}
.local-seg-actions {
  display: flex;
  gap: .4rem;
  flex-wrap: wrap;
}
.local-seg-actions .btn {
  padding: .4rem .65rem;
  font-size: .85rem;
}
.panel {
  padding: 1rem 1.25rem 1.5rem;
  border-top: 1px solid var(--line);
  background: var(--bg-elev);
}
.integ-card {
  margin-top: 1rem;
  padding: 1.1rem;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: #0f1520;
  max-width: 720px;
}
.integ-status {
  display: flex;
  align-items: center;
  gap: .55rem;
  margin-bottom: 1rem;
  font-weight: 600;
}
.integ-input {
  width: 100%;
  background: #0d131c;
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 10px;
  padding: .65rem .75rem;
  font: inherit;
}
a.btn.disabled {
  opacity: .45;
  pointer-events: none;
}
.integ-cam-list {
  display: grid;
  gap: .45rem;
  margin: .35rem 0 1rem;
}
.integ-cam-item {
  margin: 0 !important;
}
.settings-page {
  padding: 1.25rem 1.5rem 2rem;
}
.settings-page h2 {
  margin: 0 0 .35rem;
}
.row { display: flex; flex-wrap: wrap; gap: .75rem; align-items: center; }
.mono { font-family: var(--mono); letter-spacing: .08em; }
.status-pill {
  display: inline-flex;
  align-items: center;
  gap: .4rem;
  padding: .35rem .7rem;
  border-radius: 999px;
  border: 1px solid var(--line);
  color: var(--muted);
  font-size: .85rem;
}
.form-inline input, .form-inline select {
  background: #0d131c;
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 10px;
  padding: .65rem .8rem;
  font: inherit;
}
.settings-page { padding: 1.25rem; max-width: 960px; margin: 0 auto; }
.card-list { display: grid; gap: .75rem; }
.item {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 1rem;
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
  align-items: center;
}
.recordings-page { padding: 1.25rem; max-width: 1100px; margin: 0 auto; }
.storage-bar {
  height: 10px;
  border-radius: 999px;
  background: #1a2333;
  border: 1px solid var(--line);
  overflow: hidden;
}
.storage-bar-fill {
  height: 100%;
  width: 0;
  background: var(--accent);
  transition: width .25s ease;
}
.storage-bar-fill.warn { background: #f59e0b; }
.storage-bar-fill.full { background: var(--danger); }
.rec-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 1rem;
}
.rec-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  overflow: hidden;
}
.rec-card video { width: 100%; aspect-ratio: 16/10; background: #000; object-fit: cover; }
.rec-card .body { padding: .85rem 1rem 1rem; }
.muted { color: var(--muted); }
.empty {
  border: 1px dashed var(--line);
  border-radius: 12px;
  padding: 2rem;
  text-align: center;
  color: var(--muted);
}
@media (max-width: 640px) {
  .grid { grid-template-columns: 1fr; }
}
