/* Voice mode + avatar (plans/03-voice-mode-avatar.md, phase 1).
   Collapsed: portrait avatar docked bottom-right of the chat interface,
   rendered translucent so the conversation stays slightly visible under it.
   Expanded: avatar-only view over a translucent, blurred underlay — the chat
   remains faintly visible beneath (owner request 2026-08-16).
   The portrait is derived from the owner's own generated art (female matches
   the Aura voice; male counterpart at /img/avatar-male.jpg for later).
   States map to the voice state machine via the ring + badge: off =
   desaturated + slash badge · listening = blue ring ripple · thinking =
   purple ring · speaking = green ring, portrait pulses with --voice-level.
   Reactive animation is transform/opacity-only so it stays smooth. */

#voice-dock {
  position: fixed;
  right: 18px;
  bottom: 96px; /* clears the composer */
  z-index: 60;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

#voice-orb,
#voice-orb-big {
  --voice-level: 0;
  position: relative;
  border: none;
  padding: 0;
  background: transparent;
  cursor: pointer;
  width: 96px;
  height: 96px;
}

/* Interaction-reactive size: grow while the conversation is active, swell
   with her voice while speaking, settle back when off. Anchored bottom-right
   so growth reaches inward, never off-screen. Transform-only (cheap). */
#voice-orb {
  transform-origin: bottom right;
  transition: transform 0.3s ease;
}
#voice-orb.voice-listening,
#voice-orb.voice-bridging,
#voice-orb.voice-thinking {
  transform: scale(1.15);
}
#voice-orb.voice-speaking {
  transform: scale(calc(1.18 + var(--voice-level) * 0.14));
  transition: transform 0.07s linear;
}
#voice-orb-big {
  width: 220px;
  height: 220px;
}

/* Motion wrapper (plan 04 B1): procedural head movement — sway while
   speaking, nod while bridging, tilt while thinking, perk when the owner
   starts talking — lives on this span so it composes with the img's own
   level-driven scale pulse. Transform-only, like everything else here. */
.voice-head {
  position: absolute;
  inset: 0;
  transform-origin: 50% 78%; /* pivot near the neck, not the image center */
  transition: transform 0.35s ease;
}
.voice-speaking .voice-head {
  animation: head-sway 5.4s ease-in-out infinite;
}
.voice-thinking .voice-head { transform: rotate(2.2deg); }
.voice-bridging .voice-head {
  animation: head-nod 1.9s ease-in-out infinite;
}
/* attention perk: she visibly reacts the instant the owner starts talking */
.voice-perk .voice-head {
  transform: scale(1.05);
  transition: transform 0.12s ease-out;
}
.voice-perk .orb-glow { opacity: 1; }
@keyframes head-sway {
  0%, 100% { transform: rotate(-1.1deg) translateX(-1%); }
  50% { transform: rotate(1.1deg) translateX(1%); }
}
@keyframes head-nod {
  0%, 100% { transform: translateY(0) rotate(0.6deg); }
  50% { transform: translateY(1.5%) rotate(-0.6deg); }
}

.voice-avatar-img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  object-fit: cover;
  /* translucent underlay: the chat interface shows through slightly */
  opacity: 0.82;
  box-shadow: 0 6px 18px rgba(15, 23, 42, 0.45);
  transition: opacity 0.2s ease, filter 0.25s ease, transform 0.08s linear;
  user-select: none;
  -webkit-user-select: none;
}
#voice-orb:hover .voice-avatar-img { opacity: 1; }
/* speaking: portrait pulses gently with the live audio level */
.voice-speaking .voice-avatar-img {
  opacity: 0.95;
  transform: scale(calc(1 + var(--voice-level) * 0.05));
}
.voice-listening .voice-avatar-img { opacity: 0.9; }
.voice-off .voice-avatar-img { filter: grayscale(0.85) brightness(0.9); opacity: 0.7; }
#voice-orb-big .voice-avatar-img { opacity: 1; }
#voice-orb-big.voice-off .voice-avatar-img { opacity: 0.8; }

/* State ring around the portrait */
.orb-ring {
  position: absolute;
  inset: -3px;
  border-radius: 50%;
  border: 3px solid transparent;
  pointer-events: none;
  transition: border-color 0.25s ease;
}

.voice-listening .orb-ring {
  border-color: #3b82f6;
  animation: ring-breathe 2.6s ease-in-out infinite;
}
/* speech-energy ripple: a second ring drawn via ::after, transform-only */
.voice-listening .orb-ring::after {
  content: '';
  position: absolute;
  inset: -2px;
  border-radius: 50%;
  border: 2px solid rgba(59, 130, 246, 0.55);
  transform: scale(calc(1 + var(--voice-level) * 0.16));
  opacity: calc(0.2 + var(--voice-level) * 0.8);
}

.voice-thinking .orb-ring {
  border-color: #8b5cf6;
  animation: ring-breathe 1.1s ease-in-out infinite;
}
/* bridging (plan 04): Layer-1 buffer acknowledged the utterance and is
   speaking the bridge line while the heavy pipeline runs — teal, between
   listening-blue and thinking-purple in urgency */
.voice-bridging .orb-ring {
  border-color: #14b8a6;
  animation: ring-breathe 1.6s ease-in-out infinite;
}
.voice-speaking .orb-ring { border-color: #10b981; }

@keyframes ring-breathe {
  0%, 100% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.05); opacity: 0.75; }
}

/* ---- Glow / attention effect (state-tinted halo behind the portrait) ----
   transform/opacity-only animation; the gradient itself never repaints. */
.orb-glow {
  position: absolute;
  inset: -14%;
  border-radius: 50%;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.35s ease;
}
.voice-listening .orb-glow {
  background: radial-gradient(circle, rgba(59, 130, 246, 0.55), rgba(59, 130, 246, 0) 70%);
  animation: glow-breathe 4.5s ease-in-out infinite;
}
.voice-thinking .orb-glow {
  background: radial-gradient(circle, rgba(139, 92, 246, 0.6), rgba(139, 92, 246, 0) 70%);
  animation: glow-breathe 1.4s ease-in-out infinite;
}
.voice-bridging .orb-glow {
  background: radial-gradient(circle, rgba(20, 184, 166, 0.55), rgba(20, 184, 166, 0) 70%);
  animation: glow-breathe 1.8s ease-in-out infinite;
}
.voice-speaking .orb-glow {
  background: radial-gradient(circle, rgba(16, 185, 129, 0.6), rgba(16, 185, 129, 0) 70%);
  opacity: calc(0.5 + var(--voice-level) * 0.5);
  transform: scale(calc(1 + var(--voice-level) * 0.15));
  transition: transform 0.07s linear;
}
@keyframes glow-breathe {
  0%, 100% { transform: scale(1); opacity: 0.6; }
  50% { transform: scale(1.08); opacity: 1; }
}

/* Idle breathing: the portrait gently swells while she listens — alive,
   not frozen. Speaking's own level-driven transform takes over seamlessly. */
.voice-listening .voice-avatar-img {
  animation: avatar-breathe 4.5s ease-in-out infinite;
}
@keyframes avatar-breathe {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.025); }
}

/* ---- On/off badge (the start/stop affordance) ---- */
.voice-badge {
  position: absolute;
  right: 4px;
  bottom: 4px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #64748b;
  border: 2px solid #0f172a;
  pointer-events: none;
}
#voice-orb-big .voice-badge { width: 40px; height: 40px; border-width: 3px; }
/* off: grey with a slash */
.voice-off .voice-badge::after {
  content: '';
  position: absolute;
  left: 50%;
  top: 50%;
  width: 70%;
  height: 2px;
  background: #e2e8f0;
  transform: translate(-50%, -50%) rotate(-45deg);
}
/* on (any active state): green "live" dot, gently pulsing */
.voice-listening .voice-badge,
.voice-bridging .voice-badge,
.voice-thinking .voice-badge,
.voice-speaking .voice-badge {
  background: #10b981;
  animation: badge-pulse 2s ease-in-out infinite;
}
@keyframes badge-pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.15); }
}
/* transient failure flash (STT/TTS error) — overrides the state color */
.voice-error .voice-badge {
  background: #ef4444 !important;
  animation: none;
}
.voice-error .orb-ring { border-color: #ef4444 !important; }

#voice-expand {
  border: none;
  background: rgba(30, 41, 59, 0.85);
  color: #cbd5e1;
  border-radius: 10px;
  font-size: 13px;
  line-height: 1;
  padding: 4px 8px;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.15s ease;
}
#voice-dock:hover #voice-expand,
#voice-dock.voice-on #voice-expand {
  opacity: 1;
}
#voice-dock.voice-loading .voice-avatar-img { filter: grayscale(0.4) brightness(1.15); }

/* ---- Expanded conversation view ----
   Founder redesign 2026-08-18: full-body Maya standing in the bottom-right
   corner over a transparent blurred underlay — the chat stays visible
   beneath. Docked (collapsed) state is unchanged. Size tunes in one place
   via --voice-expanded-h. */
#voice-overlay {
  --voice-expanded-h: min(44vh, 360px);
  position: fixed;
  inset: 0;
  z-index: 70;
  background: rgba(2, 6, 23, 0.55);
  -webkit-backdrop-filter: blur(5px);
  backdrop-filter: blur(5px);
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  justify-content: flex-end;
  padding: 28px;
}
#voice-overlay[hidden] { display: none; }

.voice-stage {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 12px;
  max-width: 520px;
  text-align: right;
}
/* transcript reads above the standing avatar */
#voice-transcript { order: -1; }

/* Full-body avatar: portrait aspect, rounded-rect ring instead of circle */
#voice-orb-big {
  width: auto !important;
  height: var(--voice-expanded-h) !important;
  aspect-ratio: 252 / 470;
}
#voice-orb-big .voice-avatar-img,
#voice-orb-big .voice-head { border-radius: 14px; }

/* Plan 05 phase A: live 2.5D engine canvas replaces the static full-body
   img when avatar-engine.js initializes (WebGL + CDN available). The img
   stays in the DOM as fallback and is hidden only on success. */
.voice-avatar-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: none;
  border-radius: 14px;
  user-select: none;
  -webkit-user-select: none;
}
.voice-head.engine-on .voice-avatar-canvas { display: block; }
.voice-head.engine-on .voice-avatar-img { visibility: hidden; }
/* mirror the img's state treatments */
#voice-orb-big.voice-off .voice-avatar-canvas { filter: grayscale(0.85) brightness(0.9); opacity: 0.8; }
#voice-orb-big .voice-avatar-canvas { opacity: 1; transition: opacity 0.2s ease, filter 0.25s ease; }
#voice-orb-big .orb-ring { border-radius: 17px; }
#voice-orb-big .orb-ring::after { border-radius: 19px; }
#voice-orb-big .orb-glow { border-radius: 22%; inset: -6%; }

#voice-state-label {
  color: #94a3b8;
  font-size: 14px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

#voice-hint {
  color: #64748b;
  font-size: 13px;
}

#voice-mode-toggle {
  display: inline-flex;
  border: 1px solid #334155;
  border-radius: 10px;
  overflow: hidden;
}
#voice-mode-toggle button {
  border: none;
  background: transparent;
  color: #94a3b8;
  font-size: 13px;
  padding: 7px 14px;
  cursor: pointer;
}
#voice-mode-toggle button.active {
  background: #1e40af;
  color: #e2e8f0;
}

#voice-transcript { min-height: 84px; }
.voice-heard {
  color: #cbd5e1;
  font-size: 15px;
  margin: 0 0 10px;
}
.voice-spoken {
  color: #f1f5f9;
  font-size: 18px;
  line-height: 1.5;
  margin: 0;
  text-shadow: 0 1px 6px rgba(2, 6, 23, 0.8);
}

#voice-pin {
  position: absolute;
  top: 18px;
  right: 18px;
  border: 1px solid #334155;
  background: rgba(30, 41, 59, 0.7);
  color: #cbd5e1;
  border-radius: 10px;
  padding: 8px 14px;
  font-size: 13px;
  cursor: pointer;
}
#voice-pin:hover { background: rgba(51, 65, 85, 0.9); }

@media (max-width: 640px) {
  #voice-dock { right: 12px; bottom: 88px; }
  #voice-orb { width: 72px; height: 72px; }
  #voice-overlay { --voice-expanded-h: min(38vh, 280px); padding: 16px; }
}
