{{ define "search/fragments/resultCard" }}
  {{ $owner := resolve .Repo.Did }}
  {{ $slug  := .Repo.Slug }}

  <div class="bg-white dark:bg-gray-800 p-3 flex flex-col gap-2 border border-gray-200 dark:border-gray-700 rounded">
    <div class="flex items-center gap-x-1 gap-y-1">
      <div class="px-1 flex items-center gap-1 min-w-0">
        {{ template "user/fragments/pic" (list .Repo.Did "size-5") }}
        <a href="/{{ $owner }}/{{ $slug }}" class="truncate">{{ $owner }}/{{ $slug }}</a>
      </div>
      <div class="flex gap-1">
        {{ range .Branches }}
          <span class="text-xs px-2 py-1 bg-gray-100 dark:bg-gray-900 rounded shrink-0">{{ . }}</span>
        {{ end }}
      </div>
      {{ with .Repo.RepoStats }}
        <div class="shrink-0 ml-auto order-last flex gap-1 items-center text-gray-500">
          {{ i "star" "size-4" }}
          <span>{{ scaleFmt .StarCount }}</span>
        </div>
      {{ end }}
    </div>

    {{ if .File }}
      {{/* filename match */}}
      <div class="px-3.5 py-2 text-sm border border-gray-200 dark:border-gray-700 rounded-sm flex items-center justify-between">
        <div class="flex items-center gap-2 min-w-0">
          {{ i "file" "size-4 flex-shrink-0" }}
          <a href="/{{ $owner }}/{{ $slug }}/blob/{{ .Commit }}/{{ .FilePath }}" class="truncate font-mono">
            {{- range .File.NameSpans -}}
              {{- if .Match -}}<span class="chunk-match-hl">{{ .Text }}</span>
              {{- else -}}{{ .Text }}{{- end -}}
            {{- end -}}
          </a>
        </div>
        <div class="flex items-center gap-3.5">
          {{ with .Language }}
            {{ template "repo/fragments/colorBall" (dict "color" (langColor .)) }}
            <span>{{ . }}</span>
          {{ end }}
        </div>
      </div>
    {{ else if .Chunks }}
      {{/* chunk match */}}
      <div class="bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-700 rounded-sm divide-y divide-gray-200 dark:divide-gray-700">
        <div class="px-3.5 py-2 text-sm flex items-center justify-between">
          <div class="flex items-center gap-2 min-w-0">
            {{ i "file" "size-4 flex-shrink-0" }}
            <a href="/{{ $owner }}/{{ $slug }}/blob/{{ .Commit }}/{{ .FilePath }}" class="truncate font-mono">{{ .FilePath }}</a>
          </div>
          <div class="flex items-center gap-3.5">
            {{ with .Language }}
              {{ template "repo/fragments/colorBall" (dict "color" (langColor .)) }}
              <span>{{ . }}</span>
            {{ end }}
          </div>
        </div>
        {{ range $i, $chunk := .Chunks }}
          {{ if lt $i 3 }}
            {{ if gt $i 0 }}
              <div class="bg-gray-50 dark:bg-gray-700 text-center">
                <span class="text-sm text-gray-500 dark:text-gray-400 select-none">···</span>
              </div>
            {{ end }}
            {{ template "search/fragments/chunkBody" (dict "Owner" $owner "Slug" $slug "Commit" $.Commit "FilePath" $.FilePath "Chunk" $chunk) }}
          {{ end }}
        {{ end }}
        {{ if gt (len .Chunks) 3 }}
          <details class="group flex flex-col divide-y divide-gray-200 dark:divide-gray-700">
            <summary class="bg-gray-50 dark:bg-gray-700 text-gray-500 dark:text-gray-400 px-1 py-0.5 cursor-pointer list-none group-open:order-last group-open:border-t border-gray-200 dark:border-gray-700">
              <div class="flex group-open:hidden">
                {{ i "chevron-down" "size-3 m-1" }}
                <span class="text-sm select-none">Show {{ (slice .Chunks 3).MatchCount }} more matches</span>
              </div>
              <div class="hidden group-open:flex">
                {{ i "chevron-up" "size-3 m-1" }}
                <span class="text-sm select-none">Show less</span>
              </div>
            </summary>
            {{ range $i, $chunk := .Chunks }}
              {{ if ge $i 3 }}
                <div class="bg-gray-50 dark:bg-gray-700 text-center">
                  <span class="text-sm text-gray-500 dark:text-gray-400 select-none">···</span>
                </div>
                {{ template "search/fragments/chunkBody" (dict "Owner" $owner "Slug" $slug "Commit" $.Commit "FilePath" $.FilePath "Chunk" $chunk) }}
              {{ end }}
            {{ end }}
          </details>
        {{ end }}
      </div>
    {{ else }}
      {{/* repo match */}}
      <div class="flex flex-col gap-1 flex-1 min-h-16">
        {{ with .Repo.Description }}
          <div class="px-1 text-gray-600 dark:text-gray-300 text-sm line-clamp-2">
            {{ . | description }}
          </div>
        {{ end }}
        {{ with .Repo.RepoStats }}
          {{ with .Language }}
            <div class="px-1 flex gap-2 items-center text-sm text-gray-400 mt-auto">
              {{ template "repo/fragments/colorBall" (dict "color" (langColor .)) }}
              <span>{{ . }}</span>
            </div>
          {{ end }}
        {{ end }}
      </div>
    {{ end }}
  </div>
{{ end }}
