:root {
  color-scheme: dark;
  --bg: #0f1316;
  --ink: #f3f6f2;
  --muted: #aab5b7;
  --line: #334047;
  --accent: #28a66f;
  --surface: #1a2227;
  --surface-2: #253039;
  --danger: #ff8a80;
  --shadow: 0 2px 14px rgba(0, 0, 0, 0.34);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 17px;
}

a { color: inherit; }
a[href] { cursor: pointer; }

button,
a[role="button"] {
  min-height: 48px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-2);
  color: var(--ink);
  font: inherit;
  font-weight: 800;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 14px;
  cursor: pointer;
}

button:disabled,
a[role="button"][aria-disabled="true"] {
  cursor: not-allowed;
  opacity: 0.55;
}

button.is-busy,
a[role="button"].is-busy {
  pointer-events: none;
}

button.is-busy::before,
a[role="button"].is-busy::before {
  content: "";
  width: 16px;
  height: 16px;
  margin-right: 8px;
  border: 2px solid currentColor;
  border-top-color: transparent;
  border-radius: 999px;
  animation: rewind-spin 700ms linear infinite;
}

@keyframes rewind-spin {
  to { transform: rotate(360deg); }
}

h1, h2, p { margin: 0; }
h1 { font-size: 26px; line-height: 1.1; }
h2 { font-size: 20px; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) 14px 10px;
  background: rgba(15, 19, 22, 0.96);
  border-bottom: 1px solid var(--line);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  min-width: 0;
  font-size: 24px;
  font-weight: 900;
  text-decoration: none;
}

.brand img {
  width: 28px;
  height: 36px;
  object-fit: contain;
  flex: 0 0 auto;
}

.brand span { white-space: nowrap; }
.brand { cursor: pointer; }

nav { display: flex; gap: 12px; }
nav a { color: var(--muted); font-weight: 800; text-decoration: none; }

.logout-form { margin: 0; }
.logout-form input { display: none; }
.logout-btn { width: 48px; padding: 0; }
.logout-btn svg {
  width: 24px;
  height: 24px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.icon-btn { width: 48px; padding: 0; font-size: 28px; }

.controls {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
  padding: 12px;
}

label { display: grid; gap: 6px; font-weight: 800; }

input, select {
  width: 100%;
  min-height: 50px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  font: inherit;
  background: var(--surface-2);
  color: var(--ink);
}

select,
input[type="date"],
input[type="search"],
input[type="checkbox"],
input[type="radio"] {
  cursor: pointer;
}

select { font-weight: 850; }

.day-timeline {
  position: sticky;
  top: 69px;
  z-index: 9;
  display: grid;
  gap: 6px;
  margin: 0 12px 12px;
  padding: 8px 0;
  background: rgba(15, 19, 22, 0.96);
}

.timeline-head,
.timeline-scale {
  display: flex;
  justify-content: space-between;
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

.timeline-head span:first-child {
  color: var(--ink);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.day-strip {
  position: relative;
  height: 42px;
  overflow-x: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: linear-gradient(90deg, transparent 0 24.7%, var(--line) 24.7% 25%, transparent 25% 49.7%, var(--line) 49.7% 50%, transparent 50% 74.7%, var(--line) 74.7% 75%, transparent 75%);
}

.tick {
  position: absolute;
  top: 5px;
  width: 8px;
  height: 30px;
  min-height: 0;
  padding: 0;
  border: 0;
  border-radius: 999px;
  background: var(--accent);
  transform: translateX(-4px);
  cursor: pointer;
}

.tick-person, .badge-person { background: #7cc7ff; color: #061622; }
.tick-car, .badge-car { background: #33d17a; color: #06180d; }
.tick-truck, .badge-truck { background: #74d680; color: #071909; }
.tick-bus, .badge-bus { background: #b7e36b; color: #172000; }
.tick-motorcycle, .badge-motorcycle { background: #ff9f43; color: #241000; }
.tick-bicycle, .badge-bicycle { background: #f7d046; color: #241c00; }
.tick-dog, .badge-dog, .tick-cat, .badge-cat, .tick-animal, .badge-animal { background: #ffd166; color: #241a00; }
.tick-other, .badge-other { background: #c7a6ff; color: #170b2c; }
.tick-lens-vehicle {
  top: 14px;
  width: 14px;
  height: 14px;
  border-radius: 1px;
  background: #c9ced3;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.38);
  transform: translateX(-7px);
  z-index: 1;
}
.tick-plate {
  top: 14px;
  width: 14px;
  height: 14px;
  border-radius: 1px;
  background: #ffe100;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.45);
  transform: translateX(-7px);
  z-index: 2;
}

.timeline {
  display: grid;
  gap: 14px;
  padding: 0 12px 28px;
}

.event {
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
  transition: border-color 160ms ease, box-shadow 160ms ease;
}

.event-highlight {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(40, 166, 111, 0.35), var(--shadow);
}

.event-open {
  display: block;
  width: 100%;
  height: auto;
  min-height: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  text-align: left;
  cursor: pointer;
}

.event img,
.placeholder {
  display: grid;
  place-items: center;
  width: 100%;
  aspect-ratio: 16 / 9;
  object-fit: cover;
  background: #05080a;
  color: var(--muted);
  font-weight: 800;
}

.event-body { padding: 12px; }
.event-title { display: flex; justify-content: space-between; gap: 10px; font-size: 22px; font-weight: 850; }
.badge { flex: 0 0 auto; align-self: start; border-radius: 999px; padding: 4px 9px; font-size: 13px; text-transform: uppercase; }
.subtitle, .meta-row, .empty { color: var(--muted); }
.subtitle { margin-top: 4px; }
.lens-summary, .lens-description { margin-top: 8px; color: var(--ink); font-size: 15px; font-weight: 750; overflow-wrap: anywhere; }
.lens-description { color: var(--muted); font-size: 13px; line-height: 1.25; }
.lens-badges { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.lens-badge { border-radius: 4px; padding: 4px 8px; background: var(--surface-2); color: var(--ink); font-size: 15px; font-weight: 900; line-height: 1.15; }
.lens-badge-plate {
  border-radius: 1px;
  padding: 4px 9px;
  background: #ffe100;
  color: #050505;
  border: 1px solid #050505;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.45);
  font-family: "Courier New", Courier, ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 15px;
  font-weight: 900;
  letter-spacing: 0;
}
.lens-badge-face { background: #7cc7ff; color: #061622; }
.lens-badge-vehicle {
  border-radius: 1px;
  background: #d2d6da;
  color: #101417;
  font-size: 13px;
  font-weight: 850;
}
.meta-row { display: flex; flex-wrap: wrap; gap: 8px 14px; margin-top: 10px; font-size: 15px; font-weight: 750; }
.empty { padding: 30px 12px; text-align: center; }

dialog {
  width: min(96vw, 980px);
  max-height: 92vh;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0;
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 12px 44px rgba(0, 0, 0, 0.45);
}

dialog::backdrop { background: rgba(0, 0, 0, 0.88); }
.dialog-head { display: flex; justify-content: space-between; gap: 8px; padding: 12px; }
.player { width: 100%; aspect-ratio: 16 / 9; max-height: 62vh; background: #05080a; display: block; object-fit: contain; }
.player-loading { opacity: 0; }
.segment-list { display: grid; gap: 8px; padding: 12px; }
.segment-row { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 8px; }
.segment-row-combined { align-items: center; padding-top: 8px; border-top: 1px solid var(--line); }
.segment-row-combined span { color: var(--muted); font-weight: 850; }
.segment-row button.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.modal-meta-list { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 8px 12px; padding: 0 12px 14px; margin: 0; }
.modal-meta-list dt { color: var(--muted); font-weight: 800; }
.modal-meta-list dd { margin: 0; overflow-wrap: anywhere; }
.status-list { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 8px 12px; margin: 12px 0 0; }
.status-list dt { color: var(--muted); font-weight: 800; }
.status-list dd { margin: 0; overflow-wrap: anywhere; }
.gallery-panel { display: grid; gap: 10px; padding: 0 12px 14px; }
.gallery-panel[hidden] { display: none; }
.gallery-panel h3 { margin: 0; font-size: 16px; }
.image-gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(142px, 1fr)); gap: 10px; }
.gallery-card { position: relative; overflow: hidden; border: 1px solid var(--line); border-radius: 8px; background: #10171b; }
.gallery-open { display: block; width: 100%; min-height: 0; padding: 0; border: 0; border-radius: 0; background: transparent; cursor: zoom-in; }
.gallery-open img { display: block; width: 100%; aspect-ratio: 4 / 3; object-fit: cover; background: #05080a; }
.gallery-download {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 38px;
  min-height: 38px;
  padding: 0;
  border-radius: 999px;
  background: rgba(5, 8, 10, 0.78);
  border-color: rgba(255, 255, 255, 0.25);
  font-size: 20px;
  cursor: pointer;
}
.gallery-caption { display: grid; gap: 3px; padding: 8px; }
.gallery-caption span { color: var(--muted); font-size: 13px; }
.image-dialog { width: min(96vw, 1120px); }
.image-viewer { display: block; width: 100%; max-height: 72vh; object-fit: contain; background: #05080a; }
.image-dialog-actions { display: grid; grid-template-columns: 1fr auto 1fr; gap: 8px; padding: 12px; }
.image-dialog-actions button:disabled { opacity: 0.45; }

.advanced-page { display: grid; gap: 14px; padding-bottom: 28px; }
.advanced-panel { display: grid; gap: 12px; padding: 12px; border-bottom: 1px solid var(--line); background: rgba(15, 19, 22, 0.72); }
.advanced-form { display: grid; grid-template-columns: minmax(0, 0.8fr) minmax(0, 0.8fr) minmax(0, 1.4fr) minmax(0, 1fr) minmax(0, 1fr) auto; gap: 10px; align-items: end; }
.advanced-form button { min-width: 120px; background: var(--surface-2); border-color: var(--line); }
.advanced-form button:not(:disabled) { background: var(--accent); border-color: var(--accent); color: #fff; }
.advanced-actions { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; justify-content: space-between; }
.advanced-actions form { margin: 0; }
.advanced-results { padding-top: 0; }

.detail, .status { display: grid; gap: 14px; width: min(100%, 980px); margin: 0 auto; padding: 12px; }
.panel, .table-wrap { border: 1px solid var(--line); border-radius: 8px; background: var(--surface); padding: 14px; box-shadow: var(--shadow); }
.metric { font-size: 48px; font-weight: 900; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 10px; border-bottom: 1px solid var(--line); text-align: left; }
th { color: var(--muted); font-size: 14px; text-transform: uppercase; }

.login-page { display: grid; place-items: center; padding: 20px; }
.login-box { width: min(100%, 420px); background: var(--surface); border: 1px solid var(--line); border-radius: 8px; box-shadow: var(--shadow); padding: 22px; }
.login-box form { display: grid; gap: 14px; margin-top: 20px; }
.login-box button { background: var(--accent); border-color: var(--accent); color: #fff; }
.error { padding: 10px; border: 1px solid var(--danger); border-radius: 8px; color: var(--danger); font-weight: 800; }

@media (min-width: 760px) {
  .timeline { grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); }
}

@media (max-width: 700px) {
  .controls, .advanced-form { grid-template-columns: 1fr; }
  .advanced-actions { display: grid; justify-content: stretch; }
  .advanced-actions form, .advanced-actions button { width: 100%; }
  .event-title { font-size: 20px; }
}
