/* ============================================================
   Copiloto Virtual — Panel Administrativo
   ============================================================ */

:root {
  --primary: #1565C0;
  --primary-dark: #003c8f;
  --accent: #FF6F00;
  --success: #2E7D32;
  --danger: #C62828;
  --warning: #F57F17;
  --text: #212121;
  --text-secondary: #757575;
  --bg: #F5F5F5;
  --surface: #FFFFFF;
  --border: #E0E0E0;
  --sidebar-width: 240px;
  --header-height: 60px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: 'Segoe UI', Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  min-height: 100vh;
}

/* ---- Sidebar ---- */
.sidebar {
  width: var(--sidebar-width);
  background: var(--primary-dark);
  color: white;
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0; left: 0;
  height: 100vh;
  z-index: 100;
}

.sidebar-logo {
  padding: 20px 16px;
  font-size: 18px;
  font-weight: 700;
  border-bottom: 1px solid rgba(255,255,255,0.15);
  display: flex;
  align-items: center;
  gap: 10px;
}

.sidebar-logo .logo-icon { font-size: 24px; }

.sidebar-nav { flex: 1; padding: 8px 0; }

.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 20px;
  color: rgba(255,255,255,0.8);
  text-decoration: none;
  font-size: 15px;
  transition: background 0.2s, color 0.2s;
  cursor: pointer;
  border: none;
  background: none;
  width: 100%;
  text-align: left;
}

.nav-item:hover, .nav-item.active {
  background: rgba(255,255,255,0.12);
  color: white;
}

.nav-item .icon { font-size: 18px; width: 24px; text-align: center; }

.sidebar-footer {
  padding: 16px;
  font-size: 12px;
  color: rgba(255,255,255,0.4);
  border-top: 1px solid rgba(255,255,255,0.1);
}

/* ---- Main content ---- */
.main-content {
  margin-left: var(--sidebar-width);
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.topbar {
  background: var(--surface);
  height: var(--header-height);
  padding: 0 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 50;
  box-shadow: 0 1px 4px rgba(0,0,0,0.1);
}

.topbar-title { font-size: 20px; font-weight: 600; }

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

.status-badge {
  padding: 4px 12px;
  border-radius: 20px;
  font-size: 13px;
  font-weight: 500;
}
.status-badge.online { background: #E8F5E9; color: #2E7D32; }
.status-badge.offline { background: #FFEBEE; color: #C62828; }

/* ---- Page content ---- */
.page-content { padding: 24px; flex: 1; }

.page { display: none; }
.page.active { display: block; }

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

.stat-card {
  background: var(--surface);
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.06);
  display: flex;
  align-items: center;
  gap: 16px;
}

.stat-card .stat-icon {
  font-size: 32px;
  width: 56px;
  height: 56px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.stat-card .stat-value { font-size: 28px; font-weight: 700; }
.stat-card .stat-label { font-size: 13px; color: var(--text-secondary); margin-top: 2px; }

.icon-geocercas { background: #E3F2FD; }
.icon-events    { background: #FFF3E0; }
.icon-devices   { background: #E8F5E9; }
.icon-excesos   { background: #FFEBEE; }

/* ---- Tabla ---- */
.card {
  background: var(--surface);
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.06);
  overflow: hidden;
}

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

.card-header h2 { font-size: 16px; font-weight: 600; }

.card-body { padding: 0; }

.table-wrapper { overflow-x: auto; }

table { width: 100%; border-collapse: collapse; }

thead th {
  background: #F5F5F5;
  padding: 12px 16px;
  text-align: left;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

tbody td {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
  vertical-align: middle;
}

tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: #FAFAFA; }

/* ---- Botones ---- */
.btn {
  padding: 8px 16px;
  border-radius: 8px;
  border: none;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: opacity 0.2s, transform 0.1s;
}
.btn:hover { opacity: 0.88; }
.btn:active { transform: scale(0.97); }

.btn-primary { background: var(--primary); color: white; }
.btn-success { background: var(--success); color: white; }
.btn-danger  { background: var(--danger);  color: white; }
.btn-warning { background: var(--warning); color: white; }
.btn-outline {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
}

.btn-sm { padding: 5px 10px; font-size: 13px; }

/* ---- Formulario ---- */
.form-group { margin-bottom: 16px; }

.form-group label {
  display: block;
  margin-bottom: 6px;
  font-size: 14px;
  font-weight: 500;
  color: var(--text);
}

.form-group input, .form-group select, .form-group textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  color: var(--text);
  background: var(--surface);
  transition: border-color 0.2s;
}

.form-group input:focus, .form-group select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(21,101,192,0.12);
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

/* ---- Modal ---- */
.modal-overlay {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.55);
  z-index: 2000;   /* por encima de Leaflet (max ~1000) y sus controles */
  align-items: center;
  justify-content: center;
}
.modal-overlay.open { display: flex; }

.modal {
  background: var(--surface);
  border-radius: 16px;
  padding: 28px;
  width: 520px;
  max-width: 95vw;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.28);
  position: relative;
  z-index: 2001;
}

.modal h2 { font-size: 18px; margin-bottom: 20px; }

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  margin-top: 20px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}

/* ---- Mapa ---- */
#map { height: 480px; border-radius: 0 0 12px 12px; }

/* ---- Barra de confirmación de forma dibujada ---- */
.shape-confirm-bar {
  padding: 12px 20px;
  background: #E8F5E9;
  border-bottom: 2px solid #66BB6A;
  font-size: 14px;
  color: #1B5E20;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

/* ---- Barra de instrucciones de dibujo ---- */
.draw-hint {
  padding: 10px 20px;
  background: #FFF8E1;
  border-bottom: 1px solid #FFE082;
  font-size: 13px;
  color: #5D4037;
  display: flex;
  align-items: center;
  gap: 8px;
}
.draw-hint .hint-icon { font-size: 16px; }

/* Botón activo en modo dibujo */
.btn-draw-active {
  background: var(--accent) !important;
  color: white !important;
  border-color: var(--accent) !important;
}

/* Popup de geocerca en el mapa */
.map-popup-title { font-weight: 700; font-size: 14px; margin-bottom: 4px; }
.map-popup-row   { font-size: 13px; color: #555; margin-bottom: 2px; }
.map-popup-actions { margin-top: 8px; display: flex; gap: 6px; }
.map-popup-actions button {
  padding: 4px 10px;
  border-radius: 6px;
  font-size: 12px;
  cursor: pointer;
  border: none;
}

/* Panel de geocerca dibujada (preview sobre el mapa) */
.draw-preview-badge {
  position: absolute;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0,0,0,0.75);
  color: white;
  padding: 6px 16px;
  border-radius: 20px;
  font-size: 13px;
  z-index: 500;
  pointer-events: none;
  white-space: nowrap;
}

/* ---- Badges ---- */
.badge {
  padding: 3px 8px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 600;
}
.badge-success { background: #E8F5E9; color: #2E7D32; }
.badge-danger  { background: #FFEBEE; color: #C62828; }
.badge-warning { background: #FFF3E0; color: #E65100; }
.badge-info    { background: #E3F2FD; color: #1565C0; }

/* ---- Filtros ---- */
.filters {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  align-items: flex-end;
  padding: 16px 20px;
  background: #FAFAFA;
  border-bottom: 1px solid var(--border);
}
.filters .form-group { margin-bottom: 0; min-width: 160px; }

/* ---- Toast ---- */
.toast-container {
  position: fixed;
  bottom: 24px; right: 24px;
  z-index: 2100;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.toast {
  background: #333;
  color: white;
  padding: 12px 20px;
  border-radius: 8px;
  font-size: 14px;
  animation: fadeInUp 0.3s ease;
  max-width: 320px;
}
.toast.success { background: var(--success); }
.toast.error   { background: var(--danger); }

@keyframes fadeInUp {
  from { opacity: 0; transform: translateY(12px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ---- Notificaciones de eventos ---- */
#event-notif-container {
  position: fixed;
  top: 70px; right: 20px;
  z-index: 3000;
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-width: 340px;
  pointer-events: none;
}
.event-notif {
  background: #fff;
  border-radius: 10px;
  box-shadow: 0 4px 20px rgba(0,0,0,.15), 0 1px 6px rgba(0,0,0,.08);
  border-left: 5px solid #E57373;
  overflow: hidden;
  animation: slideInRight .35s ease;
  pointer-events: all;
}
.event-notif.warning { border-left-color: #FFB74D; }
.event-notif-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px 6px;
  background: #FFF3F3;
  font-size: 12px;
  font-weight: 700;
  color: #B71C1C;
  letter-spacing: .03em;
}
.event-notif.warning .event-notif-header { background: #FFF8E1; color: #E65100; }
.event-notif-close {
  background: none; border: none; cursor: pointer;
  font-size: 16px; color: #999; line-height: 1; padding: 0 2px;
}
.event-notif-body { padding: 8px 12px 10px; }
.event-notif-device { font-weight: 700; font-size: 13px; margin-bottom: 3px; color: #212121; }
.event-notif-row { font-size: 12px; color: #555; margin-bottom: 2px; }
.event-notif-speed {
  display: inline-block; margin-top: 4px;
  background: #E57373; color: #fff;
  border-radius: 4px; padding: 2px 8px;
  font-size: 13px; font-weight: 700;
}
.event-notif.warning .event-notif-speed { background: #FFA726; }
.event-notif-progress {
  height: 3px; background: #FFCDD2;
}
.event-notif-progress-bar {
  height: 100%; background: #E57373;
  animation: shrink linear forwards;
}
.event-notif.warning .event-notif-progress { background: #FFE0B2; }
.event-notif.warning .event-notif-progress-bar { background: #FFA726; }

@keyframes slideInRight {
  from { opacity: 0; transform: translateX(60px); }
  to   { opacity: 1; transform: translateX(0); }
}
@keyframes shrink {
  from { width: 100%; }
  to   { width: 0%; }
}

/* SOS event notification */
.event-notif.sos {
  border-left-color: #EF5350;
  background: #fff;
  color: #212121;
  animation: slideInRight .25s ease, sosPulse .6s ease 0s 3;
}
.sos-header {
  background: #FFEBEE !important;
  color: #C62828 !important;
  font-size: 14px !important;
}
.event-notif.sos .event-notif-device { color: #212121; }
.event-notif.sos .event-notif-row    { color: #555; }
.event-notif.sos .event-notif-progress { background: #FFCDD2; }
.sos-bar { background: #EF5350 !important; }

@keyframes sosPulse {
  0%   { box-shadow: 0 0 0 0 rgba(239,83,80,.6); }
  70%  { box-shadow: 0 0 0 12px rgba(239,83,80,0); }
  100% { box-shadow: 0 0 0 0 rgba(239,83,80,0); }
}

/* Botón mapa en tabla de eventos */
.btn-map-evento {
  background: none;
  border: 1px solid #90CAF9;
  color: #1565C0;
  border-radius: 5px;
  padding: 3px 8px;
  font-size: 12px;
  cursor: pointer;
  white-space: nowrap;
  transition: background .15s, color .15s;
}
.btn-map-evento:hover {
  background: #1565C0;
  color: #fff;
}

/* Modal mapa evento — el div del mapa necesita altura explícita */
#evento-mapa-leaflet {
  min-height: 340px;
}

/* SOS badge in events table */
.badge-sos {
  display: inline-block;
  background: #EF5350; color: #fff;
  border-radius: 5px; padding: 2px 8px;
  font-size: 12px; font-weight: 700;
  letter-spacing: .03em;
}

/* Badge notificaciones en topbar */
#notif-badge {
  display: none;
  background: #C62828; color: #fff;
  border-radius: 50%; width: 20px; height: 20px;
  font-size: 11px; font-weight: 700;
  align-items: center; justify-content: center;
  margin-left: 6px;
}
#notif-badge.visible { display: flex; }

/* ---- Marcadores GPS (estilo GPSWox) ---- */
.gps-marker-wrap {
  display: flex; flex-direction: column; align-items: center;
  white-space: nowrap; cursor: pointer;
}
.gps-label {
  display: flex; align-items: center; gap: 0; margin-bottom: 4px;
  border-radius: 6px; font-family: 'Segoe UI', Arial, sans-serif;
  pointer-events: none; line-height: 1; overflow: hidden;
  box-shadow: 0 3px 10px rgba(0,0,0,.65), 0 1px 4px rgba(0,0,0,.5);
}
.gps-plate {
  font-size: 11px; font-weight: 700; color: #fff;
  letter-spacing: .07em; padding: 4px 7px; background: #0f172a;
}
.gps-sep { font-size: 11px; color: rgba(255,255,255,.25); background: #0f172a; padding: 4px 0; }
.gps-spd { font-size: 11px; font-weight: 700; padding: 4px 7px; letter-spacing: .02em; }
.gps-label.stopped .gps-spd { background: #b71c1c; color: #fff; }
.gps-label.moving  .gps-spd { background: #1b5e20; color: #fff; }
.gps-label.offline .gps-spd { background: #616161; color: #fff; }
.gps-dot {
  width: 18px; height: 18px; border-radius: 50%;
  background: radial-gradient(circle at 38% 38%, #ff5252, #c62828);
  border: 2.5px solid #fff;
  box-shadow: 0 2px 8px rgba(198,40,40,.65), 0 1px 3px rgba(0,0,0,.35);
}
.gps-dot.offline { background: radial-gradient(circle at 38% 38%, #bdbdbd, #757575); box-shadow: 0 2px 8px rgba(0,0,0,.3); }
.gps-arrow { filter: drop-shadow(0 2px 4px rgba(0,0,0,.45)); display: block; }
.gps-tooltip {
  background: rgba(15,23,42,.92) !important; border: 1px solid rgba(255,255,255,.15) !important;
  border-radius: 6px !important; padding: 5px 10px !important;
  box-shadow: 0 3px 10px rgba(0,0,0,.55) !important; white-space: nowrap;
}
.gps-tooltip::before { border-top-color: rgba(15,23,42,.92) !important; }
.gps-tooltip-time { font-size: 12px; font-weight: 600; color: #e2e8f0; letter-spacing: .03em; }

/* ---- Módulo Mapa ---- */
.page-mapa-full {
  padding: 0 !important;
  height: calc(100vh - var(--header-height));
  overflow: hidden;
}

.mapa-layout {
  display: flex;
  height: 100%;
}

.mapa-sidebar {
  width: 300px;
  min-width: 300px;
  background: #fff;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.mapa-sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  background: #fff;
}

.mapa-device-list {
  flex: 1;
  overflow-y: auto;
}

.mapa-device-item {
  padding: 12px 16px;
  border-bottom: 1px solid #f0f0f0;
  cursor: pointer;
  transition: background .15s;
}
.mapa-device-item:hover { background: #f5f8ff; }
.mapa-device-item.offline { opacity: .55; }
.mapa-device-item.active  { background: #E3F2FD; border-left: 3px solid var(--primary); }

.mapa-device-name {
  font-weight: 600;
  font-size: 14px;
  margin-bottom: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.mapa-device-meta {
  font-size: 12px;
  color: var(--text-secondary);
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}
.mapa-device-speed {
  font-weight: 700;
  color: var(--primary);
}
.mapa-device-speed.over { color: var(--danger); }

.tracking-map {
  flex: 1;
  height: 100%;
  z-index: 1;
}

/* Marcador personalizado dispositivo */
.device-marker {
  display: flex;
  flex-direction: column;
  align-items: center;
}
.device-marker-pin {
  width: 36px;
  height: 36px;
  border-radius: 50% 50% 50% 0;
  transform: rotate(-45deg);
  border: 3px solid #fff;
  box-shadow: 0 2px 8px rgba(0,0,0,.35);
  display: flex;
  align-items: center;
  justify-content: center;
}
.device-marker-pin.online  { background: #2E7D32; }
.device-marker-pin.offline { background: #9E9E9E; }
.device-marker-icon {
  transform: rotate(45deg);
  font-size: 16px;
  line-height: 1;
}
.device-marker-label {
  margin-top: 4px;
  background: rgba(0,0,0,.7);
  color: #fff;
  padding: 2px 7px;
  border-radius: 10px;
  font-size: 11px;
  white-space: nowrap;
  max-width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ---- Responsive ---- */
@media (max-width: 768px) {
  .sidebar { transform: translateX(-100%); }
  .main-content { margin-left: 0; }
  .form-row { grid-template-columns: 1fr; }
  .mapa-sidebar { width: 220px; min-width: 220px; }
}
