/* =========================================================
   CAMADA MOBILE — carregada depois do style.css
   Tudo aqui só vale em telas pequenas; o desktop não muda.
   ========================================================= */

/* ---------- base (todas as telas) ---------- */
html { -webkit-text-size-adjust: 100%; }
img, video, canvas, svg { max-width: 100%; }
pre, code { max-width: 100%; overflow-x: auto; }

/* elementos que só existem no mobile ficam escondidos no desktop */
.nav-toggle, .mobile-subbar, .drawer-backdrop, .drawer-close { display: none; }

/* ---------- até 900px: tablet e celular ---------- */
@media (max-width: 900px) {
  html, body { overflow-x: clip; }
  body { padding-left: env(safe-area-inset-left); padding-right: env(safe-area-inset-right); }

  /* ----- topbar compacta + menu hambúrguer ----- */
  .topbar {
    height: 56px; min-height: 56px; padding: 0 14px;
    flex-wrap: nowrap; gap: 10px;
    padding-top: env(safe-area-inset-top);
  }
  .nav-toggle {
    display: inline-flex; align-items: center; justify-content: center;
    width: 44px; height: 44px; flex-shrink: 0;
    background: transparent; border: 1px solid var(--border); border-radius: 12px;
    color: var(--text); font-size: 22px; line-height: 1; cursor: pointer;
  }
  .nav-toggle:active { background: rgba(255,255,255,.06); }

  .topbar .user {
    display: none;
    position: fixed; left: 10px; right: 10px;
    top: calc(60px + env(safe-area-inset-top));
    width: auto; max-height: calc(100dvh - 80px - env(safe-area-inset-top));
    overflow-y: auto; flex-direction: column; align-items: stretch; gap: 4px;
    padding: 10px; z-index: 70;
    background: rgba(13,15,21,.98);
    border: 1px solid var(--border-strong, var(--border)); border-radius: 16px;
    box-shadow: 0 24px 60px rgba(0,0,0,.6);
  }
  body.nav-open .topbar .user { display: flex; }
  .topbar .user > a { padding: 13px 14px; min-height: 46px; display: flex; align-items: center; font-size: 15px; }
  .topbar .user > a.btn.secondary { margin: 6px 0 0; justify-content: center; }
  .topbar .user .user-chip { order: -1; margin: 0 0 6px; padding: 8px 12px 8px 8px; border-radius: 12px; }
  .topbar .user .user-chip span { font-size: 14px; }

  /* ----- faixa "Módulos" (abre a gaveta do servidor) ----- */
  .mobile-subbar {
    display: flex; align-items: center; gap: 10px;
    position: sticky; top: calc(56px + env(safe-area-inset-top)); z-index: 15;
    margin: -22px -18px 18px; padding: 10px 14px;
    background: rgba(9,11,15,.92);
    backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
    border-bottom: 1px solid var(--border);
  }
  .mobile-subbar button {
    display: inline-flex; align-items: center; gap: 8px; min-height: 42px; padding: 0 14px;
    background: rgba(12,70,231,.14); color: var(--text);
    border: 1px solid rgba(12,70,231,.5); border-radius: 12px;
    font: inherit; font-size: 14px; font-weight: 600; cursor: pointer;
  }
  .mobile-subbar .subbar-title {
    flex: 1; min-width: 0; font-size: 14px; color: var(--text-muted);
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis; text-align: right;
  }

  /* ----- menu lateral vira gaveta ----- */
  .app-shell { display: block; min-height: 0; }
  .app-sidebar {
    position: fixed; top: 0; left: 0; bottom: 0; z-index: 90;
    width: min(86vw, 320px); min-height: 0; height: 100dvh;
    padding: calc(16px + env(safe-area-inset-top)) 14px calc(24px + env(safe-area-inset-bottom));
    overflow-y: auto; overscroll-behavior: contain;
    background: #0b0d12; border-right: 1px solid var(--border); border-bottom: none;
    transform: translateX(-102%); transition: transform .22s ease;
    box-shadow: none;
  }
  body.drawer-open .app-sidebar { transform: translateX(0); box-shadow: 0 0 60px rgba(0,0,0,.7); }
  .sidebar-nav { flex-direction: column; flex-wrap: nowrap; gap: 18px; }
  .sidebar-group { flex-direction: column; flex-wrap: nowrap; }
  .sidebar-nav a { min-height: 46px; font-size: 15px; }

  .drawer-backdrop {
    display: block; position: fixed; inset: 0; z-index: 80;
    background: rgba(0,0,0,.6); opacity: 0; pointer-events: none; transition: opacity .2s;
  }
  body.drawer-open .drawer-backdrop, body.nav-open .drawer-backdrop { opacity: 1; pointer-events: auto; }
  body.nav-open .drawer-backdrop { z-index: 19; } /* abaixo da topbar (z 20), onde o painel do menu mora */
  body.drawer-open, body.nav-open { overflow: hidden; }

  /* ----- conteúdo ----- */
  .app-main { padding: 22px 16px calc(96px + env(safe-area-inset-bottom)); }
  .container { padding: 22px 16px 60px; }
  .app-main .container { padding: 0; }
  .app-main h2, .container h2 { font-size: 22px; }
  .card { padding: 16px; border-radius: 14px; }
  .ticket-head { flex-direction: column; align-items: stretch; gap: 10px; }
  .builder-toolbar, .section-title, .preview-head, .block-head, .dynamic-head { flex-wrap: wrap; }

  /* grids que ainda não tinham versão mobile */
  .setting-toggle-row, .preview-fields, .option-primary-grid, .option-secondary-grid,
  .admin-bot-row, .payment-row { grid-template-columns: 1fr !important; }
  .admin-heading, .section-title-row, .form-actions { flex-wrap: wrap; align-items: flex-start; }
  .admin-heading > .btn { width: 100%; }
  .admin-actions { flex-wrap: wrap; }
  .admin-actions .btn { flex: 1 1 calc(50% - 8px); min-width: 0; text-align: center; }
  .admin-row-actions { flex-wrap: wrap; justify-content: flex-start; }
  .admin-row-actions .btn { flex: 1 1 auto; }
  .stat-card strong { overflow-wrap: anywhere; }
  .row-flex { flex-wrap: wrap; }
  .row-flex > div { min-width: 140px; }
  .form-grid.compact { align-items: stretch; }

  /* ----- formulários mais fáceis de tocar ----- */
  input[type=text], input[type=number], input[type=url], input[type=search],
  input[type=password], input[type=email], input:not([type]), select, textarea {
    font-size: 16px; /* evita o zoom automático do iPhone */
    min-height: 46px;
  }
  textarea { min-height: 96px; }
  .btn, button.btn, a.btn { min-height: 44px; display: inline-flex; align-items: center; justify-content: center; }
  .icon-btn { min-width: 44px; min-height: 44px; }
  .toggle-row { gap: 14px; min-height: 52px; }
  .toggle-row span { flex: 1; min-width: 0; }

  /* seletor pesquisável de canais/cargos */
  .ms-box { min-height: 48px; }
  .ms-dropdown { max-width: 100%; max-height: 55dvh; }
  .ms-options { max-height: calc(55dvh - 70px); -webkit-overflow-scrolling: touch; }
  .ms-search { font-size: 16px; }

  /* barra de salvar sempre à mão */
  .save-bar {
    position: sticky; bottom: calc(10px + env(safe-area-inset-bottom));
    flex-wrap: wrap; gap: 10px; padding: 10px 12px; margin-top: 18px;
  }
  .save-bar .btn { flex: 1 1 140px; }
  .save-bar .toast { flex: 1 1 100%; order: 2; font-size: 13px; }
  .save-bar .toast:empty { display: none; }

  /* lista de servidores */
  .servers-grid, .grid { grid-template-columns: 1fr !important; }
  .guild-card, .guild-card:nth-child(4), .guild-card:nth-child(5) { grid-column: auto !important; min-height: 0; }
  .guild-card-actions { flex-direction: column; align-items: stretch; gap: 10px; width: 100%; }
  .guild-card-actions .btn { flex: none; width: 100%; }

  /* chat de compra */
  .purchase-chat { height: 62dvh; }
  .purchase-chat-composer { flex-wrap: wrap; }
  .purchase-chat-composer input[type=text], .purchase-chat-composer textarea { flex: 1 1 100%; }

  /* login e telas cheias */
  .login-wrap { height: 100dvh; padding: 20px; }
  .login-box { width: 100%; max-width: 420px; }
}

/* ---------- até 560px: celulares ---------- */
@media (max-width: 560px) {
  .app-main { padding: 18px 12px calc(96px + env(safe-area-inset-bottom)); }
  .mobile-subbar { margin: -18px -12px 16px; }
  .container { padding: 18px 12px 60px; }
  .card { padding: 14px; }
  .stats-grid { grid-template-columns: 1fr 1fr !important; }
  .panel-actions, .admin-row-actions { width: 100%; }
  .panel-actions .btn, .admin-row-actions .btn { flex: 1 1 auto; }
  .option-actions { width: 100%; }
  .option-actions .btn { flex: 1; }
  h1 { font-size: 26px; }
}

/* telas muito baixas (celular deitado): menu não ocupa tudo */
@media (max-width: 900px) and (max-height: 480px) {
  .mobile-subbar { position: static; }
}

/* quem prefere menos movimento */
@media (prefers-reduced-motion: reduce) {
  .app-sidebar, .drawer-backdrop { transition: none; }
}
