/*
 * Site header: the logo, the site's name and the menu (parts/header.html). Loaded with the
 * template-part block, so on every page.
 *
 * It stays at the top while the page scrolls under it (sticky), slimmer on phones, over a light
 * frosted background so whatever passes beneath never competes with the menu.
 */

/*
 * The header's height, which the page's scroll padding below depends on: the logo, the padding
 * above and below it, and the hairline. Kept in one place so the two cannot drift apart.
 */
:root {
	--site-header-logo-size: var(--wp--custom--header--logo-size);
	--site-header-padding-block: var(--wp--custom--spacing--header);
	--site-header-height: calc(var(--site-header-logo-size) + 2 * var(--site-header-padding-block) + 1px);

	/* The size of WordPress's menu buttons, which the hamburger menu places: its icons are 24px squares. */
	--site-header-menu-button-size: 24px;
}

/*
 * Phones: a smaller logo and less padding, so the header takes less of a
 * small screen. 600px is where WordPress's navigation turns into its menu button.
 */
@media (width < 600px) {
	:root {
		--site-header-logo-size: var(--wp--custom--header--logo-size-compact);
		--site-header-padding-block: var(--wp--custom--header--padding-compact);
	}
}

/*
 * When the browser scrolls something into view (a link reached with Tab, a jump link), it keeps
 * it below the header instead of under it (WCAG 2.2, 2.4.11 Focus Not Obscured).
 */
html {
	scroll-padding-top: calc(var(--site-header-height) + var(--wp-admin--admin-bar--height, 0px) + var(--wp--custom--header--focus-gap));
}

/*
 * WordPress wraps the header part in its own <header>, a direct child of the page; only that
 * wrapper can stick, since a sticky element stays within its parent. Logged in, it sits below
 * WordPress's toolbar.
 */
header.wp-block-template-part:has(> .site-header) {
	position: sticky;
	top: var(--wp-admin--admin-bar--height, 0);
	z-index: var(--wp--custom--header--layer);
}

/* Below 600px WordPress's toolbar scrolls away with the page, so the header goes to the very top. */
@media (width < 600px) {
	header.wp-block-template-part:has(> .site-header) {
		top: 0;
	}
}

/*
 * Block and component class together outrank WordPress's layout rules at any load order.
 * "isolation" keeps the frosted layer below behind this header's content only, never behind the
 * page; unlike a filter or transform, it does not become a frame for fixed elements inside.
 */
.wp-block-group.site-header {
	position: relative;
	isolation: isolate;
	padding: var(--site-header-padding-block) var(--wp--custom--spacing--gutter);
	border-bottom: 1px solid var(--wp--preset--color--accent-5);
}

/*
 * The frosted background, on a layer of its own behind the header's content. A
 * backdrop-filter on the header itself would make the header the frame of everything fixed inside
 * it (CSS Filter Effects), and WordPress's open menu panel on a phone is fixed to the window: it
 * was trapped in the header strip, showing only its first link.
 */
.wp-block-group.site-header::before {
	position: absolute;
	inset: 0;
	z-index: -1;
	content: "";
	background: var(--wp--custom--header--background);
	backdrop-filter: blur(var(--wp--custom--header--blur));
}

.site-header .site-logo img {
	display: block;
	width: var(--site-header-logo-size);
	height: var(--site-header-logo-size);
}

/*
 * The hamburger menu on a phone drops down from under the header, only as tall as its links, with
 * the page still visible below. WordPress's open panel is fixed to the whole
 * window by default; here its top edge moves to the header's bottom and its bottom edge lets go.
 */

@media (width < 600px) {
	/*
	 * A panel at the right, as wide as its longest link plus the header's side spacing, so the
	 * page stays visible beside it as well as below (only a little wider than the
	 * widest word). It fits its content, so a renamed or added page still fits.
	 */
	.site-header .wp-block-navigation__responsive-container.is-menu-open {
		inset: var(--site-header-height) 0 auto auto;
		width: max-content;
		max-width: 100%;
		max-height: calc(100dvh - var(--site-header-height));
		padding: var(--site-header-padding-block) var(--wp--custom--spacing--gutter);
		background-color: transparent;
		border-bottom: 1px solid var(--wp--preset--color--accent-5);
		border-left: 1px solid var(--wp--preset--color--accent-5);
		border-bottom-left-radius: var(--wp--custom--panel-radius);

		/*
		 * WordPress slides the whole panel in with a transform, which would make the panel the
		 * frame of the fixed close button inside it while it runs; the links slide instead.
		 */
		animation: none;
	}

	/* The header's frosted background, on a layer behind the panel's links for the same reason. */
	.site-header .wp-block-navigation__responsive-container.is-menu-open::before {
		position: absolute;
		inset: 0;
		z-index: -1;
		content: "";
		background: var(--wp--custom--header--background);
		backdrop-filter: blur(var(--wp--custom--header--blur));
		border-bottom-left-radius: inherit;
	}

	/* The close button takes the menu button's place in the header, so it appears where the visitor tapped. */
	.site-header .wp-block-navigation__responsive-container.is-menu-open .wp-block-navigation__responsive-container-close {
		position: fixed;
		top: calc((var(--site-header-height) - var(--site-header-menu-button-size)) / 2);
		right: var(--wp--custom--spacing--gutter);
	}

	/* ...and the menu button steps aside meanwhile, or the two icons show one over the other. */
	.site-header .wp-block-navigation:has(.is-menu-open) .wp-block-navigation__responsive-container-open {
		visibility: hidden;
	}

	/* WordPress keeps room above the links for its close button, which now sits in the header. */
	.site-header .wp-block-navigation__responsive-container.is-menu-open .wp-block-navigation__responsive-container-content {
		padding-top: 0;
	}
}

@media (width < 600px) and (prefers-reduced-motion: no-preference) {
	.site-header .wp-block-navigation__responsive-container.is-menu-open .wp-block-navigation__responsive-container-content {
		animation: site-header-menu-drop var(--wp--custom--motion--menu-drop) ease-out;
	}
}

/* The links fade in while moving down into place, over the theme's drop distance and duration. */
@keyframes site-header-menu-drop {
	from {
		opacity: 0;
		transform: translateY(calc(-1 * var(--wp--custom--motion--menu-drop-distance)));
	}

	to {
		opacity: 1;
		transform: none;
	}
}

/* The site's name reads as a name, not as a link in running text. */
.site-header .wp-block-site-title a {
	font-weight: 700;
	text-decoration: none;
}

/*
 * The menu: links a little bolder, with an underline in the accent color for the page being
 * shown (marked by includes/menu.php) and for a link pointed at.
 */
.site-header .wp-block-navigation-item__content {
	font-weight: 500;
	text-decoration-color: var(--wp--preset--color--accent-1);
	text-decoration-thickness: var(--wp--custom--navigation--underline-thickness);
	text-underline-offset: var(--wp--custom--navigation--underline-offset);
}

.site-header .wp-block-navigation-item__content[aria-current="page"] {
	text-decoration-line: underline;
}

@media (hover: hover) {
	.site-header .wp-block-navigation-item__content:hover {
		text-decoration-line: underline;
	}
}
