/* Rotating Gallery: the row, and the carousel it turns into.
 *
 * A single line of pictures that never wraps. Wide enough and they all show at
 * once and there is nothing to operate; too narrow and the line scrolls
 * sideways, snaps, advances on its own and grows a set of controls. The same
 * shortcode therefore works in a full-width card and in a narrow column with
 * nothing set differently.
 *
 * grid-auto-flow: column with grid-auto-columns: minmax(--rg-tile, 1fr) is what
 * does both jobs. 1fr shares the row out when everything fits; the minimum stops
 * twelve pictures becoming twelve slivers and pushes the row into scrolling
 * instead.
 *
 * The scrolling, the snapping and the layout are all CSS. assets/row.js adds
 * only the controls and the automatic advance, and the row still scrolls by
 * drag, wheel and touch without it.
 */

/* THE ROW IS ONE OF TWO ELEMENTS, AND BOTH ARE ADDRESSED HERE.
 *
 * Normally it is core's Gallery block figure, because a picture's lightbox only
 * offers previous and next when it shares a gallery id with the others, and
 * that id is written while a Gallery block renders. So the row is a real
 * gallery: figure.wp-block-gallery.rg-row, with figure.wp-block-image children.
 *
 * If core does not produce that, the PHP falls back to a plain div.rg-row of
 * figure.rg-row__tile wrappers - what every version up to 0.2.11 rendered.
 *
 * WHY THE SELECTORS ARE LONGER THAN THEY LOOK LIKE THEY NEED TO BE.
 *
 * Core styles its gallery as a wrapping flex layout, and it is specific about
 * it: .wp-block-gallery.has-nested-images for the container, and
 * .wp-block-gallery.has-nested-images figure.wp-block-image for the children,
 * which it gives a flex-grow and a calculated width. Those have to be beaten
 * outright, and beaten on specificity rather than on source order, because
 * which stylesheet a site prints last is not something this file can know.
 * Leading with .rg-carousel buys the class that does it.
 *
 * The sizes come from custom properties set on .rg-carousel, not on the row,
 * because when the row is core's figure it is core's element and not ours to
 * put a style attribute on. Custom properties inherit, so it makes no odds.
 */

.rg-carousel {
	position: relative;
}

.rg-carousel .rg-row,
.rg-carousel .rg-row.wp-block-gallery {
	display: grid;
	grid-auto-flow: column;
	grid-auto-columns: minmax(var(--rg-tile, 140px), 1fr);
	gap: var(--rg-gap, 12px);
	margin: 0;
	padding: 0;
	overflow-x: auto;
	overscroll-behavior-x: contain;
	/* mandatory rather than proximity, so a picture is never left half off the
	   edge after a short drag. */
	scroll-snap-type: x mandatory;
	/* The bar is hidden because the controls below say "there is more this way"
	   properly. Nothing about the scrolling itself changes: drag, wheel, touch
	   and the arrow keys all still work. */
	scrollbar-width: none;
	-ms-overflow-style: none;
}

.rg-carousel .rg-row::-webkit-scrollbar {
	display: none;
}

/* Without the scrollbar the row needs to be reachable by keyboard in its own
   right, because otherwise there is no way to scroll it without a pointer. */
.rg-carousel .rg-row:focus-visible {
	outline: 2px solid currentColor;
	outline-offset: 3px;
}

/* The tiles: core's image figures in the gallery, our own wrappers otherwise.
   width and flex-grow are reset because core's gallery gives its children both,
   and a grid item carrying a calculated flex width is neither one thing nor the
   other.

   THE !important IS NOT LAZINESS AND IT IS NOT DECORATION.

   Core defends its gallery widths with :not(#individual-image), which matches
   nothing but lends the whole selector an ID's specificity, so no number of
   classes can outrank it. The PHP now omits has-nested-images, columns-default
   and is-cropped so those rules do not match at all, and this should never be
   needed. It is here because that is a fix on the markup side, and if a future
   WordPress adds any of those classes back during its own render, the row would
   silently collapse to a third-width picture per column - which is exactly what
   0.2.12 did. Two properties, on our own scoped selector, is a cheap guarantee
   against a failure that is invisible until somebody looks at the page. */
.rg-carousel .rg-row > .rg-row__tile,
.rg-carousel .rg-row.wp-block-gallery > figure.wp-block-image {
	margin: 0;
	width: auto !important;
	max-width: none;
	flex-grow: 0;
	scroll-snap-align: start;
	min-width: 0;
}

.rg-carousel .rg-row > .rg-row__tile figure,
.rg-carousel .rg-row > .rg-row__tile .wp-block-image {
	margin: 0;
}

/* Direct children only. Core's lightbox keeps a second copy of every picture
   inside its overlay, and that copy carries the same class; sizing it to the
   tile would shrink the picture the lightbox is meant to be showing full size. */
.rg-carousel .rg-row > .rg-row__tile > img,
.rg-carousel .rg-row > .rg-row__tile > figure > img,
.rg-carousel .rg-row.wp-block-gallery > figure.wp-block-image > img {
	display: block;
	/* Same reason as the tile above: core's own image rule inside a gallery is
	   width:auto; height:auto with a max-width:100%!important of its own, and
	   the cropped variant is ID-weighted. The picture filling its tile is the
	   whole point of the row, so it is not left to a specificity contest. */
	width: 100% !important;
	height: var(--rg-tile-height, 120px) !important;
	max-width: none;
	object-fit: cover;
	border-radius: var(--rg-radius, 6px);
	/* The point set on the picture in the band editor. Written inline per
	   picture by the shortcode; this is only the fallback for one that has
	   never had a point set. */
	object-position: center;
	transition: opacity 160ms ease;
}

/* Core's gallery puts a caption band over the bottom of each picture. There is
   no room for one at this size and no caption is asked for, but a picture that
   happens to carry one would otherwise show it. */
.rg-carousel .rg-row.wp-block-gallery > figure.wp-block-image figcaption {
	display: none;
}

.rg-carousel .rg-row > .rg-row__tile:hover > figure > img,
.rg-carousel .rg-row.wp-block-gallery > figure.wp-block-image:hover > img {
	opacity: 0.9;
}

/* Core's lightbox button is not styled here at all, deliberately.
 *
 * It was in 0.2.6, and the result was a grey bar with an expand icon lying
 * across the top of the pictures. Core's own rule is
 * position:absolute; top:16px; right:16px; width:20px; height:20px; opacity:0,
 * revealed on hover - a small control in the corner. The rules here set
 * display:block and width:100% on it, which beat both the width and the
 * display, so it stretched the full width of the tile and its translucent grey
 * background became the bar. The container is left alone for the same reason:
 * core sets display:flex; flex-direction:column; position:relative on it.
 *
 * If a theme ever does put a border or a background on that button, fix it in
 * the theme. Nothing in this file should reach into core's control again. */

/* ---------------------------------------------------------------- controls */

/* Hidden until row.js has measured the row and found there is something to
   scroll. A row whose pictures all fit needs no controls, and controls that do
   nothing are worse than none.
 *
 * They sit ON the pictures rather than under them. Below the row they took a
 * line of their own, and with the card's own padding underneath that read as a
 * band of dead space at the bottom that the top of the card did not have. Lifted
 * out of the flow, the carousel is exactly as tall as its pictures and the card
 * looks even.
 *
 * Bottom right, because core puts its lightbox button at the top right of each
 * picture and two sets of controls in one corner is nobody's idea of clear. */
.rg-carousel__controls {
	position: absolute;
	right: 10px;
	bottom: 10px;
	z-index: 2;
	display: flex;
	gap: 4px;
	padding: 4px;
	border-radius: 999px;
	/* The strip carries its own colours instead of inheriting them.
	 *
	 * Everywhere else this plugin takes currentColor from the theme, which is
	 * right for something sitting on the page's own background. These sit on
	 * photographs, where the theme's text colour is as likely to be invisible as
	 * not, and a control that disappears against one picture and not the next is
	 * worse than one that does not match the theme. So the strip brings a pale
	 * ground with it and the buttons are dark on that ground - legible on any
	 * photograph, on any theme, light or dark. */
	background: rgba(255, 255, 255, 0.88);
	box-shadow: 0 1px 4px rgba(0, 0, 0, 0.22);
}

@supports (backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px)) {
	.rg-carousel__controls {
		background: rgba(255, 255, 255, 0.72);
		-webkit-backdrop-filter: blur(8px) saturate(140%);
		backdrop-filter: blur(8px) saturate(140%);
	}
}

/* Both of these are load-bearing, and neither is obvious.
 *
 * The browser hides a [hidden] element with a rule in its own stylesheet, and
 * any author rule setting display beats it - so the display:flex above and the
 * display:inline-flex below would each have shown a control that PHP wrote
 * hidden and row.js had not un-hidden. The result would have been arrows on a
 * row with nothing to scroll, and a pause button on a gallery that never
 * moves. Restate the hiding at author level and it holds. */
.rg-carousel__controls[hidden],
.rg-carousel__btn[hidden] {
	display: none;
}

.rg-carousel__btn {
	display: inline-flex;
	align-items: center;
	justify-content: center;
	width: 26px;
	height: 26px;
	padding: 0;
	border: 0;
	border-radius: 999px;
	background: none;
	/* Dark on the pale strip above, not inherited. See the note there. */
	color: #1d1d1f;
	cursor: pointer;
	opacity: 0.6;
	transition: opacity 150ms ease, background-color 150ms ease;
}

.rg-carousel__btn:hover,
.rg-carousel__btn:focus-visible {
	opacity: 1;
	background-color: rgba(0, 0, 0, 0.08);
}

.rg-carousel__btn:focus-visible {
	outline: 2px solid #1d1d1f;
	outline-offset: 1px;
}

.rg-carousel__btn svg {
	width: 15px;
	height: 15px;
	fill: currentColor;
	pointer-events: none;
}

/* One button, two icons, swapped on the state row.js writes to the wrapper.
   Doing it in CSS keeps the markup the same before and after the script runs. */
.rg-carousel__btn .rg-icon--play {
	display: none;
}

.rg-carousel[data-rg-paused="true"] .rg-carousel__btn .rg-icon--play {
	display: block;
}

.rg-carousel[data-rg-paused="true"] .rg-carousel__btn .rg-icon--pause {
	display: none;
}

@media (prefers-reduced-motion: reduce) {
	.rg-carousel .rg-row {
		scroll-behavior: auto;
	}

	.rg-carousel .rg-row > .rg-row__tile > img,
	.rg-carousel .rg-row > .rg-row__tile > figure > img,
	.rg-carousel .rg-row.wp-block-gallery > figure.wp-block-image > img {
		transition: none;
	}
}
