@php
    $addressParts = array_filter([
        $company->address ?? null,
        $company->city ?? null,
        $company->country ?? null,
    ]);
    $companyAddress = implode(', ', $addressParts);
    $fmt = fn ($n) => number_format((float) $n, 2);
    $salutation = fn ($emp) => trim(($emp->salutation ? $emp->salutation . ' ' : '') . ($emp->display_name ?: $emp->name));
    $amountLabel = $sheet['amount_label'] ?? 'Amount';
    $filterParts = ['Department: ' . ($sheet['filters']['department'] ?: 'All')];
    if ($sheet['show_religion_filter'] ?? true) {
        $filterParts[] = 'Religion: ' . ($sheet['filters']['religion'] ?: 'All');
    }
    $filterParts[] = 'Status: ' . ($sheet['status_label'] ?? 'draft');
@endphp
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>{{ $sheet['type_label'] }} — {{ $sheet['period_label'] }}{{ $sheet['is_draft'] ? ' (Draft)' : '' }}</title>
    <style>
        * { box-sizing: border-box; }
        body {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 10px;
            color: #111;
            margin: 0;
            padding: 10px 12px;
            line-height: 1.25;
        }
        .print-toolbar {
            display: flex;
            gap: 0.5rem;
            margin-bottom: 0.75rem;
            padding-bottom: 0.5rem;
            border-bottom: 1px solid #d1d5db;
        }
        .print-toolbar button {
            padding: 0.35rem 0.75rem;
            font-size: 12px;
            border: 1px solid #d1d5db;
            border-radius: 4px;
            background: #fff;
            cursor: pointer;
        }
        .sheet-wrap { position: relative; }
        .draft-watermark {
            position: fixed;
            inset: 0;
            display: flex;
            align-items: center;
            justify-content: center;
            pointer-events: none;
            z-index: 0;
        }
        .draft-watermark span {
            font-size: 120px;
            font-weight: 700;
            color: rgba(220, 38, 38, 0.18);
            transform: rotate(-35deg);
            letter-spacing: 0.08em;
            user-select: none;
        }
        .sheet-content { position: relative; z-index: 1; }
        .filter-line {
            font-size: 10px;
            margin-bottom: 6px;
            font-weight: 600;
        }
        .report-header {
            text-align: center;
            margin: 0 0 10px;
            padding-bottom: 8px;
            border-bottom: 1px solid #9ca3af;
        }
        .report-company {
            font-size: 15px;
            font-weight: 700;
            margin-bottom: 2px;
        }
        .report-address {
            font-size: 10px;
            color: #374151;
            margin-bottom: 6px;
        }
        .report-title {
            font-size: 13px;
            font-weight: 700;
            margin-bottom: 2px;
        }
        .report-period {
            font-size: 10px;
            color: #374151;
        }
        .voucher-meta {
            text-align: right;
            font-size: 9px;
            color: #4b5563;
            margin: -4px 0 8px;
        }
        table.sheet-table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 10px;
        }
        .sheet-table th,
        .sheet-table td {
            border: 1px solid #6b7280;
            padding: 3px 5px;
            vertical-align: middle;
        }
        .sheet-table th {
            background: #e5e7eb;
            font-weight: 700;
            text-align: center;
            font-size: 9px;
        }
        .sheet-table td {
            font-size: 9px;
        }
        .sheet-table td.num {
            text-align: right;
            font-variant-numeric: tabular-nums;
            white-space: nowrap;
        }
        .sheet-table td.text-left { text-align: left; }
        .dept-label td {
            background: #f3f4f6;
            font-weight: 700;
            font-size: 10px;
            border-top: 2px solid #374151;
        }
        .subtotal-row td {
            font-weight: 700;
            background: #f9fafb;
        }
        .subtotal-row td.label {
            text-align: left;
            font-style: italic;
        }
        .section-title {
            font-size: 11px;
            font-weight: 700;
            margin: 14px 0 6px;
        }
        .summary-table {
            width: auto;
            min-width: 360px;
            max-width: 520px;
        }
        .payment-table {
            width: auto;
            min-width: 320px;
            max-width: 460px;
        }
        .signatures {
            display: flex;
            justify-content: space-between;
            gap: 24px;
            margin-top: 36px;
            padding-top: 8px;
        }
        .signature-block {
            flex: 1;
            text-align: center;
            max-width: 200px;
        }
        .signature-line {
            border-top: 1px solid #111;
            margin-bottom: 4px;
            height: 28px;
        }
        .signature-label {
            font-size: 10px;
            font-weight: 600;
        }
        .empty-msg {
            padding: 24px;
            text-align: center;
            border: 1px solid #d1d5db;
            color: #6b7280;
        }
        @page {
            size: A4 landscape;
            margin: 8mm;
        }
        @media print {
            .print-toolbar { display: none !important; }
            body { padding: 0; }
            .dept-label { break-after: avoid-page; }
            .subtotal-row { break-inside: avoid; }
            .signatures { break-inside: avoid; }
        }
    </style>
</head>
<body>
    <div class="print-toolbar">
        <button type="button" onclick="window.print()">Print</button>
        <button type="button" onclick="window.close()">Close</button>
    </div>

    <div class="sheet-wrap">
        @if($sheet['is_draft'] ?? false)
        <div class="draft-watermark" aria-hidden="true"><span>DRAFT</span></div>
        @endif

        <div class="sheet-content">
            <div class="filter-line">{{ implode(' | ', $filterParts) }}</div>

            <div class="report-header">
                <div class="report-company">{{ $company->name ?? 'Company' }}</div>
                @if($companyAddress)
                <div class="report-address">{{ $companyAddress }}</div>
                @endif
                <div class="report-title">{{ $sheet['type_label'] }}</div>
                <div class="report-period">For the Month of {{ $sheet['period_label'] }}</div>
            </div>

            @if(!empty($sheet['voucher_no']) && $sheet['voucher_no'] !== 'DRAFT')
            <div class="voucher-meta">Voucher No: {{ $sheet['voucher_no'] }}</div>
            @endif

            @if(empty($sheet['department_groups']))
            <div class="empty-msg">{{ $sheet['empty_message'] ?? 'No employees match the selected filters.' }}</div>
            @else
            <table class="sheet-table">
                <thead>
                    <tr>
                        <th style="width:72px">Employee ID</th>
                        <th style="width:160px">Name</th>
                        <th style="width:90px">Designation</th>
                        <th style="width:90px">Paymt Method</th>
                        <th style="width:110px">{{ $amountLabel }}</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($sheet['department_groups'] as $group)
                    <tr class="dept-label">
                        <td colspan="5" class="text-left">Department: {{ $group['name'] }}</td>
                    </tr>
                    @foreach($group['rows'] as $row)
                    @php $emp = $row['employee']; @endphp
                    <tr>
                        <td class="text-left">{{ $emp->code }}</td>
                        <td class="text-left">{{ $salutation($emp) }}</td>
                        <td class="text-left">{{ $emp->designation ?: '—' }}</td>
                        <td class="text-left">{{ $emp->salary_payment_method ?: '—' }}</td>
                        <td class="num">{{ $fmt($row['amount']) }}</td>
                    </tr>
                    @endforeach
                    <tr class="subtotal-row">
                        <td colspan="4" class="label">Subtotal — {{ $group['name'] }} ({{ count($group['rows']) }})</td>
                        <td class="num">{{ $fmt($group['subtotal']['amount']) }}</td>
                    </tr>
                    @endforeach
                </tbody>
            </table>

            <div class="section-title">Department-wise Summary</div>
            <table class="sheet-table summary-table">
                <thead>
                    <tr>
                        <th>Department</th>
                        <th style="width:48px">Emp</th>
                        <th style="width:120px">{{ $amountLabel }}</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($sheet['department_summary'] as $summary)
                    <tr>
                        <td class="text-left">{{ $summary['department'] }}</td>
                        <td class="num">{{ $summary['employee_count'] }}</td>
                        <td class="num">{{ $fmt($summary['amount']) }}</td>
                    </tr>
                    @endforeach
                    <tr class="subtotal-row">
                        <td class="text-left">Grand Total</td>
                        <td class="num">{{ $sheet['employee_count'] }}</td>
                        <td class="num">{{ $fmt($sheet['grand_total']) }}</td>
                    </tr>
                </tbody>
            </table>

            <div class="section-title">{{ $sheet['payment_summary_title'] ?? 'Payment Method Summary' }}</div>
            <table class="sheet-table payment-table">
                <thead>
                    <tr>
                        <th>Payment Method</th>
                        <th style="width:70px">Employees</th>
                        <th style="width:110px">{{ $amountLabel }}</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($sheet['payment_summary'] as $payment)
                    <tr>
                        <td class="text-left">{{ $payment['method'] }}</td>
                        <td class="num">{{ $payment['employee_count'] }}</td>
                        <td class="num">{{ $fmt($payment['amount']) }}</td>
                    </tr>
                    @endforeach
                    <tr class="subtotal-row">
                        <td class="text-left">Total</td>
                        <td class="num">{{ $sheet['employee_count'] }}</td>
                        <td class="num">{{ $fmt($sheet['grand_total']) }}</td>
                    </tr>
                </tbody>
            </table>

            <div class="signatures">
                <div class="signature-block">
                    <div class="signature-line"></div>
                    <div class="signature-label">Prepared By</div>
                </div>
                <div class="signature-block">
                    <div class="signature-line"></div>
                    <div class="signature-label">Checked By</div>
                </div>
                <div class="signature-block">
                    <div class="signature-line"></div>
                    <div class="signature-label">Approved By</div>
                </div>
            </div>
            @endif
        </div>
    </div>
</body>
</html>
