/* The battle stage (src/ui/stage_view.js): the hero, left, facing the monster,
   right, over a backdrop. The stage keeps the shape backdrops are painted for,
   --aspect, unless that would make it taller than about half the screen, and
   the fighters stand on the backdrop's floor line, --floor (src/backdrops.js),
   each over its own shadow. Its colours are its own and stay the same in
   light and dark pages, like a picture. --dir is the way a side faces, so one
   set of keyframes moves both. Adapted from Loop of the Recursive Dragon. */

/* The frame is what the stage measures itself against: 100cqw is its width. */
.stage-frame { container-type: inline-size; }

.stage {
  --aspect: 2.4; /* the stage's shape, width over height */
  --art-aspect: 2.4; /* the backdrops' shape */
  --floor: 0.9;
  --stage-h: min(100cqw / var(--aspect), max(240px, 46vh));
  /* A fighter stands a bit under half the stage's height; a tier-4 monster, 1.15 times that. */
  --sprite-h: calc(var(--stage-h) * 0.44);
  --ink: #1b2333;
  box-sizing: border-box; /* the height includes the padding, whatever the page around it does */
  position: relative;
  isolation: isolate;
  overflow: hidden;
  height: var(--stage-h);
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  padding: 12px 16px;
  /* Puts the fighters' feet on the floor line. The backdrop covers the stage
     from the bottom up, so when the stage is shorter than the art, the art's
     floor is that much further up. */
  padding-bottom: calc((1 - var(--floor)) * max(var(--stage-h), 100cqw / var(--art-aspect)));
  border-radius: 16px;
  background: #24324c;
  color: var(--ink);
}

.stage-backdrop {
  position: absolute;
  inset: 0;
  z-index: -1;
  background-size: cover;
  background-position: center bottom;
  filter: saturate(0.9);
}
/* A little haze in the distance and a soft vignette, so the backdrop sits
   back and the fighters, in the middle, come forward. */
.stage-backdrop::after {
  content: "";
  position: absolute;
  inset: 0;
  background:
    linear-gradient(to bottom, rgb(255 255 255 / 0.14), rgb(255 255 255 / 0) 45%),
    radial-gradient(ellipse 75% 85% at 50% 75%, rgb(0 0 0 / 0) 60%, rgb(16 22 40 / 0.32));
}

.combatant { display: flex; flex-direction: column; gap: 6px; min-width: 0; min-height: 0; }
.combatant[hidden] { display: none; } /* once every monster is beaten */
.combatant--hero { --dir: 1; }
.combatant--monster { --dir: -1; }

.plate {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
  padding: 4px 10px;
  border-radius: 10px;
  background: rgb(255 255 255 / 0.9);
  font-weight: 700;
}
.plate-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.plate-level { flex: none; font-size: 0.82em; color: #4b5563; }

.hp {
  position: relative;
  height: 18px;
  border-radius: 9px;
  background: rgb(255 255 255 / 0.8);
  border: 2px solid rgb(27 35 51 / 0.85);
  overflow: hidden;
}
.hp-fill {
  position: relative; /* over the lag */
  height: 100%;
  width: calc(var(--hp, 1) * 100%);
  background: #22c55e;
  transition: width 0.3s ease, background-color 0.3s ease;
}
/* What a blow just took off, in red, catching up with the bar a moment later. */
.hp-lag {
  position: absolute;
  inset: 0 auto 0 0;
  width: calc(var(--hp, 1) * 100%);
  background: #f87171;
  transition: width 0.45s ease 0.35s;
}
.hp[data-level="mid"] .hp-fill { background: #eab308; }
.hp[data-level="low"] .hp-fill { background: #ef4444; }
.hp-text {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-size: 12px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

/* The rest of the stage, down to the floor line. Each fighter stands toward the middle. */
.sprite-slot { position: relative; flex: 1; min-height: 0; }
.actor {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: flex-end;
  transform-origin: 50% 100%;
}
.combatant--hero .actor { justify-content: flex-end; padding-right: 6%; }
.combatant--monster .actor { justify-content: flex-start; padding-left: 6%; }

.figure {
  position: relative;
  max-width: 100%;
  scale: var(--size, 1);
  transform-origin: 50% 100%;
}
/* Where the fighter meets the ground, so it stands on the floor rather than in front of it. */
.shadow {
  position: absolute;
  left: 50%;
  bottom: calc(var(--sprite-h) * -0.05);
  width: 78%;
  height: calc(var(--sprite-h) * 0.16);
  translate: -50% 0;
  border-radius: 50%;
  background: radial-gradient(closest-side, rgb(20 24 38 / 0.42), rgb(20 24 38 / 0));
}
.figure:has(.sprite:not(.is-ready)) .shadow { opacity: 0; }

.sprite {
  position: relative; /* over its shadow */
  display: block;
  height: var(--sprite-h);
  width: calc(var(--sprite-h) * var(--frame-aspect, 1));
  background-repeat: no-repeat;
  background-size: 300% 100%;
  background-position: 0 0;
  opacity: 0; /* until the strip has loaded and its shape is known */
}
.sprite.is-ready { opacity: 1; }
.sprite[data-pose="attack"] { background-position: 50% 0; }
.sprite[data-pose="recoil"] { background-position: 100% 0; }
.combatant--monster .sprite { scale: -1 1; }

/* For screen readers only: each round in words. */
.stage-news {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.float-text {
  position: absolute;
  left: 50%;
  top: 20%;
  z-index: 3;
  font-size: 1.6rem;
  font-weight: 800;
  white-space: nowrap;
  pointer-events: none;
  color: #fff;
  text-shadow: 0 2px 0 var(--ink), 2px 0 0 var(--ink), -2px 0 0 var(--ink), 0 -2px 0 var(--ink), 0 4px 6px rgb(0 0 0 / 0.4);
  animation: float-up 0.9s ease-out forwards;
}
.float-text--hit { color: #fde047; font-size: 2.1rem; }
.float-text--hurt { color: #fca5a5; font-size: 2.1rem; }
.float-text--heal { color: #86efac; }
.float-text--gold { color: #fcd34d; }
.float-text--item { font-size: 2.4rem; text-shadow: none; }
/* A blow on a streak. */
.float-text--big { font-size: 2.8rem; color: #fb923c; animation-name: float-pop; }

@keyframes float-up {
  0% { opacity: 1; transform: translate(-50%, 0) scale(1.5); }
  20% { opacity: 1; transform: translate(-50%, -8px) scale(1); }
  60% { opacity: 1; transform: translate(-50%, -30px) scale(1); }
  100% { opacity: 0; transform: translate(-50%, -52px) scale(0.9); }
}
@keyframes float-pop {
  0% { opacity: 1; transform: translate(-50%, 0) scale(1.8) rotate(-6deg); }
  25% { opacity: 1; transform: translate(-50%, -10px) scale(1) rotate(3deg); }
  60% { opacity: 1; transform: translate(-50%, -30px) scale(1.05) rotate(0); }
  100% { opacity: 0; transform: translate(-50%, -56px) scale(0.9); }
}

/* A streak's strike, named across the middle of the stage. */
.stage-callout {
  position: absolute;
  left: 50%;
  top: 34%;
  z-index: 3;
  white-space: nowrap;
  pointer-events: none;
  font-size: clamp(1.5rem, 7cqw, 2.4rem);
  font-weight: 900;
  letter-spacing: 0.02em;
  color: #fde047;
  text-shadow: 0 3px 0 #c2410c, 2px 0 0 var(--ink), -2px 0 0 var(--ink), 0 -2px 0 var(--ink), 0 5px 10px rgb(0 0 0 / 0.45);
  translate: -50% 0;
  animation: callout-pop 900ms ease-out forwards;
}
@keyframes callout-pop {
  0% { opacity: 0; transform: scale(0.4) rotate(-8deg); }
  18% { opacity: 1; transform: scale(1.15) rotate(-3deg); }
  32% { transform: scale(1) rotate(-3deg); }
  75% { opacity: 1; transform: scale(1) rotate(-3deg); }
  100% { opacity: 0; transform: translateY(-14px) scale(1.05) rotate(-3deg); }
}

/* Where a blow lands: a spiky burst of light, over the middle of the one hit. */
.impact {
  position: absolute;
  z-index: 2;
  width: calc(var(--sprite-h) * 0.62);
  aspect-ratio: 1;
  translate: -50% -50%;
  pointer-events: none;
  clip-path: polygon(50% 0%, 59.2% 27.8%, 85.4% 14.6%, 72.2% 40.8%, 100% 50%, 72.2% 59.2%, 85.4% 85.4%, 59.2% 72.2%,
    50% 100%, 40.8% 72.2%, 14.6% 85.4%, 27.8% 59.2%, 0% 50%, 27.8% 40.8%, 14.6% 14.6%, 40.8% 27.8%);
  background: radial-gradient(circle, #fff 0 22%, #fde047 45%, #f97316 100%);
  animation: impact-burst 320ms ease-out forwards;
}
.impact--big { width: calc(var(--sprite-h) * 0.95); }
@keyframes impact-burst {
  0% { opacity: 1; transform: scale(0.3) rotate(0deg); }
  45% { opacity: 1; transform: scale(1.05) rotate(12deg); }
  100% { opacity: 0; transform: scale(1.25) rotate(20deg); }
}

/* Sparks flying out from the middle of a fighter, each its own way (--angle) and distance (--reach). */
.spark {
  position: absolute;
  z-index: 2;
  width: 11px;
  height: 11px;
  border-radius: 50%;
  translate: -50% -50%;
  pointer-events: none;
  background: var(--spark, #fde047);
  box-shadow: 0 0 8px 2px rgb(251 146 60 / 0.8);
  animation: spark-fly 650ms ease-out forwards;
}
@keyframes spark-fly {
  from { opacity: 1; transform: rotate(var(--angle)) translateY(0) scale(1.2); }
  to { opacity: 0; transform: rotate(var(--angle)) translateY(calc(var(--sprite-h) * var(--reach) * -1)) scale(0.3); }
}

/* The stage shakes with a blow, and glows red at its edges when the hero is hit. */
@keyframes stage-shake {
  0%, 100% { transform: none; }
  20% { transform: translate(-4px, 2px); }
  45% { transform: translate(4px, -2px); }
  70% { transform: translate(-2px, 1px); }
}
@keyframes stage-quake {
  0%, 100% { transform: none; }
  15% { transform: translate(-9px, 4px) rotate(-0.4deg); }
  35% { transform: translate(8px, -4px) rotate(0.4deg); }
  55% { transform: translate(-5px, 2px); }
  75% { transform: translate(3px, -1px); }
}
.stage.is-shaking { animation: stage-shake 260ms ease-out; }
.stage.is-quaking { animation: stage-quake 420ms ease-out; }
.stage::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 4;
  border-radius: inherit;
  pointer-events: none;
  box-shadow: inset 0 0 50px 14px rgb(239 68 68 / 0.8);
  opacity: 0;
}
.stage.is-hurt::after { animation: hurt-flash 450ms ease-out; }
@keyframes hurt-flash {
  0%, 100% { opacity: 0; }
  25% { opacity: 1; }
}
@keyframes stage-lunge {
  0% { transform: translateX(0); }
  40% { transform: translateX(calc(var(--dir) * 48px)); }
  65% { transform: translateX(calc(var(--dir) * 40px)); }
  100% { transform: translateX(0); }
}
@keyframes stage-recoil {
  0% { transform: translateX(0); filter: none; }
  15% { transform: translateX(calc(var(--dir) * -14px)); filter: brightness(3) saturate(0); }
  40% { transform: translateX(calc(var(--dir) * -10px)); filter: none; }
  55% { filter: brightness(2) saturate(0); }
  100% { transform: translateX(0); filter: none; }
}
@keyframes stage-vanish {
  0% { opacity: 1; transform: none; filter: none; }
  20% { opacity: 1; transform: translateX(calc(var(--dir) * -8px)); filter: brightness(4) saturate(0); }
  100% { opacity: 0; transform: translateX(calc(var(--dir) * -14px)) scaleY(0.3); filter: brightness(2) saturate(0) blur(1px); }
}
@keyframes stage-down {
  0% { transform: none; filter: none; }
  30% { transform: translateX(calc(var(--dir) * -10px)) rotate(calc(var(--dir) * -20deg)); }
  100% { transform: translateX(calc(var(--dir) * -16px)) rotate(calc(var(--dir) * -85deg)); filter: grayscale(1) brightness(0.7); }
}
@keyframes stage-revive {
  0% { transform: translateX(calc(var(--dir) * -16px)) rotate(calc(var(--dir) * -85deg)); filter: grayscale(1) brightness(0.7); }
  55% { transform: none; filter: brightness(1.5) drop-shadow(0 0 12px #fcd34d); }
  100% { transform: none; filter: none; }
}
@keyframes stage-enter {
  from { opacity: 0; transform: translateX(calc(var(--dir) * -60px)); }
  to { opacity: 1; transform: none; }
}
@keyframes stage-glow {
  0%, 100% { filter: none; }
  35% { filter: brightness(1.4) drop-shadow(0 0 12px var(--glow)); }
}

.actor.is-lunging { animation: stage-lunge 420ms ease-out; }
.actor.is-lunging.is-charged { animation: stage-lunge 420ms ease-out, stage-glow 420ms ease-out; --glow: #fb923c; }
.actor.is-hit { animation: stage-recoil 380ms ease-out; }
.actor.is-vanquished { animation: stage-vanish 650ms ease-in forwards; }
.actor.is-down { animation: stage-down 550ms ease-in forwards; }
.actor.is-reviving { animation: stage-revive 700ms ease-out; }
.actor.is-entering { animation: stage-enter 400ms ease-out; }
.actor.is-glowing { animation: stage-glow 700ms ease-out; }
.actor[data-tone="heal"] { --glow: #4ade80; }
.actor[data-tone="gold"] { --glow: #fcd34d; }

/* A narrow stage, wherever it is: a phone, or a small panel. */
@container (max-width: 560px) {
  .stage { --aspect: 1.8; gap: 8px; padding-left: 10px; padding-right: 10px; padding-top: 10px; }
  .plate { padding: 3px 7px; font-size: 0.9rem; }
}

@media (prefers-reduced-motion: reduce) {
  .actor, .float-text, .stage, .stage::after { animation: none !important; }
  .impact, .spark, .stage-callout { display: none; }
  .hp-fill, .hp-lag { transition: none; }
}
