/* ==========================================
   VARIABLES
   ========================================== */
:root {
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --secondary: #64748b;
  --success: #22c55e;
  --warning: #f59e0b;
  --danger: #ef4444;
  --info: #06b6d4;

  --bg-dark: #1e293b;
  --bg-darker: #0f172a;
  --bg-light: #f8fafc;
  --bg-card: #ffffff;

  --text-primary: #1e293b;
  --text-secondary: #64748b;
  --text-light: #f8fafc;

  --border: #e2e8f0;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  --shadow-lg: 0 10px 15px rgba(0, 0, 0, 0.1);

  --sidebar-width: 260px;
  --header-height: 64px;
}

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

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  background: var(--bg-light);
  color: var(--text-primary);
  line-height: 1.5;
}

a {
  text-decoration: none;
  color: inherit;
}

button {
  cursor: pointer;
  font-family: inherit;
}

/* ==========================================
   LAYOUT
   ========================================== */
.app {
  display: flex;
  min-height: 100vh;
}

/* ==========================================
   SIDEBAR
   ========================================== */
.sidebar {
  width: var(--sidebar-width);
  background: var(--bg-darker);
  color: var(--text-light);
  display: flex;
  flex-direction: column;
  position: fixed;
  height: 100vh;
  z-index: 100;
}

.sidebar-header {
  padding: 20px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  text-align: center;
}

.sidebar-header h1 {
  font-size: 1.1rem;
  font-weight: 700;
  line-height: 1.4;
}

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

.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  border-radius: 8px;
  margin-bottom: 4px;
  transition: all 0.2s;
  color: var(--secondary);
}

.nav-item:hover {
  background: rgba(255, 255, 255, 0.05);
  color: var(--text-light);
}

.nav-item.active {
  background: var(--primary);
  color: var(--text-light);
}

.nav-item .icon {
  font-size: 1.25rem;
}

.nav-item .badge {
  margin-left: auto;
  background: var(--danger);
  color: white;
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 0.75rem;
  font-weight: 600;
}

.sidebar-footer {
  padding: 16px 24px;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
}

.server-status {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.875rem;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--secondary);
}

.status-dot.online {
  background: var(--success);
}

.status-dot.offline {
  background: var(--danger);
}

/* ==========================================
   MAIN CONTENT
   ========================================== */
.main-content {
  flex: 1;
  margin-left: var(--sidebar-width);
  display: flex;
  flex-direction: column;
}

/* ==========================================
   HEADER
   ========================================== */
.header {
  height: var(--header-height);
  background: var(--bg-card);
  border-bottom: 1px solid var(--border);
  padding: 0 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  z-index: 50;
}

.header h2 {
  font-size: 1.25rem;
  font-weight: 600;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 16px;
}

#current-time {
  color: var(--text-secondary);
  font-size: 0.875rem;
}

/* ==========================================
   BUTTONS
   ========================================== */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  border-radius: 8px;
  font-size: 0.875rem;
  font-weight: 500;
  border: none;
  transition: all 0.2s;
}

.btn-primary {
  background: var(--primary);
  color: white;
}

.btn-primary:hover {
  background: var(--primary-dark);
}

.btn-secondary {
  background: var(--bg-light);
  color: var(--text-primary);
  border: 1px solid var(--border);
}

.btn-secondary:hover {
  background: var(--border);
}

.btn-success {
  background: var(--success);
  color: white;
}

.btn-danger {
  background: var(--danger);
  color: white;
}

.btn-sm {
  padding: 6px 12px;
  font-size: 0.75rem;
}

/* ==========================================
   PAGE CONTENT
   ========================================== */
.page-content {
  padding: 24px;
  flex: 1;
}

/* ==========================================
   CARDS
   ========================================== */
.card {
  background: var(--bg-card);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 24px;
  margin-bottom: 24px;
}

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

.card-title {
  font-size: 1rem;
  font-weight: 600;
}

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

.stat-card {
  background: var(--bg-card);
  border-radius: 12px;
  padding: 20px;
  box-shadow: var(--shadow);
}

.stat-card .icon {
  font-size: 2rem;
  margin-bottom: 8px;
}

.stat-card .value {
  font-size: 2rem;
  font-weight: 700;
  color: var(--text-primary);
}

.stat-card .label {
  font-size: 0.875rem;
  color: var(--text-secondary);
}

.stat-card.primary {
  background: var(--primary);
  color: white;
}

.stat-card.primary .value,
.stat-card.primary .label {
  color: white;
}

/* ==========================================
   TABLE
   ========================================== */
.table-container {
  overflow-x: auto;
}

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

th, td {
  padding: 12px 16px;
  text-align: left;
  border-bottom: 1px solid var(--border);
}

th {
  font-weight: 600;
  font-size: 0.75rem;
  text-transform: uppercase;
  color: var(--text-secondary);
  background: var(--bg-light);
}

tr:hover {
  background: var(--bg-light);
}

/* ==========================================
   BADGES / ESTADOS
   ========================================== */
.estado {
  display: inline-flex;
  align-items: center;
  padding: 4px 12px;
  border-radius: 20px;
  font-size: 0.75rem;
  font-weight: 500;
}

.estado-pendiente {
  background: #fef3c7;
  color: #92400e;
}

.estado-recibido {
  background: #dbeafe;
  color: #1e40af;
}

.estado-en-proceso {
  background: #e0e7ff;
  color: #3730a3;
}

.estado-finalizado {
  background: #d1fae5;
  color: #065f46;
}

.estado-entregado {
  background: #f3f4f6;
  color: #374151;
}

.estado-cancelado {
  background: #fee2e2;
  color: #991b1b;
}

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

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

.form-input,
.form-select,
.form-textarea {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.875rem;
  transition: all 0.2s;
}

.form-input:focus,
.form-select:focus,
.form-textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

.form-textarea {
  min-height: 100px;
  resize: vertical;
}

/* ==========================================
   MODAL
   ========================================== */
.modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 200;
}

.modal.active {
  display: flex;
}

.modal-content {
  background: var(--bg-card);
  border-radius: 12px;
  width: 90%;
  max-width: 600px;
  max-height: 90vh;
  overflow: auto;
  box-shadow: var(--shadow-lg);
}

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

.modal-header h3 {
  font-size: 1.125rem;
  font-weight: 600;
}

.modal-close {
  background: none;
  border: none;
  font-size: 1.5rem;
  color: var(--text-secondary);
  cursor: pointer;
}

.modal-body {
  padding: 24px;
}

/* ==========================================
   TOAST NOTIFICATIONS
   ========================================== */
.toast-container {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 300;
}

.toast {
  background: var(--bg-darker);
  color: white;
  padding: 12px 20px;
  border-radius: 8px;
  margin-top: 8px;
  box-shadow: var(--shadow-lg);
  animation: slideIn 0.3s ease;
}

.toast.success {
  background: var(--success);
}

.toast.error {
  background: var(--danger);
}

@keyframes slideIn {
  from {
    transform: translateX(100%);
    opacity: 0;
  }
  to {
    transform: translateX(0);
    opacity: 1;
  }
}

/* ==========================================
   CONVERSACIONES
   ========================================== */
.conversaciones-layout {
  display: grid;
  grid-template-columns: 350px 1fr;
  gap: 24px;
  height: calc(100vh - var(--header-height) - 48px);
}

.conversaciones-lista {
  background: var(--bg-card);
  border-radius: 12px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.conversaciones-lista-header {
  padding: 16px;
  border-bottom: 1px solid var(--border);
}

.conversaciones-lista-content {
  flex: 1;
  overflow-y: auto;
}

.conversacion-item {
  padding: 16px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background 0.2s;
}

.conversacion-item:hover {
  background: var(--bg-light);
}

.conversacion-item.active {
  background: #eff6ff;
  border-left: 3px solid var(--primary);
}

.conversacion-item .nombre {
  font-weight: 600;
  margin-bottom: 4px;
}

.conversacion-item .ultimo-mensaje {
  font-size: 0.875rem;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.conversacion-item .tiempo {
  font-size: 0.75rem;
  color: var(--text-secondary);
  margin-top: 4px;
}

.chat-container {
  background: var(--bg-card);
  border-radius: 12px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

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

.chat-messages {
  flex: 1;
  padding: 16px;
  overflow-y: auto;
  background: var(--bg-light);
}

.mensaje {
  max-width: 70%;
  margin-bottom: 12px;
  padding: 12px 16px;
  border-radius: 12px;
}

.mensaje.cliente {
  background: white;
  border: 1px solid var(--border);
  margin-right: auto;
}

.mensaje.asistente,
.mensaje.bot {
  background: var(--primary);
  color: white;
  margin-left: auto;
}

.mensaje .hora {
  font-size: 0.7rem;
  opacity: 0.7;
  margin-top: 4px;
}

.chat-input {
  padding: 16px;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 12px;
}

.chat-input input {
  flex: 1;
}

/* ==========================================
   EMPTY STATE
   ========================================== */
.empty-state {
  text-align: center;
  padding: 48px;
  color: var(--text-secondary);
}

.empty-state .icon {
  font-size: 4rem;
  margin-bottom: 16px;
}

/* ==========================================
   LOADING
   ========================================== */
.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 48px;
}

.spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

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

/* ==========================================
   MENU TOGGLE (HAMBURGUESA)
   ========================================== */
.menu-toggle {
  display: none;
  background: none;
  border: none;
  font-size: 1.5rem;
  padding: 8px;
  cursor: pointer;
  color: var(--text-primary);
}

/* ==========================================
   SIDEBAR OVERLAY
   ========================================== */
.sidebar-overlay {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 99;
}

.sidebar-overlay.active {
  display: block;
}

/* ==========================================
   RESPONSIVE - TABLET (max 1024px)
   ========================================== */
@media (max-width: 1024px) {
  .conversaciones-layout {
    grid-template-columns: 280px 1fr;
  }

  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

/* ==========================================
   RESPONSIVE - MOVIL (max 768px)
   ========================================== */
@media (max-width: 768px) {
  /* Mostrar boton hamburguesa */
  .menu-toggle {
    display: block;
  }

  /* Sidebar oculto por defecto en movil */
  .sidebar {
    transform: translateX(-100%);
    transition: transform 0.3s ease;
  }

  .sidebar.open {
    transform: translateX(0);
  }

  /* Main content ocupa todo el ancho */
  .main-content {
    margin-left: 0;
  }

  /* Header ajustado */
  .header {
    padding: 0 16px;
    gap: 12px;
  }

  .header h2 {
    font-size: 1rem;
    flex: 1;
  }

  .header-actions {
    gap: 8px;
  }

  #current-time {
    display: none;
  }

  .btn-primary#btn-refresh {
    padding: 8px;
    font-size: 0;
  }

  .btn-primary#btn-refresh::before {
    content: "🔄";
    font-size: 1rem;
  }

  /* Page content */
  .page-content {
    padding: 16px;
  }

  /* Cards */
  .card {
    padding: 16px;
    margin-bottom: 16px;
  }

  .card-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }

  /* Stats grid */
  .stats-grid {
    grid-template-columns: 1fr 1fr;
    gap: 12px;
  }

  .stat-card {
    padding: 16px;
  }

  .stat-card .value {
    font-size: 1.5rem;
  }

  .stat-card .icon {
    font-size: 1.5rem;
  }

  /* Tablas responsive */
  .table-container {
    margin: 0 -16px;
    padding: 0 16px;
  }

  table {
    font-size: 0.8rem;
  }

  th, td {
    padding: 10px 8px;
  }

  /* Botones */
  .btn {
    padding: 10px 14px;
  }

  .btn-sm {
    padding: 8px 10px;
  }

  /* Conversaciones layout */
  .conversaciones-layout {
    grid-template-columns: 1fr;
    height: auto;
  }

  .conversaciones-lista {
    max-height: 300px;
  }

  .chat-container {
    height: 400px;
  }

  .mensaje {
    max-width: 85%;
  }

  /* Modal en movil */
  .modal-content {
    width: 95%;
    max-height: 85vh;
    margin: 16px;
  }

  .modal-body {
    padding: 16px;
  }

  /* Toast en movil */
  .toast-container {
    left: 16px;
    right: 16px;
    bottom: 16px;
  }

  .toast {
    width: 100%;
  }

  /* Tabs de filtro */
  .btn-tab {
    padding: 8px 12px;
    font-size: 12px;
  }

  /* Sidebar footer */
  .sidebar-footer {
    padding: 12px 16px;
  }

  .sidebar-nav {
    padding: 12px;
  }

  .nav-item {
    padding: 14px 16px;
    font-size: 1rem;
  }
}

/* ==========================================
   RESPONSIVE - MOVIL PEQUENO (max 480px)
   ========================================== */
@media (max-width: 480px) {
  .stats-grid {
    grid-template-columns: 1fr;
  }

  .stat-card {
    display: flex;
    align-items: center;
    gap: 16px;
  }

  .stat-card .icon {
    font-size: 2rem;
    margin-bottom: 0;
  }

  .stat-card .value {
    font-size: 1.25rem;
  }

  .login-container {
    padding: 1.5rem;
    margin: 0.5rem;
  }

  .login-icon {
    font-size: 3rem;
  }

  .login-logo h1 {
    font-size: 1.25rem;
  }
}

/* Estilos para Tabs de Filtro de Visitas */
.btn-tab {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  border: 2px solid var(--border);
  background: var(--card);
  color: var(--text-secondary);
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.2s ease;
}

.btn-tab:hover {
  border-color: var(--primary);
  color: var(--primary);
  background: rgba(37, 99, 235, 0.1);
}

.btn-tab.active {
  background: var(--primary);
  border-color: var(--primary);
  color: white;
}

.btn-tab.active .badge-count {
  background: white;
  color: var(--primary);
}

.badge-count {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 24px;
  height: 24px;
  padding: 0 8px;
  background: var(--bg);
  color: var(--text);
  border-radius: 12px;
  font-size: 12px;
  font-weight: 600;
}

.btn-sm {
  padding: 6px 12px;
  font-size: 12px;
}

/* ==========================================
   LOGIN SCREEN
   ========================================== */
.login-screen {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, var(--bg-darker) 0%, var(--bg-dark) 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
}

.login-container {
  background: var(--bg-card);
  padding: 2.5rem;
  border-radius: 16px;
  box-shadow: var(--shadow-lg);
  width: 100%;
  max-width: 400px;
  margin: 1rem;
}

.login-logo {
  text-align: center;
  margin-bottom: 2rem;
}

.login-icon {
  font-size: 4rem;
  display: block;
  margin-bottom: 1rem;
}

.login-logo h1 {
  font-size: 1.5rem;
  color: var(--text-primary);
  margin-bottom: 0.25rem;
}

.login-logo p {
  color: var(--text-secondary);
  font-size: 0.875rem;
}

.login-form .form-group {
  margin-bottom: 1.25rem;
}

.login-form label {
  display: block;
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--text-primary);
  margin-bottom: 0.5rem;
}

.login-form input {
  width: 100%;
  padding: 0.75rem 1rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 1rem;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.login-form input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

.login-error {
  color: var(--danger);
  font-size: 0.875rem;
  margin-bottom: 1rem;
  min-height: 1.25rem;
}

.btn-block {
  width: 100%;
  padding: 0.875rem 1.5rem;
  font-size: 1rem;
}

/* User Info in Sidebar */
.user-info {
  background: rgba(255, 255, 255, 0.1);
  padding: 0.75rem 1rem;
  border-radius: 8px;
  margin-bottom: 0.75rem;
}

.user-name {
  display: block;
  font-weight: 600;
  color: var(--text-light);
  font-size: 0.875rem;
}

.user-rol {
  display: block;
  font-size: 0.75rem;
  color: rgba(255, 255, 255, 0.6);
  text-transform: uppercase;
}

.btn-logout {
  width: 100%;
  padding: 0.5rem 1rem;
  background: rgba(239, 68, 68, 0.2);
  color: #fca5a5;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.875rem;
  margin-bottom: 1rem;
  transition: background 0.2s;
}

.btn-logout:hover {
  background: rgba(239, 68, 68, 0.3);
}
