<div class="section-title">{{ __('Análisis de Desvíos') }}</div>

<div class="grid grid-cols-1 md:grid-cols-3 gap-4 mb-6">
    <div class="stat-card">
        <div class="stat-value" style="color:#ef4444;">{{ $deviations['summary']['phases_delayed'] ?? 0 }}</div>
        <div class="stat-label">{{ __('Fases retrasadas') }}</div>
    </div>
    <div class="stat-card">
        <div class="stat-value" style="color:#10b981;">{{ $deviations['summary']['phases_early'] ?? 0 }}</div>
        <div class="stat-label">{{ __('Fases adelantadas') }}</div>
    </div>
    <div class="stat-card">
        <div class="stat-value" style="color:#6b7280;">{{ $deviations['summary']['phases_on_time'] ?? 0 }}</div>
        <div class="stat-label">{{ __('Fases en plazo') }}</div>
    </div>
    <div class="stat-card">
        <div class="stat-value" style="color:#ef4444;">{{ $deviations['summary']['features_delayed'] ?? 0 }}</div>
        <div class="stat-label">{{ __('Elementos retrasados') }}</div>
    </div>
    <div class="stat-card">
        <div class="stat-value" style="color:#10b981;">{{ $deviations['summary']['features_early'] ?? 0 }}</div>
        <div class="stat-label">{{ __('Elementos adelantados') }}</div>
    </div>
    <div class="stat-card">
        <div class="stat-value" style="color:#6b7280;">{{ $deviations['summary']['features_on_time'] ?? 0 }}</div>
        <div class="stat-label">{{ __('Elementos en plazo') }}</div>
    </div>
</div>

@if(!empty($deviations['phases']))
<div class="phase-block mb-8">
    <div class="section-title" style="margin-top:0;">{{ __('Desvíos por Fase') }}</div>
    <div class="overflow-x-auto">
        <table>
            <thead>
                <tr>
                    <th>Fase</th>
                    <th>Inicio Plan</th>
                    <th>Fin Plan</th>
                    <th>Inicio Real</th>
                    <th>Fin Real</th>
                    <th>Δ Inicio (d)</th>
                    <th>Δ Fin (d)</th>
                    <th>Prog. Plan (%)</th>
                    <th>Prog. Real (%)</th>
                    <th>Δ Prog.</th>
                    <th>SPI</th>
                    <th>En Plazo</th>
                </tr>
            </thead>
            <tbody>
                @foreach($deviations['phases'] as $phase)
                <tr>
                    <td class="font-medium">{{ $phase['name'] }}</td>
                    <td>{{ $phase['planned_start'] ?? '—' }}</td>
                    <td>{{ $phase['planned_end'] ?? '—' }}</td>
                    <td>{{ $phase['actual_start'] ?? '—' }}</td>
                    <td>{{ $phase['actual_end'] ?? '—' }}</td>
                    <td class="{{ ($phase['start_deviation'] ?? 0) > 0 ? 'text-red-600' : (($phase['start_deviation'] ?? 0) < 0 ? 'text-green-600' : '') }}">
                        {{ $phase['start_deviation'] ?? '—' }}
                    </td>
                    <td class="{{ ($phase['end_deviation'] ?? 0) > 0 ? 'text-red-600' : (($phase['end_deviation'] ?? 0) < 0 ? 'text-green-600' : '') }}">
                        {{ $phase['end_deviation'] ?? '—' }}
                    </td>
                    <td>{{ $phase['planned_progress'] }}%</td>
                    <td>{{ $phase['actual_progress'] }}%</td>
                    <td class="{{ ($phase['progress_deviation'] ?? 0) < 0 ? 'text-red-600' : 'text-green-600' }}">
                        {{ $phase['progress_deviation'] ?? '—' }}%
                    </td>
                    <td>{{ $phase['spi'] ?? '—' }}</td>
                    <td>
                        @if($phase['is_on_track'] === true)
                            <span class="badge badge-success">{{ __('Sí') }}</span>
                        @elseif($phase['is_on_track'] === false)
                            <span class="badge badge-error">{{ __('No') }}</span>
                        @else
                            <span class="badge badge-ghost">{{ __('N/A') }}</span>
                        @endif
                    </td>
                </tr>
                @endforeach
            </tbody>
        </table>
    </div>
</div>
@endif

@if(!empty($deviations['features']))
<div class="phase-block">
    <div class="section-title" style="margin-top:0;">{{ __('Desvíos por Elemento') }}</div>
    <div class="overflow-x-auto">
        <table>
            <thead>
                <tr>
                    <th>Elemento</th>
                    <th>Fase</th>
                    <th>Inicio Plan</th>
                    <th>Fin Plan</th>
                    <th>Inicio Real</th>
                    <th>Fin Real</th>
                    <th>Δ Inicio (d)</th>
                    <th>Δ Fin (d)</th>
                    <th>Prog. Plan (%)</th>
                    <th>Prog. Real (%)</th>
                    <th>Δ Prog.</th>
                    <th>SPI</th>
                    <th>En Plazo</th>
                    <th>Responsable</th>
                </tr>
            </thead>
            <tbody>
                @foreach($deviations['features'] as $feature)
                <tr>
                    <td class="font-medium">{{ $feature['name'] }}</td>
                    <td>{{ $feature['phase'] }}</td>
                    <td>{{ $feature['planned_start'] ?? '—' }}</td>
                    <td>{{ $feature['planned_end'] ?? '—' }}</td>
                    <td>{{ $feature['actual_start'] ?? '—' }}</td>
                    <td>{{ $feature['actual_end'] ?? '—' }}</td>
                    <td class="{{ ($feature['start_deviation'] ?? 0) > 0 ? 'text-red-600' : (($feature['start_deviation'] ?? 0) < 0 ? 'text-green-600' : '') }}">
                        {{ $feature['start_deviation'] ?? '—' }}
                    </td>
                    <td class="{{ ($feature['end_deviation'] ?? 0) > 0 ? 'text-red-600' : (($feature['end_deviation'] ?? 0) < 0 ? 'text-green-600' : '') }}">
                        {{ $feature['end_deviation'] ?? '—' }}
                    </td>
                    <td>{{ $feature['planned_progress'] }}%</td>
                    <td>{{ $feature['actual_progress'] }}%</td>
                    <td class="{{ ($feature['progress_deviation'] ?? 0) < 0 ? 'text-red-600' : 'text-green-600' }}">
                        {{ $feature['progress_deviation'] ?? '—' }}%
                    </td>
                    <td>{{ $feature['spi'] ?? '—' }}</td>
                    <td>
                        @if($feature['is_on_track'] === true)
                            <span class="badge badge-success">{{ __('Sí') }}</span>
                        @elseif($feature['is_on_track'] === false)
                            <span class="badge badge-error">{{ __('No') }}</span>
                        @else
                            <span class="badge badge-ghost">{{ __('N/A') }}</span>
                        @endif
                    </td>
                    <td>{{ $feature['responsible'] }}</td>
                </tr>
                @endforeach
            </tbody>
        </table>
    </div>
</div>
@endif