/*
 * ButtonPrimary component (button-primary.twig), loaded once per page by part_style().
 * In the components layer, so block CSS still overrides it the same way.
 */
@layer components {
	/* Optical spacing for the round ends (after Diego Haz's pill button technique):
	   - The curved end takes space from whatever sits next to it, so the label gets back the
	     area the curve removes, r × (1 − π/4), and looks as far from the end as --px says.
	   - The arrow's circle is pulled into the end cap so it's concentric with the curve: --py
	     from the edge, plus half the difference between the content height and the circle.
	   - The gap shrinks by how far the visible icon (the circle) sticks out past the capitals.
	   Text is trimmed to cap height (text-box in reset.css), so the content height is the taller
	   of the capitals and the arrow (--size), not one line box (1lh) as in the original
	   technique. The circle is taller than that and overflows it, as on ButtonSecondary, so the
	   button keeps its height.
	   Sizes are in em so the whole button scales with its font size (--size-body-md). At 16px
	   they match --spacing-16, --spacing-24 and --spacing-32. */
	.cta-primary {
		--size: 1em;
		--cta-icon-circle: 2em;
		--py: 1em;
		--px: 1.5em;
		--line: max(1cap, var(--size));
		--pill: calc(var(--py) + var(--line) / 2);
		--r: var(--pill);
		--cta-edge-top: var(--colour-accent-1-200);
		--cta-edge-bottom: var(--colour-accent-1-500);

		align-items: center;
		background-color: var(--colour-accent-1-base);

		/* A label that wraps keeps these corners, with straight sides between them, instead of
		   growing bigger round ends that the maths doesn't allow for. */
		border-radius: var(--r);

		/* Lit from above: a 1px lighter top edge and darker bottom edge. 300 reads grey against
		   the fill, so the top edge uses 200. Inset shadows follow the curve and fade out down the round ends, take no
		   space, and transition on hover, which separate border colours can't do. */
		box-shadow:
			inset 0 1px 0 var(--cta-edge-top),
			inset 0 -1px 0 var(--cta-edge-bottom);
		color: var(--colour-neutral-white);
		display: inline flex;
		font-size: var(--size-body-md);
		font-weight: var(--typography-weight-medium);
		gap: max(var(--py), var(--px) - (var(--cta-icon-circle) - 1cap) / 2);
		justify-content: space-between;
		line-height: var(--line-height-button);
		padding: var(--py) var(--px);
		text-decoration: none;
		text-wrap: balance;
		transition:
			background-color var(--duration-base) ease,
			box-shadow var(--duration-base) ease,
			color var(--duration-base) ease,
			scale var(--duration-press) var(--ease-out);
		width: var(--w, auto);

		@media (width >=48rem) {
			--w: max-content;
		}

		@media (hover: hover) and (pointer: fine) {
			&:hover {
				--cta-icon-background: var(--colour-accent-1-600);
				--cta-icon-edge-top: var(--colour-accent-1-700);
				--cta-icon-edge-bottom: var(--colour-accent-1-400);
				--cta-edge-top: var(--colour-accent-1-400);
				--cta-edge-bottom: var(--colour-accent-1-600);

				background-color: var(--colour-accent-1-500);
				color: var(--colour-neutral-white);
			}
		}

		/* Pressing gives instant feedback. */
		&:active {
			scale: 0.97;
		}

		/* The padding tops the trimmed label up to --line, so a text-only button is as tall as one
		   with an icon. */
		> span {
			--edge: calc(var(--r) * (1 - pi / 4) * var(--r) / var(--pill));

			padding-block: calc((var(--line) - 1cap) / 2);
		}

		/* On hover the circle sinks into the button. With the same light from above, a recess is
		   the reverse of the raised button: its top edge is in shadow and its bottom edge catches
		   the light, lighter than the hovered button too, so it doesn't blend in. The edges are
		   transparent until then. */
		> icon-lucide {
			--edge: calc(var(--py) - var(--px) + (var(--line) - var(--cta-icon-circle)) / 2);

			align-items: center;
			background-color: var(--cta-icon-background, var(--colour-accent-1-500));
			block-size: var(--cta-icon-circle);
			border-radius: var(--radius-circle);
			box-shadow:
				inset 0 1px 0 var(--cta-icon-edge-top, transparent),
				inset 0 -1px 0 var(--cta-icon-edge-bottom, transparent);
			flex-shrink: 0;
			inline-size: var(--cta-icon-circle);
			justify-content: center;
			margin-block: calc((var(--line) - var(--cta-icon-circle)) / 2);
			transition:
				background-color var(--duration-base) ease,
				box-shadow var(--duration-base) ease;
		}

		> :first-child {
			margin-inline-start: var(--edge, 0);
		}

		> :last-child {
			margin-inline-end: var(--edge, 0);
		}
	}
}
