/* ==========================================================================
   SPIFFY, the helper (assets/js/os/spiffy.js)
   Layers, outside in: .spiffy (where he stands) > __body (button; ducks aside while you
   play) > __tilt (swings as he is dragged) > __squash (lands) > __bob (idle) > the SVG.
   Desktop: above the windows, under the taskbar and Start menu. Phone: above the sheets,
   under the tab bar.
   ========================================================================== */

.spiffy {
	--px: 0;
	--py: 0;
	--w: 104px;
	position: fixed;
	left: 0;
	top: 0;
	z-index: 4900;
	width: var(--w);
	pointer-events: none;
	will-change: transform;
}
.spiffy[hidden],
.spiffy__bubble[hidden] { display: none; }
html.is-booting .spiffy,
html.is-booting .spiffy__bubble,
html.is-melting .spiffy,
html.is-melting .spiffy__bubble { display: none; }

.spiffy__body {
	display: block;
	width: 100%;
	padding: 0;
	border: 0;
	background: none;
	color: inherit;
	cursor: grab;
	pointer-events: none; /* only the figure itself catches the pointer (.spiffy__hit) */
	transform: translateX(var(--tuck, 0px));
	transition: transform 0.5s var(--ease-out), opacity 0.3s;
	touch-action: manipulation;
	-webkit-tap-highlight-color: transparent;
	-webkit-touch-callout: none;
	-webkit-user-select: none;
	user-select: none;
}
.spiffy__body:focus { outline: none; }
.spiffy__body:focus-visible .spiffy__svg { outline: 2px dashed var(--red); outline-offset: -14px; }
.spiffy.is-dragging .spiffy__body { cursor: grabbing; transition: none; }
.spiffy.is-tucked .spiffy__body { opacity: 0.85; }

.spiffy__tilt,
.spiffy__squash,
.spiffy__bob { display: block; transform-origin: 50% 88%; }
/* A red rim and a soft glow, so the black ink still reads on the dark desktop. */
.spiffy__svg {
	display: block;
	width: 100%;
	height: auto;
	overflow: visible;
	filter: drop-shadow(0 0 0.6px var(--red)) drop-shadow(0 0 1px var(--red)) drop-shadow(0 0 10px rgb(255 69 69 / calc(0.5 * var(--glow-k))));
}
.spiffy__hit { fill: transparent; pointer-events: all; }

/* Idle: a slow sway on the spot, leaning back like he owns the place. */
.spiffy__bob { transform: rotate(-5deg); animation: spiffy-bob 3.4s ease-in-out infinite; }
@keyframes spiffy-bob {
	0%, 100% { transform: translateY(0) rotate(-5deg); }
	50% { transform: translateY(-3px) rotate(-2.5deg); }
}
.spiffy.is-dragging .spiffy__bob { animation-play-state: paused; }
.spiffy.is-lifted .spiffy__squash { transform: scale(1.08); transition: transform 0.15s var(--ease-snap); }

/* ---------------------------------------------------------------- Drawing */

/* Arms and legs: paper-white with a black outline (drawn twice). */
.spiffy__limb-o { fill: none; stroke: #000; stroke-width: 6.6; stroke-linecap: round; }
.spiffy__limb { fill: none; stroke: #fbf3e8; stroke-width: 3; stroke-linecap: round; }
.spiffy__paper { fill: #fbf3e8; stroke: #000; stroke-width: 3; stroke-linejoin: round; }
.spiffy__seam { fill: none; stroke: #d6c3ae; stroke-width: 1.6; stroke-linecap: round; }
.spiffy__shine { fill: none; stroke: #fff; stroke-width: 2.4; stroke-linecap: round; }
.spiffy__crutch { fill: var(--blush); stroke: #000; stroke-width: 2.6; stroke-linejoin: round; }
.spiffy__crutch-line { fill: none; stroke: var(--red); stroke-width: 1.6; }
.spiffy__shoe { fill: var(--red); stroke: #000; stroke-width: 2.2; stroke-linejoin: round; }
.spiffy__sole { fill: #fff; stroke: #000; stroke-width: 2; stroke-linejoin: round; }
.spiffy__stripe { fill: none; stroke: #fff; stroke-width: 1.4; stroke-linecap: round; }
.spiffy__hand { fill: #fff; stroke: #000; stroke-width: 2.2; }
.spiffy__ash { fill: #8f8a86; stroke: #000; stroke-width: 2.2; stroke-linejoin: round; }
.spiffy__speck { fill: none; stroke: #5e5955; stroke-width: 1.3; stroke-linecap: round; }
.spiffy__ember { fill: url(#pd-spiffy-ember); stroke: #000; stroke-width: 2.6; }
.spiffy__halo { opacity: calc(0.85 * var(--glow-k)); animation: spiffy-glow 2.8s ease-in-out infinite; transform-box: fill-box; transform-origin: center; }
@keyframes spiffy-glow {
	0%, 100% { opacity: calc(0.55 * var(--glow-k)); transform: scale(0.9); }
	45% { opacity: calc(0.95 * var(--glow-k)); transform: scale(1.08); }
}
.spiffy__shadow { fill: rgb(0 0 0 / 0.55); transform-box: fill-box; transform-origin: center; transition: transform 0.25s, opacity 0.25s; }
.spiffy.is-dragging .spiffy__shadow { transform: translateY(10px) scale(0.55); opacity: 0.5; }

/* Smoke off the lit end: soft puffs that rise, swell and fade, one after another. */
.spiffy__puff {
	--dx: -6px;
	fill: var(--blush);
	opacity: 0;
	transform-box: fill-box;
	transform-origin: center;
	animation: spiffy-smoke 3.3s ease-out infinite;
}
.spiffy__puff--b { --dx: 7px; animation-delay: -1.1s; }
.spiffy__puff--c { --dx: -2px; animation-delay: -2.2s; }
@keyframes spiffy-smoke {
	0% { opacity: 0; transform: translate(0, 0) scale(0.5); }
	15% { opacity: 0.6; }
	100% { opacity: 0; transform: translate(var(--dx), -58px) scale(2.4); }
}
.spiffy__smoke { filter: blur(1.1px); }

/* Face. Heavy lids by default; the pupils follow the pointer (--px, --py from the JS). */
.spiffy__white { fill: #fff6f2; stroke: #000; stroke-width: 2.2; }
.spiffy__vein { fill: none; stroke: var(--red); stroke-width: 0.9; stroke-linecap: round; }
.spiffy__pupil { fill: #000; transform: translate(calc(var(--px) * 1px), calc(var(--py) * 1px)); transition: transform 0.12s; }
.spiffy__lid { fill: #efe0cc; stroke: #000; stroke-width: 2.2; stroke-linejoin: round; transform-box: fill-box; transform-origin: 50% 0%; transition: transform 0.25s var(--ease-out); }
.spiffy__eye { transform-box: fill-box; transform-origin: center; transition: transform 0.07s; }
.spiffy__brow { fill: none; stroke: #000; stroke-width: 2.6; stroke-linecap: round; transform-box: fill-box; transform-origin: center; transition: transform 0.25s var(--ease-out); }
.spiffy__cheek { fill: var(--pink); opacity: 0.55; }
.spiffy__mouth { fill: none; stroke: #000; stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; }
.spiffy__mouth--open { fill: var(--red-ink); opacity: 0; }

.spiffy.is-blink .spiffy__eye { transform: scaleY(0.12); }

/* Talking: the mouth flaps while the words type out. */
.spiffy.is-talking .spiffy__mouth--open { animation: spiffy-yap 0.2s steps(1) infinite; }
.spiffy.is-talking .spiffy__mouth--shut { animation: spiffy-yap 0.2s steps(1) infinite reverse; }
@keyframes spiffy-yap {
	0% { opacity: 1; }
	50% { opacity: 0; }
}

/* Moods. */
.spiffy.is-smug .spiffy__brow--l { transform: translateY(-2px) rotate(-10deg); }
.spiffy.is-smug .spiffy__brow--r { transform: translateY(1px) rotate(12deg); }
.spiffy.is-smug .spiffy__lid { transform: scaleY(1.25); }
.spiffy.is-impressed .spiffy__brow { transform: translateY(-2.5px); }
.spiffy.is-impressed .spiffy__lid { transform: scaleY(0.4); }
.spiffy.is-sleepy .spiffy__lid { transform: scaleY(1.6); }
.spiffy.is-sleepy .spiffy__brow { transform: translateY(1.5px); }
.spiffy.is-cross .spiffy__brow--l { transform: translateY(1px) rotate(16deg); }
.spiffy.is-cross .spiffy__brow--r { transform: translateY(1px) rotate(-16deg); }
.spiffy.is-dragging .spiffy__lid { transform: scaleY(0.35); }
.spiffy.is-dragging .spiffy__brow { transform: translateY(-2.5px); }

/* Arms and legs. The right hand waves hello; dragging him, the legs dangle. */
.spiffy__wave { transform-box: view-box; transform-origin: 67px 80px; }
.spiffy__wave { transition: transform 0.25s var(--ease-out); }
.spiffy.is-waving .spiffy__wave { animation: spiffy-wave 1.5s ease-in-out; }
@keyframes spiffy-wave {
	0%, 100% { transform: rotate(0deg); }
	18%, 50%, 82% { transform: rotate(-142deg); }
	34%, 66% { transform: rotate(-112deg); }
}
.spiffy.is-dragging .spiffy__wave { transform: rotate(-150deg); }
.spiffy__legs { transform-box: view-box; transform-origin: 50px 117px; }
.spiffy.is-dragging .spiffy__legs { animation: spiffy-dangle 0.5s ease-in-out infinite alternate; }
@keyframes spiffy-dangle {
	from { transform: rotate(-6deg); }
	to { transform: rotate(6deg); }
}

/* Coming and going. */
.spiffy.is-arriving .spiffy__squash { animation: spiffy-in 0.62s var(--ease-snap) both; }
@keyframes spiffy-in {
	from { opacity: 0; transform: translateY(40px) scale(0.3, 0.6); }
	60% { opacity: 1; transform: translateY(-6px) scale(1.06, 0.95); }
}
.spiffy.is-leaving .spiffy__squash { animation: spiffy-out 0.5s var(--ease-in) both; }
@keyframes spiffy-out {
	30% { transform: translateY(-10px) scale(0.95, 1.08); }
	to { opacity: 0; transform: translateY(60px) scale(0.4, 0.2); }
}

/* ---------------------------------------------------------------- The speech bubble */

.spiffy__bubble {
	--tail: 50%;
	position: fixed;
	left: 0;
	top: 0;
	z-index: 4901;
	box-sizing: border-box;
	width: max-content;
	max-width: min(290px, calc(100vw - 16px));
	margin: 0;
	padding: 8px 12px 10px;
	border: 3px solid #000;
	background: var(--white);
	box-shadow: 4px 4px 0 var(--red), 0 0 26px rgb(255 69 69 / calc(0.5 * var(--glow-k)));
	color: #000;
	text-align: left;
}
.spiffy__bubble.is-popping { animation: spiffy-pop 0.26s var(--ease-snap); }
@keyframes spiffy-pop {
	from { opacity: 0; transform: translateY(6px) scale(0.86); }
}
/* The tail: a black triangle with a white one inside, pointing down at him (up when below). */
.spiffy__bubble::before,
.spiffy__bubble::after {
	content: '';
	position: absolute;
	left: var(--tail);
	top: 100%;
	border: solid transparent;
	transform: translateX(-50%);
}
.spiffy__bubble::before { border-width: 13px 9px 0; border-top-color: #000; }
.spiffy__bubble::after { margin-top: -1px; border-width: 9px 6px 0; border-top-color: var(--white); }
.spiffy__bubble.is-below::before,
.spiffy__bubble.is-below::after { top: auto; bottom: 100%; }
.spiffy__bubble.is-below::before { border-width: 0 9px 13px; border-color: transparent transparent #000; }
.spiffy__bubble.is-below::after { margin-top: 0; margin-bottom: -1px; border-width: 0 6px 9px; border-color: transparent transparent var(--white); }

.spiffy__who {
	margin: 0 0 2px;
	color: var(--red-mid);
	font-family: var(--f-num);
	font-size: 10px;
	font-weight: 700;
	letter-spacing: 0.22em;
	text-transform: uppercase;
}
.spiffy__text {
	display: grid;
	margin: 0;
	font-family: var(--f-sys);
	font-size: 22px;
	line-height: 1.02;
}
.spiffy__ghost,
.spiffy__typed { grid-area: 1 / 1; }
.spiffy__ghost { visibility: hidden; }

.spiffy__acts { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.spiffy__acts:empty { display: none; }
.spiffy__act {
	min-height: 32px;
	padding: 5px 10px 4px;
	border: 2px solid #000;
	background: var(--red);
	box-shadow: 2px 2px 0 #000;
	color: #000;
	font-family: var(--f-display);
	font-size: 12px;
	letter-spacing: 0.04em;
	text-transform: uppercase;
	cursor: pointer;
}
.spiffy__act:hover,
.spiffy__act:focus-visible { background: #000; color: var(--white); outline: none; }
.spiffy__act--quiet { background: var(--white); }

/* ---------------------------------------------------------------- Phone */

@media (max-width: 899px) {
	.spiffy { --w: 74px; z-index: 66; }
	.spiffy__bubble { z-index: 67; max-width: min(250px, calc(100vw - 16px)); }
	.spiffy__text { font-size: 20px; }
}

/* ---------------------------------------------------------------- Calm */

html[data-motion='reduce'] .spiffy *,
html[data-motion='reduce'] .spiffy__bubble { animation: none !important; transition: none !important; }
@media (prefers-reduced-motion: reduce) {
	html:not([data-motion='full']) .spiffy *,
	html:not([data-motion='full']) .spiffy__bubble { animation: none !important; transition: none !important; }
}
