{{ define "main" }}
  {{ .Scratch.Set "scope" "single" }}
  
  <article class="w-full py-8">
    {{ if .Params.showHero | default (site.Params.article.showHero | default false) }}
      {{ partial (printf "hero/%s.html" (.Params.heroStyle | default site.Params.article.heroStyle)) . }}
    {{ end }}

    <header id="single_header" class="mt-5 max-w-prose mx-auto px-4">
      <h1 class="mt-0 text-4xl font-extrabold text-neutral-900 dark:text-neutral">{{ .Title | emojify }}</h1>
      <div class="mt-1 mb-6 text-base text-neutral-500 dark:text-neutral-400 print:hidden">
        {{ partial "article-meta/basic.html" (dict "context" . "scope" "single") }}
      </div>
      {{ if not (.Params.showAuthorBottom | default (site.Params.article.showAuthorBottom | default false)) }}
        {{ template "SingleAuthor" . }}
      {{ end }}
    </header>

    {{/* The Relative Wrapper: Acts as an anchor for the absolute TOC */}}
    <div class="single-post-wrapper relative mt-8 w-full max-w-7xl mx-auto px-4">
      
      {{/* Sidebar TOC: Mobile flow (Top), Desktop absolute (Right) */}}
      {{ if .Params.showTableOfContents | default site.Params.article.showTableOfContents }}
        <aside id="sidebar-toc" class="w-full mb-8 xl:mb-0">
          <div class="toc sticky top-24">
            {{ partial "toc.html" . }}
          </div>
        </aside>
      {{ end }}

      {{/* Main Content: Dead center, uninfluenced by anything */}}
      <section id="main-content" class="w-full">
        <div class="article-content prose dark:prose-invert mx-auto">
          {{ partial "series/series.html" . }}
          {{ .Content }}
          
          {{ if .Params.replyByEmail | default site.Params.replyByEmail }}
            <strong class="block mt-8">
              <a target="_blank" href="mailto:{{ site.Params.Author.email }}">Reply by Email</a>
            </strong>
          {{ end }}

          <div class="mt-10">
            {{ if (.Params.showAuthorBottom | default (site.Params.article.showAuthorBottom | default false)) }}
              {{ template "SingleAuthor" . }}
            {{ end }}
            {{ partial "series/series-closed.html" . }}
            {{ partial "sharing-links.html" . }}
            {{ partial "related.html" . }}
          </div>
        </div>
      </section>
      
    </div>

    <footer class="pt-8 mt-12 border-t border-neutral-200 dark:border-neutral-800 max-w-prose mx-auto px-4">
      {{ partial "article-pagination.html" . }}
    </footer>
  </article>
{{ end }}

{{ define "SingleAuthor" }}
  <div class="max-w-prose">
    {{ $authorsData := site.Data.authors }}
    {{ $baseURL := site.BaseURL }}
    {{ $isAuthorBottom := (.Params.showAuthorBottom | default ( site.Params.article.showAuthorBottom | default false)) }}
    {{ if not (strings.HasSuffix $baseURL "/") }}{{ $baseURL = delimit (slice $baseURL "/") "" }}{{ end }}

    {{ if .Params.showAuthor | default (site.Params.article.showAuthor | default true) }}
      {{ partial "author.html" . }}
    {{ end }}

    {{ range $author := .Page.Params.authors }}
      {{ $authorData := index $authorsData $author }}
      {{ if $authorData }}
        {{ $link := printf "%sauthors/%s/" $baseURL $author }}
        {{ partial "author-extra.html" (dict "context" . "data" $authorData "link" $link) }}
      {{ end }}
    {{ end }}
    <div class="{{ cond $isAuthorBottom "mb-10" "mb-5" }}"></div>
  </div>
{{ end }}