:root {
  --bg: #111318;
  --surface: #181b21;
  --surface-2: #1f232b;
  --surface-3: #272c35;
  --border: #2d323c;
  --text: #e7e9ee;
  --muted: #9aa2b0;
  --faint: #6b7381;
  --accent: #5b9cff;
  --accent-text: #0b1220;
  --ok: #3ecf8e;
  --warn: #ffb547;
  --bad: #ff6b6b;
  --radius: 8px;
  --topbar-h: 52px;
  --panel-w: 380px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.4 var(--font);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
[hidden] { display: none !important; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* top bar */
.topbar {
  position: fixed; inset: 0 0 auto 0; height: var(--topbar-h); z-index: 20;
  display: flex; align-items: center; gap: 12px; padding: 0 12px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 650; letter-spacing: .2px; font-size: 15px; white-space: nowrap; }
.search { position: relative; flex: 0 1 420px; min-width: 0; }
.search input {
  width: 100%; height: 36px; padding: 0 12px; border-radius: var(--radius);
  background: var(--surface-2); border: 1px solid var(--border); outline: none;
}
.search input:focus { border-color: var(--accent); }
.search input::-webkit-search-cancel-button { filter: invert(.7); }
.search-results {
  position: absolute; top: 42px; left: 0; right: 0; max-height: min(60vh, 520px); overflow: auto;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: 0 10px 30px rgba(0,0,0,.45); padding: 4px 0; z-index: 30;
}
.sr-group { padding: 6px 12px 2px; color: var(--faint); font-size: 12px; text-transform: uppercase; letter-spacing: .5px; }
.sr-item { display: flex; gap: 10px; align-items: center; padding: 7px 12px; cursor: pointer; }
.sr-item[aria-selected="true"], .sr-item:hover { background: var(--surface-3); }
.sr-item .sr-main { min-width: 0; flex: 1; }
.sr-item .sr-title { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sr-item .sr-sub { color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.actions { display: flex; gap: 6px; }
.tb-btn {
  height: 34px; padding: 0 12px; border-radius: var(--radius); white-space: nowrap;
  background: transparent; border: 1px solid transparent; color: var(--text);
  display: inline-flex; align-items: center; gap: 6px;
}
.tb-btn:hover, .tb-btn[aria-expanded="true"], .tb-btn.active { background: var(--surface-3); }
.count {
  min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px; font-size: 12px; line-height: 20px;
  text-align: center; background: var(--warn); color: #1d1300; font-weight: 600;
}
.user { margin-left: auto; display: flex; gap: 12px; align-items: center; white-space: nowrap; min-width: 0; }
.user-name { color: var(--muted); overflow: hidden; text-overflow: ellipsis; max-width: 220px; }
.signout { color: var(--text); }
.menu-btn { display: none; margin-left: auto; width: 40px; padding: 0; justify-content: center; }
.menu-icon, .menu-icon::before, .menu-icon::after {
  display: block; width: 18px; height: 2px; background: var(--text); border-radius: 1px; position: relative; content: "";
}
.menu-icon::before { position: absolute; top: -6px; }
.menu-icon::after { position: absolute; top: 6px; }

/* popovers */
.popover {
  position: fixed; top: calc(var(--topbar-h) + 6px); z-index: 25;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: 0 12px 32px rgba(0,0,0,.5); padding: 8px; max-height: calc(100vh - var(--topbar-h) - 24px); overflow: auto;
}
.popover-layers { width: 320px; }
.popover-tools { width: 220px; padding: 4px; }
.popover-phone { right: 8px; left: 8px; }
.menu-item {
  display: flex; align-items: center; width: 100%; padding: 9px 10px; border: 0; border-radius: 6px;
  background: transparent; text-align: left; gap: 8px;
}
.menu-item:hover, .menu-item.active { background: var(--surface-3); }
.menu-item .count { margin-left: auto; }
.menu-sep { height: 1px; background: var(--border); margin: 4px 0; }

/* layer list */
.layer-row { padding: 6px 4px; border-bottom: 1px solid var(--border); }
.layer-row:last-child { border-bottom: 0; }
.layer-head { display: flex; align-items: center; gap: 8px; }
.layer-head label { display: flex; align-items: center; gap: 8px; flex: 1; cursor: pointer; padding: 4px 0; }
.layer-sub { padding: 4px 0 4px 26px; display: grid; gap: 6px; }
.layer-sub .row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.layer-sub .row > label { color: var(--muted); min-width: 42px; }
.legend { display: grid; grid-template-columns: 1fr 1fr; gap: 3px 10px; font-size: 12px; color: var(--muted); }
.legend-item { display: flex; align-items: center; gap: 6px; min-width: 0; }
.legend-item span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.legend-item canvas, .legend-item img { flex: none; }
button.legend-item { background: none; border: 0; padding: 2px 0; text-align: left; color: inherit; font-size: 12px; }
button.legend-item:hover span { color: var(--text); }
button.legend-item[aria-pressed="false"] { opacity: .4; }
.ramp { height: 8px; border-radius: 4px; grid-column: 1 / -1; }
.ramp-labels { display: flex; justify-content: space-between; grid-column: 1 / -1; }
input[type="checkbox"] { width: 16px; height: 16px; accent-color: var(--accent); margin: 0; }

/* segmented control */
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; }
.seg button { border: 0; background: transparent; padding: 4px 10px; color: var(--muted); }
.seg button + button { border-left: 1px solid var(--border); }
.seg button[aria-pressed="true"] { background: var(--surface-3); color: var(--text); }

/* form controls */
.input, select.input, textarea.input {
  height: 32px; padding: 0 8px; border-radius: 6px; background: var(--surface-2); border: 1px solid var(--border);
  color-scheme: dark; min-width: 0;
}
textarea.input { height: auto; padding: 8px; font-family: var(--mono); font-size: 13px; resize: vertical; width: 100%; }
.btn {
  height: 32px; padding: 0 12px; border-radius: 6px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text); white-space: nowrap;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
}
.btn:hover { background: var(--surface-3); text-decoration: none; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.small { height: 26px; padding: 0 8px; font-size: 12px; }
.btn[disabled] { opacity: .5; cursor: default; }
.btn.on { border-color: var(--accent); color: var(--accent); }
.btn-row { display: flex; flex-wrap: wrap; gap: 6px; }
.icon-btn {
  width: 30px; height: 30px; border-radius: 6px; border: 0; background: transparent; color: var(--muted);
  display: inline-flex; align-items: center; justify-content: center; font-size: 18px; line-height: 1;
}
.icon-btn:hover { background: var(--surface-3); color: var(--text); }

/* main area */
.main { position: fixed; inset: var(--topbar-h) 0 0 0; display: flex; }
.map { flex: 1; position: relative; min-width: 0; }
.map.picking .maplibregl-canvas-container { cursor: crosshair; }

/* side panel */
.panel {
  width: var(--panel-w); flex: none; background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; min-height: 0; z-index: 10;
}
.panel.wide { width: min(720px, 60vw); }
.panel-head {
  display: flex; align-items: center; gap: 4px; padding: 8px 8px 8px 12px; border-bottom: 1px solid var(--border);
  min-height: 48px;
}
.panel-title { flex: 1; font-weight: 600; font-size: 15px; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.panel-body { overflow: auto; padding: 12px; display: grid; grid-auto-rows: max-content; grid-template-columns: minmax(0, 1fr); gap: 14px; align-content: start; flex: 1; min-height: 0; }
.panel-grip { display: none; }

.kv { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }
.subtitle { color: var(--muted); }
.chips { display: flex; flex-wrap: wrap; gap: 4px; }
.chip {
  display: inline-flex; align-items: center; gap: 4px; padding: 1px 8px; border-radius: 10px; font-size: 12px;
  background: var(--surface-3); color: var(--text); border: 1px solid var(--border); white-space: nowrap;
}
.chip.warn { border-color: rgba(255,181,71,.5); color: var(--warn); }
.chip.bad { border-color: rgba(255,107,107,.5); color: var(--bad); }
.chip.ok { border-color: rgba(62,207,142,.5); color: var(--ok); }
.chip.line { border-style: dashed; color: var(--warn); }
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; flex: none; }

.section { display: grid; gap: 6px; }
.section-head { display: flex; align-items: center; gap: 4px 8px; min-height: 26px; flex-wrap: wrap; }
.section-head h3 { margin: 0; font-size: 13px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .5px; white-space: nowrap; }
.section-head .n { color: var(--faint); font-size: 12px; white-space: nowrap; }
.section-head .spacer { flex: 1; }

.list { display: grid; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; }
.list-item { display: flex; gap: 10px; align-items: center; padding: 8px 10px; background: var(--surface-2); min-width: 0; }
.list-item + .list-item { border-top: 1px solid var(--border); }
.list-item.click { cursor: pointer; }
.list-item.click:hover { background: var(--surface-3); }
.list-item.sel { box-shadow: inset 3px 0 0 var(--accent); }
.li-main { flex: 1; min-width: 0; }
.li-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.li-sub { color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.li-side { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.li-side .li-sub { text-align: right; }
.list-item.stack { flex-wrap: wrap; row-gap: 6px; }
.li-below { flex-basis: 100%; padding-left: 24px; }
.legend.one { grid-template-columns: 1fr; }
.li-num {
  width: 22px; height: 22px; border-radius: 50%; background: var(--accent); color: var(--accent-text);
  font-size: 12px; font-weight: 700; display: inline-flex; align-items: center; justify-content: center; flex: none;
}
.mono { font-family: var(--mono); }
.error { color: var(--bad); }
.total { font-size: 15px; font-weight: 600; font-variant-numeric: tabular-nums; }

.spinner {
  width: 14px; height: 14px; border-radius: 50%; border: 2px solid var(--surface-3); border-top-color: var(--accent);
  animation: spin .8s linear infinite; display: inline-block; flex: none;
}
@keyframes spin { to { transform: rotate(360deg); } }

.coords { font-family: var(--mono); font-size: 16px; padding: 8px 10px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 6px; user-select: all; overflow-wrap: anywhere; }

/* table (SQL) */
.table-wrap { overflow: auto; border: 1px solid var(--border); border-radius: 6px; max-height: 55vh; }
table.data { border-collapse: collapse; font-size: 12px; width: max-content; min-width: 100%; }
table.data th, table.data td { padding: 4px 8px; border-bottom: 1px solid var(--border); text-align: left; white-space: nowrap; max-width: 360px; overflow: hidden; text-overflow: ellipsis; }
table.data th { position: sticky; top: 0; background: var(--surface-3); font-weight: 600; }
table.data td { font-family: var(--mono); }

/* mode bar, sync status, toast */
.mode-bar {
  position: absolute; top: 10px; left: 50%; transform: translateX(-50%); z-index: 12;
  display: flex; align-items: center; gap: 10px; padding: 6px 6px 6px 12px;
  background: var(--surface); border: 1px solid var(--accent); border-radius: 18px; box-shadow: 0 6px 20px rgba(0,0,0,.45);
  white-space: nowrap;
}
.main .panel:not([hidden]) ~ .mode-bar { left: calc(50% + var(--panel-w) / 2); }
.sync-status {
  position: absolute; left: 10px; bottom: 34px; z-index: 5; font-size: 12px; color: var(--muted);
  background: rgba(17,19,24,.82); padding: 2px 8px; border-radius: 10px; border: 1px solid var(--border);
}
.main .panel:not([hidden]) ~ .sync-status { left: calc(var(--panel-w) + 10px); }
.sync-status.warn { color: var(--warn); border-color: rgba(255,181,71,.5); }
.sync-status.bad { color: var(--bad); border-color: rgba(255,107,107,.5); }
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%); z-index: 40;
  background: var(--surface-3); border: 1px solid var(--border); padding: 8px 14px; border-radius: 18px;
  box-shadow: 0 8px 24px rgba(0,0,0,.5);
}
.toast.bad { border-color: var(--bad); color: var(--bad); }

.fatal {
  position: fixed; top: calc(var(--topbar-h) + 40px); left: 50%; transform: translateX(-50%); z-index: 50;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px;
  display: flex; gap: 10px; align-items: center; box-shadow: 0 12px 32px rgba(0,0,0,.5); max-width: 92vw;
}
details > summary { cursor: pointer; padding: 3px 0; }
details > .chips { padding: 4px 0 8px; }
.subtitle.menu-item { cursor: default; }
.subtitle.menu-item:hover { background: transparent; }

/* map pin (Pin for Dolibarr) */
.pin-marker { width: 26px; height: 36px; cursor: grab; }
.pin-marker svg { display: block; }

/* MapLibre controls, dark */
.maplibregl-ctrl-group { background: var(--surface); border: 1px solid var(--border); box-shadow: none; }
.maplibregl-ctrl-group button { width: 32px; height: 32px; }
.maplibregl-ctrl-group button + button { border-top: 1px solid var(--border); }
.maplibregl-ctrl button .maplibregl-ctrl-icon { filter: invert(1) brightness(.9); }
.maplibregl-ctrl-attrib, .maplibregl-ctrl-attrib.maplibregl-compact { background: rgba(17,19,24,.82); color: var(--muted); }
.maplibregl-ctrl-attrib a { color: var(--muted); }
.maplibregl-ctrl-attrib.maplibregl-compact-show { color: var(--muted); }
.maplibregl-ctrl-attrib-button { filter: invert(1); background-color: transparent; }
.maplibregl-ctrl-scale { background: rgba(17,19,24,.7); color: var(--muted); border-color: var(--muted); }
.maplibregl-popup-content { background: var(--surface); color: var(--text); border: 1px solid var(--border); border-radius: 6px; padding: 6px 10px; box-shadow: 0 6px 18px rgba(0,0,0,.5); }
.maplibregl-popup-anchor-bottom .maplibregl-popup-tip { border-top-color: var(--border); }
.maplibregl-popup-anchor-top .maplibregl-popup-tip { border-bottom-color: var(--border); }
.maplibregl-popup-anchor-left .maplibregl-popup-tip { border-right-color: var(--border); }
.maplibregl-popup-anchor-right .maplibregl-popup-tip { border-left-color: var(--border); }
.hover-label { font-size: 12px; white-space: nowrap; }

/* phone */
@media (max-width: 760px) {
  :root { --topbar-h: 52px; }
  body { font-size: 15px; }
  .brand, .actions, .user { display: none; }
  .search { flex: 1; }
  .menu-btn { display: inline-flex; }
  .main { flex-direction: column; }
  .panel {
    position: absolute; left: 0; right: 0; bottom: 0; width: auto; max-height: 52%;
    border-right: 0; border-top: 1px solid var(--border); border-radius: 12px 12px 0 0;
    box-shadow: 0 -10px 30px rgba(0,0,0,.45);
  }
  .panel.expanded { max-height: calc(100% - 8px); }
  .panel.wide { width: auto; }
  .panel-grip { display: block; width: 40px; height: 4px; border-radius: 2px; background: var(--border); margin: 6px auto 0; border: 0; padding: 0; }
  .panel-head { min-height: 44px; padding-top: 4px; }
  .main .panel:not([hidden]) ~ .mode-bar { left: 50%; }
  .main .panel:not([hidden]) ~ .sync-status { left: 10px; }
  .mode-bar { top: 8px; max-width: calc(100% - 16px); }
  .sync-status { top: 8px; bottom: auto; left: 8px; }
  .popover-layers { left: 8px; right: 8px; width: auto; }
  .popover-tools { left: 8px; right: 8px; width: auto; }
  .list-item { padding: 10px; }
  .btn { height: 36px; }
  .btn.small { height: 30px; }
  .maplibregl-ctrl-bottom-right { bottom: 0; }
}
