/* ==========================================================================
   SISTEMA AMÓS / GOMIB — OTIMIZAÇÃO UNIVERSAL PARA DISPOSITIVOS MÓVEIS
   Compatibilidade total: iOS Safari, Android Chrome/WebView, Tablets e Celulares
   Foco: Fluidez nativa, navegabilidade inteligente, touch-friendly (44px+)
   ========================================================================== */

/* Prevenção de rolagem horizontal descontrolada e ajustes de toque */
html, body {
  max-width: 100vw !important;
  overflow-x: hidden !important;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

/* ==========================================================================
   REGRAS PARA SMARTPHONES E TABLETS (LARGURA <= 768px)
   ========================================================================== */
@media (max-width: 768px) {
  :root {
    --sidebar-w: 285px;
    --topbar-h: 56px;
    --dock-h: 58px;
  }

  /* ------------------------------------------------------------------------
     1. GAVETA LATERAL MÓVEL (OFF-CANVAS DRAWER)
     ------------------------------------------------------------------------ */
  .sidebar {
    position: fixed !important;
    top: 0 !important;
    bottom: 0 !important;
    left: 0 !important;
    width: min(290px, 84vw) !important;
    height: 100% !important;
    height: 100dvh !important;
    transform: translate3d(-100%, 0, 0) !important;
    z-index: 10000 !important;
    box-shadow: 12px 0 35px rgba(0, 0, 0, 0.7) !important;
    transition: transform 0.28s cubic-bezier(0.32, 0.72, 0, 1) !important;
    will-change: transform;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch !important;
  }

  .sidebar.open,
  body.sidebar-open .sidebar {
    transform: translate3d(0, 0, 0) !important;
  }

  .sidebar-close {
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    width: 38px !important;
    height: 38px !important;
    border-radius: 8px !important;
    background: rgba(255, 255, 255, 0.08) !important;
    color: #F5E6B8 !important;
    border: 1px solid rgba(255, 255, 255, 0.15) !important;
    font-size: 1.1rem !important;
    cursor: pointer !important;
  }

  .sidebar-overlay {
    position: fixed !important;
    inset: 0 !important;
    background: rgba(5, 10, 26, 0.78) !important;
    backdrop-filter: blur(6px) !important;
    -webkit-backdrop-filter: blur(6px) !important;
    z-index: 9995 !important;
    opacity: 0 !important;
    visibility: hidden !important;
    pointer-events: none !important;
    transition: opacity 0.25s ease, visibility 0.25s ease !important;
  }

  .sidebar-overlay.open,
  body.sidebar-open .sidebar-overlay {
    opacity: 1 !important;
    visibility: visible !important;
    pointer-events: auto !important;
  }

  .sidebar-nav .nav-item {
    min-height: 46px !important;
    padding: 11px 16px !important;
    font-size: 0.90rem !important;
    display: flex !important;
    align-items: center !important;
    gap: 12px !important;
  }

  /* ------------------------------------------------------------------------
     2. CONTEÚDO PRINCIPAL & ESPAÇAMENTO PARA BOTTOM DOCK
     ------------------------------------------------------------------------ */
  .main-content,
  body.sidebar-collapsed .main-content {
    margin-left: 0 !important;
    width: 100% !important;
    max-width: 100vw !important;
    padding: 12px 12px calc(76px + env(safe-area-inset-bottom, 0px)) !important;
    box-sizing: border-box !important;
    min-height: calc(100vh - 56px);
  }

  /* ------------------------------------------------------------------------
     3. TOPBAR MÓVEL OTIMIZADA
     ------------------------------------------------------------------------ */
  .topbar {
    padding: 0 12px !important;
    height: 54px !important;
    position: sticky !important;
    top: 0 !important;
    z-index: 500 !important;
    background: #FFFFFF;
    display: flex !important;
    align-items: center !important;
    justify-content: space-between !important;
    box-shadow: 0 2px 8px rgba(15, 30, 75, 0.06) !important;
  }

  body.dark-theme .topbar,
  .topbar-ateneu,
  #ateneu-topbar {
    background: rgba(9, 12, 25, 0.96) !important;
    border-bottom: 1px solid rgba(212, 175, 55, 0.25) !important;
    color: #F8FAFC !important;
  }

  .menu-toggle,
  .sidebar-toggle {
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
    width: 42px !important;
    height: 42px !important;
    min-width: 42px !important;
    min-height: 42px !important;
    background: rgba(15, 30, 75, 0.05) !important;
    border: 1px solid rgba(184, 134, 11, 0.25) !important;
    border-radius: 8px !important;
    color: #0F1E4B !important;
    cursor: pointer !important;
    margin-right: 6px !important;
  }

  .topbar-title {
    font-size: 0.95rem !important;
    max-width: 55vw !important;
    white-space: nowrap !important;
    overflow: hidden !important;
    text-overflow: ellipsis !important;
  }

  .topbar-user span:not(.topbar-avatar) {
    display: none !important;
  }

  .topbar-avatar {
    width: 36px !important;
    height: 36px !important;
    font-size: 0.85rem !important;
  }

  .btn-logout {
    padding: 6px 10px !important;
    font-size: 0.75rem !important;
    min-height: 36px !important;
  }

  /* ------------------------------------------------------------------------
     4. BARRA DE NAVEGAÇÃO INFERIOR FIXA (MOBILE DOCK APP-STYLE)
     ------------------------------------------------------------------------ */
  .mobile-bottom-dock {
    display: flex !important;
    position: fixed !important;
    bottom: 0 !important;
    left: 0 !important;
    right: 0 !important;
    height: 58px !important;
    height: calc(58px + env(safe-area-inset-bottom, 0px)) !important;
    background: #0A1432 !important;
    border-top: 1.5px solid rgba(197, 162, 83, 0.35) !important;
    box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.45) !important;
    z-index: 9980 !important;
    padding-bottom: env(safe-area-inset-bottom, 0px) !important;
    justify-content: space-around !important;
    align-items: center !important;
    user-select: none !important;
    backdrop-filter: blur(12px) !important;
    -webkit-backdrop-filter: blur(12px) !important;
  }

  .dock-item {
    display: flex !important;
    flex-direction: column !important;
    align-items: center !important;
    justify-content: center !important;
    flex: 1 !important;
    height: 58px !important;
    color: #94A3B8 !important;
    text-decoration: none !important;
    font-size: 0.68rem !important;
    font-weight: 500 !important;
    letter-spacing: 0.2px !important;
    gap: 3px !important;
    transition: all 0.2s ease !important;
    background: transparent !important;
    border: none !important;
    cursor: pointer !important;
    padding: 0 !important;
    -webkit-tap-highlight-color: transparent !important;
  }

  .dock-item svg,
  .dock-item i {
    width: 20px !important;
    height: 20px !important;
    stroke: currentColor !important;
    fill: none !important;
    stroke-width: 2 !important;
    transition: transform 0.2s ease, stroke 0.2s ease !important;
  }

  .dock-item:active {
    transform: scale(0.92) !important;
  }

  .dock-item.active {
    color: #E4C878 !important;
    font-weight: 700 !important;
  }

  .dock-item.active svg,
  .dock-item.active i {
    stroke: #E4C878 !important;
    transform: translateY(-2px) !important;
  }

  .dock-item.active::after {
    content: '' !important;
    display: block !important;
    width: 4px !important;
    height: 4px !important;
    border-radius: 50% !important;
    background: #E4C878 !important;
    margin-top: -1px !important;
  }

  /* ------------------------------------------------------------------------
     5. FORMULÁRIOS, INPUTS E EVITAÇÃO DE ZOOM NO IOS
     ------------------------------------------------------------------------ */
  input[type="text"],
  input[type="email"],
  input[type="password"],
  input[type="number"],
  input[type="date"],
  input[type="search"],
  input[type="tel"],
  select,
  textarea {
    font-size: 16px !important; /* Tamanho mínimo para anular zoom forçado no iOS Safari */
    min-height: 44px !important;
    line-height: normal !important;
    box-sizing: border-box !important;
  }

  button,
  .btn,
  .btn-sm,
  .btn-action,
  .nav-item,
  .tab-btn,
  .master-tab-btn {
    min-height: 42px !important;
    touch-action: manipulation;
  }

  /* ------------------------------------------------------------------------
     6. TABELAS E ROLAGEM TOUCH COM CONTENÇÃO
     ------------------------------------------------------------------------ */
  .table-wrap,
  .table-container,
  .table-responsive {
    width: 100% !important;
    max-width: 100% !important;
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch !important;
    display: block !important;
    border-radius: 10px !important;
    border: 1px solid #E2E8F0 !important;
    background: #FFFFFF !important;
    margin-bottom: 14px !important;
    box-shadow: inset -6px 0 8px -4px rgba(0, 0, 0, 0.08) !important;
  }

  .table-wrap table,
  .table-container table,
  .table-responsive table,
  .data-table,
  .table-custom {
    min-width: 580px !important;
    width: 100% !important;
  }

  .table-wrap td,
  .table-container td,
  .table-responsive td {
    padding: 10px 12px !important;
    font-size: 0.82rem !important;
  }

  .table-wrap th,
  .table-container th,
  .table-responsive th {
    padding: 10px 12px !important;
    font-size: 0.76rem !important;
    white-space: nowrap !important;
  }

  /* ------------------------------------------------------------------------
     7. MODAIS ESTILO "BOTTOM SHEET" NO CELULAR
     ------------------------------------------------------------------------ */
  .modal-overlay,
  .modal-backdrop,
  .modal-wrapper {
    padding: 8px !important;
    display: flex !important;
    align-items: flex-end !important;
    justify-content: center !important;
  }

  .modal,
  .modal-content,
  .modal-box,
  .modal-dialog {
    width: 100% !important;
    max-width: 540px !important;
    max-height: 88vh !important;
    max-height: 88dvh !important;
    margin: 0 auto !important;
    border-radius: 18px 18px 8px 8px !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch !important;
    padding: 18px 16px 22px !important;
    box-sizing: border-box !important;
    animation: slideUpModal 0.28s cubic-bezier(0.32, 0.72, 0, 1) !important;
  }

  @keyframes slideUpModal {
    from {
      transform: translateY(100%);
      opacity: 0;
    }
    to {
      transform: translateY(0);
      opacity: 1;
    }
  }

  .modal-header {
    position: sticky !important;
    top: 0 !important;
    background: #FFFFFF !important;
    z-index: 10 !important;
    padding-bottom: 10px !important;
    border-bottom: 1px solid #E2E8F0 !important;
    margin-bottom: 14px !important;
  }

  .modal-footer {
    position: sticky !important;
    bottom: 0 !important;
    background: #FFFFFF !important;
    z-index: 10 !important;
    padding-top: 10px !important;
    border-top: 1px solid #E2E8F0 !important;
    margin-top: 14px !important;
    display: flex !important;
    flex-direction: column !important;
    gap: 8px !important;
  }

  .modal-footer button,
  .modal-footer .btn {
    width: 100% !important;
    min-height: 44px !important;
    justify-content: center !important;
  }

  /* ------------------------------------------------------------------------
     8. CARDS, KPIS E GRIDS NO CELULAR
     ------------------------------------------------------------------------ */
  .stats-grid,
  .kpi-grid,
  .cards-grid,
  .grid-2,
  .grid-3,
  .grid-4 {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 10px !important;
  }

  .stat-card,
  .kpi-card {
    padding: 14px 12px !important;
    border-radius: 12px !important;
  }

  .stat-val,
  .kpi-val,
  .stat-number {
    font-size: 1.35rem !important;
  }

  .stat-label,
  .kpi-label {
    font-size: 0.72rem !important;
  }

  /* ------------------------------------------------------------------------
     9. FILTROS E BUSCA RESPONSIVA
     ------------------------------------------------------------------------ */
  .top-filters,
  .filters-bar,
  .table-actions-bar {
    flex-direction: column !important;
    align-items: stretch !important;
    gap: 8px !important;
  }

  .filter-select,
  .filter-input,
  .search-box {
    width: 100% !important;
    max-width: 100% !important;
  }

  .master-tabs-bar {
    padding: 8px 10px !important;
    gap: 6px !important;
  }

  .master-tab-btn {
    padding: 7px 12px !important;
    font-size: 0.76rem !important;
  }
}

/* ==========================================================================
   CARTEIRINHA 3D — RESPONSIVIDADE FLUIDA (TELAS <= 540px)
   ========================================================================== */
@media (max-width: 540px) {
  .carteira-section-layout,
  .cw-main {
    grid-template-columns: 1fr !important;
    gap: 20px !important;
    padding: 12px 10px 40px !important;
  }

  .card-stage {
    width: 100% !important;
    gap: 16px !important;
  }

  .card-scene {
    width: 100% !important;
    max-width: 375px !important;
    aspect-ratio: 1.586 / 1 !important;
    margin: 0 auto !important;
  }

  .card-face {
    border-radius: 14px !important;
  }

  /* Frente da Carteirinha */
  .cf-in {
    padding: 10px 12px !important;
  }

  .cf-logo {
    width: 36px !important;
    height: 36px !important;
    border-radius: 6px !important;
  }

  .cf-org .o1 {
    font-size: 11px !important;
  }

  .cf-org .o2 {
    font-size: 6.5px !important;
    letter-spacing: 1px !important;
  }

  .cf-body {
    margin-top: 6px !important;
    gap: 10px !important;
  }

  .cf-photo {
    width: 60px !important;
    height: 76px !important;
    border-radius: 6px !important;
  }

  .cf-photo svg {
    width: 32px !important;
    height: 32px !important;
  }

  .cf-name {
    font-size: 12.5px !important;
    line-height: 1.15 !important;
  }

  .cf-grade {
    font-size: 8px !important;
    margin: 1px 0 4px !important;
  }

  .cf-fields {
    gap: 2px 8px !important;
  }

  .cf-field .l {
    font-size: 5.5px !important;
    letter-spacing: 0.8px !important;
  }

  .cf-field .v {
    font-size: 8.5px !important;
    line-height: 1.2 !important;
  }

  .cf-funcs {
    margin-top: 4px !important;
    gap: 6px !important;
  }

  .cf-fgroup .fl {
    font-size: 5px !important;
    letter-spacing: 0.8px !important;
    margin-bottom: 2px !important;
    padding-bottom: 1px !important;
  }

  .cf-fitem {
    font-size: 7.5px !important;
    line-height: 1.2 !important;
  }

  .cf-foot {
    margin-top: 4px !important;
  }

  .cf-rgm .l {
    font-size: 5.5px !important;
  }

  .cf-rgm .v {
    font-size: 13px !important;
    letter-spacing: 1px !important;
  }

  .cf-chip {
    width: 32px !important;
    height: 23px !important;
    border-radius: 4px !important;
  }

  /* Verso da Carteirinha */
  .cb-stripe {
    height: 26px !important;
    margin-top: 8px !important;
  }

  .cb-body {
    padding: 8px 12px !important;
    gap: 10px !important;
  }

  .cb-qr {
    width: 70px !important;
    height: 70px !important;
    padding: 4px !important;
    border-radius: 6px !important;
  }

  .cb-row .l {
    font-size: 5.5px !important;
  }

  .cb-row .v {
    font-size: 8.5px !important;
  }

  .cb-dates {
    gap: 10px !important;
  }

  .cb-foot {
    font-size: 6.5px !important;
    padding: 0 12px 6px !important;
  }

  .cb-seal {
    font-size: 7px !important;
  }

  /* Ações da Carteirinha */
  .card-actions {
    flex-direction: column !important;
    width: 100% !important;
    gap: 8px !important;
  }

  .btn-act {
    width: 100% !important;
    justify-content: center !important;
    min-height: 44px !important;
  }

  .flip-hint-btn {
    width: 100% !important;
    justify-content: center !important;
    min-height: 44px !important;
  }
}

/* ==========================================================================
   DISPOSITIVOS ULTRA COMPACTOS (LARGURA <= 380px)
   ========================================================================== */
@media (max-width: 380px) {
  .stats-grid,
  .kpi-grid,
  .cards-grid {
    grid-template-columns: 1fr !important;
  }

  .topbar {
    padding: 0 8px !important;
  }

  .main-content {
    padding: 10px 8px calc(76px + env(safe-area-inset-bottom, 0px)) !important;
  }

  .card-scene {
    max-width: 320px !important;
  }

  .dock-item span {
    font-size: 0.62rem !important;
  }
}
