.character-companions {
  position: absolute;
  inset: 0;
  z-index: 6;
  pointer-events: none;
  user-select: none;
  opacity: 0;
  transition: opacity 240ms ease;
}

.character-companions.is-ready {
  opacity: 1;
}

.character-companions.is-unavailable {
  display: none;
}

.character-slot {
  position: absolute;
  top: 66.7%;
  width: 31.5%;
  height: 20%;
}

/* Keep the same composition as the original static art: Xu Xuan left,
   Bai Suzhen right. */
.character-slot--male {
  left: 1.5%;
}

.character-slot--female {
  left: 67.5%;
}

.character-scale {
  position: absolute;
  left: 50%;
  bottom: 0;
  transform: translateX(-50%) scale(var(--companion-scale, 0.53));
  transform-origin: center bottom;
}

.character-slot--male {
  --companion-scale: 0.53;
}

.character-slot--female {
  --companion-scale: 0.60;
}

.character-figure {
  position: relative;
  transform-origin: center bottom;
  animation: companion-idle 4.4s ease-in-out infinite;
  will-change: transform, filter;
}

.character-figure--male {
  animation-delay: -2.1s;
}

.character-figure--female {
  top: 37px;
}

.character-layer {
  position: absolute;
  display: block;
  background-repeat: no-repeat;
  backface-visibility: hidden;
  transform: translateZ(0);
  will-change: transform;
}

.character-slot.is-blinking .character-layer--blink-face {
  background-image: var(--closed-face) !important;
  background-position: 0 0 !important;
  background-size: 100% 100% !important;
}

.character-layer--hair {
  transform-origin: 50% 92%;
  animation: companion-hair-sway 4.6s ease-in-out infinite;
}

.character-layer--ribbon,
.character-layer--cloth {
  transform-origin: 50% 18%;
  animation: companion-cloth-sway 3.8s ease-in-out infinite;
}

.character-layer--arm {
  transform-origin: 50% 12%;
  animation: companion-arm-breathe 4.4s ease-in-out infinite;
}

.character-slot--male .character-layer--hair,
.character-slot--male .character-layer--cloth {
  animation-delay: -1.7s;
}

.character-slot::before,
.character-slot::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 24%;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: #f8df86;
  box-shadow:
    -34px 16px 0 -3px #9fe7ef,
    30px -10px 0 -2px #f7aec8,
    45px 24px 0 -4px #ffffff;
  opacity: 0;
  transform: translate(-50%, 18px) scale(0.3);
}

.character-companions.is-moving .character-slot.is-active .character-figure {
  animation: companion-hop 420ms ease-out;
}

.character-companions.is-merging .character-figure {
  animation: companion-cheer 760ms cubic-bezier(0.2, 0.8, 0.2, 1);
}

.character-companions.is-merging .character-slot::before,
.character-companions.is-winning .character-slot::before,
.character-companions.is-winning .character-slot::after {
  animation: companion-sparkle 820ms ease-out both;
}

.character-companions.is-winning .character-figure {
  animation: companion-celebrate 1.15s ease-in-out infinite;
  filter: drop-shadow(0 0 10px rgba(248, 223, 134, 0.65));
}

.character-companions.is-winning .character-slot::after {
  animation-delay: 280ms;
}

.character-companions.is-losing .character-figure {
  animation: companion-sad 620ms ease-out both;
  filter: saturate(0.65) brightness(0.82);
}

@keyframes companion-idle {
  0%, 100% { transform: translateY(0) scaleY(1); }
  50% { transform: translateY(-4px) scaleY(1.008); }
}

@keyframes companion-hair-sway {
  0%, 100% { transform: rotate(-0.8deg); }
  50% { transform: rotate(1deg); }
}

@keyframes companion-cloth-sway {
  0%, 100% { transform: rotate(-1.2deg); }
  50% { transform: rotate(1.4deg); }
}

@keyframes companion-arm-breathe {
  0%, 100% { transform: rotate(-0.4deg); }
  50% { transform: rotate(0.7deg); }
}

@keyframes companion-hop {
  0%, 100% { transform: translateY(0) rotate(0); }
  42% { transform: translateY(-12px) rotate(-2deg); }
  70% { transform: translateY(-2px) rotate(1deg); }
}

@keyframes companion-cheer {
  0%, 100% { transform: translateY(0) scale(1); }
  34% { transform: translateY(-18px) scale(1.04, 0.98); }
  58% { transform: translateY(1px) scale(0.98, 1.03); }
  78% { transform: translateY(-7px) scale(1.01); }
}

@keyframes companion-celebrate {
  0%, 100% { transform: translateY(0) rotate(-1deg); }
  50% { transform: translateY(-14px) rotate(1deg); }
}

@keyframes companion-sad {
  0% { transform: translateY(0) rotate(0); }
  100% { transform: translateY(8px) rotate(-3deg) scaleY(0.97); }
}

@keyframes companion-sparkle {
  0% { opacity: 0; transform: translate(-50%, 18px) scale(0.3); }
  35% { opacity: 1; }
  100% { opacity: 0; transform: translate(-50%, -46px) scale(1.15); }
}

@media (prefers-reduced-motion: reduce) {
  .character-figure,
  .character-layer,
  .character-slot::before,
  .character-slot::after {
    animation: none !important;
    transition: none !important;
  }
}
