/* Messagerie instantanée : widget flottant, page Messages, boîte de réception admin. */
.msg-fab, .msg-panneau, .msg-page, .msg-admin {
  --msg-accent: var(--primary, #4F6EF7);
  --msg-accent-texte: #fff;
  --msg-fond: #f4f6f8;
  --msg-bord: #e3e7eb;
  --msg-texte: #1c2733;
  --msg-doux: #6b7785;
  font-family: inherit;
  color: var(--msg-texte);
}

/* ---------- Bouton flottant ---------- */
.msg-fab {
  /* Au-dessus du bouton « retour en haut » (48 px à 22 px du bord), centres alignés. */
  position: fixed; right: 17px; bottom: 82px; z-index: 1040;
  width: 58px; height: 58px; border-radius: 50%; border: 0; cursor: pointer;
  background: var(--msg-accent); color: var(--msg-accent-texte);
  box-shadow: 0 10px 28px rgba(0, 0, 0, .28);
  display: flex; align-items: center; justify-content: center;
  transition: transform .15s ease;
}
.msg-fab:hover { transform: scale(1.06); color: var(--msg-accent-texte); text-decoration: none; }
a.msg-fab { text-decoration: none; }
.msg-fab:focus-visible, .msg-icone:focus-visible, .msg-envoyer:focus-visible { outline: 3px solid rgba(0, 0, 0, .35); outline-offset: 2px; }
.msg-fab-icone { font-size: 26px; line-height: 1; }
.msg-fab[hidden] { display: none; }

.msg-badge {
  min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px;
  background: #d62839; color: #fff; font-size: 11px; font-weight: 700; line-height: 20px; text-align: center;
  display: inline-block;
}
.msg-badge[hidden] { display: none; }
.msg-badge--fab { position: absolute; top: -4px; right: -4px; box-shadow: 0 0 0 2px #fff; }

/* ---------- Panneau du widget ---------- */
.msg-panneau {
  position: fixed; right: 20px; bottom: 20px; z-index: 1050;
  width: 380px; height: min(600px, calc(100dvh - 40px));
  background: #fff; border-radius: 14px; overflow: hidden;
  box-shadow: 0 24px 60px rgba(0, 0, 0, .28);
  display: flex; flex-direction: column;
}
.msg-panneau[hidden] { display: none; }

.msg-entete {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 12px 14px; background: var(--msg-accent); color: var(--msg-accent-texte);
}
.msg-entete-titre { display: flex; flex-direction: column; min-width: 0; line-height: 1.25; }
.msg-entete-titre strong { font-size: 15px; }
.msg-entete-titre span { font-size: 12px; opacity: .85; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg-entete--page { background: #fff; color: var(--msg-texte); border-bottom: 1px solid var(--msg-bord); justify-content: flex-start; }
.msg-entete--page .msg-entete-titre span { color: var(--msg-doux); opacity: 1; }
.msg-entete .msg-icone { color: inherit; }
.msg-retour { display: none; }

.msg-corps { flex: 1; min-height: 0; display: flex; flex-direction: column; background: var(--msg-fond); }

/* ---------- Liste des messages ---------- */
.msg-liste {
  flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain;
  padding: 14px 12px; display: flex; flex-direction: column; gap: 4px;
  -webkit-overflow-scrolling: touch;
}
.msg-vide { margin: auto; padding: 16px; text-align: center; color: var(--msg-doux); font-size: 14px; }
.msg-jour {
  align-self: center; margin: 10px 0 6px; padding: 3px 12px; border-radius: 12px;
  background: #e6eaee; color: var(--msg-doux); font-size: 12px; text-transform: capitalize;
}
.msg-nouveaux {
  display: flex; align-items: center; gap: 10px; margin: 10px 0 6px;
  color: #d62839; font-size: 12px; font-weight: 700;
}
.msg-nouveaux::before, .msg-nouveaux::after { content: ""; flex: 1; height: 1px; background: currentColor; opacity: .35; }

.msg-ligne { display: flex; margin-top: 8px; }
.msg-ligne--suite { margin-top: 2px; }
/* Moi : à droite ; l'autre : à gauche — quel que soit le côté (visiteur, compte ou admin). */
.msg-ligne--moi { justify-content: flex-end; }
.msg-ligne--autre { justify-content: flex-start; }

.msg-bulle {
  max-width: 78%; padding: 8px 12px 6px; border-radius: 16px; font-size: 14.5px; line-height: 1.4;
  overflow-wrap: anywhere; word-break: break-word;
}
.msg-ligne--moi .msg-bulle { background: var(--msg-accent); color: var(--msg-accent-texte); border-bottom-right-radius: 4px; }
.msg-ligne--autre .msg-bulle { background: #fff; color: var(--msg-texte); border: 1px solid var(--msg-bord); border-bottom-left-radius: 4px; }
.msg-ligne--moi.msg-ligne--suite .msg-bulle { border-top-right-radius: 4px; }
.msg-ligne--autre.msg-ligne--suite .msg-bulle { border-top-left-radius: 4px; }

.msg-auteur { font-size: 12px; font-weight: 700; margin-bottom: 2px; color: var(--msg-accent); }
.msg-texte { white-space: pre-wrap; }
.msg-meta { margin-top: 3px; font-size: 11px; opacity: .7; text-align: right; }
.msg-vu { font-weight: 700; }

.msg-image img { display: block; max-width: 100%; max-height: 220px; border-radius: 10px; margin-top: 4px; }
.msg-fichier {
  display: flex; align-items: center; gap: 10px; margin-top: 4px; padding: 8px 10px; border-radius: 10px;
  background: rgba(0, 0, 0, .07); color: inherit; text-decoration: none;
}
.msg-fichier:hover { background: rgba(0, 0, 0, .12); color: inherit; text-decoration: none; }
.msg-fichier-icone { font-size: 22px; }
.msg-fichier-texte { display: flex; flex-direction: column; min-width: 0; }
.msg-fichier-nom { font-weight: 600; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 190px; }
.msg-fichier-taille { font-size: 11px; opacity: .75; }

/* ---------- Composeur ---------- */
.msg-composeur { position: relative; padding: 8px 10px calc(8px + env(safe-area-inset-bottom)); background: #fff; border-top: 1px solid var(--msg-bord); }
.msg-identite { display: flex; flex-direction: column; gap: 6px; margin-bottom: 8px; }
.msg-champ {
  width: 100%; padding: 8px 10px; border: 1px solid var(--msg-bord); border-radius: 8px;
  font-size: 16px; background: #fff; color: var(--msg-texte);
}
.msg-erreur { margin: 0 0 6px; color: #d62839; font-size: 12px; font-weight: 600; }
.msg-apercu {
  display: flex; align-items: center; gap: 8px; margin-bottom: 8px; padding: 6px 10px;
  background: var(--msg-fond); border-radius: 8px; font-size: 12px;
}
.msg-apercu[hidden], .msg-erreur[hidden], .msg-identite[hidden] { display: none; }
.msg-apercu-nom { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg-barre { display: flex; align-items: flex-end; gap: 6px; }
.msg-icone {
  flex: none; width: 38px; height: 38px; border: 0; border-radius: 50%; background: transparent;
  cursor: pointer; font-size: 18px; line-height: 1; color: var(--msg-doux);
}
.msg-icone:hover { background: rgba(0, 0, 0, .06); }
.msg-saisie {
  flex: 1; min-height: 40px; max-height: 120px; resize: none; padding: 9px 12px;
  border: 1px solid var(--msg-bord); border-radius: 20px; font-size: 16px; line-height: 1.3;
  background: var(--msg-fond); color: var(--msg-texte); outline: none;
}
.msg-saisie:focus { border-color: var(--msg-accent); background: #fff; }
.msg-envoyer {
  flex: none; width: 40px; height: 40px; border: 0; border-radius: 50%; cursor: pointer;
  background: var(--msg-accent); color: var(--msg-accent-texte); font-size: 16px;
}
.msg-envoyer:disabled { opacity: .45; cursor: default; }

/* ---------- Page Messages (compte) / Boîte de réception admin : remplissage plein écran ---------- */
/* .content-area (main.css) n'a pas de hauteur déclarée (elle vient de flex:1 dans .main-wrapper),
   donc un height:100% sur ses enfants ne se résout pas (piège CSS classique). On rend .content-area
   flex uniquement quand elle contient une de ces deux pages (:has, sans toucher aux autres pages),
   puis .msg-page/.msg-admin prennent flex:1 pour occuper tout l'espace restant sous la topbar. */
.content-area:has(> .msg-page),
.content-area:has(> .msg-admin) {
  display: flex;
  flex-direction: column;
}

.msg-page {
  display: flex; flex-direction: column; flex: 1; min-height: 420px;
  background: #fff; border: 1px solid var(--msg-bord); border-radius: 12px; overflow: hidden;
}

.msg-admin {
  display: grid; grid-template-columns: 320px 1fr; flex: 1; min-height: 460px;
  background: #fff; border: 1px solid var(--msg-bord); border-radius: 12px; overflow: hidden;
}
.msg-admin-liste { display: flex; flex-direction: column; min-height: 0; border-right: 1px solid var(--msg-bord); }
.msg-admin-barre { display: flex; align-items: center; justify-content: space-between; padding: 12px 14px; border-bottom: 1px solid var(--msg-bord); }
.msg-admin-filtre { margin: 0; font-size: 12px; font-weight: 500; cursor: pointer; }
.msg-admin-items { flex: 1; overflow-y: auto; }
.msg-admin-item {
  position: relative; display: block; width: 100%; text-align: left; padding: 10px 14px;
  border: 0; border-bottom: 1px solid var(--msg-bord); background: #fff; cursor: pointer;
}
.msg-admin-item:hover { background: var(--msg-fond); }
.msg-admin-item.est-active { background: #eef3f8; box-shadow: inset 3px 0 0 var(--msg-accent); }
.msg-admin-item-haut, .msg-admin-item-bas { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.msg-admin-item-haut time { font-size: 11px; color: var(--msg-doux); flex: none; }
.msg-admin-apercu { font-size: 13px; color: var(--msg-doux); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg-admin-item.est-non-lu strong, .msg-admin-item.est-non-lu .msg-admin-apercu { color: var(--msg-texte); font-weight: 700; }
.msg-admin-type { display: inline-block; margin-top: 2px; font-size: 10px; text-transform: uppercase; letter-spacing: .04em; color: var(--msg-doux); }
.msg-admin-zone { display: flex; flex-direction: column; min-height: 0; min-width: 0; }

/* ---------- Mobile ---------- */
@media (max-width: 640px) {
  .msg-panneau { inset: 0; width: 100%; height: 100dvh; border-radius: 0; box-shadow: none; }
  .msg-bulle { max-width: 86%; }
  /* Bouton retour en haut mobile : 42 px à 14 px du bord. */
  .msg-fab { width: 52px; height: 52px; right: 9px; bottom: 68px; }
  .msg-admin { grid-template-columns: 1fr; }
  .msg-admin-zone { display: none; }
  .msg-admin--conversation .msg-admin-liste { display: none; }
  .msg-admin--conversation .msg-admin-zone { display: flex; }
  .msg-admin .msg-retour { display: inline-block; }
  .msg-page { border-radius: 0; border-left: 0; border-right: 0; }
}
body.msg-verrou { overflow: hidden; }
@media (min-width: 641px) { body.msg-verrou { overflow: auto; } }

@media (prefers-reduced-motion: reduce) { .msg-fab { transition: none; } }
