/* ==========================================================================
   AUTO PROD BOT — Mobile App Layout
   Foundation v1: bottom nav, header app, cards, full-screen modals
   Ativado apenas em viewports <= 767.98px.
   ========================================================================== */

/* ===== Safe area (notch/Dynamic Island/home indicator) ===== */
:root {
  --safe-area-top: env(safe-area-inset-top, 0px);
  --safe-area-bottom: env(safe-area-inset-bottom, 0px);
  --mobile-header-h: 56px;
  --mobile-bottomnav-h: 64px;
}

/* ============================================================
   Desktop: esconde elementos mobile-only e mostra os de desktop
   ============================================================ */
.mobile-only { display: none !important; }
.desktop-only { display: initial; }

/* Estado padrão dos containers de tema mobile no desktop */
body.has-mobile-app .desktop-nav { display: block; }
body.has-mobile-app .mobile-app-header,
body.has-mobile-app .mobile-app-bottomnav { display: none; }

/* ============================================================
   Mobile breakpoint
   ============================================================ */
@media (max-width: 767.98px) {

  .mobile-only { display: initial !important; }
  .desktop-only { display: none !important; }

  /* Reset body — visual de app, sem scrollbar feia */
  body {
    background: var(--color-bg-page, #f5f6fa);
    -webkit-font-smoothing: antialiased;
    -webkit-tap-highlight-color: transparent;
    overscroll-behavior-y: contain;
  }
  body.has-mobile-app {
    padding-top: calc(var(--mobile-header-h) + var(--safe-area-top));
    padding-bottom: calc(var(--mobile-bottomnav-h) + var(--safe-area-bottom));
  }
  body.has-mobile-app .desktop-nav { display: none !important; }

  /* Esconder o botão flutuante de suporte quando o bottom nav está visível */
  body.has-mobile-app .whatsapp-float {
    bottom: calc(var(--mobile-bottomnav-h) + var(--safe-area-bottom) + 12px) !important;
    right: 12px !important;
    width: 48px;
    height: 48px;
    font-size: 1.4rem;
  }

  /* Container principal — sem largura forçada, padding ajustado */
  .container,
  .container-fluid,
  .container-lg,
  .container-md,
  .container-sm,
  .container-xl,
  .container-xxl {
    padding-left: 12px !important;
    padding-right: 12px !important;
    margin-top: 0 !important;
  }

  /* ============================================================
     Mobile Header (app bar fixo no topo)
     ============================================================ */
  .mobile-app-header {
    position: fixed;
    top: 0; left: 0; right: 0;
    height: calc(var(--mobile-header-h) + var(--safe-area-top));
    padding-top: var(--safe-area-top);
    background: var(--color-primary, #6c5ce7);
    color: #fff;
    z-index: 1030;
    display: flex !important;
    align-items: center;
    justify-content: space-between;
    padding-left: 12px;
    padding-right: 12px;
    box-shadow: 0 1px 6px rgba(0,0,0,0.12);
  }
  .mobile-app-header .mh-title {
    font-size: 1.05rem;
    font-weight: 600;
    color: #fff;
    text-decoration: none;
    display: flex;
    align-items: center;
    gap: 8px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    flex: 1;
  }
  .mobile-app-header .mh-title i {
    font-size: 1.2rem;
  }
  .mobile-app-header .mh-actions {
    display: flex;
    align-items: center;
    gap: 6px;
  }
  .mobile-app-header .mh-btn {
    background: transparent;
    border: 0;
    color: #fff;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.1rem;
    text-decoration: none;
  }
  .mobile-app-header .mh-btn:active {
    background: rgba(255,255,255,0.15);
  }
  .mobile-app-header .mh-back {
    margin-right: 4px;
  }

  /* User dropdown trigger dentro do header */
  .mobile-app-header .mh-user-trigger {
    width: 36px; height: 36px;
    border-radius: 50%;
    background: rgba(255,255,255,0.18);
    color: #fff;
    display: flex; align-items: center; justify-content: center;
    border: 0;
    font-weight: 600;
  }

  /* ============================================================
     Bottom Navigation (5 itens fixos)
     ============================================================ */
  .mobile-app-bottomnav {
    position: fixed;
    left: 0; right: 0; bottom: 0;
    height: calc(var(--mobile-bottomnav-h) + var(--safe-area-bottom));
    padding-bottom: var(--safe-area-bottom);
    background: #fff;
    border-top: 1px solid rgba(0,0,0,0.08);
    z-index: 1030;
    display: flex !important;
    align-items: stretch;
    justify-content: space-around;
    box-shadow: 0 -2px 12px rgba(0,0,0,0.06);
  }
  .mobile-app-bottomnav .mb-item {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    color: #8a8a98;
    text-decoration: none;
    font-size: 0.7rem;
    padding: 8px 2px;
    transition: color 0.15s ease;
    min-height: 44px;
    text-align: center;
  }
  .mobile-app-bottomnav .mb-item span {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 100%;
  }
  .mobile-app-bottomnav .mb-item i {
    font-size: 1.15rem;
    line-height: 1;
  }
  /* Telas muito estreitas: esconde labels, mantém só ícones */
  @media (max-width: 359px) {
    .mobile-app-bottomnav .mb-item span { display: none; }
    .mobile-app-bottomnav .mb-item i { font-size: 1.35rem; }
  }
  .mobile-app-bottomnav .mb-item.active {
    color: var(--color-primary, #6c5ce7);
  }
  .mobile-app-bottomnav .mb-item.active i {
    transform: translateY(-1px);
  }
  .mobile-app-bottomnav .mb-item:active {
    background: rgba(108, 92, 231, 0.06);
  }
  .mobile-app-bottomnav .mb-badge {
    position: absolute;
    top: 6px;
    right: 25%;
    background: var(--color-danger, #dc3545);
    color: #fff;
    font-size: 0.6rem;
    padding: 1px 5px;
    border-radius: 8px;
    line-height: 1.2;
  }

  /* ============================================================
     Cards/listas estilo app
     ============================================================ */
  .card {
    border-radius: 14px;
    border: 0 !important;
    box-shadow: 0 1px 4px rgba(0,0,0,0.06);
    margin-bottom: 12px;
  }
  .card-header {
    border-bottom: 1px solid rgba(0,0,0,0.05);
    background: transparent !important;
    padding: 14px 16px;
    border-radius: 14px 14px 0 0;
  }
  .card-body {
    padding: 14px 16px;
  }
  .card-header h4, .card-header h5 {
    font-size: 1rem;
    margin: 0;
  }

  /* ============================================================
     Tabelas → viram cards no mobile (opt-in via .table-as-cards)
     Para tabelas que NÃO usam .table-as-cards, mantém scroll horizontal.
     ============================================================ */
  .table-responsive {
    border: 0;
  }
  .table {
    font-size: 0.85rem;
  }
  .table-as-cards thead { display: none; }
  .table-as-cards tbody tr {
    display: block;
    background: #fff;
    border-radius: 12px;
    box-shadow: 0 1px 4px rgba(0,0,0,0.05);
    padding: 12px;
    margin-bottom: 10px;
  }
  .table-as-cards tbody td {
    display: block;
    border: 0 !important;
    padding: 4px 0 !important;
    text-align: left !important;
  }
  .table-as-cards tbody td::before {
    content: attr(data-label);
    display: inline-block;
    font-weight: 600;
    color: var(--color-text-muted, #6c757d);
    margin-right: 8px;
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.03em;
  }

  /* ============================================================
     Botões — touch-friendly
     ============================================================ */
  .btn {
    min-height: 44px;
    padding: 10px 16px;
    border-radius: 10px;
    font-weight: 500;
  }
  .btn-sm {
    min-height: 36px;
    padding: 6px 12px;
    font-size: 0.85rem;
  }
  .btn-block-mobile {
    width: 100%;
  }

  /* Forms — inputs maiores */
  .form-control, .form-select {
    min-height: 44px;
    font-size: 1rem;
    border-radius: 10px;
  }
  .form-label {
    font-weight: 600;
    margin-bottom: 6px;
  }

  /* ============================================================
     Modais full-screen no mobile
     ============================================================ */
  .modal-dialog:not(.modal-dialog-centered) {
    margin: 0;
    max-width: 100%;
    min-height: 100vh;
  }
  .modal-content {
    border-radius: 0;
    border: 0;
    min-height: 100vh;
  }
  .modal-header {
    padding: 14px 16px;
    border-bottom: 1px solid rgba(0,0,0,0.06);
  }
  .modal-footer {
    position: sticky;
    bottom: 0;
    background: #fff;
    padding: 12px 16px;
    border-top: 1px solid rgba(0,0,0,0.06);
  }
  .modal-footer .btn { flex: 1; }

  /* Modal centralizado (modais pequenos, ex.: alertas) */
  .modal-dialog-centered {
    margin: 12px;
    min-height: calc(100vh - 24px);
  }
  .modal-dialog-centered .modal-content {
    min-height: auto;
    border-radius: 16px;
  }

  /* ============================================================
     Alertas (flashes) compactos
     ============================================================ */
  .alert {
    border-radius: 12px;
    padding: 10px 14px;
    font-size: 0.9rem;
  }

  /* ============================================================
     Tipografia
     ============================================================ */
  h1 { font-size: 1.4rem; }
  h2 { font-size: 1.2rem; }
  h3 { font-size: 1.1rem; }
  h4 { font-size: 1rem; }
  h5 { font-size: 0.95rem; }
  h6 { font-size: 0.85rem; }

  /* ============================================================
     Páginas auth (login/registro) — full-screen
     ============================================================ */
  body.mobile-auth {
    padding-top: 0 !important;
    padding-bottom: 0 !important;
  }
  body.mobile-auth .mobile-app-header,
  body.mobile-auth .mobile-app-bottomnav { display: none !important; }
}

/* ============================================================
   Dark theme support (mobile)
   Redefinido no nível dos TOKENS: componentes que usam
   var(--color-*) herdam o tema; utilitários claros do Bootstrap
   (.bg-light/.bg-white) e as CSS vars de tabela são neutralizados
   para não produzir texto claro sobre fundo branco.
   ============================================================ */
@media (prefers-color-scheme: dark) {
  @media (max-width: 767.98px) {
    body.has-mobile-app {
      --color-bg-page: #15151f;
      --color-bg-card: #1d1d2b;
      --color-bg-input: #262637;
      --color-bg-hover: rgba(255, 255, 255, 0.06);
      --color-text: #e5e7eb;
      --color-text-secondary: #b3b8c2;
      --color-text-muted: #9ca3af;
      --color-border: rgba(255, 255, 255, 0.16);
      --color-border-light: rgba(255, 255, 255, 0.08);
      background: var(--color-bg-page);
      color: var(--color-text);
    }
    .mobile-app-bottomnav {
      background: #1d1d2b;
      border-top-color: rgba(255,255,255,0.06);
    }
    .mobile-app-bottomnav .mb-item { color: #9ca3af; }
    body.has-mobile-app .card,
    body.has-mobile-app .table-as-cards tbody tr {
      background: var(--color-bg-card) !important;
      color: var(--color-text);
    }
    body.has-mobile-app .modal-content,
    body.has-mobile-app .modal-footer {
      background: var(--color-bg-card);
      color: var(--color-text);
    }
    /* Superfícies claras do Bootstrap herdariam texto claro do card
       escuro → viram superfícies escuras equivalentes */
    body.has-mobile-app .bg-light,
    body.has-mobile-app .bg-white {
      background-color: var(--color-bg-input) !important;
      color: var(--color-text);
    }
    body.has-mobile-app .text-muted,
    body.has-mobile-app .text-secondary {
      color: var(--color-text-muted) !important;
    }
    body.has-mobile-app .border,
    body.has-mobile-app .border-top,
    body.has-mobile-app .border-bottom,
    body.has-mobile-app .border-start,
    body.has-mobile-app .border-end {
      border-color: var(--color-border-light) !important;
    }
    /* Tabelas Bootstrap 5.3 definem cor/fundo via CSS vars próprias */
    body.has-mobile-app .table {
      --bs-table-color: var(--color-text);
      --bs-table-bg: transparent;
      --bs-table-striped-color: var(--color-text);
      --bs-table-hover-color: var(--color-text);
      --bs-table-hover-bg: var(--color-bg-hover);
    }
    body.has-mobile-app .form-control,
    body.has-mobile-app .form-select,
    body.has-mobile-app .input-group-text {
      background-color: var(--color-bg-input);
      border-color: var(--color-border);
      color: var(--color-text);
    }
    body.has-mobile-app .form-control::placeholder {
      color: var(--color-text-muted);
    }
    body.has-mobile-app .list-group-item,
    body.has-mobile-app .dropdown-menu {
      background-color: var(--color-bg-card);
      color: var(--color-text);
      border-color: var(--color-border-light);
    }
    body.has-mobile-app .dropdown-item { color: var(--color-text); }
    body.has-mobile-app .dropdown-item:hover,
    body.has-mobile-app .dropdown-item:focus {
      background-color: var(--color-bg-hover);
      color: var(--color-text);
    }
    body.has-mobile-app .modal-header {
      border-bottom-color: var(--color-border-light);
    }
    body.has-mobile-app .btn-close {
      filter: invert(1) grayscale(100%) brightness(200%);
    }
  }
}
