:root {
  --bg: #f3f7f4;
  --panel-bg: #ffffff;
  --border: #dde5e0;
  --border-soft: #e7ece8;
  --text: #16241c;
  --muted: #5f7268;

  --primary: #1e7a4c;
  --primary-hover: #17603b;
  --primary-dark: #12442a;
  --primary-tint: #e6f5ec;
  --primary-tint-strong: #cceada;

  --danger: #b3261e;
  --confirmed: #1e9e50;
  --rejected: #d97a1e;
  --suspected: #b8912a;

  --radius: 12px;
  --radius-sm: 8px;
  --shadow-sm: 0 1px 2px rgba(18, 40, 27, 0.06);
  --shadow-md: 0 6px 20px rgba(18, 40, 27, 0.08);
  --shadow-lg: 0 16px 40px rgba(18, 40, 27, 0.16);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

::selection { background: var(--primary-tint-strong); color: var(--primary-dark); }

::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #c3d3c9; border-radius: 999px; }
::-webkit-scrollbar-thumb:hover { background: #a9bfb0; }

.view { min-height: 100vh; }

/* --- login / home screen --- */
#login-view {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 3rem;
  min-height: 100vh;
  padding: 3rem 4vw;
  background:
    linear-gradient(120deg, rgba(10, 26, 17, 0.90) 0%, rgba(14, 42, 26, 0.78) 45%, rgba(18, 68, 42, 0.42) 100%),
    url("/images/badc-hero.jpg") center / cover no-repeat fixed;
}

.login-hero { flex: 1 1 520px; max-width: 640px; color: #fff; }
.hero-eyebrow {
  margin: 0 0 0.4rem; font-size: 0.95rem; letter-spacing: 0.02em;
  color: #a9e8c4; font-weight: 600;
}
.hero-title { font-size: 2.1rem; line-height: 1.2; margin: 0 0 1rem; font-weight: 700; letter-spacing: -0.01em; }
.hero-desc {
  font-size: 0.95rem; line-height: 1.65; color: rgba(255,255,255,0.88);
  margin: 0 0 2rem; max-width: 560px;
}

.hero-features { display: grid; grid-template-columns: 1fr 1fr; gap: 0.9rem; }
@media (max-width: 640px) { .hero-features { grid-template-columns: 1fr; } }
.hero-feature {
  display: flex; gap: 0.75rem; align-items: flex-start;
  background: rgba(255,255,255,0.08); border: 1px solid rgba(255,255,255,0.16);
  border-radius: var(--radius-sm); padding: 0.85rem 1rem; backdrop-filter: blur(4px);
  transition: background 0.15s ease, border-color 0.15s ease;
}
.hero-feature:hover { background: rgba(255,255,255,0.14); border-color: rgba(255,255,255,0.28); }
.hero-feature-icon { font-size: 1.4rem; line-height: 1; }
.hero-feature strong { display: block; font-size: 0.88rem; margin-bottom: 0.2rem; }
.hero-feature p { margin: 0; font-size: 0.78rem; color: rgba(255,255,255,0.78); line-height: 1.4; }

.login-box {
  background: var(--panel-bg);
  padding: 2.5rem;
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  width: 360px;
  flex: 0 0 auto;
}
.login-box h1 { font-size: 1.2rem; margin: 0 0 0.25rem; color: var(--primary-dark); }
.login-box .subtitle { color: var(--muted); margin: 0 0 1.5rem; font-size: 0.9rem; }
.login-box label { display: block; margin-bottom: 1rem; font-size: 0.85rem; color: var(--muted); font-weight: 500; }
.login-box input {
  display: block; width: 100%; margin-top: 0.4rem; padding: 0.6rem 0.7rem;
  border: 1px solid var(--border); border-radius: var(--radius-sm); font-size: 0.95rem;
  background: #fbfdfc; transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.login-box input:focus {
  outline: none; border-color: var(--primary); background: #fff;
  box-shadow: 0 0 0 3px var(--primary-tint);
}
.login-box button {
  width: 100%; padding: 0.7rem; margin-top: 0.5rem; font-size: 0.95rem; font-weight: 600;
}
.error-text { color: var(--danger); font-size: 0.85rem; min-height: 1.2em; }
.status-text { color: var(--muted); font-size: 0.85rem; }

/* --- header / nav --- */
.app-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0.85rem 1.5rem; background: var(--panel-bg); border-bottom: 1px solid var(--border-soft);
  position: sticky; top: 0; z-index: 10; box-shadow: var(--shadow-sm);
}
.brand { display: flex; align-items: center; gap: 0.65rem; }
.brand-mark {
  display: flex; align-items: center; justify-content: center;
  width: 34px; height: 34px; border-radius: 9px; overflow: hidden;
  background: var(--primary-tint); flex: 0 0 auto;
}
.brand-mark img { width: 100%; height: 100%; object-fit: cover; display: block; }
.app-header h1 { font-size: 1.05rem; margin: 0; font-weight: 700; color: var(--primary-dark); letter-spacing: -0.01em; }
.header-right { display: flex; align-items: center; gap: 1rem; font-size: 0.85rem; color: var(--muted); }
#current-user { font-weight: 600; color: var(--text); }

/* --- system alerts banner (OpenRouter credit/rate-limit/auth problems) --- */
.system-alerts { padding: 0.6rem 1.5rem 0; background: var(--panel-bg); }
.system-alert {
  display: flex; align-items: center; gap: 0.6rem; padding: 0.7rem 1rem; margin-bottom: 0.6rem;
  border-radius: var(--radius-sm); font-size: 0.85rem; font-weight: 500;
}
.system-alert-error { background: #fee2e2; color: #7f1d1d; border: 1px solid #fca5a5; }
.system-alert-warning { background: #fef3c7; color: #78350f; border: 1px solid #fcd34d; }
.system-alert-icon { flex: 0 0 auto; }
.system-alert-text { flex: 1; line-height: 1.4; }
.system-alert-dismiss {
  background: none; border: none; box-shadow: none; color: inherit; font-size: 1.2rem;
  line-height: 1; padding: 0 0.3rem; cursor: pointer; opacity: 0.7;
}
.system-alert-dismiss:hover { opacity: 1; box-shadow: none; }

/* --- facility assignment picker (Users tab, admin-only) --- */
.stacked-form { margin-bottom: 1.1rem; }
.stacked-form .inline-form { margin-bottom: 0; }
.facility-picker { margin-top: 0.6rem; }
.facility-picker-label { display: block; font-size: 0.78rem; color: var(--muted); margin-bottom: 0.4rem; }
.facility-checkboxes { display: flex; flex-wrap: wrap; gap: 0.5rem 1rem; }
/* Specificity needs to beat .modal-body label (0,1,1) since this renders
   inside the user-edit modal too -- plain .facility-checkbox (0,1,0)
   would lose that fight and fall back to the modal's block/margin
   defaults. */
.facility-checkboxes label.facility-checkbox { display: flex; align-items: center; gap: 0.35rem; font-size: 0.82rem; font-weight: 400; margin-bottom: 0; }
.facility-checkboxes input[type="checkbox"] { display: inline-block; width: auto; flex: 0 0 auto; margin-top: 0; padding: 0; border: none; background: none; }
#user-edit-facility-checkboxes.facility-checkboxes { margin-top: 0.4rem; }

.tabs {
  display: flex; gap: 0.25rem; padding: 0.5rem 1.5rem 0; background: var(--panel-bg);
  border-bottom: 1px solid var(--border-soft);
}
.tab-btn {
  padding: 0.6rem 1rem; border: none; background: none; cursor: pointer;
  font-size: 0.88rem; color: var(--muted); font-weight: 500;
  border-radius: var(--radius-sm) var(--radius-sm) 0 0;
  border-bottom: 2px solid transparent;
  transition: background 0.15s ease, color 0.15s ease;
}
.tab-btn:hover { background: var(--primary-tint); color: var(--primary-dark); }
.tab-btn.active { color: var(--primary); border-bottom-color: var(--primary); font-weight: 700; background: var(--primary-tint); }

main { padding: 1.75rem 2rem 3rem; max-width: 1800px; margin: 0 auto; }
.tab-panel { display: none; }
.tab-panel.active { display: block; }

/* --- panels / forms --- */
.panel-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1.25rem; }
@media (max-width: 900px) { .panel-grid { grid-template-columns: 1fr; } }

.panel {
  background: var(--panel-bg); border: 1px solid var(--border-soft); border-radius: var(--radius);
  padding: 1.4rem; margin-bottom: 1.25rem; box-shadow: var(--shadow-sm);
}
.panel h2 {
  font-size: 1rem; margin: 0 0 1rem; font-weight: 700; color: var(--text);
  display: flex; align-items: center; gap: 0.5rem;
}
.panel h2::before {
  content: ""; display: inline-block; width: 4px; height: 1rem;
  border-radius: 999px; background: var(--primary);
}
.panel h3 { font-size: 0.85rem; margin: 1.5rem 0 0.7rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; font-weight: 700; }

.inline-form { display: flex; flex-wrap: wrap; gap: 0.6rem; margin-bottom: 1.1rem; }
.inline-form input, .inline-form select {
  padding: 0.55rem 0.7rem; border: 1px solid var(--border); border-radius: var(--radius-sm);
  font-size: 0.85rem; flex: 1 1 140px; background: #fbfdfc;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.inline-form input:focus, .inline-form select:focus {
  outline: none; border-color: var(--primary); background: #fff; box-shadow: 0 0 0 3px var(--primary-tint);
}

button {
  padding: 0.55rem 1.1rem; border: none; border-radius: var(--radius-sm); background: var(--primary);
  color: white; font-size: 0.85rem; font-weight: 600; cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: background 0.15s ease, transform 0.1s ease, box-shadow 0.15s ease;
}
button:hover { background: var(--primary-hover); box-shadow: var(--shadow-md); }
button:active { transform: translateY(1px); }
button.link-btn {
  background: none; color: var(--primary); padding: 0; text-decoration: none;
  font-weight: 600; box-shadow: none;
}
button.link-btn:hover { color: var(--primary-hover); text-decoration: underline; box-shadow: none; }
button.link-btn.danger-link { color: var(--danger); margin-left: 0.6rem; }
button.secondary { background: #eef2ef; color: var(--text); box-shadow: none; }
button.secondary:hover { background: #e3e9e4; }

.data-table { width: 100%; border-collapse: collapse; font-size: 0.85rem; }
.data-table th, .data-table td { text-align: left; padding: 0.65rem 0.6rem; border-bottom: 1px solid var(--border-soft); }
.data-table th {
  color: var(--muted); font-weight: 700; font-size: 0.75rem; text-transform: uppercase;
  letter-spacing: 0.03em; background: #fafcfa;
}
.data-table tbody tr { transition: background 0.12s ease; }
.data-table tbody tr:hover { background: var(--primary-tint); }
.data-table tbody tr:last-child td { border-bottom: none; }
.data-table input, .data-table select {
  padding: 0.35rem 0.5rem; border: 1px solid var(--border); border-radius: var(--radius-sm);
  font-size: 0.85rem; background: #fbfdfc; width: 100%; box-sizing: border-box;
}
.data-table input:focus, .data-table select:focus {
  outline: none; border-color: var(--primary); background: #fff; box-shadow: 0 0 0 3px var(--primary-tint);
}
.data-table .edit-btn, .data-table .save-btn, .data-table .cancel-btn { margin-right: 0.4rem; }
.data-table .icon-btn {
  background: none; border: none; padding: 0.3rem; border-radius: var(--radius-sm);
  color: var(--primary); cursor: pointer; box-shadow: none; line-height: 0;
  display: inline-flex; align-items: center; justify-content: center;
}
.data-table .icon-btn:hover { background: var(--primary-tint); color: var(--primary-hover); }
.data-table .icon-btn.danger-icon { color: var(--danger); }
.data-table .icon-btn.danger-icon:hover { background: var(--danger-tint, #fdecec); color: var(--danger); }

.status-badge {
  display: inline-block; padding: 0.2rem 0.65rem; border-radius: 999px; font-size: 0.72rem; font-weight: 700;
  letter-spacing: 0.02em;
}
.status-queued, .status-starting_gpu { background: #eef1ee; color: #4b5a51; }
.status-processing, .status-finalizing { background: #dbeafe; color: #1e40af; }
.status-completed { background: var(--primary-tint-strong); color: var(--primary-dark); }
.status-failed { background: #fee2e2; color: #b91c1c; }

/* Pulsing dot on the two "actively running" statuses, so a glance at the
   jobs table tells a user something is happening right now vs. sitting
   idle -- separate from the progress bar, which needs total_frames to be
   meaningful and isn't available in the split-second before a job opens
   its video. */
.status-processing::before, .status-finalizing::before {
  content: ""; display: inline-block; width: 6px; height: 6px; margin-right: 0.4rem;
  border-radius: 999px; background: #1e40af; vertical-align: middle;
  animation: status-pulse 1.4s ease-in-out infinite;
}
@keyframes status-pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.35; transform: scale(0.7); }
}

/* --- progress bar (jobs table row + job detail live summary) --- */
.progress-cell { display: flex; align-items: center; gap: 0.5rem; min-width: 120px; }
.progress-bar {
  flex: 1; height: 7px; border-radius: 999px; background: #e9efeb; overflow: hidden; min-width: 60px;
}
.progress-bar-fill {
  height: 100%; border-radius: 999px; background: var(--primary);
  transition: width 0.4s ease;
}
.progress-bar-fill.fill-failed { background: var(--danger); }
.progress-bar-fill.fill-indeterminate {
  width: 30% !important; background: linear-gradient(90deg, transparent, var(--primary), transparent);
  animation: progress-indeterminate 1.3s ease-in-out infinite;
}
@keyframes progress-indeterminate {
  0% { transform: translateX(-100%); }
  100% { transform: translateX(330%); }
}
.progress-pct { font-size: 0.72rem; color: var(--muted); font-weight: 600; white-space: nowrap; }

.live-progress { margin-bottom: 1rem; }
.live-progress .progress-bar { height: 9px; margin-bottom: 0.5rem; }

/* --- live activity log (job detail panel) --- */
.activity-log {
  max-height: 260px; overflow-y: auto; background: #0f1a13; border-radius: var(--radius-sm);
  padding: 0.75rem 0.9rem; font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  font-size: 0.78rem; line-height: 1.6; margin-bottom: 1rem;
}
.activity-log .status-text { color: rgba(255,255,255,0.5); }
.log-line { display: flex; gap: 0.6rem; padding: 0.1rem 0; border-bottom: 1px solid rgba(255,255,255,0.05); }
.log-line:last-child { border-bottom: none; }
.log-time { color: rgba(255,255,255,0.4); flex: 0 0 auto; }
.log-msg { color: #d7e3da; word-break: break-word; }
.log-line.log-success .log-msg { color: #7ee0a8; }
.log-line.log-warning .log-msg { color: #f0c76a; }
.log-line.log-error .log-msg { color: #ff9b93; font-weight: 600; }

.track-summary { display: flex; gap: 1.5rem; margin-bottom: 1.1rem; }
.track-summary .stat {
  text-align: center; background: #fafcfa; border: 1px solid var(--border-soft);
  border-radius: var(--radius-sm); padding: 0.75rem 1.25rem; flex: 1;
  font-size: 0.72rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.03em; font-weight: 600;
}
.track-summary .stat .num { font-size: 1.6rem; font-weight: 800; display: block; letter-spacing: normal; text-transform: none; }
.track-summary .confirmed .num { color: var(--confirmed); }
.track-summary .inbound .num { color: var(--confirmed); }
.track-summary .outbound .num { color: var(--rejected); }
.track-summary .suspected .num { color: var(--suspected); }

.track-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 1rem;
}
@media (min-width: 1100px) {
  .track-grid { grid-template-columns: repeat(5, 1fr); }
}
@media (min-width: 860px) and (max-width: 1099px) {
  .track-grid { grid-template-columns: repeat(4, 1fr); }
}
@media (min-width: 560px) and (max-width: 859px) {
  .track-grid { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 559px) {
  .track-grid { grid-template-columns: 1fr; }
}

.track-card {
  position: relative;
  border: 1px solid var(--border-soft); border-radius: var(--radius-sm); overflow: hidden; font-size: 0.75rem;
  background: var(--panel-bg);
  box-shadow: var(--shadow-sm); transition: transform 0.15s ease, box-shadow 0.15s ease;
  display: flex; flex-direction: column;
}
.track-card:hover { transform: translateY(-2px); box-shadow: var(--shadow-md); }

.track-card-img-wrap {
  position: relative;
  width: 100%;
  height: 155px;
  background: #121a15;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

.track-card img { width: 100%; height: 100%; object-fit: cover; display: block; background: #121a15; cursor: zoom-in; }
.track-card .no-img {
  width: 100%; height: 100%; display: flex; align-items: center; justify-content: center;
  color: var(--muted); font-size: 0.8rem; background: #edf2f7;
}

.frame-badge {
  position: absolute;
  top: 8px;
  left: 8px;
  background: rgba(15, 26, 19, 0.88);
  backdrop-filter: blur(4px);
  color: #ffffff;
  font-size: 0.72rem;
  font-weight: 700;
  padding: 0.2rem 0.55rem;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.35);
  border: 1px solid rgba(255, 255, 255, 0.2);
  z-index: 2;
  pointer-events: none;
  letter-spacing: 0.02em;
}

.frame-time-badge {
  position: absolute;
  top: 8px;
  right: 8px;
  background: rgba(0, 0, 0, 0.72);
  backdrop-filter: blur(4px);
  color: #d7e3da;
  font-size: 0.68rem;
  font-weight: 600;
  padding: 0.2rem 0.45rem;
  border-radius: 4px;
  z-index: 2;
  pointer-events: none;
}

.track-card .label { padding: 0.45rem 0.5rem; text-align: center; font-weight: 700; font-size: 0.76rem; }
/* engine="vlm_method2" only -- the VLM's own justification for this
   batch's entries/exits count (see InventoryEvent.vlm_reason). Absent
   (falsy) for every other engine, so this div just doesn't render. */
.track-card .vlm-reason {
  padding: 0 0.5rem 0.5rem; text-align: left; font-size: 0.68rem; color: var(--muted);
  display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden;
}
.track-card.confirmed .label { background: var(--primary-tint-strong); color: var(--primary-dark); }
.track-card.rejected .label { background: #fee2e2; color: #b91c1c; }
/* One card per INBOUND/OUTBOUND batch event (no per-person identity in
   this pipeline, see worker.py) -- inbound reuses the "confirmed" green
   (a sack counted into storage), outbound the "rejected" orange (just a
   distinct color here, not a negative judgement -- a sack leaving is a
   normal, correctly-counted event). */
.track-card.inbound .label { background: var(--primary-tint-strong); color: var(--primary-dark); }
.track-card.outbound .label { background: #fee2e2; color: #b91c1c; }

#correction-panel { font-size: 0.9rem; }
#correction-panel .expected-vs-actual { display: flex; gap: 1rem; margin-bottom: 1rem; }
#correction-panel .expected-vs-actual .box {
  text-align: center; background: #fafcfa; border: 1px solid var(--border-soft);
  border-radius: var(--radius-sm); padding: 0.75rem 1.5rem; flex: 1;
}
#correction-panel .expected-vs-actual .num { font-size: 1.6rem; font-weight: 800; color: var(--primary-dark); }

/* --- thumbnail lightbox --- */
/* Native crops are small (a person's bounding box inside a wide overhead
   CCTV frame, see track-card above) -- this enlarges the *display* only
   (image-rendering: pixelated avoids a blurry fake-smooth upscale), it
   does not change the underlying saved crop, which must stay an exact
   match to what the VLM evaluated for audit fidelity. */
.track-card img { cursor: zoom-in; }
.lightbox { position: fixed; inset: 0; z-index: 1000; display: flex; align-items: center; justify-content: center; }
.lightbox-backdrop { position: absolute; inset: 0; background: rgba(10, 20, 14, 0.78); }
.lightbox-content { position: relative; z-index: 1; max-width: 90vw; max-height: 90vh; text-align: center; }
.lightbox-content img {
  max-width: 90vw; max-height: 80vh; min-width: 200px; min-height: 200px;
  object-fit: contain; image-rendering: pixelated; background: #111; border-radius: var(--radius-sm);
  box-shadow: var(--shadow-lg);
}
.lightbox-caption { color: white; margin-top: 0.5rem; font-size: 0.85rem; }
.lightbox-close {
  position: absolute; top: -2.5rem; right: -0.5rem; background: none; color: white;
  font-size: 1.75rem; line-height: 1; padding: 0.25rem 0.6rem; box-shadow: none; border-radius: var(--radius-sm);
}
.lightbox-close:hover { background: rgba(255,255,255,0.15); box-shadow: none; }

/* --- edit modal --- */
.modal { position: fixed; inset: 0; z-index: 1000; display: flex; align-items: center; justify-content: center; padding: 1rem; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(10, 20, 14, 0.6); }
.modal-dialog {
  position: relative; z-index: 1; background: var(--panel-bg); border-radius: var(--radius);
  box-shadow: var(--shadow-lg); width: 100%; max-width: 460px; overflow: hidden;
}
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 1rem 1.4rem; border-bottom: 1px solid var(--border-soft);
}
.modal-header h2 { margin: 0; font-size: 1rem; font-weight: 700; color: var(--primary-dark); }
.modal-close {
  background: none; border: none; color: var(--muted); font-size: 1.5rem; line-height: 1;
  padding: 0.1rem 0.4rem; cursor: pointer; box-shadow: none; border-radius: var(--radius-sm);
}
.modal-close:hover { background: var(--primary-tint); color: var(--text); box-shadow: none; }
.modal-body { padding: 1.4rem; }
.modal-body label { display: block; margin-bottom: 1rem; font-size: 0.85rem; color: var(--muted); font-weight: 500; }
.modal-body label:last-child { margin-bottom: 0; }
.modal-body input, .modal-body select {
  display: block; width: 100%; margin-top: 0.4rem; padding: 0.6rem 0.7rem;
  border: 1px solid var(--border); border-radius: var(--radius-sm); font-size: 0.95rem;
  background: #fbfdfc; transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.modal-body input:focus, .modal-body select:focus {
  outline: none; border-color: var(--primary); background: #fff; box-shadow: 0 0 0 3px var(--primary-tint);
}
.modal-footer {
  display: flex; justify-content: flex-end; gap: 0.6rem;
  padding: 1rem 1.4rem; border-top: 1px solid var(--border-soft); background: #fafcfa;
}
