:root {
  --bg: #f5f7fa;
  --surface: #ffffff;
  --surface-2: #f0f3f7;
  --border: #dde3ea;
  --text: #17202a;
  --muted: #64707d;
  --accent: #2563eb;
  --accent-fg: #ffffff;
  --danger: #dc2626;
  --shadow: 0 6px 24px rgba(15, 23, 42, 0.14);
  --radius: 10px;
  --panel-w: 330px;
  --topbar-h: 52px;
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #0b1016;
  --surface: #121a23;
  --surface-2: #1a2430;
  --border: #263241;
  --text: #e6edf3;
  --muted: #8b98a6;
  --accent: #3b82f6;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.5);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font: 14px/1.4 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex; flex-direction: column; height: 100dvh; overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}
svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
a { color: var(--accent); }
[hidden] { display: none !important; }

/* ---------- top bar ---------- */
.topbar {
  height: var(--topbar-h); flex: none; display: flex; align-items: center; gap: 10px;
  padding: 0 12px; padding-left: max(12px, env(safe-area-inset-left)); padding-right: max(12px, env(safe-area-inset-right));
  background: var(--surface); border-bottom: 1px solid var(--border); z-index: 1100;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 16px; white-space: nowrap; }
.brand .logo { color: var(--accent); width: 22px; height: 22px; }
.spacer { flex: 1; }
.badge { font-size: 12px; color: var(--muted); background: var(--surface-2); border: 1px solid var(--border); padding: 3px 8px; border-radius: 99px; white-space: nowrap; }

.btn, .icon-btn, .seg button, .pos-picker button, .map-tools button {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; cursor: pointer; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  user-select: none; -webkit-user-select: none;
}
.btn { height: 36px; padding: 0 12px; font-weight: 600; }
.btn:hover, .icon-btn:hover, .seg button:hover, .pos-picker button:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
.btn.primary:hover { filter: brightness(1.08); background: var(--accent); }
.btn.small { height: 30px; padding: 0 10px; font-size: 13px; }
.btn.ghost { background: transparent; }
.btn.block { width: 100%; margin-top: 12px; }
.btn.active { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
.btn svg { width: 16px; height: 16px; }
.icon-btn { width: 36px; height: 36px; padding: 0; }

.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 9px; overflow: hidden; background: var(--surface-2); padding: 2px; }
.seg button { border: 0; background: transparent; height: 30px; padding: 0 12px; font-weight: 600; border-radius: 7px; color: var(--muted); }
.seg button.on { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.15); }

/* ---------- layout ---------- */
.layout { flex: 1; display: flex; min-height: 0; position: relative; }
#panel {
  width: var(--panel-w); flex: none; overflow-y: auto; background: var(--surface); border-right: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom); overscroll-behavior: contain;
}
body.panel-closed #panel { display: none; }
#stage { flex: 1; position: relative; min-width: 0; overflow: hidden; }
.mapview { position: absolute; inset: 0; }
#map-azi { touch-action: none; cursor: grab; }
#map-azi:active { cursor: grabbing; }
.qa-azi-canvas { display: block; }
.qa-overlay { position: absolute; top: 0; left: 0; pointer-events: none; z-index: 450; }
#stage.hovering .leaflet-container, #stage.hovering #map-azi { cursor: pointer; }
#stage.picking .leaflet-container, #stage.picking #map-azi { cursor: crosshair; }
.leaflet-container { background: #dfe6ea; font: inherit; }

/* ---------- panel sections ---------- */
details { border-bottom: 1px solid var(--border); }
summary {
  list-style: none; cursor: pointer; padding: 12px 16px; font-weight: 700; font-size: 13px; text-transform: uppercase;
  letter-spacing: .04em; color: var(--muted); display: flex; align-items: center; justify-content: space-between;
}
summary::-webkit-details-marker { display: none; }
summary::after { content: ""; width: 7px; height: 7px; border-right: 2px solid currentColor; border-bottom: 2px solid currentColor; transform: rotate(45deg); transition: transform .15s; margin-right: 4px; }
details[open] summary::after { transform: rotate(-135deg); }
.sec { padding: 0 16px 16px; display: flex; flex-direction: column; gap: 8px; }
.lbl { font-size: 12px; font-weight: 600; color: var(--muted); margin-top: 4px; display: flex; justify-content: space-between; }
.lbl output { font-weight: 500; }
.lbl-row { display: flex; justify-content: space-between; align-items: baseline; margin-top: 4px; }
.lbl-row .lbl { margin: 0; }
.links { font-size: 12px; }
.row { display: flex; align-items: center; }
.row.gap { gap: 8px; }
.row.wrap { flex-wrap: wrap; }
.row > input:not([type=checkbox]), .row > select { flex: 1; min-width: 0; }
.muted { color: var(--muted); }
.tiny { font-size: 12px; }
hr { border: 0; border-top: 1px solid var(--border); margin: 6px 0; width: 100%; }

input:not([type]), input[type=text], input[type=datetime-local], select {
  font: inherit; font-size: 13px; color: var(--text); background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 7px; height: 32px; padding: 0 8px; width: 100%; min-width: 0;
}
input:focus-visible, select:focus-visible, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
input[type=range] { width: 100%; accent-color: var(--accent); margin: 0; }
input[type=color] { width: 34px; height: 26px; border: 1px solid var(--border); border-radius: 6px; padding: 1px; background: var(--surface); cursor: pointer; flex: none; }
.check { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; font-size: 13px; }
.check input { accent-color: var(--accent); width: 16px; height: 16px; margin: 0; }

.chips { display: flex; flex-wrap: wrap; gap: 5px; }
.chip {
  font: inherit; font-size: 12px; font-weight: 600; display: inline-flex; align-items: center; gap: 5px; padding: 3px 8px 3px 6px;
  border-radius: 99px; border: 1px solid var(--border); background: var(--surface-2); color: var(--text); cursor: pointer; user-select: none;
}
.chip i { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.chip small { color: var(--muted); font-weight: 500; }
.chip.off { opacity: .4; text-decoration: line-through; }
.chips:empty::before { content: "—"; color: var(--muted); }

.dual-range { position: relative; height: 26px; }
.dual-range .track { position: absolute; left: 8px; right: 8px; top: 11px; height: 4px; border-radius: 2px; background: var(--border); }
.dual-range .fill { position: absolute; top: 0; bottom: 0; background: var(--accent); border-radius: 2px; }
.dual-range input { position: absolute; inset: 0; pointer-events: none; background: transparent; -webkit-appearance: none; appearance: none; height: 26px; }
.dual-range input::-webkit-slider-thumb { pointer-events: auto; -webkit-appearance: none; width: 18px; height: 18px; border-radius: 50%; background: var(--surface); border: 2px solid var(--accent); cursor: pointer; box-shadow: 0 1px 3px rgba(0,0,0,.3); }
.dual-range input::-moz-range-thumb { pointer-events: auto; width: 14px; height: 14px; border-radius: 50%; background: var(--surface); border: 2px solid var(--accent); cursor: pointer; }
.dual-range input::-webkit-slider-runnable-track { background: transparent; }
.dual-range input::-moz-range-track { background: transparent; }

.shape-picker { display: grid; grid-template-columns: repeat(5, 1fr); gap: 5px; }
.shape-picker button { height: 36px; border: 1px solid var(--border); background: var(--surface-2); border-radius: 7px; cursor: pointer; display: grid; place-items: center; padding: 0; }
.shape-picker button.on { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); background: var(--surface); }
.shape-picker canvas { width: 24px; height: 24px; }

.preset-picker { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.preset-picker button { border: 1px solid var(--border); border-radius: 8px; padding: 0; overflow: hidden; cursor: pointer; background: var(--surface-2); color: var(--text); font: inherit; font-size: 11px; font-weight: 600; text-align: left; }
.preset-picker button.on { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.preset-picker .sw { height: 30px; display: flex; }
.preset-picker .sw span { flex: 1; }
.preset-picker .nm { padding: 4px 6px; display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.color-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 10px; }
.color-grid label { display: flex; align-items: center; gap: 6px; font-size: 12px; min-width: 0; }
.color-grid label span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.pos-picker button { width: 36px; height: 30px; font-size: 15px; }
.pos-picker button.on { border-color: var(--accent); color: var(--accent); }

.progress .bar { height: 6px; border-radius: 3px; background: var(--surface-2); overflow: hidden; border: 1px solid var(--border); }
.progress .bar span { display: block; height: 100%; width: 0; background: var(--accent); transition: width .2s; }
.progress .ptext { font-size: 12px; color: var(--muted); margin-top: 4px; }
.panel-foot { padding: 12px 16px 20px; }
.warn { color: #b45309; }
:root[data-theme="dark"] .warn { color: #fbbf24; }

/* ---------- map tools ---------- */
.map-tools { position: absolute; right: 12px; top: 12px; z-index: 900; display: flex; flex-direction: column; gap: 6px; }
.map-tools button { width: 38px; height: 38px; padding: 0; box-shadow: var(--shadow); }
.map-tools button.active { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
.pick-hint {
  position: absolute; top: 12px; left: 50%; transform: translateX(-50%); z-index: 950; background: var(--surface);
  border: 1px solid var(--border); border-radius: 99px; padding: 6px 14px; box-shadow: var(--shadow); font-size: 13px; white-space: nowrap;
}

/* ---------- floating overlay panels (also painted into exports) ---------- */
.floating { position: absolute; z-index: 800; touch-action: none; cursor: move; user-select: none; -webkit-user-select: none; }
.floating.dragging { opacity: .9; }
.infobox {
  --box-bg: rgba(255,255,255,.92); --box-fg: #0f172a; --box-accent: #2563eb; --box-muted: rgba(15,23,42,.6); --box-line: rgba(15,23,42,.12);
  background: var(--box-bg); color: var(--box-fg); border-radius: 12px; padding: 14px 16px 12px; min-width: 220px; max-width: 360px;
  box-shadow: 0 8px 28px rgba(0,0,0,.25); border-left: 4px solid var(--box-accent);
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
.infobox .ib-contest { font-size: 24px; font-weight: 800; line-height: 1.15; color: var(--box-accent); white-space: nowrap; letter-spacing: .01em; }
.infobox .ib-by { font-size: 11px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; color: var(--box-muted); margin-top: 6px; white-space: nowrap; }
.infobox .ib-call { font-size: 26px; font-weight: 800; letter-spacing: .02em; line-height: 1.15; white-space: nowrap; }
.infobox .ib-sub { font-size: 12px; color: var(--box-muted); white-space: nowrap; }
.infobox .ib-rows { margin-top: 10px; display: grid; grid-template-columns: auto auto; column-gap: 16px; }
.infobox .ib-rows > div { display: contents; }
.infobox dt, .infobox dd { margin: 0; padding: 5px 0; border-top: 1px solid var(--box-line); white-space: nowrap; font-size: 13px; }
.infobox dt { color: var(--box-muted); }
.infobox dd { font-weight: 700; text-align: right; }
.infobox dd .dx-call { color: var(--box-accent); }

.legend {
  --box-bg: rgba(255,255,255,.92); --box-fg: #0f172a; --box-muted: rgba(15,23,42,.6);
  background: var(--box-bg); color: var(--box-fg); border-radius: 10px; padding: 8px 10px; box-shadow: 0 6px 20px rgba(0,0,0,.22);
  font-size: 12px; max-width: 260px;
}
.legend .lg-title { font-size: 10px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; color: var(--box-muted); margin-bottom: 4px; white-space: nowrap; }
.legend .lg-items { display: grid; grid-template-columns: repeat(var(--cols, 1), auto); gap: 2px 12px; }
.legend .lg-item { display: flex; align-items: center; gap: 6px; white-space: nowrap; }
.legend .lg-item canvas { width: 14px; height: 14px; }
.legend .lg-item small { color: var(--box-muted); }
.legend .lg-grad canvas { width: 180px; height: 10px; display: block; border-radius: 3px; }
.legend .lg-ends { display: flex; justify-content: space-between; gap: 12px; color: var(--box-muted); font-size: 11px; margin-top: 2px; white-space: nowrap; }

.clock {
  --box-bg: rgba(255,255,255,.92); --box-fg: #0f172a; --box-accent: #2563eb; --box-muted: rgba(15,23,42,.6);
  background: var(--box-bg); color: var(--box-fg); border-radius: 10px; padding: 6px 14px 7px; text-align: center;
  box-shadow: 0 6px 20px rgba(0,0,0,.22); border-bottom: 3px solid var(--box-accent);
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
.clock .ck-time { font-size: 26px; font-weight: 800; line-height: 1.1; font-variant-numeric: tabular-nums; white-space: nowrap; }
.clock .ck-utc { font-size: 12px; font-weight: 700; color: var(--box-muted); margin-left: 5px; }
.clock .ck-date { font-size: 12px; color: var(--box-muted); white-space: nowrap; }

.rec-dot { width: 10px; height: 10px; border-radius: 50%; background: #dc2626; display: inline-block; }
#btn-record { align-self: flex-start; }
.rec-overlay { position: absolute; inset: 0; z-index: 1250; display: grid; place-items: end center; padding: 24px; pointer-events: none; }
.rec-card {
  pointer-events: auto; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow);
  padding: 12px 16px; width: min(420px, 100%); display: flex; flex-direction: column; gap: 8px;
}
.rec-card b { display: flex; align-items: center; gap: 8px; }
.rec-card .rec-dot { animation: recblink 1s infinite; }
.rec-card button { align-self: flex-end; }
@keyframes recblink { 50% { opacity: .25; } }

/* ---------- popup ---------- */
.popup {
  position: absolute; z-index: 1000; background: var(--surface); color: var(--text); border: 1px solid var(--border);
  border-radius: 10px; box-shadow: var(--shadow); width: 290px; max-height: 60%; overflow: auto; font-size: 13px;
}
.popup header { display: flex; align-items: center; justify-content: space-between; padding: 8px 10px 6px 12px; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--surface); }
.popup header b { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }
.popup header button { border: 0; background: none; color: var(--muted); font-size: 20px; line-height: 1; cursor: pointer; padding: 0 4px; }
.popup .q { padding: 8px 12px; border-bottom: 1px solid var(--border); }
.popup .q:last-child { border-bottom: 0; }
.popup .q .call { font-weight: 800; font-size: 15px; text-decoration: none; }
.popup .q .tags { display: inline-flex; gap: 4px; margin-left: 6px; vertical-align: 2px; }
.popup .q .tag { font-size: 11px; font-weight: 700; padding: 1px 6px; border-radius: 99px; color: #fff; }
.popup .q .tag.m { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }
.popup .q .meta { color: var(--muted); font-size: 12px; margin-top: 2px; }
.popup .actions { padding: 6px 12px 10px; }

.toast {
  position: absolute; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 1200; max-width: min(92%, 520px);
  background: #111827; color: #f9fafb; padding: 10px 14px; border-radius: 10px; box-shadow: var(--shadow); font-size: 13px;
}
.toast.err { background: #7f1d1d; }

#drop-overlay {
  position: absolute; inset: 0; z-index: 1300; background: rgba(37, 99, 235, .18); border: 3px dashed var(--accent);
  display: grid; place-items: center; pointer-events: none;
}
#drop-overlay div { background: var(--surface); padding: 14px 20px; border-radius: 10px; font-weight: 700; box-shadow: var(--shadow); }

.empty { position: absolute; inset: 0; z-index: 850; display: grid; place-items: center; pointer-events: none; padding: 16px; }
.empty-card {
  pointer-events: auto; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow);
  padding: 24px 28px; max-width: 420px; text-align: center;
}
.empty-card h1 { margin: 0 0 8px; font-size: 22px; }
.empty-card p { margin: 0 0 16px; }
.empty-card p:last-child { margin: 14px 0 0; }

.leaflet-control-attribution { font-size: 10px; }

/* ---------- small screens: panel becomes a drawer ---------- */
@media (max-width: 820px) {
  :root { --panel-w: min(88vw, 360px); }
  .hide-sm { display: none; }
  .brand span { display: none; }
  #panel {
    position: absolute; left: 0; top: 0; bottom: 0; z-index: 1150; box-shadow: var(--shadow);
    transform: translateX(-102%); transition: transform .2s ease;
  }
  body.panel-open #panel { transform: none; }
  body.panel-closed #panel { display: block; }
  #backdrop { position: absolute; inset: 0; background: rgba(0,0,0,.35); z-index: 1140; }
  .map-tools button { width: 44px; height: 44px; }
  .popup { left: 8px !important; right: 8px; bottom: 8px; top: auto !important; width: auto; max-height: 45%; }
  .infobox { padding: 10px 12px 8px; min-width: 0; }
  .infobox .ib-call, .infobox .ib-contest { font-size: 19px; }
  .clock .ck-time { font-size: 20px; }
  .infobox dt, .infobox dd { font-size: 12px; padding: 3px 0; }
  .legend { max-width: 200px; }
  .topbar { gap: 8px; }
  .seg button { padding: 0 10px; }
  input:not([type]), input[type=datetime-local], select { font-size: 16px; height: 36px; } /* avoid iOS zoom */
}
@media (max-width: 420px) {
  #count-badge { display: none; }
}
