/*
 * TXT — Feuille de style commentée
 * =================================
 *
 * Le CSS ne contient aucune logique de messagerie. Il décrit seulement l’apparence et la mise en page.
 * Les classes d’état comme .hidden, .is-connected, .chat-open ou .modal-open sont ajoutées/retirées
 * par le JavaScript.
 *
 * Organisation du fichier :
 * 1. variables globales ;
 * 2. remise à zéro et composants génériques ;
 * 3. accueil et contacts ;
 * 4. chat plein écran ;
 * 5. fenêtres modales ;
 * 6. adaptation mobile.
 */

/* Variables réutilisées dans toute l’interface. Modifier une valeur ici change tout le thème. */
:root {
  /* Informe le navigateur que les contrôles natifs doivent être rendus en thème sombre. */
  color-scheme: dark;
  /* Couleur de fond principale. */
  --bg: #07111f;
  /* Couleur des panneaux et fenêtres. */
  --panel: #0f1c2f;
  /* Variante de panneau, conservée pour les composants secondaires. */
  --panel-2: #14243a;
  /* Bordure translucide commune. */
  --border: rgba(148, 163, 184, .20);
  /* Texte principal. */
  --text: #e8eef8;
  /* Texte secondaire moins contrasté. */
  --muted: #91a0b6;
  /* Couleur des boutons principaux. */
  --accent: #2f80ed;
  /* Couleur des boutons au survol. */
  --accent-2: #1769d1;
  /* Vert : connexion ou livraison réussie. */
  --ok: #39d477;
  /* Rouge : absence de connexion ou action destructive. */
  --danger: #ef5350;
  /* Fond des messages envoyés par l’utilisateur local. */
  --bubble-me: #1769d1;
  /* Fond des messages reçus. */
  --bubble-them: #17283f;
  /* Ombre commune aux cartes et fenêtres modales. */
  --shadow: 0 22px 70px rgba(0, 0, 0, .34);
  /* Valeurs mises à jour par visualViewport quand le clavier mobile apparaît. */
  --chat-viewport-top: 0px;
  --chat-viewport-height: 100dvh;
}

/* Remise à zéro : dimensions prévisibles et hauteur minimale de la page. */
* { box-sizing: border-box; }
html, /* Fond général et police système : aucune police externe n’est téléchargée. */
body { min-height: 100%; }
body {
  margin: 0;
  background:
    radial-gradient(circle at 15% -10%, rgba(47,128,237,.22), transparent 35%),
    linear-gradient(180deg, #081523 0%, var(--bg) 55%);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
/* Empêche la page située derrière un chat ou une modale de défiler. */
body.chat-open, body.modal-open { overflow: hidden; }
/* Les champs et boutons héritent de la même police que le reste de la page. */
button, /* Style générique des champs texte. */
input, textarea { font: inherit; }
/* Style générique de tous les boutons ; les variantes le surchargent ensuite. */
button {
  border: 0;
  border-radius: 14px;
  padding: 11px 15px;
  background: var(--accent);
  color: #fff;
  font-weight: 800;
  cursor: pointer;
  transition: transform .12s ease, background .15s ease, opacity .15s ease;
}
button:hover { background: var(--accent-2); }
button:active { transform: scale(.98); }
button:disabled { opacity: .45; cursor: not-allowed; }
button.ghost { background: rgba(148,163,184,.14); border: 1px solid var(--border); }
button.danger { color: #fecaca; }
input, textarea {
  width: 100%;
  border: 1px solid var(--border);
  background: rgba(7,17,31,.88);
  color: var(--text);
  border-radius: 14px;
  padding: 12px 14px;
  outline: none;
}
input:focus, textarea:focus { border-color: rgba(47,128,237,.85); box-shadow: 0 0 0 3px rgba(47,128,237,.15); }
/* Classe utilitaire utilisée par JavaScript pour masquer totalement un élément. */
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 13px; line-height: 1.45; }
.field-label { display: block; color: var(--muted); margin: 18px 0 8px; }

/* Conteneur centré de l’écran d’accueil. */
.app-shell { width: min(100%, 820px); margin: 0 auto; padding: 20px 18px 48px; }
/* Barre de marque et bouton Partager. */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 4px 2px 18px;
}
.brand-block h1 { margin: 0; font-size: clamp(31px, 7vw, 44px); line-height: 1; letter-spacing: -.04em; }
.subtitle { margin: 6px 0 0; color: var(--muted); font-size: 14px; }
/*
 * Nom de l’utilisateur local affiché sous la marque.
 * C’est un bouton pour rester accessible au clavier, mais son aspect est volontairement discret.
 */
.current-user-name {
  max-width: min(58vw, 360px);
  margin-top: 9px;
  padding: 5px 9px 5px 7px;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  border: 1px solid rgba(148,163,184,.22);
  border-radius: 999px;
  background: rgba(148,163,184,.09);
  color: var(--text);
  font-size: 13px;
  font-weight: 750;
  line-height: 1.2;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}
.current-user-name:hover { background: rgba(148,163,184,.15); }
.current-user-name:focus-visible { box-shadow: 0 0 0 3px rgba(96,165,250,.38); }
.current-user-label { color: var(--muted); font-weight: 650; white-space: nowrap; }
.current-user-name span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
@media (min-width: 601px) {
  .topbar { flex-wrap: wrap; }
  .brand-block { flex: 0 0 auto; min-width: max-content; }
  .current-user-name { max-width: none; white-space: nowrap; }
  .current-user-name span:last-child { overflow: visible; text-overflow: clip; }
}
.current-user-icon { color: var(--ok); font-size: 9px; flex: 0 0 auto; }
.share-button { display: inline-flex; align-items: center; gap: 9px; padding: 11px 14px; border-radius: 16px; }

/* Cartes principales de création d’identité et de contacts. */
.setup-card, /* Liste des contacts : chaque contact occupe une ligne complète. */
.contacts-home {
  background: rgba(15,28,47,.86);
  border: 1px solid var(--border);
  border-radius: 24px;
  box-shadow: var(--shadow);
}
.setup-card { padding: 24px; }
.setup-card h2, .contacts-title-row h2 { margin: 0; }
.setup-row { display: grid; grid-template-columns: 1fr auto; gap: 10px; }
.incoming-setup-notice {
  margin-top: 14px;
  padding: 12px 14px;
  border-radius: 14px;
  border: 1px solid rgba(245,158,11,.45);
  background: rgba(245,158,11,.10);
  color: #fde68a;
}

.contacts-home { overflow: hidden; }
.contacts-title-row { padding: 20px 20px 13px; border-bottom: 1px solid var(--border); }
.contacts-list { display: flex; flex-direction: column; }
/* Bouton représentant un contact ; il accepte clic, clic droit et appui long. */
.contact-item {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  text-align: left;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  padding: 18px 20px;
  border-radius: 0;
  border-bottom: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  font-size: 17px;
  font-weight: 750;
}
.contact-item:last-child { border-bottom: 0; }
.contact-item:hover { background: rgba(47,128,237,.10); }

.contact-name { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.contact-status-group {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  gap: 9px;
}
.contact-unread-count {
  min-width: 24px;
  height: 24px;
  padding: 0 7px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-size: 12px;
  font-weight: 900;
  line-height: 1;
  box-shadow: 0 3px 10px rgba(47,128,237,.28);
}
/* Petite coche d’état placée à droite de chaque contact. */
.contact-connection-indicator {
  width: 24px;
  height: 24px;
  flex-basis: 24px;
  pointer-events: none;
}
.contact-connection-indicator .connection-check { font-size: 14px; }
/* Fenêtre d’actions ouverte par appui long ou clic droit. */
/* Fenêtre utilisée pour modifier seulement le nom public de l’identité. */
.rename-user-card { width: min(100%, 440px); }
.rename-user-label { text-align: left; }

/* Zone dangereuse placée dans la fenêtre de profil local. */
.account-danger-zone { margin-top: 22px; text-align: left; }
.account-danger-separator { height: 1px; margin: 20px 0; background: var(--border); }
.account-danger-zone h3 { margin: 0 0 7px; font-size: 1rem; }
.account-reset-button {
  width: 100%;
  margin-top: 10px;
  color: #fecaca;
  border-color: rgba(239,83,80,.45);
  background: rgba(239,83,80,.10);
}
.account-reset-button:hover { background: rgba(239,83,80,.20); }
.account-reset-confirm {
  margin-top: 12px;
  padding: 14px;
  border: 1px solid rgba(239,83,80,.38);
  border-radius: 14px;
  background: rgba(239,83,80,.08);
}
.account-reset-confirm p { margin-top: 0; line-height: 1.45; }
.account-reset-confirm .modal-actions { margin-top: 12px; }

.contact-action-card { width: min(100%, 390px); }
.contact-action-icon {
  width: 54px;
  height: 54px;
  margin: 0 auto 14px;
  display: grid;
  place-items: center;
  border-radius: 18px;
  background: rgba(148,163,184,.13);
  border: 1px solid var(--border);
  font-size: 30px;
  line-height: 1;
}
.contact-delete-button {
  width: 100%;
  margin-top: 12px;
  background: rgba(239,83,80,.12);
  border: 1px solid rgba(239,83,80,.42);
  color: #fecaca;
}
.contact-delete-button:hover { background: rgba(239,83,80,.20); }
.contact-delete-confirm { margin-top: 14px; }
.contact-delete-confirm p { line-height: 1.5; }
.danger-solid { background: var(--danger); }
.danger-solid:hover { background: #d93f3c; }

.contacts-empty { padding: 42px 20px; text-align: center; }
.contacts-empty p { margin: 4px 0; }

/* Chat plein écran, dimensionné avec les variables calculées par visualViewport. */
.chat-view {
  position: fixed;
  top: var(--chat-viewport-top);
  right: 0;
  bottom: auto;
  left: 0;
  width: 100%;
  height: var(--chat-viewport-height);
  min-height: 0;
  overflow: hidden;
  overscroll-behavior: none;
  z-index: 60;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  background: var(--bg);
}
/* Première ligne de la grille : en-tête toujours visible au-dessus des messages. */
.chat-header {
  position: relative;
  top: auto;
  z-index: 10;
  min-height: 70px;
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) 14px 10px;
  border-bottom: 1px solid var(--border);
  background: rgba(10,23,39,.98);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  box-shadow: 0 5px 18px rgba(0,0,0,.18);
}
.icon-button {
  width: 44px;
  height: 44px;
  flex: 0 0 44px;
  padding: 0;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: rgba(148,163,184,.13);
}
.chat-contact-heading { min-width: 0; flex: 1 1 auto; display: flex; align-items: center; gap: 10px; }
.chat-contact-heading h2 { margin: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 20px; }
/* Cercle rouge/vert utilisé dans la liste et dans l’en-tête du chat. */
.connection-indicator {
  width: 27px;
  height: 27px;
  flex: 0 0 27px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  border: 1px solid currentColor;
  font-weight: 950;
}
.connection-indicator.is-connected { color: var(--ok); background: rgba(57,212,119,.10); }
/* Orange : le contact n’est pas en ligne, mais TXT connaît une boîte aux lettres où déposer un message. */
.connection-indicator.is-mailbox { color: #f59e0b; background: rgba(245,158,11,.12); }
/* Rouge : aucune connexion directe et aucune boîte aux lettres connue. */
.connection-indicator.is-offline,
.connection-indicator.is-disconnected { color: var(--danger); background: rgba(239,83,80,.10); }
.connection-check { font-size: 17px; line-height: 1; }

/* Deuxième ligne de la grille : seule cette zone défile. */
.messages-list {
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 18px max(14px, calc((100vw - 900px)/2));
  scroll-behavior: auto;
  overflow-anchor: none;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}
.empty-state { margin: auto; color: var(--muted); text-align: center; }
.message-row { display: flex; }
.message-row.me { justify-content: flex-end; }
.message-row.them { justify-content: flex-start; }
/* Bulle interactive : un clic révèle les dates d’envoi et de réception. */
.message-bubble {
  position: relative;
  max-width: min(76%, 680px);
  padding: 10px 12px;
  border-radius: 18px;
  line-height: 1.45;
  word-wrap: break-word;
  white-space: pre-wrap;
  cursor: pointer;
  outline: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: pan-y;
}
.message-bubble *,
.message-bubble *::before,
.message-bubble *::after {
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-user-drag: none;
}
.message-bubble::selection,
.message-bubble *::selection {
  background: transparent;
  color: inherit;
}
.message-row.me .message-bubble { background: var(--bubble-me); border-bottom-right-radius: 6px; }
.message-row.them .message-bubble { background: var(--bubble-them); border: 1px solid var(--border); border-bottom-left-radius: 6px; }
.message-bubble:focus-visible { box-shadow: 0 0 0 3px rgba(96,165,250,.45); }
/* Petite coche superposée : orange en boîte aux lettres, verte après réception réelle. */
.delivery-check {
  position: absolute;
  right: 4px;
  bottom: 3px;
  font-size: 9px;
  line-height: 1;
  font-weight: 950;
  text-shadow: 0 1px 2px rgba(0,0,0,.65);
  pointer-events: none;
}
.delivery-check.is-mailbox { color: #f59e0b; }
.delivery-check.is-delivered { color: #4ade80; }
/* Détails temporels cachés tant que la bulle n’est pas développée. */
.message-meta {
  margin-top: 7px;
  padding-top: 6px;
  border-top: 1px solid rgba(255,255,255,.16);
  color: rgba(229,238,251,.78);
  font-size: 11px;
  white-space: normal;
}
.message-row.them .message-meta { color: var(--muted); }
/* Troisième ligne de la grille : zone de saisie conservée au-dessus du clavier. */
.composer {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  padding: 12px max(12px, calc((100vw - 900px)/2)) calc(12px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  background: rgba(8,21,35,.98);
}
.composer textarea { min-height: 48px; max-height: 160px; resize: none; }
.composer button { align-self: end; min-height: 48px; }

/* Voile plein écran derrière toutes les fenêtres modales. */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(2,6,23,.84);
  backdrop-filter: blur(9px);
}
/* Carte centrale et défilable si l’écran est trop petit. */
.modal-card {
  position: relative;
  width: min(100%, 480px);
  max-height: calc(100dvh - 36px);
  overflow-y: auto;
  padding: 26px;
  border: 1px solid var(--border);
  border-radius: 25px;
  background: #0f1c2f;
  box-shadow: var(--shadow);
  text-align: center;
}
.modal-card h2 { margin: 0 0 10px; }
.modal-close {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 40px;
  height: 40px;
  padding: 0;
  border-radius: 50%;
  background: rgba(148,163,184,.13);
  font-size: 25px;
}
.modal-icon, .share-modal-icon {
  width: 58px;
  height: 58px;
  margin: 0 auto 14px;
  border-radius: 18px;
  display: grid;
  place-items: center;
  background: rgba(47,128,237,.15);
  border: 1px solid rgba(47,128,237,.35);
}
.share-status { margin: 14px 0; color: var(--muted); font-size: 13px; }
.share-status.pending { color: #fde68a; }
.share-status.error { color: #fecaca; }
/* Cadre blanc indispensable pour préserver le contraste du QR code. */
.qr-wrap {
  width: fit-content;
  margin: 0 auto 14px;
  padding: 10px;
  background: #fff;
  border-radius: 18px;
}
.qr-wrap canvas { display: block; max-width: min(264px, 72vw); height: auto !important; }
.share-link { min-height: 76px; resize: none; font-size: 12px; line-height: 1.4; }
.share-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 12px; }
.text-button { margin-top: 12px; padding: 7px 8px; background: transparent; color: #8bbdff; font-size: 13px; }
.text-button:hover { background: rgba(47,128,237,.08); }
.invite-request { font-size: 18px; line-height: 1.45; }
.fingerprint { color: var(--muted); font-size: 12px; word-break: break-all; }
.modal-fingerprint { margin: 12px 0; }
.modal-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 20px; }
.status-card.error { padding: 12px; border: 1px solid rgba(239,83,80,.55); border-radius: 14px; color: #fecaca; background: rgba(239,83,80,.08); }
/* Notification courte affichée par la fonction toast() d’app.js. */
.toast {
  position: fixed;
  z-index: 200;
  left: 50%;
  bottom: calc(18px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  max-width: calc(100vw - 32px);
  padding: 10px 15px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: #12233a;
  box-shadow: var(--shadow);
  text-align: center;
}

/* Ajustements spécifiques aux petits écrans. */
@media (max-width: 600px) {
  .app-shell { padding: 14px 12px 36px; }
  .topbar { padding-bottom: 14px; align-items: flex-start; }
  .brand-block { min-width: 0; }
  .current-user-name { max-width: calc(100vw - 92px); }
  .share-button span { display: none; }
  .share-button { width: 46px; height: 46px; padding: 0; justify-content: center; border-radius: 50%; }
  .setup-row { grid-template-columns: 1fr; }
  .message-bubble { max-width: 87%; }
  .modal-card { padding: 24px 18px; }
  .share-actions, .modal-actions { grid-template-columns: 1fr; }
}

/* ========================================================================== */
/* SAUVEGARDE LOCALE CHIFFRÉE                                                 */
/* ========================================================================== */

/* Regroupe les actions principales de l’accueil dans l’angle supérieur droit. */
.topbar-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 9px;
  flex: 0 0 auto;
}

/* Toutes les actions de l’accueil partagent le même gabarit. */
.topbar-action {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 9px;
  padding: 11px 14px;
  border-radius: 16px;
  white-space: nowrap;
}

/* Classe historique conservée pour la compatibilité des anciennes interfaces. */
.backup-button {
  background: rgba(148,163,184,.14);
  border: 1px solid var(--border);
}
.backup-button:hover { background: rgba(148,163,184,.22); }

/* Largeur adaptée au texte explicatif du gestionnaire de sauvegarde. */
.backup-modal-card, .backup-password-card { width: min(100%, 480px); }

/* Deux grandes lignes d’action faciles à toucher sur téléphone. */
.backup-choice-list {
  display: grid;
  gap: 11px;
  margin-top: 20px;
}
.backup-choice {
  width: 100%;
  display: grid;
  grid-template-columns: 42px minmax(0, 1fr);
  align-items: center;
  gap: 12px;
  padding: 14px;
  text-align: left;
}
.backup-choice > span:last-child {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.backup-choice strong { font-size: 15px; }
.backup-choice small {
  color: var(--muted);
  font-size: 12px;
  font-weight: 550;
  line-height: 1.4;
}
.backup-choice-icon {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  border-radius: 13px;
  background: rgba(255,255,255,.10);
  font-size: 22px;
  font-weight: 900;
}
.backup-warning { margin: 18px 0 0; }

/* L’erreur de mot de passe est espacée des champs sans déplacer le titre. */
#backupPasswordError { margin-top: 14px; text-align: left; }

@media (max-width: 600px) {
  /* Sur petit écran, seuls les symboles des actions supérieures sont affichés. */
  .topbar-actions { gap: 7px; }
  .topbar-action {
    width: 44px;
    height: 44px;
    padding: 0;
    border-radius: 50%;
  }
  .topbar-action span { display: none; }

  /* L’ancien calcul ne tenait compte que d’un seul bouton dans l’en-tête. */
  .current-user-name { max-width: calc(100vw - 146px); }
}

/* ========================================================================== */
/* PIÈCES JOINTES                                                             */
/* ========================================================================== */

/* Le compositeur possède désormais trois colonnes : trombone, texte, envoi. */
.composer {
  grid-template-columns: auto minmax(0, 1fr) auto;
}

.attach-button {
  width: 48px;
  min-width: 48px;
  height: 48px;
  padding: 0;
  align-self: end;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: rgba(148,163,184,.14);
  color: #dbeafe;
}
.attach-button:hover { background: rgba(148,163,184,.23); }
.attach-button:disabled { opacity: .45; cursor: not-allowed; }

/* Les bulles de fichiers ont un peu plus de largeur afin de conserver le nom lisible. */
.message-bubble.file-message-bubble {
  width: min(78vw, 440px);
  white-space: normal;
  cursor: default;
}
.file-message-card {
  display: grid;
  grid-template-columns: 42px minmax(0, 1fr);
  gap: 10px;
  align-items: center;
}
.file-message-icon {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  border-radius: 13px;
  background: rgba(255,255,255,.10);
  font-size: 21px;
}
.file-message-main { min-width: 0; }
.file-message-name {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 800;
}
.file-message-size,
.file-message-status {
  display: block;
  margin-top: 2px;
  color: rgba(229,238,251,.72);
  font-size: 12px;
}
.message-row.them .file-message-size,
.message-row.them .file-message-status { color: var(--muted); }
.file-progress {
  width: 100%;
  height: 5px;
  margin-top: 8px;
  overflow: hidden;
  border-radius: 999px;
  background: rgba(148,163,184,.22);
}
.file-progress > span {
  display: block;
  width: 0;
  height: 100%;
  border-radius: inherit;
  background: #60a5fa;
  transition: width .15s linear;
}
.file-message-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 7px;
  margin-top: 9px;
}
.file-message-actions button {
  min-height: 34px;
  padding: 6px 10px;
  border-radius: 11px;
  font-size: 12px;
}
.file-message-actions .danger { color: #fecaca; }
.file-message-bubble .message-meta { cursor: pointer; }



/* Une image est une bulle visuelle autonome : aucun cartouche, nom ou statut n'est affiché. */
.message-bubble.file-message-bubble.image-only-message-bubble {
  /* JavaScript calcule la taille d'affichage en conservant le ratio :
     - 800 px de large au maximum ;
     - 500 px de haut au maximum ;
     - une petite image n'est jamais agrandie ;
     - sur téléphone, l’aperçu reste aussi limité à 82 % de la largeur visible. */
  width: min(var(--txt-image-display-width, 500px), 800px, 82vw);
  max-width: min(800px, 82vw);
  padding: 0;
  border: 0;
  background: transparent;
  overflow: visible;
  flex: 0 1 auto;
}
.message-row.me .message-bubble.image-only-message-bubble,
.message-row.them .message-bubble.image-only-message-bubble {
  background: transparent;
  border: 0;
}
.file-message-card-image-only {
  display: block;
  width: 100%;
  padding: 0;
  background: transparent;
}
.image-only-message-bubble .file-inline-image-wrap {
  display: block;
  width: 100%;
  height: auto;
  /* Le ratio exact est injecté par JavaScript à partir de la miniature.
     La miniature tient toujours dans un cadre maximal de 800 × 500 px. */
  aspect-ratio: var(--txt-image-ratio, auto);
  max-width: 800px;
  max-height: 500px;
  overflow: hidden;
  border-radius: 16px;
  box-shadow: 0 7px 24px rgba(0,0,0,.18);
}
.image-only-message-bubble .file-inline-image {
  display: block;
  width: 100%;
  height: 100%;
  min-height: 100%;
  max-width: 800px;
  max-height: 500px;
  object-fit: contain;
  background: transparent;
}
.image-only-message-bubble .message-meta {
  margin-top: 6px;
  padding: 7px 10px;
  border: 0;
  border-radius: 10px;
  background: rgba(15,28,47,.94);
}

/* Aperçu léger : seule une miniature compressée est conservée dans TXT. */
.file-message-card-with-preview {
  grid-template-columns: 1fr;
}
.file-message-card-with-preview > .file-message-icon { display: none; }
.file-inline-image-wrap {
  width: 100%;
  max-height: min(52vh, 520px);
  overflow: hidden;
  border-radius: 14px;
  background: rgba(15,23,42,.28);
  display: grid;
  place-items: center;
}
.file-inline-image {
  display: block;
  width: 100%;
  height: auto;
  object-fit: contain;
  background: rgba(2,6,23,.28);
}
.file-message-card-with-preview .file-message-main { padding-top: 8px; }
.file-message-card.file-openable { cursor: zoom-in; }
.file-message-card.file-openable:focus-visible {
  outline: 2px solid #60a5fa;
  outline-offset: 3px;
}

/* Résumé lisible du gros fichier avant l’ouverture du sélecteur système. */
.incoming-file-summary {
  display: grid;
  gap: 5px;
  margin: 16px 0;
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: rgba(148,163,184,.08);
  text-align: left;
}
.incoming-file-summary strong {
  overflow-wrap: anywhere;
  font-size: 15px;
}
.incoming-file-summary span { font-size: 13px; }
#incomingFileError { margin: 12px 0; text-align: left; }

/* L’iframe du téléchargement progressif doit exister dans le DOM sans être visible. */
.txt-stream-download-frame {
  position: fixed;
  width: 1px;
  height: 1px;
  left: -9999px;
  top: -9999px;
  border: 0;
  opacity: 0;
  pointer-events: none;
}

@media (max-width: 600px) {
  .message-bubble.file-message-bubble { width: min(88vw, 430px); max-width: 88%; }
  .composer { gap: 8px; }
  .attach-button { width: 46px; min-width: 46px; height: 46px; }
}


/* ========================================================================== */
/* RÉPONSES ET ACTIONS CONTEXTUELLES DES MESSAGES                              */
/* ========================================================================== */

/* Citation grisée affichée dans la bulle d’un message de réponse. */
.message-reply-quote {
  display: grid;
  gap: 2px;
  margin: -2px 0 8px;
  padding: 7px 9px;
  border-left: 3px solid rgba(148,163,184,.75);
  border-radius: 8px;
  background: rgba(2,6,23,.22);
  color: rgba(226,232,240,.82);
  font-size: 12px;
  line-height: 1.35;
  white-space: normal;
}
.message-reply-quote strong {
  color: #cbd5e1;
  font-size: 11px;
}
.message-reply-quote span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Aperçu de la réponse en préparation, sur toute la largeur du compositeur. */
.reply-composer-preview {
  grid-column: 1 / -1;
  min-width: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-left: 3px solid #94a3b8;
  border-radius: 10px;
  background: rgba(148,163,184,.11);
  color: #cbd5e1;
}
.reply-composer-content {
  min-width: 0;
  display: grid;
  gap: 2px;
}
.reply-composer-content strong { font-size: 12px; color: #e2e8f0; }
.reply-composer-content span {
  overflow: hidden;
  color: var(--muted);
  font-size: 12px;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.reply-cancel-button {
  width: 32px;
  min-width: 32px;
  height: 32px;
  min-height: 32px !important;
  padding: 0;
  border-radius: 50%;
  background: transparent;
  color: #cbd5e1;
  font-size: 22px;
  line-height: 1;
}
.reply-cancel-button:hover { background: rgba(255,255,255,.09); }

/* Voile et panneau inférieur ouverts par appui long immobile ou clic droit. */
.message-action-sheet {
  position: absolute;
  inset: 0;
  z-index: 80;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}
.message-action-backdrop {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  backdrop-filter: none;
  -webkit-backdrop-filter: none;
  box-shadow: none;
  outline: none;
  -webkit-tap-highlight-color: transparent;
}
/*
 * Ce bouton transparent occupe toute la conversation pour fermer le panneau au toucher. Les règles
 * génériques button:hover/button:active ne doivent surtout pas lui appliquer le bleu de l’application.
 */
.message-action-backdrop:hover,
.message-action-backdrop:active,
.message-action-backdrop:focus,
.message-action-backdrop:focus-visible {
  background: transparent;
  transform: none;
  box-shadow: none;
  outline: none;
}
.message-action-panel {
  position: relative;
  z-index: 1;
  width: min(100%, 720px);
  padding: 12px max(14px, env(safe-area-inset-left)) calc(14px + env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-right));
  border: 1px solid rgba(255,255,255,.12);
  border-bottom: 0;
  border-radius: 18px 18px 0 0;
  background: rgba(12,27,45,.99);
  box-shadow: 0 -8px 24px rgba(0,0,0,.28);
}
.message-action-preview {
  display: grid;
  gap: 4px;
  overflow: hidden;
  margin: 0 2px 11px;
  padding: 10px 12px;
  border-left: 3px solid #64748b;
  border-radius: 10px;
  background: rgba(148,163,184,.10);
  color: var(--muted);
  font-size: 12px;
  line-height: 1.35;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}
.message-action-preview-label {
  color: #cbd5e1;
  font-size: 11px;
  font-weight: 800;
}
.message-action-preview-value {
  display: -webkit-box;
  overflow: hidden;
  color: #e2e8f0;
  overflow-wrap: anywhere;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
}
.message-action-buttons {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 9px;
}
.message-action-button {
  min-height: 62px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 5px;
  padding: 8px;
  border: 1px solid rgba(255,255,255,.10);
  border-radius: 15px;
  background: rgba(148,163,184,.10);
  color: #e5edf8;
  font-size: 12px;
}
.message-action-button:hover { background: rgba(148,163,184,.18); }
.message-action-button.danger { color: #fca5a5; background: rgba(239,68,68,.10); }
.message-action-button.danger:hover { background: rgba(239,68,68,.19); }

/* ========================================================================== */
/* PARAMÈTRES, RELAIS ET DIAGNOSTICS                                           */
/* ========================================================================== */

.settings-view {
  position: fixed;
  inset: 0;
  z-index: 70;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  background: var(--background, #07111f);
  color: var(--text, #f3f7ff);
  min-height: 100dvh;
}

.settings-view.hidden { display: none; }
body.settings-open { overflow: hidden; }

.settings-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) 14px 12px;
  border-bottom: 1px solid rgba(255,255,255,.09);
  background: rgba(7,17,31,.96);
  backdrop-filter: blur(14px);
}
.settings-header .icon-button { font-size: 34px; line-height: 1; }
.settings-header h2 { margin: 0; font-size: 1.22rem; }
.settings-header p { margin: 2px 0 0; color: var(--muted, #91a3bb); font-size: .82rem; }

.settings-content {
  overflow-y: auto;
  padding: 14px 14px calc(30px + env(safe-area-inset-bottom));
  display: grid;
  gap: 14px;
  max-width: 860px;
  width: 100%;
  margin: 0 auto;
}

.settings-card {
  border: 1px solid rgba(255,255,255,.09);
  border-radius: 18px;
  background: rgba(255,255,255,.045);
  padding: 16px;
  box-shadow: 0 14px 40px rgba(0,0,0,.13);
}
.settings-card-title {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 14px;
}
.settings-card-title h3 { margin: 0; font-size: 1.05rem; }
.settings-card-title p { margin: 4px 0 0; color: var(--muted, #91a3bb); font-size: .82rem; }
.settings-badge,
.active-pool-badge,
.optional-label {
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  padding: 4px 8px;
  background: rgba(120,160,255,.13);
  color: #bcd0ff;
  font-size: .72rem;
  white-space: nowrap;
}
.active-pool-badge { padding: 2px 7px; margin-left: 7px; color: #8ef0ba; background: rgba(55,210,130,.12); }
.optional-label { margin-left: 7px; padding: 2px 6px; color: #bac5d4; background: rgba(255,255,255,.08); }

.status-check {
  width: 28px;
  height: 28px;
  border: 0;
  border-radius: 50%;
  display: inline-grid;
  place-items: center;
  font-weight: 900;
  color: white;
  flex: 0 0 auto;
  padding: 0;
}
button.status-check { cursor: pointer; }
.status-green { background: #16a766 !important; }
.status-red { background: #d9424e !important; }
.status-unknown { background: #657388 !important; }
.status-testing { background: #c68a23 !important; animation: txt-pulse 1s infinite alternate; }
@keyframes txt-pulse { to { opacity: .52; } }


.settings-overview-card { padding-top: 10px; padding-bottom: 10px; }
.settings-overview-card .overview-status-row:first-child { border-top: 0; }
.mailbox-activation-switch {
  display: flex;
  width: 100%;
  justify-content: flex-start;
  padding: 12px 14px;
  border: 1px solid rgba(94, 234, 212, .24);
  border-radius: 14px;
  background: rgba(20, 184, 166, .08);
  font-weight: 700;
}
.mailbox-activation-switch input { width: 20px; height: 20px; accent-color: #16a766; }

.overview-status-list { display: grid; gap: 8px; }
.overview-status-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  align-items: center;
  gap: 10px;
  min-height: 36px;
  padding: 7px 0;
  border-top: 1px solid rgba(255,255,255,.06);
}
.overview-detail { color: var(--muted, #91a3bb); font-size: .8rem; }
.overview-status-row .status-check { width: 21px; height: 21px; font-size: .76rem; }

.settings-actions-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  margin-bottom: 12px;
}
.settings-actions-grid button { min-height: 42px; }
.settings-inline-field {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 12px 0;
  color: var(--muted, #91a3bb);
}
.settings-inline-field input { max-width: 90px; }
.settings-search,
.settings-textarea,
.settings-card input,
.settings-card select,
.settings-modal-card input,
.settings-modal-card textarea {
  width: 100%;
  border-radius: 12px;
  border: 1px solid rgba(255,255,255,.12);
  background: rgba(0,0,0,.16);
  color: inherit;
  padding: 11px 12px;
}
.settings-search { margin-bottom: 10px; }
.settings-textarea { resize: vertical; }
.settings-two-columns {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  margin: 9px 0 12px;
}
.settings-two-columns label { display: grid; gap: 6px; color: var(--muted, #91a3bb); font-size: .8rem; }
.settings-explanation { color: var(--muted, #91a3bb); font-size: .86rem; line-height: 1.5; }

.settings-switch,
.mini-switch { display: inline-flex; align-items: center; gap: 10px; cursor: pointer; }
.settings-switch { margin: 10px 0 14px; }
.settings-switch input { width: auto; }
.mini-switch input { position: absolute; opacity: 0; pointer-events: none; }
.mini-switch span {
  width: 34px; height: 20px; border-radius: 999px;
  background: #596578; position: relative; transition: .2s;
}
.mini-switch span::after {
  content: ""; position: absolute; width: 14px; height: 14px; border-radius: 50%;
  top: 3px; left: 3px; background: white; transition: .2s;
}
.mini-switch input:checked + span { background: #168d5b; }
.mini-switch input:checked + span::after { transform: translateX(14px); }

.settings-relay-list { display: grid; gap: 8px; }
.settings-relay-row {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  gap: 10px;
  align-items: center;
  padding: 11px;
  border-radius: 14px;
  border: 1px solid rgba(255,255,255,.08);
  background: rgba(0,0,0,.12);
}
.settings-relay-row.relay-disabled { opacity: .58; }
.relay-status-button { width: 23px; height: 23px; font-size: .76rem; }
.relay-row-main { min-width: 0; }
.relay-url-line { min-width: 0; display: flex; align-items: center; flex-wrap: wrap; }
.relay-url-line strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 100%; font-size: .88rem; }
.relay-meta { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 4px; color: var(--muted, #91a3bb); font-size: .72rem; }
.relay-error-text { color: #ff969f; font-size: .72rem; margin-top: 4px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.relay-row-controls { display: flex; align-items: center; gap: 5px; }
.relay-more-button { border: 0; background: transparent; color: inherit; font-size: 1.4rem; padding: 5px 7px; }

.settings-modal-card { width: min(92vw, 560px); }
.diagnostics-modal-card { width: min(94vw, 650px); max-height: 86dvh; overflow-y: auto; }
.diagnostics-url { word-break: break-all; color: var(--muted, #91a3bb); }
.diagnostics-tests { display: grid; gap: 8px; margin: 15px 0; }
.diagnostic-test-row {
  display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 10px; align-items: start;
  padding: 10px; border-radius: 12px; background: rgba(0,0,0,.14);
}
.diagnostic-test-row .status-check { width: 22px; height: 22px; font-size: .72rem; }
.diagnostic-test-row strong { font-size: .88rem; }
.diagnostic-test-row p { margin: 3px 0 0; color: var(--muted, #91a3bb); font-size: .77rem; line-height: 1.4; }

@media (max-width: 560px) {
  .settings-actions-grid { grid-template-columns: 1fr 1fr; }
  .settings-two-columns { grid-template-columns: 1fr; }
  .settings-relay-row { grid-template-columns: auto minmax(0,1fr); }
  .relay-row-controls { grid-column: 2; justify-content: flex-end; }
  .topbar-action.settings-button span { display: none; }
}

/* La réinitialisation complète reste la dernière carte de la page Paramètres. */
.settings-danger-card {
  margin-bottom: calc(18px + env(safe-area-inset-bottom));
  border-color: rgba(239,68,68,.38);
  background: rgba(127,29,29,.09);
}
.settings-danger-card .settings-card-title h3 { color: #fecaca; }
.settings-danger-card .account-reset-button { width: 100%; }


/* ========================================================================== */
/* AJOUT D’UN CONTACT                                                         */
/* ========================================================================== */

.add-contact-view {
  position: fixed;
  inset: 0;
  z-index: 72;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  background: var(--background, #07111f);
  color: var(--text, #e5edf8);
}

.add-contact-header {
  min-height: 66px;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: max(10px, env(safe-area-inset-top)) 16px 10px;
  border-bottom: 1px solid var(--border);
  background: rgba(7,17,31,.97);
}

.add-contact-header h2 { margin: 0; font-size: 20px; }

.add-contact-content {
  overflow-y: auto;
  width: min(100%, 720px);
  margin: 0 auto;
  padding: 20px 16px calc(28px + env(safe-area-inset-bottom));
}

.add-contact-card {
  padding: 18px;
  border: 1px solid var(--border);
  border-radius: 20px;
  background: rgba(15,29,47,.88);
}

.add-contact-card h3 { margin: 0 0 8px; }
.add-contact-card p { margin: 0 0 16px; }
.add-invite-input { min-height: 118px; resize: vertical; overflow-wrap: anywhere; font-size: 13px; line-height: 1.45; }
#btnValidateInviteLink { width: 100%; margin-top: 12px; }

.add-contact-status {
  margin-top: 12px;
  padding: 11px 12px;
  border-radius: 12px;
  background: rgba(59,130,246,.12);
  color: #bfdbfe;
  font-size: 13px;
  line-height: 1.4;
}
.add-contact-status.error { background: rgba(239,68,68,.12); color: #fecaca; }
.add-contact-status.success { background: rgba(34,197,94,.12); color: #bbf7d0; }

.add-contact-separator {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 16px 4px;
  color: var(--muted);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .12em;
}
.add-contact-separator::before,
.add-contact-separator::after { content: ""; flex: 1; height: 1px; background: var(--border); }

.qr-scan-button {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}

.qr-scanner-panel { margin-top: 14px; }
.qr-video-wrap {
  position: relative;
  overflow: hidden;
  width: 100%;
  aspect-ratio: 1 / 1;
  border-radius: 18px;
  background: #000;
}
.qr-video-wrap video { width: 100%; height: 100%; display: block; object-fit: cover; }
.qr-scanner-frame {
  position: absolute;
  inset: 16%;
  border: 3px solid rgba(255,255,255,.94);
  border-radius: 18px;
  box-shadow: 0 0 0 999px rgba(0,0,0,.34);
  pointer-events: none;
}
.qr-scanner-status { margin: 12px 0; text-align: center; color: var(--muted); font-size: 13px; }
#btnStopQrScanner { width: 100%; }

@media (max-width: 600px) {
  /* Trois actions supérieures : le nom dispose d’un peu moins de largeur. */
  .current-user-name { max-width: calc(100vw - 196px); }
}


/* Instructions de la boîte aux lettres PHP */
.relay-instructions-list {
  display: grid;
  gap: 10px;
  margin: 16px 0;
  padding-left: 22px;
  line-height: 1.5;
}
.relay-instructions-list code {
  background: rgba(255,255,255,.08);
  border-radius: 6px;
  padding: 2px 5px;
}


/* ========================================================================== */
/* SAUVEGARDE DANS LA PAGE PARAMÈTRES                                         */
/* ========================================================================== */

/* Bouton pleine largeur placé juste avant la zone de réinitialisation. */
.settings-backup-button {
  width: 100%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin-top: 12px;
}

.settings-backup-button svg { flex: 0 0 auto; }

/* Deux actions directes dans Paramètres, sans écran intermédiaire. */
.settings-backup-actions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin-top: 12px;
}
.settings-backup-actions .settings-backup-button {
  margin-top: 0;
  min-height: 48px;
}
@media (max-width: 680px) {
  .settings-backup-actions { grid-template-columns: 1fr; }
}


@media (max-width: 640px) {
  .message-bubble.file-message-bubble.image-only-message-bubble {
    width: min(var(--txt-image-display-width, 500px), 800px, 82vw);
    max-width: min(800px, 82vw);
  }
}


/* ========================================================================== */
/* VISIONNEUSE D'IMAGE PLEIN ÉCRAN                                            */
/* ========================================================================== */
.image-lightbox {
  position: fixed;
  inset: 0;
  z-index: 5000;
  display: grid;
  place-items: center;
  padding: max(18px, env(safe-area-inset-top)) max(18px, env(safe-area-inset-right))
           max(18px, env(safe-area-inset-bottom)) max(18px, env(safe-area-inset-left));
  background: rgba(0,0,0,.94);
  cursor: zoom-out;
}
.image-lightbox.hidden { display: none; }
.image-lightbox img {
  display: block;
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  object-fit: contain;
  border-radius: 8px;
  box-shadow: 0 18px 60px rgba(0,0,0,.55);
  cursor: default;
}
.image-lightbox-close {
  position: fixed;
  top: max(14px, env(safe-area-inset-top));
  right: max(14px, env(safe-area-inset-right));
  z-index: 5001;
  width: 44px;
  height: 44px;
  padding: 0;
  display: grid;
  place-items: center;
  border-radius: 50%;
  border: 1px solid rgba(255,255,255,.28);
  background: rgba(10,15,24,.72);
  color: #fff;
  font-size: 30px;
  line-height: 1;
}
.image-lightbox-close:hover { background: rgba(30,41,59,.92); }


/* --------------------------------------------------------------------------
   INSTALLATION PWA
   -------------------------------------------------------------------------- */
.pwa-install-card .settings-backup-button {
  width: 100%;
  justify-content: center;
}

.pwa-install-instructions {
  margin-top: 14px;
  padding: 14px 16px;
  border: 1px solid rgba(148, 163, 184, 0.28);
  border-radius: 14px;
  background: rgba(15, 23, 42, 0.72);
  line-height: 1.5;
}

.pwa-install-instructions ol {
  margin: 8px 0 0 20px;
  padding: 0;
}

.pwa-install-instructions li + li {
  margin-top: 5px;
}

@media (display-mode: standalone) {
  body {
    overscroll-behavior-y: none;
  }
}
