/*
 * LDN navigation: mega-menu panel and narrow layout — PRD-018 CP125_03.
 *
 * Replaces the per-site Customizer rules
 *   .mega-menu > ul { width: 1140px; left: -34% !important; }
 * whose offset was calibrated to the position of the old top-level items, so it
 * misaligns the moment the item set changes - which is exactly what PRD-018 does.
 * Panel geometry hugs the columns, then clamps to the viewport and a custom
 * property. Each panel hangs from the item that opened it (`position: relative`
 * on the item, `left: 0`), never a percentage offset tuned to item order.
 *
 * COLOURS ARE INHERITED, NOT IMPOSED. The menu lives in the theme's header, and
 * this stylesheet loads on every page including legacy editorial posts, where
 * LDN's own `--ldn-*` custom properties are NOT emitted (only the page renderers
 * emit those). So every colour resolves to `currentColor`, `inherit` or a system
 * keyword, with the LDN token as an optional override when it happens to exist.
 * That is also why there is not a single hex value below.
 *
 * Scoped to `.ldn-nav-*` throughout: a theme menu LDN does not own must be
 * untouched, and `.mega-menu` is a class name other plugins use too.
 */

/* =========================================================================
   Desktop mega-menu panel
   ========================================================================= */

/*
 * Hang the panel from the item that opened it. `static` used to pin every
 * panel to the nav container so a 1140px mega stayed on screen; content-sized
 * panels belong under the label, the same as any other dropdown.
 */
.ldn-nav-item.ldn-nav-mega {
	position: relative;
}

.ldn-nav-mega > :is(.sub-menu, .ldn-nav-sub) {
	--ldn-nav-cols: 4;

	position: absolute;
	top: 100%;
	left: 0;
	right: auto;
	z-index: 20;

	/*
	 * Hug the columns, then clamp. A fixed 1140px (or even min(100vw, 1140px))
	 * makes a two-column panel as wide as Learn's six, with empty track to the
	 * right of the last heading. Content width is the usual model for mixed
	 * 2-col / 6-col bars; the cap is only an overflow guard.
	 */
	width: max-content;
	max-width: min(calc(100vw - 2rem), var(--ldn-nav-panel-max, 1140px));
	box-sizing: border-box;

	/*
	 * GeneratePress hides a submenu with `height: 0; overflow: hidden;
	 * pointer-events: none` as well as position and opacity, and the only thing
	 * that lifts it is the theme's own `li:hover > ul`. This panel also opens on
	 * `:focus-within`, so the clip has to go unconditionally and the closed state
	 * has to be carried by visibility/opacity/pointer-events below. Left to the
	 * theme, a keyboard user reached a panel clipped to its own padding.
	 */
	height: auto;
	overflow: visible;

	display: grid;
	/*
	 * max-content so a short "Tools" column is only as wide as its labels, not
	 * an equal share of 1140px. minmax(0, …) still lets a long localised label
	 * shrink when the panel hits the viewport cap.
	 * See .cursor/rules/mobile-execution.mdc.
	 */
	grid-template-columns: repeat(var(--ldn-nav-cols), minmax(0, max-content));
	gap: 0.25rem 2rem;
	align-items: start;

	padding: 1.375rem 1.625rem 1.5rem;
	margin: 0;
	list-style: none;

	/*
	 * A solid surface lifted by a diffused downward shadow, flush with the
	 * header rule and rounded at the bottom to match Ringspo's 12px cards.
	 * Deliberately NOT a tinted panel with a matching 1px brand border -
	 * .cursor/rules/no-outlined-colour-boxes.mdc forbids that layout tell.
	 */
	background: var(--ldn-nav-panel-bg, var(--ldn-shell-surface, var(--ldn-background, Canvas)));
	color: inherit;
	border-top: 1px solid var(--ldn-shell-rule, rgb(0 0 0 / 8%));
	border-radius: 0 0 12px 12px;
	box-shadow: 0 16px 36px -8px rgb(0 0 0 / 12%), 0 4px 12px -2px rgb(0 0 0 / 6%);
}

/*
 * Last items sit on the right of a flex-end bar. Hang those panels from the
 * item's right edge so a 400px box does not run off the viewport. Counted from
 * the end of the list, not a pixel offset, so adding a label does not retune it.
 */
.ldn-shell-primary__list > .ldn-nav-mega:nth-last-child(-n+2) > :is(.sub-menu, .ldn-nav-sub),
.main-navigation > ul > .ldn-nav-mega:nth-last-child(-n+2) > :is(.sub-menu, .ldn-nav-sub) {
	left: auto;
	right: 0;
}

/*
 * Six columns is almost the header width. Hanging that from the Learn label
 * would run off the viewport, so this one pins to the header container.
 */
.ldn-nav-item.ldn-nav-mega-6-col {
	position: static;
}

.ldn-nav-mega-6-col > :is(.sub-menu, .ldn-nav-sub) {
	max-width: min(100%, calc(100vw - 2rem), var(--ldn-nav-panel-max, 1140px));
}

.ldn-shell-primary__list > .ldn-nav-mega-6-col > :is(.sub-menu, .ldn-nav-sub) {
	left: auto;
	right: 1rem;
	max-width: min(calc(100% - 2rem), calc(100vw - 2rem), var(--ldn-nav-panel-max, 1168px));
}

/* Column counts match the config vocabulary (2-6). */
.ldn-nav-mega.ldn-nav-mega-2-col > :is(.sub-menu, .ldn-nav-sub) { --ldn-nav-cols: 2; }
.ldn-nav-mega.ldn-nav-mega-3-col > :is(.sub-menu, .ldn-nav-sub) { --ldn-nav-cols: 3; }
.ldn-nav-mega.ldn-nav-mega-4-col > :is(.sub-menu, .ldn-nav-sub) { --ldn-nav-cols: 4; }
.ldn-nav-mega.ldn-nav-mega-5-col > :is(.sub-menu, .ldn-nav-sub) { --ldn-nav-cols: 5; }
.ldn-nav-mega.ldn-nav-mega-6-col > :is(.sub-menu, .ldn-nav-sub) { --ldn-nav-cols: 6; }

/*
 * Column headings are menu items too (CP125_02 emits them), so they need to break
 * the grid flow and start a new column rather than sitting in one cell.
 *
 * Weight, size and the hairline live on the heading's own `> a` (theme) or
 * `> .ldn-nav-column-title` (LDN shell), never on the `<li>`: a font-weight on
 * the list item cascades into every nested option.
 */
.ldn-nav-mega > :is(.sub-menu, .ldn-nav-sub) > .ldn-nav-column-heading {
	grid-column: auto;
	grid-row: 1;
	text-transform: none;
	padding-bottom: 0.125rem;
}

.ldn-nav-mega > :is(.sub-menu, .ldn-nav-sub) > .ldn-nav-column-heading > :is(a, .ldn-nav-column-title) {
	display: block;
	font-weight: 600;
	font-size: 0.75rem;
	text-transform: uppercase;
	letter-spacing: 0.06em;
	line-height: 1.3;
	color: var(--ldn-secondary-text, inherit);
	padding: 0 0.5rem 0.5rem;
	margin-bottom: 0.35rem;
	border-bottom: 1px solid var(--ldn-shell-rule, rgb(0 0 0 / 12%));
}

.ldn-nav-mega > :is(.sub-menu, .ldn-nav-sub) > .ldn-nav-column-heading > :is(.sub-menu, .ldn-nav-sub) a {
	font-weight: 400;
	/*
	 * Explicit size, never inherit: clickable rows are 14px (0.875rem) so
	 * destinations read clearly beneath the 12px uppercase eyebrow heading.
	 */
	font-size: 0.875rem;
	font-variant-numeric: tabular-nums;
	letter-spacing: normal;
	line-height: 1.35;
	padding: 0.35rem 0.5rem;
	margin: 0;
	border-bottom: 0;
	border-radius: 6px;
	width: max-content;
	max-width: 100%;
}

.ldn-nav-mega > :is(.sub-menu, .ldn-nav-sub) li {
	min-width: 0;
	list-style: none;
}

.ldn-nav-mega > :is(.sub-menu, .ldn-nav-sub) a {
	display: block;
	padding: 0.35rem 0.5rem;
	color: inherit;
	text-decoration: none;
	/*
	 * Normal wrapping. Never overflow-wrap: anywhere in a narrow column - it
	 * produces one character per line.
	 */
	overflow-wrap: break-word;
	hyphens: none;
}

.ldn-nav-mega > :is(.sub-menu, .ldn-nav-sub) a:hover,
.ldn-nav-mega > :is(.sub-menu, .ldn-nav-sub) a:focus-visible {
	color: var(--ldn-primary, inherit);
	background: rgb(0 0 0 / 4%);
	text-decoration: none;
}

.ldn-nav-mega > :is(.sub-menu, .ldn-nav-sub) a[aria-current="page"] {
	color: var(--ldn-primary, inherit);
	font-weight: 600;
	background: rgb(0 0 0 / 4%);
}

/* The panel is hidden until the parent is hovered or focus is inside it. */
.ldn-nav-mega > :is(.sub-menu, .ldn-nav-sub) {
	visibility: hidden;
	opacity: 0;
	pointer-events: none;
}

.ldn-nav-mega:hover > :is(.sub-menu, .ldn-nav-sub),
.ldn-nav-mega:focus-within > :is(.sub-menu, .ldn-nav-sub) {
	display: grid;
	visibility: visible;
	opacity: 1;
	pointer-events: auto;
}

/*
 * Column lists live in a nested .sub-menu under each heading. Superfish and
 * GeneratePress hide every .sub-menu until hover, which is right for a flyout
 * and wrong inside a mega panel. The Customizer rule this replaces was
 * `.mega-menu ul .sub-menu { position: static; display: block; height: auto;
 * pointer-events: auto; }`. Dropping the last two from the port is what left the
 * panel showing headings only, with a column's links appearing when that heading
 * was rolled over - the theme's flyout behaviour surviving inside the panel.
 *
 * `visibility: inherit`, not `visible`: a hardcoded `visible` here out-lives the
 * closed panel's `visibility: hidden`, leaving the link text in the a11y tree
 * (and one stacking-context change away from being on screen) with the menu shut.
 */
.ldn-nav-mega > :is(.sub-menu, .ldn-nav-sub) > .ldn-nav-column-heading > :is(.sub-menu, .ldn-nav-sub) {
	display: block;
	position: static;
	height: auto;
	overflow: visible;
	visibility: inherit;
	pointer-events: inherit;
	opacity: 1;
	width: auto;
	max-width: none;
	left: auto;
	top: auto;
	float: none;
	box-shadow: none;
	background: transparent;
	padding: 0;
	margin: 0;
	transform: none;
}

.ldn-nav-column-heading > .dropdown-menu-toggle,
.ldn-nav-column-heading > a .dropdown-menu-toggle {
	display: none;
}

/*
 * Rebalance lopsided 2- and 3-column mega panels: columns with 8+ items
 * (Prices by Carat, Sizes by Shape, Retailer Reviews) flow into two shrinkable
 * subcolumns so a 10-row list sits at 5 rows beside shorter sibling columns.
 * Six-column panels (Learn) stay single-subcolumn so they fit the header width.
 */
:is(.ldn-nav-mega-2-col, .ldn-nav-mega-3-col) > :is(.sub-menu, .ldn-nav-sub) > .ldn-nav-column-heading.ldn-nav-column--split > :is(.sub-menu, .ldn-nav-sub) {
	display: grid;
	grid-template-columns: repeat(2, minmax(0, max-content));
	gap: 0.05rem 1.25rem;
}

/* =========================================================================
   Focus visibility

   Keyboard users must see where they are on the parent AND on every child. The
   outline uses currentColor so it stays visible whatever the theme's palette.
   ========================================================================= */

.ldn-nav-item > a:focus-visible,
.ldn-nav-mega > :is(.sub-menu, .ldn-nav-sub) a:focus-visible {
	outline: 2px solid currentColor;
	outline-offset: 2px;
}

/* =========================================================================
   Narrow layout

   Required in this same change by .cursor/rules/mobile-execution.mdc: a ~50 item
   desktop panel has to have a defined phone layout, not a follow-up ticket.

   Two triggers, because GP Premium's slide-out is rendered server-side with no
   viewport: the container id when the slide-out is what is rendering, and a media
   query for the mobile header. The desktop rules above are absolute-positioned
   grids, which are wrong in a vertical drawer, so both triggers undo them.
   ========================================================================= */

#generate-slideout-menu .ldn-nav-mega > :is(.sub-menu, .ldn-nav-sub),
#mobile-header .ldn-nav-mega > :is(.sub-menu, .ldn-nav-sub) {
	position: static;
	width: auto;
	max-width: none;
	grid-template-columns: minmax(0, 1fr);
	gap: 0;
	padding: 0 0 0 1rem;
	box-shadow: none;
	background: transparent;

	/*
	 * Collapsed by default. The desktop rule this replaces set
	 * `position: static; display: block` unconditionally, which is correct in a
	 * hover dropdown and is what dumps every child inline in a drawer.
	 * The toggle that flips this is GP Premium's; CP125_04 supplies the
	 * shallower item set so there is less to reveal.
	 */
	visibility: visible;
	opacity: 1;
	display: none;
}

/*
 * `.toggled-on` is GeneratePress WITHOUT Premium: the free theme's accordion puts
 * that class on the .sub-menu itself rather than a state class on the parent, and
 * Premium (which owns the slide-out and the mobile header) is not installed on
 * every site in the network. Without it the drawer's chevron opened nothing.
 * `pointer-events` has to come back too, because the closed desktop state above
 * suppresses it and a drawer panel is revealed by display, not by hover.
 */
#generate-slideout-menu .ldn-nav-mega.ldn-nav-open > .sub-menu,
#generate-slideout-menu .ldn-nav-mega[aria-expanded="true"] > .sub-menu,
#generate-slideout-menu .ldn-nav-mega > .sub-menu.toggled-on,
#mobile-header .ldn-nav-mega.ldn-nav-open > .sub-menu,
#mobile-header .ldn-nav-mega > .sub-menu.toggled-on {
	display: grid;
	pointer-events: auto;
}

/* Tap targets: 44px minimum, per the mobile rule and WCAG target size. */
#generate-slideout-menu .ldn-nav-item > a,
#generate-slideout-menu .ldn-nav-mega > .sub-menu a,
#mobile-header .ldn-nav-item > a {
	min-height: 44px;
	display: flex;
	align-items: center;
	padding-block: 0.5rem;
}

#generate-slideout-menu .ldn-nav-column--split > :is(.sub-menu, .ldn-nav-sub),
#mobile-header .ldn-nav-column--split > :is(.sub-menu, .ldn-nav-sub) {
	display: block;
}

@media (max-width: 992px) {
	:is(.ldn-nav-mega-2-col, .ldn-nav-mega-3-col) > :is(.sub-menu, .ldn-nav-sub) > .ldn-nav-column-heading.ldn-nav-column--split > :is(.sub-menu, .ldn-nav-sub) {
		display: block;
	}
}

@media (max-width: 768px) {
	/*
	 * Same undo for a theme or shell drawer at phone width.
	 */
	.ldn-nav-mega > :is(.sub-menu, .ldn-nav-sub) {
		position: static;
		width: auto;
		max-width: none;
		grid-template-columns: minmax(0, 1fr);
		padding: 0 0 0.5rem 0.875rem;
		border-top: 0;
		border-radius: 0;
		box-shadow: none;
		background: transparent;

		/*
		 * `display: none`, not just the inherited `visibility: hidden`. Once the
		 * panel is `static` it is in normal flow, and a merely invisible element
		 * still reserves its box - which held ~500px of blank page open below the
		 * menu at 360px. Hover is also not a thing here, so nothing would ever
		 * reveal it.
		 */
		display: none;
	}

	.ldn-nav-mega.ldn-nav-open > :is(.sub-menu, .ldn-nav-sub),
	.ldn-nav-mega[aria-expanded="true"] > :is(.sub-menu, .ldn-nav-sub),
	.ldn-nav-mega > .sub-menu.toggled-on {
		display: grid;
		visibility: visible;
		opacity: 1;
		pointer-events: auto;
	}

	:is(.ldn-nav-mega-2-col, .ldn-nav-mega-3-col) > :is(.sub-menu, .ldn-nav-sub) > .ldn-nav-column-heading.ldn-nav-column--split > :is(.sub-menu, .ldn-nav-sub) {
		display: block;
	}

	.ldn-nav-mega > :is(.sub-menu, .ldn-nav-sub) > .ldn-nav-column-heading {
		grid-row: auto;
		padding-top: 0.15rem;
	}

	/* Collapsed narrow hubs are tappable links in the drawer, not muted eyebrows. */
	.ldn-nav-mega > :is(.sub-menu, .ldn-nav-sub) > .ldn-nav-column-heading.ldn-nav-narrow-hub > a {
		display: flex;
		align-items: center;
		min-height: 44px;
		padding: 0 0.5rem;
		margin: 0;
		border-bottom: 0;
		font-size: 0.875rem;
		font-weight: 400;
		text-transform: none;
		letter-spacing: normal;
		color: inherit;
	}

	.ldn-nav-item > a {
		min-height: 44px;
		display: flex;
		align-items: center;
	}
}

/*
 * Narrowest supported width. The grid is already single-column by here; this only
 * tightens the gutters so a long localised label (German and French run longer
 * than English) still has a usable measure rather than wrapping every word.
 */
@media (max-width: 400px) {
	#generate-slideout-menu .ldn-nav-mega > .sub-menu,
	.ldn-nav-mega > :is(.sub-menu, .ldn-nav-sub) {
		padding-left: 0.75rem;
	}
}

/* Utility items appended to the slide-out on a replace market. */
#generate-slideout-menu .ldn-nav-utility-start,
#mobile-header .ldn-nav-utility-start {
	margin-top: 0.75rem;
	padding-top: 0.75rem;
	border-top: 1px solid;
}

/*
 * Footer. The live Ringspo footer is widgets, so this strip is painted by the
 * plugin rather than the theme walker. Desktop is a wrapping row; a phone is
 * one stacked column with the same 44px targets as the slide-out.
 */
.ldn-nav-footer-wrap {
	width: 100%;
}

.ldn-nav-footer,
.ldn-nav-footer ul {
	display: flex;
	flex-wrap: wrap;
	gap: 0.25rem 1.25rem;
	margin: 0;
	padding: 0.5rem 0;
	list-style: none;
}

.ldn-nav-footer li {
	min-width: 0;
}

.ldn-nav-footer a {
	display: flex;
	align-items: center;
	min-height: 44px;
	text-decoration: none;
	color: inherit;
}

.ldn-nav-footer a:hover {
	text-decoration: underline;
	text-underline-offset: 0.18em;
}

.ldn-nav-footer a:focus-visible {
	outline: 2px solid;
	outline-offset: 2px;
}

@media (max-width: 768px) {
	.ldn-nav-footer,
	.ldn-nav-footer ul {
		flex-direction: column;
		align-items: stretch;
		gap: 0;
	}

	.ldn-nav-footer a {
		min-height: 44px;
	}
}

/* Respect a reduced-motion preference: skip the panel fade and link colour shift. */
@media (prefers-reduced-motion: no-preference) {
	.ldn-nav-mega > :is(.sub-menu, .ldn-nav-sub) {
		transition: opacity 120ms ease-out;
	}

	.ldn-nav-mega > :is(.sub-menu, .ldn-nav-sub) a,
	.ldn-nav-footer a {
		transition: color 140ms ease, background-color 140ms ease, text-decoration-color 140ms ease;
	}
}
