<!-- Pagination -->
{{ if gt (len .Site.RegularPages) 10 }}
<nav>
    <ul class="pagination justify-content-center mt-5">
        <!-- Previous -->
        <li class="page-item {{ if not .Paginator.HasPrev }}disabled{{ end }}">
            {{ if .Paginator.HasPrev }}
                <a class="page-link" href="{{ .Paginator.Prev.URL }}">&laquo; Previous</a>
            {{ else }}
                <a class="page-link" href="#">&laquo; Previous</a>
            {{ end }}
        </li>

        <!-- Page numbers -->
        {{ $currentPage := .Paginator.PageNumber }}
        {{ $totalPages := .Paginator.TotalPages }}
        
        <!-- Always show first page -->
        <li class="page-item {{ if eq $currentPage 1 }}active{{ end }}">
            <a class="page-link" href="{{ .Paginator.First.URL }}">1</a>
        </li>

        <!-- Show ellipsis if current page is > 4 -->
        {{ if gt $currentPage 4 }}
            <li class="page-item disabled">
                <span class="page-link">...</span>
            </li>
        {{ end }}

        <!-- Show pages around current page -->
        {{ range .Paginator.Pagers }}
            {{ $pageNumber := .PageNumber }}
            {{ if and (gt $pageNumber 1) (lt $pageNumber $totalPages) (ge $pageNumber (sub $currentPage 1)) (le $pageNumber (add $currentPage 1)) }}
                {{ if ne $pageNumber 1 }}
                    {{ if ne $pageNumber $totalPages }}
                        <li class="page-item {{ if eq $pageNumber $currentPage }}active{{ end }}">
                            <a class="page-link" href="{{ .URL }}">{{ $pageNumber }}</a>
                        </li>
                    {{ end }}
                {{ end }}
            {{ end }}
        {{ end }}

        <!-- Show ellipsis if current page is < totalPages - 3 -->
        {{ if lt $currentPage (sub $totalPages 3) }}
            <li class="page-item disabled">
                <span class="page-link">...</span>
            </li>
        {{ end }}

        <!-- Always show last page -->
        {{ if gt $totalPages 1 }}
            <li class="page-item {{ if eq $currentPage $totalPages }}active{{ end }}">
                <a class="page-link" href="{{ .Paginator.Last.URL }}">{{ $totalPages }}</a>
            </li>
        {{ end }}

        <!-- Next -->
        <li class="page-item {{ if not .Paginator.HasNext }}disabled{{ end }}">
            {{ if .Paginator.HasNext }}
                <a class="page-link" href="{{ .Paginator.Next.URL }}">Next &raquo;</a>
            {{ else }}
                <a class="page-link" href="#">Next &raquo;</a>
            {{ end }}
        </li>
    </ul>
</nav>
{{ end }}