/**
 * The viewer, on the page.
 *
 * The stylesheet's default is the no-script state: the cover, at a sensible
 * size, behaving like any other picture. Everything interactive keys off
 * `data-js="on"`, which the engine sets once it is actually wired — so nothing
 * here ever promises a gesture the page cannot answer.
 *
 * The corner is the FRAME's, and its default is still the Design System's. That
 * plugin emits a global `figure img { border-radius: var(--ds-radius-sm) }` and
 * this viewer is a figure wrapping an img, so until a placement could choose a
 * corner the picture simply inherited one and there was nothing to wire up. Now
 * that a placement can, the corner has to sit on the box that clips — otherwise
 * the inherited corner sits inside the chosen one with a wedge of background
 * between them. The frame reads the same token for its default, so a site with
 * the Design System looks exactly as it did and a site without still gets a
 * square corner. What is NOT done here, then or now, is inventing a number.
 *
 * The two radii below are the SHAPE OF A CONTROL rather than the styling of a
 * surface: a play button is round and a hint is a pill wherever they appear,
 * the way a radio button is round everywhere. They are not the site's to set.
 *
 * @package Wafflestone\Immersive
 */

/* The viewer's own furniture: a play button, a Reset, an AR button and the drag
 * hint. They are drawn at different moments and by different layers — two by
 * the server, one by the engine, one inside the library's own element — so
 * what makes them one set is stated here rather than repeated at each.
 *
 * The shape carries the meaning: everything you can press is fully rounded, so
 * a circle is an icon-only control and a pill is one that says what it does.
 * The hint is the only thing here that is NOT pressable, and it is the only
 * thing that is not round. */
:root,
.wfst-imm-viewer {
	--wfst-imm-inset: 8px;
	--wfst-imm-control: 40px;
	/* Dark chips, white marks, and a soft shadow to lift them off whatever is
	 * behind. What IS behind cannot be measured from here — a photograph, a
	 * model on a color somebody typed, the page itself — so the chip carries
	 * its own contrast. A hairline edge was tried and rejected: it drew a line
	 * round everything, and the shadow does the same job without one. */
	--wfst-imm-chrome: rgba( 0, 0, 0, 0.55 );
	--wfst-imm-chrome-lift: rgba( 0, 0, 0, 0.75 );
	--wfst-imm-chrome-ink: #fff;
	--wfst-imm-chrome-text: 0.8125em;
	--wfst-imm-chrome-edge: 0 1px 4px rgba( 0, 0, 0, 0.3 );
}

/* THE FRAME, and the two levels every part of it has.
 *
 * There are THREE levels, and each falls back to the one below:
 *
 *   --wfst-imm-*         what one PLACEMENT asked for
 *   --wfst-imm-photo-*   what the PHOTO was set to, or the site-wide default
 *   --wfst-imm-own-*     what this KIND is — a band is 16:9 and crops, a spin
 *                        is its frames' own shape and is shown whole
 *
 * The middle one is why the chain is three deep rather than two. A placement
 * that says nothing has to show what the photo itself says, and a placement
 * that says something at one screen width and nothing at another has to fall
 * back to the photo there rather than to the bare kind.
 *
 * Both levels are properties, and nothing here is a class, because a
 * per-screen-size rule outranks any class: a class such a rule switched off at
 * one width could never be put back at another. Written as properties, the
 * rule sets the placement level at one width and writes it back to `initial`
 * at the next, and the kind's own value returns by itself.
 *
 * A shape travels as one value written as `4 / 3`, which reads correctly both
 * as an aspect-ratio and as the multiplier in the calc below. */
.wfst-imm-viewer {
	margin: 0;
	width: 100%;

	/* THE CAP, in three levels.
	 *
	 * `-asked` is what this placement wants, `-own` what the kind wants, and
	 * the unsuffixed one is what is in force. A kind that adjusts the cap (the
	 * model puts a floor under it) overrides the one in force, so every reader
	 * gets the adjusted number and three separate readings cannot drift apart.
	 *
	 * There are TWO of them, and there must be: a cap in per cent is a share of
	 * a HEIGHT, and multiplying it by a shape would give a share of a width — a
	 * number that means nothing. The renderer writes the width one only for a
	 * cap that can be multiplied, so a cap in per cent still limits the height
	 * and simply implies no width.
	 */
	--wfst-imm-used-cap: var( --wfst-imm-cap-asked, var( --wfst-imm-own-cap ) );
	--wfst-imm-width-cap: var( --wfst-imm-width-cap-asked, var( --wfst-imm-own-cap ) );

	/* How wide the cap leaves the box, once the shape is taken into account. A
	 * cap has to NARROW the box rather than flatten it, or the shape drawn is
	 * not the shape asked for. Derived, so it exists only when there is a cap
	 * AND a shape to work it out from: otherwise it reads an undefined
	 * property, the declaration is invalid, and max-width below takes its own
	 * fallback instead. */
	--wfst-imm-implied-width: calc(
		var( --wfst-imm-width-cap )
		* ( var( --wfst-imm-shape, var( --wfst-imm-photo-shape, var( --wfst-imm-own-shape ) ) ) )
	);

	/* The narrowest of what this kind is, what the placement asked for, and
	 * what the cap leaves. */
	max-width: min(
		var( --wfst-imm-own-width, 640px ),
		var( --wfst-imm-max-width, 100% ),
		var( --wfst-imm-implied-width, 100% )
	);
	position: relative;

	/* The same corner as the frame inside, on the outer box that carries
	 * whatever the Customize tab put here.
	 *
	 * This box paints nothing of its own — Behind paints the frame, one level
	 * in. But a class an author adds in that tab lands HERE, so a background it
	 * carries is painted on this box and not on the frame, and was squared off
	 * at the corners while the frame beside it was round. A border-radius clips
	 * this box's own background to the same curve without touching the frame's
	 * shadow, which an overflow would have cut off. */
	border-radius: var( --wfst-imm-radius, var( --ds-radius-sm, 0 ) );
}

.wfst-imm-viewer__stage {
	position: relative;
	aspect-ratio: var( --wfst-imm-shape, var( --wfst-imm-photo-shape, var( --wfst-imm-own-shape, auto ) ) );
	max-height: var( --wfst-imm-used-cap, none );

	/* What is drawn behind the picture. It shows through wherever the picture
	 * does not reach: while it loads, around a model, through a transparent
	 * spin frame. Empty by default, so the page shows through. */
	background: var( --wfst-imm-behind, var( --wfst-imm-photo-behind, transparent ) );

	/* The corner lives HERE, on the frame, and not on the picture.
	 *
	 * Its default is still the Design System's, read from that plugin's own
	 * token: this is the same opinion, not a second one. On a site without the
	 * Design System the token is undefined and the corner is square, which is
	 * what such a site had before.
	 *
	 * It moved off the picture because the frame is what a placement can now
	 * round. Left on the picture, the Design System's small corner sat INSIDE
	 * whatever corner the placement chose, with the frame's own background
	 * showing in the wedge between them — so a radius control would have looked
	 * like it did almost nothing on exactly the sites that have one. */
	border-radius: var( --wfst-imm-radius, var( --ds-radius-sm, 0 ) );
	box-shadow: var( --wfst-imm-shadow, none );

	/* What makes the corner actually cut the picture, and what keeps a band a
	 * window rather than a full picture. */
	overflow: hidden;
}

/* The caption, when a placement asked for one. Its default color and size are
 * the site's own: a figcaption is something every design system already has an
 * opinion about, and this one has none of its own to add. */
/* TWO classes, not one. The Design System's stack colors and spaces every
 * caption on the page through `.x-wfst-ds figure figcaption` and `.ds-prose
 * figure figcaption`, both (0,1,2) — so a single class (0,1,0) loses to them
 * and the Gap control visibly does nothing. The Image element carries the same
 * scar and the same fix. */
.wfst-imm-viewer .wfst-imm-viewer__caption {
	text-align: var( --wfst-imm-caption-align, inherit );
	color: var( --wfst-imm-caption-color, inherit );
	/* Default is not zero. It is what the design gives a caption, which is the
	 * same rung the Image element's caption sits at — so two captions on one
	 * page stand off their pictures by the same amount without anybody setting
	 * anything. */
	margin-top: var( --wfst-imm-caption-gap, var( --ds-space-2, 0.5rem ) );
	/* And Default here is the frame's own corner, so the words start where the
	 * curve has finished rather than tucking under it. Also the Image
	 * element's answer. */
	padding-inline: var( --wfst-imm-caption-inset, var( --wfst-imm-radius, var( --ds-radius-sm, 0 ) ) );
}

/* The bold lead-in. Its weight is the only thing set here: its size comes from
 * whatever role the caption itself is wearing, so the two cannot disagree. */
.wfst-imm-viewer .wfst-imm-viewer__caption-title {
	font-weight: 600;
	color: var( --wfst-imm-caption-title-color, inherit );
}

.wfst-imm-viewer__image {
	display: block;
	/* Square, because the frame above carries the corner and clips to it. Two
	 * corners of different sizes is the wedge described there. */
	border-radius: 0;
	width: 100%;
	height: var( --wfst-imm-own-img-height, auto );
	-webkit-user-drag: none;
	/* An embed asking to be no taller than N gets it here, on the picture. Put
	 * on the figure alone the picture kept its own height and hung out of the
	 * bottom, over whatever came next on the page. */
	max-height: var( --wfst-imm-used-cap, none );
	/* `contain` keeps the whole of a spin visible while it shrinks; a band says
	 * `cover` instead, because a band is a window and cropping is the point. */
	object-fit: var( --wfst-imm-fit, var( --wfst-imm-own-fit, contain ) );
}

/* A panorama is a band you look along, so it is as wide as it is given and
 * shows a 16:9 slice of a picture that is at least 2:1. The slice is what
 * makes there be anywhere to pan TO: a band the shape of the picture would
 * show all of it at once and there would be nothing left to look for.
 *
 * The picture fills the band and is placed by `object-position`, which takes
 * per cent along — the same units the cover is stored in and the same ones the
 * engine writes. No measuring on either side, and the resting slice is right
 * before any script runs. */
.wfst-imm-viewer--panorama {
	--wfst-imm-own-width: 100%;
	--wfst-imm-own-shape: 16 / 9;
	--wfst-imm-own-fit: cover;
	--wfst-imm-own-img-height: 100%;
}

/* A 360° photo is a window into somewhere, so it gets a band the same shape a
 * panorama does. The size has to be HERE rather than left to the picture
 * inside: the library is handed a box and draws to fill it, so a box with no
 * height of its own gets a sphere with no height either.
 *
 * The cut sits under that box and stays there. Until the sphere reports itself
 * drawn there is nothing to see in front of it, and if it never manages to draw
 * the reader is left looking at a photograph rather than a hole. */
.wfst-imm-viewer--sphere {
	--wfst-imm-own-width: 100%;
	--wfst-imm-own-shape: 16 / 9;
	--wfst-imm-own-fit: cover;
	--wfst-imm-own-img-height: 100%;
}

/* Which of these sits on top, stated here rather than left to the order
 * things happen to appear in. The library's layer is put on the stage by SCRIPT, after
 * the server's own controls are already sitting there — so left to document
 * order it paints over them, and the play button and Reset stop being
 * clickable. Not hidden: still drawn, still inviting a press, and inert.
 *
 * The AR button is the exception and needs no rule of its own. It lives inside
 * the library's element, so it rides on that layer. */
.wfst-imm-viewer__sphere,
.wfst-imm-viewer__model {
	z-index: 0;
}

.wfst-imm-viewer__play,
.wfst-imm-viewer__reset,
.wfst-imm-viewer__hint {
	z-index: 1;
}

.wfst-imm-viewer__sphere {
	position: absolute;
	inset: 0;
	opacity: 0;
}

.wfst-imm-viewer--sphere[data-sphere="open"] .wfst-imm-viewer__sphere {
	opacity: 1;
}

/* A 3D model rests on its poster and is drawn over it once the library has
 * opened it, exactly as a 360° photo is. The stage takes the poster's own
 * shape, because the poster was cropped to the model's: the model then opens
 * into a box the same shape, and nothing jumps.
 *
 * The poster decides the size, and the viewport caps it: a standing figure is
 * taller than it is wide, and at the full width of a page it would be two
 * screens high. The stage shrinks to the picture, so the box the model opens
 * into is the picture's box and not the page's. */
/* A model carries no size of its own: unlike a spin, nothing about a GLB says
 * how big it should be drawn. Its own shape is its POSTER's, which the renderer
 * hands over, and its own cap is most of the viewport — a standing figure is
 * taller than it is wide, and at the full width of a page it would be two
 * screens high.
 *
 * The floor stops a cap somebody typed from crushing it to nothing. */
.wfst-imm-viewer--model {
	--wfst-imm-own-width: 100%;
	/* The floor goes on the cap IN FORCE, not beside it, so the box, the
	 * picture and the width the shape implies all read the same number. */
	--wfst-imm-used-cap: max( 240px, min( 80vh, var( --wfst-imm-cap-asked, 80vh ) ) );
	--wfst-imm-width-cap: max( 240px, min( 80vh, var( --wfst-imm-width-cap-asked, 80vh ) ) );
	margin-inline: auto;
}

.wfst-imm-viewer--model .wfst-imm-viewer__image {
	/* The picture fills the box rather than keeping its own height: the box is
	 * already the poster's shape, and the library draws the model into that
	 * same box, so the still picture and the live one are framed alike. */
	height: 100%;
}

.wfst-imm-viewer__model {
	position: absolute;
	inset: 0;
	width: 100%;
	height: 100%;
	opacity: 0;
	background: transparent;
	--poster-color: transparent;
	-webkit-user-drag: none;

	/* The library's own loading bar, off. It cannot do its job here: it lives
	 * inside this box, and this box is transparent until the model reports
	 * itself drawn — so the bar is invisible for the whole download and then
	 * appears already full, for a second, over a model that has arrived. Five
	 * pixels of dark gray hard against the top edge reads as a scroll bar. The
	 * poster IS the loading state, which is the same reason the 360° photo
	 * never shows its library's loading ring. */
	--progress-bar-height: 0;
}

.wfst-imm-viewer--model[data-model="open"] .wfst-imm-viewer__model {
	opacity: 1;
}

/* Until the model is drawn its box is invisible, and an invisible box must not
 * take a tap. A model whose file never arrives — deleted, or a bad deploy —
 * would otherwise leave the AR button sitting over the poster, unseen and still
 * pressable. Fading is not hiding, and the library puts nothing of its own in
 * the way of this rule reaching the button. */
.wfst-imm-viewer--model:not([data-model="open"]) .wfst-imm-viewer__model {
	pointer-events: none;
}

/* Once the model is drawn, the poster under it has to go. A sphere covers its
 * cut because a sphere is opaque everywhere; a model is drawn on NOTHING, so a
 * poster left behind it shows through around and between the model's own edges
 * as a second figure, still at the angle the poster was taken. Hidden rather
 * than removed: the poster is what gives the stage its size. */
.wfst-imm-viewer--model[data-model="open"] .wfst-imm-viewer__image {
	visibility: hidden;
}

/* The AR button is ours. The library only decides whether it is shown at all,
 * which it does per device: nothing appears on a desktop. */
/* Top right, and it is the only thing up there. The bottom edge already has
 * three tenants — Reset on the left, the drag hint in the middle, the play
 * button on the right — and this is the one control that cannot join a row
 * with them, because it lives inside the library's own element rather than on
 * the figure. Sharing the bottom-right corner with the play button put the
 * play button on top of this one's last word. */
/* The stand-in drawn where the library's own AR button would go, on a screen
 * that could never make one. Unpressable in the stylesheet as well as in the
 * markup: a preview that took a press would do nothing, which is a worse lie
 * than showing nothing at all. */
.wfst-imm-viewer__ar.is-preview {
	pointer-events: none;
	cursor: default;
}

.wfst-imm-viewer__ar {
	position: absolute;
	right: var( --wfst-imm-inset );
	top: var( --wfst-imm-inset );
	display: flex;
	align-items: center;
	gap: 0.45em;
	height: var( --wfst-imm-control );
	padding: 0 0.9em;
	border: 0;
	border-radius: 999px;
	font: inherit;
	font-size: var( --wfst-imm-chrome-text );
	color: var( --wfst-imm-chrome-ink );
	background: var( --wfst-imm-chrome );
	cursor: pointer;
	box-shadow: var( --wfst-imm-chrome-edge );
}

.wfst-imm-viewer__mark {
	flex: none;
	fill: currentColor;
}

.wfst-imm-viewer__ar:hover,
.wfst-imm-viewer__ar:focus-visible,
.wfst-imm-viewer__reset:hover,
.wfst-imm-viewer__reset:focus-visible {
	background: var( --wfst-imm-chrome-lift );
}

/* Script live. `pan-y` is load-bearing: without it a vertical swipe on a phone
 * is swallowed by the drag handler and the page stops scrolling. */
.wfst-imm-viewer[data-js="on"] {
	cursor: grab;
	touch-action: pan-y;
	user-select: none;
	-webkit-user-select: none;
}

.wfst-imm-viewer[data-js="on"].is-dragging {
	cursor: grabbing;
}

/* A facade shows its nudge before the engine exists, because the nudge is the
 * only thing telling a reader there is anything to wake. Every other viewer
 * waits for `data-js` first. */
.wfst-imm-viewer.is-facade {
	cursor: pointer;
}

.wfst-imm-viewer.is-facade .wfst-imm-viewer__hint,
.wfst-imm-viewer[data-js="on"]:not(.is-dragging) .wfst-imm-viewer__hint {
	display: block;
	position: absolute;
	left: 50%;
	/* Centered on the same line as the two corner buttons rather than sitting on
	 * the floor beneath them: its bottom edge goes to their middle, and it is
	 * then pushed down by half its own height, which is the one measurement
	 * this cannot know in advance. */
	bottom: calc( var( --wfst-imm-inset ) + var( --wfst-imm-control ) / 2 );
	transform: translate( -50%, 50% );
	padding: 0.3em 0.9em;
	/* The one thing here that is not fully rounded, because it is the one thing
	 * that is not a control. Everything round can be pressed; this can only be
	 * read. */
	border-radius: 3px;
	background: var( --wfst-imm-chrome );
	color: var( --wfst-imm-chrome-ink );
	font-size: var( --wfst-imm-chrome-text );
	white-space: nowrap;
	pointer-events: none;
	box-shadow: var( --wfst-imm-chrome-edge );
}

/* The cover alone does not say "this one moves". The hint says it until the
 * first touch, and never appears at all without the engine behind it. */
.wfst-imm-viewer__hint {
	display: none;
}


/* The play button is server-rendered when the photo offers one, so it is in the
 * markup before the engine runs — a reader tabbing straight to it finds a real
 * button rather than nothing. */
.wfst-imm-viewer__play {
	position: absolute;
	right: var( --wfst-imm-inset );
	bottom: var( --wfst-imm-inset );
	width: var( --wfst-imm-control );
	height: var( --wfst-imm-control );
	padding: 0;
	border: 0;
	border-radius: 50%;
	background: var( --wfst-imm-chrome );
	color: var( --wfst-imm-chrome-ink );
	cursor: pointer;
	display: flex;
	align-items: center;
	justify-content: center;
	box-shadow: var( --wfst-imm-chrome-edge );
}

/* Put it back where it started. Bottom LEFT, because the play button and the
 * AR button both take the right-hand corner. Shown only once the reader has
 * moved the framing, which the engine decides — until then it is hidden, and a
 * hidden button is not in anybody's way, including the keyboard's. */
.wfst-imm-viewer__reset {
	position: absolute;
	left: var( --wfst-imm-inset );
	bottom: var( --wfst-imm-inset );
	display: flex;
	align-items: center;
	justify-content: center;
	width: var( --wfst-imm-control );
	height: var( --wfst-imm-control );
	padding: 0;
	border: 0;
	border-radius: 50%;
	color: var( --wfst-imm-chrome-ink );
	background: var( --wfst-imm-chrome );
	cursor: pointer;
	box-shadow: var( --wfst-imm-chrome-edge );
}

/* The `hidden` attribute is what the engine toggles, and an author `display`
 * beats it: the rule above says `display: flex` to center the mark, which
 * quietly made the button visible from the moment the page rendered. */
.wfst-imm-viewer__reset[hidden] {
	display: none;
}

.wfst-imm-viewer__reset:hover,
.wfst-imm-viewer__reset:focus-visible {
	background: var( --wfst-imm-chrome-lift );
}

.wfst-imm-viewer__play:hover,
.wfst-imm-viewer__play:focus-visible {
	background: rgba( 0, 0, 0, 0.75 );
}

/* The mark is drawn rather than lettered so it needs no font and no image, and
 * flips between a triangle and a square off the pressed state alone. */
.wfst-imm-viewer__play-mark {
	width: 0;
	height: 0;
	border-style: solid;
	border-width: 7px 0 7px 12px;
	border-color: transparent transparent transparent currentColor;
	margin-left: 3px;
}

.wfst-imm-viewer__play.is-playing .wfst-imm-viewer__play-mark {
	width: 12px;
	height: 12px;
	border: 0;
	background: currentColor;
	margin-left: 0;
}

/* Nothing moves on its own for a reader who asked for less motion. The cover is
 * what they get, and dragging still works — a gesture they made is not motion
 * that started by itself. */
@media ( prefers-reduced-motion: reduce ) {
	.wfst-imm-viewer[data-js="on"] .wfst-imm-viewer__image {
		transition: none;
	}
}

/* An author pointed at a photo that is not there. Visitors get nothing at all;
 * this is only ever rendered for somebody who can fix it. */
.wfst-imm-ref-notice {
	display: inline-block;
	padding: 0.4em 0.8em;
	border-left: 4px solid #dba617;
	background: #fcf9e8;
	color: #1d2327;
	font-style: italic;
}

/* A block of photos rendered on another plugin's behalf. It sets no grid: the
 * consumer owns its own layout, and a provider imposing one here would be
 * deciding something that is not its to decide. */
.wfst-imm-block > .wfst-imm-viewer + .wfst-imm-viewer {
	margin-top: 1.5rem;
}
