/* WSE Perú · BI Gerencial
   Colores como variables: el modo oscuro solo redefine valores.
   Colores de datos por entidad (siempre los mismos en toda la app):
   dólares = azul, soles = aqua, ventas = violeta, cobranza = naranja,
   servicios = verde, periodo anterior = gris. Validados para daltonismo. */
:root {
  color-scheme: light;
  --page:      #f3f5f9;
  --surface:   #ffffff;
  --surface-2: #f8f9fb;
  --ink:       #0f172a;
  --ink-2:     #475569;
  --muted:     #8a94a6;
  --grid:      #e7eaf0;
  --axis:      #cbd2dc;
  --border:    #e3e7ee;
  --shadow:    0 1px 2px rgba(15, 23, 42, 0.04), 0 4px 16px rgba(15, 23, 42, 0.05);
  --accent:    #2a78d6;
  --good:      #0a7a3a;
  --good-bg:   #e3f5ea;
  --warn:      #9a6100;
  --warn-bg:   #fff3d6;
  --bad:       #c22f2f;
  --bad-bg:    #fde8e8;

  /* barra lateral */
  --side-bg:   #0d1b33;
  --side-ink:  #c9d3e3;
  --side-on:   #ffffff;
  --side-hover: rgba(255, 255, 255, 0.06);
  --side-active: rgba(57, 135, 229, 0.18);

  /* datos */
  --c-usd:       #2a78d6;
  --c-pen:       #1baf7a;
  --c-ventas:    #6250d6;
  --c-cobranza:  #eb6834;
  --c-servicios: #008300;
  --c-base:      #c3c8d2;

  /* tonos de los iconos de indicadores */
  --t-violet: #6250d6; --t-violet-bg: #efedfc;
  --t-orange: #d4561f; --t-orange-bg: #fdeee6;
  --t-blue:   #2a78d6; --t-blue-bg:   #e6f0fc;
  --t-aqua:   #138a60; --t-aqua-bg:   #e2f6ef;
  --t-green:  #0a7a0a; --t-green-bg:  #e4f4e4;
  --t-red:    #c22f2f; --t-red-bg:    #fde8e8;
  --t-slate:  #475569; --t-slate-bg:  #eef1f5;

  --radius: 14px;
  font-family: Inter, system-ui, -apple-system, "Segoe UI", sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --page:      #0b0f17;
    --surface:   #141a24;
    --surface-2: #19202c;
    --ink:       #eef1f6;
    --ink-2:     #b4bdcc;
    --muted:     #7d889a;
    --grid:      #232b38;
    --axis:      #364152;
    --border:    #232b38;
    --shadow:    none;
    --accent:    #5598e7;
    --good:      #3ccf6e;  --good-bg: rgba(60, 207, 110, 0.12);
    --warn:      #f5b942;  --warn-bg: rgba(245, 185, 66, 0.12);
    --bad:       #f07a7a;  --bad-bg:  rgba(240, 122, 122, 0.12);
    --side-bg:   #080c13;
    --c-usd:       #3987e5;
    --c-pen:       #199e70;
    --c-ventas:    #9085e9;
    --c-cobranza:  #d95926;
    --c-servicios: #0ca30c;
    --c-base:      #4a5363;
    --t-violet: #a59cf0; --t-violet-bg: rgba(144, 133, 233, 0.14);
    --t-orange: #f08a5d; --t-orange-bg: rgba(217, 89, 38, 0.16);
    --t-blue:   #6da7ec; --t-blue-bg:   rgba(57, 135, 229, 0.16);
    --t-aqua:   #3cc796; --t-aqua-bg:   rgba(25, 158, 112, 0.16);
    --t-green:  #3ccf6e; --t-green-bg:  rgba(12, 163, 12, 0.16);
    --t-red:    #f07a7a; --t-red-bg:    rgba(240, 122, 122, 0.14);
    --t-slate:  #b4bdcc; --t-slate-bg:  rgba(180, 189, 204, 0.10);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--page); color: var(--ink); font-size: 14px; line-height: 1.5; -webkit-font-smoothing: antialiased; }
h1 { font-size: 1.625rem; font-weight: 700; letter-spacing: -0.02em; margin: 0 0 4px; }
h2 { font-size: 0.9375rem; font-weight: 600; margin: 0 0 2px; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
strong { font-weight: 600; }
.muted { color: var(--ink-2); margin: 0; font-size: 0.8125rem; }

/* ---------- Estructura ---------- */
body.app { display: grid; grid-template-columns: 248px 1fr; min-height: 100vh; }
.side {
  position: sticky; top: 0; height: 100vh;
  display: flex; flex-direction: column; gap: 24px;
  padding: 20px 14px; background: var(--side-bg); color: var(--side-ink);
}
.logo { display: flex; align-items: center; gap: 12px; padding: 4px 8px 12px; border-bottom: 1px solid rgba(255, 255, 255, 0.08); }
.logo-mark {
  display: grid; place-items: center; width: 38px; height: 38px; flex: none;
  border-radius: 10px; background: #2a78d6; color: #fff; font-weight: 700; font-size: 1.125rem;
}
.logo-mark.big { width: 52px; height: 52px; font-size: 1.5rem; border-radius: 14px; }
.logo-text { display: grid; line-height: 1.2; }
.logo-text strong { color: #fff; font-size: 1rem; }
.logo-text small { color: var(--side-ink); font-size: 0.75rem; opacity: 0.8; }

.side-nav { display: grid; gap: 4px; }
.side-nav a {
  display: flex; align-items: center; gap: 12px; padding: 10px 12px; border-radius: 10px;
  color: var(--side-ink); font-weight: 500; text-decoration: none;
}
.side-nav a:hover { background: var(--side-hover); color: var(--side-on); }
.side-nav a.on { background: var(--side-active); color: var(--side-on); box-shadow: inset 3px 0 0 #3987e5; }

.side-foot { margin-top: auto; display: grid; gap: 6px; padding-top: 14px; border-top: 1px solid rgba(255, 255, 255, 0.08); }
.side-foot form { margin: 0; }
.user { display: flex; align-items: center; gap: 10px; padding: 6px 8px 10px; }
.avatar {
  display: grid; place-items: center; width: 32px; height: 32px; border-radius: 50%;
  background: rgba(255, 255, 255, 0.12); color: #fff; font-size: 0.75rem; font-weight: 600;
}
.user-name { color: #fff; font-size: 0.8125rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.side-btn {
  display: flex; align-items: center; gap: 10px; width: 100%; padding: 8px 12px; border: 0; border-radius: 8px;
  background: transparent; color: var(--side-ink); font: inherit; font-size: 0.8125rem; cursor: pointer; text-align: left;
}
.side-btn:hover { background: var(--side-hover); color: var(--side-on); }
.side-btn svg { width: 18px; height: 18px; }

.main { min-width: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 14px 32px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.crumbs { color: var(--muted); font-size: 0.8125rem; }
.chips { display: flex; gap: 8px; flex-wrap: wrap; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; border-radius: 999px;
  background: var(--surface-2); border: 1px solid var(--border); color: var(--ink-2); font-size: 0.75rem; font-weight: 500;
}
.chip svg { width: 14px; height: 14px; }

.page { width: 100%; max-width: 1360px; margin: 0 auto; padding: 28px 32px; display: grid; gap: 20px; }
.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; flex-wrap: wrap; }
.filters label { display: flex; align-items: center; gap: 8px; color: var(--ink-2); font-size: 0.8125rem; font-weight: 500; }
select, input {
  font: inherit; color: var(--ink); background: var(--surface);
  border: 1px solid var(--axis); border-radius: 10px; padding: 8px 12px;
}
select:focus, input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

.foot { padding: 0 32px 28px; max-width: 1360px; margin: 0 auto; width: 100%; color: var(--muted); font-size: 0.75rem; }

/* ---------- Tarjetas ---------- */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 20px 22px; margin: 0; min-width: 0;
}
.card-muted { background: var(--surface-2); }
.card-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
.badge {
  display: inline-grid; place-items: center; min-width: 28px; height: 24px; padding: 0 8px; border-radius: 999px;
  background: var(--t-aqua-bg); color: var(--t-aqua); font-weight: 600; font-size: 0.8125rem;
}

/* ---------- Indicadores ---------- */
.kpis { display: grid; gap: 16px; }
.kpis-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.kpis-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.kpi-stack { display: grid; gap: 16px; }
.kpi-pair { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.kpi {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 18px 20px; min-width: 0;
}
.kpi-head { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }
.kpi-icon { display: grid; place-items: center; width: 36px; height: 36px; border-radius: 10px; flex: none; }
.kpi-label { color: var(--ink-2); font-size: 0.8125rem; font-weight: 500; }
.kpi-value { font-size: 1.75rem; font-weight: 700; letter-spacing: -0.02em; line-height: 1.15; }
.kpi-caption { color: var(--muted); font-size: 0.75rem; margin-top: 2px; }
.kpi-note { color: var(--muted); font-size: 0.75rem; margin-top: 8px; }
.kpi-deltas { margin-top: 10px; font-size: 0.75rem; line-height: 1.9; }
.delta { display: inline-block; padding: 1px 8px; border-radius: 999px; font-weight: 600; }
.delta.good { color: var(--good); background: var(--good-bg); }
.delta.bad  { color: var(--bad);  background: var(--bad-bg); }
.delta-text { color: var(--ink-2); }

.tone-violet .kpi-icon { color: var(--t-violet); background: var(--t-violet-bg); }
.tone-orange .kpi-icon { color: var(--t-orange); background: var(--t-orange-bg); }
.tone-blue   .kpi-icon { color: var(--t-blue);   background: var(--t-blue-bg); }
.tone-aqua   .kpi-icon { color: var(--t-aqua);   background: var(--t-aqua-bg); }
.tone-green  .kpi-icon { color: var(--t-green);  background: var(--t-green-bg); }
.tone-red    .kpi-icon { color: var(--t-red);    background: var(--t-red-bg); }
.tone-slate  .kpi-icon { color: var(--t-slate);  background: var(--t-slate-bg); }

/* Monedas */
.cur-rows { display: flex; gap: 18px; margin-top: 12px; font-variant-numeric: tabular-nums; font-weight: 600; }
.cur-row { display: flex; align-items: center; gap: 6px; }
.cur {
  display: inline-block; padding: 1px 6px; border-radius: 6px; font-size: 0.6875rem; font-weight: 700; letter-spacing: 0.02em;
}
.cur-usd { color: #fff; background: var(--c-usd); }
.cur-pen { color: #fff; background: var(--c-pen); }
.split { display: flex; gap: 2px; height: 8px; margin-top: 12px; border-radius: 999px; overflow: hidden; background: var(--grid); }
.split-usd { background: var(--c-usd); }
.split-pen { background: var(--c-pen); }
.split-legend { display: flex; justify-content: space-between; margin-top: 4px; color: var(--muted); font-size: 0.6875rem; }

/* ---------- Gráficos ---------- */
.grid-2 { display: grid; grid-template-columns: minmax(0, 3fr) minmax(0, 2fr); gap: 20px; }
.grid-2-wide { grid-template-columns: minmax(0, 3fr) minmax(0, 2fr); }
.chart-card figcaption { margin-bottom: 14px; }
.chart { position: relative; }
.chart-tools { display: flex; justify-content: flex-end; margin-top: 10px; }
.chart-tools button {
  font: inherit; font-size: 0.75rem; padding: 4px 10px; border-radius: 8px; cursor: pointer;
  border: 1px solid var(--border); background: transparent; color: var(--ink-2);
}
.chart-tools button:hover { background: var(--surface-2); color: var(--ink); }

/* ---------- Tablas ---------- */
.table-wrap { overflow-x: auto; margin-top: 14px; }
table { width: 100%; border-collapse: collapse; font-size: 0.8125rem; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--grid); white-space: nowrap; }
thead th {
  color: var(--muted); font-weight: 600; font-size: 0.6875rem; text-transform: uppercase; letter-spacing: 0.04em;
  background: var(--surface-2); border-bottom: 1px solid var(--border);
}
thead th:first-child { border-top-left-radius: 8px; }
thead th:last-child { border-top-right-radius: 8px; }
tfoot th { border-top: 2px solid var(--border); border-bottom: 0; font-weight: 700; color: var(--ink); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
td.strong { font-weight: 600; }
.muted-num { color: var(--muted); }
tbody tr:hover { background: var(--surface-2); }
td.ellipsis { max-width: 260px; overflow: hidden; text-overflow: ellipsis; }
.bar-col { width: 120px; }
.cellbar { display: block; height: 8px; border-radius: 999px; background: var(--grid); overflow: hidden; }
.cellbar span { display: block; height: 100%; border-radius: 999px; background: var(--c-ventas); }
.pill {
  display: inline-block; min-width: 40px; padding: 2px 8px; border-radius: 999px; text-align: center;
  font-weight: 600; font-size: 0.75rem; background: var(--surface-2); color: var(--ink-2);
}
.pill-good { color: var(--good); background: var(--good-bg); }
.pill-warn { color: var(--warn); background: var(--warn-bg); }
.pill-bad  { color: var(--bad);  background: var(--bad-bg); }

/* ---------- Aviso ---------- */
.callout {
  display: flex; gap: 14px; align-items: flex-start; padding: 16px 20px; border-radius: var(--radius);
  background: var(--warn-bg); border: 1px solid color-mix(in srgb, var(--warn) 25%, transparent);
}
.callout-icon { color: var(--warn); margin-top: 2px; }
.callout p { margin: 4px 0 0; color: var(--ink-2); }

/* ---------- Login y error ---------- */
body.auth {
  min-height: 100vh; display: grid; place-items: center; padding: 16px;
  background: var(--side-bg);
  background-image: radial-gradient(circle at 20% 15%, rgba(42, 120, 214, 0.35), transparent 45%),
                    radial-gradient(circle at 85% 90%, rgba(27, 175, 122, 0.22), transparent 40%);
}
.auth-page { width: 100%; max-width: 420px; }
.auth-card { background: var(--surface); border-radius: 18px; padding: 32px; box-shadow: 0 20px 60px rgba(0, 0, 0, 0.35); }
.auth-brand { display: flex; align-items: center; gap: 14px; margin-bottom: 20px; }
.auth-brand h1 { margin: 0; font-size: 1.375rem; }
.auth-brand p { margin: 0; color: var(--muted); font-size: 0.8125rem; }
.auth-card form { display: grid; gap: 14px; margin-top: 18px; }
.auth-card label { display: grid; gap: 6px; color: var(--ink-2); font-size: 0.8125rem; font-weight: 500; }
.auth-foot { margin: 20px 0 0; color: var(--muted); font-size: 0.75rem; text-align: center; }
.btn-primary {
  display: inline-block; padding: 10px 16px; border: 0; border-radius: 10px; cursor: pointer;
  background: var(--accent); color: #fff; font: inherit; font-weight: 600; text-align: center;
}
.btn-primary:hover { filter: brightness(1.08); text-decoration: none; }
.alert { display: flex; gap: 8px; align-items: center; color: var(--bad); background: var(--bad-bg); padding: 10px 12px; border-radius: 10px; font-weight: 500; }
.error-card { max-width: 560px; margin: 40px auto; text-align: center; }
.error-icon { display: inline-grid; place-items: center; width: 48px; height: 48px; border-radius: 50%; color: var(--bad); background: var(--bad-bg); margin-bottom: 12px; }
.detalle { white-space: pre-wrap; text-align: left; color: var(--ink-2); font-size: 0.75rem; background: var(--surface-2); padding: 12px; border-radius: 8px; }

/* ---------- Responsive ---------- */
@media (max-width: 1180px) {
  .kpis-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .kpis-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 980px) {
  body.app { grid-template-columns: 1fr; }
  .side { position: sticky; top: 0; z-index: 10; height: auto; flex-direction: row; align-items: center; gap: 12px; padding: 10px 16px; }
  .logo { border: 0; padding: 0; }
  .logo-text small { display: none; }
  .side-nav { display: flex; gap: 2px; flex: 1; }
  .side-nav a { padding: 8px 10px; }
  .side-nav a.on { box-shadow: none; }
  .side-foot { margin: 0; padding: 0; border: 0; display: flex; }
  .user, .side-btn span { display: none; }
  .grid-2, .grid-2-wide { grid-template-columns: 1fr; }
  .topbar { padding: 10px 16px; }
  .page { padding: 20px 16px; }
  .foot { padding: 0 16px 24px; }
}
@media (max-width: 640px) {
  .kpis-3, .kpis-4, .kpi-pair { grid-template-columns: 1fr; }
  .side-nav a span { display: none; }
  .logo-text { display: none; }
  .kpi-value { font-size: 1.5rem; }
}
