{{- define "main" -}}
{{- $slug  := .File.Dir | strings.TrimSuffix "/" | path.Base -}}
{{- $posts := where .Site.RegularPages "Params.author" $slug -}}

<!-- ── PROFILE HEADER ────────────────────────────────────────── -->
<header class="gutter-x pt-36 pb-20 border-b border-zinc speed-lines">
  <div class="max-w-5xl mx-auto">
    <div class="flex flex-col md:flex-row gap-10 md:gap-16 items-start">

      <!-- Avatar — page bundle avatar.* takes precedence, falls back to .Params.avatar -->
      {{- $avatarRes := .Resources.GetMatch "avatar.*" -}}
      {{- $avatarSrc := "" -}}
      {{- with $avatarRes }}{{ $avatarSrc = .RelPermalink }}{{ else }}{{ with $.Params.avatar }}{{ $avatarSrc = . }}{{ end }}{{ end -}}
      {{- if $avatarSrc -}}
      <div class="flex-shrink-0 w-36 h-36 md:w-48 md:h-48 overflow-hidden border-2 border-heat neon-heat">
        <img
          src="{{ $avatarSrc }}"
          alt="{{ .Params.name | default .Title }}"
          class="w-full h-full object-cover"
          loading="lazy"
        >
      </div>
      {{- else -}}
      <div class="flex-shrink-0 w-36 h-36 md:w-48 md:h-48 bg-concrete border-2 border-zinc flex items-center justify-center">
        <span class="font-display text-5xl text-smoke">{{ substr (.Params.name | default .Title) 0 1 }}</span>
      </div>
      {{- end -}}

      <!-- Info -->
      <div class="flex-1 min-w-0">
        <span class="badge badge-gradient mb-5 inline-block">Artist</span>
        <h1 class="font-display text-4xl md:text-6xl text-paper text-balance mb-4 leading-none">
          {{ .Params.name | default .Title }}
        </h1>
        {{- with .Params.bio -}}
        <p class="text-chalk text-lg max-w-2xl leading-relaxed text-pretty mb-8 font-body font-light">{{ . }}</p>
        {{- end -}}

        <!-- Social links — values may be full URLs or bare slugs -->
        {{- with .Params.social -}}
        {{- $bases := dict "instagram" "https://instagram.com/" "x" "https://x.com/" "twitter" "https://x.com/" "github" "https://github.com/" "behance" "https://www.behance.net/" "artstation" "https://www.artstation.com/" -}}
        <div class="flex flex-wrap gap-3">
          {{- range $platform, $handle := . -}}
          {{- $href := cond (hasPrefix $handle "http") $handle (printf "%s%s" (index $bases $platform | default "#") $handle) -}}
          <a
            href="{{ $href }}"
            target="_blank"
            rel="noopener noreferrer"
            class="badge badge-outline"
          >{{ $platform | upper }} {{ partial "icon.html" "arrow-external" }}</a>
          {{- end -}}
        </div>
        {{- end -}}

        <!-- Post count -->
        {{- if $posts -}}
        <p class="label text-fog mt-6">{{ len $posts }} {{ if eq (len $posts) 1 }}editorial{{ else }}editorials{{ end }}</p>
        {{- end -}}
      </div>

    </div>
  </div>
</header>

<div class="gradient-line"></div>

<!-- ── AUTHOR BIO (Markdown body) ────────────────────────────── -->
{{- with .Content -}}
<div class="gutter-x py-12 border-b border-zinc">
  <div class="max-w-5xl mx-auto prose-editorial">{{ . }}</div>
</div>
{{- end -}}

<!-- ── AUTHOR'S POSTS ─────────────────────────────────────────── -->
{{- if $posts -}}
<section class="gutter-x py-16 md:py-24">
  <div class="max-w-5xl mx-auto">
  <div class="flex items-center gap-5 mb-10">
    <h2 class="font-display text-3xl md:text-5xl text-paper leading-none">
      Works by <span class="text-gradient">{{ .Params.name | default .Title }}</span>
    </h2>
    <div class="flex-1 gradient-line ml-2"></div>
  </div>
  <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-5">
    {{- range first 4 ($posts.ByDate.Reverse) -}}
    {{- partial "post-card.html" . -}}
    {{- end -}}
  </div>
  </div>
</section>
{{- else -}}
<div class="gutter-x py-24 text-center">
  <p class="label text-fog">No editorials published yet.</p>
</div>
{{- end -}}

{{- end -}}
