<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Dental chart · {{ $patient->fullName() }}</title>
    <style>
        body { font-family: ui-sans-serif, system-ui, sans-serif; color: #0f172a; padding: 24px; }
        h1 { font-size: 18px; margin: 0 0 4px; }
        h2 { font-size: 14px; margin: 24px 0 8px; }
        .meta { color: #64748b; font-size: 12px; }
        table { width: 100%; border-collapse: collapse; font-size: 12px; }
        th, td { border: 1px solid #e2e8f0; padding: 6px 8px; text-align: left; }
        th { background: #f8fafc; }
        .teeth { display: flex; flex-wrap: wrap; gap: 4px; margin: 8px 0; }
        .tooth { width: 36px; text-align: center; border: 1px solid #cbd5e1; border-radius: 6px; padding: 6px 0; font-size: 11px; font-weight: 600; }
        .tooth.finding { background: #fffbeb; border-color: #fcd34d; }
        @media print { button { display: none; } }
    </style>
</head>
<body>
    <button onclick="window.print()" style="margin-bottom:16px;padding:8px 12px;">Print</button>
    <h1>{{ $patient->fullName() }}</h1>
    <p class="meta">
        {{ $patient->patient_number }}
        · Visit {{ $visit->uuid }}
        · Charted {{ $chart->charted_at?->format('d M Y H:i') ?? '—' }}
    </p>

    <h2>Odontogram (FDI)</h2>
    @foreach (['upper' => 'Upper', 'lower' => 'Lower'] as $key => $label)
        <p class="meta">{{ $label }}</p>
        <div class="teeth">
            @foreach ($rows[$key] as $code)
                @php $t = $teethMap[$code] ?? []; $finding = ($t['status'] ?? 'present') !== 'present' || !empty($t['conditions']); @endphp
                <div class="tooth {{ $finding ? 'finding' : '' }}">{{ $code }}</div>
            @endforeach
        </div>
    @endforeach

    <h2>Findings</h2>
    <table>
        <thead>
            <tr><th>Tooth</th><th>Status</th><th>Surfaces</th><th>Conditions</th><th>Notes</th></tr>
        </thead>
        <tbody>
            @foreach ($teethMap as $code => $t)
                @if (($t['status'] ?? 'present') !== 'present' || ! empty($t['conditions']) || ! empty($t['notes']))
                    <tr>
                        <td>{{ $code }}</td>
                        <td>{{ $t['status'] ?? 'present' }}</td>
                        <td>{{ implode('', $t['surfaces'] ?? []) }}</td>
                        <td>
                            @foreach ($t['conditions'] ?? [] as $c)
                                {{ $conditions[$c] ?? $c }}@if(!$loop->last), @endif
                            @endforeach
                        </td>
                        <td>{{ $t['notes'] ?? '' }}</td>
                    </tr>
                @endif
            @endforeach
        </tbody>
    </table>

    @if ($plan)
        <h2>Treatment plan ({{ $plan->status }})</h2>
        <table>
            <thead>
                <tr><th>Procedure</th><th>Tooth</th><th>Status</th><th>Estimate</th></tr>
            </thead>
            <tbody>
                @foreach ($plan->items as $item)
                    <tr>
                        <td>{{ $item->label }}</td>
                        <td>{{ $item->tooth_code ?? '—' }}</td>
                        <td>{{ $item->status }}</td>
                        <td>{{ number_format($item->estimate_minor / 100, 2) }}</td>
                    </tr>
                @endforeach
            </tbody>
        </table>
    @endif
</body>
</html>
