:root {
    --bg: #f4f5fb; --surface: #ffffff; --text: #1e2235; --muted: #6b7189;
    --border: #e3e5f0; --primary: #4f46e5; --primary-d: #4338ca; --primary-soft: #eef0ff;
    --ok: #15803d; --ok-soft: #e7f6ec; --warn: #b45309; --warn-soft: #fff4e0;
    --danger: #dc2626; --danger-soft: #fdecec;
    --c1: #4f46e5; --c1s: #eceeff; --c2: #0f766e; --c2s: #e3f6f3;
    --c3: #b45309; --c3s: #fff1de; --c4: #be185d; --c4s: #fde8f1;
    --radius: 14px; --shadow: 0 1px 2px rgba(20,24,50,.06), 0 6px 20px rgba(20,24,50,.06);
}
@media (prefers-color-scheme: dark) {
    :root {
        --bg: #0f1120; --surface: #181b30; --text: #eceefb; --muted: #9aa0bd;
        --border: #2a2e4a; --primary: #7c78ff; --primary-d: #928eff; --primary-soft: #23264a;
        --ok: #4ade80; --ok-soft: #12301e; --warn: #fbbf24; --warn-soft: #33270d;
        --danger: #f87171; --danger-soft: #3a1717;
        --c1: #a5a1ff; --c1s: #26295a; --c2: #5eead4; --c2s: #123530;
        --c3: #fbbf24; --c3s: #3a2a0e; --c4: #f9a8d4; --c4s: #3d1830;
        --shadow: 0 1px 2px rgba(0,0,0,.4), 0 6px 20px rgba(0,0,0,.35);
    }
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
    margin: 0; background: var(--bg); color: var(--text);
    font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
h1, h2 { margin: 0; line-height: 1.2; }
.muted { color: var(--muted); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
[hidden] { display: none !important; }

/* ---------- Barra superior ---------- */
.topbar { background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 20; }
.topbar-in { max-width: 1120px; margin: 0 auto; padding: 12px 16px; display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand h1 { font-size: 1.05rem; }
.logo-mark { width: 38px; height: 38px; flex: none; display: grid; place-items: center; border-radius: 11px; background: var(--primary); color: #fff; font-size: 20px; }
.user { display: flex; align-items: center; gap: 8px; }
.user form { margin: 0; }
.user-name { font-weight: 600; max-width: 140px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.container { max-width: 1120px; margin: 0 auto; padding: 18px 16px 40px; }

/* ---------- Estadísticas ---------- */
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 16px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.stat-label { display: block; color: var(--muted); font-size: .8rem; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.stat-value { font-size: 1.8rem; line-height: 1.2; }
.stat-ok .stat-value { color: var(--ok); }
.stat-warn .stat-value { color: var(--warn); }
.stat-debe .stat-value { color: var(--danger); }
.stat-abono .stat-value { color: var(--warn); }
.stat-money .stat-value { color: var(--primary); font-size: 1.5rem; line-height: 1.5; overflow-wrap: anywhere; }
.bar { height: 6px; border-radius: 99px; background: var(--border); margin-top: 6px; overflow: hidden; }
.bar-fill { height: 100%; width: 0; background: var(--primary); border-radius: 99px; transition: width .4s ease; }

/* ---------- Barra de herramientas ---------- */
.toolbar { display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 14px; align-items: center; }
.search { flex: 1 1 260px; display: flex; align-items: center; gap: 8px; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 0 12px; color: var(--muted); }
.search:focus-within { border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
.search input { flex: 1; border: 0; outline: 0; background: transparent; color: var(--text); font: inherit; padding: 11px 0; min-width: 0; }
.segmented { display: inline-flex; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 3px; }
.seg { border: 0; background: transparent; color: var(--muted); font: inherit; font-weight: 600; padding: 8px 12px; border-radius: 9px; cursor: pointer; }
.seg.is-active { background: var(--primary); color: #fff; }

/* ---------- Botones ---------- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; font: inherit; font-weight: 600; padding: 10px 16px; border-radius: 11px; border: 1px solid transparent; cursor: pointer; text-decoration: none; transition: background .15s, border-color .15s; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-d); }
.btn-outline { background: var(--surface); border-color: var(--border); color: var(--text); }
.btn-outline:hover:not(:disabled) { border-color: var(--primary); color: var(--primary); }
.btn-ghost { background: transparent; color: var(--muted); }
.btn-ghost:hover { background: var(--primary-soft); color: var(--text); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger-ghost { background: transparent; color: var(--danger); }
.btn-danger-ghost:hover { background: var(--danger-soft); }
.btn-sm { padding: 7px 12px; font-size: .88rem; }
.btn-block { width: 100%; }
:focus-visible { outline: 3px solid var(--primary-soft); outline-offset: 1px; }
.btn:focus-visible, .seg:focus-visible { outline-color: var(--primary); }

/* ---------- Tabla ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; transition: opacity .15s; }
.card.cargando { opacity: .55; }
.tabla { width: 100%; border-collapse: collapse; }
.tabla th { text-align: left; font-size: .76rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); padding: 12px 14px; background: var(--bg); border-bottom: 1px solid var(--border); }
.tabla td { padding: 11px 14px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.tabla tbody tr:last-child td { border-bottom: 0; }
.tabla tbody tr:hover { background: var(--primary-soft); }
.fila-num { font-weight: 700; color: var(--muted); font-variant-numeric: tabular-nums; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { font: 600 .9rem/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; padding: 6px 9px; border-radius: 8px; letter-spacing: .03em; border: 1px solid transparent; }
.chip.c1 { color: var(--c1); background: var(--c1s); }
.chip.c2 { color: var(--c2); background: var(--c2s); }
.chip.c3 { color: var(--c3); background: var(--c3s); }
.chip.c4 { color: var(--c4); background: var(--c4s); }
.chip.hit { border-color: currentColor; box-shadow: 0 0 0 2px var(--primary-soft); }
.nombre { font-weight: 600; }
.sub { display: block; font-size: .78rem; color: var(--muted); font-weight: 400; }
.tel a { color: inherit; text-decoration: none; font-variant-numeric: tabular-nums; }
.tel a:hover { color: var(--primary); text-decoration: underline; }
.dash { color: var(--muted); }
.badge { display: inline-block; font-size: .78rem; font-weight: 700; padding: 3px 10px; border-radius: 99px; }
.badge-ok { color: var(--ok); background: var(--ok-soft); }
.badge-warn { color: var(--warn); background: var(--warn-soft); }
.select-wrap select { font: inherit; font-weight: 600; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 11px 12px; cursor: pointer; }
.select-wrap select:focus { outline: 0; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
.badge-debe { color: var(--danger); background: var(--danger-soft); }
.badge-abono { color: var(--warn); background: var(--warn-soft); }
.badge-pago { color: var(--ok); background: var(--ok-soft); }
.col-acc { text-align: right; white-space: nowrap; }
.empty { padding: 40px 16px; text-align: center; }

.pager { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-top: 14px; }
.pager .muted { text-align: center; font-size: .9rem; }

/* ---------- Diálogos ---------- */
dialog { border: 0; padding: 0; border-radius: 18px; width: min(460px, calc(100vw - 24px)); background: var(--surface); color: var(--text); box-shadow: 0 20px 60px rgba(0,0,0,.35); }
dialog::backdrop { background: rgba(10, 12, 28, .55); backdrop-filter: blur(2px); }
.dlg-body { padding: 22px; display: grid; gap: 14px; }
.dlg-body h2 { font-size: 1.25rem; }
.dlg-actions { display: flex; align-items: center; gap: 8px; margin-top: 4px; }
.spacer { flex: 1; }

.field { display: grid; gap: 6px; }
.field > span { font-weight: 600; font-size: .88rem; }
.field input { font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--border); border-radius: 11px; padding: 11px 13px; width: 100%; }
.field input:focus { outline: 0; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
.field select { font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--border); border-radius: 11px; padding: 11px 13px; width: 100%; }
.field select:focus { outline: 0; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
.field input.invalid { border-color: var(--danger); }
.field-error { color: var(--danger); min-height: 0; }
.alert { background: var(--danger-soft); color: var(--danger); border-radius: 11px; padding: 10px 13px; font-weight: 600; font-size: .9rem; }

.toast { position: fixed; left: 50%; bottom: 22px; transform: translateX(-50%); background: #1e2235; color: #fff; padding: 11px 18px; border-radius: 12px; box-shadow: var(--shadow); z-index: 60; font-weight: 600; max-width: calc(100vw - 32px); }
.toast.err { background: var(--danger); }

/* ---------- Login ---------- */
.login-body { display: grid; place-items: center; min-height: 100vh; padding: 16px; }
.login-card { width: min(400px, 100%); background: var(--surface); border: 1px solid var(--border); border-radius: 20px; padding: 28px 24px; box-shadow: var(--shadow); display: grid; gap: 14px; }
.login-card h1 { font-size: 1.3rem; }
.login-card p { margin: 0; }
.login-card form { display: grid; gap: 14px; }
.login-card .logo-mark { width: 48px; height: 48px; font-size: 24px; }

/* ---------- Tablet ---------- */
@media (max-width: 900px) {
    .tabla th, .tabla td { padding: 10px 10px; }
}

/* ---------- Celular: filas como tarjetas ---------- */
@media (max-width: 720px) {
    .stats { grid-template-columns: repeat(2, 1fr); gap: 10px; }
    .stat-value { font-size: 1.5rem; }
    .stat-money .stat-value { font-size: 1.2rem; }
    .brand small { display: none; }
    .segmented { width: 100%; }
    .select-wrap { width: 100%; }
    .select-wrap select { width: 100%; }
    .seg { flex: 1; padding: 9px 4px; }
    .toolbar .btn { width: 100%; }
    .card { background: transparent; border: 0; box-shadow: none; overflow: visible; }
    .tabla, .tabla tbody { display: block; }
    .tabla thead { display: none; }
    .tabla tr { display: grid; grid-template-columns: 1fr auto; gap: 8px 10px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px; margin-bottom: 10px; }
    .tabla td { display: block; padding: 0; border: 0; }
    .tabla tbody tr:hover { background: var(--surface); }
    .c-fila { grid-column: 1; grid-row: 1; }
    .c-estado { grid-column: 2; grid-row: 1; justify-self: end; }
    .c-nums { grid-column: 1 / -1; grid-row: 2; }
    .c-nombre { grid-column: 1; grid-row: 3; }
    .c-pago { grid-column: 2; grid-row: 3; justify-self: end; align-self: center; }
    .c-tel { grid-column: 1; grid-row: 4; align-self: center; }
    .c-acc { grid-column: 2; grid-row: 4; }
    .c-fila .fila-num::before { content: "Fila "; font-weight: 600; }
    .pager .btn { padding: 10px 12px; }
    .user-name { display: none; }
}
@media (max-width: 380px) { .chip { font-size: .82rem; padding: 6px 7px; } }
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
