/* ==========================================================================
   SISTEMA DE ESTILOS CENTRAL - COLLARNET
   Tema: Sleek Dark Mode (Aesthetics & High Contrast)
   ========================================================================== */

/* 1. TOKENS DE DISEÑO Y VARIABLES GLOBALES */
:root {
  --bg-primary: #090d16;
  --bg-secondary: #111827;
  --bg-card: rgba(31, 41, 55, 0.65); /* Glassmorphism Base */
  --border-color: rgba(75, 85, 99, 0.4);
  
  /* Paleta Tailored */
  --text-primary: #f3f4f6;
  --text-secondary: #9ca3af;
  --color-accent: #3b82f6; /* Azul Eléctrico */
  --color-accent-hover: #2563eb;
  
  /* Alertas */
  --color-safe: #10b981;     /* Emerald Green */
  --color-warning: #f59e0b;  /* Amber */
  --color-danger: #ef4444;   /* Crimson Red */
  
  /* Fuentes */
  --font-title: 'Outfit', sans-serif;
  --font-body: 'Inter', sans-serif;
  
  /* Animaciones y Bordes */
  --radius-lg: 16px;
  --radius-md: 10px;
  --radius-sm: 6px;
  --shadow-lg: 0 10px 25px -5px rgba(0, 0, 0, 0.5), 0 8px 10px -6px rgba(0, 0, 0, 0.5);
  --blur-amount: 12px;
  --transition-smooth: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* 2. REPETITIVOS Y ESTRUCTURA GENERAL */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: var(--font-body);
  scrollbar-width: thin;
  scrollbar-color: var(--border-color) transparent;
}

body, html {
  height: 100%;
  background-color: var(--bg-primary);
  color: var(--text-primary);
  overflow: hidden;
}

/* Contenedor Principal en Grid de 3 Columnas */
.app-container {
  display: flex;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
}

/* ==========================================================================
   3. BARRA LATERAL (Sidebar)
   ========================================================================== */
.sidebar {
  width: 320px;
  background-color: var(--bg-secondary);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  transition: var(--transition-smooth);
  z-index: 1000;
  box-shadow: var(--shadow-lg);
}

.sidebar.collapsed {
  width: 70px;
}

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

.logo-title {
  font-family: var(--font-title);
  font-size: 1.5rem;
  font-weight: 800;
  letter-spacing: -0.5px;
}

.logo-title span {
  color: var(--color-safe);
}

.sidebar.collapsed .logo-title {
  display: none;
}

.toggle-btn {
  background: none;
  border: none;
  color: var(--text-primary);
  font-size: 1.25rem;
  cursor: pointer;
  padding: 5px;
  border-radius: var(--radius-sm);
  transition: var(--transition-smooth);
}

.toggle-btn:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

/* Indicador de Conexión */
.connection-status {
  padding: 10px 20px;
  background-color: rgba(0, 0, 0, 0.2);
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.8rem;
}

.sidebar.collapsed .connection-status .status-text {
  display: none;
}

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

.status-dot.online {
  background-color: var(--color-safe);
  box-shadow: 0 0 8px var(--color-safe);
}

.status-dot.offline {
  background-color: var(--color-danger);
  box-shadow: 0 0 8px var(--color-danger);
  animation: pulse-glow-red 1.5s infinite;
}

/* Menú de Ítems */
.sidebar-menu {
  display: flex;
  flex-direction: column;
  padding: 15px 10px;
  gap: 5px;
}

.menu-item {
  display: flex;
  align-items: center;
  gap: 15px;
  padding: 12px 15px;
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 0.95rem;
  font-weight: 500;
  text-align: left;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: var(--transition-smooth);
}

.menu-item:hover, .menu-item.active {
  background-color: rgba(255, 255, 255, 0.05);
  color: var(--text-primary);
}

.menu-item.active {
  border-left: 3px solid var(--color-accent);
  padding-left: 12px;
}

.sidebar.collapsed .menu-item .label {
  display: none;
}

/* Área de Contenido de la Barra Lateral */
.sidebar-content {
  flex-grow: 1;
  padding: 20px;
  overflow-y: auto;
  border-top: 1px solid var(--border-color);
}

.sidebar.collapsed .sidebar-content {
  display: none;
}

.tab-panel {
  display: none;
}

.tab-panel.active {
  display: block;
}

.tab-panel h3 {
  font-family: var(--font-title);
  font-size: 1.15rem;
  margin-bottom: 15px;
  font-weight: 600;
}

.help-text {
  font-size: 0.8rem;
  color: var(--text-secondary);
  line-height: 1.4;
  margin-bottom: 20px;
}

/* Formulario e Inputs */
.form-group {
  margin-bottom: 15px;
}

.form-group label {
  display: block;
  font-size: 0.8rem;
  color: var(--text-secondary);
  margin-bottom: 5px;
  font-weight: 500;
}

input, select {
  width: 100%;
  padding: 10px 12px;
  background-color: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  color: var(--text-primary);
  font-size: 0.9rem;
  transition: var(--transition-smooth);
}

select option {
  background-color: #1e293b;
  color: #f3f4f6;
  padding: 8px;
}

input:focus, select:focus {
  outline: none;
  border-color: var(--color-accent);
  background-color: rgba(255, 255, 255, 0.08);
}

.submit-btn {
  width: 100%;
  padding: 12px;
  background-color: var(--color-accent);
  color: var(--text-primary);
  border: none;
  border-radius: var(--radius-md);
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
  transition: var(--transition-smooth);
  box-shadow: 0 4px 6px -1px rgba(59, 130, 246, 0.3);
}

.submit-btn:hover {
  background-color: var(--color-accent-hover);
}

.divider {
  border: 0;
  height: 1px;
  background-color: var(--border-color);
  margin: 20px 0;
}

/* Filtros */
.filter-buttons {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 5px;
  margin-bottom: 20px;
}

.filter-btn {
  padding: 8px;
  background-color: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  font-size: 0.8rem;
  font-weight: 500;
  cursor: pointer;
  transition: var(--transition-smooth);
}

.filter-btn:hover, .filter-btn.active {
  background-color: var(--color-accent);
  color: var(--text-primary);
  border-color: var(--color-accent);
}

/* Tarjetas de Estadísticas Mini */
.stats-mini-panel {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-top: 20px;
}

.stat-card {
  background-color: rgba(255, 255, 255, 0.02);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 10px;
  text-align: center;
  display: flex;
  flex-direction: column;
}

.stat-num {
  font-size: 1.5rem;
  font-weight: 700;
  font-family: var(--font-title);
}

.stat-card.normal .stat-num { color: var(--color-safe); }
.stat-card.alert .stat-num { color: var(--color-warning); }
.stat-card.danger .stat-num { color: var(--color-danger); }

.stat-lbl {
  font-size: 0.7rem;
  color: var(--text-secondary);
  margin-top: 3px;
}

/* Acordeón (Altas) */
.acc-item {
  margin-bottom: 10px;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  overflow: hidden;
  background-color: rgba(255, 255, 255, 0.02);
}

.acc-title {
  padding: 12px 15px;
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
  user-select: none;
  list-style: none;
  outline: none;
}

.acc-title::-webkit-details-marker {
  display: none;
}

.acc-form {
  padding: 15px;
  border-top: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  gap: 10px;
  background-color: rgba(0, 0, 0, 0.15);
}

/* Controles de Dibujo */
.draw-controls {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.action-btn {
  padding: 10px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-color);
  background-color: rgba(255, 255, 255, 0.03);
  color: var(--text-primary);
  font-weight: 500;
  cursor: pointer;
  transition: var(--transition-smooth);
}

.action-btn:hover {
  background-color: rgba(255, 255, 255, 0.08);
}

.action-btn.active {
  background-color: rgba(255, 255, 255, 0.15);
  border-color: var(--text-primary);
}

/* ==========================================================================
   4. MAPA (Viewport)
   ========================================================================== */
.map-viewport {
  flex-grow: 1;
  position: relative;
  height: 100%;
}

#map {
  width: 100%;
  height: 100%;
  background-color: #1a1a1a;
}

/* Capa Flotante de Controles de Mapa */
.map-overlay-controls {
  position: absolute;
  top: 15px;
  right: 15px;
  z-index: 1000;
  display: flex;
  gap: 5px;
  background-color: var(--bg-card);
  backdrop-filter: blur(var(--blur-amount));
  -webkit-backdrop-filter: blur(var(--blur-amount));
  padding: 4px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-color);
}

.map-control-btn {
  padding: 8px 12px;
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 0.8rem;
  font-weight: 600;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: var(--transition-smooth);
}

.map-control-btn:hover, .map-control-btn.active {
  background-color: rgba(255, 255, 255, 0.1);
  color: var(--text-primary);
}

/* Estilos de elementos de Leaflet en Dark Mode */
.leaflet-bar {
  border: 1px solid var(--border-color) !important;
  box-shadow: var(--shadow-lg) !important;
}

.leaflet-bar a {
  background-color: var(--bg-secondary) !important;
  color: var(--text-primary) !important;
  border-bottom: 1px solid var(--border-color) !important;
  transition: var(--transition-smooth);
}

.leaflet-bar a:hover {
  background-color: rgba(255, 255, 255, 0.1) !important;
}

/* Popups en el mapa */
.leaflet-popup-content-wrapper {
  background-color: var(--bg-secondary) !important;
  color: var(--text-primary) !important;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md) !important;
  box-shadow: var(--shadow-lg) !important;
  padding: 5px;
}

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

.popup-card {
  padding: 5px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.popup-card h4 {
  font-family: var(--font-title);
  font-size: 1rem;
  border-bottom: 1px solid var(--border-color);
  padding-bottom: 5px;
}

.popup-card p {
  font-size: 0.75rem;
  color: var(--text-secondary);
  line-height: 1.4;
}

.popup-card strong {
  color: var(--text-primary);
}

.popup-action-btn {
  padding: 6px;
  background-color: var(--color-accent);
  color: var(--text-primary);
  border: none;
  border-radius: var(--radius-sm);
  font-size: 0.75rem;
  font-weight: 600;
  cursor: pointer;
  transition: var(--transition-smooth);
  text-align: center;
}

.popup-action-btn:hover {
  background-color: var(--color-accent-hover);
}

/* ==========================================================================
   5. PANEL EN VIVO (Live Monitoring)
   ========================================================================== */
.live-panel {
  width: 280px;
  background-color: var(--bg-secondary);
  border-left: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  z-index: 1000;
  box-shadow: var(--shadow-lg);
}

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

.live-panel-header h2 {
  font-family: var(--font-title);
  font-size: 1.15rem;
  font-weight: 600;
}

.live-badge {
  background-color: var(--color-danger);
  color: var(--text-primary);
  padding: 3px 8px;
  border-radius: 50px;
  font-size: 0.65rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  animation: pulse-glow-red 2s infinite;
}

.live-list {
  flex-grow: 1;
  overflow-y: auto;
  padding: 15px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.placeholder-text {
  font-size: 0.85rem;
  color: var(--text-secondary);
  text-align: center;
  margin-top: 50px;
}

/* Ficha del Animal en la Lista */
.animal-card {
  background-color: rgba(255, 255, 255, 0.02);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 12px;
  cursor: pointer;
  transition: var(--transition-smooth);
}

.animal-card:hover {
  background-color: rgba(255, 255, 255, 0.05);
  border-color: rgba(255, 255, 255, 0.2);
}

/* Bordes Dinámicos de Alerta */
.animal-card.NORMAL { border-left: 4px solid var(--color-safe); }
.animal-card.INFRACCION_ROTACION { border-left: 4px solid var(--color-warning); }
.animal-card.ESCAPE_HATO { 
  border-left: 4px solid var(--color-danger); 
  animation: pulse-border-red 1.5s infinite;
}

.animal-card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.animal-arete {
  font-family: var(--font-title);
  font-weight: 700;
  font-size: 0.95rem;
}

.animal-status-tag {
  font-size: 0.65rem;
  font-weight: 600;
  padding: 2px 6px;
  border-radius: 4px;
}

.animal-card.NORMAL .animal-status-tag {
  background-color: rgba(16, 185, 129, 0.15);
  color: var(--color-safe);
}
.animal-card.INFRACCION_ROTACION .animal-status-tag {
  background-color: rgba(245, 158, 11, 0.15);
  color: var(--color-warning);
}
.animal-card.ESCAPE_HATO .animal-status-tag {
  background-color: rgba(239, 68, 68, 0.2);
  color: var(--color-danger);
}

.animal-card-body {
  font-size: 0.75rem;
  color: var(--text-secondary);
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.animal-card-footer {
  margin-top: 8px;
  border-top: 1px dashed var(--border-color);
  padding-top: 8px;
  display: flex;
  justify-content: space-between;
  font-size: 0.7rem;
  color: var(--text-secondary);
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 3px;
}

/* ==========================================================================
   6. VENTANA MODAL (Proyecciones)
   ========================================================================== */
.modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background-color: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 2000;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
}

.modal.active {
  opacity: 1;
  pointer-events: auto;
}

.modal-content {
  background-color: var(--bg-secondary);
  border: 1px solid var(--border-color);
  width: 90%;
  max-width: 800px;
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
  transform: scale(0.9);
  transition: transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.modal.active .modal-content {
  transform: scale(1);
}

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

.modal-header h2 {
  font-family: var(--font-title);
  font-size: 1.35rem;
  font-weight: 600;
}

.close-modal-btn {
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 1.75rem;
  cursor: pointer;
  line-height: 1;
  transition: var(--transition-smooth);
}

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

.modal-body {
  padding: 20px;
  max-height: 80vh;
  overflow-y: auto;
}

/* Gráfico */
.chart-container {
  position: relative;
  width: 100%;
  height: 250px;
  margin-bottom: 20px;
  background-color: rgba(0, 0, 0, 0.2);
  border-radius: var(--radius-md);
  padding: 10px;
  border: 1px solid var(--border-color);
}

/* Tarjetas de Resumen */
.projection-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-bottom: 20px;
}

.proj-card {
  background-color: rgba(255, 255, 255, 0.02);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 15px;
  text-align: center;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.proj-card-title {
  font-size: 0.75rem;
  color: var(--text-secondary);
  margin-bottom: 5px;
}

.proj-card-value {
  font-size: 1.25rem;
  font-weight: 700;
  font-family: var(--font-title);
}

.proj-card.highlight-rentable {
  background-color: rgba(16, 185, 129, 0.08);
  border-color: var(--color-safe);
}
.proj-card.highlight-rentable .proj-card-value {
  color: var(--color-safe);
}

.proj-card.highlight-loss {
  background-color: rgba(239, 68, 68, 0.08);
  border-color: var(--color-danger);
}
.proj-card.highlight-loss .proj-card-value {
  color: var(--color-danger);
}

/* Tabla Desglose */
.table-responsive {
  width: 100%;
  overflow-x: auto;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-color);
}

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

.projection-table th, .projection-table td {
  padding: 12px 15px;
  border-bottom: 1px solid var(--border-color);
}

.projection-table th {
  background-color: rgba(255, 255, 255, 0.02);
  font-weight: 600;
  color: var(--text-secondary);
}

.projection-table tbody tr:hover {
  background-color: rgba(255, 255, 255, 0.01);
}

.badge-rentable {
  background-color: rgba(16, 185, 129, 0.15);
  color: var(--color-safe);
  padding: 2px 6px;
  border-radius: 4px;
  font-weight: 600;
}

.badge-loss {
  background-color: rgba(239, 68, 68, 0.15);
  color: var(--color-danger);
  padding: 2px 6px;
  border-radius: 4px;
  font-weight: 600;
}

/* ==========================================================================
   7. ANIMACIONES CLAVE
   ========================================================================== */
@keyframes pulse-glow-red {
  0% {
    box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.7);
  }
  70% {
    box-shadow: 0 0 0 8px rgba(239, 68, 68, 0);
  }
  100% {
    box-shadow: 0 0 0 0 rgba(239, 68, 68, 0);
  }
}

@keyframes pulse-border-red {
  0% {
    border-left-color: var(--color-danger);
  }
  50% {
    border-left-color: rgba(239, 68, 68, 0.15);
  }
  100% {
    border-left-color: var(--color-danger);
  }
}

/* ==========================================================================
   8. RESPONSIVIDAD (Móviles y Tablets)
   ========================================================================== */
@media (max-width: 1024px) {
  .app-container {
    flex-direction: column;
  }
  
  .sidebar {
    width: 100% !important;
    height: auto;
    border-right: none;
    border-bottom: 1px solid var(--border-color);
  }
  
  .sidebar-header {
    padding: 15px;
  }

  .sidebar-menu {
    flex-direction: row;
    padding: 10px;
    overflow-x: auto;
  }
  
  .menu-item {
    padding: 8px 12px;
    font-size: 0.85rem;
    white-space: nowrap;
  }
  
  .menu-item.active {
    border-left: none;
    border-bottom: 2px solid var(--color-accent);
    padding-left: 12px;
  }
  
  .sidebar-content {
    display: none; /* Se abrirá bajo demanda o se colapsa */
    padding: 15px;
    border-top: none;
    max-height: 300px;
  }
  
  .sidebar-content.active {
    display: block;
  }

  .map-viewport {
    height: 50vh;
    flex-grow: 1;
  }
  
  .live-panel {
    width: 100%;
    height: 40vh;
    border-left: none;
    border-top: 1px solid var(--border-color);
  }
}

/* ==========================================================================
   LISTADO DE GEOCERCAS EN SIDEBAR
   ========================================================================== */
.geofence-list-container {
  margin-top: 20px;
}

.geofence-list-container h4 {
  font-size: 0.85rem;
  color: var(--text-secondary);
  margin-bottom: 10px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.geofences-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 200px;
  overflow-y: auto;
  padding-right: 5px;
}

.geofence-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 12px;
  background-color: rgba(255, 255, 255, 0.02);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  font-size: 0.85rem;
  transition: var(--transition-smooth);
}

.geofence-item:hover {
  background-color: rgba(255, 255, 255, 0.05);
}

.geofence-info {
  display: flex;
  align-items: center;
  gap: 8px;
}

.geofence-type-badge {
  font-size: 0.65rem;
  font-weight: 700;
  padding: 2px 6px;
  border-radius: 4px;
  text-transform: uppercase;
}

.geofence-type-badge.hato {
  background-color: rgba(239, 68, 68, 0.15);
  color: var(--color-danger);
}

.geofence-type-badge.potrero {
  background-color: rgba(245, 158, 11, 0.15);
  color: var(--color-warning);
}

.geofence-name {
  font-weight: 500;
  color: var(--text-primary);
}

.delete-fence-btn {
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 4px;
  border-radius: var(--radius-sm);
  transition: var(--transition-smooth);
  font-size: 0.9rem;
}

.delete-fence-btn:hover {
  background-color: rgba(239, 68, 68, 0.15);
  color: var(--color-danger);
}

/* Spinner de Carga animado para IA */
.spinner {
  width: 30px;
  height: 30px;
  border: 3px solid rgba(255, 255, 255, 0.1);
  border-radius: 50%;
  border-top-color: var(--color-accent);
  animation: spin 1s ease infinite;
  margin: 0 auto;
}

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

