/* Swift Chat — vista integrata nella webmail (Blocco 9A, 20 lug).
   Tutto scoped sotto #view-chat: la webmail principale ha già le sue
   classi .avatar, .composer- (ecc) con stili diversi — mai un selettore
   bare qui, per non alterare il resto dell'app.
   ATTENZIONE (bug reale scoperto 21 lug): non scrivere mai la sequenza
   di due caratteri "asterisco poi slash" dentro un commento CSS, nemmeno
   come parte di una parola — chiude il commento in anticipo e tutto il
   testo/CSS successivo fino al prossimo "chiudi commento" reale viene
   scartato come CSS non valido. Esattamente questo bug ha per settimane
   impedito a sfondo grigio e bottone Invia di comparire in QUALSIASI
   browser, non solo quello di Marco — trovato solo confrontando il file
   sorgente con le CSS rules realmente parsate da un browser reale. */
#view-chat {
  /* 21 lug: il valore scuro (#c7ccd1) era un test diagnostico deliberato
     per la caccia al bug del commento CSS malformato sopra — trovato e
     risolto, ora schiarito a un grigio più simile a WhatsApp/Telegram
     (confermato da Marco che il fondo e il bottone si vedono). */
  --sc-bg: #e9edef;
  --sc-sidebar-bg: #ffffff;
  --sc-border: #e2e5e9;
  --sc-text: #111b21;
  --sc-text-muted: #667781;
  --sc-accent: #00a884;
  --sc-selected: #f0f2f5;
  --sc-bubble-in: #ffffff;
  --sc-bubble-out: #d9fdd3;
  --sc-danger: #b3261e;
}
@media (prefers-color-scheme: dark) {
  #view-chat {
    --sc-bg: #0b141a;
    --sc-sidebar-bg: #111b21;
    --sc-border: #222d34;
    --sc-text: #e9edef;
    --sc-text-muted: #8696a0;
    --sc-accent: #00a884;
    --sc-selected: #202c33;
    --sc-bubble-in: #202c33;
    --sc-bubble-out: #005c4b;
    --sc-danger: #f2b8b5;
  }
}

#view-chat { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif; }

/* `.panel` (contenitore di tutte le view) è un flex container in
   colonna: `#view-chat` deve essere un figlio flessibile a sua volta
   (mai `height:100%`, che qui non avrebbe alcun riferimento fisso),
   altrimenti sidebar+area chat non riempiono mai lo spazio disponibile. */
#view-chat.active { display: flex !important; flex: 1; min-height: 0; width: 100%; }

#view-chat .sc-sidebar {
  width: 340px;
  min-width: 280px;
  max-width: 400px;
  background: var(--sc-sidebar-bg);
  border-right: 1px solid var(--sc-border);
  display: flex;
  flex-direction: column;
}
#view-chat .sc-sidebar-header {
  padding: 16px;
  border-bottom: 1px solid var(--sc-border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
#view-chat .sc-sidebar-header h1 { margin: 0; font-size: 1.2rem; color: var(--sc-text); }
#view-chat .new-conversation-open-button {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border: none;
  border-radius: 50%;
  background: none;
  color: var(--sc-text-muted);
  cursor: pointer;
}
#view-chat .new-conversation-open-button:hover { background: var(--sc-selected); color: var(--sc-accent); }

#view-chat .conversation-list {
  flex: 1;
  overflow-y: auto;
}
#view-chat .conversation-row {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  border: none;
  background: none;
  text-align: left;
  cursor: pointer;
  color: var(--sc-text);
  border-bottom: 1px solid var(--sc-border);
}
#view-chat .conversation-row {
  transition: background-color 0.12s ease-out;
  position: relative;
}
#view-chat .conversation-row:hover { background: var(--sc-selected); }
#view-chat .conversation-row.selected {
  background: var(--sc-selected);
  box-shadow: inset 3px 0 0 var(--sc-accent);
}

/* 20 lug: dopo la selezione, la sidebar mostra SOLO il contatto aperto
   (come WhatsApp/Telegram) — questa riga riporta alla lista completa. */
#view-chat .sidebar-back-row {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  border: none;
  background: none;
  border-bottom: 1px solid var(--sc-border);
  color: var(--sc-text-muted);
  font-size: 0.85rem;
  font-weight: 500;
  cursor: pointer;
  text-align: left;
  transition: background-color 0.12s ease-out, color 0.12s ease-out;
}
#view-chat .sidebar-back-row svg { flex-shrink: 0; transition: transform 0.12s ease-out; }
#view-chat .sidebar-back-row:hover { background: var(--sc-selected); color: var(--sc-accent); }
#view-chat .sidebar-back-row:hover svg { transform: translateX(-2px); }

#view-chat .sc-avatar {
  width: 48px;
  height: 48px;
  min-width: 48px;
  min-height: 48px;
  /* flex-shrink:0 esplicito: dentro un contenitore flex (sidebar row,
     chat header) l'avatar non deve mai essere schiacciato per far posto al
     testo accanto, anche se lo spazio è tirato. */
  flex-shrink: 0;
  border-radius: 50%;
  background: var(--sc-accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 0.95rem;
  letter-spacing: 0.02em;
  background-size: cover;
  background-position: center;
  /* Bordo grigio sempre visibile (21 lug: segnalato due volte "manca il
     contrasto grigio" — il primo tentativo, rgba(0,0,0,0.08), era troppo
     tenue per leggersi). Bordo più marcato + anello esterno via box-shadow,
     necessario soprattutto quando c'è una foto profilo reale chiara/bianca
     su sfondo altrettanto chiaro: senza un contrasto netto l'avatar si
     confonde con lo sfondo della sidebar/header invece di leggersi come un
     cerchio distinto. */
  border: 2px solid #ffffff;
  box-shadow: 0 0 0 1.5px rgba(0, 0, 0, 0.18), 0 1px 2px rgba(0, 0, 0, 0.12);
}

#view-chat .conversation-main { flex: 1; min-width: 0; }
#view-chat .conversation-name-row { display: flex; justify-content: space-between; align-items: baseline; }
#view-chat .conversation-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--sc-text); }
#view-chat .conversation-time { font-size: 0.75rem; color: var(--sc-text-muted); }
#view-chat .conversation-sub-row { display: flex; justify-content: space-between; align-items: center; margin-top: 2px; }
#view-chat .conversation-hint { font-size: 0.85rem; color: var(--sc-text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#view-chat .unread-badge {
  background: var(--sc-accent);
  color: #fff;
  border-radius: 999px;
  font-size: 0.72rem;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* --- Contatti sempre visibili (richiesta di Marco 20 lug: come WhatsApp,
   niente popup per vedere la rubrica) --- */
#view-chat .contact-section-heading {
  padding: 12px 14px 6px;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--sc-text-muted);
}
#view-chat .contact-row-disabled { opacity: 0.55; cursor: default; }

/* --- Chat area --- */
#view-chat .chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--sc-bg);
}
#view-chat .chat-empty-state { flex: 1; }
#view-chat .chat-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}
#view-chat .sc-chat-header {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 64px;
  padding: 8px 20px;
  background: var(--sc-sidebar-bg);
  border-bottom: 1px solid var(--sc-border);
}
#view-chat .chat-header-name { font-weight: 600; font-size: 1rem; color: var(--sc-text); }
#view-chat .chat-header-email { font-size: 0.78rem; color: var(--sc-text-muted); }
/* 25 lug: freccia "torna alla lista" — la sidebar ora resta SEMPRE
   visibile su desktop (Marco: "meglio avere tutti i contatti a lato"),
   quindi questo bottone serve SOLO sotto i 720px, dove la sidebar viene
   nascosta e la chat occupa tutto lo schermo (vedi `.mobile-chat-open`
   sotto) — senza, non c'era alcun modo di tornare alla lista su un
   telefono reale (bug segnalato da Marco). */
#view-chat .chat-back-button {
  display: none;
  align-items: center;
  justify-content: center;
  background: none;
  border: none;
  padding: 4px;
  margin: -4px 0 -4px -4px;
  color: var(--sc-text);
  cursor: pointer;
  flex-shrink: 0;
}

#view-chat .message-list {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  /* Guardia difensiva (21 lug): rende esplicito il proprio stacking
     context, cosi' non puo' mai finire accidentalmente SOTTO un altro
     elemento non posizionato dello stesso contenitore che lo coprirebbe
     rendendo lo sfondo grigio invisibile. */
  position: relative;
  z-index: 1;
}
#view-chat .message-bubble {
  max-width: 60%;
  padding: 8px 10px;
  border-radius: 10px;
  font-size: 0.92rem;
  line-height: 1.35;
  word-wrap: break-word;
  white-space: pre-wrap;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.06);
  animation: sc-bubble-in 0.12s ease-out;
}
@keyframes sc-bubble-in {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}
/* Angolo "a coda" sul lato del mittente, come nelle app di messaggistica —
   solo un dettaglio visivo, nessun elemento aggiuntivo nel markup. */
#view-chat .message-bubble.in { align-self: flex-start; background: var(--sc-bubble-in); color: var(--sc-text); border-bottom-left-radius: 3px; }
#view-chat .message-bubble.out { align-self: flex-end; background: var(--sc-bubble-out); color: var(--sc-text); border-bottom-right-radius: 3px; }
#view-chat .message-bubble.failed { outline: 1px solid var(--sc-danger); box-shadow: none; }
#view-chat .message-bubble.read .message-meta { color: var(--sc-accent); font-weight: 600; }
#view-chat .message-meta { font-size: 0.68rem; color: var(--sc-text-muted); text-align: right; margin-top: 4px; }
/* Checkpoint 5H-ter (4 ago) - separatore di data fra un giorno e
   l'altro nella conversazione (vedi util.js formatDayLabel). Solo
   visivo, centrato, mai un elemento interattivo. */
#view-chat .message-date-separator {
  align-self: center;
  font-size: 0.72rem;
  color: var(--sc-text-muted);
  background: var(--sc-bg-subtle, rgba(0, 0, 0, 0.05));
  padding: 3px 10px;
  border-radius: 999px;
  margin: 10px 0 4px;
}

#view-chat .composer {
  border-top: 1px solid var(--sc-border);
  background: var(--sc-sidebar-bg);
  padding: 10px 14px;
  /* 21 lug: senza questo, in spazio verticale stretto (.chat-panel è flex
     column con .message-list a flex:1) il composer può essere schiacciato
     dal browser invece di restare alla sua altezza naturale — il bottone
     Invia (seconda riga, .composer-footer) è il primo a sparire in quel
     caso. Mai lasciare che si comprima. */
  flex-shrink: 0;
  position: relative;
  z-index: 1;
}
#view-chat .composer-row {
  display: flex;
  align-items: flex-end;
  gap: 6px;
}
#view-chat .composer-input {
  flex: 1;
  width: 100%;
  resize: none;
  overflow-y: hidden;
  border: 1px solid var(--sc-border);
  border-radius: 20px;
  padding: 9px 16px;
  font: inherit;
  font-size: 0.92rem;
  /* Valori espliciti oltre alla variabile (20 lug: segnalato "tono su
     tono", testo praticamente invisibile) — la webmail principale ha un
     proprio reset globale su input/textarea che può vincere a parità di
     specificità con la sola variabile; qui fissiamo esplicitamente
     colore chiaro di sfondo + testo scuro, mai lasciato al caso. */
  color: #111b21;
  background: #ffffff;
  max-height: 120px;
  min-height: 40px;
  transition: height 0.08s ease-out, border-color 0.12s ease-out, box-shadow 0.12s ease-out;
}
#view-chat .composer-input:focus { outline: none; border-color: var(--sc-accent); box-shadow: 0 0 0 1px var(--sc-accent); }
#view-chat .composer-input::placeholder { color: #667781; opacity: 1; }
#view-chat .composer-input:disabled { opacity: 0.6; }

#view-chat .composer-popover-anchor { position: relative; }
#view-chat .composer-icon-button {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  border: none;
  border-radius: 50%;
  background: none;
  color: var(--sc-text-muted);
  cursor: pointer;
  flex-shrink: 0;
}
#view-chat .composer-icon-button:hover { background: var(--sc-selected); color: var(--sc-text); }
#view-chat .composer-icon-button:disabled { opacity: 0.5; cursor: default; }
#view-chat .composer-icon-button[aria-expanded="true"] { background: var(--sc-selected); color: var(--sc-accent); }

#view-chat .composer-popover {
  position: absolute;
  bottom: calc(100% + 8px);
  background: var(--sc-sidebar-bg);
  border: 1px solid var(--sc-border);
  border-radius: 10px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  z-index: 20;
}
#view-chat .composer-attach-menu[hidden], #view-chat .composer-emoji-picker[hidden] { display: none; }
#view-chat .composer-attach-menu {
  left: 0;
  width: 220px;
  padding: 6px;
  display: flex;
  flex-direction: column;
}
#view-chat .composer-attach-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 8px 10px;
  border: none;
  background: none;
  border-radius: 8px;
  color: var(--sc-text);
  font-size: 0.85rem;
  text-align: left;
  cursor: not-allowed;
}
#view-chat .composer-attach-item svg { flex-shrink: 0; color: var(--sc-accent); }
#view-chat .composer-attach-item[disabled] { opacity: 0.55; }
#view-chat .composer-attach-soon {
  margin-left: auto;
  font-size: 0.68rem;
  color: var(--sc-text-muted);
  white-space: nowrap;
}
#view-chat .composer-emoji-picker {
  right: 0;
  padding: 8px;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 2px;
  width: 200px;
}
#view-chat .composer-emoji-item {
  border: none;
  background: none;
  border-radius: 6px;
  font-size: 1.3rem;
  line-height: 1;
  padding: 6px;
  cursor: pointer;
}
#view-chat .composer-emoji-item:hover { background: var(--sc-selected); }
#view-chat .composer-footer {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 6px;
}
#view-chat .composer-counter { font-size: 0.72rem; color: var(--sc-text-muted); }
#view-chat .composer-counter.over-limit { color: var(--sc-danger); font-weight: 600; }
#view-chat .composer-error { font-size: 0.75rem; color: var(--sc-danger); flex: 1; }
#view-chat .composer-send-button {
  margin-left: auto;
  padding: 6px 16px;
  border-radius: 999px;
  border: none;
  background: var(--sc-accent);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
  transition: filter 0.12s ease-out, transform 0.08s ease-out;
}
#view-chat .composer-send-button:hover:not(:disabled) { filter: brightness(1.08); }
#view-chat .composer-send-button:active:not(:disabled) { transform: scale(0.96); }
#view-chat .composer-send-button:disabled { opacity: 0.5; cursor: default; }

/* --- "Nuova conversazione" (email libera, resta un popup: solo per un
   indirizzo NON già in rubrica) --- */
#view-chat .new-conversation-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
}
#view-chat .new-conversation-overlay[hidden] { display: none; }
#view-chat .new-conversation-panel {
  background: var(--sc-sidebar-bg);
  border-radius: 12px;
  width: 420px;
  max-width: 92vw;
  max-height: 82vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.25);
}
#view-chat .new-conversation-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--sc-border);
}
#view-chat .new-conversation-header h2 { margin: 0; font-size: 1.05rem; color: var(--sc-text); }
#view-chat .new-conversation-email-form {
  display: flex;
  gap: 8px;
  padding: 12px 16px;
}
#view-chat .new-conversation-email-form input {
  flex: 1;
  padding: 8px 10px;
  border: 1px solid var(--sc-border);
  border-radius: 8px;
  font: inherit;
  font-size: 0.88rem;
  background: var(--sc-bg);
  color: var(--sc-text);
}
#view-chat #new-conversation-email-result { padding: 0 16px 16px; }

#view-chat .swift-status-badge {
  font-size: 0.72rem;
  padding: 4px 10px;
  border-radius: 999px;
  white-space: nowrap;
  border: none;
}
#view-chat .swift-status-pending { color: var(--sc-text-muted); background: none; padding: 4px 0; }
#view-chat .swift-status-enabled { background: var(--sc-accent); color: #fff; cursor: pointer; font-weight: 600; }
#view-chat .swift-status-enabled:hover { filter: brightness(1.05); }
#view-chat .swift-status-disabled { background: var(--sc-selected); color: var(--sc-text-muted); cursor: default; }
#view-chat .swift-status-mailto { font-size: 0.72rem; color: var(--sc-accent); text-decoration: none; }
#view-chat .swift-status-mailto:hover { text-decoration: underline; }

/* --- Stati condivisi --- */
#view-chat .panel-loading, #view-chat .panel-empty {
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 1;
  color: var(--sc-text-muted);
  padding: 24px;
  text-align: center;
}
#view-chat .panel-error {
  color: var(--sc-danger);
  padding: 12px 16px;
  font-size: 0.85rem;
}
#view-chat .load-more-button {
  display: block;
  margin: 8px auto;
  padding: 6px 14px;
  border-radius: 999px;
  border: 1px solid var(--sc-border);
  background: var(--sc-sidebar-bg);
  color: var(--sc-text);
  cursor: pointer;
  font-size: 0.8rem;
}
#view-chat .load-more-button:hover { background: var(--sc-selected); }

@media (max-width: 720px) {
  /* 22 lug: prima qui `.chat-area` era nascosta SEMPRE, senza alcun modo
     per mostrarla — su un telefono reale la conversazione non era mai
     visibile, solo la lista (bug segnalato da Marco). La classe
     `.mobile-chat-open` (su `#view-chat`, aggiunta/rimossa da
     SwiftUi.showChatPanel/showNoConversationSelected) decide quale dei
     due pannelli occupa lo schermo, come un vero layout mobile
     sidebar/detail a singolo pannello (WhatsApp/Telegram). */
  #view-chat .sc-sidebar { width: 100%; max-width: none; }
  #view-chat .chat-area { display: none; }
  #view-chat.mobile-chat-open .sc-sidebar { display: none; }
  #view-chat.mobile-chat-open .chat-area { display: flex; width: 100%; }
  #view-chat .chat-back-button { display: flex; }
}
