/*
 * Themes: theme.js sets data-theme on the root to light, dark or sepia. Backgrounds stay six-digit hex
 * colours, which landing-bg.js takes apart. --c-* are the colours players can pick; --p0 and --p1 are
 * the two seats', which the game page points at the colours the players chose.
 */
:root {
	--bg: #fbfaf6;
	--bg-replay: #e4e2dc;
	--grid: #dfe6ef;
	--grid-replay: #c4c7cc;
	--text: #1d2330;
	--muted: #6a7384;
	--panel: #ffffffee;
	--dialog: #ffffff;
	--border: #e3e6ec;
	--shadow: 0 6px 24px #1d233018;
	--vignette: #1d233054;
	--accent: #1d2330;
	--accent-text: #ffffff;
	--danger: #c93a3f;
	--c-red: #e5484d;
	--c-blue: #3e63dd;
	--c-green: #2b9a66;
	--c-orange: #e07400;
	--p0: var(--c-red);
	--p1: var(--c-blue);
	color-scheme: light;
}

:root[data-theme="dark"] {
	--bg: #15181e;
	--bg-replay: #272a31;
	--grid: #262c36;
	--grid-replay: #3b414c;
	--text: #e8ebf0;
	--muted: #97a0af;
	--panel: #1e232bee;
	--dialog: #1e232b;
	--border: #313845;
	--shadow: 0 6px 24px #0008;
	--vignette: #000000ff;
	--accent: #e8ebf0;
	--accent-text: #15181e;
	--danger: #ff6369;
	--c-red: #ff6369;
	--c-blue: #6e8cff;
	--c-green: #3dd68c;
	--c-orange: #ff9a2e;
	color-scheme: dark;
}

/* Warm paper: brown ink on cream, with the player colours toned down to match. */
:root[data-theme="sepia"] {
	--bg: #f4ecd8;
	--bg-replay: #e2d6bc;
	--grid: #e0d2b3;
	--grid-replay: #c7b898;
	--text: #3d3122;
	--muted: #7b6a53;
	--panel: #fbf5e7ee;
	--dialog: #fbf5e7;
	--border: #ddcdad;
	--shadow: 0 6px 24px #3d312220;
	--vignette: #3d312272;
	--accent: #3d3122;
	--accent-text: #fbf5e7;
	--danger: #b93b37;
	--c-red: #c9423f;
	--c-blue: #3b5cb8;
	--c-green: #3a8a4f;
	--c-orange: #cf6d0a;
	color-scheme: light;
}

* {
	box-sizing: border-box;
}

[hidden] {
	display: none !important;
}

html,
body {
	margin: 0;
	height: 100%;
	background: var(--bg);
	color: var(--text);
	font: 700 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

button,
input {
	font: inherit;
	color: inherit;
}

.btn {
	border: 1px solid var(--border);
	background: var(--panel);
	/* A link made a button, like "New game", takes the theme's text colour rather than the browser's link blue. */
	color: var(--text);
	border-radius: 10px;
	padding: 8px 14px;
	cursor: pointer;
	white-space: nowrap;
	text-decoration: none;
	display: inline-flex;
	align-items: center;
	justify-content: center;
}

.btn:hover {
	border-color: var(--muted);
}

.btn.primary {
	background: var(--accent);
	color: var(--accent-text);
	border-color: var(--accent);
}

.btn.danger {
	color: var(--danger);
}

.btn:disabled {
	opacity: 0.5;
	cursor: default;
}

.panel {
	background: var(--panel);
	border: 1px solid var(--border);
	border-radius: 14px;
	box-shadow: var(--shadow);
	backdrop-filter: blur(8px);
}

/* Landing */

#backdrop {
	position: fixed;
	inset: 0;
	width: 100%;
	height: 100%;
	display: block;
}

.landing {
	position: relative;
	min-height: 100%;
	display: grid;
	place-items: center;
	padding: 16px;
}

.landing .card {
	width: min(380px, 100%);
	padding: 32px 28px;
	text-align: center;
}

.landing h1 {
	margin: 0 0 4px;
	font-size: 40px;
	letter-spacing: -1px;
}

.landing h1 .d0 {
	color: var(--p0);
}

.landing h1 .d1 {
	color: var(--p1);
}

.landing p {
	margin: 0 0 24px;
	color: var(--muted);
}

.landing label,
.landing .field {
	display: flex;
	justify-content: space-between;
	align-items: center;
	gap: 12px;
	margin-bottom: 20px;
	text-align: left;
}

.landing label small,
.landing .field small {
	display: block;
	color: var(--muted);
}

.landing .field .color-choices {
	flex-wrap: nowrap;
	gap: 8px;
}

.landing .field .swatch {
	width: 26px;
	height: 26px;
}

.landing .version {
	margin-top: 16px;
	color: var(--muted);
	font-size: 12px;
	font-weight: 400;
}

/* The theme button in the top right corner of the start page. */
.corner {
	position: fixed;
	top: 12px;
	right: 12px;
}

.landing input[type="number"] {
	width: 80px;
	padding: 8px 10px;
	border: 1px solid var(--border);
	border-radius: 10px;
	background: transparent;
	text-align: center;
}

.landing .btn.primary {
	width: 100%;
	padding: 12px;
	font-size: 16px;
}

/* Game */

.game {
	overflow: hidden;
	touch-action: none;
}

#board {
	position: fixed;
	inset: 0;
	width: 100%;
	height: 100%;
	display: block;
	cursor: crosshair;
}

#board.dragging {
	cursor: grabbing;
}

/*
 * Shades the board towards the edges of the screen. It sits right after the canvas, under the HUD. An
 * inset shadow reaches as far in at the corners as along the edges, unlike a radial gradient. The spread
 * makes the very edge 3/4 of --vignette rather than the half a bare blur gives, and the blur is cut to
 * match, so the shade still fades out about 14vmin in. The second, narrow shadow darkens the last few
 * vmin by half as much again (less in the dark theme, where the edge is already close to black).
 */
.vignette {
	position: fixed;
	inset: 0;
	pointer-events: none;
	box-shadow: inset 0 0 10vmin 3.4vmin var(--vignette), inset 0 0 4vmin var(--vignette);
}

.hud {
	position: fixed;
	top: 12px;
	left: 12px;
	right: 12px;
	display: flex;
	justify-content: space-between;
	align-items: flex-start;
	gap: 8px;
	pointer-events: none;
}

.hud > * {
	pointer-events: auto;
}

.hud-left {
	display: flex;
	align-items: stretch;
	gap: 8px;
	flex-wrap: wrap;
}

.chip {
	display: flex;
	align-items: center;
	padding: 8px 14px;
}

.sound-toggle,
.looks-toggle {
	justify-content: center;
	padding: 8px 12px;
	cursor: pointer;
	color: var(--text);
}

.sound-toggle:hover,
.looks-toggle:hover,
.looks-toggle.open {
	border-color: var(--muted);
}

.sound-toggle svg,
.looks-toggle svg {
	fill: none;
	stroke: currentColor;
	stroke-width: 2;
	stroke-linecap: round;
	stroke-linejoin: round;
}

.looks-toggle .paint {
	fill: currentColor;
	stroke: none;
}

.sound-toggle .speaker {
	fill: currentColor;
}

.sound-toggle .cross,
.sound-toggle.muted .waves {
	display: none;
}

.sound-toggle.muted {
	color: var(--muted);
}

.sound-toggle.muted .cross {
	display: inline;
}

/* The theme and colour menu, placed under its button by theme.js. */

.looks-menu {
	position: fixed;
	z-index: 5;
	display: flex;
	flex-direction: column;
	gap: 8px;
	padding: 12px;
	width: max-content;
	max-width: calc(100vw - 16px);
}

.menu-title {
	color: var(--muted);
	font-size: 13px;
}

.menu-title + * {
	margin-bottom: 4px;
}

.theme-choices,
.color-choices {
	display: flex;
	flex-wrap: wrap;
	gap: 6px;
}

.theme-choice {
	gap: 8px;
	padding: 6px 12px;
}

.theme-choice.active {
	border-color: var(--text);
	box-shadow: inset 0 0 0 1px var(--text);
}

/* Each theme's own background, whichever theme the page is in. */
.theme-swatch {
	width: 16px;
	height: 16px;
	border-radius: 50%;
	border: 1px solid #7f7f7f80;
}

.theme-swatch.light {
	background: #fbfaf6;
}

.theme-swatch.dark {
	background: #15181e;
}

.theme-swatch.sepia {
	background: #f4ecd8;
}

.color-choices {
	gap: 10px;
	padding: 2px;
}

.swatch {
	width: 32px;
	height: 32px;
	padding: 0;
	border: 0;
	border-radius: 50%;
	background: var(--swatch);
	cursor: pointer;
}

.swatch:hover:not(:disabled),
.swatch.selected {
	box-shadow: 0 0 0 2px var(--dialog), 0 0 0 4px var(--text);
}

.swatch:hover:not(:disabled):not(.selected) {
	box-shadow: 0 0 0 2px var(--dialog), 0 0 0 4px var(--muted);
}

/* The opponent's colour, crossed out. */
.swatch:disabled {
	opacity: 0.3;
	cursor: not-allowed;
	background: linear-gradient(135deg, transparent 45%, var(--text) 45%, var(--text) 55%, transparent 55%), var(--swatch);
}

/* Reactions: the button that opens the palette, and the emoji rising over the board. */

.reactions {
	position: relative;
}

.emoji-toggle {
	height: 100%;
	justify-content: center;
	padding: 6px 12px;
	font-size: 20px;
	line-height: 1;
	cursor: pointer;
}

.emoji-toggle:hover,
.emoji-toggle.open {
	border-color: var(--muted);
}

.emoji-palette {
	position: absolute;
	top: calc(100% + 6px);
	left: 0;
	display: flex;
	flex-wrap: wrap;
	gap: 4px;
	padding: 6px;
	width: max-content;
	max-width: min(320px, calc(100vw - 40px));
}

.emoji-palette .btn {
	width: 44px;
	height: 44px;
	padding: 0;
	font-size: 22px;
	line-height: 1;
}

.reaction-stream {
	position: fixed;
	inset: 0;
	overflow: hidden;
	pointer-events: none;
}

/*
 * A reaction is thrown up from below the sender's half of the screen, slowing down all the way to
 * the middle, where it grows and fades out.
 *
 * Every part of the movement runs as one curve from beginning to end, because a path stitched
 * together from several steps changes speed at every seam. Sideways travel, climb, spin and growth
 * each need a curve of their own, so they are separate animations: sideways on the outer element,
 * the rest on the emoji through the transform properties, which animate one at a time.
 *
 * The outer element's bottom edge sits at half the height, so the animation centres the emoji by its
 * own size and neither has to know how big it is.
 */
.flyer {
	position: absolute;
	bottom: 50%;
	animation: throw-across var(--rise) cubic-bezier(0.15, 0.55, 0.25, 1) forwards;
}

.flyer .glyph {
	display: block;
	/* Big, but never so big that a narrow or short window cuts the emoji off. */
	font-size: min(256px, 40vh, 30vw);
	line-height: 1;
	animation:
		throw-up var(--rise) cubic-bezier(0.05, 0.75, 0.15, 1) forwards,
		/* The spin is over halfway through the flight, easing out so it does not stop short. */
		throw-spin calc(var(--rise) * 0.5) cubic-bezier(0.1, 0.8, 0.25, 1) forwards,
		throw-grow var(--rise) linear forwards,
		throw-fade var(--rise) linear forwards;
}

@media (max-width: 640px) {
	.flyer .glyph {
		font-size: 48px;
	}
}

/* Sideways, easing off later than the climb, which is what bends the path into a curve. */
@keyframes throw-across {
	from {
		translate: calc(-50% + var(--from-x)) 0;
	}
	to {
		translate: -50% 0;
	}
}

/*
 * The climb, from below the screen to just above the middle: fast at first and losing speed the
 * whole way, never stopping anywhere. 50% of the emoji's own size puts its centre on the middle line.
 */
@keyframes throw-up {
	from {
		translate: 0 calc(50vh + 100%);
	}
	to {
		translate: 0 40%;
	}
}

@keyframes throw-spin {
	from {
		rotate: var(--spin);
	}
	to {
		rotate: 0deg;
	}
}

/*
 * Up to its own size while it is still travelling, then on growing as it fades. Both parts come to
 * rest at the seam — one ends at no speed, the next starts at none — so the change doesn't show.
 */
@keyframes throw-grow {
	0% {
		scale: 0.55;
		animation-timing-function: cubic-bezier(0.2, 0.85, 0.3, 1);
	}
	30% {
		scale: 1;
		animation-timing-function: cubic-bezier(0.45, 0, 0.9, 0.45);
	}
	100% {
		scale: 1.7;
	}
}

/* A reaction never goes fully opaque: the board stays readable through it. */
@keyframes throw-fade {
	0% {
		opacity: 0;
	}
	8% {
		opacity: 0.75;
	}
	45% {
		opacity: 0.75;
	}
	100% {
		opacity: 0;
	}
}

.scoreboard {
	display: flex;
	align-items: center;
	gap: 14px;
	padding: 8px 14px;
	flex-wrap: wrap;
}

.player {
	display: flex;
	align-items: center;
	gap: 8px;
	padding: 3px 8px;
	border-radius: 8px;
	border: 2px solid transparent;
}

.player.turn {
	border-color: currentColor;
	background: color-mix(in srgb, currentColor 12%, transparent);
}

.player.p0 {
	color: var(--p0);
}

.player.p1 {
	color: var(--p1);
}

.player .dot {
	width: 12px;
	height: 12px;
	border-radius: 50%;
	background: currentColor;
}

.player .name {
	color: var(--text);
}

.player .name small {
	color: var(--muted);
}

.player .score {
	font-weight: 900;
	font-size: 18px;
	min-width: 1ch;
	color: var(--text);
}

.player .away {
	font-size: 15px;
	color: var(--danger);
}

.status {
	color: var(--text);
	font-size: 15px;
}

/* The end of the game stands out in the theme's inverted colours. */
.status.over {
	font-size: 16px;
	font-weight: 700;
	background: var(--accent);
	border-color: var(--accent);
	color: var(--accent-text);
}

.actions {
	display: flex;
	gap: 8px;
	flex-wrap: wrap;
	justify-content: flex-end;
}

.waiting-banner {
	position: fixed;
	top: 50%;
	left: 16px;
	right: 16px;
	transform: translateY(-50%);
	text-align: center;
	font-size: 66px;
	line-height: 1.15;
	color: var(--muted);
	opacity: 0.6;
	pointer-events: none;
}

/* Who is ahead, under "Waiting for Red…". */
.waiting-detail {
	margin-top: 12px;
	font-size: 32px;
}

@media (max-width: 640px) {
	.waiting-banner {
		font-size: 40px;
	}

	.waiting-detail {
		font-size: 22px;
	}
}

.zoom {
	position: fixed;
	right: 12px;
	bottom: 12px;
	display: flex;
	flex-direction: column;
	gap: 6px;
}

.zoom .btn {
	width: 40px;
	height: 40px;
	padding: 0;
	font-size: 18px;
}

/* Two letters where the other buttons have one sign. */
.zoom .view3d {
	font-size: 14px;
}

.zoom .view3d[aria-pressed="true"] {
	border-color: var(--text);
	box-shadow: inset 0 0 0 1px var(--text);
}

.replay-bar {
	position: fixed;
	left: 50%;
	bottom: 12px;
	transform: translateX(-50%);
	display: flex;
	align-items: center;
	gap: 6px;
	padding: 6px;
}

.replay-bar .btn {
	width: 44px;
	height: 40px;
	padding: 0;
	font-size: 22px;
	line-height: 1;
}

.replay-bar .play svg {
	fill: currentColor;
}

.replay-bar .play .icon-pause,
.replay-bar .play.playing .icon-play {
	display: none;
}

.replay-bar .play.playing .icon-pause {
	display: inline;
}

.replay-step {
	min-width: 110px;
	text-align: center;
	font-variant-numeric: tabular-nums;
}

.landing .games-link {
	width: 100%;
	margin-top: 10px;
	padding: 10px;
}

.dialog.games-dialog {
	width: min(560px, 100%);
	display: flex;
	flex-direction: column;
	max-height: calc(100vh - 32px);
}

.games-list {
	overflow-y: auto;
	margin: 4px -8px 0;
	min-height: 60px;
}

.games-body {
	display: flex;
	flex-direction: column;
	min-height: 0;
}

.pager {
	display: flex;
	align-items: center;
	justify-content: center;
	gap: 12px;
	margin-top: 12px;
}

.pager span {
	min-width: 100px;
	text-align: center;
	font-variant-numeric: tabular-nums;
}

.games-empty {
	padding: 8px;
}

.game-row {
	display: flex;
	justify-content: space-between;
	align-items: center;
	gap: 12px;
	padding: 10px 8px;
	border-radius: 10px;
	color: inherit;
	text-decoration: none;
}

.game-row + .game-row {
	border-top: 1px solid var(--border);
}

.game-row:hover {
	background: var(--grid);
}

.game-row small {
	display: block;
	color: var(--muted);
}

.game-result {
	text-align: right;
	white-space: nowrap;
}

/* Everything is bold, so the winner stands out with the heaviest weight and an underline. */
.game-result .winner {
	font-weight: 900;
	text-decoration: underline;
	text-underline-offset: 3px;
}

.toast {
	position: fixed;
	left: 50%;
	bottom: 20px;
	transform: translateX(-50%);
	padding: 10px 16px;
	max-width: calc(100% - 32px);
	opacity: 0;
	transition: opacity 0.2s;
	pointer-events: none;
}

.toast.show {
	opacity: 1;
}

.overlay {
	position: fixed;
	inset: 0;
	display: grid;
	place-items: center;
	padding: 16px;
	background: #0003;
}

.dialog {
	width: min(440px, 100%);
	padding: 24px;
	background: var(--dialog);
}

.overlay.blocking {
	z-index: 10;
	background: #0006;
}

.dialog.reconnect-dialog {
	width: auto;
}

.reconnect {
	display: flex;
	align-items: center;
	gap: 14px;
	font-size: 16px;
}

.waiting {
	display: flex;
	align-items: center;
	gap: 14px;
	margin-top: 18px;
	color: var(--muted);
}

/* A dot sending out rings from its center. */
.pulse {
	position: relative;
	flex: none;
	width: 12px;
	height: 12px;
	margin-left: 4px;
	border-radius: 50%;
	background: var(--p0);
}

.pulse::before,
.pulse::after {
	content: "";
	position: absolute;
	inset: 0;
	border-radius: 50%;
	border: 2px solid var(--p0);
	animation: ripple 1.8s ease-out infinite;
}

.pulse::after {
	animation-delay: 0.9s;
}

@keyframes ripple {
	from {
		transform: scale(1);
		opacity: 0.9;
	}

	to {
		transform: scale(3.2);
		opacity: 0;
	}
}

.spinner {
	width: 20px;
	height: 20px;
	border: 3px solid var(--border);
	border-top-color: var(--text);
	border-radius: 50%;
	animation: spin 0.8s linear infinite;
}

@keyframes spin {
	to {
		transform: rotate(360deg);
	}
}

.dialog h2 {
	margin: 0 0 8px;
}

.dialog p {
	margin: 0 0 16px;
	color: var(--muted);
}

.dialog .row {
	display: flex;
	gap: 8px;
}

.dialog-buttons {
	display: flex;
	justify-content: flex-end;
	gap: 8px;
	margin-top: 16px;
}

.dialog > p + .dialog-buttons {
	margin-top: 0;
}

.dialog input {
	flex: 1;
	min-width: 0;
	padding: 8px 10px;
	border: 1px solid var(--border);
	border-radius: 10px;
	background: transparent;
}

@media (max-width: 640px) {
	.hud {
		flex-direction: column;
		align-items: stretch;
	}

	.hud-left {
		justify-content: center;
	}

	.scoreboard {
		gap: 8px;
		justify-content: center;
	}

	.actions {
		justify-content: center;
	}

	body.replay .zoom {
		bottom: 72px;
	}

	.game-row {
		align-items: flex-start;
	}
}
