/* ═══════════════════════════════════════════════════════════════════════════
   EVYA — Assistant conversationnel : widget web
   Partagé par le site vitrine et l'administration.

   Aucune dépendance : ni framework, ni police distante, ni icône externe
   (les pictogrammes sont des SVG inline dans le script).

   RESPONSIVE — deux dispositions, pas un simple redimensionnement :
     · ≥ 640px : panneau flottant ancré en bas à droite, la page reste visible
                 et utilisable derrière ;
     · < 640px : plein écran, car sur un mobile un panneau de 380px laisserait
                 un champ de saisie inutilisable une fois le clavier ouvert.

   Le panneau utilise dvh (et non vh) : sur iOS, vh ignore la barre d'adresse
   rétractable, ce qui poussait le champ de saisie sous l'écran.
   ═══════════════════════════════════════════════════════════════════════════ */

.evya-assistant {
  --ea-primary: #1C1C3A;
  --ea-accent: #4f46e5;
  --ea-bg: #ffffff;
  --ea-surface: #f8fafc;
  --ea-border: #e2e8f0;
  --ea-text: #1C1C3A;
  --ea-muted: #64748b;
  --ea-user-bg: #4f46e5;
  --ea-user-text: #ffffff;
  --ea-radius: 16px;
  --ea-shadow: 0 18px 48px rgba(15, 23, 42, .18);

  position: fixed;
  z-index: 2147483000; /* au-dessus des bandeaux cookies et modales du site */
  bottom: 20px;
  right: 20px;
  font-family: inherit;
  font-size: 15px;
  line-height: 1.55;
  color: var(--ea-text);
}

.evya-assistant *,
.evya-assistant *::before,
.evya-assistant *::after {
  box-sizing: border-box;
}

/* ── Bouton d'ouverture ─────────────────────────────────────────────────── */

.ea-launcher {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  padding: 0 18px 0 16px;
  height: 54px;
  border: none;
  border-radius: 999px;
  background: var(--ea-primary);
  color: #fff;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 10px 28px rgba(15, 23, 42, .28);
  transition: transform .18s ease, box-shadow .18s ease;
}

.ea-launcher:hover { transform: translateY(-2px); box-shadow: 0 14px 34px rgba(15, 23, 42, .34); }
.ea-launcher:active { transform: translateY(0); }
.ea-launcher:focus-visible { outline: 3px solid var(--ea-accent); outline-offset: 3px; }
.ea-launcher svg { width: 22px; height: 22px; flex: none; }
.ea-launcher-label { white-space: nowrap; }

/* Sur très petit écran, le libellé disparaît : un bouton texte de 160px
   masquerait le contenu de la page. */
@media (max-width: 400px) {
  .ea-launcher { padding: 0; width: 54px; justify-content: center; }
  .ea-launcher-label { display: none; }
}

.evya-assistant[data-open="true"] .ea-launcher { display: none; }

/* ── Panneau ────────────────────────────────────────────────────────────── */

.ea-panel {
  display: none;
  flex-direction: column;
  width: 400px;
  max-width: calc(100vw - 32px);
  height: 620px;
  max-height: calc(100dvh - 40px);
  background: var(--ea-bg);
  border: 1px solid var(--ea-border);
  border-radius: var(--ea-radius);
  box-shadow: var(--ea-shadow);
  overflow: hidden;
}

.evya-assistant[data-open="true"] .ea-panel { display: flex; }

@media (max-width: 639px) {
  .evya-assistant { inset: 0; bottom: 16px; right: 16px; }
  .evya-assistant[data-open="true"] { inset: 0; }
  .ea-panel {
    width: 100vw;
    max-width: 100vw;
    height: 100dvh;
    max-height: 100dvh;
    border-radius: 0;
    border: none;
  }
  .evya-assistant[data-open="false"] { inset: auto 16px 16px auto; }
}

/* ── En-tête ────────────────────────────────────────────────────────────── */

.ea-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  background: var(--ea-primary);
  color: #fff;
  flex: none;
}

.ea-avatar {
  width: 36px; height: 36px; flex: none;
  border-radius: 50%;
  background: rgba(255, 255, 255, .16);
  display: grid; place-items: center;
}
.ea-avatar svg { width: 20px; height: 20px; }

.ea-header-text { min-width: 0; flex: 1; }
.ea-title { font-weight: 700; font-size: 15px; }
.ea-subtitle { font-size: 12px; opacity: .78; }

.ea-header-btn {
  width: 34px; height: 34px; flex: none;
  border: none; border-radius: 9px;
  background: rgba(255, 255, 255, .14);
  color: #fff; cursor: pointer;
  display: grid; place-items: center;
}
.ea-header-btn:hover { background: rgba(255, 255, 255, .26); }
.ea-header-btn:focus-visible { outline: 2px solid #fff; outline-offset: 2px; }
.ea-header-btn svg { width: 18px; height: 18px; }

/* ── Fil de discussion ──────────────────────────────────────────────────── */

.ea-log {
  flex: 1 1 auto;
  /* Sur un écran court, les suggestions et le composeur écraseraient le fil
     jusqu'à le rendre illisible : le fil garde toujours de quoi afficher un
     échange, ce sont les suggestions qui cèdent (voir .ea-quick). */
  min-height: 140px;
  overflow-y: auto;
  overscroll-behavior: contain;   /* évite de faire défiler la page derrière */
  -webkit-overflow-scrolling: touch;
  padding: 16px;
  background: var(--ea-surface);
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.ea-notice {
  font-size: 12px;
  color: var(--ea-muted);
  background: #fff;
  border: 1px solid var(--ea-border);
  border-radius: 12px;
  padding: 9px 11px;
}

.ea-msg { display: flex; flex-direction: column; gap: 6px; max-width: 88%; }
.ea-msg-assistant { align-self: flex-start; }
.ea-msg-user { align-self: flex-end; align-items: flex-end; }

.ea-bubble {
  padding: 10px 13px;
  border-radius: 14px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;   /* une URL longue ne doit pas élargir le panneau */
}

.ea-msg-assistant .ea-bubble {
  background: #fff;
  border: 1px solid var(--ea-border);
  border-bottom-left-radius: 5px;
}
.ea-msg-user .ea-bubble {
  background: var(--ea-user-bg);
  color: var(--ea-user-text);
  border-bottom-right-radius: 5px;
}
.ea-msg-error .ea-bubble {
  background: #fee2e2;
  border-color: #fecaca;
  color: #991b1b;
}

.ea-meta { font-size: 11px; color: var(--ea-muted); display: flex; gap: 8px; align-items: center; }

/* Sources : rend la réponse vérifiable plutôt qu'à croire sur parole. */
.ea-sources { display: flex; flex-wrap: wrap; gap: 5px; }
.ea-source {
  font-size: 11px;
  background: #eef2ff;
  color: #3730a3;
  border-radius: 999px;
  padding: 2px 9px;
}

/* Actions suggérées */
.ea-actions { display: flex; flex-wrap: wrap; gap: 6px; }
.ea-action {
  font: inherit;
  font-size: 12.5px;
  font-weight: 600;
  padding: 7px 12px;
  border-radius: 999px;
  border: 1px solid var(--ea-accent);
  background: #fff;
  color: var(--ea-accent);
  cursor: pointer;
}
.ea-action:hover { background: var(--ea-accent); color: #fff; }
.ea-action:focus-visible { outline: 2px solid var(--ea-accent); outline-offset: 2px; }
.ea-action-escalate { border-color: #b45309; color: #b45309; }
.ea-action-escalate:hover { background: #b45309; color: #fff; }

/* Avis sur une réponse */
.ea-feedback { display: flex; gap: 5px; align-items: center; }
.ea-fb {
  width: 28px; height: 28px;
  border: 1px solid var(--ea-border);
  border-radius: 8px;
  background: #fff;
  cursor: pointer;
  display: grid; place-items: center;
  color: var(--ea-muted);
}
.ea-fb:hover { border-color: var(--ea-accent); color: var(--ea-accent); }
.ea-fb[aria-pressed="true"] { background: var(--ea-accent); border-color: var(--ea-accent); color: #fff; }
.ea-fb svg { width: 15px; height: 15px; }
.ea-fb-done { font-size: 11px; color: var(--ea-muted); }

/* Indicateur de frappe */
.ea-typing { display: flex; gap: 4px; padding: 12px 14px; }
.ea-typing span {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--ea-muted);
  animation: ea-bounce 1.1s infinite ease-in-out;
}
.ea-typing span:nth-child(2) { animation-delay: .16s; }
.ea-typing span:nth-child(3) { animation-delay: .32s; }

@keyframes ea-bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: .45; }
  30% { transform: translateY(-5px); opacity: 1; }
}

/* Respecte le réglage système : une animation en boucle peut gêner
   (troubles vestibulaires, déficit d'attention). */
@media (prefers-reduced-motion: reduce) {
  .ea-typing span { animation: none; opacity: .6; }
  .ea-launcher { transition: none; }
}

/* ── Suggestions de départ ──────────────────────────────────────────────── */

.ea-quick {
  display: flex; flex-wrap: wrap; gap: 6px;
  padding: 0 16px 12px;
  background: var(--ea-surface);
  /* Quatre suggestions un peu longues occupent quatre lignes : on les borne à
     un tiers du panneau et on les fait défiler, plutôt que de laisser le fil
     se réduire à une bande de deux lignes. */
  flex: 0 1 auto;
  max-height: 33%;
  overflow-y: auto;
  overscroll-behavior: contain;
}
.ea-quick:empty { display: none; }
.ea-quick button {
  font: inherit; font-size: 12.5px;
  padding: 7px 12px;
  border-radius: 999px;
  border: 1px solid var(--ea-border);
  background: #fff; color: var(--ea-text);
  cursor: pointer; text-align: left;
}
.ea-quick button:hover { border-color: var(--ea-accent); color: var(--ea-accent); }

/* ── Zone de saisie ─────────────────────────────────────────────────────── */

.ea-composer {
  display: flex; gap: 8px; align-items: flex-end;
  padding: 12px;
  border-top: 1px solid var(--ea-border);
  background: var(--ea-bg);
  flex: none;
  padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px)); /* encoche iOS */
}

.ea-input {
  flex: 1;
  min-height: 42px;
  max-height: 120px;
  resize: none;
  padding: 10px 12px;
  border: 1px solid var(--ea-border);
  border-radius: 12px;
  font: inherit;
  /* 16px minimum : en dessous, iOS zoome automatiquement au focus et casse
     la mise en page du panneau. */
  font-size: 16px;
  line-height: 1.4;
  color: var(--ea-text);
  background: #fff;
}
.ea-input:focus { outline: none; border-color: var(--ea-accent); box-shadow: 0 0 0 3px rgba(79, 70, 229, .14); }
.ea-input:disabled { background: var(--ea-surface); color: var(--ea-muted); }

.ea-send {
  width: 42px; height: 42px; flex: none;
  border: none; border-radius: 12px;
  background: var(--ea-accent); color: #fff;
  cursor: pointer;
  display: grid; place-items: center;
}
.ea-send:disabled { background: #cbd5e1; cursor: not-allowed; }
.ea-send:focus-visible { outline: 2px solid var(--ea-primary); outline-offset: 2px; }
.ea-send svg { width: 19px; height: 19px; }

.ea-footer {
  font-size: 10.5px;
  color: var(--ea-muted);
  text-align: center;
  padding: 0 12px 10px;
  background: var(--ea-bg);
  flex: none;
}

/* ── Formulaire d'escalade ──────────────────────────────────────────────── */

.ea-escalate {
  padding: 14px 16px;
  background: #fffbeb;
  border-top: 1px solid #fde68a;
  flex: none;
}
.ea-escalate h4 { margin: 0 0 4px; font-size: 13.5px; color: #92400e; }
.ea-escalate p { margin: 0 0 10px; font-size: 12px; color: #92400e; }
.ea-escalate label { display: block; font-size: 12px; font-weight: 600; margin: 8px 0 3px; color: #92400e; }
.ea-escalate input, .ea-escalate textarea {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid #fcd34d;
  border-radius: 9px;
  font: inherit; font-size: 15px;
  background: #fff;
}
.ea-escalate textarea { resize: vertical; min-height: 60px; }
.ea-escalate-row { display: flex; gap: 8px; margin-top: 12px; }
.ea-escalate-row button { flex: 1; font: inherit; font-weight: 600; padding: 9px; border-radius: 9px; cursor: pointer; }
.ea-escalate-submit { border: none; background: #b45309; color: #fff; }
.ea-escalate-cancel { border: 1px solid #fcd34d; background: #fff; color: #92400e; }

/* ── Message d'indisponibilité ──────────────────────────────────────────── */

.ea-unavailable {
  padding: 24px 20px;
  text-align: center;
  color: var(--ea-muted);
  font-size: 13.5px;
  flex: 1;
  display: grid;
  place-content: center;
  gap: 8px;
}

/* Lecteurs d'écran uniquement */
.ea-sr {
  position: absolute; width: 1px; height: 1px;
  padding: 0; margin: -1px; overflow: hidden;
  clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}

/* ── Variante administration ────────────────────────────────────────────── */
/* Le back-office a sa propre palette : le widget s'y aligne au lieu de
   plaquer les couleurs du site public. */

.evya-assistant[data-theme="admin"] {
  --ea-accent: #4f46e5;
  --ea-primary: #1C1C3A;
}

/* Le widget admin s'écarte de la barre d'onglets sur grand écran. */
@media (min-width: 640px) {
  .evya-assistant[data-theme="admin"] { bottom: 24px; right: 24px; }
}
