:root {
    --fondo: #f4f6fb;
    --superficie: #ffffff;
    --superficie-2: #f8fafc;
    --texto: #0f172a;
    --texto-2: #64748b;
    --borde: #e2e8f0;
    --primario: #4f46e5;
    --primario-2: #4338ca;
    --ingreso: #16a34a;
    --gasto: #dc2626;
    --alerta: #d97706;
    --sombra: 0 1px 2px rgba(15, 23, 42, .04), 0 4px 16px rgba(15, 23, 42, .06);
    --radio: 14px;
    color-scheme: light;
}
@media (prefers-color-scheme: dark) {
    :root {
        --fondo: #0b1120;
        --superficie: #111827;
        --superficie-2: #1f2937;
        --texto: #e5e7eb;
        --texto-2: #94a3b8;
        --borde: #1f2a3c;
        --primario: #6366f1;
        --primario-2: #818cf8;
        --ingreso: #22c55e;
        --gasto: #f87171;
        --sombra: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px rgba(0, 0, 0, .25);
        color-scheme: dark;
    }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
    font-family: "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
    background: var(--fondo);
    color: var(--texto);
    font-size: 15px;
    line-height: 1.45;
}
h1, h2, h3 { margin: 0; }
h3 { font-size: 1rem; font-weight: 600; }
a { color: var(--primario); text-decoration: none; }
[hidden] { display: none !important; }
.muted { color: var(--texto-2); }
.positivo { color: var(--ingreso); }
.negativo { color: var(--gasto); }
.empty { color: var(--texto-2); text-align: center; padding: 1.5rem 0; margin: 0; list-style: none; }

/* ---------- Formularios y botones ---------- */
input, select, button { font: inherit; color: inherit; }
input:not([type=checkbox]):not([type=radio]):not([type=color]), select {
    width: 100%;
    padding: .6rem .75rem;
    border: 1px solid var(--borde);
    border-radius: 10px;
    background: var(--superficie);
    outline: none;
    transition: border-color .15s, box-shadow .15s;
}
input:focus, select:focus { border-color: var(--primario); box-shadow: 0 0 0 3px color-mix(in srgb, var(--primario) 20%, transparent); }
input[type=color] { width: 100%; height: 42px; padding: 2px; border: 1px solid var(--borde); border-radius: 10px; background: var(--superficie); }
input:disabled { opacity: .6; }
label { display: flex; flex-direction: column; gap: .35rem; font-size: .88rem; font-weight: 500; }
.form { display: flex; flex-direction: column; gap: 1rem; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: .75rem; }

.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
    padding: .6rem 1.05rem; border-radius: 10px; border: 1px solid transparent;
    font-weight: 600; cursor: pointer; white-space: nowrap; transition: background .15s, opacity .15s;
}
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--primario); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primario-2); }
.btn-ghost { background: transparent; border-color: var(--borde); }
.btn-ghost:hover:not(:disabled) { background: var(--superficie-2); }
.btn-danger { background: var(--gasto); color: #fff; }
.btn-block { width: 100%; }
.btn-icon {
    background: none; border: none; cursor: pointer; padding: .35rem .5rem; border-radius: 8px;
    font-size: 1rem; line-height: 1;
}
.btn-icon:hover { background: var(--superficie-2); }

.alerta {
    padding: .7rem .9rem; border-radius: 10px; margin-bottom: 1rem; font-size: .9rem;
    background: color-mix(in srgb, var(--gasto) 12%, transparent); color: var(--gasto);
}

/* ---------- Login ---------- */
.auth-body { min-height: 100vh; display: grid; place-items: center; padding: 1.5rem;
    background: radial-gradient(circle at 15% 20%, color-mix(in srgb, var(--primario) 18%, transparent), transparent 45%), var(--fondo); }
.auth-wrap { width: 100%; max-width: 980px; display: grid; grid-template-columns: 1.1fr 1fr; gap: 3rem; align-items: center; }
.auth-hero h1 { font-size: 2.4rem; line-height: 1.15; margin: 1.5rem 0 1rem; }
.auth-hero p { color: var(--texto-2); font-size: 1.05rem; }
.auth-features { padding: 0; list-style: none; display: grid; gap: .5rem; margin-top: 1.5rem; }
.auth-features li::before { content: "✓"; color: var(--ingreso); font-weight: 700; margin-right: .6rem; }
.auth-card { background: var(--superficie); border-radius: 18px; padding: 2rem; box-shadow: var(--sombra); border: 1px solid var(--borde); }
.auth-brand-movil { display: none !important; }
@media (max-width: 860px) { .auth-brand-movil { display: flex !important; justify-content: center; margin-bottom: 1.25rem; } }
.auth-form { display: flex; flex-direction: column; gap: 1rem; }
.tabs { display: grid; grid-template-columns: 1fr 1fr; background: var(--superficie-2); border-radius: 10px; padding: 4px; margin-bottom: 1.5rem; }
.tab { border: none; background: none; padding: .55rem; border-radius: 8px; font-weight: 600; color: var(--texto-2); cursor: pointer; }
.tab.active { background: var(--superficie); color: var(--texto); box-shadow: var(--sombra); }

.brand { display: flex; align-items: center; gap: .6rem; font-weight: 700; font-size: 1.1rem; }
.brand-lg { font-size: 1.3rem; }
.brand-logo {
    width: 34px; height: 34px; border-radius: 10px; display: grid; place-items: center;
    background: linear-gradient(135deg, var(--primario), #06b6d4); color: #fff; font-weight: 800;
}

/* ---------- Layout ---------- */
.layout { display: grid; grid-template-columns: 250px 1fr; min-height: 100vh; }
.sidebar {
    position: sticky; top: 0; height: 100vh; display: flex; flex-direction: column; gap: 1.5rem;
    padding: 1.4rem 1rem; background: var(--superficie); border-right: 1px solid var(--borde);
}
.nav { display: flex; flex-direction: column; gap: .25rem; }
.nav a { display: flex; gap: .7rem; align-items: center; padding: .65rem .8rem; border-radius: 10px; color: var(--texto-2); font-weight: 500; }
.nav a:hover { background: var(--superficie-2); color: var(--texto); }
.nav a.active { background: color-mix(in srgb, var(--primario) 12%, transparent); color: var(--primario); }
.sidebar-foot { margin-top: auto; display: flex; flex-direction: column; gap: .75rem; }
.user-chip { display: flex; align-items: center; gap: .6rem; font-weight: 500; overflow: hidden; }
.avatar { flex: none; width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center; background: var(--superficie-2); font-size: .8rem; font-weight: 700; }
.sidebar-overlay { display: none; }

.main { padding: 1.25rem 2rem 3rem; min-width: 0; }
.topbar { display: flex; align-items: center; gap: 1rem; margin-bottom: 1.5rem; flex-wrap: wrap; }
.topbar h1 { font-size: 1.5rem; margin-right: auto; }
.menu-toggle { display: none; font-size: 1.3rem; }
.mes-selector { display: flex; align-items: center; gap: .25rem; }
.mes-selector input { width: 170px !important; }

.card { background: var(--superficie); border: 1px solid var(--borde); border-radius: var(--radio); padding: 1.2rem; box-shadow: var(--sombra); }
.card h3 { margin-bottom: 1rem; }
.card-head { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 1rem; }
.card-head h3 { margin: 0; }
.card-head a { font-size: .85rem; }
.toolbar { display: flex; justify-content: space-between; align-items: center; gap: 1rem; margin-bottom: 1rem; flex-wrap: wrap; }

/* ---------- Resumen ---------- */
.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 1rem; margin-bottom: 1rem; }
.kpi { background: var(--superficie); border: 1px solid var(--borde); border-radius: var(--radio); padding: 1.1rem 1.2rem; box-shadow: var(--sombra); display: flex; flex-direction: column; gap: .25rem; }
.kpi-label { color: var(--texto-2); font-size: .85rem; }
.kpi strong { font-size: 1.5rem; font-variant-numeric: tabular-nums; }
.kpi-ingreso strong { color: var(--ingreso); }
.kpi-gasto strong { color: var(--gasto); }
.grid-charts { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; margin-bottom: 1rem; }
.chart-box { position: relative; height: 270px; }

.dot { display: inline-block; flex: none; width: 10px; height: 10px; border-radius: 50%; margin-right: .5rem; }
.lista-mov { list-style: none; margin: 0; padding: 0; }
.lista-mov li { display: flex; align-items: center; gap: .5rem; padding: .6rem 0; border-bottom: 1px solid var(--borde); }
.lista-mov li:last-child { border-bottom: none; }
.mov-info { display: flex; flex-direction: column; min-width: 0; flex: 1; }
.mov-info strong { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.mov-info small { color: var(--texto-2); }
.monto { font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }
.monto-ingreso { color: var(--ingreso); }
.monto-gasto { color: var(--gasto); }
.monto-transferencia { color: var(--texto-2); }

.presu { margin-bottom: 1rem; }
.presu-head { display: flex; justify-content: space-between; gap: 1rem; font-size: .9rem; margin-bottom: .35rem; }
.presu-head > span:first-child { display: flex; align-items: center; font-weight: 500; }
.barra { height: 8px; border-radius: 99px; background: var(--superficie-2); overflow: hidden; }
.barra span { display: block; height: 100%; border-radius: 99px; transition: width .4s; }
.barra-ok span { background: var(--ingreso); }
.barra-alerta span { background: var(--alerta); }
.barra-excedido span { background: var(--gasto); }
.presu small { font-size: .78rem; }
.presu-ok { color: var(--texto-2); }
.presu-alerta { color: var(--alerta); }
.presu-excedido { color: var(--gasto); font-weight: 600; }
.presu-total { padding-bottom: 1rem; margin-bottom: 1rem; border-bottom: 1px solid var(--borde); color: var(--texto-2); }
.presu-fila { display: flex; gap: 1.25rem; align-items: center; }
.presu-fila-info { flex: 1; min-width: 0; }
.presu-input { width: 150px !important; flex: none; }

/* ---------- Movimientos ---------- */
.filtros { display: grid; grid-template-columns: 1.5fr repeat(3, minmax(0, 1fr)) auto auto; gap: .6rem; align-items: center; margin-bottom: 1rem; padding: .9rem; }
.filtros-movil { display: contents; }
#btn-toggle-filtros { display: none; }
.check { flex-direction: row; align-items: center; gap: .4rem; white-space: nowrap; font-weight: 400; }
.totales-bar { display: flex; flex-wrap: wrap; gap: .5rem 1.5rem; margin: 0 .25rem 1rem; font-size: .9rem; }
.tabla-wrap { padding: 0; overflow-x: auto; }
.tabla { width: 100%; border-collapse: collapse; }
.tabla th, .tabla td { padding: .75rem 1rem; text-align: left; border-bottom: 1px solid var(--borde); }
.tabla th { font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; color: var(--texto-2); font-weight: 600; background: var(--superficie-2); }
.tabla tr:last-child td { border-bottom: none; }
.tabla tbody tr:hover { background: var(--superficie-2); }
.tabla .num, .tabla td:first-child { text-align: right; white-space: nowrap; }
.tabla td:first-child { text-align: left; }
.tabla .acciones { text-align: right; white-space: nowrap; width: 1%; }
.badge {
    --c: #64748b;
    display: inline-block; padding: .15rem .6rem; border-radius: 99px; font-size: .8rem; font-weight: 500;
    background: color-mix(in srgb, var(--c) 15%, transparent); color: var(--c);
}
.paginacion { display: flex; justify-content: center; align-items: center; gap: 1rem; margin-top: 1rem; }

/* ---------- Cuentas y categorías ---------- */
.grid-cuentas { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 1rem; }
.cuenta { position: relative; border-top: 4px solid var(--c); display: flex; flex-direction: column; gap: .3rem; }
.cuenta h3 { margin: 0; }
.cuenta-tipo { font-size: .75rem; text-transform: uppercase; letter-spacing: .05em; color: var(--c); font-weight: 700; }
.cuenta-saldo { font-size: 1.6rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.cuenta-acciones { position: absolute; top: .75rem; right: .6rem; }
.lista-cat { list-style: none; margin: 0; padding: 0; }
.lista-cat li { display: flex; align-items: center; gap: .5rem; padding: .5rem 0; border-bottom: 1px solid var(--borde); }
.lista-cat li:last-child { border-bottom: none; }
.cat-nombre { flex: 1; }

/* ---------- Modales ---------- */
.modal {
    width: min(480px, calc(100vw - 2rem)); border: 1px solid var(--borde); border-radius: 16px; padding: 1.5rem;
    background: var(--superficie); color: var(--texto); box-shadow: 0 20px 60px rgba(0, 0, 0, .3);
}
.modal-sm { width: min(380px, calc(100vw - 2rem)); }
.modal::backdrop { background: rgba(15, 23, 42, .5); backdrop-filter: blur(2px); }
.modal[open] { animation: aparecer .18s ease-out; }
.modal-actions { display: flex; justify-content: flex-end; gap: .6rem; margin-top: .5rem; }
@keyframes aparecer { from { opacity: 0; transform: translateY(8px) scale(.98); } }

.segmented { display: grid; grid-template-columns: repeat(3, 1fr); background: var(--superficie-2); border-radius: 10px; padding: 4px; }
.segmented label { font-weight: 600; }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented span { text-align: center; padding: .5rem; border-radius: 8px; cursor: pointer; color: var(--texto-2); }
.segmented input:checked + span { background: var(--superficie); color: var(--texto); box-shadow: var(--sombra); }
.segmented input[value=gasto]:checked + span { color: var(--gasto); }
.segmented input[value=ingreso]:checked + span { color: var(--ingreso); }
.segmented input:focus-visible + span { outline: 2px solid var(--primario); }

/* ---------- Toasts ---------- */
.toasts { position: fixed; right: 1rem; bottom: 1rem; display: flex; flex-direction: column; gap: .5rem; z-index: 1000; }
.toast {
    padding: .75rem 1rem; border-radius: 10px; color: #fff; box-shadow: var(--sombra); max-width: 340px;
    animation: aparecer .2s ease-out; transition: opacity .3s, transform .3s; font-size: .92rem;
}
.toast-ok { background: #15803d; }
.toast-error { background: #b91c1c; }
.toast.salir { opacity: 0; transform: translateX(20px); }

/* ---------- Responsive ---------- */
@media (max-width: 1100px) {
    .kpis { grid-template-columns: repeat(2, 1fr); }
    .filtros { grid-template-columns: 1fr 1fr 1fr; }
    .filtros input[type=search] { grid-column: span 2; }
}
.bottom-nav { display: none; }

@media (max-width: 860px) {
    /* Evita el zoom automático de iOS al enfocar campos */
    input:not([type=checkbox]):not([type=radio]):not([type=color]), select { font-size: 16px; }

    .layout { grid-template-columns: 1fr; }
    .sidebar { position: fixed; left: 0; z-index: 60; width: 270px; transform: translateX(-100%); transition: transform .25s; }
    .menu-abierto .sidebar { transform: none; }
    .menu-abierto .sidebar-overlay { display: block; position: fixed; inset: 0; background: rgba(0, 0, 0, .4); z-index: 55; }
    .menu-toggle { display: inline-block; }
    .main { padding: .75rem 1rem calc(88px + env(safe-area-inset-bottom)); }
    .grid-charts, .auth-wrap { grid-template-columns: 1fr; }
    .auth-hero { display: none; }
    .card { padding: 1rem; }
    .card.tabla-wrap { padding: 0; }

    /* Barra superior */
    .topbar { gap: .5rem; margin-bottom: 1rem; }
    .topbar h1 { font-size: 1.25rem; }
    #btn-nuevo-mov { display: none; }
    .mes-selector { order: 3; width: 100%; }
    .mes-selector input { flex: 1; width: auto !important; }
    .btn-icon { padding: .5rem .6rem; }

    /* Navegación inferior con botón central */
    .bottom-nav {
        display: grid; grid-template-columns: repeat(5, 1fr); align-items: end;
        position: fixed; left: 0; right: 0; bottom: 0; z-index: 50;
        padding: .35rem .25rem calc(.35rem + env(safe-area-inset-bottom));
        background: var(--superficie); border-top: 1px solid var(--borde);
        box-shadow: 0 -4px 16px rgba(15, 23, 42, .06);
    }
    .bottom-nav a {
        display: flex; flex-direction: column; align-items: center; gap: .1rem;
        font-size: .68rem; font-weight: 600; color: var(--texto-2); padding: .3rem 0;
    }
    .bottom-nav a span { font-size: 1.2rem; line-height: 1.2; filter: grayscale(1); opacity: .7; }
    .bottom-nav a.active { color: var(--primario); }
    .bottom-nav a.active span { filter: none; opacity: 1; }
    .fab {
        justify-self: center; width: 56px; height: 56px; margin-top: -26px; border-radius: 50%; border: 4px solid var(--fondo);
        background: linear-gradient(135deg, var(--primario), #06b6d4); color: #fff; font-size: 1.9rem; line-height: 1;
        box-shadow: 0 6px 18px color-mix(in srgb, var(--primario) 45%, transparent); cursor: pointer;
    }
    .toasts { left: 1rem; right: 1rem; bottom: calc(84px + env(safe-area-inset-bottom)); align-items: center; }

    /* Resumen */
    .kpis { grid-template-columns: 1fr 1fr; gap: .6rem; }
    .kpis .kpi:first-child, .kpis .kpi:last-child { grid-column: 1 / -1; }
    .kpi { padding: .85rem .9rem; }
    .kpi-label { font-size: .78rem; }
    .kpi strong { font-size: clamp(1rem, 4.6vw, 1.35rem); overflow-wrap: anywhere; }
    .kpis .kpi:first-child strong { font-size: 1.6rem; }
    .grid-charts { gap: .75rem; margin-bottom: .75rem; }
    .chart-box { height: 230px; }

    /* Filtros plegables */
    .filtros { grid-template-columns: 1fr 1fr; }
    .filtros-movil { display: flex; gap: .5rem; grid-column: 1 / -1; }
    #btn-toggle-filtros { display: inline-flex; }
    #btn-toggle-filtros[aria-expanded=true] { background: var(--superficie-2); }
    .filtros:not(.abierto) > :not(.filtros-movil) { display: none; }
    .totales-bar { font-size: .82rem; gap: .25rem 1rem; }

    /* Tabla de movimientos como lista compacta de 2 líneas */
    .tabla thead { display: none; }
    .tabla, .tabla tbody { display: block; }
    .tabla tr { display: flex; flex-wrap: wrap; align-items: center; gap: .2rem .6rem; padding: .7rem 1rem; border-bottom: 1px solid var(--borde); }
    .tabla td { display: block; border: none; padding: 0; text-align: left; }
    .tabla td:nth-child(2) { order: 1; flex: 1 1 calc(100% - 10.5rem); min-width: 0; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
    .tabla td:nth-child(5) { order: 2; flex: 0 0 9rem; text-align: right; }
    .tabla td:nth-child(1) { order: 3; font-size: .78rem; color: var(--texto-2); }
    .tabla td:nth-child(3) { order: 4; }
    .tabla td:nth-child(3) .badge { font-size: .72rem; padding: .05rem .5rem; }
    .tabla td:nth-child(4) { order: 5; font-size: .78rem; color: var(--texto-2); }
    .tabla td.acciones { order: 6; margin-left: auto; width: auto; }
    .tabla td.acciones .btn-icon { padding: .3rem .45rem; font-size: .9rem; }

    /* Presupuestos */
    .presu-fila { gap: .75rem; margin-bottom: .25rem; }
    .presu-head { flex-wrap: wrap; gap: 0 .5rem; }
    .presu-input { width: 96px !important; }

    /* Modales como hoja inferior */
    .modal {
        width: 100%; max-width: 100%; margin: auto 0 0; border-radius: 20px 20px 0 0; border-bottom: none;
        padding: 1.25rem 1rem calc(1rem + env(safe-area-inset-bottom)); max-height: 92vh; overflow-y: auto;
    }
    .modal[open] { animation: subir .22s ease-out; }
    .modal-actions .btn { flex: 1; }
}
@keyframes subir { from { transform: translateY(40px); opacity: 0; } }

@media (max-width: 480px) {
    .grid-2 { grid-template-columns: 1fr; }
    .modal .grid-2 { grid-template-columns: 1fr 1fr; }
    .auth-body { padding: 1rem; align-items: start; padding-top: 12vh; }
    .auth-card { padding: 1.5rem 1.1rem; }
}
