:root {
  --bg: #0f141a;
  --panel: #171e27;
  --border: #26303d;
  --text: #d7dee8;
  --muted: #7d8b9c;
  --accent: #4da3ff;
  --ok: #3fb96f;
  --warn: #e0a63f;
  --err: #e05c5c;
}

* { box-sizing: border-box; }

/* The `hidden` attribute must always win over element/display-specific
   `display:` rules (e.g. `.placeholder { display: flex }` would otherwise
   keep the "Engine not running" overlay visible above the live video). */
[hidden] { display: none !important; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 18px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}

header h1 { font-size: 18px; margin: 0; }

.status-bar { display: flex; gap: 14px; align-items: center; }

.badge {
  padding: 2px 10px;
  border-radius: 10px;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  background: var(--border);
}
.badge.running { background: var(--ok); color: #06130a; }
.badge.starting, .badge.stopping { background: var(--warn); color: #1a1204; }
.badge.error { background: var(--err); color: #1c0808; }
.badge.idle, .badge.stopped { background: var(--border); }

.muted { color: var(--muted); font-size: 12px; }

.tabs {
  display: flex;
  gap: 4px;
  padding: 10px 18px 0;
  max-width: 1400px;
  margin: 0 auto;
}
.tab {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--border);
  border-bottom: none;
  border-radius: 8px 8px 0 0;
  padding: 7px 18px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}
.tab.active {
  background: var(--panel);
  color: var(--text);
  border-color: var(--accent);
}
.console-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}
.console {
  background: #0a0e13;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px;
  margin: 0;
  height: 70vh;
  overflow-y: auto;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-word;
}
.log-line { white-space: pre-wrap; }
.log-line.level-debug { color: var(--muted); }
.log-line.level-info { color: var(--text); }
.log-line.level-warning { color: var(--warn); }
.log-line.level-error, .log-line.level-critical { color: var(--err); }

.config-panel textarea {
  width: 100%;
  min-height: 60vh;
  background: #0a0e13;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12.5px;
  line-height: 1.5;
  resize: vertical;
}
.config-panel .controls button { background: var(--accent); color: #04101f; border: 0; padding: 6px 14px; border-radius: 6px; font-weight: 600; cursor: pointer; }
.config-panel .controls button:disabled { background: var(--border); color: var(--muted); cursor: default; }
.config-panel code { color: var(--accent); }

main {
  display: grid;
  grid-template-columns: 1.4fr 1fr;
  gap: 14px;
  padding: 14px 18px;
  max-width: 1400px;
  margin: 0 auto;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 14px;
  min-width: 0;
  /* Stack panel content and let feeds stretch: grid rows size to the
     tallest panel (the video one), so the events/reports feeds fill the
     remaining height instead of stopping at a fixed cap. */
  display: flex;
  flex-direction: column;
}

.panel h2 {
  font-size: 14px;
  margin: 0 0 10px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}

.video-frame {
  position: relative;
  background: #000;
  border-radius: 6px;
  overflow: hidden;
  aspect-ratio: 16 / 9;
}
.video-frame img, .video-frame canvas { width: 100%; height: 100%; object-fit: contain; display: block; }
.placeholder {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  color: var(--muted); font-size: 14px; padding: 20px; text-align: center;
}

/* Overlay shown while the engine reports "running" but frame fetches keep
   failing (pipeline still reaching PLAYING, transient 5xx, …). */
.spinner {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; gap: 12px;
  align-items: center; justify-content: center;
  background: rgba(0, 0, 0, 0.55);
  color: var(--muted); font-size: 13px;
}
.spinner::before {
  content: "";
  width: 34px; height: 34px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.controls {
  display: flex; flex-wrap: wrap; gap: 8px; align-items: center;
  margin-top: 10px;
}
.controls button {
  background: var(--accent); color: #04101f; border: 0;
  padding: 6px 16px; border-radius: 6px; font-weight: 600; cursor: pointer;
}
.controls button:disabled { background: var(--border); color: var(--muted); cursor: default; }
.controls input[type="text"] {
  flex: 1; min-width: 160px;
  background: var(--bg); color: var(--text);
  border: 1px solid var(--border); border-radius: 6px; padding: 6px 8px;
}
.controls label { font-size: 12px; color: var(--muted); display: flex; gap: 4px; align-items: center; }
.controls select {
  background: var(--bg); color: var(--text);
  border: 1px solid var(--border); border-radius: 6px; padding: 5px;
}

.feed {
  list-style: none; margin: 0; padding: 0;
  /* Fill the panel height (panels are flex columns; the grid row is sized
     by the video panel) and scroll the overflow. flex-basis 0 + min-height
     keep the feed's own content from inflating the row. */
  flex: 1 1 0;
  min-height: 140px;
  overflow-y: auto;
  font-size: 13px;
}
.feed li {
  cursor: pointer;
  padding: 6px 8px; border-bottom: 1px solid var(--border);
  display: flex; justify-content: space-between; gap: 8px; cursor: pointer;
}
.feed li:hover { background: var(--border); }
.feed .when { color: var(--muted); font-size: 11px; white-space: nowrap; }
.feed .tag {
  font-weight: 600; color: var(--accent); white-space: nowrap;
}
.feed li.report .tag { color: var(--ok); }

.clips { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 10px; }
.clip {
  position: relative;
  border: 1px solid var(--border); border-radius: 6px; padding: 8px;
  font-size: 12px;
}
.clip video { width: 100%; border-radius: 4px; background: #000; }
.clip .cid { font-weight: 600; }
.clip .report-line {
  margin-top: 4px; color: var(--ok); font-size: 11px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

/* Per-row actions on feed items (export / delete) and clip cards. */
.actions { display: flex; gap: 4px; align-items: center; flex-shrink: 0; }
.act {
  background: transparent; border: 1px solid var(--border);
  border-radius: 5px; color: var(--muted);
  font-size: 12px; line-height: 1; padding: 3px 6px;
  cursor: pointer; text-decoration: none;
}
.act:hover { color: var(--text); border-color: var(--muted); }
.act.delete:hover { color: var(--err); border-color: var(--err); }
.act:disabled { opacity: 0.4; cursor: default; }
.clip .clip-delete { position: absolute; top: 12px; right: 12px; }

/* Panel header rows with per-panel actions (sort / erase-all). */
.panel-head {
  display: flex; justify-content: space-between; align-items: center;
  gap: 10px; flex-wrap: wrap;
}
.panel-head h2 { margin: 0; }
.head-actions { display: flex; gap: 6px; align-items: center; }
.act.erase:hover { color: var(--err); border-color: var(--err); }

/* Events feed filter row (type / object class / received-at range). */
.event-filters {
  display: flex; gap: 6px; align-items: center; flex-wrap: wrap;
  margin: 8px 0;
}
.event-filters select,
.event-filters input {
  background: var(--bg); color: var(--text);
  border: 1px solid var(--border); border-radius: 6px;
  padding: 4px 6px; font-size: 12px;
}
.event-filters input[type="datetime-local"] { color-scheme: dark; }

/* VLM tie badge on analyzed event rows (click -> opens the report). */
.vlm-badge {
  color: var(--ok); font-size: 11px; font-weight: 600;
  white-space: nowrap; cursor: pointer;
}
.vlm-badge:hover { text-decoration: underline; }
.feed .ev-type { white-space: nowrap; }

dialog {
  background: var(--panel); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px;
  max-width: 640px; width: 90%;
}
dialog::backdrop { background: rgba(0, 0, 0, 0.6); }
dialog pre {
  background: var(--bg); padding: 10px; border-radius: 6px;
  overflow: auto; max-height: 50vh; font-size: 12px;
}
