<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vaccination Record — {{ $patient->patient_name }}</title>
<style>
/* ── Reset & base ──────────────────────────────────────── */
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
    font-family: 'Segoe UI', 'Helvetica Neue', Arial, sans-serif;
    color: #1f2937;
    background: #fff;
    font-size: 13px;
    line-height: 1.55;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
}
.page { max-width: 980px; margin: 0 auto; padding: 28px 38px 40px; }

/* ── Print toolbar (screen-only) ───────────────────────── */
.print-toolbar {
    position: fixed; top: 14px; right: 18px; z-index: 100;
    background: #fff; padding: 8px 12px; border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0,0,0,.12); display: flex; gap: 6px;
}
.print-toolbar .btn {
    font-size: 12px; padding: 7px 14px; cursor: pointer; border: none;
    border-radius: 5px; font-weight: 600; letter-spacing: .2px;
}
.btn-print { background: linear-gradient(135deg, #16a34a, #15803d); color: #fff; }
.btn-print:hover { filter: brightness(1.08); }
.btn-close { background: #e5e7eb; color: #1f2937; }

/* ── Letterhead ────────────────────────────────────────── */
.letterhead {
    display: flex; justify-content: space-between; align-items: flex-start;
    gap: 24px; padding-bottom: 18px; margin-bottom: 22px;
    border-bottom: 4px double #16a34a;
}
.lh-left { flex: 1; min-width: 0; }
.lh-emblem {
    display: inline-flex; align-items: center; gap: 10px;
    margin-bottom: 6px;
}
.lh-emblem-icon {
    width: 42px; height: 42px; border-radius: 50%;
    background: linear-gradient(135deg, #16a34a, #15803d);
    color: #fff; display: flex; align-items: center; justify-content: center;
    font-size: 22px;
}
.lh-biz-name { font-size: 22px; font-weight: 800; color: #111827; letter-spacing: -.3px; }
.lh-biz-meta { font-size: 11.5px; color: #6b7280; line-height: 1.5; margin-top: 2px; }
.lh-right {
    text-align: right; flex-shrink: 0;
    border-left: 1px solid #e5e7eb; padding-left: 22px;
}
.lh-doc-title {
    font-size: 11px; font-weight: 700; color: #16a34a;
    text-transform: uppercase; letter-spacing: 1.5px;
    margin-bottom: 4px;
}
.lh-doc-h1 {
    font-size: 26px; font-weight: 800; color: #111827;
    line-height: 1.05; letter-spacing: -.5px;
}
.lh-doc-sub { font-size: 11px; color: #9ca3af; margin-top: 6px; }
.lh-doc-pet { font-size: 13px; color: #111827; font-weight: 700; margin-top: 4px; }

/* ── Patient card ──────────────────────────────────────────── */
.pet-card {
    background: linear-gradient(180deg, #f0fdf4 0%, #ffffff 100%);
    border: 1px solid #bbf7d0; border-radius: 12px;
    padding: 18px 22px; margin-bottom: 22px;
    page-break-inside: avoid;
}
.pet-card-grid {
    display: grid;
    grid-template-columns: minmax(180px, 1fr) repeat(4, minmax(110px, 1fr));
    gap: 16px;
    align-items: start;
}
.pet-headline { display: flex; align-items: center; gap: 12px; }
.pet-avatar {
    width: 54px; height: 54px; border-radius: 14px;
    background: linear-gradient(135deg, #22c55e, #16a34a); color: #fff;
    display: flex; align-items: center; justify-content: center;
    font-size: 26px; flex-shrink: 0;
    box-shadow: 0 4px 10px rgba(22,163,74,.25);
}
.pet-name { font-size: 18px; font-weight: 800; color: #111827; line-height: 1.1; }
.pet-subline { font-size: 11.5px; color: #6b7280; margin-top: 3px; }
.field { min-width: 0; }
.field-label {
    font-size: 9.5px; text-transform: uppercase; letter-spacing: .7px;
    color: #65a30d; font-weight: 800; margin-bottom: 3px;
}
.field-value {
    font-size: 13px; font-weight: 600; color: #111827;
    word-wrap: break-word;
}
.field-sub { font-size: 11px; color: #6b7280; margin-top: 1px; }

/* ── KPI summary strip ────────────────────────────────── */
.kpi-row {
    display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px;
    margin-bottom: 22px; page-break-inside: avoid;
}
.kpi {
    border-radius: 10px; padding: 12px 14px;
    border: 1px solid var(--kc-border);
    background: var(--kc-bg);
    display: flex; align-items: center; gap: 10px;
}
.kpi-icon {
    width: 36px; height: 36px; border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
    font-size: 16px; color: #fff; flex-shrink: 0;
    background: var(--kc-color);
}
.kpi-num { font-size: 22px; font-weight: 800; color: var(--kc-color); line-height: 1; }
.kpi-lbl { font-size: 10px; color: #6b7280; text-transform: uppercase; letter-spacing: .5px; margin-top: 3px; font-weight: 700; }

.kpi.k-total    { --kc-color:#4f46e5; --kc-bg:#eef2ff; --kc-border:#c7d2fe; }
.kpi.k-overdue  { --kc-color:#dc2626; --kc-bg:#fef2f2; --kc-border:#fecaca; }
.kpi.k-due      { --kc-color:#d97706; --kc-bg:#fffbeb; --kc-border:#fde68a; }
.kpi.k-upcoming { --kc-color:#0ea5e9; --kc-bg:#f0f9ff; --kc-border:#bae6fd; }

/* ── Section heading ──────────────────────────────────── */
.section {
    margin-top: 26px;
    page-break-before: auto;
}
.section-title {
    display: flex; align-items: baseline; justify-content: space-between;
    border-left: 4px solid #16a34a; padding: 0 0 0 12px;
    margin: 0 0 12px;
}
.section-title h2 {
    font-size: 16px; font-weight: 800; color: #111827;
    margin: 0; letter-spacing: -.2px;
}
.section-title .count {
    font-size: 11.5px; color: #6b7280; font-weight: 600;
}

/* ── Upcoming vaccine cards ───────────────────────────── */
.upcoming-grid {
    display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 10px;
}
.upcoming-card {
    border: 1.5px solid var(--cc-border, #e5e7eb);
    border-left-width: 5px;
    border-radius: 8px;
    padding: 11px 13px;
    background: var(--cc-bg, #fff);
    page-break-inside: avoid;
}
.uc-vaccine { font-size: 14px; font-weight: 800; color: #111827; line-height: 1.2; }
.uc-mfr { font-size: 10.5px; color: #6b7280; margin-top: 1px; }
.uc-row { display: flex; justify-content: space-between; margin-top: 8px; gap: 10px; align-items: flex-end; }
.uc-cell { min-width: 0; }
.uc-cell .l { font-size: 9px; text-transform: uppercase; letter-spacing: .5px; color: #9ca3af; font-weight: 700; }
.uc-cell .v { font-size: 12px; font-weight: 700; color: #111827; margin-top: 1px; }
.uc-status-tag {
    display: inline-block; padding: 3px 9px; border-radius: 12px;
    font-size: 10px; font-weight: 800; text-transform: uppercase; letter-spacing: .4px;
}

.upcoming-card.uc-overdue  { --cc-border:#dc2626; --cc-bg:#fef2f2; }
.upcoming-card.uc-due      { --cc-border:#d97706; --cc-bg:#fffbeb; }
.upcoming-card.uc-soon     { --cc-border:#0ea5e9; --cc-bg:#f0f9ff; }
.upcoming-card.uc-upcoming { --cc-border:#10b981; --cc-bg:#f0fdf4; }

.tag-overdue  { background: #fef2f2; color: #b91c1c; border: 1px solid #fecaca; }
.tag-due      { background: #fffbeb; color: #92400e; border: 1px solid #fde68a; }
.tag-soon     { background: #f0f9ff; color: #075985; border: 1px solid #bae6fd; }
.tag-upcoming { background: #f0fdf4; color: #166534; border: 1px solid #bbf7d0; }

/* ── History table ────────────────────────────────────── */
.history-table { width: 100%; border-collapse: separate; border-spacing: 0; font-size: 12px; }
.history-table thead th {
    background: #f9fafb;
    color: #374151; font-weight: 700;
    font-size: 10px; text-transform: uppercase; letter-spacing: .5px;
    padding: 9px 11px;
    border-bottom: 2px solid #d1d5db;
    text-align: left;
    white-space: nowrap;
}
.history-table thead th:first-child { border-top-left-radius: 8px; }
.history-table thead th:last-child  { border-top-right-radius: 8px; }
.history-table tbody td {
    padding: 9px 11px;
    border-bottom: 1px solid #f3f4f6;
    vertical-align: top;
}
.history-table tbody tr { page-break-inside: avoid; }
.history-table tbody tr:hover { background: #fafbfc; }
.history-table .col-num { color: #9ca3af; font-weight: 700; font-size: 11px; width: 32px; }
.history-table .col-vaccine { font-weight: 700; color: #111827; }
.history-table .col-vaccine .mfr { font-size: 10.5px; color: #6b7280; font-weight: 500; margin-top: 1px; }
.history-table .col-batch { font-family: 'Courier New', monospace; font-size: 11px; color: #4b5563; }
.history-table .col-date { white-space: nowrap; font-weight: 600; }
.history-table .col-next { font-weight: 700; color: #0e7490; }

.row-note td {
    background: #fffbeb !important;
    font-size: 11px; color: #78350f; font-style: italic;
    padding: 6px 11px !important; border-top: none !important;
    border-left: 3px solid #f59e0b;
}

.status-pill {
    display: inline-block; padding: 2px 9px; border-radius: 10px;
    font-size: 9.5px; font-weight: 800; text-transform: uppercase; letter-spacing: .4px;
    border: 1px solid transparent;
}
.pill-administered { background: #dcfce7; color: #166534; border-color: #bbf7d0; }
.pill-waived       { background: #f3f4f6; color: #4b5563; border-color: #d1d5db; }
.pill-scheduled    { background: #dbeafe; color: #1e40af; border-color: #bfdbfe; }

/* ── Grouped history (by category → vaccine) ────────── */
.vax-cat-block { margin-bottom: 18px; page-break-inside: avoid; }
.vax-cat-hd {
    display: flex; align-items: baseline; justify-content: space-between;
    border-left: 4px solid #16a34a; background: #f9fafb;
    padding: 7px 14px; border-radius: 0 6px 6px 0; margin-bottom: 8px;
}
.vax-cat-name { font-size: 13px; font-weight: 800; color: #111827; text-transform: uppercase; letter-spacing: .4px; }
.vax-cat-count { font-size: 10.5px; color: #6b7280; font-weight: 600; }
.vax-name-block { margin: 8px 0 12px 18px; page-break-inside: avoid; }
.vax-name-hd { font-size: 12.5px; color: #111827; padding: 4px 0 3px; display: flex; align-items: baseline; gap: 6px; flex-wrap: wrap; }
.vax-name-hd .vax-mfr { font-size: 10.5px; color: #6b7280; font-weight: 500; }
.vax-name-hd .vax-count { font-size: 10.5px; color: #4f46e5; font-weight: 700; margin-left: auto; }
.vax-name-tbl { width: 100%; border-collapse: collapse; font-size: 11px; margin-top: 3px; }
.vax-name-tbl thead th {
    background: #f9fafb; color: #6b7280; font-weight: 700;
    font-size: 9px; text-transform: uppercase; letter-spacing: .4px;
    padding: 5px 8px; border-bottom: 1px solid #e5e7eb; text-align: left;
}
.vax-name-tbl tbody td {
    padding: 5px 8px; border-bottom: 1px solid #f3f4f6; vertical-align: top;
}
.vax-name-tbl tbody tr:last-child td { border-bottom: none; }
.vax-name-tbl .col-num { color: #9ca3af; font-weight: 700; width: 24px; }
.vax-name-tbl .col-next { color: #0e7490; font-weight: 700; }

/* ── Empty state ──────────────────────────────────────── */
.empty {
    padding: 32px 18px; text-align: center; color: #9ca3af;
    background: #fafbfc; border: 1.5px dashed #d1d5db; border-radius: 10px;
    font-size: 13px;
}
.empty .icon { font-size: 28px; margin-bottom: 6px; opacity: .45; display: block; }

/* ── Signature & footer ───────────────────────────────── */
.signature-row {
    display: grid; grid-template-columns: 1fr 1fr; gap: 60px;
    margin-top: 38px; padding-top: 16px;
    page-break-inside: avoid;
}
.sig-block { text-align: center; }
.sig-line  { border-bottom: 1.5px solid #1f2937; height: 1px; margin: 26px 0 6px; }
.sig-label { font-size: 11px; color: #6b7280; font-weight: 600; }

.print-footer {
    margin-top: 30px; padding-top: 14px; border-top: 1px solid #e5e7eb;
    font-size: 10px; color: #9ca3af; text-align: center; line-height: 1.7;
}
.print-footer strong { color: #4b5563; }
.print-footer .qr-note { display: block; font-size: 9px; margin-top: 2px; }

/* ── Print-specific ───────────────────────────────────── */
@media print {
    .print-toolbar { display: none; }
    .page { padding: 14px 22px; max-width: none; }
    .pet-card, .kpi-row, .upcoming-card, .history-table tbody tr, .signature-row {
        page-break-inside: avoid;
    }
    .section { page-break-before: auto; }
    body { font-size: 11.5px; }
}
</style>
</head>
<body>

<div class="print-toolbar">
    <button class="btn btn-print" onclick="window.print()">🖨 Print</button>
    <button class="btn btn-close" onclick="window.close()">Close</button>
</div>

@php
$today      = \Carbon\Carbon::today();
$dateFormat = session('business.date_format', 'd M Y');

// Compute due categories from all administered, non-waived rows with a next_due_date
$administered = $patient->vaccinations->where('status', 'administered')->whereNull('waived_at');
$upcoming = [];
foreach ($administered as $vax) {
    if ($vax->next_due_date) $upcoming[] = $vax;
}
usort($upcoming, fn($a, $b) => $a->next_due_date <=> $b->next_due_date);

// KPI buckets
$kpiTotal    = $patient->vaccinations->count();
$kpiOverdue  = 0;
$kpiDue      = 0; // due within 7 days
$kpiUpcoming = 0;
foreach ($upcoming as $vax) {
    $diff = $today->diffInDays($vax->next_due_date, false);
    if ($diff < 0)      $kpiOverdue++;
    elseif ($diff <= 7) $kpiDue++;
    else                $kpiUpcoming++;
}

function clc_due_state($next, $today) {
    if (!$next) return ['—', '', ''];
    $diff = $today->diffInDays($next, false);
    if ($diff < 0)        return ['Overdue ' . abs($diff) . 'd', 'uc-overdue', 'tag-overdue'];
    if ($diff === 0)      return ['Due Today',          'uc-due',      'tag-due'];
    if ($diff <= 7)       return ['Due in ' . $diff . 'd', 'uc-due',   'tag-due'];
    if ($diff <= 30)      return ['Due in ' . $diff . 'd', 'uc-soon',  'tag-soon'];
    return ['In ' . $diff . 'd', 'uc-upcoming', 'tag-upcoming'];
}

// Skin Type emoji
$speciesEmoji = ['dog'=>'🐕','cat'=>'🐈','rabbit'=>'🐇','bird'=>'🦜','horse'=>'🐎','cow'=>'🐄','sheep'=>'🐑','goat'=>'🐐','fish'=>'🐟'];
$patientEmoji = $speciesEmoji[strtolower($patient->species ?? '')] ?? '🐾';
@endphp

<div class="page">

    {{-- ═══════════ Letterhead ═══════════ --}}
    <div class="letterhead">
        <div class="lh-left">
            <div class="lh-emblem">
                <span class="lh-emblem-icon">🩺</span>
                <div>
                    <div class="lh-biz-name">{{ $business->name }}</div>
                    <div class="lh-biz-meta">
                        @if(!empty($business->address)) {{ $business->address }}<br>@endif
                        @if(!empty($business->mobile))<strong>Tel:</strong> {{ $business->mobile }}@endif
                        @if(!empty($business->email)) &nbsp;·&nbsp; <strong>Email:</strong> {{ $business->email }}@endif
                    </div>
                </div>
            </div>
        </div>
        <div class="lh-right">
            <div class="lh-doc-title">Vaccination Record</div>
            <div class="lh-doc-h1">Certificate</div>
            <div class="lh-doc-pet">{{ $patientEmoji }} {{ $patient->patient_name }}</div>
            <div class="lh-doc-sub">Issued {{ now()->format($dateFormat) }}</div>
        </div>
    </div>

    {{-- ═══════════ Patient card ═══════════ --}}
    <div class="pet-card">
        <div class="pet-card-grid">
            <div class="pet-headline">
                <div class="pet-avatar">{{ $patientEmoji }}</div>
                <div>
                    <div class="pet-name">{{ $patient->patient_name }}</div>
                    <div class="pet-subline">
                        @if(config('cliniccore.show_animal_fields', false)){{ ucfirst($patient->species ?? '—') }}{{ $patient->breed ? ' · ' . $patient->breed : '' }} @endif
                        @if($patient->dob)@if(config('cliniccore.show_animal_fields', false)) · @endif<strong>{{ \Carbon\Carbon::parse($patient->dob)->age }} yrs</strong> @endif
                    </div>
                </div>
            </div>
            <div class="field">
                <div class="field-label">Contact</div>
                <div class="field-value">{{ optional($patient->owner)->name ?? optional($patient->owner)->supplier_business_name ?? '—' }}</div>
                @if(optional($patient->owner)->mobile)
                    <div class="field-sub">📞 {{ $patient->owner->mobile }}</div>
                @endif
            </div>
            <div class="field">
                <div class="field-label">Gender</div>
                <div class="field-value">{{ ucfirst($patient->gender ?? '—') }}</div>
                @if(!is_null($patient->weight_kg ?? null))
                    <div class="field-sub">{{ $patient->weight_kg }} kg</div>
                @endif
            </div>
            @if(config('cliniccore.show_animal_fields', false))
            <div class="field">
                <div class="field-label">File No.</div>
                <div class="field-value">{{ $patient->microchip_no ?: '—' }}</div>
            </div>
            <div class="field">
                <div class="field-label">Color</div>
                <div class="field-value">{{ $patient->color ?: '—' }}</div>
            </div>
            @endif
        </div>
    </div>

    {{-- ═══════════ KPI strip ═══════════ --}}
    <div class="kpi-row">
        <div class="kpi k-total">
            <div class="kpi-icon">💉</div>
            <div>
                <div class="kpi-num">{{ $kpiTotal }}</div>
                <div class="kpi-lbl">Total Records</div>
            </div>
        </div>
        <div class="kpi k-overdue">
            <div class="kpi-icon">⚠️</div>
            <div>
                <div class="kpi-num">{{ $kpiOverdue }}</div>
                <div class="kpi-lbl">Overdue</div>
            </div>
        </div>
        <div class="kpi k-due">
            <div class="kpi-icon">⏰</div>
            <div>
                <div class="kpi-num">{{ $kpiDue }}</div>
                <div class="kpi-lbl">Due This Week</div>
            </div>
        </div>
        <div class="kpi k-upcoming">
            <div class="kpi-icon">📅</div>
            <div>
                <div class="kpi-num">{{ $kpiUpcoming }}</div>
                <div class="kpi-lbl">Upcoming</div>
            </div>
        </div>
    </div>

    {{-- ═══════════ Upcoming / Due cards ═══════════ --}}
    <div class="section">
        <div class="section-title">
            <h2>Upcoming &amp; Due Vaccinations</h2>
            <span class="count">{{ count($upcoming) }} {{ count($upcoming) === 1 ? 'reminder' : 'reminders' }}</span>
        </div>
        @if(empty($upcoming))
            <div class="empty"><span class="icon">✅</span>No upcoming or due vaccinations.<br><small>All scheduled vaccines are up to date.</small></div>
        @else
            <div class="upcoming-grid">
                @foreach($upcoming as $vax)
                    @php [$lbl, $cardCls, $tagCls] = clc_due_state($vax->next_due_date, $today); @endphp
                    <div class="upcoming-card {{ $cardCls }}">
                        <div class="uc-vaccine">{{ optional($vax->vaccineType)->name ?? '—' }}</div>
                        @if(optional($vax->vaccineType)->manufacturer)
                            <div class="uc-mfr">{{ $vax->vaccineType->manufacturer }}</div>
                        @endif
                        <div class="uc-row">
                            <div class="uc-cell">
                                <div class="l">Last given</div>
                                <div class="v">{{ optional($vax->administered_date)->format($dateFormat) ?? '—' }}</div>
                            </div>
                            <div class="uc-cell" style="text-align:right;">
                                <div class="l">Next due</div>
                                <div class="v">{{ optional($vax->next_due_date)->format($dateFormat) ?? '—' }}</div>
                            </div>
                        </div>
                        <div style="margin-top:8px;">
                            <span class="status-pill uc-status-tag {{ $tagCls }}">{{ $lbl }}</span>
                        </div>
                    </div>
                @endforeach
            </div>
        @endif
    </div>

    {{-- ═══════════ Grouped history (Category → Treatment) ═══════════ --}}
    @php
        // Category labels + colours (mirrors ClcVaccineTypeController).
        $catLabels = [
            'core_vaccine'     => 'Core Vaccines',
            'non_core_vaccine' => 'Non-Core Vaccines',
            'nasal'            => 'Nasal Vaccines',
            'oral'             => 'Oral Treatments / Treatments',
            'spot_on'          => 'Spot-on Antiparasitics',
            'deworming'        => 'Deworming',
            'topical'          => 'Topical Treatments',
            'preventive'       => 'Preventive Care',
            'other'            => 'Other',
            '_unknown'         => 'Uncategorised',
        ];
        $catColors = [
            'core_vaccine'     => '#16a34a',
            'non_core_vaccine' => '#0ea5e9',
            'nasal'            => '#06b6d4',
            'oral'             => '#8b5cf6',
            'spot_on'          => '#f59e0b',
            'deworming'        => '#22c55e',
            'topical'          => '#a855f7',
            'preventive'       => '#10b981',
            'other'            => '#94a3b8',
            '_unknown'         => '#94a3b8',
        ];
        // Build: category → vaccine name → [vaccinations sorted by date]
        $grouped = [];
        foreach ($patient->vaccinations as $vax) {
            $cat   = optional($vax->vaccineType)->category ?: '_unknown';
            $vname = optional($vax->vaccineType)->name ?: 'Unknown vaccine';
            $grouped[$cat][$vname][] = $vax;
        }
        // Order categories by the predefined sequence, vaccines alphabetically,
        // doses oldest → newest so the dose number reflects order given.
        $order = array_keys($catLabels);
        uksort($grouped, fn($a, $b) => array_search($a, $order) <=> array_search($b, $order));
        foreach ($grouped as &$byName) {
            ksort($byName);
            foreach ($byName as &$rows) {
                usort($rows, fn($a, $b) => ($a->administered_date ?? '') <=> ($b->administered_date ?? ''));
            }
        }
        unset($byName, $rows);
    @endphp

    <div class="section">
        <div class="section-title">
            <h2>Vaccination History — by Vaccine</h2>
            <span class="count">{{ $patient->vaccinations->count() }} {{ $patient->vaccinations->count() === 1 ? 'record' : 'records' }}</span>
        </div>
        @if($patient->vaccinations->isEmpty())
            <div class="empty"><span class="icon">📋</span>No vaccination records for this patient.</div>
        @else
            @foreach($grouped as $cat => $byName)
            @php $catTotal = collect($byName)->map(fn($l) => count($l))->sum(); @endphp
            <div class="vax-cat-block">
                <div class="vax-cat-hd" style="border-left-color: {{ $catColors[$cat] ?? '#94a3b8' }};">
                    <span class="vax-cat-name" style="color: {{ $catColors[$cat] ?? '#111827' }};">
                        {{ $catLabels[$cat] ?? ucfirst(str_replace('_', ' ', $cat)) }}
                    </span>
                    <span class="vax-cat-count">{{ $catTotal }} {{ $catTotal === 1 ? 'dose' : 'doses' }} · {{ count($byName) }} {{ count($byName) === 1 ? 'product' : 'products' }}</span>
                </div>
                @foreach($byName as $vname => $rows)
                <div class="vax-name-block">
                    <div class="vax-name-hd">
                        <span>💉 <strong>{{ $vname }}</strong></span>
                        @if($mfr = optional($rows[0]->vaccineType)->manufacturer)
                            <span class="vax-mfr">· {{ $mfr }}</span>
                        @endif
                        @if($validFor = optional($rows[0]->vaccineType)->valid_for_months)
                            <span class="vax-mfr">· every {{ $validFor }} mo</span>
                        @endif
                        <span class="vax-count">{{ count($rows) }} {{ count($rows) === 1 ? 'dose' : 'doses' }}</span>
                    </div>
                    <table class="vax-name-tbl">
                        <thead>
                            <tr>
                                <th class="col-num">#</th>
                                <th>Administered</th>
                                <th>Batch No.</th>
                                <th style="text-align:right;">Weight</th>
                                <th>Administered By</th>
                                <th>Status</th>
                                <th>Next Due</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($rows as $i => $vax)
                                @php
                                $statusCls = 'pill-administered';
                                $statusLabel = ucfirst($vax->status ?? 'administered');
                                if ($vax->waived_at) { $statusCls = 'pill-waived'; $statusLabel = 'Waived'; }
                                elseif (($vax->status ?? '') === 'scheduled') { $statusCls = 'pill-scheduled'; }
                                @endphp
                                <tr>
                                    <td class="col-num">{{ $i + 1 }}</td>
                                    <td>{{ optional($vax->administered_date)->format($dateFormat) ?? '—' }}</td>
                                    <td>{{ $vax->batch_no ?: '—' }}</td>
                                    <td style="text-align:right;">{{ $vax->weight_at_time ? $vax->weight_at_time . ' kg' : '—' }}</td>
                                    <td>{{ $vax->administered_by_name ?? '—' }}</td>
                                    <td><span class="status-pill {{ $statusCls }}">{{ $statusLabel }}</span></td>
                                    <td class="col-next">{{ optional($vax->next_due_date)->format($dateFormat) ?? '—' }}</td>
                                </tr>
                                @if($vax->notes)
                                    <tr class="row-note">
                                        <td colspan="7"><strong>Note:</strong> {{ $vax->notes }}</td>
                                    </tr>
                                @endif
                            @endforeach
                        </tbody>
                    </table>
                </div>
                @endforeach
            </div>
            @endforeach
        @endif
    </div>

    {{-- ═══════════ Signature ═══════════ --}}
    <div class="signature-row">
        <div class="sig-block">
            <div class="sig-line"></div>
            <div class="sig-label">Doctor Signature &amp; Stamp</div>
        </div>
        <div class="sig-block">
            <div class="sig-line"></div>
            <div class="sig-label">Patient Signature &amp; Date</div>
        </div>
    </div>

    {{-- ═══════════ Verification QR ═══════════ --}}
    @if(!empty($verifyUrl))
    <div style="margin-top: 18px; padding: 12px 16px; background: #f8fafc;
                border: 1px dashed #16a34a; border-radius: 8px;
                display: flex; align-items: center; gap: 16px;
                page-break-inside: avoid;">
        <img src="{{ $qrSrc }}" alt="Verify QR"
             style="width: 84px; height: 84px; flex-shrink: 0;
                    background: #fff; border: 1px solid #e5e7eb; border-radius: 4px;">
        <div style="font-size: 11px; color: #374151; line-height: 1.6;">
            <div style="font-weight: 700; color: #15803d; font-size: 12px; margin-bottom: 3px;">
                <i style="font-style:normal;">✓</i> Authenticity check
            </div>
            <div>Scan the QR or open the link below to verify this certificate
                was issued by <strong>{{ $business->name }}</strong>.</div>
            <div style="font-family: 'Consolas','Monaco',monospace; margin-top: 4px;
                        font-size: 10px; color: #6b7280; word-break: break-all;">
                {{ $verifyUrl }}
            </div>
        </div>
    </div>
    @endif

    {{-- ═══════════ Footer ═══════════ --}}
    <div class="print-footer">
        <strong>{{ $business->name }}</strong> · Vaccination Record · Generated {{ now()->format($dateFormat . ' H:i') }}
        @if(auth()->user())
            · by <strong>{{ trim((auth()->user()->first_name ?? '') . ' ' . (auth()->user()->last_name ?? '')) }}</strong>
        @endif
        <span class="qr-note">This document was generated electronically from the {{ $business->name }} clinic management system.</span>
    </div>

    {{-- Item #8 - TPS attribution footer (shared partial) --}}
    @include('clinicCore::partials._print_branding', ['mode' => 'footer'])

</div>{{-- /page --}}

</body>
</html>
