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

/* Google Fonts — Inter */
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');

/* ─── DESIGN TOKENS ───────────────────────────────────────────────────────── */
:root {
  /* Categoría colors */
  --color-trafico:      #FF5A5A;
  --color-obras:        #FFB347;
  --color-metro:        #4A9EE8;
  --color-emt:          #2F7ED8;
  --color-aire:         #6FBB3A;
  --color-agua:         #22C49A;
  --color-electricidad: #E8A020;
  --color-restriccion:  #E0618A;
  --color-parques:      #4A8A1A;
  --color-semaforo:     #9E9E94;
  --color-cercanias:    #E32831;
  --color-meteorologia: #9B7FE8;

  /* Dark palette */
  --bg:             #0d0f14;
  --bg-2:           #13161e;
  --bg-3:           #1a1e2a;
  --surface:        #1e2330;
  --surface-2:      #252a3a;
  --surface-hover:  #2b3145;
  --border:         rgba(255,255,255,0.07);
  --border-bright:  rgba(255,255,255,0.13);
  --text:           #e8eaf2;
  --text-muted:     #7a7f96;
  --text-dim:       #4a4f66;
  --accent:         #4A9EE8;
  --accent-glow:    rgba(74, 158, 232, 0.25);

  /* Severity */
  --sev-grave-bg:   rgba(255, 90, 90, 0.12);
  --sev-grave-fg:   #FF8080;
  --sev-mod-bg:     rgba(255, 179, 71, 0.12);
  --sev-mod-fg:     #FFB347;
  --sev-leve-bg:    rgba(111, 187, 58, 0.12);
  --sev-leve-fg:    #6FBB3A;

  /* Layout */
  --topbar-h:     68px;
  --stats-h:      48px;
  --filters-h:    48px;
  --panel-w:      320px;
  --header-total: calc(var(--topbar-h) + var(--stats-h) + var(--filters-h));

  /* Motion */
  --ease-out: cubic-bezier(0.16, 1, 0.3, 1);
  --ease-in-out: cubic-bezier(0.4, 0, 0.2, 1);

  /* Color scheme */
  color-scheme: dark;
}

/* ─── BASE ────────────────────────────────────────────────────────────────── */
html, body {
  height: 100%;
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* ─── TOPBAR ──────────────────────────────────────────────────────────────── */
.topbar {
  height: var(--topbar-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border-bright);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  position: sticky;
  top: 0;
  z-index: 1000;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 20px;
}

.topbar-brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.topbar-notices {
  display: flex;
  flex-direction: column;
  gap: 3px;
  border-right: 1px solid var(--border-bright);
  padding-right: 20px;
}

.logo {
  font-size: 16px;
  font-weight: 700;
  letter-spacing: -0.3px;
  background: linear-gradient(135deg in oklch, #e8eaf2 0%, #7a9fcf 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.live-badge {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  font-weight: 500;
  color: #FF5A5A;
  background: rgba(255, 90, 90, 0.12);
  border: 1px solid rgba(255, 90, 90, 0.25);
  padding: 3px 10px;
  border-radius: 20px;
  letter-spacing: 0.3px;
}

.live-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #FF5A5A;
  animation: pulse-dot 1.8s ease-in-out infinite;
  flex-shrink: 0;
}

@keyframes pulse-dot {
  0%, 100% { opacity: 1; transform: scale(1); }
  50%       { opacity: 0.35; transform: scale(0.75); }
}

.update-time {
  font-size: 13px;
  color: var(--text);
  font-weight: 500;
}

.disclaimer {
  font-size: 11px;
  color: #a0a8c0;
  font-weight: 400;
}

/* ─── STATS BAR ───────────────────────────────────────────────────────────── */
.stats-bar {
  height: var(--stats-h);
  background: var(--bg-2);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 20px;
  gap: 28px;
}

.stat {
  display: flex;
  align-items: baseline;
  gap: 5px;
  position: relative;
}

.stat span {
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.5px;
  transition: color 0.3s var(--ease-out);
}

.stat small {
  font-size: 10px;
  font-weight: 500;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.6px;
}

.stat-grave span   { color: var(--color-trafico); }
.stat-mod   span   { color: var(--color-obras); }
.stat-leve  span   { color: var(--color-aire); }
.stat-aire  span   { color: var(--accent); font-size: 16px; }

/* AQI pill */
#stat-aire-box {
  background: var(--surface);
  border: 1px solid var(--border-bright);
  padding: 4px 10px;
  border-radius: 8px;
  transition: border-color 0.3s;
}

/* ─── FILTROS ─────────────────────────────────────────────────────────────── */
.filters {
  height: var(--filters-h);
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 16px;
  gap: 6px;
  overflow-x: auto;
  scrollbar-width: none;
}

.filters::-webkit-scrollbar { display: none; }

.filter-btn {
  flex-shrink: 0;
  padding: 5px 14px;
  border-radius: 20px;
  border: 1px solid var(--border-bright);
  background: var(--surface);
  font-size: 12px;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  color: var(--text-muted);
  transition: all 0.18s var(--ease-in-out);
  white-space: nowrap;
}

.filter-btn:hover {
  border-color: rgba(255,255,255,0.22);
  color: var(--text);
  background: var(--surface-2);
}

.filter-btn.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
  box-shadow: 0 0 14px var(--accent-glow);
}

/* Category-colored active states */
.filter-btn[data-cat="trafico"].active     { background: var(--color-trafico); border-color: var(--color-trafico); box-shadow: 0 0 14px rgba(255,90,90,0.3); }
.filter-btn[data-cat="obras"].active       { background: var(--color-obras); border-color: var(--color-obras); color: #111; box-shadow: 0 0 14px rgba(255,179,71,0.3); }
.filter-btn[data-cat="metro"].active       { background: var(--color-metro); border-color: var(--color-metro); box-shadow: 0 0 14px rgba(74,158,232,0.3); }
.filter-btn[data-cat="emt"].active         { background: var(--color-emt); border-color: var(--color-emt); box-shadow: 0 0 14px rgba(47,126,216,0.3); }
.filter-btn[data-cat="aire"].active        { background: var(--color-aire); border-color: var(--color-aire); color: #111; box-shadow: 0 0 14px rgba(111,187,58,0.3); }
.filter-btn[data-cat="semaforo"].active    { background: var(--color-semaforo); border-color: var(--color-semaforo); box-shadow: 0 0 14px rgba(158,158,148,0.3); }
.filter-btn[data-cat="restriccion"].active { background: var(--color-restriccion); border-color: var(--color-restriccion); box-shadow: 0 0 14px rgba(224,97,138,0.3); }
.filter-btn[data-cat="parques"].active     { background: var(--color-parques); border-color: var(--color-parques); box-shadow: 0 0 14px rgba(74,138,26,0.3); }
.filter-btn[data-cat="agua"].active        { background: var(--color-agua); border-color: var(--color-agua); color: #111; box-shadow: 0 0 14px rgba(34,196,154,0.3); }
.filter-btn[data-cat="electricidad"].active  { background: var(--color-electricidad); border-color: var(--color-electricidad); color: #111; box-shadow: 0 0 14px rgba(232,160,32,0.3); }
.filter-btn[data-cat="cercanias"].active     { background: var(--color-cercanias); border-color: var(--color-cercanias); box-shadow: 0 0 14px rgba(227,40,49,0.3); }
.filter-btn[data-cat="meteorologia"].active  { background: var(--color-meteorologia); border-color: var(--color-meteorologia); box-shadow: 0 0 14px rgba(155,127,232,0.3); }

/* ─── MAIN LAYOUT ─────────────────────────────────────────────────────────── */
.main {
  display: flex;
  height: calc(100vh - var(--header-total));
}

/* ─── MAPA ────────────────────────────────────────────────────────────────── */
#map {
  flex: 1;
  min-height: 300px;
}

/* Leaflet dark map popup — semi-transparente y click-through salvo el contenido */
/* El contenedor raíz y el triángulo (tip-container) también deben ser none,
   si no interceptan clics destinados al marcador que está justo debajo. */
.leaflet-popup,
.leaflet-popup-content-wrapper,
.leaflet-popup-tip-container,
.leaflet-popup-tip {
  pointer-events: none !important;
}

.leaflet-popup-content-wrapper {
  background: rgba(20, 23, 34, 0.55) !important;
  color: #fff !important;
  border: 1px solid rgba(255,255,255,0.18) !important;
  border-radius: 10px !important;
  box-shadow: 0 8px 32px rgba(0,0,0,0.30) !important;
  backdrop-filter: blur(12px) !important;
  -webkit-backdrop-filter: blur(12px) !important;
}

.leaflet-popup-tip {
  background: rgba(20, 23, 34, 0.55) !important;
}

/* El contenido (texto, links) sigue siendo clicable */
.leaflet-popup-content {
  pointer-events: auto !important;
}

.leaflet-popup-close-button {
  color: var(--text-muted) !important;
}

/* Leaflet attribution */
.leaflet-control-attribution {
  background: var(--bg) !important;
  color: var(--text-dim) !important;
  font-size: 10px !important;
}

.leaflet-control-attribution a {
  color: var(--text-muted) !important;
}

/* Leaflet zoom controls */
.leaflet-control-zoom a {
  background: var(--surface) !important;
  border-color: var(--border-bright) !important;
  color: var(--text) !important;
}

.leaflet-control-zoom a:hover {
  background: var(--surface-2) !important;
}

/* ─── PANEL LATERAL ───────────────────────────────────────────────────────── */
.panel {
  width: var(--panel-w);
  min-width: 260px;
  background: var(--bg-2);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.panel-header {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  flex-shrink: 0;
  background: var(--surface);
  letter-spacing: -0.2px;
}

.panel-list {
  flex: 1;
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: var(--surface-2) transparent;
}

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

.panel-footer {
  padding: 8px 16px;
  border-top: 1px solid var(--border);
  font-size: 10px;
  color: var(--text-dim);
  flex-shrink: 0;
  display: flex;
  gap: 8px;
  align-items: center;
}

.panel-footer a {
  color: var(--text-dim);
  text-decoration: none;
  transition: color 0.15s;
}

.panel-footer a:hover { color: var(--text-muted); }

/* ─── TARJETAS INCIDENCIA ─────────────────────────────────────────────────── */
.inc-item {
  display: flex;
  gap: 10px;
  padding: 11px 14px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background 0.12s var(--ease-in-out);
  position: relative;
}

.inc-item::before {
  content: '';
  position: absolute;
  left: 0; top: 0; bottom: 0;
  width: 2px;
  background: transparent;
  transition: background 0.15s;
}

.inc-item:hover {
  background: var(--surface-hover);
}

.inc-item.selected {
  background: rgba(74, 158, 232, 0.08);
}

.inc-item.selected::before {
  background: var(--accent);
}

/* Severity border accent */
.inc-item:has(.sev-grave)::before  { background: var(--color-trafico); opacity: 0.6; }
.inc-item.selected:has(.sev-grave)::before { opacity: 1; background: var(--color-trafico); }

.inc-icon {
  width: 30px;
  height: 30px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  flex-shrink: 0;
  margin-top: 1px;
}

.inc-body  { flex: 1; min-width: 0; }

.inc-title {
  font-size: 12px;
  font-weight: 500;
  line-height: 1.35;
  margin-bottom: 5px;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.inc-meta {
  display: flex;
  gap: 5px;
  align-items: center;
  flex-wrap: wrap;
}

.inc-zona {
  font-size: 11px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 140px;
}

.inc-time {
  font-size: 10px;
  color: var(--text-dim);
  margin-left: auto;
  white-space: nowrap;
}

/* ─── SEVERITY BADGES ─────────────────────────────────────────────────────── */
.sev-badge {
  font-size: 10px;
  font-weight: 600;
  padding: 2px 7px;
  border-radius: 10px;
  letter-spacing: 0.3px;
  text-transform: uppercase;
  flex-shrink: 0;
}

.sev-grave    { background: var(--sev-grave-bg); color: var(--sev-grave-fg); }
.sev-moderada { background: var(--sev-mod-bg);   color: var(--sev-mod-fg); }
.sev-leve     { background: var(--sev-leve-bg);  color: var(--sev-leve-fg); }

/* ─── ICONOS DE CATEGORÍA ─────────────────────────────────────────────────── */
.cat-trafico      { background: rgba(255, 90, 90, 0.15);   }
.cat-obras        { background: rgba(255, 179, 71, 0.15);  }
.cat-metro        { background: rgba(74, 158, 232, 0.15);  }
.cat-emt          { background: rgba(47, 126, 216, 0.15);  }
.cat-aire         { background: rgba(111, 187, 58, 0.15);  }
.cat-agua         { background: rgba(34, 196, 154, 0.15);  }
.cat-electricidad { background: rgba(232, 160, 32, 0.15);  }
.cat-restriccion  { background: rgba(224, 97, 138, 0.15);  }
.cat-parques      { background: rgba(74, 138, 26, 0.15);   }
.cat-semaforo     { background: rgba(158, 158, 148, 0.15); }
.cat-cercanias    { background: rgba(227,  40,  49, 0.15); }
.cat-meteorologia { background: rgba(155, 127, 232, 0.15); }

/* ─── LOADING / EMPTY STATE ───────────────────────────────────────────────── */
.loading, .empty {
  padding: 40px 20px;
  text-align: center;
  color: var(--text-dim);
  font-size: 13px;
}

.loading::before {
  content: '';
  display: block;
  width: 28px;
  height: 28px;
  border: 2px solid var(--border-bright);
  border-top-color: var(--accent);
  border-radius: 50%;
  margin: 0 auto 12px;
  animation: spin 0.8s linear infinite;
}

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

/* ─── ERROR BANNER ────────────────────────────────────────────────────────── */
.error-banner {
  position: fixed;
  bottom: 12px;
  left: 50%;
  transform: translateX(-50%);
  background: #8b1a1a;
  color: #fff;
  padding: 10px 20px;
  border-radius: 10px;
  border: 1px solid rgba(255, 120, 120, 0.3);
  font-size: 12px;
  font-weight: 500;
  z-index: 2000;
  box-shadow: 0 4px 24px rgba(0,0,0,0.5);
  white-space: nowrap;
}

/* ─── SCROLLBAR GLOBAL ────────────────────────────────────────────────────── */
@media (prefers-contrast: more) {
  ::-webkit-scrollbar        { width: 8px; height: 8px; }
  ::-webkit-scrollbar-track  { background: var(--bg); }
  ::-webkit-scrollbar-thumb  { background: var(--border-bright); border-radius: 4px; }
}

/* ─── REDUCED MOTION ──────────────────────────────────────────────────────── */
@property --animation-reduced {
  syntax: "*";
  inherits: false;
  initial-value: none;
}

@media (prefers-reduced-motion: reduce) {
  * { animation: var(--animation-reduced) !important; transition-duration: 0.01ms !important; }
  .live-dot { animation: none !important; }
  .loading::before { animation: none !important; border-top-color: var(--accent); }
}

/* ─── BUSCADOR FLOTANTE ───────────────────────────────────────────────────── */
.map-search-container {
  position: absolute;
  top: 12px;
  right: 12px;
  z-index: 1000;
  width: 300px;
  font-family: inherit;
}

.search-box {
  display: flex;
  align-items: center;
  background: rgba(30, 35, 48, 0.85);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border: 1px solid var(--border-bright);
  border-radius: 8px;
  padding: 6px 12px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.45);
  transition: all 0.22s var(--ease-out);
}

.search-box:focus-within {
  border-color: var(--accent);
  background: var(--surface);
  box-shadow: 0 0 12px var(--accent-glow);
}

.search-icon {
  font-size: 13px;
  color: var(--text-muted);
  margin-right: 8px;
  display: flex;
  align-items: center;
}

#search-input {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  color: var(--text);
  font-family: inherit;
  font-size: 12px;
  height: 24px;
}

#search-input::placeholder {
  color: var(--text-muted);
  opacity: 0.75;
}

#search-clear-btn {
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 12px;
  cursor: pointer;
  padding: 0 2px;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  transition: all 0.15s;
}

#search-clear-btn:hover {
  color: var(--text);
  background: rgba(255,255,255,0.08);
}

.search-suggestions {
  margin-top: 5px;
  background: rgba(30, 35, 48, 0.95);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border: 1px solid var(--border-bright);
  border-radius: 8px;
  list-style: none;
  max-height: 180px;
  overflow-y: auto;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
  scrollbar-width: thin;
}

.search-suggestions::-webkit-scrollbar { width: 3px; }
.search-suggestions::-webkit-scrollbar-track { background: transparent; }
.search-suggestions::-webkit-scrollbar-thumb { background: var(--surface-hover); }

.search-suggestions li {
  padding: 9px 12px;
  font-size: 12px;
  color: var(--text);
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  transition: background 0.12s;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.search-suggestions li:last-child {
  border-bottom: none;
}

.search-suggestions li:hover {
  background: var(--surface-hover);
  color: #fff;
}

.search-suggestions li.no-results {
  color: var(--text-muted);
  font-style: italic;
  cursor: default;
}

.search-suggestions li.no-results:hover {
  background: transparent;
}

/* Pulsing effect for search marker */
@keyframes search-pulse {
  0% { transform: scale(1); opacity: 0.8; }
  50% { transform: scale(1.4); opacity: 0.35; }
  100% { transform: scale(1); opacity: 0.8; }
}

.search-pulse-marker {
  animation: search-pulse 1.8s infinite ease-in-out;
}

/* ─── RESPONSIVE MÓVIL ────────────────────────────────────────────────────── */
@media (max-width: 680px) {
  :root {
    --panel-w: 100%;
    --topbar-h: 50px;
    --stats-h: 44px;
  }

  .main {
    flex-direction: column;
    height: auto;
    min-height: calc(100vh - var(--header-total));
  }

  #map {
    height: 42vh;
    min-height: 220px;
  }

  .panel {
    width: 100%;
    min-width: 0;
    border-left: none;
    border-top: 1px solid var(--border);
    height: 45vh;
    flex-shrink: 0;
  }

  .stats-bar {
    gap: 14px;
    padding: 0 14px;
    overflow-x: auto;
    scrollbar-width: none;
  }

  .stats-bar::-webkit-scrollbar { display: none; }

  .stat span     { font-size: 16px; }
  .stat small    { font-size: 9px; }

  .logo { font-size: 14px; }

  .filters { padding: 0 10px; gap: 5px; }
  .filter-btn { font-size: 11px; padding: 4px 11px; }

  /* Ajuste de buscador en móvil */
  .map-search-container {
    width: calc(100% - 24px);
    left: 12px;
    right: 12px;
    top: 10px;
  }
}

@media (max-width: 360px) {
  #stat-aire-box { display: none; }
}

/* ─── DISTRITO TOOLTIP ────────────────────────────────────────────────────── */
.distrito-tooltip {
  background: rgba(30, 35, 48, 0.95) !important;
  border: 1px solid var(--border-bright) !important;
  color: var(--text) !important;
  border-radius: 6px !important;
  padding: 5px 9px !important;
  font-size: 11px !important;
  font-weight: 500 !important;
  box-shadow: 0 4px 16px rgba(0,0,0,0.4) !important;
  font-family: inherit !important;
}
