/*
 * Phones and tablets.
 *
 * Everything here is scoped to `html.is-touch` (set by js/touch/touch-mode.js),
 * so a desktop never matches a rule in this file. Three parts:
 *
 *   1. The thumb controls. Same register as the HUD: white frosted surfaces,
 *      hairline borders, ink labels in the condensed voice. Pressed is ink.
 *      Nothing glows; the world is light and toy-like and the controls should
 *      read as part of the scoreboard, not as an arcade overlay.
 *   2. The race HUD rearranged for a landscape phone, so nothing sits where a
 *      thumb rests: readouts and map top-left, standings top-right, the lower
 *      half left to the thumbs.
 *   3. Bigger targets and tighter rails in the menus, and the rotate prompt.
 *
 * Safe-area insets keep all of it clear of the notch and the home indicator.
 */

:root {
	--safe-top: env(safe-area-inset-top, 0px);
	--safe-right: env(safe-area-inset-right, 0px);
	--safe-bottom: env(safe-area-inset-bottom, 0px);
	--safe-left: env(safe-area-inset-left, 0px);
	--tc-surface: rgba(255, 255, 255, 0.72);
	--tc-line: rgba(23, 27, 34, 0.16);
}

/* ─── Browser behaviour ─────────────────────────────────── */

/* No double-tap zoom, no grey tap flash, no text selection on a long press. */
html.is-touch,
html.is-touch body {
	touch-action: manipulation;
	-webkit-tap-highlight-color: transparent;
	-webkit-touch-callout: none;
	-webkit-user-select: none;
	user-select: none;
	overscroll-behavior: none;
}

html.is-touch input {
	-webkit-user-select: text;
	user-select: text;
}

/* ─── Thumb controls ────────────────────────────────────── */

.touch-controls {
	position: fixed;
	inset: 0;
	z-index: 30;
	pointer-events: none;
	font-family: var(--font);
	color: var(--ink);
	-webkit-user-select: none;
	user-select: none;
	-webkit-touch-callout: none;
}

.touch-controls[hidden] { display: none; }

/*
 * The steering zone: the lower-left of the screen. Large on purpose, so the
 * thumb never has to find a target, and stopping short of the readouts.
 */
.tc-zone {
	position: absolute;
	left: 0;
	bottom: 0;
	width: 50%;
	height: 56%;
	pointer-events: auto;
	touch-action: none;
}

.tc-stick,
.tc-pad {
	position: absolute;
	/* Resting place, until a thumb lands and the control moves under it. */
	left: calc(var(--safe-left) + 104px);
	top: calc(100% - var(--safe-bottom) - 96px);
	display: flex;
	align-items: center;
	justify-content: center;
	background: rgba(23, 27, 34, 0.12);
	border: 1px solid rgba(255, 255, 255, 0.75);
	opacity: 0.7;
	transform: translate(-50%, -50%);
	transition: opacity 0.12s;
}

.tc-stick {
	width: 128px;
	height: 128px;
	border-radius: 50%;
}

.tc-pad {
	width: 160px;
	height: 52px;
	border-radius: 26px;
}

.tc-zone.is-down .tc-stick,
.tc-zone.is-down .tc-pad { opacity: 1; transition: none; }

.tc-knob {
	width: 48px;
	height: 48px;
	border-radius: 50%;
	background: var(--surface);
	border: 1px solid var(--tc-line);
	box-shadow: 0 2px 8px rgba(16, 20, 28, 0.18);
}

.touch-controls[data-scheme='stick'] .tc-pad { display: none; }
.touch-controls[data-scheme='pedals'] .tc-stick { display: none; }

/* Buttons: the HUD's frosted card, at thumb size. */
.tc-btn {
	display: flex;
	align-items: center;
	justify-content: center;
	padding: 0;
	font: inherit;
	font-stretch: var(--wd-display);
	font-size: 12px;
	font-weight: 800;
	letter-spacing: 0.16em;
	text-transform: uppercase;
	color: var(--ink);
	background: var(--tc-surface);
	border: 1px solid var(--tc-line);
	border-radius: 14px;
	backdrop-filter: blur(10px);
	-webkit-backdrop-filter: blur(10px);
	pointer-events: auto;
	touch-action: none;
	cursor: default;
}

.tc-btn.is-down {
	color: #fff;
	background: var(--ink);
	border-color: var(--ink);
}

.tc-btn[hidden] { display: none; }

.tc-cluster {
	position: absolute;
	right: calc(var(--safe-right) + 18px);
	bottom: calc(var(--safe-bottom) + 16px);
	display: grid;
	grid-template-areas:
		'.     fire'
		'brake gas';
	gap: 12px;
	align-items: end;
	justify-items: end;
}

.tc-gas { grid-area: gas; width: 88px; height: 112px; }
.tc-brake { grid-area: brake; width: 76px; height: 76px; }
.tc-fire { grid-area: fire; width: 88px; height: 52px; border-radius: 26px; }

/* With the stick the throttle is automatic, so Brake takes the Gas position. */
.touch-controls[data-scheme='stick'] .tc-gas { display: none; }
.touch-controls[data-scheme='stick'] .tc-brake { grid-area: gas; width: 88px; height: 88px; }

/* Nothing to fire: present, so it does not jump in when armed, but quiet. */
.tc-fire:not(.is-armed) { opacity: 0.4; }
.tc-fire.is-armed { background: var(--surface); border-color: var(--ink); }

/* Pause and the steering switch: top centre, out of both thumbs' way. */
.tc-top {
	position: absolute;
	top: calc(var(--safe-top) + 10px);
	left: 50%;
	transform: translateX(-50%);
	display: flex;
	gap: 8px;
}

.tc-small {
	height: 44px;
	min-width: 44px;
	padding: 0 14px;
	border-radius: 22px;
	font-size: 11px;
}

.tc-pause { padding: 0; }

.tc-pause svg {
	width: 18px;
	height: 18px;
	fill: currentColor;
}

/* ─── Race HUD on a phone ───────────────────────────────── */

html.is-touch .hud-primary {
	top: calc(var(--safe-top) + 10px);
	left: calc(var(--safe-left) + 10px);
	border-radius: 12px;
}

html.is-touch .hud-cell {
	padding: 6px 12px 7px;
	min-width: 56px;
}

html.is-touch .hud-cell .k { font-size: 9px; margin-bottom: 0; }
html.is-touch .hud-cell .v { font-size: 20px; }
html.is-touch .hud-cell .v small { font-size: 12px; }

/* The map goes under the readouts, which leaves the lower left to the thumb. */
html.is-touch .minimap {
	top: calc(var(--safe-top) + 72px);
	left: calc(var(--safe-left) + 10px);
	right: auto;
	bottom: auto;
	padding: 4px;
	border-radius: 12px;
}

html.is-touch .minimap canvas {
	width: 96px !important;
	height: 96px !important;
}

/* Standings: the leaders and you, small, top-right. POS already says where you are. */
html.is-touch .hud-standings {
	top: calc(var(--safe-top) + 10px);
	right: calc(var(--safe-right) + 10px);
	left: auto;
	bottom: auto;
	width: 168px;
	border-radius: 12px;
}

html.is-touch .hud-standings ol { display: block; padding: 4px; overflow: visible; }

html.is-touch .hud-standings li {
	padding: 4px 8px;
	gap: 8px;
	font-size: 12px;
}

html.is-touch .hud-standings li:nth-child(n + 4):not(.is-you) { display: none; }

html.is-touch .hud-banner {
	bottom: calc(var(--safe-bottom) + 14px);
	padding: 9px 16px;
	font-size: 13px;
}

/* Lights drop below the pause button; the toast clears the top row. */
html.is-touch .lights { top: 30%; padding: 12px 16px; gap: 10px; }
html.is-touch .lights .bulb { width: 30px; height: 30px; }
html.is-touch .lights-caption { top: calc(30% + 64px); }
html.is-touch .toast { top: calc(var(--safe-top) + 62px); }

/* ─── Menus ─────────────────────────────────────────────── */

/* Clear of the notch on a phone held sideways. */
html.is-touch .rail {
	padding-left: calc(var(--safe-left) + 16px);
	padding-right: calc(var(--safe-right) + 16px);
}

html.is-touch .rail--foot { padding-bottom: var(--safe-bottom); }

html.is-touch .stage-grid {
	padding-left: calc(var(--safe-left) + 16px);
	padding-right: calc(var(--safe-right) + 16px);
}

html.is-touch .pane-tabs {
	padding-left: calc(var(--safe-left) + 16px);
	padding-right: calc(var(--safe-right) + 16px);
}

/* Every target at least 44px: Apple's minimum, and a thumb's real size. */
html.is-touch .button--rail,
html.is-touch .button--mini,
html.is-touch .pane-tabs button,
html.is-touch .segmented button {
	min-height: 44px;
}

html.is-touch .segmented button { height: auto; min-width: 44px; }

/* A phone held sideways is short: the rails give up height before the panels. */
@media (max-height: 520px) {

	html.is-touch .rail { min-height: 52px; padding-top: 4px; padding-bottom: 4px; }
	html.is-touch .rail--foot { min-height: 56px; padding-bottom: calc(var(--safe-bottom) + 4px); }
	html.is-touch .rail-title { display: none; }
	html.is-touch .pane-tabs { padding-top: 6px; }
	html.is-touch .stage-grid { padding-top: 8px; padding-bottom: 8px; }

	/* The panel scrolls inside itself rather than clipping a setting. */
	html.is-touch .panel { overflow-y: auto; }

	/* Home and join: the foot rail is only a tagline, and the form needs it. */
	html.is-touch #screen-home .rail--foot,
	html.is-touch #screen-join .rail--foot { display: none; }

	html.is-touch .entry-panel { padding: 16px 18px; }
	html.is-touch .entry-panel h2 { display: none; }
	html.is-touch .entry-panel .field { margin-bottom: 12px; }
	html.is-touch .entry-panel .divider { margin: 10px 0; }
	html.is-touch .entry-panel .room-code { margin-bottom: 12px; }

	/* Your car: the tab already names the pane, and the class control shares
	   its row with its label, so the picker keeps its full height. */
	html.is-touch .viewport-top { padding: 8px 12px; }
	html.is-touch .viewport-top .panel-head { display: none; }
	html.is-touch .viewport-foot { padding: 8px 12px; }
	html.is-touch .car-card { width: 78px; }

	/* Results: a shorter podium, so the classification under it shows too. */
	html.is-touch .podium-slot { min-height: 0; padding: 10px 14px; }
	html.is-touch .podium-place { font-size: 32px; }

	html.is-touch .viewport-top .setting--wide {
		display: flex;
		align-items: center;
		gap: 14px;
	}

	html.is-touch .viewport-top .setting--wide .segmented { flex: 1; margin-top: 0; }

}

/* ─── Rotate prompt ─────────────────────────────────────── */

.rotate-prompt { display: none; }

@media (orientation: portrait) {

	html.is-touch[data-landscape] .rotate-prompt {
		position: fixed;
		inset: 0;
		z-index: 100;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		gap: 18px;
		padding: 24px;
		background: var(--steel);
		color: #fff;
		font-family: var(--font);
		text-align: center;
	}

	/* The race cannot be driven upright, so the controls stand down too. */
	html.is-touch[data-landscape] .touch-controls { display: none; }

}

.rotate-prompt svg {
	width: 56px;
	height: 56px;
	fill: none;
	stroke: currentColor;
	stroke-width: 2;
	stroke-linecap: round;
	stroke-linejoin: round;
}

.rotate-prompt p {
	margin: 0;
	font-stretch: var(--wd-display);
	font-size: 15px;
	font-weight: 800;
	letter-spacing: 0.18em;
	text-transform: uppercase;
}
