/* --- RESET Y BASE --- */
*,
*::before,
*::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html {
    font-size: 16px;
    -webkit-text-size-adjust: 100%;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    scroll-behavior: smooth;
    -webkit-tap-highlight-color: transparent;
    /* Reserva estable del espacio de la barra de desplazamiento */
    scrollbar-gutter: stable;
}

/* En desktop, mantener siempre visible el riel de scroll vertical en vistas del portal para evitar saltos de ancho,
   excepto en pantallas de autenticación (.auth-page) donde la tarjeta se centra limpiamente sin barras de scroll */
@media (min-width: 768px) {
    html:not(:has(.auth-page)):not(.auth-html) {
        overflow-y: scroll;
    }
}

body {
    font-family: var(--font-sans);
    background-color: var(--bg-app);
    color: var(--text-primary);
    line-height: 1.5;
    min-height: 100vh;
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
}

img,
svg {
    display: block;
    max-width: 100%;
}

a {
    color: inherit;
    text-decoration: none;
}

button {
    font-family: inherit;
    cursor: pointer;
    border: none;
    background: none;
}

/* --- ESTRUCTURA GENERAL (APP SHELL) --- */
.app-shell {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

.app-main {
    flex: 1;
    padding: 1.5rem 1rem 3rem;
}

.container {
    width: 100%;
    max-width: var(--container-max-width);
    margin: 0 auto;
    padding: 0 0.5rem;
}

@media (min-width: 640px) {
    .container {
        padding: 0 1rem;
    }
}

/* --- ESTRUCTURA GENERAL (APP SHELL) --- */
html,
body {
    overflow-x: clip;
    width: 100%;
}

.app-shell {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    width: 100%;
}

.app-main {
    flex: 1;
    padding: 1rem 0.75rem 1.5rem;
    width: 100%;
}

@media (min-width: 640px) {
    .app-main {
        padding: 1.75rem 1rem 3rem;
    }
}

.container {
    width: 100%;
    max-width: var(--container-max-width);
    margin: 0 auto;
    padding: 0 0.5rem;
}

@media (min-width: 640px) {
    .container {
        padding: 0 1rem;
    }
}

/* --- ENCABEZADO EN DOS BARRAS SEPARADAS (STICKY) --- */
.app-header {
    position: -webkit-sticky;
    position: sticky;
    top: 0;
    z-index: 1000;
    background: var(--bg-surface);
    box-shadow: 0 4px 14px -1px rgba(15, 23, 42, 0.08), 0 2px 6px -1px rgba(15, 23, 42, 0.04);
    width: 100%;
}

.header-container {
    max-width: var(--container-max-width);
    margin: 0 auto;
    padding: 0 0.5rem;
}

@media (min-width: 640px) {
    .header-container {
        padding: 0 1rem;
    }
}

/* --- BARRA 1: IDENTIFICACIÓN Y ACCIONES DE USUARIO --- */
.app-top-bar {
    background: var(--bg-surface);
    border-bottom: 1px solid var(--border-subtle);
    padding: max(0.75rem, env(safe-area-inset-top)) max(0.25rem, env(safe-area-inset-right)) 0.5rem max(0.25rem, env(safe-area-inset-left));
}

.top-bar-content {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 0.45rem;
    min-height: auto;
}

.header-brand {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    user-select: none;
    min-width: 0;
    flex-shrink: 1;
    text-decoration: none;
    transition: opacity var(--transition-fast);
}

.header-brand:hover {
    opacity: 0.88;
}

.brand-logo {
    width: 36px;
    height: 36px;
    object-fit: contain;
    flex-shrink: 0;
    filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.08));
}

.brand-text {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.brand-title {
    font-size: 1.05rem;
    font-weight: 700;
    color: var(--primary-900);
    letter-spacing: -0.01em;
    line-height: 1.2;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.brand-subtitle {
    font-size: 0.75rem;
    font-weight: 500;
    color: var(--text-secondary);
    letter-spacing: 0.01em;
    line-height: 1.15;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.header-user-actions {
    display: flex;
    align-items: stretch;
    gap: 0;
    margin-left: calc(36px + 0.6rem);
    min-width: 0;
    width: auto;
}

.user-chip {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    height: 28px;
    padding: 0 0.55rem;
    background: var(--bg-surface-muted);
    border: 1px solid var(--border-subtle);
    border-right: none;
    border-top-left-radius: var(--radius-sm);
    border-bottom-left-radius: var(--radius-sm);
    border-top-right-radius: 0;
    border-bottom-right-radius: 0;
    font-size: 0.76rem;
    font-weight: 500;
    color: var(--text-secondary);
    flex: 1 1 auto;
    min-width: 0;
    max-width: none;
    box-sizing: border-box;
    transition: all var(--transition-fast);
}

.user-chip-email {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    min-width: 0;
}

.user-chip-icon {
    width: 12px;
    height: 12px;
    color: var(--primary-700);
    flex-shrink: 0;
}

.btn-user-key {
    position: relative;
    z-index: 1;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    height: 28px;
    width: 32px;
    padding: 0;
    background: #fef3c7;
    border: 1px solid var(--border-subtle);
    margin-right: -1px;
    border-radius: 0;
    color: #b45309;
    text-decoration: none;
    flex-shrink: 0;
    box-sizing: border-box;
    transition: all var(--transition-fast);
}

.btn-user-key:hover {
    background: #fde68a;
    color: #92400e;
    border-color: #d97706;
    z-index: 2;
}

.btn-user-key.active {
    background: linear-gradient(135deg, #f59e0b 0%, #d97706 100%);
    color: #ffffff;
    border-color: #b45309;
    box-shadow: 0 1px 4px rgba(217, 119, 6, 0.4);
    z-index: 2;
}

.btn-user-key.active:hover {
    background: linear-gradient(135deg, #d97706 0%, #b45309 100%);
    color: #ffffff;
    border-color: #78350f;
    z-index: 2;
}

.btn-user-key svg {
    width: 15px;
    height: 15px;
}

.btn-logout {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.25rem;
    height: 28px;
    padding: 0 0.55rem;
    border-top-left-radius: 0;
    border-bottom-left-radius: 0;
    border-top-right-radius: var(--radius-sm);
    border-bottom-right-radius: var(--radius-sm);
    font-size: 0.76rem;
    font-weight: 600;
    color: var(--danger-text);
    background: var(--danger-bg);
    border: 1px solid var(--border-subtle);
    flex-shrink: 0;
    text-decoration: none;
    box-sizing: border-box;
    transition: all var(--transition-fast);
}

.btn-logout:hover {
    background: #fee2e2;
    border-color: var(--danger-border);
    color: var(--danger-text);
    z-index: 1;
}

.btn-logout svg {
    width: 13px;
    height: 13px;
}

/* --- BOTÓN DE TEMA (DARK / LIGHT TOGGLE) --- */
.btn-theme-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 34px;
    height: 34px;
    padding: 0;
    border-radius: 3px;
    background: var(--bg-nav-pill);
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    transition: all var(--transition-fast);
    user-select: none;
    flex-shrink: 0;
}

.btn-theme-toggle:hover {
    background: var(--bg-surface-active);
    color: var(--primary-600);
    border-color: var(--border-accent);
    transform: translateY(-1px);
}

.theme-icon {
    width: 17px;
    height: 17px;
    transition: transform 0.35s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.btn-theme-toggle:hover .theme-icon {
    transform: rotate(20deg) scale(1.1);
}

/* Visibilidad de íconos según tema */
html:not([data-theme="dark"]) .theme-icon-sun {
    display: none;
}

html:not([data-theme="dark"]) .theme-icon-moon {
    display: block;
}

html[data-theme="dark"] .theme-icon-sun {
    display: block;
    color: #fbbf24;
}

html[data-theme="dark"] .theme-icon-moon {
    display: none;
}

/* --- BARRA 2: NAVEGACIÓN DE PESTAÑAS (SUB-HEADER) --- */
.app-nav-bar {
    background: var(--bg-surface-muted);
    border-bottom: 1px solid var(--border-subtle);
    padding: 0.35rem 0;
    width: 100%;
}

.nav-bar-container {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.65rem;
}

.nav-bar-wrapper {
    flex: 1;
    min-width: 0;
}

.nav-pills {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 0.3rem;
    list-style: none;
    margin: 0;
    padding: 0;
    width: 100%;
}

.nav-pill-item {
    width: 100%;
    margin: 0;
}

.nav-pill-link {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.3rem;
    width: 100%;
    height: 34px;
    padding: 0 0.25rem;
    border-radius: 3px;
    font-size: 0.84rem;
    font-weight: 600;
    color: var(--text-secondary);
    background: var(--bg-nav-pill);
    border: none;
    transition: all var(--transition-fast);
    user-select: none;
    white-space: nowrap;
    text-align: center;
    text-decoration: none;
}

.nav-pill-link svg {
    width: 15px;
    height: 15px;
    color: currentColor;
    flex-shrink: 0;
    transition: transform var(--transition-fast);
}

.nav-pill-link:hover {
    color: var(--primary-700);
    background: var(--primary-50);
    border-color: rgba(5, 150, 105, 0.3);
}

.nav-pill-link.active {
    color: var(--text-white);
    background: var(--gradient-primary);
    border-color: transparent;
    box-shadow: 0 2px 8px rgba(5, 150, 105, 0.3);
    font-weight: 600;
}

.nav-pill-link.active svg {
    color: var(--text-white);
}

/* Spinner indicador de carga en el propio botón */
.nav-pill-spinner {
    display: none;
    width: 14px;
    height: 14px;
    border: 2px solid rgba(5, 150, 105, 0.25);
    border-top-color: var(--primary-700);
    border-radius: 50%;
    animation: navPillSpin 0.6s linear infinite;
    flex-shrink: 0;
}

.nav-pill-link.active .nav-pill-spinner {
    border-color: rgba(255, 255, 255, 0.35);
    border-top-color: #ffffff;
}

@keyframes navPillSpin {
    0% {
        transform: rotate(0deg);
    }

    100% {
        transform: rotate(360deg);
    }
}

.nav-pill-link.htmx-request .nav-pill-icon,
.nav-pill-link.is-loading .nav-pill-icon {
    display: none !important;
}

.nav-pill-link.htmx-request .nav-pill-spinner,
.nav-pill-link.is-loading .nav-pill-spinner {
    display: inline-block !important;
}

.nav-pill-link.htmx-request,
.nav-pill-link.is-loading {
    opacity: 0.88;
}

/* Adaptación para pantallas pequeñas (<= 380px) */
@media (max-width: 380px) {
    .nav-pills {
        gap: 0.2rem;
    }

    .nav-pill-link {
        font-size: 0.76rem;
        padding: 0.4rem 0.15rem;
        gap: 0.2rem;
    }

    .nav-pill-link svg {
        width: 13px;
        height: 13px;
    }

    .brand-title {
        font-size: 0.98rem;
    }

    .brand-subtitle {
        font-size: 0.72rem;
    }
}

@media (max-width: 350px) {
    .header-user-actions {
        margin-left: 0;
    }

    .brand-title {
        font-size: 0.94rem;
    }
}

@media (max-width: 330px) {
    .btn-logout span {
        display: none;
    }

    .btn-logout {
        padding: 0.24rem 0.45rem;
    }
}

/* Adaptación para pantallas de escritorio (>= 640px) */
@media (min-width: 640px) {
    .app-top-bar {
        padding: 0.55rem 0;
    }

    .top-bar-content {
        display: flex;
        flex-direction: row;
        align-items: center;
        justify-content: space-between;
        min-height: 52px;
        gap: 0.75rem;
        padding-top: 0;
        padding-bottom: 0;
    }

    .header-brand {
        gap: 0.65rem;
    }

    .brand-logo {
        width: 38px;
        height: 38px;
    }

    .brand-title {
        font-size: 1.15rem;
    }

    .brand-subtitle {
        font-size: 0.80rem;
    }

    .app-top-bar {
        padding: 0.65rem 0;
    }

    .header-user-actions {
        display: inline-flex;
        align-items: stretch;
        gap: 0;
        margin-left: 0;
        flex: 0 0 auto;
        width: auto;
    }

    .user-chip {
        flex: 0 1 auto;
        max-width: 280px;
        height: 32px;
        margin-left: 0;
        padding: 0 0.75rem;
        font-size: 0.82rem;
        border-radius: var(--radius-sm) 0 0 var(--radius-sm);
        border-right: none;
    }

    .btn-user-key {
        height: 32px;
        width: 36px;
        margin-right: -1px;
    }

    .btn-user-key svg {
        width: 16px;
        height: 16px;
    }

    .btn-logout {
        height: 32px;
        padding: 0 0.75rem;
        font-size: 0.82rem;
        border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
        border: 1px solid var(--border-subtle);
        gap: 0.3rem;
    }

    .btn-logout span {
        display: inline;
    }

    .btn-logout svg {
        width: 14px;
        height: 14px;
    }

    .app-nav-bar {
        padding: 0.4rem 0;
    }

    /* En desktop: alineados a la izquierda */
    .nav-pills {
        display: flex;
        justify-content: flex-start;
        gap: 0.65rem;
    }

    .nav-pill-item {
        width: auto;
    }

    .nav-pill-link {
        width: auto;
        height: 38px;
        padding: 0 1.25rem;
        font-size: 0.88rem;
        gap: 0.4rem;
        border-radius: 3px;
    }

    .nav-bar-wrapper {
        flex: 0 1 auto;
    }

    .btn-theme-toggle {
        width: 38px;
        height: 38px;
        border-radius: 3px;
        margin-left: auto;
    }
}

/* --- HEADER DE PÁGINA (PAGE HEADER) --- */
.page-header {
    margin-bottom: 1rem;
}

.page-title-row {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.page-title {
    font-size: 1.45rem;
    font-weight: 700;
    color: var(--text-primary);
    letter-spacing: -0.02em;
    line-height: 1.25;
}

@media (min-width: 640px) {
    .page-title {
        font-size: 1.75rem;
    }
}

.page-subtitle {
    font-size: 0.92rem;
    color: var(--text-secondary);
}

/* --- FOOTER MODERNO COMPACTO (ALINEADO A LA IZQUIERDA) --- */
.app-footer {
    background: var(--bg-surface);
    border-top: 1px solid var(--border-subtle);
    padding: 0.85rem max(0.75rem, env(safe-area-inset-right)) max(0.85rem, env(safe-area-inset-bottom)) max(0.75rem, env(safe-area-inset-left));
    font-size: 0.80rem;
    color: var(--text-secondary);
    text-align: left;
}

@media (min-width: 640px) {
    .app-footer {
        padding: 1.25rem 1rem;
        font-size: 0.82rem;
    }
}

.footer-content {
    max-width: var(--container-max-width);
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    text-align: left;
    gap: 0.25rem;
    padding: 0 0.5rem;
}

@media (min-width: 640px) {
    .footer-content {
        padding: 0 1rem;
    }
}

.footer-brand {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    gap: 0.4rem;
    font-weight: 700;
    color: var(--text-primary);
    font-size: 0.80rem;
    white-space: nowrap;
    text-align: left;
}

@media (min-width: 640px) {
    .footer-brand {
        font-size: 0.95rem;
        gap: 0.5rem;
    }
}

.footer-brand img {
    width: 16px;
    height: 16px;
    object-fit: contain;
}

@media (min-width: 640px) {
    .footer-brand img {
        width: 20px;
        height: 20px;
    }
}

.footer-subtext {
    display: flex;
    align-items: flex-start;
    justify-content: flex-start;
    gap: 0.4rem;
    font-size: 0.63rem;
    color: var(--text-muted);
    line-height: 1.35;
    text-align: left;
}

.footer-dimuch-logo {
    height: 13px;
    width: auto;
    object-fit: contain;
    flex-shrink: 0;
    margin-top: 1px;
}

.footer-dimuch-logo.footer-logo-dark {
    display: none;
}

[data-theme="dark"] .footer-dimuch-logo.footer-logo-light {
    display: none;
}

[data-theme="dark"] .footer-dimuch-logo.footer-logo-dark {
    display: block;
}

@media (min-width: 640px) {
    .footer-subtext {
        font-size: 0.72rem;
        gap: 0.45rem;
    }

    .footer-dimuch-logo {
        height: 14px;
        margin-top: 1px;
    }
}

/* --- TRANSICIONES HTMX --- */
.htmx-request.app-main {
    opacity: 0.6;
    transition: opacity 150ms ease-out;
}

.htmx-indicator {
    opacity: 0;
    transition: opacity 200ms ease-in;
}

.htmx-request .htmx-indicator {
    opacity: 1;
}

/* --- CLASES DE UTILIDAD (UTILITIES) --- */
.d-none {
    display: none !important;
}

.d-flex {
    display: flex !important;
}

.d-block {
    display: block !important;
}

.text-accent {
    color: var(--primary-700) !important;
}

.text-secondary {
    color: var(--text-secondary) !important;
}

.text-muted {
    color: var(--text-muted) !important;
}

.mb-1 {
    margin-bottom: 0.25rem !important;
}

.mb-2 {
    margin-bottom: 0.5rem !important;
}

.mb-3 {
    margin-bottom: 0.75rem !important;
}

.mb-4 {
    margin-bottom: 1rem !important;
}

.mt-2 {
    margin-top: 0.5rem !important;
}

.mt-4 {
    margin-top: 1rem !important;
}

.py-2 {
    padding-top: 0.5rem !important;
    padding-bottom: 0.5rem !important;
}

.icon-rotate-180 {
    transform: rotate(180deg);
}

.icon-svg {
    width: 18px;
    height: 18px;
    flex-shrink: 0;
}

.icon-sm {
    width: 14px;
    height: 14px;
    flex-shrink: 0;
}

.icon-md {
    width: 20px;
    height: 20px;
    flex-shrink: 0;
}

.icon-lg {
    width: 28px;
    height: 28px;
    flex-shrink: 0;
}

/* --- REFINAMIENTOS ESPECÍFICOS DE MODO OSCURO --- */
[data-theme="dark"] .brand-title {
    color: #ffffff;
}

[data-theme="dark"] .brand-subtitle {
    color: var(--text-muted);
}

[data-theme="dark"] .brand-logo {
    filter: drop-shadow(0 2px 6px rgba(0, 0, 0, 0.5));
}

[data-theme="dark"] .btn-user-key {
    background: rgba(245, 158, 11, 0.18);
    color: #fbbf24;
    border-color: var(--border-subtle);
}

[data-theme="dark"] .btn-user-key:hover {
    background: rgba(245, 158, 11, 0.32);
    color: #fef3c7;
    border-color: #f59e0b;
    z-index: 2;
}

[data-theme="dark"] .btn-user-key.active {
    background: linear-gradient(135deg, #f59e0b 0%, #d97706 100%);
    color: #ffffff;
    border-color: #fbbf24;
    box-shadow: 0 0 10px rgba(245, 158, 11, 0.55);
    z-index: 2;
}

[data-theme="dark"] .btn-logout:hover {
    background: rgba(239, 68, 68, 0.28);
    border-color: rgba(239, 68, 68, 0.5);
}

[data-theme="dark"] .history-card.expanded .history-card-header {
    background: rgba(16, 185, 129, 0.12);
}

[data-theme="dark"] .history-card.expanded .history-card-header:hover {
    background: rgba(16, 185, 129, 0.18);
}

[data-theme="dark"] .history-card.expanded .history-month-year {
    color: var(--primary-300);
}

[data-theme="dark"] .btn-toggle-amounts-action,
[data-theme="dark"] .btn-toggle-all-amounts {
    background: linear-gradient(135deg, rgba(16, 185, 129, 0.16) 0%, rgba(5, 150, 105, 0.28) 100%);
    border-color: var(--primary-500);
    color: var(--primary-300);
}

[data-theme="dark"] .btn-toggle-amounts-action:hover,
[data-theme="dark"] .btn-toggle-all-amounts:hover {
    background: linear-gradient(135deg, rgba(16, 185, 129, 0.25) 0%, rgba(5, 150, 105, 0.4) 100%);
    color: var(--primary-200);
}

[data-theme="dark"] .liquid-masked {
    color: var(--text-muted);
}