:root {
	font-family: var(--font-body);
	font-synthesis: none;
}

/* Cross-fade the complete painted palette, including gradients and SVG art. */
::view-transition {
	pointer-events: none;
}

::view-transition-old(root) {
	animation: theme-fade-out var(--theme-transition-duration)
		var(--theme-transition-easing) both;
}

::view-transition-new(root) {
	animation: theme-fade-in var(--theme-transition-duration)
		var(--theme-transition-easing) both;
}

@keyframes theme-fade-out {
	to {
		opacity: 0;
	}
}

@keyframes theme-fade-in {
	from {
		opacity: 0;
	}
}

/* Fall back to color transitions when view transitions are unavailable.
   Enable them after setup so the initial theme appears without animation. */
.theme-ready:not(.has-theme-crossfade)
	:is(
		body,
		.game-panel,
		.game-sidebar,
		.game-summary,
		.game-status,
		select,
		.result-dialog
	) {
	transition:
		color var(--theme-transition-duration) var(--theme-transition-easing),
		background-color var(--theme-transition-duration)
			var(--theme-transition-easing),
		border-color var(--theme-transition-duration)
			var(--theme-transition-easing);
}

.theme-ready:not(.has-theme-crossfade) .button {
	transition:
		color var(--theme-transition-duration) var(--theme-transition-easing),
		background-color var(--theme-transition-duration)
			var(--theme-transition-easing),
		border-color var(--theme-transition-duration)
			var(--theme-transition-easing),
		transform 160ms ease;
}

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

body {
	min-width: min(20rem, 100vw);
	min-height: 100vh;
	margin: 0;
	color: var(--ink);
	background:
		linear-gradient(var(--grid-line) 1px, transparent 1px),
		linear-gradient(90deg, var(--grid-line) 1px, transparent 1px),
		var(--page);
	background-size: 2rem 2rem;
}

button,
select {
	font: inherit;
	border-radius: 0;
}

button:focus-visible,
select:focus-visible,
.board-viewport:focus-visible {
	outline: 3px solid var(--focus);
	outline-offset: 3px;
}

.visually-hidden {
	position: absolute;
	width: 1px;
	height: 1px;
	padding: 0;
	margin: -1px;
	overflow: hidden;
	clip: rect(0 0 0 0);
	white-space: nowrap;
	border: 0;
}

.game-shell {
	width: min(100% - 1rem, 100rem);
	margin-inline: auto;
	padding-block: 0.5rem;
}

h1 {
	margin: 0;
	font-family: var(--font-heading);
	font-size: 1.75rem;
	line-height: 1.1;
	letter-spacing: -0.025em;
}

.game-panel {
	position: relative;
	display: grid;
	/* A zero minimum lets the board column shrink within the available width. */
	grid-template-columns: 15rem minmax(0, 1fr);
	gap: 0.75rem;
	padding: 0.75rem;
	border: 1px solid var(--line);
	background: var(--panel);
	box-shadow: var(--panel-shadow);
}

.game-sidebar {
	min-width: 0;
	padding-right: 0.75rem;
	border-right: 1px solid var(--line);
	background: var(--panel);
}

.drawer-header {
	display: flex;
	align-items: center;
	justify-content: space-between;
	gap: 0.5rem;
	margin-bottom: 1rem;
}

.game-toolbar {
	display: grid;
	gap: 0.75rem;
	margin-bottom: 0.75rem;
}

.layout-control {
	display: grid;
	gap: 0.4rem;
	color: var(--muted);
	font-weight: 700;
}

.icon-definitions {
	position: absolute;
	width: 0;
	height: 0;
	overflow: hidden;
}

.icon {
	flex: 0 0 1.5rem;
	width: 1.5rem;
	height: 1.5rem;
	fill: none;
	stroke: currentColor;
	stroke-width: 1.75;
	stroke-linecap: round;
	stroke-linejoin: round;
}

#layout-toggle,
.theme-sun,
.dark .theme-moon {
	display: none;
}

.dark .theme-sun {
	display: block;
}

.game-panel.is-collapsed {
	grid-template-columns: 3.5rem minmax(0, 1fr);
}

.is-collapsed .drawer-label {
	/* Keep names (including the page heading) available to assistive technology. */
	position: absolute;
	width: 1px;
	height: 1px;
	padding: 0;
	margin: -1px;
	overflow: hidden;
	clip-path: inset(50%);
	white-space: nowrap;
}

.is-collapsed .layout-control {
	display: none;
}

.is-collapsed #layout-toggle {
	display: flex;
}

.is-collapsed #drawer-toggle .icon {
	transform: rotate(180deg);
}

select,
.button {
	/* Compute on each button so hover follows its own accent color. */
	--control-hover: color-mix(
		in oklch,
		var(--control),
		var(--control-hover-mix) var(--control-hover-amount)
	);
	min-height: 2.75rem;
	border: 1px solid var(--control);
}

select {
	width: 100%;
	padding: 0.55rem 2rem 0.55rem 0.75rem;
	color: var(--ink);
	background: var(--paper);
	font-weight: 650;
}

.button {
	display: flex;
	align-items: center;
	justify-content: center;
	gap: 0.6rem;
	padding: 0.6rem;
	color: var(--button-ink);
	background: var(--control);
	font-weight: 800;
	cursor: pointer;
	transition:
		background-color 160ms ease,
		transform 160ms ease;
}

.button:hover:not(:disabled) {
	background: var(--control-hover);
	transform: translateY(-1px);
}

.button:active:not(:disabled) {
	transform: translateY(1px);
}

.button:disabled,
select:disabled {
	cursor: not-allowed;
	color: var(--disabled-ink);
	background: var(--disabled-bg);
	border-color: var(--line);
}

.button-secondary {
	color: var(--ink);
	background: color-mix(in oklch, var(--control) 8%, var(--paper));
}

#undo {
	--control: var(--svg-color-secondary);
}

#shuffle,
#theme-toggle {
	--control: var(--svg-color-tertiary);
}

#hint {
	--control: var(--svg-color-neutral);
}

.button-secondary:hover:not(:disabled) {
	color: var(--button-ink);
}

.icon-button {
	flex: 0 0 2.75rem;
	width: 2.75rem;
	padding: 0.5rem;
}

#sound-toggle,
#theme-toggle {
	width: 100%;
	margin-top: 0.75rem;
}

/* Fixed squares keep padding from stretching the icon rail. */
.is-collapsed .game-sidebar .button,
.is-collapsed #sound-toggle,
.is-collapsed #theme-toggle,
.is-collapsed .game-summary {
	width: 2.75rem;
	height: 2.75rem;
}

.is-collapsed .game-sidebar .button {
	padding: 0.25rem;
}

/* The real button stays disabled. A clipped copy reveals both enabled colors
   from the bottom, without adding another accessible name or focus target. */
#hint {
	position: relative;
}

.hint-icon {
	display: grid;
	place-items: center;
	width: 2rem;
	height: 2rem;
}

.hint-reveal {
	display: none;
	position: absolute;
	inset: 0;
	align-items: center;
	justify-content: center;
	gap: inherit;
	padding: inherit;
	color: var(--button-ink);
	background: var(--control);
	clip-path: inset(100% 0 0);
	pointer-events: none;
}

.is-cooling .hint-reveal {
	display: flex;
}

.game-summary {
	display: grid;
	border: 1px solid var(--line);
	user-select: none;
}

.stat {
	display: flex;
	gap: 0.5rem;
	align-items: baseline;
	justify-content: space-between;
	padding: 0.6rem;
	font-size: 1rem;
	font-weight: 400;
}

.is-collapsed .stat {
	justify-content: center;
	padding-inline: 0;
}

.stat span {
	color: var(--muted);
	font: inherit;
}

.stat strong {
	font: inherit;
	font-variant-numeric: tabular-nums;
}

.game-area {
	min-width: 0;
}

.game-status {
	min-height: 1.5em;
	margin: 0 0 0.5rem;
	color: var(--ink);
	font-weight: 700;
	text-align: center;
}

.board-viewport {
	width: 100%;
	min-height: 12rem;
	overflow: auto;
	padding: 0.25rem;
	border: 1px solid var(--board-dark);
	background:
		radial-gradient(
			circle at 50% 42%,
			var(--board-highlight),
			transparent 24rem
		),
		linear-gradient(145deg, var(--board), var(--board-dark));
	box-shadow: var(--board-shadow);
}

/* JavaScript sizes this wrapper because absolutely positioned tiles do not
   give their parent a natural size. It reserves the scaled board area. */
.board-stage {
	position: relative;
	margin-inline: auto;
}

.game-board {
	position: absolute;
	top: 0;
	left: 0;
	/* Keep the large tile z-index values inside the board's stacking context. */
	isolation: isolate;
}

.game-board.is-arranging .tile {
	pointer-events: none;
	transition: none;
}

/* Keep new tiles hidden until their travel animations take over. */
.game-board[data-arrangement="deal"] .tile {
	opacity: 0;
}

.tile {
	--current-tile-shadow: calc(var(--tile-depth-x) + 2px)
		calc(var(--tile-depth-y) + 3px) 5px var(--tile-shadow-color);
	position: absolute;
	top: calc(var(--tile-top) * var(--board-scale, 1));
	left: calc(var(--tile-left) * var(--board-scale, 1));
	z-index: var(--tile-z);
	display: block;
	inline-size: var(--tile-width);
	block-size: var(--tile-height);
	padding: 0;
	border: 1px solid var(--tile-face-border);
	border-radius: var(--tile-radius);
	background-color: var(--tile-face-dark);
	background-image:
		var(--tile-image),
		linear-gradient(
			145deg,
			var(--tile-face-light) 0%,
			var(--tile-face-dark) 100%
		);
	background-position:
		center,
		0 0;
	background-repeat: no-repeat;
	background-size:
		100% 100%,
		100% 100%;
	box-shadow: var(--current-tile-shadow);
	cursor: default;
	transition: filter 180ms ease;
}

.tile-cover {
	position: absolute;
	inset: 0;
	border-radius: inherit;
	background: linear-gradient(
		145deg,
		var(--tile-face-light),
		var(--tile-face-dark)
	);
	opacity: 0;
	pointer-events: none;
}

.game-board.is-arranging .tile-cover {
	opacity: 1;
}

/* The button draws the face. The depth SVGs are transparent except for their
	bottom and right planes, so the pseudo-elements can paint above it. */
.tile::before,
.tile::after {
	content: "";
	position: absolute;
	inset: -1px auto auto -1px;
	z-index: 0;
	inline-size: calc(var(--tile-width) + var(--tile-depth-x));
	block-size: calc(var(--tile-height) + var(--tile-depth-y));
	background-position: 0 0;
	background-repeat: no-repeat;
	background-size: 100% 100%;
	pointer-events: none;
}

.tile::before {
	background-image: var(--svg-image-tile-bottom);
}

.tile::after {
	background-image: var(--svg-image-tile-right);
}

.tile.is-free {
	cursor: pointer;
}

.tile.is-free:focus-visible,
.tile.is-removing {
	/* Keep the interaction treatment visible above neighboring board layers. */
	z-index: 1000000;
}

.tile.is-free:hover,
.tile.is-free:focus-visible,
.tile.is-free:active,
.tile.is-selected,
.tile.is-hinted {
	filter: saturate(6);
	/* Keep hovered and selected tiles in place so their edges stay aligned. */
	transform: none;
}

.tile.is-free:focus-visible {
	outline: 0.125rem solid var(--tile-focus);
	outline-offset: -0.375rem;
}

.tile.is-free:active,
.tile.is-selected {
	box-shadow:
		inset 0 0 0 0.125rem var(--tile-selected),
		inset 0 0 0 0.1875rem var(--tile-selected-inner),
		var(--current-tile-shadow);
}

.tile.is-removing {
	pointer-events: none;
	filter: brightness(1.18) saturate(1.12);
	opacity: 0;
	transform: translateY(-24px) rotate(4deg) scale(0.72);
	/* Fade only matched pairs. Keep the 280ms opacity and transform durations
	   in sync with REMOVE_DURATION in mahjong.js. */
	transition:
		filter 180ms ease,
		opacity 280ms ease,
		transform 280ms cubic-bezier(0.2, 0.75, 0.25, 1.12);
}

.tile.is-mismatch {
	animation: mismatch 480ms ease;
}

@keyframes mismatch {
	0%,
	100% {
		transform: translateX(0);
	}
	25% {
		transform: translateX(-5px);
	}
	50% {
		transform: translateX(5px);
	}
	75% {
		transform: translateX(-3px);
	}
}

.result-dialog {
	max-width: min(90vw, 30rem);
	padding: 1.5rem;
	border: 1px solid var(--control);
	border-radius: 0;
	color: var(--ink);
	background: var(--paper);
	box-shadow: var(--dialog-shadow);
}

.result-dialog::backdrop {
	background: var(--dialog-backdrop);
}

.result-dialog h2 {
	margin: 0 0 0.5rem;
	font-family: var(--font-heading);
	font-size: 1.8rem;
}

.result-dialog p {
	margin: 0 0 1.25rem;
	line-height: 1.5;
}

.dialog-actions {
	display: flex;
	flex-wrap: wrap;
	gap: 0.6rem;
}

@media (max-width: 48rem) {
	/* Keep controls in normal flow so wrapping or larger text pushes the board
	   down. An overlay would hide tiles on narrow screens. */
	.game-panel,
	.game-panel.is-collapsed {
		grid-template-columns: minmax(0, 1fr);
	}

	.game-sidebar {
		display: flex;
		flex-wrap: wrap;
		align-items: center;
		gap: 0.375rem;
		padding: 0 0 0.75rem;
		border-right: 0;
		border-bottom: 1px solid var(--line);
	}

	.drawer-header,
	.game-toolbar {
		display: contents;
	}

	.game-sidebar .drawer-label {
		position: absolute;
		width: 1px;
		height: 1px;
		padding: 0;
		margin: -1px;
		overflow: hidden;
		clip-path: inset(50%);
		white-space: nowrap;
	}

	#drawer-toggle,
	.layout-control {
		display: none;
	}

	#layout-toggle {
		display: flex;
	}

	.game-sidebar .button,
	.game-summary {
		flex: 0 0 2.75rem;
		width: 2.75rem;
		min-height: 2.75rem;
	}

	.game-sidebar .button {
		height: 2.75rem;
		padding: 0.25rem;
	}

	.stat {
		justify-content: center;
		padding-inline: 0;
	}

	#sound-toggle,
	#theme-toggle {
		margin: 0;
	}

	.game-panel.is-choosing-layout .layout-control {
		display: grid;
		flex-basis: 100%;
		order: 1;
	}
}

@media (max-width: 36rem) {
	.game-shell {
		width: min(100% - 0.5rem, 100rem);
		padding-block: 0.25rem;
	}
}

@media (prefers-reduced-motion: reduce) {
	::view-transition-group(root),
	::view-transition-old(root),
	::view-transition-new(root) {
		animation: none !important;
	}

	.hint-reveal {
		display: none !important;
	}

	*,
	*::before,
	*::after {
		scroll-behavior: auto !important;
		transition: none !important;
		animation-duration: 0.01ms !important;
		animation-iteration-count: 1 !important;
	}
}
