:root {
    --bg: #f5f7fb;
    --surface: #ffffff;
    --surface-soft: #f8fafc;
    --surface-blue: #eff6ff;
    --text: #0f172a;
    --muted: #64748b;
    --border: #e2e8f0;
    --border-strong: #cbd5e1;
    --primary: #2563eb;
    --primary-dark: #1d4ed8;
    --success: #16a34a;
    --danger: #dc2626;
    --warning: #d97706;
    --shadow: 0 16px 40px rgba(15, 23, 42, .08);
    --shadow-soft: 0 8px 22px rgba(15, 23, 42, .06);
    --radius: 18px;
    --radius-sm: 12px;
    --sidebar-width: 280px;
}

* { box-sizing: border-box; }

html { min-height: 100%; }

body {
    margin: 0;
    min-height: 100%;
    font-family: Arial, Helvetica, sans-serif;
    background: var(--bg);
    color: var(--text);
    font-size: 15px;
    line-height: 1.45;
}

body.theme-dark {
    --bg: #0b1220;
    --surface: #111827;
    --surface-soft: #172033;
    --surface-blue: #1b2a45;
    --text: #e5eefc;
    --muted: #93a4bf;
    --border: #233048;
    --border-strong: #33425f;
    --primary: #3b82f6;
    --primary-dark: #2563eb;
    --success: #22c55e;
    --danger: #f87171;
    --warning: #f59e0b;
    --shadow: 0 16px 40px rgba(0, 0, 0, .28);
    --shadow-soft: 0 8px 22px rgba(0, 0, 0, .22);
}

a { color: inherit; }

.app { min-height: 100vh; }

.sidebar {
    width: var(--sidebar-width);
    min-height: 100vh;
    padding: 18px 14px;
    background: rgba(255, 255, 255, .96);
    border-right: 1px solid var(--border);
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 30;
    overflow-y: auto;
    box-shadow: 8px 0 30px rgba(15, 23, 42, .04);
    transition: transform .22s ease;
}

body.theme-dark .sidebar {
    background: rgba(12, 19, 32, .96);
    box-shadow: 8px 0 30px rgba(0, 0, 0, .22);
}

.brand {
    display: flex;
    gap: 12px;
    align-items: center;
    padding: 8px 8px 18px;
    margin-bottom: 8px;
    border-bottom: 1px solid var(--border);
}

.sidebar-close {
    display: none;
    margin-left: auto;
    width: 38px;
    height: 38px;
    border: 1px solid var(--border);
    border-radius: 12px;
    background: #fff;
    color: var(--text);
    cursor: pointer;
    font-weight: 800;
}

.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, .45);
    z-index: 25;
}

.logo {
    width: 44px;
    height: 44px;
    border-radius: 14px;
    background: var(--primary);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 800;
    font-size: 22px;
    box-shadow: 0 12px 24px rgba(37, 99, 235, .22);
}

.brand strong { display: block; font-size: 15px; }
.brand span { display: block; color: var(--muted); font-size: 12px; margin-top: 3px; }

.sidebar nav { display: flex; flex-direction: column; gap: 16px; padding: 8px 0; }
.nav-group { display: flex; flex-direction: column; gap: 4px; }
.nav-label {
    padding: 8px 12px 4px;
    color: var(--muted);
    font-size: 11px;
    font-weight: 800;
    letter-spacing: .08em;
    text-transform: uppercase;
}

.sidebar nav a {
    display: flex;
    align-items: center;
    gap: 10px;
    min-height: 40px;
    padding: 9px 11px;
    border-radius: 999px;
    text-decoration: none;
    color: #334155;
    font-weight: 700;
    transition: background .16s ease, color .16s ease, box-shadow .16s ease;
}

.nav-icon {
    width: 28px;
    height: 28px;
    border-radius: 10px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: var(--surface-soft);
    color: var(--muted);
    font-size: 12px;
    font-weight: 800;
    flex: 0 0 auto;
}

.icon-svg {
    width: 16px;
    height: 16px;
    stroke: currentColor;
    fill: none;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
    flex: 0 0 auto;
}

.nav-icon .icon-svg {
    width: 16px;
    height: 16px;
}

.sidebar nav a:hover {
    background: var(--surface-blue);
    color: var(--primary-dark);
}

.sidebar nav a.active {
    background: #dbeafe;
    color: var(--primary-dark);
    box-shadow: inset 0 0 0 1px #bfdbfe;
}

.sidebar nav a.active .nav-icon {
    background: var(--primary);
    color: #fff;
}

.sidebar-footer {
    margin-top: 18px;
    padding: 16px 8px 8px;
    border-top: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.sidebar-logout { width: 100%; }

.theme-switcher {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 10px 12px;
    border-radius: 18px;
    background: #ffffff;
    border: 1px solid #dbe4f0;
    box-shadow: 0 10px 22px rgba(15, 23, 42, .06);
}

.theme-switcher-label {
    color: var(--text);
    font-size: 14px;
    font-weight: 800;
    letter-spacing: 0;
    text-transform: none;
    padding: 0;
    flex: 0 1 auto;
    line-height: 1.1;
}

.theme-switcher-buttons {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 4px;
    border-radius: 999px;
    background: linear-gradient(180deg, #e6eaf3, #d9e1ee);
    border: 1px solid #d4ddeb;
    box-shadow: inset 0 1px 0 rgba(255,255,255,.92);
    flex: 0 0 auto;
}

.theme-btn {
    min-width: 58px;
    min-height: 32px;
    padding: 5px 14px;
    border-radius: 999px;
    border: 0;
    background: transparent;
    color: #334155;
    font-weight: 800;
    font-size: 12px;
    cursor: pointer;
    box-shadow: none;
    transition: background .16s ease, color .16s ease, transform .12s ease, box-shadow .16s ease;
}

.theme-btn:hover {
    transform: translateY(-1px);
}

.theme-btn.active {
    background: #ffffff;
    color: #334155;
    box-shadow: 0 2px 6px rgba(15, 23, 42, .12), inset 0 1px 0 rgba(255,255,255,.95);
}

body.theme-dark .theme-switcher {
    background: #111827;
    border-color: #25324a;
    box-shadow: 0 14px 30px rgba(0, 0, 0, .22);
}

body.theme-dark .theme-switcher-buttons {
    background: linear-gradient(180deg, #2a364c, #1d2637);
    border-color: #34435f;
    box-shadow: inset 0 1px 0 rgba(255,255,255,.04);
}

body.theme-dark .theme-switcher-label {
    color: var(--text);
}

body.theme-dark .theme-btn {
    color: #cbd5e1;
}

body.theme-dark .theme-btn.active {
    background: #f8fafc;
    color: #1e293b;
    box-shadow: 0 2px 8px rgba(0, 0, 0, .3);
}

@media (max-width: 900px) {
    .theme-switcher {
        padding: 10px 10px 10px 12px;
        border-radius: 16px;
    }

    .theme-switcher-buttons {
        gap: 4px;
        padding: 3px;
    }

    .theme-btn {
        min-width: 54px;
        min-height: 32px;
        padding: 5px 12px;
    }

    .theme-switcher-label {
        font-size: 13px;
    }
}

.main {
    min-width: 0;
    margin-left: var(--sidebar-width);
}

.topbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 16px;
    min-height: 72px;
    padding: 14px 24px;
    background: rgba(255, 255, 255, .9);
    backdrop-filter: blur(12px);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 20;
}

body.theme-dark .topbar {
    background: rgba(14, 20, 34, .92);
}

body.theme-dark .alert-bell {
    background: #0f172a;
}

body.theme-dark .alert-bell.has-alerts {
    background: rgba(127, 29, 29, .28);
    border-color: rgba(248, 113, 113, .35);
    color: #fca5a5;
}

body.theme-dark .alerts-item.pending,
body.theme-dark .alerts-toast {
    background: rgba(127, 29, 29, .14);
    border-color: rgba(248, 113, 113, .22);
}

body.theme-dark .alerts-dropdown {
    background: var(--surface);
}

.topbar-title { min-width: 0; }
.topbar-title strong { display: block; font-size: 22px; line-height: 1.15; }
.topbar-title small { display: block; color: var(--muted); margin-top: 4px; }
.topbar-actions { display: flex; align-items: center; gap: 10px; }

.alerts-menu { position: relative; }

.alert-bell {
    position: relative;
    width: 42px;
    height: 42px;
    border-radius: 14px;
    border: 1px solid var(--border);
    background: #fff;
    color: var(--text);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: var(--shadow-soft);
}

.alert-bell .icon-svg {
    width: 18px;
    height: 18px;
}

.alert-bell.has-alerts {
    background: #fff1f2;
    border-color: #fecdd3;
    color: #b91c1c;
}

.alert-bell-count {
    position: absolute;
    top: -5px;
    right: -5px;
    min-width: 20px;
    height: 20px;
    padding: 0 5px;
    border-radius: 999px;
    background: #ef4444;
    color: #fff;
    font-size: 11px;
    font-weight: 800;
    display: none;
    align-items: center;
    justify-content: center;
}

.alert-bell.has-alerts .alert-bell-count {
    display: inline-flex;
}

.alerts-dropdown {
    position: absolute;
    top: calc(100% + 10px);
    right: 0;
    width: min(390px, calc(100vw - 32px));
    max-height: min(70vh, 620px);
    overflow: hidden;
    display: none;
    background: #fffded;
    border: 1px solid var(--border);
    border-radius: 18px;
    box-shadow: var(--shadow);
    z-index: 70;
}

.alerts-menu.open .alerts-dropdown {
    display: block;
}

.alerts-head,
.alerts-footer {
    padding: 14px 16px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
}

.alerts-footer {
    border-top: 1px solid var(--border);
    border-bottom: 0;
    justify-content: flex-end;
}

.alerts-section {
    padding: 12px 16px 0;
}

.alerts-section-title {
    font-size: 12px;
    font-weight: 800;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: .04em;
    margin-bottom: 8px;
}

.alerts-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
    max-height: 220px;
    overflow-y: auto;
    padding-bottom: 12px;
}

.alerts-history {
    max-height: 210px;
}

.alerts-item {
    padding: 11px 12px;
    border: 1px solid var(--border);
    border-radius: 14px;
    background: var(--surface-soft);
}

.alerts-item.pending {
    border-color: #fecdd3;
    background: #fff7f8;
}

.alerts-item-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    margin-bottom: 4px;
}

.alerts-item-title {
    font-weight: 800;
}

.alerts-item-time {
    color: var(--muted);
    font-size: 12px;
    white-space: nowrap;
}

.alerts-item p {
    margin: 0 0 8px;
}

.alerts-item-actions {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
}

.alerts-empty {
    padding: 12px;
    border: 1px dashed var(--border);
    border-radius: 14px;
    color: var(--muted);
    background: var(--surface-soft);
}

.alerts-toast-wrap {
    position: fixed;
    right: 18px;
    bottom: 18px;
    width: min(360px, calc(100vw - 36px));
    display: flex;
    flex-direction: column;
    gap: 10px;
    z-index: 90;
}

.alerts-toast {
    padding: 14px 14px 12px;
    border-radius: 16px;
    border: 1px solid #fecdd3;
    background: #fff7f8;
    box-shadow: var(--shadow);
}

.alerts-toast strong {
    display: block;
    margin-bottom: 4px;
}

.alerts-toast small {
    display: block;
    margin-top: 8px;
    color: var(--muted);
}

.menu {
    display: none;
    border: 1px solid var(--border);
    background: #fff;
    color: var(--text);
    width: 42px;
    height: 42px;
    border-radius: 14px;
    cursor: pointer;
    font-size: 20px;
    box-shadow: var(--shadow-soft);
}

.content { padding: 24px; }

.panel,
.hero,
.kpi,
.summary-chip {
    background: var(--surface);
    border: 1px solid var(--border);
    box-shadow: var(--shadow-soft);
    border-radius: var(--radius);
}

.panel { padding: 20px; margin-bottom: 18px; }

.hero {
    padding: 24px;
    display: flex;
    justify-content: space-between;
    gap: 16px;
    align-items: center;
    flex-wrap: wrap;
    margin-bottom: 18px;
    background: #0f172a;
    color: #fff;
    border-color: rgba(255, 255, 255, .1);
}

.hero h2 { margin: 0 0 8px; font-size: 30px; letter-spacing: 0; }
.hero p { margin: 0; color: rgba(255, 255, 255, .78); }

.kpis { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 16px; margin-bottom: 18px; }
.kpi { padding: 20px; }
.kpi.k1 { border-left: 5px solid var(--success); }
.kpi.k2 { border-left: 5px solid var(--danger); }
.kpi.k3 { border-left: 5px solid var(--warning); }
.kpi.k4 { border-left: 5px solid var(--primary); }
.kpi span { display: block; color: var(--muted); margin-bottom: 8px; font-weight: 700; }
.kpi strong { font-size: 28px; }

.section-head {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 14px;
    flex-wrap: wrap;
    margin-bottom: 16px;
}

.section-head h2,
.section-head h3 { margin: 0 0 6px; letter-spacing: 0; }
.section-head p { margin: 0; color: var(--muted); }

.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.form-grid.three { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.form-grid.line { grid-template-columns: 2fr 1fr 2fr 1.5fr 1.5fr 1.2fr 1fr auto auto; }
.field { display: flex; flex-direction: column; gap: 7px; }
.full { grid-column: 1 / -1; }

label {
    font-weight: 800;
    font-size: 13px;
    color: #334155;
}

input,
select,
textarea {
    width: 100%;
    min-height: 46px;
    border-radius: 12px;
    border: 1px solid var(--border-strong);
    padding: 10px 12px;
    font-size: 15px;
    background: #fff;
    color: var(--text);
    outline: none;
    transition: border .15s ease, box-shadow .15s ease;
}

textarea { min-height: 92px; resize: vertical; }
input:focus, select:focus, textarea:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, .14);
}

body.theme-dark input,
body.theme-dark select,
body.theme-dark textarea {
    background: #0f172a;
    color: var(--text);
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    border-radius: 12px;
    min-height: 42px;
    padding: 10px 15px;
    text-decoration: none;
    cursor: pointer;
    font-weight: 800;
    border: 1px solid transparent;
    white-space: nowrap;
    transition: transform .12s ease, box-shadow .12s ease, background .12s ease;
}

.btn:hover { transform: translateY(-1px); box-shadow: var(--shadow-soft); }
.btn-small { min-height: 34px; padding: 6px 10px; font-size: 13px; border-radius: 10px; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-light,
.btn-secondary { background: #fff; border-color: var(--border); color: var(--text); }
.btn-danger { background: #fff1f2; border-color: #fecdd3; color: var(--danger); }
.btn-success { background: #ecfdf5; border-color: #bbf7d0; color: #166534; }
.btn-icon-only { min-width: 34px; padding-left: 8px; padding-right: 8px; }

body.theme-dark .btn-light,
body.theme-dark .btn-secondary {
    background: #0f172a;
}

body.theme-dark .btn-danger {
    background: rgba(127, 29, 29, .24);
    border-color: rgba(248, 113, 113, .28);
}

body.theme-dark .btn-success {
    background: rgba(20, 83, 45, .28);
    border-color: rgba(34, 197, 94, .28);
    color: #bbf7d0;
}

.table-wrap,
.panel > table.table {
    overflow-x: auto;
    border: 1px solid var(--border);
    border-radius: 14px;
    background: #fff;
}

.panel > table.table {
    display: block;
}

.panel > table.table thead,
.panel > table.table tbody {
    display: table;
    width: 100%;
}

.table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    min-width: 980px;
}

.table th,
.table td {
    padding: 12px 12px;
    border-bottom: 1px solid var(--border);
    text-align: left;
    vertical-align: middle;
}

.table thead th {
    background: var(--surface-soft);
    color: #334155;
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: .04em;
    font-weight: 800;
    position: sticky;
    top: 0;
    z-index: 1;
}

body.theme-dark .table-wrap,
body.theme-dark .panel > table.table {
    background: var(--surface);
}

@media print {
    @page {
        size: A4;
        margin: 8mm;
    }
    body {
        font-size: 11px !important;
    }
    .sidebar,
    .topbar,
    .no-print,
    .sidebar-overlay,
    .alerts-toast-wrap,
    .alerts-menu,
    .menu {
        display: none !important;
    }
    .main {
        margin-left: 0 !important;
    }
    .content {
        padding: 0 !important;
    }
    body,
    .app,
    .main,
    .content {
        background: #fff !important;
        color: #000 !important;
    }
    .panel,
    .hero,
    .kpi,
    .summary-chip {
        box-shadow: none !important;
    }
    .panel {
        padding: 8px !important;
        border: 1px solid #ddd !important;
        break-inside: avoid;
        page-break-inside: avoid;
    }
    .table th,
    .table td {
        padding: 4px 5px !important;
        font-size: 10px !important;
        line-height: 1.2 !important;
        color: #000 !important;
        background: #fff !important;
    }
    .section-head {
        margin-bottom: 8px !important;
    }
    .section-head h2,
    .section-head h3 {
        margin-bottom: 4px !important;
    }
    .section-head p,
    .summary-chip,
    small {
        font-size: 10px !important;
    }
    a {
        color: #000 !important;
        text-decoration: none !important;
    }
}

.table tbody tr:hover { background: #f8fafc; }
.table tbody tr:last-child td { border-bottom: 0; }
.table td:nth-child(n+4) { font-variant-numeric: tabular-nums; }
.table th a,
.table thead th a {
    color: inherit !important;
    text-decoration: none !important;
    font: inherit !important;
    display: inline-flex;
    align-items: center;
    gap: 6px;
}
.table.sortable thead th,
.table thead th.sortable,
.table thead th a {
    cursor: pointer;
}
.table.sortable thead th:hover,
.table thead th.sortable:hover,
.table thead th a:hover {
    color: var(--primary-dark) !important;
}
.table .inline-actions .btn,
td.inline-actions .btn {
    min-width: 34px;
}

.badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 6px 10px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 800;
}

.badge-deuda,
.badge-gasto,
.badge-retirada { background: #fee2e2; color: #991b1b; }
.badge-cobro { background: #dcfce7; color: #166534; }
.badge-ingreso { background: #dbeafe; color: #1d4ed8; }
.badge-pendiente { background: #fef3c7; color: #92400e; }
.badge-validado { background: #dcfce7; color: #166534; }

.positive { color: var(--success); font-weight: 800; }
.negative { color: var(--danger); font-weight: 800; }
.muted { color: var(--muted); }

.flash {
    padding: 12px 14px;
    border-radius: 14px;
    margin-bottom: 14px;
    font-weight: 800;
    border: 1px solid transparent;
}

.flash-success { background: #ecfdf5; color: #166534; border-color: #bbf7d0; }
.flash-error { background: #fff1f2; color: #991b1b; border-color: #fecdd3; }
.flash-info { background: #eff6ff; color: #1d4ed8; border-color: #bfdbfe; }

.filters-bar {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    margin-bottom: 14px;
    align-items: center;
}

.filters-box {
    display: none;
    margin-bottom: 16px;
    padding: 16px;
    background: var(--surface-soft);
    border: 1px solid var(--border);
    border-radius: 16px;
}

.filters-box.active { display: block; }
.inline-actions { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }

.status-cuadrada,
.status-negativa,
.status-positiva {
    padding: 12px 14px;
    border-radius: 14px;
    font-weight: 800;
    border: 1px solid transparent;
}

.status-cuadrada { background: #ecfdf5; color: #166534; border-color: #bbf7d0; }
.status-negativa { background: #fff1f2; color: #991b1b; border-color: #fecdd3; }
.status-positiva { background: #fff7ed; color: #9a3412; border-color: #fed7aa; }

.login-wrap {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
}

.login-card {
    background: #fff;
    box-shadow: var(--shadow);
    border: 1px solid var(--border);
    border-radius: 20px;
    padding: 26px;
    width: min(420px, 100%);
}

.login-card h1 { margin: 0 0 8px; }
.login-card p { margin: 0 0 18px; color: var(--muted); }

.counter-grid { display: grid; grid-template-columns: 2fr 1fr 1fr; gap: 12px; }
.counter-grid .header { font-weight: 800; color: #64748b; }
.search-input { max-width: 320px; }

.summary-bar { display: flex; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.summary-chip { padding: 9px 13px; font-weight: 800; }

.modal,
.modal-backdrop,
.overlay {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, .45);
    z-index: 60;
}

.modal-box,
.modal-content {
    width: min(720px, calc(100% - 32px));
    margin: 8vh auto;
    background: #fff;
    border-radius: 20px;
    border: 1px solid var(--border);
    box-shadow: var(--shadow);
    padding: 20px;
}

@media (max-width: 1100px) {
    .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .form-grid.three,
    .counter-grid,
    .form-grid.line { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 900px) {
    .sidebar { transform: translateX(-105%); }
    .sidebar.open { transform: translateX(0); }
    .sidebar-close { display: inline-flex; align-items: center; justify-content: center; }
    .sidebar-overlay.active { display: block; }
    body.sidebar-open { overflow: hidden; }
    .main { margin-left: 0; }
    .topbar { padding: 12px 16px; z-index: 26; }
    .content { padding: 16px; }
    .menu { display: inline-flex; align-items: center; justify-content: center; }
    .alerts-dropdown { right: -8px; }
}

@media (max-width: 720px) {
    body { font-size: 14px; }
    .form-grid,
    .form-grid.three,
    .kpis,
    .counter-grid,
    .form-grid.line { grid-template-columns: 1fr; }
    .hero h2 { font-size: 24px; }
    .panel { padding: 16px; border-radius: 16px; }
    .topbar-title strong { font-size: 18px; }
    .topbar { align-items: flex-start; }
    .topbar-actions { margin-left: auto; }
    .btn { width: auto; }
    .alerts-toast-wrap {
        right: 12px;
        bottom: 12px;
        width: calc(100vw - 24px);
    }
}
