:root {
  --bg: #0f1419; --panel: #161c24; --panel2: #1d2530; --line: #2b3542;
  --text: #e6ebf0; --muted: #8a97a6; --accent: #3b9ddd; --warn: #e0a326; --neg: #d94a3a; --pos: #3b78dd;
  --font: "Segoe UI", system-ui, -apple-system, Roboto, Helvetica, Arial, sans-serif;
  --mono: Consolas, "SFMono-Regular", Menlo, monospace;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font: 13px/1.4 var(--font); }
#map { position: absolute; top: var(--topbar-h, 56px); left: 0; right: 0; bottom: 0; background: #0b0f14; }
.leaflet-container { font: inherit; }

/* ---------- top bar ---------- */
#topbar { position: absolute; top: 0; left: 0; right: 0; height: 56px; z-index: 1000;
  background: linear-gradient(180deg, #1b2430, #131a22); border-bottom: 1px solid var(--line);
  display: flex; align-items: center; gap: 16px; padding: 0 14px; white-space: nowrap; }
.brand { display: flex; align-items: center; gap: 10px; flex: none; }
.brand .logo { font-size: 22px; color: var(--accent); }
.brand .title { font-weight: 600; font-size: 15px; letter-spacing: .2px; }
.brand .subtitle { color: var(--muted); font-size: 11px; }
.topcontrols { display: flex; align-items: center; gap: 10px; flex: 1; min-width: 0; overflow: hidden; }
.topcontrols label { color: var(--muted); display: flex; align-items: center; gap: 6px; }
#dataset { max-width: 260px; text-overflow: ellipsis; }
#loadInfo { overflow: hidden; text-overflow: ellipsis; }
.tabs { flex: none; }
@media (max-width: 1500px) { .brand .subtitle { display: none; } #dataset { max-width: 180px; } }
@media (max-width: 1250px) { .topcontrols label span.lbl { display: none; } .search input { width: 130px; } }
.seg { display: flex; border: 1px solid var(--line); border-radius: 4px; overflow: hidden; flex: none; }
.seg button { border: none; border-radius: 0; background: transparent; color: var(--muted); padding: 4px 8px; font-size: 12px; white-space: nowrap; }
@media (max-width: 1450px) { .seg { display: none; } }
.seg button + button { border-left: 1px solid var(--line); }
.seg button.on { background: var(--accent); color: #06111a; font-weight: 600; }
.seg:empty { display: none; }
.search { display: flex; }
.search input { width: 200px; border-radius: 4px 0 0 4px; }
.search button { border-radius: 0 4px 4px 0; }
.badge { padding: 2px 8px; border-radius: 3px; font-size: 11px; font-weight: 600; letter-spacing: .4px; }
.badge.warn { background: var(--warn); color: #201700; }
.muted { color: var(--muted); }
.small { font-size: 11px; }
.tabs { display: flex; gap: 4px; }
.tabs button { background: transparent; border: 1px solid transparent; color: var(--muted); padding: 6px 10px; border-radius: 4px; cursor: pointer; }
.tabs button:hover { color: var(--text); border-color: var(--line); }
.tabs button.on { color: var(--text); background: var(--panel2); border-color: var(--line); }
.tabs a.tab-link { color: var(--muted); text-decoration: none; padding: 6px 10px; border: 1px solid transparent; border-radius: 4px; }
.tabs a.tab-link:hover { color: var(--text); border-color: var(--line); }
#map.drawing { cursor: crosshair; }
button.active { border-color: var(--warn); color: var(--warn); }

/* ---------- controls ---------- */
input, select, button { font: inherit; color: var(--text); background: var(--panel2); border: 1px solid var(--line); border-radius: 4px; padding: 4px 7px; }
input[type=number] { width: 64px; }
input[type=range] { padding: 0; vertical-align: middle; width: 130px; accent-color: var(--accent); }
input[type=checkbox] { accent-color: var(--accent); }
button { cursor: pointer; }
button:hover { border-color: var(--accent); }
button.close { background: transparent; border: none; font-size: 18px; line-height: 1; padding: 0 4px; color: var(--muted); }
button.close:hover { color: var(--text); }

/* ---------- panels ---------- */
.panel { position: absolute; z-index: 900; background: var(--panel); border: 1px solid var(--line); border-radius: 6px;
  box-shadow: 0 6px 24px rgba(0,0,0,.45); display: flex; flex-direction: column; max-height: calc(100% - 80px); }
.panel[hidden] { display: none; }
.panel.right { top: calc(var(--topbar-h, 56px) + 12px); right: 12px; width: 300px; }
.panel.right.wide { width: 440px; }
.panel.bottomleft { left: 12px; bottom: 12px; width: min(980px, calc(100% - 340px)); height: 400px; }
.phead { display: flex; justify-content: space-between; align-items: center; padding: 7px 10px; font-weight: 600;
  background: var(--panel2); border-bottom: 1px solid var(--line); border-radius: 6px 6px 0 0; }
.phead-actions { display: flex; gap: 6px; align-items: center; }
.phead-actions button { padding: 2px 8px; font-size: 12px; }
.pbody { padding: 10px; overflow: auto; }
.pbody h4 { margin: 12px 0 6px; font-size: 12px; text-transform: uppercase; letter-spacing: .5px; color: var(--muted); }
.row { display: flex; align-items: center; gap: 8px; margin: 6px 0; flex-wrap: wrap; }
.row label { display: flex; align-items: center; gap: 6px; }

/* ramp */
.ramp-wrap { margin-bottom: 8px; }
#ramp { width: 100%; height: 14px; border-radius: 3px; display: block; }
.ramp-labels { display: flex; justify-content: space-between; font-size: 11px; color: var(--muted); margin-top: 3px; }

/* kv tables */
table.kv { border-collapse: collapse; width: 100%; }
table.kv td { padding: 2px 6px 2px 0; vertical-align: top; }
table.kv td:first-child { color: var(--muted); white-space: nowrap; width: 38%; }
table.kv td:last-child { font-family: var(--mono); font-size: 12px; }

/* data grid */
.tscroll { overflow: auto; max-height: 260px; border: 1px solid var(--line); border-radius: 4px; }
.tscroll.tall { max-height: 70vh; }
table.grid { border-collapse: collapse; width: 100%; font-size: 12px; }
table.grid th, table.grid td { padding: 3px 8px; border-bottom: 1px solid var(--line); text-align: right; white-space: nowrap; font-family: var(--mono); }
table.grid th { position: sticky; top: 0; background: var(--panel2); color: var(--muted); font-family: var(--font); cursor: pointer; }
table.grid th:first-child, table.grid td:first-child { text-align: left; }
table.grid tr:hover td { background: #202a36; }

/* kpis */
.kpis { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 8px 0; }
.kpi { background: var(--panel2); border: 1px solid var(--line); border-radius: 4px; padding: 6px 8px; }
.kpi .v { font-size: 18px; font-weight: 600; font-family: var(--mono); }
.kpi .l { font-size: 11px; color: var(--muted); }
.cls { display: grid; grid-template-columns: 120px 1fr 70px; align-items: center; gap: 8px; font-size: 12px; margin: 3px 0; }
.cls .bar { height: 10px; border-radius: 2px; background: var(--line); overflow: hidden; }
.cls .bar i { display: block; height: 100%; }
.cls .n { text-align: right; font-family: var(--mono); }

/* point panel */
.ptbody { display: grid; grid-template-columns: 330px 1fr; grid-template-rows: 1fr auto; gap: 8px 14px; height: 100%; padding: 8px 10px; }
.ptmeta { grid-row: 1 / span 2; overflow: auto; }
.ptchart { position: relative; min-height: 0; }
.ptctl { display: flex; gap: 14px; align-items: center; flex-wrap: wrap; color: var(--muted); }
.ptfit { margin-top: 8px; font-size: 12px; padding: 6px 8px; background: var(--panel2); border-radius: 4px; border: 1px solid var(--line); }
.ptfit b { font-family: var(--mono); color: var(--text); }
.help p { margin: 6px 0; }
.help code { font-family: var(--mono); background: var(--panel2); padding: 0 4px; border-radius: 3px; }

/* modal */
.modal { position: fixed; inset: 0; z-index: 2000; background: rgba(0,0,0,.55); display: flex; align-items: center; justify-content: center; }
.modal[hidden] { display: none; }
.modal-box { background: var(--panel); border: 1px solid var(--line); border-radius: 6px; width: min(900px, 92vw); max-height: 90vh; overflow: auto; }

#toast { position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%); z-index: 3000;
  background: var(--panel2); border: 1px solid var(--accent); padding: 8px 14px; border-radius: 4px; }

.leaflet-control-attribution { background: rgba(15,20,25,.7) !important; color: var(--muted) !important; }
.leaflet-control-attribution a { color: var(--accent) !important; }
.leaflet-bar a { background: var(--panel2) !important; color: var(--text) !important; border-color: var(--line) !important; }
.leaflet-control-layers { background: var(--panel) !important; color: var(--text) !important; border: 1px solid var(--line) !important; border-radius: 4px !important; font-size: 12px; }
.leaflet-control-layers-expanded { padding: 6px 10px 6px 6px !important; }
.leaflet-control-layers label { display: block; margin: 2px 0; cursor: pointer; }
.leaflet-control-scale-line { background: rgba(15,20,25,.6) !important; color: var(--text) !important; border-color: var(--muted) !important; }

@media (max-width: 900px) {
  .panel.right, .panel.right.wide { width: calc(100% - 24px); }
  .panel.bottomleft { width: calc(100% - 24px); height: 60vh; }
  .ptbody { grid-template-columns: 1fr; grid-template-rows: auto 1fr auto; }
  .ptmeta { grid-row: auto; max-height: 120px; }
  .brand .subtitle, .topcontrols label { display: none; }
}

/* ---------- auth ---------- */
.tabs form.logout { display: inline; margin: 0; }
.tabs form.logout button { background: transparent; border: 1px solid transparent; color: var(--muted); padding: 6px 10px; border-radius: 4px; }
.tabs form.logout button:hover { color: var(--text); border-color: var(--line); }

/* ---------- phones / narrow screens ---------- */
@media (max-width: 700px) {
  #topbar { height: auto; flex-wrap: wrap; gap: 6px 10px; padding: 6px 10px 4px; white-space: normal; }
  .brand { width: 100%; }
  .brand .title { font-size: 14px; }
  .topcontrols { order: 2; width: 100%; flex: none; overflow: visible; gap: 6px; }
  .topcontrols label { display: flex; flex: 1 1 auto; min-width: 0; }
  #dataset { flex: 1; max-width: none; min-width: 0; }
  .search { flex: 0 0 auto; }
  .search input { width: 120px; }
  #loadInfo, #demoBadge { display: none; }
  .tabs { order: 3; width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; scrollbar-width: none; padding-bottom: 2px; }
  .tabs::-webkit-scrollbar { display: none; }
  .tabs button, .tabs a.tab-link, .tabs form.logout button { white-space: nowrap; padding: 7px 10px; }
  /* panels become bottom sheets so the top half of the map stays visible and tappable */
  .panel.right, .panel.right.wide, .panel.bottomleft {
    position: fixed; top: auto; left: 0; right: 0; bottom: 0; width: auto; height: auto;
    max-height: 58vh; border-radius: 10px 10px 0 0; border-bottom: none; z-index: 950; }
  .panel.bottomleft { height: 62vh; max-height: 62vh; }
  .phead { padding: 9px 12px; }
  .close { font-size: 20px; padding: 0 8px; }
  .leaflet-control-layers { display: none; }             /* Basemap select in the Display panel does the same */
  .leaflet-top.leaflet-left { top: 4px; }
  .leaflet-control-scale { margin-bottom: 4px !important; }
  .kpis { grid-template-columns: repeat(2, 1fr); }
  #toast { bottom: 64vh; max-width: 90vw; }
  .panel.right, .panel.right.wide, .panel.bottomleft { z-index: 1100; }   /* above Leaflet's scale / attribution */
  #pointPanel .phead { flex-wrap: wrap; gap: 4px 8px; }
  #pointPanel #ptTitle { flex: 1 1 100%; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
  #pointPanel .phead-actions { flex: 1 1 100%; flex-wrap: wrap; justify-content: flex-end; }
}
#aoiBuf { width: 64px; }
