/*
 * Chrome, layout, and every rule the tubes are drawn with.
 *
 * This is the half of the colour work a stylesheet can express. The other half --
 * "this band of liquid is the fifth colour" -- is set per band from
 * js/ui/palette.js as the --colour custom property.
 *
 * A dark bench, and every saturated colour on the screen is liquid. Buttons,
 * labels and glass are all greys, so nothing competes with the one thing the
 * player is actually reading: which colour is where.
 */

:root {
	--bench: #12161e;
	--bench-lift: #1b2230; /* panels and buttons */
	--bench-high: #232c3c; /* a button under the finger */

	--ink: #eef2f9;
	--ink-soft: #97a3ba; /* secondary labels */
	--ink-faint: #55607a; /* disabled button text */

	--edge: #313b4d;
	--edge-strong: #4b5772;

	--glass-fill: rgb(255 255 255 / 5%);
	--glass-edge: rgb(255 255 255 / 24%);
	--glass-edge-done: rgb(255 255 255 / 55%);
	--shine: rgb(255 255 255 / 45%);

	--radius-control: 13px;
	--radius-card: 20px;

	color-scheme: dark;
}

* {
	box-sizing: border-box;
}

html,
body {
	height: 100%;
	margin: 0;
}

body {
	background-color: var(--bench);
	background-image: radial-gradient(125% 75% at 50% 0%, #1e2635 0%, #12161e 62%);
	color: var(--ink);
	font-family: ui-rounded, "SF Pro Rounded", "Segoe UI Variable Display", system-ui,
		-apple-system, "Helvetica Neue", sans-serif;
	font-size: clamp(0.95rem, 3.4vw, 1.1rem);
	line-height: 1.35;
	/* No rubber-banding and no double-tap zoom: this is a board you tap, not a
	   document you scroll. */
	overscroll-behavior: none;
	touch-action: manipulation;
	-webkit-text-size-adjust: 100%;
}

#app {
	position: relative;
	width: 100%;
	max-width: 32rem;
	height: 100%;
	margin-inline: auto;
	/* A phone's design width on a desktop screen. Wider than this and the tubes
	   grow past any reasonable reach without getting easier to read. */
}

/* A phone held sideways has half the height and twice the width, and a rack of
   twelve tubes squeezed into a portrait column is the one place that hurts. */
@media (orientation: landscape) and (max-height: 560px) {
	#app {
		max-width: 46rem;
	}
}

.screen {
	position: absolute;
	inset: 0;
	display: flex;
	flex-direction: column;
	padding: max(0.75rem, env(safe-area-inset-top)) 0.875rem
		max(0.75rem, env(safe-area-inset-bottom));
}

.screen[hidden] {
	display: none;
}

/* --- Controls ------------------------------------------------------------- */

button {
	min-height: 3.1rem;
	padding: 0.5rem 0.875rem;
	border: 1px solid var(--edge);
	border-radius: var(--radius-control);
	background: var(--bench-lift);
	color: var(--ink);
	font: inherit;
	font-weight: 600;
	cursor: pointer;
	-webkit-user-select: none;
	user-select: none;
	transition: background-color 90ms ease-out, border-color 90ms ease-out;
}

button.primary {
	min-height: 3.6rem;
	border-color: transparent;
	background: var(--ink);
	color: var(--bench);
	font-size: 1.05em;
}

button.quiet {
	min-height: 2.75rem;
	border-color: transparent;
	background: none;
	color: var(--ink-soft);
	font-weight: 500;
}

@media (hover: hover) {
	button:hover:not(:disabled) {
		background: var(--bench-high);
		border-color: var(--edge-strong);
	}

	button.primary:hover:not(:disabled) {
		background: #ffffff;
	}

	button.quiet:hover:not(:disabled) {
		background: var(--bench-lift);
		color: var(--ink);
	}
}

button:active:not(:disabled) {
	background: var(--bench-high);
	border-color: var(--edge-strong);
}

button:focus-visible {
	outline: 2px solid var(--ink);
	outline-offset: 2px;
}

button:disabled {
	background: none;
	border-color: var(--edge);
	color: var(--ink-faint);
	cursor: default;
}

/* --- Menu ---------------------------------------------------------------- */

.menu {
	justify-content: center;
	gap: 0.6rem;
	text-align: center;
	padding-inline: 1.625rem;
}

.hero {
	display: block;
	width: 100%;
	max-height: 34vh;
	margin-bottom: 0.5rem;
}

.menu h1 {
	margin: 0;
	font-size: clamp(2.1rem, 9.5vw, 2.8rem);
	font-weight: 700;
	letter-spacing: -0.01em;
}

.subtitle {
	margin: 0 0 0.4rem;
	color: var(--ink-soft);
	text-wrap: balance;
}

/* Three ways onto the ladder, three across a phone. */
.start-at {
	margin-top: 0.25rem;
}

.start-at h2 {
	margin: 0 0 0.375rem;
	font-size: 1em;
	font-weight: 700;
}

.difficulty {
	display: grid;
	grid-template-columns: repeat(3, 1fr);
	gap: 0.5rem;
}

.difficulty button {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	gap: 0.1rem;
	min-height: 3.1rem;
	padding-inline: 0.375rem;
}

/*
 * Two lines under the name rather than one wrapping line: at three buttons to a
 * phone's width, "Level 31 · 9 colours" breaks wherever it likes and leaves the
 * separator stranded. Both inherit their colour, so they follow the button
 * through hover and press.
 */
.difficulty .level {
	font-size: 0.8em;
	line-height: 1.2;
	opacity: 0.85;
}

.difficulty .colours {
	font-size: 0.7em;
	line-height: 1.2;
	opacity: 0.6;
}

.stats {
	margin-top: 0.75rem;
	color: var(--ink-soft);
	font-size: 0.95em;
}

.stats h2 {
	margin: 0 0 0.25rem;
	color: var(--ink);
	font-size: 1em;
	font-weight: 700;
}

.stats p {
	margin: 0.15rem 0;
}

/*
 * A landscape phone, or a short desktop window. The art has shrunk to nothing by
 * this height anyway, so it goes, and the tag line and both headings go with it:
 * three buttons reading Easy, Medium and Hard say what they are, the progress
 * lines say what they are, and the difficulty group keeps its label for a screen
 * reader. Without this the stack runs past the bottom of the screen. Standalone
 * installs are locked to portrait, so this is a browser tab.
 */
@media (max-height: 34rem) {
	.menu .hero,
	.menu .subtitle,
	.start-at h2,
	.stats h2 {
		display: none;
	}

	.menu {
		gap: 0.375rem;
	}

	.menu h1 {
		font-size: 1.75rem;
	}

	.stats {
		margin-top: 0;
	}
}

/* --- Game screen --------------------------------------------------------- */

.game {
	gap: 0.55rem;
}

/* Three columns rather than a flex row, so the level name is centred on the
   screen rather than on whatever space the buttons beside it leave over. */
.header-row {
	display: grid;
	grid-template-columns: 1fr auto 1fr;
	align-items: center;
	gap: 0.5rem;
}

.corner {
	justify-self: start;
	min-height: 2.75rem; /* 44px: the smallest target a thumb reliably hits */
	padding-inline: 0.75rem;
	font-size: 0.9em;
	font-weight: 500;
}

.level {
	font-weight: 600;
	white-space: nowrap;
}

.time {
	justify-self: end;
	color: var(--ink-soft);
	font-variant-numeric: tabular-nums;
}

.tally {
	display: flex;
	justify-content: center;
	gap: 0.75rem;
	margin: 0;
	color: var(--ink-soft);
	font-size: 0.9em;
	font-variant-numeric: tabular-nums;
}

.tally span + span::before {
	content: "·";
	margin-right: 0.75rem;
}

/* --- The rack ------------------------------------------------------------ */

/*
 * One measured number, --seg, drives the tubes: RackView works out how tall a
 * single unit of liquid can be in the space the rack has been given, and every
 * size below is a calc() off it. The fallbacks are what the first frame uses,
 * before the rack has been measured.
 */
.rack {
	position: relative;
	z-index: 0;
	display: flex;
	flex: 1 1 auto;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	gap: var(--row-gap, 16px);
	min-height: 0;
	/* Room for a tube to tilt into on its way over its neighbours. */
	padding-top: var(--tilt-room, 28px);

	--seg: 34px;
	--tube-w: 47px;
	--rim: 13px;
	--capacity: 4;
	--stream-w: max(4px, calc(var(--seg) * 0.2));
}

.rack-row {
	position: relative;
	display: flex;
	align-items: flex-end;
	justify-content: center;
	gap: var(--tube-gap, 9px);
}

/* The shelf the tubes stand on. Without it they float on the background. */
.rack-row::after {
	content: "";
	position: absolute;
	inset: auto -3% calc(var(--seg) * -0.22) -3%;
	height: 2px;
	border-radius: 2px;
	background: linear-gradient(90deg, transparent, var(--edge) 15%, var(--edge) 85%, transparent);
}

.tube {
	position: relative;
	z-index: 1;
	width: var(--tube-w);
	/* Not a control-sized button: the tube is as tall as its liquid says. */
	min-height: 0;
	height: calc(var(--seg) * var(--capacity) + var(--rim));
	padding: 0;
	border: 0;
	border-radius: 0;
	background: none;
	transform-origin: 50% 100%;
	transition: transform var(--return-ms, 160ms) cubic-bezier(0.3, 0.7, 0.4, 1);
	-webkit-tap-highlight-color: transparent;
	touch-action: manipulation;
}

.tube:focus-visible {
	outline-offset: 4px;
}

.tube.is-entering {
	animation: tube-in 280ms ease-out backwards;
	animation-delay: calc(var(--order, 0) * 30ms);
}

.tube.is-new {
	animation: tube-arrive 340ms cubic-bezier(0.2, 1.25, 0.45, 1) backwards;
}

/* Lifted clear of the shelf: the tube in hand is the one thing on the board that
   is not currently standing on it. */
.tube.is-picked {
	transform: translateY(calc(var(--seg) * -0.5));
}

/* Raised over its neighbours for as long as it is out of its place. */
.tube.is-pouring,
.tube.is-returning {
	z-index: 3;
}

.tube.is-pouring {
	transition-duration: var(--lift-ms, 170ms);
	transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
}

.tube.is-refused {
	animation: tube-refuse var(--refuse-ms, 240ms) ease-in-out;
}

.glass {
	position: absolute;
	inset: 0;
	border: max(1.5px, calc(var(--seg) * 0.055)) solid var(--glass-edge);
	border-top-color: var(--shine);
	border-radius: 9% 9% 50% 50% / 3% 3% 18% 18%;
	background: linear-gradient(100deg, rgb(255 255 255 / 9%), rgb(255 255 255 / 2%) 40%);
	pointer-events: none;
}

/* A finished tube stops asking for attention: brighter glass, and the game will
   not pick it up again. */
.tube.is-done .glass {
	border-color: var(--glass-edge-done);
}

.shine {
	position: absolute;
	top: calc(var(--rim) * 0.5);
	bottom: 14%;
	left: 15%;
	width: max(2px, calc(var(--seg) * 0.08));
	border-radius: 999px;
	background: linear-gradient(180deg, var(--shine), rgb(255 255 255 / 4%));
}

/* Sits below the glass, so the glass edge is drawn over the liquid it holds. */
.liquid {
	position: absolute;
	inset: var(--rim) 0 0;
	overflow: hidden;
	border-radius: 4% 4% 50% 50% / 2% 2% 20% 20%;
}

/*
 * Only the top band of a tube ever changes size, so height is the only thing that
 * transitions. Everything below it is exactly where it was, and a band's distance
 * from the bottom never moves while the band exists -- which is what lets a band
 * grow out of the surface rather than slide up from the floor.
 */
.band {
	position: absolute;
	right: 0;
	bottom: calc(var(--base) * var(--seg));
	left: 0;
	height: calc(var(--size) * var(--seg));
	overflow: hidden;
	background: var(--colour, transparent);
	transition: height var(--pour-ms, 280ms) cubic-bezier(0.45, 0.05, 0.3, 1);
}

/* The surface of the liquid, and only ever on the band that has one. */
.band.is-top::after {
	content: "";
	position: absolute;
	inset: 0 0 auto;
	height: max(2px, calc(var(--seg) * 0.07));
	background: rgb(255 255 255 / 26%);
}

/*
 * The falling liquid. Behind every tube, so it disappears into the destination
 * instead of running down the front of it.
 */
.stream {
	position: absolute;
	z-index: 0;
	width: var(--stream-w);
	margin-left: calc(var(--stream-w) / -2);
	border-radius: 999px;
	background: var(--colour, #fff);
	opacity: 0;
	transform: scaleY(0);
	transform-origin: top center;
	transition: opacity 110ms linear;
	pointer-events: none;
}

.stream.is-flowing {
	opacity: 1;
	transform: scaleY(1);
	transition: transform 120ms ease-in, opacity 70ms linear;
}

/* A board arriving whole -- a level loading, a move undone -- is drawn where it
   already is rather than animated into place from wherever the last one was. */
.rack.is-still,
.rack.is-still * {
	transition: none !important;
	animation: none !important;
}

@keyframes tube-in {
	from {
		opacity: 0;
		transform: translateY(9%) scale(0.95);
	}
}

@keyframes tube-arrive {
	from {
		opacity: 0;
		transform: scale(0.7);
	}
}

@keyframes tube-refuse {
	25% {
		transform: translateX(-5%);
	}

	75% {
		transform: translateX(5%);
	}
}

/* --- Bottom bar ---------------------------------------------------------- */

.bottom-bar {
	display: flex;
	flex-direction: column;
	gap: 0.5rem;
}

.status {
	min-height: 2.4em;
	margin: 0;
	color: var(--ink-soft);
	font-size: 0.85em;
	text-align: center;
	text-wrap: pretty;
}

.toolbar {
	display: grid;
	grid-template-columns: repeat(3, 1fr);
	gap: 0.4rem;
}

.toolbar button {
	padding-inline: 0.25rem;
}

/* --- Overlays ------------------------------------------------------------ */

.overlay {
	position: absolute;
	inset: 0;
	z-index: 5;
	display: grid;
	place-items: center;
	padding: 1.25rem;
	background: rgb(10 13 18 / 72%);
}

.overlay[hidden] {
	display: none;
}

.card {
	display: flex;
	flex-direction: column;
	gap: 1rem;
	width: 100%;
	padding: 1.75rem 1.5rem;
	border: 1px solid var(--edge);
	border-radius: var(--radius-card);
	background: var(--bench-lift);
	text-align: center;
}

.card h2 {
	margin: 0;
	font-size: clamp(1.6rem, 7.5vw, 2.2rem);
	font-weight: 700;
}

#result-detail {
	margin: 0;
	color: var(--ink-soft);
	font-variant-numeric: tabular-nums;
	white-space: pre-wrap;
}

@media (prefers-reduced-motion: reduce) {
	button,
	.tube,
	.band,
	.stream {
		transition: none;
	}

	.tube {
		animation: none;
	}
}
