:root {
  --bg: #f6f7f9; --surface: #fff; --surface-2: #f0f2f5; --border: #dde1e6; --text: #1b1f24; --muted: #646d78;
  --accent: #1f6feb; --accent-text: #fff; --danger: #c62828; --ok: #2e7d32; --warn: #b26a00;
  --radius: 8px; --shadow: 0 1px 2px rgba(0,0,0,.06), 0 4px 16px rgba(0,0,0,.06);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1217; --surface: #171b22; --surface-2: #1e232c; --border: #2c333e; --text: #e6e9ee; --muted: #949dab;
    --accent: #4c8dff; --danger: #ef6b6b; --ok: #5cbf63; --warn: #e0a243; --shadow: none;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--text); }
[hidden] { display: none !important; }
h1 { font-size: 22px; margin: 0 0 4px; }
h2 { font-size: 15px; margin: 0 0 12px; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
.muted { color: var(--muted); }
.small-text { font-size: 12.5px; }
.error { color: var(--danger); }

input, select, textarea, button, .button {
  font: inherit; color: inherit; border: 1px solid var(--border); border-radius: 6px; background: var(--surface);
  padding: 6px 10px; min-height: 32px;
}
textarea { width: 100%; min-height: 90px; resize: vertical; }
button, .button { cursor: pointer; display: inline-flex; align-items: center; gap: 6px; }
button:disabled { opacity: .55; cursor: default; }
.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.ghost { background: transparent; }
.ghost:hover, .button.ghost:hover { background: var(--surface-2); text-decoration: none; }
.small { padding: 3px 8px; min-height: 26px; font-size: 12.5px; }
label { display: grid; gap: 4px; color: var(--muted); font-size: 12.5px; }
label.check { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--text); white-space: nowrap; }
label.check input[type=number] { width: 90px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }

/* Login */
.login { min-height: 100%; display: grid; place-items: center; padding: 24px; }
.login-card { width: min(360px, 100%); display: grid; gap: 14px; }

/* Layout */
#app { height: 100%; display: flex; flex-direction: column; }
.topbar { display: flex; align-items: center; gap: 16px; padding: 8px 16px; background: var(--surface); border-bottom: 1px solid var(--border); }
.brand { font-weight: 700; }
.tabs { display: flex; gap: 4px; }
.tabs button { border: none; background: transparent; padding: 6px 12px; border-radius: 6px; color: var(--muted); }
.tabs button.active { background: var(--surface-2); color: var(--text); font-weight: 600; }
.pill { margin-left: auto; font-size: 12.5px; color: var(--muted); padding: 3px 10px; border-radius: 999px; background: var(--surface-2); white-space: nowrap; }
.pill.busy { color: var(--accent); }
.pill.err { color: var(--danger); }

.filters { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; padding: 10px 16px; border-bottom: 1px solid var(--border); background: var(--surface); }
.filters input[type=search] { width: 240px; }
.filters select { max-width: 230px; }
.spacer { flex: 1; }

.tab { flex: 1; min-height: 0; }
#tab-list { display: flex; flex-direction: column; }
.table-wrap { flex: 1; overflow: auto; }
table.grid { width: 100%; border-collapse: collapse; }
.grid th, .grid td { text-align: left; padding: 7px 12px; border-bottom: 1px solid var(--border); vertical-align: top; }
.grid th { position: sticky; top: 0; background: var(--surface-2); font-weight: 600; font-size: 12.5px; color: var(--muted); z-index: 1; }
.grid tbody tr { cursor: pointer; }
.grid tbody tr:hover { background: var(--surface-2); }
.grid .num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.grid .name { font-weight: 600; }
.grid .sub { color: var(--muted); font-size: 12px; }
.grid select { min-height: 26px; padding: 2px 6px; font-size: 12.5px; }
.contact a { margin-right: 8px; white-space: nowrap; }
.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 6px; vertical-align: 1px; }
.empty { padding: 40px; text-align: center; color: var(--muted); }
.pager { display: flex; justify-content: center; align-items: center; gap: 12px; padding: 8px; border-top: 1px solid var(--border); background: var(--surface); }

/* Map */
#tab-map { position: relative; }
#map { position: absolute; inset: 0; }
.legend { position: absolute; right: 10px; bottom: 24px; z-index: 500; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 8px 10px; font-size: 12px; max-height: 50%; overflow: auto; box-shadow: var(--shadow); }
.legend div { white-space: nowrap; }
.leaflet-popup-content { font: 13px/1.4 system-ui, sans-serif; }

/* Scans */
.scan-layout { display: grid; grid-template-columns: minmax(320px, 440px) 1fr; gap: 16px; padding: 16px; overflow: auto; align-items: start; }
.stack { display: grid; gap: 16px; }
.scan-layout > .card { display: grid; gap: 12px; position: sticky; top: 0; }
.cat-actions { display: flex; gap: 6px; }
.cat-groups { display: grid; gap: 10px; max-height: 52vh; overflow: auto; padding-right: 4px; }
.cat-group { border: 1px solid var(--border); border-radius: 6px; padding: 8px 10px; }
.cat-group > label.check { font-weight: 600; }
.cat-group .items { display: grid; gap: 3px; margin: 6px 0 0 22px; }
.cat-group .items label.check { font-size: 13px; }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 4px 14px; margin: 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }
.scan { border-top: 1px solid var(--border); padding: 10px 0; }
.scan:first-child { border-top: none; padding-top: 0; }
.scan-head { display: flex; flex-wrap: wrap; gap: 8px 14px; align-items: center; }
.scan-head .title { font-weight: 600; }
.progress { height: 6px; border-radius: 3px; background: var(--surface-2); overflow: hidden; margin: 8px 0 4px; display: flex; }
.progress span { display: block; height: 100%; }
.progress .p-done { background: var(--ok); }
.progress .p-fail { background: var(--danger); }
.badge { font-size: 11.5px; padding: 1px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.badge.Running, .badge.Queued { color: var(--accent); }
.badge.Done { color: var(--ok); }
.badge.DoneWithErrors, .badge.Failed { color: var(--danger); }
.jobs { width: 100%; border-collapse: collapse; margin-top: 8px; font-size: 12.5px; }
.jobs td, .jobs th { padding: 4px 6px; border-bottom: 1px solid var(--border); text-align: left; }
.jobs .err { color: var(--danger); }
.inline-form { display: flex; gap: 6px; margin-bottom: 10px; }
.inline-form input { width: 120px; }
.region { display: flex; align-items: center; gap: 8px; padding: 4px 0; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.region .rname { flex: 1; min-width: 160px; }
.region input { width: 56px; min-height: 26px; padding: 2px 6px; }

/* Drawer */
.drawer { position: fixed; top: 0; right: 0; bottom: 0; width: min(520px, 100%); background: var(--surface); border-left: 1px solid var(--border);
  box-shadow: -8px 0 24px rgba(0,0,0,.15); z-index: 1000; overflow: auto; padding: 20px; }
.drawer .close { position: absolute; top: 12px; right: 12px; }
.drawer h2 { font-size: 18px; margin: 0 32px 2px 0; }
.drawer section { margin-top: 18px; }
.drawer h3 { font-size: 12.5px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 0 0 8px; }
.drawer .actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.tags { width: 100%; border-collapse: collapse; font-size: 12.5px; }
.tags td { padding: 3px 6px; border-bottom: 1px solid var(--border); word-break: break-word; vertical-align: top; }
.tags td:first-child { color: var(--muted); width: 40%; }

@media (max-width: 900px) {
  .scan-layout { grid-template-columns: 1fr; }
  .scan-layout > .card { position: static; }
  .filters input[type=search] { width: 100%; }
}
