/* Zap Mágico · painel responsivo (celular e tablet)
 * Camada separada: só age abaixo de 900px e não altera o layout de desktop.
 * Usa as classes que já existem no index.html; o botão de menu e o "voltar"
 * da conversa são criados por responsive.js. */

.zm-menu-btn,
.zm-drawer-backdrop,
.zm-chat-back,
.zm-topbar-brand { display: none; }

@media (max-width: 900px) {
  html, body { height: 100%; }
  body { overflow: hidden; }

  .app-layout { height: 100vh; height: 100dvh; width: 100%; }
  .main-content { height: 100vh; height: 100dvh; min-width: 0; width: 100%; }

  /* Barra lateral vira gaveta */
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(84vw, 300px);
    z-index: 60;
    transform: translateX(-102%);
    transition: transform .22s ease;
    box-shadow: 0 0 40px rgba(11, 20, 26, .25);
    padding-bottom: env(safe-area-inset-bottom);
  }
  body.zm-drawer-open .sidebar { transform: translateX(0); }
  .zm-drawer-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 55;
    background: rgba(11, 20, 26, .45);
    opacity: 0;
    pointer-events: none;
    transition: opacity .2s ease;
  }
  body.zm-drawer-open .zm-drawer-backdrop { opacity: 1; pointer-events: auto; }
  .nav-menu { overflow-y: auto; }
  .nav-item { min-height: 48px; font-size: 15px; }

  /* Topo: menu + logo + ações na primeira linha, conta ativa na segunda */
  .topbar {
    height: auto;
    min-height: 56px;
    padding: 8px 12px;
    padding-top: max(8px, env(safe-area-inset-top));
    flex-wrap: wrap;
    gap: 8px;
  }
  .zm-menu-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    flex: none;
    border: 1px solid var(--border-color);
    border-radius: 10px;
    background: transparent;
    color: var(--text-main);
    font-size: 20px;
    cursor: pointer;
  }
  .zm-topbar-brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 16px; color: var(--text-main); flex: none; }
  .zm-topbar-brand img { width: 30px; height: 30px; border-radius: 7px; }
  .account-selector-wrapper { flex: 1 1 100%; order: 3; min-width: 0; gap: 8px; }
  .account-selector-wrapper label { display: none; }
  .account-selector-wrapper .form-select { flex: 1; min-width: 0; }
  .topbar-actions { margin-left: auto; gap: 8px; }
  .api-token-badge { white-space: nowrap; }

  .content-body { padding: 14px 12px calc(20px + env(safe-area-inset-bottom)); -webkit-overflow-scrolling: touch; }

  /* Cartões em uma coluna */
  .card-grid { grid-template-columns: minmax(0, 1fr); gap: 14px; }
  .card { padding: 16px; min-width: 0; }
  .card h2 { font-size: 17px; }
  .form-grid { grid-template-columns: minmax(0, 1fr); }
  .form-inline { flex-wrap: wrap; }
  .form-inline > * { flex: 1 1 100%; }

  /* QR e botões */
  .qr-container { width: min(256px, 72vw); height: min(256px, 72vw); }
  .qr-actions { flex-wrap: wrap; justify-content: center; width: 100%; }
  .qr-actions .btn { flex: 1 1 140px; }
  .btn { min-height: 44px; }

  /* Evita o zoom automático do iPhone ao tocar em campos */
  .form-input, .form-select, textarea, input, select { font-size: 16px; }

  /* Tabelas rolam na horizontal dentro do cartão */
  .table-responsive { -webkit-overflow-scrolling: touch; }
  .data-table { min-width: 560px; }

  /* Atendimento: lista OU conversa, com botão de voltar */
  .inbox-layout { height: calc(100vh - 150px); height: calc(100dvh - 150px); min-height: 420px; position: relative; }
  .chat-list-sidebar { width: 100%; border-right: 0; }
  .chat-search .form-input { width: 100%; }
  .chat-window { display: none; width: 100%; min-width: 0; }
  .inbox-layout.zm-chat-open .chat-list-sidebar { display: none; }
  .inbox-layout.zm-chat-open .chat-window { display: flex; }
  .chat-header { padding: 0 10px; gap: 8px; }
  .zm-chat-back {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    flex: none;
    border: 0;
    border-radius: 50%;
    background: transparent;
    color: var(--text-main);
    font-size: 22px;
    cursor: pointer;
  }
  .chat-contact-info { min-width: 0; }
  .chat-contact-info h3, .chat-contact-info span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .chat-messages { padding: 12px; }
  .chat-bubble { max-width: 85%; }
  .chat-input-area { padding: 8px; gap: 8px; padding-bottom: max(8px, env(safe-area-inset-bottom)); }
  .chat-input { height: 44px; }

  /* Textos longos (tokens, links MCP, JSON) não estouram a tela */
  pre, code { white-space: pre-wrap; word-break: break-all; }
  img, video { max-width: 100%; }
}

@media (max-width: 420px) {
  .topbar-actions #btnRefresh { padding: 8px 10px; }
  .api-token-badge { font-size: 11px; padding: 6px 8px; }
  .card { padding: 14px; }
}

@media (prefers-reduced-motion: reduce) {
  .sidebar, .zm-drawer-backdrop { transition: none; }
}
