@import 'mediawiki.skin.variables.less';
@import 'mediawiki.mixins.less';

// @margin-content will be used to break out of MinervaNeue's margins,
// but MinervaNeue need not be an explicit dependency
@margin-content: 0px;
@import (optional) '../../../../skins/MinervaNeue/minerva.less/minerva.variables.less';

@min-size-carousel: 172px;
@carousel-item-gap: @spacing-35;
// Width of the first off-screen thumbnail to show a peek of, to illustrate
// that the carousel is a horizontally scrollable thing
@carousel-item-peek: 15px;
// Reserve space for the scrollbar. Scrollbar height is determined by browser
@space-placeholder-scrollbar: @spacing-100;
@size-icon-carousel-toggle: 10px;

// Keep wide article content from expanding the page and the image preview viewport.
/* stylelint-disable-next-line plugin/no-unsupported-browser-features */
body:has( .mmv-carousel ) .mw-parser-output {
	overflow-x: auto;
}

.mmv-carousel {
	position: relative;

	/* stylelint-disable-next-line plugin/no-unsupported-browser-features */
	&:has( &__item[ data-visible='true' ] ),
	&:not( :has( &__item[ data-visible ] ) ) {
		// Only apply layout-shift inducing styles when there are
		// visible images, or when we don't yet know any of that
		// (as will be the case upon initial load, where we already
		// want these applied to prevent FOUC - rare cases where
		// we then find none of the images are visible, will collapse)
		margin-top: @spacing-50;
		margin-bottom: @spacing-50;
	}

	&__controls {
		position: absolute;
		display: flex;
		justify-content: space-between;
		padding: @spacing-50;
		top: 0;
		right: 0;
		isolation: isolate;
	}

	&__count {
		display: none;
		align-items: center;
		gap: @spacing-35;
		font-size: @font-size-x-small;
		line-height: @line-height-x-small;
		color: @color-base;
	}

	&__count-icon {
		.cdx-mixin-css-icon( @cdx-icon-image-gallery, @param-size-icon: @size-icon-x-small );
		flex-shrink: 0;
	}

	.cdx-button__icon-eye-closed {
		.cdx-mixin-css-icon( @cdx-icon-eye-closed, @param-is-button-icon: true, @param-size-icon: @size-icon-carousel-toggle );
	}

	.cdx-button__icon-eye {
		.cdx-mixin-css-icon( @cdx-icon-eye, @param-is-button-icon: true, @param-size-icon: @size-icon-carousel-toggle );
	}

	.cdx-button.mmv-carousel__toggle {
		flex-shrink: 0;
		color: @color-base;
		font-size: @font-size-x-small;
		font-weight: @font-weight-normal;
		line-height: @line-height-x-small;

		// Codex style override
		&.cdx-button--weight-quiet {
			background-color: @background-color-neutral;

			&:hover,
			&:active {
				background-color: @background-color-neutral;
			}

			&:focus {
				border-color: @border-color-progressive--focus;
			}
		}
	}

	&:not( .mmv-carousel--collapsed ) {
		.mmv-carousel__toggle--collapsed {
			display: none;
		}
	}

	&.mmv-carousel--collapsed {
		.mmv-carousel__toggle--expanded {
			display: none;
		}

		.mmv-carousel__controls {
			position: static;
			background: @background-color-neutral-subtle;
		}

		.mmv-carousel__count {
			display: inline-flex;
		}

		ul.mmv-carousel__items {
			display: none;
		}
	}

	.client-nojs & {
		&__controls {
			display: none;
		}
	}

	// The horizontally scrolling element. Kept separate from .mmv-carousel,
	// which is the containing block for the absolutely positioned controls. If
	// the two were the same element, the controls would scroll with the items
	// and be clipped by overflow-y.
	&__scroll {
		overflow: auto hidden;
	}

	/* stylelint-disable-next-line plugin/no-unsupported-browser-features */
	&__items {
		background: @background-color-neutral-subtle;
		padding: 2px;
		display: flex;
		align-items: center;
		list-style: none;
		margin: 0;
		height: @size-full;
		gap: @carousel-item-gap;
	}

	&__item {
		flex: 0 0 auto;
		cursor: pointer;
		aspect-ratio: 1;
		min-width: @min-size-carousel;
		max-width: 30vh;
		width: calc( calc( 100% + @margin-content + @carousel-item-gap ) / 2 - @carousel-item-peek );

		&[ data-visible='false' ] {
			// Make sure to properly hide items where the image is not visible
			display: none;
		}

		// Deferred items (data-src, no src) can only be loaded by carousel.js;
		// without JS, show a shorter carousel instead of blank tiles.
		.client-nojs &--deferred {
			display: none;
		}

		&-link {
			display: flex;
			flex-direction: column;
			width: @size-full;
			height: @size-full;
			border: @border-subtle;
			border-radius: @border-radius-base;
			overflow: hidden;

			/* stylelint-disable-next-line plugin/no-unsupported-browser-features */
			&:focus-visible {
				outline-offset: -2px;
			}
		}

		// Skeleton state for deferred tiles whose image has not loaded yet;
		// carousel.js removes the class once the image arrives.
		img.mmv-carousel__item-image--pending {
			background-color: @background-color-neutral;
		}

		/* stylelint-disable declaration-no-important */
		img.mmv-carousel__item-image {
			flex: 1;
			min-height: 0;
			object-fit: cover;
			// Bias the crop towards the top of the image. With a near-square
			// tile, only taller-than-tile (portrait) images have vertical
			// overflow to reposition, so this keeps their subject (typically a
			// head, near the top) instead of clipping it. Landscape images have
			// no vertical overflow, so this is a no-op for them.
			object-position: center top;
			box-sizing: border-box;
			width: 100%;
			// !important to override minerva style
			height: 100% !important;
		}
		/* stylelint-enable declaration-no-important */

		/* stylelint-disable declaration-no-important */
		&-caption {
			color: @color-base;
			padding: 0;
			margin: @spacing-50 !important;
			font-size: @font-size-small !important;
			line-height: @line-height-x-small;
			display: -webkit-box;
			-webkit-box-orient: vertical;
			-webkit-line-clamp: 2; // Truncate after 2 lines
			overflow: hidden;

			&:empty {
				display: none;
			}
		}
		/* stylelint-enable declaration-no-important */
	}

	// Fix render height of images hidden through
	// https://en.wikipedia.org/wiki/Help:Options_to_hide_an_image#Hide_all_images_until_they_are_clicked_on
	span.hide-images-wrapper {
		height: @size-full;
	}
}

// Override Minerva styles
body.skin-minerva .content .mmv-carousel {
	// Bleed the scrolling element into the page margins, so peeking items stay
	// visible there instead of being clipped at the edge.
	.mmv-carousel__scroll {
		margin-left: -@margin-content;
		margin-right: -@margin-content;
		padding: 0 @margin-content;
	}

	ul.mmv-carousel__items {
		list-style-type: none;
		padding-left: 0;
		padding-inline: 0;
	}

	li.mmv-carousel__item {
		margin-bottom: 0;
	}
}

// The carousel root carries mw-mmv-wrapper (so the lightbox overlay keeps it
// visible), which brings the lightbox's own positioning with it: position:
// absolute, top/left/right offsets and z-index: 1001. Reset those, but
// re-assert position: relative so the floating controls anchor to the carousel
// instead of to whichever positioned ancestor happens to sit above it such as
// a fundraising banner.
.mw-mmv-wrapper.mmv-carousel {
	position: relative;
	top: auto;
	left: auto;
	right: auto;
	z-index: auto;
}

// In dark mode, apply a light background to carousel images so transparent
// images with dark content remain visible against the dark page background.
// Tiles still waiting on their image keep the theme-aware placeholder
// background instead.
img.mmv-carousel__item-image:not( .mmv-carousel__item-image--pending ) {
	.darkmode-override( {
		background-color: @background-color-base-fixed;
	} );
}
