:root {
  --bg: #0f1216;
  --panel: #171b21;
  --panel-2: #1e232b;
  --line: #2a313b;
  --text: #e6e9ee;
  --muted: #8b95a3;
  --accent: #2563eb;
  --F: #3b82f6;
  --H: #ef4444;
  --N: #22c55e;
  --U: #eab308;
  --warn: #f59e0b;
  --radius: 10px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", "Malgun Gothic", sans-serif;
}
h1, h2, h3, h4 { margin: 0; font-weight: 650; }
h2 { font-size: 18px; margin-bottom: 8px; }
.muted { color: var(--muted); }
small { font-size: 0.8em; }

button, .button, select, input {
  font: inherit;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 6px 12px;
}
button, .button { cursor: pointer; text-decoration: none; display: inline-block; }
button:hover:not(:disabled), .button:hover { border-color: #3b4552; background: #252b34; }
button:disabled { opacity: 0.45; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover:not(:disabled) { background: #1d4ed8; }
button.small { padding: 3px 9px; font-size: 12px; }
button:focus-visible, input:focus-visible, select:focus-visible, .button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* 상단 */
.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px 16px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
  position: sticky;
  top: 0;
  z-index: 5;
}
.topbar h1 { font-size: 22px; letter-spacing: 0.01em; }
.controls { display: flex; flex-wrap: wrap; gap: 6px; }
.chips { display: flex; gap: 6px; margin-left: auto; }
.chip {
  font-size: 12px;
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 2px 10px;
  font-variant-numeric: tabular-nums;
}

.settings {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 10px 16px;
  padding: 12px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.settings label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
.settings-actions { display: flex; gap: 6px; align-items: end; }

.alert-banner {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 18px;
  padding: 8px 16px;
  background: #3a1214;
  border-bottom: 1px solid var(--H);
  color: #ffd5d5;
  animation: pulse 1.2s ease-in-out infinite alternate;
}
@keyframes pulse { from { background: #3a1214; } to { background: #4d1619; } }
@media (prefers-reduced-motion: reduce) { .alert-banner { animation: none; } }

/* 레이아웃 */
.layout {
  display: grid;
  grid-template-columns: minmax(0, 1.35fr) minmax(320px, 1fr);
  gap: 16px;
  padding: 16px;
  align-items: start;
}
@media (max-width: 900px) { .layout { grid-template-columns: 1fr; } }

.stage {
  position: relative;
  aspect-ratio: 16 / 9;
  background: #000;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
}
.stage video, .stage canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
}
.stage-empty {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  text-align: center;
  padding: 24px;
  gap: 4px;
}
.stage-empty p { margin: 0; }

.manual { display: flex; gap: 6px; margin-top: 10px; }
.manual input { flex: 1; min-width: 0; }

/* 요약 */
.summary {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(110px, 1fr));
  gap: 8px;
  margin-top: 14px;
}
.sum-cell {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 8px 10px;
  display: flex;
  flex-direction: column;
}
.sum-cell.iff-F { border-color: color-mix(in srgb, var(--F) 55%, var(--line)); }
.sum-cell.iff-H { border-color: color-mix(in srgb, var(--H) 55%, var(--line)); }
.sum-cell.iff-N { border-color: color-mix(in srgb, var(--N) 55%, var(--line)); }
.sum-cell.iff-U { border-color: color-mix(in srgb, var(--U) 55%, var(--line)); }
.sum-label { font-size: 12px; color: var(--muted); }
.sum-value { font-size: 20px; font-weight: 650; font-variant-numeric: tabular-nums; }
.sum-value small { color: var(--muted); font-weight: 400; }
.sum-note { grid-column: 1 / -1; margin: 0; font-size: 12px; }

/* 이벤트 */
.log { margin-top: 14px; }
.log-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 6px; }
.log-head h2 { margin: 0; }
#event-log {
  list-style: none;
  margin: 0;
  padding: 6px 10px;
  max-height: 240px;
  overflow: auto;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  font-size: 12.5px;
}
#event-log li { padding: 2px 0; border-bottom: 1px solid #20262e; }
#event-log li:last-child { border-bottom: 0; }
#event-log time { color: var(--muted); font-variant-numeric: tabular-nums; }
#event-log .ev-warn { color: #fcd34d; }
#event-log .ev-alert { color: #fca5a5; }

/* 대상 카드 */
.tracks { display: flex; flex-direction: column; gap: 10px; }
.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 12px;
  opacity: 0.78;
  transition: opacity 0.2s, border-color 0.2s;
}
.card.in-view { opacity: 1; }
.card.iff-edge-F { border-color: color-mix(in srgb, var(--F) 55%, var(--line)); }
.card.iff-edge-H { border-color: color-mix(in srgb, var(--H) 55%, var(--line)); }
.card.iff-edge-N { border-color: color-mix(in srgb, var(--N) 55%, var(--line)); }
.card.iff-edge-U { border-color: color-mix(in srgb, var(--U) 55%, var(--line)); }
.card.level-alert { box-shadow: 0 0 0 1px var(--H) inset; }
.card-head { display: flex; align-items: center; gap: 8px; }
.card-head h3 { font-size: 15px; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.iff-badge { font-size: 12px; font-weight: 700; padding: 1px 8px; border-radius: 6px; color: #fff; }
.iff-badge.iff-F { background: #2563eb; }
.iff-badge.iff-H { background: #dc2626; }
.iff-badge.iff-N { background: var(--N); color: #062b12; }
.iff-badge.iff-U { background: var(--U); color: #2a2000; }
.iff-badge.marker { background: #7c3aed; }
.sum-cell.sum-marker { border-color: color-mix(in srgb, #7c3aed 55%, var(--line)); }
.card.marker { border-color: color-mix(in srgb, #7c3aed 55%, var(--line)); }
.level { font-size: 11px; padding: 1px 6px; border-radius: 6px; border: 1px solid; }
.level-alert { color: #fca5a5; border-color: var(--H); }
.level-warn { color: #fcd34d; border-color: var(--warn); }
.view { font-size: 11px; color: var(--muted); }
.view.on { color: #86efac; }
.reasons { margin: 8px 0 0; padding-left: 18px; font-size: 12.5px; color: #fcd34d; }
.card.level-alert .reasons { color: #fca5a5; }
.fields {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 4px 12px;
  margin: 8px 0 0;
}
.f { display: flex; flex-direction: column; min-width: 0; }
.f dt { font-size: 11px; color: var(--muted); }
.f dd { margin: 0; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.bar { display: inline-block; width: 60px; height: 6px; background: #2a313b; border-radius: 3px; vertical-align: middle; overflow: hidden; }
.bar span { display: block; height: 100%; background: var(--N); }
.card-actions { display: flex; gap: 6px; margin-top: 8px; }

/* 상세 */
dialog {
  width: min(720px, 92vw);
  max-height: 86vh;
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0;
}
dialog::backdrop { background: rgb(0 0 0 / 0.6); }
.detail-head { display: flex; justify-content: space-between; align-items: center; padding: 12px 16px; border-bottom: 1px solid var(--line); }
#detail-body, .dialog-body { padding: 4px 16px 16px; overflow: auto; }
#detail-body h4, .dialog-body h4 { font-size: 12px; color: var(--muted); margin: 12px 0 4px; }
pre {
  margin: 0;
  padding: 8px 10px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  font-size: 12px;
}

/* 캡처 */
.settings label.check { justify-content: end; }
.settings label.check span { display: flex; align-items: center; gap: 6px; color: var(--text); font-size: 13px; }
.stage.flash { animation: flash 0.45s ease-out; }
@keyframes flash { from { box-shadow: 0 0 0 4px #fff inset; } to { box-shadow: 0 0 0 0 transparent inset; } }
@media (prefers-reduced-motion: reduce) { .stage.flash { animation: none; } }
.captures { margin-top: 14px; }
.captures .log-head h2 { margin: 0; }
.captures #records-empty { margin: 0; font-size: 12.5px; }
.records {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 8px;
  max-height: 360px;
  overflow: auto;
}
.record {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 6px;
  text-align: left;
  border-radius: var(--radius);
}
.record img, .record-noimg {
  width: 100%;
  aspect-ratio: 16 / 9;
  object-fit: cover;
  border-radius: 6px;
  background: #000;
}
.record-noimg { display: grid; place-content: center; font-size: 12px; color: var(--muted); background: var(--bg); }
.record-meta { display: flex; flex-direction: column; min-width: 0; }
.record-meta b { font-size: 12.5px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.record-meta small { color: var(--muted); }
.record.marker { border-color: color-mix(in srgb, #7c3aed 55%, var(--line)); }
.record.iff-F { border-color: color-mix(in srgb, var(--F) 55%, var(--line)); }
.record.iff-H { border-color: color-mix(in srgb, var(--H) 55%, var(--line)); }
.record.iff-N { border-color: color-mix(in srgb, var(--N) 55%, var(--line)); }
.record.iff-U { border-color: color-mix(in srgb, var(--U) 55%, var(--line)); }
.record-full { width: 100%; border-radius: 8px; margin-top: 8px; }

/* 색 박스 감지 */
:root { --obj-pothole: #14b8a6; --obj-missile: #f97316; --obj-friendly: #3b82f6; --obj-enemy: #ef4444; }
.sum-cell.obj-pothole, .record.obj-pothole, .obj-chip.obj-pothole { border-color: color-mix(in srgb, var(--obj-pothole) 60%, var(--line)); }
.sum-cell.obj-missile, .record.obj-missile, .obj-chip.obj-missile { border-color: color-mix(in srgb, var(--obj-missile) 60%, var(--line)); }
.sum-cell.obj-friendly, .record.obj-friendly, .obj-chip.obj-friendly { border-color: color-mix(in srgb, var(--obj-friendly) 60%, var(--line)); }
.sum-cell.obj-enemy, .record.obj-enemy, .obj-chip.obj-enemy { border-color: color-mix(in srgb, var(--obj-enemy) 60%, var(--line)); }
.objects { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 6px; }
.obj-chip { border: 1px solid var(--line); border-radius: 999px; padding: 3px 12px; background: var(--panel); font-size: 13px; font-variant-numeric: tabular-nums; }
.list-col > h2 + .objects + p { margin-top: 0; }
.list-col h2:not(:first-child) { margin-top: 18px; }
.calib { display: block; }
.calib-row { display: flex; flex-wrap: wrap; align-items: end; gap: 10px 14px; margin-bottom: 10px; }
.calib-row label { min-width: 150px; }
.calib-row label.check { min-width: 0; }
.calib-tabs { display: flex; gap: 6px; margin-bottom: 10px; }
.calib-tabs .tab[aria-pressed="true"] { background: #2a313b; border-color: #4b5563; }
.calib-help { margin: 0; font-size: 12px; }
.stage.sampling { cursor: crosshair; outline: 2px dashed var(--accent); outline-offset: 2px; }
