/* White, and above the page hero's scroll darkening (its ::after), so the bar stays the top edge of
   the page below as the hero dims. At rest the hero is white here too, so nothing changes. */
.breadcrumbs {
	background-color: var(--surface-white);
	border-block-end: none;
	position: relative;
	z-index: 1;
	border-block-start: var(--border-grey);
	border-inline: var(--border-grey);
	border-radius: 2rem 2rem 0 0;
	container: breadcrumbs / inline-size;
	display: flex;
	grid-column: full;
	grid-row: breadcrumbs;
	overflow: clip;
	padding-inline: var(--spacing-16);

	@media (width >= 64rem) {
		grid-column: content;
	}

	@supports (corner-shape: squircle) {
		border-radius: 4rem 4rem 0 0;
		corner-shape: squircle;
	}
}

@scope (.breadcrumbs) {
	/* Ancestors keep their width, so crumbs never overlap: the current page gives way instead */
	.breadcrumb-anchor,
	.breadcrumb-more {
		align-items: center;
		display: flex;
		flex: none;
		position: relative;

		&::after {
			background-color: var(--colour-text-body-default);
			border-radius: 1px;
			content: '';
			height: 12px;
			inset: calc(50% - 4px) 0 auto auto;
			opacity: 0.2;
			position: absolute;
			rotate: 25deg;
			width: 1.5px;
		}
	}

	/* Collapsing the middle crumbs is the last step, once the trail doesn't fit even with shortened
	   labels: below tablet width for five levels or more (six crumbs), and on phones for three and
	   four levels. One- and two-level trails always fit. The widths are the nav's content box. */
	.breadcrumb-more {
		display: none;
	}

	@container breadcrumbs (width < 48rem) {
		:scope:not([data-crumbs='4'], [data-crumbs='5']) {
			.breadcrumb-middle {
				display: none;
			}

			.breadcrumb-more {
				display: flex;
			}
		}
	}

	@container breadcrumbs (width < 40rem) {
		.breadcrumb-middle {
			display: none;
		}

		.breadcrumb-more {
			display: flex;
		}
	}

	.breadcrumb-anchor {
		--size: 1cap;

		color: var(--colour-text-body-default);
		text-decoration: none;

		&:hover span {
			text-decoration: underline;
		}
	}

	.breadcrumb {
		align-items: center;
		display: flex;
		gap: var(--spacing-8);
		justify-content: center;
		padding: var(--spacing-16);
	}

	/* The current page: never shortened up front, only shrinks when the trail doesn't fit */
	:scope > .breadcrumb {
		min-width: 0;
	}

	/* The nav clips its overflow, which would cut off an outline outside the box */
	.breadcrumb-anchor:focus-visible,
	.breadcrumb-more-button:focus-visible {
		outline-offset: -2px;
	}

	.breadcrumb-more-button {
		anchor-name: --breadcrumbs-more;
		background: none;
		border: none;
		color: var(--colour-text-body-default);
		cursor: pointer;
		font: inherit;

		&:hover span {
			text-decoration: underline;
		}
	}

	.breadcrumbs-menu {
		background-color: var(--surface-white);
		border: var(--border-grey);
		border-radius: var(--radius-card);
		margin: 0;
		padding: var(--spacing-8);

		/* Without anchor positioning the popover keeps its default place in the middle of the screen */
		@supports (position-area: bottom) {
			inset: auto;
			margin-block-start: var(--spacing-8);
			position-anchor: --breadcrumbs-more;
			position-area: bottom span-right;
			position-try-fallbacks: flip-inline;
		}

		&:popover-open {
			display: grid;
		}
	}

	.breadcrumbs-menu-link {
		color: var(--colour-text-body-default);
		padding: var(--spacing-10) var(--spacing-16);

		&:hover {
			text-decoration: underline;
		}

		&:focus-visible {
			outline-offset: -2px;
		}
	}

	.icon + .body-breadcrumb {
		display: none;

		@media (width >= 64rem) {
			display: block;
		}
	}

	.body-breadcrumb {
		display: block;
		line-height: normal;
		overflow-x: hidden;
		text-box-edge: text;
		text-overflow: ellipsis;
		white-space: nowrap;

		.breadcrumbs:has(> :nth-child(3)) & {
			max-width: calc(13ch - 2rem);

			@media (width >= 40rem) {
				max-width: none;
			}
		}

		@media (width >= 40rem) {
			max-width: calc(30ch - 2rem);
		}

		@media (width >= 64rem) {
			max-width: none;
			overflow: visible;
			text-box-edge: cap alphabetic;
		}

		:scope > .breadcrumb > & {
			max-width: none;
		}
	}
}
