:root {
  color-scheme: light;
  --bg: #f5f5f3;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --border: #e3e2de;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #8a8983;
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --grid: #ebeae6;
  --series-1: #2a78d6;  /* ingresos / Atom */
  --series-2: #eb6834;  /* egresos / sin cliente */
  --series-3: #1baf7a;  /* Nexus */
  --pos: #2a78d6;
  --neg: #e34948;
  --good: #0ca30c;
  --good-ink: #006300;
  --warning: #fab219;
  --critical: #d03b3b;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 2px 8px rgba(0,0,0,.04);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121211; --surface: #1a1a19; --surface-2: #242422; --border: #33332f;
    --text: #ffffff; --text-2: #c3c2b7; --muted: #8f8e86; --accent: #3987e5; --grid: #2a2a27;
    --series-1: #3987e5; --series-2: #d95926; --series-3: #199e70; --pos: #3987e5; --neg: #e66767;
    --good-ink: #4cc24c; --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121211; --surface: #1a1a19; --surface-2: #242422; --border: #33332f;
  --text: #ffffff; --text-2: #c3c2b7; --muted: #8f8e86; --accent: #3987e5; --grid: #2a2a27;
  --series-1: #3987e5; --series-2: #d95926; --series-3: #199e70; --pos: #3987e5; --neg: #e66767;
  --good-ink: #4cc24c; --shadow: none;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
.num { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.muted { color: var(--muted); }
.hidden { display: none !important; }

/* Layout */
.app { display: grid; grid-template-columns: 232px 1fr; min-height: 100vh; }
.sidebar {
  background: var(--surface); border-right: 1px solid var(--border);
  padding: 20px 12px; position: sticky; top: 0; height: 100vh; overflow-y: auto;
  display: flex; flex-direction: column; gap: 2px;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 10px 18px; font-weight: 700; font-size: 16px; }
.brand-mark { width: 28px; height: 28px; border-radius: 8px; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; font-size: 14px; }
.nav-group { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); padding: 14px 10px 4px; }
.nav a {
  display: block; padding: 7px 10px; border-radius: 8px; color: var(--text-2); text-decoration: none;
}
.nav a:hover { background: var(--surface-2); color: var(--text); }
.nav a.active { background: var(--surface-2); color: var(--text); font-weight: 600; }
.sidebar-foot { margin-top: auto; padding: 12px 10px 0; border-top: 1px solid var(--border); font-size: 13px; }
.sidebar-foot .role { display: inline-block; font-size: 11px; padding: 1px 7px; border-radius: 99px; background: var(--surface-2); color: var(--text-2); margin-left: 4px; }
.sidebar-foot .links { display: flex; gap: 12px; margin-top: 8px; }
.linkbtn { background: none; border: 0; padding: 0; color: var(--accent); cursor: pointer; }

main { padding: 24px 28px 48px; min-width: 0; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 18px; }
.page-head h1 { font-size: 22px; margin: 0; }
.page-head p { margin: 2px 0 0; color: var(--text-2); }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 14px; }
.toolbar .grow { flex: 1; }

/* Controls */
.btn {
  border: 1px solid var(--border); background: var(--surface); padding: 7px 12px; border-radius: 8px; cursor: pointer;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.07); }
.btn.danger { color: var(--critical); }
.btn.sm { padding: 3px 8px; font-size: 13px; }
input, select, textarea {
  border: 1px solid var(--border); background: var(--surface); padding: 7px 10px; border-radius: 8px; min-width: 0;
}
input:focus, select:focus, textarea:focus, .btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; background: var(--surface); }
.seg button { border: 0; background: none; padding: 6px 11px; cursor: pointer; color: var(--text-2); }
.seg button.on { background: var(--surface-2); color: var(--text); font-weight: 600; }

/* Cards / KPIs */
.grid { display: grid; gap: 14px; }
.kpis { grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); margin-bottom: 14px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 16px; box-shadow: var(--shadow); min-width: 0; }
.card h2 { font-size: 14px; margin: 0 0 2px; }
.card .sub { color: var(--muted); font-size: 12px; margin-bottom: 10px; }
.kpi .label { color: var(--text-2); font-size: 13px; }
.kpi .value { font-size: clamp(18px, 1.6vw, 24px); font-weight: 700; margin: 4px 0 2px; font-variant-numeric: tabular-nums; letter-spacing: -.01em; }
.kpi .delta { font-size: 12px; color: var(--text-2); }
.delta .up::before { content: "▲ "; } .delta .down::before { content: "▼ "; }
.delta .good { color: var(--good-ink); } .delta .bad { color: var(--critical); }
.charts { grid-template-columns: repeat(12, 1fr); }
.span-12 { grid-column: span 12; } .span-8 { grid-column: span 8; } .span-7 { grid-column: span 7; }
.span-6 { grid-column: span 6; } .span-5 { grid-column: span 5; } .span-4 { grid-column: span 4; }
.chart-box { position: relative; height: 280px; }
.chart-box.tall { height: 340px; }
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 12px; color: var(--text-2); margin-bottom: 8px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }
.card-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }

/* Tables */
.table-wrap { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; overflow: auto; box-shadow: var(--shadow); }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 9px 12px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; }
th { font-size: 12px; font-weight: 600; color: var(--text-2); background: var(--surface-2); position: sticky; top: 0; white-space: nowrap; }
tbody tr:hover { background: var(--surface-2); }
tbody tr:last-child td { border-bottom: 0; }
td.actions { white-space: nowrap; text-align: right; }
tfoot td { font-weight: 700; background: var(--surface-2); }
.empty { padding: 36px; text-align: center; color: var(--muted); }
.card table th { background: none; position: static; }
.card table td, .card table th { padding: 7px 4px; }

.badge { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; padding: 1px 8px; border-radius: 99px; background: var(--surface-2); color: var(--text-2); white-space: nowrap; }
.badge::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: var(--muted); }
.badge.pagada::before, .badge.ingreso::before, .badge.cobro::before, .badge.ok::before { background: var(--good); }
.badge.pendiente::before, .badge.parcial::before { background: var(--warning); }
.badge.vencida::before, .badge.egreso::before, .badge.pago::before { background: var(--critical); }
.badge.Atom::before { background: var(--series-1); }
.badge.Nexus::before { background: var(--series-3); }
.badge.anulada { text-decoration: line-through; }
.amount-in { color: var(--good-ink); }

/* Modal */
dialog { border: 1px solid var(--border); border-radius: 14px; padding: 0; background: var(--surface); color: var(--text); width: min(680px, calc(100vw - 32px)); box-shadow: 0 20px 60px rgba(0,0,0,.25); }
dialog::backdrop { background: rgba(0,0,0,.35); }
.modal-head { padding: 16px 20px; border-bottom: 1px solid var(--border); display: flex; justify-content: space-between; align-items: center; }
.modal-head h3 { margin: 0; font-size: 17px; }
.modal-body { padding: 16px 20px; max-height: 70vh; overflow-y: auto; }
.modal-foot { padding: 12px 20px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 8px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px 14px; }
.field { display: flex; flex-direction: column; gap: 4px; }
.field.full { grid-column: 1 / -1; }
.field label { font-size: 12px; font-weight: 600; color: var(--text-2); }
.field .hint { font-size: 12px; color: var(--muted); }
.checkbox { flex-direction: row; align-items: center; gap: 8px; }

/* Adjuntos */
.dropzone { display: block; border: 2px dashed var(--border); border-radius: 12px; padding: 22px; text-align: center; cursor: pointer; color: var(--text-2); }
.dropzone:hover, .dropzone.over { border-color: var(--accent); background: var(--surface-2); }
a.btn { text-decoration: none; color: inherit; display: inline-block; }


/* Comprobantes PDF */
dialog.wide { width: min(1040px, calc(100vw - 32px)); }
.comp-card { border: 1px solid var(--border); border-radius: 12px; padding: 12px 14px; margin-top: 12px; }
.comp-card.off { opacity: .55; }
.comp-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 6px; flex-wrap: wrap; }
.comp-head strong { word-break: break-all; }
.comp-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); margin-top: 8px; }
.comp-grid .span2 { grid-column: span 2; }
.warn-box { border: 1px solid var(--warning); border-radius: 8px; padding: 8px 10px; font-size: 13px; margin: 6px 0; background: var(--surface-2); }
@media (max-width: 820px) {
  .comp-grid { grid-template-columns: 1fr 1fr; }
}
@media (max-width: 480px) {
  .comp-grid { grid-template-columns: 1fr; }
  .comp-grid .span2 { grid-column: auto; }
}

/* Login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login .card { width: min(380px, 100%); padding: 28px; }
.login h1 { font-size: 20px; margin: 12px 0 4px; }
.login form { display: grid; gap: 12px; margin-top: 18px; }
.error { color: var(--critical); font-size: 13px; min-height: 18px; }

/* Toast */
#toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; z-index: 1000; opacity: 0; transition: opacity .2s; pointer-events: none; max-width: calc(100vw - 32px); }
#toast.show { opacity: 1; }

/* Estado de resultados */
.er td:first-child, .er th:first-child { position: sticky; left: 0; background: var(--surface); z-index: 1; min-width: 200px; }
.er th:first-child { background: var(--surface-2); }
.er tr.section td { font-weight: 700; background: var(--surface-2); }
.er tr.total td { font-weight: 700; border-top: 2px solid var(--border); }
.neg { color: var(--critical); }

.menu-btn { display: none; }

@media (max-width: 1100px) {
  .span-8, .span-7, .span-6, .span-5, .span-4 { grid-column: span 12; }
}
@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: 260px; z-index: 50; transform: translateX(-100%); transition: transform .2s; }
  .sidebar.open { transform: none; box-shadow: 0 0 40px rgba(0,0,0,.3); }
  .menu-btn { display: inline-block; margin-bottom: 12px; }
  main { padding: 16px; }
  .form-grid { grid-template-columns: 1fr; }
}
@media (max-width: 480px) {
  .kpis { grid-template-columns: 1fr 1fr; }
  .kpi .value { font-size: 17px; }
  .page-head h1 { font-size: 20px; }
}
@media print {
  .sidebar, .toolbar, .menu-btn, .no-print { display: none !important; }
  .app { display: block; } main { padding: 0; }
  .card, .table-wrap { box-shadow: none; break-inside: avoid; }
}
