:root {
  --bg: #0f1216;
  --panel: #171b21;
  --border: #2a2f37;
  --text: #e4e7eb;
  --muted: #8b93a1;
  --accent: #4f9cff;
  --ok: #3fbf6f;
  --warn: #e0a93f;
  --err: #e05f5f;
}
* { box-sizing: border-box; }
body {
  margin: 0; font-family: system-ui, sans-serif; background: var(--bg); color: var(--text);
}
header { display: flex; align-items: center; gap: 2rem; padding: 0.75rem 1.5rem; border-bottom: 1px solid var(--border); }
header h1 { font-size: 1.1rem; margin: 0; color: var(--accent); }
nav { display: flex; gap: 0.5rem; }
.tab-btn {
  background: none; border: 1px solid var(--border); color: var(--muted);
  padding: 0.4rem 0.9rem; border-radius: 6px; cursor: pointer;
}
.tab-btn.active { color: var(--text); border-color: var(--accent); }
main { padding: 1.5rem; }
.tab-panel { display: none; }
.tab-panel.active { display: block; }
table { width: 100%; border-collapse: collapse; margin-top: 1rem; }
th, td { text-align: left; padding: 0.4rem 0.6rem; border-bottom: 1px solid var(--border); font-size: 0.9rem; }
th { color: var(--muted); font-weight: 600; }
form { display: flex; gap: 0.5rem; flex-wrap: wrap; align-items: center; margin-top: 0.5rem; }
input, select, button {
  background: var(--panel); color: var(--text); border: 1px solid var(--border);
  border-radius: 6px; padding: 0.4rem 0.6rem; font-size: 0.9rem;
}
button { cursor: pointer; }
button:hover { border-color: var(--accent); }
.hint { color: var(--muted); font-size: 0.8rem; }
.gpu-status { margin: 0.5rem 0; color: var(--muted); font-size: 0.85rem; }
#cluster-plot { border: 1px solid var(--border); border-radius: 8px; margin-top: 0.75rem; background: var(--panel); min-height: 520px; }
#cluster-frames img { width: 90px; height: 90px; object-fit: cover; border-radius: 4px; margin: 4px; }
.badge-provisional { color: var(--warn); }
.badge-confirmed { color: var(--ok); }
#training-log { white-space: pre-wrap; font-family: monospace; font-size: 0.8rem; margin-top: 1rem; max-height: 300px; overflow-y: auto; }

.cameras-grid, .detections-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 1rem; margin-top: 1rem;
}
.camera-card, .detection-card {
  background: var(--panel); border: 1px solid var(--border); border-radius: 8px;
  padding: 0.6rem; font-size: 0.85rem;
}
.camera-card img, .detection-card img {
  width: 100%; aspect-ratio: 4/3; object-fit: cover; border-radius: 4px; background: #000;
}
.camera-card .cam-name { font-weight: 600; margin: 0.4rem 0 0.1rem; }
.camera-card .cam-status.ok { color: var(--ok); }
.camera-card .cam-status.err { color: var(--err); }
.detection-card .det-crops { display: flex; gap: 4px; }
.detection-card .det-crops img { flex: 1; aspect-ratio: 1/1; }
.detection-card .det-meta { margin-top: 0.4rem; color: var(--muted); }
.detections-controls { display: flex; gap: 0.5rem; align-items: center; margin-top: 0.5rem; }
.detections-pager { display: flex; gap: 0.5rem; margin-top: 1rem; }

.camera-card { cursor: pointer; transition: border-color .12s; }
.camera-card:hover { border-color: var(--accent); }
.section-sep { margin-top: 2rem; border-top: 1px solid var(--border); padding-top: 1rem; color: var(--muted); font-size: .95rem; }

.detail-head { display: flex; align-items: center; gap: 1rem; }
.detail-head h2 { margin: 0; }
.detail-grid { display: grid; grid-template-columns: minmax(0, 2fr) minmax(0, 1fr); gap: 1.25rem; margin-top: 1rem; }
@media (max-width: 900px) { .detail-grid { grid-template-columns: 1fr; } }

.live-wrap { position: relative; background: #000; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.live-wrap img { width: 100%; display: block; }
.box-overlay { position: absolute; inset: 0; pointer-events: none; }
.box-overlay .bx { position: absolute; border: 2px solid var(--ok); border-radius: 2px; }
.box-overlay .bx.eartag { border-color: var(--warn); }
.box-overlay .bx span {
  position: absolute; top: -1.15rem; left: 0; font-size: .7rem; white-space: nowrap;
  background: rgba(0,0,0,.65); padding: 0 .25rem; border-radius: 3px;
}
.fs-btn { position: absolute; top: .5rem; right: .5rem; opacity: .85; font-size: 1rem; line-height: 1; padding: .3rem .5rem; }
.live-wrap:fullscreen { display: flex; align-items: center; justify-content: center; }
.live-wrap:fullscreen img { max-height: 100vh; width: auto; }
.detail-live-bar { display: flex; gap: .75rem; align-items: center; margin-top: .5rem; flex-wrap: wrap; }
.detail-models { background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: .75rem; font-size: .9rem; }
.detail-models h3 { margin-top: 0; font-size: .95rem; }
.kv { display: flex; justify-content: space-between; gap: .5rem; padding: .15rem 0; border-bottom: 1px dotted var(--border); }
.kv span:last-child { color: var(--text); }
.kv span:first-child { color: var(--muted); }
.digit-grid { display: grid; grid-template-columns: repeat(10, 1fr); gap: 2px; margin-top: .35rem; font-size: .62rem; text-align: center; }
.digit-grid div { padding: 2px 0; border-radius: 2px; background: #1f242c; }
.digit-grid div.best { background: var(--accent); color: #fff; font-weight: 600; }

.presence { white-space: nowrap; }
.presence.online { color: var(--ok); }
.presence.offline { color: var(--err); }
.presence.muted { color: var(--muted); }
.detail-side { display: flex; flex-direction: column; gap: 1rem; }
.esp32-raw { white-space: pre-wrap; word-break: break-all; margin: .25rem 0; font-size: .8rem; color: var(--muted); }
