/* Se carga después de los estilos de componentes para corregir también los
   formularios Razor con CSS aislado. */
*, *::before, *::after { box-sizing: border-box; }

html,
body { max-width: 100%; }

img,
svg,
video,
canvas { max-width: 100%; }

:where(.row, [class*="grid"], [class*="flex"]) > * { min-width: 0; }

.app-action-busy {
    position: relative !important;
    min-width: var(--app-action-button-width, auto) !important;
    pointer-events: none !important;
    cursor: wait !important;
    filter: saturate(.82);
}

.app-action-busy::after {
    content: "";
    position: absolute;
    top: calc(50% - .5rem);
    right: .75rem;
    display: block;
    width: 1rem;
    height: 1rem;
    border: 2px solid currentColor;
    border-right-color: transparent;
    border-radius: 50%;
    animation: app-action-button-spin .68s linear infinite;
}

.app-action-busy:is(input)::after { content: none; }

/* Si el componente ya dibuja su propio indicador, se conserva uno solo. */
.app-action-busy:has(.spinner-border, .spinner-grow, [class*="spinner"], [class*="loading"])::after {
    display: none;
}

@keyframes app-action-button-spin { to { transform: rotate(360deg); } }

@media (max-width: 767.98px) {
    html,
    body {
        width: 100%;
        min-height: var(--app-visual-viewport-height, 100dvh);
        overflow-x: clip !important;
        scroll-padding-top: calc(9.2rem + env(safe-area-inset-top, 0px)) !important;
    }

    /* Todo destino de validación o navegación automática se detiene debajo
       del encabezado fijo y permanece completamente visible. */
    html body .app-content-surface :where([id], [tabindex="-1"], [role="alert"], input, select, textarea) {
        scroll-margin-top: calc(9.2rem + env(safe-area-inset-top, 0px)) !important;
    }

    html body .app-shell {
        width: 100%;
        max-width: 100%;
        min-height: var(--app-visual-viewport-height, 100dvh);
        overflow-x: clip;
    }

    html body :where(input:not([type="checkbox"]):not([type="radio"]), select, textarea, [contenteditable="true"]) {
        max-width: 100%;
        font-size: 16px !important;
        scroll-margin-top: 5.5rem;
        scroll-margin-bottom: 5.5rem;
    }

    body.app-keyboard-open {
        min-height: var(--app-visual-viewport-height, 100dvh);
        padding-bottom: var(--app-keyboard-inset, 0px);
    }

    body.app-keyboard-open :where(.app-content, .app-content-surface, form, .modal-body, [class*="-body"]) {
        scroll-padding-top: 5.5rem;
        scroll-padding-bottom: calc(5rem + var(--app-keyboard-inset, 0px));
    }

    body.app-keyboard-open :where(.modal.show, [class$="-overlay"], [class$="-backdrop"]) {
        top: var(--app-visual-viewport-offset-top, 0px);
        height: var(--app-visual-viewport-height, 100dvh);
        max-height: var(--app-visual-viewport-height, 100dvh);
    }

    body.app-keyboard-open .modal-dialog,
    body.app-keyboard-open .modal-content {
        max-height: calc(var(--app-visual-viewport-height, 100dvh) - .5rem) !important;
    }

    body.app-keyboard-open .portal-connectivity { display: none; }

    /* Saldos y pagos: la página es el único scroll. Los historiales crecen
       completos y cada modal conserva solamente su propio cuerpo desplazable. */
    html body .resident-balances .pp-history {
        max-height: none !important;
        overflow: visible !important;
        overscroll-behavior: auto !important;
        scrollbar-gutter: auto !important;
        padding-right: 0 !important;
    }

    html body :is(.pp-benefits-modal, .pp-bank-modal, .pp-success-modal) {
        max-height: calc(var(--app-visual-viewport-height, 100dvh) - .5rem) !important;
    }

    html body .pp-success-modal { overflow-y: auto !important; }

    html body .rb-detail-drawer {
        width: 100vw !important;
        height: var(--app-visual-viewport-height, 100dvh) !important;
        max-height: var(--app-visual-viewport-height, 100dvh) !important;
    }
}

/* Capa responsive transversal por perfiles.
   Se mantiene al final para cubrir estilos aislados de Razor sin modificar
   el comportamiento de escritorio ni convertir tablas densas en formularios. */
@media (max-width: 767.98px) {
    :root {
        --app-mobile-font: .9rem;
        --app-mobile-body-font: .84rem;
        --app-mobile-small-font: .78rem;
        --app-mobile-detail-font: .72rem;
        --app-mobile-action-font: .86rem;
        --app-mobile-touch-target: 2.75rem;
        --app-mobile-gutter: clamp(.55rem, 2.8vw, .9rem);
    }

    /* Acceso, registro, residente, administrador, porteria y superadministrador. */
    html body :is(
        .login-page, .validate-client-page, .identity-page, .recover-page, .reset-page,
        .resident-page, .family-page, .pet-page, .telephone-page,
        .app-content-surface, .sa-layout > main) {
        font-size: var(--app-mobile-font) !important;
        line-height: 1.5;
        text-size-adjust: 100%;
        -webkit-text-size-adjust: 100%;
    }

    html body :is(.app-content-surface, .sa-layout > main)
        :is(p, li, label, legend, dd, dt, td, th, span:not([class*="icon"]), strong) {
        overflow-wrap: anywhere;
        word-break: normal;
    }

    html body :is(.app-content-surface, .sa-layout > main) p,
    html body :is(.app-content-surface, .sa-layout > main) li,
    html body :is(.app-content-surface, .sa-layout > main) label,
    html body :is(.app-content-surface, .sa-layout > main) legend {
        font-size: var(--app-mobile-body-font);
        line-height: 1.5 !important;
    }

    html body :is(.app-content-surface, .sa-layout > main) small {
        font-size: var(--app-mobile-small-font);
        line-height: 1.4 !important;
        overflow-wrap: anywhere;
    }

    html body :is(.app-content-surface, .sa-layout > main)
        :where([class*="kicker"], [class*="eyebrow"], [class*="caption"], [class*="badge"], [class*="status"], [class*="label"]) {
        font-size: var(--app-mobile-detail-font);
        line-height: 1.3 !important;
        overflow-wrap: anywhere;
    }

    html body :is(.app-content-surface, .sa-layout > main) h1 {
        font-size: clamp(1.12rem, 5.1vw, 1.32rem);
        line-height: 1.16 !important;
        overflow-wrap: anywhere;
    }

    html body :is(.app-content-surface, .sa-layout > main) h2 {
        font-size: clamp(1.08rem, 5vw, 1.28rem);
        line-height: 1.2 !important;
        overflow-wrap: anywhere;
    }

    html body :is(.app-content-surface, .sa-layout > main) h3 {
        font-size: clamp(.98rem, 4.2vw, 1.1rem);
        line-height: 1.25 !important;
        overflow-wrap: anywhere;
    }

    /* Formularios legibles y sin zoom automatico en iOS. */
    html body :is(.app-content-surface, .sa-layout > main)
        :is(input:not([type="checkbox"]):not([type="radio"]), select, textarea, .form-control, .form-select) {
        width: 100%;
        max-width: 100% !important;
        min-width: 0 !important;
        min-height: 3rem;
        padding: .7rem .8rem;
        font-size: 16px !important;
        line-height: 1.35 !important;
    }

    html body :is(.app-content-surface, .sa-layout > main) textarea {
        min-height: 7rem;
    }

    html body :is(.app-content-surface, .sa-layout > main)
        :is(button, .btn, a[class*="button"], a[class*="action"]) {
        max-width: 100%;
        min-height: var(--app-mobile-touch-target);
        font-size: var(--app-mobile-action-font);
        line-height: 1.2 !important;
        white-space: normal;
        overflow-wrap: anywhere;
        text-align: center;
    }

    /* Los botones exclusivamente iconograficos conservan una huella cuadrada. */
    html body :is(.app-content-surface, .sa-layout > main)
        :is(button, .btn)[aria-label]:not(:has(span:not([aria-hidden="true"]))) {
        min-width: var(--app-mobile-touch-target);
        flex: 0 0 auto;
    }

    /* Contenedores y rejillas internas nunca exceden el viewport. */
    html body :is(.app-content-surface, .sa-layout > main)
        :is(main, section, article, form, fieldset, .card, [class*="shell"], [class*="panel"], [class*="content"]) {
        max-width: 100%;
        min-width: 0;
    }

    html body .app-content-surface > :is(main, section, [class*="page"], [class*="shell"], .container, .container-fluid),
    html body .sa-layout > main > :is(main, section, [class*="page"], [class*="shell"], .container, .container-fluid) {
        width: 100% !important;
        max-width: 100% !important;
        min-width: 0 !important;
        margin-inline: 0 !important;
        padding-inline: var(--app-mobile-gutter) !important;
    }

    html body :is(.app-content-surface, .sa-layout > main)
        :is([class*="form-grid"], [class*="detail-grid"], [class*="config-grid"],
            [class*="summary-grid"], [class*="stats-grid"], [class*="card-grid"],
            [class*="fields-grid"], [class*="filter-grid"], [class*="toolbar-grid"]) {
        grid-template-columns: minmax(0, 1fr) !important;
    }

    html body :is(.app-content-surface, .sa-layout > main)
        :is([class*="actions"], [class*="toolbar"], [class*="header-actions"], [class*="footer-actions"]) {
        max-width: 100%;
        flex-wrap: wrap;
    }

    html body :is(.app-content-surface, .sa-layout > main)
        :is(header, [class*="-header"], [class*="-hero"], [class*="-heading"]) {
        max-width: 100%;
        min-width: 0;
        flex-wrap: wrap;
    }

    /* Tablas administrativas: texto movil legible y un solo scroll horizontal. */
    html body :is(.app-content-surface, .sa-layout > main)
        :is(.table-responsive, [class*="table-wrap"], [class*="table-scroll"], [class*="table-container"]) {
        display: block;
        width: 100% !important;
        max-width: 100% !important;
        overflow-x: auto !important;
        overflow-y: hidden !important;
        overscroll-behavior-inline: contain;
        -webkit-overflow-scrolling: touch;
        scrollbar-gutter: stable;
    }

    html body :is(.app-content-surface, .sa-layout > main) table {
        font-size: .875rem !important;
    }

    html body :is(.app-content-surface, .sa-layout > main) table :is(th, td) {
        padding: .65rem .7rem !important;
        font-size: .875rem !important;
        line-height: 1.35 !important;
    }

    /* Dialogos y paneles laterales usan el ancho util del telefono. */
    html body :is(.modal-dialog, [class*="-drawer"], [class$="-modal"], [class*="detail-modal"], [class*="manage-modal"]) {
        width: min(100%, calc(100vw - .5rem));
        max-width: calc(100vw - .5rem) !important;
        max-height: calc(100dvh - .5rem);
    }

    html body .modal-dialog {
        width: calc(100vw - .5rem) !important;
        margin: .25rem auto !important;
    }

    html body .modal-content {
        max-height: calc(100dvh - .5rem) !important;
        border-radius: 1rem !important;
        overflow: hidden;
    }

    html body .modal-body {
        min-height: 0;
        overflow-y: auto !important;
        padding: var(--app-mobile-gutter) !important;
        overscroll-behavior: contain;
    }

    html body .modal-footer {
        display: grid !important;
        grid-template-columns: minmax(0, 1fr) !important;
        gap: .55rem !important;
        padding: .7rem var(--app-mobile-gutter) max(.7rem, env(safe-area-inset-bottom, 0px)) !important;
    }

    html body .modal-footer :is(button, .btn, a) {
        width: 100% !important;
        margin: 0 !important;
    }

    /* Superadministrador no usa MainLayout y necesita su propia cabecera movil. */
    html body .sa-layout__header {
        min-height: 4.25rem;
        height: auto;
        gap: .55rem;
        padding: .55rem var(--app-mobile-gutter);
    }

    html body .sa-layout__brand {
        min-width: 0;
        flex: 1 1 auto;
    }

    html body .sa-layout__brand img {
        width: 2.35rem;
        height: 2.35rem;
        flex: 0 0 auto;
    }

    html body .sa-layout__brand span {
        min-width: 0;
    }

    html body .sa-layout__brand strong {
        font-size: .9rem;
        overflow-wrap: anywhere;
    }

    html body .sa-layout__logout {
        min-width: 2.75rem;
        min-height: 2.75rem;
        padding: .55rem .7rem;
        font-size: .875rem;
        white-space: normal;
        text-align: center;
    }

    html body .sa-layout > main {
        width: 100%;
        padding: .55rem 0 calc(1rem + env(safe-area-inset-bottom, 0px));
        overflow-x: clip;
    }
}

/* Un solo desplazamiento vertical para todas las páginas. Los componentes
   raíz crecen con su contenido y el desplazamiento pertenece al documento.
   Se excluyen deliberadamente modales, paneles laterales y menús flotantes. */
html body :is(
    .app-shell, .app-main, .app-content, .app-content-surface,
    .sa-layout, .sa-layout > main,
    .login-page, .validate-client-page, .identity-page, .recover-page, .reset-page,
    .resident-page, .family-page, .pet-page, .telephone-page) {
    height: auto !important;
    max-height: none !important;
    overflow-x: clip !important;
    overflow-y: visible !important;
}

html body .app-content-surface > :where(
    [class$="-page"], [class*="-page "],
    [class$="-shell"], [class*="-shell "],
    [class$="-view"], [class*="-view "]),
html body .sa-layout > main > :where(
    [class$="-page"], [class*="-page "],
    [class$="-shell"], [class*="-shell "],
    [class$="-view"], [class*="-view "]) {
    height: auto !important;
    max-height: none !important;
    overflow-x: clip !important;
    overflow-y: visible !important;
}

@media (max-width: 389.98px) {
    :root {
        --app-mobile-gutter: .5rem;
    }

    html body .sa-layout__brand small {
        display: none;
    }

    html body .sa-layout__logout {
        font-size: .8rem;
    }

    html body :is(.app-content-surface, .sa-layout > main)
        :is(button, .btn, a[class*="button"], a[class*="action"]) {
        padding-inline: .65rem;
    }
}

/* Escala móvil uniforme para acceso y registro. Mantiene los controles en
   16px para evitar el zoom automático de iOS y usa objetivos táctiles de 48px. */
@media (max-width: 767.98px) {
    html body :is(
        .login-page,
        .validate-client-page,
        .resident-page,
        .family-page,
        .pet-page,
        .telephone-page,
        .recover-page,
        .reset-page) {
        font-size: 1rem !important;
        line-height: 1.5;
        overflow-x: clip;
    }

    html body :is(.login-container, .identity-shell, .validate-client-shell,
        .resident-shell, .family-shell, .pet-shell, .telephone-shell,
        .recover-page > .container, .reset-page > .container) {
        width: 100% !important;
        max-width: 100% !important;
        padding-inline: clamp(.55rem, 3vw, .9rem) !important;
    }

    html body :is(.login-card, .identity-card, .validate-client-panel,
        .resident-card, .family-card, .pet-card, .telephone-card) {
        width: 100% !important;
        max-width: 100% !important;
        min-width: 0 !important;
        border-radius: 1.25rem !important;
    }

    html body :is(.resident-card, .family-card, .pet-card, .telephone-card) > .card-body {
        padding: .9rem !important;
    }

    html body :is(.login-page, .resident-page, .family-page, .pet-page, .telephone-page)
        :is(input:not([type="checkbox"]):not([type="radio"]), select, textarea, .form-control, .form-select) {
        min-height: 3.15rem !important;
        font-size: 16px !important;
        line-height: 1.35 !important;
    }

    html body .validate-client-page .validate-client-search-input {
        min-height: 3.15rem !important;
        font-size: 16px !important;
    }

    html body :is(.login-page, .identity-page, .validate-client-page,
        .resident-page, .family-page, .pet-page, .telephone-page)
        :is(label, .resident-label, .family-label, .pet-label, .telephone-label) {
        font-size: 1rem !important;
        line-height: 1.35 !important;
    }

    html body :is(.resident-intro, .family-intro, .pet-intro, .telephone-intro) h1,
    html body :is(.login-card, .identity-card, .recover-panel, .reset-panel) h1 {
        font-size: clamp(1.35rem, 6vw, 1.65rem) !important;
        line-height: 1.15 !important;
    }

    html body :is(.resident-intro, .family-intro, .pet-intro, .telephone-intro) p,
    html body :is(.login-card, .identity-card, .recover-panel, .reset-panel) p,
    html body :is(.resident-notice-copy, .family-notice-copy, .pet-notice-copy, .telephone-notice-copy) {
        font-size: .94rem !important;
        line-height: 1.5 !important;
    }

    html body :is(.resident-section-title, .family-section-title, .pet-section-title, .telephone-section-title,
        .resident-notice-title, .family-notice-title, .pet-notice-title, .telephone-notice-title) {
        font-size: 1rem !important;
        line-height: 1.3 !important;
    }

    html body :is(.login-submit-primary, .login-action-pill, .identity-primary-action,
        .validate-client-button, .resident-submit,
        .resident-btn-primary, .resident-btn-secondary,
        .family-btn-primary, .family-btn-secondary, .family-btn-danger,
        .pet-btn-primary, .pet-btn-secondary, .pet-btn-danger,
        .telephone-btn-primary, .telephone-btn-secondary, .telephone-btn-danger) {
        min-height: 3rem !important;
        padding: .68rem .9rem !important;
        font-size: 1rem !important;
        line-height: 1.2 !important;
    }

    html body :is(.login-return-link, .recover-return-link, .reset-return-link, .identity-return-link) {
        font-size: .9rem !important;
    }

    html body :is(.recover-topbar, .reset-topbar) {
        align-items: stretch !important;
    }

    html body :is(.recover-return-link, .reset-return-link) {
        flex: 1 1 auto;
        justify-content: center;
        text-align: center;
    }

    html body :is(.recover-panel-body, .reset-panel-body, .recover-form-surface, .reset-form-surface) {
        padding-inline: .85rem !important;
    }

    html body :is(.table-responsive, [class*="table-scroll"], [class*="table-wrapper"]) {
        max-width: 100%;
        overflow-x: auto;
        overscroll-behavior-inline: contain;
        -webkit-overflow-scrolling: touch;
    }

    html body .login-logo-shell {
        width: clamp(108px, 32vw, 145px) !important;
        min-height: clamp(108px, 32vw, 145px) !important;
        margin-bottom: .75rem !important;
        padding: .75rem !important;
        border-radius: 1.3rem !important;
    }

    html body .login-card {
        padding: .9rem !important;
    }

    html body .login-form :is(.form-group.mt-4, .text-center.mt-4) {
        margin-top: .85rem !important;
    }

    html body .login-actions {
        gap: .6rem !important;
    }

    html body :is(.resident-input-icon, .family-input-icon, .pet-input-icon, .telephone-input-icon) {
        width: 3.1rem !important;
        min-width: 3.1rem !important;
        font-size: 1.05rem !important;
    }

    html body :is(.resident-intro, .family-intro, .pet-intro, .telephone-intro,
        .family-member-card, .pet-entry-card, .telephone-phone-card,
        .resident-notice, .family-notice, .pet-notice, .telephone-notice) {
        padding: .85rem !important;
        border-radius: 1.1rem !important;
    }

    html body :is(.resident-form, .family-form, .pet-form, .telephone-form)
        :is(.validation-message, .field-validation-error) {
        font-size: .9rem !important;
        line-height: 1.35 !important;
    }

    html body :is(.portal-save-confirm-modal, .pet-document-modal, .pet-card-modal,
        .pet-vaccine-modal, .pet-virtual-vaccine-modal, .login-modal-dialog) {
        width: calc(100vw - .75rem) !important;
        max-width: calc(100vw - .75rem) !important;
        margin: .375rem auto !important;
    }

    html body :is(.portal-save-confirm-modal, .pet-document-modal, .pet-card-modal,
        .pet-vaccine-modal, .pet-virtual-vaccine-modal, .login-modal-dialog) .modal-content {
        max-height: calc(100dvh - .75rem) !important;
        border-radius: 1.1rem !important;
        overflow: hidden !important;
    }

    html body :is(.portal-save-confirm-modal, .pet-document-modal, .pet-card-modal,
        .pet-vaccine-modal, .pet-virtual-vaccine-modal, .login-modal-dialog)
        :is(.modal-body, [class$="-modal__body"], [class$="-modal-body"]) {
        min-height: 0;
        overflow-y: auto !important;
        overscroll-behavior: contain;
        padding: .85rem !important;
        font-size: .94rem !important;
    }

    html body :is(.portal-save-confirm-modal, .pet-document-modal, .pet-card-modal,
        .pet-vaccine-modal, .pet-virtual-vaccine-modal, .login-modal-dialog)
        :is(.modal-footer, [class$="-modal-footer"], [class$="-footer"]) {
        display: flex !important;
        flex-direction: column !important;
        gap: .55rem !important;
        padding: .65rem .85rem max(.75rem, env(safe-area-inset-bottom, 0px)) !important;
    }

    html body :is(.portal-save-confirm-modal, .pet-document-modal, .pet-card-modal,
        .pet-vaccine-modal, .pet-virtual-vaccine-modal, .login-modal-dialog)
        :is(.modal-footer, [class$="-modal-footer"], [class$="-footer"]) :is(button, .btn, a) {
        width: 100% !important;
        min-height: 3rem !important;
        margin: 0 !important;
        justify-content: center !important;
        font-size: 1rem !important;
    }

    html body .login-modal-header {
        padding: .85rem 3.25rem .85rem .85rem !important;
    }

    html body .login-modal-header-copy h2 {
        font-size: 1.1rem !important;
    }

    html body :is(.login-modal-option-copy, .login-modal-phones-copy, .login-modal-list) {
        font-size: .92rem !important;
        line-height: 1.5 !important;
    }

    html body .pet-document-item {
        grid-template-columns: 2.5rem minmax(0, 1fr) !important;
        gap: .7rem !important;
        padding: .8rem !important;
    }

    html body .pet-document-item__actions {
        grid-column: 1 / -1;
        width: 100%;
    }

    html body .pet-document-item__actions .btn {
        min-height: 2.75rem !important;
        font-size: .94rem !important;
    }
}

@media (max-width: 360px) {
    html body :is(.resident-card, .family-card, .pet-card, .telephone-card) > .card-body,
    html body .login-card {
        padding: .72rem !important;
    }

    html body :is(.resident-shell, .family-shell, .pet-shell, .telephone-shell, .login-container) {
        padding-inline: .4rem !important;
    }
}

/* Los modales de Saldos y pagos siempre quedan sobre el layout fijo. */
html body :is(.rb-drawer-overlay, .pp-benefits-overlay, .pp-bank-overlay, .pp-success-overlay) {
    z-index: 3200 !important;
}

/* Impide que el fondo produzca un segundo scroll mientras el modal está abierto. */
body:has(.rb-drawer-overlay, .pp-benefits-overlay, .pp-bank-overlay, .pp-success-overlay) {
    overflow: hidden !important;
    overscroll-behavior: none !important;
}

/* Bloqueo compatible con la aplicación instalada y navegadores móviles que
   no aplican :has de forma consistente durante actualizaciones de Blazor. */
body.app-global-modal-open {
    overflow: hidden !important;
    overscroll-behavior: none !important;
}

/* Jerarquía única: ningún modal u overlay puede quedar atrapado debajo del
   encabezado fijo. Los selectores de fecha permanecen sobre esos modales. */
html body .modal { z-index: 5000 !important; }
html body > .modal-backdrop { z-index: 4990 !important; }
html body :where([class$="-overlay"], [class*="-overlay "]) { z-index: 5000 !important; }
html body .app-date-dialog { z-index: 6000 !important; }

/* Saldos y pagos se comporta como una página raíz, no como una página dentro
   de otra: sin marcos, alturas ni scrolls intermedios. */
html body .app-shell.is-full-window-route :where(.app-main, .app-content, .app-content-surface, .resident-balances, .rb-view, .pp-shell) {
    height: auto !important;
    max-height: none !important;
    overflow: visible !important;
    contain: none !important;
    isolation: auto !important;
}

html body .app-shell.is-full-window-route .resident-balances {
    width: 100% !important;
    max-width: none !important;
    margin: 0 !important;
    padding: 1rem clamp(.75rem, 2.5vw, 2rem) 6rem !important;
}

html body .app-shell.is-full-window-route .pp-history {
    max-height: none !important;
    overflow: visible !important;
    scrollbar-gutter: auto !important;
}

@media (max-width: 767.98px) {
    html body .app-shell.is-full-window-route .app-content {
        padding: 0 0 calc(1.5rem + env(safe-area-inset-bottom, 0px)) !important;
    }

    html body .app-shell.is-full-window-route .app-content-surface {
        min-height: calc(100dvh - 8.45rem - env(safe-area-inset-top, 0px)) !important;
    }

    html body .app-shell.is-full-window-route .resident-balances {
        padding: .35rem .25rem 5.5rem !important;
    }
}
