/**
 * Front-end language switcher.
 *
 * The governing rule: this file may lay things out, but it may not decide what
 * they look like. No font, no colour, no weight is stated outright — untouched,
 * the switcher is entirely `inherit`, `currentColor` and `transparent`, so it
 * takes on whatever the theme has already set at that point in the page. On a
 * dark header it comes out light; dropped into a block that already has a
 * colour, it comes out that colour.
 *
 * Every fixed value comes from the settings screen as a custom property — the
 * five colours, the corner radius, the padding and the corner offsets — so a
 * field left empty falls back to inheritance rather than overriding it with
 * nothing.
 *
 * Every selector is prefixed and single-class, so a theme can override any of
 * it without an arms race over specificity.
 */

.rt-switcher {
	--rt-sw-gap: 0.5em;
	--rt-sw-pad-y: 0.35em;
	--rt-sw-pad-x: 0.7em;
	--rt-sw-radius: 4px;

	display: inline-block;
	max-width: 100%;
	font: inherit;
	color: inherit;
	line-height: inherit;
}

.rt-switcher *,
.rt-switcher *::before,
.rt-switcher *::after {
	box-sizing: border-box;
}

/* ------------------------------------------------------------------ lists */

.rt-switcher__list,
.rt-switcher__panel {
	display: flex;
	flex-wrap: wrap;
	align-items: center;
	gap: var(--rt-sw-gap);
	margin: 0;
	padding: 0;
	list-style: none;
}

.rt-switcher__item::marker {
	content: none;
}

/* ------------------------------------------------------- links and toggle */

.rt-switcher__link,
.rt-switcher__toggle {
	display: inline-flex;
	align-items: center;
	gap: 0.4em;
	margin: 0;
	border-radius: var(--rt-sw-radius);
	font: inherit;
	line-height: inherit;
	text-decoration: inherit;
	cursor: pointer;
}

.rt-switcher__toggle {
	-webkit-appearance: none;
	appearance: none;
}

.rt-switcher__link:focus-visible,
.rt-switcher__toggle:focus-visible {
	outline: 2px solid currentColor;
	outline-offset: 2px;
}

.rt-switcher__flag {
	font-size: 1.1em;
	line-height: 1;
}

.rt-switcher__caret {
	width: 0;
	height: 0;
	margin-inline-start: 0.15em;
	border-inline: 0.3em solid transparent;
	border-top: 0.35em solid currentColor;
	transition: transform 0.15s ease;
}

[aria-expanded="true"] > .rt-switcher__caret,
[aria-expanded="true"] .rt-switcher__caret {
	transform: rotate(180deg);
}

/*
 * Visually hidden but readable by assistive technology. Themes usually ship
 * their own screen-reader class, but the switcher cannot count on being placed
 * somewhere that class exists.
 */
.rt-switcher__sr {
	position: absolute !important;
	width: 1px;
	height: 1px;
	margin: -1px;
	padding: 0;
	overflow: hidden;
	clip-path: inset(50%);
	white-space: nowrap;
	border: 0;
}

/* --------------------------------------------------------------- dropdown */

.rt-switcher--dropdown {
	position: relative;
}

.rt-switcher--dropdown .rt-switcher__panel {
	position: absolute;
	z-index: 999;
	top: calc(100% + 0.4em);
	inset-inline-start: 0;
	flex-direction: column;
	align-items: stretch;
	gap: 0;
	min-width: max-content;
	padding: 0.25em;
	border-radius: var(--rt-sw-radius);

	/*
	 * A floating panel is the one place transparency is not an option: it sits
	 * over arbitrary page content. Canvas/CanvasText follow the reader's colour
	 * scheme; the plain values before them are the fallback.
	 */
	background: #fff;
	background: Canvas;
	color: #1a1a1a;
	color: CanvasText;
	box-shadow: 0 2px 8px rgb(0 0 0 / 18%);
}

.rt-switcher__panel[hidden] {
	display: none;
}

.rt-switcher--dropdown .rt-switcher__panel .rt-switcher__link {
	width: 100%;
	padding: var(--rt-sw-pad-y) var(--rt-sw-pad-x);
	/*
	 * Match the panel’s own corners (minus its 0.25em padding) so a hover fill
	 * does not square off inside a rounded shell.
	 */
	border-radius: max( 0px, calc( var(--rt-sw-radius) - 0.25em ) );
}

.rt-switcher--dropdown .rt-switcher__panel .rt-switcher__link:hover,
.rt-switcher--dropdown .rt-switcher__panel .rt-switcher__link:focus {
	background: color-mix(in srgb, currentColor 10%, transparent);
}

/* ------------------------------------------------------------- is-current */

.rt-switcher__item.is-current > .rt-switcher__link {
	font-weight: bolder;
}

/* ------------------------------------------------------------------- look */

/*
 * There is one look, and by default it is no look at all: every value below
 * comes from a custom property the site owner set, and an unset one falls back
 * to inheriting or to nothing. Padding is the exception — the switcher always
 * needs room around its own text — and even that is stated in `em` so it
 * scales with whatever it was dropped next to until a pixel value replaces it.
 */
.rt-switcher__link,
.rt-switcher__toggle {
	padding: var(--rt-sw-pad-y) var(--rt-sw-pad-x);
	border: 1px solid var(--rt-sw-border, transparent);
	background: var(--rt-sw-bg, transparent);
	color: var(--rt-sw-text, inherit);
}

/*
 * The fill goes on the item and the text colour on the link inside it, never
 * both on one element: a currentColor tint is mixed from that element's own
 * colour, so a rule that sets the colour it is meant to sit behind ends up
 * mixing the fill out of it.
 */
.rt-switcher__item.is-current {
	border-radius: var(--rt-sw-radius);
	background: var(--rt-sw-active-bg, var(--rt-sw-bg, transparent));
}

.rt-switcher__item.is-current > .rt-switcher__link {
	background: none;
	color: var(--rt-sw-active-text, var(--rt-sw-text, inherit));
}

.rt-switcher__link:hover {
	filter: brightness(0.95);
}

/* ------------------------------------------------------------- placements */

/*
 * Which corner is configurable because the bottom right is crowded: cookie
 * banners, chat bubbles and back-to-top buttons all default to it, and no
 * amount of z-index settles an argument between two fixed elements sitting on
 * the same 40 pixels.
 *
 * When moving corner is not enough — a cookie bar spanning the whole bottom,
 * a sticky header along the top — the offsets add to the inset, sliding the
 * whole pill along the page away from the edge it is pinned to. They move the
 * control, not its contents: nothing inside it changes size.
 *
 * The bottom offset defaults to 4px so the pill lines up with a cookie manager
 * button rather than sitting a few pixels off it: re:Cookies pins its 48px
 * circle at `bottom: 20px`, and 16px of our own inset plus 4 is that same 20.
 * Two round things at the bottom of the page at two different heights is the
 * kind of detail that reads as an accident, because it is one.
 *
 * In the same corner they would overlap instead of lining up — that is what the
 * offset is a setting for, and what the other three corners are for.
 */
.rt-switcher-floating {
	position: fixed;
	z-index: 9999;
	padding: 0.3em;
	/*
	 * Tracks the configured chip radius plus this shell’s own padding, so the
	 * corner-radius setting reshapes the button and the outer edge together.
	 */
	border-radius: calc( var(--rt-sw-radius) + 0.3em );

	background: #fff;
	background: Canvas;
	color: #1a1a1a;
	color: CanvasText;
	box-shadow: 0 2px 12px rgb(0 0 0 / 18%);
}

.rt-switcher-floating--bottom-right,
.rt-switcher-floating--bottom-left {
	bottom: calc(max(1rem, env(safe-area-inset-bottom)) + var(--rt-sw-offset-y, 4px));
}

.rt-switcher-floating--top-right,
.rt-switcher-floating--top-left {
	top: calc(max(1rem, env(safe-area-inset-top)) + var(--rt-sw-offset-y, 0px));
}

.rt-switcher-floating--bottom-right,
.rt-switcher-floating--top-right {
	right: calc(max(1rem, env(safe-area-inset-right)) + var(--rt-sw-offset-x, 0px));
}

.rt-switcher-floating--bottom-left,
.rt-switcher-floating--top-left {
	left: calc(max(1rem, env(safe-area-inset-left)) + var(--rt-sw-offset-x, 0px));
}

/* Pinned to the bottom, the panel has nowhere to go but up. */
.rt-switcher-floating--bottom-right.rt-switcher--dropdown .rt-switcher__panel,
.rt-switcher-floating--bottom-left.rt-switcher--dropdown .rt-switcher__panel {
	top: auto;
	bottom: calc(100% + 0.4em);
}

/* Pinned to the right, it has to grow leftwards or it runs off the viewport. */
.rt-switcher-floating--bottom-right.rt-switcher--dropdown .rt-switcher__panel,
.rt-switcher-floating--top-right.rt-switcher--dropdown .rt-switcher__panel {
	inset-inline-start: auto;
	inset-inline-end: 0;
}

@media print {
	.rt-switcher-floating {
		display: none;
	}
}

@media (prefers-reduced-motion: reduce) {
	.rt-switcher__caret {
		transition: none;
	}
}
