:root,
[data-theme="light"] {
  --bg-body: #f1f5f9;
  --bg-card: #ffffff;
  --bg-sidebar: #0f172a;
  --bg-sidebar-hover: #1e293b;
  --text-primary: #0f172a;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --topbar-bg: #ffffff;
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.08);
}

[data-theme="dark"] {
  --bg-body: #0b1220;
  --bg-card: #111827;
  --bg-sidebar: #020617;
  --bg-sidebar-hover: #1e293b;
  --text-primary: #f1f5f9;
  --text-muted: #94a3b8;
  --border: #1e293b;
  --accent: #3b82f6;
  --accent-hover: #60a5fa;
  --topbar-bg: #111827;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg-body);
  color: var(--text-primary);
}

.app-body {
  min-height: 100vh;
}

.app-wrapper {
  display: flex;
  min-height: 100vh;
}

/* Sidebar */
.sidebar {
  width: 260px;
  background: var(--bg-sidebar);
  color: #e2e8f0;
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  z-index: 1040;
  transition: transform 0.25s ease;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 1.25rem 1rem;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}

.sidebar-brand strong {
  display: block;
  font-size: 1rem;
  color: #fff;
}

.sidebar-brand small {
  color: #94a3b8;
  font-size: 0.7rem;
}

.brand-icon {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: linear-gradient(135deg, #2563eb, #1e40af);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 1.1rem;
}

.brand-icon.lg {
  width: 56px;
  height: 56px;
  font-size: 1.5rem;
  margin: 0 auto;
}

.login-logo-img {
  display: block;
  max-width: 140px;
  height: auto;
  margin: 0 auto 0.25rem;
  border-radius: 12px;
}

.sidebar-nav {
  flex: 1;
  padding: 1rem 0.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

.sidebar-nav .nav-link {
  color: #94a3b8;
  text-decoration: none;
  padding: 0.65rem 1rem;
  border-radius: 8px;
  display: flex;
  align-items: center;
  gap: 0.65rem;
  font-size: 0.9rem;
  transition: background 0.15s, color 0.15s;
}

.sidebar-nav .nav-link:hover {
  background: var(--bg-sidebar-hover);
  color: #f1f5f9;
}

.sidebar-nav .nav-link.active {
  background: rgba(37, 99, 235, 0.25);
  color: #fff;
}

.sidebar-footer {
  padding: 1rem;
  border-top: 1px solid rgba(255, 255, 255, 0.08);
}

.main-panel {
  flex: 1;
  margin-left: 260px;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.topbar {
  background: var(--topbar-bg);
  border-bottom: 1px solid var(--border);
  padding: 0.75rem 1.5rem;
  display: flex;
  align-items: center;
  gap: 1rem;
  position: sticky;
  top: 0;
  z-index: 1020;
  box-shadow: var(--shadow);
}

.topbar-title {
  flex: 1;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.content-area {
  padding: 1.5rem;
  flex: 1;
}

.btn-icon {
  width: 38px;
  height: 38px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--border);
  background: var(--bg-card);
  color: var(--text-primary);
  border-radius: 8px;
}

.btn-user {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  border: 1px solid var(--border);
  background: var(--bg-card);
  color: var(--text-primary);
  border-radius: 8px;
  padding: 0.35rem 0.75rem 0.35rem 0.35rem;
}

.avatar,
.profile-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: linear-gradient(135deg, #2563eb, #1e40af);
  color: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 0.85rem;
}

.profile-avatar {
  width: 72px;
  height: 72px;
  font-size: 1.75rem;
}

.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  color: var(--text-primary);
}

.card-header {
  background: transparent;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  padding: 0.85rem 1rem;
  color: var(--text-primary);
}

.list-group-item {
  background: var(--bg-card);
  color: var(--text-primary);
  border-color: var(--border);
}

.table {
  color: var(--text-primary);
  --bs-table-bg: transparent;
  --bs-table-hover-bg: rgba(37, 99, 235, 0.05);
}

.form-control,
.form-select {
  background: var(--bg-card);
  border-color: var(--border);
  color: var(--text-primary);
}

.form-control:focus,
.form-select:focus {
  background: var(--bg-card);
  border-color: var(--accent);
  color: var(--text-primary);
  box-shadow: 0 0 0 0.2rem rgba(37, 99, 235, 0.15);
}

/* Stats */
.stat-card .stat-label {
  font-size: 0.8rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.stat-value {
  font-size: 1.75rem;
  font-weight: 700;
}

.stat-change {
  font-size: 0.8rem;
  color: #16a34a;
}

.stat-icon {
  width: 44px;
  height: 44px;
  border-radius: 10px;
  background: rgba(37, 99, 235, 0.12);
  color: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.25rem;
}

.action-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

/* Badges */
.badge.tc-passed { background: #dcfce7; color: #166534; }
.badge.tc-running { background: #dbeafe; color: #1e40af; }
.badge.tc-pending { background: #f1f5f9; color: #475569; }
.badge.tc-failed { background: #fee2e2; color: #991b1b; }

.badge.db-active { background: #dcfce7; color: #166534; }
.badge.db-draft { background: #fef3c7; color: #92400e; }
.badge.db-pending { background: #dbeafe; color: #1e40af; }
.badge.db-archived,
.badge.db-inactive { background: #f1f5f9; color: #64748b; }

.badge.status-resuelto { background: #dcfce7; color: #166534; }
.badge.status-investigando { background: #fef3c7; color: #92400e; }
.badge.status-monitoreo { background: #dbeafe; color: #1e40af; }

.sortable {
  cursor: pointer;
  user-select: none;
}

.sortable:hover {
  color: var(--accent);
}

/* Login */
.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(145deg, #0f172a 0%, #1e3a5f 50%, #0f172a 100%);
  padding: 1rem;
}

.login-card {
  width: 100%;
  max-width: 400px;
  background: #fff;
  border-radius: 16px;
  padding: 2rem;
  box-shadow: 0 25px 50px rgba(0, 0, 0, 0.25);
}

.login-badge {
  display: inline-block;
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: #2563eb;
  font-weight: 600;
  margin-bottom: 0.25rem;
}

.login-title {
  font-weight: 700;
  color: #0f172a;
}

.login-hint code {
  font-size: 0.75rem;
}

/* Advanced */
.alert-advanced {
  background: rgba(37, 99, 235, 0.08);
  border: 1px solid rgba(37, 99, 235, 0.2);
  color: var(--text-primary);
}

.advanced-card {
  border-left: 3px solid var(--accent);
}

.advanced-icon {
  font-size: 1.5rem;
  color: var(--accent);
}

.log-panel {
  font-family: ui-monospace, monospace;
  font-size: 0.78rem;
  padding: 1rem;
  max-height: 220px;
  overflow-y: auto;
  background: #f8fafc;
}

[data-theme="dark"] .log-panel {
  background: #020617;
}

.log-panel-dark {
  background: #0f172a !important;
  color: #e2e8f0;
}

.log-line {
  padding: 0.2rem 0;
  border-bottom: 1px solid var(--border);
}

.log-time {
  color: var(--text-muted);
  margin-right: 0.5rem;
}

.log-info .log-level { color: #2563eb; }
.log-warn .log-level { color: #d97706; }

.pref-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
}

.emergency-panel {
  border-left: 4px solid #dc2626;
}

.emergency-stat-icon {
  font-size: 2rem;
  color: var(--accent);
}

.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.5rem 0.75rem;
  border-radius: 8px;
  font-size: 0.85rem;
  font-weight: 500;
}

.status-ok {
  background: #dcfce7;
  color: #166534;
}

.status-warn {
  background: #fef3c7;
  color: #92400e;
}

.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 1035;
}

.sidebar-backdrop.show {
  display: block;
}

@media (max-width: 991.98px) {
  .sidebar {
    transform: translateX(-100%);
  }

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

  .main-panel {
    margin-left: 0;
  }
}

[data-theme="dark"] .dropdown-menu {
  background: var(--bg-card);
  border-color: var(--border);
}

[data-theme="dark"] .dropdown-item {
  color: var(--text-primary);
}

[data-theme="dark"] .dropdown-item:hover {
  background: var(--bg-sidebar-hover);
}

[data-theme="dark"] .login-page .login-card {
  background: var(--bg-card);
}

[data-theme="dark"] .login-title {
  color: var(--text-primary);
}

/* Tabla customers — responsive y legibilidad */
.customers-table-hint {
  background: rgba(37, 99, 235, 0.06);
  border-bottom: 1px solid var(--border);
  padding-top: 0.5rem;
  padding-bottom: 0.5rem;
}

.customers-table-wrap {
  max-height: min(75vh, 900px);
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  border-radius: 0 0 12px 12px;
}

.customers-data-table {
  font-size: 0.8125rem;
  border-collapse: separate;
  border-spacing: 0;
  table-layout: auto;
  width: max-content;
  min-width: 100%;
}

.customers-data-table thead th {
  position: sticky;
  top: 0;
  z-index: 4;
  background: #1e3a5f;
  color: #f8fafc;
  font-weight: 600;
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.02em;
  padding: 0.5rem 0.5rem;
  vertical-align: bottom;
  border-bottom: 2px solid #0f172a;
  white-space: normal;
  line-height: 1.25;
  max-width: 11rem;
}

.customers-data-table thead th.th-sortable {
  cursor: pointer;
}

.customers-data-table thead th.th-sortable a.th-sort-link {
  color: inherit;
  text-decoration: none;
  display: flex;
  align-items: flex-end;
  justify-content: inherit;
  gap: 0.2rem;
  width: 100%;
  line-height: 1.25;
  text-align: inherit;
}

.customers-data-table thead th.col-align-num.th-sortable a.th-sort-link,
.customers-data-table thead th.col-align-date.th-sortable a.th-sort-link,
.customers-data-table thead th.col-align-flag.th-sortable a.th-sort-link {
  justify-content: flex-end;
}

.customers-data-table thead th.col-align-text.th-sortable a.th-sort-link {
  justify-content: flex-start;
}

.customers-data-table thead th.th-sortable a.th-sort-link:hover {
  color: #93c5fd;
}

.customers-data-table thead th.th-sorted a.th-sort-link {
  color: #bfdbfe;
}

.customers-data-table thead th .th-sort-icon {
  flex: 0 0 auto;
  font-size: 0.65rem;
  opacity: 0.45;
  line-height: 1;
}

.customers-data-table thead th.th-sorted .th-sort-icon {
  opacity: 1;
  color: #fde68a;
}

.customers-data-table thead th .th-label {
  display: block;
  white-space: normal;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}

.customers-data-table tbody td {
  padding: 0.45rem 0.5rem;
  vertical-align: top;
  border-bottom: 1px solid var(--border);
  background: var(--bg-card);
  max-width: 14rem;
}

.customers-data-table tbody td .cell-value {
  display: block;
  width: 100%;
  text-align: inherit;
  white-space: normal;
  word-wrap: break-word;
  overflow-wrap: anywhere;
  line-height: 1.35;
}

/* Alineación cabecera = alineación datos */
.customers-data-table th.col-align-num,
.customers-data-table td.col-align-num {
  text-align: right;
}

.customers-data-table th.col-align-text,
.customers-data-table td.col-align-text {
  text-align: left;
}

.customers-data-table th.col-align-date,
.customers-data-table td.col-align-date {
  text-align: center;
}

.customers-data-table th.col-align-flag,
.customers-data-table td.col-align-flag {
  text-align: center;
}

.customers-data-table tbody tr:nth-child(even) td:not(.col-sticky-start):not(.col-sticky-end) {
  background: rgba(15, 23, 42, 0.02);
}

.customers-data-table .col-sticky-start {
  position: sticky;
  left: 0;
  z-index: 2;
  min-width: 4.25rem;
  font-weight: 600;
  box-shadow: 4px 0 8px -2px rgba(15, 23, 42, 0.12);
}

.customers-data-table .col-sticky-end {
  position: sticky;
  right: 0;
  z-index: 2;
  min-width: 5.5rem;
  box-shadow: -4px 0 8px -2px rgba(15, 23, 42, 0.12);
}

.customers-data-table thead .col-sticky-start,
.customers-data-table thead .col-sticky-end {
  z-index: 5;
  background: #1e3a5f;
}

.customers-data-table .col-money,
.customers-data-table td.col-align-num {
  font-variant-numeric: tabular-nums;
}

/* Anchos mínimos por columna (estilo hoja de cálculo, contenido visible) */
.customers-data-table .col-id_Lote { min-width: 3.5rem; max-width: 5rem; }
.customers-data-table .col-Numero_Bultos,
.customers-data-table .col-Numero_Unidades { min-width: 3.5rem; max-width: 5rem; }
.customers-data-table .col-Albaran_Venta { min-width: 5.5rem; max-width: 9rem; }
.customers-data-table .col-SA { min-width: 4.5rem; max-width: 8rem; }
.customers-data-table .col-Cliente { min-width: 7rem; max-width: 14rem; }
.customers-data-table .col-Persona_Recogida { min-width: 6.5rem; max-width: 12rem; }
.customers-data-table .col-Fecha_Salida_Mercancia,
.customers-data-table .col-Fecha { min-width: 5.5rem; max-width: 7rem; }
.customers-data-table .col-Coste,
.customers-data-table .col-Factura,
.customers-data-table .col-Kilos,
.customers-data-table .col-Descuento,
.customers-data-table .col-Pendiente_Kilos,
.customers-data-table .col-Total { min-width: 5.5rem; max-width: 8rem; }
.customers-data-table .col-Pagado,
.customers-data-table .col-Documento_Eliminado,
.customers-data-table .col-Documento_Eliminado_David,
.customers-data-table .col-Pendiente,
.customers-data-table .col-check_kilos { min-width: 3.5rem; max-width: 5rem; }
.customers-data-table .col-Observaciones,
.customers-data-table .col-Nombre_Lote { min-width: 9rem; max-width: 22rem; }
.customers-data-table .col-sticky-end { min-width: 5.5rem; max-width: none; }

.customers-data-table tbody tr.row-pagado > td {
  background-color: #dee2e6 !important;
}

.customers-data-table tbody tr.row-pendiente > td {
  background-color: #ffc9b8 !important;
}

.customers-data-table tbody tr.row-pagado:hover > td,
.customers-data-table tbody tr.row-pendiente:hover > td {
  filter: brightness(0.98);
}

[data-theme="dark"] .customers-data-table thead th {
  background: #0f172a;
  border-bottom-color: #334155;
}

[data-theme="dark"] .customers-data-table tbody tr:nth-child(even) td:not(.col-sticky-start):not(.col-sticky-end) {
  background: rgba(255, 255, 255, 0.02);
}

[data-theme="dark"] .customers-data-table tbody tr.row-pagado > td {
  background-color: #374151 !important;
}

[data-theme="dark"] .customers-data-table tbody tr.row-pendiente > td {
  background-color: #7c3d2e !important;
}

/* Vista tarjetas en pantallas medianas/pequeñas */
@media (max-width: 1199.98px) {
  .customers-data-table {
    min-width: 0;
  }

  .customers-data-table thead {
    display: none;
  }

  .customers-data-table tbody tr.customer-row {
    display: block;
    margin: 0.75rem;
    padding: 0.5rem 0.75rem;
    border: 1px solid var(--border);
    border-radius: 10px;
    box-shadow: var(--shadow);
  }

  .customers-data-table tbody tr.customer-row td {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 0.75rem;
    padding: 0.35rem 0;
    border: none;
    border-bottom: 1px dashed rgba(15, 23, 42, 0.08);
    background: transparent !important;
    position: static;
    box-shadow: none;
    min-width: 0;
    max-width: none;
  }

  .customers-data-table tbody tr.customer-row td:last-child {
    border-bottom: none;
    padding-top: 0.5rem;
    justify-content: flex-end;
  }

  .customers-data-table tbody tr.customer-row td::before {
    content: attr(data-label);
    font-weight: 600;
    font-size: 0.7rem;
    text-transform: uppercase;
    color: var(--text-muted);
    flex: 0 0 42%;
    max-width: 42%;
    line-height: 1.3;
  }

  .customers-data-table tbody tr.customer-row td.col-align-num .cell-value {
    text-align: right;
  }

  .customers-data-table tbody tr.customer-row td.col-align-flag .cell-value {
    text-align: center;
  }

  .customers-data-table tbody tr.customer-row td.col-align-text .cell-value,
  .customers-data-table tbody tr.customer-row td.col-align-date .cell-value {
    text-align: left;
  }

  .customers-data-table tbody tr.customer-row td .cell-value {
    flex: 1 1 55%;
    white-space: normal;
    word-break: break-word;
  }

  .customers-data-table tbody tr.customer-row td.col-sticky-start::before {
    color: #1e40af;
  }

  .customers-data-table tbody tr.customer-row.row-pagado {
    background: #e9ecef;
  }

  .customers-data-table tbody tr.customer-row.row-pendiente {
    background: #ffc9b8;
  }

  .customers-data-table tbody tr.customer-row td.d-none {
    display: none !important;
  }
}

@media (max-width: 767.98px) {
  .customers-table-wrap {
    max-height: none;
  }

  .content-area {
    padding-left: 0.75rem;
    padding-right: 0.75rem;
  }
}

.legend-swatch {
  display: inline-block;
  width: 12px;
  height: 12px;
  border-radius: 2px;
  vertical-align: middle;
}

.legend-pagado {
  background: #dee2e6;
}

.legend-pendiente {
  background: #ffc9b8;
}
