/* ============================================================
   mobile-modals.css — modal, popover, form (font 16px+ touch 44px+)
   Owner: teammate "mobile-modals"
   Breakpoint: tablet ≤1024px, mobile ≤640px
   Vincolo: solo override dentro media query, mai regole desktop.
   Nessun !important. Variabili CSS condivise (--accent ecc.).
   ============================================================ */

/* ──────────────────────────────────────────────────────────────
   TABLET — ≤ 1024px
   - Modali allargati ma centrati, padding ridotto
   - Detail panel a 90vw (desktop è 520px fisso)
   - Form gap leggermente più ampio
   - Tap target ≥44px su bottoni primari
   ────────────────────────────────────────────────────────────── */
@media (max-width: 1024px) {

  /* ─── Detail panel (popover ticket) ─── */
  .detail-panel {
    width: 90vw;
    max-width: 720px;
  }
  .detail-panel.expanded {
    width: 95vw;
  }

  /* ─── Modal generici ─── */
  .modal {
    max-width: 92vw;
    padding: 24px 20px 20px;
  }
  .modal-overlay {
    /* su tablet riduco blur per perf */
    backdrop-filter: blur(4px);
  }

  /* ─── Form inputs e select: tap target 44px su tablet ─── */
  .modal .form-input,
  .modal .modal-select,
  .modal-overlay .form-input,
  .modal-overlay .modal-select {
    min-height: 44px;
    padding: 11px 14px;
    font-size: 14px;
  }

  /* ─── Bottoni modale ─── */
  .modal .btn-cancel,
  .modal .btn-save,
  .modal .btn-modal-cancel,
  .modal .btn-modal-confirm {
    min-height: 40px;
    padding: 10px 18px;
  }

  /* ─── Reply box (sotto thread ticket) ─── */
  .reply-textarea {
    font-size: 14px;
  }
  .btn-send {
    min-height: 40px;
  }
}

/* ──────────────────────────────────────────────────────────────
   MOBILE — ≤ 640px
   - Tutti i modali e il detail-panel diventano fullscreen sheet
   - Form a colonna singola (modal-row diventa 1fr)
   - Input/textarea/select font ≥16px (no zoom auto iOS Safari)
   - Tap target ≥44px ovunque
   - Footer modale con bottoni 50/50 flex
   - Allegati popover diventa bottom sheet full-width
   ────────────────────────────────────────────────────────────── */
@media (max-width: 640px) {

  /* ─── Detail panel: già full-screen in index.html via !important.
        Aggiungo solo coerenza border-radius e safe-area. ─── */
  .detail-panel {
    border-radius: 0;
  }
  .panel-header {
    padding: 14px 16px 12px;
    position: sticky;
    top: 0;
    background: var(--surface);
    z-index: 2;
  }
  .panel-subject {
    font-size: 15px;
  }
  /* close button del pannello ≥44px */
  .panel-close {
    width: 40px;
    height: 40px;
  }
  .panel-close svg {
    width: 18px;
    height: 18px;
  }
  .messages {
    padding: 14px 16px;
  }
  /* Reply box sticky in basso, padding-safe-area */
  .reply-box {
    padding: 12px 14px 16px;
    padding-bottom: max(16px, env(safe-area-inset-bottom));
    position: sticky;
    bottom: 0;
    background: var(--surface);
  }
  .reply-textarea {
    font-size: 16px; /* anti-zoom iOS */
    min-height: 88px;
    padding: 12px;
  }
  .reply-footer {
    flex-wrap: wrap;
    gap: 8px;
  }
  .reply-hint {
    width: 100%;
    text-align: left;
  }
  .btn-send {
    min-height: 44px;
    padding: 10px 18px;
    font-size: 14px;
  }
  .btn-allegati {
    min-height: 44px;
    padding: 10px 14px;
    font-size: 14px;
  }
  .btn-conserva,
  .btn-expand {
    min-width: 40px;
    min-height: 40px;
  }

  /* ─── Allegati popover: bottom sheet full-width ─── */
  .allegati-popover {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    max-width: none;
    max-height: 70vh;
    border-radius: 16px 16px 0 0;
    box-shadow: 0 -8px 32px rgba(0, 0, 0, 0.4);
  }

  /* ─── Modal overlay: padding ridotto a zero ─── */
  .modal-overlay {
    align-items: stretch;
    justify-content: stretch;
    padding: 0;
  }

  /* ─── Modal: fullscreen sheet con flex column corretto ─── */
  .modal {
    width: 100vw;
    max-width: 100vw;
    height: 100vh;
    max-height: 100vh;
    border-radius: 0;
    padding: 16px;
    gap: 14px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    /* safe area top per notch iPhone; bottom gestito dal footer */
    padding-top: max(16px, env(safe-area-inset-top));
    padding-bottom: 0;
  }
  .modal-title {
    font-size: 17px;
    padding-top: 4px;
  }
  .modal-fields {
    flex: 1;
    gap: 14px;
    overflow-y: auto;
  }

  /* ─── Modal row: 2 colonne → 1 colonna su mobile ─── */
  .modal-row {
    grid-template-columns: 1fr;
    gap: 14px;
  }

  /* ─── Form inputs: anti-zoom iOS, 44px tap target ─── */
  .modal .form-input,
  .modal .modal-select,
  .modal-overlay .form-input,
  .modal-overlay .modal-select,
  .modal-overlay textarea,
  .modal-overlay input[type="text"],
  .modal-overlay input[type="email"],
  .modal-overlay input[type="password"],
  .modal-overlay input[type="tel"],
  .modal-overlay input[type="number"] {
    min-height: 44px;
    padding: 12px 14px;
    font-size: 16px;
    line-height: 1.4;
  }
  .form-label {
    font-size: 13px;
    margin-bottom: 6px;
  }

  /* extra contacts row (residente N email/telefoni) */
  .extra-contacts {
    gap: 8px;
  }
  .extra-contacts .extra-row input {
    min-height: 44px;
    font-size: 16px;
    padding: 12px 14px;
  }
  .extra-contacts .extra-row button {
    min-width: 44px;
    min-height: 44px;
    font-size: 18px;
  }
  .btn-add-extra {
    min-height: 40px;
    padding: 10px 14px;
    font-size: 13px;
  }

  /* ─── Modal footer: bottoni full-width 50/50, sticky in basso ─── */
  .modal-footer,
  .modal-actions {
    position: sticky;
    bottom: 0;
    background: var(--surface);
    margin: 0 -16px -16px;
    padding: 12px 16px;
    padding-bottom: max(12px, env(safe-area-inset-bottom));
    border-top: 1px solid var(--border);
    gap: 10px;
  }
  .modal-footer > button,
  .modal-actions > button {
    flex: 1;
    min-height: 44px;
    padding: 12px 16px;
    font-size: 15px;
  }

  /* Modal con header + body + footer (es. modalSlug, modalPortale) */
  .modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    position: sticky;
    top: 0;
    background: var(--surface);
    z-index: 2;
    padding-bottom: 12px;
    margin-bottom: 4px;
    border-bottom: 1px solid var(--border);
  }
  .modal-header .modal-title {
    margin: 0;
    flex: 1;
  }
  .modal-close {
    display: flex !important;
    align-items: center;
    justify-content: center;
    min-width: 44px;
    min-height: 44px;
    background: transparent;
    border: none;
    font-size: 22px;
    color: var(--muted);
    cursor: pointer;
    border-radius: 8px;
    padding: 0;
  }
  .modal-close:active {
    background: var(--bg, rgba(0,0,0,0.05));
  }
  .modal-body {
    padding: 0;
  }

  /* ─── Buttons primari globali ≥44px ─── */
  .btn-primary {
    min-height: 44px;
    padding: 12px 18px;
    font-size: 14px;
  }
  .btn-secondary {
    min-height: 44px;
    padding: 12px 16px;
    font-size: 14px;
  }
  .btn-add-residente {
    min-height: 40px;
    padding: 10px 14px;
    font-size: 13px;
  }

  /* ── Fix overflow .content + settings-card su mobile (Bug 4 Fase 3) ──
     Il .content di #impostazioniView e #knowledgeView ha padding inline
     28px+28px+gap che con contenuto interno espandibile (es.
     portaleStudioCard child div ~414px) forza la flex container a
     ~471px, oltre i 390px viewport. View ha overflow-x:hidden quindi
     visualmente le card sembrano "tagliate a destra".
     Fix: padding ridotto, min-width:0 + max-width:100% per permettere
     shrink, clamp interno wrap su contenuti larghi (textarea, code,
     input, link slug). */
  #impostazioniView .content,
  #knowledgeView .content {
    padding: 16px 12px 32px !important;
    min-width: 0;
    max-width: 100% !important;
    width: 100%;
    box-sizing: border-box;
  }
  #impostazioniView .settings-card,
  #knowledgeView .kb-card {
    min-width: 0;
    max-width: 100%;
    padding: 16px;
    box-sizing: border-box;
    overflow-wrap: break-word;
    word-break: break-word;
  }
  #impostazioniView .settings-card > *,
  #knowledgeView .kb-card > * {
    min-width: 0;
    max-width: 100%;
  }

  /* ── Fix portaleStudioCard actions row wrap (Bug 6 Fase 3) ──
     Desktop: input flex:1 + 2 button su una riga.
     Mobile: input full-width row 1, 2 button half/half row 2. */
  .portale-studio-actions {
    flex-wrap: wrap !important;
  }
  .portale-studio-actions input {
    flex: 1 1 100% !important;
    min-width: 0 !important;
  }
  .portale-studio-actions button {
    flex: 1 1 calc(50% - 4px) !important;
    min-width: 0 !important;
  }
}
