/*
 * Event Carousel (inc/events.php)
 *
 * A Query Loop variation (`kindling-event-carousel` class) that shows upcoming
 * events as a row of cards: three per view on desktop, two on tablets, one on
 * phones. The row runs past the wide width to the viewport edge, so the next
 * card peeks in and signals that the row scrolls. It is a scroll-snap list, so
 * it swipes and scrolls on its own; assets/js/event-carousel.js adds the
 * previous/next buttons.
 *
 * Card parts are classes on Groups in the card template:
 *   kindling-event-card         the card (white, 1px border)
 *   kindling-event-card__media  photo area, 3:2 (--kindling-event-card-ratio), Mist when there is no photo
 *   kindling-event-card__date   Navy date badge over the photo's top left
 *   kindling-event-card__body   text; the last block (Details) gets extra space above it
 *
 * @package Kindling
 * @since Kindling 4.1.0
 */

.kindling-event-carousel {
	--kindling-carousel-gap: var(--wp--preset--spacing--30);
	--kindling-carousel-per-view: 3;
	/* Distance from the row's edge to the viewport edge: the wide width's side space, or the page gutter on narrower screens. */
	--kindling-carousel-bleed: max(
		var(--wp--custom--layout--site-outer-padding),
		var(--wp--custom--layout--main-wide-width-side-spacing)
	);

	/* The row: run past the wide width to the viewport edge, but snap to the content edge. */
	> .wp-block-post-template {
		/*
		 * Positioned so absolutely positioned card content, like the Details
		 * link's screen-reader text, is clipped by the row's scrolling instead
		 * of widening the page by the length of the row.
		 */
		position: relative;
		display: flex;
		gap: var(--kindling-carousel-gap);

		/* Core caps the Post Template at 100%, which would cancel the bleed. */
		max-width: none;
		margin-block: 0;
		margin-inline-end: calc(-1 * var(--kindling-carousel-bleed));
		padding: 0 var(--kindling-carousel-bleed) 0 0;
		list-style: none;
		overflow-x: auto;
		overscroll-behavior-x: contain;
		scroll-snap-type: x mandatory;
		scroll-padding-inline-end: var(--kindling-carousel-bleed);
		scrollbar-width: none;

		&::-webkit-scrollbar {
			display: none;
		}

		> li {
			flex: 0 0
				calc(
					(
							100% - (var(--kindling-carousel-per-view) - 1) *
								var(--kindling-carousel-gap)
						) /
						var(--kindling-carousel-per-view)
				);
			margin: 0;
			scroll-snap-align: start;
		}
	}

	@media (max-width: 1023px) {
		--kindling-carousel-per-view: 2;
	}

	/* Phones: one card, with a sliver of the next. */
	@media (max-width: 599px) {
		--kindling-carousel-per-view: 1.15;
	}

	@media (prefers-reduced-motion: no-preference) {
		> .wp-block-post-template {
			scroll-behavior: smooth;
		}
	}
}

/*
 * The bleed is measured with 100vw, which includes a classic (non-overlay)
 * scrollbar, so the row can reach a scrollbar's width past the page edge.
 * Clip horizontal overflow on the top-level template area that holds a
 * carousel, as the image badges do (core-image.css). `clip` rather than
 * `hidden`, so it does not become a scroll container and sticky positioning
 * still works.
 */
.wp-site-blocks > :has(.kindling-event-carousel),
.is-root-container:has(.kindling-event-carousel) {
	overflow-x: clip;
}

/* A full-width row already reaches the viewport edge. */
.kindling-event-carousel.alignfull {
	--kindling-carousel-bleed: 0px;
}

.kindling-event-card {
	/*
	 * The card is always white, so restore the light-surface type tokens. On a
	 * dark section (Style 4 or 5) the card would otherwise inherit its white
	 * headings, links and Details button. The date badge sets its own surface.
	 */
	--wp--custom--color--caption: var(--wp--preset--color--theme-07);
	--wp--custom--color--focus: var(--wp--preset--color--theme-13);
	--wp--custom--color--text--body: var(--wp--custom--color--text--body-on-light-surface);
	--wp--custom--color--text--body-large: var(--wp--preset--color--theme-03);
	--wp--custom--color--text--heading: var(--wp--preset--color--theme-03);
	--wp--custom--color--text--subheading: var(--wp--preset--color--theme-13);
	--wp--custom--color--text--link: var(--wp--preset--color--theme-03);
	--wp--custom--color--text--link-hover: var(--wp--preset--color--theme-13);
	--wp--custom--color--text--link-underline: var(--wp--preset--color--theme-14);
	--wp--custom--color--button--tertiary--icon: var(--wp--preset--color--theme-03);
	--wp--custom--color--button--tertiary--text: var(--wp--preset--color--theme-03);
	--wp--custom--color--button--tertiary--hover--icon: var(--wp--preset--color--theme-13);
	--wp--custom--color--button--tertiary--hover--text: var(--wp--preset--color--theme-13);

	display: flex;
	flex-direction: column;
	height: 100%;
	background-color: var(--wp--custom--color--surface--background);
	border: 1px solid var(--wp--custom--color--surface--border);

	> * {
		margin-block: 0;
	}
}

.kindling-event-card__media {
	position: relative;
	aspect-ratio: var(--kindling-event-card-ratio, 3 / 2);
	/* Keeps the ratio when a photo is taller than it (min-height: auto would grow the box). */
	overflow: hidden;
	background-color: var(--wp--preset--color--theme-09);

	.wp-block-post-featured-image {
		height: 100%;
		margin: 0;

		:is(a, img) {
			display: block;
			width: 100%;
			height: 100%;
		}

		img {
			object-fit: cover;
		}
	}
}

/*
 * Scoped to the card so it outranks the editor's
 * `.block-editor-block-list__layout .block-editor-block-list__block`
 * `position: relative`, which would drop the badge below the photo in the editor.
 */
.kindling-event-card .kindling-event-card__media > .kindling-event-card__date {
	position: absolute;
	inset-block-start: 0;
	inset-inline-start: 0;
	z-index: 1;
	display: flex;
	flex-direction: column;
	justify-content: center;
	min-width: 5rem;
	min-height: 5rem;
	margin: 0 !important;
	text-align: center;
	text-transform: uppercase;

	> * {
		margin-block: 0;
	}
}

.kindling-event-card__body {
	display: flex;
	flex-direction: column;
	flex: 1;

	/* Details: a set space below the venue, not pinned to the bottom of a tall card. */
	> :last-child {
		margin-block-start: var(--wp--preset--spacing--20);
	}
}

.kindling-event-carousel__nav {
	display: flex;
	gap: var(--wp--preset--spacing--20);
	margin-block-start: var(--wp--preset--spacing--50);

	&[hidden] {
		display: none;
	}
}

.kindling-event-carousel__button {
	display: inline-flex;
	align-items: center;
	justify-content: center;
	width: 2.875rem;
	height: 2.875rem;
	padding: 0;
	color: var(--wp--custom--color--button--fill--text);
	background-color: var(--wp--custom--color--button--fill--background);
	border: 0;
	border-radius: 50%;
	cursor: pointer;
	transition:
		background-color 0.2s ease-out,
		opacity 0.2s ease-out;

	&:hover:not(:disabled) {
		background-color: var(
			--wp--custom--color--button--fill--hover--background
		);
	}

	&:focus-visible {
		outline: 2px solid var(--wp--custom--color--focus, currentColor);
		outline-offset: 2px;
	}

	&:disabled {
		cursor: default;
		opacity: 0.4;
	}

	&.is-previous svg {
		rotate: 180deg;
	}

	@media (prefers-reduced-motion: reduce) {
		transition: none;
	}
}
