/* Rabin Craft — section edges (includes/edges.php, includes/edge-tiles.php). Stands alone; no script.

   The section itself is cut with a mask, so its own background (colour, gradient, photo, video) ends in
   the edge's shape and the neighbour shows through: nothing is colour-matched. The section overlaps that
   neighbour by the edge's height.

   includes/edges.php gives each edged section its tiles as custom properties (t = top, b = bottom):
     --_tm / --_bm   the section's mask tile          --_td / --_bd   the style's default height
     --_tw / --_bw   the tile's width ÷ height
     --_ts / --_bs   the shadow tile (::after)        --_tk / --_bk   the ::after mask (paper rim, shadow)
     --_tc / --_bc   a picture that cuts its shape out of a solid side (a silhouette that belongs to the
                     neighbour); composited with exclude, it turns the picture's alpha round
     --_tf / --_bf   the far side of a colour band (::after mask), with --_tn / --_bn: 1 when a side has one
     --_ex           "" when the section needs the ::after at all

   Settings (on the section or any ancestor): --mkit-edge-h (the edge's height; the pattern scales with
   it), --mkit-edge-paper (the paper rim's colour, default #fff), --mkit-edge-color (the band's colour,
   default the text colour). */

@supports ((-webkit-mask-image: none) or (mask-image: none)) {
	:where([class*="mkit-edge-"]) {
		position: relative;
	}

	[class*="mkit-edge-"] {
		--_th: 0px;
		--_bh: 0px;
		/* A side without an edge: an empty layer. Not `none`, which makes Firefox drop the whole mask. */
		--_no: linear-gradient(transparent, transparent);
		z-index: 1;
		/* Two cut layers on top (empty unless a silhouette belongs to the neighbour), then the two tiles and the
		   middle. A cut layer is excluded from what is under it, so where the picture is solid the section isn't. */
		-webkit-mask-image: var(--_tc, var(--_no)), var(--_bc, var(--_no)), var(--_tm, var(--_no)), var(--_bm, var(--_no)), linear-gradient(#000 0 0);
		mask-image: var(--_tc, var(--_no)), var(--_bc, var(--_no)), var(--_tm, var(--_no)), var(--_bm, var(--_no)), linear-gradient(#000 0 0);
		-webkit-mask-repeat: repeat-x, repeat-x, repeat-x, repeat-x, no-repeat;
		mask-repeat: repeat-x, repeat-x, repeat-x, repeat-x, no-repeat;
		/* The middle overlaps both tiles by a pixel: they are solid there, and a fractional height leaves no hairline */
		-webkit-mask-position: 0 0, 0 100%, 0 0, 0 100%, 0 calc(var(--_th) - 1px);
		mask-position: 0 0, 0 100%, 0 0, 0 100%, 0 calc(var(--_th) - 1px);
		-webkit-mask-size: var(--_ts-t) var(--_th), var(--_ts-b) var(--_bh), var(--_ts-t) var(--_th), var(--_ts-b) var(--_bh), 100% calc(100% - var(--_th) - var(--_bh) + 2px);
		mask-size: var(--_ts-t) var(--_th), var(--_ts-b) var(--_bh), var(--_ts-t) var(--_th), var(--_ts-b) var(--_bh), 100% calc(100% - var(--_th) - var(--_bh) + 2px);
		-webkit-mask-composite: xor, xor, source-over, source-over, source-over;
		mask-composite: exclude, exclude, add, add, add;
		/* A tile's width: in proportion to its height */
		--_ts-t: auto;
		--_ts-b: auto;
	}

	/* …rounded to a whole pixel where the browser can: a tile that ends on a part of a pixel shows a hairline
	   where it repeats */
	@supports (width: round(1.5px, 1px)) {
		[class*="mkit-edge-"] {
			--_ts-t: round(calc(var(--_th) * var(--_tw, 37.5)), 1px);
			--_ts-b: round(calc(var(--_bh) * var(--_bw, 37.5)), 1px);
		}
	}

	/* A silhouette set by class has a picture of unknown proportions: the browser keeps its own */
	[class*="-silhouette"] {
		--_ts-t: auto;
		--_ts-b: auto;
	}

	[class*="mkit-edge-top-"] {
		--_th: var(--mkit-edge-h, var(--_td, 44px));
		margin-top: calc(var(--_th) * -1);
	}

	/* A bottom edge lies over the section after it, so it has to be above it, however many follow each other */
	[class*="mkit-edge-bottom-"] {
		--_bh: var(--mkit-edge-h, var(--_bd, 44px));
		z-index: 2;
		margin-bottom: calc(var(--_bh) * -1);
	}

	/* …and over any number that follow it, with or without a strip of props (.mkit-props) between them */
	[class*="mkit-edge-bottom-"]:has(+ [class*="mkit-edge-bottom-"], + .mkit-props + [class*="mkit-edge-bottom-"]) {
		z-index: 3;
	}

	[class*="mkit-edge-bottom-"]:has(+ [class*="mkit-edge-bottom-"] + [class*="mkit-edge-bottom-"], + [class*="mkit-edge-bottom-"] + .mkit-props + [class*="mkit-edge-bottom-"], + .mkit-props + [class*="mkit-edge-bottom-"] + [class*="mkit-edge-bottom-"], + .mkit-props + [class*="mkit-edge-bottom-"] + .mkit-props + [class*="mkit-edge-bottom-"]) {
		z-index: 4;
	}

	[class*="mkit-edge-bottom-"]:has(+ [class*="mkit-edge-bottom-"] + [class*="mkit-edge-bottom-"] + [class*="mkit-edge-bottom-"], + [class*="mkit-edge-bottom-"] + [class*="mkit-edge-bottom-"] + .mkit-props + [class*="mkit-edge-bottom-"], + [class*="mkit-edge-bottom-"] + .mkit-props + [class*="mkit-edge-bottom-"] + [class*="mkit-edge-bottom-"], + [class*="mkit-edge-bottom-"] + .mkit-props + [class*="mkit-edge-bottom-"] + .mkit-props + [class*="mkit-edge-bottom-"], + .mkit-props + [class*="mkit-edge-bottom-"] + [class*="mkit-edge-bottom-"] + [class*="mkit-edge-bottom-"], + .mkit-props + [class*="mkit-edge-bottom-"] + [class*="mkit-edge-bottom-"] + .mkit-props + [class*="mkit-edge-bottom-"], + .mkit-props + [class*="mkit-edge-bottom-"] + .mkit-props + [class*="mkit-edge-bottom-"] + [class*="mkit-edge-bottom-"], + .mkit-props + [class*="mkit-edge-bottom-"] + .mkit-props + [class*="mkit-edge-bottom-"] + .mkit-props + [class*="mkit-edge-bottom-"]) {
		z-index: 5;
	}

	/* The paper rim and the shadow: the shadow tile is black outside the edge, where the section's mask is
	   only a soft fringe; the paper colour shows between the edge and the rim's inner line.
	   A colour band: the strip under the edge is solid, and the strip after it ends in the far side's tile
	   (the same style's opposite edge), so the colour runs from the edge to that far side. */
	[class*="mkit-edge-"]::after {
		content: var(--_ex, none);
		position: absolute;
		inset: 0;
		/* Above the section's background, under its content (the mask makes the section its own layer) */
		z-index: -1;
		pointer-events: none;
		background:
			var(--_ts, none) 0 0 / var(--_ts-t) var(--_th) repeat-x,
			var(--_bs, none) 0 100% / var(--_ts-b) var(--_bh) repeat-x,
			var(--mkit-edge-color, var(--_ec, var(--mkit-edge-paper, #fff)));
		--_am:
			var(--_tk, var(--_no)) 0 0 / var(--_ts-t) var(--_th) repeat-x,
			var(--_bk, var(--_no)) 0 100% / var(--_ts-b) var(--_bh) repeat-x,
			linear-gradient(#000 0 0) 0 0 / 100% calc((var(--_th) + 1px) * var(--_tn, 0)) no-repeat,
			var(--_tf, var(--_no)) 0 var(--_th) / var(--_ts-t) var(--_th) repeat-x,
			linear-gradient(#000 0 0) 0 100% / 100% calc((var(--_bh) + 1px) * var(--_bn, 0)) no-repeat,
			var(--_bf, var(--_no)) 0 calc(100% - var(--_bh)) / var(--_ts-b) var(--_bh) repeat-x;
		-webkit-mask: var(--_am);
		mask: var(--_am);
	}

	/* Depth: two more layers of the shape inside the section, in a tint of the text colour, nearer ones
	   darker. Each is a box with the shape as its mask (its own --_t1/--_t2 or --_b1/--_b2 tile, placed
	   further along each time). Only their transform moves (below), so scrolling never redraws them. */
	/* Named so [class*="mkit-edge-"] above never matches it */
	.mkit-depth-layers {
		position: absolute;
		inset-inline: -8%;
		z-index: -1;
		pointer-events: none;
	}

	.mkit-depth-layers > i {
		position: absolute;
		inset-inline: 0;
		-webkit-mask-repeat: repeat-x, no-repeat;
		mask-repeat: repeat-x, no-repeat;
	}

	.mkit-depth-layers--top {
		top: 0;
		height: calc(var(--_th) * 2.4);
		--_lh: calc(var(--_th) * .75);
	}

	/* Below the edge, fading out into the section */
	.mkit-depth-layers--top > i {
		top: calc(var(--_th) * .6);
		bottom: 0;
		background: linear-gradient(color-mix(in srgb, var(--mkit-edge-depth, currentColor) 11%, transparent) 25%, transparent);
		-webkit-mask-image: var(--_t1, var(--_no)), linear-gradient(#000 0 0);
		mask-image: var(--_t1, var(--_no)), linear-gradient(#000 0 0);
		-webkit-mask-position: 31% 0, 0 calc(var(--_lh) - 1px);
		mask-position: 31% 0, 0 calc(var(--_lh) - 1px);
		-webkit-mask-size: var(--_lw, auto) var(--_lh), 100% 100%;
		mask-size: var(--_lw, auto) var(--_lh), 100% 100%;
		--_lw: round(calc(var(--_lh) * var(--_tw, 37.5)), 1px);
	}

	.mkit-depth-layers--top > i + i {
		top: calc(var(--_th) * 1.05);
		background: linear-gradient(color-mix(in srgb, var(--mkit-edge-depth, currentColor) 18%, transparent) 25%, transparent);
		-webkit-mask-image: var(--_t2, var(--_no)), linear-gradient(#000 0 0);
		mask-image: var(--_t2, var(--_no)), linear-gradient(#000 0 0);
		-webkit-mask-position: 63% 0, 0 calc(var(--_lh) - 1px);
		mask-position: 63% 0, 0 calc(var(--_lh) - 1px);
	}

	/* Above the edge, down to it: the next section's shape in front, these ranges behind it */
	.mkit-depth-layers--bottom {
		bottom: 0;
		height: calc(var(--_bh) * 1.9);
		--_lh: calc(var(--_bh) * .75);
	}

	.mkit-depth-layers--bottom > i {
		top: calc(var(--_bh) * .25);
		bottom: 0;
		background: color-mix(in srgb, var(--mkit-edge-depth, currentColor) 11%, transparent);
		-webkit-mask-image: var(--_b1, var(--_no)), linear-gradient(#000 0 0);
		mask-image: var(--_b1, var(--_no)), linear-gradient(#000 0 0);
		-webkit-mask-position: 31% 0, 0 calc(var(--_lh) - 1px);
		mask-position: 31% 0, 0 calc(var(--_lh) - 1px);
		-webkit-mask-size: var(--_lw, auto) var(--_lh), 100% 100%;
		mask-size: var(--_lw, auto) var(--_lh), 100% 100%;
		--_lw: round(calc(var(--_lh) * var(--_bw, 37.5)), 1px);
	}

	.mkit-depth-layers--bottom > i + i {
		top: calc(var(--_bh) * .7);
		background: color-mix(in srgb, var(--mkit-edge-depth, currentColor) 18%, transparent);
		-webkit-mask-image: var(--_b2, var(--_no)), linear-gradient(#000 0 0);
		mask-image: var(--_b2, var(--_no)), linear-gradient(#000 0 0);
		-webkit-mask-position: 63% 0, 0 calc(var(--_lh) - 1px);
		mask-position: 63% 0, 0 calc(var(--_lh) - 1px);
	}

	/* A picture of unknown proportions keeps its own; so do browsers without round() */
	[class*="-silhouette"] > .mkit-depth-layers > i {
		--_lw: auto;
	}

	@supports not (width: round(1.5px, 1px)) {
		.mkit-depth-layers > i {
			--_lw: auto !important;
		}
	}

	/* The drift: tied to the scroll, on the compositor, in opposite directions. Off for reduced motion and in
	   browsers without scroll-driven animation, where the layers stay still. */
	@supports (animation-timeline: view()) {
		@media (prefers-reduced-motion: no-preference) {
			.mkit-depth-layers > i {
				animation: mkit-edge-drift linear both;
				animation-timeline: view();
				will-change: transform;
			}

			.mkit-depth-layers > i + i {
				animation-name: mkit-edge-drift-near;
			}
		}
	}

	@keyframes mkit-edge-drift {
		from { transform: translateX(2.5%); }
		to { transform: translateX(-2.5%); }
	}

	@keyframes mkit-edge-drift-near {
		from { transform: translateX(-6%); }
		to { transform: translateX(6%); }
	}

	/* Forced colours replace backgrounds, so a paper rim would be a band of the system colour: leave it out */
	@media (forced-colors: active) {
		[class*="mkit-edge-"]::after,
		.mkit-depth-layers {
			display: none;
		}
	}
}

/* Edge props (includes/edges.php): a zero-high strip between two sections, above both, holding pictures and
   tape centred on the join. It sits outside the @supports block above: props need no mask support. Its own
   block formatting context keeps the sections' margins from collapsing through it, and it clips sideways
   (never down), so a prop near the side can't make the page scroll sideways. */
.mkit-props {
	display: flow-root;
	position: relative;
	z-index: 7;
	height: 0;
	overflow-x: clip;
	overflow-y: visible;
	pointer-events: none;
	--_jo: calc(var(--_jh, 0px) * -.5);
}

.mkit-props--bottom {
	--_jo: calc(var(--_jh, 0px) * .5);
}

.mkit-prop {
	position: absolute;
	left: var(--x, 50%);
	top: calc(var(--_jo) + var(--y, 0px));
	/* Lengths come from the prop's own width: a percentage here would be of the whole strip, the page width */
	--_pw: var(--w, clamp(80px, 10vw, 170px));
	width: var(--_pw);
	box-sizing: border-box;
	transform: translate(-50%, -50%);
	rotate: var(--r, 0deg);
}

.mkit-prop__img {
	display: block;
	width: 100%;
	height: auto;
}

.mkit-prop--plain,
.mkit-prop--lift {
	filter: drop-shadow(0 6px 10px rgb(0 0 0 / .22));
}

/* A ready-made prop: an inline drawing in currentColor */
.mkit-prop--art {
	color: var(--c, #2f7d48);
}

.mkit-prop__art {
	display: block;
	width: 100%;
	height: auto;
	overflow: visible;
}

.mkit-prop--polaroid {
	padding: calc(var(--_pw) * .06) calc(var(--_pw) * .06) calc(var(--_pw) * .2);
	background: #fff;
	box-shadow: 0 1px 2px rgb(0 0 0 / .12), 0 12px 26px -10px rgb(0 0 0 / .45);
}

.mkit-prop--sticker {
	filter: drop-shadow(2px 0 #fff) drop-shadow(-2px 0 #fff) drop-shadow(0 2px #fff) drop-shadow(0 -2px #fff) drop-shadow(0 6px 8px rgb(0 0 0 / .25));
}

/* Tape: on a picture, a strip of translucent paper across its top; on its own, a strip of washi tape */
.mkit-prop__tape,
.mkit-prop--tape {
	clip-path: polygon(2% 0, 98% 0, 100% 25%, 97% 50%, 100% 75%, 98% 100%, 2% 100%, 0 75%, 3% 50%, 0 25%);
}

.mkit-prop__tape {
	position: absolute;
	top: calc(var(--_pw) * -.06);
	left: 50%;
	width: calc(var(--_pw) * .46);
	height: max(14px, calc(var(--_pw) * .13));
	translate: -50% 0;
	rotate: -4deg;
	background: rgb(255 255 255 / .6);
	box-shadow: 0 1px 2px rgb(0 0 0 / .1);
}

.mkit-prop--tape {
	--_pw: var(--w, clamp(90px, 12vw, 200px));
	height: clamp(20px, 1.2rem + .6vw, 32px);
	opacity: .92;
	--_c: var(--c, #f58fb6);
	background: repeating-linear-gradient(-45deg, var(--_c) 0 7px, color-mix(in srgb, var(--_c) 70%, #fff) 7px 14px);
}

.mkit-prop--dots {
	background: radial-gradient(circle, rgb(255 255 255 / .85) 0 2.5px, transparent 3px) 0 0 / 12px 12px, var(--_c);
}

.mkit-prop--plain.mkit-prop--tape {
	filter: none;
	background: var(--_c);
}

@media (max-width: 767px) {
	.mkit-prop--wide {
		display: none;
	}
}

/* A light float with the scroll, nearer props further: only the translate moves, tied to the scroll.
   Still for reduced motion and where scroll-driven animation isn't supported. */
@supports (animation-timeline: view()) {
	@media (prefers-reduced-motion: no-preference) {
		.mkit-prop {
			animation: mkit-prop-float linear both;
			animation-timeline: view();
		}
	}
}

@keyframes mkit-prop-float {
	from { translate: 0 calc(var(--d, 30) * .5px); }
	to { translate: 0 calc(var(--d, 30) * -.5px); }
}

@media (forced-colors: active) {
	.mkit-props {
		display: none;
	}
}
