/* WP Advance Gallery — layout
 *
 * Two layout modes share one stylesheet:
 *   .lt-gallery              zig-zag (default) — alternating wide/narrow rows
 *   .lt-gallery.lt-masonry   masonry — columns of natural-aspect images
 *
 * Every dimension is driven by a custom property that the widget's Elementor
 * controls write into. Nothing here needs editing to retheme the gallery.
 *
 * The lt- prefix is kept from the plugin's first release because pages are
 * already published against it; renaming would restyle live sites.
 */

/* ===========================================================================
   Shell — filter bar beside the gallery

   One flex wrapper covers all four filter positions. `filter_position` writes
   two directions at once through selectors_dictionary: which way the wrapper
   stacks, and which way the tabs themselves run. Top/bottom differ only by
   column vs column-reverse, left/right only by row vs row-reverse, so the
   responsive control gets every breakpoint for free.
   =========================================================================== */

.lt-wrap {
	--wpag-wrap-dir: column;
	--wpag-wrap-gap: 30px;
	--wpag-bar-dir: row;
	--wpag-bar-gap: 10px;
	--wpag-bar-basis: auto;
	--wpag-bar-wrap: wrap;
	--wpag-bar-align: center;
	--wpag-bar-justify: center;
	--wpag-bar-width: 200px;

	display: flex;
	flex-direction: var(--wpag-wrap-dir);
	gap: var(--wpag-wrap-gap);
	align-items: stretch;
	width: 100%;
}

/* min-width:0 stops a wide grid from forcing the flex line open and squashing
   the tab column next to it. */
.lt-main {
	flex: 1 1 auto;
	min-width: 0;
}

.lt-filters {
	display: flex;
	flex: 0 0 var(--wpag-bar-basis);
	flex-direction: var(--wpag-bar-dir);
	flex-wrap: var(--wpag-bar-wrap);
	gap: var(--wpag-bar-gap);
	align-items: var(--wpag-bar-align);
	justify-content: var(--wpag-bar-justify);
	margin: 0;
	padding: 0;
}

.lt-filter {
	display: inline-block;
	margin: 0;
	font: inherit;
	line-height: 1.2;
	text-align: center;
	text-transform: uppercase;
	letter-spacing: 0.05em;
	white-space: nowrap;
	cursor: pointer;
	background: transparent;
	border: 1px solid currentColor;
	border-radius: 0;
	transition: all 0.25s ease-in-out;
}

.lt-filter:focus-visible {
	outline: 2px solid currentColor;
	outline-offset: 2px;
}

/* Many tabs on a narrow screen wrap into a tall stack that pushes the gallery
   off-screen. One swipeable line reads better; scrollbar hidden, because the
   overflow is the affordance. */
@media (max-width: 767px) {
	.wpag-swipe-yes .lt-filters {
		flex-wrap: nowrap;
		overflow-x: auto;
		overflow-y: hidden;
		justify-content: flex-start;
		scrollbar-width: none;
		-webkit-overflow-scrolling: touch;
	}

	.wpag-swipe-yes .lt-filters::-webkit-scrollbar {
		display: none;
	}

	.wpag-swipe-yes .lt-filter {
		flex: 0 0 auto;
	}
}

.lt-gallery {
	--lt-gap: 15px;
	--lt-wide: 66%;
	--lt-ratio: 64.69%;
	--lt-max-h: 100vh;
	--lt-cols: 3;

	display: flex;
	flex-direction: row;
	flex-wrap: wrap;
	/* A real gap, not space-between. space-between hands the LEFTOVER room to
	   the gutter, so one mis-sized tile silently changes the spacing of its
	   whole row. A fixed gap doesn't care what the tiles measure. */
	justify-content: flex-start;
	align-items: stretch;
	gap: var(--lt-gap);
	width: 100%;
	margin: 0;
	padding: 0;
	font-size: 0;
	line-height: 0;

	/* Tile height comes from a ::after padding spacer, so the gallery's own
	   box must be free to grow. If an ancestor Container has a fixed height,
	   Min Height, or a height set on a single breakpoint, the grid paints its
	   later rows outside its layout box and whatever follows draws on top. */
	height: auto !important;
	min-height: 0;
	overflow: visible;
}

.lt-gallery > .lt-sq {
	position: relative;
	display: block;
	margin: 0;
	padding: 0;
	overflow: hidden;
	line-height: 0;
	align-self: stretch;
	max-height: var(--lt-max-h);
	text-decoration: none;
	background: transparent;
}

/* ===========================================================================
   Zig-zag mode
   =========================================================================== */

/*
 * The aspect-ratio spacer, and the whole trick behind the layout.
 * Only WIDE tiles get a padding-bottom, so only they have an intrinsic
 * height. Narrow tiles have none and take their height from
 * `align-items: stretch`, matching whatever the wide tile on that row
 * worked out to. That's what keeps rows flush with no equal-height JS.
 */
.lt-gallery:not(.lt-masonry):not(.lt-grid) > .lt-sq::after {
	content: "";
	display: block;
}

.lt-gallery:not(.lt-masonry):not(.lt-grid) > .lt-sq:nth-child(4n + 1),
.lt-gallery:not(.lt-masonry):not(.lt-grid) > .lt-sq:nth-child(4n + 4) {
	/* The 0.5px is slack against sub-pixel rounding. Without it a row can
	   overflow by a fraction and wrap the second tile onto its own line. */
	flex: 0 0 calc(var(--lt-wide) - var(--lt-gap) - 0.5px);
	max-width: calc(var(--lt-wide) - var(--lt-gap) - 0.5px);
}

.lt-gallery:not(.lt-masonry):not(.lt-grid) > .lt-sq:nth-child(4n + 1)::after,
.lt-gallery:not(.lt-masonry):not(.lt-grid) > .lt-sq:nth-child(4n + 4)::after {
	/* min() clamps the ratio against the height cap, so on wide-and-short
	   viewports the tile stops growing and the image crops via object-fit
	   instead of running off the screen. */
	padding-bottom: min(var(--lt-ratio), var(--lt-max-h));
}

.lt-gallery:not(.lt-masonry):not(.lt-grid) > .lt-sq:nth-child(4n + 2),
.lt-gallery:not(.lt-masonry):not(.lt-grid) > .lt-sq:nth-child(4n + 3) {
	flex: 0 0 calc(100% - var(--lt-wide) - 0.5px);
	max-width: calc(100% - var(--lt-wide) - 0.5px);
}

/* A trailing narrow tile with no wide sibling has nothing to stretch to,
   so it needs its own ratio or it collapses to nothing. */
.lt-gallery:not(.lt-masonry):not(.lt-grid) > .lt-sq.lt-solo::after {
	padding-bottom: min(100%, var(--lt-max-h));
}

/* ===========================================================================
   Masonry mode

   Each tile carries its own --lt-ar (width/height), written inline by PHP from
   the attachment's real dimensions. That means heights are known before a
   single image has loaded — no waiting on decode, no layout jump.

   Without JS this renders as a plain aligned grid: correct, just not staggered.
   The script then upgrades it to real masonry by assigning each tile to the
   shortest column. Progressive enhancement, so a JS failure degrades to a
   tidy grid rather than a pile of overlapping images.
   =========================================================================== */

.lt-gallery.lt-masonry {
	display: grid;
	grid-template-columns: repeat(var(--lt-cols), minmax(0, 1fr));
	column-gap: var(--lt-gap);
	row-gap: var(--lt-gap);
	align-items: start;
}

.lt-gallery.lt-masonry > .lt-sq {
	flex: none;
	max-width: none;
	/* Masonry exists to show images at their own proportions, so the row-height
	   cap that zig-zag uses would defeat the point. */
	max-height: none;
	align-self: start;
	aspect-ratio: var(--lt-ar, 1);
}

/* JS-upgraded state: 1px rows so a span count is a pixel count, and no row-gap
   because the gap is folded into each tile's computed start row instead. */
.lt-gallery.lt-masonry.is-masonry {
	grid-auto-rows: 1px;
	row-gap: 0;
}

/* ===========================================================================
   Grid mode

   Equal tiles in strict rows and columns. Tile height comes from the real
   aspect-ratio property rather than a padding spacer, so the gallery always
   has an intrinsic height of its own and the gaps line up exactly.
   =========================================================================== */

.lt-gallery.lt-grid {
	display: grid;
	grid-template-columns: repeat(var(--lt-cols), minmax(0, 1fr));
	gap: var(--lt-gap);
	align-items: stretch;
}

.lt-gallery.lt-grid > .lt-sq {
	flex: none;
	max-width: none;
	max-height: none;
	align-self: stretch;
	aspect-ratio: var(--lt-grid-ar, 1 / 1);
}

/* ===========================================================================
   Shared
   =========================================================================== */

/* Images fill their tile regardless of their own aspect */
.lt-gallery > .lt-sq img {
	position: absolute;
	top: 0;
	left: 0;
	display: block;
	width: 100%;
	height: 100%;
	max-width: none;
	object-fit: cover;
	object-position: center;
	border-radius: inherit;
}

/* Load More */
.lt-gallery-more {
	display: flex;
	justify-content: center;
	width: 100%;
}

.lt-view-more {
	display: inline-block;
	text-transform: uppercase;
	letter-spacing: 0.05em;
	cursor: pointer;
	background: transparent;
	border: 1px solid currentColor;
	border-radius: 0;
	transition: all 0.3s ease-in-out;
}

/* Numbered pagination */
.lt-pagination {
	display: flex;
	flex-wrap: wrap;
	align-items: center;
	justify-content: center;
	gap: 8px;
	width: 100%;
}

/* Empty means one page or fewer — collapse the margin too, not just the box,
   or a paginated gallery keeps a phantom gap under it. */
.lt-pagination:empty {
	display: none;
}

.lt-page {
	display: inline-flex;
	align-items: center;
	justify-content: center;
	margin: 0;
	font: inherit;
	line-height: 1.2;
	text-align: center;
	cursor: pointer;
	background: transparent;
	border: 1px solid currentColor;
	border-radius: 0;
	transition: all 0.25s ease-in-out;
}

.lt-page:disabled {
	opacity: 0.35;
	cursor: default;
}

.lt-page.is-current {
	cursor: default;
}

.lt-page:focus-visible {
	outline: 2px solid currentColor;
	outline-offset: 2px;
}

/* Gap marker between distant page numbers — not a button, so it takes none of
   the button styling and cannot be clicked or tabbed to. */
.lt-page-gap {
	display: inline-flex;
	align-items: center;
	justify-content: center;
	opacity: 0.5;
	user-select: none;
}

.lt-gallery-empty {
	padding: 40px;
	text-align: center;
	border: 1px dashed currentColor;
	opacity: 0.6;
	font-size: 14px;
	line-height: 1.5;
}

/* Optional mobile stack — zig-zag only; masonry has its own column count */
@media (max-width: 767px) {
	.lt-stack-mobile-yes .lt-gallery:not(.lt-masonry):not(.lt-grid) > .lt-sq,
	.lt-stack-mobile-yes .lt-gallery:not(.lt-masonry):not(.lt-grid) > .lt-sq:nth-child(4n + 1),
	.lt-stack-mobile-yes .lt-gallery:not(.lt-masonry):not(.lt-grid) > .lt-sq:nth-child(4n + 2),
	.lt-stack-mobile-yes .lt-gallery:not(.lt-masonry):not(.lt-grid) > .lt-sq:nth-child(4n + 3),
	.lt-stack-mobile-yes .lt-gallery:not(.lt-masonry):not(.lt-grid) > .lt-sq:nth-child(4n + 4) {
		flex: 0 0 100%;
		max-width: 100%;
	}

	.lt-stack-mobile-yes .lt-gallery:not(.lt-masonry):not(.lt-grid) > .lt-sq::after {
		padding-bottom: min(var(--lt-ratio), var(--lt-max-h));
	}
}

@media (prefers-reduced-motion: reduce) {
	.lt-gallery > .lt-sq img,
	.lt-view-more,
	.lt-filter,
	.lt-page {
		transition: none;
	}
}

/* ---------------------------------------------------------------------------
 * Collapse guard
 *
 * A flex Container set to flex-direction:column AND flex-wrap:wrap has to
 * resolve its main size (height) intrinsically. Percentage padding does not
 * contribute to intrinsic sizing — and zig-zag tile height comes entirely from
 * a percentage ::after spacer. The result is a widget that measures ~33px while
 * painting 328px, so whatever follows it draws straight over the gallery.
 *
 * Elementor sets --flex-wrap-mobile:wrap on containers by default, which is
 * why this only ever showed up below 768px.
 *
 * Column + wrap is never meaningful, so forcing nowrap is safe. Scoped to
 * :only-child so a row Container holding other widgets beside the gallery
 * keeps its own wrapping behaviour untouched.
 * ------------------------------------------------------------------------- */
.e-con.e-flex:has( > .elementor-widget-lt-gallery:only-child ) {
	flex-wrap: nowrap;
}

/* ---------------------------------------------------------------------------
 * Lightbox UI
 *
 * Elementor renders its lightbox into <body>, outside every widget wrapper,
 * so these have to hang off body classes that the script sets on click.
 * Arrows and the counter are Elementor defaults — these rules only ever
 * remove chrome, never add it.
 * ------------------------------------------------------------------------- */

body.lt-lb-no-arrows .elementor-lightbox .elementor-swiper-button,
body.lt-lb-no-arrows .elementor-lightbox .elementor-swiper-button-prev,
body.lt-lb-no-arrows .elementor-lightbox .elementor-swiper-button-next {
	display: none !important;
}

body.lt-lb-no-counter .elementor-lightbox .elementor-slideshow__counter {
	display: none !important;
}

body.lt-lb-no-icons .elementor-lightbox .elementor-slideshow__icon-zoom-in,
body.lt-lb-no-icons .elementor-lightbox .elementor-slideshow__icon-zoom-out,
body.lt-lb-no-icons .elementor-lightbox .elementor-slideshow__icon-expand,
body.lt-lb-no-icons .elementor-lightbox .elementor-slideshow__icon-share,
body.lt-lb-no-icons .elementor-lightbox .elementor-slideshow__share {
	display: none !important;
}

/* Keep the header row from collapsing when only one field is present */
.elementor-lightbox .elementor-slideshow__title:empty,
.elementor-lightbox .elementor-slideshow__description:empty {
	display: none;
}
