/**
 * Header component: branding, primary nav, mobile menu overlay, search
 * modal, and the hamburger/search trigger icons.
 *
 * Always present in the header (every page), so it is enqueued
 * unconditionally by Kivora\Assets\Manager rather than per-block
 * (04-css-architecture.md §5).
 */

/*
 * The header's own height, exposed as a token because a transparent header
 * leaves the flow and the content underneath has to reserve the space itself
 * (see global.css §3).
 *
 * The token cannot be a guess: it was one, and it came out 24px short of the
 * rendered header, so the reserved space did not clear the header and the page
 * title still rendered inside the header band. It is now true by construction
 * — the header is built from parts that cannot exceed it: the container's
 * vertical padding, the tallest header item (the search and menu buttons, and
 * the branding, all capped at the control size), and the bottom border.
 */
:root {
	--kivora-header-control-size: 2.5rem;
	--kivora-header-height: calc((var(--kivora-spacing-xs) * 2) + var(--kivora-header-control-size) + 1px);
	--kivora-header-height-stacked: calc((var(--kivora-spacing-xs) * 5) + (var(--kivora-header-control-size) * 2) + 1px);
}

/*
 * A sticky header covers the top of whatever the browser scrolls into view,
 * so in-page links and focused elements land underneath it. scroll-padding
 * belongs to the scrolling element (html), and is only needed where the
 * header actually sticks: body sets --kivora-header-height for the stacked
 * layout, which html cannot inherit from, hence its own token above.
 */
html:has(.kivora-site-header--sticky) {
	scroll-padding-top: var(--kivora-header-height);
}

@media (min-width: 783px) {
	html:has(body.kivora-header-layout-stacked .kivora-site-header--sticky) {
		scroll-padding-top: var(--kivora-header-height-stacked);
	}
}

.kivora-site-header {
	border-block-end: 1px solid var(--kivora-color-border-muted);
}

/*
 * Announcement bar: a one-line navy notice above the header. It scrolls
 * away with the page — only the header itself ever sticks.
 */
.kivora-announcement {
	background: var(--kivora-color-primary);
	color: var(--kivora-color-background);
}

.kivora-announcement__text {
	margin: 0;
	padding-block: var(--kivora-spacing-xs);
	padding-inline: var(--kivora-spacing-sm);
	text-align: center;
	font-size: var(--kivora-font-size-small);
}

.kivora-announcement__link {
	color: var(--kivora-color-secondary);
	text-decoration: underline;
	text-underline-offset: 0.15em;
}

.kivora-announcement__link:hover {
	color: var(--kivora-color-background);
}

.kivora-site-header--sticky {
	position: sticky;
	top: 0;
	z-index: 500;
	background: var(--kivora-color-background);
}

.kivora-site-header--sticky.is-sticky {
	box-shadow: 0 2px 8px rgb(0 0 0 / 10%);
}

/*
 * Transparent header: overlays the content below it (e.g. a hero image)
 * instead of taking up its own block in the flow.
 */
.kivora-site-header--transparent {
	position: absolute;
	inset-inline: 0;
	top: 0;
	z-index: 500;
	background: transparent;
	border-block-end-color: transparent;
}

/*
 * Flipping the text and icons to the background color is only legible over a
 * dark, full-bleed hero. That is what .kivora-header-over-hero marks (see
 * Kivora\Page\Settings::filter_body_class) — everywhere else the header floats
 * over the page's own background, where background-colored text would be
 * invisible, so it keeps the normal foreground colors and the content below
 * reserves its height instead (global.css §3).
 */
.kivora-header-over-hero .kivora-site-header--transparent .kivora-site-title a {
	color: var(--kivora-color-background);
}

/*
 * Only the top-level items sit on the hero, and only on the wide layout.
 * Dropdowns and the mobile panel paint their own background-coloured
 * surface, so flipping every link there left background text on background
 * (1:1). The submenu chevron flips with its link: left dark, it measured
 * 1.25:1 to 2.27:1 against the hero.
 */
@media (min-width: 783px) {
	.kivora-header-over-hero .kivora-site-header--transparent .kivora-primary-nav__list > li > a,
	.kivora-header-over-hero .kivora-site-header--transparent .kivora-primary-nav__list > li > .kivora-submenu-toggle {
		color: var(--kivora-color-background);
	}
}

.kivora-header-over-hero .kivora-site-header--transparent .kivora-search-trigger__icon,
.kivora-header-over-hero .kivora-site-header--transparent .kivora-menu-toggle__icon {
	color: var(--kivora-color-background);
}

.kivora-site-header--transparent.kivora-site-header--sticky.is-sticky {
	position: sticky;
	background: var(--kivora-color-background);
	border-block-end-color: var(--kivora-color-border-muted);
}

.kivora-site-header--transparent.kivora-site-header--sticky.is-sticky .kivora-site-title a,
.kivora-site-header--transparent.kivora-site-header--sticky.is-sticky .kivora-primary-nav__list a,
.kivora-site-header--transparent.kivora-site-header--sticky.is-sticky .kivora-primary-nav__list .kivora-submenu-toggle {
	color: var(--kivora-color-foreground);
}

.kivora-site-header--transparent.kivora-site-header--sticky.is-sticky .kivora-search-trigger__icon,
.kivora-site-header--transparent.kivora-site-header--sticky.is-sticky .kivora-menu-toggle__icon {
	color: var(--kivora-color-foreground);
}

/*
 * The header spans the wide container, not the reading container: branding
 * and navigation have to line up with the page content below them, and the
 * content templates use the wide width (.kivora-layout--wide) whenever they
 * carry a grid or a sidebar.
 */
.kivora-site-header .kivora-container {
	max-width: var(--wp--style--global--wide-size, 1200px);
	display: flex;
	align-items: center;
	justify-content: space-between;
	gap: var(--kivora-spacing-sm);
	padding-block: var(--kivora-spacing-xs);
}

/*
 * Branding: whether it holds a logo or the site title, it stays within the
 * control size so it never makes the header taller than its own token.
 */
.kivora-site-branding {
	display: flex;
	align-items: center;
	min-block-size: var(--kivora-header-control-size);
}

.kivora-site-title {
	margin-block: 0;
}

.kivora-site-branding .custom-logo {
	display: block;
	inline-size: auto;
	block-size: auto;
	max-block-size: var(--kivora-header-control-size);
}

.kivora-site-title a {
	font-size: var(--kivora-font-size-h5);
	font-weight: 700;
	text-decoration: none;
	color: var(--kivora-color-foreground);
}

/* Primary nav: horizontal list on desktop */
.kivora-primary-nav__list {
	display: flex;
	flex-wrap: wrap;
	align-items: center;
	gap: var(--kivora-spacing-sm);
	list-style: none;
	margin: 0;
	padding: 0;
}

.kivora-primary-nav__list a {
	color: var(--kivora-color-foreground);
	text-decoration: none;
}

.kivora-primary-nav__list a:hover,
.kivora-primary-nav__list .current-menu-item > a {
	color: var(--kivora-color-primary);
}

/*
 * Colour alone marked the current page: primary against the foreground of
 * the other items is 1.25:1 to 2.27:1, and over a hero or in forced-colors
 * mode the colour difference is gone entirely. The underline survives both.
 */
.kivora-primary-nav__list .current-menu-item > a,
.kivora-primary-nav__list a[aria-current="page"] {
	text-decoration: underline;
	text-decoration-thickness: 2px;
	text-underline-offset: 0.3em;
}

/*
 * Two-row header layouts. Row 1 always measures (xs + control + xs) like the
 * standard header; row 2 is the nav with xs padding and control-height links,
 * so the two-row height token below stays true by the same construction.
 */
.kivora-site-header--layout-centered .kivora-container,
.kivora-site-header--layout-stacked .kivora-container {
	display: grid;
	align-items: center;
	row-gap: var(--kivora-spacing-xs);
}

.kivora-site-header--layout-centered .kivora-container {
	grid-template-columns: 1fr auto 1fr;
}

/* The empty first cell balances the actions, so the branding sits truly centered. */
.kivora-site-header--layout-centered .kivora-site-branding {
	grid-column: 2;
	grid-row: 1;
	justify-self: center;
}

.kivora-site-header--layout-centered .kivora-header-actions {
	grid-column: 3;
	grid-row: 1;
	justify-self: end;
}

.kivora-site-header--layout-centered .kivora-primary-nav {
	grid-column: 1 / -1;
	grid-row: 2;
	justify-self: center;
}

/* Stacked: branding and actions share row 1, the nav takes a divided row 2. */
.kivora-site-header--layout-stacked .kivora-container {
	grid-template-columns: 1fr auto;
}

.kivora-site-header--layout-stacked .kivora-site-branding {
	grid-column: 1;
	grid-row: 1;
}

.kivora-site-header--layout-stacked .kivora-header-actions {
	grid-column: 2;
	grid-row: 1;
	justify-self: end;
}

.kivora-site-header--layout-stacked .kivora-primary-nav {
	grid-column: 1 / -1;
	grid-row: 2;
}

.kivora-site-header--layout-centered .kivora-primary-nav__list a,
.kivora-site-header--layout-stacked .kivora-primary-nav__list a {
	line-height: var(--kivora-header-control-size);
}

.kivora-site-header--layout-centered .kivora-primary-nav,
.kivora-site-header--layout-stacked .kivora-primary-nav {
	padding-block: var(--kivora-spacing-xs);
}

/*
 * Two rows reserve two rows: row 1 + gap + row 2 + the header border, for the
 * transparent header's content reserve (global.css §3).
 */
body.kivora-header-layout-centered,
body.kivora-header-layout-stacked {
	--kivora-header-height: calc((var(--kivora-spacing-xs) * 5) + (var(--kivora-header-control-size) * 2) + 1px);
}

/* Submenus: hover on desktop, click-driven on mobile via aria-expanded */
.kivora-primary-nav__list ul {
	list-style: none;
	margin: 0;
	padding: var(--kivora-spacing-xs);
}

.kivora-primary-nav__list li {
	position: relative;
}

.kivora-primary-nav__list ul {
	position: absolute;
	inset-inline-start: 0;
	top: 100%;
	min-width: 12rem;
	background: var(--kivora-color-background);
	border: 1px solid var(--kivora-color-border-muted);
	display: none;
}

/*
 * Visibility belongs to hover (pointing devices only) and to the toggle
 * button. Two rules are deliberately absent:
 * - :focus-within: the toggle keeps focus after it is pressed, so such a
 *   rule re-showed the submenu the toggle had just closed.
 * - bare :hover: touch screens emulate hover on tap and it sticks, so a
 *   collapsed submenu would stay visible under the finger. Keyboard users
 *   operate the toggle itself, the next Tab stop after its parent link.
 */
@media (hover: hover) {
	.kivora-primary-nav__list li:hover:not([data-kivora-hover-dismissed="true"]) > ul {
		display: block;
	}
}

/*
 * Without script the toggle buttons do nothing, so the keyboard reaches a
 * submenu by focus instead. Scoped to .no-js because with script the toggle
 * owns the state (see the note above on why :focus-within is not used then).
 */
.no-js .kivora-primary-nav__list li:focus-within > ul {
	display: block;
}

.kivora-primary-nav__list li[data-kivora-submenu-open="true"] > ul {
	display: block;
}

/*
 * Deeper levels fly out to the side instead of piling onto the same spot:
 * every nested ul used to open at top:100%/inset-inline-start:0 of its own
 * parent, so level 3 sat exactly on top of level 2. The last top-level item
 * flies inward so its menus never leave the viewport.
 */
.kivora-primary-nav__list ul ul {
	inset-inline-start: 100%;
	top: 0;
}

.kivora-primary-nav__list > li:last-child ul ul {
	inset-inline-start: auto;
	inset-inline-end: 100%;
}

/*
 * Level 3 and deeper drop below their parent item instead of flying further
 * right: every additional flyout marches toward (and past) the viewport
 * edge, which is exactly the page-level scrollbar a ten-level menu shows.
 * Two columns of flyout is all the width there is.
 */
.kivora-primary-nav__list ul ul ul {
	inset-inline-start: 0;
	top: 100%;
}

/*
 * Submenu toggle: a visible chevron beside parent links. An unstyled
 * <button> would be an invisible keyboard stop — and with no cue at all,
 * child items read as top-level siblings.
 */
.kivora-submenu-toggle {
	display: inline-flex;
	align-items: center;
	justify-content: center;
	inline-size: 1.75rem;
	block-size: 1.75rem;
	padding: 0;
	background: none;
	border: 1px solid transparent;
	font-size: 0.875rem;
	line-height: 1;
	color: var(--kivora-color-foreground);
	cursor: pointer;
}

.kivora-submenu-toggle::before {
	content: "▾";
	content: "▾" / "";
}

.kivora-submenu-toggle[aria-expanded="true"]::before {
	content: "▴";
	content: "▴" / "";
}

.kivora-submenu-toggle:hover {
	color: var(--kivora-color-primary);
}

/* Nesting cues: nested links step in, every level deeper than the last —
 * on desktop dropdowns and in the mobile stack alike — so children never
 * read as top-level siblings. */
.kivora-primary-nav__list ul a {
	padding-inline-start: var(--kivora-spacing-sm);
}

.kivora-primary-nav__list ul ul a {
	padding-inline-start: var(--kivora-spacing-md);
}

.kivora-primary-nav__list ul ul ul a {
	padding-inline-start: calc(var(--kivora-spacing-md) * 2);
}

.kivora-header-actions {
	display: flex;
	align-items: center;
	gap: var(--kivora-spacing-xs);
}

/* Search trigger */
.kivora-search-trigger,
.kivora-menu-toggle {
	display: inline-flex;
	align-items: center;
	justify-content: center;
	inline-size: var(--kivora-header-control-size);
	block-size: var(--kivora-header-control-size);
	padding: 0;
	background: none;
	border: 1px solid transparent;
	cursor: pointer;
}

/* Inline SVG icons: they inherit the button's color via currentColor, so the
   transparent-header and sticky states need no separate icon rules. */
.kivora-search-trigger__icon,
.kivora-menu-toggle__icon {
	width: 1.25rem;
	height: 1.25rem;
	color: var(--kivora-color-foreground);
}

.kivora-search-trigger:hover .kivora-search-trigger__icon,
.kivora-menu-toggle:hover .kivora-menu-toggle__icon {
	color: var(--kivora-color-primary);
}

.kivora-menu-toggle {
	display: none;
}

/*
 * Core Navigation block overlay (in-post navigation blocks): give its
 * content breathing room — core ships it flush against the viewport edge.
 */
.wp-block-navigation__responsive-container.is-menu-open .wp-block-navigation__responsive-container-content {
	padding: var(--kivora-spacing-lg) var(--kivora-spacing-md);
}

/* In-overlay close button: hidden everywhere except the open mobile menu.
 * Without it touch users have no way back — the opening toggle sits under
 * the overlay and there is no Escape key. */
.kivora-menu-close {
	display: none;
}

@media (max-width: 782px) {
	.js .kivora-primary-nav.is-open .kivora-menu-close {
		display: inline-flex;
		align-items: center;
		justify-content: center;
		position: absolute;
		top: var(--kivora-spacing-md);
		inset-inline-end: var(--kivora-spacing-md);
		inline-size: var(--kivora-header-control-size);
		block-size: var(--kivora-header-control-size);
		padding: 0;
		background: none;
		border: 1px solid transparent;
		font-size: 1.75rem;
		line-height: 1;
		color: var(--kivora-color-foreground);
		cursor: pointer;
	}

	.js .kivora-primary-nav.is-open .kivora-menu-close:hover,
	.js .kivora-primary-nav.is-open .kivora-menu-close:focus-visible {
		color: var(--kivora-color-primary);
	}
}

/* Search modal */
.kivora-search-modal {
	position: fixed;
	inset: 0;
	z-index: 1000;
	display: flex;
	align-items: flex-start;
	justify-content: center;
	padding-top: var(--kivora-spacing-xl);
	background: rgb(0 0 0 / 60%);

	/*
	 * The page behind is scroll-locked while the dialog is open, so the
	 * dialog scrolls itself: at 400% zoom or on a landscape phone the
	 * results ran past the bottom of the viewport with no way to reach them.
	 */
	overflow-y: auto;
	overscroll-behavior: contain;
	padding-bottom: var(--kivora-spacing-md);
}

.kivora-search-modal[hidden] {
	display: none;
}

/*
 * Same toolbar problem as the mobile menu: the modal covers the viewport but
 * the toolbar (z-index 99999) sits above it, so the panel's top edge, and
 * the close button inside it, slid underneath. The backdrop stays full
 * height; only the panel starts lower.
 */
.admin-bar .kivora-search-modal {
	padding-top: calc(var(--wp-admin--admin-bar--height, 32px) + var(--kivora-spacing-md));
}

.kivora-search-modal--noscript {
	position: static;
	background: none;
	padding: var(--kivora-spacing-sm) 0;
}

.kivora-search-modal__panel {
	position: relative;
	width: min(32rem, 90vw);
	padding: var(--kivora-spacing-md);
	background: var(--kivora-color-background);

	/* Transparent until forced-colors mode draws it: then the panel keeps an
	   edge against the backdrop. */
	border: 1px solid transparent;
}

/*
 * A 20.5x16.5 button sat under WCAG 2.5.8's 24x24 minimum; padding rather
 * than font-size keeps the glyph where it was and grows only the hit area.
 */
.kivora-search-modal__close {
	position: absolute;
	top: var(--kivora-spacing-xs);
	inset-inline-end: var(--kivora-spacing-xs);
	display: inline-flex;
	align-items: center;
	justify-content: center;
	min-inline-size: 2rem;
	min-block-size: 2rem;
	padding: 0;
	background: none;
	border: 0;
	font-size: var(--kivora-font-size-h5);
	line-height: 1;
	cursor: pointer;
}

/* Live search results (Kivora\Search\Manager, when enabled) */
.kivora-search-modal__results-list {
	list-style: none;
	margin: var(--kivora-spacing-sm) 0 0;
	padding: 0;
	max-height: 60vh;
	overflow-y: auto;

	/* Room for the focus ring, which the scroll container would clip. */
	padding-inline: 0.25rem;
}

.kivora-search-modal__result {
	padding-block: var(--kivora-spacing-xs);
	border-block-start: 1px solid var(--kivora-color-border-muted);
}

.kivora-search-modal__result-title {
	display: block;
	font-weight: 700;
	color: var(--kivora-color-foreground);
	text-decoration: none;
}

.kivora-search-modal__result-title:hover {
	color: var(--kivora-color-primary);
}

.kivora-search-modal__result-excerpt {
	margin: 0.25rem 0 0;
	color: var(--kivora-color-border-muted);
	font-size: var(--kivora-font-size-small);
}

/* The stacked divider is a shadow, not a border, so the two-row height
 * token above stays exact. */
@media (min-width: 783px) {
	.kivora-site-header--layout-stacked .kivora-primary-nav {
		box-shadow: inset 0 1px 0 var(--kivora-color-border-muted);
	}
}

/* Mobile navigation: collapsed by default once JS is available */
@media (max-width: 782px) {
	/* The nav leaves the flow for its overlay, so release its grid row —
	 * otherwise an empty row plus the row gap pads the header bottom.
	 * justify-self must also go back to stretch: it shrink-wraps even a
	 * fixed element, and then translateX(100%) only moves that sliver,
	 * leaving the "hidden" overlay peeking into the viewport. */
	.kivora-site-header--layout-centered .kivora-primary-nav,
	.kivora-site-header--layout-stacked .kivora-primary-nav {
		grid-row: auto;
		justify-self: stretch;
		padding-block: 0;
	}

	/* Only with the theme's script does the toggle do anything; without it
	   (or when main.js failed and the page fell back to no-js) the menu
	   shows as a plain list and a dead button would only mislead. */
	.js .kivora-menu-toggle {
		display: inline-flex;
	}

	.js .kivora-primary-nav {
		position: fixed;
		inset: 0;
		z-index: 999;
		background: var(--kivora-color-background);
		padding: var(--kivora-spacing-lg) var(--kivora-spacing-md);
		overflow-y: auto;
		transform: translateX(100%);
		/*
		 * visibility, not just the transform: an off-screen panel is still in
		 * the tab order and in the accessibility tree, so Tab walked through
		 * six invisible links before reaching the search and menu buttons,
		 * and a screen reader offered a menu the user could not see. The
		 * delay keeps the slide-out animation visible after closing.
		 */
		visibility: hidden;
		transition: transform 0.2s ease, visibility 0s linear 0.2s;
	}

	.js .kivora-primary-nav.is-open {
		transform: translateX(0);
		visibility: visible;
		transition: transform 0.2s ease, visibility 0s;
	}

	/*
	 * The toolbar sits above the panel (z-index 99999) and used to cover its
	 * top row, including the close button, so the panel's contents have to
	 * start below it. Padding, not a `top` offset: the toolbar is positioned
	 * absolutely and scrolls away with the page, and a panel that began below
	 * it would leave a strip of the page showing above the menu once the page
	 * was scrolled. The panel keeps covering the whole viewport; only its
	 * content moves down. WordPress exposes the height as a custom property
	 * (46px on phones, 32px on wider screens).
	 */
	.admin-bar .kivora-primary-nav {
		padding-block-start: calc(var(--wp-admin--admin-bar--height, 32px) + var(--kivora-spacing-lg));
	}

	/*
	 * The close button is absolutely positioned inside the panel, so the
	 * padding above does not move it: it still has to be pushed down on its
	 * own, or the toolbar covers the one control that closes the menu.
	 */
	.admin-bar .kivora-primary-nav.is-open .kivora-menu-close {
		top: calc(var(--wp-admin--admin-bar--height, 32px) + var(--kivora-spacing-md));
	}

	.js .kivora-primary-nav__list {
		flex-direction: column;
		align-items: flex-start;
	}

	.js .kivora-primary-nav__list ul {
		position: static;
		display: none;
		border: 0;
	}

	.js .kivora-primary-nav__list li[data-kivora-submenu-open="true"] > ul {
		display: block;
	}
}
