:root {
  --bg: #0f172a; /* Slate 900 */
  --panel: #1e293b; /* Slate 800 */
  --border: #334155; /* Slate 700 */
  --text: #f8fafc; /* Slate 50 */
  --muted: #94a3b8; /* Slate 400 */
  --accent: #3b82f6; /* Blue 500 */
  --accent-hover: #60a5fa; /* Blue 400 */
  --row-hover: #334155;
  --ok: #10b981; /* Emerald 500 */
  --latest-bg: rgba(59, 130, 246, 0.15);
}

* { box-sizing: border-box; }

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

header {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 16px 24px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}

h1 { 
  font-size: 22px; 
  margin: 0; 
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 8px;
}

#subtitle { 
  color: var(--muted); 
  font-size: 14px; 
  font-weight: 400;
}

.header-actions {
  display: flex;
  gap: 10px;
  margin-left: 16px;
}

.btn-toggle {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 6px 12px;
  font-size: 12px;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s;
}

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

.btn-toggle.active {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
}

#updated { 
  margin-left: auto; 
  color: var(--muted); 
  font-size: 13px; 
  display: flex;
  align-items: center;
  gap: 6px;
}

.controls {
  flex-shrink: 0;
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 16px;
  padding: 12px 24px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  transition: max-height 0.3s ease, padding 0.3s ease, border-bottom 0.3s ease;
  overflow: hidden;
}

.controls.hidden {
  max-height: 0 !important;
  padding-top: 0;
  padding-bottom: 0;
  border-bottom: 0;
}

/* ...rest of controls... */
.controls label {
  display: flex;
  flex-direction: column;
  font-size: 12px;
  font-weight: 500;
  color: var(--muted);
  gap: 6px;
}

select, input, button:not(.btn-toggle) {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 12px;
  font-size: 13px;
  transition: border-color 0.2s, box-shadow 0.2s;
  outline: none;
}

select:focus, input:focus, button:not(.btn-toggle):focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.2);
}

input[type="range"] { 
  padding: 0; 
  accent-color: var(--accent); 
  width: 140px; 
  height: 20px;
}

#magMinLabel { 
  color: var(--text); 
  font-size: 13px; 
  font-weight: 700; 
  display: inline-block;
  min-width: 24px;
}

button:not(.btn-toggle) { 
  cursor: pointer; 
  font-weight: 500;
  background: var(--panel);
}

button:not(.btn-toggle):hover { 
  border-color: var(--accent); 
  color: var(--accent-hover);
}

#guardarVista {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

#guardarVista:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
  color: #fff;
}

.layout {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 1.5fr;
  gap: 0;
  min-height: 0; /* important for flex children scrolling */
  transition: grid-template-columns 0.3s ease;
}

.layout.table-hidden {
  grid-template-columns: 1fr 0fr;
}

.mapa {
  width: 100%;
  height: 100%;
  position: relative;
}

#map { 
  width: 100%;
  height: 100%; 
  background: #0f172a; 
  z-index: 1; 
}

.tabla-wrap { 
  overflow: auto; 
  border-left: 1px solid var(--border); 
  background: var(--panel);
  transition: opacity 0.3s ease, transform 0.3s ease;
}

.layout.table-hidden .tabla-wrap {
  opacity: 0;
  pointer-events: none;
  transform: translateX(20px);
}

/* Feed Layout (replaces table) */
.feed-controls {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  gap: 12px;
  position: sticky;
  top: 0;
  z-index: 5;
  font-size: 13px;
  color: var(--muted);
}

.feed-list {
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.feed-day-separator {
  color: var(--accent);
  font-weight: 700;
  text-align: center;
  padding: 16px 0 8px;
  font-size: 13px;
  letter-spacing: 0.5px;
  text-transform: uppercase;
}

.feed-card {
  display: flex;
  align-items: center;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 40px; /* Óvalo style */
  padding: 8px 16px 8px 8px;
  cursor: pointer;
  transition: all 0.2s ease;
  gap: 12px;
}

.feed-card:hover {
  border-color: var(--accent);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

.feed-card.active {
  border-color: var(--accent);
  background: rgba(59, 130, 246, 0.1);
}

.feed-card-latest {
  border-color: rgba(59, 130, 246, 0.5);
  box-shadow: 0 0 10px rgba(59, 130, 246, 0.2);
}

.fc-mag {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  color: #fff;
  font-weight: 700;
  font-size: 15px;
  text-shadow: 0 1px 2px rgba(0,0,0,0.5);
  box-shadow: inset 0 0 0 1px rgba(0,0,0,0.2);
  flex-shrink: 0;
}

.fc-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0; /* for truncation */
}

.fc-zona {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.fc-time {
  font-size: 12px;
  color: var(--muted);
}

.fc-coords {
  font-size: 11px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  text-align: right;
}

.mag { 
  font-weight: 700; 
}

.mag-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 28px;
  height: 22px;
  border-radius: 12px;
  color: #fff;
  font-size: 12px;
  text-shadow: 0 1px 2px rgba(0,0,0,0.5);
  box-shadow: inset 0 0 0 1px rgba(0,0,0,0.2);
}

td.num { 
  font-variant-numeric: tabular-nums; 
}

.row-latest td {
  background-color: var(--latest-bg);
}

.row-latest:hover td {
  background-color: rgba(59, 130, 246, 0.25);
}

/* Latest marker animation */
@keyframes pulse {
  0% { stroke-width: 2; stroke-opacity: 1; fill-opacity: 1; }
  50% { stroke-width: 6; stroke-opacity: 0.5; fill-opacity: 0.8; }
  100% { stroke-width: 2; stroke-opacity: 1; fill-opacity: 1; }
}

.marker-latest path {
  animation: pulse 1.5s infinite;
  transform-origin: center;
}

/* Leaflet overrides for dark mode */
.leaflet-popup-content-wrapper { 
  background: var(--panel); 
  color: var(--text); 
  border: 1px solid var(--border);
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.5);
}
.leaflet-popup-tip { 
  background: var(--panel); 
  border-bottom: 1px solid var(--border);
  border-right: 1px solid var(--border);
}

.leaflet-container .leaflet-control-attribution {
  background: rgba(15, 23, 42, 0.7);
  color: var(--muted);
}

.empty {
  padding: 32px 20px;
  color: var(--muted);
  text-align: center;
  font-size: 14px;
}

/* Modal */
.modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(15, 23, 42, 0.8);
  backdrop-filter: blur(4px);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 1;
  transition: opacity 0.3s ease;
}

.modal.hidden {
  opacity: 0;
  pointer-events: none;
}

.modal-content {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  width: 90%;
  max-width: 600px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.5), 0 10px 10px -5px rgba(0, 0, 0, 0.3);
  transform: translateY(0);
  transition: transform 0.3s ease;
}

.modal.hidden .modal-content {
  transform: translateY(20px);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 24px;
  border-bottom: 1px solid var(--border);
}

.modal-header h2 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
}

.btn-close {
  background: transparent;
  border: none;
  color: var(--muted);
  font-size: 18px;
  cursor: pointer;
  padding: 4px;
}

.btn-close:hover {
  color: var(--text);
}

.modal-body {
  padding: 24px;
}

/* Stats Layout */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}

.stat-card {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 16px;
  text-align: center;
}

.stat-val {
  font-size: 28px;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 4px;
}

.stat-val.accent { color: var(--accent); }
.stat-val.danger { color: #ef4444; }
.stat-val.ok { color: var(--ok); }

.stat-label {
  font-size: 12px;
  color: var(--muted);
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

/* CSS Bar Chart */
.chart-container {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 20px;
}

.chart-title {
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 16px;
  color: var(--text);
}

.chart-row {
  display: flex;
  align-items: center;
  margin-bottom: 12px;
  gap: 12px;
}
.chart-row:last-child { margin-bottom: 0; }

.chart-label {
  width: 60px;
  font-size: 12px;
  color: var(--muted);
  font-weight: 500;
  text-align: right;
}

.chart-bar-bg {
  flex: 1;
  background: var(--panel);
  height: 12px;
  border-radius: 6px;
  overflow: hidden;
}

.chart-bar-fill {
  height: 100%;
  border-radius: 6px;
  transition: width 0.5s ease-out;
}

.chart-val {
  width: 40px;
  font-size: 13px;
  font-weight: 600;
}

@media (max-width: 900px) {
  header {
    flex-wrap: wrap;
    padding: 12px 16px;
  }
  
  h1 {
    font-size: 18px;
    width: 100%;
    margin-bottom: 8px;
  }
  
  .header-actions {
    margin-left: 0;
  }
  
  #updated {
    margin-left: auto;
  }

  .layout { 
    display: flex;
    flex-direction: column;
  }

  .mapa {
    flex: 1;
    width: 100%;
    min-height: 40vh;
  }

  .tabla-wrap { 
    border-left: none; 
    border-top: 1px solid var(--border); 
    flex: 1;
    max-height: 50vh;
  }
  
  .layout.table-hidden .tabla-wrap {
    display: none;
  }
  
  /* Ocultar columnas de Latitud y Longitud en móvil para que encaje mejor */
  th:nth-child(4), td:nth-child(4),
  th:nth-child(5), td:nth-child(5) {
    display: none;
  }
}