/* ==========================================================================
   Tokens. Light is the base; dark applies when the OS prefers it (unless the
   user picked light) or when the theme toggle stamps data-theme="dark".
   Chart series colours (--s1..--s8) are a CVD-validated categorical order:
   assign them to entities in that fixed order, never cycled or re-ranked.
   ========================================================================== */
:root {
  /* Escala tipográfica: seis pasos con un trabajo cada uno. 12px es el mínimo de la app; el cuerpo es de 14px. */
  --fs-xs: 12px;   /* etiquetas, metadatos y notas */
  --fs-sm: 13px;   /* botones, celdas y texto secundario */
  --fs-md: 14px;   /* cuerpo y campos */
  --fs-lg: 16px;   /* títulos de página y de sección; campos en el celular */
  --fs-xl: 20px;   /* valor destacado de una tarjeta */
  --fs-2xl: 26px;  /* cifras de los indicadores */
  --lh-texto: 1.45;
  --lh-dato: 1.2;
  --bg: #f3f5f9;
  --panel: #ffffff;
  --panel-2: #eef1f7;
  --border: #dfe4ed;
  --border-strong: #c7cfdc;
  --ink: #0e1a2f;
  --ink-2: #42506a;
  --ink-3: #5d6a80;
  --primary: #1e40af;
  --primary-soft: #e5ebfb;
  --on-primary: #ffffff;
  --brand: #d97706;
  --good: #19804c;
  --good-soft: #e2f3e9;
  --warn: #9a5f06;
  --warn-soft: #fbefd9;
  --bad: #c42f2f;
  --bad-soft: #fbe5e5;
  --cost: #c3ccdb;
  --util: #2a78d6;
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100;
  --s5: #e87ba4; --s6: #008300; --s7: #4a3aa7; --s8: #e34948;
  --shadow: 0 1px 2px rgba(14, 26, 47, .05);
  --sans: "Fira Sans", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --mono: "Fira Code", ui-monospace, "Cascadia Mono", Consolas, monospace;

  /* Older names, still used by detail.html */
  --text: var(--ink);
  --text-dim: var(--ink-3);
  --accent: var(--primary);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0b111e; --panel: #121a2b; --panel-2: #192237; --border: #232d44; --border-strong: #33405c;
    --ink: #e8ecf4; --ink-2: #b2bccf; --ink-3: #8591a8;
    --primary: #7d9cff; --primary-soft: #1c2950; --on-primary: #0b111e; --brand: #f0a030;
    --good: #43c585; --good-soft: #13301f; --warn: #e6a73e; --warn-soft: #33260e; --bad: #f27373; --bad-soft: #3a191c;
    --cost: #36425c; --util: #3987e5;
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
    --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0b111e; --panel: #121a2b; --panel-2: #192237; --border: #232d44; --border-strong: #33405c;
  --ink: #e8ecf4; --ink-2: #b2bccf; --ink-3: #8591a8;
  --primary: #7d9cff; --primary-soft: #1c2950; --on-primary: #0b111e; --brand: #f0a030;
  --good: #43c585; --good-soft: #13301f; --warn: #e6a73e; --warn-soft: #33260e; --bad: #f27373; --bad-soft: #3a191c;
  --cost: #36425c; --util: #3987e5;
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
  --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
  --shadow: none;
}

/* ==========================================================================
   Base
   ========================================================================== */
* { box-sizing: border-box; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 400 var(--fs-md)/var(--lh-texto) var(--sans);
  -webkit-font-smoothing: antialiased;
}
button, select, input { font: inherit; color: inherit; }
a { color: var(--primary); }
:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; border-radius: 4px; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.mono { font-family: var(--mono); font-size: var(--fs-sm); }
.muted { color: var(--ink-3); }
[hidden] { display: none !important; }

input[type="text"], input[type="password"], input[type="search"], input[type="date"], select {
  background: var(--panel); border: 1px solid var(--border-strong); color: var(--ink);
  height: 36px; padding: 0 12px; border-radius: 8px; font-size: var(--fs-md);
}

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 8px; height: 36px; padding: 0 14px;
  border-radius: 8px; border: 1px solid var(--border-strong); background: var(--panel); color: var(--ink);
  font-size: var(--fs-sm); font-weight: 500; cursor: pointer; text-decoration: none;
  transition: background .15s, border-color .15s;
}
.btn:hover { background: var(--panel-2); }
.btn.primary { background: var(--primary); color: var(--on-primary); border-color: var(--primary); }
.btn.primary:hover { filter: brightness(1.08); background: var(--primary); }
.btn.icon { width: 36px; padding: 0; justify-content: center; }
.btn.sm { height: 28px; padding: 0 10px; font-size: var(--fs-xs); }
.btn svg { width: 16px; height: 16px; flex: none; }
.btn.busy svg { animation: spin .8s linear infinite; }
.btn:disabled { cursor: progress; opacity: .8; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ==========================================================================
   App bar (dashboard .appbar and detail header.topbar share it)
   ========================================================================== */
.appbar, header.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap;
  padding: 12px 24px; background: var(--panel); border-bottom: 1px solid var(--border);
}
header.topbar h1 { font-size: var(--fs-lg); margin: 0; font-weight: 600; }
header.topbar .sub { color: var(--ink-3); font-size: var(--fs-sm); }
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; text-decoration: none; color: inherit; }
.brand-mark {
  width: 32px; height: 32px; border-radius: 8px; display: grid; place-items: center; flex: none;
  background: var(--ink); color: var(--panel); font-weight: 700; font-size: var(--fs-lg); position: relative;
}
.brand-mark::after { content: ""; position: absolute; right: -2px; bottom: -2px; width: 10px; height: 10px; border-radius: 50%; background: var(--brand); border: 2px solid var(--panel); }
.brand h1 { margin: 0; font-size: var(--fs-lg); font-weight: 600; letter-spacing: -.005em; }
.brand .sub { font-size: var(--fs-xs); color: var(--ink-3); display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.live-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--good); }
.live-dot.stale { background: var(--bad); }
.actions, .theme-toggle { display: flex; align-items: center; gap: 8px; }
.actions form { display: contents; }
.refreshing-indicator { display: inline-flex; align-items: center; gap: 6px; color: var(--primary); }
.spinner {
  width: 12px; height: 12px; border: 2px solid var(--border); border-top-color: var(--primary);
  border-radius: 50%; animation: spin .7s linear infinite;
}

/* ---------- Tabs + filters ---------- */
.subbar {
  position: sticky; top: env(safe-area-inset-top, 0px); z-index: 5;
  background: color-mix(in srgb, var(--bg) 92%, transparent); backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.subbar-inner { max-width: 1600px; margin: 0 auto; padding: 0 24px; }
.tabs { display: flex; gap: 4px; padding-top: 10px; }
.tab {
  appearance: none; border: 0; background: none; cursor: pointer;
  padding: 8px 12px 10px; font-size: var(--fs-md); font-weight: 500; color: var(--ink-3);
  border-bottom: 2px solid transparent; display: inline-flex; gap: 8px; align-items: center;
}
.tab:hover { color: var(--ink); }
.tab[aria-selected="true"] { color: var(--ink); border-bottom-color: var(--primary); }
.tab .count { font-size: var(--fs-xs); font-weight: 600; background: var(--panel-2); border: 1px solid var(--border); border-radius: 999px; padding: 0 7px; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.filters { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 16px; padding: 10px 0 12px; border-top: 1px solid var(--border); }
.filter-group { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.filter-label { font-size: var(--fs-xs); font-weight: 600; letter-spacing: .06em; text-transform: uppercase; color: var(--ink-3); }
.seg { display: inline-flex; background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px; padding: 2px; gap: 2px; flex-wrap: wrap; }
.seg button {
  border: 0; background: none; cursor: pointer; padding: 5px 10px; border-radius: 6px;
  font-size: var(--fs-sm); font-weight: 500; color: var(--ink-2); min-height: 30px;
}
.seg button:hover { color: var(--ink); }
.seg button[aria-pressed="true"] { background: var(--panel); color: var(--ink); box-shadow: 0 0 0 1px var(--border-strong); }
.seg .sep { width: 1px; background: var(--border-strong); margin: 4px 2px; }
select.sel {
  height: 34px; padding: 0 30px 0 10px; font-size: var(--fs-sm); cursor: pointer; max-width: 100%;
  background: var(--panel) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%2369758b' stroke-width='2.5'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E") no-repeat right 10px center;
  appearance: none;
}
.clear { font-size: var(--fs-sm); color: var(--primary); background: none; border: 0; cursor: pointer; padding: 4px; }
.clear:hover { text-decoration: underline; }

/* ==========================================================================
   Layout
   ========================================================================== */
main { max-width: 1600px; margin: 0 auto; padding: 20px 24px 48px; display: grid; gap: 16px; }
.view { display: grid; gap: 16px; min-width: 0; }
.grid { display: grid; grid-template-columns: repeat(12, minmax(0, 1fr)); gap: 16px; }
.c-8 { grid-column: span 8; } .c-4 { grid-column: span 4; } .c-7 { grid-column: span 7; } .c-6 { grid-column: span 6; } .c-5 { grid-column: span 5; } .c-12 { grid-column: span 12; }
@media (max-width: 1080px) { .c-8, .c-4, .c-7, .c-6, .c-5 { grid-column: span 12; } }

.panel { background: var(--panel); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow); min-width: 0; }
.panel-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; padding: 16px 18px 0; flex-wrap: wrap; }
.panel-head h2 { margin: 0; font-size: var(--fs-lg); font-weight: 600; text-wrap: balance; }
.panel-head p { margin: 2px 0 0; font-size: var(--fs-sm); color: var(--ink-3); }
.panel-body { padding: 14px 18px 18px; }
.foot { font-size: var(--fs-xs); color: var(--ink-3); padding: 0 18px 16px; }
.scroll-x { overflow-x: auto; }
.empty { padding: 32px; text-align: center; color: var(--ink-3); }

.error-box {
  background: var(--bad-soft); border: 1px solid var(--bad); color: var(--bad);
  padding: 12px 16px; border-radius: 8px; font-size: var(--fs-md); white-space: pre-wrap;
}

/* ---------- KPIs ---------- */
.kpis { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); background: var(--panel); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow); }
.kpi { padding: 16px 18px; display: grid; gap: 6px; align-content: start; border-left: 1px solid var(--border); min-width: 0; }
.kpi:first-child { border-left: 0; }
.kpi-label { font-size: var(--fs-xs); font-weight: 500; color: var(--ink-3); display: flex; justify-content: space-between; gap: 8px; }
.kpi-row { display: flex; align-items: flex-end; justify-content: space-between; gap: 10px; }
.kpi-value { font-size: var(--fs-2xl); font-weight: 600; letter-spacing: -.02em; line-height: var(--lh-dato); font-variant-numeric: tabular-nums; white-space: nowrap; }
.kpi-meta { font-size: var(--fs-xs); color: var(--ink-3); display: flex; gap: 6px; align-items: center; flex-wrap: wrap; min-height: 18px; }
.kpi-meta b { color: var(--ink); font-weight: 600; font-variant-numeric: tabular-nums; }
.delta { font-weight: 600; font-variant-numeric: tabular-nums; display: inline-flex; align-items: center; gap: 2px; }
.delta.up { color: var(--good); } .delta.down { color: var(--bad); } .delta.flat { color: var(--ink-3); }
.spark { flex: none; }
.gauge { position: relative; height: 6px; border-radius: 3px; background: var(--panel-2); margin-top: 4px; }
.gauge i { position: absolute; top: -3px; width: 1px; height: 12px; background: var(--border-strong); }
.gauge b { position: absolute; left: 0; top: 0; height: 6px; border-radius: 3px; }
@media (max-width: 1080px) {
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .kpi { border-left: 0; border-top: 1px solid var(--border); }
  .kpi:nth-child(-n+2) { border-top: 0; }
  .kpi:nth-child(2n) { border-left: 1px solid var(--border); }
  .kpi:last-child { grid-column: span 2; border-left: 0; }
}

/* ---------- Pills / tags ---------- */
.pill { display: inline-flex; align-items: center; gap: 5px; padding: 1px 8px; border-radius: 999px; font-size: var(--fs-xs); font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; line-height: 20px; background: var(--panel-2); color: var(--ink-2); }
.pill.good, .pill.warn, .pill.bad, .pill.neutral { gap: 5px; }
.pill.good::before, .pill.warn::before, .pill.bad::before, .pill.neutral::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.pill.good { color: var(--good); background: var(--good-soft); }
.pill.warn { color: var(--warn); background: var(--warn-soft); }
.pill.bad { color: var(--bad); background: var(--bad-soft); }
.pill.neutral { color: var(--ink-2); background: var(--panel-2); }
.pill.plain::before { display: none; }
.tag, .area-pill { display: inline-block; font-size: var(--fs-xs); font-weight: 400; padding: 1px 7px; border-radius: 5px; background: var(--panel-2); border: 1px solid var(--border); color: var(--ink-2); white-space: nowrap; }
.area-pill { margin: 1px 3px 1px 0; }

/* ---------- Monthly chart ---------- */
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: var(--fs-sm); color: var(--ink-2); align-items: center; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }
.chart-scroll { overflow-x: auto; overscroll-behavior-x: contain; }
.chart-box { position: relative; height: 290px; }
.chart-box.short { height: 240px; }
.chart-detail { margin: 10px 0 0; padding: 10px 12px; border-radius: 8px; background: var(--panel-2); color: var(--ink-2); font-size: var(--fs-sm); line-height: 1.5; font-variant-numeric: tabular-nums; }
.chart-detail b { color: var(--ink); }
.pf-total { display: grid; gap: 6px; margin-bottom: 8px; }
.pf-num { font-size: var(--fs-2xl); font-weight: 600; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
.pf-meta { font-size: var(--fs-sm); color: var(--ink-3); }
.pf-meta b { color: var(--ink); }
.fact-pct { margin-left: 6px; font-size: var(--fs-xs); color: var(--ink-3); }
.chart-hint { margin: 6px 0 0; font-size: var(--fs-xs); color: var(--ink-3); }

/* ---------- Margin strip plot ---------- */
.strip svg { width: 100%; height: auto; display: block; overflow: visible; }
.strip text { font: 500 11px var(--sans); fill: var(--ink-3); }
.buckets { display: grid; gap: 10px; margin-top: 14px; }
.bucket { display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; align-items: center; }
.bucket-name { font-size: var(--fs-sm); display: flex; align-items: center; gap: 8px; }
.bucket-val { font-size: var(--fs-sm); color: var(--ink-3); font-variant-numeric: tabular-nums; text-align: right; }
.bucket-val b { color: var(--ink); }
.bucket-bar { grid-column: 1 / -1; height: 6px; background: var(--panel-2); border-radius: 3px; overflow: hidden; }
.bucket-bar b { display: block; height: 100%; border-radius: 3px; }

/* ---------- Rank tables (ejecutivos / áreas) ---------- */
.rank { width: 100%; border-collapse: collapse; font-size: var(--fs-sm); }
.rank th { text-align: left; font-size: var(--fs-xs); font-weight: 600; letter-spacing: .05em; text-transform: uppercase; color: var(--ink-3); padding: 0 8px 8px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.rank td { padding: 9px 8px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.rank tr:last-child td { border-bottom: 0; }
.rank th:first-child, .rank td:first-child { padding-left: 0; }
.rank th:last-child, .rank td:last-child { padding-right: 0; }
.rank .r { text-align: right; font-variant-numeric: tabular-nums; }
.rank tbody tr.click { cursor: pointer; transition: background .12s; }
.rank tbody tr.click:hover { background: var(--panel-2); }
.rank tbody tr.sel { background: var(--primary-soft); }
.rank tbody tr.dim { opacity: .45; }
.who { display: flex; align-items: center; gap: 8px; min-width: 0; font-weight: 500; }
.who .dot { width: 9px; height: 9px; border-radius: 50%; flex: none; }
.who span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.barcell { display: grid; gap: 4px; min-width: 110px; }
.barcell .v { font-variant-numeric: tabular-nums; font-weight: 500; }
.hbar { height: 6px; background: var(--panel-2); border-radius: 3px; overflow: hidden; }
.hbar b { display: block; height: 100%; border-radius: 3px; }

/* ---------- Top clientes ---------- */
.top-list { display: grid; gap: 12px; }
.top-item { display: grid; grid-template-columns: 22px minmax(0, 1fr) auto; gap: 4px 10px; align-items: center; }
.top-rank { font-family: var(--mono); font-size: var(--fs-xs); color: var(--ink-3); }
.top-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 500; font-size: var(--fs-sm); }
.top-val { text-align: right; font-variant-numeric: tabular-nums; font-size: var(--fs-sm); font-weight: 500; display: flex; gap: 8px; align-items: center; justify-content: flex-end; }
.top-item .hbar { grid-column: 2 / -1; height: 8px; }

/* ---------- Requiere revisión ---------- */
.issue-chips { display: grid; gap: 10px; }
.issue-tier { display: grid; gap: 2px; }
.issue-tier-head { font-size: var(--fs-md); color: var(--ink); }
.issue-tier-head b { font-variant-numeric: tabular-nums; }
.issue-tier-detail { font-size: var(--fs-sm); line-height: 1.45; color: var(--ink-2); }
.issue-list { display: grid; margin-top: 12px; }
.issue { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; gap: 2px 12px; padding: 10px 0; border-top: 1px solid var(--border); align-items: start; color: inherit; text-decoration: none; }
.issue:hover .t { text-decoration: underline; }
.issue .sev { width: 4px; align-self: stretch; border-radius: 2px; grid-row: span 2; }
.issue .t { font-size: var(--fs-sm); font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.issue .d { font-size: var(--fs-xs); color: var(--ink-3); grid-column: 2 / -1; display: flex; gap: 6px; flex-wrap: wrap; }

/* ---------- Notas de venta table ---------- */
.nv-toolbar { display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap; align-items: center; padding: 14px 18px; border-bottom: 1px solid var(--border); }
.search { position: relative; flex: 1 1 260px; max-width: 360px; }
.search svg { position: absolute; left: 10px; top: 50%; transform: translateY(-50%); width: 16px; height: 16px; color: var(--ink-3); pointer-events: none; }
.search input { width: 100%; font-size: var(--fs-sm); padding-left: 34px; }
.nv-summary { display: flex; gap: 18px; flex-wrap: wrap; font-size: var(--fs-sm); color: var(--ink-3); }
.nv-sort { display: none; }
.nv-pre { display: none; }
.nv-summary b { color: var(--ink); font-weight: 600; font-variant-numeric: tabular-nums; }
table.nv { width: 100%; border-collapse: collapse; font-size: var(--fs-sm); min-width: 960px; }
table.nv th { background: var(--panel); text-align: left; font-size: var(--fs-xs); font-weight: 600; letter-spacing: .05em; text-transform: uppercase; color: var(--ink-3); padding: 10px 8px; border-bottom: 1px solid var(--border); white-space: nowrap; }
table.nv th button { all: unset; cursor: pointer; display: inline-flex; gap: 4px; align-items: center; }
table.nv th button:hover { color: var(--ink); }
table.nv th button:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
table.nv th.active { color: var(--ink); }
table.nv td { padding: 10px 8px; border-bottom: 1px solid var(--border); white-space: nowrap; }
table.nv tbody tr { transition: background .12s; }
table.nv tbody tr:hover { background: var(--panel-2); }
table.nv .r { text-align: right; font-variant-numeric: tabular-nums; }
table.nv .cli { max-width: 190px; overflow: hidden; text-overflow: ellipsis; }
table.nv td.vend { max-width: 160px; }
table.nv td.vend .who span { overflow: hidden; text-overflow: ellipsis; }
table.nv td.areas { white-space: normal; min-width: 100px; }
table.nv td.areas .tag { margin: 1px 0; }
.nvlink { color: var(--primary); font-family: var(--mono); font-weight: 500; text-decoration: none; }
.nvlink:hover { text-decoration: underline; }
.mcell { display: inline-grid; grid-template-columns: 40px auto; align-items: center; gap: 6px; }
.mbar { height: 4px; border-radius: 2px; background: var(--panel-2); position: relative; overflow: hidden; }
.mbar b { position: absolute; left: 0; top: 0; bottom: 0; border-radius: 2px; }
.plan { color: var(--ink-3); }
.plan .dd { font-size: var(--fs-xs); margin-left: 4px; }
.alert-btn { display: inline-flex; align-items: center; gap: 4px; color: var(--warn); font-weight: 600; font-size: var(--fs-xs); cursor: help; }
.alert-btn svg { width: 14px; height: 14px; }

/* ==========================================================================
   Detail page + login (older markup)
   ========================================================================== */
.card { background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 18px; box-shadow: var(--shadow); }
.card table { width: 100%; border-collapse: collapse; font-size: var(--fs-md); }
.card th, .card td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); }
.card th { color: var(--ink-3); font-weight: 600; font-size: var(--fs-xs); text-transform: uppercase; letter-spacing: .05em; }
.card tr:hover td { background: var(--panel-2); }
.card th.num, .card td.num { text-align: right; }
.detail-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px; margin-bottom: 20px; }
.detail-grid .card { padding: 14px 16px; }
.comision-desglose { list-style: none; margin: 8px 0 0; padding: 8px 0 0; border-top: 1px solid var(--border); display: grid; gap: 4px; font-size: var(--fs-xs); color: var(--ink-3); }
.comision-desglose li { display: flex; justify-content: space-between; gap: 12px; }
.comision-desglose b { color: var(--ink); font-weight: 500; font-variant-numeric: tabular-nums; white-space: nowrap; }
.nv-totales { margin: 14px 0 0 auto; padding: 12px 0 0; max-width: 340px; display: grid; gap: 6px; border-top: 1px solid var(--border); font-variant-numeric: tabular-nums; }
.nv-totales div { display: flex; justify-content: space-between; gap: 24px; font-size: var(--fs-md); }
.nv-totales dt { color: var(--ink-3); }
.nv-totales dd { margin: 0; font-weight: 500; }
.nv-totales .total { margin-top: 2px; padding-top: 8px; border-top: 1px solid var(--border-strong); font-size: var(--fs-lg); }
.nv-totales .total dt { color: var(--ink); font-weight: 600; }
.nv-totales .total dd { font-weight: 700; }
.label { font-size: var(--fs-xs); }
.section-title { font-size: var(--fs-xs); font-weight: 600; text-transform: uppercase; letter-spacing: .06em; color: var(--ink-3); margin: 24px 0 10px; }
.warnings-list { list-style: none; padding: 0; margin: 0; }
.warnings-list li { padding: 8px 12px; border-left: 3px solid var(--warn); background: var(--warn-soft); margin-bottom: 6px; font-size: var(--fs-sm); border-radius: 4px; }
.warn-badge { color: var(--warn); font-size: var(--fs-xs); cursor: default; }
.back-link { font-size: var(--fs-sm); }
main.detail { display: block; max-width: 1200px; }

.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.login-card { width: 340px; max-width: 100%; display: grid; gap: 14px; }
.login-card .login-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.login-card h2 { margin: 0; font-size: var(--fs-lg); font-weight: 600; }
.login-card form { display: grid; gap: 10px; }
.login-card input { width: 100%; }
.login-card .btn { justify-content: center; }

@media (max-width: 640px) {
  .appbar, header.topbar { padding-left: 16px; padding-right: 16px; }
  .subbar-inner, main { padding-left: 16px; padding-right: 16px; }
  .btn .lbl { display: none; }
  .btn.refresh { width: 36px; padding: 0; justify-content: center; }
  .kpi-value { font-size: var(--fs-xl); }
  .spark { display: none; }
  .rank .hide-sm { display: none; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; }
  .btn.busy svg, .spinner { animation-duration: 2.5s; }
}

/* ---------- Asignar tipo de documento (detalle NV) ---------- */
.doc-type-form { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.doc-type-form select, .doc-type-form input { height: 30px; font-size: var(--fs-sm); padding: 0 8px; }
.doc-type-form select { max-width: 220px; }
.doc-type-form input { width: 140px; }
.doc-type-form .btn { height: 30px; padding: 0 10px; font-size: var(--fs-xs); }
.notice { padding: 10px 14px; border-radius: 8px; font-size: var(--fs-md); margin-bottom: 12px; }
.notice.good { background: var(--good-soft); color: var(--good); border: 1px solid var(--good); }

/* ---------- Correcciones manuales (comisión, áreas) ---------- */
.override-details { margin-top: 10px; }
.override-details > summary { cursor: pointer; font-size: var(--fs-sm); color: var(--accent); list-style: none; }
.override-details > summary::-webkit-details-marker { display: none; }
.override-form { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-top: 8px; }
.override-form input[type="text"] { height: 30px; font-size: var(--fs-sm); padding: 0 8px; width: 160px; }
.checkbox-label { display: inline-flex; align-items: center; gap: 5px; font-size: var(--fs-sm); color: var(--ink-2); white-space: nowrap; }
.override-actions { width: 100%; }
.comision-componentes { display: grid; gap: 8px; }
.comision-comp { display: grid; grid-template-columns: 9.5em 160px 1fr; gap: 8px; align-items: center; font-size: var(--fs-sm); }
@media (max-width: 640px) { .comision-comp { grid-template-columns: 1fr; gap: 3px; } }
.override-actions .btn { height: 30px; padding: 0 10px; font-size: var(--fs-xs); }

/* ---------- Usuarios ---------- */
.user-menu { position: relative; }
.user-menu > summary { list-style: none; }
.user-menu > summary::-webkit-details-marker { display: none; }
.user-menu-panel {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 20; min-width: 240px;
  background: var(--panel); border: 1px solid var(--border-strong); border-radius: 10px;
  box-shadow: 0 8px 24px rgba(14, 26, 47, .14); padding: 6px; display: grid;
}
.user-menu-panel a { padding: 8px 10px; border-radius: 6px; color: var(--ink); text-decoration: none; font-size: var(--fs-md); }
.user-menu-panel a:hover { background: var(--panel-2); }
.user-menu-who { display: grid; gap: 3px; padding: 8px 10px 10px; margin-bottom: 4px; border-bottom: 1px solid var(--border); font-size: var(--fs-sm); color: var(--ink-3); }
.user-menu-who b { color: var(--ink); font-size: var(--fs-md); }
.user-menu-who .pill { justify-self: start; margin-top: 2px; }
.notice.scope { background: var(--primary-soft); color: var(--ink); border: 1px solid var(--border-strong); margin-bottom: 0; }
.roles-legend { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 10px; margin-bottom: 8px; }
.roles-legend div { background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; display: grid; gap: 2px; font-size: var(--fs-sm); }
.roles-legend span { color: var(--ink-3); font-size: var(--fs-sm); }
.user-form { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 12px 16px; align-items: start; }
.user-form > label:not(.check) { display: grid; gap: 4px; font-size: var(--fs-sm); color: var(--ink-2); font-weight: 500; }
.user-form input[type="text"], .user-form input[type="password"], .user-form select { width: 100%; }
.user-form .check { display: flex; gap: 8px; align-items: center; font-size: var(--fs-md); }
.areas-field { grid-column: 1 / -1; border: 1px solid var(--border); border-radius: 8px; padding: 8px 12px 10px; display: flex; flex-wrap: wrap; gap: 8px 18px; margin: 0; }
.areas-field legend { font-size: var(--fs-sm); color: var(--ink-2); font-weight: 500; padding: 0 4px; }
.areas-field .field-note { flex-basis: 100%; font-size: var(--fs-xs); }
.user-form-actions { grid-column: 1 / -1; display: flex; gap: 8px; }
.row-actions { display: flex; gap: 6px; align-items: center; white-space: nowrap; }
.row-actions form { display: contents; }
.row-actions .btn { height: 30px; padding: 0 10px; font-size: var(--fs-xs); }
.btn.danger { color: var(--bad); border-color: var(--bad); }
.btn.danger:hover { background: var(--bad-soft); }
/* Confirmación en la misma página para acciones con efectos (aprobar, rechazar, enviar): ver static/aprobacion.js */
.aprobar-barra .contador { font-size: var(--fs-sm); color: var(--ink-2); font-variant-numeric: tabular-nums; min-width: 11.5em; }
.btn[data-requiere-marca]:disabled { cursor: not-allowed; opacity: .5; }
.confirmar-barra { display: flex; flex-wrap: wrap; gap: 10px 14px; align-items: center; padding: 12px 16px; border-top: 1px solid var(--border-strong); background: var(--panel-2); }
.confirmar-barra[hidden] { display: none; }
.confirmar-barra.rechazo { border-top-color: var(--bad); }
.confirmar-barra p { margin: 0; flex: 1 1 320px; max-width: 70ch; font-size: var(--fs-md); line-height: 1.45; color: var(--ink); }
.override-details > summary.peligro { color: var(--bad); }
/* ---------- Barra de módulos (templates/_modnav.html) ---------- */
.modnav { background: var(--panel); border-bottom: 1px solid var(--border); }
.modnav-in { position: relative; max-width: 1600px; margin: 0 auto; padding: 0 24px; display: flex; align-items: stretch; gap: 2px; overflow-x: auto; scrollbar-width: none; }
.modnav-in::-webkit-scrollbar { display: none; }
.modnav a { display: flex; align-items: center; height: 42px; padding: 0 12px; font-size: var(--fs-md); font-weight: 500; color: var(--ink-2); text-decoration: none; white-space: nowrap; border-bottom: 2px solid transparent; }
.modnav a:hover { color: var(--ink); }
.modnav a[aria-current="page"] { color: var(--primary); border-bottom-color: var(--primary); font-weight: 600; }
.modnav a.brand-mark { align-self: center; height: 28px; width: 28px; padding: 0; margin-right: 10px; border-bottom: 0; color: var(--panel); font-size: var(--fs-sm); text-decoration: none; justify-content: center; }
.modnav a.brand-mark::after { width: 8px; height: 8px; }
.modnav-sub { background: var(--panel-2); border-top: 1px solid var(--border); }
.modnav-sub a { height: 36px; font-size: var(--fs-sm); }
.herramienta { margin-top: 16px; }
.herramienta > summary { cursor: pointer; padding: 12px 16px; font-size: var(--fs-md); font-weight: 600; list-style: none; }
.herramienta > summary::-webkit-details-marker { display: none; }
.herramienta > summary::before { content: "▸"; display: inline-block; width: 1.1em; color: var(--ink-3); }
.herramienta[open] > summary::before { content: "▾"; }
.herramienta[open] > summary { border-bottom: 1px solid var(--border); }
.herramienta .cuerpo { padding: 14px 16px; }
@media (max-width: 640px) {
  .modnav-in { padding: 0 8px; }
  .modnav a { height: 44px; padding: 0 12px; }
  .modnav-sub a { height: 40px; }
}
.login-card input[type="text"] { width: 100%; }

/* ---------- Gastos ---------- */
.gasto-contacto { display: grid; gap: 4px; font-size: var(--fs-sm); color: var(--ink-2); font-weight: 500; max-width: 360px; margin-bottom: 16px; }
.gasto-contacto input { width: 100%; }
.gasto-row {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px 16px; align-items: start;
  border: 1px solid var(--border); border-radius: 10px; padding: 14px 16px 16px; margin-bottom: 14px;
}
.gasto-row > label { display: grid; gap: 4px; font-size: var(--fs-sm); color: var(--ink-2); font-weight: 500; }
.gasto-row input, .gasto-row select { width: 100%; }
.gasto-row-head { grid-column: 1 / -1; font-size: var(--fs-xs); color: var(--ink-3); text-transform: uppercase; letter-spacing: .05em; }
.gasto-row-wide { grid-column: 1 / -1; }
/* A segmented control (same visual language as .seg elsewhere): two radios,
   styled as pill buttons via :has() instead of the bare "( ) label" pair
   that used to crowd this into three wrapped lines at narrow widths. */
.asignacion-toggle {
  grid-column: 1 / -1; display: flex; gap: 2px; background: var(--panel-2);
  border: 1px solid var(--border); border-radius: 8px; padding: 2px; width: fit-content; max-width: 100%;
}
.asignacion-toggle .check {
  position: relative; flex: 1 1 auto; display: flex; align-items: center; justify-content: center; gap: 6px;
  padding: 7px 14px; border-radius: 6px; font-size: var(--fs-sm); font-weight: 500; color: var(--ink-2);
  cursor: pointer; text-align: center; white-space: nowrap;
}
.asignacion-toggle .check:has(input:checked) { background: var(--panel); color: var(--ink); box-shadow: 0 0 0 1px var(--border-strong); }
.asignacion-toggle .check input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.asignacion-toggle .check:has(input:focus-visible) { outline: 2px solid var(--primary); outline-offset: 2px; }
@media (max-width: 480px) { .asignacion-toggle { flex-direction: column; } .asignacion-toggle .check { white-space: normal; } }

.gasto-row .check { display: flex; gap: 8px; align-items: center; font-size: var(--fs-md); font-weight: 400; }
.gasto-rows-actions { display: flex; gap: 8px; margin-bottom: 16px; }

/* A styled ::file-selector-button instead of the bare OS control, matching .btn. */
.gasto-row input[type="file"] {
  border: 1px solid var(--border-strong); background: var(--panel); border-radius: 8px;
  height: 36px; padding: 0; font-size: var(--fs-sm); color: var(--ink-2); display: flex; align-items: center; overflow: hidden;
}
.gasto-row input[type="file"]::file-selector-button {
  height: 100%; padding: 0 14px; margin-right: 12px; border: 0; border-right: 1px solid var(--border-strong);
  background: var(--panel-2); color: var(--ink); font: inherit; font-weight: 500; font-size: var(--fs-sm); cursor: pointer;
}
.gasto-row input[type="file"]::file-selector-button:hover { background: var(--border); }

/* The native date icon renders square and can overhang the input's rounded
   corner; padding + radius seats it inside, and it needs inverting in dark
   mode (the browser always draws it as a dark glyph). */
input[type="date"] { padding-right: 8px; }
input[type="date"]::-webkit-calendar-picker-indicator {
  padding: 4px; margin-right: 2px; border-radius: 6px; cursor: pointer; filter: var(--date-icon-filter, none);
}
input[type="date"]::-webkit-calendar-picker-indicator:hover { background: var(--panel-2); }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) { --date-icon-filter: invert(1); } }
:root[data-theme="dark"] { --date-icon-filter: invert(1); }

.gastos-admin-filters { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 12px 16px; margin-bottom: 16px; }
.gastos-admin-filters label { display: grid; gap: 4px; font-size: var(--fs-sm); color: var(--ink-2); font-weight: 500; }
.gastos-admin-filters input, .gastos-admin-filters select { min-width: 0; width: 100%; }
.gastos-admin-filters label.wide { grid-column: span 2; }
.gastos-summary { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 14px; }
.gastos-summary .pill { font-size: var(--fs-sm); padding: 3px 10px; }
.gastos-summary .pill-monto { display: none; }

.gastos-charts { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 16px; margin-bottom: 18px; }
.gastos-charts .panel-title { font-size: var(--fs-xs); font-weight: 600; color: var(--ink-3); margin-bottom: 10px; }

/* Row separators used to look broken because .row-actions (display:flex)
   sat directly on the <td>, overriding its table-cell layout and letting
   adjacent rows' borders drift out of alignment whenever one row's actions
   cell held two buttons and the next held one or none. The flex container
   is now an inner <span> instead, so the <td> stays a normal table cell;
   the shared min-height keeps every row (and its divider line) the same
   height regardless of how many buttons it holds. */
#gastos-table td.actions-cell { padding-top: 8px; padding-bottom: 8px; }
#gastos-table td.actions-cell .row-actions { min-height: 30px; }
#gastos-table tbody tr:nth-child(even) td { background: color-mix(in srgb, var(--panel-2) 55%, transparent); }
#gastos-table tbody tr:hover td { background: var(--panel-2); }
#gastos-table input[type="checkbox"] { width: 16px; height: 16px; cursor: pointer; }
#gastos-table input[type="checkbox"]:disabled { cursor: not-allowed; opacity: .35; }

.gastos-bulk-bar {
  display: flex; gap: 10px; align-items: center; flex-wrap: wrap;
  background: var(--primary-soft); border: 1px solid var(--border-strong); border-radius: 8px;
  padding: 10px 14px; margin-bottom: 14px;
}
.gastos-bulk-bar #bulk-count { font-size: var(--fs-sm); font-weight: 600; color: var(--ink); white-space: nowrap; }
.gastos-bulk-bar input[type="text"] { height: 32px; font-size: var(--fs-sm); max-width: 220px; }
.gastos-bulk-bar .btn { height: 32px; padding: 0 12px; font-size: var(--fs-sm); }

.field-error { grid-column: 1 / -1; color: var(--bad); font-size: var(--fs-xs); margin-top: -8px; }
.field-error:empty { display: none; margin-top: 0; }
.gasto-row input[aria-invalid="true"], .gasto-row select[aria-invalid="true"] { border-color: var(--bad); }
.gasto-row.invalid { border-color: var(--bad); }
[role="alert"], [role="status"] { outline: none; }
[role="alert"]:focus-visible, [role="status"]:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }


/* ==========================================================================
   Phone layout (<= 640px). Touch targets of 40px+, 16px form text so iOS
   doesn't zoom on focus, the user menu kept inside the screen, and the
   documents table turned into one card per file.
   ========================================================================== */
@media (max-width: 640px) {
  .btn { height: 40px; }
  .btn.sm { height: 34px; }
  .btn.icon, .btn.refresh { width: 40px; }
  input[type="text"], input[type="password"], input[type="search"], input[type="date"], select { height: 40px; font-size: var(--fs-lg); }
  select.sel { height: 40px; font-size: var(--fs-md); }
  .seg button { min-height: 36px; }
  .tab { padding: 10px 12px 12px; }

  /* Tabs plus four filters stacked would cover ~40% of a phone screen if they
     stayed pinned while scrolling. */
  .subbar { position: static; }

  /* The menu button sits wherever the header wraps it to; anchoring the panel
     to the header (not the button) keeps it on screen. */
  .appbar, header.topbar { position: relative; }
  .user-menu { position: static; }
  .user-menu-panel { left: 12px; right: 12px; top: 100%; min-width: 0; margin-top: 4px; }
  .user-menu-panel a { padding: 12px 10px; font-size: var(--fs-lg); }

  .panel-head { padding: 14px 14px 0; }
  .panel-body { padding: 12px 14px 14px; }
  .card { padding: 14px; }
  .detail-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .detail-grid .card { padding: 12px; }

  /* Documentos considerados: one card per file instead of a 5-column table. */
  .docs-table thead { display: none; }
  .docs-table, .docs-table tbody, .docs-table tr, .docs-table td { display: block; width: 100%; }
  .docs-table tr { padding: 12px 0; border-bottom: 1px solid var(--border); }
  .docs-table tr:last-child { border-bottom: 0; }
  .card .docs-table td { padding: 3px 0; border: 0; }
  .card .docs-table tr:hover td { background: none; }
  .docs-table .doc-name { font-weight: 600; font-size: var(--fs-md); overflow-wrap: anywhere; padding-bottom: 4px; }
  .docs-table td[data-label]::before {
    content: attr(data-label); display: block; font-size: var(--fs-xs); font-weight: 600; letter-spacing: .05em;
    text-transform: uppercase; color: var(--ink-3);
  }
  .docs-table .doc-type-form { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 8px; margin-top: 6px; }
  .docs-table .doc-type-form select, .docs-table .doc-type-form input { max-width: none; width: 100%; height: 40px; font-size: var(--fs-lg); }
  .docs-table .doc-type-form input[name="monto"] { grid-column: 1 / -1; }
  .docs-table .doc-type-form .btn { height: 40px; padding: 0 16px; font-size: var(--fs-sm); }
  .docs-table td:last-child { padding-top: 8px; }
  .docs-table td:last-child .btn { width: 100%; justify-content: center; height: 40px; font-size: var(--fs-sm); }
}

/* ---------- Phone layout: gastos / pagos lists ---------- */
@media (max-width: 640px) {
  /* Every row of a .cards-table becomes a card; each cell shows its own
     column name (data-label) in front of its value. */
  table.cards-table { min-width: 0; }
  .cards-table thead { display: none; }
  .cards-table, .cards-table tbody, .cards-table tr, .cards-table td { display: block; width: 100%; }
  .cards-table tbody tr {
    margin: 0 0 10px; padding: 8px 12px; border: 1px solid var(--border); border-radius: 10px; background: var(--panel);
  }
  .cards-table tbody tr[hidden] { display: none; }
  .card .cards-table td, #gastos-table.cards-table td {
    display: grid; grid-template-columns: 96px minmax(0, 1fr); gap: 8px; align-items: start;
    padding: 6px 0; border: 0; text-align: left; background: none;
  }
  .card .cards-table td::before {
    content: attr(data-label); font-size: var(--fs-xs); font-weight: 600; letter-spacing: .05em;
    text-transform: uppercase; color: var(--ink-3); padding-top: 2px;
  }
  .card .cards-table td .cell { min-width: 0; overflow-wrap: anywhere; }
  .card .cards-table td .cell .tag { white-space: normal; margin: 2px 2px 2px 0; }
  .card .cards-table td:not([data-label]), #gastos-table.cards-table td:not([data-label]) { display: block; }
  .card .cards-table td.num { text-align: left; font-weight: 600; }
  .card .cards-table td[colspan] { display: block; }
  .card .cards-table tr:hover td, #gastos-table.cards-table tbody tr:hover td,
  #gastos-table.cards-table tbody tr:nth-child(even) td { background: none; }
  .cards-table tbody tr:has(.bulk-check:checked) { border-color: var(--primary); background: var(--primary-soft); }

  /* The select-box cell sits alone on the card's first line, bigger to tap. */
  #gastos-table.cards-table td:first-child:not([data-label]) { padding-bottom: 2px; }
  #gastos-table input[type="checkbox"] { width: 22px; height: 22px; }
  #gastos-table.cards-table td.actions-cell { padding: 8px 0 2px; }
  #gastos-table td.actions-cell .row-actions { min-height: 0; }
  .cards-table .row-actions { white-space: normal; flex-wrap: wrap; }
  .cards-table .row-actions .btn, .cards-table .row-actions form { flex: 1 1 120px; }
  .cards-table .row-actions form { display: block; }
  .cards-table .row-actions .btn { width: 100%; justify-content: center; height: 40px; font-size: var(--fs-sm); }
  .cards-table td .btn.sm { height: 36px; }
  .card .cards-table td.row-actions { display: flex; padding-top: 8px; }
  .cards-table td.row-actions .btn { height: 40px; }

  /* Panels: pills with amounts instead of the eight stacked bars; filters two per row. */
  .gastos-charts { display: none; }
  .gastos-summary .pill { flex-wrap: wrap; row-gap: 0; }
  .gastos-summary .pill-monto { display: inline; font-weight: 500; opacity: .85; }
  .gastos-summary .pill-monto::before { content: "· "; }
  .gastos-admin-filters { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .gastos-admin-filters label:last-child { grid-column: 1 / -1; }
  .gastos-admin-filters input, .gastos-admin-filters select { min-width: 0; width: 100%; }

  /* Bulk bar: stays reachable at the bottom while ticking cards, with room for the home indicator. */
  .gastos-bulk-bar {
    position: sticky; bottom: 0; z-index: 6; margin: 0 -14px 12px; border-radius: 12px 12px 0 0;
    padding: 10px 14px calc(10px + env(safe-area-inset-bottom, 0px)); box-shadow: 0 -4px 16px rgba(0, 0, 0, .25);
  }
  .gastos-bulk-bar { gap: 8px; }
  .gastos-bulk-bar input[type="text"] { max-width: none; flex: 1 1 100%; height: 38px; font-size: var(--fs-lg); }
  .gastos-bulk-bar .btn { flex: 1 1 calc(50% - 8px); height: 38px; padding: 0 8px; justify-content: center; font-size: var(--fs-sm); }
  .gastos-bulk-bar #bulk-count { flex: 1 1 100%; }
  .gasto-rows-actions { flex-wrap: wrap; }
  .gasto-rows-actions .btn { flex: 1 1 100%; justify-content: center; }
  .user-form-actions .btn { flex: 1; justify-content: center; }
  .areas-field { padding: 10px 14px 12px; gap: 10px 18px; }
  .areas-field .check, .user-form .check { min-height: 32px; }
  .user-form input[type="checkbox"], .areas-field input[type="checkbox"] { width: 20px; height: 20px; }
}

/* ---------- Phone layout: Resumen + Notas de venta ---------- */
@media (max-width: 640px) {
  /* Tabs share the width; filters become a compact 2-column block. */
  .tabs { gap: 0; }
  .tab { flex: 1; justify-content: center; padding: 12px 8px; }
  .filters { display: grid; grid-template-columns: minmax(0, 1fr); gap: 10px; padding: 12px 0; }
  .filter-group { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .filter-group:first-child { grid-template-columns: auto minmax(0, 1fr); align-items: center; }
  .filter-group:first-child .filter-label { grid-column: 1 / -1; }
  .filter-group:first-child .sel { grid-column: 1 / -1; }
  #f-periodo { grid-column: 1 / -1; flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; }
  #f-periodo button { white-space: nowrap; flex: none; }
  #f-vendedor, .filter-group .clear { grid-column: 1 / -1; }
  .filter-group .clear { justify-self: start; min-height: 36px; }
  select.sel { width: 100%; }

  main { padding-top: 14px; gap: 14px; }
  .grid { gap: 14px; }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .kpi { padding: 14px; }
  .kpi-value { font-size: var(--fs-xl); }

  /* Ejecutivos / Áreas: a header line (name + margin) and the revenue bar under it. */
  .rank thead { display: none; }
  .rank, .rank tbody, .rank tr, .rank td { display: block; }
  .rank tbody tr {
    display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 6px 12px; align-items: center;
    padding: 10px 0; border-bottom: 1px solid var(--border);
  }
  .rank tbody tr:last-child { border-bottom: 0; }
  .rank td { padding: 0; border: 0; }
  .rank td:nth-child(1) { grid-column: 1; grid-row: 1; }
  .rank td:nth-child(5) { grid-column: 2; grid-row: 1; justify-self: end; }
  .rank td:nth-child(3) { grid-column: 1; grid-row: 2; }
  .rank td:nth-child(2) { grid-column: 2; grid-row: 2; justify-self: end; font-size: var(--fs-sm); color: var(--ink-3); }
  .rank td:nth-child(2)::after { content: " NV"; }
  .rank td.empty { grid-column: 1 / -1; padding: 16px 0; }
  .barcell { min-width: 0; }
  .rank tbody tr.click { padding-left: 4px; padding-right: 4px; margin: 0 -4px; }

  .top-item { grid-template-columns: 22px minmax(0, 1fr); }
  .top-name {
    grid-column: 2; white-space: normal; display: -webkit-box; -webkit-line-clamp: 2;
    -webkit-box-orient: vertical; overflow: hidden;
  }
  .top-val { grid-column: 2; justify-content: flex-start; }
  .top-rank { grid-row: 1; }
  .issue .t { white-space: normal; }

  /* Notas de venta: one card per NV. */
  .nv-toolbar { padding: 12px 14px; gap: 10px; }
  .search { flex: 1 1 100%; max-width: none; }
  .nv-sort { display: grid; gap: 4px; width: 100%; font-size: var(--fs-xs); font-weight: 600; letter-spacing: .05em; text-transform: uppercase; color: var(--ink-3); }
  .nv-summary { gap: 4px 14px; font-size: var(--fs-sm); }
  #view-notas .scroll-x { overflow: visible; }
  table.nv { display: block; min-width: 0; }
  table.nv thead { display: none; }
  table.nv tbody { display: grid; gap: 10px; padding: 12px; }
  table.nv tbody tr {
    display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 4px 8px; align-items: start;
    padding: 12px; border: 1px solid var(--border); border-radius: 10px; background: var(--panel);
  }
  table.nv tbody tr:hover { background: var(--panel); }
  table.nv td {
    display: block; padding: 0; border: 0; white-space: normal; max-width: none; min-width: 0;
    overflow: visible; text-overflow: clip; text-align: left; overflow-wrap: anywhere;
  }
  table.nv td[data-label]::before {
    content: attr(data-label); display: block; margin-bottom: 1px; font-size: var(--fs-xs); font-weight: 600;
    letter-spacing: .05em; text-transform: uppercase; color: var(--ink-3);
  }
  table.nv td:nth-child(1) { grid-column: span 2; font-size: var(--fs-lg); }
  table.nv td:nth-child(2) { grid-column: span 2; color: var(--ink-3); font-size: var(--fs-sm); align-self: center; }
  table.nv td:nth-child(3) { grid-column: span 2; justify-self: end; }
  table.nv td:nth-child(4) { grid-column: 1 / -1; font-weight: 600; font-size: var(--fs-md); margin-top: 4px; }
  table.nv td:nth-child(5), table.nv td:nth-child(6) { grid-column: 1 / -1; font-size: var(--fs-sm); }
  table.nv td:nth-child(7), table.nv td:nth-child(8), table.nv td:nth-child(9), table.nv td:nth-child(10) { grid-column: span 3; margin-top: 8px; font-variant-numeric: tabular-nums; }
  table.nv td:nth-child(11) { grid-column: span 4; margin-top: 8px; }
  table.nv td:nth-child(12) { grid-column: span 2; margin-top: 8px; }
  table.nv td:nth-child(13) { grid-column: 1 / -1; }
  table.nv td:nth-child(13):empty { display: none; }
  .fact-pct { display: block; margin: 0; }
  table.nv td.empty { grid-column: 1 / -1; padding: 20px 0; }
  table.nv td.r { text-align: left; }
  .nv-pre { display: inline; }
  table.nv .cli, table.nv td.vend { max-width: none; }
  .mcell { grid-template-columns: 56px auto; }
}

@media (max-width: 640px) {
  .card .cards-table td.item-name { font-weight: 600; font-size: var(--fs-md); overflow-wrap: anywhere; padding-bottom: 4px; }
  .nv-totales { max-width: none; margin-top: 4px; }
  .nv-totales div { font-size: var(--fs-md); }
}

/* Panel de pagos: la NV de la solicitud contra la que nombra el documento. */
.nv-check { font-size: var(--fs-xs); line-height: 1.35; padding: 4px 7px; border-radius: 6px; border-left: 3px solid currentColor; }
.nv-check.ok { color: var(--good); background: var(--good-soft); }
.nv-check.bad { color: var(--bad); background: var(--bad-soft); font-weight: 500; }

/* Estados de carga: bloques que brillan mientras llegan los datos, en vez de una pantalla vacía. */
.skeleton-list { display: grid; gap: 10px; margin: 18px auto 4px; max-width: 760px; }
.skeleton { height: 38px; border-radius: 8px; background: linear-gradient(90deg, var(--panel-2) 25%, var(--border) 50%, var(--panel-2) 75%); background-size: 200% 100%; animation: skeleton-shimmer 1.4s ease-in-out infinite; }
@keyframes skeleton-shimmer { from { background-position: 200% 0; } to { background-position: -200% 0; } }
@media (prefers-reduced-motion: reduce) { .skeleton { animation: none; } }

/* Aviso cuando falla el JavaScript de una página, en vez de dejarla muerta sin explicación. */
.client-error { position: fixed; left: 16px; right: 16px; bottom: 16px; z-index: 50; max-width: 560px; margin: 0 auto; padding: 12px 14px; border-radius: 10px; border-left: 4px solid var(--bad); background: var(--bad-soft); color: var(--bad); box-shadow: 0 6px 24px rgba(0, 0, 0, .18); display: flex; gap: 10px; align-items: center; flex-wrap: wrap; font-size: var(--fs-md); }
.client-error button { font: inherit; cursor: pointer; }
.draft-note { margin: 0 0 12px; }

/* Un documento del que no se pidió pagar el total (o se pidió de más). */
.doc-alert { font-size: var(--fs-xs); line-height: 1.4; padding: 5px 8px; margin-top: 6px; border-radius: 6px; border-left: 3px solid currentColor; display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.doc-alert.warn { color: var(--warn); background: var(--warn-soft); }
.doc-alert.bad { color: var(--bad); background: var(--bad-soft); font-weight: 500; }

/* ---------- Pagos a proveedores: wider page, stat tiles and a compact table ---------- */
main.detail.wide { max-width: 1480px; }

.stat-tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; margin-bottom: 18px; }
.stat-tile {
  --tile: var(--ink-3);
  display: grid; gap: 2px; text-align: left; cursor: pointer; font: inherit; color: var(--ink);
  background: var(--panel); border: 1px solid var(--border); border-top: 3px solid var(--tile);
  border-radius: 10px; padding: 12px 14px 12px;
  transition: border-color .15s, box-shadow .15s, transform .15s;
}
.stat-tile:hover { border-color: var(--tile); box-shadow: var(--shadow); }
.stat-tile:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
.stat-tile.active { background: color-mix(in srgb, var(--tile) 9%, var(--panel)); border-color: var(--tile); }
.stat-tile.pendiente { --tile: var(--warn); }
.stat-tile.aprobado { --tile: var(--primary); }
.stat-tile.pagado { --tile: var(--good); }
.stat-tile.rechazado { --tile: var(--bad); }
.stat-label { font-size: var(--fs-xs); font-weight: 600; letter-spacing: .04em; text-transform: uppercase; color: var(--ink-3); }
.stat-n { font-size: var(--fs-2xl); font-weight: 600; letter-spacing: -.02em; line-height: var(--lh-dato); font-variant-numeric: tabular-nums; }
.stat-amt { font-size: var(--fs-sm); color: var(--ink-2); font-variant-numeric: tabular-nums; }
a.stat-tile { text-decoration: none; }
/* Resumen por estado de un módulo (templates/_resumen_estados.html): tonos del tablero + barra de proporciones */
.t-warn { --tile: var(--warn); }
.t-primary { --tile: var(--primary); }
.t-good { --tile: var(--good); }
.t-bad { --tile: var(--bad); }
.t-neutral { --tile: var(--ink-3); }
.resumen-estados { grid-template-columns: repeat(auto-fit, minmax(128px, 1fr)); }
.estado-barra { display: flex; gap: 2px; height: 10px; margin: -6px 0 18px; border-radius: 5px; overflow: hidden; background: var(--panel-2); }
.estado-barra b { background: var(--tile); min-width: 4px; }

#gastos-table, .my-requests { width: 100%; border-collapse: separate; border-spacing: 0; }
#gastos-table th, .my-requests th { white-space: nowrap; }
#gastos-table td, .my-requests td { vertical-align: top; padding-top: 12px; padding-bottom: 12px; }
#gastos-table td.check-col, #gastos-table th.check-col { width: 36px; padding-right: 0; }
#gastos-table tbody tr td:first-child { box-shadow: inset 3px 0 0 var(--row-accent, transparent); }
#gastos-table tr[data-estado="pendiente"] { --row-accent: var(--warn); }
#gastos-table tr[data-estado="aprobado"] { --row-accent: var(--primary); }
#gastos-table tr[data-estado="pagado"] { --row-accent: var(--good); }
#gastos-table tr[data-estado="rechazado"] { --row-accent: var(--bad); }
.my-requests tbody tr td:first-child { box-shadow: inset 3px 0 0 var(--row-accent, transparent); }
.my-requests tr[data-estado="pendiente"] { --row-accent: var(--warn); }
.my-requests tr[data-estado="aprobado"] { --row-accent: var(--primary); }
.my-requests tr[data-estado="pagado"] { --row-accent: var(--good); }
.my-requests tr[data-estado="rechazado"] { --row-accent: var(--bad); }

.cell-main { font-weight: 500; color: var(--ink); overflow-wrap: break-word; }
a.cell-main { color: var(--primary); }
.cell-sub { font-size: var(--fs-xs); line-height: 1.4; color: var(--ink-3); overflow-wrap: anywhere; }
.cell-sub.mono { font-family: var(--mono); font-size: var(--fs-xs); }
.cell-sub.trunc { max-width: 170px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cell-sub.note { margin-top: 5px; max-width: 240px; white-space: normal; }
.cell.monto { font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }
.clamp { display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; font-size: var(--fs-sm); color: var(--ink-2); max-width: 300px; }
.doc-links { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; margin-top: 6px; }
.doc-links:first-child { margin-top: 0; }
.row-actions.stack { flex-direction: column; align-items: stretch; gap: 6px; min-width: 112px; }
.row-actions.stack .btn { width: 100%; justify-content: center; }
.nv-check { max-width: 240px; }
#gastos-table td[data-label="Solicitud"] { min-width: 130px; }
#gastos-table td[data-label="Proveedor"] { min-width: 170px; }
#gastos-table td[data-label="Asignación"] { min-width: 160px; }
#gastos-table td[data-label="Detalle"] { min-width: 150px; }
#gastos-table td[data-label="Estado"] { min-width: 130px; }
@media (min-width: 641px) and (max-width: 1180px) { .cell-sub.trunc { display: none; } }
@media (max-width: 640px) {
  /* The phone rule that gives each button a 120px basis would make the stacked ones 120px tall. */
  .cards-table .row-actions.stack form, .cards-table .row-actions.stack .btn { flex: 0 0 auto; }
  .cards-table .row-actions.stack { min-width: 0; }
  #gastos-table td[data-label] { min-width: 0; }
}
.my-requests td[data-label="Fecha"] .cell { white-space: nowrap; }
.area-chart { margin-bottom: 18px; padding: 14px 16px; border: 1px solid var(--border); border-radius: 10px; background: var(--panel); }
.area-chart .panel-title { font-size: var(--fs-xs); font-weight: 600; letter-spacing: .04em; text-transform: uppercase; color: var(--ink-3); margin-bottom: 10px; }
#gastos-table td[data-label="Comentarios"] { min-width: 130px; }

/* "Mostrando 100 de 813": the notas de venta table draws a page at a time */
.nv-more { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; padding: 12px 4px 4px; color: var(--ink-3); font-size: var(--fs-sm); }
.nv-more:empty { display: none; }

/* ---------- Pantallas de alertas: reglas que compartían varias plantillas ---------- */
.tabla-alertas td.peq { font-size: var(--fs-sm); color: var(--ink-2); }
.tabla-alertas td.num, .tabla-alertas th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.fila-acciones { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin: 12px 0; }
.caso { background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 14px 16px; box-shadow: var(--shadow); margin-bottom: 10px; }
.caso h2 { margin: 0 0 2px; font-size: var(--fs-md); }
.caso .ayuda { color: var(--ink-3); font-size: var(--fs-sm); margin: 0 0 10px; }
