/* ═══════════════════════════════════════════════════════════════
   Smart City Jaén — Dashboard IoT
   Dark mode premium design system
   ═══════════════════════════════════════════════════════════════ */

/* ── CSS Variables ──────────────────────────────────────── */
:root {
  /* Background */
  --bg-primary: #07080d;
  --bg-secondary: #0e1018;
  --bg-card: rgba(16, 18, 30, 0.85);
  --bg-card-hover: rgba(22, 25, 42, 0.95);

  /* Accent colors */
  --accent-air: #00d4aa;
  --accent-air-dim: rgba(0, 212, 170, 0.15);
  --accent-traffic: #4d7cfe;
  --accent-traffic-dim: rgba(77, 124, 254, 0.15);
  --accent-container: #ff9f43;
  --accent-container-dim: rgba(255, 159, 67, 0.15);

  /* Alert colors */
  --alert-critical: #ff4757;
  --alert-high: #ff6b6b;
  --alert-medium: #ffd93d;
  --alert-low: #6bcb77;
  --alert-info: #4d7cfe;

  /* Text */
  --text-primary: #e8eaf0;
  --text-secondary: #8b8fa3;
  --text-muted: #52556a;

  /* Borders / Glassmorphism */
  --border-color: rgba(255, 255, 255, 0.06);
  --glass-blur: 20px;
  --glass-bg: rgba(255, 255, 255, 0.03);

  /* Shadows */
  --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 4px 24px rgba(0, 0, 0, 0.4);
  --shadow-lg: 0 8px 48px rgba(0, 0, 0, 0.5);
  --shadow-glow-air: 0 0 30px rgba(0, 212, 170, 0.15);
  --shadow-glow-traffic: 0 0 30px rgba(77, 124, 254, 0.15);
  --shadow-glow-container: 0 0 30px rgba(255, 159, 67, 0.15);

  /* Spacing */
  --gap: 20px;
  --radius: 16px;
  --radius-sm: 10px;

  /* Transitions */
  --transition: 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

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

html {
  scroll-behavior: smooth;
}

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  min-height: 100vh;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

/* Subtle grid background */
body::before {
  content: '';
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background:
    radial-gradient(ellipse at 20% 0%, rgba(0, 212, 170, 0.04) 0%, transparent 60%),
    radial-gradient(ellipse at 80% 100%, rgba(77, 124, 254, 0.04) 0%, transparent 60%);
  pointer-events: none;
  z-index: 0;
}

/* ═══════════════════════════════════════════════════════════════
   HEADER
   ═══════════════════════════════════════════════════════════════ */
#main-header {
  position: sticky;
  top: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 28px;
  background: rgba(7, 8, 13, 0.8);
  backdrop-filter: blur(var(--glass-blur));
  -webkit-backdrop-filter: blur(var(--glass-blur));
  border-bottom: 1px solid var(--border-color);
}

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

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

.logo-icon {
  font-size: 32px;
  filter: drop-shadow(0 0 12px rgba(0, 212, 170, 0.4));
}

.logo-text h1 {
  font-size: 1.3rem;
  font-weight: 700;
  background: linear-gradient(135deg, var(--accent-air), var(--accent-traffic));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  letter-spacing: -0.02em;
}

.logo-sub {
  font-size: 0.72rem;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.1em;
  font-weight: 500;
}

.live-indicator {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 14px;
  background: rgba(255, 71, 87, 0.1);
  border: 1px solid rgba(255, 71, 87, 0.25);
  border-radius: 20px;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  color: var(--alert-critical);
  text-transform: uppercase;
}

.pulse {
  width: 8px;
  height: 8px;
  background: var(--alert-critical);
  border-radius: 50%;
  animation: pulse-anim 1.5s ease-in-out infinite;
}

@keyframes pulse-anim {
  0%, 100% { opacity: 1; box-shadow: 0 0 0 0 rgba(255, 71, 87, 0.5); }
  50% { opacity: 0.7; box-shadow: 0 0 0 8px rgba(255, 71, 87, 0); }
}

.header-time {
  font-size: 0.85rem;
  color: var(--text-secondary);
  font-weight: 500;
  font-variant-numeric: tabular-nums;
}

/* ═══════════════════════════════════════════════════════════════
   MAIN LAYOUT
   ═══════════════════════════════════════════════════════════════ */
#dashboard {
  position: relative;
  z-index: 1;
  max-width: 1440px;
  margin: 0 auto;
  padding: var(--gap);
  display: flex;
  flex-direction: column;
  gap: var(--gap);
}

/* ── KPI Cards ─────────────────────────────────────────── */
.kpi-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: var(--gap);
}

.kpi-card {
  position: relative;
  overflow: hidden;
  padding: 22px 24px;
  border-radius: var(--radius);
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  backdrop-filter: blur(var(--glass-blur));
  display: flex;
  align-items: center;
  gap: 16px;
  transition: transform var(--transition), box-shadow var(--transition);
  animation: fadeInUp 0.5s ease both;
}

.kpi-card:nth-child(2) { animation-delay: 0.07s; }
.kpi-card:nth-child(3) { animation-delay: 0.14s; }
.kpi-card:nth-child(4) { animation-delay: 0.21s; }

.kpi-card:hover {
  transform: translateY(-3px);
}

/* Gradient accent bar */
.kpi-card::before {
  content: '';
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 3px;
}

.kpi-sensors::before { background: linear-gradient(90deg, var(--accent-air), var(--accent-traffic)); }
.kpi-sensors:hover { box-shadow: var(--shadow-glow-air); }

.kpi-readings::before { background: linear-gradient(90deg, var(--accent-traffic), #a855f7); }
.kpi-readings:hover { box-shadow: var(--shadow-glow-traffic); }

.kpi-air::before { background: linear-gradient(90deg, var(--accent-air), #6bcb77); }
.kpi-air:hover { box-shadow: var(--shadow-glow-air); }

.kpi-alerts::before { background: linear-gradient(90deg, var(--alert-critical), var(--alert-medium)); }
.kpi-alerts:hover { box-shadow: 0 0 30px rgba(255, 71, 87, 0.15); }

.kpi-icon {
  font-size: 2.2rem;
  filter: saturate(1.2);
}

.kpi-data {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.kpi-value {
  font-size: 1.9rem;
  font-weight: 800;
  letter-spacing: -0.03em;
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
}

.kpi-label {
  font-size: 0.78rem;
  color: var(--text-secondary);
  font-weight: 500;
}

.kpi-sub {
  position: absolute;
  bottom: 8px;
  right: 14px;
  font-size: 0.68rem;
  color: var(--text-muted);
  font-weight: 500;
}

/* ── Generic Card ──────────────────────────────────────── */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  backdrop-filter: blur(var(--glass-blur));
  overflow: hidden;
  animation: fadeInUp 0.5s ease both;
  animation-delay: 0.15s;
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 22px;
  border-bottom: 1px solid var(--border-color);
  flex-wrap: wrap;
  gap: 10px;
}

.card-header h2 {
  font-size: 1rem;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.card-subtitle {
  font-size: 0.75rem;
  color: var(--text-muted);
  font-weight: 500;
}

.badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 26px;
  height: 26px;
  padding: 0 8px;
  border-radius: 13px;
  background: var(--alert-critical);
  color: #fff;
  font-size: 0.75rem;
  font-weight: 700;
}

/* ── Map + Alerts ──────────────────────────────────────── */
.map-alerts-row {
  display: grid;
  grid-template-columns: 1fr 380px;
  gap: var(--gap);
}

.map-card { min-height: 480px; display: flex; flex-direction: column; }

#map {
  flex: 1;
  min-height: 420px;
  background: var(--bg-secondary);
}

/* Dark map tiles */
.leaflet-tile-pane { filter: brightness(0.7) contrast(1.15) saturate(0.85); }
.leaflet-control-attribution { display: none !important; }

.leaflet-popup-content-wrapper {
  background: var(--bg-card) !important;
  color: var(--text-primary) !important;
  border: 1px solid var(--border-color) !important;
  border-radius: var(--radius-sm) !important;
  backdrop-filter: blur(12px);
  box-shadow: var(--shadow-md) !important;
}

.leaflet-popup-tip {
  background: var(--bg-card) !important;
  border: 1px solid var(--border-color) !important;
}

.leaflet-popup-content {
  margin: 12px 16px !important;
  font-family: 'Inter', sans-serif !important;
  font-size: 0.82rem !important;
  line-height: 1.6 !important;
}

.popup-title {
  font-weight: 700;
  font-size: 0.9rem;
  margin-bottom: 6px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.popup-row {
  display: flex;
  justify-content: space-between;
  padding: 3px 0;
  border-bottom: 1px solid var(--border-color);
}

.popup-row:last-child { border-bottom: none; }
.popup-label { color: var(--text-secondary); }
.popup-value { font-weight: 600; font-variant-numeric: tabular-nums; }

/* Map filter buttons */
.map-filters {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.filter-btn {
  padding: 5px 12px;
  border: 1px solid var(--border-color);
  border-radius: 20px;
  background: transparent;
  color: var(--text-secondary);
  font-size: 0.72rem;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition);
  font-family: inherit;
}

.filter-btn:hover {
  border-color: var(--accent-air);
  color: var(--accent-air);
}

.filter-btn.active {
  background: rgba(0, 212, 170, 0.12);
  border-color: var(--accent-air);
  color: var(--accent-air);
}

/* ── Alerts Panel ──────────────────────────────────────── */
.alerts-card {
  display: flex;
  flex-direction: column;
  max-height: 520px;
}

.alerts-list {
  flex: 1;
  overflow-y: auto;
  padding: 6px;
}

.alerts-list::-webkit-scrollbar { width: 4px; }
.alerts-list::-webkit-scrollbar-track { background: transparent; }
.alerts-list::-webkit-scrollbar-thumb { background: var(--text-muted); border-radius: 4px; }

.alert-item {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 12px 14px;
  margin: 4px 0;
  border-radius: var(--radius-sm);
  border-left: 3px solid transparent;
  transition: background var(--transition);
  animation: slideInRight 0.3s ease both;
}

.alert-item:hover { background: var(--glass-bg); }

.alert-item.critica {
  border-left-color: var(--alert-critical);
  background: rgba(255, 71, 87, 0.05);
}
.alert-item.alta { border-left-color: var(--alert-high); }
.alert-item.media { border-left-color: var(--alert-medium); }
.alert-item.baja { border-left-color: var(--alert-low); }

.alert-icon { font-size: 1.3rem; flex-shrink: 0; margin-top: 2px; }

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

.alert-msg {
  font-size: 0.8rem;
  font-weight: 500;
  line-height: 1.4;
  word-break: break-word;
}

.alert-meta {
  display: flex;
  gap: 10px;
  margin-top: 4px;
  font-size: 0.68rem;
  color: var(--text-muted);
}

.alert-severity {
  padding: 1px 8px;
  border-radius: 8px;
  font-size: 0.65rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.alert-severity.critica { background: rgba(255, 71, 87, 0.2); color: var(--alert-critical); }
.alert-severity.alta { background: rgba(255, 107, 107, 0.15); color: var(--alert-high); }
.alert-severity.media { background: rgba(255, 217, 61, 0.15); color: var(--alert-medium); }
.alert-severity.baja { background: rgba(107, 203, 119, 0.15); color: var(--alert-low); }

.alert-empty {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 200px;
  color: var(--text-muted);
  font-size: 0.85rem;
}

/* ── Charts Row ────────────────────────────────────────── */
.charts-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--gap);
}

.chart-container {
  padding: 20px;
  height: 320px;
  position: relative;
}

/* ── Traffic Grid ──────────────────────────────────────── */
.traffic-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
  padding: 16px;
}

.traffic-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-radius: var(--radius-sm);
  background: var(--glass-bg);
  border: 1px solid var(--border-color);
  transition: all var(--transition);
  animation: fadeInUp 0.4s ease both;
}

.traffic-item:hover {
  background: var(--bg-card-hover);
  transform: translateX(4px);
}

.traffic-left {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.traffic-calle {
  font-weight: 600;
  font-size: 0.88rem;
}

.traffic-barrio {
  font-size: 0.72rem;
  color: var(--text-muted);
}

.traffic-right {
  display: flex;
  align-items: center;
  gap: 14px;
}

.traffic-stat {
  text-align: center;
}

.traffic-stat-value {
  font-size: 1.1rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  display: block;
}

.traffic-stat-label {
  font-size: 0.65rem;
  color: var(--text-muted);
  text-transform: uppercase;
}

.traffic-badge {
  padding: 5px 12px;
  border-radius: 20px;
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  white-space: nowrap;
}

.traffic-badge.fluido {
  background: rgba(107, 203, 119, 0.15);
  color: var(--alert-low);
  border: 1px solid rgba(107, 203, 119, 0.3);
}
.traffic-badge.denso {
  background: rgba(255, 217, 61, 0.15);
  color: var(--alert-medium);
  border: 1px solid rgba(255, 217, 61, 0.3);
}
.traffic-badge.atasco {
  background: rgba(255, 71, 87, 0.15);
  color: var(--alert-critical);
  border: 1px solid rgba(255, 71, 87, 0.3);
}

/* ── Select (Evolution chart) ──────────────────────────── */
.select-wrapper { position: relative; }

.custom-select {
  appearance: none;
  padding: 6px 30px 6px 12px;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  background: var(--bg-secondary);
  color: var(--text-primary);
  font-family: inherit;
  font-size: 0.78rem;
  font-weight: 500;
  cursor: pointer;
  transition: border-color var(--transition);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M0 0l5 6 5-6z' fill='%238b8fa3'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
}

.custom-select:focus {
  outline: none;
  border-color: var(--accent-air);
}

/* ── Sensors Table ─────────────────────────────────────── */
.table-section { animation-delay: 0.3s; }

.table-wrapper {
  overflow-x: auto;
  padding: 0 4px 4px;
}

#sensors-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.82rem;
}

#sensors-table th {
  padding: 14px 16px;
  text-align: left;
  font-weight: 600;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border-color);
  position: sticky;
  top: 0;
  background: var(--bg-card);
}

#sensors-table td {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-color);
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
}

#sensors-table tr {
  transition: background var(--transition);
}

#sensors-table tbody tr:hover {
  background: var(--glass-bg);
}

.sensor-type-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 10px;
  border-radius: 12px;
  font-size: 0.72rem;
  font-weight: 600;
}

.sensor-type-badge.aire {
  background: var(--accent-air-dim);
  color: var(--accent-air);
}

.sensor-type-badge.trafico {
  background: var(--accent-traffic-dim);
  color: var(--accent-traffic);
}

.sensor-type-badge.contenedor {
  background: var(--accent-container-dim);
  color: var(--accent-container);
}

.status-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-right: 6px;
}

.status-dot.activo { background: var(--alert-low); box-shadow: 0 0 6px rgba(107, 203, 119, 0.5); }
.status-dot.mantenimiento { background: var(--alert-medium); }
.status-dot.inactivo { background: var(--alert-critical); }

/* ═══════════════════════════════════════════════════════════════
   FOOTER
   ═══════════════════════════════════════════════════════════════ */
footer {
  text-align: center;
  padding: 28px;
  font-size: 0.75rem;
  color: var(--text-muted);
  border-top: 1px solid var(--border-color);
  margin-top: 20px;
}

/* ═══════════════════════════════════════════════════════════════
   ANIMATIONS
   ═══════════════════════════════════════════════════════════════ */
@keyframes fadeInUp {
  from { opacity: 0; transform: translateY(20px); }
  to   { opacity: 1; transform: translateY(0); }
}

@keyframes slideInRight {
  from { opacity: 0; transform: translateX(20px); }
  to   { opacity: 1; transform: translateX(0); }
}

@keyframes countUp {
  from { opacity: 0; transform: scale(0.8); }
  to   { opacity: 1; transform: scale(1); }
}

/* Pulse animation for critical alerts */
.alert-item.critica .alert-icon {
  animation: pulseIcon 1.5s ease-in-out infinite;
}

@keyframes pulseIcon {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

/* ═══════════════════════════════════════════════════════════════
   RESPONSIVE
   ═══════════════════════════════════════════════════════════════ */
@media (max-width: 1100px) {
  .map-alerts-row {
    grid-template-columns: 1fr;
  }
  .alerts-card {
    max-height: 300px;
  }
}

@media (max-width: 900px) {
  .kpi-row {
    grid-template-columns: repeat(2, 1fr);
  }
  .charts-row {
    grid-template-columns: 1fr;
  }
  #main-header {
    padding: 12px 16px;
  }
}

@media (max-width: 550px) {
  .kpi-row {
    grid-template-columns: 1fr;
  }
  .logo-text h1 { font-size: 1.1rem; }
  .kpi-value { font-size: 1.5rem; }
  #dashboard { padding: 12px; }
}

/* ═══════════════════════════════════════════════════════════════
   CHART.JS OVERRIDES
   ═══════════════════════════════════════════════════════════════ */
canvas {
  max-width: 100%;
}

/* ── Leaflet custom markers ────────────────────────────── */
.custom-marker {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  font-size: 18px;
  border: 2px solid rgba(255,255,255,0.3);
  box-shadow: 0 2px 12px rgba(0,0,0,0.4);
  transition: transform var(--transition);
}

.custom-marker:hover {
  transform: scale(1.2);
}

.custom-marker.aire {
  background: radial-gradient(circle, var(--accent-air), #009980);
}

.custom-marker.trafico {
  background: radial-gradient(circle, var(--accent-traffic), #3a5fcc);
}

.custom-marker.contenedor {
  background: radial-gradient(circle, var(--accent-container), #cc7a2e);
}

/* Loading skeleton */
.skeleton {
  background: linear-gradient(90deg, var(--bg-secondary) 25%, rgba(255,255,255,0.04) 50%, var(--bg-secondary) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.5s infinite;
  border-radius: 4px;
}

@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}
