/* ==========================================================================
   Cuentas Personales - Estilos
   Tema moderno, profesional y responsive.
   ========================================================================== */

:root {
    --primary: #4f46e5;
    --primary-hover: #4338ca;
    --primary-soft: #eef2ff;
    --bg: #f4f5fb;
    --surface: #ffffff;
    --border: #e5e7eb;
    --text: #1f2937;
    --text-muted: #6b7280;
    --danger: #dc2626;
    --danger-soft: #fef2f2;
    --success: #15803d;
    --success-soft: #f0fdf4;
    --sidebar-w: 260px;
    --header-h: 64px;
    --radius: 12px;
    --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.06);
    --shadow-md: 0 4px 12px rgba(16, 24, 40, 0.08);
    --focus: 0 0 0 3px rgba(79, 70, 229, 0.25);
}

* { box-sizing: border-box; }

html, body { margin: 0; padding: 0; }

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
        "Helvetica Neue", Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}

a { color: var(--primary); text-decoration: none; }

:focus-visible {
    outline: none;
    box-shadow: var(--focus);
    border-radius: 6px;
}

/* --- Layout principal ------------------------------------------------ */
.app { display: flex; min-height: 100vh; }

.app__main {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
}

.content {
    padding: 28px 32px 48px;
    max-width: 1100px;
    width: 100%;
    margin: 0 auto;
}

/* --- Sidebar --------------------------------------------------------- */
.sidebar {
    width: var(--sidebar-w);
    flex-shrink: 0;
    background: var(--surface);
    border-right: 1px solid var(--border);
    position: sticky;
    top: 0;
    height: 100vh;
    display: flex;
    flex-direction: column;
    overflow-y: auto;
}

.sidebar__brand {
    padding: 20px 24px;
    font-weight: 700;
    font-size: 1.15rem;
    border-bottom: 1px solid var(--border);
}

.sidebar__brand a { color: var(--text); }

.sidebar__nav {
    padding: 16px 12px;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.sidebar__link,
.sidebar__group-toggle {
    display: flex;
    align-items: center;
    gap: 12px;
    width: 100%;
    padding: 11px 12px;
    border: none;
    background: transparent;
    color: var(--text);
    font-size: 0.95rem;
    text-align: left;
    cursor: pointer;
    border-radius: 8px;
    transition: background 0.15s ease, color 0.15s ease;
}

.sidebar__link:hover,
.sidebar__group-toggle:hover { background: #f3f4f6; }

.sidebar__link.is-active {
    background: var(--primary-soft);
    color: var(--primary);
    font-weight: 600;
}

.sidebar__icon {
    width: 20px;
    text-align: center;
    color: var(--text-muted);
    flex-shrink: 0;
}

.sidebar__chevron {
    margin-left: auto;
    color: var(--text-muted);
    font-size: 0.8rem;
    transition: transform 0.2s ease;
}

.sidebar__group-toggle[aria-expanded="false"] .sidebar__chevron {
    transform: rotate(-90deg);
}

.sidebar__submenu {
    display: flex;
    flex-direction: column;
    gap: 2px;
    padding-left: 20px;
    margin-top: 2px;
    border-left: 2px solid var(--border);
    margin-left: 21px;
}

.sidebar__submenu[hidden] { display: none; }

.sidebar__link--sub {
    font-size: 0.9rem;
    padding: 9px 12px;
    color: var(--text-muted);
}

.sidebar__link--sub:hover { color: var(--text); }
.sidebar__link--sub.is-active { color: var(--primary); }


/* --- Header ---------------------------------------------------------- */
.header {
    height: var(--header-h);
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 0 24px;
    position: sticky;
    top: 0;
    z-index: 40;
}

.header__menu-btn {
    display: none;
    flex-direction: column;
    justify-content: center;
    gap: 5px;
    width: 40px;
    height: 40px;
    padding: 8px;
    background: transparent;
    border: 1px solid var(--border);
    border-radius: 8px;
    cursor: pointer;
}

.header__menu-line {
    display: block;
    height: 2px;
    width: 100%;
    background: var(--text);
    border-radius: 2px;
}

.header__brand {
    font-weight: 700;
    font-size: 1.05rem;
    color: var(--text);
}

.header__user { margin-left: auto; }

/* --- Menú de usuario ------------------------------------------------- */
.user-menu { position: relative; }

.user-menu__toggle {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 6px 10px;
    background: transparent;
    border: 1px solid var(--border);
    border-radius: 999px;
    cursor: pointer;
    transition: background 0.15s ease;
}

.user-menu__toggle:hover { background: #f3f4f6; }

.user-menu__avatar {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 30px;
    height: 30px;
    border-radius: 50%;
    background: var(--primary);
    color: #fff;
    font-weight: 600;
    font-size: 0.85rem;
}

.user-menu__name { font-size: 0.9rem; font-weight: 500; color: var(--text); }
.user-menu__chevron { color: var(--text-muted); font-size: 0.75rem; }

.user-menu__dropdown {
    position: absolute;
    top: calc(100% + 8px);
    right: 0;
    min-width: 220px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-md);
    overflow: hidden;
    z-index: 50;
}

.user-menu__dropdown[hidden] { display: none; }

.user-menu__info {
    padding: 14px 16px;
    border-bottom: 1px solid var(--border);
}

.user-menu__info-name {
    display: block;
    font-weight: 600;
    font-size: 0.9rem;
}

.user-menu__info-note {
    display: block;
    font-size: 0.78rem;
    color: var(--text-muted);
}

.user-menu__item {
    display: block;
    width: 100%;
    text-align: left;
    padding: 12px 16px;
    background: transparent;
    border: none;
    color: var(--danger);
    font-size: 0.9rem;
    cursor: pointer;
}

.user-menu__item:hover { background: var(--danger-soft); }

/* --- Cabecera de página --------------------------------------------- */
.page-head {
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 24px;
    flex-wrap: wrap;
}

.page-title { margin: 0; font-size: 1.6rem; font-weight: 700; }

.page-subtitle {
    margin: 4px 0 0;
    color: var(--text-muted);
    font-size: 0.95rem;
}

/* --- Tarjetas de estadísticas --------------------------------------- */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 20px;
    margin-bottom: 24px;
}

.stat-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 20px;
    box-shadow: var(--shadow-sm);
}

.stat-card--primary {
    background: linear-gradient(135deg, var(--primary), #6366f1);
    color: #fff;
    border: none;
}

.stat-card__label {
    margin: 0;
    font-size: 0.82rem;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    opacity: 0.85;
}

.stat-card--primary .stat-card__label { opacity: 0.9; }

.stat-card__value {
    margin: 8px 0 4px;
    font-size: 1.7rem;
    font-weight: 700;
}

.stat-card__meta { margin: 0; font-size: 0.85rem; opacity: 0.8; }

.stat-card__link {
    display: inline-block;
    margin-top: 10px;
    font-size: 0.85rem;
    font-weight: 600;
}


/* --- Paneles --------------------------------------------------------- */
.panel {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-sm);
    overflow: hidden;
}

.panel__head {
    padding: 18px 20px;
    border-bottom: 1px solid var(--border);
}

.panel__title { margin: 0; font-size: 1.05rem; font-weight: 600; }

.panel__empty {
    padding: 32px 20px;
    text-align: center;
    color: var(--text-muted);
}

.panel--form { padding: 24px; }

/* --- Lista de gastos ------------------------------------------------ */
.expense-list { list-style: none; margin: 0; padding: 0; }

.expense-list__item {
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
}

.expense-list__item:last-child { border-bottom: none; }

.expense-list__info { flex: 1; min-width: 0; }

.expense-list__desc {
    display: block;
    font-weight: 500;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.expense-list__meta {
    display: block;
    font-size: 0.82rem;
    color: var(--text-muted);
}

.expense-list__amount {
    font-weight: 700;
    font-size: 1rem;
    white-space: nowrap;
}

.expense-list__receipt { font-size: 1.1rem; }

/* --- Tabla ----------------------------------------------------------- */
.table-wrap { overflow-x: auto; }

.table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.92rem;
}

.table th {
    text-align: left;
    padding: 12px 20px;
    background: #f9fafb;
    color: var(--text-muted);
    font-weight: 600;
    font-size: 0.78rem;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    border-bottom: 1px solid var(--border);
}

.table td {
    padding: 14px 20px;
    border-bottom: 1px solid var(--border);
    vertical-align: top;
}

.table tr:last-child td { border-bottom: none; }

.table__num { text-align: right; }
.table__center { text-align: center; }
.table__amount { font-weight: 700; white-space: nowrap; }
.table__muted { color: var(--text-muted); }
.table__desc { font-weight: 500; }
.table__notes {
    font-size: 0.82rem;
    color: var(--text-muted);
    margin-top: 2px;
}

/* --- Botones --------------------------------------------------------- */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 10px 18px;
    border-radius: 8px;
    font-size: 0.9rem;
    font-weight: 600;
    border: 1px solid transparent;
    cursor: pointer;
    transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease;
    white-space: nowrap;
}

.btn--primary { background: var(--primary); color: #fff; }
.btn--primary:hover { background: var(--primary-hover); }

.btn--ghost {
    background: var(--surface);
    border-color: var(--border);
    color: var(--text);
}

.btn--ghost:hover { background: #f3f4f6; }

.btn--sm { padding: 6px 12px; font-size: 0.82rem; }


/* --- Formularios ----------------------------------------------------- */
.form-group { margin-bottom: 20px; }

.form-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
}

.form-label {
    display: block;
    margin-bottom: 6px;
    font-weight: 600;
    font-size: 0.88rem;
}

.form-optional { color: var(--text-muted); font-weight: 400; }

.form-control {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 0.95rem;
    color: var(--text);
    background: var(--surface);
    transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.form-control:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: var(--focus);
}

.form-control.is-invalid { border-color: var(--danger); }

.form-control--with-prefix {
    position: relative;
    display: flex;
    align-items: center;
}

.form-prefix {
    position: absolute;
    left: 12px;
    color: var(--text-muted);
    pointer-events: none;
}

.form-control--with-prefix .form-control { padding-left: 28px; }

.form-control--file { padding: 8px; }

.form-hint {
    margin: 6px 0 0;
    font-size: 0.78rem;
    color: var(--text-muted);
}

.field-error {
    margin: 6px 0 0;
    font-size: 0.8rem;
    color: var(--danger);
}

.form-actions {
    display: flex;
    gap: 12px;
    margin-top: 8px;
}

/* --- Alertas --------------------------------------------------------- */
.alert {
    padding: 12px 16px;
    border-radius: 8px;
    margin-bottom: 20px;
    font-size: 0.9rem;
}

.alert--success {
    background: var(--success-soft);
    color: var(--success);
    border: 1px solid #bbf7d0;
}

.alert--error {
    background: var(--danger-soft);
    color: var(--danger);
    border: 1px solid #fecaca;
}

/* --- Estado vacío ---------------------------------------------------- */
.empty-state {
    text-align: center;
    background: var(--surface);
    border: 1px dashed var(--border);
    border-radius: var(--radius);
    padding: 48px 24px;
    margin-bottom: 24px;
}

.empty-state__icon { font-size: 2.5rem; margin-bottom: 8px; }

.empty-state__title { margin: 0 0 8px; font-size: 1.2rem; font-weight: 600; }

.empty-state__text {
    margin: 0 auto 20px;
    max-width: 420px;
    color: var(--text-muted);
}

/* --- Página de error ------------------------------------------------- */
.error-page {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
}

.error-box { text-align: center; max-width: 420px; }

.error-code {
    font-size: 4rem;
    font-weight: 800;
    color: var(--primary);
    margin: 0;
}

.error-box h1 { margin: 0 0 8px; font-size: 1.4rem; }
.error-box p { color: var(--text-muted); margin: 0 0 20px; }

/* --- Overlay (móvil) ------------------------------------------------- */
.overlay {
    position: fixed;
    inset: 0;
    background: rgba(16, 24, 40, 0.4);
    z-index: 60;
}

.overlay[hidden] { display: none; }

/* --- Responsive ------------------------------------------------------ */
@media (max-width: 900px) {
    .header__menu-btn { display: flex; }

    .sidebar {
        position: fixed;
        top: 0;
        left: 0;
        z-index: 70;
        transform: translateX(-100%);
        transition: transform 0.25s ease;
    }

    .sidebar.is-open {
        transform: translateX(0);
        box-shadow: var(--shadow-md);
    }

    .content { padding: 20px 16px 40px; }
    .stats-grid { grid-template-columns: 1fr; }
    .form-row { grid-template-columns: 1fr; }
    .page-head { align-items: flex-start; }
}

@media (min-width: 901px) {
    .overlay { display: none !important; }
}

@media (max-width: 560px) {
    .header { padding: 0 14px; }
    .header__brand { font-size: 0.95rem; }
    .user-menu__name { display: none; }
}

/* --- Barra de filtros ------------------------------------------------ */
.filter-bar {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-end;
    gap: 12px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
    margin-bottom: 20px;
}

.filter-bar__field { display: flex; flex-direction: column; }
.filter-bar__field--grow { flex: 1; min-width: 180px; }
.filter-bar__field .form-control { min-width: 120px; }
.filter-bar__actions { display: flex; gap: 8px; padding-bottom: 2px; }

.page-head__actions { display: flex; gap: 10px; flex-wrap: wrap; }
.page-title__muted { color: var(--text-muted); font-weight: 500; font-size: 1.1rem; }

/* --- Badges ---------------------------------------------------------- */
.badge {
    display: inline-block;
    padding: 3px 10px;
    border-radius: 999px;
    font-size: 0.78rem;
    font-weight: 600;
    white-space: nowrap;
}

.badge--colilla { background: var(--primary-soft); color: var(--primary); }
.badge--informe { background: #ecfdf5; color: var(--success); }

/* --- Detalle (dl) ---------------------------------------------------- */
.detail-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 14px;
    margin: 0;
}

.detail-grid__item dt {
    font-size: 0.78rem;
    text-transform: uppercase;
    letter-spacing: 0.02em;
    color: var(--text-muted);
}

.detail-grid__item dd {
    margin: 2px 0 0;
    font-size: 0.95rem;
}

.detail-grid__item--em dd { font-weight: 700; font-size: 1.05rem; }
.detail-grid__item.is-highlight dd { color: var(--primary); }

.detail-text { margin: 0; color: var(--text); white-space: pre-wrap; }

/* --- Galería de imágenes -------------------------------------------- */
.gallery {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
    gap: 16px;
}

.gallery__item { margin: 0; }

.gallery__img {
    width: 100%;
    aspect-ratio: 3 / 4;
    object-fit: cover;
    border: 1px solid var(--border);
    border-radius: 8px;
    display: block;
    background: #f9fafb;
}

.gallery__caption {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    margin-top: 6px;
    font-size: 0.78rem;
    color: var(--text-muted);
}

.gallery__caption span {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* --- Filas dinámicas (conceptos/líneas) ----------------------------- */
.line-list { display: flex; flex-direction: column; gap: 10px; }

.line-item {
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 12px;
    background: #fafafa;
}

.line-item__row {
    display: grid;
    grid-template-columns: 150px 1fr 1fr 120px 36px;
    gap: 10px;
    align-items: end;
}

.line-item__row .form-group { margin-bottom: 0; }

.line-item__remove {
    height: 36px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--danger);
    border-radius: 8px;
    cursor: pointer;
    font-size: 0.9rem;
}

.line-item__remove:hover { background: var(--danger-soft); }

/* --- Botón de peligro ----------------------------------------------- */
.btn--danger {
    background: var(--danger-soft);
    color: var(--danger);
    border-color: #fecaca;
}

.btn--danger:hover { background: #fee2e2; }

/* --- Alerta de advertencia ------------------------------------------ */
.alert--warn {
    background: #fffbeb;
    color: #92400e;
    border: 1px solid #fde68a;
}

@media (max-width: 900px) {
    .filter-bar { flex-direction: column; align-items: stretch; }
    .line-item__row { grid-template-columns: 1fr 1fr; }
    .line-item__row .line-item__desc { grid-column: 1 / -1; }
    .line-item__row .line-item__cat { grid-column: 1 / 2; }
    .line-item__row .line-item__monto { grid-column: 2 / 3; }
    .line-item__row .line-item__tipo { grid-column: 1 / 2; }
    .line-item__remove { grid-column: 2 / 3; justify-self: end; }
}


/* --- UI Modernization Overrides --- */
:root {
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --primary-soft: #eff6ff;
    --bg: #f8fafc;
    --surface: #ffffff;
    --border: #e2e8f0;
    --text: #0f172a;
    --text-muted: #64748b;
    --radius: 16px;
    --shadow-sm: 0 1px 3px rgba(15, 23, 42, 0.05);
    --shadow-md: 0 10px 15px -3px rgba(15, 23, 42, 0.04), 0 4px 6px -4px rgba(15, 23, 42, 0.02);
    --shadow-lg: 0 20px 25px -5px rgba(15, 23, 42, 0.03), 0 8px 10px -6px rgba(15, 23, 42, 0.01);
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

.panel {
    border: none;
    box-shadow: var(--shadow-md);
    border-radius: var(--radius);
    padding: 24px;
    background: var(--surface);
    transition: transform 0.2s, box-shadow 0.2s;
}

.stat-card {
    border: none;
    box-shadow: var(--shadow-md);
    border-radius: var(--radius);
    padding: 24px;
}

.stat-card:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-lg);
}

.btn {
    border-radius: 8px;
    font-weight: 500;
    transition: all 0.2s;
}

.form-control {
    border-radius: 8px;
    border-color: var(--border);
    transition: border-color 0.2s, box-shadow 0.2s;
}

.sidebar {
    border-right: none;
    box-shadow: 1px 0 10px rgba(0,0,0,0.03);
}

.header {
    border-bottom: none;
    box-shadow: 0 1px 5px rgba(0,0,0,0.02);
    background: rgba(255, 255, 255, 0.9);
    backdrop-filter: blur(8px);
}

.page-title {
    font-weight: 700;
    letter-spacing: -0.025em;
    color: var(--text);
}

.detail-grid__item {
    padding: 16px;
    background: var(--bg);
    border-radius: 12px;
    border: 1px solid var(--border);
}

.detail-grid__item.is-highlight {
    background: var(--primary-soft);
    border-color: var(--primary);
    color: var(--primary-hover);
}

.table th {
    font-weight: 600;
    text-transform: uppercase;
    font-size: 0.75rem;
    letter-spacing: 0.05em;
    color: var(--text-muted);
}

/* --- Expense Cards Layout (Gastos Básicos) --- */
.expense-cards {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(380px, 1fr));
    gap: 20px;
    margin-top: 16px;
}

.expense-card {
    background: var(--surface);
    border-radius: var(--radius);
    box-shadow: var(--shadow-md);
    overflow: hidden;
    transition: transform 0.2s, box-shadow 0.2s;
}

.expense-card:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-lg);
}

.expense-card__header {
    display: flex;
    align-items: flex-start;
    gap: 12px;
    padding: 20px 20px 12px;
}

.expense-card__icon {
    font-size: 2rem;
    line-height: 1;
    flex-shrink: 0;
}

.expense-card__info {
    flex: 1;
    min-width: 0;
}

.expense-card__title {
    font-size: 1rem;
    font-weight: 600;
    margin: 0;
    color: var(--text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.expense-card__meta {
    font-size: 0.8125rem;
    color: var(--text-muted);
}

.expense-card__amount {
    font-size: 1.125rem;
    font-weight: 700;
    color: var(--primary);
    white-space: nowrap;
}

.expense-card__body {
    padding: 0 20px 12px;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.expense-card__detail {
    display: flex;
    justify-content: space-between;
    font-size: 0.875rem;
    color: var(--text-muted);
}

.expense-card__label {
    font-weight: 500;
    color: var(--text);
}

.expense-card__footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 20px;
    border-top: 1px solid var(--border);
    background: var(--bg);
}

/* --- Table Actions (inline buttons) --- */
.table__actions {
    white-space: nowrap;
}
.table__actions .btn {
    margin: 0 2px;
}

@media (max-width: 640px) {
    .expense-cards {
        grid-template-columns: 1fr;
    }
}


/* ==========================================================================
   Estados de carga y mensajes con cierre automático
   ========================================================================== */

/* Spinner giratorio */
.spinner {
    display: inline-block;
    width: 1em;
    height: 1em;
    border: 2px solid currentColor;
    border-top-color: transparent;
    border-radius: 50%;
    animation: spinner-rotate 0.7s linear infinite;
    flex-shrink: 0;
    vertical-align: -0.125em;
}

@keyframes spinner-rotate {
    to { transform: rotate(360deg); }
}

/* Botón en estado de carga */
.btn.is-loading {
    pointer-events: none;
    opacity: 0.72;
    cursor: default;
}

/* Mensajes flash: desvanecimiento al cerrarse automáticamente */
.alert {
    transition: opacity 0.4s ease, transform 0.4s ease;
}

.alert.is-hiding {
    opacity: 0;
    transform: translateY(-6px);
}

