/*
 * Chrome, layout, and every rule that draws a ring.
 *
 * This is the half of the colour work a stylesheet can express. The other half
 * -- "this ring is the fourth colour" -- is set per element from js/ui/palette.js
 * as the --ring custom property, and every ring rule below derives from it.
 *
 * Deep indigo rather than paper. Ten colours have to sit next to each other and
 * stay ten colours; a dark, desaturated ground is what lets them.
 *
 * Sizes are set from script, not here. layout.js measures the board and every
 * peg, ring and tap target is given its width, height and transform in pixels,
 * because the arrangement has to be solved as a whole -- ring size, stack height
 * and the space between pegs are one answer, not three declarations.
 */

:root {
	--night: #161a33;
	--night-soft: #1f2542;
	--platter: #2b3157;
	--platter-edge: #3b4370;

	--ink: #eef1ff;
	--ink-soft: #9aa3cf;
	--ink-faint: #5f678f;

	--post: #6b74a8;
	--post-dark: #464d78;

	--accent: #ffb454;
	--accent-deep: #e8952f;

	--radius-control: 14px;
	--radius-card: 22px;

	color-scheme: dark;
}

* {
	box-sizing: border-box;
}

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

body {
	background: var(--night);
	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 loop
	   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: 3.1rem;
	padding: 0.5rem 0.875rem;
	border: 2px solid var(--platter-edge);
	border-radius: var(--radius-control);
	background: var(--night-soft);
	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.6rem;
	border-color: var(--accent-deep);
	background: var(--accent);
	color: #33240c;
	font-size: 1.1em;
}

button.quiet {
	min-height: 2.4rem;
	padding-inline: 0.7rem;
	border-color: transparent;
	background: none;
	color: var(--ink-soft);
	font-size: 0.85em;
}

@media (hover: hover) {
	button:hover:not(:disabled) {
		background: var(--platter);
		border-color: var(--post-dark);
	}

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

button:active:not(:disabled) {
	background: var(--platter-edge);
}

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

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

button:disabled {
	border-color: #2a3055;
	background: #1b203c;
	color: var(--ink-faint);
	cursor: default;
}

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

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

.emblem {
	display: block;
	width: min(100%, 20rem);
	height: auto;
	margin-inline: auto;
	/* The one thing on the menu that can give on a short screen. A flex item is
	   never squeezed below its own height without this. */
	min-height: 0;
}

.emblem-track {
	fill: none;
	stroke: var(--platter-edge);
	stroke-width: 13;
}

.emblem-posts rect {
	fill: var(--post);
}

/* Stroked rather than filled, so the post shows through the hole -- the same
   trick the board itself uses on a real ring. */
.emblem-rings rect {
	fill: none;
	stroke: var(--ring);
	stroke-width: 6.5;
}

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

.subtitle {
	margin: 0;
	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: 3rem;
	padding-inline: 0.375rem;
}

/*
 * The shape is two short lines rather than one that wraps: at three buttons to a
 * phone's width, "10 colours, 4 high" breaks on some phones and not others, and
 * the three buttons stop matching. All of these 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 .shape {
	font-size: 0.7em;
	line-height: 1.2;
	opacity: 0.6;
}

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

.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 emblem has shrunk to nothing
 * by this height anyway, so it goes, and with it the tag line and both headings:
 * three buttons reading Easy, Medium and Hard say what they are, and the group
 * keeps its label for a screen reader.
 */
@media (max-height: 34rem) {
	.emblem,
	.subtitle,
	.start-at h2,
	.stats h2 {
		display: none;
	}

	.menu {
		gap: 0.375rem;
	}

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

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

.header-row {
	display: flex;
	align-items: center;
	gap: 0.5rem;
	font-size: 0.9em;
}

.level {
	flex: 1;
	font-weight: 600;
}

.moves,
.time {
	color: var(--ink-soft);
	font-variant-numeric: tabular-nums;
}

.time::before {
	content: "·";
	margin-inline: 0.35rem;
	color: var(--ink-faint);
}

/*
 * The board takes whatever is left between the header and the toolbar and hands
 * the number to layout.js. min-height: 0 is what stops a flex item from refusing
 * to shrink below the size of its content -- which here would be a board that
 * pushes the toolbar off a short screen.
 */
.board {
	position: relative;
	flex: 1 1 auto;
	min-height: 0;
	/* Rings stack among themselves with z-indexes in the hundreds -- see
	   LoopView -- and without a stacking context of their own they would sort
	   against the result card and win. */
	isolation: isolate;
	-webkit-tap-highlight-color: transparent;
	-webkit-touch-callout: none;
}

.loop-track {
	position: absolute;
	inset: 0;
	width: 100%;
	height: 100%;
}

.loop-track rect {
	fill: none;
	stroke: var(--platter);
	stroke-linejoin: round;
}

.layer {
	position: absolute;
	inset: 0;
}

.layer > * {
	position: absolute;
	top: 0;
	left: 0;
}

/* --- Pegs ---------------------------------------------------------------- */

.peg-base {
	border-radius: 50%;
	background: var(--post-dark);
}

.peg-post {
	border-radius: 999px;
	/* Lit from the left, so a flat rectangle reads as a round post. */
	background: linear-gradient(to right, var(--post-dark), var(--post) 45%, var(--post-dark));
	transition: filter 200ms ease-out;
}

.peg-post.is-done {
	filter: brightness(1.35);
}

.peg-post.is-fresh {
	animation: arrive 520ms ease-out;
}

.peg-hit {
	min-height: 0;
	padding: 0;
	border: none;
	border-radius: 999px;
	background: none;
	/* Transparent, and on top of everything: which peg a tap belongs to is a
	   question about the arrangement, not about which ring happened to be under
	   the finger. */
}

.peg-hit.is-held {
	background: rgb(255 180 84 / 9%);
}

@media (hover: hover) {
	.peg-hit:hover {
		background: rgb(255 255 255 / 5%);
	}
}

.peg-hit:focus-visible {
	outline: 3px solid var(--accent);
	outline-offset: -3px;
}

/* --- Rings --------------------------------------------------------------- */

/*
 * A ring is a torus seen from above: a rounded box with a hole through it. The
 * hole is genuinely transparent, so the post passes through it and the platter
 * shows in the rest -- which is what makes a stack read as rings on a peg rather
 * than stripes on a bar. Border widths come from script, because the sides show
 * the whole tube and the top and bottom show it edge on.
 */
.ring {
	border-style: solid;
	border-color: var(--ring);
	border-radius: 999px;
	background: transparent;
	box-shadow: 0 2px 4px rgb(0 0 0 / 32%);
	will-change: transform;
	pointer-events: none;
}

.ring.is-done {
	filter: brightness(1.12) saturate(1.1);
}

.ring.is-refused {
	animation: refuse 320ms ease-in-out;
}

.is-won .ring {
	animation: settle 640ms var(--settle-delay, 0ms) ease-out;
}

.peg-post.is-refused,
.peg-base.is-refused {
	animation: refuse 320ms ease-in-out;
}

/*
 * These move the element with the individual translate and scale properties, not
 * with transform. Position is a transform set from script, and an animation on
 * the same property would throw the ring back to the middle of the board for the
 * length of the shake.
 */
@keyframes refuse {
	0%, 100% { translate: 0; }
	20% { translate: -7%; }
	55% { translate: 7%; }
	80% { translate: -3%; }
}

@keyframes settle {
	0% { scale: 1; }
	35% { scale: 1.14 1.3; }
	100% { scale: 1; }
}

@keyframes arrive {
	0% { scale: 1 0.1; opacity: 0; }
	100% { scale: 1; opacity: 1; }
}

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

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

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

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

.toolbar button {
	padding-inline: 0.25rem;
	font-size: 0.95em;
}

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

.overlay {
	position: absolute;
	inset: 0;
	z-index: 3;
	display: grid;
	padding: 1.25rem;
	background: rgb(22 26 51 / 72%);
	place-items: center;
}

.overlay[hidden] {
	display: none;
}

.card {
	display: flex;
	flex-direction: column;
	gap: 1rem;
	width: 100%;
	padding: 2rem 2.25rem;
	border: 2px solid var(--platter-edge);
	border-radius: var(--radius-card);
	background: var(--night-soft);
	text-align: center;
}

.card h2 {
	margin: 0;
	font-size: clamp(1.7rem, 8vw, 2.3rem);
	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,
	.peg-post {
		transition: none;
	}

	.ring.is-refused,
	.is-won .ring,
	.peg-post.is-fresh,
	.peg-post.is-refused,
	.peg-base.is-refused {
		animation: none;
	}
}
