:root {
  --bg: #0b0e14;
  --panel: #131822;
  --panel-2: #1a2029;
  --line: #232b38;
  --fg: #e6e9ef;
  --dim: #8b95a7;
  --accent: #4aa3ff;
  --ok: #35d07f;
  --warn: #f0b429;
  --bad: #ff5c5c;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font: 14px/1.55 -apple-system, "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
}

body { display: flex; flex-direction: column; min-height: 100vh; }

header {
  display: flex; align-items: center; justify-content: space-between;
  gap: 16px; padding: 12px 18px;
  background: var(--panel); border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand h1 { font-size: 16px; margin: 0; font-weight: 600; letter-spacing: .2px; }
.brand .ver {
  color: var(--dim); font: 11px var(--mono); white-space: nowrap;
  padding: 1px 8px; border: 1px solid var(--line); border-radius: 999px;
  background: var(--panel-2);
}
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--dim); flex: none; }
.dot.ok { background: var(--ok); box-shadow: 0 0 8px var(--ok); }
.dot.bad { background: var(--bad); box-shadow: 0 0 8px var(--bad); }

.pills { display: flex; gap: 8px; flex-wrap: wrap; }
.pill {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 4px 10px; border-radius: 999px;
  background: var(--panel-2); border: 1px solid var(--line);
  font-size: 12px; color: var(--dim);
}
.pill i { width: 7px; height: 7px; border-radius: 50%; background: var(--dim); }
.pill b { color: var(--fg); font-weight: 600; font-family: var(--mono); font-size: 11px; }
.pill.ok i { background: var(--ok); } .pill.ok b { color: var(--ok); }
.pill.bad i { background: var(--bad); } .pill.bad b { color: var(--bad); }

.metrics {
  display: grid; gap: 1px; background: var(--line);
  grid-template-columns: repeat(auto-fit, minmax(112px, 1fr));
  border-bottom: 1px solid var(--line);
}
.m { background: var(--panel); padding: 8px 12px; }
.m label { display: block; color: var(--dim); font-size: 11px; margin-bottom: 2px; }
.m b { font-family: var(--mono); font-size: 17px; font-weight: 600; }
.m span { color: var(--dim); font-size: 11px; margin-left: 3px; }

/* One column per site. --cols is set from JS to the number of configured
   stations, so two sites sit side by side and a third just widens the grid. */
.sites {
  flex: 1; display: grid; gap: 12px; padding: 12px 18px;
  grid-template-columns: repeat(var(--cols, 2), minmax(0, 1fr));
  grid-template-rows: minmax(0, 1fr);
  min-height: 0;
}

.site {
  display: flex; flex-direction: column; gap: 12px;
  min-height: 0; min-width: 0;
}
.site .video-panel { flex: 1 1 58%; }
.site .feed-panel { flex: 1 1 42%; }
/* An offline site stays readable but visibly dimmer than a live one. */
.site.off .video-panel, .site.off .feed-panel { opacity: .72; }
.site.off .site-head h2 { color: var(--dim); }

.site-head h2 { display: flex; align-items: center; gap: 8px; }
.site-head .hint { font-weight: 400; }

.site-metrics { border-bottom: none; border-top: 1px solid var(--line); }

.panel {
  background: var(--panel); border: 1px solid var(--line);
  border-radius: 10px; overflow: hidden; display: flex; flex-direction: column;
  min-height: 0;
}
.panel-head {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 9px 12px; background: var(--panel-2); border-bottom: 1px solid var(--line);
}
.panel-head h2 { margin: 0; font-size: 13px; font-weight: 600; }
.count {
  font-family: var(--mono); font-size: 11px; color: var(--dim);
  background: var(--bg); padding: 1px 7px; border-radius: 999px; margin-left: 4px;
}
.tools { display: flex; align-items: center; gap: 10px; }
.hint { color: var(--dim); font-size: 11px; font-family: var(--mono); }
.btn {
  background: var(--panel); color: var(--fg); border: 1px solid var(--line);
  border-radius: 6px; padding: 3px 10px; font-size: 12px; cursor: pointer;
  font-family: inherit;
}
.btn:hover { border-color: var(--accent); color: var(--accent); }
.btn.on { background: var(--accent); border-color: var(--accent); color: #06121f; }
.chk { display: inline-flex; align-items: center; gap: 5px; color: var(--dim); font-size: 12px; cursor: pointer; }
.chk input { accent-color: var(--accent); }

.video-wrap {
  position: relative; background: #000; flex: 1; min-height: 0;
  display: flex; align-items: center; justify-content: center;
}
.video { display: block; width: 100%; height: 100%; object-fit: contain; }
/* Critical-focus aid: nearest-neighbour 2.5x centre zoom so blur vs. sharp
   edges is obvious while the lens barrel is being turned. */
.video-wrap.zoom .video {
  transform: scale(2.5);
  transform-origin: 50% 50%;
  image-rendering: pixelated;
}
.video-overlay {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  background: rgba(6, 9, 14, .88); color: var(--dim); font-size: 13px;
  transition: opacity .25s; pointer-events: none;
}
.video-overlay.hide { opacity: 0; }

.focusbar {
  display: flex; align-items: center; gap: 10px;
  padding: 7px 12px; background: var(--panel-2); border-top: 1px solid var(--line);
  font-size: 11px; color: var(--dim); flex-wrap: wrap;
}
.focusbar label { flex: none; }
.focusbar b.sharp { font-family: var(--mono); font-size: 16px; min-width: 44px; text-align: right; }
.focusbar .bar {
  position: relative; flex: 1; min-width: 90px; height: 6px;
  background: var(--bg); border-radius: 3px; overflow: hidden;
}
.focusbar .bar i {
  position: absolute; inset: 0 auto 0 0; width: 0%;
  background: linear-gradient(90deg, var(--bad), var(--warn) 45%, var(--ok));
  transition: width .3s;
}
.focusbar .bar u {
  position: absolute; top: -2px; bottom: -2px; width: 2px; background: var(--fg);
  opacity: .8; left: 0%;
}
.focusbar .peak b { font-family: var(--mono); color: var(--fg); }
.focusbar .hint2 { color: var(--dim); font-size: 10.5px; }

.sharp-bad { color: var(--bad) !important; }
.sharp-warn { color: var(--warn) !important; }
.sharp-ok { color: var(--ok) !important; }

/* Alert snapshot: up to three frames (before / analysed / after) beside the
   text, each clickable for the full frame. */
.alert-body { display: flex; gap: 10px; align-items: flex-start; }
.alert-main { flex: 1; min-width: 0; }
.thumbs { display: flex; gap: 6px; flex: none; flex-wrap: wrap; }
.thumbbox { margin: 0; display: flex; flex-direction: column; gap: 2px; }
.thumbbox figcaption {
  font: 10px var(--mono); color: var(--dim); text-align: center;
  white-space: nowrap;
}
.thumb {
  flex: none; width: 96px; height: 54px; object-fit: cover;
  border-radius: 5px; border: 1px solid rgba(255, 92, 92, .5);
  cursor: zoom-in; background: #000;
}
.thumb:hover { border-color: var(--bad); filter: brightness(1.15); }

.lightbox {
  position: fixed; inset: 0; z-index: 60; cursor: zoom-out;
  background: rgba(2, 4, 8, .88); backdrop-filter: blur(3px);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 28px;
}
.lightbox[hidden] { display: none; }
.lightbox img {
  max-width: 86vw; max-height: 84vh; border-radius: 8px;
  border: 1px solid var(--line); box-shadow: 0 24px 80px rgba(0, 0, 0, .6);
}
.lightbox .lb-cap { margin-top: 10px; color: var(--dim); font: 11px var(--mono); }
/* Gallery navigation: ← → cycle through the alert's prev/cur/next frames.
   Buttons swallow their own clicks (app.js) so tapping them never closes. */
.lb-nav {
  position: absolute; top: 50%; transform: translateY(-50%);
  width: 52px; height: 84px; border: 1px solid var(--line);
  border-radius: 8px; background: rgba(16, 22, 34, .72);
  color: var(--dim); font: 300 34px/1 var(--mono); cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: background .15s, color .15s; user-select: none;
}
.lb-nav:hover { background: rgba(40, 52, 74, .9); color: #fff; }
.lb-prev { left: 18px; }
.lb-next { right: 18px; }
.lb-count {
  position: absolute; top: 16px; right: 20px;
  color: var(--dim); font: 11px var(--mono);
  background: rgba(16, 22, 34, .72); border: 1px solid var(--line);
  padding: 3px 10px; border-radius: 5px;
}

.services, .chips {
  display: flex; flex-wrap: wrap; gap: 6px; padding: 8px 12px;
  border-top: 1px solid var(--line); background: var(--panel-2);
  align-items: center;
}
.svc {
  display: inline-flex; align-items: center; gap: 5px;
  font-size: 11px; font-family: var(--mono); color: var(--dim);
  background: var(--panel); border: 1px solid var(--line);
  padding: 2px 8px; border-radius: 5px;
}
.svc i { width: 6px; height: 6px; border-radius: 50%; background: var(--dim); }
.svc.ok i { background: var(--ok); } .svc.ok { color: var(--fg); }
.svc.bad i { background: var(--bad); } .svc.bad { color: var(--bad); }
/* Linked but producing nothing yet: the model may simply be mid-inference. */
.svc.warn i { background: var(--warn); } .svc.warn { color: var(--warn); }

.chips { border-top: none; border-bottom: 1px solid var(--line); }
.chips .hint.grow {
  flex: 1; text-align: right; min-width: 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.badge-alert {
  font-size: 10px; font-family: var(--mono); color: #fff; background: var(--bad);
  padding: 1px 7px; border-radius: 999px; white-space: nowrap;
}
.badge-alert[hidden] { display: none; }

.feed {
  overflow-y: auto; padding: 10px 12px; display: flex; flex-direction: column; gap: 9px;
  flex: 1; min-height: 0;
  scrollbar-width: thin; scrollbar-color: var(--line) transparent;
}
.feed::-webkit-scrollbar { width: 8px; }
.feed::-webkit-scrollbar-thumb { background: var(--line); border-radius: 4px; }
.empty { color: var(--dim); text-align: center; padding: 40px 0; font-size: 13px; }

.item {
  background: var(--panel-2); border: 1px solid var(--line);
  border-left: 2px solid var(--accent); border-radius: 7px; padding: 8px 11px;
  animation: in .25s ease-out;
}
.item.skip { border-left-color: var(--line); opacity: .55; }
.item.err { border-left-color: var(--bad); }

/* Alert entries: unmistakably red. */
.item.alert {
  border-left-color: var(--bad); border-color: rgba(255, 92, 92, .45);
  background: rgba(255, 92, 92, .10);
}
.item.alert .txt { color: #ff8a8a; font-weight: 600; }
.item.alert .t { color: var(--bad); }
/* A normal analysis that tripped a rule keeps its text but is flagged red. */
.item.triggered { border-left-color: var(--bad); }
.badge {
  font-size: 10px; font-family: var(--mono); color: #fff; background: var(--bad);
  padding: 1px 7px; border-radius: 999px; margin-left: auto; white-space: nowrap;
}
/* Per-analysis verdict pill: red 告警 / amber 待复核 / green 正常 / grey 无人. */
.top .badge + .badge { margin-left: 6px; }
.badge.verdict { background: #3a4552; }
.badge.verdict-abnormal { background: var(--bad); }
.badge.verdict-undetermined { background: #8a6d1f; }
.badge.verdict-normal { background: #2f7d4f; }
.badge.verdict-none { background: #3a4552; }

.pill.bad-alert i { background: var(--bad); animation: blink 1s infinite; }
.pill.bad-alert b { color: var(--bad); }
@keyframes blink { 50% { opacity: .35; } }

.modal {
  position: fixed; inset: 0; z-index: 50; background: rgba(4, 7, 12, .66);
  display: flex; align-items: center; justify-content: center; padding: 24px;
}
.modal[hidden] { display: none; }
.modal-box {
  width: min(760px, 100%); max-height: 86vh; display: flex; flex-direction: column;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  box-shadow: 0 24px 64px rgba(0, 0, 0, .5);
}
.modal-head, .modal-foot {
  display: flex; align-items: center; gap: 12px; padding: 11px 14px;
  background: var(--panel-2);
}
.modal-head { border-bottom: 1px solid var(--line); border-radius: 12px 12px 0 0; }
.modal-head h2 { margin: 0; font-size: 14px; }
.modal-head .hint { flex: 1; color: var(--dim); font-size: 11px; }
.modal-foot { border-top: 1px solid var(--line); border-radius: 0 0 12px 12px; justify-content: flex-end; }
.modal-foot .hint { flex: 1; color: var(--warn); font-size: 11px; }
.modal-body { overflow-y: auto; padding: 14px; display: flex; flex-direction: column; gap: 12px; }

.rule-card {
  border: 1px solid var(--line); border-radius: 9px; padding: 10px 12px;
  background: var(--panel-2); display: flex; flex-direction: column; gap: 8px;
}
.rule-card.off { opacity: .55; }
.rule-top { display: flex; align-items: center; gap: 9px; }
.rule-top input[name="name"] { flex: 1; }
.rule-grid { display: grid; grid-template-columns: 88px 1fr; gap: 6px 10px; align-items: center; }
.rule-grid label { color: var(--dim); font-size: 11px; text-align: right; }
.rule-grid input, .rule-grid textarea, .rules-test textarea {
  background: var(--bg); color: var(--fg); border: 1px solid var(--line);
  border-radius: 6px; padding: 4px 8px; font: 12px/1.5 var(--mono); width: 100%;
}
.rule-grid input[name="cooldown"] { width: 76px; }
.rule-msg { color: var(--bad); font-size: 12px; font-family: var(--mono); }
.btn.danger { color: var(--bad); border-color: rgba(255, 92, 92, .4); }
.btn.danger:hover { background: var(--bad); color: #fff; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #06121f; font-weight: 600; }

.rules-test { border-top: 1px dashed var(--line); padding-top: 11px; display: flex; flex-direction: column; gap: 7px; }
.rules-test label { color: var(--dim); font-size: 11px; }
.rules-test .row { display: flex; gap: 8px; }
.test-out { font-size: 12px; font-family: var(--mono); min-height: 18px; }
.test-out .hit { color: var(--bad); }
.test-out .miss { color: var(--dim); }
.test-out .gate { color: var(--dim); font-size: 11px; margin-bottom: 4px; }
.test-out .evi { color: var(--fg); opacity: .85; font-size: 11px; }
@keyframes in { from { opacity: 0; transform: translateY(-5px); } to { opacity: 1; transform: none; } }

.item .top { display: flex; align-items: baseline; gap: 8px; margin-bottom: 3px; }
.item .t { font-family: var(--mono); font-size: 11px; color: var(--accent); flex: none; }
.item .tag { font-size: 10px; color: var(--dim); font-family: var(--mono); }
.item .txt { font-size: 14px; line-height: 1.6; word-break: break-word; }
.item .meta {
  margin-top: 5px; font-family: var(--mono); font-size: 10.5px; color: var(--dim);
  display: none; flex-wrap: wrap; gap: 3px 10px;
}
body.show-meta .item .meta { display: flex; }

footer {
  display: flex; justify-content: space-between; gap: 14px;
  padding: 7px 18px; border-top: 1px solid var(--line); background: var(--panel);
  color: var(--dim); font-size: 11px; font-family: var(--mono); flex-wrap: wrap;
}
footer #foot-msg { color: var(--warn); }

/* Below this width two videos side by side are too small to judge focus, so
   the columns stack and the page scrolls instead. */
@media (max-width: 1100px) {
  .sites {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: none;
    grid-auto-rows: minmax(480px, auto);
    overflow-y: auto;
  }
  .site { min-height: 480px; }
}

@media (max-width: 940px) {
  .video-wrap { flex: none; }
  .video { height: auto; }
  header { padding: 10px 12px; }
  .sites { padding: 10px 12px; }
}

/* history entries: seeded from the site's permanent analysis archive */
.item.hist { opacity: .88; border-left-color: #556; }
.item.hist .histb { background: #445; color: #cdd; }
.hthumb { width: 112px; border-radius: 4px; margin-top: 5px; cursor: zoom-in;
  display: block; background: #222; }
