<div>
    {{-- Grand totals --}}
    @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">{{ $confirmed }}</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;">{{ $unreviewed }}</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">{{ $dismissed }}</div>
                        <div class="small text-muted">Dismissed</div>
                    </div>
                </div>
            </div>
        </div>
    @endif

    {{-- DSAR Attachments Section --}}
    <div class="mb-5">
        <div class="d-flex align-items-center justify-content-between mb-3">
            <h6 class="mb-0 fw-semibold">DSAR Response Attachments</h6>
            @can('synapse.use')
                <span class="badge text-white" style="background:#7c3aed;font-size:.65rem;">
                    <i class="mdi mdi-brain"></i> Synapse
                </span>
            @endcan
        </div>

        @if($dsarRows->isEmpty())
            <div class="text-center py-4 text-muted">
                <i class="mdi mdi-file-search-outline fs-1 d-block mb-2 opacity-25"></i>
                <p class="mb-0 small">No DSAR response attachments found.</p>
                <small>Evidence is extracted 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>DSAR / Institution</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($dsarRows as $row)
                            <tr wire:key="att-{{ $row['attachment']->id }}">
                                <td>
                                    <div class="d-flex align-items-center gap-2">
                                        <i class="mdi mdi-{{ str_contains($row['attachment']->mime ?? '', 'pdf') ? 'file-pdf-box text-danger' : 'file-outline text-secondary' }} fs-5"></i>
                                        <div>
                                            <div class="fw-medium small">{{ $row['attachment']->filename }}</div>
                                            <div class="text-muted" style="font-size:.65rem;">{{ $row['attachment']->mime }}</div>
                                        </div>
                                    </div>
                                </td>
                                <td>
                                    <div class="small">{{ $row['dsar']->institution?->name ?? '—' }}</div>
                                    <div class="text-muted" style="font-size:.65rem;">{{ $row['dsar']->reference ?? $row['dsar']->slug }}</div>
                                </td>
                                <td>
                                    @if($row['attachment']->evidence_status === 'completed')
                                        <span class="badge bg-success-subtle text-success">Extracted</span>
                                    @elseif($row['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($row['attachment']->evidence_status === 'failed')
                                        <span class="badge bg-danger-subtle text-danger">Failed</span>
                                    @elseif($row['attachment']->evidence_status === 'skipped')
                                        <span class="badge bg-secondary-subtle text-secondary">Skipped</span>
                                    @elseif($row['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($row['items']->count() > 0)
                                        <span class="badge bg-secondary-subtle text-secondary">{{ $row['items']->count() }}</span>
                                    @else
                                        <span class="text-muted">—</span>
                                    @endif
                                </td>
                                <td class="text-center">
                                    @if($row['confirmed'] > 0)
                                        <span class="badge bg-success-subtle text-success">{{ $row['confirmed'] }}</span>
                                    @else
                                        <span class="text-muted">—</span>
                                    @endif
                                </td>
                                <td class="text-center">
                                    @if($row['unreviewed'] > 0)
                                        <span class="badge" style="background:#fffbeb;color:#d97706;">{{ $row['unreviewed'] }}</span>
                                    @else
                                        <span class="text-muted">—</span>
                                    @endif
                                </td>
                                <td class="text-end">
                                    <div class="d-flex gap-1 justify-content-end">
                                        @if($row['items']->isNotEmpty())
                                            <a href="{{ route('dsars.evidence', [$row['dsar'], $row['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($row['attachment']->evidence_status, ['processing', 'completed']))
                                                <button wire:click="runAttachmentExtraction({{ $row['attachment']->id }}, {{ $row['dsar']->id }})"
                                                        wire:loading.attr="disabled"
                                                        wire:target="runAttachmentExtraction({{ $row['attachment']->id }}, {{ $row['dsar']->id }})"
                                                        class="btn btn-sm text-white"
                                                        style="background:#7c3aed;">
                                                    <span wire:loading.remove wire:target="runAttachmentExtraction({{ $row['attachment']->id }}, {{ $row['dsar']->id }})">
                                                        <i class="mdi mdi-brain me-1"></i>Extract
                                                    </span>
                                                    <span wire:loading wire:target="runAttachmentExtraction({{ $row['attachment']->id }}, {{ $row['dsar']->id }})">
                                                        <span class="spinner-border spinner-border-sm"></span>
                                                    </span>
                                                </button>
                                            @elseif($row['attachment']->evidence_status === 'completed')
                                                <button wire:click="runAttachmentExtraction({{ $row['attachment']->id }}, {{ $row['dsar']->id }})"
                                                        wire:confirm="Re-run extraction? This will add new items alongside existing ones."
                                                        class="btn btn-sm btn-outline-secondary">
                                                    <i class="mdi mdi-refresh me-1"></i>Re-run
                                                </button>
                                            @endif
                                        @endcan
                                    </div>
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        @endif
    </div>

    {{-- Document Manager Section --}}
    <div>
        <div class="d-flex align-items-center justify-content-between mb-3">
            <h6 class="mb-0 fw-semibold">Documents</h6>
            @can('synapse.use')
                <span class="badge text-white" style="background:#7c3aed;font-size:.65rem;">
                    <i class="mdi mdi-brain"></i> Synapse
                </span>
            @endcan
        </div>

        @if($documentRows->isEmpty())
            <div class="text-center py-4 text-muted">
                <i class="mdi mdi-file-document-outline fs-1 d-block mb-2 opacity-25"></i>
                <p class="mb-0 small">No documents uploaded yet.</p>
                <small>Upload documents in the Documents tab to extract evidence.</small>
            </div>
        @else
            <div class="table-responsive">
                <table class="table table-hover align-middle">
                    <thead class="table-light">
                        <tr>
                            <th>Name</th>
                            <th>Type</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($documentRows as $row)
                            <tr wire:key="doc-{{ $row['document']->id }}">
                                <td>
                                    <div class="d-flex align-items-center gap-2">
                                        <i class="mdi mdi-{{ strtolower($row['document']->file_type ?? '') === 'pdf' ? 'file-pdf-box text-danger' : 'file-outline text-secondary' }} fs-5"></i>
                                        <div>
                                            <div class="fw-medium small">{{ $row['document']->display_name }}</div>
                                            <div class="text-muted" style="font-size:.65rem;">{{ strtoupper($row['document']->file_type ?? '') }}</div>
                                        </div>
                                    </div>
                                </td>
                                <td>
                                    <span class="small text-muted">{{ $row['document']->type?->display_name ?? '—' }}</span>
                                </td>
                                <td>
                                    @if($row['document']->evidence_status === 'completed')
                                        <span class="badge bg-success-subtle text-success">Extracted</span>
                                    @elseif($row['document']->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($row['document']->evidence_status === 'failed')
                                        <span class="badge bg-danger-subtle text-danger">Failed</span>
                                    @elseif($row['document']->evidence_status === 'skipped')
                                        <span class="badge bg-secondary-subtle text-secondary">Skipped</span>
                                    @else
                                        <span class="badge bg-light text-muted">Not run</span>
                                    @endif
                                </td>
                                <td class="text-center">
                                    @if($row['items']->count() > 0)
                                        <span class="badge bg-secondary-subtle text-secondary">{{ $row['items']->count() }}</span>
                                    @else
                                        <span class="text-muted">—</span>
                                    @endif
                                </td>
                                <td class="text-center">
                                    @if($row['confirmed'] > 0)
                                        <span class="badge bg-success-subtle text-success">{{ $row['confirmed'] }}</span>
                                    @else
                                        <span class="text-muted">—</span>
                                    @endif
                                </td>
                                <td class="text-center">
                                    @if($row['unreviewed'] > 0)
                                        <span class="badge" style="background:#fffbeb;color:#d97706;">{{ $row['unreviewed'] }}</span>
                                    @else
                                        <span class="text-muted">—</span>
                                    @endif
                                </td>
                                <td class="text-end">
                                    <div class="d-flex gap-1 justify-content-end">
                                        @if($row['items']->isNotEmpty())
                                            <a href="{{ route('documents.evidence', $row['document']) }}"
                                               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($row['document']->evidence_status, ['processing', 'completed']))
                                                <button wire:click="runDocumentExtraction({{ $row['document']->id }})"
                                                        wire:loading.attr="disabled"
                                                        wire:target="runDocumentExtraction({{ $row['document']->id }})"
                                                        class="btn btn-sm text-white"
                                                        style="background:#7c3aed;">
                                                    <span wire:loading.remove wire:target="runDocumentExtraction({{ $row['document']->id }})">
                                                        <i class="mdi mdi-brain me-1"></i>Extract
                                                    </span>
                                                    <span wire:loading wire:target="runDocumentExtraction({{ $row['document']->id }})">
                                                        <span class="spinner-border spinner-border-sm"></span>
                                                    </span>
                                                </button>
                                            @elseif($row['document']->evidence_status === 'completed')
                                                <button wire:click="runDocumentExtraction({{ $row['document']->id }})"
                                                        wire:confirm="Re-run extraction? This will add new items alongside existing ones."
                                                        class="btn btn-sm btn-outline-secondary">
                                                    <i class="mdi mdi-refresh me-1"></i>Re-run
                                                </button>
                                            @endif
                                        @endcan
                                    </div>
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        @endif
    </div>
</div>
