/* ============================================================
   Mobile App Optimizations — Insights de Mercado
   2026-05-16 — D-47..D-56 + D-54 mobile-perfect

   Estratégia: regras defensivas globais que garantem:
   - Zero horizontal scroll (overflow-x hidden + max-width 100%)
   - Touch targets >= 44×44px (WCAG 2.5.5)
   - Sem zoom auto iOS em inputs (font-size >= 16px)
   - Safe-area-inset respeitado (notch iPhone, gesture bar Android 10+)
   - PWA standalone styles (esconde elementos "voltar pro site" quando rodando como app)
   - Imagens nunca extrapolam container
   - Tabelas e charts contidos em wrappers scrolláveis

   IMPORTÂNCIA DA ORDEM: este arquivo carrega APÓS theme.css mas ANTES das CSS
   específicas de página, pra que páginas possam sobrescrever quando precisarem.
   ============================================================ */

/* ── 1. Reset defensivo de overflow horizontal — APENAS mobile ──────
   BUG (2026-05-18): aplicar `overflow-x: hidden` em html+body globalmente
   BLOQUEIA mouse.wheel no Chromium em desktop. Solução: restringir a viewports <=768px.

   BUG (2026-05-19): `overflow-x: hidden` no html/body QUEBRA `position: sticky`
   em iOS Safari quando o native <select> picker abre/fecha. O header sai da
   tela ao trocar o tipo de fluxo. Solução: usar `overflow-x: clip` (não quebra
   sticky positioning) em vez de `hidden`. Fallback: `overflow-x: hidden` para
   browsers que não suportam clip (cobertura ~98% em 2026). */
html, body {
  -webkit-tap-highlight-color: rgba(212, 175, 55, 0.18); /* gold tint no tap mobile */
  -webkit-text-size-adjust: 100%;       /* impede iOS auto-zoom em landscape */
  text-size-adjust: 100%;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
@media (max-width: 768px) {
  html, body {
    overflow-x: hidden; /* fallback para browsers antigos */
    overflow-x: clip;   /* preserva position: sticky em iOS Safari */
    width: 100%;
    max-width: 100vw;
  }
}

/* Qualquer descendente NUNCA extrapola container — pega elementos legados largos */
img, video, canvas, iframe, table, pre, code {
  max-width: 100%;
  height: auto;
}

/* Wrapper genérico pra tabelas que precisam scrollar horizontal sem mover a página */
.table-scroll-wrap,
.admin-table-wrap {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch; /* momentum scroll iOS */
}

/* ── 2. Inputs e botões — sem zoom auto iOS + touch targets ──── */
@media (max-width: 768px) {
  input, select, textarea {
    font-size: 16px !important; /* < 16px aciona zoom auto chato em iOS Safari */
    max-width: 100%;
  }
  /* Mantém o helper text pequeno mesmo com input 16px */
  .form-field__helper, .form-field__error {
    font-size: 12.5px !important;
  }
}

/* Touch targets WCAG 2.5.5 — botões e links interativos */
@media (max-width: 768px) and (pointer: coarse) {
  button:not([class*="-sm"]):not([class*="--xs"]),
  a.btn, .btn,
  .auth-btn,
  [role="button"] {
    min-height: 44px;
  }
  /* Não força em ícones grandes ou avatars */
  .avatar-dropdown__button {
    min-height: 36px; /* já é 36px, mas garante */
  }
}

/* ── 3. Safe area iOS (notch + home indicator) ───────────────── */
@supports (padding: env(safe-area-inset-top)) {
  body {
    padding-left:  env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
  }
  /* Header sticky precisa empurrar pro abaixo do notch */
  .site-header {
    padding-top: env(safe-area-inset-top);
  }
  /* Footer respeita gesture bar Android / home indicator iOS */
  .site-footer,
  .admin-footer {
    padding-bottom: max(20px, calc(env(safe-area-inset-bottom) + 12px));
  }
  /* Banners fixed pegam o notch */
  .deletion-banner,
  .pwa-modal__card {
    padding-top: max(10px, env(safe-area-inset-top));
  }
}

/* ── 4. PWA standalone — quando rodando como app instalado ────── */
@media (display-mode: standalone) {
  /* Esconde link "Voltar pro site" do admin (faz parte da chrome do navegador) */
  .admin-sidebar__link-out { display: none !important; }

  /* Header padding extra pro status bar do sistema operacional */
  .site-header,
  .admin-sidebar {
    padding-top: max(16px, env(safe-area-inset-top));
  }

  /* Body fica fullscreen sem margem */
  body {
    min-height: 100vh;
    min-height: 100dvh; /* dynamic viewport — exclui barra do browser */
  }

  /* Esconde footer com links externos no app instalado (visual mais clean) */
  .site-footer__brand { font-size: 11px; }
}

/* Indicador visual de modo PWA (apenas pra debug — esconder em prod) */
.is-pwa-only { display: none; }
@media (display-mode: standalone) {
  .is-pwa-only { display: initial; }
  .is-browser-only { display: none !important; }
}

/* ── 5. Containers principais nunca estouram em mobile ────────── */
.site-header__inner,
.hero,
section,
main,
footer {
  max-width: 100%;
}

@media (max-width: 768px) {
  .site-header__inner,
  main,
  section,
  footer {
    padding-left: 16px;
    padding-right: 16px;
  }
  /* Reduz padding de admin pra ganhar tela */
  .admin-main { padding: 14px !important; }
  .admin-card { padding: 14px 16px 16px !important; }
}

@media (max-width: 480px) {
  .site-header__inner,
  main,
  section,
  footer {
    padding-left: 12px;
    padding-right: 12px;
  }
}

/* ── 6. Charts em mobile — altura controlada (Chart.js + ApexCharts) ──── */
@media (max-width: 768px) {
  /* Chart.js: container precisa de altura fixa porque maintainAspectRatio:false.
     A altura ABSOLUTA é decidida pelo .chart-wrap específico da página (fluxo.html
     usa 380px ≤640px, dashboard.html usa 320px ≤900px). Aqui só garantimos que
     nada estoura horizontalmente — sem max-height global, que causava espaço
     vazio no card quando o wrap pedia mais altura que o canvas. */
  .chart-wrap,
  .chart-card .chart-wrap,
  canvas {
    max-width: 100% !important;
  }
  /* ApexCharts respira melhor — só limita altura máxima.
     overflow-x: hidden evita scroll horizontal; overflow-y: visible para
     não cortar tooltips/legends do gráfico. */
  .admin-chart {
    max-width: 100% !important;
    overflow-x: hidden;
    overflow-y: visible;
  }
}

/* ── 6b. Chart cards em telas muito pequenas — respiro lateral ─────── */
@media (max-width: 480px) {
  .chart-card {
    padding-left: 10px !important;
    padding-right: 10px !important;
  }
}

/* ── 7. Grid principal de páginas em mobile ───────────────────── */
@media (max-width: 768px) {
  /* Dashboard macro: grid 2 cols → 1 col */
  .grid-main,
  .grid-secondary,
  .grid-3,
  .grid-4 {
    grid-template-columns: 1fr !important;
  }
  /* Admin grids */
  .admin-grid-main,
  .admin-grid-secondary {
    grid-template-columns: 1fr !important;
  }
}

/* ── 8. Cards e elementos VIP não cortam em mobile ────────────── */
@media (max-width: 480px) {
  .auth-card,
  .pwa-modal__card,
  .resend-modal__card,
  .admin-card,
  .modal__card {
    border-radius: 12px;
    margin-left: auto;
    margin-right: auto;
    max-width: calc(100vw - 24px);
  }
}

/* ── 9. Anti-bounce iOS quando faz overscroll ─────────────────── */
body {
  overscroll-behavior-y: contain;
}

/* ── 10. Inputs nunca quebram layout em mobile pequeno ────────── */
@media (max-width: 360px) {
  .form-field__input,
  .form-field__label {
    font-size: 15px;
  }
  .auth-card__title { font-size: 19px !important; }
  .admin-kpi__value { font-size: 22px !important; }
}

/* ── 11. Sticky header sempre acima de tudo em mobile ───────────
   FIX 2026-05-19: z-index 50 estava sendo coberto por canvas Chart.js e
   ApexCharts. Bump para 1000 garante que o logo NUNCA suma — requisito
   absoluto do sócio ("logo sempre precisa ficar visivel SEMPRE!"). */
.site-header {
  z-index: 1000 !important;
  position: sticky !important;
  top: 0 !important;
}

/* ── 13. Header mobile compacto — botão "Criar conta" SEMPRE visível ──── */
@media (max-width: 768px) {
  .site-header__inner {
    padding: 10px 14px !important;
    gap: 8px !important;
  }

  /* Brand: só logo, esconde texto "INSIGHTS DE MERCADO" pra liberar espaço */
  .site-header__brand-text { display: none !important; }
  .site-header__brand img { width: 32px; height: 32px; }

  /* Nav: links menores e compactos */
  .site-header__nav {
    gap: 2px !important;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none; /* Firefox */
  }
  .site-header__nav::-webkit-scrollbar { display: none; }
  .site-header__nav a {
    padding: 6px 8px !important;
    font-size: 12px !important;
    white-space: nowrap;
  }

  /* Slot auth: botões compactos mas SEMPRE inteiros (nada sumindo) */
  .site-header__auth {
    margin-left: auto !important;
    flex-shrink: 0 !important;
    gap: 6px !important;
  }
  .site-header__auth a.site-header__auth-link {
    padding: 7px 10px !important;
    font-size: 12px !important;
    white-space: nowrap !important;
  }
}

/* ── DESKTOP (>768px) — layout do header pós-reorganização JS ──── */
/* O JS move .site-header__auth pra fora do nav. Regras desktop garantem
   o layout: [brand]  [nav 3 links centralizados]  [auth direita] em 1 linha. */
@media (min-width: 769px) {
  .site-header__inner {
    display: flex !important;
    flex-wrap: nowrap !important;
    align-items: center !important;
    gap: 16px !important;
    padding: 14px 24px !important;
  }
  .site-header__brand {
    flex-shrink: 0;
    margin-right: 0;
  }
  .site-header__nav {
    flex: 1 1 auto !important;
    display: flex !important;
    justify-content: center !important;
    align-items: center !important;
    gap: 4px !important;
    min-width: 0;
  }
  .site-header__auth {
    flex-shrink: 0 !important;
    margin-left: 0 !important; /* override: nav já cresce e empurra pra direita */
    gap: 8px;
    display: flex !important;
    align-items: center !important;
  }
  /* Hamburger fica escondido no desktop (mobile only) */
  .site-header__hamburger {
    display: none !important;
  }
}

/* ── HAMBURGER + DRAWER (mobile only) ─────────────────────────── */

/* Hamburger button — só visível em <=768px (injetado por mobile-menu.js) */
.site-header__hamburger {
  display: none;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 8px;
  color: var(--text, #e8e0cc);
  cursor: pointer;
  padding: 0;
  margin: 0 8px 0 4px;
  transition: all 180ms cubic-bezier(.4,0,.2,1);
  font-family: inherit;
}
.site-header__hamburger:hover {
  background: rgba(255,255,255,0.06);
  border-color: rgba(255,255,255,0.08);
}
.site-header__hamburger:focus-visible {
  outline: 2px solid var(--gold, #d4af37);
  outline-offset: 2px;
}

/* DRAWER — fixed full-height direita */
.site-drawer {
  position: fixed;
  inset: 0;
  z-index: 950;
  pointer-events: none;
  visibility: hidden;
}
.site-drawer.is-open {
  pointer-events: auto;
  visibility: visible;
}
.site-drawer__backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  opacity: 0;
  transition: opacity 240ms cubic-bezier(.4,0,.2,1);
}
.site-drawer.is-open .site-drawer__backdrop { opacity: 1; }

.site-drawer__panel {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(340px, 84vw);
  background: #0c0d11;
  border-left: 1px solid rgba(212, 175, 55, 0.12);
  box-shadow: -16px 0 48px rgba(0, 0, 0, 0.5);
  transform: translateX(100%);
  transition: transform 280ms cubic-bezier(.4,0,.2,1);
  display: flex;
  flex-direction: column;
  padding-top: max(16px, env(safe-area-inset-top));
  padding-bottom: max(16px, env(safe-area-inset-bottom));
}
.site-drawer.is-open .site-drawer__panel { transform: translateX(0); }

.site-drawer__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 20px 16px;
  border-bottom: 1px solid rgba(255,255,255,0.06);
}
.site-drawer__brand {
  display: flex;
  align-items: center;
  gap: 10px;
  text-decoration: none;
  font-family: Montserrat, sans-serif;
  font-weight: 700;
  font-size: 13px;
  letter-spacing: 0.05em;
  color: #e8e0cc;
}
.site-drawer__brand img { width: 28px; height: 28px; border-radius: 6px; }
.site-drawer__brand .gold { color: var(--gold, #d4af37); }
.site-drawer__close {
  width: 40px;
  height: 40px;
  background: rgba(255,255,255,0.04);
  border: 0;
  border-radius: 8px;
  color: #e8e0cc;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 200ms cubic-bezier(.4,0,.2,1);
}
.site-drawer__close:hover {
  background: rgba(255, 255, 255, 0.10);
  transform: rotate(90deg);
}

.site-drawer__nav,
.site-drawer__auth {
  padding: 16px 12px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.site-drawer__auth {
  margin-top: auto; /* empurra pro fim do drawer */
  border-top: 1px solid rgba(255,255,255,0.06);
}

.site-drawer__section-label {
  font-size: 10.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: #6b6f76;
  padding: 0 12px 8px;
}

.site-drawer__link {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 13px 14px;
  border-radius: 10px;
  color: #e8e0cc;
  text-decoration: none;
  font-family: Inter, sans-serif;
  font-size: 14.5px;
  font-weight: 500;
  background: transparent;
  border: 0;
  cursor: pointer;
  text-align: left;
  width: 100%;
  font-variant-numeric: tabular-nums;
  transition: all 180ms cubic-bezier(.4,0,.2,1);
}
.site-drawer__link:hover,
.site-drawer__link:focus-visible {
  background: rgba(255, 255, 255, 0.05);
  outline: none;
}
.site-drawer__link.is-active {
  background: rgba(212, 175, 55, 0.12);
  color: var(--gold, #d4af37);
}
.site-drawer__link-icon {
  font-size: 17px;
  width: 22px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  opacity: 0.85;
}

.site-drawer__link--gold {
  background: linear-gradient(135deg, var(--gold, #d4af37), #a78329) !important;
  color: #1a1300 !important;
  font-weight: 700;
  letter-spacing: 0.02em;
  justify-content: center;
  margin-top: 6px;
  box-shadow: 0 4px 12px rgba(212, 175, 55, 0.25);
}
.site-drawer__link--gold:hover {
  background: linear-gradient(135deg, #f5cf57, #d4af37) !important;
  transform: translateY(-1px);
}

.site-drawer__link--ghost {
  color: #b8bcc4;
}

.site-drawer__link--admin {
  color: var(--gold, #d4af37) !important;
  font-weight: 600;
  background: rgba(212, 175, 55, 0.08);
}

.site-drawer__greeting {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  margin-bottom: 8px;
  background: rgba(212, 175, 55, 0.05);
  border-radius: 10px;
  border: 1px solid rgba(212, 175, 55, 0.10);
}
.site-drawer__avatar {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--gold, #d4af37), #a78329);
  color: #1a1300;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-family: Montserrat, sans-serif;
  font-weight: 700;
  font-size: 13px;
  flex-shrink: 0;
}
.site-drawer__greeting small {
  display: block;
  color: #6b6f76;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 2px;
}
.site-drawer__greeting strong {
  display: block;
  color: #e8e0cc;
  font-size: 14px;
  font-weight: 600;
}

.site-drawer__footer {
  padding: 16px 20px;
  border-top: 1px solid rgba(255,255,255,0.06);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-size: 12px;
  color: #6b6f76;
}
.site-drawer__footer a {
  color: #b8bcc4;
  text-decoration: none;
  transition: color 180ms ease;
}
.site-drawer__footer a:hover { color: var(--gold, #d4af37); }

/* MOBILE HEADER — inline nav (drawer removido 2026-05-23, redesign Promise-first).
   Layout: [brand]  [Início] [Fluxo B3]  [auth: login-icon|avatar + CTA]
   Tudo numa linha, todos visíveis a partir de 320px. */
@media (max-width: 768px) {
  /* Header em UMA LINHA SÓ — sem wrap */
  .site-header__inner {
    display: flex !important;
    flex-wrap: nowrap !important;
    align-items: center !important;
    flex-direction: row !important;
    padding: 10px 14px !important;
    gap: 8px !important;
    min-height: 56px;
  }
  .site-header__brand {
    flex-shrink: 0;
    min-width: 0;
  }
  /* Hamburger não existe mais (drawer removido), mas se voltar a aparecer
     em alguma página legada ele continua funcional. */
  .site-header__hamburger {
    display: none !important;
  }

  /* Nav inline — VISÍVEL em mobile, ocupa o espaço entre brand e auth */
  .site-header__nav.site-header__nav--inline {
    display: flex !important;
    flex: 1 1 auto !important;
    align-items: center !important;
    gap: 6px !important;
    min-width: 0; /* permite shrink dos filhos */
    overflow: visible;
    justify-content: flex-start !important;
  }

  /* Links de texto (Início, Fluxo B3) — compactos, com truncate se preciso */
  .site-header__nav--inline > a:not(.site-header__login-icon):not(.site-header__auth-link) {
    font-size: 12.5px !important;
    padding: 6px 8px !important;
    flex-shrink: 0;
    white-space: nowrap;
  }
  .site-header__nav--inline > a.active {
    color: var(--gold) !important;
    background: var(--bg-elev-2) !important;
  }

  /* Auth slot (filho do nav) — empurra pra direita com margin-left auto */
  .site-header__nav--inline > .site-header__auth {
    display: inline-flex !important;
    visibility: visible !important;
    align-items: center !important;
    margin-left: auto !important;
    gap: 6px !important;
    flex-shrink: 0 !important;
  }
  .site-header__nav--inline .site-header__login-icon {
    flex-shrink: 0;
    width: 34px;
    height: 34px;
  }
  .site-header__nav--inline a.btn--outline-gold.site-header__auth-link {
    flex-shrink: 0;
    padding: 7px 12px !important;
    font-size: 12px !important;
    white-space: nowrap;
  }

  /* GARANTIA: avatar (logged-in) sempre visível e clicável */
  .site-header__auth .avatar-dropdown,
  .site-header__auth .avatar-dropdown__button,
  .site-header__auth .avatar-dropdown__initials {
    display: inline-flex !important;
    visibility: visible !important;
  }
  .site-header__auth .avatar-dropdown__initials {
    width: 34px !important;
    height: 34px !important;
    font-size: 12.5px !important;
  }
  .site-header__auth .avatar-dropdown__button {
    padding: 2px 4px 2px 2px !important;
  }
  /* Esconde o chevron em mobile pra ganhar espaço — tap no avatar abre o menu */
  .site-header__auth .avatar-dropdown__chevron {
    display: none !important;
  }
}

/* Mobile estreito (<=390px iPhone padrão): aperta mais sem quebrar */
@media (max-width: 390px) {
  .site-header__inner { gap: 6px !important; padding: 10px 12px !important; }
  .site-header__nav.site-header__nav--inline { gap: 4px !important; }
  .site-header__nav--inline > a:not(.site-header__login-icon):not(.site-header__auth-link) {
    font-size: 12px !important;
    padding: 5px 6px !important;
  }
  .site-header__nav--inline a.btn--outline-gold.site-header__auth-link {
    padding: 6px 10px !important;
    font-size: 11.5px !important;
  }
}

/* Mobile minúsculo (<=360px): brand sem texto */
@media (max-width: 360px) {
  .site-header__brand img { width: 28px; height: 28px; }
}

/* Reduced motion respeitado */
@media (prefers-reduced-motion: reduce) {
  .site-drawer__panel,
  .site-drawer__backdrop,
  .site-drawer__close { transition: none !important; }
}

/* ── 12. Reduce motion respect (D-55 derivado) ────────────────── */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}
