:root {
  --bg: #0e1116;
  --panel: #161b22;
  --panel-2: #1d232c;
  --border: #2a313c;
  --text: #e6e9ee;
  --muted: #8b94a3;
  --accent: #f59e0b;
  --accent-ink: #1a1203;
  --ok: #22c55e;
  --bad: #ef4444;
  --rec: #ef4444;
  --radius: 8px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); font-size: 14px; }
a { color: var(--accent); text-decoration: none; }
button, input, select { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* ---------- top bar ---------- */
#topbar {
  display: flex; align-items: center; gap: 16px; height: 52px; padding: 0 16px;
  background: var(--panel); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 10;
}
.brand { font-weight: 700; font-size: 16px; color: var(--text); display: flex; align-items: center; gap: 8px; }
.brand-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--accent); box-shadow: 0 0 8px var(--accent); }
#nav { display: flex; gap: 4px; }
#nav a { color: var(--muted); padding: 6px 10px; border-radius: 6px; }
#nav a.active, #nav a:hover { color: var(--text); background: var(--panel-2); }
.spacer { flex: 1; }
#user-menu { display: flex; align-items: center; gap: 10px; color: var(--muted); }

main { padding: 16px; max-width: 1600px; margin: 0 auto; }

/* ---------- controls ---------- */
.btn {
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 6px;
  padding: 7px 12px; cursor: pointer; white-space: nowrap;
}
.btn:hover { border-color: #3b4452; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
.btn.danger { color: #fca5a5; }
.btn.small { padding: 4px 8px; font-size: 12px; }
.btn:disabled { opacity: .5; cursor: default; }
input[type=text], input[type=email], input[type=password], input[type=number], input[type=date], select {
  background: var(--bg); border: 1px solid var(--border); border-radius: 6px; padding: 7px 10px; min-width: 0;
}
input:focus, select:focus, .btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
label.field { display: flex; flex-direction: column; gap: 4px; color: var(--muted); font-size: 12px; }
label.field input, label.field select { color: var(--text); font-size: 14px; }
label.check { display: flex; align-items: center; gap: 8px; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.muted { color: var(--muted); }
.error { color: #fca5a5; }
h2 { margin: 0 0 12px; font-size: 18px; }
h3 { margin: 0 0 10px; font-size: 14px; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }

.card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; }

/* ---------- login ---------- */
.login-wrap { display: grid; place-items: center; min-height: 80vh; }
.login { width: 340px; display: flex; flex-direction: column; gap: 12px; }
.login .brand { justify-content: center; font-size: 22px; margin-bottom: 8px; }

/* ---------- live grid ---------- */
.toolbar { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; flex-wrap: wrap; }
.camgrid { display: grid; gap: 8px; }
.tile {
  position: relative; background: #000; border-radius: 6px; overflow: hidden; aspect-ratio: 16/9;
  border: 1px solid var(--border); cursor: pointer;
}
.tile:hover { border-color: var(--accent); }
.tile video { width: 100%; height: 100%; object-fit: contain; display: block; }
.tile .label {
  position: absolute; left: 0; right: 0; bottom: 0; padding: 6px 8px; display: flex; gap: 8px; align-items: center;
  background: linear-gradient(transparent, rgba(0,0,0,.75)); font-size: 13px;
}
.offline-msg { position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--bad); flex: none; }
.dot.on { background: var(--ok); }
.badge { font-size: 11px; padding: 1px 6px; border-radius: 4px; background: var(--panel-2); border: 1px solid var(--border); color: var(--muted); }
.badge.rec { color: #fff; background: var(--rec); border-color: var(--rec); }
.empty { padding: 48px; text-align: center; color: var(--muted); }

/* ---------- camera view ---------- */
.camview { display: grid; grid-template-columns: 1fr 280px; gap: 16px; }
@media (max-width: 1000px) { .camview { grid-template-columns: 1fr; } }
.player { position: relative; background: #000; border-radius: var(--radius); overflow: hidden; aspect-ratio: 16/9; }
.player video { width: 100%; height: 100%; object-fit: contain; display: block; }
.player.clickable video { cursor: crosshair; }
.player .overlay { position: absolute; top: 10px; left: 10px; display: flex; gap: 6px; }
.side { display: flex; flex-direction: column; gap: 16px; }

.ptzpad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.ptzpad .btn { height: 52px; font-size: 18px; touch-action: none; user-select: none; }
.ptzpad .btn.active { background: var(--accent); color: var(--accent-ink); }
.zoomrow { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-top: 6px; }
.zoomrow .btn { height: 40px; touch-action: none; user-select: none; }
input[type=range] { width: 100%; accent-color: var(--accent); }

/* ---------- timeline ---------- */
.timeline-card { margin-top: 16px; }
.timeline {
  position: relative; height: 44px; background: var(--bg); border: 1px solid var(--border); border-radius: 6px;
  cursor: pointer; margin-top: 10px; overflow: hidden;
}
.timeline .span { position: absolute; top: 8px; bottom: 16px; background: #2563eb; opacity: .85; border-radius: 2px; }
.timeline .tick { position: absolute; bottom: 0; font-size: 10px; color: var(--muted); transform: translateX(-50%); }
.timeline .tick::before { content: ""; position: absolute; left: 50%; bottom: 12px; height: 6px; border-left: 1px solid var(--border); }
.timeline .cursor { position: absolute; top: 0; bottom: 0; width: 2px; background: var(--accent); pointer-events: none; }
.timeline .hover { position: absolute; top: 0; bottom: 0; width: 1px; background: #fff6; pointer-events: none; }

/* ---------- tables / admin ---------- */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 16px; }
.tabs a { padding: 8px 14px; color: var(--muted); border-bottom: 2px solid transparent; }
.tabs a.active { color: var(--text); border-bottom-color: var(--accent); }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { color: var(--muted); font-weight: 500; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; }
td .row { flex-wrap: nowrap; }
.meter { height: 10px; background: var(--bg); border: 1px solid var(--border); border-radius: 5px; overflow: hidden; }
.meter > div { height: 100%; background: var(--accent); }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat .v { font-size: 22px; font-weight: 600; margin-top: 4px; }

/* ---------- modal & toast ---------- */
.modal-back { position: fixed; inset: 0; background: rgba(0,0,0,.6); display: grid; place-items: center; z-index: 50; padding: 16px; }
.modal { width: 560px; max-width: 100%; max-height: 90vh; overflow: auto; display: flex; flex-direction: column; gap: 12px; }
.modal .actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }
#toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--panel-2);
  border: 1px solid var(--border); padding: 10px 16px; border-radius: 6px; opacity: 0; transition: opacity .2s;
  pointer-events: none; z-index: 60; max-width: 90vw;
}
#toast.show { opacity: 1; }
#toast.err { border-color: var(--bad); color: #fecaca; }

@media (max-width: 640px) {
  #topbar { gap: 8px; padding: 8px 10px; height: auto; flex-wrap: wrap; }
  #topbar .spacer { display: none; }
  #org-select { order: 5; flex: 1 1 100%; }
  #user-menu .email, #user-menu .role { display: none; }
  .camgrid { grid-template-columns: 1fr !important; }
  main { padding: 12px; }
  .grid2 { grid-template-columns: 1fr; }
  #user-menu .email { display: none; }
}
