.ctoc-box {
	display: grid;
	gap: var(--ctoc-title-spacing, 12px);

	&,
	& * {
		box-sizing: border-box;
	}

	&.ctoc-sticky {
		top: var(--ctoc-sticky-top, 20px);
		align-self: start;
		z-index: 1;
		max-height: calc(100vh - var(--ctoc-sticky-top, 20px) - var(--ctoc-sticky-bottom-gap, 100px));
		grid-template-rows: auto minmax(0, 1fr);

		& .ctoc-list {
			max-height: none;
			min-height: 0;
			overflow-y: auto;
			scrollbar-width: thin;
			scrollbar-color: rgb(0 0 0 / 25%) transparent;

			&::-webkit-scrollbar {
				width: 5px;
			}

			&::-webkit-scrollbar-track {
				background: transparent;
			}

			&::-webkit-scrollbar-thumb {
				background: rgb(0 0 0 / 25%);
				border-radius: 999px;
			}
		}

		&.ctoc-collapsed .ctoc-list {
			max-height: 0;
			overflow-y: hidden;
		}

		@media (max-width: 767px) {
			position: static !important;
		}
	}
}

.ctoc-header {
	display: flex;
	align-items: center;
	justify-content: space-between;

	&.ctoc-toggle {
		cursor: pointer;

		&:focus-visible {
			outline: 2px solid currentColor;
			outline-offset: 4px;
		}
	}
}

.ctoc-title {
	margin: 0;
	line-height: 1.3;
}

.ctoc-toggle-icon {
	display: block;
	width: 10px;
	height: 10px;
	border-right: 2px solid currentColor;
	border-bottom: 2px solid currentColor;
	transform: rotate(45deg);
	transition: transform 0.2s ease;
}

.ctoc-collapsed {
	& .ctoc-toggle-icon {
		transform: rotate(-45deg);
	}

	& .ctoc-list {
		max-height: 0;
		opacity: 0;
	}
}

.ctoc-list {
	margin: 0;
	padding: 0;
	max-height: 2000px;
	overflow: hidden;
	list-style: none;
	opacity: 1;
	transition:
		max-height 0.3s ease,
		opacity 0.2s ease;

	/* Plain list: indentation via nested lists. */
	& ul {
		margin: var(--ctoc-item-spacing, 8px) 0 0;
		padding-left: var(--ctoc-indent, 16px);
		list-style: none;
	}

	& li {
		position: relative;
		margin-bottom: var(--ctoc-item-spacing, 8px);

		&:last-child {
			margin-bottom: 0;
		}
	}

	& a {
		display: inline-block;
		text-decoration: none;
		transition: color 0.15s ease;

		&:hover,
		&.ctoc-active {
			color: var(--e-global-color-primary, #fc6321);
		}
	}

	/* Bullet list: changing marker per nesting level. */
	&[data-liststyle="disc"] {
		padding-left: 30px;
		list-style: disc outside;

		& ul {
			padding-left: var(--ctoc-indent, 16px);
			list-style: circle outside;

			& ul {
				list-style-type: square;
			}
		}
	}

	/* Numbered list: CSS counters for hierarchical numbering. */
	&[data-liststyle="number"] {
		padding-left: 24px;
		counter-reset: toc-counter;

		& ul {
			padding-left: var(--ctoc-indent, 24px);
			counter-reset: toc-counter;
		}

		& li {
			padding-inline-start: 0.5rem;
			counter-increment: toc-counter;

			&::before {
				position: absolute;
				left: -1.5rem;
				display: inline-block;
				min-width: 1.5rem;
				content: counters(toc-counter, ".") ". ";
				text-align: end;
			}
		}
	}

	&[data-liststyle="disc"],
	&[data-liststyle="number"] {
		& a {
			display: inline;
			vertical-align: baseline;
		}
	}
}

.ctoc-empty {
	margin: 0;
	font-size: 13px;
	opacity: 0.7;
}
