/*
 * Image block styles
 *
 * Enqueued for core/image via wp_enqueue_block_style().
 * The Rounded radius itself is a token in theme.json
 * (styles.blocks.core/image.variations.rounded).
 *
 * @package Kindling
 * @since 1.0.0
 */

.wp-block-image.is-style-image-rounded {
	overflow: hidden;
	border-radius: var(--wp--preset--border-radius--rounded-medium);
}

.wp-block-image.is-style-image-rounded img {
	border-radius: inherit;
	display: block;
}

.wp-block-image.is-style-image-rounded-full {
	overflow: hidden;
	border-radius: var(--wp--preset--border-radius--rounded-full);
}

.wp-block-image.is-style-image-rounded-full img {
	border-radius: inherit;
	display: block;
}

/*
 * Image treatments: Framed (block style), badge and tilt.
 *
 * The frame is the Framed block style (styles/image/image-framed.json: frame
 * color and padding). Badge and tilt are block attributes that add
 * classes, on the front end in inc/image-treatments.php and in the editor
 * canvas in assets/js/image-treatments.js:
 *
 *   has-kindling-tilt-{left|right}
 *   has-kindling-badge  has-kindling-badge-{bottom-left|bottom-right|top-left|top-right}
 *   --kindling-image-badge: url(...)   (an approved badge from the registry)
 *
 * Every treatment applies to the figure, so the frame, the badge's corner and
 * the tilt all follow the same box. A caption sits inside the frame, like a
 * printed photo's label.
 *
 * Tilt and the badge offsets use the individual `rotate` and `translate`
 * properties, so they combine with any `transform` an animation sets.
 */

figure.wp-block-image:is(.is-style-image-framed, .has-kindling-badge, .has-kindling-tilt-left, .has-kindling-tilt-right) {
	position: relative;

	/* Rounded Full clips to its radius; the image keeps its own radius, so the badge can overlap. */
	overflow: visible;

	/* Links and images as blocks, so the frame hugs the image with no line gap. */
	& > a,
	& img {
		display: block;
	}

	/* The caption takes the image's width instead of widening the figure. */
	& > figcaption {
		display: block;
		contain: inline-size;
	}
}

/*
 * Size the figure to the image, so the frame and the badge's corner follow the
 * image rather than the column. Wide and full images already fill the column.
 * `display: block` replaces core's `display: table` on aligned images.
 *
 * The max-width is a fallback only, at zero specificity: constrained layouts
 * cap their children at the content width, which is where this theme's page
 * gutter comes from, and that cap has to win.
 */
figure.wp-block-image:is(.is-style-image-framed, .has-kindling-badge, .has-kindling-tilt-left, .has-kindling-tilt-right):not(.alignwide, .alignfull) {
	display: block;
	width: fit-content;
}

:where(figure.wp-block-image:is(.is-style-image-framed, .has-kindling-badge, .has-kindling-tilt-left, .has-kindling-tilt-right):not(.alignwide, .alignfull)) {
	max-width: 100%;
}

/*
 * Tilt. The designs tilt left-hand images about 20deg and right-hand images
 * about 12deg; both ease off as the column narrows. Once Columns stack
 * (781px and below) the tilt is gentle and the image is sized so its turned
 * corners stay inside the column (see below).
 */
.wp-block-image.has-kindling-tilt-left {
	--kindling-image-tilt: -20deg;
}

.wp-block-image.has-kindling-tilt-right {
	--kindling-image-tilt: 12deg;
}

@media (width <= 1024px) {
	.wp-block-image.has-kindling-tilt-left {
		--kindling-image-tilt: -12deg;
	}

	.wp-block-image.has-kindling-tilt-right {
		--kindling-image-tilt: 8deg;
	}
}

@media (width <= 781px) {
	.wp-block-image.has-kindling-tilt-left {
		--kindling-image-tilt: -5deg;
	}

	.wp-block-image.has-kindling-tilt-right {
		--kindling-image-tilt: 3deg;
	}
}

.wp-block-image:is(.has-kindling-tilt-left, .has-kindling-tilt-right) {
	rotate: var(--kindling-image-tilt);
}

/*
 * Stacked: a full-width image turned even 5deg is wider than its column, and
 * the page clips the corners at the screen edge. Narrow it so the turned box
 * fits: a box turned by t is w * cos(t) + h * sin(|t|) wide, so allow for
 * images up to 3:4 portrait (h = 1.34w) and center it. The page gutter is
 * kept clear too, since some patterns run their Columns edge to edge, and a
 * badge reaches into it.
 */
@media (width <= 781px) {
	.wp-block-image:is(.has-kindling-tilt-left, .has-kindling-tilt-right):not(.alignwide, .alignfull) {
		max-width: calc(
			(100% - 2 * var(--wp--custom--layout--site-outer-padding)) /
				(
					cos(var(--kindling-image-tilt)) + 1.34 *
						max(sin(var(--kindling-image-tilt)), -1 * sin(var(--kindling-image-tilt)))
				)
		);
		margin-inline: auto;
	}
}

/* An edge-to-edge image has no room to tilt. */
.wp-block-image.alignfull:is(.has-kindling-tilt-left, .has-kindling-tilt-right) {
	--kindling-image-tilt: 0deg;
}

/*
 * Badge. Drawn from the approved SVG as a decorative pseudo-element, so it adds
 * nothing to the accessibility tree or the image's accessible name.
 *
 * Size: about a third of the image, capped at the artwork's native 9rem (the
 * design size) and never below 3.5rem.
 *
 * Position: centered on the side edge, a third of the way past the bottom (or
 * top) edge, as in the designs. The offsets are fractions of the badge, so the
 * overlap scales with it. The badge stays upright on a tilted image.
 *
 * The vertical overlap is reserved as margin on that side, so the block above
 * or below always clears the badge. (The margin's percentage resolves against
 * the column rather than the image, so on a narrow image it reserves a little
 * more than it needs.)
 */
.wp-block-image.has-kindling-badge {
	--kindling-image-badge-size: clamp(3.5rem, 35%, 9rem);
	--kindling-image-badge-offset-x: 50%;
	--kindling-image-badge-overlap: 0.35;
	--kindling-image-badge-offset-y: calc(var(--kindling-image-badge-overlap) * 100%);
	--kindling-image-badge-reserve: calc(var(--kindling-image-badge-size) * var(--kindling-image-badge-overlap));

	&::after {
		content: "";
		position: absolute;
		z-index: 1;
		width: var(--kindling-image-badge-size);
		aspect-ratio: 1;
		background: var(--kindling-image-badge) center / contain no-repeat;
		pointer-events: none;
		rotate: calc(-1 * var(--kindling-image-tilt, 0deg));
	}
}

.wp-block-image:is(.has-kindling-badge-bottom-left, .has-kindling-badge-bottom-right) {
	margin-block-end: max(1em, var(--kindling-image-badge-reserve));
}

.wp-block-image:is(.has-kindling-badge-top-left, .has-kindling-badge-top-right) {
	margin-block-start: max(var(--wp--style--block-gap, 0px), var(--kindling-image-badge-reserve));
}

.wp-block-image.has-kindling-badge-bottom-left::after {
	bottom: 0;
	left: 0;
	translate: calc(-1 * var(--kindling-image-badge-offset-x)) var(--kindling-image-badge-offset-y);
}

.wp-block-image.has-kindling-badge-bottom-right::after {
	right: 0;
	bottom: 0;
	translate: var(--kindling-image-badge-offset-x) var(--kindling-image-badge-offset-y);
}

.wp-block-image.has-kindling-badge-top-left::after {
	top: 0;
	left: 0;
	translate: calc(-1 * var(--kindling-image-badge-offset-x)) calc(-1 * var(--kindling-image-badge-offset-y));
}

.wp-block-image.has-kindling-badge-top-right::after {
	top: 0;
	right: 0;
	translate: var(--kindling-image-badge-offset-x) calc(-1 * var(--kindling-image-badge-offset-y));
}

/*
 * Once Columns stack (781px and below) images usually span the column, so the
 * badge only reaches a little way into the page gutter, and a little less
 * past the image edge.
 */
@media (width <= 781px) {
	.wp-block-image.has-kindling-badge {
		--kindling-image-badge-offset-x: min(50%, var(--wp--custom--spacing--12));
		--kindling-image-badge-overlap: 0.25;
	}
}

/* Edge-to-edge images have no gutter to overlap into, so the badge sits inside. */
.wp-block-image.alignfull.has-kindling-badge {
	--kindling-image-badge-offset-x: calc(-1 * var(--wp--custom--layout--site-outer-padding));
}

/*
 * Tilted corners and overlapping badges reach past their column by design, and
 * at the page edge that would become a horizontal scrollbar. Clip horizontal
 * overflow on the top-level template area that holds them (in the editor, on
 * the canvas). `clip` rather than `hidden`, so it does not become a scroll
 * container and sticky positioning still works.
 */
.wp-site-blocks > :has(.has-kindling-badge, .has-kindling-tilt-left, .has-kindling-tilt-right),
.is-root-container:has(.has-kindling-badge, .has-kindling-tilt-left, .has-kindling-tilt-right) {
	overflow-x: clip;
}
