/*
 * Hidden Edge Studio: the motion layer. Loaded only while the motion switch is on (the preview key,
 * a signed-in editor, and for everyone once the owner approves).
 *
 * Rules of this file:
 * - Transform and opacity only, so nothing moves the layout.
 * - Nothing is hidden for a reveal until motion.js has added "he-motion-ready" to <html> (and, per
 *   element, "he-hero-armed" or "he-arm"), so a page whose script fails or arrives late stays fully
 *   visible. The marquee band is plain CSS, so a late script cannot shift the layout either.
 * - Everything that moves lives inside prefers-reduced-motion: no-preference. Under reduced motion
 *   the page is the plain static page; the decorative furniture below stays display: none.
 * - Everything leans 9.09 degrees where it leans, like the buttons and the lettering.
 */

/* ---------- Furniture that exists only when motion runs ---------- */

.he-motion .he-marquee,
.he-motion .he-work__no {
	display: none;
}

/*
 * The real image that is the main visual of each "Our own work" item (the motion version only; the markup
 * is not printed when motion is off). It is content, not motion, so it shows in the plain list too,
 * above the item's text: a 16:9 frame, the image covering it. The profile cover and the sen.help page are
 * pinned to the top edge, so the crop never cuts through the cover's title. The small mark that stood in
 * for an image is not needed beside a real one.
 */
.he-motion .he-work__frame {
	display: block;
	margin: 0 0 18px;
	aspect-ratio: 16 / 9;
	overflow: hidden;
	background: var(--he-ink);
}

.he-motion .he-work__img {
	display: block;
	width: 100%;
	height: 100%;
	object-fit: cover;
	object-position: 50% 50%;
}

.he-motion .he-work__frame--profile .he-work__img,
.he-motion .he-work__frame--senhelp .he-work__img {
	object-position: 50% 0;
}

.he-motion .he-work__frame ~ .he-work__mark {
	display: none;
}

@media (prefers-reduced-motion: no-preference) {

	/* ---------- 1. Hero: slow zoom on the still, the video fades in over it ---------- */

	.he-motion-ready .he-hero__img {
		transform-origin: 50% 55%;
		animation: he-zoom 28s cubic-bezier(0.2, 0.6, 0.2, 1) both;
	}

	@keyframes he-zoom {
		from {
			transform: scale(1);
		}

		to {
			transform: scale(1.1);
		}
	}

	.he-hero__video {
		position: absolute;
		inset: 0;
		width: 100%;
		height: 100%;
		object-fit: cover;
		object-position: center;
		opacity: 0;
		transition: opacity 1.4s ease;
		pointer-events: none;
	}

	.he-hero__video.is-on {
		opacity: 1;
	}

	@media (min-width: 1000px) {
		.he-hero__video {
			object-position: center 60%;
		}
	}

	/*
	 * Over the moving video the label (#DD7578) passed by only 0.02 to 0.1 at the brightest pixel behind it
	 * (4.52:1 at 1280px, 4.57 at 1000 and 1920px, 4.58 at 1440px; AA needs 4.5). The lighter tint #E5868A, in the
	 * same oxblood family, measures 5.32, 5.38 and 5.39:1 at those same pixels (6.07 at 390px, 5.83 at 768px).
	 */
	.he-motion .he-hero .he-label--tint {
		color: #E5868A;
	}

	/* On a phone and tablet the text block ends 40px above the hero bottom edge: the pause button gets room below the buttons. */
	@media (max-width: 999.98px) {
		.he-motion .he-hero__in {
			padding-bottom: 76px;
		}
	}

	/* ---------- 0. Pause, Stop, Hide (WCAG 2.2.2): one button on the hero, one on the marquee band ---------- */

	/* A paused page is still. The hero zoom is the only animation driven by time; the reveals are one-shot or
	   scroll-driven, and are left alone (freezing one at its start would leave a card hidden). */
	.he-motion-paused .he-hero__img {
		animation-play-state: paused;
	}

	.he-motion-toggle {
		position: absolute;
		z-index: 3;
		isolation: isolate;
		display: inline-flex;
		align-items: center;
		justify-content: center;
		min-height: 44px;
		padding: 0 26px;
		border: 0;
		background: transparent;
		color: var(--he-on-dark);
		font-family: var(--he-mono);
		font-weight: 600;
		font-size: 0.8125rem;
		letter-spacing: 0.18em;
		line-height: 1.5;
		text-align: center;
		text-transform: uppercase;
		white-space: nowrap;
		cursor: pointer;
	}

	/* Charcoal fill with light type (15:1) whatever is behind it; it leans like the other buttons. */
	.he-motion-toggle::before {
		content: "";
		position: absolute;
		z-index: -1;
		inset: 0 5px;
		border: 1px solid rgba(242, 240, 239, 0.55);
		border-radius: 2px;
		background: var(--he-ink);
		transform: skewX(-9.09deg);
		transition: background-color 0.15s ease, border-color 0.15s ease;
	}

	.he-motion-toggle:hover::before {
		border-color: var(--he-on-dark);
		background: var(--he-ox);
	}

	/* Two rings, light inside and charcoal outside, so the focus shows on a dark photo and on the light band alike. */
	.he-motion-ready .he-motion-toggle:focus-visible {
		outline: none;
	}

	.he-motion-ready .he-motion-toggle:focus-visible::before {
		outline: 3px solid var(--he-ink);
		outline-offset: 3px;
		box-shadow: 0 0 0 3px var(--he-on-dark);
	}

	/* The visible edge of the leaning shape lines up with the right edge of the page column. */
	.he-motion-toggle--hero,
	.he-motion-toggle--band {
		right: max(calc(var(--he-gutter) - 5px), calc((100% - var(--he-max)) / 2 + var(--he-gutter) - 5px));
	}

	.he-motion-toggle--hero {
		bottom: 16px;
	}

	.he-motion-toggle--band {
		bottom: clamp(10px, 1.6vw, 20px);
	}

	/*
	 * The rest of the hero follows the title in. In every reveal below the transition is declared on
	 * the revealed state, not the hidden one: hiding is then instant when motion.js starts, and only
	 * the way in is animated. motion.js arms the hero only when it runs before the first paint, so a
	 * title that is already on screen is never hidden and replayed.
	 */
	.he-hero-armed .he-hero .he-label,
	.he-hero-armed .he-hero__sub,
	.he-hero-armed .he-hero .he-actions {
		opacity: 0;
		transform: translateY(18px);
	}

	.he-hero-in .he-hero .he-label,
	.he-hero-in .he-hero__sub,
	.he-hero-in .he-hero .he-actions {
		opacity: 1;
		transform: translateY(0);
		transition: opacity 0.9s ease var(--he-d, 0ms), transform 1.1s cubic-bezier(0.16, 1, 0.3, 1) var(--he-d, 0ms);
	}

	.he-hero__sub {
		--he-d: 560ms;
	}

	.he-hero .he-actions {
		--he-d: 720ms;
	}

	/* ---------- 2 and 6. Words arrive leaning, rising out of a mask ---------- */

	.he-split .he-w {
		display: inline-block;
		vertical-align: top;
		/* The mask is the line plus room for descenders; padding and margin cancel, so layout is unchanged. */
		padding: 0.14em 0.08em;
		margin: -0.14em -0.08em;
		clip-path: inset(0 -0.6em);
	}

	.he-split .he-w__i {
		display: inline-block;
		transform-origin: 0 100%;
	}

	.he-hero-armed .he-hero__h1 .he-w__i,
	.he-motion-ready .he-arm .he-w__i {
		transform: translateY(125%) skewX(-9.09deg);
	}

	.he-hero-in .he-hero__h1 .he-w__i,
	.he-motion-ready .he-arm.he-in .he-w__i {
		transform: translateY(0) skewX(0deg);
		transition: transform 1.15s cubic-bezier(0.16, 1, 0.3, 1) calc(var(--i, 0) * 60ms);
	}

	.he-hero-in .he-hero__h1 .he-w__i {
		transition-delay: calc(var(--i, 0) * 85ms + 140ms);
	}

	/* Once the title has landed (about 2.6s) the mask opens up, so the title's shadow is no longer trimmed. */
	.he-hero-in .he-hero__h1 .he-w {
		clip-path: inset(-0.5em -0.6em);
		transition: clip-path 0.01s linear 2.6s;
	}

	/* ---------- 3. The marquee band ---------- */

	/* The wrapper carries the band's pause button; the band itself is hidden from assistive technology. */
	.he-motion .he-marquee-wrap {
		position: relative;
	}

	.he-motion .he-marquee {
		display: block;
		overflow: hidden;
		overflow: clip;
		padding: clamp(10px, 1.6vw, 28px) 0;
		background: var(--he-bg);
		color: var(--he-ink);
		font-family: var(--he-display);
		font-weight: 800;
		font-size: clamp(4.5rem, 24vw, 8rem);
		line-height: 1.05;
		letter-spacing: -0.02em;
		user-select: none;
	}

	/* From 700px the type is 16vw: the capitals stand about 12.5vw tall (measured at 1440px), so on a desktop the band is over 12vw. */
	@media (min-width: 700px) {
		.he-motion .he-marquee {
			font-size: clamp(6.5rem, 16vw, 18rem);
		}
	}

	.he-marquee__track {
		display: flex;
		width: max-content;
		will-change: transform;
		transform: translate3d(0, 0, 0) skewX(-9.09deg);
	}

	.he-marquee__set {
		display: flex;
		flex: none;
		align-items: center;
	}

	.he-marquee__w {
		flex: none;
		white-space: nowrap;
	}

	/*
	 * Outlined words. The variable font has overlapping contours, which a plain stroke would draw as
	 * stray lines inside the letters. Painting the stroke first and filling over it in the ground colour
	 * hides every inner line and keeps the outer half of the stroke (so the stroke is set to twice the
	 * line wanted). Where paint-order is not supported the words fall back to a plain outline.
	 */
	.he-marquee__w--line {
		color: var(--he-bg);
		-webkit-text-stroke: clamp(4px, 0.036em, 8px) var(--he-ink);
		paint-order: stroke fill;
	}

	.he-marquee__mark {
		flex: none;
		width: 0.36em;
		height: 0.5em;
		margin: 0 0.42em;
		background: url("../img/mark-charcoal.svg") center / contain no-repeat;
	}

	/* ---------- 4. What we make, and the work list on a phone: staggered, leaning entrances ---------- */

	.he-cards__list--make .he-card,
	.he-work__item {
		--c: 0;
	}

	@media (min-width: 640px) {
		.he-cards__list--make .he-card:nth-child(even) {
			--c: 1;
		}
	}

	@media (min-width: 1040px) {
		.he-cards__list--make .he-card:nth-child(3) {
			--c: 2;
		}

		.he-cards__list--make .he-card:nth-child(4) {
			--c: 3;
		}
	}

	@media (min-width: 760px) {
		.he-work__item:nth-child(even) {
			--c: 1;
		}
	}

	@keyframes he-lean-in {
		from {
			opacity: 0;
			transform: translateY(64px) skewX(-9.09deg);
		}

		to {
			opacity: 1;
			transform: translateY(0) skewX(0deg);
		}
	}

	/* Where scroll-driven animations exist, the scroll position plays the entrance. */
	.he-sdr .he-cards__list--make .he-card.he-arm,
	.he-sdr .he-work__item.he-arm {
		animation: he-lean-in cubic-bezier(0.25, 0.46, 0.45, 0.94) both;
		animation-timeline: view();
		animation-range-start: cover calc(var(--c) * 5%);
		animation-range-end: cover calc(26% + var(--c) * 5%);
	}

	/* Everywhere else, an observer adds .he-in as each one scrolls in. */
	.he-motion-ready:not(.he-sdr) .he-cards__list--make .he-card.he-arm,
	.he-motion-ready:not(.he-sdr) .he-work__item.he-arm {
		opacity: 0;
		transform: translateY(64px) skewX(-9.09deg);
	}

	.he-motion-ready:not(.he-sdr) .he-cards__list--make .he-card.he-arm.he-in,
	.he-motion-ready:not(.he-sdr) .he-work__item.he-arm.he-in {
		opacity: 1;
		transform: translateY(0) skewX(0deg);
		transition: opacity 0.8s ease calc(var(--c) * 110ms), transform 1.1s cubic-bezier(0.16, 1, 0.3, 1) calc(var(--c) * 110ms);
	}

	/*
	 * The leaning entrance moves the corners of a tall item sideways by 0.08 of its height. With a picture an item
	 * is about 310px tall on a phone, so a corner can pass the 16px gutter and the page would scroll sideways.
	 * The section clips that overhang (clip, not hidden, so nothing becomes a scroll container and the pin keeps sticking).
	 */
	.he-motion #work {
		overflow-x: clip;
	}

	/* ---------- 5. Our own work: a pinned horizontal gallery (motion.js adds .he-pin at 900px and wider) ---------- */

	.he-motion-ready .he-pin {
		padding: 0;
		background: var(--he-ink);
		color: var(--he-on-dark);
	}

	.he-motion-ready .he-pin > .he-wrap {
		--he-pan-w: min(62vw, 56rem);
		--he-pan-h: min(60vh, 36rem);
		position: sticky;
		top: 0;
		display: flex;
		flex-direction: column;
		justify-content: center;
		height: 100vh;
		height: 100svh;
		max-width: none;
		/* Same left edge as every other section, however wide the screen. */
		padding-inline: max(var(--he-gutter), calc((100% - var(--he-max)) / 2 + var(--he-gutter)));
		overflow: hidden;
		overflow: clip;
	}

	/* Signed in, the admin bar covers the top 32px: the pin starts below it, and a jump to #work lands below it. */
	.he-motion-ready .admin-bar .he-pin > .he-wrap {
		top: 32px;
		height: calc(100vh - 32px);
		height: calc(100svh - 32px);
	}

	/* The pin only exists from 900px wide; the plain list below that is the same #work section, so it gets the same offset. */
	.he-motion-ready .admin-bar .he-pin,
	.he-motion-ready .admin-bar #work {
		scroll-margin-top: 32px;
	}

	/* WordPress makes the admin bar 46px tall at 782px wide and narrower. */
	@media (max-width: 782px) {
		.he-motion-ready .admin-bar .he-pin,
		.he-motion-ready .admin-bar #work {
			scroll-margin-top: 46px;
		}
	}

	.he-motion-ready .he-pin .he-label {
		color: var(--he-tint);
	}

	.he-motion-ready .he-pin .he-h2 {
		max-width: 16em;
		color: var(--he-on-dark);
		font-size: clamp(2rem, 1rem + 3vw, 3.75rem);
	}

	.he-motion-ready .he-pin .he-work {
		display: flex;
		align-items: stretch;
		gap: clamp(28px, 4vw, 64px);
		width: max-content;
		height: var(--he-pan-h);
		margin: clamp(20px, 4vh, 44px) 0 0;
		will-change: transform;
	}

	/* The panel: the image on top, the title and the text below. */
	.he-motion-ready .he-pin .he-work .he-work__item {
		position: relative;
		isolation: isolate;
		display: grid;
		flex: 0 0 var(--he-pan-w);
		grid-template-columns: minmax(0, 1fr);
		grid-template-rows: minmax(0, 1fr) auto auto;
		grid-template-areas: "media" "title" "text";
		width: var(--he-pan-w);
		/* 0.16 of the height is how far a 9.09 degree lean pushes a corner, so the text clears it. */
		padding: clamp(22px, 4vh, 40px) calc(var(--he-pan-h) * 0.16 + 20px);
		border-top: 0;
		color: var(--he-ink);
		opacity: 1;
		transform: none;
		transition: none;
		animation: none;
	}

	.he-motion-ready .he-pin .he-work .he-work__item::before {
		content: "";
		position: absolute;
		z-index: -1;
		inset: 0 calc(var(--he-pan-h) * 0.08);
		background: var(--he-bg);
		transform: skewX(-9.09deg);
	}

	/*
	 * The image frame leans with the panel. The frame is skewed and clips its picture; the picture is skewed
	 * back, so it stands upright inside the leaning edges. It is wider than the frame by 0.05 of the panel
	 * height each side (a 9.09 degree lean moves a frame corner 0.08 of the frame height, and the frame is
	 * never taller than 0.6 of the panel), so no corner is left bare.
	 */
	.he-motion-ready .he-pin .he-work .he-work__frame {
		grid-area: media;
		position: relative;
		min-height: 0;
		aspect-ratio: auto;
		margin: 0 0 18px;
		transform: skewX(-9.09deg);
	}

	.he-motion-ready .he-pin .he-work .he-work__img {
		position: absolute;
		top: 0;
		left: calc(var(--he-pan-h) * -0.05);
		width: calc(100% + var(--he-pan-h) * 0.1);
		max-width: none;
		height: 100%;
		transform: skewX(9.09deg);
	}

	/*
	 * Where subgrid exists the four panels share their rows, so the four pictures are the same height and the
	 * titles and texts sit on common lines. Elsewhere each panel sizes its own picture from what is left over.
	 */
	@supports (grid-template-rows: subgrid) {
		.he-motion-ready .he-pin .he-work {
			display: grid;
			grid-auto-flow: column;
			grid-auto-columns: var(--he-pan-w);
			grid-template-rows: minmax(0, 1fr) auto auto;
			gap: 0 clamp(28px, 4vw, 64px);
		}

		.he-motion-ready .he-pin .he-work .he-work__item {
			grid-row: 1 / span 3;
			grid-template-rows: subgrid;
		}
	}

	/* The big outlined number sits on the top right corner of the frame. */
	.he-motion-ready .he-pin .he-work__no {
		display: block;
		position: absolute;
		z-index: 2;
		top: calc(clamp(22px, 4vh, 40px) + 12px);
		right: calc(var(--he-pan-h) * 0.16 + 20px + 30px);
		font-family: var(--he-display);
		font-weight: 800;
		font-size: clamp(3rem, 2.5vw + 3vh, 5.5rem);
		line-height: 0.85;
		letter-spacing: -0.02em;
		color: var(--he-bg);
		-webkit-text-stroke: clamp(3px, 0.04em, 5px) var(--he-ink);
		paint-order: stroke fill;
	}

	.he-motion-ready .he-pin .he-card__title {
		grid-area: title;
		align-self: end;
		margin: 0 0 12px;
		font-size: clamp(1.5rem, 0.9rem + 1.5vw, 2.5rem);
		line-height: 1.1;
	}

	/* Charcoal on the light ground of the panel (15:1), 18px and up, a step larger than before. */
	.he-motion-ready .he-pin .he-work__item p {
		grid-area: text;
		max-width: 30em;
		font-size: clamp(1.125rem, 1rem + 0.25vw, 1.3125rem);
		color: var(--he-ink);
	}

	.he-pin__bar {
		position: absolute;
		left: max(var(--he-gutter), calc((100% - var(--he-max)) / 2 + var(--he-gutter)));
		right: max(var(--he-gutter), calc((100% - var(--he-max)) / 2 + var(--he-gutter)));
		bottom: clamp(18px, 5vh, 48px);
		height: 2px;
		background: rgba(242, 240, 239, 0.18);
	}

	.he-pin__bar i {
		display: block;
		height: 100%;
		background: var(--he-tint);
		transform: scaleX(0);
		transform-origin: 0 50%;
	}
}

/* Windows high contrast replaces colours, so outlined type would vanish: no band there. */
@media (forced-colors: active) {
	.he-motion .he-marquee {
		display: none !important;
	}

	/* No band, so no button for it; the hero button stays. The leaning shape is not drawn: a plain border on the button. */
	.he-marquee-wrap .he-motion-toggle {
		display: none !important;
	}

	.he-motion-toggle {
		border: 1px solid ButtonText;
		background: ButtonFace;
		color: ButtonText;
	}

	.he-motion-toggle::before {
		display: none;
	}

	.he-motion-ready .he-motion-toggle:focus-visible {
		outline: 3px solid Highlight;
		outline-offset: 3px;
	}
}
