/*
 * Widget chrome: buttons, cards, layout, and the tarmac.
 *
 * This is the half of the colour work a stylesheet can express. The other half --
 * "this bus is the red one" -- is set per element from js/ui/palette.js as the
 * --base, --deep and --ink custom properties, and every rule that paints a bus or
 * a passenger derives from those.
 *
 * The chrome is deliberately quiet. Colour is the whole of the puzzle, so the
 * only saturated things on the screen should be the things the player has to
 * match: pale card, dark tarmac, one blue for the controls.
 */

:root {
	--page: #eef1f5;
	--card: #ffffff;

	--ink: #26303d; /* headings and body text */
	--ink-soft: #66748a; /* secondary labels */
	--ink-faint: #a3aebd; /* disabled button text */

	--accent: #3d6ea8;
	--accent-deep: #2b5183;

	--tarmac: #58637a;
	--tarmac-deep: #495266;
	--paint: rgb(255 255 255 / 16%); /* the stall lines on the tarmac */
	--platform: #dbe1e9;
	--platform-edge: #c3ccd8;

	--edge: #dbe1e9; /* button and card borders */
	--edge-hover: #c7d0dc;
	--disabled: #eef1f5;
	--disabled-edge: #e2e7ee;

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

	/* The lot's metrics, measured once by LotView and published here so the
	   stall lines, the gaps and the bus corners all follow one number. */
	--cell: 48px;
	--lot-gap: 4px;
	--lot-pad: 8px;

	/* The waiting line's metrics. One passenger's width plus the gap is the
	   distance everybody walks when the person in front of them boards. */
	--rider: 2rem;
	--rider-gap: 0.3rem;
	--step: calc(var(--rider) + var(--rider-gap));

	color-scheme: light;
}

* {
	box-sizing: border-box;
}

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

body {
	background: var(--page);
	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.05rem);
	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: 30rem;
	height: 100%;
	margin-inline: auto;
	/* A phone's design width on a desktop screen. Wider than this and the lot
	   grows past any reasonable reach without getting easier to read. */
}

.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: 3rem;
	padding: 0.5rem 0.875rem;
	border: 2px solid var(--edge);
	border-radius: var(--radius-control);
	background: var(--card);
	color: var(--ink);
	font: inherit;
	font-weight: 600;
	cursor: pointer;
	-webkit-user-select: none;
	user-select: none;
	-webkit-tap-highlight-color: transparent;
	transition: background-color 90ms ease-out, border-color 90ms ease-out;
}

button.primary {
	min-height: 3.5rem;
	border-color: var(--accent-deep);
	background: var(--accent);
	color: #fff;
	font-size: 1.08em;
}

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

button.quiet.is-armed {
	color: #b23b32;
}

@media (hover: hover) {
	button:hover:not(:disabled) {
		border-color: var(--edge-hover);
		background: #f7f9fc;
	}

	button.primary:hover:not(:disabled) {
		background: var(--accent-deep);
	}
}

button:active:not(:disabled) {
	scale: 0.985;
}

button:focus-visible {
	outline: none;
	border-color: var(--accent-deep);
	box-shadow: 0 0 0 3px rgb(61 110 168 / 30%);
}

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

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

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

.mark-slot {
	display: flex;
	justify-content: center;
}

.mark {
	width: min(15rem, 62%);
	height: auto;
}

.menu h1 {
	margin: 0;
	font-size: clamp(2rem, 9vw, 2.5rem);
	font-weight: 700;
	letter-spacing: -0.015em;
}

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

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

.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;
	padding-inline: 0.375rem;
}

/* Inherits its colour, so it follows the button through hover and press. */
.difficulty .range {
	font-size: 0.75em;
	line-height: 1.2;
	opacity: 0.7;
}

.summary {
	margin: 0.75rem 0 0;
	color: var(--ink-soft);
	font-size: 0.9em;
}

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

.level-chip {
	min-height: 0;
	aspect-ratio: 1;
	padding: 0;
	border-radius: 10px;
	font-variant-numeric: tabular-nums;
}

.level-chip.is-cleared {
	border-color: var(--accent);
	background: #e7effa;
	color: var(--accent-deep);
}

/*
 * A landscape phone, or a short desktop window. The menu is a column of buttons
 * that cannot shrink, so the mark goes first and the title stops taking a tenth
 * of the screen. Standalone installs are locked to portrait, so this is a
 * browser tab.
 */
@media (max-height: 38rem) {
	.menu .mark-slot {
		display: none;
	}

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

	.menu {
		gap: 0.5rem;
	}
}

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

.game {
	/* Room for the anchored Menu button, so the header can never ride up
	   underneath it. */
	padding-top: calc(max(0.75rem, env(safe-area-inset-top)) + 3.25rem);
	gap: 0.5rem;
}

/* Anchored rather than stacked: a way out belongs at the top of the screen, not
   travelling up and down with the play area. */
.corner {
	position: absolute;
	top: max(0.75rem, env(safe-area-inset-top));
	left: 0.875rem;
	min-height: 2.5rem;
	z-index: 2;
}

.header-row {
	display: flex;
	align-items: baseline;
	gap: 0.625rem;
}

.title-block {
	display: flex;
	flex: 1;
	flex-wrap: wrap;
	align-items: baseline;
	gap: 0.4rem;
	min-width: 0;
}

.level {
	font-weight: 700;
}

.level-name {
	color: var(--ink-soft);
	font-size: 0.85em;
}

.tally {
	color: var(--ink-soft);
	font-size: 0.85em;
	font-variant-numeric: tabular-nums;
	white-space: nowrap;
}

.play-area {
	display: flex;
	flex: 1;
	flex-direction: column;
	gap: 0.625rem;
	min-height: 0;
}

/* --- The lot -------------------------------------------------------------- */

/* The frame takes whatever height is left over and the lot is centred in it.
   LotView measures the frame and picks a cell size that fits both ways, so the
   lot is never the thing that pushes the queue off the bottom of the screen. */
.lot-frame {
	display: grid;
	flex: 1;
	place-items: center;
	min-height: 0;
}

.lot {
	display: grid;
	grid-template-columns: repeat(var(--cols, 4), var(--cell));
	grid-template-rows: repeat(var(--rows, 4), var(--cell));
	gap: var(--lot-gap);
	padding: var(--lot-pad);
	border-radius: 16px;
	/* Stall markings, painted rather than drawn: one line down every cell
	   boundary, so they follow the measured cell size for free. */
	background-color: var(--tarmac);
	background-image:
		repeating-linear-gradient(
			to right,
			var(--paint) 0,
			var(--paint) 2px,
			transparent 2px,
			transparent calc(var(--cell) + var(--lot-gap))
		),
		repeating-linear-gradient(
			to bottom,
			var(--paint) 0,
			var(--paint) 2px,
			transparent 2px,
			transparent calc(var(--cell) + var(--lot-gap))
		),
		linear-gradient(160deg, var(--tarmac) 0%, var(--tarmac-deep) 100%);
	background-position: calc(var(--lot-pad) - 3px) calc(var(--lot-pad) - 3px);
	box-shadow: inset 0 1px 0 rgb(255 255 255 / 12%), 0 6px 18px rgb(38 48 61 / 18%);
}

.bus {
	position: relative;
	min-height: 0;
	padding: 0;
	border: 0;
	border-radius: 10px;
	background: none;
	transition: translate 340ms cubic-bezier(0.4, 0, 0.7, 0.2), opacity 340ms ease-in;
}

.bus > .bus-art {
	display: block;
	width: 100%;
	height: 100%;
	/* The drawing carries the shadow, so it survives being squashed to whatever
	   proportions the cell turns out to have. */
	filter: drop-shadow(0 2px 3px rgb(20 26 34 / 35%));
}

.bus:active:not(:disabled) {
	scale: 0.94;
}

.bus:hover:not(:disabled),
.bus:focus-visible {
	background: none;
	box-shadow: none;
}

.bus:focus-visible > .bus-art {
	filter: drop-shadow(0 0 0 2px #fff) drop-shadow(0 2px 3px rgb(20 26 34 / 35%));
}

.bus.is-leaving {
	translate: var(--exit);
	opacity: 0;
	pointer-events: none;
}

.bus.is-refused {
	animation: shake 400ms ease-in-out;
}

.bus.is-blocker > .bus-art {
	animation: blocker 400ms ease-in-out;
}

@keyframes shake {
	0%, 100% { translate: 0 0; }
	20% { translate: -6% 0; }
	45% { translate: 5% 0; }
	70% { translate: -3% 0; }
}

@keyframes blocker {
	0%, 100% { filter: drop-shadow(0 2px 3px rgb(20 26 34 / 35%)); }
	40% { filter: drop-shadow(0 0 7px #fff) brightness(1.25); }
}

/* --- The bays ------------------------------------------------------------- */

.bays {
	display: grid;
	grid-template-columns: repeat(var(--bays, 3), 1fr);
	gap: 0.5rem;
	width: 100%;
	/* Capped against the viewport height as well, so on a short phone the bays
	   give ground to the lot rather than the other way round. */
	max-width: min(15rem, 32vh);
	margin-inline: auto;
}

.bay {
	position: relative;
	aspect-ratio: 5 / 7;
	border: 2px dashed var(--platform-edge);
	border-radius: 12px;
	background: rgb(255 255 255 / 55%);
}

.bay-bus {
	position: absolute;
	inset: 6%;
	transition: translate 380ms ease-in, opacity 380ms ease-in;
}

.bay-bus > .bus-art {
	display: block;
	width: 100%;
	height: 100%;
	filter: drop-shadow(0 2px 3px rgb(20 26 34 / 22%));
}

.bay-bus.is-arriving {
	animation: arrive 260ms ease-out;
}

.bay-bus.is-boarding {
	animation: board 260ms ease-out;
}

.bay-bus.is-leaving {
	translate: 0 -220%;
	opacity: 0;
}

@keyframes arrive {
	from { translate: 0 40%; opacity: 0; }
	to { translate: 0 0; opacity: 1; }
}

@keyframes board {
	0%, 100% { scale: 1; }
	45% { scale: 1.07; }
}

/* --- The waiting line ----------------------------------------------------- */

.queue {
	position: relative;
	height: 3.5rem;
	overflow: hidden;
	/* The line is drawn in full and clipped. Who is next matters far more than
	   who is twentieth, and a line that shrank to fit would make everyone
	   smaller exactly when the level got long. */
	mask-image: linear-gradient(to right, #000 86%, transparent 100%);
}

.platform {
	position: absolute;
	bottom: 0;
	left: 0;
	width: calc(var(--span, 4) * var(--step) - var(--rider-gap) + 0.5rem);
	height: 100%;
	border: 2px solid var(--platform-edge);
	border-radius: 12px;
	background: var(--platform);
}

.platform-label {
	position: absolute;
	top: 0.15rem;
	left: 0.4rem;
	color: var(--ink-soft);
	font-size: 0.6rem;
	font-weight: 700;
	letter-spacing: 0.09em;
	text-transform: uppercase;
}

.rider {
	position: absolute;
	bottom: 0.25rem;
	left: 0;
	width: var(--rider);
	height: var(--rider);
	translate: calc(0.25rem + var(--place, 0) * var(--step)) 0;
	transition: translate 260ms ease-out, opacity 220ms ease-in;
}

.rider-art {
	display: block;
	width: 100%;
	height: 100%;
}

/* Anyone off the platform is waiting their turn and cannot board yet, so they
   are drawn back rather than removed -- the colours coming up are what the next
   few moves have to be planned around. */
.rider:not(.is-at-stop) {
	opacity: 0.5;
	scale: 0.85;
}

.rider.is-boarding {
	translate: calc(0.25rem + var(--place, 0) * var(--step)) -160%;
	opacity: 0;
}

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

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

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

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

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

.overlay {
	position: absolute;
	inset: 0;
	z-index: 3;
	display: grid;
	padding: 1.25rem;
	place-items: center;
	background: rgb(238 241 245 / 72%);
	backdrop-filter: blur(3px);
}

.overlay[hidden] {
	display: none;
}

.card {
	display: flex;
	flex-direction: column;
	gap: 1rem;
	width: 100%;
	padding: 1.75rem 1.75rem 1.5rem;
	border: 2px solid var(--edge);
	border-radius: var(--radius-card);
	background: var(--card);
	box-shadow: 0 12px 32px rgb(38 48 61 / 18%);
	text-align: center;
}

.card .mark {
	width: 60%;
}

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

#result-detail {
	margin: 0;
	color: var(--ink-soft);
}

@media (prefers-reduced-motion: reduce) {
	*,
	*::before,
	*::after {
		animation-duration: 1ms !important;
		animation-iteration-count: 1 !important;
		transition-duration: 1ms !important;
	}
}
