/* ── CSS custom properties ───────────────────────────────────────────────── */

:root {
  /* surfaces */
  --c-bg:        #0f172a;
  --c-surface:   #1e293b;
  --c-surface-2: #334155;
  --c-surface-3: #475569;
  /* text */
  --c-text:         #e2e8f0;
  --c-text-heading: #f1f5f9;
  --c-text-muted:   #94a3b8;
  --c-text-subtle:  #64748b;
  /* accent */
  --c-accent:        #93c5fd;
  --c-accent-bg:     #1e3a5f;
  --c-accent-border: #2d4f7c;
  /* danger */
  --c-danger-bg:   #7f1d1d;
  --c-danger-text: #fca5a5;
  --c-danger-hover:#991b1b;
  /* overlays */
  --c-overlay:       rgba(15, 23, 42, 0.90);
  --c-overlay-md:    rgba(15, 23, 42, 0.92);
  --c-overlay-heavy: rgba(15, 23, 42, 0.85);
  --c-ctrl-bg:       rgba(30, 41, 59, 0.95);
  /* semantic */
  --c-link:         #38bdf8;
  --c-code:         #7dd3fc;
  --c-export-text:  #cbd5e1;
  --c-table-border: rgba(30, 58, 95, 0.13);
}

[data-theme="light"] {
  --c-bg:        #f1f5f9;
  --c-surface:   #ffffff;
  --c-surface-2: #e2e8f0;
  --c-surface-3: #cbd5e1;
  --c-text:         #1e293b;
  --c-text-heading: #0f172a;
  --c-text-muted:   #475569;
  --c-text-subtle:  #64748b;
  --c-accent:        #2563eb;
  --c-accent-bg:     #eff6ff;
  --c-accent-border: #bfdbfe;
  --c-danger-bg:   #fee2e2;
  --c-danger-text: #991b1b;
  --c-danger-hover:#fecaca;
  --c-overlay:       rgba(241, 245, 249, 0.95);
  --c-overlay-md:    rgba(255, 255, 255, 0.96);
  --c-overlay-heavy: rgba(241, 245, 249, 0.92);
  --c-ctrl-bg:       rgba(255, 255, 255, 0.95);
  --c-link:         #0891b2;
  --c-code:         #0369a1;
  --c-export-text:  #334155;
  --c-table-border: rgba(37, 99, 235, 0.08);
}

/* ── Reset ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 13px;
  background: var(--c-bg);
  color: var(--c-text);
  height: 100vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

/* ── Header ── */
#header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 16px;
  height: 46px;
  background: var(--c-surface);
  border-bottom: 1px solid var(--c-surface-2);
  flex-shrink: 0;
}

#header h1 {
  font-size: 15px;
  font-weight: 600;
  color: var(--c-text-heading);
  letter-spacing: 0.02em;
}

#header .subtitle {
  font-size: 11px;
  color: var(--c-text-subtle);
}

.header-controls {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 6px;
}

/* ── Main layout ── */
#main {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* ── Map ── */
#map-wrap {
  position: relative;
  flex: 1;
  min-width: 0;
}

#map {
  width: 100%;
  height: 100%;
}

/* ── Map toolbar (floating) ── */
#toolbar {
  position: absolute;
  top: 10px;
  left: 50px;
  z-index: 10;
  display: flex;
  gap: 6px;
  align-items: center;
  background: var(--c-overlay);
  border: 1px solid var(--c-surface-2);
  border-radius: 8px;
  padding: 6px 8px;
  backdrop-filter: blur(4px);
}

#toolbar .sep { width: 1px; height: 20px; background: var(--c-surface-2); margin: 0 2px; }

/* ── Draw result box ── */
#draw-result {
  position: absolute;
  bottom: 36px;
  left: 50px;
  z-index: 10;
  max-width: 420px;
  background: var(--c-overlay-md);
  border: 1px solid var(--c-surface-2);
  border-radius: 8px;
  padding: 10px 12px;
  display: none;
  backdrop-filter: blur(4px);
}

/* ── Legend ── */
#legend {
  position: absolute;
  bottom: 36px;
  right: 10px;
  z-index: 10;
  background: var(--c-overlay);
  border: 1px solid var(--c-surface-2);
  border-radius: 8px;
  padding: 10px 12px;
  min-width: 140px;
  backdrop-filter: blur(4px);
}

#legend h5 { font-size: 11px; color: var(--c-text-muted); margin-bottom: 6px; text-transform: uppercase; letter-spacing: 0.05em; }
.legend-item { display: flex; align-items: center; gap: 6px; margin-bottom: 4px; font-size: 11px; }
.legend-dot { width: 12px; height: 12px; border-radius: 3px; flex-shrink: 0; }

/* ── Search box ── */
#search-box {
  position: relative;
  padding: 8px 10px;
  border-bottom: 1px solid var(--c-surface-2);
  flex-shrink: 0;
}

.search-input-wrap {
  position: relative;
}

#search-input {
  width: 100%;
  background: var(--c-bg);
  border: 1px solid var(--c-surface-2);
  border-radius: 5px;
  padding: 6px 28px 6px 10px;
  color: var(--c-text);
  font-size: 13px;
  font-family: inherit;
  outline: none;
}
#search-input:focus { border-color: #0891b2; }
#search-input::placeholder { color: var(--c-surface-3); }

#search-clear {
  display: none;
  position: absolute;
  right: 6px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  padding: 2px;
  color: #ef4444;
  line-height: 0;
  border-radius: 3px;
}
#search-clear:hover { color: #f87171; }
#search-clear.visible { display: block; }

#search-suggestions {
  display: none;
  position: absolute;
  top: 100%;
  left: 10px;
  right: 10px;
  background: var(--c-surface);
  border: 1px solid var(--c-surface-2);
  border-top: none;
  border-radius: 0 0 6px 6px;
  z-index: 50;
  max-height: 240px;
  overflow-y: auto;
  box-shadow: 0 6px 16px rgba(0,0,0,0.3);
}

.suggestion-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 7px 10px;
  cursor: pointer;
  border-bottom: 1px solid var(--c-bg);
  gap: 8px;
}
.suggestion-item:last-child { border-bottom: none; }
.suggestion-item:hover, .suggestion-item.selected { background: var(--c-surface-2); }

.suggestion-name {
  font-size: 12px;
  color: var(--c-text);
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.suggestion-type {
  font-size: 10px;
  color: var(--c-text-subtle);
  flex-shrink: 0;
}

.suggestion-none {
  padding: 10px;
  font-size: 12px;
  color: var(--c-text-subtle);
  text-align: center;
}

#search-suggestions mark {
  background: none;
  color: var(--c-link);
  font-weight: 600;
}

/* ── Side panel ── */
#panel {
  width: 340px;
  flex-shrink: 0;
  background: var(--c-surface);
  border-left: 1px solid var(--c-surface-2);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#panel-header {
  padding: 12px 14px;
  border-bottom: 1px solid var(--c-surface-2);
  font-size: 12px;
  color: var(--c-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  flex-shrink: 0;
}

#panel-content {
  flex: 1;
  overflow-y: auto;
  padding: 12px 14px;
}

#panel-content::-webkit-scrollbar { width: 4px; }
#panel-content::-webkit-scrollbar-track { background: transparent; }
#panel-content::-webkit-scrollbar-thumb { background: var(--c-surface-2); border-radius: 2px; }

#panel-footer {
  flex-shrink: 0;
  border-top: 1px solid var(--c-surface-2);
  padding: 8px 14px;
  text-align: center;
}

#panel-footer a {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 11px;
  color: var(--c-text-subtle);
  text-decoration: none;
}
#panel-footer a:hover { color: var(--c-accent); }

/* ── Feature detail ── */
.feature-header {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  margin-bottom: 12px;
}

.feature-header h3 {
  font-size: 14px;
  font-weight: 600;
  color: var(--c-text-heading);
  flex: 1;
  line-height: 1.3;
}

.attr-table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 14px;
}

.attr-table td {
  padding: 4px 0;
  border-bottom: 1px solid var(--c-table-border);
  vertical-align: top;
  line-height: 1.4;
}

.attr-label {
  color: var(--c-text-subtle);
  width: 95px;
  padding-right: 8px;
  font-size: 11px;
}

/* ── Multi-selection remove button ── */
.remove-cell { width: 24px; padding: 0 !important; text-align: center; }
.remove-item {
  background: none;
  border: none;
  color: #ef4444;
  padding: 3px 4px;
  border-radius: 3px;
  line-height: 0;
  opacity: 0.7;
}
.remove-item:hover { opacity: 1; background: rgba(239,68,68,0.12); }

/* ── Query results ── */
.query-header { margin-bottom: 10px; }
.query-header h3 { font-size: 14px; font-weight: 600; color: var(--c-text-heading); }
.query-header .hint { font-size: 11px; color: var(--c-text-subtle); margin-top: 2px; }
.trunc-note { font-size: 11px; color: #F59E0B; margin-bottom: 6px; }

.result-table { width: 100%; border-collapse: collapse; margin-top: 8px; }
.result-table th { font-size: 11px; color: var(--c-text-subtle); text-align: left; padding: 4px 4px; border-bottom: 1px solid var(--c-surface-2); text-transform: uppercase; }
.result-row { cursor: pointer; border-bottom: 1px solid var(--c-surface); }
.result-row:hover { background: var(--c-surface-2); }
.result-row td { padding: 5px 4px; font-size: 12px; }

/* ── Export section ── */
.export-section { border-top: 1px solid var(--c-surface-2); padding-top: 12px; margin-top: 4px; }
.export-section h4 { font-size: 11px; color: var(--c-text-muted); text-transform: uppercase; letter-spacing: 0.05em; margin-bottom: 6px; }
.export-always { font-size: 11px; color: var(--c-text-subtle); margin-bottom: 8px; }

/* Optional field toggles */
.export-opts-row { display: flex; gap: 5px; margin-bottom: 8px; }
.btn-opt {
  padding: 4px 10px;
  background: var(--c-surface-2);
  color: var(--c-text-muted);
  font-size: 11px;
  border: 1px solid var(--c-surface-3);
  border-radius: 4px;
}
.btn-opt:hover:not(:disabled) { background: var(--c-surface-3); color: var(--c-text); }
.btn-opt.active { background: var(--c-accent-bg); color: var(--c-accent); border-color: var(--c-accent-border); }
.btn-opt:disabled { opacity: 0.35; cursor: not-allowed; }

.buffer-control { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.buffer-control input[type=range] { flex: 1; accent-color: #0891b2; }
#buffer-value { font-size: 11px; color: var(--c-text-muted); min-width: 36px; }

/* Bottom action bar: format toggle + copy/download */
.export-bottom {
  display: flex; align-items: center; justify-content: space-between; gap: 6px;
  border-top: 1px solid var(--c-surface-2); padding-top: 8px; margin-top: 4px;
}

/* Format selector (CSV / JSON toggle) */
.export-fmt-group { display: flex; gap: 0; flex-shrink: 0; }
.btn-fmt {
  padding: 3px 9px;
  background: var(--c-surface-2);
  color: var(--c-text-muted);
  font-size: 11px;
  border: 1px solid var(--c-surface-3);
}
.btn-fmt:first-child { border-radius: 4px 0 0 4px; }
.btn-fmt:last-child  { border-radius: 0 4px 4px 0; border-left: none; }
.btn-fmt.active { background: #0f766e; color: #fff; border-color: #0f766e; }
.btn-fmt:hover:not(.active) { background: var(--c-surface-3); color: var(--c-text); }

/* Action buttons (Copy / Download) */
.export-act-group { display: flex; gap: 5px; }
.btn-act-copy, .btn-act-dl {
  display: flex; align-items: center; gap: 4px;
  padding: 4px 10px;
  font-size: 11px;
  justify-content: center;
}
.btn-act-copy {
  background: var(--c-accent-bg);
  color: var(--c-accent);
  border: 1px solid var(--c-accent-border);
}
.btn-act-copy:hover { background: var(--c-accent-border); }
.btn-act-dl { background: #0f766e; color: #fff; border: 1px solid transparent; }
.btn-act-dl:hover { background: #0d9488; }

/* ── Shared buttons ── */
button {
  cursor: pointer;
  border: none;
  border-radius: 5px;
  font-size: 12px;
  font-family: inherit;
  transition: background 0.15s, opacity 0.15s, color 0.15s;
}

.btn-group { display: flex; gap: 4px; }

.btn-tool {
  padding: 5px 10px;
  background: var(--c-accent-bg);
  color: var(--c-accent);
  font-size: 12px;
  border: 1px solid var(--c-accent-border);
}
.btn-tool:hover { background: var(--c-accent-border); }
.btn-tool.active { background: #1d4ed8; color: #fff; border-color: #1d4ed8; }

.btn-basemap {
  padding: 4px 9px;
  background: var(--c-accent-bg);
  color: var(--c-accent);
  font-size: 11px;
  border: 1px solid var(--c-accent-border);
}
.btn-basemap:hover { background: var(--c-accent-border); }
.btn-basemap.active { background: #0369a1; color: #fff; border-color: #0369a1; }

.btn-danger {
  padding: 5px 10px;
  background: var(--c-danger-bg);
  color: var(--c-danger-text);
  font-size: 12px;
}
.btn-danger:hover { background: var(--c-danger-hover); }

#btn-query {
  display: none;
  padding: 5px 12px;
  background: #0891b2;
  color: #fff;
  font-size: 12px;
}
#btn-query:hover { background: #0e7490; }

/* ── Type badge ── */
.type-badge {
  display: inline-block;
  padding: 2px 7px;
  border-radius: 4px;
  font-size: 11px;
  color: #fff;
  white-space: nowrap;
  flex-shrink: 0;
}
.type-badge.small { font-size: 10px; padding: 1px 5px; }

/* ── Draw result content ── */
.hint-text { font-size: 12px; color: var(--c-text-muted); }
.result-label { font-size: 11px; color: var(--c-text-subtle); margin-bottom: 3px; }
.result-bbox { display: block; font-family: monospace; font-size: 12px; color: var(--c-code); margin-bottom: 4px; }
.result-text {
  width: 100%;
  background: var(--c-bg);
  color: var(--c-code);
  border: 1px solid var(--c-surface-2);
  border-radius: 4px;
  padding: 4px 6px;
  font-family: monospace;
  font-size: 11px;
  resize: vertical;
}

.copy-btn-row { display: flex; gap: 5px; margin-top: 5px; }
.copy-btn {
  padding: 3px 10px;
  background: var(--c-surface-2);
  color: var(--c-export-text);
  font-size: 11px;
}
.copy-btn:hover { background: var(--c-surface-3); }

/* ── Loading overlay ── */
#loading {
  position: absolute;
  inset: 0;
  background: var(--c-overlay-heavy);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 100;
  gap: 12px;
}

#loading p { font-size: 14px; color: var(--c-text-muted); }

.spinner {
  width: 36px; height: 36px;
  border: 3px solid var(--c-surface-2);
  border-top-color: #0891b2;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ── Panel placeholder ── */
.panel-placeholder { color: var(--c-surface-3); text-align: center; padding: 40px 16px; line-height: 1.8; font-size: 13px; }

/* ── Status bar (phase-2 background load) ── */
#status-bar {
  position: absolute;
  bottom: 30px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--c-overlay-heavy);
  color: var(--c-text-muted);
  font-size: 12px;
  padding: 4px 12px;
  border-radius: 4px;
  z-index: 50;
  pointer-events: none;
  white-space: nowrap;
}
#status-bar.error { color: #f87171; }

.geo-pending { color: var(--c-text-muted); font-size: 10px; margin-left: 4px; }

/* ── Hover tooltip ── */
#hover-tooltip {
  display: none;
  position: absolute;
  background: var(--c-overlay-md);
  color: var(--c-text);
  font-size: 12px;
  padding: 4px 8px;
  border-radius: 4px;
  pointer-events: none;
  z-index: 60;
  max-width: 220px;
  white-space: normal;
  box-shadow: 0 2px 6px rgba(0,0,0,0.3);
}

/* ── Help / theme buttons ── */
.btn-help {
  margin-left: 6px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--c-surface-2);
  color: var(--c-text-muted);
  font-size: 13px;
  font-weight: 700;
  line-height: 1;
  border: 1px solid var(--c-surface-3);
  cursor: pointer;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
.btn-help:hover { background: var(--c-surface-3); color: var(--c-text); }

/* ── Help modal ── */
#help-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(2px);
}
#help-overlay.hidden { display: none; }

#help-modal {
  position: relative;
  background: var(--c-surface);
  border: 1px solid var(--c-surface-2);
  border-radius: 10px;
  padding: 28px 32px;
  max-width: 560px;
  width: 90%;
  max-height: 85vh;
  overflow-y: auto;
  box-shadow: 0 8px 32px rgba(0,0,0,0.5);
}

#help-modal h2 {
  font-size: 16px;
  font-weight: 600;
  color: var(--c-text-heading);
  margin-bottom: 20px;
}

#help-modal h3 {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: #0891b2;
  margin: 18px 0 8px;
}

#help-modal p, #help-modal li {
  font-size: 13px;
  color: var(--c-export-text);
  line-height: 1.6;
}

#help-modal ul { padding-left: 18px; }
#help-modal li { margin-bottom: 5px; }

#help-modal a { color: var(--c-link); text-decoration: none; }
#help-modal a:hover { text-decoration: underline; }

#help-close {
  position: absolute;
  top: 14px;
  right: 16px;
  background: none;
  border: none;
  color: var(--c-text-subtle);
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 4px;
}
#help-close:hover { background: var(--c-surface-2); color: var(--c-text); }

/* ── MapLibre draw overrides ── */
.mapboxgl-ctrl-group { background: var(--c-ctrl-bg) !important; border-color: var(--c-surface-2) !important; }
.mapboxgl-ctrl-group button { color: var(--c-accent) !important; }
.mapboxgl-ctrl-group button:hover { background: var(--c-surface-2) !important; }

/* ── MapLibre navigation controls ── */
.maplibregl-ctrl-group { background: var(--c-ctrl-bg) !important; border-color: var(--c-surface-2) !important; }
.maplibregl-ctrl-group button { color: var(--c-accent) !important; display: flex; align-items: center; justify-content: center; }
.maplibregl-ctrl-group button:hover { background: var(--c-surface-2) !important; }

/* ── Zoom +/- icons ── */
.maplibregl-ctrl button.maplibregl-ctrl-zoom-in .maplibregl-ctrl-icon {
  background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='29' height='29' fill='%2393c5fd' viewBox='0 0 29 29'%3E%3Cpath d='M14.5 8.5c-.75 0-1.5.75-1.5 1.5v3h-3c-.75 0-1.5.75-1.5 1.5S9.25 16 10 16h3v3c0 .75.75 1.5 1.5 1.5S16 19.75 16 19v-3h3c.75 0 1.5-.75 1.5-1.5S19.75 13 19 13h-3v-3c0-.75-.75-1.5-1.5-1.5z'/%3E%3C/svg%3E");
}
.maplibregl-ctrl button.maplibregl-ctrl-zoom-out .maplibregl-ctrl-icon {
  background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='29' height='29' fill='%2393c5fd' viewBox='0 0 29 29'%3E%3Cpath d='M10 13c-.75 0-1.5.75-1.5 1.5S9.25 16 10 16h9c.75 0 1.5-.75 1.5-1.5S19.75 13 19 13h-9z'/%3E%3C/svg%3E");
}

[data-theme="light"] .maplibregl-ctrl button.maplibregl-ctrl-zoom-in .maplibregl-ctrl-icon {
  background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='29' height='29' fill='%232563eb' viewBox='0 0 29 29'%3E%3Cpath d='M14.5 8.5c-.75 0-1.5.75-1.5 1.5v3h-3c-.75 0-1.5.75-1.5 1.5S9.25 16 10 16h3v3c0 .75.75 1.5 1.5 1.5S16 19.75 16 19v-3h3c.75 0 1.5-.75 1.5-1.5S19.75 13 19 13h-3v-3c0-.75-.75-1.5-1.5-1.5z'/%3E%3C/svg%3E");
}
[data-theme="light"] .maplibregl-ctrl button.maplibregl-ctrl-zoom-out .maplibregl-ctrl-icon {
  background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='29' height='29' fill='%232563eb' viewBox='0 0 29 29'%3E%3Cpath d='M10 13c-.75 0-1.5.75-1.5 1.5S9.25 16 10 16h9c.75 0 1.5-.75 1.5-1.5S19.75 13 19 13h-9z'/%3E%3C/svg%3E");
}

/* ── Compass needle colours (driven by theme variables) ── */
.compass-n { fill: var(--c-accent); }
.compass-s { fill: var(--c-surface-3); }
