* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --character-scale: 1;
}

body {
  margin: 0;
  height: 100vh;
  width: 100vw;
  background: radial-gradient(circle at top, #222 0, #050505 60%);
  font-family: system-ui, sans-serif;
  overflow: hidden;
}

/* world fills whole screen */
#world {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(0, 0, 0, 0.15);
  overflow: hidden;
}

/* info label */
#label {
  position: fixed;
  bottom: 8px;
  right: 12px;
  font-size: 10px;
  color: #bbb;
  opacity: 0.8;
  pointer-events: none;
  user-select: none;
}

/* base character container (positioned by JS) */
.character {
  position: absolute;
  width: calc(72px * var(--character-scale));
  height: calc(96px * var(--character-scale));
  transform-origin: center;
  touch-action: none;
  transition: opacity 0.2s ease;
  --skin-color: #d9b28c;
  z-index: 1;
}

.character.is-hidden {
  opacity: 0;
  pointer-events: none;
}

.character.was-zapped {
  filter: grayscale(1) blur(0.5px);
}

.character.is-scared {
  filter: drop-shadow(0 0 8px rgba(255, 96, 96, 0.5));
}

.character.is-royal {
  filter: drop-shadow(0 0 8px rgba(255, 215, 0, 0.45));
}

.character.is-royal::after {
  content: "";
  position: absolute;
  top: -16px;
  left: -16px;
  width: calc(100% + 32px);
  height: calc(100% + 32px);
  border-radius: 40%;
  background: radial-gradient(circle, rgba(255, 223, 88, 0.35), rgba(255, 223, 88, 0));
  animation: royalPulse 2s ease-in-out infinite;
  pointer-events: none;
  z-index: -1;
}

@keyframes royalPulse {
  0% {
    transform: scale(0.95);
    opacity: 0.6;
  }
  50% {
    transform: scale(1.05);
    opacity: 0.9;
  }
  100% {
    transform: scale(0.95);
    opacity: 0.6;
  }
}

.character.is-dragging .person {
  animation: drag-wobble 0.22s ease-in-out infinite;
}

.character.is-dragging .arm,
.character.is-dragging .leg {
  animation-duration: 0.18s;
}

@keyframes drag-wobble {
  0% {
    transform: translateX(-50%) scale(var(--character-scale)) rotate(-6deg);
  }
  100% {
    transform: translateX(-50%) scale(var(--character-scale)) rotate(6deg);
  }
}

.character.is-scared:not(.is-dragging) .person {
  animation: scared-lean 0.18s alternate infinite;
}

.character.is-scared .arm,
.character.is-scared .leg {
  animation-duration: 0.18s;
}

@keyframes scared-lean {
  0% {
    transform: translateX(-50%) scale(var(--character-scale)) skewX(-4deg);
  }
  100% {
    transform: translateX(-50%) scale(var(--character-scale)) skewX(-8deg);
  }
}

/* "person" sprite root */
.person {
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%) scale(var(--character-scale));
  transform-origin: bottom center;
}

/* head */
.head {
  position: relative;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: var(--skin-color, #d9b28c);
  margin: 0 auto 4px auto;
}

.crown {
  position: absolute;
  top: -16px;
  left: 50%;
  width: 28px;
  height: 14px;
  transform: translateX(-50%);
  background: linear-gradient(180deg, #ffe766 0%, #fabc05 100%);
  clip-path: polygon(
    0% 100%,
    8% 35%,
    20% 100%,
    32% 35%,
    44% 100%,
    56% 35%,
    68% 100%,
    80% 35%,
    92% 100%,
    100% 35%,
    100% 100%
  );
  border-radius: 2px 2px 0 0;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.35);
}

.crown::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 50%;
  width: 6px;
  height: 6px;
  transform: translateX(-50%);
  border-radius: 50%;
  background: #fff6c7;
  opacity: 0.9;
}

/* eyes */
.eye {
  position: absolute;
  top: 11px;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #fff;
  overflow: hidden;
}

.eye::after {
  content: "";
  position: absolute;
  top: 1px;
  left: 2px;
  width: 3px;
  height: 3px;
  border-radius: 50%;
  background: #000;
}

.eye.left {
  left: 7px;
}

.eye.right {
  right: 7px;
}

/* body/torso region */
.body {
  position: relative;
  width: 40px;
  height: 36px;
  margin: 0 auto;
}

/* torso color is driven by currentColor set on .character/.person */
.torso {
  position: absolute;
  top: 4px;
  left: 50%;
  transform: translateX(-50%);
  width: 32px;
  height: 26px;
  border-radius: 8px;
  background: currentColor;
  z-index: 1; /* torso behind name tag */
}

/* name tag overlayed on right chest */
.name-tag {
  position: absolute;
  top: 40px;      /* << changed to 40px */
  right: 2px;     /* sit on the right side of torso */
  padding: 1px 4px;
  font-size: 9px;
  line-height: 1.1;
  color: #f5f5f5;
  background: rgba(0, 0, 0, 0.65);
  border-radius: 6px;
  white-space: nowrap;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.7);
  pointer-events: none;
  z-index: 2;      /* draw over torso */
}

/* arms */
.arm {
  position: absolute;
  top: 6px;
  width: 12px;
  height: 6px;
  border-radius: 6px;
  background: currentColor;
  transform-origin: 0% 50%;
  animation: swing 0.3s alternate infinite;
  z-index: 2; /* arms above torso too */
}

.arm.right {
  right: -4px;
  transform-origin: 100% 50%;
  animation-delay: 0.15s;
}

.arm.left {
  left: -4px;
}

@keyframes swing {
  from {
    transform: rotate(20deg);
  }
  to {
    transform: rotate(-20deg);
  }
}

/* legs */
.legs {
  position: relative;
  width: 30px;
  height: 26px;
  margin: 0 auto;
  margin-top: 0;
}

.leg {
  position: absolute;
  bottom: 0;
  width: 10px;
  height: 22px;
  border-radius: 5px;
  background: #222;
  transform-origin: top center;
  animation: walk 0.25s alternate infinite;
}

.leg.left {
  left: 3px;
}

.leg.right {
  right: 3px;
  animation-delay: 0.125s;
}

@keyframes walk {
  from {
    transform: rotate(12deg);
  }
  to {
    transform: rotate(-12deg);
  }
}

/* speech bubble (global, not inside .character) */
.bubble {
  position: absolute;
  max-width: 260px;
  background: rgba(0, 0, 0, 0.85);
  border-radius: 10px;
  padding: 6px 8px;
  font-size: 11px;
  line-height: 1.25;
  color: #f5f5f5;
  pointer-events: none;
  white-space: normal;
  text-align: center;
  opacity: 0;
  z-index: 10;
  transform: translate(-50%, -110%); /* default: above */
  transition: opacity 0.18s ease-out;
}

.bubble::after {
  content: "";
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  width: 0;
  height: 0;
  border-left: 6px solid transparent;
  border-right: 6px solid transparent;
  border-top: 6px solid rgba(0, 0, 0, 0.85);
  bottom: -6px; /* tail pointing down by default */
}

.bubble.visible {
  opacity: 1;
}

.death-bubble {
  position: absolute;
  min-width: 60px;
  max-width: 160px;
  transform: translate(-50%, -30%) scale(0.9);
  padding: 4px 8px;
  border-radius: 999px;
  font-size: 11px;
  text-align: center;
  color: #fff;
  background: rgba(0, 0, 0, 0.8);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 12;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.4);
}

.death-bubble.visible {
  opacity: 1;
  transform: translate(-50%, -110%) scale(1);
}

.death-bubble.cause-royal {
  background: linear-gradient(135deg, rgba(255, 199, 44, 0.95), rgba(255, 91, 53, 0.9));
  color: #2b1300;
  font-weight: 600;
}

.death-bubble.cause-user {
  background: rgba(74, 144, 226, 0.95);
}

.confetti-poof {
  position: absolute;
  width: 0;
  height: 0;
  pointer-events: none;
  z-index: 15;
}

.confetti-poof span {
  position: absolute;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #ff3b30;
  opacity: 0.95;
  transform: translate(-50%, -50%);
  animation: confetti-pop 0.65s ease-out forwards;
  animation-delay: var(--delay, 0ms);
}

@keyframes confetti-pop {
  0% {
    transform: translate(-50%, -50%) scale(0.3);
    opacity: 1;
  }
  100% {
    transform: translate(calc(-50% + var(--dx, 0)), calc(-50% + var(--dy, 0))) scale(0);
    opacity: 0;
  }
}
