/**
 * Post Table of Contents ([tps_toc]).
 *
 * Loaded only on templates that render the TOC. Behaviour lives in
 * assets/js/post-toc.js, which sets these custom properties on <html>:
 *   --tps-sticky-top  bottom edge of persistent UI (admin bar + sticky header)
 *   --tps-toc-bar-h   height of the mobile "On this page" bar (0 on desktop)
 * Fallbacks below match the measured header heights for no-JS visitors.
 *
 * Breakpoint: 1100px, same as the single-post sidebar collapse
 * (.tps-hide-on-mobile / .tps-hide-on-desktop in style.css).
 */

:root {
	--tps-toc-gap: 16px;
	--tps-toc-border: #e2e0e8;
}

/* ── Heading landing offsets ───────────────────────────────── */

.tps-post-main :is(h1, h2, h3, h4, h5, h6)[id] {
	scroll-margin-top: calc(var(--tps-sticky-top, 64px) + var(--tps-toc-bar-h, 0px) + var(--tps-toc-gap));
}

@media (max-width: 1100px) {
	.tps-post-main :is(h1, h2, h3, h4, h5, h6)[id] {
		scroll-margin-top: calc(var(--tps-sticky-top, 122px) + var(--tps-toc-bar-h, 0px) + var(--tps-toc-gap));
	}
}

/* Headings receive programmatic focus after a TOC jump. Show a ring only
   for keyboard users. */
.tps-post-main :is(h1, h2, h3, h4, h5, h6)[tabindex="-1"]:focus {
	outline: none;
}

.tps-post-main :is(h1, h2, h3, h4, h5, h6)[tabindex="-1"]:focus-visible {
	outline: 2px solid var(--wp--preset--color--accent-1);
	outline-offset: 4px;
	border-radius: 2px;
}

/* ── Shared widget + list ──────────────────────────────────── */

.tps-toc {
	display: block;
	background: var(--wp--preset--color--base);
	border: 1.5px solid var(--tps-toc-border);
	border-radius: 16px;
	overflow: hidden;
}

.tps-toc__head {
	padding: 16px 18px;
}

.tps-toc__head .tps-toc__eyebrow {
	margin: 0 0 4px;
	color: rgba(255, 255, 255, 0.7);
	font-size: 10.5px;
	font-weight: 700;
	letter-spacing: 0.15em;
	line-height: 1.4;
	text-transform: uppercase;
}

.tps-toc__head .tps-toc__title {
	margin: 0;
	color: #fff;
	font-size: 17px;
	font-weight: 400;
	line-height: 1.3;
}

.sb-toc-links {
	padding: 8px 0;
}

.sb-toc-list,
.sb-toc-list > li {
	margin: 0;
	padding: 0;
	list-style: none;
}

.sb-toc-link {
	display: flex;
	align-items: center;
	box-sizing: border-box;
	min-height: 44px;
	padding: 10px 18px;
	font-size: 13.5px;
	font-weight: 500;
	line-height: 1.35;
	text-decoration: none;
	overflow-wrap: anywhere;
}

.sb-toc-link--indent-1 {
	padding-left: 34px;
	font-size: 12.5px;
}

.sb-toc-link--indent-2 {
	padding-left: 50px;
	font-size: 12px;
}

.sb-toc-link:focus-visible {
	outline: 2px solid var(--wp--preset--color--accent-1);
	outline-offset: -2px;
}

/* Current section. Darker text than the generic .sb-nav-link.active so the
   label keeps >= 4.5:1 contrast on the periwinkle tint. */
.sb-toc-link.active {
	color: var(--wp--preset--color--accent-2);
}

/* ── Desktop: sticky sidebar widget ────────────────────────── */

@media (min-width: 1101px) {
	/* Let the sidebar wrapper span the full article height so the TOC can
	   stick inside it and release when the article (columns) ends, before
	   comments and footer. */
	.tps-post-sidebar:has(> .tps-sidebar-has-toc) {
		display: flex;
		flex-direction: column;
	}

	.tps-sidebar-has-toc {
		position: static;
		flex: 1 1 auto;
	}

	.tps-toc-desktop {
		position: sticky;
		top: calc(var(--tps-sticky-top, 64px) + 24px);
		display: flex;
		flex-direction: column;
		max-height: calc(100vh - var(--tps-sticky-top, 64px) - 48px);
	}

	.tps-toc-desktop > .tps-toc__head {
		flex: 0 0 auto;
	}

	/* Long TOCs scroll inside the widget; every link stays reachable. */
	.tps-toc-desktop > .sb-toc-links {
		flex: 1 1 auto;
		min-height: 0;
		overflow-y: auto;
		overscroll-behavior: contain;
		scrollbar-width: thin;
	}
}

/* ── Mobile / tablet ───────────────────────────────────────── */

@media (max-width: 1100px) {
	/* No-JS fallback: in-flow card, as before. */
	.tps-toc-mobile {
		margin-bottom: 28px;
	}

	.tps-toc-mobile__toggle {
		display: none;
	}

	/* Enhanced: compact bar in the TOC's original spot (space reserved from
	   first paint, so no layout shift), sticking under the site header while
	   the article column scrolls. Releases at the end of the main column. */
	html.tps-toc-js .tps-toc-mobile {
		position: sticky;
		z-index: 90; /* below header (100), its menu overlay, consent + ad UI */
		top: var(--tps-sticky-top, 122px);
		margin: 0 0 20px;
		overflow: visible; /* let the dropdown panel extend below the bar */
		border-radius: 12px;
		box-shadow: 0 6px 18px rgba(24, 44, 127, 0.08);
	}

	html.tps-toc-js .tps-toc-mobile.is-open {
		border-bottom-left-radius: 0;
		border-bottom-right-radius: 0;
	}

	/* Hide while a core navigation overlay (mobile menu) is open. */
	html.has-modal-open.tps-toc-js .tps-toc-mobile {
		visibility: hidden;
	}

	html.tps-toc-js .tps-toc-mobile__fallback-head {
		display: none;
	}

	html.tps-toc-js .tps-toc-mobile__toggle {
		display: flex;
		align-items: center;
		gap: 12px;
		width: 100%;
		min-height: 48px;
		margin: 0;
		padding: 0 16px;
		border: 0;
		border-radius: 12px;
		background: var(--wp--preset--color--base);
		color: var(--wp--preset--color--charcoal);
		font: inherit;
		text-align: left;
		cursor: pointer;
	}

	html.tps-toc-js .tps-toc-mobile__toggle:focus-visible {
		outline: 2px solid var(--wp--preset--color--accent-1);
		outline-offset: -3px;
	}

	.tps-toc-mobile__label {
		flex: 0 0 auto;
		color: var(--wp--preset--color--accent-2);
		font-size: 11px;
		font-weight: 700;
		letter-spacing: 0.12em;
		text-transform: uppercase;
	}

	.tps-toc-mobile__current {
		flex: 1 1 auto;
		min-width: 0;
		overflow: hidden;
		color: var(--wp--preset--color--charcoal);
		font-size: 14px;
		font-weight: 500;
		white-space: nowrap;
		text-overflow: ellipsis;
	}

	.tps-toc-mobile__chevron {
		flex: 0 0 auto;
		margin-left: auto;
		color: var(--wp--preset--color--accent-2);
		transition: transform 0.18s ease;
	}

	html.tps-toc-js .tps-toc-mobile.is-open .tps-toc-mobile__chevron {
		transform: rotate(180deg);
	}

	/* Collapsed: links are display:none, so they can't take focus. */
	html.tps-toc-js .tps-toc-mobile:not(.is-open) .tps-toc-mobile__panel {
		display: none;
	}

	/* Dropdown overlays the article instead of pushing it down. Bar + panel
	   together never exceed 50% of the viewport left below the persistent
	   header (and above the bottom safe area). */
	html.tps-toc-js .tps-toc-mobile__panel {
		position: absolute;
		top: 100%;
		right: -1.5px;
		left: -1.5px;
		box-sizing: border-box;
		background: var(--wp--preset--color--base);
		border: 1.5px solid var(--tps-toc-border);
		border-radius: 0 0 12px 12px;
		box-shadow: 0 12px 24px rgba(24, 44, 127, 0.12);
		/* 1px = the bar's bottom border. */
		max-height: calc((100vh - var(--tps-sticky-top, 122px) - env(safe-area-inset-bottom, 0px)) * 0.5 - var(--tps-toc-bar-h, 48px) - 1px);
		overflow-y: auto;
		overscroll-behavior: contain;
		padding: 4px 0;
	}

	@supports (height: 100dvh) {
		html.tps-toc-js .tps-toc-mobile__panel {
			max-height: calc((100dvh - var(--tps-sticky-top, 122px) - env(safe-area-inset-bottom, 0px)) * 0.5 - var(--tps-toc-bar-h, 48px) - 1px);
		}
	}

	html.tps-toc-js .tps-toc-mobile__panel .sb-toc-link {
		font-size: 14px;
	}

	html.tps-toc-js .tps-toc-mobile__panel .sb-toc-link--indent-1 {
		font-size: 13px;
	}

	html.tps-toc-js .tps-toc-mobile__panel .sb-toc-link--indent-2 {
		font-size: 12.5px;
	}
}

@media (max-width: 279px) {
	.tps-toc-mobile__current {
		display: none;
	}
}

@media (prefers-reduced-motion: reduce) {
	.tps-toc-mobile__chevron {
		transition: none !important;
	}
}
