/**
 * Smooth scroll for Table of Contents anchor links.
 */
(function () {
	'use strict';

	function init() {
		document.querySelectorAll('.wpps-toc__link').forEach(function (link) {
			var href = link.getAttribute('href');
			if (!href || href.charAt(0) !== '#') return;
			link.addEventListener('click', function (e) {
				var id = href.slice(1);
				var target = id ? document.getElementById(id) : null;
				if (target) {
					e.preventDefault();
					target.setAttribute('tabindex', '-1');
					target.scrollIntoView({ behavior: 'smooth', block: 'start' });
				}
			});
		});
	}

	if (document.readyState === 'loading') {
		document.addEventListener('DOMContentLoaded', init);
	} else {
		init();
	}
})();
