:root {
  --bg: #0e1013;
  --panel: #171a1f;
  --border: #2a2f37;
  --text: #e6e8eb;
  --muted: #8b929c;
  --accent: #ff6a2b;
  --ok: #34c759;
  --warn: #ffb020;
  --err: #ff453a;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex;
  flex-direction: column;
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  padding: 8px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
h1 { font-size: 16px; margin: 0; color: var(--accent); }
.controls { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
#clock { font-variant-numeric: tabular-nums; color: var(--muted); }

button, select, .map-bar a {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 10px;
  font: inherit;
  cursor: pointer;
  text-decoration: none;
}
button:hover, .map-bar a:hover { border-color: var(--muted); }
label { color: var(--muted); display: flex; gap: 6px; align-items: center; }

.seg { display: flex; }
.seg button { border-radius: 0; margin-left: -1px; }
.seg button:first-child { border-radius: 6px 0 0 6px; }
.seg button:last-child { border-radius: 0 6px 6px 0; }
.seg button.active, .tabs button.active { background: var(--accent); border-color: var(--accent); color: #111; }

main {
  flex: 1;
  min-height: 0;
  display: grid;
  gap: 8px;
  padding: 8px;
}
main[data-view="cams"] { grid-template-columns: 1fr; }
main[data-view="map"] { grid-template-columns: 1fr; }
main[data-view="split"] { grid-template-columns: 3fr 2fr; }
main[data-view="cams"] .map, main[data-view="map"] .cams { display: none; }

/* Cameras */
.cams {
  --cols: 1;
  --gap: 8px;
  display: grid;
  gap: var(--gap);
  min-height: 0;
  overflow: auto;
  align-content: start;
  grid-template-columns: repeat(var(--cols), minmax(0, 1fr));
  /* Set by app.js to 9/16 of a column, so a 2x2 tile is exactly 2x2 cells */
  grid-auto-rows: var(--row, 180px);
}
.tile {
  position: relative;
  background: #000;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  grid-column: span var(--span, 1);
  grid-row: span var(--h, 1);
}
.tile video, .tile img, .tile iframe {
  width: 100%; height: 100%;
  object-fit: contain;
  border: 0;
  display: block;
  background: #000;
}
.tile-bar {
  position: absolute;
  inset: 0 0 auto 0;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  background: linear-gradient(rgba(0,0,0,.75), transparent);
  z-index: 2;
}
.tile-bar .name { flex: 1; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; text-shadow: 0 1px 2px #000; }
.tile-bar button { padding: 0 6px; background: rgba(0,0,0,.5); opacity: 0; transition: opacity .15s; }
.tile:hover .tile-bar button { opacity: 1; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--warn); flex: none; }
.dot.ok { background: var(--ok); }
.dot.err { background: var(--err); }
.tile .msg {
  position: absolute; inset: 0;
  display: grid; place-items: center;
  padding: 16px; text-align: center;
  color: var(--muted);
  pointer-events: none;
}
.tile:fullscreen { border-radius: 0; border: 0; aspect-ratio: auto; }
/* Main stream layer above the preview while it starts (see mountMain in app.js) */
.tile .main-stream { position: absolute; inset: 0; z-index: 1; }
.tile .main-stream video { background: transparent; }
.empty { color: var(--muted); padding: 24px; }

/* Map */
.map {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}
.map-bar { display: flex; gap: 6px; padding: 6px; border-bottom: 1px solid var(--border); align-items: center; }
.tabs { display: flex; gap: 6px; flex: 1; overflow-x: auto; }
.tabs button { white-space: nowrap; }
.map-frame { flex: 1; position: relative; }
.map-frame iframe { position: absolute; inset: 0; width: 100%; height: 100%; border: 0; background: #fff; }
.map-frame .external { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 16px; text-align: center; }
.map-frame .external p { max-width: 380px; margin: 0; }
button.open-map { background: var(--accent); border-color: var(--accent); color: #111; font-weight: 600; padding: 10px 18px; }

@media (max-width: 800px) {
  main[data-view="split"] { grid-template-columns: 1fr; grid-template-rows: auto 60vh; }
  main { overflow: auto; }
  .cams { overflow: visible; }
  .tile-bar button { opacity: 1; }
}
/* Age of a snapshot tile's picture */
.tile .age {
  position: absolute; left: 8px; bottom: 6px;
  padding: 1px 6px; border-radius: 4px;
  background: rgba(0,0,0,.6); color: var(--muted);
  font-size: 12px; pointer-events: none;
}
/* Loading state of a full screen stream (see loadingIndicator in app.js) */
.tile .loading {
  position: absolute; left: 50%; bottom: 24px; transform: translateX(-50%);
  display: flex; align-items: center; gap: 10px;
  padding: 8px 14px; border-radius: 999px;
  background: rgba(0,0,0,.7); color: #fff; font-size: 14px;
  pointer-events: none; white-space: nowrap;
}
.tile .loading[hidden] { display: none; }
.tile .loading .elapsed { color: var(--muted); font-variant-numeric: tabular-nums; }
.tile .loading .spinner {
  width: 14px; height: 14px; flex: none;
  border: 2px solid rgba(255,255,255,.3); border-top-color: #fff; border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
