/* ---------- Tokens ---------- */

:root {
	--bg: #f8f5ee;
	--surface: #fffdf8;
	--fg: #26231e;
	--muted: #6f685d;
	--rule: #e3ddd0;
	--accent: #1d7f47;
	--accent-soft: rgb(29 127 71 / 0.08);
	--shadow: 0 1px 2px rgb(38 35 30 / 0.06), 0 4px 16px rgb(38 35 30 / 0.05);

	--serif: "Literata", "Noto Serif", Georgia, serif;
	--sans: "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
	--mono: "JetBrains Mono", "Noto Sans Mono", ui-monospace, monospace;

	--measure: 38rem;
	color-scheme: light;
}

@media (prefers-color-scheme: dark) {
	:root:not([data-theme="light"]) {
		--bg: #15161a;
		--surface: #1c1e23;
		--fg: #e7e2d8;
		--muted: #9a9488;
		--rule: #2e3037;
		--accent: #5ccf8d;
		--accent-soft: rgb(92 207 141 / 0.08);
		--shadow: none;
		color-scheme: dark;
	}
}

:root[data-theme="dark"] {
	--bg: #15161a;
	--surface: #1c1e23;
	--fg: #e7e2d8;
	--muted: #9a9488;
	--rule: #2e3037;
	--accent: #5ccf8d;
	--accent-soft: rgb(92 207 141 / 0.08);
	--shadow: none;
	color-scheme: dark;
}

/* ---------- Base ---------- */

*,
*::before,
*::after {
	box-sizing: border-box;
}

html {
	font-size: 100%;
	-webkit-text-size-adjust: 100%;
}

body {
	margin: 0;
	background: var(--bg);
	color: var(--fg);
	font: 400 1.125rem/1.7 var(--serif);
	font-optical-sizing: auto;
	text-rendering: optimizeLegibility;
	transition:
		background-color 0.2s,
		color 0.2s;
}

a {
	color: var(--accent);
	text-decoration-thickness: 1px;
	text-underline-offset: 0.2em;
}

::selection {
	background: var(--accent);
	color: var(--bg);
}

/* ---------- Top bar ---------- */

.topbar {
	position: sticky;
	top: 0;
	z-index: 10;
	display: flex;
	align-items: center;
	justify-content: space-between;
	gap: 1rem;
	padding: 0.75rem max(1rem, calc((100% - var(--measure)) / 2));
	background: color-mix(in srgb, var(--bg) 85%, transparent);
	backdrop-filter: blur(8px);
	border-bottom: 1px solid var(--rule);
	font: 500 0.875rem/1 var(--sans);
}

.topbar .home {
	color: var(--fg);
	text-decoration: none;
	letter-spacing: 0.08em;
	text-transform: uppercase;
	font-size: 0.75rem;
}

.topbar .home:hover {
	color: var(--accent);
}

.topbar .here {
	color: var(--muted);
	margin-right: auto;
	font-weight: 400;
}

.topbar .here::before {
	content: "/";
	margin-right: 1rem;
	opacity: 0.5;
}

#toggle-colour-scheme {
	display: grid;
	place-items: center;
	width: 2rem;
	height: 2rem;
	padding: 0;
	border: 1px solid var(--rule);
	border-radius: 999px;
	background: transparent;
	color: var(--muted);
	cursor: pointer;
}

#toggle-colour-scheme:hover {
	color: var(--accent);
	border-color: var(--accent);
}

/* Reading progress (where scroll-driven animations are supported) */
.progress {
	position: absolute;
	left: 0;
	bottom: -1px;
	height: 2px;
	width: 100%;
	background: var(--accent);
	transform-origin: left;
	transform: scaleX(0);
}

@supports (animation-timeline: scroll()) {
	.progress {
		animation: grow linear both;
		animation-timeline: scroll(root);
	}
}

@keyframes grow {
	to {
		transform: scaleX(1);
	}
}

/* ---------- Chapter ---------- */

main {
	max-width: var(--measure);
	margin-inline: auto;
	padding: 4rem 1rem 3rem;
}

.chapter h1 {
	margin: 0 0 3.5rem;
	text-align: center;
	font-weight: 300;
	font-size: clamp(2rem, 6vw, 2.75rem);
	line-height: 1.15;
	letter-spacing: -0.01em;
}

.chapter h1::after {
	content: "";
	display: block;
	width: 3rem;
	height: 2px;
	margin: 1.5rem auto 0;
	background: var(--accent);
}

.chapter p {
	margin: 0;
	text-align: justify;
	hyphens: auto;
}

.chapter p + p {
	text-indent: 1.5em;
}

.chapter h1 + p::first-letter {
	float: left;
	font-size: 3.6em;
	line-height: 0.85;
	margin: 0.08em 0.1em 0 0;
	color: var(--accent);
	font-weight: 500;
}

/* System messages */
.chapter blockquote {
	margin: 3rem 0;
	padding: 1rem 1.25rem;
	border: 1px solid color-mix(in srgb, var(--accent) 30%, transparent);
	border-radius: 6px;
	background: var(--accent-soft);
	font: 400 0.8125rem/1.6 var(--mono);
}

.chapter blockquote p {
	text-align: left;
	hyphens: manual;
}

.chapter blockquote p + p {
	text-indent: 0;
	margin-top: 0.5rem;
}

.chapter blockquote ul,
.chapter blockquote ol {
	margin: 0.5rem 0;
	padding-left: 1.5rem;
}

.chapter blockquote ul {
	list-style: "–\A0";
}

.chapter hr {
	border: 0;
	margin: 2.5rem auto;
	text-align: center;
	color: var(--muted);
	font-size: 0.875rem;
}

.chapter hr::before {
	content: "✷\A0\A0\A0✷\A0\A0\A0✷";
}

/* Previous / next */
.chapter-nav {
	display: grid;
	grid-template-columns: 1fr auto 1fr;
	align-items: center;
	gap: 1rem;
	max-width: var(--measure);
	margin: 0 auto;
	padding: 2rem 1rem 4rem;
	border-top: 1px solid var(--rule);
	font: 500 0.875rem/1.3 var(--sans);
}

.chapter-nav a {
	text-decoration: none;
}

.chapter-nav a:hover {
	text-decoration: underline;
}

.chapter-nav .prev {
	justify-self: start;
}

.chapter-nav .toc {
	justify-self: center;
	color: var(--muted);
}

.chapter-nav .next {
	justify-self: end;
}

/* ---------- Index ---------- */

.index header {
	margin-bottom: 3rem;
	text-align: center;
}

.index h1 {
	margin: 0;
	font-weight: 300;
	font-size: clamp(2.5rem, 9vw, 3.75rem);
	line-height: 1.05;
	letter-spacing: -0.02em;
}

.index .tagline {
	margin: 1rem 0 0;
	color: var(--muted);
	font: 400 0.875rem/1.4 var(--sans);
}

.index h2 {
	margin: 0 0 1rem;
	color: var(--muted);
	font: 600 0.75rem/1 var(--sans);
	letter-spacing: 0.12em;
	text-transform: uppercase;
}

.chapters {
	display: grid;
	gap: 0.75rem;
	margin: 0;
	padding: 0;
	list-style: none;
}

.chapters a {
	display: flex;
	align-items: center;
	gap: 1rem;
	padding: 1.1rem 1.25rem;
	border: 1px solid var(--rule);
	border-radius: 10px;
	background: var(--surface);
	box-shadow: var(--shadow);
	color: var(--fg);
	text-decoration: none;
	transition:
		border-color 0.15s,
		transform 0.15s;
}

.chapters a:hover,
.chapters a:focus-visible {
	border-color: var(--accent);
	transform: translateY(-1px);
}

.chapters .text {
	flex: 1;
	min-width: 0;
}

.chapters .title {
	display: block;
	font-size: 1.25rem;
	line-height: 1.3;
}

.chapters .meta {
	display: block;
	margin-top: 0.25rem;
	color: var(--muted);
	font: 400 0.8125rem/1.4 var(--sans);
}

.chapters .arrow {
	color: var(--muted);
	font-family: var(--sans);
	transition:
		color 0.15s,
		transform 0.15s;
}

.chapters a:hover .arrow {
	color: var(--accent);
	transform: translateX(3px);
}

/* ---------- Small screens ---------- */

@media (max-width: 40rem) {
	body {
		font-size: 1.0625rem;
	}

	main {
		padding-top: 2.5rem;
	}

	.chapter p {
		text-align: left;
	}
}
