<!-- 역추적 결과 -->
<div class="space-y-6">
    <!-- 역할로 권한 부여된 사용자 -->
    <div>
        <h3 class="text-sm font-semibold text-gray-800 mb-3 flex items-center gap-2">
            <span class="inline-flex items-center justify-center w-6 h-6 bg-purple-100 text-purple-800 rounded-full text-xs font-bold">
                {{ count($trace['by_role'] ?? []) }}
            </span>
            역할로 권한 부여된 사용자
        </h3>
        @if(count($trace['by_role'] ?? []) > 0)
            <div class="overflow-x-auto">
                <table class="w-full text-sm border border-gray-200 rounded-lg">
                    <thead class="bg-purple-50">
                        <tr>
                            <th class="px-4 py-2 text-left font-medium text-gray-700">사용자</th>
                            <th class="px-4 py-2 text-left font-medium text-gray-700">역할</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @foreach($trace['by_role'] as $item)
                            <tr class="hover:bg-gray-50">
                                <td class="px-4 py-2">
                                    <div class="font-medium text-gray-900 cursor-pointer hover:text-blue-600"
                                         data-context-menu="user"
                                         data-entity-id="{{ $item['user_id'] }}"
                                         data-entity-name="{{ $item['user_name'] }}"
                                         title="클릭하여 메뉴 열기">{{ $item['user_name'] }}</div>
                                    <div class="text-xs text-gray-500">{{ $item['email'] }}</div>
                                </td>
                                <td class="px-4 py-2">
                                    <span class="inline-flex items-center px-2 py-0.5 text-xs font-medium bg-purple-100 text-purple-800 rounded">
                                        {{ $item['role']['display_name'] ?? $item['role']['name'] }}
                                    </span>
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        @else
            <div class="text-center py-4 text-gray-500 bg-gray-50 rounded-lg">
                <p class="text-sm">역할로 권한 부여된 사용자가 없습니다.</p>
            </div>
        @endif
    </div>

    <!-- 부서로 권한 부여된 사용자 -->
    <div>
        <h3 class="text-sm font-semibold text-gray-800 mb-3 flex items-center gap-2">
            <span class="inline-flex items-center justify-center w-6 h-6 bg-blue-100 text-blue-800 rounded-full text-xs font-bold">
                {{ count($trace['by_department'] ?? []) }}
            </span>
            부서로 권한 부여된 사용자
        </h3>
        @if(count($trace['by_department'] ?? []) > 0)
            <div class="overflow-x-auto">
                <table class="w-full text-sm border border-gray-200 rounded-lg">
                    <thead class="bg-blue-50">
                        <tr>
                            <th class="px-4 py-2 text-left font-medium text-gray-700">사용자</th>
                            <th class="px-4 py-2 text-left font-medium text-gray-700">부서</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @foreach($trace['by_department'] as $item)
                            <tr class="hover:bg-gray-50">
                                <td class="px-4 py-2">
                                    <div class="font-medium text-gray-900 cursor-pointer hover:text-blue-600"
                                         data-context-menu="user"
                                         data-entity-id="{{ $item['user_id'] }}"
                                         data-entity-name="{{ $item['user_name'] }}"
                                         title="클릭하여 메뉴 열기">{{ $item['user_name'] }}</div>
                                    <div class="text-xs text-gray-500">{{ $item['email'] }}</div>
                                </td>
                                <td class="px-4 py-2">
                                    <span class="inline-flex items-center px-2 py-0.5 text-xs font-medium bg-blue-100 text-blue-800 rounded">
                                        {{ $item['department']['name'] }}
                                        @if($item['department']['code'])
                                            <span class="ml-1 text-blue-600">({{ $item['department']['code'] }})</span>
                                        @endif
                                    </span>
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        @else
            <div class="text-center py-4 text-gray-500 bg-gray-50 rounded-lg">
                <p class="text-sm">부서로 권한 부여된 사용자가 없습니다.</p>
            </div>
        @endif
    </div>

    <!-- 개인 ALLOW로 권한 부여된 사용자 -->
    <div>
        <h3 class="text-sm font-semibold text-gray-800 mb-3 flex items-center gap-2">
            <span class="inline-flex items-center justify-center w-6 h-6 bg-green-100 text-green-800 rounded-full text-xs font-bold">
                {{ count($trace['by_personal'] ?? []) }}
            </span>
            개인 ALLOW로 권한 부여된 사용자
        </h3>
        @if(count($trace['by_personal'] ?? []) > 0)
            <div class="overflow-x-auto">
                <table class="w-full text-sm border border-gray-200 rounded-lg">
                    <thead class="bg-green-50">
                        <tr>
                            <th class="px-4 py-2 text-left font-medium text-gray-700">사용자</th>
                            <th class="px-4 py-2 text-left font-medium text-gray-700">이메일</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @foreach($trace['by_personal'] as $item)
                            <tr class="hover:bg-gray-50">
                                <td class="px-4 py-2">
                                    <div class="font-medium text-gray-900 cursor-pointer hover:text-blue-600"
                                         data-context-menu="user"
                                         data-entity-id="{{ $item['user_id'] }}"
                                         data-entity-name="{{ $item['user_name'] }}"
                                         title="클릭하여 메뉴 열기">{{ $item['user_name'] }}</div>
                                </td>
                                <td class="px-4 py-2">
                                    <div class="text-gray-500">{{ $item['email'] }}</div>
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        @else
            <div class="text-center py-4 text-gray-500 bg-gray-50 rounded-lg">
                <p class="text-sm">개인 ALLOW로 권한 부여된 사용자가 없습니다.</p>
            </div>
        @endif
    </div>

    <!-- 개인 DENY로 거부된 사용자 -->
    <div>
        <h3 class="text-sm font-semibold text-gray-800 mb-3 flex items-center gap-2">
            <span class="inline-flex items-center justify-center w-6 h-6 bg-red-100 text-red-800 rounded-full text-xs font-bold">
                {{ count($trace['denied_users'] ?? []) }}
            </span>
            개인 DENY로 거부된 사용자
        </h3>
        @if(count($trace['denied_users'] ?? []) > 0)
            <div class="overflow-x-auto">
                <table class="w-full text-sm border border-gray-200 rounded-lg">
                    <thead class="bg-red-50">
                        <tr>
                            <th class="px-4 py-2 text-left font-medium text-gray-700">사용자</th>
                            <th class="px-4 py-2 text-left font-medium text-gray-700">이메일</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @foreach($trace['denied_users'] as $item)
                            <tr class="hover:bg-gray-50">
                                <td class="px-4 py-2">
                                    <div class="font-medium text-gray-900 cursor-pointer hover:text-blue-600"
                                         data-context-menu="user"
                                         data-entity-id="{{ $item['user_id'] }}"
                                         data-entity-name="{{ $item['user_name'] }}"
                                         title="클릭하여 메뉴 열기">{{ $item['user_name'] }}</div>
                                </td>
                                <td class="px-4 py-2">
                                    <div class="text-gray-500">{{ $item['email'] }}</div>
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        @else
            <div class="text-center py-4 text-gray-500 bg-gray-50 rounded-lg">
                <p class="text-sm">개인 DENY로 거부된 사용자가 없습니다.</p>
            </div>
        @endif
    </div>
</div>