/* Gallery (docs/widgets/gallery.md), in the design language (spec §3.7). This is Soft, the default:
   images with rounded corners and no frame, captions in the soft text colour, filters as pills that
   fill with a tint of the accent when chosen, and a light viewer. gallery-outline.css and
   gallery-solid.css change only the --hafe-gallery-d-* tokens below, so the Style tab still wins. */
.hafe-gallery {
	--hafe-gallery-d-radius: var(--hafe-radius-sm);
	--hafe-gallery-d-line: transparent;
	--hafe-gallery-d-line-width: 0px;
	--hafe-gallery-d-mat: 0px;
	--hafe-gallery-d-ground: var(--hafe-tint);
	--hafe-gallery-d-scrim: rgb(14 16 20 / 64%);
	--hafe-gallery-d-filter-ink: var(--hafe-ink-soft);
	--hafe-gallery-d-filter-bg: transparent;
	--hafe-gallery-d-filter-line: transparent;
	--hafe-gallery-d-filter-hover: var(--hafe-tint);
	--hafe-gallery-d-filter-now-ink: var(--hafe-accent-ink);
	--hafe-gallery-d-filter-now-bg: var(--hafe-tint);
	--hafe-gallery-d-filter-now-line: transparent;
	--hafe-gallery-d-filter-radius: var(--hafe-pill);
	--hafe-gallery-d-more-ink: var(--hafe-accent-ink);
	--hafe-gallery-d-more-bg: var(--hafe-tint);
	--hafe-gallery-d-more-line: transparent;
	--hafe-gallery-d-button-bg: var(--hafe-surface);
	--hafe-gallery-d-button-line: var(--hafe-line);
	--hafe-gallery-gap-used: var(--hafe-gallery-gap, 0.75rem);
	color: var(--hafe-ink);
}

/* The filters: a row of buttons the script shows. */
.hafe-gallery__filters {
	display: flex;
	flex-wrap: wrap;
	justify-content: var(--hafe-gallery-filter-align, center);
	gap: 0.5rem;
	margin-block-end: var(--hafe-gallery-filter-space, 1.5rem);
}

.hafe-gallery:not(.is-ready) .hafe-gallery__filters,
.hafe-gallery:not(.is-ready) .hafe-gallery__more {
	display: none;
}

/* Hovered and focused too (spec §4). */
.hafe-gallery .hafe-gallery__filter,
.hafe-gallery .hafe-gallery__filter:is(:hover, :focus),
.hafe-gallery .hafe-gallery__more,
.hafe-gallery .hafe-gallery__more:is(:hover, :focus) {
	margin: 0;
	padding: 0.5em 1.1em;
	border: 1px solid var(--hafe-gallery-filter-line-used, var(--hafe-gallery-d-filter-line));
	border-radius: var(--hafe-gallery-filter-radius, var(--hafe-gallery-d-filter-radius));
	background: var(--hafe-gallery-filter-bg, var(--hafe-gallery-d-filter-bg));
	color: var(--hafe-gallery-filter-ink, var(--hafe-gallery-d-filter-ink));
	font: inherit;
	font-size: 0.9375em;
	font-weight: 600;
	line-height: 1.3;
	text-transform: none;
	letter-spacing: normal;
	box-shadow: none;
	cursor: pointer;
	transition: background-color 0.15s, color 0.15s, border-color 0.15s;
}

.hafe-gallery .hafe-gallery__filter:hover {
	background: var(--hafe-gallery-filter-bg, var(--hafe-gallery-d-filter-hover));
	color: var(--hafe-gallery-filter-ink, var(--hafe-ink));
}

.hafe-gallery .hafe-gallery__filter[aria-pressed="true"] {
	--hafe-gallery-filter-line-used: var(--hafe-gallery-d-filter-now-line);
	background: var(--hafe-gallery-filter-now-bg, var(--hafe-gallery-d-filter-now-bg));
	color: var(--hafe-gallery-filter-now-ink, var(--hafe-gallery-d-filter-now-ink));
}

/* Buttons keep their own ring, whatever a theme or another plugin does to buttons. */
.hafe-gallery .hafe-gallery__filter:focus-visible,
.hafe-gallery .hafe-gallery__more:focus-visible,
.hafe-gallery .hafe-gallery__viewer button:focus-visible {
	outline: 2px solid var(--hafe-accent);
	outline-offset: 2px;
}

/* The list. Each image is a figure; its frame holds the image in its shape. */
.hafe-gallery__items {
	display: grid;
	grid-template-columns: repeat(var(--hafe-gallery-columns, 4), minmax(0, 1fr));
	align-items: start;
	gap: var(--hafe-gallery-gap-used);
	margin: 0;
	padding: 0;
	list-style: none;
}

.hafe-gallery__item {
	min-inline-size: 0;
	margin: 0;
}

.hafe-gallery__item::marker {
	content: none;
}

/* Only when scripts run: images outside the chosen filter, or past those shown at first. */
@media (scripting: enabled) {
	.hafe-gallery .hafe-gallery__item.is-out,
	.hafe-gallery .hafe-gallery__item.is-later {
		display: none;
	}
}

.hafe-gallery__figure {
	position: relative;
	margin: 0;
}

.hafe-gallery .hafe-gallery__frame {
	display: block;
	position: relative;
	box-sizing: border-box;
	aspect-ratio: var(--hafe-gallery-r);
	overflow: hidden;
	padding: var(--hafe-gallery-d-mat);
	border: var(--hafe-gallery-line-width, var(--hafe-gallery-d-line-width)) solid var(--hafe-gallery-line, var(--hafe-gallery-d-line));
	border-radius: var(--hafe-gallery-radius, var(--hafe-gallery-d-radius));
	background: var(--hafe-gallery-d-ground);
	color: inherit;
	text-decoration: none;
}

.hafe-gallery--grid .hafe-gallery__frame {
	aspect-ratio: var(--hafe-gallery-ratio, 1);
}

.hafe-gallery .hafe-gallery__image {
	display: block;
	inline-size: 100%;
	block-size: 100%;
	max-inline-size: none;
	margin: 0;
	border: 0;
	--hafe-img-radius: calc(var(--hafe-gallery-radius, var(--hafe-gallery-d-radius)) - var(--hafe-gallery-d-mat));
	object-fit: cover;
	box-shadow: none;
	transition: transform 0.5s cubic-bezier(0.2, 0.7, 0.3, 1), filter 0.3s;
}

.hafe-gallery-hover-zoom .hafe-gallery__frame:is(:hover, :focus-visible) .hafe-gallery__image {
	transform: scale(1.04);
}

.hafe-gallery-hover-dim .hafe-gallery__frame:is(:hover, :focus-visible) .hafe-gallery__image {
	filter: brightness(0.86);
}

@media (prefers-reduced-motion: reduce) {
	.hafe-gallery .hafe-gallery__image {
		transition: none;
	}
}

/* Masonry: columns, each image whole. Where the browser lays out masonry itself, it does; elsewhere
   the script spans each image over as many one-pixel rows as it's tall. */
.hafe-gallery--masonry.is-packed .hafe-gallery__items {
	grid-auto-rows: 1px;
	row-gap: 0;
}

.hafe-gallery--masonry.is-packed .hafe-gallery__item {
	grid-row-end: span var(--hafe-gallery-span, 1);
}

@supports (display: grid-lanes) {
	.hafe-gallery--masonry .hafe-gallery__items {
		display: grid-lanes;
	}
}

/* Justified: rows of whole images at one height, grown to fill the width. Each image grows in
   proportion to its shape, so every image in a row keeps the same height. The last row keeps its
   height unless set to fill. */
.hafe-gallery--justified .hafe-gallery__items {
	display: flex;
	flex-wrap: wrap;
	align-items: flex-start;
}

.hafe-gallery--justified .hafe-gallery__item {
	flex: var(--hafe-gallery-r) 1 calc(var(--hafe-gallery-r) * var(--hafe-gallery-row, 240px));
}

/* With the script, each row ends where its height comes closest to the one asked for. */
.hafe-gallery--justified.is-justified .hafe-gallery__item {
	flex: 0 0 var(--hafe-gallery-w);
}

.hafe-gallery--justified:not(.hafe-gallery--fill) .hafe-gallery__items::after {
	content: "";
	flex: 1000000 1 0;
}

/* Captions: below the image, over it, or over it on hover and focus (always on touch screens). */
.hafe-gallery__caption {
	margin: 0;
	padding-block-start: 0.55em;
	color: var(--hafe-gallery-caption-color, var(--hafe-ink-soft));
	font-size: 0.875em;
	line-height: 1.45;
	text-align: var(--hafe-gallery-caption-align, start);
}

.hafe-gallery--caption-over .hafe-gallery__caption,
.hafe-gallery--caption-hover .hafe-gallery__caption {
	position: absolute;
	inset: auto var(--hafe-gallery-d-mat) var(--hafe-gallery-d-mat);
	padding: 0.6em 0.85em;
	border-end-start-radius: inherit;
	border-end-end-radius: inherit;
	background: var(--hafe-gallery-caption-bg, var(--hafe-gallery-d-scrim));
	color: var(--hafe-gallery-caption-color, #fff);
	pointer-events: none;
}

.hafe-gallery--caption-over .hafe-gallery__figure,
.hafe-gallery--caption-hover .hafe-gallery__figure {
	border-radius: var(--hafe-gallery-radius, var(--hafe-gallery-d-radius));
}

@media (hover: hover) {
	.hafe-gallery--caption-hover .hafe-gallery__caption {
		opacity: 0;
		transition: opacity 0.2s;
	}

	.hafe-gallery--caption-hover .hafe-gallery__figure:is(:hover, :focus-within) .hafe-gallery__caption {
		opacity: 1;
	}
}

.hafe-gallery .hafe-gallery__more,
.hafe-gallery .hafe-gallery__more:is(:hover, :focus) {
	display: block;
	margin: 1.75rem auto 0;
	--hafe-gallery-filter-line-used: var(--hafe-gallery-d-more-line);
	background: var(--hafe-gallery-more-bg, var(--hafe-gallery-d-more-bg));
	color: var(--hafe-gallery-more-ink, var(--hafe-gallery-d-more-ink));
}

.hafe-gallery .hafe-gallery__more[hidden] {
	display: none;
}

/* The viewer: the browser's modal dialog over the whole screen, in the widget's scheme. */
.hafe-gallery .hafe-gallery__viewer {
	position: fixed;
	inset: 0;
	box-sizing: border-box;
	inline-size: 100%;
	block-size: 100%;
	max-inline-size: none;
	max-block-size: none;
	margin: 0;
	padding: 4rem 5rem;
	border: 0;
	background: var(--hafe-gallery-viewer-bg, var(--hafe-surface));
	overflow: hidden;
	color: var(--hafe-gallery-viewer-ink, var(--hafe-ink));
	overscroll-behavior: contain;
}

.hafe-gallery .hafe-gallery__viewer[open] {
	display: grid;
	place-items: center;
}

.hafe-gallery__viewer::backdrop {
	background: transparent;
}

.hafe-gallery__view {
	display: flex;
	flex-direction: column;
	align-items: center;
	gap: 0.85rem;
	max-block-size: 100%;
	margin: 0;
}

.hafe-gallery .hafe-gallery__full {
	display: block;
	inline-size: auto;
	block-size: auto;
	max-inline-size: 100%;
	max-block-size: calc(100dvh - 12rem);
	--hafe-img-radius: var(--hafe-radius-sm);
	background: center / contain no-repeat;
	object-fit: contain;
	touch-action: pan-y pinch-zoom;
	user-select: none;
}

.hafe-gallery__note {
	max-inline-size: 60ch;
	color: inherit;
	font-size: 0.9375em;
	line-height: 1.5;
	text-align: center;
}

.hafe-gallery__count {
	position: absolute;
	inset-block-start: 1.4rem;
	inset-inline-start: 1.5rem;
	margin: 0;
	font-size: 0.875em;
	font-variant-numeric: tabular-nums;
}

.hafe-gallery .hafe-gallery__viewer button {
	display: grid;
	place-items: center;
	position: absolute;
	inline-size: 2.75rem;
	block-size: 2.75rem;
	margin: 0;
	padding: 0;
	border: 1px solid var(--hafe-gallery-d-button-line);
	border-radius: var(--hafe-pill);
	background: var(--hafe-gallery-d-button-bg);
	color: inherit;
	box-shadow: none;
	cursor: pointer;
}

.hafe-gallery .hafe-gallery__viewer button:hover {
	background: var(--hafe-tint);
}

.hafe-gallery__viewer svg {
	inline-size: 1.25rem;
	block-size: 1.25rem;
	fill: none;
	stroke: currentColor;
	stroke-width: 2;
	stroke-linecap: round;
	stroke-linejoin: round;
}

.hafe-gallery .hafe-gallery__close {
	inset-block-start: 0.75rem;
	inset-inline-end: 0.75rem;
}

.hafe-gallery .hafe-gallery__nav {
	inset-block-start: calc(50% - 1.375rem);
}

.hafe-gallery .hafe-gallery__nav--prev {
	inset-inline-start: 1rem;
}

.hafe-gallery .hafe-gallery__nav--next {
	inset-inline-end: 1rem;
}

.hafe-gallery .hafe-gallery__nav[hidden] {
	display: none;
}

/* Arrows point the way pages turn. */
.hafe-gallery__viewer:dir(rtl) .hafe-gallery__nav svg {
	transform: scaleX(-1);
}

/* On a phone the arrows move below the image, out of the thumb's way. */
@media (max-width: 767px) {
	.hafe-gallery .hafe-gallery__viewer {
		padding: 3.75rem 0.75rem 5rem;
	}

	.hafe-gallery .hafe-gallery__nav {
		inset-block: auto 1rem;
	}

	.hafe-gallery .hafe-gallery__nav--prev {
		inset-inline-start: calc(50% - 3.25rem);
	}

	.hafe-gallery .hafe-gallery__nav--next {
		inset-inline-end: calc(50% - 3.25rem);
	}
}

@media print {
	.hafe-gallery .hafe-gallery__filters,
	.hafe-gallery .hafe-gallery__more {
		display: none;
	}
}
