/* ============================================================================
 * NextMap Viewer — modern glassmorphic chrome
 * ========================================================================== */

/* 1. Reset ----------------------------------------------------------------- */
*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; overflow: hidden; }
button { font: inherit; color: inherit; background: transparent; border: 0; cursor: pointer; padding: 0; }
input { font: inherit; color: inherit; }
ul { list-style: none; margin: 0; padding: 0; }
h1, h2, h3 { margin: 0; font-weight: 600; }

/* 2. Theme variables ------------------------------------------------------- */
:root, [data-theme="light"] {
  --bg: #f8fafc;
  --surface-rgb: 255, 255, 255;
  --text: #0f172a;
  --text-muted: #475569;
  --border-rgb: 226, 232, 240;
  --accent: #6366f1;
  --accent-rgb: 99, 102, 241;
  --danger: #ef4444;
  --shadow-1: 0 8px 24px -12px rgba(0,0,0,0.18), 0 2px 6px -2px rgba(0,0,0,0.08);
}

[data-theme="dark"] {
  --bg: #0b1220;
  --surface-rgb: 17, 24, 39;
  --text: #e5e7eb;
  --text-muted: #94a3b8;
  --border-rgb: 51, 65, 85;
  --accent: #60a5fa;
  --accent-rgb: 96, 165, 250;
  --danger: #f87171;
  --shadow-1: 0 8px 24px -12px rgba(0,0,0,0.55), 0 2px 6px -2px rgba(0,0,0,0.4);
}

/* 3. Base ------------------------------------------------------------------ */
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  transition: background-color 200ms ease-out, color 200ms ease-out;
}

#map { position: absolute; inset: 0; }

/* 4. Glass mixin ----------------------------------------------------------- */
.glass {
  background: rgba(var(--surface-rgb), 0.72);
  backdrop-filter: blur(20px) saturate(180%);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  border: 1px solid rgba(var(--border-rgb), 0.7);
  box-shadow: var(--shadow-1);
}
@supports not ((-webkit-backdrop-filter: blur(1px)) or (backdrop-filter: blur(1px))) {
  .glass { background: rgba(var(--surface-rgb), 0.96); }
}

/* 5. Search ---------------------------------------------------------------- */
.search-shell {
  position: fixed;
  top: 18px; left: 50%;
  transform: translateX(-50%);
  width: min(540px, calc(100vw - 32px));
  z-index: 5;
  display: flex; flex-direction: column; gap: 8px;
}
.search-bar {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 14px;
  border-radius: 999px;
  transition: border-color 160ms ease-out, box-shadow 160ms ease-out;
}
.search-bar:focus-within {
  border-color: var(--accent);
  box-shadow:
    var(--shadow-1),
    0 0 0 6px rgba(var(--accent-rgb), 0.18);
}
.search-icon { color: var(--text-muted); flex: 0 0 auto; }
.search-bar input {
  flex: 1; min-width: 0;
  border: 0; background: transparent;
  font: 500 15px / 1.4 inherit;
  outline: none;
  padding: 4px 0;
}
.search-bar input::placeholder { color: var(--text-muted); }
.search-clear {
  width: 26px; height: 26px;
  border-radius: 50%;
  display: grid; place-items: center;
  color: var(--text-muted);
  transition: background-color 160ms, color 160ms;
}
.search-clear:hover { background: rgba(var(--border-rgb), 0.7); color: var(--text); }

.search-status {
  font-size: 12px;
  color: var(--text-muted);
  padding: 0 14px;
  min-height: 14px;
}

.search-results {
  border-radius: 18px;
  padding: 6px;
  max-height: 60vh;
  overflow-y: auto;
  animation: dropdown-in 180ms cubic-bezier(0.16, 1, 0.3, 1);
}
.search-results[hidden] { display: none; }
.search-result {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 14px;
  border-radius: 12px;
  cursor: pointer;
  transition: background-color 120ms;
}
.search-result[aria-selected="true"],
.search-result:hover {
  background: rgba(var(--accent-rgb), 0.12);
}
.search-result__main {
  flex: 1;
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.search-result__sub {
  font-size: 11px; font-weight: 500;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  color: var(--text-muted);
  text-transform: uppercase; letter-spacing: 0.04em;
  padding: 2px 8px;
  border-radius: 999px;
  background: rgba(var(--border-rgb), 0.5);
  flex: 0 0 auto;
}

@keyframes dropdown-in {
  from { opacity: 0; transform: translateY(-4px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* 6. Toolbar --------------------------------------------------------------- */
.toolbar {
  position: fixed;
  right: 18px; top: 50%;
  transform: translateY(-50%);
  display: flex; flex-direction: column;
  gap: 8px;
  z-index: 4;
}
.tb-btn {
  width: 44px; height: 44px;
  border-radius: 14px;
  display: grid; place-items: center;
  color: var(--text);
  transition: transform 160ms ease-out, border-color 160ms ease-out, background-color 160ms;
}
.tb-btn:hover {
  transform: translateY(-1px);
  border-color: var(--accent);
}
.tb-btn:active { transform: translateY(0); }
.tb-btn:focus-visible {
  outline: 0;
  box-shadow: var(--shadow-1), 0 0 0 4px rgba(var(--accent-rgb), 0.35);
}
.tb-btn.is-active {
  background: rgba(var(--accent-rgb), 0.18);
  border-color: var(--accent);
  color: var(--accent);
}
.tb-btn svg { width: 20px; height: 20px; }

/* 7. Layers panel ---------------------------------------------------------- */
.panel {
  position: fixed;
  top: 80px; right: 18px;
  width: 320px;
  max-height: 70vh;
  display: flex; flex-direction: column;
  border-radius: 24px;
  z-index: 6;
  transform: translateX(calc(100% + 32px));
  opacity: 0;
  pointer-events: none;
  transition: transform 280ms cubic-bezier(0.22, 1, 0.36, 1), opacity 200ms ease-out;
}
.panel[aria-hidden="false"] {
  transform: translateX(0);
  opacity: 1;
  pointer-events: auto;
}
.panel-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px 12px;
}
.panel-header h2 { font-size: 15px; letter-spacing: 0.02em; }
.panel-close {
  width: 28px; height: 28px;
  border-radius: 50%;
  display: grid; place-items: center;
  color: var(--text-muted);
  transition: background-color 160ms, color 160ms;
}
.panel-close:hover { background: rgba(var(--border-rgb), 0.7); color: var(--text); }

.panel-body {
  flex: 1;
  overflow-y: auto;
  padding: 4px 12px 12px;
}
.panel-footer {
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px 20px 16px;
  border-top: 1px solid rgba(var(--border-rgb), 0.6);
  font-size: 12px;
  color: var(--text-muted);
}
.panel-footer-value { color: var(--text); font-weight: 600; }

/* Toggle row */
.toggle {
  display: grid;
  grid-template-columns: 12px 1fr auto 40px;
  align-items: center; gap: 12px;
  padding: 10px 12px;
  border-radius: 12px;
  cursor: pointer; user-select: none;
  position: relative;
  transition: background-color 120ms;
}
.toggle:hover { background: rgba(var(--accent-rgb), 0.08); }
.toggle-swatch {
  width: 12px; height: 12px; border-radius: 4px;
  background: var(--c);
  box-shadow: 0 0 0 1px rgba(0,0,0,0.08) inset;
}
.toggle-label { font-size: 14px; font-weight: 500; color: var(--text); }
.toggle-count {
  font-size: 11px; color: var(--text-muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.toggle input {
  position: absolute;
  opacity: 0; pointer-events: none;
  width: 0; height: 0;
}
.toggle-track {
  width: 36px; height: 20px; border-radius: 999px;
  background: rgba(var(--border-rgb), 1);
  position: relative;
  transition: background-color 160ms ease-out;
  justify-self: end;
}
.toggle-thumb {
  position: absolute; top: 2px; left: 2px;
  width: 16px; height: 16px; border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0,0,0,0.25);
  transition: transform 160ms cubic-bezier(0.22, 1, 0.36, 1);
}
.toggle input:checked ~ .toggle-track { background: var(--accent); }
.toggle input:checked ~ .toggle-track .toggle-thumb { transform: translateX(16px); }
.toggle input:focus-visible ~ .toggle-track {
  box-shadow: 0 0 0 3px rgba(var(--accent-rgb), 0.4);
}

/* 8. HUD ------------------------------------------------------------------- */
.hud {
  position: fixed;
  bottom: 18px; right: 18px;
  padding: 8px 14px;
  border-radius: 999px;
  font: 500 12px / 1 ui-monospace, SFMono-Regular, Menlo, monospace;
  display: flex; align-items: center; gap: 8px;
  color: var(--text-muted);
  z-index: 3;
  pointer-events: none;
}
.hud b { color: var(--text); font-weight: 600; }
.hud-sep { opacity: 0.5; }

/* 9. Toast ----------------------------------------------------------------- */
.toast {
  position: fixed;
  bottom: 24px; left: 50%;
  transform: translateX(-50%) translateY(8px);
  padding: 10px 18px;
  border-radius: 14px;
  font-size: 13px;
  color: var(--text);
  max-width: 320px;
  text-align: center;
  z-index: 100;
  opacity: 0;
  transition: opacity 220ms cubic-bezier(0.16, 1, 0.3, 1), transform 220ms cubic-bezier(0.16, 1, 0.3, 1);
}
.toast.is-visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* 10. Pulse marker --------------------------------------------------------- */
.pulse-marker {
  position: relative; width: 18px; height: 18px;
  pointer-events: none;
}
.pulse-marker__dot {
  position: absolute; inset: 2px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid #fff;
  box-shadow: 0 1px 4px rgba(0,0,0,0.25);
}
.pulse-marker__halo {
  position: absolute; inset: -8px;
  border-radius: 50%;
  background: rgba(var(--accent-rgb), 0.35);
  animation: pulse 1800ms ease-out infinite;
}
@keyframes pulse {
  0%   { transform: scale(0.6); opacity: 0.9; }
  100% { transform: scale(2.4); opacity: 0;   }
}

/* 11. MapLibre overrides --------------------------------------------------- */
.maplibregl-ctrl-attrib {
  background: rgba(var(--surface-rgb), 0.72) !important;
  backdrop-filter: blur(12px) saturate(180%);
  -webkit-backdrop-filter: blur(12px) saturate(180%);
  border-radius: 999px !important;
  color: var(--text-muted) !important;
  font-size: 11px !important;
}
.maplibregl-ctrl-attrib a { color: var(--text) !important; }
.maplibregl-ctrl-group {
  background: rgba(var(--surface-rgb), 0.72) !important;
  backdrop-filter: blur(20px) saturate(180%);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  border: 1px solid rgba(var(--border-rgb), 0.7) !important;
  box-shadow: var(--shadow-1) !important;
  border-radius: 14px !important;
  overflow: hidden;
}
.maplibregl-ctrl-group button { background-color: transparent !important; }
.maplibregl-ctrl-group button + button { border-top: 1px solid rgba(var(--border-rgb), 0.6) !important; }
[data-theme="dark"] .maplibregl-ctrl-group button span { filter: invert(1); }
[data-theme="dark"] .maplibregl-ctrl-attrib-button { filter: invert(0.85); }
.maplibregl-ctrl-scale {
  background: rgba(var(--surface-rgb), 0.72) !important;
  border-color: var(--text-muted) !important;
  color: var(--text) !important;
  padding: 2px 8px !important;
  border-radius: 6px !important;
}

/* 12. Mobile --------------------------------------------------------------- */
@media (max-width: 640px) {
  .search-shell { top: 12px; width: calc(100vw - 24px); }
  .toolbar {
    top: auto; right: 50%; bottom: 18px;
    transform: translateX(50%);
    flex-direction: row;
  }
  .panel {
    top: auto; bottom: 80px;
    right: 12px; left: 12px;
    width: auto;
    max-height: 50vh;
  }
  .hud { display: none; }
}

/* 13. Reduced motion ------------------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
  .pulse-marker__halo { animation: none; opacity: 0; }
}
