{{ define "main" }}
  <article class="single library-entry" style="--section-color: var(--palette-ichigo)">
    <header class="single-header">
      <h1>{{ .Title }}</h1>
      {{ with .Params.summary }}<p class="single-summary">{{ . }}</p>{{ end }}
    </header>

    {{ $hasToC := .Params.toc | default false }}
    {{ $headers := findRE "<h[2-6]" .Content }}
    {{ if and $hasToC (ge (len $headers) 2) }}
      <nav class="toc">
        <strong>Inhalt</strong>
        <div class="toc-content">{{ .TableOfContents }}</div>
      </nav>
    {{ end }}

    {{/* Wiki-Links [[Titel]] / [[slug]] → Link auf die passende Library-Seite. */}}
    {{ $html := .Content }}
    {{ range (where site.RegularPages "Section" "library") }}
      {{ $a := printf `<a href="%s" class="wikilink">%s</a>` .RelPermalink .LinkTitle }}
      {{ $html = replace $html (printf "[[%s]]" .LinkTitle) $a }}
      {{ $html = replace $html (printf "[[%s]]" .File.ContentBaseName) $a }}
    {{ end }}
    {{/* Übrige (noch nicht angelegte) Verweise: ohne Klammern, dezent markiert. */}}
    {{ $html = replaceRE `\[\[([^\]]+)\]\]` `<span class="wikilink-missing" title="Seite existiert noch nicht">$1</span>` $html }}
    <div class="single-content">{{ $html | safeHTML }}</div>

    {{/* ── Siehe auch: gleiche Gruppe + geteilte Tags ── */}}
    {{ $cur := . }}
    {{ $related := slice }}
    {{ range where (where site.RegularPages "Section" "library") "Params.group" .Params.group }}
      {{ if ne .RelPermalink $cur.RelPermalink }}{{ $related = $related | append . }}{{ end }}
    {{ end }}
    {{ with .Params.tags }}
      {{ range $t := . }}
        {{ range (where site.RegularPages "Section" "library") }}
          {{ if and (ne .RelPermalink $cur.RelPermalink) (in (.Params.tags | default slice) $t) }}
            {{ $related = $related | append . }}
          {{ end }}
        {{ end }}
      {{ end }}
    {{ end }}
    {{ $related = $related | uniq }}
    {{ with $related }}
      <nav class="entry-links" aria-label="Siehe auch">
        <span class="entry-links-label">Siehe auch</span>
        <ul>{{ range . }}<li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>{{ end }}</ul>
      </nav>
    {{ end }}

    {{/* ── Erwähnt in (Backlinks): Seiten, die per Link oder [[…]] hierher zeigen ── */}}
    {{ $back := slice }}
    {{ $url := .RelPermalink }}
    {{ $tok1 := printf "[[%s]]" .Title }}
    {{ $tok2 := printf "[[%s]]" .File.ContentBaseName }}
    {{ range site.RegularPages }}
      {{ if ne .RelPermalink $url }}
        {{ $raw := .RawContent }}
        {{ if or (in $raw $url) (in $raw $tok1) (in $raw $tok2) }}
          {{ $back = $back | append . }}
        {{ end }}
      {{ end }}
    {{ end }}
    {{ with $back }}
      <nav class="entry-links" aria-label="Erwähnt in">
        <span class="entry-links-label">Erwähnt in</span>
        <ul>{{ range . }}<li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>{{ end }}</ul>
      </nav>
    {{ end }}

    {{/* Fuss: Gruppe + zuletzt bearbeitet + bearbeiten. */}}
    <div class="entry-foot">
      <span class="entry-more"><strong>{{ .Params.group | default "Allgemein" }}</strong></span>
      {{ if .Lastmod }}<span>Zuletzt bearbeitet am {{ .Lastmod.Format "02.01.2006" }}</span>{{ end }}
      {{ with .File }}<a href="{{ site.Params.repoURL }}/_edit/branch/main/content/{{ .Path }}" rel="nofollow">bearbeiten ↗</a>{{ end }}
    </div>
  </article>
{{ end }}
