---
// Custom header: identical to Starlight's default, plus a mobile-visible
// language selector (the default hides the whole right group behind the
// hamburger menu below the md breakpoint).
import config from 'virtual:starlight/user-config';

import LanguageSelect from 'virtual:starlight/components/LanguageSelect';
import Search from 'virtual:starlight/components/Search';
import SiteTitle from 'virtual:starlight/components/SiteTitle';
import SocialIcons from 'virtual:starlight/components/SocialIcons';
import ThemeSelect from 'virtual:starlight/components/ThemeSelect';

const shouldRenderSearch =
	config.pagefind || config.components.Search !== '@astrojs/starlight/components/Search.astro';
---

<div class="header">
	<div class="title-wrapper sl-flex">
		<SiteTitle />
	</div>
	<div class="sl-flex print:hidden">
		{shouldRenderSearch && <Search />}
	</div>
	<div class="sl-flex md:sl-hidden print:hidden mobile-lang">
		<LanguageSelect />
	</div>
	<div class="sl-hidden md:sl-flex print:hidden right-group">
		<div class="sl-flex social-icons">
			<SocialIcons />
		</div>
		<ThemeSelect />
		<LanguageSelect />
	</div>
</div>

<style>
	@layer starlight.core {
		.header {
			display: flex;
			gap: var(--sl-nav-gap);
			justify-content: space-between;
			align-items: center;
			height: 100%;
		}

		.title-wrapper {
			overflow: clip;
			padding: 0.25rem;
			margin: -0.25rem;
			min-width: 0;
		}

		.right-group,
		.social-icons {
			gap: 1rem;
			align-items: center;
		}
		.social-icons::after {
			content: '';
			height: 2rem;
			border-inline-end: 1px solid var(--sl-color-gray-5);
		}

		.mobile-lang {
			align-items: center;
			/* Keep the compact icon + caret, drop the wordy label so the
			   narrow bar (logo · search · language · menu) still fits. */
		}
		.mobile-lang :global(select) {
			width: 3.25rem;
			padding-inline: 0.25rem;
			color: transparent; /* hide the selected-label text, keep icon/caret */
		}
		.mobile-lang :global(option) {
			color: var(--sl-color-text);
		}

		@media (min-width: 50rem) {
			:global(:root[data-has-sidebar]) {
				--__sidebar-pad: calc(2 * var(--sl-nav-pad-x));
			}
			:global(:root:not([data-has-toc])) {
				--__toc-width: 0rem;
			}
			.header {
				--__sidebar-width: max(0rem, var(--sl-content-inline-start, 0rem) - var(--sl-nav-pad-x));
				--__main-column-fr: calc(
					(
							100% + var(--__sidebar-pad, 0rem) - var(--__toc-width, var(--sl-sidebar-width)) -
								(2 * var(--__toc-width, var(--sl-nav-pad-x))) - var(--sl-content-inline-start, 0rem) -
								var(--sl-content-width)
						) / 2
				);
				display: grid;
				grid-template-columns:
					minmax(
						calc(var(--__sidebar-width) + max(0rem, var(--__main-column-fr) - var(--sl-nav-gap))),
						auto
					)
					1fr
					auto;
				align-content: center;
			}
		}
	}
</style>
