:root {
  --surface-1: #fffcf9;
  --surface-2: #f6efe6;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #898781;
  --gridline: #e6ddcf;
  --baseline: #cabfa9;
  --border: rgba(122,22,38,0.14);

  --series-1: #2a78d6; /* blue */
  --series-2: #1baf7a; /* aqua */
  --series-3: #eda100; /* yellow */
  --series-4: #008300; /* green */
  --series-5: #4a3aa7; /* violet */
  --series-6: #e34948; /* red */
  --series-7: #e87ba4; /* magenta */
  --series-8: #eb6834; /* orange */

  --seq-100: #cde2fb; --seq-250: #86b6ef; --seq-450: #2a78d6; --seq-650: #104281;

  --good: #0ca30c;
  --warning: #fab219;
  --serious: #ec835a;
  --critical: #d03b3b;

  --brand-wine: #7a1626;
}

@media (prefers-color-scheme: dark) {
  :root {
    --surface-1: #1a1a19;
    --surface-2: #0d0d0d;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #898781;
    --gridline: #2c2c2a;
    --baseline: #383835;
    --border: rgba(255,255,255,0.10);

    --series-1: #3987e5;
    --series-2: #199e70;
    --series-3: #c98500;
    --series-4: #008300;
    --series-5: #9085e9;
    --series-6: #e66767;
    --series-7: #d55181;
    --series-8: #d95926;

    --good: #0ca30c;
    --warning: #fab219;
    --serious: #ec835a;
    --critical: #d03b3b;

    --brand-wine: #9c2438;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--surface-2);
  color: var(--text-primary);
  min-height: 100vh;
}

#app { min-height: 100vh; }
.hidden { display: none !important; }

/* ---------- Login ---------- */
.login-screen {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
}
.login-card {
  background: var(--surface-1); border: 1px solid var(--border); border-radius: 12px;
  padding: 32px; width: 320px; box-shadow: 0 1px 3px rgba(0,0,0,0.08);
}
.login-card h1 { font-size: 18px; margin: 0 0 4px; }
.login-card p { color: var(--text-secondary); font-size: 13px; margin: 0 0 20px; }
.login-card input {
  width: 100%; padding: 10px 12px; margin-bottom: 10px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text-primary); font-size: 14px;
}
.login-card button {
  width: 100%; padding: 10px; border-radius: 8px; border: none; background: var(--brand-wine);
  color: white; font-weight: 600; font-size: 14px; cursor: pointer; margin-top: 6px;
}
.login-error { color: var(--critical); font-size: 13px; margin-top: 8px; min-height: 16px; }
.login-logo { display: block; height: 46px; margin: 0 auto 18px; }

/* ---------- Shell ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 24px; background: var(--surface-1); border-bottom: 2px solid var(--brand-wine);
  position: sticky; top: 0; z-index: 10; flex-wrap: wrap; gap: 12px;
}
.topbar .brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 15px; }
.topbar .brand img { height: 34px; display: block; }
.topbar .brand span { color: var(--text-secondary); font-weight: 400; }
.period-picker { display: flex; gap: 8px; align-items: center; }
.period-picker select, .period-picker input {
  padding: 6px 10px; border-radius: 6px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text-primary); font-size: 13px;
}
.topbar .user-box { display: flex; align-items: center; gap: 10px; font-size: 13px; color: var(--text-secondary); }
.topbar .user-box button { background: none; border: 1px solid var(--border); border-radius: 6px; padding: 6px 10px; color: var(--text-primary); cursor: pointer; font-size: 12px; }

.tabs { display: flex; gap: 4px; padding: 0 24px; background: var(--surface-1); border-bottom: 1px solid var(--border); overflow-x: auto; }
.tab-btn {
  padding: 10px 14px; border: none; background: none; color: var(--text-secondary);
  font-size: 13px; cursor: pointer; border-bottom: 2px solid transparent; white-space: nowrap;
}
.tab-btn.active { color: var(--brand-wine); border-bottom-color: var(--brand-wine); font-weight: 600; }

main { max-width: 1180px; margin: 0 auto; padding: 24px; }
.view { display: none; }
.view.active { display: block; }

/* ---------- KPI tiles ---------- */
.kpi-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; margin-bottom: 20px; }
.kpi-tile {
  background: var(--surface-1); border: 1px solid var(--border); border-radius: 10px; padding: 14px 16px;
}
.kpi-tile .label { font-size: 12px; color: var(--text-muted); margin-bottom: 6px; }
.kpi-tile .value { font-size: 20px; font-weight: 700; font-variant-numeric: tabular-nums; }
.kpi-tile .sub { font-size: 12px; color: var(--text-secondary); margin-top: 4px; }
.status-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; }
.status-good { background: var(--good); }
.status-warning { background: var(--warning); }
.status-serious { background: var(--serious); }
.status-critical { background: var(--critical); }

/* ---------- Alerts ---------- */
.alerts-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 10px; margin-bottom: 20px; }
.alert-card { background: var(--surface-1); border: 1px solid var(--border); border-radius: 10px; padding: 12px 14px; font-size: 13px; display: flex; align-items: center; gap: 8px; }
.alert-card .txt { color: var(--text-secondary); }
.alert-card .txt b { color: var(--text-primary); }

/* ---------- Card / section ---------- */
.card {
  background: var(--surface-1); border: 1px solid var(--border); border-top: 3px solid var(--brand-wine);
  border-radius: 10px; padding: 18px 20px; margin-bottom: 20px; box-shadow: 0 1px 2px rgba(122,22,38,0.05);
}
.card h2 {
  font-size: 13px; margin: 0 0 14px; color: var(--brand-wine); font-weight: 700;
  text-transform: uppercase; letter-spacing: .03em;
}
.hint { font-size: 12px; color: var(--text-muted); font-weight: 400; }
.card h2 .hint { text-transform: none; letter-spacing: normal; }

/* ---------- Waterfall table ---------- */
table.eerr { width: 100%; border-collapse: collapse; font-size: 13px; }
table.eerr td { padding: 6px 8px; border-bottom: 1px solid var(--gridline); }
table.eerr td.concepto { color: var(--text-secondary); }
table.eerr tr.total td { font-weight: 700; color: var(--text-primary); border-top: 1px solid var(--baseline); border-bottom: 1px solid var(--baseline); }
table.eerr tr.total.net td { color: var(--series-1); }
table.eerr td.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
table.eerr td.pct { text-align: right; color: var(--text-muted); width: 60px; }

/* ---------- Generic data table ---------- */
table.datatbl { width: 100%; border-collapse: collapse; font-size: 12.5px; }
table.datatbl th { text-align: left; font-weight: 600; color: var(--text-muted); font-size: 11px; text-transform: uppercase; letter-spacing: .02em; padding: 6px 8px; border-bottom: 1px solid var(--baseline); }
table.datatbl td { padding: 6px 8px; border-bottom: 1px solid var(--gridline); font-variant-numeric: tabular-nums; }
table.datatbl tr:hover td { background: var(--surface-2); }
table.datatbl .num { text-align: right; }
table.datatbl .actions { white-space: nowrap; }
table.datatbl .actions button { border: none; background: none; cursor: pointer; color: var(--text-muted); font-size: 12px; padding: 2px 4px; }
table.datatbl .actions button:hover { color: var(--critical); }
table.datatbl tr.pending-row td { background: color-mix(in srgb, var(--warning) 12%, transparent); }
table.datatbl tr.pending-row td:first-child { font-weight: 600; box-shadow: inset 3px 0 0 var(--warning); }
table.datatbl tr:not(.pending-row) td:first-child { box-shadow: inset 3px 0 0 var(--good); }
table.datatbl td .status-dot { margin-right: 8px; vertical-align: middle; }

.progress-badge {
  display: flex; align-items: center; gap: 6px; font-size: 12.5px; font-weight: 600; color: var(--text-secondary);
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 999px; padding: 5px 12px;
}

/* ---------- Charts (Chart.js) ---------- */
.legend { display: flex; gap: 16px; flex-wrap: wrap; margin-bottom: 10px; font-size: 12px; color: var(--text-secondary); }
.legend .item { display: flex; align-items: center; gap: 6px; }
.legend .swatch { width: 10px; height: 10px; border-radius: 2px; }
.chart-wrap { position: relative; height: 280px; }
.doughnut-wrap { position: relative; height: 240px; max-width: 280px; margin: 0 auto 12px; }

/* ---------- Forms (carga de datos) ---------- */
.subtabs { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 16px; }
.subtab-btn { padding: 6px 12px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface-1); color: var(--text-secondary); font-size: 12.5px; cursor: pointer; }
.subtab-btn.active { background: var(--brand-wine); color: white; border-color: var(--brand-wine); }

.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 10px; margin-bottom: 12px; align-items: end; }
.field label { display: block; font-size: 11px; color: var(--text-muted); margin-bottom: 4px; }
.field input, .field select {
  width: 100%; padding: 7px 9px; border-radius: 6px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text-primary); font-size: 13px;
}
.btn { padding: 8px 14px; border-radius: 6px; border: none; background: var(--brand-wine); color: white; font-size: 13px; font-weight: 600; cursor: pointer; }
.btn:hover { filter: brightness(1.08); }
.btn.secondary { background: var(--surface-2); color: var(--text-primary); border: 1px solid var(--border); }
.btn.small { padding: 5px 10px; font-size: 12px; }
.form-actions { display: flex; gap: 8px; }
.empty-msg { color: var(--text-muted); font-size: 13px; padding: 20px 0; text-align: center; }
.table-scroll { overflow-x: auto; }
.bonif-line { padding-bottom: 10px; margin-bottom: 10px; border-bottom: 1px dashed var(--border); }
.bonif-line:last-child { border-bottom: none; margin-bottom: 0; padding-bottom: 0; }

/* ---------- Barra de búsqueda/filtro (Carga de Datos > Costos) ---------- */
.costos-filtro-bar { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 16px; }
.search-wrap {
  flex: 2 1 260px; display: flex; align-items: center; gap: 8px;
  background: var(--surface-1); border: 1px solid var(--border); border-radius: 999px; padding: 9px 16px;
}
.search-icon { color: var(--text-muted); font-size: 14px; }
.search-input { flex: 1; border: none; background: transparent; outline: none; font-size: 13.5px; color: var(--text-primary); }
.search-input::placeholder { color: var(--text-muted); }
.filter-select {
  flex: 1 1 180px; padding: 9px 14px; border-radius: 999px; border: 1px solid var(--border);
  background: var(--surface-1); color: var(--text-secondary); font-size: 13px;
}

/* ---------- Badges de categoría ---------- */
.badge {
  display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: 11.5px; font-weight: 600;
  border: 1px solid transparent; white-space: nowrap;
}
.badge-muted { background: var(--surface-2); color: var(--text-muted); border-color: var(--border); }

/* ---------- Analítica: sidebar de filtros + tarjetas BI ---------- */
.analitica-layout { display: flex; gap: 20px; align-items: flex-start; }
.analitica-sidebar {
  width: 220px; flex-shrink: 0; background: var(--surface-1); border: 1px solid var(--border);
  border-radius: 12px; padding: 16px;
}
.analitica-main { flex: 1; min-width: 0; }
@media (max-width: 860px) {
  .analitica-layout { flex-direction: column; }
  .analitica-sidebar { width: 100%; }
}

.filter-group { margin-bottom: 20px; }
.filter-title {
  font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .03em;
  color: var(--text-muted); margin-bottom: 8px; display: flex; justify-content: space-between; align-items: center;
}
.filter-title span { display: flex; gap: 4px; }
.filter-title .btn.small { padding: 2px 6px; font-size: 10px; }
.filter-list { display: flex; flex-direction: column; gap: 6px; max-height: 220px; overflow-y: auto; }
.filter-check { display: flex; align-items: center; gap: 8px; font-size: 12.5px; color: var(--text-secondary); cursor: pointer; }
.filter-check input { accent-color: var(--brand-wine); }
.filter-group .field { margin-bottom: 8px; }

.bi-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 14px; margin-bottom: 20px; }
.bi-card { border: 1px solid var(--border); border-radius: 10px; overflow: hidden; background: var(--surface-1); }
.bi-card-head {
  background: var(--brand-wine); color: #fff; font-size: 11px; font-weight: 700; text-transform: uppercase;
  letter-spacing: .04em; padding: 8px 14px;
}
.bi-card-body { padding: 14px; }
.bi-card-value { font-size: 20px; font-weight: 700; font-variant-numeric: tabular-nums; color: var(--text-primary); }
.bi-card-sub { font-size: 12px; color: var(--text-secondary); margin-top: 4px; }
