<div>
        {{-- Header stats --}}
        @if($totalItems > 0)
            <div class="row g-2 mb-4">
                <div class="col-sm-3">
                    <div class="card text-center border-0 bg-light">
                        <div class="card-body py-2">
                            <div class="fs-4 fw-bold">{{ $totalItems }}</div>
                            <div class="small text-muted">Total items</div>
                        </div>
                    </div>
                </div>
                <div class="col-sm-3">
                    <div class="card text-center border-0" style="background:#f0fdf4;">
                        <div class="card-body py-2">
                            <div class="fs-4 fw-bold text-success">{{ $confirmedCount }}</div>
                            <div class="small text-muted">Confirmed</div>
                        </div>
                    </div>
                </div>
                <div class="col-sm-3">
                    <div class="card text-center border-0" style="background:#fffbeb;">
                        <div class="card-body py-2">
                            <div class="fs-4 fw-bold" style="color:#d97706;">{{ $unreviewedCount }}</div>
                            <div class="small text-muted">Unreviewed</div>
                        </div>
                    </div>
                </div>
                <div class="col-sm-3">
                    <div class="card text-center border-0 bg-light">
                        <div class="card-body py-2">
                            <div class="fs-4 fw-bold text-secondary">{{ $dismissedCount }}</div>
                            <div class="small text-muted">Dismissed</div>
                        </div>
                    </div>
                </div>
            </div>
        @endif

        {{-- Toolbar --}}
        @can('synapse.use')
        <div class="d-flex align-items-center justify-content-between mb-3">
            <div class="d-flex align-items-center gap-2">
                <span class="badge text-white" style="background:#7c3aed;font-size:.65rem;">
                    <i class="mdi mdi-brain"></i> Synapse
                </span>
                <span class="text-muted small">Evidence extraction from DSAR response documents</span>
            </div>
            @if($attachments->isNotEmpty())
                <button wire:click="runAllExtraction"
                        wire:confirm="Run evidence extraction on all unprocessed attachments?"
                        class="btn btn-sm text-white"
                        style="background:#7c3aed;">
                    <span wire:loading.remove wire:target="runAllExtraction">
                        <i class="mdi mdi-brain me-1"></i>Extract all
                    </span>
                    <span wire:loading wire:target="runAllExtraction">
                        <span class="spinner-border spinner-border-sm me-1"></span>Running…
                    </span>
                </button>
            @endif
        </div>
        @endcan

        {{-- Attachments table --}}
        @if($attachments->isEmpty())
            <div class="text-center py-5 text-muted">
                <i class="mdi mdi-file-search-outline fs-1 d-block mb-2 opacity-25"></i>
                <p class="mb-0">No DSAR response attachments found for this request.</p>
                <small>Evidence is extracted automatically when an institution sends records to the <strong>dsar_evidence</strong> triage queue.</small>
            </div>
        @else
            <div class="table-responsive">
                <table class="table table-hover align-middle">
                    <thead class="table-light">
                        <tr>
                            <th>File</th>
                            <th>Status</th>
                            <th class="text-center">Items</th>
                            <th class="text-center">Confirmed</th>
                            <th class="text-center">Unreviewed</th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($attachments as $attachment)
                            @php
                                $items       = $attachment->evidenceItems;
                                $confirmed   = $items->filter->isConfirmed()->count();
                                $unreviewed  = $items->filter->isUnreviewed()->count();
                                $dismissed   = $items->filter->isDismissed()->count();
                            @endphp
                            <tr wire:key="att-{{ $attachment->id }}">
                                <td>
                                    <div class="d-flex align-items-center gap-2">
                                        <i class="mdi mdi-{{ str_contains($attachment->mime, 'pdf') ? 'file-pdf-box text-danger' : 'file-outline text-secondary' }} fs-5"></i>
                                        <div>
                                            <div class="fw-medium small">{{ $attachment->filename }}</div>
                                            <div class="text-muted" style="font-size:.65rem;">{{ $attachment->mime }}</div>
                                        </div>
                                    </div>
                                </td>
                                <td>
                                    @if($attachment->evidence_status === 'completed')
                                        <span class="badge bg-success-subtle text-success">Extracted</span>
                                    @elseif($attachment->evidence_status === 'processing')
                                        <span class="badge bg-primary-subtle text-primary">
                                            <span class="spinner-border spinner-border-sm me-1" style="width:.6rem;height:.6rem;"></span>Processing
                                        </span>
                                    @elseif($attachment->evidence_status === 'failed')
                                        <span class="badge bg-danger-subtle text-danger">Failed</span>
                                    @elseif($attachment->evidence_status === 'skipped')
                                        <span class="badge bg-secondary-subtle text-secondary"
                                              title="{{ $attachment->evidence_is_dsar_doc === false ? 'Not a DSAR data document' : 'No readable evidence pages' }}">
                                            Skipped
                                        </span>
                                    @elseif($attachment->evidence_status === 'pending')
                                        <span class="badge bg-warning-subtle text-warning">Pending</span>
                                    @else
                                        <span class="badge bg-light text-muted">Not run</span>
                                    @endif
                                </td>
                                <td class="text-center">
                                    @if($items->count() > 0)
                                        <span class="badge bg-secondary-subtle text-secondary">{{ $items->count() }}</span>
                                    @else
                                        <span class="text-muted">—</span>
                                    @endif
                                </td>
                                <td class="text-center">
                                    @if($confirmed > 0)
                                        <span class="badge bg-success-subtle text-success">{{ $confirmed }}</span>
                                    @else
                                        <span class="text-muted">—</span>
                                    @endif
                                </td>
                                <td class="text-center">
                                    @if($unreviewed > 0)
                                        <span class="badge" style="background:#fffbeb;color:#d97706;">{{ $unreviewed }}</span>
                                    @else
                                        <span class="text-muted">—</span>
                                    @endif
                                </td>
                                <td class="text-end">
                                    <div class="d-flex gap-1 justify-content-end">
                                        @if($items->isNotEmpty())
                                            <a href="{{ route('dsars.evidence', [$dsar, $attachment->id]) }}"
                                               class="btn btn-sm btn-outline-primary">
                                                <i class="mdi mdi-eye-outline me-1"></i>View
                                            </a>
                                        @endif
                                        @can('synapse.use')
                                            @if(!in_array($attachment->evidence_status, ['processing', 'completed']))
                                                <button wire:click="runExtraction({{ $attachment->id }})"
                                                        wire:loading.attr="disabled"
                                                        wire:target="runExtraction({{ $attachment->id }})"
                                                        class="btn btn-sm text-white"
                                                        style="background:#7c3aed;">
                                                    <span wire:loading.remove wire:target="runExtraction({{ $attachment->id }})">
                                                        <i class="mdi mdi-brain me-1"></i>Extract
                                                    </span>
                                                    <span wire:loading wire:target="runExtraction({{ $attachment->id }})">
                                                        <span class="spinner-border spinner-border-sm"></span>
                                                    </span>
                                                </button>
                                            @elseif($attachment->evidence_status === 'completed')
                                                <button wire:click="runExtraction({{ $attachment->id }})"
                                                        wire:confirm="Re-run extraction? This will add new items alongside existing ones."
                                                        class="btn btn-sm btn-outline-secondary btn-sm">
                                                    <i class="mdi mdi-refresh me-1"></i>Re-run
                                                </button>
                                            @endif
                                        @endcan
                                    </div>
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        @endif
</div>
