/* Mosaic Preview Gallery
   Easy tweaks: change the three variables below. */
:is(.wp-block-gallery.is-style-mosaic, .mosaic-gallery) {
	--mg-gap: 6px;      /* space between tiles */
	--mg-max: 100%;     /* cap the square, e.g. 900px */
	--mg-radius: 0px;   /* rounded tile corners */

	display: grid !important;
	grid-template-columns: repeat(4, minmax(0, 1fr));
	grid-template-rows: repeat(4, minmax(0, 1fr));
	grid-template-areas:
		"a a b b"
		"a a c d"
		"e f f d"
		"g f f h";
	gap: var(--mg-gap) !important;
	width: 100%;
	max-width: var(--mg-max);
	aspect-ratio: 1 / 1;
	margin-left: auto;
	margin-right: auto;
	padding: 0;
	box-sizing: border-box;
}

:is(.wp-block-gallery.is-style-mosaic, .mosaic-gallery) > figure {
	position: relative;
	display: block !important;
	width: auto !important;
	height: auto !important;
	min-width: 0;
	min-height: 0;
	margin: 0 !important;
	overflow: hidden;
	border-radius: var(--mg-radius);
	cursor: pointer;
	flex: none !important;
}

/* Only the first 8 are shown on the page; the rest live in the viewer. */
:is(.wp-block-gallery.is-style-mosaic, .mosaic-gallery) > figure:nth-child(n+9) {
	display: none !important;
}

:is(.wp-block-gallery.is-style-mosaic, .mosaic-gallery) > figure:nth-child(1) { grid-area: a; }
:is(.wp-block-gallery.is-style-mosaic, .mosaic-gallery) > figure:nth-child(2) { grid-area: b; }
:is(.wp-block-gallery.is-style-mosaic, .mosaic-gallery) > figure:nth-child(3) { grid-area: c; }
:is(.wp-block-gallery.is-style-mosaic, .mosaic-gallery) > figure:nth-child(4) { grid-area: d; }
:is(.wp-block-gallery.is-style-mosaic, .mosaic-gallery) > figure:nth-child(5) { grid-area: e; }
:is(.wp-block-gallery.is-style-mosaic, .mosaic-gallery) > figure:nth-child(6) { grid-area: f; }
:is(.wp-block-gallery.is-style-mosaic, .mosaic-gallery) > figure:nth-child(7) { grid-area: g; }
:is(.wp-block-gallery.is-style-mosaic, .mosaic-gallery) > figure:nth-child(8) { grid-area: h; }

:is(.wp-block-gallery.is-style-mosaic, .mosaic-gallery) > figure a {
	display: block;
	width: 100%;
	height: 100%;
}

:is(.wp-block-gallery.is-style-mosaic, .mosaic-gallery) > figure img {
	display: block;
	width: 100% !important;
	height: 100% !important;
	max-width: none !important;
	object-fit: cover !important;
	border-radius: 0;
	transition: transform 0.35s ease;
}

:is(.wp-block-gallery.is-style-mosaic, .mosaic-gallery) > figure:hover img,
:is(.wp-block-gallery.is-style-mosaic, .mosaic-gallery) > figure:focus-visible img {
	transform: scale(1.04);
}

:is(.wp-block-gallery.is-style-mosaic, .mosaic-gallery) > figure:focus-visible {
	outline: 3px solid #fff;
	outline-offset: -3px;
	box-shadow: inset 0 0 0 5px #000;
}

/* Captions are shown in the viewer, not on the tiles. */
:is(.wp-block-gallery.is-style-mosaic, .mosaic-gallery) > figure figcaption,
:is(.wp-block-gallery.is-style-mosaic, .mosaic-gallery) > figure::before {
	display: none !important;
}

/* "+64" badge on the 8th tile */
:is(.wp-block-gallery.is-style-mosaic, .mosaic-gallery) > figure[data-mg-more]::after {
	content: "+" attr(data-mg-more);
	position: absolute;
	inset: 0;
	display: flex;
	align-items: center;
	justify-content: center;
	background: rgba(0, 0, 0, 0.55);
	color: #fff;
	font-size: clamp(1.1rem, 3.2vw, 2.4rem);
	font-weight: 600;
	letter-spacing: 0.02em;
	pointer-events: none;
}

/* Fewer than 8 images: fall back to a plain even grid instead of a broken mosaic. */
:is(.wp-block-gallery.is-style-mosaic, .mosaic-gallery):not(:has(> figure:nth-child(8))) {
	grid-template-areas: none;
	grid-template-rows: none;
	grid-template-columns: repeat(auto-fill, minmax(min(180px, 45%), 1fr));
	aspect-ratio: auto;
}
:is(.wp-block-gallery.is-style-mosaic, .mosaic-gallery):not(:has(> figure:nth-child(8))) > figure {
	grid-area: auto;
	aspect-ratio: 1 / 1;
}

/* ---------- Fullscreen viewer ---------- */
html.mg-lb-open {
	overflow: hidden;
}

.mg-lb {
	position: fixed;
	inset: 0;
	z-index: 999999;
	display: none;
	flex-direction: column;
	background: rgba(10, 10, 10, 0.96);
	color: #fff;
	font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
	-webkit-user-select: none;
	user-select: none;
}
.mg-lb.is-open {
	display: flex;
}

.mg-lb__top {
	display: flex;
	align-items: center;
	justify-content: space-between;
	padding: 10px 14px;
	font-size: 14px;
	flex: none;
}
.mg-lb__count {
	font-variant-numeric: tabular-nums;
	opacity: 0.85;
}

.mg-lb button {
	appearance: none;
	border: 0;
	margin: 0;
	padding: 0;
	background: none;
	color: inherit;
	font: inherit;
	cursor: pointer;
}
.mg-lb__close,
.mg-lb__nav {
	display: flex;
	align-items: center;
	justify-content: center;
	width: 44px;
	height: 44px;
	border-radius: 50%;
	background: rgba(255, 255, 255, 0.1) !important;
	transition: background 0.15s;
}
.mg-lb__close:hover,
.mg-lb__nav:hover {
	background: rgba(255, 255, 255, 0.25) !important;
}
.mg-lb button:focus-visible {
	outline: 2px solid #fff;
	outline-offset: 2px;
}
.mg-lb svg {
	width: 22px;
	height: 22px;
	fill: none;
	stroke: currentColor;
	stroke-width: 2;
	stroke-linecap: round;
	stroke-linejoin: round;
}

.mg-lb__stage {
	position: relative;
	flex: 1 1 auto;
	min-height: 0;
	display: flex;
	align-items: center;
	justify-content: center;
	padding: 0 8px;
}
.mg-lb__img {
	max-width: 100%;
	max-height: 100%;
	width: auto;
	height: auto;
	object-fit: contain;
	opacity: 0;
	transition: opacity 0.2s;
}
.mg-lb__img.is-loaded {
	opacity: 1;
}
.mg-lb__nav {
	position: absolute;
	top: 50%;
	transform: translateY(-50%);
	z-index: 2;
}
.mg-lb__nav--prev { left: 12px; }
.mg-lb__nav--next { right: 12px; }

.mg-lb__caption {
	flex: none;
	min-height: 1.4em;
	padding: 8px 16px 4px;
	text-align: center;
	font-size: 14px;
	line-height: 1.4;
	opacity: 0.9;
}

.mg-lb__thumbs {
	flex: none;
	display: flex;
	gap: 6px;
	padding: 8px 12px 12px;
	overflow-x: auto;
	scrollbar-width: thin;
	scrollbar-color: rgba(255, 255, 255, 0.3) transparent;
}
.mg-lb__thumb {
	flex: none;
	width: 72px;
	height: 54px;
	overflow: hidden;
	border-radius: 3px;
	opacity: 0.45;
	transition: opacity 0.15s;
	background: rgba(255, 255, 255, 0.08) !important;
}
.mg-lb__thumb:hover {
	opacity: 0.8;
}
.mg-lb__thumb.is-active {
	opacity: 1;
	outline: 2px solid #fff;
	outline-offset: -2px;
}
.mg-lb__thumb img {
	display: block;
	width: 100%;
	height: 100%;
	object-fit: cover;
}

@media (max-width: 600px) {
	.mg-lb__nav { display: none; } /* swipe on phones */
	.mg-lb__thumb { width: 56px; height: 42px; }
}

@media (prefers-reduced-motion: reduce) {
	:is(.wp-block-gallery.is-style-mosaic, .mosaic-gallery) > figure img,
	.mg-lb__img {
		transition: none;
	}
}
