<div>
    {{-- Encabezado con volver --}}
    <div class="mb-6 flex items-center justify-between">
        <div class="flex items-center gap-4">
            <a href="{{ route('deudores.index') }}"
               class="inline-flex items-center px-3 py-2 bg-white border border-gray-300 rounded-md text-sm font-medium text-gray-700 hover:bg-gray-50">
                <svg class="w-4 h-4 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7"/>
                </svg>
                Volver al listado
            </a>
            <div>
                <h1 class="text-2xl font-bold text-gray-900">{{ $deudor->nombre_completo }}</h1>
                <p class="text-sm text-gray-500">{{ $deudor->tipo_documento }} {{ $deudor->numero_documento }}</p>
            </div>
        </div>
        <div class="flex gap-2">
            @if($procesos->count() > 0)
                <span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-medium bg-purple-100 text-purple-800">
                    ⚖️ {{ $procesos->count() }} {{ Str::plural('proceso', $procesos->count()) }}
                </span>
            @endif
            @if($casosPrejuridicos->count() > 0)
                <span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-medium bg-amber-100 text-amber-800">
                    📁 {{ $casosPrejuridicos->count() }} {{ Str::plural('caso', $casosPrejuridicos->count()) }} prejurídico{{ $casosPrejuridicos->count() != 1 ? 's' : '' }}
                </span>
            @endif
        </div>
    </div>

    {{-- Bloque: Datos de contacto --}}
    <div class="bg-white shadow rounded-lg overflow-hidden mb-6">
        <div class="px-6 py-4 border-b border-gray-200 bg-gray-50">
            <h2 class="text-lg font-semibold text-gray-900 flex items-center gap-2">
                📋 Datos del Deudor
            </h2>
        </div>
        <div class="p-6 grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
            <div>
                <p class="text-xs uppercase tracking-wide text-gray-500 mb-1">Nombre completo</p>
                <p class="text-sm font-medium text-gray-900">{{ $deudor->nombre_completo }}</p>
            </div>
            <div>
                <p class="text-xs uppercase tracking-wide text-gray-500 mb-1">Documento</p>
                <p class="text-sm font-medium text-gray-900">{{ $deudor->tipo_documento }} {{ $deudor->numero_documento }}</p>
            </div>
            <div>
                <p class="text-xs uppercase tracking-wide text-gray-500 mb-1">Email</p>
                <p class="text-sm text-gray-900">
                    @if($deudor->email)
                        <a href="mailto:{{ $deudor->email }}" class="text-indigo-600 hover:text-indigo-900">{{ $deudor->email }}</a>
                    @else
                        <span class="text-gray-400">—</span>
                    @endif
                </p>
            </div>
            <div>
                <p class="text-xs uppercase tracking-wide text-gray-500 mb-1">Teléfono</p>
                <p class="text-sm text-gray-900">{{ $deudor->telefono ?? '—' }}</p>
            </div>
            <div>
                <p class="text-xs uppercase tracking-wide text-gray-500 mb-1">Ciudad</p>
                <p class="text-sm text-gray-900">{{ $deudor->ciudad ?? $deudor->ciudad_residencia ?? '—' }}</p>
            </div>
            <div>
                <p class="text-xs uppercase tracking-wide text-gray-500 mb-1">Departamento</p>
                <p class="text-sm text-gray-900">{{ $deudor->departamento ?? '—' }}</p>
            </div>
            <div class="md:col-span-2 lg:col-span-3">
                <p class="text-xs uppercase tracking-wide text-gray-500 mb-1">Dirección de notificación</p>
                <p class="text-sm text-gray-900">{{ $deudor->direccion_notificacion ?? '—' }}</p>
            </div>
            @if($deudor->banco)
                <div>
                    <p class="text-xs uppercase tracking-wide text-gray-500 mb-1">Banco</p>
                    <p class="text-sm text-gray-900">{{ $deudor->banco }}</p>
                </div>
            @endif
            @if($deudor->numero_juicio)
                <div>
                    <p class="text-xs uppercase tracking-wide text-gray-500 mb-1">N° Juicio</p>
                    <p class="text-sm text-gray-900">{{ $deudor->numero_juicio }}</p>
                </div>
            @endif
            @if($deudor->consecutivo_yber)
                <div>
                    <p class="text-xs uppercase tracking-wide text-gray-500 mb-1">Consecutivo Yber</p>
                    <p class="text-sm text-gray-900">{{ $deudor->consecutivo_yber }}</p>
                </div>
            @endif
        </div>
    </div>

    {{-- Bloque: Bienes --}}
    <div class="bg-white shadow rounded-lg overflow-hidden mb-6">
        <div class="px-6 py-4 border-b border-gray-200 bg-gray-50">
            <h2 class="text-lg font-semibold text-gray-900 flex items-center gap-2">
                💼 Bienes
            </h2>
        </div>
        <div class="p-6">
            <div class="flex items-center gap-3 mb-3">
                <span class="text-sm text-gray-500">¿Tiene bienes?</span>
                @if($deudor->tiene_bienes)
                    <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-green-100 text-green-800">
                        ✓ Sí
                    </span>
                @else
                    <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-gray-100 text-gray-800">
                        — No registrado
                    </span>
                @endif
            </div>
            @if($deudor->notas_bienes)
                <div>
                    <p class="text-xs uppercase tracking-wide text-gray-500 mb-1">Notas</p>
                    <p class="text-sm text-gray-700 whitespace-pre-line">{{ $deudor->notas_bienes }}</p>
                </div>
            @endif
        </div>
    </div>

    {{-- Bloque: Procesos jurídicos --}}
    <div class="bg-white shadow rounded-lg overflow-hidden mb-6">
        <div class="px-6 py-4 border-b border-gray-200 bg-gray-50 flex items-center justify-between">
            <h2 class="text-lg font-semibold text-gray-900 flex items-center gap-2">
                ⚖️ Procesos Jurídicos
                <span class="ml-2 inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium bg-purple-100 text-purple-800">
                    {{ $procesos->count() }}
                </span>
            </h2>
        </div>
        <div class="overflow-x-auto">
            @if($procesos->count() > 0)
                <table class="min-w-full divide-y divide-gray-200">
                    <thead class="bg-gray-50">
                        <tr>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Radicado</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Cliente</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Cuantía</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Etapa</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Estado</th>
                            <th class="px-6 py-3 text-center text-xs font-medium text-gray-500 uppercase">Acciones</th>
                        </tr>
                    </thead>
                    <tbody class="bg-white divide-y divide-gray-200">
                        @foreach($procesos as $p)
                        <tr class="hover:bg-gray-50">
                            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">
                                {{ $p->radicado ?? '—' }}
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-700">
                                {{ optional($p->clienteCorporativo)->razon_social ?? '—' }}
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-700">
                                @if(isset($p->cuantia))
                                    ${{ number_format($p->cuantia, 0, ',', '.') }}
                                @else
                                    —
                                @endif
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-700">
                                {{ $p->etapa_procesal ?? '—' }}
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-700">
                                <span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-blue-100 text-blue-800">
                                    {{ $p->estado ?? '—' }}
                                </span>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-center">
                                <a href="{{ route('procesos.show', $p->id) }}"
                                   class="text-indigo-600 hover:text-indigo-900 text-sm font-medium">
                                    Ver →
                                </a>
                            </td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            @else
                <div class="p-12 text-center text-gray-500 text-sm">
                    Este deudor no tiene procesos jurídicos asociados.
                </div>
            @endif
        </div>
    </div>

    {{-- Bloque: Casos prejurídicos --}}
    <div class="bg-white shadow rounded-lg overflow-hidden">
        <div class="px-6 py-4 border-b border-gray-200 bg-gray-50 flex items-center justify-between">
            <h2 class="text-lg font-semibold text-gray-900 flex items-center gap-2">
                📁 Casos Prejurídicos
                <span class="ml-2 inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium bg-amber-100 text-amber-800">
                    {{ $casosPrejuridicos->count() }}
                </span>
            </h2>
        </div>
        <div class="overflow-x-auto">
            @if($casosPrejuridicos->count() > 0)
                <table class="min-w-full divide-y divide-gray-200">
                    <thead class="bg-gray-50">
                        <tr>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Radicado</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Cliente</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Total Deuda</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Etapa</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Estado</th>
                            <th class="px-6 py-3 text-center text-xs font-medium text-gray-500 uppercase">Mora</th>
                        </tr>
                    </thead>
                    <tbody class="bg-white divide-y divide-gray-200">
                        @foreach($casosPrejuridicos as $c)
                        <tr class="hover:bg-gray-50">
                            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">
                                {{ $c->radicado ?? '—' }}
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-700">
                                {{ optional($c->clienteCorporativo)->razon_social ?? '—' }}
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-700">
                                @if(isset($c->total_deuda))
                                    ${{ number_format($c->total_deuda, 0, ',', '.') }}
                                @else
                                    —
                                @endif
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-700">
                                {{ $c->etapa_label ?? $c->etapa ?? '—' }}
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-700">
                                <span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-amber-100 text-amber-800">
                                    {{ $c->estado_label ?? $c->estado ?? '—' }}
                                </span>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-center text-sm text-gray-700">
                                {{ $c->dias_mora ?? '—' }}
                            </td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            @else
                <div class="p-12 text-center text-gray-500 text-sm">
                    Este deudor no tiene casos prejurídicos asociados.
                </div>
            @endif
        </div>
    </div>
</div>
