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

:root {
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --success: #16a34a;
  --warning: #d97706;
  --danger: #dc2626;
  --gray-50: #f9fafb;
  --gray-100: #f3f4f6;
  --gray-200: #e5e7eb;
  --gray-400: #9ca3af;
  --gray-600: #4b5563;
  --gray-800: #1f2937;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,.12), 0 1px 2px rgba(0,0,0,.08);
  --shadow-md: 0 4px 6px -1px rgba(0,0,0,.1), 0 2px 4px -1px rgba(0,0,0,.06);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--gray-50);
  color: var(--gray-800);
  min-height: 100vh;
  overflow-x: hidden;
}

/* NAV */
nav {
  background: var(--primary);
  color: #fff;
  padding: 0 1.5rem;
  display: flex;
  align-items: center;
  gap: 2rem;
  height: 56px;
  box-shadow: var(--shadow-md);
  position: sticky;
  top: 0;
  z-index: 100;
}
nav .brand { font-weight: 700; font-size: 1.1rem; white-space: nowrap; text-decoration: none; background: none !important; padding: 0 !important; }
nav a {
  color: rgba(255,255,255,.85);
  text-decoration: none;
  font-size: .9rem;
  padding: .3rem .6rem;
  border-radius: 4px;
  transition: background .15s;
}
nav a:hover, nav a.active { background: rgba(255,255,255,.15); color: #fff; }

/* LAYOUT */
.page { max-width: 1400px; margin: 0 auto; padding: 1.5rem; }
.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1.5rem;
  flex-wrap: wrap;
  gap: .75rem;
}
.page-title { font-size: 1.4rem; font-weight: 700; }

/* CARDS */
.card {
  background: #fff;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.25rem;
}
.card-header {
  font-weight: 600;
  margin-bottom: 1rem;
  font-size: 1rem;
  color: var(--gray-800);
}

/* GRID STATS */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 1rem;
  margin-bottom: 1.5rem;
}
.stat-card {
  background: #fff;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.1rem 1.25rem;
  border-left: 4px solid var(--primary);
}
.stat-card.green { border-color: var(--success); }
.stat-card.yellow { border-color: var(--warning); }
.stat-card.red { border-color: var(--danger); }
.stat-label { font-size: .78rem; color: var(--gray-600); margin-bottom: .3rem; }
.stat-value { font-size: 1.8rem; font-weight: 700; }

/* BUTTONS */
.btn {
  display: inline-flex;
  align-items: center;
  gap: .4rem;
  padding: .5rem 1rem;
  border-radius: var(--radius);
  font-size: .875rem;
  font-weight: 500;
  cursor: pointer;
  border: none;
  transition: filter .15s, opacity .15s;
  text-decoration: none;
  white-space: nowrap;
}
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn:not(:disabled):hover { filter: brightness(.92); }
.btn-primary { background: var(--primary); color: #fff; }
.btn-success { background: var(--success); color: #fff; }
.btn-danger  { background: var(--danger); color: #fff; }
.btn-warning { background: var(--warning); color: #fff; }
.btn-ghost   { background: transparent; color: var(--gray-600); border: 1px solid var(--gray-200); }
.btn-sm { padding: .3rem .65rem; font-size: .8rem; }

/* FORMS */
.form-group { margin-bottom: 1rem; }
.form-group label { display: block; font-size: .85rem; font-weight: 500; margin-bottom: .35rem; }
.form-control {
  width: 100%;
  padding: .5rem .75rem;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius);
  font-size: .9rem;
  outline: none;
  transition: border-color .15s;
}
.form-control:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37,99,235,.15); }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
.form-row-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: .75rem; }

/* TABLE */
.table-wrapper { overflow-x: auto; width: 100%; }
table { width: 100%; border-collapse: collapse; font-size: .875rem; }
table.fixed-layout { table-layout: fixed; }
th { background: var(--gray-50); text-align: left; padding: .65rem .9rem; font-weight: 600; font-size: .8rem; text-transform: uppercase; letter-spacing: .03em; color: var(--gray-600); border-bottom: 1px solid var(--gray-200); }
td { padding: .65rem .9rem; border-bottom: 1px solid var(--gray-100); vertical-align: middle; }
tr:last-child td { border-bottom: none; }
tr:hover td { background: var(--gray-50); }

/* BADGES */
.badge {
  display: inline-block;
  padding: .2rem .55rem;
  border-radius: 9999px;
  font-size: .72rem;
  font-weight: 600;
}
.badge-green  { background: #dcfce7; color: #15803d; }
.badge-yellow { background: #fef9c3; color: #a16207; }
.badge-red    { background: #fee2e2; color: #b91c1c; }
.badge-gray   { background: var(--gray-100); color: var(--gray-600); }
.badge-blue   { background: #dbeafe; color: #1d4ed8; }
.badge-purple { background: #ede9fe; color: #6d28d9; }

/* SEARCH BAR */
.search-bar {
  display: flex;
  gap: .75rem;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}
.search-bar .form-control { max-width: 320px; }

/* ALERTS */
.alert {
  padding: .75rem 1rem;
  border-radius: var(--radius);
  font-size: .875rem;
  margin-bottom: 1rem;
  display: flex;
  align-items: center;
  gap: .5rem;
}
.alert-warning { background: #fef3c7; color: #92400e; border: 1px solid #fde68a; }
.alert-danger  { background: #fee2e2; color: #991b1b; border: 1px solid #fecaca; }
.alert-success { background: #dcfce7; color: #166534; border: 1px solid #bbf7d0; }
.alert-info    { background: #dbeafe; color: #1e40af; border: 1px solid #bfdbfe; }

/* MODAL */
.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.45);
  z-index: 1000;
  display: flex; align-items: center; justify-content: center;
  padding: 1rem;
}
.modal-backdrop.hidden { display: none; }
.modal {
  background: #fff;
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  width: 100%;
  max-width: 560px;
  max-height: 90vh;
  overflow-y: auto;
}
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--gray-200);
  font-weight: 600;
}
.modal-body { padding: 1.25rem; }
.modal-footer {
  padding: 1rem 1.25rem;
  border-top: 1px solid var(--gray-200);
  display: flex; justify-content: flex-end; gap: .75rem;
}
.btn-close { background: none; border: none; font-size: 1.3rem; cursor: pointer; color: var(--gray-400); line-height: 1; }
.btn-close:hover { color: var(--gray-800); }

/* HORARIO GRID */
.horario-grid { display: grid; gap: .6rem; }
.horario-row {
  display: grid;
  grid-template-columns: 90px 1fr 1fr 1fr;
  align-items: center;
  gap: .5rem;
  font-size: .85rem;
}
.horario-row label { font-weight: 500; }
.horario-row input[type=time] {
  padding: .3rem .5rem;
  border: 1px solid var(--gray-200);
  border-radius: 4px;
  font-size: .85rem;
}
.horario-row input[type=checkbox] { width: 16px; height: 16px; cursor: pointer; }

/* CLIENTE SELECTOR */
.cliente-selector { max-height: 380px; overflow-y: auto; border: 1px solid var(--gray-200); border-radius: var(--radius); }
.cliente-item {
  display: flex; align-items: center; gap: .75rem;
  padding: .7rem 1rem;
  border-bottom: 1px solid var(--gray-100);
  cursor: pointer;
  transition: background .1s;
}
.cliente-item:last-child { border-bottom: none; }
.cliente-item:hover { background: var(--gray-50); }
.cliente-item.selected { background: #eff6ff; }
.cliente-item input[type=checkbox] { width: 16px; height: 16px; flex-shrink: 0; }
.cliente-item-info { flex: 1; min-width: 0; }
.cliente-item-nombre { font-weight: 500; font-size: .875rem; }
.cliente-item-dir { font-size: .78rem; color: var(--gray-600); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cliente-item-horario { font-size: .75rem; color: var(--primary); white-space: nowrap; }
.cliente-item-warn { font-size: .75rem; color: var(--warning); }

/* RUTA PANEL */
.ruta-layout { display: grid; grid-template-columns: 360px 1fr; gap: 1rem; height: calc(100vh - 100px); }
.ruta-sidebar { overflow-y: auto; }
.ruta-map { border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }
.parada-item {
  display: flex; gap: .75rem; align-items: flex-start;
  padding: .8rem 1rem;
  border-bottom: 1px solid var(--gray-100);
  cursor: pointer;
  transition: background .1s;
}
.parada-item:hover { background: var(--gray-50); }
.parada-item.visitado { opacity: .6; }
.parada-num {
  width: 28px; height: 28px; border-radius: 50%;
  background: var(--primary); color: #fff;
  font-size: .8rem; font-weight: 700;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.parada-num.visitado-num { background: var(--success); }
.parada-num.no-entregado-num { background: var(--danger); }
.parada-info { flex: 1; min-width: 0; }
.parada-nombre { font-weight: 600; font-size: .875rem; }
.parada-dir { font-size: .78rem; color: var(--gray-600); }
.parada-meta { display: flex; gap: .75rem; margin-top: .25rem; flex-wrap: wrap; }
.parada-eta { font-size: .78rem; color: var(--primary); font-weight: 500; }
.parada-ventana { font-size: .78rem; color: var(--gray-600); }
.parada-actions { display: flex; gap: .4rem; flex-wrap: wrap; margin-top: .4rem; }

/* EMPTY STATE */
.empty-state { text-align: center; padding: 3rem 1rem; color: var(--gray-400); }
.empty-state-icon { font-size: 3rem; margin-bottom: .75rem; }
.empty-state p { font-size: .9rem; }

/* LOADING */
.spinner {
  width: 20px; height: 20px;
  border: 2px solid var(--gray-200);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin .7s linear infinite;
  display: inline-block;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* UPLOAD ZONE */
.upload-zone {
  border: 2px dashed var(--gray-200);
  border-radius: var(--radius);
  padding: 2rem;
  text-align: center;
  cursor: pointer;
  transition: border-color .2s, background .2s;
}
.upload-zone:hover, .upload-zone.dragover {
  border-color: var(--primary);
  background: #eff6ff;
}
.upload-zone p { color: var(--gray-600); font-size: .875rem; margin-top: .5rem; }

/* HORARIO COMPACTO (tabla de clientes) */
.horario-compacto {
  display: flex;
  flex-direction: column;
  gap: 2px;
  cursor: default;
}
.horario-chips-fila {
  display: flex;
  flex-wrap: wrap;
  gap: 2px;
}
.horario-chip {
  background: #dbeafe;
  color: #1d4ed8;
  font-size: .68rem;
  font-weight: 700;
  padding: .1rem .3rem;
  border-radius: 3px;
  white-space: nowrap;
}
.horario-hora-fila {
  font-size: .73rem;
  color: #374151;
  display: flex;
  flex-wrap: wrap;
  gap: 3px;
  line-height: 1.3;
}
.horario-hora-item {
  white-space: nowrap;
  background: #f3f4f6;
  border-radius: 3px;
  padding: .05rem .25rem;
}
.horario-sin-hora {
  color: var(--gray-400);
  font-style: italic;
}

/* BULK TOOLBAR */
.bulk-toolbar {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: .6rem 1rem;
  background: #eff6ff;
  border: 1px solid #bfdbfe;
  border-radius: var(--radius);
  margin-bottom: .75rem;
  font-size: .875rem;
}
.bulk-toolbar .bulk-count { font-weight: 600; color: #1d4ed8; }

/* ── NAV HAMBURGER ─────────────────────────────────────────────────────────── */
.nav-toggle {
  display: none;
  background: none;
  border: none;
  color: #fff;
  cursor: pointer;
  padding: .4rem;
  margin-left: auto;
  flex-direction: column;
  gap: 5px;
}
.nav-toggle span {
  display: block;
  width: 22px;
  height: 2px;
  background: #fff;
  border-radius: 2px;
  transition: all .25s;
}
.nav-links { display: contents; }

/* ── RESPONSIVE — tablet ───────────────────────────────────────────────────── */
@media (max-width: 768px) {
  .ruta-layout { grid-template-columns: 1fr; height: auto; }
  .ruta-map { height: 320px; }
  .form-row, .form-row-3 { grid-template-columns: 1fr; }
  .horario-row { grid-template-columns: 80px 1fr 1fr auto; }
}

/* ── RESPONSIVE — mobile ───────────────────────────────────────────────────── */
@media (max-width: 600px) {
  /* Nav hamburger */
  nav {
    flex-wrap: wrap;
    height: auto;
    padding: .6rem 1rem;
    gap: 0;
    position: relative;
  }
  nav .brand { flex: 1; }
  .nav-toggle { display: flex; }
  .nav-links {
    display: none;
    flex-direction: column;
    width: 100%;
    padding: .5rem 0 .25rem;
    gap: .15rem;
    contents: unset;
  }
  .nav-links.open { display: flex; }
  .nav-links a {
    padding: .55rem .5rem;
    font-size: .9rem;
    border-radius: 6px;
  }
  #nav-user {
    display: block;
    padding: .4rem .5rem;
    font-size: .78rem;
    opacity: .7;
    border-top: 1px solid rgba(255,255,255,.15);
    margin-top: .25rem;
    width: 100%;
  }

  /* Page */
  .page { padding: .65rem .75rem; }
  .page-header { margin-bottom: 1rem; }
  .page-title { font-size: 1.15rem; }

  /* Cards */
  .card { padding: .85rem .9rem; }

  /* Forms */
  .form-row, .form-row-3 { grid-template-columns: 1fr; }
  .horario-row { grid-template-columns: 75px 1fr 1fr auto; gap: .3rem; font-size: .8rem; }

  /* Modals — bottom sheet */
  .modal-backdrop {
    align-items: flex-end;
    padding: 0;
  }
  .modal {
    max-width: 100%;
    max-height: 92vh;
    border-radius: 14px 14px 0 0;
  }

  /* Tables */
  .table-wrapper { -webkit-overflow-scrolling: touch; }
  th, td { padding: .5rem .6rem; font-size: .8rem; }

  /* Search bar */
  .search-bar { gap: .5rem; }
  .search-bar .form-control { max-width: 100%; }

  /* Bulk toolbar */
  .bulk-toolbar { flex-wrap: wrap; gap: .5rem; padding: .5rem .75rem; font-size: .82rem; }

  /* Ruta layout (ver-ruta) */
  .ruta-layout { grid-template-columns: 1fr; height: auto; }
  .ruta-map { height: 280px; }

  /* Stats grid */
  .stats-grid { grid-template-columns: 1fr 1fr; gap: .6rem; }
  .stat-value { font-size: 1.4rem; }
}

/* Chat FAB flotante */
#fab-chat {
  position: fixed; bottom: 22px; right: 22px;
  width: 52px; height: 52px; border-radius: 50%;
  background: #2563eb; color: #fff; border: none;
  font-size: 1.35rem; cursor: pointer;
  z-index: 998; box-shadow: 0 4px 14px rgba(37,99,235,.45);
  display: flex; align-items: center; justify-content: center;
  transition: background .15s, transform .15s;
}
#fab-chat:hover { background: #1d4ed8; transform: scale(1.07); }
#fab-chat #chat-badge {
  position: absolute; top: -3px; right: -3px;
  background: #ef4444; color: #fff; border-radius: 10px;
  padding: 1px 5px; font-size: .7rem; font-weight: 700;
  min-width: 18px; text-align: center; line-height: 16px;
  pointer-events: none;
}

/* Chat panel */
#chat-panel {
  position: fixed; right: 0; top: 0; height: 100vh; width: 340px;
  background: #fff; box-shadow: -4px 0 16px rgba(0,0,0,.15);
  z-index: 1000; display: flex; flex-direction: column;
  transform: translateX(100%); transition: transform .25s ease;
}
#chat-panel.open { transform: translateX(0); }
#chat-panel-header {
  padding: 12px 16px; background: #1e40af; color: #fff;
  display: flex; justify-content: space-between; align-items: center; font-weight: 600;
}
#chat-panel-body { flex: 1; overflow-y: auto; padding: 8px; }
@media (max-width: 600px) { #chat-panel { width: 100vw; } }
