/* =========================================================================
   MOBILE RESPONSIVE — Refinos completos para todas as telas
   Sobrescreve as regras mobile do app.css base.

   Estratégia:
   - Sidebar: tabbar inferior fixo
   - Conversas: lista vira drawer, conversa ocupa 100%, painel lateral some
     (acessível via modal de contato)
   - Pipeline: scroll horizontal com colunas mais estreitas
   - Tarefas/Dashboard/Outros: grid colapsa para 1 coluna
   ========================================================================= */

/* === Esconde o botão de voltar do mobile em qualquer tela > 768px === */
/* (o botão é inserido no DOM pelo mobile-responsive.js, mas só deve aparecer
   no mobile real; sem essa regra, ele vaza pro desktop como "quadrado branco") */
.btn-mobile-back { display: none !important; }
@media (max-width: 768px) {
  .btn-mobile-back { display: flex !important; }
}

/* =========================================================================
   BREAKPOINT: TABLET (≤ 1100px)
   ========================================================================= */
@media (max-width: 1100px) {
  /* Layout de conversas: já sem painel lateral, garantido */
  .chat-side { display: none; }
  .chat-layout { grid-template-columns: 320px 1fr; }

  /* Dashboard: cards em 2 colunas */
  .dashboard-grid,
  .row.g-3 .col-lg-3,
  .row.g-3 .col-lg-4 {
    flex: 0 0 50%;
    max-width: 50%;
  }
}

/* =========================================================================
   BREAKPOINT: MOBILE (≤ 768px)
   ========================================================================= */
@media (max-width: 768px) {

  /* ---------- LAYOUT GERAL ---------- */
  .app-shell {
    flex-direction: column;
    height: 100vh;
    height: 100dvh; /* dynamic viewport, evita barra do navegador */
  }

  .main {
    order: 1;
    flex: 1;
    min-height: 0;
    overflow: hidden;
    display: flex;
    flex-direction: column;
  }

  .topbar {
    padding: 0 12px;
    height: 52px;
    gap: 8px;
  }
  .topbar .page-title {
    font-size: 15px;
    flex: 1;
    min-width: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .topbar .user-chip {
    padding: 4px 8px;
  }
  .topbar .user-chip > div:not(.avatar) {
    display: none; /* só o avatar no mobile */
  }

  .content {
    overflow: auto;
    flex: 1;
    padding: 12px;
  }
  .content.no-padding {
    padding: 0;
  }

  /* ---------- SIDEBAR — TABBAR INFERIOR ---------- */
  .sidebar {
    width: 100%;
    height: 58px;
    flex-direction: row;
    padding: 0;
    border-right: none;
    border-top: 1px solid var(--border-soft);
    order: 2;
    overflow-x: auto;
    overflow-y: hidden;
    flex-shrink: 0;
    background: var(--bg-soft);
    z-index: 50;
    /* esconde scrollbar */
    scrollbar-width: none;
  }
  .sidebar::-webkit-scrollbar { display: none; }

  .sidebar .sidebar-toggle,
  .sidebar .brand,
  .sidebar .nav-divider,
  .sidebar .nav-section-label,
  .sidebar .nav-bottom {
    display: none !important;
  }

  .sidebar .nav-item {
    height: 100%;
    min-width: 64px;
    padding: 6px 10px;
    margin: 0;
    flex-direction: column;
    gap: 3px;
    font-size: 10px;
    border-radius: 0;
    color: var(--text-mute);
    position: relative;
  }
  .sidebar .nav-item i {
    font-size: 18px;
  }
  .sidebar .nav-item .nav-label {
    font-size: 10px;
    display: block !important;
    line-height: 1.1;
  }
  .sidebar .nav-item.active {
    color: var(--brand);
    background: transparent;
    box-shadow: inset 0 -3px 0 var(--brand);
  }
  .sidebar .nav-badge {
    position: absolute;
    top: 4px;
    right: 14px;
    font-size: 9px;
    min-width: 14px;
    height: 14px;
    padding: 0 3px;
    line-height: 1.5;
  }

  /* ---------- TOPBAR EXTRA — esconde botões não essenciais ---------- */
  .topbar-spacer { display: none; }

  /* ---------- CONVERSAS — drawer da lista ---------- */
  .chat-layout {
    display: block;
    height: 100%;
    position: relative;
    overflow: hidden;
  }

  .chat-list {
    position: absolute;
    top: 0; left: 0; bottom: 0;
    width: 88%;
    max-width: 340px;
    z-index: 40;
    transform: translateX(-100%);
    transition: transform .25s cubic-bezier(.4,0,.2,1);
    box-shadow: 0 0 24px rgba(0,0,0,.4);
    display: flex;
  }
  .chat-layout.show-list .chat-list {
    transform: translateX(0);
  }

  .chat-window {
    width: 100%;
    height: 100%;
  }

  .chat-side {
    /* No mobile vira um drawer que desliza da direita.
       Por padrão fica fora da tela; classe .show-side no .chat-layout abre. */
    display: flex !important;
    flex-direction: column;
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: 88%;
    max-width: 360px;
    z-index: 45;
    background: var(--bg-soft);
    border-left: 1px solid var(--border-soft);
    box-shadow: 0 0 24px rgba(0,0,0,.5);
    transform: translateX(100%);
    transition: transform .25s cubic-bezier(.4,0,.2,1);
    overflow-y: auto;
    padding-top: 50px;  /* espaço pro botão X */
  }
  .chat-layout.show-side .chat-side {
    transform: translateX(0);
  }
  /* Botão X de fechar dentro do drawer */
  .chat-side::before {
    content: '\F62A'; /* bootstrap icon "x-lg" */
    font-family: bootstrap-icons !important;
    position: absolute;
    top: 12px;
    right: 14px;
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    background: var(--bg-elev);
    color: var(--text);
    font-size: 18px;
    cursor: pointer;
    z-index: 1;
  }
  /* Backdrop quando drawer está aberto */
  .chat-layout.show-side ~ .mobile-backdrop,
  body:has(.chat-layout.show-side) .mobile-backdrop {
    opacity: 1;
    pointer-events: auto;
  }

  /* Backdrop quando drawer está aberto */
  .mobile-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,.5);
    z-index: 35;
    opacity: 0;
    pointer-events: none;
    transition: opacity .25s;
  }
  .chat-layout.show-list ~ .mobile-backdrop,
  body:has(.chat-layout.show-list) .mobile-backdrop {
    opacity: 1;
    pointer-events: auto;
  }

  /* Botão de voltar (inserido via JS) */
  .btn-mobile-back {
    width: 36px;
    height: 36px;
    border: none;
    border-radius: 50%;
    background: transparent;
    color: var(--text);
    font-size: 20px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-right: 4px;
    flex-shrink: 0;
  }
  .btn-mobile-back:hover { background: var(--bg-elev); }
  /* Header da conversa: mais compacto */
  .chat-window .chat-header {
    height: 56px;
    padding: 0 10px;
    gap: 8px;
  }
  .chat-window .chat-header .avatar {
    width: 36px; height: 36px;
    font-size: 13px;
  }
  .chat-window .chat-header .name {
    font-size: 14px;
  }
  .chat-window .chat-header .sub {
    font-size: 11px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: calc(100vw - 200px);
  }
  /* Esconde alguns botões secundários */
  .chat-window .chat-header .btn-ghost:not(:first-of-type) {
    display: none;
  }
  .chat-window .chat-header .btn-toggle-side {
    display: none; /* substituído pelo modal de contato */
  }

  /* Input bar mais compacta */
  .chat-input-bar {
    padding: 8px 10px calc(8px + env(safe-area-inset-bottom));
  }
  .quick-actions {
    overflow-x: auto;
    overflow-y: hidden;
    flex-wrap: nowrap;
    margin-bottom: 6px;
    margin-left: -10px;  /* sangra um pouco pras laterais sem cortar pill no início */
    margin-right: -10px;
    padding: 0 10px 4px;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
    max-width: 100vw;
  }
  .quick-actions::-webkit-scrollbar { display: none; }
  .quick-actions .qa-pill {
    flex-shrink: 0;
    white-space: nowrap;
  }
  /* Dica visual de scroll: gradiente na borda direita */
  .chat-input-bar { position: relative; }
  .chat-input-bar::after {
    content: '';
    position: absolute;
    top: 10px;
    right: 0;
    width: 24px;
    height: 28px;
    background: linear-gradient(90deg, transparent, var(--bg-soft) 70%);
    pointer-events: none;
  }
  .chat-input-form textarea {
    font-size: 16px; /* iOS não dá zoom em ≥16px */
    min-height: 40px;
  }
  .chat-input-form button {
    width: 40px;
    height: 40px;
    flex-shrink: 0;
  }
  .btn-attach {
    width: 40px;
    height: 40px;
    flex-shrink: 0;
  }

  /* ---------- PIPELINE ---------- */
  .kanban-board {
    gap: 10px;
    padding: 4px 0 14px;
    min-height: calc(100vh - 130px);
  }
  .kanban-col {
    width: 78vw;
    max-width: 280px;
    flex-shrink: 0;
  }
  .kanban-col-header {
    padding: 8px 10px;
    font-size: 12px;
  }
  .kanban-col-body {
    padding: 6px;
  }
  .kanban-card {
    padding: 8px 10px;
    margin-bottom: 6px;
  }
  .card-name {
    font-size: 13.5px;
  }

  /* Dica de scroll horizontal */
  .kanban-board::before {
    content: '⇠ deslize para ver mais ⇢';
    display: block;
    width: 100%;
    font-size: 10.5px;
    color: var(--text-mute);
    text-align: center;
    padding: 4px 0 8px;
    position: sticky;
    left: 0;
  }

  /* ---------- DASHBOARD / TAREFAS / OUTROS ---------- */
  .row.g-3 > [class*="col-"] {
    flex: 0 0 100%;
    max-width: 100%;
  }
  .row.g-2 .col-md-6 {
    flex: 0 0 100%;
    max-width: 100%;
  }
  .card-x {
    padding: 12px;
  }
  .card-x .card-title {
    font-size: 14px;
  }

  /* Tabelas: scroll horizontal */
  .x-table,
  .table {
    font-size: 12.5px;
  }
  .card-x:has(table) {
    overflow-x: auto;
  }

  /* ---------- MODALS ---------- */
  .modal-dialog {
    margin: 8px;
    max-width: calc(100% - 16px);
  }

  /* ---------- TOASTS — no mobile aparece no TOPO CENTRALIZADO,
       como notificação do WhatsApp, não no canto inferior direito. ---------- */
  .toast-container-fixed {
    top: 12px;
    bottom: auto;
    right: 8px;
    left: 8px;
    align-items: center;
    z-index: 10000;
  }
  .crm-toast {
    min-width: 0;
    width: 100%;
    max-width: 420px;
    font-size: 13px;
    margin: 0 auto;
  }
  /* Animação entra de cima pra baixo no mobile */
  @keyframes toastInMobile {
    from { opacity: 0; transform: translateY(-20px); }
    to   { opacity: 1; transform: translateY(0); }
  }
  .crm-toast {
    animation: toastInMobile .3s cubic-bezier(.4,0,.2,1);
  }

  /* ---------- LOGIN ---------- */
  .login-card {
    margin: 16px;
    padding: 20px;
  }
}

/* =========================================================================
   BREAKPOINT: SMALLER (≤ 380px) — iPhone SE etc.
   ========================================================================= */
@media (max-width: 380px) {
  .sidebar .nav-item {
    min-width: 56px;
    font-size: 9.5px;
  }
  .sidebar .nav-item .nav-label {
    font-size: 9.5px;
  }
  .chat-list-item {
    padding: 9px 10px;
    gap: 9px;
  }
  .chat-list-item .avatar {
    width: 38px;
    height: 38px;
    font-size: 14px;
  }
  .kanban-col { width: 85vw; }
}

/* =========================================================================
   PORTRAIT/LANDSCAPE LOCKS para inputs (zoom prevention)
   ========================================================================= */
@media (max-width: 768px) {
  input[type="text"],
  input[type="email"],
  input[type="password"],
  input[type="number"],
  input[type="search"],
  input[type="tel"],
  input[type="datetime-local"],
  select,
  textarea {
    font-size: 16px !important; /* impede zoom no iOS */
  }
}
