/* ============================================================================
   LIVING AVATAR - Pixar-Inspired Presence System
   ============================================================================
   Creates a deeply human connection through:
   - Breathing animation (squash & stretch)
   - WALL-E style curious eye tracking
   - Emotion-responsive waveform expressions
   - Ambient glow and warmth
   - Secondary actions (heartbeat, attention)
   ============================================================================ */

/* ============================================================================
   CSS CUSTOM PROPERTIES - Animation Tokens
   ============================================================================ */

:root {
  /* Timing - Fibonacci-based for natural rhythm */
  --avatar-breath-duration: 4000ms;
  --avatar-breath-duration-active: 2800ms;
  --avatar-reaction-fast: 200ms;
  --avatar-reaction-normal: 400ms;
  --avatar-reaction-slow: 800ms;
  
  /* Easing - Pixar-quality curves */
  --ease-spring: cubic-bezier(0.34, 1.56, 0.64, 1);
  --ease-spring-gentle: cubic-bezier(0.25, 1.25, 0.5, 1);
  --ease-organic: cubic-bezier(0.4, 0.2, 0.2, 1.1);
  --ease-anticipate: cubic-bezier(0.38, -0.4, 0.88, 0.65);
  --ease-gentle: cubic-bezier(0.25, 0.1, 0.25, 1);
  
  /* Avatar Colors - Using design tokens */
  --avatar-primary: var(--color-ferni, #4a6741);
  --avatar-secondary: var(--color-ferni-secondary, #3d5a35);
  --avatar-glow: var(--color-ferni-glow, rgba(74, 103, 65, 0.28));
  --avatar-glow-strong: rgba(74, 103, 65, 0.45);
  --avatar-inner-light: rgba(255, 255, 255, 0.15);
  
  /* Eye tracking range */
  --eye-offset-x: 0px;
  --eye-offset-y: 0px;
  --eye-track-strength: 4px;
}

/* ============================================================================
   AVATAR OVERLAY CONTAINER
   ============================================================================ */

.living-avatar-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  pointer-events: none;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.8s var(--ease-gentle), visibility 0.8s;
}

.living-avatar-overlay.active {
  opacity: 1;
  visibility: visible;
}

.living-avatar-overlay.fade-out {
  opacity: 0;
  transition-duration: 1.2s;
}

/* Dark backdrop with vignette */
.avatar-backdrop {
  position: absolute;
  inset: 0;
  background: radial-gradient(
    ellipse 100% 100% at 50% 50%,
    rgba(245, 241, 232, 0.85) 0%,
    rgba(245, 241, 232, 0.95) 40%,
    rgba(232, 224, 213, 0.98) 100%
  );
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}

/* ============================================================================
   AMBIENT PARTICLES - Floating dust motes
   ============================================================================ */

.avatar-particles {
  position: absolute;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
}

.ambient-particle {
  position: absolute;
  width: 4px;
  height: 4px;
  background: var(--avatar-primary);
  border-radius: 50%;
  opacity: 0;
  animation: particleFloat 8s ease-in-out infinite;
}

@keyframes particleFloat {
  0% {
    opacity: 0;
    transform: translateY(100vh) scale(0.5);
  }
  10% {
    opacity: 0.4;
  }
  50% {
    opacity: 0.6;
  }
  90% {
    opacity: 0.3;
  }
  100% {
    opacity: 0;
    transform: translateY(-100px) scale(1.2);
  }
}

/* Stagger particles */
.ambient-particle:nth-child(1) { left: 10%; animation-delay: 0s; animation-duration: 12s; }
.ambient-particle:nth-child(2) { left: 25%; animation-delay: 2s; animation-duration: 10s; }
.ambient-particle:nth-child(3) { left: 40%; animation-delay: 4s; animation-duration: 14s; }
.ambient-particle:nth-child(4) { left: 55%; animation-delay: 1s; animation-duration: 11s; }
.ambient-particle:nth-child(5) { left: 70%; animation-delay: 3s; animation-duration: 9s; }
.ambient-particle:nth-child(6) { left: 85%; animation-delay: 5s; animation-duration: 13s; }
.ambient-particle:nth-child(7) { left: 15%; animation-delay: 6s; animation-duration: 15s; }
.ambient-particle:nth-child(8) { left: 60%; animation-delay: 7s; animation-duration: 10s; }

/* ============================================================================
   AURORA GLOW - Soft ambient light
   ============================================================================ */

.avatar-aurora {
  position: absolute;
  width: 800px;
  height: 800px;
  border-radius: 50%;
  background: radial-gradient(
    ellipse at center,
    var(--avatar-glow) 0%,
    rgba(74, 103, 65, 0.1) 30%,
    transparent 60%
  );
  filter: blur(60px);
  animation: auroraBreath 6s ease-in-out infinite;
  transform-origin: center;
}

@keyframes auroraBreath {
  0%, 100% {
    transform: scale(1) rotate(0deg);
    opacity: 0.6;
  }
  33% {
    transform: scale(1.1) rotate(2deg);
    opacity: 0.8;
  }
  66% {
    transform: scale(0.95) rotate(-1deg);
    opacity: 0.5;
  }
}

/* ============================================================================
   MAIN AVATAR CONTAINER - Breathing wrapper
   ============================================================================ */

.living-avatar {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 24px;
  pointer-events: auto;
  cursor: default;
  transform-origin: center bottom;
  
  /* WALL-E eye tracking offset - updated via JS */
  transform: translate(var(--eye-offset-x), var(--eye-offset-y));
  transition: transform 0.15s var(--ease-gentle);
}

/* Pixar breathing - applied via JS for proper squash & stretch */
.living-avatar.breathing {
  animation: avatarBreath var(--avatar-breath-duration) ease-in-out infinite;
}

@keyframes avatarBreath {
  /* Rest */
  0% {
    transform: 
      translate(var(--eye-offset-x), var(--eye-offset-y))
      scale3d(1, 1, 1);
  }
  /* Anticipation - slight squash */
  8% {
    transform: 
      translate(var(--eye-offset-x), calc(var(--eye-offset-y) + 1px))
      scale3d(1.003, 0.997, 1);
  }
  /* Inhale - stretch up */
  35% {
    transform: 
      translate(var(--eye-offset-x), calc(var(--eye-offset-y) - 3px))
      scale3d(0.995, 1.012, 1);
  }
  /* Follow-through */
  42% {
    transform: 
      translate(var(--eye-offset-x), calc(var(--eye-offset-y) - 4px))
      scale3d(0.994, 1.015, 1);
  }
  /* Exhale slow */
  70% {
    transform: 
      translate(var(--eye-offset-x), calc(var(--eye-offset-y) - 1px))
      scale3d(0.998, 1.005, 1);
  }
  /* Settle */
  92% {
    transform: 
      translate(var(--eye-offset-x), calc(var(--eye-offset-y) + 0.5px))
      scale3d(1.001, 0.999, 1);
  }
  100% {
    transform: 
      translate(var(--eye-offset-x), var(--eye-offset-y))
      scale3d(1, 1, 1);
  }
}

/* ============================================================================
   AVATAR ORB - The main visual element
   ============================================================================ */

.avatar-orb-wrapper {
  position: relative;
  width: 200px;
  height: 200px;
}

/* Outer glow rings - pulsing presence */
.avatar-glow-ring {
  position: absolute;
  inset: -20px;
  border-radius: 50%;
  border: 1px solid var(--avatar-glow);
  animation: glowRingPulse 4s ease-in-out infinite;
}

.avatar-glow-ring:nth-child(2) {
  inset: -40px;
  animation-delay: 0.5s;
  opacity: 0.7;
}

.avatar-glow-ring:nth-child(3) {
  inset: -60px;
  animation-delay: 1s;
  opacity: 0.4;
}

@keyframes glowRingPulse {
  0%, 100% { 
    transform: scale(1);
    opacity: var(--ring-opacity, 0.5);
    border-color: var(--avatar-glow);
  }
  50% { 
    transform: scale(1.05);
    opacity: calc(var(--ring-opacity, 0.5) * 1.4);
    border-color: var(--avatar-glow-strong);
  }
}

/* The orb itself */
.avatar-orb {
  position: relative;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background: linear-gradient(145deg, var(--avatar-secondary) 0%, var(--avatar-primary) 50%, #5a7a4d 100%);
  background-size: 200% 200%;
  background-position: 50% 50%;
  box-shadow: 
    0 0 80px var(--avatar-glow),
    0 0 120px rgba(74, 103, 65, 0.15),
    inset 0 -20px 40px rgba(0, 0, 0, 0.15),
    inset 0 20px 40px var(--avatar-inner-light);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  transition: 
    box-shadow 0.6s var(--ease-gentle),
    background-position 1s var(--ease-organic);
}

/* Inner highlight for depth */
.avatar-orb::before {
  content: '';
  position: absolute;
  top: 10%;
  left: 15%;
  width: 30%;
  height: 25%;
  background: radial-gradient(
    ellipse at center,
    rgba(255, 255, 255, 0.35) 0%,
    transparent 70%
  );
  border-radius: 50%;
  filter: blur(8px);
}

/* Active state - more glow, warmer */
.living-avatar.active .avatar-orb {
  background-position: 0% 0%;
  box-shadow: 
    0 0 100px var(--avatar-glow-strong),
    0 0 160px rgba(74, 103, 65, 0.25),
    inset 0 -20px 40px rgba(0, 0, 0, 0.1),
    inset 0 20px 50px var(--avatar-inner-light);
}

/* ============================================================================
   WAVEFORM - Emotion-responsive bars
   ============================================================================ */

.avatar-waveform {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  height: 60px;
  padding: 0 10px;
}

.waveform-bar {
  width: 5px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 3px;
  transform-origin: center bottom;
  transition: height 0.1s var(--ease-gentle);
  box-shadow: 
    0 2px 4px rgba(0, 0, 0, 0.1),
    inset 0 1px 0 rgba(255, 255, 255, 0.4);
}

/* Default idle heights - gentle curve */
.waveform-bar:nth-child(1) { height: 12px; animation: waveIdle 2s ease-in-out infinite 0s; }
.waveform-bar:nth-child(2) { height: 20px; animation: waveIdle 2s ease-in-out infinite 0.1s; }
.waveform-bar:nth-child(3) { height: 32px; animation: waveIdle 2s ease-in-out infinite 0.2s; }
.waveform-bar:nth-child(4) { height: 40px; animation: waveIdle 2s ease-in-out infinite 0.25s; }
.waveform-bar:nth-child(5) { height: 48px; animation: waveIdle 2s ease-in-out infinite 0.3s; }
.waveform-bar:nth-child(6) { height: 40px; animation: waveIdle 2s ease-in-out infinite 0.35s; }
.waveform-bar:nth-child(7) { height: 32px; animation: waveIdle 2s ease-in-out infinite 0.4s; }
.waveform-bar:nth-child(8) { height: 20px; animation: waveIdle 2s ease-in-out infinite 0.5s; }
.waveform-bar:nth-child(9) { height: 12px; animation: waveIdle 2s ease-in-out infinite 0.6s; }

@keyframes waveIdle {
  0%, 100% { 
    transform: scaleY(1) scaleX(1); 
  }
  50% { 
    transform: scaleY(0.6) scaleX(1.1); 
  }
}

/* Speaking state - energetic */
.living-avatar.speaking .waveform-bar {
  animation: waveSpeaking 0.5s ease-in-out infinite;
}

@keyframes waveSpeaking {
  0%, 100% { 
    transform: scaleY(1); 
  }
  25% { 
    transform: scaleY(0.4); 
  }
  50% { 
    transform: scaleY(1.2); 
  }
  75% { 
    transform: scaleY(0.6); 
  }
}

/* ============================================================================
   EMOTION SHAPES - Waveform morphs to express feelings
   ============================================================================ */

/* Happy - Smile shape (edges up, center dips) */
.living-avatar[data-emotion="happy"] .waveform-bar:nth-child(1) { height: 40px; }
.living-avatar[data-emotion="happy"] .waveform-bar:nth-child(2) { height: 32px; }
.living-avatar[data-emotion="happy"] .waveform-bar:nth-child(3) { height: 24px; }
.living-avatar[data-emotion="happy"] .waveform-bar:nth-child(4) { height: 18px; }
.living-avatar[data-emotion="happy"] .waveform-bar:nth-child(5) { height: 16px; }
.living-avatar[data-emotion="happy"] .waveform-bar:nth-child(6) { height: 18px; }
.living-avatar[data-emotion="happy"] .waveform-bar:nth-child(7) { height: 24px; }
.living-avatar[data-emotion="happy"] .waveform-bar:nth-child(8) { height: 32px; }
.living-avatar[data-emotion="happy"] .waveform-bar:nth-child(9) { height: 40px; }

/* Excited - All bars high and bouncy */
.living-avatar[data-emotion="excited"] .waveform-bar {
  animation: waveExcited 0.3s ease-in-out infinite;
}

.living-avatar[data-emotion="excited"] .waveform-bar:nth-child(1) { height: 38px; }
.living-avatar[data-emotion="excited"] .waveform-bar:nth-child(2) { height: 45px; }
.living-avatar[data-emotion="excited"] .waveform-bar:nth-child(3) { height: 52px; }
.living-avatar[data-emotion="excited"] .waveform-bar:nth-child(4) { height: 48px; }
.living-avatar[data-emotion="excited"] .waveform-bar:nth-child(5) { height: 55px; }
.living-avatar[data-emotion="excited"] .waveform-bar:nth-child(6) { height: 48px; }
.living-avatar[data-emotion="excited"] .waveform-bar:nth-child(7) { height: 52px; }
.living-avatar[data-emotion="excited"] .waveform-bar:nth-child(8) { height: 45px; }
.living-avatar[data-emotion="excited"] .waveform-bar:nth-child(9) { height: 38px; }

@keyframes waveExcited {
  0%, 100% { transform: scaleY(1) translateY(0); }
  50% { transform: scaleY(1.15) translateY(-3px); }
}

/* Calm - Gentle, even wave */
.living-avatar[data-emotion="calm"] .waveform-bar {
  animation: waveCalm 4s ease-in-out infinite;
}

.living-avatar[data-emotion="calm"] .waveform-bar:nth-child(1) { height: 20px; }
.living-avatar[data-emotion="calm"] .waveform-bar:nth-child(2) { height: 24px; }
.living-avatar[data-emotion="calm"] .waveform-bar:nth-child(3) { height: 28px; }
.living-avatar[data-emotion="calm"] .waveform-bar:nth-child(4) { height: 30px; }
.living-avatar[data-emotion="calm"] .waveform-bar:nth-child(5) { height: 32px; }
.living-avatar[data-emotion="calm"] .waveform-bar:nth-child(6) { height: 30px; }
.living-avatar[data-emotion="calm"] .waveform-bar:nth-child(7) { height: 28px; }
.living-avatar[data-emotion="calm"] .waveform-bar:nth-child(8) { height: 24px; }
.living-avatar[data-emotion="calm"] .waveform-bar:nth-child(9) { height: 20px; }

@keyframes waveCalm {
  0%, 100% { transform: scaleY(1); }
  50% { transform: scaleY(0.85); }
}

/* Curious - Tilted, asymmetric */
.living-avatar[data-emotion="curious"] .waveform-bar:nth-child(1) { height: 15px; }
.living-avatar[data-emotion="curious"] .waveform-bar:nth-child(2) { height: 20px; }
.living-avatar[data-emotion="curious"] .waveform-bar:nth-child(3) { height: 25px; }
.living-avatar[data-emotion="curious"] .waveform-bar:nth-child(4) { height: 32px; }
.living-avatar[data-emotion="curious"] .waveform-bar:nth-child(5) { height: 40px; }
.living-avatar[data-emotion="curious"] .waveform-bar:nth-child(6) { height: 48px; }
.living-avatar[data-emotion="curious"] .waveform-bar:nth-child(7) { height: 52px; }
.living-avatar[data-emotion="curious"] .waveform-bar:nth-child(8) { height: 48px; }
.living-avatar[data-emotion="curious"] .waveform-bar:nth-child(9) { height: 40px; }

/* ============================================================================
   AVATAR TEXT & INFO
   ============================================================================ */

.avatar-info {
  text-align: center;
  opacity: 0;
  transform: translateY(20px);
  transition: 
    opacity 0.8s var(--ease-gentle) 0.3s,
    transform 0.8s var(--ease-gentle) 0.3s;
}

.living-avatar-overlay.active .avatar-info {
  opacity: 1;
  transform: translateY(0);
}

.avatar-greeting {
  font-family: var(--font-display);
  font-size: clamp(24px, 4vw, 36px);
  font-weight: 600;
  color: var(--color-text);
  margin-bottom: 8px;
  letter-spacing: -0.02em;
  transition: 
    opacity 0.4s ease,
    transform 0.4s var(--ease-spring);
}

.avatar-subtext {
  font-size: 16px;
  color: var(--color-text-secondary);
  max-width: 400px;
  line-height: 1.6;
}

/* CTA */
.avatar-cta {
  margin-top: 32px;
  opacity: 0;
  transform: translateY(20px);
  transition: 
    opacity 0.8s var(--ease-gentle) 0.6s,
    transform 0.8s var(--ease-gentle) 0.6s;
}

.living-avatar-overlay.active .avatar-cta {
  opacity: 1;
  transform: translateY(0);
}

/* Skip/Close button */
.avatar-skip {
  position: absolute;
  bottom: 40px;
  left: 50%;
  transform: translateX(-50%);
  background: transparent;
  border: none;
  color: var(--color-text-muted);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  padding: 12px 24px;
  opacity: 0;
  transition: 
    opacity 0.5s ease 1s,
    color 0.2s ease;
  pointer-events: auto;
}

.living-avatar-overlay.active .avatar-skip {
  opacity: 1;
}

.avatar-skip:hover {
  color: var(--color-text);
}

/* ============================================================================
   PIXAR REACTIONS - Triggered via JS
   ============================================================================ */

/* Bounce - Joy reaction */
.living-avatar.react-bounce {
  animation: pixarBounce 0.6s var(--ease-spring);
}

@keyframes pixarBounce {
  0% { transform: translateY(0) scale(1, 1); }
  15% { transform: translateY(3px) scale(1.04, 0.92); }
  30% { transform: translateY(-15px) scale(0.92, 1.08); }
  45% { transform: translateY(-18px) scale(0.94, 1.06); }
  60% { transform: translateY(-8px) scale(1.02, 0.98); }
  75% { transform: translateY(4px) scale(0.97, 1.02); }
  100% { transform: translateY(0) scale(1, 1); }
}

/* Nod - Agreement */
.living-avatar.react-nod {
  animation: pixarNod 0.5s var(--ease-spring);
}

@keyframes pixarNod {
  0% { transform: translateY(0) rotate(0deg); }
  15% { transform: translateY(-3px) rotate(-0.5deg); }
  30% { transform: translateY(6px) rotate(1deg); }
  50% { transform: translateY(8px) rotate(0.5deg); }
  70% { transform: translateY(3px) rotate(-0.2deg); }
  100% { transform: translateY(0) rotate(0deg); }
}

/* Attention - Something caught interest */
.living-avatar.react-attention {
  animation: pixarAttention 0.4s var(--ease-spring);
}

@keyframes pixarAttention {
  0% { transform: scale(1) rotate(0deg); }
  30% { transform: scale(1.05) rotate(-1deg); }
  60% { transform: scale(1.02) rotate(0.5deg); }
  100% { transform: scale(1) rotate(0deg); }
}

/* Curious tilt - WALL-E style */
.living-avatar.react-curious {
  animation: pixarCurious 0.8s var(--ease-gentle);
}

@keyframes pixarCurious {
  0% { transform: rotate(0deg); }
  25% { transform: rotate(-4deg) translateX(-3px); }
  50% { transform: rotate(3deg) translateX(2px); }
  100% { transform: rotate(0deg); }
}

/* ============================================================================
   HERO INTEGRATION - Avatar in hero section
   ============================================================================ */

.hero-avatar-container {
  position: fixed;
  bottom: 80px;
  right: 40px;
  z-index: 50;
  pointer-events: auto;
  opacity: 0;
  transform: translateY(30px) scale(0.9);
  transition: 
    opacity 0.6s var(--ease-gentle),
    transform 0.6s var(--ease-spring);
}

.hero-avatar-container.visible {
  opacity: 1;
  transform: translateY(0) scale(1);
}

/* Smaller version for corner presence */
.hero-avatar-container .avatar-orb-wrapper {
  width: 80px;
  height: 80px;
}

.hero-avatar-container .avatar-waveform {
  gap: 2px;
  height: 24px;
}

.hero-avatar-container .waveform-bar {
  width: 3px;
}

.hero-avatar-container .avatar-glow-ring {
  inset: -10px;
}

.hero-avatar-container .avatar-glow-ring:nth-child(2) {
  inset: -20px;
}

.hero-avatar-container .avatar-glow-ring:nth-child(3) {
  inset: -30px;
}

/* Tooltip */
.hero-avatar-tooltip {
  position: absolute;
  bottom: calc(100% + 16px);
  right: 0;
  background: var(--color-bg-elevated);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: 12px 16px;
  font-size: 14px;
  color: var(--color-text);
  white-space: nowrap;
  box-shadow: var(--shadow-md);
  opacity: 0;
  transform: translateY(10px);
  transition: 
    opacity 0.3s ease,
    transform 0.3s var(--ease-spring);
  pointer-events: none;
}

.hero-avatar-tooltip::after {
  content: '';
  position: absolute;
  bottom: -6px;
  right: 30px;
  width: 12px;
  height: 12px;
  background: var(--color-bg-elevated);
  border-right: 1px solid var(--color-border);
  border-bottom: 1px solid var(--color-border);
  transform: rotate(45deg);
}

.hero-avatar-container:hover .hero-avatar-tooltip {
  opacity: 1;
  transform: translateY(0);
}

/* Click animation for hero avatar */
.hero-avatar-container:active .avatar-orb {
  transform: scale(0.95);
  transition: transform 0.1s ease;
}

/* Ambient pulse for hero avatar */
.hero-avatar-container .living-avatar {
  animation: heroAvatarFloat 4s ease-in-out infinite;
}

@keyframes heroAvatarFloat {
  0%, 100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-4px);
  }
}

/* Hero avatar breathing waveform */
.hero-avatar-container .waveform-bar {
  animation: heroWave 1.5s ease-in-out infinite;
}

.hero-avatar-container .waveform-bar:nth-child(1) { height: 6px; animation-delay: 0s; }
.hero-avatar-container .waveform-bar:nth-child(2) { height: 10px; animation-delay: 0.1s; }
.hero-avatar-container .waveform-bar:nth-child(3) { height: 14px; animation-delay: 0.15s; }
.hero-avatar-container .waveform-bar:nth-child(4) { height: 10px; animation-delay: 0.2s; }
.hero-avatar-container .waveform-bar:nth-child(5) { height: 6px; animation-delay: 0.25s; }

@keyframes heroWave {
  0%, 100% { transform: scaleY(1); }
  50% { transform: scaleY(0.5); }
}

/* ============================================================================
   RESPONSIVE
   ============================================================================ */

@media (max-width: 768px) {
  .avatar-orb-wrapper {
    width: 160px;
    height: 160px;
  }
  
  .avatar-waveform {
    gap: 3px;
    height: 48px;
  }
  
  .waveform-bar {
    width: 4px;
  }
  
  .hero-avatar-container {
    bottom: 100px;
    right: 20px;
  }
  
  .hero-avatar-container .avatar-orb-wrapper {
    width: 64px;
    height: 64px;
  }
}

/* ============================================================================
   REDUCED MOTION
   ============================================================================ */

@media (prefers-reduced-motion: reduce) {
  .living-avatar,
  .waveform-bar,
  .avatar-glow-ring,
  .avatar-aurora,
  .ambient-particle {
    animation: none !important;
  }
  
  .living-avatar.react-bounce,
  .living-avatar.react-nod,
  .living-avatar.react-attention,
  .living-avatar.react-curious {
    animation: none !important;
  }
}

