/* Agent scenes: how figures move around a page. The figures themselves are
   in css/agents.css; the scenes are mounted by
   js/agents/agent-scene-library.js.
   A mirrored scene (agent-scene--mirrored: right-to-left pages, or
   data-agent-flip) plays the other way round: positions are measured from
   the other side and the drawings are flipped. */

/* ── Scene: highlight ────────────────────────────────── */
/* The track covers the same span as the .highlight stroke. Its far edge is
   the tip of the stroke, and the figure hangs off that edge with its marker
   tip on it. Sliding the track draws the agent along the phrase while the
   stroke is revealed at the same rate. The figure is as tall as the heading's
   font size, with a floor so it stays readable under small headings.
   Offsets: the stroke's centre line sits 0.23em above the phrase's box; the
   marker tip sits 13.75% up and 10% in from the figure's bottom corner. */
.agent-scene-highlight__track {
  --agent-size: max(1em, 52px);
  position: absolute;
  z-index: 1;
  left: -0.06em;
  right: -0.06em;
  bottom: calc(0.23em - var(--agent-size) * 0.1375);
  height: var(--agent-size);
  pointer-events: none;
}
.agent-scene-highlight__track .agent-figure {
  position: absolute;
  left: calc(100% - var(--agent-size) * 0.1);
  bottom: 0;
  width: calc(var(--agent-size) * 0.9);
  height: var(--agent-size);
}
.agent-scene--mirrored .agent-scene-highlight__track .agent-figure {
  left: auto;
  right: calc(100% - var(--agent-size) * 0.1);
  scale: -1 1;
}

.agent-scene--armed.highlight::after {
  clip-path: inset(-0.1em 100% -0.1em -0.1em);
}
.agent-scene--armed .agent-scene-highlight__track {
  transform: translateX(-100%);
}
.agent-scene--mirrored.agent-scene--armed.highlight::after {
  clip-path: inset(-0.1em -0.1em -0.1em 100%);
}
.agent-scene--mirrored.agent-scene--armed .agent-scene-highlight__track {
  transform: translateX(100%);
}

/* No room to stay at the end of the phrase: it leaves once it is done */
.agent-scene--done .agent-scene-highlight__track--leaves .agent-figure {
  opacity: 0;
  transition: opacity 0.4s ease-in 1.3s;
}

/* Stroke and track share one duration and one easing, so the marker tip
   stays on the end of the stroke for the whole walk */
.agent-scene--playing.highlight::after {
  animation: agent-stroke-draw var(--agent-walk-time) cubic-bezier(0.45, 0, 0.4, 1) both;
}
.agent-scene--playing .agent-scene-highlight__track {
  animation: agent-track-walk var(--agent-walk-time) cubic-bezier(0.45, 0, 0.4, 1) both;
}
.agent-scene--mirrored.agent-scene--playing.highlight::after { animation-name: agent-stroke-draw-mirrored; }
.agent-scene--mirrored.agent-scene--playing .agent-scene-highlight__track { animation-name: agent-track-walk-mirrored; }

@keyframes agent-stroke-draw {
  from { clip-path: inset(-0.1em 100% -0.1em -0.1em); }
  to { clip-path: inset(-0.1em 0 -0.1em -0.1em); }
}
@keyframes agent-track-walk {
  from { transform: translateX(-100%); }
  to { transform: translateX(0); }
}
@keyframes agent-stroke-draw-mirrored {
  from { clip-path: inset(-0.1em -0.1em -0.1em 100%); }
  to { clip-path: inset(-0.1em -0.1em -0.1em 0); }
}
@keyframes agent-track-walk-mirrored {
  from { transform: translateX(100%); }
  to { transform: translateX(0); }
}

/* ── Scene: ledge ────────────────────────────────────── */
/* The agent walks along the host's top edge, from --agent-from to --agent-to
   (percent of the host's width, set by the scene), and stops. Its feet are
   17.2% up from the bottom of its box; --agent-ledge-lift raises it onto a
   top border. Positions are measured from the near side of the track, which
   is the left, or the right when the scene is mirrored. */
.agent-scene-ledge { position: relative; }
.agent-scene-ledge__track {
  --agent-size: 60px;
  position: absolute;
  left: 0;
  right: 0;
  bottom: 100%;
  height: 0;
  direction: ltr;
  pointer-events: none;
}
.agent-scene--mirrored > .agent-scene-ledge__track { direction: rtl; }
.agent-scene-ledge__runner {
  position: absolute;
  inset-inline-start: var(--agent-from);
  bottom: calc(var(--agent-ledge-lift, 0px) - var(--agent-size) * 0.172);
  width: calc(var(--agent-size) * 0.9);
  height: var(--agent-size);
}
.agent-scene--mirrored .agent-scene-ledge__runner { scale: -1 1; }
.agent-scene-ledge__runner .agent-figure {
  width: 100%;
  height: 100%;
}
.agent-scene--playing .agent-scene-ledge__runner {
  animation: agent-ledge-walk var(--agent-walk-time) cubic-bezier(0.45, 0, 0.4, 1) var(--agent-entrance-delay, 0s) both;
}
.agent-scene--done .agent-scene-ledge__runner { inset-inline-start: var(--agent-to); }

@keyframes agent-ledge-walk {
  from { inset-inline-start: var(--agent-from); }
  to { inset-inline-start: var(--agent-to); }
}

/* A parade: each agent walks the same stretch, one place behind the agent
   in front and a beat later */
.agent-scene-ledge__runner--in-line {
  --agent-entrance-delay: calc(var(--agent-rank) * 0.16s);
  margin-inline-start: calc(var(--agent-size) * -0.82 * var(--agent-rank));
}

/* Other ways onto the edge. All stay at --agent-to and move only up and
   down; the feet line is 17.2% above the bottom of the runner's box.
   Climbing: the runner rises from below the edge while a clip keeps
   everything under the feet line hidden. Shift and clip change together, so
   the clip edge stays on the line: up to the eyes, a look around, a duck,
   then all the way up. */
.agent-scene--playing .agent-scene-ledge__runner--climbs {
  animation: agent-ledge-climb 2.4s ease-in-out var(--agent-entrance-delay, 0s) both;
}
/* Dropping: falls in from above and bounces twice */
.agent-scene--playing .agent-scene-ledge__runner--drops {
  animation: agent-ledge-drop 1s linear var(--agent-entrance-delay, 0s) both;
}
/* Parachuting: drifts down, swaying about a point above the canopy */
.agent-scene-ledge__runner--parachutes { transform-origin: 50% -40%; }
.agent-scene--playing .agent-scene-ledge__runner--parachutes {
  animation: agent-ledge-parachute 3.4s ease-in-out var(--agent-entrance-delay, 0s) both;
}

@keyframes agent-ledge-climb {
  0% { transform: translateY(85%); clip-path: inset(-60% -60% 102.2% -60%); }
  22%, 48% { transform: translateY(30%); clip-path: inset(-60% -60% 47.2% -60%); }
  60% { transform: translateY(42%); clip-path: inset(-60% -60% 59.2% -60%); }
  100% { transform: translateY(0); clip-path: inset(-60% -60% 17.2% -60%); }
}
@keyframes agent-ledge-drop {
  0% { transform: translateY(-260%); animation-timing-function: cubic-bezier(0.5, 0, 1, 0.6); }
  50% { transform: translateY(0); animation-timing-function: ease-out; }
  66% { transform: translateY(-20%); animation-timing-function: ease-in; }
  82% { transform: translateY(0); animation-timing-function: ease-out; }
  91% { transform: translateY(-6%); animation-timing-function: ease-in; }
  100% { transform: translateY(0); }
}
@keyframes agent-ledge-parachute {
  0% { transform: translateY(-300%) rotate(-7deg); }
  25% { transform: translateY(-225%) rotate(6deg); }
  50% { transform: translateY(-150%) rotate(-6deg); }
  75% { transform: translateY(-75%) rotate(5deg); }
  100% { transform: translateY(0) rotate(0deg); }
}

/* Phones: a shorter figure, so it clears the line of text above a rule */
@media (max-width: 767px) {
  .agent-scene-ledge__track { --agent-size: 52px; }
}

/* ── Extras: what travels in the agent's own box ─────── */
/* Shown only with the agent they belong to. */
.agent-extra {
  position: absolute;
  opacity: 0;
}
.agent-figure.is-walking ~ .agent-extra,
.agent-figure.is-resting ~ .agent-extra { opacity: 1; }

/* The parachute hangs from the antenna and folds away on landing */
.agent-extra--chute {
  left: 50%;
  bottom: 77%;
  width: calc(var(--agent-size) * 0.8);
  height: calc(var(--agent-size) * 0.55);
  translate: -50% 0;
  transform-origin: 50% 100%;
  transition: opacity 0.25s ease-in 0.25s, transform 0.5s ease-in;
}
.agent-figure.is-resting ~ .agent-extra--chute {
  opacity: 0;
  transform: scaleY(0.15);
}

/* The ball is dribbled in front of the feet, then shot into the goal that
   stands 150 figure units from the agent's box */
.agent-extra--ball {
  left: 70%;
  bottom: 17.2%;
  width: calc(var(--agent-size) * 0.175);
  height: calc(var(--agent-size) * 0.175);
}
.agent-figure.is-walking ~ .agent-extra--ball {
  animation: agent-ball-dribble calc(var(--agent-step-time) * 2) linear infinite;
}
.agent-figure.is-resting ~ .agent-extra--ball {
  animation: agent-ball-shot 0.75s linear 0.3s both;
}
@keyframes agent-ball-dribble {
  0% { transform: translateX(0) rotate(0deg); }
  50% { transform: translateX(calc(var(--agent-size) * 0.1)) rotate(180deg); }
  100% { transform: translateX(0) rotate(360deg); }
}
@keyframes agent-ball-shot {
  0% { transform: translate(0, 0) rotate(0deg); animation-timing-function: ease-out; }
  45% { transform: translate(calc(var(--agent-size) * 0.62), calc(var(--agent-size) * -0.4)) rotate(300deg); animation-timing-function: ease-in; }
  100% { transform: translate(calc(var(--agent-size) * 1.4), 0) rotate(700deg); }
}

/* ── Scene: deliver-icons ────────────────────────────── */
/* Each agent carries a copy of a button's icon over its head (the cargo),
   walks in along the top of the first button and lets go. The real icon is
   hidden until its copy has landed on it. --cargo-dx / --cargo-dy are
   measured by the scene at the moment of the drop. The first agent drops its
   icon straight down; the ones behind it toss theirs in an arc. */
.agent-deliver__slot:not(.is-filled) > svg:first-child { opacity: 0; }
.agent-deliver__cargo {
  position: absolute;
  left: 50%;
  bottom: 84%;
  translate: -50% 0;
  transition: opacity 0.2s ease-out;
}
/* Flipped back inside a flipped runner, so the icon reads the right way */
.agent-scene--mirrored .agent-deliver__cargo { scale: -1 1; }
/* The cargo shows only with the agent that carries it, so an agent that
   arrives late does not leave its icon hanging in the air beforehand. */
.agent-deliver__cargo { opacity: 0; }
.agent-figure.is-walking ~ .agent-deliver__cargo,
.agent-figure.is-resting ~ .agent-deliver__cargo { opacity: 1; }
.agent-deliver__cargo.is-dropping {
  animation: agent-cargo-drop 0.5s cubic-bezier(0.5, 0, 0.9, 0.6) both;
}
.agent-deliver__cargo--tossed.is-dropping {
  animation: agent-cargo-toss 0.8s linear both;
}

@keyframes agent-cargo-drop {
  from { transform: translate(0, 0); }
  to { transform: translate(var(--cargo-dx), var(--cargo-dy)); }
}
@keyframes agent-cargo-toss {
  0% { transform: translate(0, 0) rotate(0deg); animation-timing-function: ease-out; }
  35% { transform: translate(calc(var(--cargo-dx) * 0.35), -22px) rotate(-120deg); animation-timing-function: ease-in; }
  100% { transform: translate(var(--cargo-dx), var(--cargo-dy)) rotate(-360deg); }
}

/* ── Scene: route (Business OS couriers) ─────────────── */
/* Each actor's feet follow four points given in percent of the host, the
   same units as the diagram's connector lines (viewBox 0 0 100 100). A still
   actor has one point and only waits for its cue. Sideways positions are
   measured from the side the page starts on, so on a right-to-left page,
   where the diagram's boxes swap sides, the routes swap with them. */
.agent-scene-route__actor {
  --agent-size: clamp(44px, 4.6vw, 56px);
  --route-time: 1.9s;
  --route-delay: 0s;
  position: absolute;
  z-index: 1;
  inset-inline-start: var(--route-x0);
  top: var(--route-y0);
  width: calc(var(--agent-size) * 0.9);
  height: var(--agent-size);
  transform: translate(-50%, -82.8%);
  pointer-events: none;
}
/* Only the routes swap sides; the figures are not flipped */
.agent-scene-route--swapped > .agent-scene-route__actor { transform: translate(50%, -82.8%); }
.agent-scene-route__actor .agent-figure {
  width: 100%;
  height: 100%;
}
.agent-scene--playing .agent-scene-route__actor {
  animation: agent-route-walk var(--route-time) linear var(--route-delay) both;
}
.agent-scene--playing .agent-scene-route__actor[data-agent-still] {
  animation: agent-cue 1ms linear var(--route-delay) both;
}
.agent-scene--done .agent-scene-route__actor {
  inset-inline-start: var(--route-x3);
  top: var(--route-y3);
}

/* Sales box, down its connector, to the left edge of the OS box */
.agent-scene-route__actor--sales-to-os {
  --route-x0: 27.5%; --route-y0: 14%;
  --route-x1: 30.4%; --route-y1: 19.6%;
  --route-x2: 31.4%; --route-y2: 32%;
  --route-x3: 31.2%; --route-y3: 45%;
}
/* Right edge of the OS box, down its connector, to the Finance box */
.agent-scene-route__actor--os-to-finance {
  --route-delay: 2.2s;
  --route-x0: 67%; --route-y0: 50%;
  --route-x1: 67.5%; --route-y1: 56%;
  --route-x2: 68%; --route-y2: 68%;
  --route-x3: 69%; --route-y3: 82%;
}
/* Standing beside "Your team", ticket held up for approval */
.agent-scene-route__actor--team-approval {
  --route-delay: 1.9s;
  --route-x0: 31%; --route-y0: 98%;
  --route-x3: 31%; --route-y3: 98%;
}

@keyframes agent-route-walk {
  0% { inset-inline-start: var(--route-x0); top: var(--route-y0); }
  28% { inset-inline-start: var(--route-x1); top: var(--route-y1); }
  64% { inset-inline-start: var(--route-x2); top: var(--route-y2); }
  100% { inset-inline-start: var(--route-x3); top: var(--route-y3); }
}
@keyframes agent-cue {
  from { opacity: 1; }
  to { opacity: 1; }
}
