/*
 * Footer Mobile — Layout vertical centralizado
 * Site Unimed Ceará
 *
 * Camada desacoplada sobre css/main.css, mobile.css e demais estilos.
 * Escopo: max-width 768px.
 * Desktop (>768px) permanece com o layout legado.
 *
 * Princípio: um bloco principal por linha, fluxo vertical, sem colunas lado a lado.
 */

/* --------------------------------------------------------------------------
 * Tokens
 * -------------------------------------------------------------------------- */

@media only screen and (max-width: 768px) {
    body.institucional {
        --footer-gap: clamp(8px, 2vw, 12px);
        --footer-gap-lg: clamp(12px, 3vw, 16px);
        --footer-padding: clamp(10px, 3vw, 14px);
        --footer-radius: 12px;
        --footer-radius-sm: 8px;
        --footer-radius-md: 10px;
        --footer-bg: #f1f1f1;
        --footer-menu-bg: #0a5f55;
        --footer-card-bg: #ffffff;
        --footer-touch: 44px;
        --footer-block-width: calc(100% - (var(--footer-padding) * 2));
        --footer-menu-item-gap: clamp(2px, 0.6vw, 4px);
        /* STYLES-09 — alinhado ao inline footer legado (bottom 20px / right 90px) + safe area iOS */
        --footer-chat-offset-bottom: max(20px, env(safe-area-inset-bottom, 0px));
        --footer-chat-offset-right: max(90px, env(safe-area-inset-right, 0px));
        --footer-safe-bottom: clamp(72px, 18vw, 96px);
    }
}

/* --------------------------------------------------------------------------
 * Bloco base reutilizável (um card por linha)
 * -------------------------------------------------------------------------- */

@media only screen and (max-width: 768px) {
    body.institucional .footer-mobile-block {
        grid-column: 1;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        width: var(--footer-block-width);
        max-width: var(--footer-block-width);
        margin: var(--footer-gap) auto 0;
        padding: var(--footer-padding);
        box-sizing: border-box;
        background: var(--footer-card-bg);
        border-radius: var(--footer-radius-md);
        text-align: center;
        float: none;
        clear: both;
        position: static;
        height: auto;
        min-height: 0;
        overflow: visible;
    }
}

/* --------------------------------------------------------------------------
 * Zona do footer — coluna única no body (integra siblings fora do <footer>)
 * -------------------------------------------------------------------------- */

@media only screen and (max-width: 768px) {
    body.institucional {
        display: grid;
        grid-template-columns: minmax(0, 1fr);
        gap: 0;
        box-sizing: border-box;
    }

    /* RD Station / Bricks — popups injetados antes do header no <body> */
    body.institucional > div[id^="popup-"],
    body.institucional > [id^="bricks-component-"][id$="-wrapper"] {
        grid-column: 1;
        order: -2;
        height: 0 !important;
        max-height: 0 !important;
        margin: 0 !important;
        padding: 0 !important;
        overflow: hidden !important;
        visibility: hidden !important;
        pointer-events: none !important;
        display: none !important;
    }

    body.institucional > .container-externo,
    body.institucional > header {
        grid-column: 1;
        order: -1;
    }

    /*
     * STYLES-06 — Header fixo e offset do conteúdo: canônico em mobile.css
     * (seção Header, ≤1024px). Não duplicar position/margin-top aqui.
     * Este bloco mantém apenas o posicionamento no grid do body.
     */

    /* Scripts/styles não ocupam espaço visual no grid */
    body.institucional > style,
    body.institucional > script {
        grid-column: 1;
        order: 0;
        height: 0;
        margin: 0;
        padding: 0;
        overflow: hidden;
        visibility: hidden;
    }

    body.institucional > div[vw],
    body.institucional > #chat-g4-widget {
        grid-column: 1;
        order: 0;
        height: 0;
        margin: 0;
        padding: 0;
        overflow: visible;
    }

    body.institucional > footer {
        display: contents !important;
        margin: 0 !important;
    }

    /* ------------------------------------------------------------------
     * Ordem visual do footer (mobile):
     * 1 menuAcessoRapido → 2 Logo ANS → 3 Endereço → 4 Redes
     * → 5 DPOEasy → 6 Privacidade → 7 Cookies → 8 Copyright
     *
     * Chat / Atendente Virtual: widget flutuante (fora do fluxo do footer).
     * ------------------------------------------------------------------ */

    /* Chat — container neutro; widget flutuante permanece fixo na viewport */
    body.institucional footer > .fab {
        grid-column: 1;
        order: 0;
        height: 0;
        margin: 0;
        padding: 0;
        overflow: visible;
        border: none;
        background: transparent;
    }

    body.institucional footer > .fab #frame {
        display: none !important;
    }

    body.institucional footer .endereco {
        display: contents;
    }

    /* Bloco 1 — Menu Acesso Rápido */
    body.institucional footer #menuAcessoRapido {
        order: 1;
        grid-column: 1;
        display: flex;
        flex-direction: column;
        position: relative;
        width: var(--footer-block-width);
        max-width: var(--footer-block-width);
        margin: var(--footer-gap-lg) auto 0;
        padding: var(--footer-gap) 0;
        box-sizing: border-box;
        background: var(--footer-menu-bg);
        border-radius: var(--footer-radius);
        overflow: hidden;
        float: none;
        height: auto;
        min-height: 0;
    }

    body.institucional footer #menuAcessoRapido .container-interno.row-fluid {
        display: flex !important;
        flex-direction: column;
        float: none !important;
        gap: var(--footer-menu-item-gap);
        width: 100%;
        max-width: 100%;
        margin: 0;
        padding: 0;
        box-sizing: border-box;
    }

    body.institucional footer #menuAcessoRapido .agregador,
    body.institucional footer #menuAcessoRapido .agregador.span2 {
        width: 100% !important;
        max-width: 100% !important;
        float: none !important;
        display: block !important;
        margin: 0 !important;
        box-sizing: border-box;
        border-bottom: 1px solid rgba(255, 255, 255, 0.25);
    }

    body.institucional footer #menuAcessoRapido .agregador:last-child {
        border-bottom: none;
    }

    body.institucional footer #menuAcessoRapido .agregador li {
        position: relative;
        display: flex;
        align-items: stretch;
        min-height: calc(var(--footer-touch) + 6px);
        box-sizing: border-box;
        float: none !important;
    }

    body.institucional footer #menuAcessoRapido .agregador a.tb6 {
        display: flex;
        align-items: center;
        width: 100%;
        box-sizing: border-box;
        padding: 14px var(--footer-padding);
        padding-right: calc(var(--footer-touch) + 16px);
        min-height: calc(var(--footer-touch) + 6px);
        text-align: left;
        line-height: 1.45;
        font-size: 0.95rem;
        color: #ffffff;
        float: none !important;
        position: static;
    }

    body.institucional footer #menuAcessoRapido .btn-collapse-menu {
        position: absolute;
        top: 50%;
        right: var(--footer-padding);
        transform: translateY(-50%);
        min-width: var(--footer-touch);
        min-height: var(--footer-touch);
        display: flex;
        align-items: center;
        justify-content: center;
        border: none;
        background: transparent;
        color: #ffffff;
        border-radius: var(--footer-radius-sm);
        padding: 0;
    }

    body.institucional footer #menuAcessoRapido .agregador-submenu {
        border-radius: 0;
    }

    body.institucional footer #menuAcessoRapido::after {
        content: none;
        display: none;
    }

    /* Bloco 2 — Logo ANS */
    body.institucional footer .endereco .logo-ans {
        order: 2;
        grid-column: 1;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: var(--footer-gap);
        width: var(--footer-block-width);
        max-width: var(--footer-block-width);
        margin: var(--footer-gap) auto 0;
        padding: var(--footer-padding);
        background: var(--footer-card-bg);
        border-radius: var(--footer-radius-md);
        box-sizing: border-box;
        text-align: center;
        float: none;
    }

    body.institucional footer .endereco .logo-ans a {
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: var(--footer-gap);
    }

    body.institucional footer .endereco .logo-ans img {
        max-width: 92px;
        width: auto;
        height: auto;
        object-fit: contain;
        display: block;
        margin: 0 auto;
    }

    /* Bloco 3 — Endereço */
    body.institucional footer .endereco address {
        order: 3;
        grid-column: 1;
        float: none;
        width: var(--footer-block-width);
        max-width: var(--footer-block-width);
        margin: var(--footer-gap) auto 0;
        padding: var(--footer-padding);
        background: var(--footer-card-bg);
        border-radius: var(--footer-radius-md);
        box-sizing: border-box;
        font-size: 0.85rem;
        line-height: 1.5;
        text-align: center;
        overflow-wrap: break-word;
        word-wrap: break-word;
    }

    body.institucional footer .endereco address center {
        display: block;
        text-align: center;
    }

    /* Bloco 4 — Redes Sociais */
    body.institucional footer .endereco .nav-social {
        order: 4;
        grid-column: 1;
        float: none !important;
        clear: both;
        display: flex;
        flex-direction: row;
        flex-wrap: wrap;
        align-items: center;
        justify-content: center;
        gap: var(--footer-gap);
        width: var(--footer-block-width);
        max-width: var(--footer-block-width);
        margin: var(--footer-gap) auto 0;
        padding: var(--footer-padding);
        background: var(--footer-card-bg);
        border-radius: var(--footer-radius-md);
        box-sizing: border-box;
        text-align: center;
    }

    body.institucional footer .endereco .nav-social h4 {
        margin: 0;
        flex: 1 1 100%;
        width: 100%;
        text-align: center;
        font-size: 0.95rem;
    }

    body.institucional footer .endereco .nav-social a {
        float: none !important;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        min-width: var(--footer-touch);
        min-height: var(--footer-touch);
        border-radius: var(--footer-radius-sm);
        margin: 0;
    }

    body.institucional footer .endereco br[clear] {
        display: none;
    }

    /* Bloco 5 — DPOEasy */
    body.institucional #dpoeasy-banner {
        order: 5;
        grid-column: 1;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        width: var(--footer-block-width);
        max-width: var(--footer-block-width);
        margin: var(--footer-gap) auto 0;
        padding: var(--footer-padding);
        box-sizing: border-box;
        background: var(--footer-card-bg);
        border-radius: var(--footer-radius-md);
        text-align: center;
        overflow: visible;
        height: auto;
    }

    body.institucional #dpoeasy-banner img,
    body.institucional #dpoeasy-banner iframe,
    body.institucional #dpoeasy-banner button {
        max-width: 100%;
        max-height: 80px;
        width: auto;
        height: auto;
        object-fit: contain;
        border-radius: var(--footer-radius-sm);
        margin: 0 auto;
    }

    /* Bloco 6 — Selo de privacidade (após #dpoeasy-banner) */
    body.institucional footer ~ #dpoeasy-banner + div[style*="text-align"] {
        order: 6;
        grid-column: 1;
        display: flex;
        align-items: center;
        justify-content: center;
        width: var(--footer-block-width);
        max-width: var(--footer-block-width);
        margin: var(--footer-gap) auto 0;
        padding: var(--footer-padding);
        box-sizing: border-box;
        background: var(--footer-card-bg);
        border-radius: var(--footer-radius-md);
        text-align: center;
        height: auto;
    }

    body.institucional footer ~ #dpoeasy-banner + div[style*="text-align"] a {
        display: flex;
        align-items: center;
        justify-content: center;
    }

    body.institucional footer ~ #dpoeasy-banner + div[style*="text-align"] img {
        max-width: 100%;
        max-height: 72px;
        width: auto;
        height: auto;
        object-fit: contain;
        border-radius: var(--footer-radius-sm);
        margin: 0 auto;
    }

    /* Bloco 7 — Cookies (banner legado + widget DPO no fluxo) */
    body.institucional .aviso-cookie {
        order: 7;
        grid-column: 1;
        position: relative !important;
        bottom: auto !important;
        width: var(--footer-block-width);
        max-width: var(--footer-block-width);
        margin: var(--footer-gap) auto 0;
        border-radius: var(--footer-radius-md);
        box-sizing: border-box;
        padding: var(--footer-padding);
        text-align: center;
        height: auto;
        z-index: auto;
    }

    body.institucional .aviso-cookie-content {
        max-width: 100%;
        padding: var(--footer-padding);
        margin: 0 auto;
        box-sizing: border-box;
        border-radius: var(--footer-radius-sm);
        text-align: center;
    }

    body.institucional .aviso-cookie .btn-ok {
        border-radius: var(--footer-radius-sm);
        min-height: var(--footer-touch);
        display: inline-flex;
        align-items: center;
        justify-content: center;
        padding: 8px 16px;
        margin-top: var(--footer-gap);
    }

    /* Bloco 8 — Copyright */
    body.institucional footer .footer-copyright {
        order: 8;
        grid-column: 1;
        display: block;
        width: var(--footer-block-width);
        max-width: var(--footer-block-width);
        margin: var(--footer-gap) auto var(--footer-safe-bottom);
        padding: var(--footer-padding);
        box-sizing: border-box;
        background: var(--footer-card-bg);
        border-radius: var(--footer-radius-md);
        font-size: 0.8rem;
        line-height: 1.45;
        text-align: center;
        opacity: 0.9;
        color: inherit;
    }

    /* Widget flutuante — Atendente Virtual (STYLES-09: cascata reconciliada com footer inline legado) */
    body.institucional #chat-g4-widget.chat-container {
        display: flex !important;
        align-items: center;
        gap: 10px;
        cursor: pointer;
        position: fixed !important;
        bottom: var(--footer-chat-offset-bottom) !important;
        right: var(--footer-chat-offset-right) !important;
        left: auto !important;
        top: auto !important;
        z-index: 9999 !important;
        width: auto !important;
        height: auto !important;
        margin: 0;
        box-sizing: border-box;
        overflow: visible;
        transform: none !important;
    }

    body.institucional #chat-g4-widget .chat-balloon {
        position: absolute !important;
        bottom: calc(100% + 10px) !important;
        left: auto !important;
        right: 0 !important;
        display: block !important;
        visibility: visible !important;
        opacity: 1 !important;
        white-space: nowrap;
        min-width: 0;
        max-width: none;
        font-size: 18px;
        line-height: 1.35;
        text-align: center;
        margin: 0;
    }

    body.institucional #chat-g4-widget #chat-g4 > div {
        position: static !important;
    }

    body.institucional #chat-g4-widget #chat-g4 > div > button {
        position: static !important;
        bottom: auto !important;
        right: auto !important;
        width: 75px !important;
        height: 75px !important;
        border: 4px solid #f47920 !important;
        border-radius: 50% !important;
    }

    /* Neutralizar legado global do footer */
    body.institucional footer .endereco.relative {
        position: static;
    }

    body.institucional footer .margin-top-2x,
    body.institucional footer .margin-bottom-2x {
        margin-top: 0;
        margin-bottom: 0;
    }

    body.institucional footer .padding-left-2x,
    body.institucional footer .padding-right-2x {
        padding-left: 0;
        padding-right: 0;
    }

    body.institucional footer {
        margin: 0 !important;
        height: auto;
    }

    /* Neutraliza margem global legada: footer { margin: 40px 0 0 } */
    footer {
        margin-top: 0 !important;
    }
}

/* --------------------------------------------------------------------------
 * Mobile pequeno
 * -------------------------------------------------------------------------- */

@media only screen and (max-width: 320px) {
    body.institucional {
        --footer-chat-offset-right: max(90px, env(safe-area-inset-right, 0px));
        --footer-safe-bottom: clamp(72px, 22vw, 96px);
    }

    body.institucional footer .endereco address {
        font-size: 0.8rem;
    }

    body.institucional footer #menuAcessoRapido .agregador a.tb6 {
        padding: 12px 12px;
        padding-right: calc(var(--footer-touch) + 10px);
        font-size: 0.875rem;
        line-height: 1.4;
    }

    body.institucional #chat-g4-widget .chat-balloon {
        font-size: 16px;
    }
}
