/**
 * Component: table of contents (blog post right rail; inline card on
 * phones).
 *
 * Markup: template-parts/components/toc.php. js/toc.js sets aria-current on
 * the link of the section in view and turns the title into a toggle
 * (.toc--collapsible) with an orange up arrow that turns down when the list
 * is collapsed. The arrow is vertical, so it needs no RTL mirroring.
 *
 * Measured from design/subpages/blog-single.png at 1920 (TODO: verify in
 * Figma): a white card with a 1px grey border, a ~46px title row (list icon,
 * "Table of contents" at 14px, the arrow), numbered 14px grey items on 24px
 * lines, the current item orange. Tokens are local to the component.
 *
 * @package cartrack
 */

.toc {
	--toc-bg: var(--color-bg);
	--toc-border: var(--color-border);
	--toc-padding-x: 1rem;
	--toc-title-height: 2.875rem;         /* ~46px. */
	--toc-title-size: var(--t-small-size);
	--toc-icon-size: 1.125rem;
	--toc-arrow-colour: var(--color-primary);
	--toc-link-size: var(--t-small-size);
	--toc-link-line-height: 1.7;          /* ~24px lines at 14px. */
	--toc-link-colour: var(--color-text-muted);
	--toc-active-colour: var(--color-text-accent);
	--toc-item-gap: 0.25rem;
	--toc-number-space: 1.25rem;          /* Room for "10." before the text. */
	--toc-list-pad-y: 0.75rem;

	border: 1px solid var(--toc-border);
	background-color: var(--toc-bg);
}

.toc__title {
	display: flex;
	align-items: center;
	gap: var(--space-xs);
	min-block-size: var(--toc-title-height);
	margin: 0;
	padding-inline: var(--toc-padding-x);
	color: var(--color-text);
	font-family: var(--t-small-family);
	font-size: var(--toc-title-size);
	font-weight: var(--t-small-weight);
	line-height: var(--t-small-line-height);
}

.toc__title .icon {
	flex: none;
	inline-size: var(--toc-icon-size);
	block-size: var(--toc-icon-size);
}

.toc__label {
	flex: 1;
	min-inline-size: 0;
	overflow-wrap: anywhere;
}

.toc__list {
	display: grid;
	gap: var(--toc-item-gap);
	margin: 0;
	padding-block: var(--toc-list-pad-y);
	padding-inline: calc(var(--toc-padding-x) + var(--toc-number-space)) var(--toc-padding-x);
	border-block-start: 1px solid var(--toc-border);
	list-style: decimal;
}

.toc__sublist {
	display: grid;
	gap: var(--toc-item-gap);
	margin: var(--toc-item-gap) 0 0;
	padding-inline-start: var(--toc-number-space);
	list-style: lower-alpha;
}

.toc__item {
	color: var(--toc-link-colour);
	font-family: var(--t-small-family);
	font-size: var(--toc-link-size);
	line-height: var(--toc-link-line-height);
}

/* The number turns orange with its link. */
.toc__item:has(> .toc__link[aria-current="true"]) {
	color: var(--toc-active-colour);
}

.toc__link {
	color: inherit;
	text-decoration: none;
	overflow-wrap: anywhere;
}

.toc__link[aria-current="true"] {
	color: var(--toc-active-colour);
}

.toc__link:focus-visible {
	outline: 2px solid var(--color-focus-dark);
	outline-offset: 2px;
}

/* TODO: verify hover state in Figma (no hover frame supplied). */
@media (hover: hover) {
	.toc__link:hover {
		color: var(--toc-active-colour);
		text-decoration: underline;
		text-underline-offset: 0.2em;
	}
}

/* Toggle (JS): the title row is the button. */
.toc__toggle {
	display: flex;
	flex: 1;
	align-items: center;
	gap: var(--space-xs);
	min-block-size: var(--toc-title-height);
	margin-inline: calc(-1 * var(--toc-padding-x));
	padding-inline: var(--toc-padding-x);
	border: 0;
	background: none;
	color: inherit;
	font: inherit;
	text-align: start;
	cursor: pointer;
}

.toc__toggle:focus-visible {
	outline: 2px solid var(--color-focus-dark);
	outline-offset: -2px;
}

.toc__toggle .toc__arrow {
	color: var(--toc-arrow-colour);
	transition: transform var(--transition-fast);
}

/* Collapsed: the arrow turns down. */
.toc__toggle[aria-expanded="false"] .toc__arrow {
	transform: rotate(180deg);
}

.toc__list[hidden] {
	display: none;
}

@media (prefers-reduced-motion: reduce) {
	.toc__toggle .toc__arrow {
		transition: none;
	}
}

@media print {
	.toc {
		display: none;
	}
}
