/**
 * UTOC — Sommaire Universel
 * Tout est piloté par des variables CSS : surchargez-les dans votre thème.
 */

.utoc {
	--utoc-accent: #1d3557;
	--utoc-bg: #ffffff;
	--utoc-fg: #1f2933;
	--utoc-muted: #6b7280;
	--utoc-border: rgba(29, 53, 87, 0.14);
	--utoc-hover: rgba(29, 53, 87, 0.06);
	--utoc-radius: 10px;
	--utoc-gap: 0.45em;
	--utoc-offset: 80px;

	box-sizing: border-box;
	margin: 2em 0;
	padding: 1.25em 1.5em;
	background: var(--utoc-bg);
	color: var(--utoc-fg);
	border-radius: var(--utoc-radius);
	font-size: 0.95em;
	line-height: 1.5;
}

.utoc *,
.utoc *::before,
.utoc *::after {
	box-sizing: inherit;
}

.utoc--width-auto {
	display: inline-block;
	min-width: min(100%, 320px);
	max-width: 100%;
}

/* --- En-tête --- */

.utoc__header {
	display: flex;
	align-items: baseline;
	justify-content: space-between;
	gap: 1em;
	margin-bottom: 0.75em;
}

.utoc__title {
	margin: 0;
	padding: 0;
	font-size: 1.05em;
	line-height: 1.3;
	font-weight: 700;
	letter-spacing: 0.01em;
	color: var(--utoc-fg);
	border: 0;
}

.utoc__title::before,
.utoc__title::after {
	content: none;
}

.utoc__anchor {
	display: block;
	position: relative;
	top: calc(var(--utoc-offset) * -1);
	height: 0;
	visibility: hidden;
}

.utoc__toggle {
	flex: 0 0 auto;
	display: inline-flex;
	align-items: center;
	gap: 0.4em;
	margin: 0;
	padding: 0.2em 0.5em;
	background: none;
	border: 0;
	border-radius: 6px;
	color: var(--utoc-muted);
	font: inherit;
	font-size: 0.85em;
	cursor: pointer;
}

.utoc__toggle:hover {
	color: var(--utoc-accent);
	background: var(--utoc-hover);
}

.utoc__toggle:focus-visible,
.utoc__link:focus-visible {
	outline: 2px solid var(--utoc-accent);
	outline-offset: 2px;
}

.utoc__toggle-icon {
	width: 0.55em;
	height: 0.55em;
	border-right: 2px solid currentColor;
	border-bottom: 2px solid currentColor;
	transform: rotate(45deg) translate(-0.1em, -0.1em);
	transition: transform 0.2s ease;
}

.utoc.is-collapsed .utoc__toggle-icon {
	transform: rotate(-135deg) translate(-0.1em, -0.1em);
}

/* --- Listes --- */

.utoc__list {
	margin: 0;
	padding: 0;
	list-style: none;
	counter-reset: none;
}

.utoc__list--sub {
	margin: 0.15em 0 0.35em 0;
	padding-left: 1.15em;
	border-left: 1px solid var(--utoc-border);
}

.utoc--flat .utoc__list--sub {
	padding-left: 0;
	border-left: 0;
}

.utoc__item {
	margin: 0;
	padding: 0;
	list-style: none;
}

.utoc__item::before,
.utoc__item::marker {
	content: none;
}

.utoc__link {
	display: flex;
	align-items: baseline;
	gap: var(--utoc-gap);
	padding: 0.28em 0.45em;
	margin-left: -0.45em;
	border-radius: 6px;
	color: inherit;
	text-decoration: none;
	transition: background-color 0.15s ease, color 0.15s ease;
}

.utoc__link:hover {
	color: var(--utoc-accent);
	background: var(--utoc-hover);
	text-decoration: none;
}

.utoc__number {
	flex: 0 0 auto;
	color: var(--utoc-accent);
	font-weight: 600;
	font-variant-numeric: tabular-nums;
	font-size: 0.92em;
	opacity: 0.85;
}

.utoc__text {
	flex: 1 1 auto;
	min-width: 0;
}

.utoc__link:hover .utoc__text {
	text-decoration: underline;
	text-underline-offset: 0.18em;
}

/* Section en cours de lecture */

.utoc__item.is-active > .utoc__link {
	color: var(--utoc-accent);
	background: var(--utoc-hover);
	font-weight: 600;
}

.utoc__item.is-active > .utoc__link .utoc__number {
	opacity: 1;
}

/* Profondeur : le premier niveau porte le poids */

.utoc--nested .utoc__item--depth-1 > .utoc__link {
	font-weight: 600;
}

.utoc--nested .utoc__item--depth-2 > .utoc__link,
.utoc--nested .utoc__item--depth-3 > .utoc__link {
	font-size: 0.95em;
	color: var(--utoc-muted);
}

.utoc--nested .utoc__item--depth-2 > .utoc__link:hover,
.utoc--nested .utoc__item--depth-3 > .utoc__link:hover,
.utoc--nested .utoc__item--depth-2.is-active > .utoc__link,
.utoc--nested .utoc__item--depth-3.is-active > .utoc__link {
	color: var(--utoc-accent);
}

/* Repli */

.utoc.is-collapsed .utoc__list--root {
	display: none;
}

/* Collant */

.utoc--sticky {
	position: sticky;
	top: calc(var(--utoc-offset) + 16px);
	max-height: calc(100vh - var(--utoc-offset) - 48px);
	overflow-y: auto;
	overscroll-behavior: contain;
}

/* --- Thèmes --- */

.utoc--theme-card {
	border: 1px solid var(--utoc-border);
	box-shadow: 0 1px 2px rgba(15, 23, 42, 0.04);
}

.utoc--theme-bordered {
	padding-left: 1.15em;
	border-left: 3px solid var(--utoc-accent);
	border-radius: 0;
	background: transparent;
}

.utoc--theme-minimal {
	padding: 0;
	background: transparent;
	border: 0;
}

.utoc--theme-minimal .utoc__header {
	padding-bottom: 0.5em;
	border-bottom: 1px solid var(--utoc-border);
}

.utoc--theme-dark {
	--utoc-bg: #12161f;
	--utoc-fg: #e8eaf0;
	--utoc-muted: #9aa3b2;
	--utoc-border: rgba(255, 255, 255, 0.12);
	--utoc-hover: rgba(255, 255, 255, 0.07);
	border: 1px solid var(--utoc-border);
}

.utoc--theme-dark .utoc__number,
.utoc--theme-dark .utoc__link:hover,
.utoc--theme-dark .utoc__item.is-active > .utoc__link {
	color: #ffffff;
}

.utoc--theme-dark .utoc__item.is-active > .utoc__link {
	background: rgba(255, 255, 255, 0.1);
}

/* --- Lien retour au sommaire --- */

.utoc-back {
	margin: 0.35em 0 1.2em;
	font-size: 0.82em;
	line-height: 1.4;
}

.utoc-back__link {
	color: var(--utoc-accent, #1d3557);
	text-decoration: none;
	opacity: 0.7;
}

.utoc-back__link:hover {
	opacity: 1;
	text-decoration: underline;
}

/* --- Réglages fins --- */

@media (max-width: 600px) {
	.utoc {
		padding: 1em 1.1em;
		font-size: 0.92em;
	}

	.utoc--sticky {
		position: static;
		max-height: none;
	}

	.utoc__list--sub {
		padding-left: 0.85em;
	}
}

@media (prefers-reduced-motion: reduce) {
	.utoc *,
	.utoc *::before,
	.utoc *::after {
		transition: none !important;
	}
}

@media print {
	.utoc__toggle,
	.utoc-back {
		display: none;
	}

	.utoc {
		border: 1px solid #ccc;
		box-shadow: none;
	}
}
