:root {
  --bg: #f4f6f9;
  --panel: #ffffff;
  --panel-2: #f0f3f7;
  --border: #dde3ea;
  --text: #16202c;
  --muted: #637083;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --ok: #15803d;
  --warn: #b45309;
  --bad: #c62828;
  --alert-bg: #fdecec;
  --allow-bg: #e8f6ee;
  --plate-bg: #fff8d6;
  --plate-text: #111111;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --radius: 10px;
  --mono: ui-monospace, "Cascadia Mono", "Segoe UI Mono", Consolas, monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e141b;
    --panel: #151d27;
    --panel-2: #1b2531;
    --border: #273342;
    --text: #e6edf5;
    --muted: #93a1b3;
    --accent: #3b82f6;
    --ok: #34c77b;
    --warn: #f0a33a;
    --bad: #f26b6b;
    --alert-bg: #3a1c1f;
    --allow-bg: #16301f;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
a { color: var(--accent); text-decoration: none; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--bad); min-height: 1.2em; margin: 4px 0; }
.mono { font-family: var(--mono); }
h1 { font-size: 20px; margin: 0; }
h2 { font-size: 16px; margin: 0; }
h3 { font-size: 14px; margin: 0 0 8px; }

/* ---------- brand + top bar ---------- */
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 16px; white-space: nowrap; }
.brand-lg { font-size: 22px; justify-content: center; }
.logo {
  width: 26px; height: 26px; border-radius: 7px; background: var(--accent); position: relative; flex: none;
}
.logo::after {
  content: ""; position: absolute; inset: 7px; border: 3px solid #fff; border-radius: 50%;
}
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 24px;
  padding: 0 20px; height: 56px;
  background: var(--panel); border-bottom: 1px solid var(--border);
}
#nav { display: flex; gap: 2px; overflow-x: auto; flex: 1; }
#nav a {
  color: var(--muted); padding: 8px 12px; border-radius: 8px; white-space: nowrap; font-weight: 500;
}
#nav a:hover { background: var(--panel-2); color: var(--text); }
#nav a.active { background: var(--panel-2); color: var(--text); box-shadow: inset 0 -2px 0 var(--accent); }
.topbar-right { display: flex; align-items: center; gap: 12px; white-space: nowrap; }
main { padding: 20px; max-width: 1500px; margin: 0 auto; }

/* ---------- login ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 360px; background: var(--panel); border: 1px solid var(--border);
  border-radius: 14px; padding: 28px; box-shadow: var(--shadow); display: grid; gap: 12px; text-align: left;
}
.login-card p.muted { text-align: center; margin: 0 0 8px; }

/* ---------- controls ---------- */
label { display: grid; gap: 4px; font-weight: 500; font-size: 13px; }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--panel); border: 1px solid var(--border);
  border-radius: 8px; padding: 8px 10px; min-width: 0;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
input[type="checkbox"] { width: 16px; height: 16px; padding: 0; accent-color: var(--accent); }
label.check { display: flex; align-items: center; gap: 8px; font-weight: 500; }
.btn {
  font: inherit; font-weight: 600; cursor: pointer; border-radius: 8px; padding: 8px 14px;
  border: 1px solid var(--border); background: var(--panel); color: var(--text);
  display: inline-flex; align-items: center; gap: 6px; white-space: nowrap;
}
.btn:hover { background: var(--panel-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--bad); }
.btn.ghost { background: transparent; }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.icon-btn { background: none; border: 0; color: var(--muted); font-size: 24px; line-height: 1; cursor: pointer; padding: 4px 8px; }
.icon-btn:hover { color: var(--text); }

/* ---------- layout pieces ---------- */
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.panel { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.panel-pad { padding: 16px; }
.toolbar { display: flex; gap: 10px; align-items: end; flex-wrap: wrap; padding: 14px 16px; }
.toolbar label { min-width: 140px; }
.grid-form { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px 16px; }
.grid-form .full { grid-column: 1 / -1; }
.form-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
.hint { font-size: 12px; color: var(--muted); font-weight: 400; }
.empty { padding: 40px 16px; text-align: center; color: var(--muted); }

/* ---------- KPI tiles ---------- */
.kpis { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi { padding: 14px 16px; }
.kpi .label { color: var(--muted); font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.kpi .value { font-size: 26px; font-weight: 700; margin-top: 2px; font-variant-numeric: tabular-nums; }
.kpi .value small { font-size: 14px; color: var(--muted); font-weight: 500; }
.kpi.alert .value { color: var(--bad); }

/* ---------- plates ---------- */
.plate {
  display: inline-block; font-family: var(--mono); font-weight: 700; letter-spacing: .06em;
  background: var(--plate-bg); color: var(--plate-text); border: 2px solid #1f2937;
  border-radius: 6px; padding: 1px 8px; white-space: nowrap;
}
.plate.lg { font-size: 30px; padding: 2px 14px; border-width: 3px; }
.badge {
  display: inline-block; font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 999px;
  text-transform: uppercase; letter-spacing: .03em; background: var(--panel-2); color: var(--muted);
}
.badge.alert { background: var(--bad); color: #fff; }
.badge.allow { background: var(--ok); color: #fff; }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--muted); margin-right: 6px; vertical-align: middle; }
.dot.ok { background: var(--ok); }
.dot.warn { background: var(--warn); }
.dot.bad { background: var(--bad); }

/* ---------- live feed ---------- */
.live-layout { display: grid; grid-template-columns: minmax(0, 1.1fr) minmax(0, 1fr); gap: 16px; align-items: start; }
.latest { padding: 16px; position: sticky; top: 72px; }
.latest .full { width: 100%; aspect-ratio: 16 / 9; object-fit: contain; background: #000; border-radius: 8px; display: block; }
.latest .row { display: flex; align-items: center; gap: 16px; margin-top: 14px; flex-wrap: wrap; }
.latest .crop { height: 64px; border-radius: 6px; background: #000; }
.latest dl { display: grid; grid-template-columns: auto 1fr; gap: 4px 14px; margin: 14px 0 0; }
.latest dt { color: var(--muted); }
.latest dd { margin: 0; }
.feed { overflow: hidden; }
.feed-item {
  display: grid; grid-template-columns: 110px minmax(0, 1fr) auto; gap: 12px; align-items: center;
  padding: 10px 14px; border-bottom: 1px solid var(--border); cursor: pointer;
}
.feed-item:last-child { border-bottom: 0; }
.feed-item:hover { background: var(--panel-2); }
.feed-item.alert { background: var(--alert-bg); }
.feed-item.allow { background: var(--allow-bg); }
.feed-item.new { animation: flash 1.2s ease-out; }
@keyframes flash { from { box-shadow: inset 4px 0 0 var(--accent); background: var(--panel-2); } }
.thumb { width: 110px; height: 40px; object-fit: contain; background: #000; border-radius: 4px; display: block; }
.thumb.none { display: grid; place-items: center; color: var(--muted); font-size: 11px; background: var(--panel-2); }
.feed-meta { min-width: 0; }
.feed-meta .sub { color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.feed-right { text-align: right; font-size: 12px; color: var(--muted); white-space: nowrap; }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 14px; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; background: var(--panel-2); white-space: nowrap; }
tbody tr:last-child td { border-bottom: 0; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: var(--panel-2); }
tr.alert td { background: var(--alert-bg); }
td.actions { text-align: right; white-space: nowrap; }
.pager { display: flex; justify-content: space-between; align-items: center; padding: 10px 14px; border-top: 1px solid var(--border); }

/* ---------- camera grid ---------- */
.cam-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 16px; }
.cam { overflow: hidden; display: flex; flex-direction: column; }
.cam-img { position: relative; aspect-ratio: 16 / 9; background: #000; }
.cam-img img { width: 100%; height: 100%; object-fit: cover; display: block; }
.cam-img .noimg { position: absolute; inset: 0; display: grid; place-items: center; color: #8793a3; font-size: 13px; }
.cam-img .state {
  position: absolute; left: 10px; top: 10px; background: rgba(0, 0, 0, .65); color: #fff;
  font-size: 12px; font-weight: 600; padding: 3px 9px; border-radius: 999px;
}
.cam-body { padding: 12px 14px; display: grid; gap: 4px; }
.cam-body .title { font-weight: 600; display: flex; justify-content: space-between; gap: 8px; }
.cam-body .sub { color: var(--muted); font-size: 12px; }
.cam-body .err { color: var(--bad); font-size: 12px; overflow-wrap: anywhere; }
.cam-actions { display: flex; gap: 8px; padding: 0 14px 14px; margin-top: auto; }

/* ---------- modal ---------- */
.modal { position: fixed; inset: 0; z-index: 50; background: rgba(8, 12, 18, .6); display: grid; place-items: center; padding: 16px; }
.modal-card {
  width: 100%; max-width: 640px; max-height: calc(100vh - 32px); overflow: auto;
  background: var(--panel); border: 1px solid var(--border); border-radius: 14px;
}
.modal-card.wide { max-width: 1100px; }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 14px 18px; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--panel); }
.modal-body { padding: 18px; }
.player { width: 100%; aspect-ratio: 16 / 9; background: #000; border-radius: 8px; display: block; }
.player-status { margin-top: 10px; color: var(--muted); min-height: 1.4em; }

/* Live video: plates read at the site appear at the bottom centre (sized to the video, also full screen) */
.player-wrap { position: relative; container-type: inline-size; background: #000; border-radius: 8px; overflow: hidden; }
.player-wrap:fullscreen { display: flex; align-items: center; border-radius: 0; }
.player-wrap:-webkit-full-screen { display: flex; align-items: center; border-radius: 0; }
.player-wrap:fullscreen .player { height: 100%; aspect-ratio: auto; border-radius: 0; }
.player-wrap:-webkit-full-screen .player { height: 100%; aspect-ratio: auto; border-radius: 0; }
.plate-overlay {
  position: absolute; left: 0; right: 0; bottom: max(44px, 8%); padding: 0 3cqw; pointer-events: none;
  display: flex; flex-wrap: wrap; justify-content: center; align-items: flex-end; gap: 2cqw;
}
.plate-pop { display: flex; flex-direction: column; align-items: center; gap: .5cqw; animation: plate-in .25s ease-out; transition: opacity .6s; }
.plate-pop.fade { opacity: 0; }
.plate-pop .plate {
  font-size: clamp(18px, 4.5cqw, 76px); padding: .12em .55em; border-width: max(2px, .3cqw);
  border-radius: max(6px, .6cqw); box-shadow: 0 4px 18px rgba(0, 0, 0, .55);
}
.plate-pop .badge { font-size: clamp(10px, 1.4cqw, 22px); box-shadow: 0 2px 8px rgba(0, 0, 0, .45); }
@container (max-width: 520px) { .plate-pop:not(:last-child) { display: none; } } /* phones: the newest plate only */
@keyframes plate-in { from { opacity: 0; transform: translateY(12px); } }
.player-full {
  position: absolute; top: 10px; right: 10px; display: grid; place-items: center; width: 34px; height: 34px;
  border: 0; border-radius: 8px; background: rgba(0, 0, 0, .55); color: #fff; cursor: pointer; opacity: .8;
}
.player-full:hover, .player-full:focus-visible { opacity: 1; background: rgba(0, 0, 0, .75); }
.event-full { width: 100%; background: #000; border-radius: 8px; display: block; }
.keybox {
  font-family: var(--mono); background: var(--panel-2); border: 1px dashed var(--border); border-radius: 8px;
  padding: 10px 12px; overflow-wrap: anywhere; user-select: all; margin: 6px 0 12px;
}
.callout { background: var(--panel-2); border-radius: 8px; padding: 12px 14px; margin: 12px 0; }
.callout ol { margin: 6px 0 0; padding-left: 20px; }

/* ---------- recordings ---------- */
.rec-layout { display: grid; grid-template-columns: 300px minmax(0, 1fr); gap: 16px; align-items: start; }
.rec-list { max-height: 70vh; overflow: auto; }
.rec-item { display: block; width: 100%; text-align: left; padding: 10px 14px; border: 0; border-bottom: 1px solid var(--border); background: none; color: var(--text); font: inherit; cursor: pointer; }
.rec-item:hover, .rec-item.active { background: var(--panel-2); }

/* ---------- toasts ---------- */
.toasts { position: fixed; right: 16px; bottom: 16px; display: grid; gap: 8px; z-index: 60; }
.toast {
  background: var(--panel); border: 1px solid var(--border); border-left: 4px solid var(--accent);
  border-radius: 8px; padding: 10px 14px; box-shadow: 0 6px 24px rgba(0, 0, 0, .2); max-width: 380px;
}
.toast.bad { border-left-color: var(--bad); }
.toast.ok { border-left-color: var(--ok); }

@media (max-width: 1100px) {
  .kpis { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .live-layout, .rec-layout { grid-template-columns: 1fr; }
  .latest { position: static; }
}
@media (max-width: 700px) {
  .topbar { height: auto; flex-wrap: wrap; padding: 10px 16px; gap: 10px; }
  #nav { order: 3; flex-basis: 100%; }
  main { padding: 16px; }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-form { grid-template-columns: 1fr; }
  .feed-item { grid-template-columns: 80px minmax(0, 1fr); }
  .feed-right { grid-column: 2; text-align: left; }
  .thumb { width: 80px; }
}

.btn.disabled { opacity: .5; pointer-events: none; }
.page-head > span { display: flex; gap: 8px; flex-wrap: wrap; }


/* ---------- users and roles ---------- */
.badge.role-admin { background: var(--accent); color: #fff; }
.badge.role-operator { background: #0e7490; color: #fff; }
.badge.role-viewer { background: var(--panel-2); color: var(--text); border: 1px solid var(--border); }
.role-cards { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; margin-bottom: 16px; }
.role-card { padding: 12px 14px; display: grid; gap: 6px; align-content: start; font-size: 13px; }
.role-card .badge { justify-self: start; }
fieldset { border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px 12px; margin: 0; display: grid; gap: 8px; min-width: 0; }
legend { font-weight: 600; font-size: 13px; padding: 0 4px; }
.role-option { display: flex; gap: 10px; align-items: flex-start; font-weight: 400; }
.role-option input { margin-top: 3px; }
.role-option > span { display: grid; gap: 1px; }
.site-checks { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 6px 14px; padding-left: 24px; }
.dimmed { opacity: .55; }
@media (max-width: 700px) { .role-cards { grid-template-columns: 1fr; } }
#nav { scrollbar-width: none; }
#nav::-webkit-scrollbar { display: none; }


/* ---------- districts, categories, site details ---------- */
.group-title { font-size: 15px; margin: 22px 0 10px; display: flex; gap: 8px; align-items: baseline; }
#cam-groups > .group-title:first-child { margin-top: 0; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.section-title { margin: 18px 0 8px; font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--accent); }
.kv-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px 24px; }
.kv-grid .full { grid-column: 1 / -1; }
.kv > .muted { font-size: 12px; }
.pre { white-space: pre-wrap; }
.small { font-size: 12px; }
.doc-list { display: grid; gap: 6px; }
.doc { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; padding: 7px 10px; background: var(--panel-2); border-radius: 8px; }
.doc a { font-weight: 600; overflow-wrap: anywhere; }
.upload-row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
span.check { display: flex; align-items: center; gap: 6px; }
.settings-lists { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; max-width: 1100px; margin-top: 16px; }
.inline-add { display: flex; gap: 8px; }
.inline-add input { flex: 1; }
.name-list { max-height: 380px; overflow: auto; margin-top: 10px; border-top: 1px solid var(--border); }
.name-item { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 5px 0; border-bottom: 1px solid var(--border); }
@media (max-width: 700px) { .kv-grid, .settings-lists { grid-template-columns: 1fr; } }

/* ---------- plate read quality + API docs ---------- */
.badge.read-uncertain { background: #b45309; color: #fff; }
.badge.read-partial { background: #6b7280; color: #fff; }
.badge.read-unreadable { background: #7f1d1d; color: #fff; }
.plate.none { opacity: .55; letter-spacing: 2px; }

/* Recognition area editor (camera form) */
.zone-edit { display: grid; gap: 8px; }
.zone-pic { position: relative; max-width: 640px; border-radius: 8px; overflow: hidden; background: var(--panel-2);
  border: 1px solid var(--border); cursor: crosshair; user-select: none; touch-action: none; }
.zone-pic.no-picture { cursor: default; }
.zone-pic img { display: block; width: 100%; height: auto; pointer-events: none; }
.zone-pic .empty { padding: 28px 16px; }
.zone-box { position: absolute; border: 2px solid #22c55e; background: rgba(34, 197, 94, .12);
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, .45); pointer-events: none; }
.zone-foot { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.doc-table { margin: 8px 0 12px; }
.doc-table td { font-size: 13px; }
.share-copy { display: flex; gap: 8px; flex-wrap: wrap; margin: 10px 0 4px; }
.embed-preview { max-width: 640px; aspect-ratio: 16 / 9; background: #000; border-radius: 8px; overflow: hidden; }
.embed-preview iframe { width: 100%; height: 100%; border: 0; display: block; }
.remote-actions { display: flex; gap: 8px; flex-wrap: wrap; margin: 12px 0 6px; }
table.compact td { padding: 4px 8px; font-size: 13px; }
/* Remote screen page (remote.html): desktop, tablet and phone */
body.remote-page { margin: 0; height: 100vh; height: 100dvh; display: flex; flex-direction: column; overflow: hidden;
  overscroll-behavior: none; }
.remote-bar { display: flex; align-items: center; gap: 8px; padding: 6px 10px; flex-wrap: nowrap;
  padding-top: max(6px, env(safe-area-inset-top)); background: var(--panel); border-bottom: 1px solid var(--border); }
.remote-title { flex: 1; min-width: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.remote-tools { display: flex; align-items: center; gap: 6px; overflow-x: auto; scrollbar-width: none; }
.remote-tools::-webkit-scrollbar { display: none; }
.remote-tools > * { flex: 0 0 auto; }
.remote-tools .check { display: flex; align-items: center; gap: 4px; font-size: 13px; white-space: nowrap; }
.remote-menu { display: flex; flex-wrap: wrap; gap: 6px; padding: 6px 10px; background: var(--panel-2);
  border-bottom: 1px solid var(--border); }
#screen { flex: 1; min-height: 0; width: 100%; background: #0b0f14; touch-action: none; }
.remote-kbd { position: fixed; left: 0; bottom: 0; width: 1px; height: 1px; padding: 0; border: 0; opacity: 0;
  resize: none; font-size: 16px; }
.remote-overlay { position: fixed; inset: 56px 0 0 0; display: grid; place-items: center; pointer-events: none; padding: 16px; }
.remote-overlay .panel { pointer-events: auto; max-width: 520px; text-align: center; }
@media (max-width: 760px) {
  .remote-bar { flex-wrap: wrap; gap: 6px; }
  .remote-title { flex-basis: 100%; font-size: 14px; }
  .remote-tools { width: 100%; flex-wrap: wrap; overflow: visible; }
  .remote-tools .btn.small { padding: 6px 10px; }
  .remote-overlay { inset: 130px 0 0 0; }
}
.inst-slot { padding: 10px 0; border-top: 1px solid var(--border); }
.inst-slot .callout { margin: 8px 0; }

/* Reports page */
.tabs { display: flex; gap: 4px; background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px;
  padding: 3px; flex-wrap: wrap; }
.tab { padding: 6px 14px; border-radius: 6px; color: var(--muted); font-weight: 600; text-decoration: none;
  background: none; border: 0; cursor: pointer; font: inherit; font-size: 14px; }
.tab:hover { color: var(--text); }
.tab.active { background: var(--panel); color: var(--text); box-shadow: var(--shadow); }
.small-tabs .tab { padding: 4px 10px; font-size: 13px; }
.kpis.kpis-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.kpis.kpis-6 { grid-template-columns: repeat(6, minmax(0, 1fr)); }
.kpi .value.ok { color: var(--ok); }
.kpi .value.warn { color: var(--warn); }
.badge.warn { background: var(--warn); color: #fff; }
table.kv { width: 100%; }
table.kv td { padding: 6px 8px; border-bottom: 1px solid var(--border); vertical-align: top; }
table.kv td:first-child { color: var(--muted); width: 34%; }
/* Contacts page */
.badge.role-po { background: var(--accent); color: var(--accent-text); }
.badge.role-sra { background: var(--ok); color: #fff; }
tr.group-start td { border-top: 2px solid var(--border); }
@media (max-width: 1100px) { .kpis.kpis-6 { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
@media (max-width: 700px) { .kpis.kpis-3, .kpis.kpis-6 { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
