<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>{{ $referral->referral_no }}</title>
<style>
*, *::before, *::after { box-sizing:border-box; margin:0; padding:0; }
body { font-family:'Segoe UI', Arial, sans-serif; font-size:13px; color:#1e293b; background:#fff; }
* { -webkit-print-color-adjust:exact !important; print-color-adjust:exact !important; }
.page { max-width:800px; margin:0 auto; min-height:100vh; display:flex; flex-direction:column; }
.letterhead { background:linear-gradient(135deg,#1a3a5c 0%,#2980b9 60%,#3498db 100%); color:#fff; padding:22px 32px 18px; }
.letterhead-inner { display:flex; align-items:center; gap:20px; }
.clinic-icon { width:58px; height:58px; background:rgba(255,255,255,.18); border:1.5px solid rgba(255,255,255,.32); border-radius:14px; display:flex; align-items:center; justify-content:center; font-size:26px; flex-shrink:0; }
.clinic-name { font-size:22px; font-weight:800; margin-bottom:3px; }
.clinic-contact { font-size:11.5px; color:rgba(255,255,255,.82); display:flex; flex-wrap:wrap; gap:12px; }
.ord-strip { background:#f8fafc; border-bottom:2px solid #e2e8f0; padding:12px 32px; display:flex; align-items:center; gap:14px; }
.ord-label { background:linear-gradient(135deg,#1a3a5c,#2980b9); color:#fff; padding:4px 14px; border-radius:20px; font-size:11px; font-weight:700; letter-spacing:.5px; }
.ord-ref { font-size:16px; font-weight:800; color:#1a3a5c; }
.ord-meta { margin-left:auto; font-size:11.5px; color:#64748b; }
.urgent { color:#b45309; font-weight:700; }
.emergency { color:#dc2626; font-weight:700; }
.body-content { padding:22px 32px; flex:1; }
.info-grid { display:grid; grid-template-columns:1fr 1fr; gap:12px; margin-bottom:18px; }
.info-box { border:1px solid #e2e8f0; border-radius:8px; overflow:hidden; }
.info-box-hd { background:#f1f5f9; padding:5px 12px; font-size:10px; font-weight:700; text-transform:uppercase; letter-spacing:.5px; color:#64748b; border-bottom:1px solid #e2e8f0; }
.info-box-body { padding:10px 12px; }
.info-name { font-size:14px; font-weight:700; margin-bottom:2px; }
.info-sub { font-size:11.5px; color:#64748b; }
.reason-box { background:#eff6ff; border:1px solid #bfdbfe; border-left:4px solid #2980b9; border-radius:8px; padding:10px 14px; margin-bottom:16px; font-size:13px; color:#1e3a5f; white-space:pre-wrap; }
.section-hd { font-size:11px; font-weight:700; text-transform:uppercase; letter-spacing:.5px; color:#475569; border-bottom:2px solid #e2e8f0; padding-bottom:7px; margin:16px 0 12px; }
.summary-box { font-size:13px; color:#334155; white-space:pre-wrap; }
table.items { width:100%; border-collapse:collapse; font-size:12.5px; margin-bottom:4px; }
table.items th, table.items td { border:1px solid #e2e8f0; padding:6px 10px; text-align:left; }
table.items th { background:#f8fafc; font-size:10.5px; text-transform:uppercase; letter-spacing:.4px; color:#64748b; }
.allergy-sev-severe, .allergy-sev-life_threatening { color:#dc2626; font-weight:700; }
.empty { color:#94a3b8; font-style:italic; }
.footer { margin-top:22px; padding:10px 32px; border-top:1px solid #e2e8f0; display:flex; justify-content:space-between; font-size:10.5px; color:#94a3b8; background:#f8fafc; }
.sig { margin-top:26px; display:flex; justify-content:flex-end; }
.sig-box { width:260px; text-align:center; }
.sig-line { border-top:1px solid #94a3b8; margin-top:34px; padding-top:5px; font-size:11.5px; color:#475569; }
@media print { @page { margin:0; } .page { max-width:100%; } }
</style>
</head>
<body>
<div class="page">
    <div class="letterhead">
        <div class="letterhead-inner">
            <div class="clinic-icon"><i>&#9670;</i></div>
            <div>
                <div class="clinic-name">{{ $business->name ?? 'Clinic' }}</div>
                <div class="clinic-contact">
                    @if(!empty($business->mobile))<span>{{ $business->mobile }}</span>@endif
                    @if(!empty($business->email))<span>{{ $business->email }}</span>@endif
                    @if(!empty($business->city))<span>{{ $business->city }}</span>@endif
                </div>
            </div>
        </div>
    </div>

    <div class="ord-strip">
        <span class="ord-label">{{ strtoupper(__('clinicCore::lang.ref_letter')) }}</span>
        <span class="ord-ref">{{ $referral->referral_no }}</span>
        <span class="ord-meta">
            {{ __('clinicCore::lang.urgency') }}:
            <span class="{{ $referral->urgency === 'emergency' ? 'emergency' : ($referral->urgency === 'urgent' ? 'urgent' : '') }}">{{ ucfirst($referral->urgency) }}</span>
            &nbsp;·&nbsp; {{ __('clinicCore::lang.status') }}: {{ ucfirst(str_replace('_',' ', $referral->status)) }}
            &nbsp;·&nbsp; {{ optional($referral->created_at)->format('d M Y H:i') }}
        </span>
    </div>

    <div class="body-content">
        <div class="info-grid">
            <div class="info-box">
                <div class="info-box-hd">{{ __('clinicCore::lang.patients') }}</div>
                <div class="info-box-body">
                    <div class="info-name">{{ optional($referral->patient)->patient_name ?? '—' }}</div>
                    <div class="info-sub">
                        @if(config('cliniccore.show_animal_fields', false)){{ optional($referral->patient)->species }}@endif
                        @if(optional(optional($referral->patient)->owner)->name)
                            @if(config('cliniccore.show_animal_fields', false)) · @endif{{ $referral->patient->owner->name }}
                        @endif
                    </div>
                </div>
            </div>
            <div class="info-box">
                <div class="info-box-hd">
                    @if($referral->direction === 'external_incoming')
                        {{ __('clinicCore::lang.ref_from_provider') }}
                    @else
                        {{ __('clinicCore::lang.ref_from_doctor') }}
                    @endif
                </div>
                <div class="info-box-body">
                    <div class="info-name">
                        {{ $referral->direction === 'external_incoming'
                            ? ($referral->from_provider_name ?? '—')
                            : (optional($referral->fromDoctor)->name ?? '—') }}
                    </div>
                    @if($referral->direction !== 'external_incoming' && optional($referral->fromDoctor)->specialization)
                    <div class="info-sub">{{ $referral->fromDoctor->specialization }}</div>
                    @endif
                </div>
            </div>
        </div>

        {{-- Referred-to --}}
        <div class="info-box" style="margin-bottom:16px;">
            <div class="info-box-hd">{{ __('clinicCore::lang.ref_referred_to') }}</div>
            <div class="info-box-body">
                @if($referral->direction === 'internal')
                    <div class="info-name">{{ optional($referral->toDoctor)->name ?? '—' }}</div>
                    @if(optional($referral->toDoctor)->specialization)
                    <div class="info-sub">{{ $referral->toDoctor->specialization }}</div>
                    @endif
                @elseif($referral->direction === 'external_outgoing')
                    <div class="info-name">{{ $referral->to_facility_name ?? '—' }}</div>
                    @if($referral->to_provider_name)<div class="info-sub">{{ $referral->to_provider_name }}</div>@endif
                @else
                    <div class="info-name">{{ $business->name ?? '—' }}</div>
                    <div class="info-sub">{{ __('clinicCore::lang.ref_dir_external_incoming') }}</div>
                @endif
            </div>
        </div>

        <div class="reason-box"><strong>{{ __('clinicCore::lang.ref_reason') }}:</strong> {{ $referral->reason }}</div>

        @if($referral->clinical_summary)
        <div class="section-hd">{{ __('clinicCore::lang.ref_clinical_summary') }}</div>
        <div class="summary-box">{{ $referral->clinical_summary }}</div>
        @endif

        @if(!empty($referral->requested_services))
        <div class="section-hd">{{ __('clinicCore::lang.ref_requested_services') }}</div>
        <table class="items">
            <thead><tr><th>{{ __('clinicCore::lang.service_name') }}</th><th>ICD</th><th>{{ __('clinicCore::lang.notes') }}</th></tr></thead>
            <tbody>
                @foreach($referral->requested_services as $s)
                <tr>
                    <td>{{ $s['service'] ?? '' }}</td>
                    <td>{{ $s['icd_code'] ?? '—' }}</td>
                    <td>{{ $s['notes'] ?? '—' }}</td>
                </tr>
                @endforeach
            </tbody>
        </table>
        @endif

        <div class="section-hd">{{ __('clinicCore::lang.ref_current_medications') }}</div>
        @if($meds->count())
        <table class="items">
            <thead><tr><th>{{ __('clinicCore::lang.medicine') }}</th><th>{{ __('clinicCore::lang.dosage') }}</th><th>{{ __('clinicCore::lang.frequency') }}</th></tr></thead>
            <tbody>
                @foreach($meds as $m)
                <tr><td>{{ $m->medicine_name }}</td><td>{{ $m->dosage ?: '—' }}</td><td>{{ $m->frequency ?: '—' }}</td></tr>
                @endforeach
            </tbody>
        </table>
        @else
        <div class="empty">{{ __('clinicCore::lang.ref_none_recorded') }}</div>
        @endif

        <div class="section-hd">{{ __('clinicCore::lang.allergies') }}</div>
        @if($allergies->count())
        <table class="items">
            <thead><tr><th>{{ __('clinicCore::lang.allergen') }}</th><th>{{ __('clinicCore::lang.type') }}</th><th>{{ __('clinicCore::lang.severity') }}</th></tr></thead>
            <tbody>
                @foreach($allergies as $a)
                <tr>
                    <td>{{ $a->allergen }}</td>
                    <td>{{ ucfirst($a->type) }}</td>
                    <td class="allergy-sev-{{ $a->severity }}">{{ ucfirst(str_replace('_',' ', $a->severity)) }}</td>
                </tr>
                @endforeach
            </tbody>
        </table>
        @else
        <div class="empty">{{ __('clinicCore::lang.ref_none_recorded') }}</div>
        @endif

        <div class="sig">
            <div class="sig-box">
                <div class="sig-line">
                    {{ $referral->direction === 'external_incoming'
                        ? ($referral->from_provider_name ?? '')
                        : (optional($referral->fromDoctor)->name ?? '') }}
                    <br><small>{{ __('clinicCore::lang.ref_referring_signature') }}</small>
                </div>
            </div>
        </div>
    </div>

    <div class="footer">
        <span>{{ __('clinicCore::lang.generated') }}: {{ now()->format('d M Y, H:i') }}</span>
        <span>{{ $referral->referral_no }} &bull; {{ $business->name ?? '' }}</span>
    </div>
</div>

@includeIf('clinicCore::partials._print_branding', ['mode' => 'footer'])

<script>window.onload = function () { window.print(); };</script>
</body>
</html>
