:root {
  --bg: #0b0f17; --panel: #131826; --panel-2: #1a2031; --border: #232b3d;
  --text: #e6ecf5; --muted: #8a93a8;
  --primary: #4f8cff; --primary-hi: #6aa1ff;
  --danger: #ef4444; --success: #22c55e; --warn: #f59e0b;
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; background: var(--bg); color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; font-size: 14px; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
button { font-family: inherit; }

.auth-shell { min-height: 100vh; display: grid; place-items: center; padding: 20px;
  background: radial-gradient(ellipse at top, #1a2244 0%, var(--bg) 60%); }
.auth-card { background: var(--panel); border: 1px solid var(--border); border-radius: 14px;
  padding: 32px; width: 100%; max-width: 420px; box-shadow: 0 20px 60px rgba(0,0,0,0.4); }
.brand { font-size: 28px; font-weight: 700; margin: 0 0 4px; letter-spacing: -0.02em; }
.brand span { color: var(--primary); }
.brand.small { font-size: 18px; margin: 0; }
.brand-row { display: flex; align-items: center; gap: 10px; }
.brand-logo { display: block; object-fit: contain; }
.brand-logo-lg { height: 64px; max-width: 200px; margin: 0 auto 16px; }
.brand-logo-sm { height: 28px; max-width: 80px; }
.tagline { color: var(--muted); margin: 0 0 24px; font-size: 13px; }
.tabs { display: flex; gap: 4px; background: var(--bg); padding: 4px; border-radius: 10px; margin-bottom: 20px; }
.tab { flex: 1; background: transparent; color: var(--muted); border: none; padding: 10px;
  border-radius: 7px; cursor: pointer; font-weight: 500; }
.tab.active { background: var(--panel-2); color: var(--text); }
.auth-form { display: flex; flex-direction: column; gap: 14px; }
.auth-form label { display: flex; flex-direction: column; gap: 6px; font-size: 12px; color: var(--muted); font-weight: 500; }
.auth-form input { background: var(--bg); border: 1px solid var(--border); color: var(--text);
  padding: 10px 12px; border-radius: 8px; font-size: 14px; outline: none; transition: border-color 0.15s; }
.auth-form input:focus { border-color: var(--primary); }

.btn { background: var(--panel-2); color: var(--text); border: 1px solid var(--border);
  padding: 9px 14px; border-radius: 8px; cursor: pointer; font-weight: 500; font-size: 13px;
  transition: all 0.15s; display: inline-flex; align-items: center; gap: 6px; }
.btn:hover:not(:disabled) { background: #222a3f; }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: white; }
.btn.primary:hover:not(:disabled) { background: var(--primary-hi); }
.btn.danger { background: transparent; border-color: var(--danger); color: var(--danger); }
.btn.danger:hover:not(:disabled) { background: var(--danger); color: white; }
.btn.ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn.ghost:hover { color: var(--text); }
.btn.small { padding: 6px 10px; font-size: 12px; }
.btn.live { background: var(--danger); border-color: var(--danger); color: white; }
.error { color: var(--danger); font-size: 12px; min-height: 16px; }

.topbar { display: flex; justify-content: space-between; align-items: center;
  padding: 12px 20px; background: var(--panel); border-bottom: 1px solid var(--border); }
.tenant-info { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 13px; }
.tenant-info .dot { width: 4px; height: 4px; background: var(--muted); border-radius: 50%; }
.tenant-info #tenant-slug { color: var(--primary); font-weight: 600; }
.role { background: var(--panel-2); padding: 2px 8px; border-radius: 4px; font-size: 11px;
  text-transform: uppercase; letter-spacing: 0.05em; }
.role.super { background: linear-gradient(135deg, #f59e0b, #ef4444); color: white;
  font-weight: 600; box-shadow: 0 0 8px rgba(245,158,11,0.4); }
.tenant-switcher { background: var(--bg); border: 1px solid var(--border); color: var(--text);
  padding: 4px 8px; border-radius: 6px; font-size: 12px; max-width: 180px; }
.tenant-switcher:focus { outline: none; border-color: var(--primary); }

.camera-item .cam-tenant { display: inline-block; font-size: 10px; padding: 1px 5px;
  background: var(--bg); color: var(--muted); border-radius: 3px; margin-left: 4px;
  font-family: ui-monospace, monospace; }

.users-modal-card { max-width: 900px; }
.users-table-wrap { max-height: 60vh; overflow-y: auto; margin: 12px 0; border: 1px solid var(--border); border-radius: 8px; }
.users-table { width: 100%; border-collapse: collapse; font-size: 12px; }
.users-table th, .users-table td { padding: 8px 10px; text-align: left; border-bottom: 1px solid var(--border); }
.users-table th { background: var(--panel-2); color: var(--muted); font-weight: 500;
  text-transform: uppercase; font-size: 11px; letter-spacing: 0.05em;
  position: sticky; top: 0; z-index: 1; }
.users-table tr:last-child td { border-bottom: none; }
.users-table tr:hover td { background: var(--panel-2); }
.users-table .role-pill { padding: 1px 6px; border-radius: 3px; font-size: 10px;
  font-weight: 600; text-transform: uppercase; letter-spacing: 0.05em; }
.users-table .role-pill.super_admin { background: linear-gradient(135deg, #f59e0b, #ef4444); color: white; }
.users-table .role-pill.admin { background: rgba(79,140,255,0.2); color: var(--primary); }
.users-table .role-pill.user { background: var(--bg); color: var(--muted); }
.users-table .status-on { color: var(--success); }
.users-table .status-off { color: var(--muted); }
.cuda-badge { background: var(--panel-2); padding: 2px 8px; border-radius: 4px; font-size: 11px;
  border: 1px solid var(--border); font-family: ui-monospace, monospace; }
.cuda-badge.cuda { color: var(--success); border-color: rgba(34,197,94,0.3); }
.cuda-badge.cpu  { color: var(--warn); border-color: rgba(245,158,11,0.3); }

.layout { display: grid; grid-template-columns: 280px 1fr; height: calc(100vh - 53px); }
.sidebar { background: var(--panel); border-right: 1px solid var(--border); overflow-y: auto; padding: 16px; }
.sidebar-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.sidebar-header h2 { margin: 0; font-size: 14px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); }
.camera-list { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 4px; }
.camera-item { padding: 10px 12px; border-radius: 8px; cursor: pointer; transition: background 0.1s;
  border: 1px solid transparent; position: relative; }
.camera-item:hover { background: var(--panel-2); }
.camera-item.active { background: var(--panel-2); border-color: var(--primary); }
.camera-item .cam-name { font-weight: 500; }
.camera-item .cam-loc { font-size: 11px; color: var(--muted); margin-top: 2px; }
.camera-item .cam-status { display: inline-block; width: 6px; height: 6px; border-radius: 50%;
  background: var(--success); margin-right: 6px; }
.camera-item.inactive .cam-status { background: var(--muted); }
.camera-item.live::after { content: "●"; color: var(--danger); position: absolute; right: 12px; top: 10px;
  font-size: 12px; animation: pulse 1.5s infinite; }
@keyframes pulse { 50% { opacity: 0.4; } }

.workspace { overflow-y: auto; padding: 24px; }
.empty { display: grid; place-items: center; height: 100%; text-align: center; color: var(--muted); }
.empty h2 { color: var(--text); margin-bottom: 8px; }
.workspace-header { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 16px; }
.workspace-header h2 { margin: 0 0 4px; }
.workspace-actions { display: flex; gap: 8px; }
.canvas-wrap { background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 16px; margin-bottom: 16px; }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 12px;
  padding-bottom: 12px; border-bottom: 1px solid var(--border); }
.toolbar .sep { width: 1px; height: 24px; background: var(--border); margin: 0 4px; }
.toolbar .inline { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 12px; }
.toolbar select, .toolbar input { background: var(--bg); border: 1px solid var(--border);
  color: var(--text); padding: 6px 8px; border-radius: 6px; font-size: 12px; }
.hint { color: var(--warn); font-size: 12px; }

/* Canvas host — both snapshot and live tabs use the SAME sizing rules so
   ROIs render at identical screen pixel coordinates across both views,
   AND across screen sizes.

   The challenge: simply using `width:100%; aspect-ratio:16/9; max-height:70vh`
   silently breaks the aspect ratio on wide screens — max-height clamps the
   height but width stays at 100%, producing a non-16:9 box and inconsistent
   appearance across different screen resolutions.

   The fix: cap BOTH dimensions proportionally so 16:9 is always preserved.
   - max-width = 70vh × 16/9 means when the viewport limits height to 70vh,
     width auto-shrinks to keep 16:9.
   - width:100% still applies when the workspace is narrower than that cap.
   - margin:0 auto centers the canvas if max-width takes effect.
   Result: on every screen (1920×1080, 1366×768, 4K, mobile), the canvas
   is exactly 16:9, centered, at the largest size that fits within 70% of
   the viewport height. */
.canvas-host { position: relative; width: 100%;
  aspect-ratio: 16 / 9;
  max-width: calc(70vh * 16 / 9);
  max-height: 70vh;
  margin: 0 auto;
  background: #000; border-radius: 8px; overflow: hidden; }
.canvas-host img { position: absolute; top: 0; left: 0;
  width: 100%; height: 100%;
  object-fit: contain;  /* letterbox if source != 16:9 — but the BOX is fixed */
  user-select: none; -webkit-user-drag: none; }
#live-img { background: #000; }
.canvas-host canvas { position: absolute; top: 0; left: 0; width: 100%; height: 100%;
  cursor: crosshair; z-index: 2; }
#detect-canvas { pointer-events: none; }
.canvas-host canvas.not-drawing { cursor: default; }
.no-snapshot { padding: 60px 30px; text-align: center; color: var(--muted); display: flex;
  flex-direction: column; align-items: center; justify-content: center; gap: 8px; min-height: 300px; }
.no-snapshot .ns-icon { font-size: 48px; opacity: 0.5; margin-bottom: 8px; }
.no-snapshot h3 { color: var(--text); margin: 0; font-size: 18px; }
.no-snapshot p { margin: 0; max-width: 440px; line-height: 1.5; }
.live-pill { position: absolute; top: 12px; left: 12px; background: var(--danger); color: white;
  font-size: 11px; font-weight: 600; padding: 4px 10px; border-radius: 20px; z-index: 3; letter-spacing: 0.05em;
  box-shadow: 0 0 10px rgba(239,68,68,0.5); }

.detect-summary { margin-top: 12px; padding: 12px; background: var(--bg); border-radius: 8px;
  border-left: 3px solid var(--primary); font-size: 13px; display: flex; gap: 16px; flex-wrap: wrap; }
.detect-summary .stat { display: flex; flex-direction: column; gap: 2px; }
.detect-summary .stat .v { font-size: 18px; font-weight: 600; }
.detect-summary .stat .l { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; }
.detect-summary.has-violations { border-left-color: var(--danger); background: rgba(239,68,68,0.05); }
.detect-summary .v.violations { color: var(--danger); }
.detect-summary .v.success { color: var(--success); }

.bottom-grid { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 16px; }

/* ============================ Tabs ============================ */
.tab-bar { display: flex; gap: 4px; margin-bottom: 16px; border-bottom: 1px solid var(--border); }
.tab { background: none; border: none; padding: 10px 18px; color: var(--muted);
  font-size: 14px; font-weight: 500; cursor: pointer; border-bottom: 2px solid transparent;
  transition: color 0.15s, border-color 0.15s; margin-bottom: -1px; }
.tab:hover { color: var(--text); }
.tab.active { color: var(--text); border-bottom-color: var(--primary); }
.tab-panel { display: none; }
.tab-panel.active { display: block; animation: fade-in 0.15s; }
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }

/* Snapshot tab: zones list spans full width below the canvas */
.roi-panel-wide { background: var(--panel); border: 1px solid var(--border);
  border-radius: 12px; padding: 16px; margin-top: 16px; }
.roi-panel-wide h3 { margin: 0 0 12px; font-size: 14px; text-transform: uppercase;
  letter-spacing: 0.05em; color: var(--muted); font-weight: 500; }

/* History tab: 2-column grid */
.history-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
@media (max-width: 900px) { .history-grid { grid-template-columns: 1fr; } }

/* Inline live pill on the live-tab toolbar */
.live-pill-inline { display: inline-flex; align-items: center; padding: 4px 10px;
  background: var(--danger); color: white; border-radius: 12px; font-size: 12px;
  font-weight: 600; animation: pulse 2s infinite; }

/* ============================ Dashboard ============================ */
.dash-stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 20px; }
.dash-stat { background: var(--panel); border: 1px solid var(--border); border-radius: 10px;
  padding: 14px 18px; display: flex; flex-direction: column; gap: 4px; }
.dash-stat .v { font-size: 28px; font-weight: 700; color: var(--text); letter-spacing: -0.02em; }
.dash-stat .l { font-size: 11px; color: var(--muted); text-transform: uppercase;
  letter-spacing: 0.05em; }
@media (max-width: 800px) { .dash-stats { grid-template-columns: repeat(2, 1fr); } }

.dash-section { background: var(--panel); border: 1px solid var(--border);
  border-radius: 12px; padding: 16px; margin-bottom: 16px; }
.dash-section-header { display: flex; justify-content: space-between; align-items: center;
  margin-bottom: 12px; }
.dash-section-header h3 { margin: 0; font-size: 14px; text-transform: uppercase;
  letter-spacing: 0.05em; color: var(--muted); font-weight: 500; }

.dash-camera-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 12px; }
.dash-camera-card { background: var(--bg); border: 1px solid var(--border);
  border-left: 3px solid var(--muted); border-radius: 8px; padding: 12px;
  cursor: pointer; transition: border-color 0.15s, transform 0.1s; }
.dash-camera-card:hover { transform: translateY(-1px); border-left-color: var(--primary); }
.dash-camera-card.running { border-left-color: var(--success); }
.dash-camera-card.error { border-left-color: var(--danger); }
.dash-camera-card .cam-name { font-weight: 600; margin-bottom: 4px; }
.dash-camera-card .cam-meta { font-size: 11px; color: var(--muted);
  font-family: ui-monospace, monospace; }
.dash-camera-card .cam-status-dot { display: inline-block; width: 8px; height: 8px;
  border-radius: 50%; background: var(--muted); margin-right: 6px; vertical-align: middle; }
.dash-camera-card.running .cam-status-dot { background: var(--success); animation: pulse 2s infinite; }
.dash-camera-card.error .cam-status-dot { background: var(--danger); }
.dash-camera-card .cam-err { color: var(--danger); font-size: 11px; margin-top: 4px;
  word-break: break-word; }

/* ============================ Violations gallery ============================ */
.violations-gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 10px; }
.violation-card { background: var(--bg); border: 1px solid var(--border);
  border-radius: 8px; overflow: hidden; cursor: pointer; transition: transform 0.1s; }
.violation-card:hover { transform: translateY(-2px); border-color: var(--primary); }
.violation-card .thumb-wrap { background: #000; aspect-ratio: 16/9; display: grid;
  place-items: center; overflow: hidden; }
.violation-card .thumb-wrap img { width: 100%; height: 100%; object-fit: cover; }
.violation-card .thumb-wrap .no-thumb { color: var(--muted); font-size: 11px; }
.violation-card .meta { padding: 8px 10px; font-size: 11px; }
.violation-card .meta .row1 { font-weight: 600; color: var(--text); margin-bottom: 2px; }
.violation-card .meta .row2 { color: var(--muted); font-family: ui-monospace, monospace; }
.violation-preview-wrap { background: #000; border-radius: 8px; margin: 12px 0;
  display: grid; place-items: center; max-height: 65vh; overflow: hidden; }
.violation-preview-wrap img { max-width: 100%; max-height: 65vh; display: block; }

/* ============================ Notification modal ============================ */
.notify-fieldset { border: 1px solid var(--border); border-radius: 8px;
  padding: 12px 14px 8px; margin: 0 0 12px; }
.notify-fieldset legend { padding: 0 8px; font-size: 12px; font-weight: 600;
  color: var(--text); text-transform: uppercase; letter-spacing: 0.05em; }
.notify-fieldset code { background: var(--bg); padding: 1px 5px; border-radius: 3px;
  font-size: 11px; font-family: ui-monospace, monospace; }
.roi-panel, .event-panel, .export-panel { background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 16px; }
.roi-panel h3, .event-panel h3, .export-panel h3 { margin: 0 0 12px; font-size: 14px; }
.roi-list, .event-list, .export-list { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 8px;
  max-height: 380px; overflow-y: auto; }
.roi-item { display: flex; align-items: center; gap: 12px; padding: 10px 12px;
  background: var(--panel-2); border-radius: 8px; border-left: 3px solid var(--primary); }
.roi-swatch { width: 14px; height: 14px; border-radius: 3px; flex-shrink: 0; }
.roi-info { flex: 1; }
.roi-info .name { font-weight: 500; }
.roi-info .meta { font-size: 11px; color: var(--muted); }
.roi-info .coords { font-family: ui-monospace, monospace; font-size: 10px;
  white-space: pre; margin-top: 6px; background: var(--bg); padding: 6px 8px;
  border-radius: 4px; max-height: 120px; overflow-y: auto; }
.roi-type { padding: 2px 8px; border-radius: 4px; font-size: 11px;
  background: rgba(239,68,68,0.15); color: var(--danger); }
.roi-type.allowed { background: rgba(34,197,94,0.15); color: var(--success); }

.event-item { display: flex; gap: 10px; padding: 8px 10px; background: var(--panel-2); border-radius: 6px;
  font-size: 12px; align-items: center; border-left: 3px solid var(--muted); }
.event-item.enter { border-left-color: var(--danger); }
.event-item.exit { border-left-color: var(--success); }
.event-item .badge { padding: 1px 6px; border-radius: 3px; font-size: 10px; font-weight: 600;
  text-transform: uppercase; letter-spacing: 0.05em; }
.event-item.enter .badge { background: rgba(239,68,68,0.2); color: var(--danger); }
.event-item.exit  .badge { background: rgba(34,197,94,0.2); color: var(--success); }
.event-item.violation { background: rgba(239,68,68,0.06); border-left: 3px solid var(--danger); }
.event-item .vio-tag { color: var(--danger); font-weight: bold; margin-right: 4px; }
.event-item .when { color: var(--muted); font-family: ui-monospace, monospace; font-size: 11px; min-width: 70px; }
.event-item .desc { flex: 1; }
.event-item .tid { background: var(--bg); padding: 1px 5px; border-radius: 3px;
  font-family: ui-monospace, monospace; font-size: 11px; color: var(--primary); }

.export-item { display: flex; gap: 10px; padding: 6px; background: var(--panel-2); border-radius: 8px;
  align-items: center; }
.export-item img { width: 64px; height: 48px; object-fit: cover; border-radius: 4px;
  background: #000; flex-shrink: 0; cursor: zoom-in; border: 1px solid var(--border); }
.export-item .info { flex: 1; min-width: 0; font-size: 12px; }
.export-item .info .name { font-weight: 500; font-family: ui-monospace, monospace;
  font-size: 11px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.export-item .info .meta { color: var(--muted); font-size: 11px; margin-top: 2px; }
.export-item .info .note { color: var(--text); font-size: 11px; margin-top: 2px;
  font-style: italic; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.export-item .actions { display: flex; gap: 4px; flex-shrink: 0; }
.export-item .actions .btn { padding: 4px 6px; font-size: 11px; }

.export-modal-card { max-width: 900px; }
.export-preview-wrap { margin: 12px 0; background: #000; border-radius: 8px; overflow: hidden;
  display: grid; place-items: center; min-height: 240px; max-height: 65vh; transition: opacity 0.15s; }
.export-preview-wrap img { max-width: 100%; max-height: 65vh; display: block; }
.export-preview-wrap:hover { opacity: 0.92; }

.modal { position: fixed; inset: 0; background: rgba(0,0,0,0.6); display: grid; place-items: center; z-index: 100; }
.modal-card { background: var(--panel); border: 1px solid var(--border); border-radius: 12px;
  padding: 24px; width: 100%; max-width: 460px; }
.modal-card h3 { margin: 0 0 16px; }
.modal-card form { display: flex; flex-direction: column; gap: 12px; }
.modal-card label { display: flex; flex-direction: column; gap: 6px; font-size: 12px; color: var(--muted); }
.modal-card input { background: var(--bg); border: 1px solid var(--border); color: var(--text);
  padding: 8px 10px; border-radius: 6px; outline: none; }
.modal-card input:focus { border-color: var(--primary); }
.modal-card .row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.probe-result { padding: 10px 12px; background: var(--bg); border-radius: 6px;
  border-left: 3px solid var(--muted); font-size: 12px; }
.probe-result.ok { border-left-color: var(--success); }
.probe-result.fail { border-left-color: var(--danger); }
.probe-result .head { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; font-weight: 500; }
.probe-result .head .icon { font-size: 14px; }
.probe-result .ok .icon { color: var(--success); }
.probe-result .fail .icon { color: var(--danger); }
.probe-result .stats { color: var(--muted); font-family: ui-monospace, monospace; font-size: 11px; }
.probe-result .err { color: var(--danger); margin-top: 4px; }
.probe-result .hint-text { color: var(--warn); margin-top: 6px; padding: 6px 8px;
  background: rgba(245,158,11,0.1); border-radius: 4px; font-size: 11px; line-height: 1.4; }
.probe-result .thumb { margin-top: 6px; max-width: 320px; max-height: 180px; display: block; border-radius: 4px; }
.modal-card .checkbox-row { flex-direction: row; align-items: center; gap: 8px; }
.modal-card .checkbox-row input[type="checkbox"] { width: auto; }
.modal-card .checkbox-row span { font-size: 13px; color: var(--text); }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }

@media (max-width: 1400px) {
  .bottom-grid { grid-template-columns: 1fr 1fr; }
  .export-panel { grid-column: 1 / -1; }
  .export-list { max-height: 240px; }
}
@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { display: none; }
  .bottom-grid { grid-template-columns: 1fr; }
  .export-panel { grid-column: auto; }
}