{% macro toc_nav(docs_section, current_page=false) %}
<div class="toc">
<div class="toc-sticky">
{% set current_group = "" %}
{% for doc_page in docs_section.pages %}
{% set is_current = current_page and doc_page.permalink == current_page.permalink %}
{% set page_group = doc_page.extra.group | default(value="") %}
{# Show group label when group changes #}
{% if page_group != current_group and page_group != "" %}
<div class="toc-group-label">{{ page_group }}</div>
{% set_global current_group = page_group %}
{% endif %}
<div class="toc-item">
<a class="subtext {% if is_current %}active{% endif %}" href="{{ doc_page.permalink | safe }}">
{{ doc_page.title }}
</a>
</div>
{# Show page TOC for current page (h1s with nested h2s, or standalone h2s) #}
{% if is_current and current_page.toc %}
{% for h in current_page.toc %}
{% if h.level == 1 %}
<div class="toc-item-child toc-h1">
<a class="subtext" href="{{ h.permalink | safe }}">
<small>{{ h.title }}</small>
</a>
</div>
{# Show H2 children nested under this H1 #}
{% for child in h.children | default(value=[]) %}
{% if child.level == 2 %}
<div class="toc-item-child toc-h2-under-h1">
<a class="subtext" href="{{ child.permalink | safe }}">
<small>{{ child.title }}</small>
</a>
</div>
{% endif %}
{% endfor %}
{% elif h.level == 2 %}
{# Standalone H2s (before any H1) #}
<div class="toc-item-child">
<a class="subtext" href="{{ h.permalink | safe }}">
<small>{{ h.title }}</small>
</a>
</div>
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
</div>
</div>
{% endmacro toc_nav %}