:root {
  --bg: #f3f5f8;
  --surface: #ffffff;
  --surface-alt: #f8fafc;
  --border: #cbd5e1;
  --border-soft: #e2e8f0;
  --text: #0f172a;
  --text-muted: #475569;
  --text-light: #64748b;

  --brand-dark: #14432a;
  --brand: #166534;
  --brand-mid: #15803d;
  --brand-tint: #f0fdf4;
  --brand-border: #86efac;

  --edit-bg: #fff7ed;
  --edit-border: #fdba74;
  --edit-focus: #ea580c;
  --edit-text: #9a3412;

  --blue: #1d4ed8;
  --blue-soft: #eff6ff;
  --amber: #b45309;
  --amber-soft: #fef3c7;
  --danger: #b91c1c;
  --salmon-loss: #f4a285;

  --radius: 8px;
  --radius-sm: 5px;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.05);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: 'Avenir Next', 'Avenir', 'Nunito Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 13.5px;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
}

/* HEADER */
.app-header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.header-top {
  max-width: 1480px;
  margin: 0 auto;
  padding: 8px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  flex-wrap: wrap;
}

.brand-block {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-logo-slot {
  width: 44px;
  height: 34px;
  border-radius: 6px;
  border: 1.5px dashed #94a3b8;
  background: var(--surface-alt);
  color: var(--text-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.06em;
}

.brand-title {
  font-size: 15.5px;
  font-weight: 800;
  color: var(--brand-dark);
  letter-spacing: -0.01em;
}

.brand-subtitle {
  font-size: 11px;
  color: var(--text-light);
  font-weight: 600;
}

.header-controls {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 5px 11px;
  border-radius: 999px;
  font-family: inherit;
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
  border: 1px solid transparent;
}

.status-pill.online {
  background: var(--brand-tint);
  color: var(--brand-dark);
  border-color: var(--brand-border);
}

.status-pill.offline {
  background: var(--amber-soft);
  color: var(--amber);
  border-color: #fcd34d;
}

.dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  display: inline-block;
}
.online .dot { background: #16a34a; }
.offline .dot { background: #d97706; }

.role-switcher {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  padding: 3px 9px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 700;
  color: var(--text-muted);
}

.role-switcher select {
  border: none;
  background: transparent;
  font-weight: 800;
  color: var(--text);
  font-size: 12.5px;
  cursor: pointer;
  padding: 2px 4px;
}

/* TABS */
.tabs-nav {
  background: var(--brand-dark);
}

.tabs-inner {
  max-width: 1480px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  gap: 2px;
  overflow-x: auto;
}

.tab-btn {
  appearance: none;
  background: transparent;
  border: none;
  font-family: inherit;
  color: rgba(255, 255, 255, 0.78);
  padding: 9px 16px;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
  border-bottom: 3px solid transparent;
  white-space: nowrap;
  transition: background 0.12s ease, color 0.12s ease;
}

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

.tab-btn.active {
  color: #ffffff;
  background: rgba(255, 255, 255, 0.14);
  border-bottom-color: #4ade80;
}

/* MAIN WORKSPACE */
.main-container {
  max-width: 1480px;
  margin: 0 auto;
  padding: 16px 20px 32px;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 12px;
}

.section-title {
  margin: 0;
  font-size: 19px;
  font-weight: 800;
  color: var(--text);
}

.action-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  align-items: center;
}

.filter-bar {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 4px 8px;
  border-radius: var(--radius-sm);
  font-size: 12px;
  font-weight: 700;
  color: var(--text-muted);
}

/* BUTTONS */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 7px 13px;
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 12.5px;
  font-weight: 700;
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  transition: background 0.12s ease, border-color 0.12s ease;
}

.btn:hover {
  background: var(--surface-alt);
  border-color: #94a3b8;
}

.btn-primary {
  background: var(--brand);
  border-color: var(--brand-dark);
  color: #ffffff;
}
.btn-primary:hover {
  background: var(--brand-dark);
}

.btn-blue {
  background: var(--blue);
  border-color: #1e40af;
  color: #ffffff;
}
.btn-blue:hover {
  background: #1e40af;
}

.btn-subtle {
  background: var(--surface-alt);
  border-color: var(--border);
  color: var(--text-muted);
}

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

/* CARDS */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  margin-bottom: 12px;
  overflow: hidden;
}

.card-header {
  padding: 8px 14px;
  background: var(--surface-alt);
  border-bottom: 1px solid var(--border-soft);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
}

.card-header.band-green {
  background: #dcfce7;
  border-bottom: 1px solid #86efac;
  color: #14532d;
}

.card-title {
  margin: 0;
  font-size: 13px;
  font-weight: 800;
}

.card-body {
  padding: 12px 14px;
}

.card-body.compact {
  padding: 0;
}

/* GRIDS & LAYOUT */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 12px;
  margin-bottom: 12px;
}

.kpi-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 3px solid var(--brand);
  border-radius: var(--radius);
  padding: 11px 14px;
}

.kpi-card.blue { border-left-color: var(--blue); }
.kpi-card.amber { border-left-color: #d97706; }
.kpi-card.danger { border-left-color: var(--danger); }

.kpi-label {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--text-muted);
}

.kpi-value {
  font-size: 20px;
  font-weight: 800;
  margin-top: 2px;
  font-variant-numeric: tabular-nums;
}

.kpi-sub {
  font-size: 11.5px;
  color: var(--text-light);
  margin-top: 2px;
}

.grid-2 {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.grid-remesa-top {
  display: grid;
  grid-template-columns: minmax(240px, 340px) minmax(0, 1fr);
  gap: 12px;
  align-items: start;
}

.grid-sidebar-main {
  display: grid;
  grid-template-columns: 300px 1fr;
  gap: 14px;
  align-items: start;
}

/* REMESAS: columna lateral izquierda con Viajes + Comprobación apiladas verticalmente
   (se quedan fijas al scrollear). Las tarjetas tienen ancho máximo para que en media
   pantalla sigan pudiendo acomodarse lado a lado. */
.remesas-layout {
  display: grid;
  grid-template-columns: minmax(260px, 340px) minmax(0, 1fr);
  gap: 14px;
  align-items: start;
}
.remesas-lateral {
  position: sticky;
  top: 96px;
  max-height: calc(100vh - 112px);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding-right: 2px;
}
.entity-list-scroll {
  max-height: 240px;
  overflow-y: auto;
}
.remesas-principal {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-width: 0;
}

.sticky-sidebar {
  position: sticky;
  top: 96px;
  max-height: calc(100vh - 112px);
  overflow-y: auto;
}

@media (max-width: 1080px) {
  .grid-2, .grid-remesa-top, .grid-sidebar-main {
    grid-template-columns: 1fr;
  }
  .sticky-sidebar {
    position: static;
    max-height: none;
  }
  /* En media pantalla las tarjetas de remesa se mantienen lado a lado
     (el ancho máximo hace que quepan) y la columna lateral deja de flotar. */
  .remesas-layout {
    grid-template-columns: 1fr;
  }
  .remesas-lateral {
    position: static;
    max-height: none;
    overflow: visible;
    flex-direction: row;
    flex-wrap: wrap;
    align-items: flex-start;
  }
  .remesas-lateral > .card {
    flex: 1 1 280px;
    max-width: 420px;
  }
}

/* TABLES */
.table-wrap {
  width: 100%;
  overflow-x: auto;
}

table.erp-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12.5px;
}

table.erp-table th {
  background: #e2e8f0;
  color: #1e293b;
  font-weight: 800;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  padding: 6px 9px;
  border-bottom: 1.5px solid var(--border);
  text-align: left;
  white-space: nowrap;
}

table.erp-table td {
  padding: 5px 9px;
  border-bottom: 1px solid var(--border-soft);
  vertical-align: middle;
}

table.erp-table tbody tr:hover {
  background: #f8fafc;
}

table.erp-table tfoot td,
table.erp-table tr.total-row td {
  background: #e2e8f0;
  font-weight: 800;
  border-top: 1.5px solid #94a3b8;
  color: var(--text);
}

td.group-pct-cell {
  border-left: 1.5px solid var(--border);
  background: #f8fafc;
  text-align: center;
  vertical-align: middle !important;
  padding: 6px 10px !important;
}

.group-pct-label {
  font-size: 10.5px;
  font-weight: 800;
  text-transform: uppercase;
  color: var(--text-muted);
}

.group-pct-val {
  font-size: 14px;
  font-weight: 800;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

.group-pct-kg {
  font-size: 11px;
  color: var(--text-light);
}

.num {
  text-align: right !important;
  font-variant-numeric: tabular-nums;
}

.center {
  text-align: center !important;
}

.pos-val { color: #15803d; font-weight: 700; }
.neg-val { color: #b91c1c; font-weight: 700; }

/* UTILIDAD SUMMARY STRIP (Destacado en Remesas) */
.util-strip {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 8px;
  margin-bottom: 10px;
}

.util-metric {
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
  text-align: center;
}

.util-metric.highlight {
  border-width: 1.5px;
  border-color: #94a3b8;
  background: #f1f5f9;
}

.util-metric-label {
  font-size: 10.5px;
  font-weight: 800;
  text-transform: uppercase;
  color: var(--text-muted);
}

.util-metric-val {
  font-size: 16.5px;
  font-weight: 800;
  margin-top: 2px;
  font-variant-numeric: tabular-nums;
}

/* INPUTS */
input[type="text"],
input[type="number"],
input[type="date"],
input[type="month"],
select,
textarea {
  padding: 5px 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 12.5px;
  font-family: inherit;
  color: var(--text);
  background: #ffffff;
}

input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--brand-mid);
  border-color: var(--brand-mid);
}

input.cell-input {
  width: 66px;
  padding: 3px 6px;
  border: 1.5px solid var(--edit-border);
  background: var(--edit-bg);
  color: var(--edit-text);
  font-weight: 700;
  text-align: right;
  border-radius: 4px;
  font-variant-numeric: tabular-nums;
  -moz-appearance: textfield;
  appearance: textfield;
}

input.cell-input::-webkit-outer-spin-button,
input.cell-input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

input.cell-input:focus {
  outline: 2px solid var(--edit-focus);
  background: #ffedd5;
}

input.cell-input:disabled {
  background: #e2e8f0;
  border-color: #cbd5e1;
  color: #94a3b8;
  cursor: not-allowed;
}

/* KV & COMPROBACION */
.kv-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12.5px;
}
.kv-table th {
  width: 40%;
  background: #cbd5e1;
  color: #0f172a;
  font-weight: 700;
  padding: 5px 10px;
  border: 1px solid #94a3b8;
  text-align: left;
}
.kv-table td {
  padding: 5px 10px;
  border: 1px solid #cbd5e1;
  font-weight: 600;
  background: #ffffff;
}

.summary-box-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  border: 1.5px solid #0f172a;
  border-radius: 6px;
  overflow: hidden;
}
.summary-box-cell {
  padding: 7px 10px;
  border-bottom: 1px solid #cbd5e1;
  border-right: 1px solid #cbd5e1;
  text-align: center;
}
.summary-box-cell:nth-child(2n) {
  border-right: none;
}
.summary-box-label {
  font-size: 11px;
  color: var(--text-muted);
  font-weight: 700;
}
.summary-box-val {
  font-size: 14.5px;
  font-weight: 800;
  margin-top: 1px;
  font-variant-numeric: tabular-nums;
}
.summary-final-banner {
  grid-column: 1 / -1;
  padding: 8px 12px;
  text-align: center;
}
.summary-final-banner.loss {
  background: var(--salmon-loss);
  color: #431407;
}
.summary-final-banner.gain {
  background: #bbf7d0;
  color: #14532d;
}

/* ENTITY LIST */
.entity-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.entity-item {
  padding: 9px 11px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  cursor: pointer;
}

.entity-item:hover {
  border-color: #94a3b8;
  background: var(--surface-alt);
}

.entity-item.active {
  border-color: var(--brand);
  border-width: 2px;
  background: var(--brand-tint);
}

.badge {
  display: inline-block;
  padding: 2px 7px;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 700;
}
.badge-green { background: #dcfce7; color: #166534; }
.badge-amber { background: #fef3c7; color: #92400e; }
.badge-red { background: #fee2e2; color: #991b1b; }
.badge-blue { background: #dbeafe; color: #1e40af; }
.badge-slate { background: #e2e8f0; color: #334155; }

.subprod-bar {
  padding: 9px 14px;
  background: var(--surface-alt);
  border-top: 1px solid var(--border-soft);
  display: flex;
  gap: 28px;
  flex-wrap: wrap;
  font-size: 12.5px;
}

/* FORMS & DIALOG */
.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 10px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.field label {
  font-size: 11px;
  font-weight: 700;
  color: var(--text-muted);
}

dialog.erp-modal {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0;
  max-width: 640px;
  width: calc(100% - 32px);
  box-shadow: 0 20px 40px rgba(15, 23, 42, 0.22);
}

dialog.erp-modal::backdrop {
  background: rgba(15, 23, 42, 0.4);
}

.modal-head {
  padding: 12px 16px;
  background: var(--surface-alt);
  border-bottom: 1px solid var(--border-soft);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.modal-body { padding: 16px; }
.modal-foot {
  padding: 10px 16px;
  background: var(--surface-alt);
  border-top: 1px solid var(--border-soft);
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.toast-container {
  position: fixed;
  bottom: 18px;
  right: 18px;
  z-index: 999;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.toast {
  background: #0f172a;
  color: #ffffff;
  padding: 9px 14px;
  border-radius: 6px;
  font-size: 12.5px;
  font-weight: 600;
  border-left: 3px solid #4ade80;
}

/* PRINT ISOLATION (Solo activa #print-area cuando se dispara impresión explícita de documento) */
#print-area {
  display: none;
}

@media print {
  body.printing-doc .app-header,
  body.printing-doc .main-container,
  body.printing-doc .toast-container,
  body.printing-doc dialog {
    display: none !important;
  }
  body.printing-doc #print-area {
    display: block !important;
  }
  .print-sheet {
    width: 100%;
    max-width: 195mm;
    margin: 0 auto;
    padding: 8mm;
    font-family: 'Avenir Next', 'Avenir', 'Nunito Sans', Arial, sans-serif;
    color: #000;
  }
  .print-tarima-sheet {
    border: 3px solid #000;
    padding: 14mm;
    text-align: center;
  }
  .print-tarima-calibre {
    font-size: 52pt;
    font-weight: 900;
    margin: 12px 0;
  }
  .print-tarima-boxes {
    font-size: 36pt;
    font-weight: 800;
    border-top: 2px solid #000;
    border-bottom: 2px solid #000;
    padding: 10px 0;
    margin: 14px 0;
  }
  .print-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 12px;
    font-size: 10pt;
  }
  .print-table th, .print-table td {
    border: 1px solid #333;
    padding: 5px 8px;
  }
  .print-table th {
    background: #eaeaea !important;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }
}

/* AUTH OVERLAY */
.auth-overlay {
  position: fixed;
  inset: 0;
  z-index: 2000;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, var(--brand-dark), var(--brand));
  padding: 16px;
}
.auth-overlay[hidden] { display: none; }
.auth-card {
  width: 100%;
  max-width: 360px;
  background: var(--surface);
  border: 1px solid var(--border-soft);
  border-radius: 12px;
  box-shadow: 0 20px 50px rgba(15, 23, 42, 0.35);
  padding: 26px 24px;
}
.auth-card .brand-title { font-size: 19px; }
.auth-card .field { margin-bottom: 12px; }
.auth-error {
  background: var(--salmon-loss);
  color: #431407;
  font-size: 12.5px;
  font-weight: 700;
  padding: 8px 10px;
  border-radius: 6px;
  margin-top: 6px;
}
.auth-error[hidden] { display: none; }
