:root {
  --azul-osc: #0B4870;
  --azul: #1B96C8;
  --azul-claro: #E6F4F9;
  --fondo: #F4F7FA;
  --texto: #1E2A36;
  --muted: #6B7C8E;
  --positivo: #1B9E5A;
  --negativo: #D64545;
  --borde: #E1E8EF;
  --sombra: 0 1px 3px rgba(11,72,112,.08), 0 4px 16px rgba(11,72,112,.06);
  --radius: 14px;
}
* { box-sizing: border-box; }
body {
  margin: 0; font-family: 'Plus Jakarta Sans', system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  background: var(--fondo); color: var(--texto); font-size: 14px;
}
.muted { color: var(--muted); font-weight: 500; }
.hidden { display: none !important; }

/* Header */
.brandbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 24px; background: var(--azul-osc); color: #fff;
  box-shadow: 0 2px 8px rgba(0,0,0,.12);
}
.brand { display: flex; align-items: baseline; gap: 12px; }
.brand .logo { font-weight: 800; font-size: 22px; letter-spacing: 1px; }
.brand .subtitle { font-weight: 500; opacity: .85; }
.actions { display: flex; align-items: center; gap: 10px; }
.currency-badge {
  background: rgba(255,255,255,.15); padding: 4px 10px; border-radius: 20px;
  font-weight: 700; font-size: 12px;
}
.user-chip { color: rgba(255,255,255,.9); font-size: 13px; font-weight: 600; }
.btn {
  background: rgba(255,255,255,.12); color: #fff; border: 1px solid rgba(255,255,255,.25);
  padding: 8px 14px; border-radius: 8px; cursor: pointer; font-weight: 600; font-size: 13px;
  transition: background .15s;
}
.btn:hover { background: rgba(255,255,255,.22); }
.btn.btn-primary { background: var(--azul); border-color: var(--azul); }
.btn.btn-primary:hover { background: #1690b8; }
.btn.sm { padding: 5px 10px; font-size: 12px; }

/* Filtros */
.filters { padding: 14px 24px; background: #fff; border-bottom: 1px solid var(--borde); }
.filter-row { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; margin-bottom: 10px; }
.filter-row:last-child { margin-bottom: 0; }
.filters label { display: flex; flex-direction: column; font-size: 11px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .3px; }
.filters label.grow { flex: 1; min-width: 180px; }
.filters input, .filters select {
  margin-top: 4px; padding: 8px 10px; border: 1px solid var(--borde); border-radius: 8px;
  font-size: 13px; font-family: inherit; background: #fff; color: var(--texto); min-width: 120px;
}
.filters input:focus, .filters select:focus { outline: none; border-color: var(--azul); box-shadow: 0 0 0 3px var(--azul-claro); }
.filters .multi { min-width: 180px; }

/* Multiselect con búsqueda (filtro Grupo) */
.ms { position: relative; min-width: 200px; margin-top: 4px; }
.ms-toggle {
  width: 100%; text-align: left; padding: 8px 28px 8px 10px; border: 1px solid var(--borde);
  border-radius: 8px; background: #fff; font-family: inherit; font-size: 13px; cursor: pointer;
  color: var(--texto); position: relative; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.ms-toggle::after { content: '▾'; position: absolute; right: 10px; top: 50%; transform: translateY(-50%); color: var(--muted); }
.ms-toggle:focus { outline: none; border-color: var(--azul); box-shadow: 0 0 0 3px var(--azul-claro); }
.ms-panel {
  position: absolute; z-index: 60; top: calc(100% + 4px); left: 0; width: min(320px, 92vw);
  background: #fff; border: 1px solid var(--borde); border-radius: 10px; box-shadow: var(--sombra); padding: 8px;
}
.ms-search {
  width: 100%; padding: 7px 9px; border: 1px solid var(--borde); border-radius: 7px;
  font-size: 13px; font-family: inherit; margin-bottom: 6px; min-width: 0;
}
.ms-search:focus { outline: none; border-color: var(--azul); box-shadow: 0 0 0 3px var(--azul-claro); }
.ms-actions { display: flex; gap: 12px; margin-bottom: 6px; padding: 0 2px; }
.ms-link { background: none; border: none; color: var(--azul); font-size: 12px; font-weight: 700; cursor: pointer; padding: 0; text-transform: none; letter-spacing: normal; }
.ms-link:hover { text-decoration: underline; }
.ms-list { max-height: 240px; overflow-y: auto; }
/* `.ms .ms-opt` (especificidad 0,2,0) para ganarle a `.filters label` (0,1,1),
   que impone flex-direction:column + uppercase y descuadraba el checkbox. */
.ms .ms-opt {
  display: flex; flex-direction: row; align-items: center; justify-content: flex-start;
  gap: 8px; padding: 6px 8px; border-radius: 6px; font-size: 13px; cursor: pointer;
  text-transform: none; letter-spacing: normal; color: var(--texto); font-weight: 500;
  text-align: left; width: 100%; margin: 0;
}
.ms .ms-opt:hover { background: var(--azul-claro); }
.ms .ms-opt input { margin: 0; width: auto; min-width: 0; flex: 0 0 auto; }
.ms .ms-opt span { flex: 1; min-width: 0; }
.ms-empty { color: var(--muted); font-size: 12px; padding: 8px 6px; }

/* Autocomplete server-side (Cliente / Artículo) */
.ac { position: relative; margin-top: 4px; }
.ac .ac-input { width: 100%; margin-top: 0; padding-right: 26px; }
.ac-clear {
  position: absolute; right: 8px; top: 50%; transform: translateY(-50%);
  background: none; border: none; color: var(--muted); font-size: 18px; line-height: 1;
  cursor: pointer; padding: 0;
}
.ac-clear:hover { color: var(--negativo); }
.ac-list {
  position: absolute; z-index: 60; top: calc(100% + 4px); left: 0; width: 100%; min-width: 220px;
  background: #fff; border: 1px solid var(--borde); border-radius: 10px; box-shadow: var(--sombra);
  max-height: 260px; overflow-y: auto;
}
.ac-opt {
  display: flex; align-items: baseline; justify-content: space-between; gap: 10px;
  padding: 7px 10px; font-size: 13px; cursor: pointer; color: var(--texto);
}
.ac-opt:hover { background: var(--azul-claro); }
.ac-code { color: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; white-space: nowrap; }
.ac-empty { padding: 8px 10px; color: var(--muted); font-size: 12px; }
.warn {
  margin-top: 8px; padding: 8px 12px; background: #FFF6E6; border: 1px solid #F0C36D;
  border-radius: 8px; color: #8A5A00; font-size: 13px; font-weight: 500;
}

/* KPIs */
.kpis { display: grid; grid-template-columns: repeat(7, 1fr); gap: 12px; padding: 18px 24px; }
.kpi {
  background: #fff; border-radius: var(--radius); padding: 16px; box-shadow: var(--sombra);
  display: flex; flex-direction: column; gap: 6px;
}
.kpi .label { font-size: 11px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .4px; }
.kpi .value { font-size: 22px; font-weight: 800; color: var(--azul-osc); font-variant-numeric: tabular-nums; }

/* Gráficos */
.charts { display: grid; grid-template-columns: repeat(2, 1fr); gap: 14px; padding: 0 24px 18px; }
.card { background: #fff; border-radius: var(--radius); padding: 16px; box-shadow: var(--sombra); }
.card.span-2 { grid-column: span 2; }
.card h3 { margin: 0 0 10px; font-size: 14px; font-weight: 700; color: var(--azul-osc); }
.card h3 small { font-weight: 500; color: var(--muted); font-size: 11px; }

/* Contenedor de altura FIJA para Chart.js (responsive + maintainAspectRatio:false).
   Sin esto el canvas y el .card se realimentan y el gráfico crece sin límite. */
.chart-box { position: relative; height: 280px; width: 100%; }
.card.span-2 .chart-box { height: 320px; }
.chart-box canvas { position: absolute; inset: 0; width: 100% !important; height: 100% !important; }
@media (max-width: 640px) { .chart-box, .card.span-2 .chart-box { height: 240px; } }

/* Detalle */
.detalle { margin: 0 24px 24px; }
.detalle-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; gap: 12px; flex-wrap: wrap; }
.detalle-head h3 { margin: 0; }
.detalle-tools { display: flex; align-items: center; gap: 8px; }
.detalle-tools input { padding: 6px 10px; border: 1px solid var(--borde); border-radius: 8px; font-size: 13px; }
.tabla-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
thead th { text-align: left; padding: 9px 10px; background: var(--azul-claro); color: var(--azul-osc); font-weight: 700; border-bottom: 2px solid var(--borde); white-space: nowrap; }
tbody td { padding: 8px 10px; border-bottom: 1px solid var(--borde); font-variant-numeric: tabular-nums; }
tbody tr:hover { background: var(--azul-claro); }

/* Toast */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--negativo); color: #fff; padding: 12px 20px; border-radius: 10px;
  font-weight: 600; box-shadow: 0 6px 20px rgba(0,0,0,.2); z-index: 1000; max-width: 90vw;
}

/* Responsive */
@media (max-width: 1100px) {
  .kpis { grid-template-columns: repeat(4, 1fr); }
  .charts { grid-template-columns: 1fr; }
  .card.span-2 { grid-column: span 1; }
}
@media (max-width: 640px) {
  .kpis { grid-template-columns: repeat(2, 1fr); }
  .brandbar { flex-wrap: wrap; gap: 8px; }
  .filters { padding: 12px; }
}
