/* ============================================================
   AVATAR PANEL — Teatro dell'Anima
   Full-screen theatrical experience
   ============================================================ */

/* ── Bottone Anima (nelle card) ── */
#avatar-anima-btn {
  display: none;
  align-items: center;
  gap: 8px;
  margin: 12px auto 0;
  padding: 10px 22px;
  background: transparent;
  border: 1px solid var(--gold, #c9a84c);
  color: var(--gold, #c9a84c);
  font-family: var(--font-nav, serif);
  font-size: 0.78rem;
  letter-spacing: 0.15em;
  text-transform: uppercase;
  cursor: pointer;
  transition: background 0.25s, color 0.25s;
}
#avatar-anima-btn:hover {
  background: var(--gold, #c9a84c);
  color: #1a0a00;
}

/* ── Spinner Hedra ── */
#avatar-generating {
  display: none;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  margin: 14px auto 0;
  color: rgba(255,255,255,0.6);
  font-size: 0.75rem;
  letter-spacing: 0.1em;
}
.hedra-spinner {
  width: 28px; height: 28px;
  border: 2px solid rgba(201,168,76,0.25);
  border-top-color: #c9a84c;
  border-radius: 50%;
  animation: hedra-spin 0.9s linear infinite;
}
@keyframes hedra-spin { to { transform: rotate(360deg); } }

/* ── Pannello principale ── */
#avatar-panel {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 2000;
  background: #000;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  overflow: hidden;
}
#avatar-panel.open { display: flex; }

/* ── Sfondo full-screen ── */
#avatar-bg {
  position: absolute;
  inset: 0;
  z-index: 0;
}
#avatar-bg-img {
  width: 100%; height: 100%;
  object-fit: cover;
  object-position: center top;
  opacity: 0;
  transition: opacity 1.8s ease;
}
#avatar-bg-img.visible { opacity: 1; }
#avatar-bg-overlay {
  position: absolute;
  inset: 0;
  background: linear-gradient(
    to bottom,
    rgba(0,0,0,0.2) 0%,
    rgba(0,0,0,0.5) 60%,
    rgba(0,0,0,0.85) 100%
  );
}

/* ── Chiudi ── */
#avatar-close {
  position: absolute;
  top: calc(16px + env(safe-area-inset-top, 0px)); right: 16px;
  z-index: 10;
  background: rgba(0,0,0,0.5);
  border: 1px solid rgba(201,168,76,0.4);
  color: #c9a84c;
  width: 36px; height: 36px;
  border-radius: 50%;
  font-size: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s;
}
#avatar-close:hover { background: rgba(201,168,76,0.2); }

/* ── Indicatore stadio ── */
#avatar-stage-indicator {
  position: absolute;
  top: calc(20px + env(safe-area-inset-top, 0px)); left: 50%;
  transform: translateX(-50%);
  z-index: 10;
  display: flex;
  gap: 8px;
}
.stage-dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: rgba(255,255,255,0.2);
  transition: background 0.5s;
}
.stage-dot.active { background: #c9a84c; }

/* ── Curtain (attesa Atto I) ── */
#avatar-curtain {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  opacity: 1;
  transition: opacity 0.8s ease;
}
#avatar-curtain.hidden { opacity: 0; pointer-events: none; }
#avatar-curtain-name {
  font-family: 'Georgia', serif;
  font-size: clamp(1.4rem, 4vw, 2.2rem);
  color: #c9a84c;
  letter-spacing: 0.08em;
  text-align: center;
}
.curtain-sub {
  font-family: 'Georgia', serif;
  font-style: italic;
  font-size: 0.9rem;
  color: rgba(255,255,255,0.5);
  letter-spacing: 0.1em;
}
.curtain-dots {
  display: flex;
  gap: 6px;
  margin-top: 8px;
}
.curtain-dots span {
  width: 6px; height: 6px;
  border-radius: 50%;
  background: rgba(201,168,76,0.6);
  animation: curtainBlink 1.4s infinite;
  opacity: 0;
}
.curtain-dots span:nth-child(2) { animation-delay: 0.3s; }
.curtain-dots span:nth-child(3) { animation-delay: 0.6s; }
@keyframes curtainBlink {
  0%, 80%, 100% { opacity: 0; }
  40% { opacity: 1; }
}

/* ── Frase congedo (Atto III) ── */
#avatar-farewell {
  position: absolute;
  inset: 0;
  z-index: 6;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 40px;
  font-family: 'Georgia', serif;
  font-size: clamp(1.1rem, 3vw, 1.6rem);
  color: #c9a84c;
  text-align: center;
  line-height: 1.6;
  opacity: 0;
  transition: opacity 1.2s ease;
}
#avatar-farewell.visible {
  display: flex;
  opacity: 1;
}

/* ── Layout dialogo ── */
#avatar-dialogue {
  position: relative;
  z-index: 5;
  width: 100%;
  max-width: 700px;
  padding: 0 16px 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  opacity: 0;
  transition: opacity 0.6s ease;
}
#avatar-dialogue.visible { opacity: 1; }

/* ── Ritratto circolare ── */
@keyframes portraitGlow {
  0%, 100% { box-shadow: 0 0 8px rgba(201,168,76,0.3), 0 0 0 2px rgba(201,168,76,0.2); }
  50%       { box-shadow: 0 0 22px rgba(201,168,76,0.7), 0 0 0 4px rgba(201,168,76,0.5); }
}
@keyframes portraitGlowSpeak {
  0%, 100% { box-shadow: 0 0 25px rgba(201,168,76,0.8), 0 0 0 5px rgba(201,168,76,0.6); }
  50%       { box-shadow: 0 0 55px rgba(201,168,76,1),   0 0 0 9px rgba(201,168,76,0.9); }
}
@keyframes imgBreath {
  0%, 100% { filter: brightness(1.0) saturate(0.95); }
  50%       { filter: brightness(1.08) saturate(1.05); }
}
@keyframes imgSpeak {
  0%   { filter: brightness(1.0); }
  25%  { filter: brightness(1.18); }
  50%  { filter: brightness(1.06); }
  75%  { filter: brightness(1.20); }
  100% { filter: brightness(1.0); }
}

#avatar-portrait-wrap {
  width: 130px; height: 130px;
  border-radius: 50%;
  overflow: hidden;
  border: 2px solid rgba(201,168,76,0.85);
  background: #111;
  position: relative;
  margin-bottom: 10px;
  flex-shrink: 0;
  animation: portraitGlow 3s ease-in-out infinite;
  transition: border-color 0.3s;
}
#avatar-portrait-wrap.speaking {
  animation: portraitGlowSpeak 0.6s ease-in-out infinite;
  border-color: #f0d070;
}
#avatar-portrait {
  width: 100%; height: 100%;
  object-fit: cover;
  object-position: center center;
  display: block;
  animation: imgBreath 4s ease-in-out infinite;
}
#avatar-portrait-wrap.speaking #avatar-portrait {
  animation: imgSpeak 0.55s ease-in-out infinite;
}
#avatar-video {
  position: absolute;
  inset: 0;
  width: 100%; height: 100%;
  object-fit: cover;
  display: none;
  animation: imgBreath 4s ease-in-out infinite;
}
#avatar-portrait-wrap.speaking #avatar-video {
  animation: imgSpeak 0.55s ease-in-out infinite;
}
#avatar-placeholder {
  width: 100%; height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.4rem;
  color: #c9a84c;
  font-family: 'Georgia', serif;
}

/* ── Nome personaggio ── */
#avatar-char-name {
  font-family: var(--font-nav, 'Cormorant Garamond', Georgia, serif);
  font-size: clamp(1.1rem, 3vw, 1.9rem);
  letter-spacing: 0.25em;
  color: #c9a84c;
  text-transform: uppercase;
  text-align: center;
  margin-bottom: 16px;
  order: -1;          /* appare sopra il tondo */
  align-self: center;
}

/* ── Waveform ── */
#avatar-waveform {
  display: none;
  align-items: flex-end;
  gap: 3px;
  height: 20px;
  margin-bottom: 8px;
}
#avatar-waveform.active { display: flex; }
.av-wave-bar {
  width: 3px;
  background: #c9a84c;
  border-radius: 2px;
  animation: waveAnim 0.8s ease-in-out infinite;
}
.av-wave-bar:nth-child(1) { animation-delay: 0.0s; height: 8px; }
.av-wave-bar:nth-child(2) { animation-delay: 0.1s; height: 14px; }
.av-wave-bar:nth-child(3) { animation-delay: 0.2s; height: 20px; }
.av-wave-bar:nth-child(4) { animation-delay: 0.1s; height: 14px; }
.av-wave-bar:nth-child(5) { animation-delay: 0.0s; height: 8px; }
@keyframes waveAnim {
  0%, 100% { transform: scaleY(0.4); opacity: 0.5; }
  50%       { transform: scaleY(1.0); opacity: 1.0; }
}

/* ── Bubble wrap ── */
#avatar-bubble-wrap {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

/* ── Status ── */
#avatar-status {
  font-family: var(--font-nav, 'Cormorant Garamond', serif);
  color: rgba(201,168,76,0.6);
  font-size: 0.7rem;
  letter-spacing: 0.15em;
  text-transform: uppercase;
  text-align: center;
  min-height: 16px;
}

/* ── Bolla risposta ── */
#avatar-bubble {
  background: rgba(0,0,0,0.75);
  border: 1px solid rgba(201,168,76,0.3);
  border-radius: 16px 16px 16px 4px;
  padding: 14px 18px;
  color: #fff;
  font-family: 'Georgia', serif;
  font-size: clamp(0.88rem, 2.2vw, 1.05rem);
  line-height: 1.65;
  min-height: 48px;
  backdrop-filter: blur(8px);
}
#avatar-bubble:empty { display: none; }

/* ── Testo utente ── */
#avatar-user-text {
  display: none;
  align-self: flex-end;
  background: rgba(201,168,76,0.12);
  border: 1px solid rgba(201,168,76,0.35);
  border-radius: 16px 16px 4px 16px;
  padding: 10px 16px;
  color: #c9a84c;
  font-family: 'Georgia', serif;
  font-style: italic;
  font-size: 0.9rem;
  max-width: 85%;
}

/* ── Controlli ── */
#avatar-controls {
  position: relative;
  z-index: 5;
  width: 100%;
  max-width: 700px;
  padding: 8px 16px 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
}

/* ── Mic ── */
#avatar-mic-btn {
  width: 62px; height: 62px;
  border-radius: 50%;
  background: #c9a84c;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
  display: none;
  align-items: center;
  justify-content: center;
  transition: transform 0.15s, background 0.2s;
  box-shadow: 0 0 20px rgba(201,168,76,0.35);
}
#avatar-mic-btn:hover { transform: scale(1.06); }
#avatar-mic-btn.listening {
  background: #e05555;
  animation: micPulse 1s ease-in-out infinite;
}
#avatar-mic-btn.disabled {
  background: #444;
  cursor: not-allowed;
  box-shadow: none;
}
@keyframes micPulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(224,85,85,0.5); }
  50%       { box-shadow: 0 0 0 12px rgba(224,85,85,0); }
}

/* ── Fallback testuale ── */
#avatar-text-fallback {
  display: none;
  gap: 8px;
  width: 100%;
  max-width: 420px;
}
#avatar-text-input {
  flex: 1;
  padding: 12px 16px;
  border-radius: 24px;
  border: 1px solid rgba(201,168,76,0.4);
  background: rgba(0,0,0,0.6);
  color: #fff;
  font-family: 'Georgia', serif;
  font-size: 0.95rem;
  outline: none;
}
#avatar-text-input::placeholder { color: rgba(255,255,255,0.35); }
#avatar-text-send {
  padding: 12px 22px;
  border-radius: 24px;
  background: #c9a84c;
  border: none;
  color: #000;
  font-family: var(--font-nav, serif);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.2s;
}
#avatar-text-send:hover { opacity: 0.85; }

/* ── Footer ── */
#avatar-footer-note {
  position: relative;
  z-index: 5;
  font-size: 0.65rem;
  color: rgba(255,255,255,0.2);
  letter-spacing: 0.08em;
  padding-bottom: 8px;
  text-align: center;
}

/* ── Animazioni bozzetto (legacy - mantenute per compatibilità) ── */
#avatar-img-wrapper.idle  { animation: avatarIdle  3s ease-in-out infinite; }
#avatar-img-wrapper.speak { animation: avatarSpeak 0.9s ease-in-out infinite; }
@keyframes avatarIdle {
  0%, 100% { box-shadow: 0 0 6px rgba(201,168,76,0.15); }
  50%       { box-shadow: 0 0 14px rgba(201,168,76,0.35); }
}
@keyframes avatarSpeak {
  0%   { transform: scale(1.0);   box-shadow: 0 0 8px rgba(201,168,76,0.3); }
  50%  { transform: scale(1.03); box-shadow: 0 0 24px rgba(201,168,76,0.7); }
  100% { transform: scale(1.0);   box-shadow: 0 0 8px rgba(201,168,76,0.3); }
}

/* ── Responsive mobile ── */
@media (max-width: 480px) {
  #avatar-portrait-wrap { width: 160px; height: 160px; }
  #avatar-bubble { font-size: 0.85rem; padding: 11px 14px; }
  #avatar-mic-btn { width: 54px; height: 54px; font-size: 1.3rem; }
  #avatar-text-input { font-size: 1rem; }
}

/* ── Desktop: bozzetto a destra, dialogo a sinistra ── */
@media (min-width: 601px) {
  /* Pannello: sfondo scuro, contenuto allineato a sinistra */
  #avatar-panel {
    background: #080408;
    align-items: flex-start;
  }

  /* Bozzetto fisso nella metà DESTRA — artwork chiaro, non sfocato */
  #avatar-bg {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: auto;
    width: 46%;
    height: 100%;
  }
  #avatar-bg-img {
    filter: sepia(6%) contrast(1.08);
    opacity: 0;
    transition: opacity 1.4s ease;
  }
  #avatar-bg-overlay {
    background: linear-gradient(
      to left,
      rgba(0,0,0,0.05) 0%,
      rgba(0,0,0,0.25) 65%,
      rgba(8,4,8,1.0) 100%
    );
  }

  /* Stage indicator sulla sinistra */
  #avatar-stage-indicator {
    left: 3rem;
    transform: none;
  }

  /* Curtain e farewell nella metà SINISTRA */
  #avatar-curtain {
    left: 0;
    right: 46%;
    top: 0; bottom: 0;
    width: auto;
  }
  #avatar-farewell {
    left: 0;
    right: 46%;
    top: 0; bottom: 0;
    width: auto;
  }

  /* Dialogo e controlli a sinistra — centrati nel pannello */
  #avatar-dialogue {
    max-width: none;
    width: 54%;
    padding: 0 2rem 16px 3.5rem;
    align-items: center;
  }
  #avatar-controls {
    max-width: none;
    width: 54%;
    padding: 8px 2rem 20px 3.5rem;
    justify-content: center;
  }
  #avatar-footer-note {
    width: 54%;
    padding-left: 3.5rem;
    text-align: left;
  }

  /* Ritratto più grande su desktop */
  #avatar-portrait-wrap {
    width: 200px; height: 200px;
  }

  /* Bolla testo allineata a sinistra */
  #avatar-bubble {
    border-radius: 4px 16px 16px 16px;
  }
  #avatar-user-text {
    align-self: flex-start;
    border-radius: 4px 16px 16px 16px;
  }
}
