{{ define "main" }}
{{/*
  Load metrics - supports multi-repo with dynamic switching.
  Renders content for all repos, JavaScript handles showing the selected one.
*/}}
{{ $reposIndex := site.Data.repos }}

{{ if and $reposIndex $reposIndex.repositories }}
  {{/* ===== All-Repositories Summary View ===== */}}
  {{ $chartColors := slice "rgb(0, 102, 204)" "rgb(40, 167, 69)" "rgb(255, 193, 7)" "rgb(220, 53, 69)" "rgb(108, 117, 125)" "rgb(153, 102, 255)" "rgb(255, 159, 64)" }}

  {{/* Gather per-repo data once for totals, table rows, and chart series */}}
  {{ $totalStars := 0 }}
  {{ $totalForks := 0 }}
  {{ $totalOpenIssues := 0 }}
  {{ $totalOpenPRs := 0 }}
  {{ $totalActive := 0 }}
  {{ $totalAwaitingIssues := 0 }}
  {{ $totalAwaitingPRs := 0 }}
  {{ $rows := slice }}
  {{ $issuesSeries := slice }}
  {{ $pullsSeries := slice }}
  {{ $starsSeries := slice }}
  {{ $downloadsSeries := slice }}
  {{ $dlWeeklySeries := slice }}
  {{ $dlMonthlySeries := slice }}
  {{ $dlTotalSeries := slice }}
  {{ $allDates := slice }}
  {{ $allWeeks := slice }}
  {{ $allMonths := slice }}

  {{/* Global latest snapshot date — used as the trailing-week anchor so all repos bin to the same x-axis even if one lags. */}}
  {{ $latestDate := "" }}
  {{ range $rc := $reposIndex.repositories }}
    {{ with index site.Data.repos $rc.owner }}{{ with index . $rc.repo }}{{ range .snapshots }}
      {{ if gt .date $latestDate }}{{ $latestDate = .date }}{{ end }}
    {{ end }}{{ end }}{{ end }}
  {{ end }}
  {{ $latest := time.AsTime (cond (ne $latestDate "") $latestDate (now.Format "2006-01-02")) }}
  {{ $anyInbound := false }}
  {{ $anyDownloads := false }}

  {{ range $i, $rc := $reposIndex.repositories }}
    {{ $rm := false }}
    {{ $rsnaps := false }}
    {{ $ra := false }}
    {{ $ri := false }}
    {{ with index site.Data.repos $rc.owner }}{{ with index . $rc.repo }}
      {{ $rm = .metrics }}
      {{ $rsnaps = .snapshots }}
      {{ $ra = .actionability }}
      {{ $ri = .issues }}
    {{ end }}{{ end }}
    {{ if $rm }}
      {{ $totalStars = add $totalStars ($rm.repository.stars | default 0) }}
      {{ $totalForks = add $totalForks ($rm.repository.forks | default 0) }}
      {{ $totalOpenIssues = add $totalOpenIssues ($rm.issues.open_count | default 0) }}
      {{ $totalOpenPRs = add $totalOpenPRs ($rm.pulls.open_count | default 0) }}
      {{ $totalActive = add $totalActive ($rm.contributors.active_30d | default 0) }}
      {{ $totalAwaitingIssues = add $totalAwaitingIssues ($rm.issues.without_response_24h | default 0) }}
      {{ $totalAwaitingPRs = add $totalAwaitingPRs ($rm.pulls.without_review_24h | default 0) }}

      {{ $starsSpark := partial "get-sparkline.html" (dict "snapshots" $rsnaps "path" "repository.stars" "limit" 8) }}
      {{ $issuesSpark := partial "get-sparkline.html" (dict "snapshots" $rsnaps "path" "issues.open" "limit" 8) }}
      {{ $pullsSpark := partial "get-sparkline.html" (dict "snapshots" $rsnaps "path" "pulls.open" "limit" 8) }}

      {{ $hasAct := and $ra $ra.summary }}
      {{ $hasIss := and $ri $ri.summary }}
      {{ if or $hasAct $hasIss }}{{ $anyInbound = true }}{{ end }}

      {{ $dl := $rm.downloads }}
      {{ $dlDaily := 0 }}
      {{ $dlTrend := 0.0 }}
      {{ $dlWeekly := $dl.last_week | default 0 }}
      {{ $dlMonthly := $dl.last_month | default 0 }}
      {{ if $dl }}
        {{ $anyDownloads = true }}
        {{/* Daily from most-recent snapshot with a value — registry APIs lag 1-2 days so metrics.json's daily is often undefined. */}}
        {{ $spark := partial "get-sparkline.html" (dict "snapshots" $rsnaps "path" "downloads.daily" "limit" 2) }}
        {{ if $spark }}{{ $dlDaily = index $spark (sub (len $spark) 1) }}{{ end }}
        {{ $dlTrend = partial "calc-trend.html" (dict "sparkline" $spark) }}
        {{ if not $dlWeekly }}
          {{ $wk := partial "get-sparkline.html" (dict "snapshots" $rsnaps "path" "downloads.daily" "limit" 7) }}
          {{ range $wk }}{{ $dlWeekly = add $dlWeekly . }}{{ end }}
        {{ end }}
        {{ if not $dlMonthly }}
          {{ $mo := partial "get-sparkline.html" (dict "snapshots" $rsnaps "path" "downloads.daily" "limit" 30) }}
          {{ range $mo }}{{ $dlMonthly = add $dlMonthly . }}{{ end }}
        {{ end }}
      {{ end }}

      {{ $rows = $rows | append (dict
        "owner" $rc.owner
        "repo" $rc.repo
        "name" ($rc.name | default (printf "%s/%s" $rc.owner $rc.repo))
        "stars" $rm.repository.stars
        "starsTrend" (partial "calc-trend.html" (dict "sparkline" $starsSpark))
        "issues" $rm.issues.open_count
        "issuesTrend" (partial "calc-trend.html" (dict "sparkline" $issuesSpark))
        "pulls" $rm.pulls.open_count
        "pullsTrend" (partial "calc-trend.html" (dict "sparkline" $pullsSpark))
        "awaitingIssues" $rm.issues.without_response_24h
        "awaitingPRs" $rm.pulls.without_review_24h
        "active" $rm.contributors.active_30d
        "hasAct" $hasAct
        "actionablePRs" (cond (not (not $hasAct)) $ra.summary.actionable_count 0)
        "hasIss" $hasIss
        "bugsEnh" (cond (not (not $hasIss)) (add ($ri.summary.by_type.bug | default 0) ($ri.summary.by_type.enhancement | default 0)) 0)
        "triagePending" (cond (not (not $hasIss)) (add ($ri.summary.untriaged_count | default 0) ($ri.summary.partially_triaged_count | default 0)) 0)
        "p0Breach" (cond (not (not $hasIss)) $ri.summary.sla.p0_7d_breach 0)
        "p1Breach" (cond (not (not $hasIss)) $ri.summary.sla.p1_90d_breach 0)
        "hasDl" (not (not $dl))
        "dlDaily" $dlDaily
        "dlWeekly" $dlWeekly
        "dlMonthly" $dlMonthly
        "dlTotal" ($dl.total | default 0)
        "dlTrend" $dlTrend
        "pkg" $rc.package
      ) }}

      {{/* Build chart series from snapshots - use {x,y} points so repos with fewer snapshots align correctly */}}
      {{ if $rsnaps }}
        {{ $entries := slice }}
        {{ range $fn, $d := $rsnaps }}
          {{ $entries = $entries | append (dict "date" $d.date "issues" (index $d "issues" "open") "pulls" (index $d "pulls" "open") "stars" (index $d "repository" "stars") "dlDaily" (index $d "downloads" "daily") "dlTotal" (index $d "downloads" "total")) }}
        {{ end }}
        {{ $sorted := sort $entries "date" "asc" }}
        {{ $dates := slice }}{{ $iv := slice }}{{ $pv := slice }}{{ $sv := slice }}{{ $dv := slice }}{{ $tv := slice }}
        {{ $weekBins := dict }}
        {{ $monthBins := dict }}
        {{ range $sorted }}
          {{ $dates = $dates | append .date }}
          {{ if ne .issues nil }}{{ $iv = $iv | append (dict "x" .date "y" .issues) }}{{ end }}
          {{ if ne .pulls nil }}{{ $pv = $pv | append (dict "x" .date "y" .pulls) }}{{ end }}
          {{ if ne .stars nil }}{{ $sv = $sv | append (dict "x" .date "y" .stars) }}{{ end }}
          {{ if ne .dlDaily nil }}
            {{ $dv = $dv | append (dict "x" .date "y" .dlDaily) }}
            {{ $ago := int (div ($latest.Sub (time.AsTime .date)).Hours 24) }}
            {{ $bin := div $ago 7 }}
            {{ $key := ($latest.Add (time.ParseDuration (printf "-%dh" (mul 168 $bin)))).Format "2006-01-02" }}
            {{ $weekBins = merge $weekBins (dict $key (add (index $weekBins $key | default 0) .dlDaily)) }}
            {{ $mkey := (time.AsTime .date).Format "2006-01" }}
            {{ $monthBins = merge $monthBins (dict $mkey (add (index $monthBins $mkey | default 0) .dlDaily)) }}
          {{ end }}
          {{ if ne .dlTotal nil }}{{ $tv = $tv | append (dict "x" .date "y" .dlTotal) }}{{ end }}
        {{ end }}
        {{ $wv := slice }}
        {{ range $k, $v := $weekBins }}{{ $wv = $wv | append (dict "x" $k "y" $v) }}{{ end }}
        {{ $wv = sort $wv "x" "asc" }}
        {{/* Drop oldest bin when snapshot count mod 7 != 0 — it's a partial week that looks like a cliff. */}}
        {{ if and (gt (len $wv) 1) (ne (mod (len $dv) 7) 0) }}{{ $wv = after 1 $wv }}{{ end }}
        {{ range $wv }}{{ $allWeeks = $allWeeks | union (slice .x) }}{{ end }}
        {{ $mv := slice }}
        {{ range $k, $v := $monthBins }}{{ $mv = $mv | append (dict "x" $k "y" $v) }}{{ end }}
        {{ $mv = sort $mv "x" "asc" }}
        {{ range $mv }}{{ $allMonths = $allMonths | union (slice .x) }}{{ end }}
        {{ $allDates = $allDates | union $dates }}
        {{ $color := index $chartColors (mod $i (len $chartColors)) }}
        {{ $label := $rc.name | default (printf "%s/%s" $rc.owner $rc.repo) }}
        {{ $issuesSeries = $issuesSeries | append (dict "label" $label "data" $iv "borderColor" $color "backgroundColor" "transparent" "tension" 0.3) }}
        {{ $pullsSeries = $pullsSeries | append (dict "label" $label "data" $pv "borderColor" $color "backgroundColor" "transparent" "tension" 0.3) }}
        {{ $starsSeries = $starsSeries | append (dict "label" $label "data" $sv "borderColor" $color "backgroundColor" "transparent" "tension" 0.3) }}
        {{ if $dv }}{{ $downloadsSeries = $downloadsSeries | append (dict "label" $label "data" $dv "borderColor" $color "backgroundColor" "transparent" "tension" 0.3) }}{{ end }}
        {{ if $wv }}{{ $dlWeeklySeries = $dlWeeklySeries | append (dict "label" $label "data" $wv "borderColor" $color "backgroundColor" "transparent" "tension" 0.3) }}{{ end }}
        {{ if $mv }}{{ $dlMonthlySeries = $dlMonthlySeries | append (dict "label" $label "data" $mv "borderColor" $color "backgroundColor" "transparent" "tension" 0.3) }}{{ end }}
        {{ if $tv }}{{ $dlTotalSeries = $dlTotalSeries | append (dict "label" $label "data" $tv "borderColor" $color "backgroundColor" "transparent" "tension" 0.3) }}{{ end }}
      {{ end }}
    {{ end }}
  {{ end }}
  {{ $allDates = sort $allDates }}

  <div class="repo-content" data-repo="__all__">
    <div class="mb-6">
      <h1 class="text-2xl font-bold">All Repositories</h1>
    </div>

    <!-- Aggregate Totals -->
    <section class="mb-6">
      <h2 class="text-sm font-semibold mb-3">Totals</h2>
      <div class="grid grid-cols-2 sm:grid-cols-3 lg:grid-cols-6 gap-3">
        {{ partial "metric-card.html" (dict "label" "Stars" "value" $totalStars "info" "Combined GitHub stars across all repositories") }}
        {{ partial "metric-card.html" (dict "label" "Forks" "value" $totalForks "info" "Combined forks across all repositories") }}
        {{ partial "metric-card.html" (dict "label" "Open Issues" "value" $totalOpenIssues "info" "Combined open issues across all repositories") }}
        {{ partial "metric-card.html" (dict "label" "Open PRs" "value" $totalOpenPRs "info" "Combined open pull requests across all repositories") }}
        {{ partial "metric-card.html" (dict "label" "Awaiting" "value" (add $totalAwaitingIssues $totalAwaitingPRs) "descriptor" ">24h" "info" "Issues awaiting response + PRs awaiting review for over 24 hours") }}
        {{ partial "metric-card.html" (dict "label" "Active Contributors" "value" $totalActive "descriptor" "30d" "info" "Combined active contributors in the last 30 days") }}
      </div>
    </section>

    <!-- Per-Repo Comparison -->
    <section class="mb-6">
      <h2 class="text-sm font-semibold mb-3">By Repository</h2>
      <div class="rounded-lg border border-base-300 bg-base-100">
        <table class="table table-sm w-full">
          <thead>
            <tr>
              <th class="bg-base-200">Repository</th>
              <th class="bg-base-200 text-right">
                <span class="inline-flex items-center gap-1">Stars<span class="tooltip tooltip-top font-normal normal-case"><span class="tooltip-content text-xs">Total GitHub stars. Delta shows change over the last 7 days.</span><i class="fa-solid fa-circle-info opacity-40 cursor-help"></i></span></span>
              </th>
              <th class="bg-base-200 text-right">
                <span class="inline-flex items-center gap-1">Open Issues<span class="tooltip tooltip-top font-normal normal-case"><span class="tooltip-content text-xs">Currently open issues. Delta shows change over the last 7 days.</span><i class="fa-solid fa-circle-info opacity-40 cursor-help"></i></span></span>
              </th>
              <th class="bg-base-200 text-right">
                <span class="inline-flex items-center gap-1">Open PRs<span class="tooltip tooltip-top font-normal normal-case"><span class="tooltip-content text-xs">Currently open pull requests. Delta shows change over the last 7 days.</span><i class="fa-solid fa-circle-info opacity-40 cursor-help"></i></span></span>
              </th>
              <th class="bg-base-200 text-right">
                <span class="inline-flex items-center gap-1">Awaiting Response<span class="tooltip tooltip-left font-normal normal-case"><span class="tooltip-content text-xs">Open issues with no maintainer response after 24 hours</span><i class="fa-solid fa-circle-info opacity-40 cursor-help"></i></span></span>
              </th>
              <th class="bg-base-200 text-right">
                <span class="inline-flex items-center gap-1">Awaiting Review<span class="tooltip tooltip-left font-normal normal-case"><span class="tooltip-content text-xs">Open PRs with no review after 24 hours</span><i class="fa-solid fa-circle-info opacity-40 cursor-help"></i></span></span>
              </th>
              <th class="bg-base-200 text-right">
                <span class="inline-flex items-center gap-1">Active (30d)<span class="tooltip tooltip-left font-normal normal-case"><span class="tooltip-content text-xs">Contributors with activity in the last 30 days</span><i class="fa-solid fa-circle-info opacity-40 cursor-help"></i></span></span>
              </th>
            </tr>
          </thead>
          <tbody>
            {{ range $rows }}
            <tr class="hover:bg-base-200/50 cursor-pointer transition-colors" onclick="RepoPicker.setSelectedRepo('{{ .owner }}','{{ .repo }}')">
              <td>
                <div class="font-medium text-sm">{{ .name }}</div>
                <code class="font-mono text-[10px] text-base-content/50">{{ .owner }}/{{ .repo }}</code>
              </td>
              <td class="text-right">
                <span class="font-semibold">{{ lang.FormatNumber 0 .stars }}</span>
                {{ if ne .starsTrend 0.0 }}{{ $up := gt .starsTrend 0.0 }}{{ $abs := cond $up .starsTrend (mul .starsTrend -1) }}
                <span class="block text-[10px] opacity-60 {{ cond $up "text-success" "text-error" }}">{{ cond $up "↑" "↓" }}{{ printf "%.1f" $abs }}%</span>
                {{ end }}
              </td>
              <td class="text-right">
                <span class="font-semibold">{{ .issues }}</span>
                {{ if ne .issuesTrend 0.0 }}{{ $up := gt .issuesTrend 0.0 }}{{ $abs := cond $up .issuesTrend (mul .issuesTrend -1) }}
                <span class="block text-[10px] opacity-60 {{ cond $up "text-error" "text-success" }}">{{ cond $up "↑" "↓" }}{{ printf "%.1f" $abs }}%</span>
                {{ end }}
              </td>
              <td class="text-right">
                <span class="font-semibold">{{ .pulls }}</span>
                {{ if ne .pullsTrend 0.0 }}{{ $up := gt .pullsTrend 0.0 }}{{ $abs := cond $up .pullsTrend (mul .pullsTrend -1) }}
                <span class="block text-[10px] opacity-60 {{ cond $up "text-error" "text-success" }}">{{ cond $up "↑" "↓" }}{{ printf "%.1f" $abs }}%</span>
                {{ end }}
              </td>
              <td class="text-right font-semibold">{{ .awaitingIssues }}</td>
              <td class="text-right font-semibold">{{ .awaitingPRs }}</td>
              <td class="text-right font-semibold">{{ .active }}</td>
            </tr>
            {{ end }}
          </tbody>
        </table>
      </div>
    </section>

    <!-- Inbound Health (actionable/SLO view) -->
    {{ if $anyInbound }}
    <section class="mb-6">
      <h2 class="text-sm font-semibold mb-3">Inbound Health</h2>
      <div class="rounded-lg border border-base-300 bg-base-100">
        <table class="table table-sm w-full">
          <thead>
            <tr>
              <th class="bg-base-200">Repository</th>
              <th class="bg-base-200 text-right">
                <span class="inline-flex items-center gap-1">Actionable PRs<span class="tooltip tooltip-top font-normal normal-case"><span class="tooltip-content text-xs">PRs where the ball is in our court.<br>Green: ≤ 25 · Yellow: 26–50 · Red: &gt; 50</span><i class="fa-solid fa-circle-info opacity-40 cursor-help"></i></span></span>
              </th>
              <th class="bg-base-200 text-right">
                <span class="inline-flex items-center gap-1">Bugs + Enh<span class="tooltip tooltip-top font-normal normal-case"><span class="tooltip-content text-xs">Open issues labeled bug or enhancement.</span><i class="fa-solid fa-circle-info opacity-40 cursor-help"></i></span></span>
              </th>
              <th class="bg-base-200 text-right">
                <span class="inline-flex items-center gap-1">Triage Pending<span class="tooltip tooltip-top font-normal normal-case"><span class="tooltip-content text-xs">Issues missing a type or priority label.<br>Green: &lt; 5 · Yellow: 5–9 · Red: ≥ 10</span><i class="fa-solid fa-circle-info opacity-40 cursor-help"></i></span></span>
              </th>
              <th class="bg-base-200 text-right">
                <span class="inline-flex items-center gap-1">P0 &gt; 7d<span class="tooltip tooltip-left font-normal normal-case"><span class="tooltip-content text-xs">P0 issues open past the 7-day SEP-1730 SLA.<br>Green: 0 · Red: any</span><i class="fa-solid fa-circle-info opacity-40 cursor-help"></i></span></span>
              </th>
              <th class="bg-base-200 text-right">
                <span class="inline-flex items-center gap-1">P1 &gt; 90d<span class="tooltip tooltip-left font-normal normal-case"><span class="tooltip-content text-xs">P1 issues open past 90 days — surfaces aspirational-P1 rot.<br>Green: 0 · Yellow: 1–4 · Red: ≥ 5</span><i class="fa-solid fa-circle-info opacity-40 cursor-help"></i></span></span>
              </th>
            </tr>
          </thead>
          <tbody>
            {{ $dash := "<span class=\"opacity-30\">—</span>" | safeHTML }}
            {{ range $rows }}
            <tr class="hover:bg-base-200/50 cursor-pointer transition-colors" onclick="location.href='{{ "pulls/" | relURL }}?repo={{ .owner }}%2F{{ .repo }}'">
              <td>
                <div class="font-medium text-sm">{{ .name }}</div>
                <code class="font-mono text-[10px] text-base-content/50">{{ .owner }}/{{ .repo }}</code>
              </td>
              <td class="text-right font-semibold">
                {{- if .hasAct -}}
                  {{- $c := cond (le .actionablePRs 25) "text-success" (cond (le .actionablePRs 50) "text-warning" "text-error") -}}
                  <span class="{{ $c }}">{{ .actionablePRs }}</span>
                {{- else }}{{ $dash }}{{ end -}}
              </td>
              <td class="text-right font-semibold">
                {{- if .hasIss }}{{ .bugsEnh }}{{ else }}{{ $dash }}{{ end -}}
              </td>
              <td class="text-right font-semibold">
                {{- if .hasIss -}}
                  {{- $c := cond (lt .triagePending 5) "text-success" (cond (lt .triagePending 10) "text-warning" "text-error") -}}
                  <span class="{{ $c }}">{{ .triagePending }}</span>
                {{- else }}{{ $dash }}{{ end -}}
              </td>
              <td class="text-right font-semibold">
                {{- if .hasIss -}}
                  {{- $c := cond (gt .p0Breach 0) "text-error" "text-success" -}}
                  <span class="{{ $c }}">{{ .p0Breach }}</span>
                {{- else }}{{ $dash }}{{ end -}}
              </td>
              <td class="text-right font-semibold">
                {{- if .hasIss -}}
                  {{- $c := cond (eq .p1Breach 0) "text-success" (cond (lt .p1Breach 5) "text-warning" "text-error") -}}
                  <span class="{{ $c }}">{{ .p1Breach }}</span>
                {{- else }}{{ $dash }}{{ end -}}
              </td>
            </tr>
            {{ end }}
          </tbody>
        </table>
      </div>
    </section>
    {{ end }}

    <!-- SDK Downloads -->
    {{ if $anyDownloads }}
    <section class="mb-6">
      <h2 class="text-sm font-semibold mb-3">SDK Downloads</h2>
      <div class="rounded-lg p-4 mb-3 text-sm flex gap-3 items-start" style="background-color: color-mix(in oklab, var(--color-error) 10%, transparent)">
        <i class="fa-solid fa-triangle-exclamation text-error text-xl shrink-0"></i>
        <span><strong class="text-error">Total column caveat:</strong> npm doesn't expose all-time cumulative download counts, so that total is a self-computed running sum — treat it as a trend indicator, not a true lifetime total. PyPI totals come from BigQuery's full download log history starting from the package's first-published date.</span>
      </div>
      <div class="rounded-lg border border-base-300 bg-base-100">
        <table class="table table-sm w-full">
          <thead>
            <tr>
              <th class="bg-base-200">SDK</th>
              <th class="bg-base-200 text-right">
                <span class="inline-flex items-center gap-1">Daily<span class="tooltip tooltip-top font-normal normal-case"><span class="tooltip-content text-xs">Downloads on the most recent day with data. Delta compares to the day before.<br>NuGet derives this from cumulative-total diffs, so C# needs two days of data before a value appears.</span><i class="fa-solid fa-circle-info opacity-40 cursor-help"></i></span></span>
              </th>
              <th class="bg-base-200 text-right">
                <span class="inline-flex items-center gap-1">Weekly<span class="tooltip tooltip-top font-normal normal-case"><span class="tooltip-content text-xs">Downloads over the last 7 days.</span><i class="fa-solid fa-circle-info opacity-40 cursor-help"></i></span></span>
              </th>
              <th class="bg-base-200 text-right">
                <span class="inline-flex items-center gap-1">Monthly<span class="tooltip tooltip-top font-normal normal-case"><span class="tooltip-content text-xs">Downloads over the last 30 days.</span><i class="fa-solid fa-circle-info opacity-40 cursor-help"></i></span></span>
              </th>
              <th class="bg-base-200 text-right">
                <span class="inline-flex items-center gap-1">Total<span class="tooltip tooltip-left font-normal normal-case"><span class="tooltip-content text-xs">Cumulative downloads. Native where the registry reports it; npm is a running sum we maintain from daily counts; PyPI comes from BigQuery's full download log history.</span><i class="fa-solid fa-circle-info opacity-40 cursor-help"></i></span></span>
              </th>
            </tr>
          </thead>
          <tbody>
            {{ $dash := "<span class=\"opacity-30\">—</span>" | safeHTML }}
            {{ $regUrl := dict "npm" "https://www.npmjs.com/package/" "pypi" "https://pypi.org/project/" "nuget" "https://www.nuget.org/packages/" }}
            {{ range $rows }}{{ if .hasDl }}
            <tr class="hover:bg-base-200/50">
              <td>
                <div class="font-medium text-sm">{{ .name }}</div>
                {{ with .pkg }}<a href="{{ index $regUrl .registry }}{{ .name }}" target="_blank" rel="noopener" class="font-mono text-[10px] opacity-50 hover:opacity-100 hover:underline">{{ .registry }} · {{ .name }}</a>{{ end }}
              </td>
              <td class="text-right">
                {{ if .dlDaily }}
                <span class="font-semibold">{{ lang.FormatNumber 0 .dlDaily }}</span>
                {{ if ne .dlTrend 0.0 }}{{ $up := gt .dlTrend 0.0 }}{{ $abs := cond $up .dlTrend (mul .dlTrend -1) }}
                <span class="block text-[10px] opacity-60 {{ cond $up "text-success" "text-error" }}">{{ cond $up "↑" "↓" }}{{ printf "%.1f" $abs }}%</span>
                {{ end }}
                {{ else }}{{ $dash }}{{ end }}
              </td>
              <td class="text-right font-semibold">{{ if .dlWeekly }}{{ lang.FormatNumber 0 .dlWeekly }}{{ else }}{{ $dash }}{{ end }}</td>
              <td class="text-right font-semibold">{{ if .dlMonthly }}{{ lang.FormatNumber 0 .dlMonthly }}{{ else }}{{ $dash }}{{ end }}</td>
              <td class="text-right font-semibold">{{ if .dlTotal }}{{ lang.FormatNumber 0 .dlTotal }}{{ else }}{{ $dash }}{{ end }}</td>
            </tr>
            {{ end }}{{ end }}
          </tbody>
        </table>
      </div>
      <div class="space-y-4 mt-4">
        {{ if $downloadsSeries }}
        <div class="bg-base-100 border border-base-300 rounded-lg p-4">
          <h3 class="text-xs font-medium opacity-70 mb-3">Daily Downloads <span class="opacity-60">(UTC)</span></h3>
          <canvas data-chart='{"type":"line","data":{"labels":{{ $allDates | jsonify }},"datasets":{{ $downloadsSeries | jsonify }}},"options":{"responsive":true,"plugins":{"legend":{"position":"bottom","labels":{"boxWidth":12,"padding":12}}}}}'></canvas>
        </div>
        {{ end }}
        {{ if $dlWeeklySeries }}
        <div class="bg-base-100 border border-base-300 rounded-lg p-4">
          <h3 class="text-xs font-medium opacity-70 mb-3">Weekly Downloads <span class="opacity-60">(UTC)</span></h3>
          <canvas data-chart='{"type":"line","data":{"labels":{{ (sort $allWeeks) | jsonify }},"datasets":{{ $dlWeeklySeries | jsonify }}},"options":{"responsive":true,"plugins":{"legend":{"position":"bottom","labels":{"boxWidth":12,"padding":12}}}}}'></canvas>
        </div>
        {{ end }}
        {{ if $dlMonthlySeries }}
        <div class="bg-base-100 border border-base-300 rounded-lg p-4">
          <h3 class="text-xs font-medium opacity-70 mb-3">Monthly Downloads <span class="opacity-60">(UTC)</span></h3>
          <canvas data-chart='{"type":"line","data":{"labels":{{ (sort $allMonths) | jsonify }},"datasets":{{ $dlMonthlySeries | jsonify }}},"options":{"responsive":true,"plugins":{"legend":{"position":"bottom","labels":{"boxWidth":12,"padding":12}}}}}'></canvas>
        </div>
        {{ end }}
        {{ if $dlTotalSeries }}
        <div class="bg-base-100 border border-base-300 rounded-lg p-4">
          <h3 class="text-xs font-medium opacity-70 mb-3">Total Downloads <span class="opacity-60">(UTC)</span></h3>
          <canvas data-chart='{"type":"line","data":{"labels":{{ $allDates | jsonify }},"datasets":{{ $dlTotalSeries | jsonify }}},"options":{"responsive":true,"plugins":{"legend":{"position":"bottom","labels":{"boxWidth":12,"padding":12}}}}}'></canvas>
        </div>
        {{ end }}
      </div>
    </section>
    {{ end }}

    <!-- Combined Trend Charts -->
    {{ if gt (len $allDates) 1 }}
    <section class="mb-6">
      <h2 class="text-sm font-semibold mb-3">Trends</h2>
      <div class="space-y-4">
        <div class="bg-base-100 border border-base-300 rounded-lg p-4">
          <h3 class="text-xs font-medium text-base-content/70 mb-3">Open Issues <span class="opacity-60">(UTC)</span></h3>
          <canvas data-chart='{"type":"line","data":{"labels":{{ $allDates | jsonify }},"datasets":{{ $issuesSeries | jsonify }}},"options":{"responsive":true,"plugins":{"legend":{"position":"bottom","labels":{"boxWidth":12,"padding":12}}}}}'></canvas>
        </div>
        <div class="bg-base-100 border border-base-300 rounded-lg p-4">
          <h3 class="text-xs font-medium text-base-content/70 mb-3">Open PRs <span class="opacity-60">(UTC)</span></h3>
          <canvas data-chart='{"type":"line","data":{"labels":{{ $allDates | jsonify }},"datasets":{{ $pullsSeries | jsonify }}},"options":{"responsive":true,"plugins":{"legend":{"position":"bottom","labels":{"boxWidth":12,"padding":12}}}}}'></canvas>
        </div>
        <div class="bg-base-100 border border-base-300 rounded-lg p-4">
          <h3 class="text-xs font-medium text-base-content/70 mb-3">Stars <span class="opacity-60">(UTC)</span></h3>
          <canvas data-chart='{"type":"line","data":{"labels":{{ $allDates | jsonify }},"datasets":{{ $starsSeries | jsonify }}},"options":{"responsive":true,"plugins":{"legend":{"position":"bottom","labels":{"boxWidth":12,"padding":12}}}}}'></canvas>
        </div>
      </div>
    </section>
    {{ end }}
  </div>

  {{/* Multi-repo mode - render content for each repository */}}
  {{ range $index, $repoConfig := $reposIndex.repositories }}
    {{ $owner := $repoConfig.owner }}
    {{ $repo := $repoConfig.repo }}
    {{ $repoKey := printf "%s/%s" $owner $repo }}
    {{ $repoName := $repoConfig.name | default $repoKey }}
    {{ $m := false }}

    {{ $snapshots := false }}
    {{ with index site.Data.repos $owner }}
      {{ with index . $repo }}
        {{ $m = .metrics }}
        {{ $snapshots = .snapshots }}
      {{ end }}
    {{ end }}

    {{/* Extract sparkline data for key metrics */}}
    {{ $starsSparkline := partial "get-sparkline.html" (dict "snapshots" $snapshots "path" "repository.stars") }}
    {{ $forksSparkline := partial "get-sparkline.html" (dict "snapshots" $snapshots "path" "repository.forks") }}
    {{ $contributorsSparkline := partial "get-sparkline.html" (dict "snapshots" $snapshots "path" "contributors.active_30d") }}
    {{ $issuesSparkline := partial "get-sparkline.html" (dict "snapshots" $snapshots "path" "issues.open") }}
    {{ $issuesAwaitingSparkline := partial "get-sparkline.html" (dict "snapshots" $snapshots "path" "issues.without_response_24h") }}
    {{ $issuesResponseMedianSparkline := partial "get-sparkline.html" (dict "snapshots" $snapshots "path" "issues.response_time.median_hours") }}
    {{ $pullsSparkline := partial "get-sparkline.html" (dict "snapshots" $snapshots "path" "pulls.open") }}
    {{ $pullsAwaitingSparkline := partial "get-sparkline.html" (dict "snapshots" $snapshots "path" "pulls.without_review_24h") }}
    {{ $pullsReviewMedianSparkline := partial "get-sparkline.html" (dict "snapshots" $snapshots "path" "pulls.review_time.median_hours") }}

    <div class="repo-content" data-repo="{{ $repoKey }}" style="display: none;">
      {{ if not $m }}
      <div class="alert alert-warning">
        <svg xmlns="http://www.w3.org/2000/svg" class="stroke-current shrink-0 h-6 w-6" fill="none" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z" /></svg>
        <div>
          <h3 class="font-bold">No data available</h3>
          <div class="text-sm">Run the aggregation script to populate metrics data for {{ $repoName }}.</div>
        </div>
      </div>
      {{ else }}

      <div class="mb-6">
        <h1 class="text-2xl font-bold">{{ $repoName }}</h1>
        <a href="https://github.com/{{ $repoKey }}" target="_blank" rel="noopener" class="inline-flex items-center gap-1.5 text-xs text-base-content/50 hover:text-base-content/70 mt-1">
          <svg class="w-3.5 h-3.5" fill="currentColor" viewBox="0 0 24 24"><path d="M12 0c-6.626 0-12 5.373-12 12 0 5.302 3.438 9.8 8.207 11.387.599.111.793-.261.793-.577v-2.234c-3.338.726-4.033-1.416-4.033-1.416-.546-1.387-1.333-1.756-1.333-1.756-1.089-.745.083-.729.083-.729 1.205.084 1.839 1.237 1.839 1.237 1.07 1.834 2.807 1.304 3.492.997.107-.775.418-1.305.762-1.604-2.665-.305-5.467-1.334-5.467-5.931 0-1.311.469-2.381 1.236-3.221-.124-.303-.535-1.524.117-3.176 0 0 1.008-.322 3.301 1.23.957-.266 1.983-.399 3.003-.404 1.02.005 2.047.138 3.006.404 2.291-1.552 3.297-1.23 3.297-1.23.653 1.653.242 2.874.118 3.176.77.84 1.235 1.911 1.235 3.221 0 4.609-2.807 5.624-5.479 5.921.43.372.823 1.102.823 2.222v3.293c0 .319.192.694.801.576 4.765-1.589 8.199-6.086 8.199-11.386 0-6.627-5.373-12-12-12z"/></svg>
          <code class="font-mono">{{ $repoKey }}</code>
        </a>
      </div>

      {{/* Calculate trends from sparkline data */}}
      {{ $starsTrend := partial "calc-trend.html" (dict "sparkline" $starsSparkline) }}
      {{ $forksTrend := partial "calc-trend.html" (dict "sparkline" $forksSparkline) }}
      {{ $contributorsTrend := partial "calc-trend.html" (dict "sparkline" $contributorsSparkline) }}
      {{ $issuesTrend := partial "calc-trend.html" (dict "sparkline" $issuesSparkline) }}
      {{ $issuesAwaitingTrend := partial "calc-trend.html" (dict "sparkline" $issuesAwaitingSparkline) }}
      {{ $issuesResponseTrend := partial "calc-trend.html" (dict "sparkline" $issuesResponseMedianSparkline) }}
      {{ $pullsTrend := partial "calc-trend.html" (dict "sparkline" $pullsSparkline) }}
      {{ $pullsAwaitingTrend := partial "calc-trend.html" (dict "sparkline" $pullsAwaitingSparkline) }}
      {{ $pullsReviewTrend := partial "calc-trend.html" (dict "sparkline" $pullsReviewMedianSparkline) }}

      {{/* Build GitHub filter URLs */}}
      {{ $ghRepoUrl := printf "https://github.com/%s" $repoKey }}
      {{ $openIssuesLink := printf "%s/issues?q=is:issue+is:open" $ghRepoUrl }}
      {{ $opened7dIssuesLink := printf "%s/issues?q=is:issue+created:>%s" $ghRepoUrl (now.AddDate 0 0 -7 | time.Format "2006-01-02") }}
      {{ $opened30dIssuesLink := printf "%s/issues?q=is:issue+created:>%s" $ghRepoUrl (now.AddDate 0 0 -30 | time.Format "2006-01-02") }}
      {{ $closed7dIssuesLink := printf "%s/issues?q=is:issue+is:closed+closed:>%s" $ghRepoUrl (now.AddDate 0 0 -7 | time.Format "2006-01-02") }}
      {{ $closed30dIssuesLink := printf "%s/issues?q=is:issue+is:closed+closed:>%s" $ghRepoUrl (now.AddDate 0 0 -30 | time.Format "2006-01-02") }}
      {{ $noResponse24hLink := printf "%s/issues?q=is:issue+is:open+comments:0+created:<=%s+sort:created-asc" $ghRepoUrl (now.AddDate 0 0 -1 | time.Format "2006-01-02") }}
      {{ $openPRsLink := printf "%s/pulls?q=is:pr+is:open" $ghRepoUrl }}
      {{ $opened7dPRsLink := printf "%s/pulls?q=is:pr+created:>%s" $ghRepoUrl (now.AddDate 0 0 -7 | time.Format "2006-01-02") }}
      {{ $opened30dPRsLink := printf "%s/pulls?q=is:pr+created:>%s" $ghRepoUrl (now.AddDate 0 0 -30 | time.Format "2006-01-02") }}
      {{ $merged7dPRsLink := printf "%s/pulls?q=is:pr+is:merged+merged:>%s" $ghRepoUrl (now.AddDate 0 0 -7 | time.Format "2006-01-02") }}
      {{ $merged30dPRsLink := printf "%s/pulls?q=is:pr+is:merged+merged:>%s" $ghRepoUrl (now.AddDate 0 0 -30 | time.Format "2006-01-02") }}
      {{ $noReview24hLink := printf "%s/pulls?q=is:pr+is:open+review:none+created:<=%s+sort:created-asc" $ghRepoUrl (now.AddDate 0 0 -1 | time.Format "2006-01-02") }}
      {{ $starsLink := printf "%s/stargazers" $ghRepoUrl }}
      {{ $forksLink := printf "%s/forks" $ghRepoUrl }}

      <!-- Repository Stats -->
      <section class="mb-6">
        <h2 class="text-sm font-semibold mb-3">Repository</h2>
        <div class="grid grid-cols-2 sm:grid-cols-4 gap-3">
          {{ partial "metric-card.html" (dict "label" "Stars" "value" $m.repository.stars "info" "Total GitHub stars on this repository" "sparkline" $starsSparkline "sparklineColor" "rgb(255, 193, 7)" "trend" $starsTrend "trendGoodDirection" "up" "link" $starsLink) }}
          {{ partial "metric-card.html" (dict "label" "Forks" "value" $m.repository.forks "info" "Total forks of this repository" "sparkline" $forksSparkline "sparklineColor" "rgb(108, 117, 125)" "trend" $forksTrend "trendGoodDirection" "up" "link" $forksLink) }}
          {{ partial "metric-card.html" (dict "label" "Total Contributors" "value" $m.contributors.total_known "info" "All known contributors who have ever contributed") }}
          {{ partial "metric-card.html" (dict "label" "Active (30d)" "value" $m.contributors.active_30d "info" "Contributors with activity in the last 30 days" "sparkline" $contributorsSparkline "sparklineColor" "rgb(40, 167, 69)" "trend" $contributorsTrend "trendGoodDirection" "up") }}
        </div>
      </section>

      <!-- Issues Summary -->
      <section class="mb-6">
        <div class="flex justify-between items-center mb-3">
          <h2 class="text-sm font-semibold">Issues</h2>
          <a href="{{ "issues/" | relURL }}" class="text-sm text-base-content/60 hover:text-base-content">View details →</a>
        </div>
        <div class="grid grid-cols-2 sm:grid-cols-4 gap-3 mb-4">
          {{ partial "metric-card.html" (dict "label" "Open Issues" "value" $m.issues.open_count "info" "Currently open issues" "sparkline" $issuesSparkline "sparklineColor" "rgb(0, 102, 204)" "trend" $issuesTrend "trendGoodDirection" "down" "link" $openIssuesLink) }}
          {{ partial "metric-card.html" (dict "label" "Opened" "value" $m.issues.opened_7d "descriptor" "last 7 days" "info" "Issues opened in the last 7 days" "link" $opened7dIssuesLink) }}
          {{ partial "metric-card.html" (dict "label" "Opened" "value" $m.issues.opened_30d "descriptor" "last 30 days" "info" "Issues opened in the last 30 days" "link" $opened30dIssuesLink) }}
          {{ partial "metric-card.html" (dict "label" "Awaiting Response" "value" $m.issues.without_response_24h "descriptor" ">24 hours" "info" "Open issues with no maintainer response after 24 hours" "sparkline" $issuesAwaitingSparkline "sparklineColor" "rgb(220, 53, 69)" "trend" $issuesAwaitingTrend "trendGoodDirection" "down" "link" $noResponse24hLink) }}
          {{ partial "metric-card.html" (dict "label" "Closed" "value" $m.issues.closed_7d "descriptor" "last 7 days" "info" "Issues closed in the last 7 days" "link" $closed7dIssuesLink) }}
          {{ partial "metric-card.html" (dict "label" "Closed" "value" $m.issues.closed_30d "descriptor" "last 30 days" "info" "Issues closed in the last 30 days" "link" $closed30dIssuesLink) }}
        </div>
        <h3 class="text-xs font-semibold text-base-content/70 mb-3">Response Time</h3>
        <div class="grid grid-cols-2 sm:grid-cols-4 gap-3">
          {{ partial "metric-card.html" (dict "label" "Average" "value" (partial "format-hours.html" $m.issues.response_time.avg_hours) "info" "Average time to first response on issues") }}
          {{ partial "metric-card.html" (dict "label" "Median" "value" (partial "format-hours.html" $m.issues.response_time.median_hours) "info" "Median time to first response (50th percentile)" "sparkline" $issuesResponseMedianSparkline "sparklineColor" "rgb(108, 117, 125)" "trend" $issuesResponseTrend "trendGoodDirection" "down") }}
          {{ partial "metric-card.html" (dict "label" "P90" "value" (partial "format-hours.html" $m.issues.response_time.p90_hours) "info" "90th percentile response time") }}
          {{ partial "metric-card.html" (dict "label" "P95" "value" (partial "format-hours.html" $m.issues.response_time.p95_hours) "info" "95th percentile response time") }}
        </div>
      </section>

      <!-- PRs Summary -->
      <section class="mb-6">
        <div class="flex justify-between items-center mb-3">
          <h2 class="text-sm font-semibold">Pull Requests</h2>
          <a href="{{ "pulls/" | relURL }}" class="text-sm text-base-content/60 hover:text-base-content">View details →</a>
        </div>
        <div class="grid grid-cols-2 sm:grid-cols-4 gap-3 mb-4">
          {{ partial "metric-card.html" (dict "label" "Open PRs" "value" $m.pulls.open_count "info" "Currently open pull requests" "sparkline" $pullsSparkline "sparklineColor" "rgb(40, 167, 69)" "trend" $pullsTrend "trendGoodDirection" "down" "link" $openPRsLink) }}
          {{ partial "metric-card.html" (dict "label" "Opened" "value" $m.pulls.opened_7d "descriptor" "last 7 days" "info" "PRs opened in the last 7 days" "link" $opened7dPRsLink) }}
          {{ partial "metric-card.html" (dict "label" "Opened" "value" $m.pulls.opened_30d "descriptor" "last 30 days" "info" "PRs opened in the last 30 days" "link" $opened30dPRsLink) }}
          {{ partial "metric-card.html" (dict "label" "Awaiting Review" "value" $m.pulls.without_review_24h "descriptor" ">24 hours" "info" "Open PRs with no review after 24 hours" "sparkline" $pullsAwaitingSparkline "sparklineColor" "rgb(220, 53, 69)" "trend" $pullsAwaitingTrend "trendGoodDirection" "down" "link" $noReview24hLink) }}
          {{ partial "metric-card.html" (dict "label" "Merged" "value" $m.pulls.merged_7d "descriptor" "last 7 days" "info" "PRs merged in the last 7 days" "link" $merged7dPRsLink) }}
          {{ partial "metric-card.html" (dict "label" "Merged" "value" $m.pulls.merged_30d "descriptor" "last 30 days" "info" "PRs merged in the last 30 days" "link" $merged30dPRsLink) }}
        </div>
        <h3 class="text-xs font-semibold text-base-content/70 mb-3">Review Time</h3>
        <div class="grid grid-cols-2 sm:grid-cols-4 gap-3">
          {{ partial "metric-card.html" (dict "label" "Average" "value" (partial "format-hours.html" $m.pulls.review_time.avg_hours) "info" "Average time to first review on PRs") }}
          {{ partial "metric-card.html" (dict "label" "Median" "value" (partial "format-hours.html" $m.pulls.review_time.median_hours) "info" "Median time to first review (50th percentile)" "sparkline" $pullsReviewMedianSparkline "sparklineColor" "rgb(108, 117, 125)" "trend" $pullsReviewTrend "trendGoodDirection" "down") }}
          {{ partial "metric-card.html" (dict "label" "P90" "value" (partial "format-hours.html" $m.pulls.review_time.p90_hours) "info" "90th percentile review time") }}
          {{ partial "metric-card.html" (dict "label" "P95" "value" (partial "format-hours.html" $m.pulls.review_time.p95_hours) "info" "95th percentile review time") }}
        </div>
      </section>

      {{/* Additional links for At a Glance */}}
      {{ $stale30dLink := printf "%s/issues?q=is:issue+is:open+updated:<=%s+sort:updated-asc" $ghRepoUrl (now.AddDate 0 0 -30 | time.Format "2006-01-02") }}
      {{ $stale60dLink := printf "%s/issues?q=is:issue+is:open+updated:<=%s+sort:updated-asc" $ghRepoUrl (now.AddDate 0 0 -60 | time.Format "2006-01-02") }}
      {{ $stale90dLink := printf "%s/issues?q=is:issue+is:open+updated:<=%s+sort:updated-asc" $ghRepoUrl (now.AddDate 0 0 -90 | time.Format "2006-01-02") }}
      {{ $draftPRsLink := printf "%s/pulls?q=is:pr+is:open+draft:true" $ghRepoUrl }}
      {{ $closedUnmerged90dLink := printf "%s/pulls?q=is:pr+is:closed+is:unmerged+closed:>%s" $ghRepoUrl (now.AddDate 0 0 -90 | time.Format "2006-01-02") }}
      {{/* Calculate stale issue percentages (as % of open issues) */}}
      {{ $stale30dPct := partial "calc-percent.html" (dict "numerator" $m.issues.stale_30d "denominator" $m.issues.open_count) }}
      {{ $stale60dPct := partial "calc-percent.html" (dict "numerator" $m.issues.stale_60d "denominator" $m.issues.open_count) }}
      {{ $stale90dPct := partial "calc-percent.html" (dict "numerator" $m.issues.stale_90d "denominator" $m.issues.open_count) }}
      {{/* PR rejection rate is now pre-computed in metrics */}}
      {{ $prRejectRate90d := $m.pulls.rejection_rate_pct | default 0.0 }}

      <!-- Quick Stats -->
      <section class="mb-6">
        <h2 class="text-sm font-semibold mb-3">At a Glance</h2>
        <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
          <div>
            <h3 class="text-xs font-medium text-base-content/70 mb-2">Issue Health</h3>
            <div class="overflow-x-auto rounded-lg border border-base-300 bg-base-100">
              <table class="table table-sm table-bordered table-fixed w-full">
                <thead>
                  <tr>
                    <th class="bg-base-200 w-1/2">Metric</th>
                    <th class="bg-base-200 w-1/2 text-left">Value</th>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    <td>Label Coverage</td>
                    <td class="text-left font-semibold">{{ printf "%.0f%%" ($m.issues.label_coverage_pct | default 0) }}</td>
                  </tr>
                  <tr>
                    <td>Median Close Time</td>
                    <td class="text-left font-semibold">{{ printf "%.1f" ($m.issues.close_time.median_days | default 0) }}d</td>
                  </tr>
                  <tr class="hover:bg-base-200/50 cursor-pointer transition-colors" onclick="window.open('{{ $stale30dLink }}', '_blank')">
                    <td>Stale (>30d)</td>
                    <td class="text-left font-semibold">{{ $m.issues.stale_30d }}{{ if gt $stale30dPct 0.0 }} <span class="text-xs font-normal text-base-content/50">({{ printf "%.0f" $stale30dPct }}%)</span>{{ end }}</td>
                  </tr>
                  <tr class="hover:bg-base-200/50 cursor-pointer transition-colors" onclick="window.open('{{ $stale90dLink }}', '_blank')">
                    <td>Stale (>90d)</td>
                    <td class="text-left font-semibold">{{ $m.issues.stale_90d }}{{ if gt $stale90dPct 0.0 }} <span class="text-xs font-normal text-base-content/50">({{ printf "%.0f" $stale90dPct }}%)</span>{{ end }}</td>
                  </tr>
                  <tr>
                    <td>Reopen Rate</td>
                    <td class="text-left font-semibold">{{ printf "%.0f%%" (mul $m.issues.reopen_rate 100) }}</td>
                  </tr>
                </tbody>
              </table>
            </div>
          </div>
          <div>
            <h3 class="text-xs font-medium text-base-content/70 mb-2">PR Health</h3>
            <div class="overflow-x-auto rounded-lg border border-base-300 bg-base-100">
              <table class="table table-sm table-bordered table-fixed w-full">
                <thead>
                  <tr>
                    <th class="bg-base-200 w-1/2">Metric</th>
                    <th class="bg-base-200 w-1/2 text-left">Value</th>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    <td>Code Review Rate</td>
                    <td class="text-left font-semibold">{{ printf "%.0f%%" ($m.pulls.code_review_rate_pct | default 0) }}</td>
                  </tr>
                  <tr>
                    <td>Avg Reviews/PR</td>
                    <td class="text-left font-semibold">{{ printf "%.1f" ($m.pulls.avg_reviews_per_pr | default 0) }}</td>
                  </tr>
                  <tr class="hover:bg-base-200/50 cursor-pointer transition-colors" onclick="window.open('{{ $closedUnmerged90dLink }}', '_blank')">
                    <td>Rejection Rate (90d)</td>
                    <td class="text-left font-semibold">{{ printf "%.0f%%" $prRejectRate90d }}</td>
                  </tr>
                  <tr class="hover:bg-base-200/50 cursor-pointer transition-colors" onclick="window.open('{{ $draftPRsLink }}', '_blank')">
                    <td>Draft PRs</td>
                    <td class="text-left font-semibold">{{ $m.pulls.draft_count }}</td>
                  </tr>
                  <tr>
                    <td>Median Merge Time</td>
                    <td class="text-left font-semibold">{{ partial "format-hours.html" $m.pulls.merge_time.median_hours }}</td>
                  </tr>
                </tbody>
              </table>
            </div>
          </div>
          <div>
            <h3 class="text-xs font-medium text-base-content/70 mb-2">Contributor Health</h3>
            <div class="overflow-x-auto rounded-lg border border-base-300 bg-base-100">
              <table class="table table-sm table-bordered table-fixed w-full">
                <thead>
                  <tr>
                    <th class="bg-base-200 w-1/2">Metric</th>
                    <th class="bg-base-200 w-1/2 text-left">Value</th>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    <td>Retention Rate</td>
                    <td class="text-left font-semibold">{{ printf "%.0f%%" ($m.contributors.retention_rate_pct | default 0) }}</td>
                  </tr>
                  <tr>
                    <td>Churned (30d)</td>
                    <td class="text-left font-semibold">{{ $m.contributors.churned_30d | default 0 }}</td>
                  </tr>
                  <tr>
                    <td>Commits/Week (avg)</td>
                    <td class="text-left font-semibold">{{ printf "%.1f" ($m.contributors.commits_per_week_avg | default 0) }}</td>
                  </tr>
                  <tr>
                    <td>First-Time (30d)</td>
                    <td class="text-left font-semibold">{{ $m.contributors.first_time_30d | default 0 }}</td>
                  </tr>
                  <tr>
                    <td>Active (30d)</td>
                    <td class="text-left font-semibold">{{ $m.contributors.active_30d }}</td>
                  </tr>
                </tbody>
              </table>
            </div>
          </div>
        </div>
      </section>

      {{ end }}
    </div>
  {{ end }}
{{ else }}
  {{/* Legacy single-repo mode */}}
  {{ $m := site.Data.metrics }}
  {{ $repoName := site.Params.repo | default "Repository" }}

  {{ if not $m }}
  <div class="alert alert-warning">
    <svg xmlns="http://www.w3.org/2000/svg" class="stroke-current shrink-0 h-6 w-6" fill="none" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z" /></svg>
    <div>
      <h3 class="font-bold">No data available</h3>
      <div class="text-sm">Run the aggregation script to populate metrics data.</div>
    </div>
  </div>
  {{ else }}
  <h1 class="text-3xl font-bold mb-6">{{ $repoName }}</h1>
  <!-- Content for legacy mode would go here -->
  {{ end }}
{{ end }}
{{ end }}
