/* ==========================================================================
   137 — Typing Indicator
   Indicateur de frappe type chat : la bulle « en train d'écrire » apparaît
   avec un spring (scale depuis le coin bas côté expéditeur), ses 3 points
   ondulent en vague amortie (montée franche, retombée avec rebond qui
   s'éteint), puis la bulle se TRANSFORME en vrai message : les points
   s'effacent en crossfade pendant que le texte apparaît et que la bulle
   s'élargit élastiquement (FLIP sur width, piloté par anim.js).
   Racine : .typing-indicator — conversation scriptée en boucle + rejouer.
   ========================================================================== */

.typing-indicator {
  --ti-accent: var(--ml-accent, #7c8cff);
  --ti-them: color-mix(in srgb, var(--ml-surface, #151a24) 78%, white);

  position: relative;
  width: 300px;
  max-width: 100%;
  height: 248px;
  display: flex;
  flex-direction: column;
  border-radius: 16px;
  overflow: hidden;
  background: var(--ml-surface, #151a24);
  border: 1px solid rgba(255, 255, 255, 0.06);
  color: var(--ml-fg, #e8eaf2);
}

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

.typing-indicator__header {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 10px 12px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
  background: color-mix(in srgb, var(--ml-bg, #0d1017) 40%, transparent);
}

.typing-indicator__avatar {
  width: 28px;
  height: 28px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--ti-accent), var(--ml-accent-2, #2dd4bf));
  color: var(--ml-bg, #0d1017);
  font-size: 0.72rem;
  font-weight: 700;
}

.typing-indicator__who {
  display: flex;
  flex-direction: column;
  gap: 1px;
  line-height: 1.2;
}

.typing-indicator__name {
  font-size: 0.78rem;
  font-weight: 650;
}

.typing-indicator__presence {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 0.62rem;
  color: var(--ml-muted, #8b91a3);
}

.typing-indicator__presence::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--ml-accent-2, #2dd4bf);
}

.typing-indicator__replay {
  width: 28px;
  height: 28px;
  margin-left: auto;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--ml-muted, #8b91a3);
  cursor: pointer;
  transition: background 0.18s ease, color 0.18s ease, transform 0.3s cubic-bezier(0.22, 1, 0.36, 1);
}

.typing-indicator__replay:hover {
  background: rgba(255, 255, 255, 0.07);
  color: var(--ml-fg, #e8eaf2);
  transform: rotate(-40deg);
}

.typing-indicator__replay:active { transform: rotate(-180deg) scale(0.92); }

.typing-indicator__replay:focus-visible {
  outline: 2px solid var(--ti-accent);
  outline-offset: 2px;
}

.typing-indicator__replay svg {
  display: block;
  width: 14px;
  height: 14px;
}

/* --- Fil de messages ---------------------------------------------------------- */

.typing-indicator__feed {
  flex: 1;
  min-height: 0;
  padding: 12px 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  overflow-y: auto;
  scrollbar-width: none; /* démo : fil épuré */
}

.typing-indicator__feed::-webkit-scrollbar { display: none; }

.typing-indicator__msg {
  display: flex;
  flex: none;
}

.typing-indicator__msg--them { justify-content: flex-start; }
.typing-indicator__msg--me   { justify-content: flex-end; }

/* --- Bulles --------------------------------------------------------------------
   Spring d'apparition : scale depuis le coin bas côté expéditeur. */

.typing-indicator__bubble {
  position: relative;
  max-width: 82%;
  padding: 7px 12px 8px;
  border-radius: 14px;
  font-size: 0.74rem;
  line-height: 1.45;
  overflow: hidden;
  white-space: nowrap;
}

.typing-indicator__msg--them .typing-indicator__bubble {
  background: var(--ti-them);
  border-bottom-left-radius: 5px;
  transform-origin: left bottom;
}

.typing-indicator__msg--me .typing-indicator__bubble {
  background: color-mix(in srgb, var(--ti-accent) 88%, black);
  color: white;
  border-bottom-right-radius: 5px;
  transform-origin: right bottom;
}

/* Messages du markup initial : pas d'animation (état de repos).
   Ceux ajoutés par le script portent .is-appear. */
.typing-indicator__msg.is-appear .typing-indicator__bubble {
  animation: typing-indicator-bubble-in 0.42s cubic-bezier(0.22, 1.36, 0.36, 1) both;
}

@keyframes typing-indicator-bubble-in {
  from { opacity: 0; transform: scale(0.62) translateY(8px); }
  to   { opacity: 1; transform: scale(1) translateY(0); }
}

/* --- Bulle « en train d'écrire » → message --------------------------------------
   La largeur est pilotée par anim.js (FLIP) : width points → width texte,
   avec un bezier à overshoot pour l'élargissement élastique. */

.typing-indicator__bubble--typing {
  width: 52px; /* état points — élargie vers la largeur du texte par le JS */
  height: 32px;
  transition: width 0.45s cubic-bezier(0.3, 1.25, 0.4, 1);
}

.typing-indicator__dots {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  transition: opacity 0.16s ease, transform 0.2s ease;
}

.typing-indicator__dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--ml-muted, #8b91a3);
  animation: typing-indicator-wave 1.3s cubic-bezier(0.45, 0, 0.4, 1) infinite;
}

.typing-indicator__dot:nth-child(2) { animation-delay: 0.16s; }
.typing-indicator__dot:nth-child(3) { animation-delay: 0.32s; }

/* Vague amortie : montée franche puis retombée avec rebond qui s'éteint —
   pas un blink d'opacité. */
@keyframes typing-indicator-wave {
  0%   { transform: translateY(0);      opacity: 0.55; }
  18%  { transform: translateY(-5px);   opacity: 1; }
  34%  { transform: translateY(1.6px);  opacity: 0.85; }
  46%  { transform: translateY(-0.7px); opacity: 0.7; }
  56%  { transform: translateY(0);      opacity: 0.6; }
  100% { transform: translateY(0);      opacity: 0.55; }
}

.typing-indicator__text {
  opacity: 0;
  transition: opacity 0.24s ease 0.08s;
}

/* Morph : crossfade points → texte */
.typing-indicator__bubble--typing.is-message .typing-indicator__dots {
  opacity: 0;
  transform: scale(0.6);
}

.typing-indicator__bubble--typing.is-message .typing-indicator__dot {
  animation-play-state: paused;
}

.typing-indicator__bubble--typing.is-message .typing-indicator__text {
  opacity: 1;
}

/* Fin de boucle : le fil s'efface en douceur avant de rejouer */
.typing-indicator__feed.is-resetting .typing-indicator__msg {
  opacity: 0;
  transform: translateY(6px);
  transition: opacity 0.28s ease, transform 0.28s ease;
}

/* --- Reduced motion --------------------------------------------------------------- */

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

  .typing-indicator__replay:hover,
  .typing-indicator__replay:active {
    transform: none;
  }

  .typing-indicator__msg.is-appear .typing-indicator__bubble {
    animation-duration: 0.01s;
  }

  .typing-indicator__bubble--typing {
    transition-duration: 0.01s;
  }

  .typing-indicator__dot {
    animation: typing-indicator-fade 1.3s ease infinite;
  }

  /* fallback sans mouvement : pulsation d'opacité douce */
  @keyframes typing-indicator-fade {
    0%, 100% { opacity: 0.4; }
    30%      { opacity: 1; }
  }

  .typing-indicator__dots,
  .typing-indicator__text {
    transition-duration: 0.01s;
    transition-delay: 0s;
  }

  .typing-indicator__feed.is-resetting .typing-indicator__msg {
    transition-duration: 0.01s;
  }
}
