/*
 * Sortable game board, shared by every game: tokens, buttons, the task card, the item list, the
 * result banner and the start countdown. Written once against the neutral classes
 * gaming-platform-sortable__*: a game puts them next to its own classes in its templates, and the
 * shared scripts add them to every element they create. Builds on the platform component kit
 * (gaming-platform-ui) and falls back to the theme colours without it. States use text and icons,
 * never colour alone. What differs between games or modes stays in the game's own style sheets.
 */

.gaming-platform-sortable {
	--gp-sortable-text: var(--gp-text, var(--wp--preset--color--contrast, CanvasText));
	--gp-sortable-canvas: var(--gp-bg, var(--wp--preset--color--base, Canvas));
	--gp-sortable-muted: var(--gp-text-muted, color-mix(in srgb, var(--gp-sortable-text) 72%, var(--gp-sortable-canvas)));
	--gp-sortable-surface: var(--gp-surface-1, color-mix(in srgb, var(--gp-sortable-text) 5%, var(--gp-sortable-canvas)));
	--gp-sortable-surface-raised: var(--gp-surface-2, color-mix(in srgb, var(--gp-sortable-text) 9%, var(--gp-sortable-canvas)));
	--gp-sortable-border: var(--gp-border-strong, color-mix(in srgb, var(--gp-sortable-text) 22%, var(--gp-sortable-canvas)));
	--gp-sortable-border-strong: var(--gp-border-control, color-mix(in srgb, var(--gp-sortable-text) 40%, var(--gp-sortable-canvas)));
	--gp-sortable-accent: var(--gp-accent, var(--gp-sortable-text));
	--gp-sortable-accent-hover: var(--gp-accent-hover, color-mix(in srgb, var(--gp-sortable-text) 82%, var(--gp-sortable-canvas)));
	--gp-sortable-accent-text: var(--gp-accent-text, var(--gp-sortable-text));
	--gp-sortable-on-accent: var(--gp-on-accent, var(--gp-sortable-canvas));
	--gp-sortable-success: var(--gp-success, currentColor);
	--gp-sortable-danger: var(--gp-danger, currentColor);
	--gp-sortable-warning: var(--gp-warning, currentColor);
	--gp-sortable-focus: var(--gp-focus, 3px solid var(--gp-sortable-text));
	--gp-sortable-radius: var(--gp-radius-md, 10px);
	--gp-sortable-radius-lg: var(--gp-radius-lg, 16px);
	color: var(--gp-sortable-text);
}

/* Layout rules below set display, so the hidden attribute must win explicitly. */
.gaming-platform-sortable [hidden] {
	display: none !important;
}

@keyframes gaming-platform-sortable-spin {
	to {
		transform: rotate(360deg);
	}
}

/* Buttons: primary by default, touch targets of at least 44 × 44 CSS pixels. */
.gaming-platform-sortable__button {
	display: inline-flex;
	align-items: center;
	justify-content: center;
	gap: 0.5em;
	box-sizing: border-box;
	min-width: 44px;
	min-height: 48px;
	padding: 0.625rem 1.5rem;
	border: 1px solid transparent;
	border-radius: var(--gp-radius-pill, 4px);
	background: var(--gp-sortable-accent);
	box-shadow: var(--gp-edge, none);
	color: var(--gp-sortable-on-accent);
	font: inherit;
	font-family: var(--gp-font-display, inherit);
	font-size: 1.1875rem;
	font-style: italic;
	font-weight: 700;
	letter-spacing: 0.03em;
	line-height: 1.2;
	text-transform: uppercase;
	cursor: pointer;
	transition: background-color 0.15s ease, box-shadow 0.15s ease, border-color 0.15s ease;
}

.gaming-platform-sortable__button:hover {
	background: var(--gp-sortable-accent-hover);
	box-shadow: var(--gp-edge, none), var(--gp-glow, none);
}

.gaming-platform-sortable__button[aria-disabled="true"] {
	box-shadow: none;
	cursor: progress;
	opacity: 0.7;
}

/* The button that started a request shows a spinner next to its label. */
.gaming-platform-sortable__button[aria-busy="true"]::after {
	content: "";
	width: 1em;
	height: 1em;
	box-sizing: border-box;
	border: 2px solid currentColor;
	border-right-color: transparent;
	border-radius: 50%;
	animation: gaming-platform-sortable-spin 0.8s linear infinite;
}

/* The current task: instruction and expiry in one card. */
.gaming-platform-sortable__task {
	margin-block: 0 1rem;
	padding: 1.25rem 1.5rem;
	border: 1px solid color-mix(in srgb, var(--gp-sortable-accent) 55%, var(--gp-sortable-canvas));
	border-radius: var(--gp-sortable-radius-lg);
	background: linear-gradient(140deg, color-mix(in srgb, var(--gp-sortable-accent) 20%, var(--gp-sortable-canvas)), var(--gp-sortable-surface) 70%);
	box-shadow: var(--gp-edge, none);
}

.gaming-platform-sortable__task:not(:has(.gaming-platform-sortable__instruction:not([hidden]))) {
	display: none;
}

.gaming-platform-sortable__instruction {
	margin: 0;
	font-family: var(--gp-font-display, inherit);
	font-size: clamp(1.25rem, 3.5vw, 1.625rem);
	font-style: italic;
	font-weight: 700;
	line-height: 1.2;
	text-wrap: balance;
}

.gaming-platform-sortable__expiry {
	margin: 0.5em 0 0;
	color: var(--gp-sortable-muted);
	font-size: 0.9375rem;
	font-variant-numeric: tabular-nums;
}

.gaming-platform-sortable__expiry::before {
	content: "⏱ ";
	content: "⏱ " / "";
}

/* The list: one card per item with its current position. */
.gaming-platform-sortable__list {
	display: flex;
	flex-direction: column;
	gap: 0.5rem;
	margin: 0 0 1.25rem;
	padding: 0;
	list-style: none;
	counter-reset: gaming-platform-sortable-position;
	container-type: inline-size;
}

.gaming-platform-sortable__item {
	position: relative;
	display: flex;
	align-items: center;
	gap: 0.75rem;
	min-height: 56px;
	padding: 0.375rem 0.5rem 0.375rem 0.75rem;
	border: 1px solid var(--gp-sortable-border);
	border-radius: var(--gp-sortable-radius);
	background: var(--gp-sortable-surface);
	box-shadow: var(--gp-edge, none);
	counter-increment: gaming-platform-sortable-position;
	transition: transform 0.15s ease, box-shadow 0.15s ease, border-color 0.15s ease, background-color 0.15s ease;
}

.gaming-platform-sortable__item::before {
	content: counter(gaming-platform-sortable-position);
	content: counter(gaming-platform-sortable-position) / "";
	display: grid;
	flex: none;
	/* The position on a number plate. */
	width: 2.1em;
	height: 1.7em;
	place-items: center;
	border: 2px solid var(--gp-sortable-canvas);
	border-radius: var(--gp-sortable-radius-lg);
	outline: 1px solid var(--gp-sortable-border-strong);
	background: var(--gp-sortable-text);
	color: var(--gp-sortable-canvas);
	font-family: var(--gp-font-display, inherit);
	font-size: 1.125em;
	font-style: italic;
	font-weight: 700;
	font-variant-numeric: tabular-nums;
	line-height: 1;
}

.gaming-platform-sortable__item.is-movable {
	cursor: grab;
}

.gaming-platform-sortable__item.is-movable:hover {
	border-color: var(--gp-sortable-border-strong);
	background: var(--gp-sortable-surface-raised);
}

.gaming-platform-sortable__item.is-dragging {
	z-index: 1;
	border-color: var(--gp-sortable-accent-text);
	background: var(--gp-sortable-surface-raised);
	box-shadow: 0 16px 36px -12px rgb(0 0 0 / 60%), 0 0 0 1px var(--gp-sortable-accent-text), var(--gp-glow, none);
	cursor: grabbing;
	transition: box-shadow 0.15s ease;
}

.gaming-platform-sortable__handle {
	display: grid;
	flex: none;
	width: 32px;
	height: 44px;
	place-items: center;
	margin-block: -0.375rem;
	color: var(--gp-sortable-muted);
	font-size: 1.25em;
	line-height: 1;
	touch-action: none;
}

.gaming-platform-sortable__item:not(.is-movable) .gaming-platform-sortable__handle {
	display: none;
}

.gaming-platform-sortable__name {
	flex: 1 1 auto;
	min-width: 0;
	overflow-wrap: anywhere;
	font-size: 1.0625rem;
	font-weight: 600;
}

.gaming-platform-sortable__move {
	display: inline-flex;
	flex: none;
	align-items: center;
	justify-content: center;
	gap: 0.35em;
	min-width: 44px;
	min-height: 44px;
	padding: 0 0.75em;
	border: 1px solid var(--gp-sortable-border);
	border-radius: var(--gp-sortable-radius);
	background: transparent;
	color: inherit;
	font: inherit;
	font-size: 0.875em;
	cursor: pointer;
	transition: border-color 0.15s ease, background-color 0.15s ease;
}

.gaming-platform-sortable__move:hover:not(:disabled) {
	border-color: var(--gp-sortable-text);
	background: var(--gp-sortable-surface-raised);
}

.gaming-platform-sortable__move:disabled {
	cursor: not-allowed;
	opacity: 0.35;
}

.gaming-platform-sortable__move-icon {
	font-size: 1.15em;
	line-height: 1;
}

/* Narrow lists show arrows only; the accessible name ("Move <item> up") is unchanged. */
@container (max-width: 30rem) {
	.gaming-platform-sortable__move {
		padding: 0;
	}

	.gaming-platform-sortable__move-text {
		position: absolute;
		width: 1px;
		height: 1px;
		overflow: hidden;
		clip: rect(0, 0, 0, 0);
		white-space: nowrap;
	}
}

.gaming-platform-sortable__actions {
	margin-block: 1rem;
}

/* Result and error banners carry a text marker (✓ ✗ ⚠ …); colour only reinforces it. */
.gaming-platform-sortable__result {
	--gp-sortable-state: var(--gp-sortable-danger);
	display: flex;
	flex-wrap: wrap;
	align-items: center;
	justify-content: space-between;
	gap: 0.75rem 1rem;
	margin-block: 1rem;
	padding: 1.25rem 1.5rem;
	/* A pit board: black, with a hard frame in the colour of its state. */
	border: 2px solid var(--gp-sortable-state);
	border-left-width: 8px;
	border-radius: var(--gp-sortable-radius-lg);
	background: var(--gp-board, var(--gp-sortable-canvas));
	animation: gaming-platform-sortable-reveal 0.25s ease-out;
}

@keyframes gaming-platform-sortable-reveal {
	from {
		opacity: 0;
		transform: translateY(6px);
	}
}

.gaming-platform-sortable__message {
	flex: 1 1 16rem;
	margin: 0;
	font-family: var(--gp-font-display, inherit);
	font-size: 1.1875rem;
	font-weight: 700;
	line-height: 1.3;
}

/* The time of a finished game as a lap time, and the running clock above the list. */
.gaming-platform-sortable__time,
.gaming-platform-sortable__timer {
	margin: 0;
	font-family: var(--gp-font-mono, monospace);
	font-variant-numeric: tabular-nums;
	font-weight: 700;
	line-height: 1;
}

.gaming-platform-sortable__time {
	flex: 1 1 100%;
	color: var(--gp-sortable-state);
	font-size: clamp(3rem, 14vw, 5.5rem);
	letter-spacing: -0.03em;
}

.gaming-platform-sortable__timer {
	margin-top: 0.5em;
	font-size: 1.75rem;
}

.gaming-platform-sortable__alert {
	margin: 0;
}

.gaming-platform-sortable__alert:not(:empty) {
	margin-block: 1rem;
	padding: 0.875rem 1rem;
	border: 1px solid color-mix(in srgb, var(--gp-sortable-danger) 45%, var(--gp-sortable-canvas));
	border-radius: var(--gp-sortable-radius);
	background: color-mix(in srgb, var(--gp-sortable-danger) 10%, var(--gp-sortable-canvas));
	font-weight: 600;
}

.gaming-platform-sortable__help {
	margin-top: 1.5rem;
	padding: 1rem 1.25rem;
	border: 1px dashed var(--gp-sortable-border-strong);
	border-radius: var(--gp-sortable-radius-lg);
	color: var(--gp-sortable-muted);
	font-size: 0.9375rem;
}

.gaming-platform-sortable__help > summary {
	display: flex;
	align-items: center;
	min-height: 44px;
	margin-block: -0.625rem;
	cursor: pointer;
}

.gaming-platform-sortable__help[open] > summary {
	margin-bottom: 0;
}

/* The marker is text, so the state is not carried by colour or shape alone for assistive technology either: <details> exposes it. */
.gaming-platform-sortable__help > summary::before {
	content: "▸";
	margin-right: 0.5em;
	color: var(--gp-sortable-text);
}

.gaming-platform-sortable__help[open] > summary::before {
	content: "▾";
}

.gaming-platform-sortable__help > summary::-webkit-details-marker {
	display: none;
}

.gaming-platform-sortable__help > summary:focus-visible {
	outline: var(--gp-sortable-focus);
	outline-offset: 2px;
}

.gaming-platform-sortable__help > summary h3 {
	margin: 0;
	color: var(--gp-sortable-text);
	font-family: inherit;
	font-size: 0.8125rem;
	font-weight: 600;
	letter-spacing: 0.06em;
	text-transform: uppercase;
}

.gaming-platform-sortable__help ul {
	margin: 0;
	padding-left: 1.25em;
}

.gaming-platform-sortable__notice {
	padding: 0.875rem 1rem;
	border: 1px solid color-mix(in srgb, var(--gp-sortable-warning) 45%, var(--gp-sortable-canvas));
	border-radius: var(--gp-sortable-radius);
	background: color-mix(in srgb, var(--gp-sortable-warning) 10%, var(--gp-sortable-canvas));
}

/* Visible keyboard focus: the same ring as on every platform screen. */
.gaming-platform-sortable__move:focus-visible,
.gaming-platform-sortable__button:focus-visible,
.gaming-platform-sortable__message:focus-visible {
	outline: var(--gp-sortable-focus);
	outline-offset: 2px;
}

/* The instruction receives focus when a game starts; the ring frames the whole task card. */
.gaming-platform-sortable__instruction:focus {
	outline: none;
}

.gaming-platform-sortable__task:has(.gaming-platform-sortable__instruction:focus-visible) {
	outline: var(--gp-sortable-focus);
	outline-offset: 2px;
}

/* Live regions are for assistive technology only; the result banner and alert show the same text. */
.gaming-platform-sortable__status,
.gaming-platform-sortable__announcer,
.gaming-platform-sortable__result:not([hidden]) ~ .gaming-platform-sortable__alert {
	position: absolute;
	width: 1px;
	height: 1px;
	margin: -1px;
	padding: 0;
	overflow: hidden;
	clip: rect(0, 0, 0, 0);
	white-space: nowrap;
	border: 0;
}

@media (prefers-reduced-motion: reduce) {
	.gaming-platform-sortable__item,
	.gaming-platform-sortable__button,
	.gaming-platform-sortable__move {
		transition: none;
	}

	.gaming-platform-sortable__result {
		animation: none;
	}

	.gaming-platform-sortable__button[aria-busy="true"]::after {
		border-style: dotted;
		animation: none;
	}
}

/*
 * The countdown before a game starts (sortable-game-countdown.js). It is appended to the body, so it
 * reads the platform tokens directly. Each beat cuts a number in hard behind the start gate; on
 * "Go" the gate drops and dirt flies.
 */

.gaming-platform-countdown {
	--gaming-platform-countdown-color: var(--gp-text-muted);
	position: fixed;
	inset: 0;
	z-index: 100000;
	overflow: hidden;
	/* Near-black with a faint tyre tread. */
	background: linear-gradient(rgb(0 0 0 / 86%), rgb(0 0 0 / 86%)), var(--gp-tread), rgb(0 0 0);
	color: var(--gp-text);
}

.gaming-platform-countdown[data-step="2"] {
	--gaming-platform-countdown-color: var(--gp-highlight);
}

.gaming-platform-countdown[data-step="1"] {
	--gaming-platform-countdown-color: var(--gp-accent);
}

.gaming-platform-countdown[data-step="go"] {
	--gaming-platform-countdown-color: var(--gp-success);
}

.gaming-platform-countdown.is-go {
	pointer-events: none;
	animation: gaming-platform-countdown-out 0.5s 0.15s ease-in both;
}

.gaming-platform-countdown__beat {
	position: absolute;
	inset: 0;
	display: grid;
	place-items: center;
	animation: gaming-platform-countdown-shake 0.3s linear both;
}

.gaming-platform-countdown__number {
	font-family: var(--gp-font-display, inherit);
	font-size: clamp(11rem, 58vmin, 32rem);
	font-style: italic;
	font-weight: 700;
	font-variant-numeric: tabular-nums;
	line-height: 0.8;
	text-shadow: 0.05em 0.05em 0 var(--gaming-platform-countdown-color);
	text-transform: uppercase;
	animation: gaming-platform-countdown-slam 1s both;
}

.is-go .gaming-platform-countdown__number {
	font-size: clamp(7rem, 34vmin, 20rem);
	animation: gaming-platform-countdown-launch 0.5s ease-in both;
}

/* Speed lines that rip across the screen with every beat. */
.gaming-platform-countdown__speed {
	position: absolute;
	inset: 0;
	background: repeating-linear-gradient(0deg, transparent 0 9vmin, var(--gaming-platform-countdown-color) 9vmin 9.6vmin);
	opacity: 0;
	animation: gaming-platform-countdown-speed 0.4s ease-out both;
}

/* The start gate: a striped bar across the track that falls on "Go". */
.gaming-platform-countdown__gate {
	position: absolute;
	right: 0;
	bottom: 16vmin;
	left: 0;
	height: 5vmin;
	border-block: 0.6vmin solid var(--gp-text);
	background: repeating-linear-gradient(115deg, var(--gp-highlight) 0 6vmin, rgb(0 0 0) 6vmin 12vmin);
}

.is-go .gaming-platform-countdown__gate {
	animation: gaming-platform-countdown-drop 0.22s cubic-bezier(0.6, 0, 1, 0.4) both;
}

/* Dirt thrown up from the gate; each fleck gets its direction from the script. */
.gaming-platform-countdown__dirt {
	position: absolute;
	bottom: 16vmin;
	left: var(--x, 50%);
	width: var(--s, 2vmin);
	height: var(--s, 2vmin);
	border-radius: 40% 60% 55% 45%;
	background: color-mix(in srgb, var(--gp-accent) 32%, var(--gp-bg));
	animation: gaming-platform-countdown-dirt 0.6s ease-out both;
}

@keyframes gaming-platform-countdown-out {
	to {
		opacity: 0;
	}
}

@keyframes gaming-platform-countdown-slam {
	0% {
		opacity: 0;
		transform: scale(2.6) skewX(-14deg);
	}

	9% {
		opacity: 1;
		transform: scale(0.94) skewX(4deg);
	}

	16%,
	82% {
		opacity: 1;
		transform: none;
	}

	100% {
		opacity: 0;
		transform: translateX(70vw) skewX(-24deg);
	}
}

@keyframes gaming-platform-countdown-launch {
	0% {
		transform: scale(0.7);
	}

	100% {
		opacity: 0;
		transform: scale(4.5);
	}
}

@keyframes gaming-platform-countdown-speed {
	0% {
		opacity: 0.5;
		transform: translateX(-60%);
	}

	100% {
		opacity: 0;
		transform: translateX(60%);
	}
}

@keyframes gaming-platform-countdown-drop {
	60% {
		transform: translateY(16vmin) scaleY(0.5);
	}

	80% {
		transform: translateY(13vmin) scaleY(0.6);
	}

	100% {
		transform: translateY(16vmin) scaleY(0.3);
	}
}

@keyframes gaming-platform-countdown-dirt {
	0% {
		transform: none;
	}

	100% {
		opacity: 0;
		transform: translate(var(--dx, 0), var(--dy, -40vmin)) rotate(200deg);
	}
}

@keyframes gaming-platform-countdown-shake {
	0%,
	100% {
		transform: none;
	}

	25% {
		transform: translate(-1.4vmin, 0.9vmin);
	}

	50% {
		transform: translate(1.2vmin, -1vmin);
	}

	75% {
		transform: translate(-0.6vmin, 0.4vmin);
	}
}

/* Reduced motion: the numbers still count behind the gate, without slam, shake, lines or dirt. */
@media (prefers-reduced-motion: reduce) {
	.gaming-platform-countdown.is-go,
	.gaming-platform-countdown__beat,
	.gaming-platform-countdown__number,
	.is-go .gaming-platform-countdown__number,
	.is-go .gaming-platform-countdown__gate {
		animation: none;
	}

	.gaming-platform-countdown__speed,
	.gaming-platform-countdown__dirt {
		display: none;
	}
}
