:root {
  --bg: #0f1722;
  --panel: rgba(18, 26, 38, 0.92);
  --panel-solid: #121a26;
  --border: rgba(255, 255, 255, 0.10);
  --text: #e8eef6;
  --muted: #94a3b8;
  --input: #0b121c;
  --primary: #2f7df6;
  --primary-hover: #4b90ff;
  --danger: #e5484d;
  --warning: #f5a524;
  --success: #22a559;
  --radius: 10px;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.35);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font: 14px/1.4 var(--font); color: var(--text); background: var(--bg); }
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }
.muted { color: var(--muted); margin: 0; font-size: 12px; }
kbd { background: #243246; border-radius: 4px; padding: 0 5px; font-size: 11px; border: 1px solid var(--border); }

/* ---------- buttons & inputs ---------- */
.btn {
  border: 1px solid var(--border); background: #1d2a3b; color: var(--text);
  padding: 7px 13px; border-radius: 8px; cursor: pointer; white-space: nowrap;
  transition: background .15s, border-color .15s;
}
.btn:hover { background: #26364b; }
.btn:disabled { opacity: .55; cursor: default; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-danger { background: transparent; border-color: rgba(229, 72, 77, .6); color: #ff8a8e; }
.btn-danger:hover { background: rgba(229, 72, 77, .15); }
.btn-warning { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-black { background: #000; border-color: #000; color: #fff; }
.btn-add { background: var(--success); border-color: var(--success); color: #fff; font-weight: 600; }
.btn-add:hover { background: #2bbb68; }
.btn-add.active { background: #fff; color: var(--success); }
.btn-block { width: 100%; padding: 10px; }
.icon-btn {
  border: 1px solid var(--border); background: #1d2a3b; border-radius: 8px; width: 34px; height: 34px;
  cursor: pointer; font-size: 16px; display: inline-grid; place-items: center; flex: none;
}
.icon-btn:hover { background: #26364b; }
.icon-btn.active { background: var(--primary); }
.link-btn { background: none; border: 0; color: var(--primary-hover); cursor: pointer; padding: 0; font-size: 12px; }
.mini { border: 1px solid var(--border); background: #1d2a3b; border-radius: 6px; width: 28px; cursor: pointer; }

input, select, textarea {
  width: 100%; background: var(--input); border: 1px solid var(--border); border-radius: 7px;
  padding: 6px 8px; outline: none; min-width: 0;
}
input:focus, select:focus, textarea:focus { border-color: var(--primary); box-shadow: 0 0 0 2px rgba(47, 125, 246, .25); }
input[readonly], input:disabled, select:disabled, textarea:disabled { opacity: .7; }
textarea { resize: vertical; }
label { display: block; font-size: 12px; color: var(--muted); }
label > input, label > select, label > textarea, label > .angle-input { margin-top: 3px; color: var(--text); }
.form-error { color: #ff8a8e; background: rgba(229, 72, 77, .12); border: 1px solid rgba(229, 72, 77, .35); padding: 7px 9px; border-radius: 7px; margin: 6px 0; font-size: 13px; }
.glass { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); backdrop-filter: blur(8px); }

/* ---------- login ---------- */
.login-page {
  display: grid; place-items: center; min-height: 100vh; padding: 16px;
  background: radial-gradient(circle at 20% 10%, #1c3555 0, transparent 50%), radial-gradient(circle at 90% 90%, #3a1b25 0, transparent 45%), var(--bg);
}
.login-card { width: 100%; max-width: 360px; background: var(--panel-solid); border: 1px solid var(--border); border-radius: 14px; padding: 28px; box-shadow: var(--shadow); }
.brand { display: flex; gap: 12px; align-items: center; margin-bottom: 22px; }
.brand h1 { margin: 0; font-size: 20px; }
.brand p { margin: 0; color: var(--muted); font-size: 13px; }
.login-card label { margin-bottom: 14px; font-size: 13px; }
.login-card input { padding: 9px 10px; }

/* ---------- app layout ---------- */
.app { overflow: hidden; }
#map { position: fixed; inset: 0 0 52px 0; background: #0b0f14; }
#map.crosshair, #map.crosshair .leaflet-interactive { cursor: crosshair !important; }

.search { position: fixed; top: 12px; left: 50%; transform: translateX(-50%); z-index: 1000; display: flex; gap: 6px; padding: 6px; width: min(460px, calc(100% - 140px)); }
.search input { padding: 8px 10px; }

.toolbar { position: fixed; top: 12px; right: 12px; z-index: 1000; display: flex; flex-direction: column; gap: 8px; }
.tool {
  width: 44px; height: 44px; border-radius: 10px; border: 1px solid var(--border); background: var(--panel);
  display: grid; place-items: center; cursor: pointer; box-shadow: var(--shadow);
}
.tool svg { width: 24px; height: 24px; fill: none; stroke: var(--text); stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tool:hover { background: #26364b; }
.tool.active { background: var(--primary); border-color: var(--primary); }

.popover { position: fixed; top: 12px; right: 66px; z-index: 1001; width: 290px; padding: 12px 14px; max-height: calc(100vh - 90px); overflow: auto; }
.popover h3 { margin: 4px 0 8px; font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.popover h3:not(:first-child) { margin-top: 14px; }
.popover fieldset { border: 0; padding: 0; margin: 0 0 8px; }
.popover legend { font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.check { display: flex; align-items: center; gap: 8px; color: var(--text); font-size: 13px; padding: 3px 0; cursor: pointer; }
.check input { width: auto; margin: 0; }
.check .swatch { width: 10px; height: 10px; border-radius: 50%; flex: none; }
.check .count { margin-left: auto; color: var(--muted); font-size: 12px; }
.row { display: flex; align-items: center; gap: 6px; color: var(--text); font-size: 13px; padding: 4px 0; }
.row input { width: 64px; }

/* ---------- editor ---------- */
.editor { position: fixed; top: 12px; left: 12px; bottom: 64px; z-index: 1000; width: 330px; display: flex; flex-direction: column; overflow: hidden; }
.editor-head { display: flex; justify-content: space-between; align-items: flex-start; padding: 12px 14px 8px; border-bottom: 1px solid var(--border); }
.editor-head h2 { margin: 0; font-size: 16px; }
.editor-form { padding: 10px 14px; overflow-y: auto; display: flex; flex-direction: column; gap: 8px; flex: 1; }
.editor-form .two { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.coord-row { display: flex; gap: 6px; align-items: flex-end; }
.coord-row label { flex: 1; }
.angle-input { display: flex; gap: 4px; }
.angle-input input { text-align: center; }
.section-end { border: 1px solid var(--border); border-radius: 8px; padding: 6px 8px 8px; margin: 0; }
.section-end legend { font-size: 12px; color: var(--muted); padding: 0 4px; }
.hint { color: var(--muted); font-size: 11.5px; margin: 2px 0 0; }
.editor-actions { display: flex; flex-wrap: wrap; gap: 6px; padding-top: 4px; }
.editor-actions .btn-primary { flex: 1; }
.editor.dirty #ed-title::after { content: " • unsaved"; color: var(--warning); font-size: 12px; font-weight: 400; }
.editor.readonly .editor-actions .btn:not(#ed-cancel) { display: none; }
.dup-warning { margin: 8px 14px 0; padding: 8px 10px; border-radius: 8px; background: rgba(245, 165, 36, .12); border: 1px solid rgba(245, 165, 36, .45); font-size: 12.5px; }
.dup-warning ul { margin: 4px 0 0; padding-left: 18px; }
.dup-warning a, .table a { color: var(--primary-hover); cursor: pointer; }
.history { border-top: 1px solid var(--border); padding: 8px 14px; max-height: 30%; overflow-y: auto; font-size: 12.5px; }
.history summary { cursor: pointer; color: var(--muted); }
.history ol { list-style: none; padding: 0; margin: 6px 0 0; }
.history li { padding: 5px 0; border-bottom: 1px dashed var(--border); }
.history .diff { color: var(--muted); font-size: 11.5px; word-break: break-word; }

/* ---------- bottom bar ---------- */
.bottombar {
  position: fixed; left: 0; right: 0; bottom: 0; height: 52px; z-index: 1000; display: flex; align-items: center;
  gap: 12px; padding: 0 12px; background: #0d141e; border-top: 1px solid var(--border);
}
.stats { font-size: 13px; white-space: nowrap; }
.stats b { color: #fff; }
.view-hint { color: var(--muted); font-size: 12.5px; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.actions { display: flex; gap: 6px; align-items: center; }
.menu { position: relative; }
.menu-list { position: absolute; bottom: 44px; right: 0; min-width: 190px; padding: 6px; display: flex; flex-direction: column; z-index: 1002; }
.menu-list p { padding: 4px 8px 6px; }
.menu-list button { text-align: left; background: none; border: 0; padding: 7px 8px; border-radius: 6px; cursor: pointer; }
.menu-list button:hover { background: #26364b; }
.ctx { position: fixed; bottom: auto; right: auto; }

.mode-banner {
  position: fixed; top: 66px; left: 50%; transform: translateX(-50%); z-index: 1000;
  background: var(--primary); color: #fff; padding: 7px 14px; border-radius: 20px; box-shadow: var(--shadow); font-size: 13px;
}
.toasts { position: fixed; bottom: 64px; left: 50%; transform: translateX(-50%); z-index: 3000; display: flex; flex-direction: column; gap: 6px; align-items: center; pointer-events: none; }
.toast { background: #1d2a3b; border: 1px solid var(--border); padding: 8px 14px; border-radius: 8px; box-shadow: var(--shadow); animation: fade .2s; }
.toast.error { background: #4a1d22; border-color: #8c2f36; }
.toast.ok { background: #173b28; border-color: #2a7a4c; }
@keyframes fade { from { opacity: 0; transform: translateY(6px); } }

/* ---------- dialog ---------- */
.dialog { border: 1px solid var(--border); border-radius: 12px; background: var(--panel-solid); color: var(--text); padding: 0; width: min(640px, calc(100% - 32px)); box-shadow: var(--shadow); }
.dialog::backdrop { background: rgba(0, 0, 0, .55); }
.dialog-inner header { display: flex; justify-content: space-between; align-items: center; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.dialog-inner header h2 { margin: 0; font-size: 16px; }
#dlg-body { padding: 14px 16px; max-height: 65vh; overflow: auto; display: flex; flex-direction: column; gap: 10px; }
#dlg-foot { padding: 10px 16px 14px; display: flex; justify-content: flex-end; gap: 8px; }
#dlg-foot:empty { display: none; }
.choice { display: flex; gap: 10px; align-items: flex-start; padding: 9px 10px; border: 1px solid var(--border); border-radius: 8px; cursor: pointer; color: var(--text); font-size: 13px; }
.choice input { width: auto; margin-top: 2px; }
.choice small { display: block; color: var(--muted); }
.choice:has(input:checked) { border-color: var(--primary); background: rgba(47, 125, 246, .08); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.table { width: 100%; border-collapse: collapse; font-size: 12.5px; }
.table th, .table td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--border); }
.table th { color: var(--muted); font-weight: 500; position: sticky; top: 0; background: var(--panel-solid); }
.badge { display: inline-block; padding: 1px 7px; border-radius: 10px; font-size: 11px; background: #243246; }
.badge.create, .badge.restore { background: #173b28; }
.badge.delete, .badge.purge { background: #4a1d22; }
.badge.update, .badge.import, .badge.revert { background: #1c3555; }
.result-box { background: var(--input); border: 1px solid var(--border); border-radius: 8px; padding: 10px; font-size: 13px; }

/* ---------- map markers ---------- */
.cam-pin { background: none; border: 0; }
.cam-pin svg { overflow: visible; filter: drop-shadow(0 2px 2px rgba(0, 0, 0, .5)); }
.cam-pin .spd { font: 700 10px var(--font); fill: #111; }
.cluster { background: none; border: 0; }
.cluster div {
  width: 100%; height: 100%; border-radius: 50%; display: grid; place-items: center;
  background: rgba(47, 125, 246, .85); color: #fff; font: 600 12px var(--font);
  border: 2px solid rgba(255, 255, 255, .85); box-shadow: 0 2px 6px rgba(0, 0, 0, .4);
}
.handle { background: #fff; border: 2px solid #e5484d; border-radius: 50%; box-shadow: 0 1px 4px rgba(0, 0, 0, .6); cursor: grab; }
.dist-handle { background: #1f4bff; border: 2px solid #fff; border-radius: 3px; box-shadow: 0 1px 4px rgba(0, 0, 0, .6); cursor: ew-resize; }
.end-handle { background: #a259ff; border: 2px solid #fff; border-radius: 3px; cursor: grab; }
.ruler-label { background: rgba(0, 0, 0, .75); color: #fff; border: 0; border-radius: 4px; padding: 2px 6px; font: 600 12px var(--font); white-space: nowrap; box-shadow: none; }
.ruler-label::before { display: none; }
.leaflet-container { font: 13px var(--font); }
.leaflet-control-zoom a { background: var(--panel) !important; color: var(--text) !important; border-color: var(--border) !important; }
.leaflet-control-attribution { background: rgba(0, 0, 0, .55) !important; color: #bbb !important; font-size: 10px; }
.leaflet-control-attribution a { color: #ddd !important; }
.leaflet-control-scale-line { background: rgba(0, 0, 0, .55); color: #fff; border-color: #fff; }
.leaflet-tooltip.cam-tip { background: var(--panel-solid); color: var(--text); border: 1px solid var(--border); box-shadow: var(--shadow); }
.leaflet-tooltip.cam-tip::before { display: none; }

@media (max-width: 1150px) { .view-hint { visibility: hidden; } }
@media (max-width: 900px) {
  .bottombar { height: auto; flex-wrap: wrap; padding: 8px; }
  .view-hint { display: none; }
  .actions { flex-wrap: wrap; }
  #map { bottom: 96px; }
  .editor { width: calc(100% - 24px); bottom: 108px; top: auto; max-height: 60vh; }
  .search { left: 12px; transform: none; width: calc(100% - 80px); top: 12px; }
  .leaflet-top.leaflet-left { top: 60px; }
}
