<x-filament::page>
    @php
        $reference = is_array($reference ?? null) ? $reference : [];
        $tenantSummaries = is_array($tenantSummaries ?? null) ? $tenantSummaries : [];
        $denseRows = is_array($denseRows ?? null) ? $denseRows : [];
        $compactResults = is_array($compactResults ?? null) ? $compactResults : [];
        $policyTypeOptions = is_array($policyTypeOptions ?? null) ? $policyTypeOptions : [];
        $tenantSortOptions = is_array($tenantSortOptions ?? null) ? $tenantSortOptions : [];
        $subjectSortOptions = is_array($subjectSortOptions ?? null) ? $subjectSortOptions : [];
        $stateLegend = is_array($stateLegend ?? null) ? $stateLegend : [];
        $freshnessLegend = is_array($freshnessLegend ?? null) ? $freshnessLegend : [];
        $trustLegend = is_array($trustLegend ?? null) ? $trustLegend : [];
        $emptyState = is_array($emptyState ?? null) ? $emptyState : null;
        $currentFilters = is_array($currentFilters ?? null) ? $currentFilters : [];
        $draftFilters = is_array($draftFilters ?? null) ? $draftFilters : [];
        $presentationState = is_array($presentationState ?? null) ? $presentationState : [];
        $supportSurfaceState = is_array($supportSurfaceState ?? null) ? $supportSurfaceState : [];
        $referenceReady = ($reference['referenceState'] ?? null) === 'ready';
        $activeFilterCount = $this->activeFilterCount();
        $activeFilterSummary = $this->activeFilterSummary();
        $stagedFilterSummary = $this->stagedFilterSummary();
        $hasStagedFilterChanges = (bool) ($presentationState['hasStagedFilterChanges'] ?? false);
        $requestedMode = (string) ($presentationState['requestedMode'] ?? 'auto');
        $resolvedMode = (string) ($presentationState['resolvedMode'] ?? 'compact');
        $visibleTenantCount = (int) ($presentationState['visibleTenantCount'] ?? 0);
        $autoRefreshActive = (bool) ($presentationState['autoRefreshActive'] ?? false);
        $lastUpdatedAt = $presentationState['lastUpdatedAt'] ?? null;
        $compactModeAvailable = (bool) ($presentationState['compactModeAvailable'] ?? false);
        $hiddenAssignedTenantCount = max(0, (int) ($reference['assignedTenantCount'] ?? 0) - $visibleTenantCount);

        $stateBadge = static fn (mixed $value) => \App\Support\Badges\BadgeCatalog::spec(\App\Support\Badges\BadgeDomain::BaselineCompareMatrixState, $value);
        $freshnessBadge = static fn (mixed $value) => \App\Support\Badges\BadgeCatalog::spec(\App\Support\Badges\BadgeDomain::BaselineCompareMatrixFreshness, $value);
        $trustBadge = static fn (mixed $value) => \App\Support\Badges\BadgeCatalog::spec(\App\Support\Badges\BadgeDomain::BaselineCompareMatrixTrust, $value);
        $severityBadge = static fn (mixed $value) => \App\Support\Badges\BadgeCatalog::spec(\App\Support\Badges\BadgeDomain::FindingSeverity, $value);
        $profileStatusBadge = static fn (mixed $value) => \App\Support\Badges\BadgeCatalog::spec(\App\Support\Badges\BadgeDomain::BaselineProfileStatus, $value);
        $profileStatusSpec = $profileStatusBadge($reference['baselineStatus'] ?? null);
        $modeBadgeColor = match ($resolvedMode) {
            'dense' => 'info',
            'compact' => 'success',
            default => 'gray',
        };
        $modeLabel = $this->presentationModeLabel($resolvedMode);
    @endphp

    @if ($autoRefreshActive)
        <div aria-hidden="true" wire:poll.5s="pollMatrix"></div>
    @endif

    <x-filament::section heading="Reference overview">
        <x-slot name="description">
            Compare assigned environments remains simulation only. This operator view changes presentation density, not compare truth, visible-set scope, or the existing drilldown path.
        </x-slot>

        <div class="space-y-4">
            <div class="flex flex-col gap-4 xl:flex-row xl:items-start xl:justify-between">
                <div class="space-y-3">
                    <div class="flex flex-wrap items-center gap-2">
                        <x-filament::badge :color="$profileStatusSpec->color" :icon="$profileStatusSpec->icon" size="sm">
                            {{ $profileStatusSpec->label }}
                        </x-filament::badge>

                        <x-filament::badge :color="$referenceReady ? 'success' : 'warning'" :icon="$referenceReady ? 'heroicon-m-check-badge' : 'heroicon-m-exclamation-triangle'" size="sm">
                            {{ $referenceReady ? 'Reference snapshot ready' : 'Reference snapshot blocked' }}
                        </x-filament::badge>

                        <x-filament::badge :color="$modeBadgeColor" size="sm">
                            {{ $modeLabel }}
                        </x-filament::badge>

                        @if (filled($reference['referenceSnapshotId'] ?? null))
                            <x-filament::badge color="gray" size="sm">
                                Snapshot #{{ (int) $reference['referenceSnapshotId'] }}
                            </x-filament::badge>
                        @endif

                        @if ($hiddenAssignedTenantCount > 0)
                            <x-filament::badge color="info" icon="heroicon-m-eye-slash" size="sm">
                                {{ $hiddenAssignedTenantCount }} hidden by access scope
                            </x-filament::badge>
                        @endif
                    </div>

                    <div class="space-y-1">
                        <h2 class="text-xl font-semibold text-gray-950 dark:text-white" data-testid="baseline-compare-matrix-profile">
                            {{ $reference['baselineProfileName'] ?? ($profile->name ?? 'Baseline compare matrix') }}
                        </h2>

                        <p class="text-sm text-gray-600 dark:text-gray-300">
                            Assigned environments: {{ (int) ($reference['assignedTenantCount'] ?? 0) }}.
                            Visible environments: {{ $visibleTenantCount }}.
                            @if (filled($reference['referenceSnapshotCapturedAt'] ?? null))
                                Reference captured {{ \Illuminate\Support\Carbon::parse($reference['referenceSnapshotCapturedAt'])->diffForHumans() }}.
                            @endif
                        </p>

                        <p class="text-sm text-gray-600 dark:text-gray-300">
                            Auto mode resolves from the visible environment set. Manual mode stays local to this route and never becomes stored preference truth.
                        </p>

                        @if (filled($reference['referenceReasonCode'] ?? null))
                            <p class="text-sm text-warning-700 dark:text-warning-300">
                                Reference reason: {{ \Illuminate\Support\Str::headline(str_replace('.', ' ', (string) $reference['referenceReasonCode'])) }}
                            </p>
                        @endif
                    </div>
                </div>

                <dl class="grid gap-3 sm:grid-cols-2 xl:w-[28rem]">
                    <div class="rounded-2xl border border-gray-200 bg-white px-4 py-3 shadow-sm dark:border-gray-800 dark:bg-gray-900/70">
                        <dt class="text-[11px] font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">Visible environments</dt>
                        <dd class="mt-1 text-3xl font-semibold text-gray-950 dark:text-white">
                            {{ $visibleTenantCount }}
                        </dd>
                    </div>

                    <div class="rounded-2xl border border-gray-200 bg-white px-4 py-3 shadow-sm dark:border-gray-800 dark:bg-gray-900/70">
                        <dt class="text-[11px] font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">Rendered subjects</dt>
                        <dd class="mt-1 text-3xl font-semibold text-gray-950 dark:text-white">
                            {{ $resolvedMode === 'compact' ? count($compactResults) : count($denseRows) }}
                        </dd>
                    </div>

                    <div class="rounded-2xl border border-gray-200 bg-white px-4 py-3 shadow-sm dark:border-gray-800 dark:bg-gray-900/70">
                        <dt class="text-[11px] font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">Active filters</dt>
                        <dd class="mt-1 text-sm font-medium text-gray-900 dark:text-white">
                            @if ($activeFilterCount === 0)
                                All visible results
                            @else
                                {{ $activeFilterCount }} active {{ \Illuminate\Support\Str::plural('filter', $activeFilterCount) }}
                            @endif
                        </dd>
                    </div>

                    <div class="rounded-2xl border border-gray-200 bg-white px-4 py-3 shadow-sm dark:border-gray-800 dark:bg-gray-900/70">
                        <dt class="text-[11px] font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">Resolved mode</dt>
                        <dd class="mt-1 text-sm font-medium text-gray-900 dark:text-white">
                            {{ $modeLabel }}
                        </dd>
                    </div>
                </dl>
            </div>

            <div class="grid gap-4 xl:grid-cols-[minmax(0,1fr)_minmax(19rem,23rem)]">
                <div class="rounded-2xl border border-gray-200 bg-gray-50/80 p-4 shadow-sm dark:border-gray-800 dark:bg-gray-900/50" data-testid="baseline-compare-matrix-mode-switcher">
                    <div class="flex flex-col gap-3 lg:flex-row lg:items-center lg:justify-between">
                        <div class="space-y-1">
                            <div class="text-sm font-semibold text-gray-950 dark:text-white">Presentation mode</div>
                            <p class="text-sm text-gray-600 dark:text-gray-300">
                                Requested: {{ $this->presentationModeLabel($requestedMode) }}. Resolved: {{ $modeLabel }}.
                            </p>
                        </div>

                        <div class="flex flex-wrap gap-2">
                            <x-filament::button tag="a" :href="$this->modeUrl('auto')" :color="$requestedMode === 'auto' ? 'primary' : 'gray'" size="sm">
                                Auto
                            </x-filament::button>

                            <x-filament::button tag="a" :href="$this->modeUrl('dense')" :color="$requestedMode === 'dense' ? 'primary' : 'gray'" size="sm">
                                Dense
                            </x-filament::button>

                            @if ($compactModeAvailable)
                                <x-filament::button tag="a" :href="$this->modeUrl('compact')" :color="$requestedMode === 'compact' ? 'primary' : 'gray'" size="sm">
                                    Compact
                                </x-filament::button>
                            @else
                                <x-filament::badge color="gray" size="sm">
                                    Compact unlocks at one visible environment
                                </x-filament::badge>
                            @endif
                        </div>
                    </div>
                </div>

                <div class="rounded-2xl border border-gray-200 bg-white/90 p-4 shadow-sm dark:border-gray-800 dark:bg-gray-900/70">
                    <div class="flex flex-col gap-3">
                        <div class="flex flex-wrap items-center gap-2" data-testid="baseline-compare-matrix-last-updated">
                            @if (($supportSurfaceState['showLastUpdated'] ?? true) && filled($lastUpdatedAt))
                                <x-filament::badge color="gray" icon="heroicon-m-clock" size="sm">
                                    Last updated {{ \Illuminate\Support\Carbon::parse($lastUpdatedAt)->diffForHumans() }}
                                </x-filament::badge>
                            @endif

                            @if (($supportSurfaceState['showAutoRefreshHint'] ?? false) && $autoRefreshActive)
                                <x-filament::badge color="info" icon="heroicon-m-arrow-path" size="sm">
                                    Passive auto-refresh every 5 seconds
                                </x-filament::badge>
                            @endif

                            <div wire:loading.flex wire:target="refreshMatrix,applyFilters,resetFilters" class="items-center">
                                <x-filament::badge color="warning" icon="heroicon-m-arrow-path" size="sm">
                                    Refreshing now
                                </x-filament::badge>
                            </div>
                        </div>

                        <div class="flex flex-wrap items-center gap-3">
                            <x-filament::button type="button" wire:click="refreshMatrix" wire:loading.attr="disabled" wire:target="refreshMatrix,applyFilters,resetFilters" color="gray" size="sm">
                                Refresh matrix
                            </x-filament::button>

                            @if ($hiddenAssignedTenantCount > 0)
                                <span class="text-xs text-gray-500 dark:text-gray-400">
                                    Visible-set only. Hidden environments never contribute to summaries or drilldowns.
                                </span>
                            @endif
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </x-filament::section>

    <x-filament::section heading="Filters">
        <x-slot name="description">
            Heavy filters stage locally first. The matrix keeps rendering the applied scope until you explicitly apply or reset the draft.
        </x-slot>

        <div class="space-y-4" data-testid="baseline-compare-matrix-filters">
            <div class="rounded-2xl border border-gray-200 bg-gray-50/80 px-4 py-3 shadow-sm dark:border-gray-800 dark:bg-gray-900/50" data-testid="matrix-active-filters">
                <div class="flex flex-col gap-3 xl:flex-row xl:items-start xl:justify-between">
                    <div class="space-y-2 min-w-0">
                        <div class="text-sm font-semibold text-gray-950 dark:text-white">Applied matrix scope</div>
                        <p class="text-sm leading-6 text-gray-600 dark:text-gray-300">
                            @if ($activeFilterCount === 0)
                                No narrowing filters are active. Showing every visible subject and environment in the current baseline scope.
                            @else
                                {{ $activeFilterCount }} active {{ \Illuminate\Support\Str::plural('filter', $activeFilterCount) }} are already shaping the rendered matrix.
                            @endif
                        </p>

                        @if ($activeFilterSummary !== [])
                            <div class="flex flex-wrap gap-2">
                                @foreach ($activeFilterSummary as $label => $value)
                                    <x-filament::badge color="info" size="sm">
                                        {{ $label }}: {{ $value }}
                                    </x-filament::badge>
                                @endforeach
                            </div>
                        @endif
                    </div>

                    <div class="flex flex-wrap items-center gap-2 xl:justify-end">
                        <x-filament::badge :color="$activeFilterCount === 0 ? 'gray' : 'info'" icon="heroicon-m-funnel" size="sm">
                            @if ($activeFilterCount === 0)
                                All visible results
                            @else
                                {{ $activeFilterCount }} active {{ \Illuminate\Support\Str::plural('filter', $activeFilterCount) }}
                            @endif
                        </x-filament::badge>

                        <x-filament::badge color="gray" size="sm">
                            Environment sort: {{ $tenantSortOptions[$currentFilters['tenant_sort'] ?? 'tenant_name'] ?? 'Environment name' }}
                        </x-filament::badge>

                        <x-filament::badge color="gray" size="sm">
                            Subject sort: {{ $subjectSortOptions[$currentFilters['subject_sort'] ?? 'deviation_breadth'] ?? 'Deviation breadth' }}
                        </x-filament::badge>
                    </div>
                </div>

                @if ($hasStagedFilterChanges)
                    <div class="mt-3 rounded-xl border border-primary-200 bg-primary-50/70 px-3 py-3 dark:border-primary-900/60 dark:bg-primary-950/20" data-testid="baseline-compare-matrix-staged-filters">
                        <div class="flex flex-col gap-2 lg:flex-row lg:items-start lg:justify-between">
                            <div class="space-y-1">
                                <div class="text-sm font-semibold text-primary-900 dark:text-primary-100">Draft filters are staged</div>
                                <p class="text-sm text-primary-800/90 dark:text-primary-200/90">
                                    The controls below differ from the current route state. Apply them when you are ready to redraw the matrix. Refreshing the page discards these unapplied draft edits.
                                </p>
                            </div>

                            @if ($stagedFilterSummary !== [])
                                <div class="flex flex-wrap gap-2">
                                    @foreach ($stagedFilterSummary as $label => $value)
                                        <x-filament::badge color="primary" size="sm">
                                            {{ $label }}: {{ is_string($value) ? \Illuminate\Support\Str::headline(str_replace('_', ' ', $value)) : $value }}
                                        </x-filament::badge>
                                    @endforeach
                                </div>
                            @endif
                        </div>
                    </div>
                @endif
            </div>

            <form wire:submit.prevent="applyFilters" class="space-y-4">
                {{ $this->form }}

                <div class="flex flex-col gap-3 lg:flex-row lg:items-start lg:justify-between">
                    <div class="flex-1 rounded-xl border border-dashed border-gray-300 bg-gray-50 px-4 py-3 dark:border-gray-700 dark:bg-gray-900/40">
                        <div class="flex flex-wrap items-center gap-2 text-sm">
                            <span class="font-semibold text-gray-950 dark:text-white">Focused subject</span>

                            @if (filled($currentFilters['subject_key'] ?? null))
                                <x-filament::badge color="info" icon="heroicon-m-funnel" size="sm">
                                    {{ $currentFilters['subject_key'] }}
                                </x-filament::badge>

                                <x-filament::button tag="a" :href="$this->clearSubjectFocusUrl()" color="gray" size="sm">
                                    Clear subject focus
                                </x-filament::button>
                            @else
                                <span class="text-gray-500 dark:text-gray-400">
                                    None set yet. Use Focus subject from a row when you want a subject-first drilldown.
                                </span>
                            @endif
                        </div>

                        <p class="mt-2 text-xs text-gray-500 dark:text-gray-400">
                            Applied filters and the focused subject are carried by the URL so the current matrix scan can be reopened or shared.
                        </p>
                    </div>

                    <div class="flex flex-wrap items-center gap-3 lg:shrink-0">
                        <x-filament::button type="submit" color="primary" size="sm" wire:loading.attr="disabled" wire:target="applyFilters,resetFilters,refreshMatrix">
                            Apply filters
                        </x-filament::button>

                        <x-filament::button type="button" wire:click="resetFilters" color="gray" size="sm" wire:loading.attr="disabled" wire:target="applyFilters,resetFilters,refreshMatrix">
                            Reset filters
                        </x-filament::button>
                    </div>
                </div>
            </form>

            <x-filament-actions::modals />
        </div>
    </x-filament::section>

    <x-filament::section heading="Support context">
        <x-slot name="description">
            Status, legends, and refresh cues stay compact so the matrix body remains the primary working surface.
        </x-slot>

        <div class="grid gap-4 xl:grid-cols-[minmax(0,1fr)_minmax(22rem,26rem)]">
            <div class="grid gap-3 sm:grid-cols-2">
                <div class="rounded-2xl border border-gray-200 bg-white p-4 shadow-sm dark:border-gray-800 dark:bg-gray-900/70">
                    <div class="text-sm font-semibold text-gray-950 dark:text-white">Current scope</div>
                    <p class="mt-1 text-sm text-gray-600 dark:text-gray-300">
                        {{ $visibleTenantCount }} visible {{ \Illuminate\Support\Str::plural('environment', $visibleTenantCount) }}.
                        {{ $resolvedMode === 'dense' ? 'State-first dense scan stays active.' : 'Compact single-environment review stays active.' }}
                    </p>

                    @if ($policyTypeOptions !== [])
                        <div class="mt-3 flex flex-wrap gap-2">
                            <x-filament::badge color="gray" size="sm">
                                {{ count($policyTypeOptions) }} searchable governed subjects
                            </x-filament::badge>
                            @if ($hiddenAssignedTenantCount > 0)
                                <x-filament::badge color="gray" size="sm">
                                    Visible-set only
                                </x-filament::badge>
                            @endif
                        </div>
                    @endif
                </div>

                <div class="rounded-2xl border border-gray-200 bg-white p-4 shadow-sm dark:border-gray-800 dark:bg-gray-900/70">
                    <div class="text-sm font-semibold text-gray-950 dark:text-white">Refresh honesty</div>
                    <p class="mt-1 text-sm text-gray-600 dark:text-gray-300">
                        Manual refresh shows a blocking state only while you explicitly redraw. Background polling remains a passive hint.
                    </p>

                    @if ($autoRefreshActive)
                        <div class="mt-3">
                            <x-filament::badge color="info" icon="heroicon-m-arrow-path" size="sm">
                                Compare work is still queued or running
                            </x-filament::badge>
                        </div>
                    @endif
                </div>
            </div>

            <details class="rounded-2xl border border-gray-200 bg-gray-50/80 p-4 shadow-sm open:bg-white dark:border-gray-800 dark:bg-gray-900/50 dark:open:bg-gray-900/70">
                <summary class="cursor-pointer list-none">
                    <div class="flex flex-col gap-3 lg:flex-row lg:items-center lg:justify-between">
                        <div class="space-y-1">
                            <div class="text-sm font-semibold text-gray-950 dark:text-white">Grouped legend</div>
                            <p class="text-sm text-gray-600 dark:text-gray-300">
                                State, freshness, and trust stay available on demand without pushing the matrix down the page.
                            </p>
                        </div>

                        <div class="flex flex-wrap gap-2">
                            <x-filament::badge color="gray" size="sm">{{ count($stateLegend) }} states</x-filament::badge>
                            <x-filament::badge color="gray" size="sm">{{ count($freshnessLegend) }} freshness cues</x-filament::badge>
                            <x-filament::badge color="gray" size="sm">{{ count($trustLegend) }} trust cues</x-filament::badge>
                        </div>
                    </div>
                </summary>

                <div class="mt-4 grid gap-3">
                    <div class="rounded-xl border border-gray-200 bg-white px-3 py-3 dark:border-gray-800 dark:bg-gray-950/40">
                        <div class="mb-2 text-[11px] font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">State legend</div>
                        <div class="flex flex-wrap gap-2">
                            @foreach ($stateLegend as $item)
                                <x-filament::badge :color="$item['color']" :icon="$item['icon']" size="sm">
                                    {{ $item['label'] }}
                                </x-filament::badge>
                            @endforeach
                        </div>
                    </div>

                    <div class="rounded-xl border border-gray-200 bg-white px-3 py-3 dark:border-gray-800 dark:bg-gray-950/40">
                        <div class="mb-2 text-[11px] font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">Freshness legend</div>
                        <div class="flex flex-wrap gap-2">
                            @foreach ($freshnessLegend as $item)
                                <x-filament::badge :color="$item['color']" :icon="$item['icon']" size="sm">
                                    {{ $item['label'] }}
                                </x-filament::badge>
                            @endforeach
                        </div>
                    </div>

                    <div class="rounded-xl border border-gray-200 bg-white px-3 py-3 dark:border-gray-800 dark:bg-gray-950/40">
                        <div class="mb-2 text-[11px] font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">Trust legend</div>
                        <div class="flex flex-wrap gap-2">
                            @foreach ($trustLegend as $item)
                                <x-filament::badge :color="$item['color']" :icon="$item['icon']" size="sm">
                                    {{ $item['label'] }}
                                </x-filament::badge>
                            @endforeach
                        </div>
                    </div>
                </div>
            </details>
        </div>
    </x-filament::section>

    <div class="relative" data-testid="baseline-compare-matrix-results">
        @if ($emptyState !== null)
            <x-filament::section heading="Results">
                <div class="rounded-2xl border border-dashed border-gray-300 bg-gray-50 px-6 py-8 dark:border-gray-700 dark:bg-gray-900/40">
                    <div class="space-y-3" data-testid="baseline-compare-matrix-empty-state">
                        <h3 class="text-lg font-semibold text-gray-950 dark:text-white">{{ $emptyState['title'] ?? 'Nothing to show' }}</h3>
                        <p class="text-sm text-gray-600 dark:text-gray-300">{{ $emptyState['body'] ?? 'Adjust the current inputs and try again.' }}</p>

                        @if ($activeFilterCount > 0)
                            <div class="pt-1">
                                <x-filament::button type="button" wire:click="resetFilters" color="primary" size="sm" wire:loading.attr="disabled" wire:target="applyFilters,resetFilters,refreshMatrix">
                                    Reset filters
                                </x-filament::button>
                            </div>
                        @endif
                    </div>
                </div>
            </x-filament::section>
        @elseif ($resolvedMode === 'compact')
            @php
                $compactTenant = $tenantSummaries[0] ?? null;
                $compactTenantFreshnessSpec = $freshnessBadge($compactTenant['freshnessState'] ?? null);
                $compactTenantTrustSpec = $trustBadge($compactTenant['trustLevel'] ?? null);
            @endphp

            <x-filament::section heading="Compact compare results">
                <x-slot name="description">
                    One visible environment remains in scope, so the matrix collapses into a shorter subject-result list instead of a pseudo-grid.
                </x-slot>

                @if ($compactTenant)
                    <div class="mb-4 rounded-2xl border border-gray-200 bg-white p-4 shadow-sm dark:border-gray-800 dark:bg-gray-900/70" data-testid="baseline-compare-matrix-compact-shell">
                        <div class="flex flex-col gap-3 lg:flex-row lg:items-center lg:justify-between">
                            <div class="space-y-1">
                                <div class="text-sm font-semibold text-gray-950 dark:text-white">{{ $compactTenant['tenantName'] }}</div>
                                <p class="text-sm text-gray-600 dark:text-gray-300">
                                    Compact mode stays visible-set only. Subject drilldowns and run links still preserve the matrix context.
                                </p>
                            </div>

                            <div class="flex flex-wrap gap-2">
                                <x-filament::badge :color="$compactTenantFreshnessSpec->color" :icon="$compactTenantFreshnessSpec->icon" size="sm">
                                    {{ $compactTenantFreshnessSpec->label }}
                                </x-filament::badge>
                                <x-filament::badge :color="$compactTenantTrustSpec->color" :icon="$compactTenantTrustSpec->icon" size="sm">
                                    {{ $compactTenantTrustSpec->label }}
                                </x-filament::badge>
                            </div>
                        </div>
                    </div>
                @endif

                <div class="space-y-3">
                    @foreach ($compactResults as $result)
                        @php
                            $stateSpec = $stateBadge($result['state'] ?? null);
                            $freshnessSpec = $freshnessBadge($result['freshnessState'] ?? null);
                            $trustSpec = $trustBadge($result['trustLevel'] ?? null);
                            $severitySpec = filled($result['severity'] ?? null) ? $severityBadge($result['severity']) : null;
                            $tenantId = (int) ($result['tenantId'] ?? 0);
                            $subjectKey = $result['subjectKey'] ?? null;
                            $primaryUrl = filled($result['findingId'] ?? null)
                                ? $this->findingUrl($tenantId, (int) $result['findingId'], $subjectKey)
                                : $this->tenantCompareUrl($tenantId, $subjectKey);
                            $runUrl = filled($result['compareRunId'] ?? null)
                                ? $this->runUrl((int) $result['compareRunId'], $tenantId, $subjectKey)
                                : null;
                            $attentionClasses = match ((string) ($result['attentionLevel'] ?? 'review')) {
                                'needs_attention' => 'bg-danger-100 text-danger-700 dark:bg-danger-950/40 dark:text-danger-300',
                                'refresh_recommended' => 'bg-warning-100 text-warning-700 dark:bg-warning-950/40 dark:text-warning-300',
                                'aligned' => 'bg-success-100 text-success-700 dark:bg-success-950/40 dark:text-success-300',
                                default => 'bg-gray-100 text-gray-700 dark:bg-gray-800 dark:text-gray-300',
                            };
                            $attentionLabel = \Illuminate\Support\Str::headline(str_replace('_', ' ', (string) ($result['attentionLevel'] ?? 'review')));
                        @endphp

                        <div class="rounded-2xl border border-gray-200 bg-white p-4 shadow-sm dark:border-gray-800 dark:bg-gray-900/70">
                            <div class="flex flex-col gap-4 xl:flex-row xl:items-start xl:justify-between">
                                <div class="space-y-3">
                                    <div class="space-y-1">
                                        <div class="text-base font-semibold text-gray-950 dark:text-white">
                                            {{ $result['displayName'] ?? $result['subjectKey'] ?? 'Subject' }}
                                        </div>
                                        <div class="text-sm text-gray-500 dark:text-gray-400">
                                            {{ $result['governedSubjectLabel'] ?? ($result['policyType'] ?? 'Unknown governed subject') }}
                                        </div>
                                        @if (filled($result['baselineExternalId'] ?? null))
                                            <div class="text-xs text-gray-500 dark:text-gray-400">
                                                Reference ID: {{ $result['baselineExternalId'] }}
                                            </div>
                                        @endif
                                    </div>

                                    <div class="flex flex-wrap gap-2">
                                        <x-filament::badge :color="$stateSpec->color" :icon="$stateSpec->icon" size="sm">
                                            {{ $stateSpec->label }}
                                        </x-filament::badge>
                                        <x-filament::badge :color="$freshnessSpec->color" :icon="$freshnessSpec->icon" size="sm">
                                            {{ $freshnessSpec->label }}
                                        </x-filament::badge>
                                        <x-filament::badge :color="$trustSpec->color" :icon="$trustSpec->icon" size="sm">
                                            {{ $trustSpec->label }}
                                        </x-filament::badge>
                                        @if ($severitySpec)
                                            <x-filament::badge :color="$severitySpec->color" :icon="$severitySpec->icon" size="sm">
                                                {{ $severitySpec->label }}
                                            </x-filament::badge>
                                        @endif
                                        <span class="inline-flex items-center rounded-full px-2 py-1 text-[11px] font-semibold uppercase tracking-wide {{ $attentionClasses }}">
                                            {{ $attentionLabel }}
                                        </span>
                                    </div>

                                    @if (filled($result['reasonSummary'] ?? null) || filled($result['lastComparedAt'] ?? null))
                                        <div class="space-y-1 text-sm text-gray-600 dark:text-gray-300">
                                            @if (filled($result['reasonSummary'] ?? null))
                                                <div>{{ $result['reasonSummary'] }}</div>
                                            @endif
                                            @if (filled($result['lastComparedAt'] ?? null))
                                                <div>Compared {{ \Illuminate\Support\Carbon::parse($result['lastComparedAt'])->diffForHumans() }}</div>
                                            @endif
                                        </div>
                                    @endif
                                </div>

                                <div class="flex flex-col gap-3 xl:items-end">
                                    <div class="flex flex-wrap gap-2">
                                        <x-filament::badge color="gray" size="sm">
                                            Drift breadth {{ (int) ($result['deviationBreadth'] ?? 0) }}
                                        </x-filament::badge>
                                        <x-filament::badge color="gray" size="sm">
                                            Missing {{ (int) ($result['missingBreadth'] ?? 0) }}
                                        </x-filament::badge>
                                        <x-filament::badge color="gray" size="sm">
                                            Ambiguous {{ (int) ($result['ambiguousBreadth'] ?? 0) }}
                                        </x-filament::badge>
                                    </div>

                                    <div class="flex flex-wrap gap-3 text-sm">
                                        @if ($primaryUrl)
                                            <x-filament::link :href="$primaryUrl" size="sm">
                                                {{ filled($result['findingId'] ?? null) ? 'Open finding' : 'Open environment compare' }}
                                            </x-filament::link>
                                        @endif

                                        @if ($runUrl)
                                            <x-filament::link :href="$runUrl" color="gray" size="sm">
                                                Open run
                                            </x-filament::link>
                                        @endif

                                        @if (filled($result['subjectKey'] ?? null))
                                            <x-filament::link :href="$this->filterUrl(['subject_key' => $result['subjectKey']])" color="gray" size="sm">
                                                Focus subject
                                            </x-filament::link>
                                        @endif
                                    </div>
                                </div>
                            </div>
                        </div>
                    @endforeach
                </div>
            </x-filament::section>
        @else
            <x-filament::section heading="Dense multi-environment scan">
                <x-slot name="description">
                    The matrix body is state-first. Row click stays forbidden, the subject column stays pinned, and repeated follow-up actions move behind compact secondary reveals.
                </x-slot>

                <div class="mb-4 grid gap-3 md:grid-cols-2 2xl:grid-cols-3">
                    @foreach ($tenantSummaries as $tenantSummary)
                        @php
                            $freshnessSpec = $freshnessBadge($tenantSummary['freshnessState'] ?? null);
                            $trustSpec = $trustBadge($tenantSummary['trustLevel'] ?? null);
                            $tenantSeveritySpec = filled($tenantSummary['maxSeverity'] ?? null) ? $severityBadge($tenantSummary['maxSeverity']) : null;
                            $tenantCompareUrl = $this->tenantCompareUrl((int) $tenantSummary['tenantId']);
                            $tenantRunUrl = filled($tenantSummary['compareRunId'] ?? null)
                                ? $this->runUrl((int) $tenantSummary['compareRunId'], (int) $tenantSummary['tenantId'])
                                : null;
                        @endphp

                        <div class="rounded-2xl border border-gray-200 bg-white p-4 shadow-sm dark:border-gray-800 dark:bg-gray-900/70">
                            <div class="flex flex-col gap-3">
                                <div class="space-y-1">
                                    <div class="text-sm font-semibold text-gray-950 dark:text-white">{{ $tenantSummary['tenantName'] }}</div>
                                    <div class="flex flex-wrap gap-2">
                                        <x-filament::badge :color="$freshnessSpec->color" :icon="$freshnessSpec->icon" size="sm">
                                            {{ $freshnessSpec->label }}
                                        </x-filament::badge>
                                        <x-filament::badge :color="$trustSpec->color" :icon="$trustSpec->icon" size="sm">
                                            {{ $trustSpec->label }}
                                        </x-filament::badge>
                                        @if ($tenantSeveritySpec)
                                            <x-filament::badge :color="$tenantSeveritySpec->color" :icon="$tenantSeveritySpec->icon" size="sm">
                                                {{ $tenantSeveritySpec->label }}
                                            </x-filament::badge>
                                        @endif
                                    </div>
                                </div>

                                <div class="grid grid-cols-2 gap-3 text-sm">
                                    <div>
                                        <div class="text-[11px] font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">Aligned</div>
                                        <div class="mt-1 font-semibold text-gray-950 dark:text-white">{{ (int) ($tenantSummary['matchedCount'] ?? 0) }}</div>
                                    </div>
                                    <div>
                                        <div class="text-[11px] font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">Drift</div>
                                        <div class="mt-1 font-semibold text-gray-950 dark:text-white">{{ (int) ($tenantSummary['differingCount'] ?? 0) }}</div>
                                    </div>
                                    <div>
                                        <div class="text-[11px] font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">Missing</div>
                                        <div class="mt-1 font-semibold text-gray-950 dark:text-white">{{ (int) ($tenantSummary['missingCount'] ?? 0) }}</div>
                                    </div>
                                    <div>
                                        <div class="text-[11px] font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">Ambiguous</div>
                                        <div class="mt-1 font-semibold text-gray-950 dark:text-white">{{ (int) ($tenantSummary['ambiguousCount'] ?? 0) }}</div>
                                    </div>
                                </div>

                                <div class="flex flex-wrap gap-3 text-sm">
                                    @if ($tenantCompareUrl)
                                        <x-filament::link :href="$tenantCompareUrl" size="sm">
                                            Open environment compare
                                        </x-filament::link>
                                    @endif

                                    @if ($tenantRunUrl)
                                        <x-filament::link :href="$tenantRunUrl" color="gray" size="sm">
                                            Open latest run
                                        </x-filament::link>
                                    @endif
                                </div>
                            </div>
                        </div>
                    @endforeach
                </div>

                <div class="overflow-x-auto rounded-2xl" data-testid="baseline-compare-matrix-grid">
                    <div class="min-w-[82rem] overflow-hidden rounded-2xl border border-gray-200 dark:border-gray-800" data-testid="baseline-compare-matrix-dense-shell">
                        <table class="min-w-full border-separate border-spacing-0">
                            <thead class="bg-gray-50 dark:bg-gray-950/70">
                                <tr>
                                    <th class="sticky left-0 z-20 w-[22rem] border-r border-gray-200 bg-gray-50 px-4 py-3 text-left text-xs font-semibold uppercase tracking-wide text-gray-500 dark:border-gray-800 dark:bg-gray-950/70 dark:text-gray-400">
                                        Baseline subject
                                    </th>

                                    @foreach ($tenantSummaries as $tenantSummary)
                                        @php
                                            $freshnessSpec = $freshnessBadge($tenantSummary['freshnessState'] ?? null);
                                        @endphp

                                        <th class="min-w-[16rem] border-b border-gray-200 px-4 py-3 text-left align-top text-xs font-semibold uppercase tracking-wide text-gray-500 dark:border-gray-800 dark:text-gray-400">
                                            <div class="space-y-2">
                                                <div class="text-sm font-semibold normal-case text-gray-950 dark:text-white">{{ $tenantSummary['tenantName'] }}</div>
                                                <div class="flex flex-wrap gap-2">
                                                    <x-filament::badge :color="$freshnessSpec->color" :icon="$freshnessSpec->icon" size="sm">
                                                        {{ $freshnessSpec->label }}
                                                    </x-filament::badge>
                                                </div>
                                            </div>
                                        </th>
                                    @endforeach
                                </tr>
                            </thead>

                            <tbody class="divide-y divide-gray-200 dark:divide-gray-800">
                                @foreach ($denseRows as $row)
                                    @php
                                        $subject = is_array($row['subject'] ?? null) ? $row['subject'] : [];
                                        $cells = is_array($row['cells'] ?? null) ? $row['cells'] : [];
                                        $subjectTrustSpec = $trustBadge($subject['trustLevel'] ?? null);
                                        $subjectSeveritySpec = filled($subject['maxSeverity'] ?? null) ? $severityBadge($subject['maxSeverity']) : null;
                                        $rowKey = (string) ($subject['subjectKey'] ?? 'subject-'.$loop->index);
                                        $rowSurfaceClasses = $loop->even
                                            ? 'bg-gray-50/70 dark:bg-gray-950/20'
                                            : 'bg-white dark:bg-gray-900/60';
                                        $subjectAttentionClasses = match ((string) ($subject['attentionLevel'] ?? 'review')) {
                                            'needs_attention' => 'bg-danger-100 text-danger-700 dark:bg-danger-950/40 dark:text-danger-300',
                                            'refresh_recommended' => 'bg-warning-100 text-warning-700 dark:bg-warning-950/40 dark:text-warning-300',
                                            'aligned' => 'bg-success-100 text-success-700 dark:bg-success-950/40 dark:text-success-300',
                                            default => 'bg-gray-100 text-gray-700 dark:bg-gray-800 dark:text-gray-300',
                                        };
                                    @endphp

                                    <tr wire:key="baseline-compare-matrix-row-{{ $rowKey }}" class="group transition-colors hover:bg-primary-50/30 dark:hover:bg-primary-950/10 {{ $rowSurfaceClasses }}" data-testid="baseline-compare-matrix-row">
                                        <td class="sticky left-0 z-10 border-r border-gray-200 px-4 py-4 align-top dark:border-gray-800 {{ $rowSurfaceClasses }}">
                                            <div class="space-y-3">
                                                <div class="space-y-1">
                                                    <div class="text-sm font-semibold text-gray-950 dark:text-white">
                                                        {{ $subject['displayName'] ?? $subject['subjectKey'] ?? 'Subject' }}
                                                    </div>
                                                    <div class="text-xs text-gray-500 dark:text-gray-400">
                                                        {{ $subject['governedSubjectLabel'] ?? ($subject['policyType'] ?? 'Unknown governed subject') }}
                                                    </div>
                                                    @if (filled($subject['baselineExternalId'] ?? null))
                                                        <div class="text-xs text-gray-500 dark:text-gray-400">
                                                            Reference ID: {{ $subject['baselineExternalId'] }}
                                                        </div>
                                                    @endif
                                                </div>

                                                <div class="flex flex-wrap gap-2">
                                                    <x-filament::badge color="gray" size="sm">
                                                        Drift breadth {{ (int) ($subject['deviationBreadth'] ?? 0) }}
                                                    </x-filament::badge>
                                                    <x-filament::badge color="gray" size="sm">
                                                        Missing {{ (int) ($subject['missingBreadth'] ?? 0) }}
                                                    </x-filament::badge>
                                                    <x-filament::badge :color="$subjectTrustSpec->color" :icon="$subjectTrustSpec->icon" size="sm">
                                                        {{ $subjectTrustSpec->label }}
                                                    </x-filament::badge>
                                                    @if ($subjectSeveritySpec)
                                                        <x-filament::badge :color="$subjectSeveritySpec->color" :icon="$subjectSeveritySpec->icon" size="sm">
                                                            {{ $subjectSeveritySpec->label }}
                                                        </x-filament::badge>
                                                    @endif
                                                    <span class="inline-flex items-center rounded-full px-2 py-1 text-[11px] font-semibold uppercase tracking-wide {{ $subjectAttentionClasses }}">
                                                        {{ \Illuminate\Support\Str::headline(str_replace('_', ' ', (string) ($subject['attentionLevel'] ?? 'review'))) }}
                                                    </span>
                                                </div>

                                                @if (filled($subject['subjectKey'] ?? null))
                                                    <div class="flex flex-wrap gap-3 text-sm">
                                                        <x-filament::link :href="$this->filterUrl(['subject_key' => $subject['subjectKey']])" color="gray" size="sm" data-testid="matrix-focus-subject">
                                                            Focus subject
                                                        </x-filament::link>
                                                    </div>
                                                @endif
                                            </div>
                                        </td>

                                        @foreach ($cells as $cell)
                                            @php
                                                $cellStateSpec = $stateBadge($cell['state'] ?? null);
                                                $cellFreshnessSpec = $freshnessBadge($cell['freshnessState'] ?? null);
                                                $cellTrustSpec = $trustBadge($cell['trustLevel'] ?? null);
                                                $cellSeveritySpec = filled($cell['severity'] ?? null) ? $severityBadge($cell['severity']) : null;
                                                $tenantId = (int) ($cell['tenantId'] ?? 0);
                                                $subjectKey = $subject['subjectKey'] ?? ($cell['subjectKey'] ?? null);
                                                $primaryUrl = filled($cell['findingId'] ?? null)
                                                    ? $this->findingUrl($tenantId, (int) $cell['findingId'], $subjectKey)
                                                    : $this->tenantCompareUrl($tenantId, $subjectKey);
                                                $runUrl = filled($cell['compareRunId'] ?? null)
                                                    ? $this->runUrl((int) $cell['compareRunId'], $tenantId, $subjectKey)
                                                    : null;
                                                $attentionClasses = match ((string) ($cell['attentionLevel'] ?? 'review')) {
                                                    'needs_attention' => 'bg-danger-100 text-danger-700 dark:bg-danger-950/40 dark:text-danger-300',
                                                    'refresh_recommended' => 'bg-warning-100 text-warning-700 dark:bg-warning-950/40 dark:text-warning-300',
                                                    'aligned' => 'bg-success-100 text-success-700 dark:bg-success-950/40 dark:text-success-300',
                                                    default => 'bg-gray-100 text-gray-700 dark:bg-gray-800 dark:text-gray-300',
                                                };
                                                $cellSurfaceClasses = match ((string) ($cell['attentionLevel'] ?? 'review')) {
                                                    'needs_attention' => 'border-danger-300 bg-danger-50/80 ring-1 ring-danger-200/70 dark:border-danger-900/70 dark:bg-danger-950/15 dark:ring-danger-900/40',
                                                    'refresh_recommended' => 'border-warning-300 bg-warning-50/80 ring-1 ring-warning-200/70 dark:border-warning-900/70 dark:bg-warning-950/15 dark:ring-warning-900/40',
                                                    'aligned' => 'border-success-200 bg-success-50/70 dark:border-success-900/60 dark:bg-success-950/10',
                                                    default => 'border-gray-200 bg-gray-50 dark:border-gray-800 dark:bg-gray-950/40',
                                                };
                                            @endphp

                                            <td wire:key="baseline-compare-matrix-cell-{{ $rowKey }}-{{ $tenantId > 0 ? $tenantId : $loop->index }}" class="px-4 py-4 align-top">
                                                <div class="flex h-full flex-col gap-3 rounded-xl border p-3 text-xs transition-colors group-hover:border-primary-200 dark:group-hover:border-primary-900 {{ $cellSurfaceClasses }}">
                                                    <div class="flex flex-wrap items-start justify-between gap-2">
                                                        <div class="flex flex-wrap gap-2">
                                                            <x-filament::badge :color="$cellStateSpec->color" :icon="$cellStateSpec->icon" size="sm">
                                                                {{ $cellStateSpec->label }}
                                                            </x-filament::badge>
                                                            @if ($cellSeveritySpec)
                                                                <x-filament::badge :color="$cellSeveritySpec->color" :icon="$cellSeveritySpec->icon" size="sm">
                                                                    {{ $cellSeveritySpec->label }}
                                                                </x-filament::badge>
                                                            @endif
                                                        </div>

                                                        <span class="inline-flex items-center rounded-full px-2 py-1 text-[11px] font-semibold uppercase tracking-wide {{ $attentionClasses }}">
                                                            {{ \Illuminate\Support\Str::headline(str_replace('_', ' ', (string) ($cell['attentionLevel'] ?? 'review'))) }}
                                                        </span>
                                                    </div>

                                                    <div class="flex flex-wrap gap-2">
                                                        <x-filament::badge :color="$cellFreshnessSpec->color" :icon="$cellFreshnessSpec->icon" size="sm">
                                                            {{ $cellFreshnessSpec->label }}
                                                        </x-filament::badge>
                                                        <x-filament::badge :color="$cellTrustSpec->color" :icon="$cellTrustSpec->icon" size="sm">
                                                            {{ $cellTrustSpec->label }}
                                                        </x-filament::badge>
                                                    </div>

                                                    @if (filled($cell['reasonSummary'] ?? null) || filled($cell['lastComparedAt'] ?? null))
                                                        <div class="space-y-1 text-xs leading-5 text-gray-600 dark:text-gray-300">
                                                            @if (filled($cell['reasonSummary'] ?? null))
                                                                <div>{{ $cell['reasonSummary'] }}</div>
                                                            @endif

                                                            @if (filled($cell['lastComparedAt'] ?? null))
                                                                <div>Compared {{ \Illuminate\Support\Carbon::parse($cell['lastComparedAt'])->diffForHumans() }}</div>
                                                            @endif
                                                        </div>
                                                    @endif

                                                    <div class="mt-auto space-y-2">
                                                        @if ($primaryUrl)
                                                            <div class="text-sm">
                                                                <x-filament::link :href="$primaryUrl" size="sm">
                                                                    {{ filled($cell['findingId'] ?? null) ? 'Open finding' : 'Open environment compare' }}
                                                                </x-filament::link>
                                                            </div>
                                                        @endif

                                                        @if ($runUrl || filled($subjectKey))
                                                            <details class="rounded-lg border border-gray-200 bg-white/70 px-2 py-1.5 dark:border-gray-800 dark:bg-gray-950/50">
                                                                <summary class="cursor-pointer list-none text-[11px] font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">
                                                                    More follow-up
                                                                </summary>

                                                                <div class="mt-2 flex flex-wrap gap-3 text-sm">
                                                                    @if ($runUrl)
                                                                        <x-filament::link :href="$runUrl" color="gray" size="sm">
                                                                            Open run
                                                                        </x-filament::link>
                                                                    @endif

                                                                    @if (filled($subjectKey))
                                                                        <x-filament::link :href="$this->filterUrl(['subject_key' => $subjectKey])" color="gray" size="sm">
                                                                            Focus subject
                                                                        </x-filament::link>
                                                                    @endif
                                                                </div>
                                                            </details>
                                                        @endif
                                                    </div>
                                                </div>
                                            </td>
                                        @endforeach
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </x-filament::section>
        @endif
    </div>
</x-filament::page>
