/**
 * ORSON Letter Vortex Styles v2
 * =============================
 * Full implementation based on orson-letter-vortex.jsx
 */

/* =============================================================================
   CSS VARIABLES
   ============================================================================= */

:root {
  --orson-primary: #B91C1C;
  --orson-glow: rgba(185, 28, 28, 0.4);
  --orson-mist: rgba(185, 28, 28, 0.06);
  --orson-bg: #1f1616;
}

/* =============================================================================
   ORSON BUTTON MODIFICATIONS
   ============================================================================= */

#orson-btn,
#orson-btn-mobile {
  position: relative;
  overflow: hidden;
}

#orson-btn .mode-btn-text {
  font-weight: 600;
  letter-spacing: 0.5px;
}

.orson-hold-indicator {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 0;
  background: linear-gradient(180deg, rgba(185, 28, 28, 0.3) 0%, rgba(185, 28, 28, 0.6) 100%);
  transition: height 0.1s linear;
  pointer-events: none;
}

.orson-hold-indicator.active {
  animation: orson-fill 0.8s linear forwards;
}

@keyframes orson-fill {
  0% { height: 0; }
  100% { height: 100%; }
}

/* =============================================================================
   ORSON POPUP - Full screen overlay
   ============================================================================= */

.orson-popup {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  /* Removed width: 100vw / height: 100vh - causes overflow on mobile */
  /* inset (top/left/right/bottom: 0) handles fullscreen correctly */
  z-index: 100000;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.3s ease, visibility 0.3s ease;
  background-color: rgba(255, 255, 255, 0.7);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}

.orson-popup.orson-hidden {
  display: none;
}

.orson-popup:not(.orson-hidden) {
  display: flex;
}

.orson-popup.orson-visible {
  opacity: 1;
  visibility: visible;
}

/* =============================================================================
   BACKGROUND GRADIENT - Dark center fading out
   ============================================================================= */

.orson-bg-gradient {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

/* =============================================================================
   MAIN SMOKE - Large blur layer
   ============================================================================= */

.orson-smoke-main {
  position: absolute;
  left: 50%;
  top: 50%;
  margin-left: -300px;
  margin-top: -300px;
  width: 600px;
  height: 600px;
  pointer-events: none;
  opacity: 0;
  filter: blur(15px);
  transition: opacity 0.5s ease;
  will-change: transform, opacity;
  backface-visibility: hidden;
}

.orson-popup.orson-visible .orson-smoke-main {
  opacity: 1;
  animation: smokeDrift 8s ease-in-out infinite;
}

/* =============================================================================
   BACKFLOW RINGS - Pulsing toward center
   ============================================================================= */

.orson-backflow {
  position: absolute;
  left: 50%;
  top: 50%;
  margin-left: -225px;
  margin-top: -225px;
  width: 450px;
  height: 450px;
  border-radius: 50%;
  pointer-events: none;
  filter: blur(10px);
  opacity: 0;
  will-change: transform, opacity;
  backface-visibility: hidden;
}

.orson-popup.orson-visible .orson-backflow {
  animation: backflow 4s ease-in-out infinite;
}

.orson-backflow-1 { animation-delay: 0s; }
.orson-backflow-2 { animation-delay: 0.8s; }
.orson-backflow-3 { animation-delay: 1.6s; }
.orson-backflow-4 { animation-delay: 2.4s; }

/* =============================================================================
   TURBULENCE - Small swirling blobs
   ============================================================================= */

.orson-turbulence {
  position: absolute;
  pointer-events: none;
  filter: blur(15px);
  opacity: 0;
  transform: translate(-50%, -50%);
  will-change: transform, opacity;
  backface-visibility: hidden;
}

.orson-popup.orson-visible .orson-turbulence {
  animation: turbulence 3s ease-in-out infinite;
}

/* =============================================================================
   ADDITIONAL SMOKE LAYERS
   ============================================================================= */

.orson-smoke-layer {
  position: absolute;
  left: 50%;
  top: 50%;
  margin-left: -188px;
  margin-top: -188px;
  width: 375px;
  height: 375px;
  pointer-events: none;
  filter: blur(20px);
  opacity: 0;
  will-change: transform, opacity;
  backface-visibility: hidden;
}

.orson-popup.orson-visible .orson-smoke-layer {
  animation: smokeDrift 6s ease-in-out infinite;
}

.orson-smoke-layer-1 { transform: rotate(0deg); animation-delay: 0s; }
.orson-smoke-layer-2 { transform: rotate(120deg); animation-delay: 0.7s; }
.orson-smoke-layer-3 { transform: rotate(240deg); animation-delay: 1.4s; }

/* =============================================================================
   RIPPLE SHADOWS - Under ripples
   ============================================================================= */

.orson-ripple-shadow {
  position: absolute;
  left: 50%;
  top: 50%;
  margin-left: -75px;
  margin-top: -75px;
  width: 150px;
  height: 150px;
  border-radius: 50%;
  pointer-events: none;
  filter: blur(6px);
  opacity: 0;
  will-change: transform, opacity;
  backface-visibility: hidden;
}

.orson-popup.orson-visible .orson-ripple-shadow {
  animation: rippleWobble 8s ease-out infinite;
}

.orson-popup[data-state="listening"] .orson-ripple-shadow,
.orson-popup[data-state="thinking"] .orson-ripple-shadow,
.orson-popup[data-state="speaking"] .orson-ripple-shadow,
.orson-popup[data-state="executing"] .orson-ripple-shadow {
  animation-duration: 5s;
}

.orson-popup.orson-visible .orson-ripple-shadow-1 { animation-delay: 0s; }
.orson-popup.orson-visible .orson-ripple-shadow-2 { animation-delay: 2.5s; }
.orson-popup.orson-visible .orson-ripple-shadow-3 { animation-delay: 5s; }

/* Fast state delays (5s animation, proportional to 8s base) */
.orson-popup[data-state="listening"] .orson-ripple-shadow-1,
.orson-popup[data-state="thinking"] .orson-ripple-shadow-1,
.orson-popup[data-state="speaking"] .orson-ripple-shadow-1,
.orson-popup[data-state="executing"] .orson-ripple-shadow-1 { animation-delay: 0s; }

.orson-popup[data-state="listening"] .orson-ripple-shadow-2,
.orson-popup[data-state="thinking"] .orson-ripple-shadow-2,
.orson-popup[data-state="speaking"] .orson-ripple-shadow-2,
.orson-popup[data-state="executing"] .orson-ripple-shadow-2 { animation-delay: 1.56s; }

.orson-popup[data-state="listening"] .orson-ripple-shadow-3,
.orson-popup[data-state="thinking"] .orson-ripple-shadow-3,
.orson-popup[data-state="speaking"] .orson-ripple-shadow-3,
.orson-popup[data-state="executing"] .orson-ripple-shadow-3 { animation-delay: 3.12s; }

/* =============================================================================
   RIPPLE RINGS - Wobbling organic expansion
   ============================================================================= */

.orson-ripple {
  position: absolute;
  left: 50%;
  top: 50%;
  margin-left: -75px;
  margin-top: -75px;
  width: 150px;
  height: 150px;
  border-radius: 50%;
  pointer-events: none;
  filter: blur(3px);
  opacity: 0;
  will-change: transform, opacity;
  backface-visibility: hidden;
}

.orson-popup.orson-visible .orson-ripple {
  animation: rippleWobble 8s ease-out infinite;
}

.orson-popup[data-state="listening"] .orson-ripple,
.orson-popup[data-state="thinking"] .orson-ripple,
.orson-popup[data-state="speaking"] .orson-ripple,
.orson-popup[data-state="executing"] .orson-ripple {
  animation-duration: 5s;
}

.orson-popup.orson-visible .orson-ripple-1 { animation-delay: 0s; }
.orson-popup.orson-visible .orson-ripple-2 { animation-delay: 2.5s; }
.orson-popup.orson-visible .orson-ripple-3 { animation-delay: 5s; }

/* Fast state delays (5s animation, proportional to 8s base) */
.orson-popup[data-state="listening"] .orson-ripple-1,
.orson-popup[data-state="thinking"] .orson-ripple-1,
.orson-popup[data-state="speaking"] .orson-ripple-1,
.orson-popup[data-state="executing"] .orson-ripple-1 { animation-delay: 0s; }

.orson-popup[data-state="listening"] .orson-ripple-2,
.orson-popup[data-state="thinking"] .orson-ripple-2,
.orson-popup[data-state="speaking"] .orson-ripple-2,
.orson-popup[data-state="executing"] .orson-ripple-2 { animation-delay: 1.56s; }

.orson-popup[data-state="listening"] .orson-ripple-3,
.orson-popup[data-state="thinking"] .orson-ripple-3,
.orson-popup[data-state="speaking"] .orson-ripple-3,
.orson-popup[data-state="executing"] .orson-ripple-3 { animation-delay: 3.12s; }

/* =============================================================================
   DYNAMIC ELEMENT CONTAINERS
   ============================================================================= */

#orson-turbulence-container {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 5;
}

#orson-particles-container {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 100;
}

/* =============================================================================
   CONTENT WRAPPER
   ============================================================================= */

.orson-popup-wrapper {
  position: relative;
  width: 375px;
  height: 300px;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}

/* =============================================================================
   MIST - Expanding from center
   ============================================================================= */

.orson-mist {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 0;
  height: 0;
  opacity: 0;
  filter: blur(40px);
  transition: all 1s ease-out;
  pointer-events: none;
}

.orson-popup.orson-visible .orson-mist {
  width: 450px;
  height: 375px;
  opacity: 1;
  animation: mistPulse 4s ease-in-out infinite;
}

/* =============================================================================
   CONTENT AREA (replacing frame)
   ============================================================================= */

.orson-content {
  position: relative;
  width: 375px;
  height: 300px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

/* =============================================================================
   EYE - Main animation container
   ============================================================================= */

.orson-eye {
  position: relative;
  width: 135px;
  height: 135px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* =============================================================================
   PUPIL
   ============================================================================= */

.orson-pupil {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background-color: var(--orson-primary);
  box-shadow: 0 0 10px var(--orson-glow), 0 0 20px var(--orson-glow);
  z-index: 20;
  animation: pupilBreath 3s ease-in-out infinite;
}

.orson-popup[data-state="thinking"] .orson-pupil {
  animation: pupilPulse 0.4s ease-in-out infinite;
}

/* IDLE state - dimmer pupil, waiting for wake word */
.orson-popup[data-state="idle"] .orson-pupil {
  opacity: 0.6;
  width: 7px;
  height: 7px;
  box-shadow: 0 0 5px var(--orson-glow);
}

/* Wake word activation pulse */
.orson-eye.pulse-activation {
  animation: wakeWordPulse 0.5s ease-out;
}

@keyframes wakeWordPulse {
  0% {
    transform: scale(1);
    filter: brightness(1);
  }
  50% {
    transform: scale(1.2);
    filter: brightness(1.5);
  }
  100% {
    transform: scale(1);
    filter: brightness(1);
  }
}

/* =============================================================================
   LETTERS CONTAINER
   ============================================================================= */

.orson-letters {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

/* =============================================================================
   INDIVIDUAL LETTERS
   ============================================================================= */

.orson-letter {
  position: absolute;
  left: 50%;
  top: 50%;
  font-family: 'JetBrains Mono', 'Fira Code', 'Courier New', monospace;
  font-size: 9px;
  font-weight: 400;
  transform-origin: center center;
  z-index: 1;
  user-select: none;
  will-change: transform;
}

.orson-letter-primary {
  font-size: 14px;
  font-weight: 700;
  z-index: 10;
}

/* =============================================================================
   LETTER ANIMATIONS - By State
   ============================================================================= */

/* IDLE - Orbit */
.orson-popup[data-state="idle"] .orson-letter {
  animation: orbit var(--speed) linear infinite;
  animation-delay: var(--delay);
}

/* LISTENING - Spiral In */
.orson-popup[data-state="listening"] .orson-letter {
  animation: spiralIn 2.5s ease-in-out infinite;
  animation-delay: var(--delay);
}

/* THINKING - Chaos */
.orson-popup[data-state="thinking"] .orson-letter {
  animation: chaos 0.6s ease-in-out infinite;
  animation-delay: calc(var(--delay) * 0.15);
}

/* SPEAKING - Burst Out */
.orson-popup[data-state="speaking"] .orson-letter {
  animation: burstOut 2s ease-out infinite;
  animation-delay: var(--delay);
}

/* EXECUTING - Fast Orbit */
.orson-popup[data-state="executing"] .orson-letter {
  animation: orbit calc(var(--speed) * 0.4) linear infinite;
  animation-delay: var(--delay);
}

/* =============================================================================
   PARTICLES - Floating letters on ripples
   ============================================================================= */

.orson-particle {
  position: absolute;
  left: 50%;
  top: 50%;
  font-family: 'JetBrains Mono', 'Fira Code', 'Courier New', monospace;
  font-size: 9px;
  font-weight: 600;
  pointer-events: none;
  user-select: none;
  opacity: 0;
  z-index: 100;
  will-change: transform, opacity;
  transform: translateZ(0);
  backface-visibility: hidden;
}

.orson-popup.orson-visible .orson-particle {
  animation: particleFloat 8s ease-out infinite;
}

.orson-popup[data-state="listening"] .orson-particle,
.orson-popup[data-state="thinking"] .orson-particle,
.orson-popup[data-state="speaking"] .orson-particle,
.orson-popup[data-state="executing"] .orson-particle {
  animation-duration: 5s;
}

/* =============================================================================
   EXECUTING RING
   ============================================================================= */

.orson-exec-ring {
  position: absolute;
  width: 120px;
  height: 120px;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  color: var(--orson-primary);
  display: none;
  pointer-events: none;
}

.orson-popup[data-state="executing"] .orson-exec-ring {
  display: block;
  animation: spin 3s linear infinite;
}

/* =============================================================================
   STREAMING TEXT (Speaking state)
   ============================================================================= */

.orson-streaming {
  position: absolute;
  bottom: -60px;
  left: 0;
  right: 0;
  padding: 0 32px;
  text-align: center;
  min-height: 40px;
  display: none;
}

.orson-streaming-text {
  font-family: 'Inter', sans-serif;
  font-size: 14px;
  font-weight: 500;
  color: var(--orson-primary);
  padding: 12px 24px;
  border-radius: 16px;
  background: rgba(255, 255, 255, 0.08);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
}

.orson-cursor {
  display: inline-block;
  width: 2px;
  height: 16px;
  background-color: var(--orson-primary);
  margin-left: 4px;
  vertical-align: middle;
  animation: blink 0.8s step-end infinite;
}

/* =============================================================================
   STATUS TEXT
   ============================================================================= */

.orson-status {
  position: absolute;
  bottom: -50px;
  text-align: center;
  font-family: 'Inter', sans-serif;
  font-size: 14px;
  font-weight: 500;
  color: var(--orson-primary);
  padding: 12px 24px;
  border-radius: 16px;
  background: rgba(255, 255, 255, 0.08);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
}

/* =============================================================================
   CLOSE BUTTON
   ============================================================================= */

.orson-close-btn {
  position: absolute;
  top: 16px;
  right: 16px;
  width: 36px;
  height: 36px;
  border: none;
  background: rgba(0, 0, 0, 0.05);
  color: rgba(0, 0, 0, 0.4);
  border-radius: 50%;
  font-size: 24px;
  cursor: pointer;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
  z-index: 200;
}

.orson-close-btn:hover {
  background: rgba(0, 0, 0, 0.1);
  color: rgba(0, 0, 0, 0.7);
}

/* =============================================================================
   BODY STATE
   ============================================================================= */

body.orson-active {
  overflow: hidden;
}

/* =============================================================================
   KEYFRAME ANIMATIONS
   ============================================================================= */

@keyframes mistPulse {
  0%, 100% {
    opacity: 1;
    transform: scale(1);
  }
  50% {
    opacity: 0.7;
    transform: scale(1.05);
  }
}

@keyframes pupilBreath {
  0%, 100% { transform: translate(-50%, -50%) scale(1); }
  50% { transform: translate(-50%, -50%) scale(1.3); }
}

@keyframes pupilPulse {
  0%, 100% { transform: translate(-50%, -50%) scale(1); }
  50% { transform: translate(-50%, -50%) scale(1.6); }
}

@keyframes orbit {
  from {
    transform: translate(-50%, -50%) rotate(var(--angle)) translateX(var(--radius)) rotate(calc(-1 * var(--angle)));
  }
  to {
    transform: translate(-50%, -50%) rotate(calc(var(--angle) + 360deg)) translateX(var(--radius)) rotate(calc(-1 * var(--angle) - 360deg));
  }
}

@keyframes spiralIn {
  0%, 100% {
    transform: translate(-50%, -50%) rotate(var(--angle)) translateX(var(--radius)) scale(1);
    opacity: 1;
  }
  50% {
    transform: translate(-50%, -50%) rotate(calc(var(--angle) + 180deg)) translateX(calc(var(--radius) * 0.4)) scale(0.7);
    opacity: 0.5;
  }
}

@keyframes chaos {
  0%, 100% {
    transform: translate(calc(-50% + var(--x)), calc(-50% + var(--y))) rotate(0deg);
  }
  25% {
    transform: translate(calc(-50% + var(--x) + 12px), calc(-50% + var(--y) - 8px)) rotate(15deg);
  }
  50% {
    transform: translate(calc(-50% + var(--x) - 8px), calc(-50% + var(--y) + 12px)) rotate(-12deg);
  }
  75% {
    transform: translate(calc(-50% + var(--x) + 8px), calc(-50% + var(--y) - 12px)) rotate(8deg);
  }
}

@keyframes burstOut {
  0% {
    transform: translate(-50%, -50%) rotate(var(--angle)) translateX(var(--start-radius)) scale(1);
    opacity: 1;
  }
  100% {
    transform: translate(-50%, -50%) rotate(var(--angle)) translateX(90px) scale(0.15);
    opacity: 0;
  }
}

@keyframes spin {
  from { transform: translate(-50%, -50%) rotate(0deg); }
  to { transform: translate(-50%, -50%) rotate(360deg); }
}

@keyframes blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0; }
}

@keyframes rippleWobble {
  0% {
    transform: scale(0.3) rotate(0deg);
    opacity: 1;
  }
  25% {
    transform: scale(1.5) rotate(25deg);
    opacity: 0.8;
  }
  50% {
    transform: scale(2.8) rotate(55deg);
    opacity: 0.5;
  }
  75% {
    transform: scale(4) rotate(90deg);
    opacity: 0.25;
  }
  100% {
    transform: scale(5) rotate(120deg);
    opacity: 0;
  }
}

@keyframes smokeDrift {
  0%, 100% {
    transform: scale(1) rotate(0deg);
    opacity: 1;
  }
  33% {
    transform: scale(1.1) rotate(5deg);
    opacity: 0.8;
  }
  66% {
    transform: scale(0.95) rotate(-3deg);
    opacity: 0.9;
  }
}

@keyframes backflow {
  0%, 100% {
    transform: scale(1.2) rotate(0deg);
    opacity: 0.3;
  }
  50% {
    transform: scale(0.8) rotate(-10deg);
    opacity: 0.7;
  }
}

@keyframes turbulence {
  0%, 100% {
    transform: translate(-50%, -50%) scale(1) rotate(0deg);
    opacity: 0.6;
    border-radius: 40% 60% 55% 45% / 55% 45% 60% 40%;
  }
  25% {
    transform: translate(-50%, -50%) scale(1.3) rotate(90deg);
    opacity: 0.3;
    border-radius: 55% 45% 40% 60% / 45% 55% 45% 55%;
  }
  50% {
    transform: translate(-50%, -50%) scale(0.8) rotate(180deg);
    opacity: 0.8;
    border-radius: 60% 40% 45% 55% / 40% 60% 55% 45%;
  }
  75% {
    transform: translate(-50%, -50%) scale(1.1) rotate(270deg);
    opacity: 0.4;
    border-radius: 45% 55% 60% 40% / 60% 40% 40% 60%;
  }
}

@keyframes particleFloat {
  0% {
    transform: translate(-50%, -50%) rotate(var(--angle)) translateX(22px);
    opacity: 1;
  }
  25% {
    transform: translate(-50%, -50%) rotate(calc(var(--angle) + 25deg)) translateX(112px);
    opacity: 0.8;
  }
  50% {
    transform: translate(-50%, -50%) rotate(calc(var(--angle) + 55deg)) translateX(210px);
    opacity: 0.5;
  }
  75% {
    transform: translate(-50%, -50%) rotate(calc(var(--angle) + 90deg)) translateX(300px);
    opacity: 0.25;
  }
  100% {
    transform: translate(-50%, -50%) rotate(calc(var(--angle) + 120deg)) translateX(375px);
    opacity: 0;
  }
}

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

@media (max-width: 540px) {
  .orson-popup-wrapper {
    width: 95vw;
    height: 80vw;
    max-width: 400px;
    max-height: 320px;
  }

  .orson-content {
    width: 95%;
    height: 95%;
  }

  .orson-eye {
    width: 140px;
    height: 140px;
  }

  .orson-letter-primary {
    font-size: 14px;
  }

  .orson-letter {
    font-size: 10px;
  }

  .orson-bg-gradient {
    width: 800px;
    height: 800px;
  }

  .orson-smoke-main {
    width: 500px;
    height: 500px;
  }

  .orson-backflow {
    width: 400px;
    height: 400px;
  }
}

/* =============================================================================
   MINI VOICE MODE - Voice listening indicator in chat
   ============================================================================= */

.mini-voice-listening {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px !important;
  background: linear-gradient(135deg, rgba(185, 28, 28, 0.08) 0%, rgba(185, 28, 28, 0.03) 100%) !important;
  border: 1px solid rgba(185, 28, 28, 0.2);
  border-radius: 12px;
}

.mini-voice-listening .loading-text {
  flex: 1;
  font-size: 14px;
  color: var(--text-secondary, #999);
}

.mini-voice-stop-btn {
  background: rgba(255, 255, 255, 0.1);
  border: none;
  border-radius: 50%;
  width: 28px;
  height: 28px;
  color: var(--text-secondary, #999);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  transition: all 0.2s ease;
  flex-shrink: 0;
}

.mini-voice-stop-btn:hover {
  background: rgba(185, 28, 28, 0.3);
  color: var(--orson-primary);
}
