:root {
  --bg: #0d1116;
  --panel: #141a21;
  --panel-2: #1a222b;
  --line: #26303b;
  --text: #e6ebf0;
  --muted: #8b98a6;
  --accent: #f2b84b;
  --bg-flag: #4fb3a9;
  --tr-flag: #e06c5a;
  --ok: #5cc98a;
  --bad: #e06c5a;
  --warn: #f2b84b;
  --radius: 10px;
  color-scheme: dark;
}

* { 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;
}
button, input, select { font: inherit; color: inherit; }
a { color: var(--accent); }
.muted { color: var(--muted); }

/* ---------- header */
.top {
  display: flex; flex-wrap: wrap; align-items: center; gap: 12px 20px;
  padding: 10px 16px; border-bottom: 1px solid var(--line); background: var(--panel);
}
.brand { display: flex; align-items: center; gap: 10px; }
.logo { width: 30px; height: 30px; border-radius: 8px; background: url(/static/icon.svg) center/cover; }
.brand h1 { font-size: 16px; margin: 0; letter-spacing: .2px; }
.brand p { margin: 0; color: var(--muted); font-size: 12px; }
.controls { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 14px; flex: 1; }
.btn {
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px;
  padding: 6px 10px; cursor: pointer;
}
.btn:hover { border-color: #3a4756; }
.btn.ghost { background: transparent; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  border: 1px solid var(--line); background: transparent; border-radius: 999px;
  padding: 4px 10px; cursor: pointer; font-size: 12px;
}
.chip input { position: absolute; opacity: 0; pointer-events: none; }
.chip:has(input:checked) { background: var(--panel-2); border-color: var(--accent); color: var(--accent); }
.chip:has(input:focus-visible) { outline: 2px solid var(--accent); outline-offset: 2px; }
.chip { position: relative; user-select: none; }
.search-top { background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 5px 10px; min-width: 180px; }
.toggle { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); cursor: pointer; }
.toggle.small { font-size: 12px; }
.toggle select { background: var(--panel-2); border: 1px solid var(--line); border-radius: 6px; padding: 2px 4px; }

.health { display: flex; gap: 6px; flex-wrap: wrap; font-size: 12px; }
.pill { display: inline-flex; align-items: center; gap: 6px; padding: 3px 9px; border-radius: 999px; background: var(--panel-2); border: 1px solid var(--line); }
.pill::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.pill.ok::before { background: var(--ok); }
.pill.bad::before { background: var(--bad); }
.pill.warn::before { background: var(--warn); }

/* ---------- layout */
.layout { flex: 1; display: grid; grid-template-columns: minmax(320px, 400px) 1fr; min-height: 0; }
.side { display: flex; flex-direction: column; border-right: 1px solid var(--line); min-height: 0; background: var(--panel); }
.map { height: 38vh; min-height: 220px; border-bottom: 1px solid var(--line); background: #0b0f13; }
.list-head { display: flex; align-items: center; gap: 8px; padding: 8px 10px; border-bottom: 1px solid var(--line); }
.list-head { justify-content: space-between; font-size: 12px; }
.list { list-style: none; margin: 0; padding: 0 0 12px; overflow: auto; flex: 1; }

.seg { position: sticky; top: 0; z-index: 1; display: flex; align-items: center; gap: 8px; padding: 8px 12px 6px; background: var(--panel); border-bottom: 1px solid var(--line); }
.seg .flag { width: 6px; height: 18px; border-radius: 3px; background: var(--muted); }
.seg.BG .flag { background: var(--bg-flag); }
.seg.TR .flag { background: var(--tr-flag); }
.seg.border .flag { background: linear-gradient(var(--bg-flag) 50%, var(--tr-flag) 50%); }
.seg h3 { margin: 0; font-size: 13px; flex: 1; }
.seg .km { font-size: 11px; color: var(--muted); font-variant-numeric: tabular-nums; }
.seg button { border: 1px solid var(--line); background: transparent; border-radius: 6px; padding: 2px 8px; cursor: pointer; font-size: 12px; }
.seg.empty-seg h3 { color: var(--muted); font-weight: 500; }

.cam { display: flex; align-items: center; gap: 8px; padding: 7px 12px 7px 26px; cursor: pointer; border-bottom: 1px solid #1c242d; }
.cam:hover { background: var(--panel-2); }
.cam.active { background: #1f2a35; box-shadow: inset 3px 0 var(--accent); }
.cam .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.cam.ok .dot { background: var(--ok); }
.cam.bad .dot { background: var(--bad); }
.cam .nm { flex: 1; min-width: 0; }
.cam .nm b { display: block; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cam .nm small { color: var(--muted); font-size: 11px; }
.cam .fav { background: none; border: 0; cursor: pointer; color: var(--muted); font-size: 15px; padding: 0 2px; }
.cam .fav.on { color: var(--accent); }
.note { padding: 4px 12px 8px 26px; color: var(--muted); font-size: 12px; }

.links { padding: 8px 12px 12px; border-top: 1px solid var(--line); font-size: 13px; }
.links summary { cursor: pointer; color: var(--muted); }
.links ul { margin: 8px 0 0; padding-left: 18px; }
.links li { margin: 4px 0; }
.links .grp { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .5px; margin-right: 4px; }

/* ---------- stage */
.stage { display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.stage-head { display: flex; align-items: center; justify-content: space-between; padding: 10px 16px; border-bottom: 1px solid var(--line); }
.stage-head h2 { margin: 0; font-size: 14px; }
.stage-actions { display: flex; align-items: center; gap: 10px; }
.grid { flex: 1; overflow: auto; padding: 12px; display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(380px, 1fr)); grid-auto-rows: max-content; align-content: start; }
.grid.one { grid-template-columns: 1fr; }
.empty { margin: auto; text-align: center; padding: 40px; max-width: 460px; }
.empty[hidden] { display: none; }

.tile { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; display: flex; flex-direction: column; }
.tile .media { position: relative; aspect-ratio: 16 / 9; background: #000; }
.tile video, .tile img { width: 100%; height: 100%; object-fit: contain; display: block; background: #000; }
.tile .overlay {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 6px; text-align: center; padding: 16px; background: rgba(0,0,0,.55); font-size: 13px;
}
.tile .overlay.hide { display: none; }
.tile .live { position: absolute; top: 8px; left: 8px; background: #c0392b; color: #fff; font-size: 10px; font-weight: 700; padding: 2px 6px; border-radius: 4px; letter-spacing: .5px; }
.tile .stamp { position: absolute; top: 8px; right: 8px; background: rgba(0,0,0,.6); font-size: 11px; padding: 2px 6px; border-radius: 4px; }
.tile footer { display: flex; align-items: center; gap: 8px; padding: 8px 10px; }
.t-meta { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.t-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: 500; }
.t-sub { font-size: 11px; }
.t-actions { display: flex; gap: 4px; }
.icon { background: var(--panel-2); border: 1px solid var(--line); border-radius: 6px; width: 28px; height: 28px; display: inline-grid; place-items: center; cursor: pointer; text-decoration: none; color: var(--text); }
.icon.fav.on { color: var(--accent); }

/* ---------- map markers */
.mk { width: 12px; height: 12px; border-radius: 50%; border: 2px solid #0d1116; box-shadow: 0 0 0 1px rgba(255,255,255,.35); }
.mk.BG { background: var(--bg-flag); }
.mk.TR { background: var(--tr-flag); }
.mk.bad { background: #555; }
.mk.active { box-shadow: 0 0 0 3px var(--accent); }
.leaflet-container { font: inherit; background: #0b0f13; }
.dark-tiles { filter: invert(1) hue-rotate(180deg) brightness(.8) contrast(.9) saturate(.6); }
.leaflet-popup-content-wrapper, .leaflet-popup-tip { background: var(--panel-2); color: var(--text); }
.stop-label { background: transparent; border: 0; box-shadow: none; color: #cfd8e1; font-size: 11px; font-weight: 600; text-shadow: 0 0 3px #000, 0 0 3px #000; }
.stop-label::before { display: none; }

/* ---------- small screens */
@media (max-width: 860px) {
  /* Phone: one normal scrolling page – players first, then map and list. */
  body { display: block; }
  .top { gap: 8px 12px; }
  .controls { flex: 1 1 100%; }
  .layout { display: flex; flex-direction: column; }
  .stage { order: -1; min-height: auto; }
  .stage-head { padding: 8px 12px; }
  .grid { overflow: visible; padding: 8px; grid-template-columns: 1fr; }
  .empty { padding: 16px; }
  .side { border-right: 0; border-top: 1px solid var(--line); }
  .map { height: 32vh; }
  .list { flex: none; height: 55vh; }
}

/* ---------- player states (no inline styles: strict CSP) */
.tile video.gone { visibility: hidden; }
.tile .overlay.bottom { inset: auto 0 0 0; background: rgba(0,0,0,.7); padding: 6px; }
.tile .stamp.left { left: 8px; right: auto; color: var(--text); text-decoration: none; }
.cam:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.play-seg { border: 1px solid var(--line); background: transparent; border-radius: 6px; padding: 2px 8px; cursor: pointer; font-size: 12px; }
