:root {
  --bg: #0f1117;
  --surface: #1a1d27;
  --surface2: #22263a;
  --accent: #3b82f6;
  --danger: #ef4444;
  --warn: #f59e0b;
  --success: #22c55e;
  --text: #e2e8f0;
  --text-dim: #94a3b8;
  --border: #2d3348;
  --radius: 8px;
  --font: 'Inter', system-ui, sans-serif;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ── Header ── */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  height: 52px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.logo { font-size: 18px; font-weight: 700; letter-spacing: .5px; }
.header-right { display: flex; align-items: center; gap: 16px; color: var(--text-dim); font-size: 13px; }

/* ── Layout ── */
.layout {
  display: grid;
  grid-template-columns: 1fr 360px;
  gap: 0;
  flex: 1;
  overflow: hidden;
  min-height: 0;
}

.layout.single-camera-focus {
  grid-template-columns: minmax(0, 1fr) 320px;
}

/* ── Camera grid ── */
.cameras-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  grid-auto-rows: minmax(260px, 1fr);
  gap: 6px;
  padding: 10px;
  overflow: auto;
  background: var(--bg);
  align-content: start;
  min-height: 0;
}

.cam-tile {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
  cursor: pointer;
  min-height: 260px;
}
.cam-tile:hover { border-color: var(--accent); }
.cam-tile.focused-camera { border-color: var(--accent); box-shadow: 0 0 0 1px rgba(59,130,246,.35); }
.cam-tile .cam-header {
  padding: 4px 8px;
  font-size: 11px;
  color: var(--text-dim);
  background: rgba(0,0,0,.4);
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.cam-tile .cam-feed {
  flex: 1;
  background: #000;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
}
.cam-tile .cam-feed img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.cam-tile .cam-feed .no-signal {
  color: var(--text-dim);
  font-size: 12px;
}
.cam-tile .cam-footer {
  padding: 3px 8px;
  display: flex;
  gap: 6px;
  background: rgba(0,0,0,.3);
}
.cam-tile .cam-footer button {
  font-size: 10px;
  padding: 2px 6px;
}

.cameras-grid.single-camera-view {
  grid-template-columns: 1fr;
  grid-template-rows: minmax(0, 1fr);
  grid-auto-rows: minmax(0, 1fr);
  overflow: hidden;
  padding: 6px;
  align-content: stretch;
}

.cameras-grid.single-camera-view .cam-tile {
  grid-column: auto !important;
  grid-row: 1;
  min-height: 0;
  height: 100%;
}

.cameras-grid.single-camera-view .cam-feed {
  min-height: 0;
}

.cameras-grid.single-camera-view .cam-feed img {
  object-fit: contain;
  background: #000;
}

/* ── Sidebar ── */
.sidebar {
  border-left: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  flex-direction: column;
  gap: 0;
  overflow-y: auto;
}
.card {
  padding: 14px;
  border-bottom: 1px solid var(--border);
}
.card h3 { font-size: 13px; font-weight: 600; margin-bottom: 10px; color: var(--text-dim); text-transform: uppercase; letter-spacing: .5px; }

/* Filters */
.filter-card select,
.filter-card input[type=text] {
  width: 100%;
  margin-bottom: 8px;
  padding: 6px 8px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text);
  font-size: 13px;
}
.toggle-label {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 8px;
  color: var(--text-dim);
  cursor: pointer;
}

/* Stats */
.stats-container { display: flex; flex-direction: column; gap: 4px; }
.stat-row {
  display: flex;
  justify-content: space-between;
  padding: 4px 8px;
  background: var(--surface2);
  border-radius: 4px;
  font-size: 12px;
}
.stat-count { font-weight: 700; color: var(--accent); }

/* Alert log */
.alerts-card { flex: 1; }
#alert-log {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 380px;
  overflow-y: auto;
}
.alert-item {
  padding: 8px 10px;
  border-radius: 6px;
  background: var(--surface2);
  border-left: 3px solid var(--warn);
  cursor: pointer;
  transition: opacity .15s;
}
.alert-item:hover { opacity: .85; }
.alert-item.false-positive { opacity: .7; }
.alert-item.severity-high { border-left-color: var(--warn); }
.alert-item.severity-critical { border-left-color: #a21caf; }
.alert-item.severity-low { border-left-color: var(--success); }
.alert-item.event-guard_absence { border-left-color: var(--danger); }
.alert-item .alert-type { font-weight: 600; font-size: 12px; }
.alert-item .alert-meta { font-size: 11px; color: var(--text-dim); margin-top: 2px; }
.alert-item .alert-time { font-size: 10px; color: var(--text-dim); float: right; }

/* ── Badges ── */
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 600;
}
.badge-online { background: rgba(34,197,94,.2); color: var(--success); }
.badge-offline { background: rgba(239,68,68,.2); color: var(--danger); }
.badge-danger { background: var(--danger); color: #fff; }

/* ── Buttons ── */
.btn {
  padding: 6px 14px;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  font-size: 12px;
  font-weight: 600;
  background: var(--accent);
  color: #fff;
  transition: opacity .15s;
}
.btn:hover { opacity: .85; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-success { background: var(--success); }
.btn-danger { background: var(--danger); }
.btn-sm { padding: 2px 8px; font-size: 11px; }

/* ── Modals ── */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.modal.hidden { display: none; }
.modal-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  min-width: 400px;
  max-width: 90vw;
  max-height: 90vh;
  overflow-y: auto;
}
.modal-box h3 { margin-bottom: 10px; }
.hint { font-size: 12px; color: var(--text-dim); margin-bottom: 10px; }
.zone-editor-wrap { position: relative; }
.modal-box canvas {
  border: 1px solid var(--border);
  border-radius: 4px;
  cursor: crosshair;
  display: block;
  max-width: 100%;
}
.modal-controls {
  display: flex;
  gap: 8px;
  margin-top: 10px;
  align-items: center;
}
.modal-controls input {
  padding: 6px 8px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text);
}

.snapshot-box { text-align: center; }
.snapshot-box img { max-width: 100%; border-radius: 4px; margin-bottom: 10px; }
.snapshot-meta {
  margin-bottom: 10px;
  color: var(--text-dim);
  font-size: 12px;
  text-align: left;
}
.snapshot-actions {
  justify-content: flex-end;
  flex-wrap: wrap;
}
.snapshot-feedback-status {
  flex: 1 1 220px;
  margin-bottom: 0;
  text-align: left;
}

/* Zone list in modal */
#zone-list-modal { margin-top: 14px; }
.zone-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 8px;
  background: var(--surface2);
  border-radius: 4px;
  margin-bottom: 4px;
  font-size: 12px;
}

/* Scrollbars */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

/* Alert indicator on camera */
.cam-alert-flash {
  position: absolute;
  inset: 0;
  border: 3px solid var(--danger);
  border-radius: 6px;
  pointer-events: none;
  animation: flash 0.4s ease-in-out 3;
}
@keyframes flash {
  0%, 100% { opacity: 1; }
  50% { opacity: 0; }
}

@media (max-width: 1200px) {
  .layout,
  .layout.single-camera-focus {
    grid-template-columns: 1fr;
  }

  .sidebar {
    border-left: none;
    border-top: 1px solid var(--border);
  }
}
