:root {
  --bg: #050b14;
  --panel: #08111f;
  --panel-2: #0c1828;
  --line: #17304a;
  --cyan: #22d3ee;
  --cyan-dim: #0e7490;
  --text: #dbeafe;
  --muted: #7d95b4;
  --green: #4ade80;
  --amber: #fbbf24;
  --red: #f87171;
  --radius: 10px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
}

#app { display: flex; flex-direction: column; height: 100%; }

/* ---------------- top bar ---------------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 10px 14px;
  background: linear-gradient(180deg, #071527, #050d1a);
  border-bottom: 1px solid var(--line);
  z-index: 30;
  flex: 0 0 auto;
}

.brand { display: flex; align-items: center; gap: 9px; flex: 0 0 auto; }

.brand .dot {
  width: 9px; height: 9px; border-radius: 50%;
  background: var(--cyan);
  box-shadow: 0 0 10px var(--cyan);
  animation: pulse 2.4s ease-in-out infinite;
}

@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: .35; } }

.brand h1 {
  margin: 0; font-size: 15px; font-weight: 700; letter-spacing: .14em;
  text-transform: uppercase;
}
.brand h1 span { color: var(--cyan); margin-left: 4px; }

.tabs { display: flex; gap: 4px; flex: 0 0 auto; }

.tab {
  padding: 6px 14px;
  border-radius: 8px;
  border: 1px solid transparent;
  color: var(--muted);
  text-decoration: none;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: .06em;
  text-transform: uppercase;
  cursor: pointer;
}
.tab:hover { color: var(--text); background: rgba(34, 211, 238, .06); }
.tab.active {
  color: var(--cyan);
  border-color: rgba(34, 211, 238, .4);
  background: rgba(34, 211, 238, .1);
}

.search { flex: 1 1 auto; max-width: 420px; }

.search input {
  width: 100%;
  padding: 8px 12px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 13px;
  outline: none;
}
.search input:focus { border-color: var(--cyan-dim); }

.stats {
  margin-left: auto;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 11px;
  color: var(--muted);
  white-space: nowrap;
}

.panel-toggle {
  display: none;
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: var(--cyan);
  border-radius: 8px;
  padding: 6px 10px;
  cursor: pointer;
}

/* ---------------- filter bar ---------------- */
.filterbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 7px 14px;
  background: #060f1c;
  border-bottom: 1px solid var(--line);
  z-index: 25;
  flex: 0 0 auto;
}

.filterbar select {
  padding: 5px 9px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--text);
  font-size: 12px;
  outline: none;
  max-width: 220px;
}

.switch { display: flex; align-items: center; gap: 6px; font-size: 11px; color: var(--muted); cursor: pointer; }
.switch input { accent-color: var(--cyan); width: 13px; height: 13px; cursor: pointer; }
.offline-count { font-size: 10px; color: var(--muted); font-family: ui-monospace, Menlo, monospace; }

/* ---------------- layout ---------------- */
main { position: relative; flex: 1 1 auto; min-height: 0; }

.view { position: absolute; inset: 0; }
.view[hidden] { display: none; }

#map { position: absolute; inset: 0; background: #071120; }

.maplibregl-ctrl-attrib { display: none; }

/* ---------------- camera list ---------------- */
.panel {
  position: absolute;
  top: 12px; left: 12px; bottom: 12px;
  width: 330px;
  display: flex;
  flex-direction: column;
  background: rgba(8, 17, 31, .93);
  backdrop-filter: blur(10px);
  border: 1px solid var(--line);
  border-radius: 14px;
  overflow: hidden;
  z-index: 10;
}

.panel-hint {
  padding: 8px 12px;
  font-size: 11px;
  color: var(--muted);
  border-bottom: 1px solid var(--line);
  background: rgba(12, 24, 40, .6);
}

.panel-list { flex: 1 1 auto; overflow-y: auto; overscroll-behavior: contain; }

.cam {
  padding: 9px 12px;
  border-bottom: 1px solid rgba(23, 48, 74, .55);
  cursor: pointer;
  transition: background .12s;
  outline: none;
}
.cam:hover { background: rgba(34, 211, 238, .07); }
.cam:focus-visible { background: rgba(34, 211, 238, .12); }
.cam.active { background: rgba(34, 211, 238, .13); border-left: 2px solid var(--cyan); }
.cam.dragging { opacity: .45; }

.cam .t { font-size: 12.5px; font-weight: 600; color: var(--text); }
.cam .s { font-size: 11px; color: var(--muted); margin-top: 2px; }
.cam .b { display: flex; gap: 6px; margin-top: 5px; align-items: center; }

.tag {
  font-family: ui-monospace, Menlo, monospace;
  font-size: 9.5px;
  letter-spacing: .08em;
  text-transform: uppercase;
  padding: 2px 6px;
  border-radius: 5px;
  border: 1px solid transparent;
}
.tag.hls { color: var(--cyan); border-color: rgba(34, 211, 238, .35); background: rgba(34, 211, 238, .08); }
.tag.whep { color: var(--amber); border-color: rgba(251, 191, 36, .35); background: rgba(251, 191, 36, .08); }
.tag.mp4 { color: #a78bfa; border-color: rgba(167, 139, 250, .35); background: rgba(167, 139, 250, .08); }
.tag.youtube, .tag.embed { color: var(--green); border-color: rgba(74, 222, 128, .35); background: rgba(74, 222, 128, .08); }
.tag.proxied { color: var(--red); border-color: rgba(248, 113, 113, .35); background: rgba(248, 113, 113, .08); }

.empty { padding: 24px 14px; text-align: center; color: var(--muted); font-size: 12px; }

/* ---------------- player ---------------- */
.player {
  position: relative;
  width: 100%;
  height: 100%;
  background: #000;
  overflow: hidden;
}

.player video,
.player iframe {
  width: 100%;
  height: 100%;
  display: block;
  border: 0;
  background: #000;
}
.player iframe[hidden], .player video[hidden] { display: none; }

.player-msg {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 8px;
  padding: 16px;
  text-align: center;
  font-size: 12px;
  color: var(--muted);
  background: rgba(0, 0, 0, .78);
}
.player-msg.show { display: flex; }

.spinner {
  width: 22px; height: 22px; border-radius: 50%;
  border: 2px solid rgba(34, 211, 238, .25);
  border-top-color: var(--cyan);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------------- viewer (map) ---------------- */
.viewer {
  position: absolute;
  top: 12px; right: 12px;
  width: 440px;
  max-width: calc(100% - 24px);
  background: rgba(8, 17, 31, .95);
  backdrop-filter: blur(10px);
  border: 1px solid var(--line);
  border-radius: 14px;
  overflow: hidden;
  z-index: 12;
  display: none;
}
.viewer.open { display: block; }

.viewer-head {
  display: flex; align-items: flex-start; gap: 10px;
  padding: 11px 12px;
  border-bottom: 1px solid var(--line);
}
.viewer-head h2 { margin: 0; font-size: 13px; font-weight: 650; }
.viewer-head p { margin: 3px 0 0; font-size: 11px; color: var(--muted); }
.viewer-head p a { color: var(--cyan-dim); }
.viewer-head button {
  margin-left: auto; background: none; border: none; color: var(--muted);
  font-size: 16px; cursor: pointer; line-height: 1;
}
.viewer-head button:hover { color: var(--red); }

.viewer-body { position: relative; background: #000; aspect-ratio: 16 / 9; }
.player-host { position: absolute; inset: 0; }

/* ---------------- nvr ---------------- */
.nvr-main {
  position: absolute;
  top: 0; right: 0; bottom: 0; left: 342px;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.nvr-toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--line);
  background: rgba(6, 15, 28, .8);
  flex: 0 0 auto;
}

.presets { display: flex; gap: 3px; }

.preset {
  min-width: 34px;
  padding: 5px 9px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 7px;
  color: var(--muted);
  font-family: ui-monospace, Menlo, monospace;
  font-size: 12px;
  cursor: pointer;
}
.preset:hover { color: var(--text); border-color: var(--cyan-dim); }
.preset.active {
  color: var(--cyan);
  border-color: rgba(34, 211, 238, .5);
  background: rgba(34, 211, 238, .12);
}

.nvr-actions { margin-left: auto; display: flex; align-items: center; gap: 8px; }

.nvr-status {
  font-family: ui-monospace, Menlo, monospace;
  font-size: 11px;
  color: var(--muted);
}

.ghost-btn {
  padding: 5px 10px;
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 7px;
  color: var(--muted);
  font-size: 11px;
  cursor: pointer;
}
.ghost-btn:hover { color: var(--cyan); border-color: var(--cyan-dim); }

.nvr-grid {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  gap: 2px;
  padding: 2px;
  background: #03070e;
  grid-auto-rows: 1fr;
}
.nvr-grid[data-preset="1"]  { grid-template-columns: 1fr; }
.nvr-grid[data-preset="4"]  { grid-template-columns: repeat(2, 1fr); }
.nvr-grid[data-preset="6"]  { grid-template-columns: repeat(3, 1fr); }
.nvr-grid[data-preset="9"]  { grid-template-columns: repeat(3, 1fr); }
.nvr-grid[data-preset="16"] { grid-template-columns: repeat(4, 1fr); }

.nvr-slot {
  position: relative;
  background: #000;
  overflow: hidden;
  border: 1px solid transparent;
  min-width: 0;
  min-height: 0;
  cursor: pointer;
}
.nvr-slot.drop-target { border-color: var(--cyan); box-shadow: inset 0 0 0 1px var(--cyan); }

.nvr-slot-body { position: absolute; inset: 0; }

.nvr-slot-overlay {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  background: linear-gradient(0deg, rgba(3, 8, 16, .92), rgba(3, 8, 16, 0));
  font-size: 11px;
  pointer-events: none;
}
.nvr-slot-title {
  font-weight: 600;
  color: #e6f2ff;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1 1 auto;
}
.nvr-slot-meta {
  display: flex;
  gap: 6px;
  color: var(--muted);
  font-size: 10px;
  white-space: nowrap;
  flex: 0 0 auto;
}
.nvr-slot-meta a { color: var(--cyan-dim); pointer-events: auto; }

.nvr-slot-actions { display: flex; gap: 3px; pointer-events: auto; flex: 0 0 auto; }

.slot-btn {
  width: 22px; height: 22px;
  display: grid; place-items: center;
  background: rgba(8, 17, 31, .85);
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1;
  cursor: pointer;
}
.slot-btn:hover { color: var(--cyan); border-color: var(--cyan-dim); }

.nvr-slot-empty {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  background: repeating-linear-gradient(
    45deg, rgba(34, 211, 238, .03) 0 8px,
    transparent 8px 16px
  );
  border: 1px dashed rgba(34, 211, 238, .22);
  color: var(--muted);
  font-size: 11px;
  cursor: pointer;
}
.nvr-slot-empty:hover { border-color: var(--cyan); color: var(--cyan); }
.nvr-slot-empty .plus { font-size: 20px; line-height: 1; }

/* ---------------- modal ---------------- */
.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(2, 6, 14, .72);
  backdrop-filter: blur(3px);
  padding: 20px;
}
.modal[hidden] { display: none; }

.modal-card {
  width: min(560px, 100%);
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  overflow: hidden;
}

.modal-head {
  display: flex;
  gap: 8px;
  padding: 10px;
  border-bottom: 1px solid var(--line);
}
.modal-head input {
  flex: 1 1 auto;
  padding: 8px 12px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 9px;
  color: var(--text);
  font-size: 13px;
  outline: none;
}
.modal-head input:focus { border-color: var(--cyan-dim); }
.modal-head button {
  background: none; border: none; color: var(--muted);
  font-size: 16px; cursor: pointer; line-height: 1;
}
.modal-head button:hover { color: var(--red); }

.modal-list { overflow-y: auto; overscroll-behavior: contain; }

.picker-row {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 9px 12px;
  background: none;
  border: none;
  border-bottom: 1px solid rgba(23, 48, 74, .5);
  color: var(--text);
  text-align: left;
  cursor: pointer;
}
.picker-row:hover { background: rgba(34, 211, 238, .08); }
.picker-name { flex: 1 1 auto; font-size: 12.5px; font-weight: 600; }
.picker-sub { flex: 0 1 auto; font-size: 10.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 40%; }

/* ---------------- responsive ---------------- */
@media (max-width: 1100px) {
  .nvr-main { left: 342px; }
}

@media (max-width: 900px) {
  .panel {
    top: auto; left: 8px; right: 8px; bottom: 8px;
    width: auto; max-height: 42vh;
    transform: translateY(calc(100% - 46px));
    transition: transform .22s ease;
  }
  .panel.open { transform: translateY(0); }
  .panel-toggle { display: block; }
  .viewer { width: auto; left: 8px; right: 8px; top: 8px; }
  .stats { display: none; }
  .nvr-main { inset: 0; }
  .nvr-grid { padding-bottom: 50vh; }
  .tabs { order: 3; }
}
