/* Pip, the Playground's buddy (frontend/src/playground/buddy-view.ts draws it, buddy.ts decides what it says): a small
   robot docked under the board, its speech bubble to its right (to its left on a phone), a Hide button, and Show Pip once
   hidden. The geometry, moods and motion are the approved mock's (buddy canvas: Main and InContext), in style.css's
   tokens. Moods are classes on .pg-buddy (pg-buddy--idle, --cheer, --think, --comfort, --sleep); one-off animations are
   classes for a moment (--wave hello, --nod a disc dropped, --talk a new line, --jump a won set, --wake). With
   prefers-reduced-motion nothing moves: the face and pose still change with the mood. No inline styles anywhere (the
   page's CSP): the confetti's random paths come in as custom properties set through the CSSOM. */

/* A compact row above the code: Pip, the bubble right beside it, then Hide. Without a bubble the row is Pip's height only. */
.pg-buddy { position: relative; display: flex; align-items: center; gap: 12px; min-height: 100px; margin: 0 0 0.5rem; }
.pg-buddy [hidden] { display: none !important; }

/* ---- the robot ---- */
/* Pip is a button (a click pokes it): none of the site's button look, just the robot. Its margins are the room the 3D
   canvas takes past it (below: 40 px above, 24 px each side, 6 px under), so a jump or a spin draws over Pip's own
   space and never over the page around it; the 2D Pip keeps them too, so nothing moves when the 3D one comes in. */
.pg-buddy button.pg-buddy-fig { position: relative; flex: none; width: 80px; height: 100px; min-height: 0; margin: 40px 24px 6px; padding: 0;
  border: 0; border-radius: 14px; background: none; box-shadow: none; color: inherit; cursor: pointer; }
.pg-buddy button.pg-buddy-fig:hover { background: none; }
/* Hover: Pip perks up (rises a little, the antenna light quickens, the right arm lifts). Independent translate / scale /
   rotate properties, so they add to the moods' own transform animations instead of replacing them. */
.pg-buddy .pg-buddy-pip { transition: translate .25s ease, scale .25s ease; }
.pg-buddy-fig:hover .pg-buddy-pip, .pg-buddy-fig:focus-visible .pg-buddy-pip { translate: 0 -4px; scale: 1.06; }
.pg-buddy-fig:hover .pip-bulb { animation-duration: .55s; }
.pg-buddy-pip .pip-arm-r { transition: rotate .3s ease, transform .35s ease; }
.pg-buddy-fig:hover .pip-arm-r { rotate: -28deg; }
/* The 3D Pip (buddy-3d.ts): a canvas larger than the figure, so a jump or a spin has room (inside the figure's margins,
   above); it never takes clicks (the figure button does). Once it is in (.is-3d), the 2D drawing steps aside; the confetti stays a DOM layer above it. */
.pg-buddy-3d { position: absolute; left: -24px; right: -24px; top: -40px; bottom: -6px; width: calc(100% + 48px); height: calc(100% + 46px);
  pointer-events: none; animation: pg-buddy-3d-in .4s ease-out; }
.pg-buddy.is-3d .pg-buddy-pip { visibility: hidden; }
/* ...but a sleeping Pip's floating z's stay: a child may be visible inside a hidden parent. */
.pg-buddy.is-3d.pg-buddy--sleep .pip-z { visibility: visible; }
.pg-buddy-confetti { z-index: 1; }
@keyframes pg-buddy-3d-in { from { opacity: 0; } to { opacity: 1; } }
/* The eyes follow the pointer (buddy-view.ts sets --pip-look-x/-y, a few units at most). */
.pg-buddy-pip .pip-eyes { translate: var(--pip-look-x, 0) var(--pip-look-y, 0); }
/* A poke: a quick wiggle and a happy squint. */
.pg-buddy--poke .pg-buddy-pip { animation: pg-buddy-wiggle .65s ease-in-out; }
.pg-buddy--poke .pip-eye { animation: none; transform: scaleY(.35); }
@keyframes pg-buddy-wiggle { 0%, 100% { rotate: 0deg; } 20% { rotate: -9deg; } 45% { rotate: 8deg; } 70% { rotate: -5deg; } 85% { rotate: 2deg; } }
.pg-buddy .pg-buddy-pip { display: block; width: 100%; height: 100%; overflow: visible; pointer-events: none; }   /* a jump over the controls above never takes their clicks */
.pg-buddy-pip .pip-body, .pg-buddy-pip .pip-act, .pg-buddy-pip .pip-head, .pg-buddy-pip .pip-eyes, .pg-buddy-pip .pip-eye, .pg-buddy-pip .pip-arm, .pg-buddy-pip .pip-shadow,
.pg-buddy-pip .pip-spark, .pg-buddy-pip .pip-z, .pg-buddy-pip .pip-mouth { transform-box: fill-box; }
.pg-buddy-pip .pip-body, .pg-buddy-pip .pip-act, .pg-buddy-pip .pip-head { transform-origin: 50% 100%; }
.pg-buddy-pip .pip-eye { transform-origin: 50% 50%; animation: pg-buddy-blink 4.6s infinite; }
.pg-buddy-pip .pip-eyes { transform-origin: 50% 50%; transition: transform .35s ease; }
.pg-buddy-pip .pip-shadow { transform-origin: 50% 50%; fill: #000; opacity: .45; }
.pg-buddy-pip .pip-mouth { opacity: 0; transition: opacity .2s; transform-origin: 50% 50%; }
.pg-buddy-pip .pip-line { fill: none; stroke: var(--teal); stroke-width: 4; stroke-linecap: round; }
.pg-buddy-pip .m-open, .pg-buddy-pip .m-talk { fill: var(--teal); }
.pg-buddy-pip .pip-antenna { stroke: var(--slate-line); stroke-width: 4; stroke-linecap: round; }
.pg-buddy-pip .pip-bulb { fill: var(--teal); animation: pg-buddy-pulse 1.8s ease-in-out infinite; transition: opacity .3s; }
.pg-buddy-pip .pip-skull { fill: var(--hover); stroke: var(--teal); stroke-width: 3; }
.pg-buddy-pip .pip-screen { fill: var(--paper); }
.pg-buddy-pip .pip-eye { fill: var(--teal); }
.pg-buddy-pip .pip-cheek { fill: var(--coral); opacity: .55; }
.pg-buddy-pip .pip-torso { fill: var(--sheet); stroke: var(--slate-line); stroke-width: 2; }
.pg-buddy-pip .pip-arm, .pg-buddy-pip .pip-hand { fill: var(--slate-line); }
.pg-buddy-pip .pip-arm { transition: transform .35s ease; }
.pg-buddy-pip .pip-arm-l { transform-origin: 100% 50%; }
.pg-buddy-pip .pip-arm-r { transform-origin: 0% 50%; }
.pg-buddy-pip .pip-spark { opacity: 0; fill: var(--amber); transform-origin: 50% 50%; }
/* The hello forearm: raised from the right arm's elbow while Pip waves, swinging around it (viewBox units). */
.pg-buddy-pip .pip-wave { opacity: 0; transform-box: view-box; transform-origin: 130.5px 147px; transform: rotate(20deg); }
.pg-buddy-pip .pip-forearm, .pg-buddy-pip .pip-hand { fill: var(--slate-line); }
/* The z's of a sleeping Pip. */
.pg-buddy-pip .pip-z { opacity: 0; fill: none; stroke: var(--ink-2); stroke-width: 2.5; stroke-linecap: round; stroke-linejoin: round; transform-origin: 50% 50%; }

/* ---- moods ---- */
.pg-buddy--idle .pip-body { animation: pg-buddy-bob 3.2s ease-in-out infinite; }
.pg-buddy--idle .pip-shadow { animation: pg-buddy-shadow-bob 3.2s ease-in-out infinite; }
.pg-buddy--idle .m-smile, .pg-buddy--comfort .m-soft, .pg-buddy--cheer .m-open, .pg-buddy--think .m-flat, .pg-buddy--sleep .m-sleep { opacity: 1; }

.pg-buddy--cheer .pip-body { animation: pg-buddy-hop .72s cubic-bezier(.3, .6, .4, 1) infinite; }
.pg-buddy--cheer .pip-shadow { animation: pg-buddy-shadow-hop .72s cubic-bezier(.3, .6, .4, 1) infinite; }
.pg-buddy--cheer .pip-eye { animation: none; transform: scaleY(.45); }
.pg-buddy--cheer .pip-arm-l { animation: pg-buddy-wave-l .36s ease-in-out infinite alternate; }
.pg-buddy--cheer .pip-arm-r { animation: pg-buddy-wave-r .36s ease-in-out infinite alternate; }
.pg-buddy--cheer .pip-spark { animation: pg-buddy-twinkle 1.1s ease-in-out infinite; }
.pg-buddy--cheer .pip-spark.s2 { animation-delay: .3s; }
.pg-buddy--cheer .pip-spark.s3 { animation-delay: .6s; }
.pg-buddy--cheer .pip-spark.s4 { animation-delay: .85s; }

.pg-buddy--think .pip-body { animation: pg-buddy-tilt 2.6s ease-in-out infinite; }
.pg-buddy--think .pip-eyes { transform: translate(5px, -6px); }
.pg-buddy--think .pip-bulb { animation: pg-buddy-pulse .6s ease-in-out infinite; }

.pg-buddy--comfort .pip-body { animation: pg-buddy-sway 4.2s ease-in-out infinite; }
.pg-buddy--comfort .pip-eyes { transform: translateY(3px) scaleY(.75); }
.pg-buddy--comfort .pip-arm-l { transform: rotate(18deg); }
.pg-buddy--comfort .pip-arm-r { transform: rotate(-18deg); }

/* Asleep (two minutes without an event): eyes half shut, the lights low, slow breathing, z's floating up. */
.pg-buddy--sleep .pip-body { animation: pg-buddy-breathe 4.8s ease-in-out infinite; }
.pg-buddy--sleep .pip-shadow { animation: pg-buddy-shadow-breathe 4.8s ease-in-out infinite; }
.pg-buddy--sleep .pip-eyes { transform: translateY(4px); }
.pg-buddy--sleep .pip-eye { animation: none; transform: scaleY(.22); }
.pg-buddy--sleep .pip-bulb { animation: none; opacity: .3; }
.pg-buddy--sleep .pip-head { transform: rotate(-3deg); }
.pg-buddy--sleep .pip-arm-l { transform: rotate(-10deg); }
.pg-buddy--sleep .pip-arm-r { transform: rotate(10deg); }
.pg-buddy--sleep .pip-z.z1 { opacity: .9; }
.pg-buddy--sleep .pip-z.z2 { opacity: .55; }
.pg-buddy--sleep .pip-z { animation: pg-buddy-z 3.2s ease-in-out infinite; }
.pg-buddy--sleep .pip-z.z2 { animation-delay: 1.6s; }

/* ---- one-off animations ---- */
/* Hello: the right forearm comes up beside the head and waves. */
.pg-buddy--wave .pip-wave { opacity: 1; animation: pg-buddy-hello .3s ease-in-out 6 alternate; }
/* A disc dropped: a small nod. */
.pg-buddy--nod .pip-head { animation: pg-buddy-nod .45s ease-in-out; }
/* Woken up: a little start. */
.pg-buddy--wake .pip-act { animation: pg-buddy-startle .6s ease-out; }
/* A won set: a big jump with sparkles (the confetti is DOM, below). The mood's hop waits for it. */
.pg-buddy--jump .pip-body, .pg-buddy--jump .pip-shadow { animation: none; }
.pg-buddy--jump .pip-act { animation: pg-buddy-jump 1s cubic-bezier(.3, .6, .4, 1); }
.pg-buddy--jump .pip-shadow { animation: pg-buddy-shadow-jump 1s cubic-bezier(.3, .6, .4, 1); }
.pg-buddy--jump .pip-spark { animation: pg-buddy-burst .9s ease-out 1 both; }
.pg-buddy--jump .pip-spark.s2 { animation-delay: .1s; }
.pg-buddy--jump .pip-spark.s3 { animation-delay: .2s; }
.pg-buddy--jump .pip-spark.s4 { animation-delay: .3s; }
/* Talking (a second after a new line): the mouth flaps. Only with motion: without it the mood's mouth stays. */
@media (prefers-reduced-motion: no-preference) {
  .pg-buddy.pg-buddy--talk .pip-mouth { opacity: 0; transition: none; }
  .pg-buddy.pg-buddy--talk .m-talk { opacity: 1; animation: pg-buddy-talk .16s ease-in-out infinite alternate; }
}

.pg-buddy-confetti { position: absolute; left: 50%; top: 28%; width: 0; height: 0; pointer-events: none; }
.pg-buddy-bit { position: absolute; left: -3px; top: -5px; width: 6px; height: 10px; border-radius: 1px; opacity: 0;
  animation: pg-buddy-confetti 1.5s cubic-bezier(.2, .7, .4, 1) var(--delay, 0ms) forwards; }
.pg-buddy-bit.is-teal { background: var(--teal); }
.pg-buddy-bit.is-coral { background: var(--coral); }
.pg-buddy-bit.is-amber { background: var(--amber); }

/* ---- the bubble ---- */
.pg-buddy-say { flex: 0 1 auto; min-width: 0; }
.pg-buddy-bubble { position: relative; display: flex; flex-wrap: wrap; align-items: center; gap: 6px 12px; max-width: 40rem; padding: 8px 12px;
  background: var(--hover); border: 1px solid var(--slate-line); border-radius: 12px; color: var(--ink); font: 500 0.875rem/1.4 var(--font); }
.pg-buddy-bubble::before { content: ""; position: absolute; left: -7px; top: calc(50% - 6px); width: 12px; height: 12px; background: var(--hover);
  border-left: 1px solid var(--slate-line); border-bottom: 1px solid var(--slate-line); transform: rotate(45deg); }
.pg-buddy-bubble.is-pop { animation: pg-buddy-pop .35s cubic-bezier(.2, 1.4, .4, 1); }
.pg-buddy-line { margin: 0; overflow-wrap: anywhere; }
.pg-buddy-dots { display: inline-flex; gap: 4px; margin-left: 6px; vertical-align: middle; }
.pg-buddy-dots span { width: 6px; height: 6px; border-radius: 50%; background: var(--ink-2); animation: pg-buddy-dot 1.1s ease-in-out infinite; }
.pg-buddy-dots span:nth-child(2) { animation-delay: .15s; }
.pg-buddy-dots span:nth-child(3) { animation-delay: .3s; }
.pg-buddy-acts { display: flex; flex-wrap: wrap; gap: 6px; }
.pg-buddy-acts button { min-height: 2rem; }
.pg-buddy .pg-buddy-later:hover { background: var(--field); }

/* ---- hide and show ---- */
.pg-buddy-hide { flex: none; width: 2rem; height: 2rem; min-height: 2rem; padding: 0; color: var(--ink-2); }
.pg-buddy-hide:hover { color: var(--ink); }
.pg-buddy-x { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }
.pg-buddy-show { min-height: 2.25rem; color: var(--ink-2); }
.pg-buddy-show:hover { color: var(--ink); }
.pg-buddy.is-hidden { min-height: 0; }
/* No ✕ beside a quiet Pip: Hide shows only while Pip is saying something (buddy-view.ts sets .has-line). */
.pg-buddy:not(.has-line) .pg-buddy-hide { display: none; }
.pg-buddy:focus { outline: none; }
.pg-buddy button:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

/* ---- a phone: the same row, a smaller Pip; the bubble takes the rest, its buttons tall enough to tap ---- */
@media (max-width: 600px) {
  .pg-buddy { gap: 8px; min-height: 75px; }
  .pg-buddy button.pg-buddy-fig { width: 60px; height: 75px; }
  .pg-buddy-say { flex: 1 1 auto; }
  .pg-buddy-hide { width: 2.75rem; height: 2.75rem; min-height: 2.75rem; }
  .pg-buddy-acts button, .pg-buddy-show { min-height: 2.75rem; }
  .pg-buddy-acts button { flex: 1 1 auto; }
}

/* ---- motion ---- */
@keyframes pg-buddy-bob { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-6px); } }
@keyframes pg-buddy-shadow-bob { 0%, 100% { transform: scaleX(1); opacity: .45; } 50% { transform: scaleX(.86); opacity: .3; } }
@keyframes pg-buddy-hop { 0% { transform: translateY(0) scale(1.1, .9); } 30% { transform: translateY(-20px) scale(.94, 1.06); }
  55% { transform: translateY(-23px) scale(1, 1); } 85% { transform: translateY(0) scale(1.12, .88); } 100% { transform: translateY(0) scale(1.1, .9); } }
@keyframes pg-buddy-shadow-hop { 0%, 100% { transform: scaleX(1.1); opacity: .5; } 55% { transform: scaleX(.6); opacity: .2; } }
@keyframes pg-buddy-tilt { 0%, 100% { transform: rotate(-5deg); } 50% { transform: rotate(5deg); } }
@keyframes pg-buddy-sway { 0%, 100% { transform: rotate(-2deg) translateY(2px); } 50% { transform: rotate(2deg) translateY(4px); } }
@keyframes pg-buddy-breathe { 0%, 100% { transform: translateY(2px) scale(1, 1); } 50% { transform: translateY(2px) scale(1.025, .97); } }
@keyframes pg-buddy-shadow-breathe { 0%, 100% { transform: scaleX(1); } 50% { transform: scaleX(1.05); } }
@keyframes pg-buddy-blink { 0%, 92%, 100% { transform: scaleY(1); } 95% { transform: scaleY(.1); } }
@keyframes pg-buddy-pulse { 0%, 100% { opacity: 1; } 50% { opacity: .35; } }
@keyframes pg-buddy-wave-l { from { transform: rotate(-30deg); } to { transform: rotate(-75deg); } }
@keyframes pg-buddy-wave-r { from { transform: rotate(30deg); } to { transform: rotate(75deg); } }
@keyframes pg-buddy-twinkle { 0%, 100% { opacity: 0; transform: scale(.4) rotate(0); } 50% { opacity: 1; transform: scale(1.1) rotate(45deg); } }
@keyframes pg-buddy-pop { from { opacity: 0; transform: translateY(6px) scale(.96); } to { opacity: 1; transform: none; } }
@keyframes pg-buddy-dot { 0%, 100% { transform: translateY(0); opacity: .5; } 50% { transform: translateY(-4px); opacity: 1; } }
@keyframes pg-buddy-hello { from { transform: rotate(4deg); } to { transform: rotate(38deg); } }
@keyframes pg-buddy-nod { 0%, 100% { transform: none; } 40% { transform: translateY(4px) scaleY(.97); } 70% { transform: translateY(-1px); } }
@keyframes pg-buddy-startle { 0%, 100% { transform: none; } 30% { transform: translateY(-12px) scale(.96, 1.04); } 60% { transform: translateY(0) scale(1.06, .94); } }
@keyframes pg-buddy-jump { 0%, 100% { transform: none; } 15% { transform: translateY(0) scale(1.12, .86); } 45% { transform: translateY(-42px) scale(.92, 1.1); }
  60% { transform: translateY(-46px) scale(1, 1); } 85% { transform: translateY(0) scale(1.14, .86); } }
@keyframes pg-buddy-shadow-jump { 0%, 100% { transform: scaleX(1); opacity: .45; } 15% { transform: scaleX(1.15); opacity: .5; } 60% { transform: scaleX(.5); opacity: .15; } 85% { transform: scaleX(1.15); opacity: .5; } }
@keyframes pg-buddy-burst { 0% { opacity: 0; transform: scale(.2) rotate(0); } 40% { opacity: 1; transform: scale(1.5) rotate(45deg); } 100% { opacity: 0; transform: scale(.6) rotate(90deg); } }
@keyframes pg-buddy-talk { from { transform: scaleY(.25); } to { transform: scaleY(1); } }
@keyframes pg-buddy-z { 0% { opacity: 0; transform: translate(0, 4px) scale(.6); } 30% { opacity: .9; } 100% { opacity: 0; transform: translate(8px, -16px) scale(1.1); } }
@keyframes pg-buddy-confetti { 0% { opacity: 1; transform: translate(0, 0) rotate(0); }
  45% { opacity: 1; transform: translate(calc(var(--dx) * .7), var(--up)) rotate(calc(var(--rot) * .5)); }
  100% { opacity: 0; transform: translate(var(--dx), var(--fall)) rotate(var(--rot)); } }

@media (prefers-reduced-motion: reduce) {
  .pg-buddy *, .pg-buddy *::before, .pg-buddy *::after { animation: none !important; transition: none !important; }
  .pg-buddy-fig:hover .pg-buddy-pip, .pg-buddy-fig:focus-visible .pg-buddy-pip { translate: none; scale: none; }
  .pg-buddy-fig:hover .pip-arm-r { rotate: none; }
}
