/**
 * MaisBook — App Shell (fundação responsiva compartilhada, Fase 0)
 *
 * Utilitários reaproveitáveis para dar comportamento de aplicativo no mobile.
 * Só classes com prefixo `mb-`; não altera nenhum seletor existente.
 * Tokens vindos de assets/css/tokens.css. Breakpoints: sm 480 / md 768 / lg 1024 / xl 1280.
 *
 * Enfileirado em todas as rotas públicas do MaisBook (handle `maisbook-app-shell`).
 *
 * ── Shell de página ─────────────────────────────────────────────────────────
 * .mb-shell                  coluna flex de altura mínima 100dvh (fallback 100vh)
 * .mb-shell--locked          altura fixa 100dvh sem scroll na página (telão, kiosk)
 * .mb-shell-main             região de conteúdo que cresce (flex: 1)
 * .mb-shell-main--scroll     área de scroll própria dentro de .mb-shell--locked
 * .mb-has-tabbar             reserva espaço no fim para a bottom tab bar
 * .mb-has-tabbar--persist    mantém a reserva também em md+ (com .mb-tabbar--persist)
 * .mb-has-actionbar          reserva espaço no fim para a barra de ação
 *
 * ── Header compacto ────────────────────────────────────────────────────────
 * .mb-shell-header           header sticky glass: 56px no mobile, 72px a partir de md
 * .mb-shell-header--static   mesmo visual, sem sticky
 * .mb-shell-header__inner    linha do header (marca à esquerda, ações à direita)
 * .mb-shell-header__back     botão voltar com alvo de toque de 44px
 * .mb-shell-header__title    título da tela, truncado em uma linha no mobile
 * .mb-shell-header__actions  grupo de ações à direita
 * .is-scrolled / .scrolled   estado de header rolado (glass forte + sombra)
 *
 * ── Container ──────────────────────────────────────────────────────────────
 * .mb-container              container centralizado, padding fluido + safe-area lateral
 * .mb-container--narrow      max-width 720px (formulários, fluxo de uma coluna)
 * .mb-container--wide        max-width 1440px (grids de foto)
 * .mb-container--flush       sem padding lateral (mídia sangrando até a borda)
 * .mb-measure                largura de leitura confortável (68ch) para texto corrido
 *
 * ── Bottom tab bar ─────────────────────────────────────────────────────────
 * .mb-tabbar                 barra de navegação fixa no rodapé, só abaixo de md
 * .mb-tabbar--persist        mantém a barra visível também em md+
 * .mb-tabbar__item           item de navegação (44px+ de alvo); `.is-active` = atual
 * .mb-tabbar__icon           slot do ícone (SVG 22px)
 * .mb-tabbar__label          rótulo curto abaixo do ícone
 * .mb-tabbar__badge          contador sobreposto ao ícone
 *
 * ── Barra de ação sticky ───────────────────────────────────────────────────
 * .mb-actionbar              barra de ação no rodapé (position: sticky; bottom: 0)
 * .mb-actionbar--fixed       variante fixa à viewport
 * .mb-actionbar--above-tabbar  empilha a barra de ação acima da tab bar
 * .mb-actionbar--above-tabbar-persist  mantém esse empilhamento também em md+
 * .mb-actionbar__inner       linha interna com o container do conteúdo
 * .mb-actionbar__info        texto/contador à esquerda
 * .mb-actionbar__actions     grupo de botões à direita (empilha abaixo de sm)
 * .is-hidden                 estado escondido (desliza para baixo)
 *
 * ── Sheet deslizante (substitui modal centrado no mobile) ──────────────────
 * .mb-sheet                  raiz do sheet, fechado por padrão; `.is-open` abre
 * .mb-sheet__scrim           fundo escurecido, alvo de clique para fechar
 * .mb-sheet__panel           painel: sobe do rodapé no mobile, diálogo centrado em md+
 * .mb-sheet__handle          pega visual de arrastar
 * .mb-sheet__header          cabeçalho fixo do painel
 * .mb-sheet__title           título do sheet
 * .mb-sheet__close           botão fechar (alvo de 44px)
 * .mb-sheet__body            corpo com scroll próprio
 * .mb-sheet__footer          rodapé de ações, respeita safe-area
 * .mb-sheet--full            sheet de altura cheia (100dvh)
 * .mb-scroll-lock            aplicar no <body> enquanto o sheet está aberto
 *
 * ── Alvos de toque, foco e helpers ─────────────────────────────────────────
 * .mb-tap                    garante 44x44 de área clicável
 * .mb-tap--lg                garante 56x56
 * .mb-tap-expand             expande a área de toque via ::after, sem mudar o visual
 * .mb-focus-ring             anel de foco visível em :focus-visible
 * .mb-sr-only                escondido visualmente, ainda lido por leitor de tela
 * .mb-hscroll                trilha horizontal com scroll-snap (chips, filtros)
 * .mb-no-scrollbar           esconde a barra de scroll mantendo o scroll
 * .mb-stack                  coluna com gap (--mb-stack-gap)
 * .mb-cluster                linha que quebra, com gap (--mb-cluster-gap)
 * .mb-hide-md-down           esconde abaixo de md (só desktop)
 * .mb-hide-md-up             esconde a partir de md (só mobile)
 * .mb-safe-top/.mb-safe-bottom  padding de safe-area avulso
 */

/* ==========================================================================
   1. Shell de página
   ========================================================================== */

.mb-shell {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    min-height: var(--shell-min-height, 100dvh);
    /* Impede scroll horizontal acidental em grids largos no mobile */
    overflow-x: clip;
}

.mb-shell--locked {
    height: 100vh;
    height: var(--shell-min-height, 100dvh);
    min-height: 0;
    overflow: hidden;
}

.mb-shell-main {
    flex: 1 1 auto;
    min-height: 0;
    min-width: 0;
    position: relative;
    z-index: var(--z-content, 3);
}

.mb-shell--locked .mb-shell-main--scroll,
.mb-shell-main--scroll {
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-y: contain;
}

/* Reserva de rodapé: some no desktop porque a tab bar também some. */
.mb-has-tabbar {
    padding-bottom: var(--tabbar-space, 60px);
}

.mb-has-actionbar {
    padding-bottom: var(--actionbar-space, 68px);
}

.mb-has-tabbar.mb-has-actionbar {
    padding-bottom: calc(var(--tabbar-height, 60px) + var(--actionbar-height, 68px) + var(--safe-bottom, 0px));
}

@media (min-width: 768px) {
    .mb-has-tabbar:not(.mb-has-tabbar--persist) {
        padding-bottom: 0;
    }

    .mb-has-tabbar.mb-has-actionbar:not(.mb-has-tabbar--persist) {
        padding-bottom: var(--actionbar-space, 68px);
    }
}

/* ==========================================================================
   2. Header compacto
   ========================================================================== */

.mb-shell-header {
    position: sticky;
    top: 0;
    z-index: var(--z-header, 100);
    padding-top: var(--safe-top, 0px);
    background: var(--glass-bg);
    backdrop-filter: blur(24px) saturate(1.2);
    -webkit-backdrop-filter: blur(24px) saturate(1.2);
    border-bottom: 1px solid var(--color-border);
    transition: background var(--transition-normal), box-shadow var(--transition-normal);
}

.mb-shell-header--static {
    position: static;
}

.mb-shell-header.is-scrolled,
.mb-shell-header.scrolled {
    background: var(--glass-bg-strong);
    box-shadow: var(--shadow-md);
}

.mb-shell-header__inner {
    display: flex;
    align-items: center;
    gap: var(--space-fluid-sm, 0.875rem);
    height: var(--header-height-mobile, 56px);
    width: 100%;
    max-width: var(--container-max, 1200px);
    margin-inline: auto;
    padding-inline: max(var(--space-gutter, 1rem), var(--safe-left, 0px)) max(var(--space-gutter, 1rem), var(--safe-right, 0px));
}

@media (min-width: 768px) {
    .mb-shell-header__inner {
        height: var(--header-height, 72px);
    }
}

.mb-shell-header__back {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 auto;
    min-width: var(--tap-min, 44px);
    min-height: var(--tap-min, 44px);
    margin-inline-start: calc(var(--space-fluid-2xs, 0.375rem) * -1);
    padding: 0;
    border: 0;
    border-radius: var(--radius-md);
    background: transparent;
    color: var(--color-text-muted);
    cursor: pointer;
    transition: background var(--transition-fast), color var(--transition-fast);
}

.mb-shell-header__back:hover {
    background: var(--color-surface-hover);
    color: var(--color-text);
}

.mb-shell-header__title {
    flex: 1 1 auto;
    min-width: 0;
    margin: 0;
    font-size: var(--font-size-fluid-lg, 1.0625rem);
    font-weight: var(--font-weight-semibold, 600);
    letter-spacing: var(--letter-spacing-tight);
    color: var(--color-text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.mb-shell-header__actions {
    display: flex;
    align-items: center;
    gap: var(--space-fluid-2xs, 0.375rem);
    margin-inline-start: auto;
    flex: 0 0 auto;
}

/* ==========================================================================
   3. Container responsivo
   ========================================================================== */

.mb-container {
    width: 100%;
    max-width: var(--container-max, 1200px);
    margin-inline: auto;
    padding-inline: max(var(--space-gutter, 1rem), var(--safe-left, 0px)) max(var(--space-gutter, 1rem), var(--safe-right, 0px));
}

.mb-container--narrow {
    max-width: var(--container-max-narrow, 720px);
}

.mb-container--wide {
    max-width: var(--container-max-wide, 1440px);
}

.mb-container--flush {
    padding-inline: 0;
}

.mb-measure {
    max-width: var(--content-measure, 68ch);
    font-size: var(--font-size-fluid-base, 1rem);
    line-height: var(--line-height-relaxed, 1.75);
}

/* ==========================================================================
   4. Bottom tab bar (telas autenticadas do cliente)
   ========================================================================== */

.mb-tabbar {
    position: fixed;
    inset-inline: 0;
    bottom: 0;
    z-index: var(--z-tabbar, 1200);
    display: flex;
    align-items: stretch;
    justify-content: space-around;
    gap: 2px;
    padding: 4px max(6px, var(--safe-left, 0px)) calc(4px + var(--safe-bottom, 0px)) max(6px, var(--safe-right, 0px));
    background: var(--glass-bg-strong);
    backdrop-filter: blur(22px) saturate(1.2);
    -webkit-backdrop-filter: blur(22px) saturate(1.2);
    border-top: 1px solid var(--color-border);
    box-shadow: var(--shadow-tabbar);
}

@media (min-width: 768px) {
    .mb-tabbar:not(.mb-tabbar--persist) {
        display: none;
    }
}

.mb-tabbar__item {
    flex: 1 1 0;
    min-width: 0;
    min-height: var(--tap-min, 44px);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    padding: 6px 4px;
    appearance: none;
    -webkit-appearance: none;
    border: 0;
    border-radius: var(--radius-md);
    background: transparent;
    color: var(--color-text-muted);
    font-family: inherit;
    font-size: var(--font-size-xs);
    font-weight: var(--font-weight-semibold, 600);
    line-height: 1;
    text-decoration: none;
    cursor: pointer;
    position: relative;
    transition: color var(--transition-fast), background var(--transition-fast);
}

.mb-tabbar__item:hover {
    color: var(--color-text);
}

.mb-tabbar__item.is-active {
    color: var(--color-primary);
    background: var(--color-primary-muted);
}

.mb-tabbar__icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 22px;
    height: 22px;
    flex: 0 0 auto;
}

.mb-tabbar__icon svg {
    width: 100%;
    height: 100%;
    stroke: currentColor;
}

.mb-tabbar__label {
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    letter-spacing: 0.005em;
}

.mb-tabbar__badge {
    position: absolute;
    top: 2px;
    inset-inline-end: max(6px, 18%);
    min-width: 18px;
    height: 18px;
    padding: 0 5px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-full);
    background: var(--color-primary);
    color: #fff;
    font-size: 0.6875rem;
    font-weight: var(--font-weight-bold, 700);
    line-height: 1;
}

/* ==========================================================================
   5. Barra de ação sticky (seleção, aprovação)
   ========================================================================== */

.mb-actionbar {
    position: sticky;
    bottom: 0;
    z-index: var(--z-actionbar, 1100);
    background: var(--glass-bg-strong);
    backdrop-filter: blur(22px) saturate(1.2);
    -webkit-backdrop-filter: blur(22px) saturate(1.2);
    border-top: 1px solid var(--color-border);
    box-shadow: var(--shadow-actionbar);
    transition: transform var(--transition-normal), opacity var(--transition-normal);
}

.mb-actionbar--fixed {
    position: fixed;
    inset-inline: 0;
}

/* Quando as duas barras coexistem, a de ação senta em cima da tab bar. */
.mb-actionbar--above-tabbar {
    bottom: var(--tabbar-space, 60px);
}

@media (min-width: 768px) {
    .mb-actionbar--above-tabbar:not(.mb-actionbar--above-tabbar-persist) {
        bottom: 0;
    }
}

.mb-actionbar.is-hidden {
    transform: translateY(110%);
    opacity: 0;
    pointer-events: none;
}

.mb-actionbar__inner {
    display: flex;
    align-items: center;
    gap: var(--space-fluid-sm, 0.875rem);
    width: 100%;
    max-width: var(--container-max, 1200px);
    margin-inline: auto;
    min-height: var(--actionbar-height, 68px);
    padding: var(--space-fluid-xs, 0.625rem) max(var(--space-gutter, 1rem), var(--safe-left, 0px));
    padding-inline-end: max(var(--space-gutter, 1rem), var(--safe-right, 0px));
    padding-bottom: calc(var(--space-fluid-xs, 0.625rem) + var(--safe-bottom, 0px));
}

.mb-actionbar__info {
    flex: 1 1 auto;
    min-width: 0;
    font-size: var(--font-size-fluid-sm, 0.9rem);
    color: var(--color-text-muted);
    line-height: var(--line-height-snug, 1.35);
}

.mb-actionbar__actions {
    display: flex;
    align-items: center;
    gap: var(--space-fluid-2xs, 0.5rem);
    flex: 0 0 auto;
}

.mb-actionbar__actions > * {
    min-height: var(--tap-min, 44px);
}

@media (max-width: 479px) {
    .mb-actionbar__inner {
        flex-direction: column;
        align-items: stretch;
        gap: var(--space-fluid-2xs, 0.5rem);
    }

    .mb-actionbar__actions {
        width: 100%;
    }

    .mb-actionbar__actions > * {
        flex: 1 1 0;
        justify-content: center;
    }
}

/* ==========================================================================
   6. Sheet deslizante
   ========================================================================== */

.mb-sheet {
    position: fixed;
    inset: 0;
    z-index: var(--z-sheet, 9500);
    display: flex;
    align-items: flex-end;
    justify-content: center;
    opacity: 0;
    visibility: hidden;
    transition: opacity var(--transition-normal), visibility var(--transition-normal);
}

.mb-sheet.is-open {
    opacity: 1;
    visibility: visible;
}

.mb-sheet__scrim {
    position: absolute;
    inset: 0;
    z-index: 0;
    appearance: none;
    -webkit-appearance: none;
    border: 0;
    padding: 0;
    background: var(--color-scrim);
    cursor: pointer;
}

.mb-sheet__panel {
    position: relative;
    z-index: 1;
    display: flex;
    flex-direction: column;
    width: 100%;
    max-height: var(--sheet-max-height, 88dvh);
    background: var(--color-bg-card);
    border: 1px solid var(--color-border);
    border-bottom: 0;
    border-start-start-radius: var(--sheet-radius, 30px);
    border-start-end-radius: var(--sheet-radius, 30px);
    box-shadow: var(--shadow-sheet);
    transform: translateY(100%);
    transition: transform var(--transition-sheet, 320ms ease);
}

.mb-sheet.is-open .mb-sheet__panel {
    transform: translateY(0);
}

.mb-sheet--full .mb-sheet__panel {
    max-height: 100dvh;
    height: 100dvh;
    border-radius: 0;
    border: 0;
}

/* Em md+ o sheet vira diálogo centrado com fade + subida curta. */
@media (min-width: 768px) {
    .mb-sheet {
        align-items: center;
    }

    .mb-sheet__panel {
        width: min(var(--sheet-width-md, 520px), calc(100% - var(--space-fluid-xl, 2rem)));
        max-height: min(var(--sheet-max-height, 88dvh), 720px);
        border: 1px solid var(--color-border);
        border-radius: var(--radius-lg);
        transform: translateY(16px) scale(0.98);
    }

    .mb-sheet.is-open .mb-sheet__panel {
        transform: translateY(0) scale(1);
    }

    .mb-sheet--full .mb-sheet__panel {
        height: auto;
    }
}

.mb-sheet__handle {
    flex: 0 0 auto;
    width: 40px;
    height: 4px;
    margin: 10px auto 0;
    border-radius: var(--radius-full);
    background: var(--color-border-strong);
}

@media (min-width: 768px) {
    .mb-sheet__handle {
        display: none;
    }
}

.mb-sheet__header {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    gap: var(--space-fluid-xs, 0.625rem);
    padding: var(--space-fluid-sm, 0.875rem) var(--space-fluid-md, 1.25rem);
    border-bottom: 1px solid var(--color-border);
}

.mb-sheet__title {
    flex: 1 1 auto;
    min-width: 0;
    margin: 0;
    font-size: var(--font-size-fluid-lg, 1.0625rem);
    font-weight: var(--font-weight-bold, 700);
    color: var(--color-text);
    letter-spacing: var(--letter-spacing-tight);
}

.mb-sheet__close {
    flex: 0 0 auto;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: var(--tap-min, 44px);
    min-height: var(--tap-min, 44px);
    margin-inline-end: calc(var(--space-fluid-2xs, 0.375rem) * -1);
    padding: 0;
    border: 0;
    border-radius: var(--radius-md);
    background: transparent;
    color: var(--color-text-muted);
    cursor: pointer;
    transition: background var(--transition-fast), color var(--transition-fast);
}

.mb-sheet__close:hover {
    background: var(--color-surface-hover);
    color: var(--color-text);
}

.mb-sheet__body {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    padding: var(--space-fluid-md, 1.25rem);
}

.mb-sheet__footer {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    gap: var(--space-fluid-2xs, 0.5rem);
    padding: var(--space-fluid-sm, 0.875rem) var(--space-fluid-md, 1.25rem);
    padding-bottom: calc(var(--space-fluid-sm, 0.875rem) + var(--safe-bottom, 0px));
    border-top: 1px solid var(--color-border);
    background: var(--color-bg-card);
}

.mb-sheet__footer > * {
    flex: 1 1 0;
    min-height: var(--tap-min, 44px);
    justify-content: center;
}

@media (min-width: 768px) {
    .mb-sheet__footer {
        justify-content: flex-end;
        padding-bottom: var(--space-fluid-sm, 0.875rem);
    }

    .mb-sheet__footer > * {
        flex: 0 0 auto;
    }
}

/* Aplicar no <body> enquanto o sheet está aberto (o JS das fases seguintes
   deve guardar/restaurar o scrollTop; aqui só travamos o scroll). */
.mb-scroll-lock {
    overflow: hidden !important;
    touch-action: none;
}

/* ==========================================================================
   7. Alvos de toque, foco e helpers
   ========================================================================== */

.mb-tap {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: var(--tap-min, 44px);
    min-height: var(--tap-min, 44px);
}

.mb-tap--lg {
    min-width: var(--tap-min-lg, 56px);
    min-height: var(--tap-min-lg, 56px);
}

/* Para ícones pequenos que não podem crescer visualmente. */
.mb-tap-expand {
    position: relative;
}

.mb-tap-expand::after {
    content: "";
    position: absolute;
    top: 50%;
    left: 50%;
    width: max(100%, var(--tap-min, 44px));
    height: max(100%, var(--tap-min, 44px));
    transform: translate(-50%, -50%);
}

.mb-focus-ring:focus-visible,
.mb-tabbar__item:focus-visible,
.mb-sheet__close:focus-visible,
.mb-sheet__scrim:focus-visible,
.mb-shell-header__back:focus-visible {
    outline: 2px solid var(--color-focus);
    outline-offset: 2px;
    box-shadow: var(--focus-ring);
}

.mb-sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    padding: 0;
    overflow: hidden;
    clip: rect(0 0 0 0);
    clip-path: inset(50%);
    white-space: nowrap;
    border: 0;
}

.mb-hscroll {
    display: flex;
    align-items: center;
    gap: var(--space-fluid-2xs, 0.5rem);
    overflow-x: auto;
    scroll-snap-type: x proximity;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-x: contain;
    padding-bottom: 2px;
}

.mb-hscroll > * {
    flex: 0 0 auto;
    scroll-snap-align: start;
}

.mb-no-scrollbar,
.mb-hscroll {
    scrollbar-width: none;
    -ms-overflow-style: none;
}

.mb-no-scrollbar::-webkit-scrollbar,
.mb-hscroll::-webkit-scrollbar {
    width: 0;
    height: 0;
    display: none;
}

.mb-stack {
    display: flex;
    flex-direction: column;
    gap: var(--mb-stack-gap, var(--space-fluid-md, 1.25rem));
}

.mb-cluster {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--mb-cluster-gap, var(--space-fluid-xs, 0.625rem));
}

.mb-safe-top {
    padding-top: var(--safe-top, 0px);
}

.mb-safe-bottom {
    padding-bottom: var(--safe-bottom, 0px);
}

@media (max-width: 767px) {
    .mb-hide-md-down {
        display: none !important;
    }
}

@media (min-width: 768px) {
    .mb-hide-md-up {
        display: none !important;
    }
}

/* ==========================================================================
   8. prefers-reduced-motion
   ========================================================================== */

@media (prefers-reduced-motion: reduce) {

    .mb-shell-header,
    .mb-tabbar__item,
    .mb-actionbar,
    .mb-sheet,
    .mb-sheet__panel,
    .mb-sheet__close,
    .mb-shell-header__back {
        transition-duration: 1ms !important;
    }

    .mb-sheet__panel,
    .mb-sheet.is-open .mb-sheet__panel {
        transform: none !important;
    }

    .mb-actionbar.is-hidden {
        transform: none !important;
    }

    .mb-hscroll {
        scroll-behavior: auto;
    }
}
