<?php
$e=static fn($v)=>htmlspecialchars((string)$v,ENT_QUOTES,'UTF-8');
$b=$invoice['business'];
$money=static fn($v)=>number_format((float)$v,2);

?>
<!doctype html><html><head><meta charset="utf-8"><style>
@page{margin:52px 58px 50px 58px}
body{font-family:DejaVu Sans,sans-serif;font-size:11.5px;line-height:1.5;color:#17202a;margin:0}
h1{font-size:24px;margin:0 0 3px;letter-spacing:.2px}
.invoice-title{font-size:30px;letter-spacing:1.5px;margin:0 0 7px}
.muted{color:#4b5563}
.header{width:100%;border-bottom:2px solid #cfd5dc;padding-bottom:18px;margin-bottom:22px}
.header-table{width:100%;border-collapse:collapse}
.header-table td{border:0;padding:0;vertical-align:top}
.brand{width:60%;padding-right:24px!important}
.meta{width:40%;text-align:right}
.logo{max-width:150px;max-height:72px;margin-bottom:9px}
.brand-name{font-size:18px;font-weight:700;letter-spacing:.2px;margin-bottom:3px;color:#111827}
.tagline{font-size:10.5px;color:#4b5563;margin-bottom:5px}
.contact{font-size:10.5px;color:#374151;line-height:1.65}.contact-label{font-weight:700;color:#111827}
.website{font-size:9.5px;color:#374151;margin-top:3px}
.badge{display:inline-block;padding:5px 10px;border-radius:10px;background:#eaf7f2;color:#176b5b;font-size:9px;font-weight:bold;text-transform:uppercase;margin-bottom:5px}
.grid{width:100%;border-collapse:separate;border-spacing:12px 0;margin:0 -12px 20px}
.grid td{width:50%;border:1px solid #d7dce2;padding:12px;vertical-align:top}
.box strong{display:block;margin-bottom:6px;font-size:9px;text-transform:uppercase;letter-spacing:.8px;color:#4b5563}
table.items{width:100%;border-collapse:collapse;margin-top:8px}
table.items th,table.items td{padding:10px 8px;border-bottom:1px solid #e5e7eb;text-align:left}
table.items th{background:#f1f3f5;font-size:9px;text-transform:uppercase;letter-spacing:.5px;color:#374151;font-weight:700}
table.items td.num,table.items th.num{text-align:right}
.totals{width:46%;margin-left:auto;margin-top:16px;border-collapse:collapse}
.totals td{border:0;padding:5px 4px;font-size:11px;color:#374151}
.total td{font-size:15px;font-weight:bold;border-top:1.5px solid #20252b;padding-top:8px}
.balance td{font-size:13px;font-weight:bold;padding-top:6px}
.notes{margin-top:22px;padding:12px;background:#f7f8fa;border:1px solid #edf0f2}
.footer{margin-top:28px;border-top:1px solid #ddd;padding-top:9px;font-size:9px;color:#4b5563;text-align:center}
</style></head><body>
<div class="header"><table class="header-table"><tr>
<td class="brand">
<?php if(!empty($b['logo'])): ?><img class="logo" src="<?= $e($b['logo']) ?>" alt="Logo"><br><?php endif; ?>
<div class="brand-name"><?= $e($b['name']) ?></div>
<div class="tagline"><?= $e($b['tagline']) ?></div>
<div class="contact">
<?php if(!empty($b['address'])): ?><div><?= nl2br($e($b['address'])) ?></div><?php endif; ?>
<?php if(!empty($b['country'])): ?><div><?= $e($b['country']) ?></div><?php endif; ?>
<?php if(!empty($b['phone'])): ?><div><span class="contact-label">Phone:</span> <?= $e($b['phone']) ?></div><?php endif; ?>
<?php if(!empty($b['email'])): ?><div><span class="contact-label">Email:</span> <?= $e($b['email']) ?></div><?php endif; ?>
<?php if(!empty($b['website'])): ?><div class="website"><span class="contact-label">Web:</span> <?= $e($b['website']) ?></div><?php endif; ?>
</div>
</td>
<td class="meta"><div class="invoice-title">INVOICE</div><strong><?= $e($invoice['invoice_number']) ?></strong><br><span class="badge"><?= $e(ucfirst(str_replace('_',' ',$invoice['status']))) ?></span><div class="muted">Date: <?= $e($invoice['invoice_date']) ?></div><div class="muted">Due: <?= $e($invoice['due_date']??'—') ?></div></td>
</tr></table></div>
<table class="grid"><tr><td class="box"><strong>Bill To</strong><?= $e($invoice['first_name'].' '.$invoice['last_name']) ?><br><?= $e($invoice['patient_reference']) ?></td><td class="box"><strong>Invoice Details</strong>Invoice: <?= $e($invoice['invoice_number']) ?><br>Status: <?= $e(ucfirst(str_replace('_',' ',$invoice['status']))) ?><br>Created by: <?= $e($invoice['created_by_name']??'') ?></td></tr></table>
<table class="items"><thead><tr><th>Date</th><th>Description</th><th class="num">Amount</th></tr></thead><tbody><?php foreach($invoice['items'] as $item): ?><tr><td><?= !empty($item['service_date']) ? $e(date('M d, Y',strtotime($item['service_date']))) : "—" ?></td><td><?= $e($item['description']??$item['service_name']??'') ?></td><td class="num"><?= $e($b['currency']) ?> <?= $money($item['line_total']) ?></td></tr><?php endforeach; ?></tbody></table>
<table class="totals"><tr><td>Subtotal</td><td class="num"><?= $e($b['currency']) ?> <?= $money($invoice['subtotal']) ?></td></tr><tr><td>Discount</td><td class="num">- <?= $e($b['currency']) ?> <?= $money($invoice['discount']) ?></td></tr><tr><td>Tax</td><td class="num"><?= $e($b['currency']) ?> <?= $money($invoice['tax']) ?></td></tr><tr class="total"><td>Total</td><td class="num"><?= $e($b['currency']) ?> <?= $money($invoice['total']) ?></td></tr><tr><td>Paid</td><td class="num"><?= $e($b['currency']) ?> <?= $money($invoice['paid_amount']) ?></td></tr><tr class="balance"><td>Balance due</td><td class="num"><?= $e($b['currency']) ?> <?= $money($invoice['balance']) ?></td></tr></table>
<?php if(!empty($invoice['notes'])): ?><div class="notes"><strong>Notes</strong><br><?= nl2br($e($invoice['notes'])) ?></div><?php endif; ?>
<div class="footer">Thank you for choosing <?= $e($b['name']) ?><?= $b['website']?'  •  '.$e($b['website']):'' ?><?= $b['email']?'  •  '.$e($b['email']):'' ?></div>
</body></html>
