/* ===================================
   AI SPACEMAN - CSS-First Animation
   Theme-aware via CSS variables
   =================================== */

/* Space theme (default) */
:root,
[data-theme="space"] {
  --spaceman-bubble-bg: rgba(255, 255, 255, 0.97);
  --spaceman-bubble-text: #1a1a1a;
  --spaceman-bubble-shadow: 0 4px 16px rgba(0, 0, 0, 0.25), inset 0 1px 0 rgba(255, 255, 255, 1);
  --spaceman-cursor-color: #6a0dad;
  --spaceman-tail-dot-bg: rgba(255, 255, 255, 0.95);
  --spaceman-helmet-bg: linear-gradient(145deg, #e8e8e8, #c4c4c4);
  --spaceman-helmet-shadow: inset -5px -5px 15px rgba(0, 0, 0, 0.2), inset 5px 5px 15px rgba(255, 255, 255, 0.5), 0 5px 20px rgba(0, 0, 0, 0.3);
  --spaceman-visor-bg: linear-gradient(135deg, #1a1a3e, #2d2d5e, #1a1a3e);
  --spaceman-torso-bg: linear-gradient(180deg, #e0e0e0, #b8b8b8);
  --spaceman-chest-panel-bg: #2a2a2a;
  --spaceman-arm-bg: linear-gradient(90deg, #d0d0d0, #e0e0e0, #d0d0d0);
  --spaceman-leg-bg: linear-gradient(180deg, #e0e0e0, #c8c8c8, #b0b0b0);
  --spaceman-pack-bg: linear-gradient(180deg, #5a5a5a, #3a3a3a, #2a2a2a);
  --spaceman-pack-nozzle-bg: linear-gradient(180deg, #4a4a4a, #2a2a2a);
  --spaceman-flame-bg: linear-gradient(180deg, rgba(255, 200, 100, 1) 0%, #fbbf24 15%, #f97316 40%, #ef4444 65%, rgba(239, 68, 68, 0.5) 85%, transparent 100%);
  --spaceman-flame-shadow: 0 0 8px #f97316, 0 0 15px #fbbf24, 0 0 25px rgba(251, 191, 36, 0.4);
  --spaceman-legs-glow: drop-shadow(0 8px 12px rgba(251, 146, 60, 0.3));
}

[data-theme="garden"] {
  --spaceman-bubble-bg: rgba(255, 252, 240, 0.96);
  --spaceman-bubble-text: #1a2f1a;
  --spaceman-bubble-shadow: 0 4px 16px rgba(30, 50, 30, 0.2), inset 0 1px 0 rgba(255, 255, 255, 0.9);
  --spaceman-cursor-color: #b22222;
  --spaceman-tail-dot-bg: rgba(255, 252, 240, 0.95);
  /* Superman-style: head/hair, blue suit, red cape, yellow shield */
  --spaceman-helmet-bg: linear-gradient(145deg, #2c2416, #1a1510);
  --spaceman-helmet-shadow: inset -3px -3px 10px rgba(0, 0, 0, 0.3), 0 4px 16px rgba(0, 0, 0, 0.25);
  --spaceman-visor-bg: linear-gradient(135deg, #1a1a2e, #2d2d4e, #1a1a2e);
  --spaceman-torso-bg: linear-gradient(180deg, #1e3a8a, #1565c0, #0d47a1);
  --spaceman-chest-panel-bg: linear-gradient(180deg, #fbbf24, #f59e0b);
  --spaceman-arm-bg: linear-gradient(90deg, #1565c0, #1e3a8a, #1565c0);
  --spaceman-leg-bg: linear-gradient(180deg, #1e3a8a 0%, #1565c0 55%, #b22222 55%, #dc2626 100%);
  --spaceman-pack-bg: transparent;
  --spaceman-pack-nozzle-bg: transparent;
  --spaceman-flame-bg: transparent;
  --spaceman-flame-shadow: none;
  --spaceman-legs-glow: drop-shadow(0 4px 8px rgba(0, 0, 0, 0.2));
  --hero-cape-bg: linear-gradient(180deg, #b91c1c 0%, #dc2626 30%, #991b1b 100%);
}

/* Container */
.spaceman {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  animation: float 6s ease-in-out infinite;
  cursor: pointer;
  transition: transform 0.3s ease;
}

.spaceman:hover {
  transform: scale(1.02);
}

.spaceman.boost {
  animation: boost 0.6s ease-out;
}

/* ===== QUIET MODE OPTION MENU ===== */
.spaceman-outer {
  position: relative;
  display: inline-block;
}

.spaceman-quiet-menu {
  position: absolute;
  bottom: 100%;
  left: 50%;
  transform: translateX(-50%);
  margin-bottom: 10px;
  z-index: 100;
  pointer-events: auto;
}

.spaceman-quiet-menu[hidden] {
  display: none !important;
}

.spaceman-quiet-menu-btn {
  display: block;
  width: 100%;
  padding: 10px 18px;
  background: var(--spaceman-bubble-bg);
  color: var(--spaceman-bubble-text);
  border: none;
  border-radius: 14px;
  font-family: 'Open Sans', sans-serif;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  box-shadow: var(--spaceman-bubble-shadow);
  white-space: nowrap;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.spaceman-quiet-menu-btn:hover {
  transform: scale(1.03);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.2);
}

.spaceman-quiet-menu-btn:active {
  transform: scale(0.98);
}

/* ===== THOUGHT BUBBLE ===== */
.thought-bubble {
  background: var(--spaceman-bubble-bg);
  color: var(--spaceman-bubble-text);
  padding: 12px 16px;
  border-radius: 18px;
  min-width: 120px;
  max-width: 240px;
  box-shadow: var(--spaceman-bubble-shadow);
  position: relative;
  font-family: 'Open Sans', sans-serif;
  font-size: 13px;
  line-height: 1.45;
  text-align: center;
  animation: bubblePulse 6s ease-in-out infinite;
  /* Expand vertically: width fixed so text wraps, height grows with content */
  display: inline-block;
  vertical-align: top;
}

.thought-text {
  display: inline;
  word-wrap: break-word;
  overflow-wrap: break-word;
  word-break: break-word;
}

.cursor {
  display: inline-block;
  color: var(--spaceman-cursor-color);
  font-weight: bold;
  margin-left: 1px;
  animation: blink 0.8s infinite;
}

/* Tail: connects bubble (bottom-left) to head; dots curve from left toward center */
.thought-tail {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  margin-top: -6px;
  margin-left: -36px;
  margin-bottom: 2px;
}

.bubble-dot {
  background: var(--spaceman-tail-dot-bg);
  border-radius: 50%;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.18), inset 0 1px 0 rgba(255, 255, 255, 0.7);
  flex-shrink: 0;
}

[data-theme="garden"] .bubble-dot {
  box-shadow: 0 2px 8px rgba(30, 50, 30, 0.2), inset 0 1px 0 rgba(255, 255, 255, 0.85);
}

/* Dots curve from bubble (left) toward head (right): small → larger offset */
.dot-1 {
  width: 12px;
  height: 12px;
  margin-left: 0;
  margin-top: 0;
}

.dot-2 {
  width: 10px;
  height: 10px;
  margin-left: 10px;
  margin-top: 2px;
}

.dot-3 {
  width: 8px;
  height: 8px;
  margin-left: 18px;
  margin-top: 2px;
}

/* ===== SPACEMAN BODY ===== */
.spaceman-body {
  position: relative;
  width: 120px;
  height: 200px;
  margin-top: 10px;
}

/* Hero head (garden theme – bare head, hair, domino mask) */
.hero-head {
  display: none;
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 70px;
  height: 70px;
  z-index: 3;
  pointer-events: none;
}

[data-theme="garden"] .hero-head {
  display: block;
}

[data-theme="garden"] .helmet {
  display: none;
}

.hero-face {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  background: linear-gradient(145deg, #e8c4a0 0%, #d4a574 40%, #c4956a 100%);
  box-shadow: inset -3px -3px 8px rgba(0, 0, 0, 0.15), inset 3px 3px 8px rgba(255, 255, 255, 0.4), 0 4px 12px rgba(0, 0, 0, 0.2);
}

.hero-hair {
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 50px;
  height: 14px;
  background: #1a1510;
  border-radius: 50% 50% 0 0 / 100% 100% 0 0;
  clip-path: ellipse(80% 50% at 50% 40%);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  z-index: 0;
}

.hero-mask {
  position: absolute;
  top: 18px;
  left: 50%;
  transform: translateX(-50%);
  width: 48px;
  height: 20px;
  background: linear-gradient(180deg, #1a1510 0%, #2c2416 100%);
  border-radius: 50% 50% 45% 45%;
  box-shadow: inset 0 2px 4px rgba(255, 255, 255, 0.1), 0 2px 4px rgba(0, 0, 0, 0.3);
  z-index: 2;
}

.hero-eyes {
  display: flex;
  justify-content: center;
  gap: 14px;
  position: absolute;
  top: 22px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 3;
}

.hero-eyes .eye {
  width: 8px;
  height: 8px;
  background: #fff;
  border-radius: 50%;
  box-shadow: 0 0 10px rgba(255, 255, 255, 0.8);
  animation: eyeGlow 2s ease-in-out infinite alternate;
}

/* Helmet */
.helmet {
  width: 80px;
  height: 80px;
  background: var(--spaceman-helmet-bg);
  border-radius: 50%;
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  box-shadow: var(--spaceman-helmet-shadow);
  overflow: hidden;
  z-index: 3;
}

.visor {
  width: 60px;
  height: 45px;
  background: var(--spaceman-visor-bg);
  border-radius: 30px 30px 25px 25px;
  position: absolute;
  top: 15px;
  left: 50%;
  transform: translateX(-50%);
  overflow: hidden;
  box-shadow: inset 0 0 20px rgba(0, 0, 0, 0.5);
}

.visor-reflection {
  position: absolute;
  top: 5px;
  left: 5px;
  width: 20px;
  height: 10px;
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.4), transparent);
  border-radius: 50%;
}

.eyes {
  display: flex;
  justify-content: center;
  gap: 12px;
  margin-top: 15px;
}

.eye {
  width: 8px;
  height: 8px;
  background: #fff;
  border-radius: 50%;
  box-shadow: 0 0 10px rgba(255, 255, 255, 0.8);
  animation: eyeGlow 2s ease-in-out infinite alternate;
}

/* Torso */
.torso {
  width: 60px;
  height: 55px;
  background: var(--spaceman-torso-bg);
  border-radius: 15px 15px 20px 20px;
  position: absolute;
  top: 70px;
  left: 50%;
  transform: translateX(-50%);
  box-shadow:
    inset -3px 0 10px rgba(0, 0, 0, 0.1),
    0 5px 15px rgba(0, 0, 0, 0.2);
  z-index: 2;
}

[data-theme="garden"] .torso {
  width: 48px;
  height: 50px;
  top: 68px;
  border-radius: 12px 12px 16px 16px;
}

.chest-panel {
  width: 30px;
  height: 20px;
  background: var(--spaceman-chest-panel-bg);
  border-radius: 5px;
  position: absolute;
  top: 15px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  justify-content: center;
  gap: 8px;
  padding-top: 5px;
}

.light {
  width: 6px;
  height: 6px;
  border-radius: 50%;
}

.light-1 {
  background: #4ade80;
  box-shadow: 0 0 8px #4ade80;
  animation: lightPulse 1s ease-in-out infinite;
}

.light-2 {
  background: #60a5fa;
  box-shadow: 0 0 8px #60a5fa;
  animation: lightPulse 1.5s ease-in-out infinite 0.5s;
}

[data-theme="garden"] .chest-panel {
  width: 28px;
  height: 22px;
  border-radius: 50% 50% 45% 45%;
  border: 2px solid #b45309;
  box-shadow: inset 0 2px 4px rgba(255, 255, 255, 0.4), 0 2px 6px rgba(0, 0, 0, 0.2);
  top: 12px;
}

[data-theme="garden"] .chest-panel .light {
  width: 4px;
  height: 4px;
  background: #1a1a2e;
  box-shadow: none;
  animation: none;
}

/* Arms */
.arm {
  width: 30px;
  height: 14px;
  background: var(--spaceman-arm-bg);
  border-radius: 7px;
  position: absolute;
  top: 85px;
  box-shadow:
    0 2px 4px rgba(0, 0, 0, 0.2),
    inset 0 -2px 4px rgba(0, 0, 0, 0.1);
  z-index: 1;
}

[data-theme="garden"] .arm {
  width: 22px;
  height: 12px;
  top: 82px;
  border-radius: 5px;
}

.left-arm {
  left: 5px;
  transform: rotate(-5deg);
  transform-origin: right center;
  animation: leftArmFloat 6s ease-in-out infinite;
}

.right-arm {
  right: 5px;
  transform: rotate(5deg);
  transform-origin: left center;
  animation: rightArmFloat 6s ease-in-out infinite;
}

/* ===== JETPACK WITH FLAMES (BEHIND BODY) ===== */

.jetpack {
  position: absolute;
  top: 80px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 0;  /* Behind torso and legs */
}

.pack {
  width: 40px;
  height: 50px;
  background: var(--spaceman-pack-bg);
  border-radius: 8px 8px 5px 5px;
  box-shadow: 
    inset 2px 0 4px rgba(255, 255, 255, 0.1),
    0 5px 15px rgba(0, 0, 0, 0.4);
  position: relative;
}

/* Jetpack nozzles */
.pack::before,
.pack::after {
  content: '';
  position: absolute;
  bottom: -8px;
  width: 12px;
  height: 10px;
  background: var(--spaceman-pack-nozzle-bg);
  border-radius: 2px 2px 4px 4px;
}

.pack::before {
  left: 6px;
}

.pack::after {
  right: 6px;
}

/* Flames container - positioned at jetpack nozzles */
.flames-container {
  position: absolute;
  top: 52px;  /* Just below the pack nozzles */
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 16px;
  z-index: -1;  /* Behind everything */
}

.flame {
  width: 14px;
  height: 40px;
  position: relative;
}

.flame-inner {
  width: 100%;
  height: 100%;
  background: var(--spaceman-flame-bg);
  border-radius: 50% 50% 50% 50% / 20% 20% 80% 80%;
  animation: flameFlicker 0.12s ease-in-out infinite alternate;
  filter: blur(0.5px);
  box-shadow: var(--spaceman-flame-shadow);
}

/* ===== HERO CAPE (garden theme – replaces thrust/jetpack) ===== */
.hero-cape {
  display: none;
  position: absolute;
  top: 70px;
  left: 50%;
  transform: translateX(-50%);
  width: 80px;
  height: 140px;
  z-index: 0;
  pointer-events: none;
}

.hero-cape .cape-cloth {
  width: 100%;
  height: 100%;
  background: var(--hero-cape-bg, linear-gradient(180deg, #b91c1c, #991b1b));
  border-radius: 0 0 50% 50% / 0 0 30% 30%;
  box-shadow:
    inset -10px 0 20px rgba(0, 0, 0, 0.2),
    4px 8px 16px rgba(0, 0, 0, 0.25);
  transform-origin: top center;
}

[data-theme="garden"] .hero-cape {
  display: block;
  width: 55px;
  height: 100px;
  top: 66px;
  left: 50%;
  transform: translateX(-50%);
}

[data-theme="garden"] .hero-cape .cape-cloth {
  background: var(--hero-cape-bg);
  animation: capeFlow 4s ease-in-out infinite;
}

[data-theme="garden"] .jetpack .pack,
[data-theme="garden"] .jetpack .flames-container {
  display: none !important;
}

[data-theme="garden"] .spaceman.boost .hero-cape .cape-cloth {
  animation: capeBoost 0.6s ease-out;
}

@keyframes capeFlow {
  0%, 100% { transform: scaleY(1) skewX(1deg); }
  50% { transform: scaleY(1.01) skewX(-1.5deg); }
}

@keyframes capeBoost {
  0% { transform: scaleY(1) scaleX(1); }
  40% { transform: scaleY(1.25) scaleX(1.1); }
  100% { transform: scaleY(1) scaleX(1); }
}

/* Legs */
.legs {
  position: absolute;
  top: 125px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 8px;
  z-index: 2;
  filter: var(--spaceman-legs-glow);
}

.leg {
  width: 18px;
  height: 45px;
  background: var(--spaceman-leg-bg);
  border-radius: 8px 8px 12px 12px;
  box-shadow:
    inset -2px 0 4px rgba(0, 0, 0, 0.1),
    0 3px 6px rgba(0, 0, 0, 0.2);
}

[data-theme="garden"] .leg {
  width: 14px;
  height: 42px;
  border-radius: 6px 6px 10px 10px;
}

[data-theme="garden"] .legs {
  top: 118px;
}

.left-leg {
  transform: rotate(-4deg);
  transform-origin: top center;
  animation: leftLegFloat 6s ease-in-out infinite;
}

.right-leg {
  transform: rotate(4deg);
  transform-origin: top center;
  animation: rightLegFloat 6s ease-in-out infinite;
}

/* ===== ANIMATIONS ===== */
@keyframes float {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-5px); }
}

@keyframes boost {
  0% { transform: translateY(0) scale(1); }
  30% { transform: translateY(-12px) scale(1.02); }
  100% { transform: translateY(0) scale(1); }
}

@keyframes bubblePulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.005); }
}

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

@keyframes eyeGlow {
  0% { box-shadow: 0 0 5px rgba(255, 255, 255, 0.5); }
  100% { box-shadow: 0 0 15px rgba(255, 255, 255, 1); }
}

@keyframes lightPulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.5; transform: scale(0.8); }
}

@keyframes leftArmFloat {
  0%, 100% { transform: rotate(-5deg); }
  50% { transform: rotate(-10deg); }
}

@keyframes rightArmFloat {
  0%, 100% { transform: rotate(5deg); }
  50% { transform: rotate(10deg); }
}

@keyframes leftLegFloat {
  0%, 100% { transform: rotate(-4deg); }
  50% { transform: rotate(-2deg); }
}

@keyframes rightLegFloat {
  0%, 100% { transform: rotate(4deg); }
  50% { transform: rotate(2deg); }
}

@keyframes flameFlicker {
  0% { 
    transform: scaleY(1) scaleX(1);
    opacity: 0.95;
  }
  100% { 
    transform: scaleY(1.15) scaleX(0.9);
    opacity: 1;
  }
}

/* State change animation */
.spaceman.state-change {
  animation: stateChange 0.5s ease-out;
}

@keyframes stateChange {
  0% { transform: scale(1) rotate(0deg); }
  25% { transform: scale(1.05) rotate(-2deg); }
  75% { transform: scale(1.05) rotate(2deg); }
  100% { transform: scale(1) rotate(0deg); }
}

/* Boost animation - bigger flames */
.spaceman.boost .flame {
  height: 60px;
}

.spaceman.boost .flame-inner {
  animation: flameBoost 0.1s ease-in-out infinite alternate;
}

@keyframes flameBoost {
  0% { transform: scaleY(1.2) scaleX(1.1); }
  100% { transform: scaleY(1.5) scaleX(0.95); }
}

/* ===== RESTORED ANIMATIONS ===== */

/* Blink */
.eye.blink {
  animation: blinkEye 0.15s ease-in-out;
}

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

/* Wave */
.spaceman.wave .right-arm {
  animation: waveHello 0.5s ease-in-out 3;
}

@keyframes waveHello {
  0%, 100% { transform: rotate(10deg); }
  50% { transform: rotate(-45deg); }
}

/* ===== SPACEMAN POSITIONING & MOVEMENT ===== */

#spacemanContainer {
  position: fixed;
  top: 50%;
  left: 50%;
  /* CSS always centers; JS only changes variables */
  transform: translate(-50%, -50%)
             translate(var(--sx, 0px), var(--sy, 0px))
             scale(var(--ss, 1));
  transform-origin: center center;
  z-index: 50;
  pointer-events: none;
  transition: transform 0.5s ease-out;
  will-change: transform;
}

#spacemanContainer .spaceman {
  pointer-events: auto;  /* But spaceman itself is clickable */
}

/* Movement state - boost flames */
.spaceman.thrust .flame {
  height: 50px;
}

.spaceman.thrust .flame-inner {
  animation: thrustFlame 0.1s ease-in-out infinite alternate;
}

@keyframes thrustFlame {
  0% { transform: scaleY(1.3) scaleX(1); }
  100% { transform: scaleY(1.6) scaleX(0.85); }
}

/* Moving state - slight tilt */
.spaceman.moving {
  animation: moveTilt 0.5s ease-out;
}

@keyframes moveTilt {
  0% { filter: none; }
  50% { filter: drop-shadow(0 0 20px rgba(251, 146, 60, 0.5)); }
  100% { filter: none; }
}

/* ===== Z-INDEX MANAGEMENT ===== */

/* Lower spaceman when content is open to allow clicking content */
body.content-open #spacemanContainer {
  z-index: 10;
}

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

/* Mobile: single scale from JS (--ss) - no extra body scale to avoid double scaling and misaligned bounds */
@media (max-width: 767px) {
  .thought-bubble {
    max-width: 200px;
    padding: 10px 14px;
    font-size: 12px;
  }

  /* Gentler float so movement stays within safe area on small screens */
  .spaceman {
    animation: floatMobile 6s ease-in-out infinite;
  }
}

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

/* Small mobile */
@media (max-width: 480px) {
  .thought-bubble {
    max-width: 160px;
    padding: 8px 12px;
    font-size: 11px;
  }

  .thought-tail {
    margin-left: -28px;
  }

  .dot-1 { width: 10px; height: 10px; }
  .dot-2 { width: 8px; height: 8px; margin-left: 8px; }
  .dot-3 { width: 6px; height: 6px; margin-left: 14px; }
}

/* Touch devices - disable hover effects */
@media (hover: none) {
  .spaceman:hover {
    transform: none;
  }

  .spaceman:active {
    transform: scale(0.95);
  }
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
  .spaceman,
  .eye,
  .arm,
  .leg,
  .flame-inner,
  .thought-bubble,
  .hero-cape .cape-cloth,
  #spacemanContainer {
    animation: none !important;
  }

  #spacemanContainer {
    transition: transform 0.1s ease-out;
  }
}
