/*
 * Table of contents.
 * Colours come only from the theme palette variables (dark mode redefines them, so the block
 * follows), with the brand values as fallbacks. Fonts are inherited; the title uses the heading font.
 * Class names deliberately avoid words ad blockers target.
 */
.pff-toc {
	--pff-toc-accent: var(--wp--preset--color--primary, #A15A6D);
	--pff-toc-accent-dark: var(--wp--preset--color--primary-dark, #713A4C);
	--pff-toc-accent-light: var(--wp--preset--color--primary-light, #DFA8B7);
	--pff-toc-gap: 1.5rem;
	box-sizing: border-box;
	margin-block: 2rem;
	padding: 1.25rem 1.5rem;
	border: 1px solid color-mix(in srgb, var(--pff-toc-accent-light) 60%, transparent);
	border-radius: 14px;
	background-color: var(--wp--preset--color--surface, #F3E4E8);
	color: var(--wp--preset--color--contrast, #29232B);
	font-size: 0.95em;
	line-height: 1.45;
	scroll-margin-top: 1.5rem;
}

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

/* Header row: title and open/close chevron. */
.pff-toc__summary {
	display: flex;
	align-items: center;
	justify-content: space-between;
	gap: 1rem;
	margin: 0;
	cursor: pointer;
	list-style: none;
	border-radius: 6px;
}

.pff-toc__summary::-webkit-details-marker {
	display: none;
}

.pff-toc__summary::after {
	content: "";
	flex: none;
	width: 0.55em;
	height: 0.55em;
	margin-right: 0.2em;
	border-right: 2px solid var(--pff-toc-accent);
	border-bottom: 2px solid var(--pff-toc-accent);
	transform: translateY(-25%) rotate(45deg);
	transition: transform 0.2s ease;
}

.pff-toc__panel[open] > .pff-toc__summary::after {
	transform: translateY(15%) rotate(-135deg);
}

.pff-toc__title {
	font-family: var(--wp--preset--font-family--heading, inherit);
	font-size: 1.45em;
	font-weight: 600;
	line-height: 1.15;
	letter-spacing: 0.01em;
	color: var(--pff-toc-accent-dark);
}

.pff-toc.has-text-color .pff-toc__title {
	color: inherit;
}

.pff-toc__body {
	counter-reset: pff-toc-a;
	padding-top: 0.9rem;
}

/* Lists. Doubled class so theme rules like ".entry-content ul" don't add their own spacing. */
.pff-toc .pff-toc__list {
	margin: 0;
	padding: 0;
	list-style: none;
}

.pff-toc .pff-toc__list .pff-toc__list {
	counter-reset: pff-toc-b;
	margin: 0.4rem 0 0.1rem;
	padding-left: 0.15rem;
	font-size: 0.95em;
}

.pff-toc .pff-toc__list .pff-toc__list .pff-toc__list {
	counter-reset: pff-toc-c;
}

.pff-toc .pff-toc__item {
	position: relative;
	margin: 0;
	padding-left: 1.3rem;
}

.pff-toc .pff-toc__item + .pff-toc__item {
	margin-top: 0.4rem;
}

/* Rose diamond bullets: filled at the top level, outlined below it. */
.pff-toc__item::before {
	content: "";
	position: absolute;
	left: 0.2em;
	top: 0.55em;
	width: 0.42em;
	height: 0.42em;
	background: var(--pff-toc-accent);
	transform: rotate(45deg);
}

.pff-toc__item:not(.pff-toc__item--d1)::before {
	width: 0.36em;
	height: 0.36em;
	background: transparent;
	border: 1.5px solid var(--pff-toc-accent);
}

/* Numbered: 1. / 1.2 / 1.2.3, then diamonds for anything deeper. */
.pff-toc--numbered .pff-toc__item--d1 {
	counter-increment: pff-toc-a;
}

.pff-toc--numbered .pff-toc__item--d2 {
	counter-increment: pff-toc-b;
}

.pff-toc--numbered .pff-toc__item--d3 {
	counter-increment: pff-toc-c;
}

.pff-toc--numbered .pff-toc__item--d1,
.pff-toc--numbered .pff-toc__item--d2,
.pff-toc--numbered .pff-toc__item--d3 {
	display: grid;
	grid-template-columns: auto minmax(0, 1fr);
	column-gap: 0.5em;
	padding-left: 0;
}

.pff-toc--numbered .pff-toc__item--d1 > .pff-toc__list,
.pff-toc--numbered .pff-toc__item--d2 > .pff-toc__list,
.pff-toc--numbered .pff-toc__item--d3 > .pff-toc__list {
	grid-column: 2;
}

.pff-toc--numbered .pff-toc__item--d1::before,
.pff-toc--numbered .pff-toc__item--d2::before,
.pff-toc--numbered .pff-toc__item--d3::before {
	position: static;
	width: auto;
	height: auto;
	border: 0;
	background: none;
	transform: none;
	color: var(--pff-toc-accent);
	font-weight: 600;
	font-variant-numeric: tabular-nums;
}

.pff-toc--numbered .pff-toc__item--d1::before {
	content: counter(pff-toc-a) ".";
}

.pff-toc--numbered .pff-toc__item--d2::before {
	content: counter(pff-toc-a) "." counter(pff-toc-b);
}

.pff-toc--numbered .pff-toc__item--d3::before {
	content: counter(pff-toc-a) "." counter(pff-toc-b) "." counter(pff-toc-c);
}

/* Links. */
.pff-toc a {
	color: inherit;
	text-decoration: none;
	border-radius: 3px;
}

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

.pff-toc a[aria-current="location"] {
	color: var(--pff-toc-accent-dark);
	font-weight: 600;
}

.pff-toc a:focus-visible,
.pff-toc summary:focus-visible {
	outline: 2px solid var(--pff-toc-accent);
	outline-offset: 2px;
}

/* "Show all (n)" / "Show fewer". When open, browsers that can lay out <details> as flex
   put the toggle after the extra items. */
.pff-toc__rest {
	margin-top: 0.4rem;
}

.pff-toc__rest[open] {
	display: flex;
	flex-direction: column;
}

.pff-toc__rest[open] > .pff-toc__more {
	order: 2;
	margin-top: 0.6rem;
}

.pff-toc__more {
	display: inline-flex;
	align-self: flex-start;
	align-items: center;
	gap: 0.35em;
	margin-top: 0.35rem;
	cursor: pointer;
	list-style: none;
	color: var(--pff-toc-accent-dark);
	font-weight: 600;
	font-size: 0.95em;
	border-radius: 4px;
}

.pff-toc__more::-webkit-details-marker {
	display: none;
}

.pff-toc__more:hover {
	text-decoration: underline;
	text-underline-offset: 0.2em;
}

.pff-toc__rest[open] .pff-toc__more-closed,
.pff-toc__rest:not([open]) .pff-toc__more-open {
	display: none;
}

.pff-toc .pff-toc__top {
	margin: 1rem 0 0;
	font-size: 0.875em;
}

.pff-toc__top-link::before {
	content: "\2191\00a0";
	color: var(--pff-toc-accent);
}

/* Editor-only notes. */
.pff-toc .pff-toc__note,
.pff-toc .pff-toc__editor-info {
	margin: 0.9rem 0 0;
	font-size: 0.8em;
	opacity: 0.8;
}

/* Section headings land below the top of the window when jumped to. */
:where(.wp-block-post-content, .entry-content) :where(h2, h3, h4, h5, h6)[id] {
	scroll-margin-top: 6rem;
}

/*
 * Collapsed on phones only. The panel is rendered closed (so phones never see it shut after
 * loading); from 782px the list shows anyway until the script marks the block ready and opens it.
 */
@supports selector(::details-content) {
	@media (min-width: 782px) {
		.pff-toc:not(.is-ready) .pff-toc__panel:not([open])::details-content {
			content-visibility: visible;
			display: block;
		}

		.pff-toc:not(.is-ready) .pff-toc__panel:not([open]) > .pff-toc__summary::after {
			transform: translateY(15%) rotate(-135deg);
		}
	}
}

/* Floating "Contents" button on phones (added by view.js once the list has scrolled away).
   Bottom left, clear of the reCAPTCHA badge in the bottom-right corner. */
.pff-toc-jump {
	position: fixed;
	left: max(1rem, env(safe-area-inset-left));
	bottom: calc(1rem + env(safe-area-inset-bottom));
	z-index: 90;
	display: inline-flex;
	align-items: center;
	gap: 0.5em;
	padding: 0.65rem 1.05rem;
	border: 0;
	border-radius: 999px;
	background: var(--wp--preset--color--primary, #A15A6D);
	color: var(--wp--preset--color--base, #FAF7F5);
	box-shadow: 0 6px 18px color-mix(in srgb, var(--wp--preset--color--contrast, #29232B) 22%, transparent);
	font: inherit;
	font-size: 0.9rem;
	font-weight: 600;
	line-height: 1;
	cursor: pointer;
	opacity: 0;
	transform: translateY(0.5rem);
	transition: opacity 0.2s ease, transform 0.2s ease;
}

.pff-toc-jump.is-visible {
	opacity: 1;
	transform: none;
}

.pff-toc-jump[hidden] {
	display: none;
}

.pff-toc-jump:hover {
	background: var(--wp--preset--color--primary-dark, #713A4C);
}

.pff-toc-jump:focus-visible {
	outline: 2px solid var(--wp--preset--color--contrast, #29232B);
	outline-offset: 3px;
}

.pff-toc-jump__icon {
	width: 0.5em;
	height: 0.5em;
	background: currentColor;
	transform: rotate(45deg);
}

@media (min-width: 782px) {
	.pff-toc-jump {
		display: none;
	}
}

/*
 * Side contents on wide screens. The block stays where it is in the article but takes no room:
 * it becomes a zero-height sticky strip the width of the 720px column, and the card hangs off
 * its right edge into the gutter. Being sticky inside the post content, it travels with the
 * reader and stops at the end of the article. The card is sized from the gutter so it never
 * reaches the column (1200px: 176px card in a 240px gutter). view.js hides it while a wide or
 * full-width block passes underneath, and falls back to the inline box if the gutter is too narrow.
 */
@media (min-width: 1200px) {
	.pff-toc--side:not(.pff-toc--side-off) {
		position: sticky;
		top: calc(var(--wp-admin--admin-bar--height, 0px) + 2rem);
		z-index: 5;
		height: 0;
		margin-block: 0 !important;
		padding: 0 !important;
		border: 0;
		overflow: visible;
	}

	.pff-toc--side:not(.pff-toc--side-off) > .pff-toc__panel {
		position: absolute;
		top: 0;
		left: calc(100% + var(--pff-toc-gap));
		width: clamp(11rem, calc((100vw - var(--wp--style--global--content-size, 720px)) / 2 - 4.5rem), 18rem);
		max-height: calc(100vh - var(--wp-admin--admin-bar--height, 0px) - 4rem);
		overflow-y: auto;
		overscroll-behavior: contain;
		scrollbar-width: thin;
		padding: 1rem 1.1rem;
		border: 1px solid color-mix(in srgb, var(--pff-toc-accent-light) 60%, transparent);
		border-radius: 14px;
		background-color: inherit;
		box-shadow: 0 10px 30px color-mix(in srgb, var(--wp--preset--color--contrast, #29232B) 8%, transparent);
		font-size: 0.875em;
		transition: opacity 0.2s ease, visibility 0.2s ease;
	}

	.pff-toc--side.is-obscured:not(.pff-toc--side-off) > .pff-toc__panel {
		opacity: 0;
		visibility: hidden;
	}

	.pff-toc--side:not(.pff-toc--side-off) .pff-toc__title {
		font-size: 1.3em;
	}

	/* Every section is listed at the side, so no "Show all". */
	.pff-toc--side:not(.pff-toc--side-off) .pff-toc__more {
		display: none;
	}

	/* With the script running, only the current section's sub-headings are shown. */
	.pff-toc--side.is-ready:not(.pff-toc--side-off) .pff-toc__item:not(.is-current-branch) > .pff-toc__list {
		display: none;
	}
}

@supports selector(::details-content) {
	@media (min-width: 1200px) {
		.pff-toc--side:not(.pff-toc--side-off) .pff-toc__rest:not([open])::details-content {
			content-visibility: visible;
			display: block;
		}
	}
}

@media (prefers-reduced-motion: reduce) {
	.pff-toc__summary::after,
	.pff-toc-jump,
	.pff-toc--side > .pff-toc__panel {
		transition: none;
	}
}
