/* --- Fonts (self-hosted, SIL Open Font License) --- */
@font-face {
	font-family: "Geist";
	src: url("fonts/geist-latin.woff2") format("woff2");
	font-weight: 100 900;
	font-style: normal;
	font-display: swap;
}

@font-face {
	font-family: "Geist Mono";
	src: url("fonts/geist-mono-latin.woff2") format("woff2");
	font-weight: 100 900;
	font-style: normal;
	font-display: swap;
}

/* --- Tokens --- */
:root {
	color-scheme: dark;

	--bg: #0b0c0f;
	--surface: #111317;
	--surface-2: #181b21;
	--surface-3: #20242b;
	--line: rgba(255, 255, 255, 0.07);
	--line-strong: rgba(255, 255, 255, 0.13);

	--text: #ececef;
	--text-muted: #9ca1ab;
	--text-faint: #7d828c;

	--accent: #f2b544;
	--accent-hover: #f6c465;
	--accent-ink: #1c1405;
	--accent-soft: rgba(242, 181, 68, 0.12);
	--danger: #f27272;

	/* Canvas drawing colors (read by script.js) */
	--canvas-grid: rgba(255, 255, 255, 0.055);
	--canvas-edge: rgba(255, 255, 255, 0.22);
	--point-uncolored: #c6c9d1;

	--radius: 10px;
	--font-sans: "Geist", ui-sans-serif, system-ui, sans-serif;
	--font-mono: "Geist Mono", ui-monospace, "SFMono-Regular", monospace;

	--ease-out: cubic-bezier(0.23, 1, 0.32, 1);
}

/* --- Base --- */
* {
	box-sizing: border-box;
}

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

body {
	background: var(--bg);
	color: var(--text);
	font-family: var(--font-sans);
	font-size: 14px;
	line-height: 1.5;
	-webkit-font-smoothing: antialiased;
	display: flex;
	flex-direction: column;
}

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

/* --- Top bar --- */
.topbar {
	flex: none;
	height: 56px;
	padding: 0 24px;
	display: flex;
	align-items: center;
	justify-content: space-between;
	border-bottom: 1px solid var(--line);
}

.topbar h1 {
	margin: 0;
	font-size: 16px;
	font-weight: 600;
	letter-spacing: -0.01em;
}

.topbar-actions {
	display: flex;
	align-items: center;
	gap: 20px;
}

.topbar-link {
	padding: 0;
	border: 0;
	background: none;
	color: var(--text-muted);
	font: inherit;
	font-size: 13px;
	text-decoration: none;
	cursor: pointer;
	transition: color 150ms ease;
}

@media (hover: hover) and (pointer: fine) {
	.topbar-link:hover {
		color: var(--text);
	}
}

/* --- Layout --- */
.layout {
	flex: 1;
	min-height: 0;
	display: grid;
	grid-template-columns: minmax(0, 1fr) 380px;
}

.stage {
	min-width: 0;
	min-height: 0;
	padding: 24px;
	display: flex;
	align-items: center;
	justify-content: center;
}

/* Toolbar + canvas share one width: as large as fits beside the panel and
   below the top bar (56px), stage padding (48px) and toolbar (46px). */
.stage-inner {
	width: min(100%, calc(100dvh - 56px - 48px - 46px), 760px);
	display: flex;
	flex-direction: column;
	gap: 10px;
}

.canvas-frame {
	position: relative;
	width: 100%;
	aspect-ratio: 1;
	border: 1px solid var(--line);
	border-radius: var(--radius);
	background: var(--surface);
	overflow: hidden;
}

#canvas {
	display: block;
	width: 100%;
	height: 100%;
	touch-action: none; /* Let touch drags draw circles instead of scrolling */
}

#canvas.circle-mode {
	cursor: crosshair;
}

.canvas-hint {
	position: absolute;
	left: 50%;
	bottom: 16px;
	margin: 0;
	padding: 6px 12px;
	border: 1px solid var(--line-strong);
	border-radius: 999px;
	background: rgba(11, 12, 15, 0.8);
	color: var(--text-muted);
	font-size: 13px;
	white-space: nowrap;
	pointer-events: none;
	opacity: 0;
	transform: translate(-50%, 4px);
	transition: opacity 200ms ease, transform 200ms var(--ease-out);
}

.canvas-hint.is-visible {
	opacity: 1;
	transform: translate(-50%, 0);
}

/* --- Canvas toolbar --- */
.toolbar {
	display: flex;
	flex-wrap: wrap;
	align-items: center;
	gap: 8px;
}

.segmented {
	display: flex;
	padding: 3px;
	gap: 2px;
	border: 1px solid var(--line);
	border-radius: var(--radius);
	background: var(--surface);
}

.tool {
	height: 28px;
	padding: 0 12px;
	border: 1px solid transparent;
	border-radius: 7px;
	background: transparent;
	color: var(--text-muted);
	font: inherit;
	font-size: 13px;
	font-weight: 500;
	white-space: nowrap;
	cursor: pointer;
	transition: background-color 150ms ease, border-color 150ms ease,
		color 150ms ease, transform 160ms var(--ease-out);
}

/* Standalone tools get the same outer height as the segmented group */
.tool-solo {
	height: 36px;
	border-color: var(--line);
	border-radius: var(--radius);
	background: var(--surface);
}

.tool-end {
	margin-left: auto;
}

/* Rounds stepper: replaces Triangulation and Clear while viewing rounds */
.stepper {
	align-items: center;
}

.stepper[hidden],
.toolbar.is-rounds #toolTriangulation,
.toolbar.is-rounds #toolClear {
	display: none;
}

.tool-icon {
	width: 28px;
	padding: 0;
	font-size: 18px;
	line-height: 1;
}

.stepper-label {
	min-width: 104px;
	padding: 0 6px;
	text-align: center;
	font-family: var(--font-mono);
	font-size: 12px;
	font-variant-numeric: tabular-nums;
	color: var(--text);
}

.tool[aria-pressed="true"] {
	background: var(--accent-soft);
	border-color: var(--accent);
	color: var(--accent);
}

.tool:active:not(:disabled) {
	transform: scale(0.97);
}

.tool:disabled {
	cursor: not-allowed;
	color: var(--text-faint);
	opacity: 0.6;
}

@media (hover: hover) and (pointer: fine) {
	.tool:hover:not(:disabled):not([aria-pressed="true"]) {
		background: var(--surface-2);
		color: var(--text);
	}
}

/* --- Panel --- */
.panel {
	min-height: 0;
	overflow-y: auto; /* Scrolls only when "How it works" is open on short screens */
	display: flex;
	flex-direction: column;
	border-left: 1px solid var(--line);
	background: var(--surface);
}

.intro {
	padding: 20px 24px;
	color: var(--text-muted);
}

.intro p {
	margin: 0;
	max-width: 65ch;
}

/* --- How it works --- */
.how {
	margin-top: 10px;
}

.how summary {
	width: fit-content;
	color: var(--accent);
	font-weight: 500;
	cursor: pointer;
	list-style: none;
	display: flex;
	align-items: center;
	gap: 6px;
	border-radius: 4px;
}

.how summary::-webkit-details-marker {
	display: none;
}

/* Chevron built from a bordered square, rotates when open */
.how summary::after {
	content: "";
	width: 6px;
	height: 6px;
	border-right: 1.5px solid currentColor;
	border-bottom: 1.5px solid currentColor;
	transform: translateY(-2px) rotate(45deg);
	transition: transform 200ms var(--ease-out);
}

.how[open] summary::after {
	transform: translateY(1px) rotate(-135deg);
}

@media (hover: hover) and (pointer: fine) {
	.how summary:hover {
		color: var(--accent-hover);
	}
}

.how-body {
	margin: 10px 0 0;
	padding-left: 18px;
	display: flex;
	flex-direction: column;
	gap: 6px;
	max-width: 65ch;
}

.how-body li::marker {
	color: var(--text-faint);
	font-family: var(--font-mono);
	font-size: 12px;
}

@media (prefers-reduced-motion: no-preference) {
	.how[open] .how-body {
		animation: settle-in 220ms var(--ease-out);
	}
}

.group {
	padding: 20px 24px;
	border-top: 1px solid var(--line);
	display: flex;
	flex-direction: column;
	gap: 12px;
}

label {
	font-weight: 500;
}

.field-row {
	display: grid;
	grid-template-columns: minmax(0, 1fr) auto;
	gap: 8px;
}

input[type="number"] {
	width: 100%;
	height: 40px;
	padding: 0 12px;
	border: 1px solid var(--line-strong);
	border-radius: var(--radius);
	background: var(--bg);
	color: var(--text);
	font-family: var(--font-mono);
	font-size: 14px;
	transition: border-color 150ms ease;
}

input[type="number"]::placeholder {
	color: var(--text-faint);
}

input[type="number"]:focus-visible {
	outline: none;
	border-color: var(--accent);
	box-shadow: 0 0 0 3px var(--accent-soft);
}

input[aria-invalid="true"] {
	border-color: var(--danger);
}

.helper {
	margin: -4px 0 0;
	color: var(--text-faint);
	font-size: 13px;
}

.helper.is-error {
	color: var(--danger);
}

/* --- Buttons --- */
.btn {
	height: 40px;
	padding: 0 16px;
	border: 1px solid transparent;
	border-radius: var(--radius);
	font: inherit;
	font-weight: 500;
	white-space: nowrap;
	cursor: pointer;
	transition: background-color 150ms ease, border-color 150ms ease,
		color 150ms ease, transform 160ms var(--ease-out);
}

.btn:active:not(:disabled) {
	transform: scale(0.97);
}

.btn:disabled,
.btn:disabled[aria-pressed] {
	cursor: not-allowed;
	background: var(--surface-2);
	border-color: var(--line);
	color: var(--text-faint);
}

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

.btn-neutral {
	background: var(--surface-3);
	border-color: var(--line-strong);
	color: var(--text);
}

@media (hover: hover) and (pointer: fine) {
	.btn-primary:hover:not(:disabled) {
		background: var(--accent-hover);
	}

	.btn-neutral:hover:not(:disabled) {
		background: var(--surface-2);
		border-color: rgba(255, 255, 255, 0.2);
	}
}

/* --- Stats --- */
.stats {
	display: grid;
	grid-template-columns: 1fr 1fr;
	gap: 8px;
}

.stat {
	padding: 12px 14px;
	border: 1px solid var(--line);
	border-radius: var(--radius);
	background: var(--bg);
	display: flex;
	flex-direction: column;
	gap: 2px;
}

.stat-label {
	color: var(--text-muted);
	font-size: 12px;
}

.stat-value-row {
	display: flex;
	align-items: center;
	gap: 8px;
}

.stat-value {
	display: inline-block;
	font-family: var(--font-mono);
	font-size: 22px;
	font-weight: 500;
	font-variant-numeric: tabular-nums;
	letter-spacing: -0.02em;
}

.stat-value.conflict {
	color: var(--danger);
}

.summary {
	margin: 0;
	color: var(--text);
}

.summary strong {
	font-family: var(--font-mono);
	font-weight: 500;
	color: var(--accent);
}

.summary[hidden] {
	display: none;
}

.note {
	margin: -4px 0 0;
	color: var(--danger);
	font-size: 13px;
}

.note[hidden] {
	display: none;
}

/* Color dots are always round; everything else uses --radius */
.swatch {
	flex: none;
	width: 10px;
	height: 10px;
	border-radius: 50%;
	box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.12);
}

.swatch[hidden] {
	display: none;
}

/* --- Points table --- */
.table-group {
	flex: 1;
	min-height: 140px;
	padding-bottom: 0;
}

.table-scroll {
	flex: 1;
	min-height: 0;
	overflow-y: auto;
	margin: 0 -24px;
	padding: 0 24px 20px;
}

table {
	width: 100%;
	table-layout: fixed;
	border-collapse: collapse;
	font-family: var(--font-mono);
	font-size: 13px;
	font-variant-numeric: tabular-nums;
}

th {
	position: sticky;
	top: 0;
	padding: 8px 0;
	background: var(--surface);
	color: var(--text-muted);
	font-family: var(--font-sans);
	font-size: 12px;
	font-weight: 500;
	text-align: left;
	border-bottom: 1px solid var(--line);
}

td {
	padding: 6px 0;
	color: var(--text);
}

th:nth-child(1) {
	width: 22%;
}

th:nth-child(2),
th:nth-child(3) {
	width: 18%;
}

td:first-child {
	color: var(--text-faint);
}

tbody tr.is-hovered td {
	background: var(--surface-3);
}

.color-cell {
	display: flex;
	align-items: center;
	gap: 8px;
}

.table-empty {
	margin: 16px 0 0;
	color: var(--text-faint);
}

.table-empty[hidden] {
	display: none;
}

/* --- Credit --- */
.credit {
	flex: none;
	padding: 12px 24px;
	border-top: 1px solid var(--line);
	color: var(--text-faint);
	font-size: 12px;
}

/* --- Guide dialog (first visit) --- */
.guide {
	width: min(560px, calc(100% - 32px));
	max-height: calc(100dvh - 32px);
	padding: 0;
	border: 1px solid var(--line-strong);
	border-radius: var(--radius);
	background: var(--surface);
	color: var(--text);
	box-shadow: 0 24px 64px rgba(0, 0, 0, 0.5);
	/* Enter: fade + slight scale from center. Exit is faster than enter. */
	opacity: 1;
	transform: scale(1);
	transition: opacity 200ms var(--ease-out), transform 200ms var(--ease-out),
		overlay 200ms allow-discrete, display 200ms allow-discrete;
}

.guide:not([open]) {
	opacity: 0;
	transform: scale(0.96);
	transition-duration: 140ms;
}

@starting-style {
	.guide[open] {
		opacity: 0;
		transform: scale(0.96);
	}
}

.guide::backdrop {
	background: rgba(5, 6, 8, 0.7);
	opacity: 1;
	transition: opacity 200ms ease, overlay 200ms allow-discrete,
		display 200ms allow-discrete;
}

.guide:not([open])::backdrop {
	opacity: 0;
	transition-duration: 140ms;
}

@starting-style {
	.guide[open]::backdrop {
		opacity: 0;
	}
}

/* Header and footer stay put; only the body scrolls on short screens */
.guide-card {
	max-height: calc(100dvh - 32px);
	display: flex;
	flex-direction: column;
}

.guide-header {
	flex: none;
	padding: 20px 24px 12px;
	display: flex;
	align-items: flex-start;
	justify-content: space-between;
	gap: 16px;
}

.guide h2 {
	margin: 4px 0 0;
	font-size: 18px;
	font-weight: 600;
	letter-spacing: -0.01em;
}

.guide-body {
	flex: 1;
	min-height: 0;
	overflow-y: auto;
	padding: 0 24px 4px;
	color: var(--text-muted);
}

.guide-body p {
	margin: 0 0 10px;
}

.guide-lead {
	color: var(--text);
}

.guide h3 {
	margin: 20px 0 6px;
	color: var(--text);
	font-size: 14px;
	font-weight: 600;
}

.guide-body strong {
	color: var(--text);
	font-weight: 500;
}

.guide-steps,
.guide-list {
	margin: 0 0 10px;
	padding-left: 20px;
	display: flex;
	flex-direction: column;
	gap: 6px;
}

.guide-steps li::marker {
	color: var(--accent);
	font-family: var(--font-mono);
	font-size: 12px;
}

.guide-list li::marker {
	color: var(--accent);
}

.guide-footer {
	flex: none;
	padding: 16px 24px 20px;
	border-top: 1px solid var(--line);
	display: flex;
	align-items: center;
	justify-content: space-between;
	gap: 16px;
}

.guide-hint {
	color: var(--text-faint);
	font-size: 12px;
}

.guide-footer .btn {
	padding: 0 24px;
}

.guide-close {
	flex: none;
	margin: -2px -8px 0 0;
	width: 32px;
	height: 32px;
	border: 1px solid transparent;
	border-radius: var(--radius);
	background: transparent;
	color: var(--text-muted);
	font-size: 22px;
	line-height: 1;
	cursor: pointer;
	transition: background-color 150ms ease, color 150ms ease,
		transform 160ms var(--ease-out);
}

.guide-close:active {
	transform: scale(0.94);
}

@media (hover: hover) and (pointer: fine) {
	.guide-close:hover {
		background: var(--surface-2);
		color: var(--text);
	}
}

/* --- Load-in: panel sections settle in once, staggered --- */
@media (prefers-reduced-motion: no-preference) {
	.panel > * {
		animation: settle-in 420ms var(--ease-out) both;
		animation-delay: calc(var(--i, 0) * 60ms);
	}

	.stage-inner {
		animation: settle-in 420ms var(--ease-out) both;
	}
}

@keyframes settle-in {
	from {
		opacity: 0;
		transform: translateY(8px);
	}
}

@media (prefers-reduced-motion: reduce) {
	.guide,
	.guide:not([open]) {
		transform: none;
	}

	@starting-style {
		.guide[open] {
			transform: none;
		}
	}

	.guide-close:active,
	.tool:active:not(:disabled),
	.btn:active:not(:disabled) {
		transform: none;
	}

	.canvas-hint {
		transform: translate(-50%, 0);
	}
}

/* --- Small screens: stack the canvas above the controls --- */
@media (max-width: 900px) {
	body {
		height: auto;
		min-height: 100dvh;
	}

	.topbar {
		padding: 0 16px;
	}

	.layout {
		grid-template-columns: minmax(0, 1fr);
	}

	.stage {
		padding: 16px;
	}

	.stage-inner {
		width: 100%;
	}

	.panel {
		border-left: none;
		border-top: 1px solid var(--line);
	}

	.intro,
	.group,
	.credit {
		padding-left: 16px;
		padding-right: 16px;
	}

	.guide-header,
	.guide-body,
	.guide-footer {
		padding-left: 20px;
		padding-right: 20px;
	}

	.table-scroll {
		max-height: 360px;
		margin: 0 -16px;
		padding: 0 16px 16px;
	}
}
