/* ===========================================================================
   PHOTO VIEWER — the full-screen, browsable view of a set of photographs
   Markup: vub_photo_viewer() in inc/helpers.php. Behaviour:
   assets/js/photo-viewer.js. Loaded only where a page prints the viewer.

   The whole photograph, never cropped or stretched, as large as the room
   left for it allows (never past its own size), the count at the top left,
   close at the top right, previous and next at the sides and the caption
   underneath. A native modal <dialog> covering the screen: the page behind
   cannot be reached and holds still.
   =========================================================================== */

.vub-viewer {
	/* The bar along the top, the room kept either side of the photograph for
	   the arrows, and the caption's line under it. */
	--vub-viewer-bar: 4rem;
	--vub-viewer-side: clamp(0.75rem, 6vw, 6rem);
	--vub-viewer-foot: clamp(1rem, 3vh, 2rem);
	--vub-viewer-caption: 2.75rem;
	--vub-viewer-max-h: calc(100vh - var(--vub-viewer-bar) - var(--vub-viewer-caption) - var(--vub-viewer-foot));

	position: fixed;
	inset: 0;
	width: 100%;
	height: 100%;
	max-width: none;
	max-height: none;
	margin: 0;
	padding: 0;
	border: 0;
	background-color: rgba(8, 9, 10, 0.95);
	color: var(--vub-white);
	text-shadow: none;
	overflow: hidden;
	overscroll-behavior: contain;

	/* In and out with a short fade. Browsers without @starting-style simply
	   show and hide it. */
	opacity: 0;
	transition-property: opacity, overlay, display;
	transition-duration: var(--vub-duration);
	transition-timing-function: var(--vub-ease);
	transition-behavior: allow-discrete;
}

@supports (height: 100dvh) {
	.vub-viewer {
		--vub-viewer-max-h: calc(100dvh - var(--vub-viewer-bar) - var(--vub-viewer-caption) - var(--vub-viewer-foot));
	}
}

.vub-viewer[open] {
	display: flex;
	flex-direction: column;
	opacity: 1;
}

@starting-style {
	.vub-viewer[open] { opacity: 0; }
}

.vub-viewer::backdrop {
	background-color: rgba(8, 9, 10, 0.6);
	-webkit-backdrop-filter: blur(6px);
	backdrop-filter: blur(6px);
}

.vub-viewer [hidden] { display: none !important; }

/* --- The bar: the count, and close --------------------------------------- */
.vub-viewer__bar {
	flex: 0 0 var(--vub-viewer-bar);
	display: flex;
	align-items: center;
	justify-content: space-between;
	padding-inline: clamp(1rem, 2.5vw, 1.5rem);
}

.vub-theme .vub-viewer__count {
	margin: 0;
	font-size: var(--vub-text-sm);
	font-weight: var(--vub-weight-semibold);
	font-variant-numeric: tabular-nums;
	letter-spacing: var(--vub-tracking-wide);
	color: var(--vub-text-muted);
}

/* Close, previous and next: the site's square buttons, dark until pointed at. */
.vub-theme .vub-viewer__close,
.vub-theme .vub-viewer__nav {
	display: grid;
	place-items: center;
	width: 3rem;
	height: 3rem;
	padding: 0;
	border: 1px solid rgba(255, 255, 255, 0.45);
	border-radius: var(--vub-radius-button);
	background-color: rgba(8, 9, 10, 0.7);
	color: var(--vub-white);
	cursor: pointer;
	transition: var(--vub-transition-color);
}

.vub-theme .vub-viewer__close:hover,
.vub-theme .vub-viewer__close:focus-visible,
.vub-theme .vub-viewer__nav:hover,
.vub-theme .vub-viewer__nav:focus-visible {
	border-color: var(--vub-red);
	background-color: var(--vub-red);
	color: var(--vub-white);
}

.vub-theme .vub-viewer__close:focus-visible,
.vub-theme .vub-viewer__nav:focus-visible {
	outline: 2px solid var(--vub-focus);
	outline-offset: 3px;
}

.vub-viewer__close svg,
.vub-viewer__nav svg {
	width: 1.25rem;
	height: 1.25rem;
	fill: none;
	stroke: currentColor;
	stroke-width: 1.8;
}

/* Halfway down the room the photograph has, either side of it. */
.vub-theme .vub-viewer__nav {
	position: absolute;
	top: calc(var(--vub-viewer-bar) + (100% - var(--vub-viewer-bar) - var(--vub-viewer-caption) - var(--vub-viewer-foot)) / 2);
	z-index: 1;
	transform: translateY(-50%);
}

.vub-viewer__nav--prev { left: clamp(0.5rem, 2vw, 1.5rem); }
.vub-viewer__nav--next { right: clamp(0.5rem, 2vw, 1.5rem); }

/* --- The photograph ------------------------------------------------------ */
.vub-viewer__stage {
	flex: 1 1 auto;
	min-height: 0;
	display: flex;
	align-items: flex-start;
	justify-content: center;
	padding: 0 var(--vub-viewer-side) var(--vub-viewer-foot);
	/* Sideways belongs to the swipe; up and down, and pinching, stay the
	   browser's. */
	touch-action: pan-y pinch-zoom;
	-webkit-user-select: none;
	user-select: none;
}

.vub-viewer__figure {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	gap: var(--vub-space-xs);
	width: 100%;
	height: 100%;
	margin: 0;
	transition: transform var(--vub-duration) var(--vub-ease-out);
}

.vub-viewer__figure.is-dragging { transition: none; }

/* Sized from the full file's own width and height (set by the script), so
   the thumbnail standing in and the photograph that replaces it fill the same
   box: as wide as the room allows, no taller than the room, never larger than
   the file itself. object-fit: contain keeps it whole either way. */
.vub-viewer__img {
	display: block;
	flex: 0 0 auto;
	aspect-ratio: var(--vub-viewer-w) / var(--vub-viewer-h);
	width: min(100%, calc(var(--vub-viewer-w) * 1px), calc(var(--vub-viewer-max-h) * var(--vub-viewer-w) / var(--vub-viewer-h)));
	height: auto;
	max-width: 100%;
	max-height: var(--vub-viewer-max-h);
	object-fit: contain;
	box-shadow: 0 30px 80px rgba(0, 0, 0, 0.6);
	-webkit-user-drag: none;
}

.vub-theme .vub-viewer__caption {
	flex: 0 0 auto;
	max-width: 60ch;
	min-height: 1.5em;
	font-size: var(--vub-text-sm);
	line-height: var(--vub-leading-normal);
	color: var(--vub-text);
	text-align: center;
}

/* A new photograph slides in a little from the side it came from. */
.vub-viewer__figure.is-from-next { animation: vub-viewer-from-next var(--vub-duration-slow) var(--vub-ease-out); }
.vub-viewer__figure.is-from-prev { animation: vub-viewer-from-prev var(--vub-duration-slow) var(--vub-ease-out); }

@keyframes vub-viewer-from-next {
	from { opacity: 0.35; transform: translateX(1.5rem); }
	to   { opacity: 1;    transform: none; }
}

@keyframes vub-viewer-from-prev {
	from { opacity: 0.35; transform: translateX(-1.5rem); }
	to   { opacity: 1;    transform: none; }
}

/* The page under an open viewer holds still. The gutter is kept, so where a
   scrollbar takes room nothing shifts sideways when it goes. */
html.vub-viewer-open {
	overflow: hidden;
	scrollbar-gutter: stable;
}

/* Phones: the arrows sit over the photograph's edges, smaller. */
@media (max-width: 640px) {
	.vub-viewer { --vub-viewer-bar: 3.5rem; }

	.vub-theme .vub-viewer__close,
	.vub-theme .vub-viewer__nav {
		width: 2.75rem;
		height: 2.75rem;
	}
}

@media (prefers-reduced-motion: reduce) {
	.vub-viewer,
	.vub-viewer__figure { transition: none; }

	.vub-viewer__figure.is-from-next,
	.vub-viewer__figure.is-from-prev { animation: none; }
}
