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

/* ==========================================================================
   DESIGN TOKENS & SYSTEM VARIABLES (Forced Dark Theme)
   ========================================================================== */
:root {
  /* Fonts */
  --font-display: 'Outfit', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --font-body: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;

  /* Deep Space Theme Variables */
  --bg-app: radial-gradient(circle at top, #0f172a 0%, #020617 100%);
  --bg-card: rgba(15, 23, 42, 0.45);
  --border-card: 1px solid rgba(255, 255, 255, 0.06);
  --shadow-card: 0 12px 40px 0 rgba(0, 0, 0, 0.6);
  --text-main: #f8fafc;
  --text-muted: #94a3b8;
  --text-inverse: #020617;
  
  --primary: #3b82f6;
  --primary-glow: rgba(59, 130, 246, 0.3);
  --accent: #22d3ee;
  
  /* Status Colors */
  --color-safe: #10b981;
  --color-safe-glow: rgba(16, 185, 129, 0.15);
  
  --color-azul: #3b82f6;
  --color-azul-glow: rgba(59, 130, 246, 0.2);
  
  --color-amarilla: #eab308;
  --color-amarilla-glow: rgba(234, 179, 8, 0.2);
  
  --color-roja: #ef4444;
  --color-roja-glow: rgba(239, 68, 68, 0.25);
  
  --color-sirena: #f43f5e;
  --color-sirena-glow: rgba(244, 63, 94, 0.35);

  /* Wave Liquid Colors */
  --wave-color: #2563eb;
  --wave-color-back: #38bdf8;

  /* Glass blur */
  --glass-blur: 24px;
  --transition-speed: 0.3s;
}

/* ==========================================================================
   GLOBAL RESET & BASIC STYLES
   ========================================================================== */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: var(--font-body);
  background: var(--bg-app);
  color: var(--text-main);
  min-height: 100vh;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  overflow-x: hidden;
}

h1, h2, h3, h4, h5, h6 {
  font-family: var(--font-display);
  font-weight: 700;
  letter-spacing: -0.02em;
}

/* ==========================================================================
   HEADER SECTION
   ========================================================================== */
header {
  background: rgba(2, 6, 23, 0.4);
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
  backdrop-filter: blur(8px);
  padding: 1.25rem 2rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: sticky;
  top: 0;
  z-index: 100;
}

.logo-container {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.logo-icon {
  background: linear-gradient(135deg, var(--primary) 0%, var(--accent) 100%);
  width: 2.75rem;
  height: 2.75rem;
  border-radius: 0.75rem;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 1.5rem;
  box-shadow: 0 4px 15px var(--primary-glow);
}

.logo-text h1 {
  font-size: 1.5rem;
  font-weight: 800;
  background: linear-gradient(135deg, var(--primary) 0%, var(--accent) 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.logo-text span {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.15em;
  color: var(--text-muted);
  font-weight: 600;
  display: block;
}

.nav-controls {
  display: flex;
  align-items: center;
  gap: 1.25rem;
}

.status-badge {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.8rem;
  font-weight: 600;
  padding: 0.5rem 0.85rem;
  border-radius: 2rem;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.05);
}

.status-badge .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background-color: var(--color-safe);
  box-shadow: 0 0 10px var(--color-safe);
}

.status-badge.loading .dot {
  background-color: var(--color-amarilla);
  box-shadow: 0 0 10px var(--color-amarilla);
  animation: pulse-dot 1s infinite alternate;
}

.status-badge.offline .dot {
  background-color: var(--color-roja);
  box-shadow: 0 0 10px var(--color-roja);
}

/* ==========================================================================
   MAIN DASHBOARD GRID LAYOUT
   ========================================================================== */
main {
  max-width: 1500px;
  width: 100%;
  margin: 0 auto;
  padding: 2rem;
  display: flex;
  flex-direction: column;
  gap: 2rem;
  flex: 1;
}

.dashboard-row {
  display: grid;
  grid-template-columns: 1fr;
  gap: 2rem;
}

/* ==========================================================================
   GLASSMORPHIC CARDS
   ========================================================================== */
.card {
  background: var(--bg-card);
  border: var(--border-card);
  border-radius: 1.25rem;
  padding: 1.75rem;
  box-shadow: var(--shadow-card);
  backdrop-filter: blur(var(--glass-blur));
  transition: transform var(--transition-speed) ease, box-shadow var(--transition-speed) ease, border-color var(--transition-speed) ease;
  position: relative;
  overflow: hidden;
}

.card:hover {
  transform: translateY(-2px);
  border-color: rgba(255, 255, 255, 0.1);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.5rem;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
  padding-bottom: 0.75rem;
}

.card-title {
  font-size: 1.35rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

.card-title i {
  color: var(--primary);
}

/* ==========================================================================
   SENSORS ROW LAYOUT
   ========================================================================== */
.sensor-card-layout {
  display: flex;
  flex-direction: column;
  gap: 1.75rem;
}

.card-top-row {
  display: grid;
  grid-template-columns: 280px 1fr;
  gap: 2.5rem;
  align-items: stretch;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
  padding-bottom: 1.5rem;
}

.gauge-panel-vertical {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
  width: 100%;
}

.gauge-header {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 0.35rem;
}

.gauge-header .river-badge-alert {
  position: static;
  display: inline-block;
  margin-bottom: 0.15rem;
}

.gauge-header .river-name {
  margin-bottom: 0;
}

.lbl-time-update {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.gauge-limits-legend-condensed {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem 0.75rem;
  background: rgba(255, 255, 255, 0.01);
  border: 1px solid rgba(255, 255, 255, 0.03);
  padding: 0.75rem;
  border-radius: 0.75rem;
  width: 100%;
  font-size: 0.75rem;
}

.gauge-limits-legend-condensed .limit-item {
  display: flex;
  align-items: center;
}

.card-bottom-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 2rem;
  align-items: start;
}

.table-box {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  width: 100%;
}

.chart-column {
  background: rgba(255, 255, 255, 0.01);
  border: 1px solid rgba(255, 255, 255, 0.03);
  border-radius: 1rem;
  padding: 1.25rem;
  width: 100%;
  display: flex;
  flex-direction: column;
}

@media (max-width: 1100px) {
  .card-top-row {
    grid-template-columns: 1fr;
    justify-items: center;
  }
  .card-bottom-row {
    grid-template-columns: 1fr;
  }
}

.gauge-panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 1rem;
  background: rgba(255, 255, 255, 0.01);
  border: 1px solid rgba(255, 255, 255, 0.03);
  border-radius: 1rem;
  position: relative;
}

.river-badge-alert {
  position: absolute;
  top: 1rem;
  right: 1rem;
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  padding: 0.35rem 0.75rem;
  border-radius: 2rem;
  letter-spacing: 0.05em;
  background: var(--color-safe-glow);
  color: var(--color-safe);
  border: 1px solid rgba(255, 255, 255, 0.05);
  transition: all 0.3s ease;
}

.river-badge-alert.azul {
  background: var(--color-azul-glow);
  color: var(--color-azul);
  animation: pulse-alert 2.5s infinite;
}

.river-badge-alert.amarilla {
  background: var(--color-amarilla-glow);
  color: var(--color-amarilla);
  animation: pulse-alert 2s infinite;
}

.river-badge-alert.roja {
  background: var(--color-roja-glow);
  color: var(--color-roja);
  animation: pulse-alert 1.2s infinite;
}

.river-badge-alert.sirena {
  background: var(--color-sirena-glow);
  color: var(--color-sirena);
  animation: pulse-alert-fast 0.6s infinite alternate;
  font-weight: 900;
  letter-spacing: 0.1em;
}

.river-name {
  font-size: 1.35rem;
  font-weight: 800;
  margin-bottom: 1.5rem;
  background: linear-gradient(135deg, #ffffff 0%, var(--text-muted) 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

/* Custom Water Dome Gauge */
.gauge-container {
  position: relative;
  width: 160px;
  height: 160px;
  margin-bottom: 1.25rem;
}

.wave-gauge {
  position: relative;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.25);
  border: 4px solid var(--border-card);
  overflow: hidden;
  box-shadow: inset 0 8px 30px rgba(0, 0, 0, 0.5), 0 8px 24px rgba(0, 0, 0, 0.2);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}

.wave-gauge-water {
  position: absolute;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 40%; /* JS sets this height dynamically */
  background: var(--wave-color);
  transition: height 1.5s cubic-bezier(0.4, 0, 0.2, 1);
  z-index: 2;
}

.wave-gauge-water::before,
.wave-gauge-water::after {
  content: "";
  position: absolute;
  width: 280px;
  height: 280px;
  bottom: 100%;
  left: 50%;
  background: #0b0f19; /* Solid background matching wave-gauge bg */
  border-radius: 38%;
  transform: translate(-50%, 4px) rotate(0deg);
  animation: wave-rotate 10s infinite linear;
  z-index: 3;
}

.wave-gauge-water::after {
  background: var(--wave-color-back);
  border-radius: 41%;
  opacity: 0.45;
  transform: translate(-50%, 0px) rotate(0deg);
  animation: wave-rotate 6s infinite linear;
  z-index: 1;
}

.gauge-value-overlay {
  position: absolute;
  z-index: 5;
  color: var(--text-main);
  text-align: center;
  pointer-events: none;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.8);
}

.gauge-time-overlay {
  font-size: 0.68rem;
  color: var(--text-muted);
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  margin-top: -0.1rem;
  opacity: 0.85;
}

.gauge-num {
  font-family: var(--font-display);
  font-size: 2.75rem;
  font-weight: 800;
  line-height: 1;
}

.gauge-unit {
  font-size: 0.9rem;
  font-weight: 700;
  color: var(--text-muted);
}

.gauge-limits-legend {
  display: flex;
  flex-direction: column;
  width: 100%;
  gap: 0.35rem;
  font-size: 0.75rem;
  margin-top: 0.5rem;
  padding: 0.5rem;
  border-top: 1px solid rgba(255, 255, 255, 0.04);
}

.limit-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

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

/* Pulsing Alerts for Entire Card */
.sensor-card-layout.alert-state-azul {
  box-shadow: 0 0 30px rgba(59, 130, 246, 0.15);
  border-color: rgba(59, 130, 246, 0.3);
}

.sensor-card-layout.alert-state-amarilla {
  box-shadow: 0 0 30px rgba(234, 179, 8, 0.2);
  border-color: rgba(234, 179, 8, 0.4);
}

.sensor-card-layout.alert-state-roja {
  box-shadow: 0 0 35px rgba(239, 68, 68, 0.3);
  border-color: rgba(239, 68, 68, 0.5);
  animation: bg-alert-red 2s infinite alternate;
}

.sensor-card-layout.alert-state-sirena {
  box-shadow: 0 0 45px rgba(244, 63, 94, 0.45);
  border-color: rgba(244, 63, 94, 0.7);
  animation: bg-alert-sirena 1s infinite alternate;
}

/* ==========================================================================
   GLASSMORPHIC RECENT DATA TABLES
   ========================================================================== */
.table-panel {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  width: 100%;
}

.panel-title {
  font-size: 0.9rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.data-table-wrapper {
  width: 100%;
  border-radius: 0.75rem;
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.04);
  background: rgba(0, 0, 0, 0.15);
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  text-align: left;
  font-size: 0.8rem;
}

.data-table th,
.data-table td {
  padding: 0.75rem 1rem;
  border-bottom: 1px solid rgba(255, 255, 255, 0.03);
}

.data-table th {
  background: rgba(255, 255, 255, 0.02);
  color: var(--text-muted);
  font-weight: 600;
  text-transform: uppercase;
  font-size: 0.7rem;
  letter-spacing: 0.05em;
}

.data-table tbody tr:hover {
  background: rgba(255, 255, 255, 0.015);
}

.data-table tbody tr:last-child td {
  border-bottom: none;
}

.td-time {
  font-family: monospace;
  color: var(--accent);
}

.td-val-avg {
  font-weight: 700;
  color: var(--text-main);
}

.td-val-alert {
  font-weight: 600;
}

/* ==========================================================================
   CHARTS STYLING
   ========================================================================== */
.chart-panel {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
}

#chartVenado,
#chartFrio {
  flex: 1;
  min-height: 0;
}

/* ==========================================================================
   SECURITY CAMERAS SECTION (Flicker-Free 5s Grid)
   ========================================================================== */
.cameras-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 2rem;
}

@media (max-width: 768px) {
  .cameras-grid {
    grid-template-columns: 1fr;
  }
}

.camera-box {
  background: rgba(0, 0, 0, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.03);
  border-radius: 1.25rem;
  padding: 1rem;
  display: flex;
  flex-direction: column;
}

.camera-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.75rem;
}

.camera-name {
  font-size: 1.05rem;
  font-weight: 600;
  font-family: var(--font-display);
}

.camera-status {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.65rem;
  font-weight: 700;
  text-transform: uppercase;
  color: var(--color-safe);
}

.camera-status.offline {
  color: var(--text-muted);
}

.camera-status .pulse-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background-color: var(--color-safe);
  box-shadow: 0 0 6px var(--color-safe);
  animation: pulse-dot 1.2s infinite alternate;
}

.camera-status.offline .pulse-dot {
  background-color: var(--text-muted);
  box-shadow: none;
  animation: none;
}

/* Double Buffered Stream */
.camera-viewport {
  width: 100%;
  aspect-ratio: 16/9;
  border-radius: 0.85rem;
  background-color: #020617;
  overflow: hidden;
  position: relative;
  box-shadow: inset 0 0 20px rgba(0,0,0,0.8), 0 4px 15px rgba(0,0,0,0.25);
  border: 1px solid rgba(255, 255, 255, 0.02);
}

.camera-img-buffer {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  opacity: 0;
  transition: opacity 0.5s ease-in-out;
  cursor: zoom-in;
}

.camera-img-buffer.active {
  opacity: 1;
  z-index: 2;
}

.camera-img-buffer.preload {
  opacity: 0;
  z-index: 1;
}

/* Timestamp overlays */
.camera-overlay {
  position: absolute;
  bottom: 0.6rem;
  right: 0.6rem;
  background: rgba(2, 6, 23, 0.75);
  backdrop-filter: blur(4px);
  color: #fff;
  font-family: monospace;
  font-size: 0.7rem;
  padding: 0.3rem 0.6rem;
  border-radius: 0.4rem;
  pointer-events: none;
  z-index: 3;
  border: 1px solid rgba(255,255,255,0.05);
}

.camera-overlay.rate-info {
  bottom: unset;
  top: 0.6rem;
  right: unset;
  left: 0.6rem;
  color: var(--text-muted);
}

.camera-spinner {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 28px;
  height: 28px;
  border: 2px solid rgba(255,255,255,0.05);
  border-radius: 50%;
  border-top-color: var(--primary);
  animation: spinner-spin 0.8s linear infinite;
  opacity: 0;
  transition: opacity 0.3s ease;
  pointer-events: none;
  z-index: 5;
}

.camera-viewport.loading .camera-spinner {
  opacity: 1;
}

/* ==========================================================================
   THEATER LIGHTBOX ZOOM VIEW
   ========================================================================== */
.lightbox {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(2, 6, 23, 0.96);
  backdrop-filter: blur(12px);
  z-index: 2000;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.lightbox.open {
  opacity: 1;
  pointer-events: all;
}

.lightbox-close {
  position: absolute;
  top: 1.5rem;
  right: 1.5rem;
  color: #fff;
  font-size: 2.5rem;
  background: none;
  border: none;
  cursor: pointer;
  transition: transform 0.2s ease;
}

.lightbox-close:hover {
  transform: scale(1.1) rotate(90deg);
}

.lightbox-content {
  max-width: 90%;
  max-height: 80%;
  border-radius: 1rem;
  box-shadow: 0 25px 60px rgba(0, 0, 0, 0.9);
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.lightbox-title {
  color: #fff;
  margin-top: 1.5rem;
  font-size: 1.35rem;
  font-family: var(--font-display);
}

/* ==========================================================================
   EMERGENCY BANNER SYSTEM (Sirena)
   ========================================================================== */
.alert-banner {
  background: var(--color-sirena);
  color: white;
  text-align: center;
  padding: 0.85rem 2rem;
  font-weight: 800;
  font-size: 1.15rem;
  font-family: var(--font-display);
  display: none;
  align-items: center;
  justify-content: center;
  gap: 0.65rem;
  z-index: 99;
  box-shadow: 0 4px 20px rgba(244, 63, 94, 0.5);
  letter-spacing: 0.02em;
  text-transform: uppercase;
}

.alert-banner.active {
  display: flex;
  animation: banner-slide-down 0.4s cubic-bezier(0.16, 1, 0.3, 1), banner-glow 1s infinite alternate;
}

/* ==========================================================================
   KEYFRAME ANIMATIONS
   ========================================================================== */
@keyframes wave-rotate {
  from {
    transform: translate(-50%, 0%) rotate(0deg);
  }
  to {
    transform: translate(-50%, 0%) rotate(360deg);
  }
}

@keyframes pulse-dot {
  from {
    transform: scale(0.9);
    opacity: 0.5;
  }
  to {
    transform: scale(1.1);
    opacity: 1;
  }
}

@keyframes pulse-alert {
  0% {
    transform: scale(1);
    box-shadow: 0 0 0 0 rgba(255, 255, 255, 0.2);
  }
  50% {
    transform: scale(1.03);
    box-shadow: 0 0 8px 2px rgba(255, 255, 255, 0.05);
  }
  100% {
    transform: scale(1);
    box-shadow: 0 0 0 0 rgba(255, 255, 255, 0.2);
  }
}

@keyframes pulse-alert-fast {
  from {
    transform: scale(0.96);
    box-shadow: 0 0 2px rgba(244, 63, 94, 0.2);
  }
  to {
    transform: scale(1.04);
    box-shadow: 0 0 15px 4px rgba(244, 63, 94, 0.6);
  }
}

@keyframes bg-alert-red {
  from {
    border-color: rgba(239, 68, 68, 0.2);
    box-shadow: 0 0 10px rgba(239, 68, 68, 0.1);
  }
  to {
    border-color: rgba(239, 68, 68, 0.6);
    box-shadow: 0 0 35px rgba(239, 68, 68, 0.35);
  }
}

@keyframes bg-alert-sirena {
  from {
    border-color: rgba(244, 63, 94, 0.25);
    background: rgba(244, 63, 94, 0.01);
    box-shadow: 0 0 15px rgba(244, 63, 94, 0.15);
  }
  to {
    border-color: rgba(244, 63, 94, 0.85);
    background: rgba(244, 63, 94, 0.06);
    box-shadow: 0 0 45px rgba(244, 63, 94, 0.5);
  }
}

@keyframes banner-glow {
  from {
    background-color: #f43f5e;
    box-shadow: 0 4px 15px rgba(244, 63, 94, 0.4);
  }
  to {
    background-color: #be123c;
    box-shadow: 0 4px 30px rgba(244, 63, 94, 0.8);
  }
}

@keyframes banner-slide-down {
  from {
    transform: translateY(-100%);
  }
  to {
    transform: translateY(0);
  }
}

@keyframes spinner-spin {
  to {
    transform: translate(-50%, -50%) rotate(360deg);
  }
}

/* ==========================================================================
   FOOTER
   ========================================================================== */
footer {
  text-align: center;
  padding: 2rem;
  margin-top: auto;
  border-top: 1px solid rgba(255, 255, 255, 0.04);
  background: rgba(2, 6, 23, 0.4);
  color: var(--text-muted);
  font-size: 0.8rem;
  font-weight: 500;
}

footer a {
  color: var(--primary);
  text-decoration: none;
  transition: color 0.2s ease;
}

footer a:hover {
  color: var(--accent);
}
