/* @svelte-lean/styles — nav (Tier 0, <nav> + <ul> of links + <details name> groups).
 * Markup: <nav data-slean="nav" aria-label="Main"> [<h2>Section</h2>] <ul> <li><a href="/"
 * aria-current="page"><span data-slean-part="icon">svg</span>Dashboard <span
 * data-slean="badge">3</span></a></li> <li><details name="main-nav" open><summary><span
 * data-slean-part="icon">svg</span>Players</summary> <ul><li><a href="/players">All
 * players</a></li></ul></details></li> </ul> </nav>.
 * A row is a link, a group's summary, or a button (the trigger of a rail flyout), each a child of
 * an <li>. The current page is aria-current="page": a soft fill in the current color; the group
 * around it is marked through :has(), and a closed group holding it takes the fill itself. The
 * chevron is masked on summary::after. A nested list hangs under its summary with a guide line;
 * opening animates where ::details-content and interpolate-size exist. A badge at the end of a row
 * is pushed to the inline end; an actions part (a button beside the link, in the same <li>) sits
 * over the row's inline end. Section labels are <h2>/<h3> children of the root, or the
 * group-label part. Options: data-slean-orientation="horizontal" draws the top-level links as a
 * strip of line tabs that scrolls sideways (scrollbar hidden; focus scrolls a link into view);
 * data-slean-collapsed narrows a vertical nav to an icon rail: each top-level row is a square
 * that clips its label (still the accessible name), a badge moves to the row's corner, a nested
 * list and a summary's chevron are hidden, and a section label becomes a short rule; a group
 * there is a button followed by its hover card (popover="hint", interestfor), marked when the card
 * holds the current page; data-slean-scrollspy makes the root a scroll target group, so the link whose target is in view
 * is :target-current and styled as current (Chromium; elsewhere nothing is marked). Color:
 * data-color on the root or an ancestor colors the current link (primary by default). */
@layer slean.reset, slean.tokens, slean.base, slean.components, slean.compositions, slean.utilities;

@layer slean.components {
	:where([data-slean='nav']) {
		display: flex;
		flex-direction: column;
		gap: var(--slean-space-1);
		min-inline-size: 0;
		color: var(--slean-fg);
		font-size: var(--slean-text-sm);
		line-height: var(--slean-leading);
	}

	:where([data-slean='nav'] ul) {
		display: flex;
		flex-direction: column;
		gap: 0.125rem;
		margin: 0;
		padding: 0;
		list-style: none;
	}

	:where([data-slean='nav'] li) {
		position: relative;
		min-inline-size: 0;
	}

	/* Section labels: a heading child of the root, or the group-label part. */
	:where([data-slean='nav'] > h2),
	:where([data-slean='nav'] > h3),
	:where([data-slean='nav'] > [data-slean-part='group-label']) {
		margin: 0;
		padding-block: var(--slean-space-3) 0;
		padding-inline: var(--slean-space-3);
		color: var(--slean-fg-muted);
		font-size: var(--slean-typography-overline-size);
		font-weight: var(--slean-font-weight-semibold);
		line-height: var(--slean-typography-overline-line-height);
		letter-spacing: 0.08em;
		text-transform: var(--slean-typography-overline-transform);
	}

	:where([data-slean='nav'] > h2:first-child),
	:where([data-slean='nav'] > h3:first-child),
	:where([data-slean='nav'] > [data-slean-part='group-label']:first-child) {
		padding-block-start: 0;
	}

	/* Rows: a link, a group's summary, a flyout trigger. */
	:where([data-slean='nav'] li > a),
	:where([data-slean='nav'] li > details > summary),
	:where([data-slean='nav'] li > button:not([data-slean-part])) {
		appearance: none;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		gap: var(--slean-space-3);
		inline-size: 100%;
		min-block-size: var(--slean-control-height-md);
		margin: 0;
		padding-block: var(--slean-space-1);
		padding-inline: var(--slean-space-3);
		border: 0;
		border-radius: var(--slean-radius-md);
		background: transparent;
		color: var(--slean-fg);
		font: inherit;
		text-align: start;
		text-decoration: none;
		overflow-wrap: anywhere;
		cursor: pointer;
		-webkit-tap-highlight-color: transparent;
		transition:
			background-color var(--slean-duration-fast) var(--slean-ease),
			color var(--slean-duration-fast) var(--slean-ease);
	}

	:where([data-slean='nav'] li > details > summary) {
		list-style: none;
		user-select: none;
	}

	:where([data-slean='nav'] li > details > summary)::-webkit-details-marker {
		display: none;
	}

	/* The chevron of a group, turned while the group is open. */
	:where([data-slean='nav'] li > details > summary)::after {
		content: '';
		flex: none;
		inline-size: 1rem;
		block-size: 1rem;
		margin-inline-start: auto;
		background: currentColor;
		mask: var(--slean-icon-chevron-down) center / contain no-repeat;
		opacity: 0.6;
		transition: rotate var(--slean-duration-normal) var(--slean-ease);
	}

	:where([data-slean='nav'] li > details[open] > summary)::after {
		rotate: 180deg;
	}

	:where([data-slean='nav'] li > details > summary:has(> [data-slean='badge']))::after {
		margin-inline-start: 0;
	}

	:where([data-slean='nav'] [data-slean-part='icon']) {
		display: inline-flex;
		flex: none;
		align-items: center;
		justify-content: center;
		inline-size: 1.125rem;
		block-size: 1.125rem;
		font-size: 1.125rem;
	}

	/* A badge closes the row at its inline end. */
	:where([data-slean='nav'] li > a > [data-slean='badge']),
	:where([data-slean='nav'] li > details > summary > [data-slean='badge']) {
		flex: none;
		margin-inline-start: auto;
	}

	/* A button beside the link (pin, more), over the row's inline end. */
	:where([data-slean='nav'] li > [data-slean-part='actions']) {
		position: absolute;
		inset-block-start: calc(var(--slean-control-height-md) / 2);
		inset-inline-end: var(--slean-space-1);
		display: inline-flex;
		align-items: center;
		gap: var(--slean-space-1);
		translate: 0 -50%;
	}

	:where([data-slean='nav'] li:has(> [data-slean-part='actions']) > a) {
		padding-inline-end: calc(var(--slean-control-height-sm) + var(--slean-space-2));
	}

	@media (hover: hover) {
		:where([data-slean='nav'] li > a[href]:hover),
		:where([data-slean='nav'] li > details > summary:hover),
		:where([data-slean='nav'] li > button:not([data-slean-part]):hover) {
			background: var(--slean-muted);
		}
	}

	/* The current page; listed after hover so it keeps its fill under the pointer. */
	:where([data-slean='nav'] li > a[aria-current='page']),
	:where([data-slean='nav'][data-slean-scrollspy] li > a:target-current) {
		background: var(--slean-color-soft);
		color: var(--slean-color-soft-text);
		font-weight: var(--slean-font-weight-medium);
	}

	/* The group holding the current page: its name in full weight; while closed, it carries the
	 * current page's fill, since the link itself is hidden. */
	:where([data-slean='nav'] li > details:has([aria-current='page']) > summary) {
		font-weight: var(--slean-font-weight-medium);
	}

	:where([data-slean='nav'] li > details:not([open]):has([aria-current='page']) > summary),
	:where(
		[data-slean='nav'][data-slean-collapsed]
			> ul
			> li
			> details:has([aria-current='page'])
			> summary
	),
	:where([data-slean='nav'] li:has([aria-current='page']) > button:not([data-slean-part])) {
		background: var(--slean-color-soft);
		color: var(--slean-color-soft-text);
		font-weight: var(--slean-font-weight-medium);
	}

	:where([data-slean='nav'] li > a[aria-disabled='true']) {
		opacity: 0.5;
		cursor: not-allowed;
	}

	:where([data-slean='nav'] li > a:focus-visible),
	:where([data-slean='nav'] li > details > summary:focus-visible),
	:where([data-slean='nav'] li > button:not([data-slean-part]):focus-visible) {
		outline: var(--slean-focus-ring);
		outline-offset: calc(var(--slean-focus-ring-width) * -1);
	}

	/* A group's list hangs under the middle of the summary's icon, with a guide line. */
	:where([data-slean='nav'] li > details > ul) {
		margin-block: 0.125rem;
		margin-inline-start: calc(var(--slean-space-3) + 0.5rem);
		padding-inline-start: var(--slean-space-2);
		border-inline-start: 1px solid var(--slean-border);
	}

	:where([data-slean='nav'] li > details) {
		interpolate-size: allow-keywords;
	}

	:where([data-slean='nav'] li > details)::details-content {
		block-size: 0;
		overflow: clip;
		transition:
			block-size var(--slean-duration-normal) var(--slean-ease),
			content-visibility var(--slean-duration-normal) allow-discrete;
	}

	:where([data-slean='nav'] li > details[open])::details-content {
		block-size: auto;
	}

	/* The scroll target group of data-slean-scrollspy: one current link for the whole nav. */
	:where([data-slean='nav'][data-slean-scrollspy]) {
		scroll-target-group: auto;
	}

	/* Horizontal: the top-level links as a strip of line tabs, scrolling sideways. Content after
	 * the list (a settings button) stays at the end of the row. */
	:where([data-slean='nav'][data-slean-orientation='horizontal']) {
		flex-direction: row;
		align-items: center;
		gap: var(--slean-space-2);
	}

	:where([data-slean='nav'][data-slean-orientation='horizontal'] > ul) {
		flex: 1 1 auto;
		flex-direction: row;
		gap: var(--slean-space-1);
		min-inline-size: 0;
		box-shadow: inset 0 -1px 0 var(--slean-border);
		overflow-x: auto;
		overscroll-behavior-x: contain;
		scrollbar-width: none;
	}

	:where([data-slean='nav'][data-slean-orientation='horizontal'] > ul > li) {
		flex: none;
	}

	:where([data-slean='nav'][data-slean-orientation='horizontal'] > ul > li > a) {
		inline-size: auto;
		gap: var(--slean-space-2);
		border-block-end: 2px solid transparent;
		border-radius: 0;
		color: var(--slean-fg-muted);
		font-weight: var(--slean-font-weight-medium);
		white-space: nowrap;
		overflow-wrap: normal;
		transition:
			color var(--slean-duration-fast) var(--slean-ease),
			border-color var(--slean-duration-fast) var(--slean-ease);
	}

	@media (hover: hover) {
		:where([data-slean='nav'][data-slean-orientation='horizontal'] > ul > li > a[href]:hover) {
			background: transparent;
			color: var(--slean-fg);
		}
	}

	:where(
		[data-slean='nav'][data-slean-orientation='horizontal'] > ul > li > a[aria-current='page']
	),
	:where(
		[data-slean='nav'][data-slean-orientation='horizontal'][data-slean-scrollspy]
			> ul
			> li
			> a:target-current
	) {
		border-block-end-color: var(--slean-color-main);
		background: transparent;
		color: var(--slean-color-main);
	}

	/* Collapsed: an icon rail. Each top-level row is a square that clips its label after the icon
	 * (the label stays the row's accessible name); data-slean-hint names it on screen. */
	:where([data-slean='nav'][data-slean-collapsed] > ul > li > a),
	:where([data-slean='nav'][data-slean-collapsed] > ul > li > details > summary),
	:where([data-slean='nav'][data-slean-collapsed] > ul > li > button:not([data-slean-part])) {
		position: relative;
		inline-size: var(--slean-control-height-lg);
		min-block-size: var(--slean-control-height-lg);
		margin-inline: auto;
		padding-inline: calc((var(--slean-control-height-lg) - 1.125rem) / 2);
		gap: var(--slean-control-height-lg);
		overflow: clip;
		white-space: nowrap;
	}

	:where([data-slean='nav'][data-slean-collapsed] > ul > li > details > summary)::after {
		display: none;
	}

	:where([data-slean='nav'][data-slean-collapsed] > ul > li > details > ul),
	:where([data-slean='nav'][data-slean-collapsed] > ul > li > [data-slean-part='actions']) {
		display: none;
	}

	/* A badge moves to the corner of its square. */
	:where([data-slean='nav'][data-slean-collapsed] > ul > li > a > [data-slean='badge']),
	:where(
		[data-slean='nav'][data-slean-collapsed] > ul > li > details > summary > [data-slean='badge']
	) {
		position: absolute;
		inset-block-start: 0.125rem;
		inset-inline-end: 0.125rem;
		margin: 0;
	}

	/* A section label becomes a short rule; its text stays for assistive technology. */
	:where([data-slean='nav'][data-slean-collapsed] > h2),
	:where([data-slean='nav'][data-slean-collapsed] > h3),
	:where([data-slean='nav'][data-slean-collapsed] > [data-slean-part='group-label']) {
		inline-size: 1.5rem;
		block-size: 1px;
		margin-block: var(--slean-space-2);
		margin-inline: auto;
		padding: 0;
		overflow: clip;
		background: var(--slean-border);
		color: transparent;
		white-space: nowrap;
	}

	:where([data-slean='nav'][data-slean-collapsed] > h2:first-child),
	:where([data-slean='nav'][data-slean-collapsed] > h3:first-child),
	:where([data-slean='nav'][data-slean-collapsed] > [data-slean-part='group-label']:first-child) {
		margin-block-start: 0;
	}

	@media (forced-colors: active) {
		:where([data-slean='nav'] li > details > summary)::after {
			forced-color-adjust: none;
			background: CanvasText;
		}

		:where([data-slean='nav'] li > a[aria-current='page']),
		:where([data-slean='nav'][data-slean-scrollspy] li > a:target-current),
		:where([data-slean='nav'] li > details:not([open]):has([aria-current='page']) > summary),
		:where(
			[data-slean='nav'][data-slean-collapsed]
				> ul
				> li
				> details:has([aria-current='page'])
				> summary
		),
		:where([data-slean='nav'] li:has([aria-current='page']) > button:not([data-slean-part])) {
			forced-color-adjust: none;
			background: Highlight;
			color: HighlightText;
		}

		:where(
			[data-slean='nav'][data-slean-orientation='horizontal'] > ul > li > a[aria-current='page']
		) {
			border-block-end-color: Highlight;
			background: Canvas;
			color: LinkText;
		}

		:where([data-slean='nav'] li > a[aria-disabled='true']) {
			color: GrayText;
			opacity: 1;
		}

		:where([data-slean='nav'][data-slean-collapsed] > h2),
		:where([data-slean='nav'][data-slean-collapsed] > h3),
		:where([data-slean='nav'][data-slean-collapsed] > [data-slean-part='group-label']) {
			forced-color-adjust: none;
			background: CanvasText;
		}
	}
}
