:root {
  --surface: #fcfcfb; --plane: #f9f9f7; --ink: #0b0b0b; --ink-2: #52514e; --muted: #898781;
  --grid: #e1e0d9; --axis: #c3c2b7; --ring: rgba(11,11,11,0.10);
  --accent: #256abf; --accent-ink: #ffffff; --bar: #2a78d6; --good: #006300;
  --font: "Inter", "IBM Plex Sans Arabic", system-ui, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --surface: #1a1a19; --plane: #0d0d0d; --ink: #f0efec; --ink-2: #c3c2b7; --muted: #898781;
    --grid: #2c2c2a; --axis: #383835; --ring: rgba(255,255,255,0.10); --accent: #3987e5; --bar: #3987e5; --good: #0ca30c;
  }
}
:root[data-theme="dark"] {
  --surface: #1a1a19; --plane: #0d0d0d; --ink: #f0efec; --ink-2: #c3c2b7; --muted: #898781;
  --grid: #2c2c2a; --axis: #383835; --ring: rgba(255,255,255,0.10); --accent: #3987e5; --bar: #3987e5; --good: #0ca30c;
}
html[lang="ar"] { --font: "IBM Plex Sans Arabic", "Inter", system-ui, sans-serif; }

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--plane); color: var(--ink); font: 14px/1.45 var(--font); display: flex; flex-direction: column; }
button, select, textarea, input { font: inherit; color: inherit; }

.topbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 16px;
  background: var(--surface); border-bottom: 1px solid var(--ring); }
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand-mark { width: 28px; height: 28px; border-radius: 8px; flex: none;
  background: conic-gradient(from 210deg, #256abf, #1baf7a, #eda100, #256abf); }
.brand-title { font-weight: 600; font-size: 15px; }
.brand-sub { color: var(--ink-2); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-right { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.pill { font-size: 12px; padding: 3px 9px; border-radius: 999px; border: 1px solid var(--ring); color: var(--ink-2); white-space: nowrap; }
.pill::before { content: "●"; margin-inline-end: 5px; color: var(--muted); }
.pill.on::before { color: var(--good); }
.btn-ghost { background: none; border: 1px solid var(--ring); border-radius: 8px; padding: 5px 12px; cursor: pointer; }
.btn-primary { background: var(--accent); color: var(--accent-ink); border: 0; border-radius: 8px; padding: 8px 16px; cursor: pointer; font-weight: 500; }
.btn-primary:disabled { opacity: .5; cursor: default; }

.layout { flex: 1; display: flex; min-height: 0; }
.map-wrap { position: relative; flex: 1; min-width: 0; }
#map { position: absolute; inset: 0; }
.side { width: 420px; flex: none; display: flex; flex-direction: column; background: var(--surface);
  border-inline-start: 1px solid var(--ring); min-height: 0; }

.map-panel { position: absolute; inset-inline-start: 12px; bottom: 28px; z-index: 2; background: var(--surface);
  border: 1px solid var(--ring); border-radius: 10px; padding: 10px 12px; max-width: 240px; box-shadow: 0 2px 10px rgba(0,0,0,.08); }
.panel-title { font-size: 12px; font-weight: 600; color: var(--ink-2); margin-bottom: 6px; }
.row { display: flex; align-items: center; gap: 6px; font-size: 13px; padding: 2px 0; cursor: pointer; }
.swatch { width: 10px; height: 10px; border-radius: 50%; flex: none; box-shadow: 0 0 0 2px var(--surface); }
.legend { margin-top: 8px; font-size: 11px; color: var(--ink-2); }
.legend-ramp { display: flex; height: 8px; border-radius: 4px; overflow: hidden; margin: 4px 0 2px; }
.legend-ramp span { flex: 1; }
.legend-ends { display: flex; justify-content: space-between; color: var(--muted); }

.map-tools { position: absolute; top: 12px; inset-inline-start: 12px; z-index: 2; display: flex; gap: 6px; align-items: center;
  background: var(--surface); border: 1px solid var(--ring); border-radius: 10px; padding: 6px 8px; }
.btn-tool { background: none; border: 1px solid var(--ring); border-radius: 7px; padding: 4px 10px; cursor: pointer; font-size: 13px; }
.btn-tool.active { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.map-tools select { border: 1px solid var(--ring); border-radius: 6px; background: var(--surface); padding: 3px; }
.map-note { position: absolute; top: 60px; inset-inline-start: 12px; z-index: 2; background: var(--surface); border: 1px solid var(--ring);
  border-radius: 10px; padding: 8px 12px; max-width: 360px; font-size: 13px; }
.muted { color: var(--muted); font-size: 12px; }

.tabs { display: flex; border-bottom: 1px solid var(--ring); padding: 0 8px; }
.tabs button { background: none; border: 0; padding: 12px 12px 10px; cursor: pointer; color: var(--ink-2); border-bottom: 2px solid transparent; font-weight: 500; }
.tabs button.active { color: var(--ink); border-bottom-color: var(--accent); }
.tab-body { flex: 1; overflow: auto; padding: 14px 16px; min-height: 0; }
#tab-ai { display: flex; flex-direction: column; gap: 10px; }
#tab-ai[hidden] { display: none; }

.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(118px, 1fr)); gap: 8px; }
.tile { border: 1px solid var(--ring); border-radius: 10px; padding: 10px 12px; }
.tile-label { font-size: 12px; color: var(--ink-2); }
.tile-value { font-size: 20px; font-weight: 600; font-variant-numeric: tabular-nums; }
.filters { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 14px 0 6px; }
.filters label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--ink-2); }
.filters select { border: 1px solid var(--ring); border-radius: 8px; padding: 6px; background: var(--surface); color: var(--ink); }
.section-title { font-size: 13px; font-weight: 600; margin: 14px 0 8px; }

.bars { display: flex; flex-direction: column; gap: 4px; }
.bar-row { display: grid; grid-template-columns: 110px 1fr 64px; align-items: center; gap: 8px; cursor: pointer;
  padding: 3px 4px; border-radius: 6px; font-size: 12.5px; }
.bar-row:hover, .bar-row.sel { background: color-mix(in srgb, var(--accent) 10%, transparent); }
.bar-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--ink); }
.bar-track { height: 10px; }
.bar-fill { height: 100%; background: var(--bar); border-start-end-radius: 4px; border-end-end-radius: 4px; min-width: 2px; }
.bar-val { text-align: end; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.footnote { color: var(--muted); font-size: 11.5px; margin-top: 14px; }

.chat { flex: 1; overflow: auto; display: flex; flex-direction: column; gap: 10px; min-height: 120px; }
.msg { padding: 9px 12px; border-radius: 12px; max-width: 92%; white-space: pre-wrap; }
.msg.user { align-self: flex-end; background: var(--accent); color: var(--accent-ink); }
.msg.bot { align-self: flex-start; background: var(--plane); border: 1px solid var(--ring); }
.msg.bot { white-space: normal; width: 92%; }
.msg.bot p { margin: 0 0 6px; }
.msg.bot ul, .msg.bot ol:not(.steps) { margin: 4px 0 6px; padding-inline-start: 18px; }
.msg.bot code { font-size: 12px; background: var(--grid); padding: 0 4px; border-radius: 4px; }
.md-h { font-weight: 600; margin: 6px 0 4px; }
.steps { list-style: none; margin: 0 0 6px; padding: 0; font-size: 12px; color: var(--ink-2); }
.steps:empty { display: none; }
.step { display: flex; gap: 6px; align-items: baseline; padding: 1px 0; }
.step::before { content: "◌"; color: var(--muted); }
.step.running::before { content: "◌"; animation: pulse 1s infinite; color: var(--accent); }
.step.done::before { content: "✓"; color: var(--good); }
.step.failed::before { content: "!"; color: #d03b3b; font-weight: 600; }
.step[data-summary]::after { content: "· " attr(data-summary); color: var(--muted); }
.typing { color: var(--muted); animation: pulse 1.2s infinite; }
.err { color: #d03b3b; }
@keyframes pulse { 50% { opacity: .35; } }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { border: 1px solid var(--ring); background: var(--surface); border-radius: 999px; padding: 4px 10px; font-size: 12px; cursor: pointer; text-align: start; }
.chat-form { display: flex; gap: 8px; align-items: flex-end; }
.chat-form textarea { flex: 1; resize: none; border: 1px solid var(--ring); border-radius: 10px; padding: 8px 10px; background: var(--surface); }

table.cat { width: 100%; border-collapse: collapse; font-size: 12px; }
table.cat th, table.cat td { text-align: start; padding: 6px 4px; border-bottom: 1px solid var(--grid); vertical-align: top; }
table.cat th { color: var(--ink-2); font-weight: 500; }
table.cat td.num { font-variant-numeric: tabular-nums; }
.caveats { padding-inline-start: 18px; color: var(--ink-2); font-size: 12.5px; }
.caveats li { margin-bottom: 6px; }

.maplibregl-popup-content { background: var(--surface); color: var(--ink); font: 12.5px/1.4 var(--font); border-radius: 10px; }
.pop-title { font-weight: 600; margin-bottom: 4px; }
.pop-grid { display: grid; grid-template-columns: auto auto; gap: 2px 12px; }
.pop-grid span:nth-child(odd) { color: var(--ink-2); }
.pop-grid span:nth-child(even) { text-align: end; font-variant-numeric: tabular-nums; }

@media (max-width: 860px) {
  .layout { flex-direction: column; }
  .map-wrap { flex: none; height: 55vh; }
  .side { width: 100%; border-inline-start: 0; border-top: 1px solid var(--ring); flex: 1; }
  .brand-sub { display: none; }
  .map-panel { max-width: 180px; }
}
.md-table { overflow-x: auto; margin: 6px 0 8px; }
.md-table table { border-collapse: collapse; font-size: 12px; width: 100%; }
.md-table th, .md-table td { padding: 4px 6px; border-bottom: 1px solid var(--grid); text-align: start; white-space: nowrap; }
.md-table th { color: var(--ink-2); font-weight: 500; }

.arcgis-link { text-decoration: none; color: inherit; font-size: 13px; }
