<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>{{ $doc['offer_number'] ?? '' }}</title>
    <style>
        * { box-sizing: border-box; }
        html, body { min-height: 100%; }
        body {
            font-family: DejaVu Sans, sans-serif;
            font-size: 12px;
            color: #111827;
            margin: 0;
            padding: 24px 36px;
            line-height: 1.45;
            @if(!empty($doc['document_background_url']))
            background-image: url('{{ $doc['document_background_url'] }}');
            background-size: cover;
            background-position: center;
            background-repeat: no-repeat;
            @endif
        }
        .doc-sheet {
            background: rgba(255,255,255,0.94);
            border-radius: 6px;
            padding: 20px 8px;
            margin: 0 auto;
            max-width: 100%;
        }
        .header { margin-bottom: 24px; border-bottom: 1px solid #e5e7eb; padding-bottom: 16px; }
        .logo { max-height: 48px; max-width: 200px; margin-bottom: 8px; display: block; }
        .grid { width: 100%; }
        .grid td { vertical-align: top; width: 50%; padding: 0 8px 0 0; }
        .muted { color: #6b7280; font-size: 11px; }
        h1 { font-size: 20px; margin: 0 0 8px 0; }
        h2 { font-size: 14px; margin: 20px 0 8px 0; }
        table.lines { width: 100%; border-collapse: collapse; margin-top: 8px; }
        table.lines th, table.lines td { border-bottom: 1px solid #e5e7eb; padding: 6px 4px; text-align: left; }
        table.lines th { font-size: 10px; text-transform: uppercase; letter-spacing: .04em; color: #374151; }
        .numeric { text-align: right !important; }
        .totals { width: 320px; margin-left: auto; margin-top: 16px; }
        .totals td { padding: 4px 0; }
        .grand { font-weight: bold; border-top: 1px solid #111827; }
        .sign { margin-top: 32px; padding-top: 16px; border-top: 1px dashed #9ca3af; }
        .sign img { max-height: 80px; }
    </style>
</head>
<body>
<div class="doc-sheet">
<div class="header">
    @if(!empty($doc['logo_url']))
        <img class="logo" src="{{ $doc['logo_url'] }}" alt="">
    @endif
    <table class="grid">
        <tr>
            <td>
                <div class="muted">Sender</div>
                <strong>{{ data_get($doc, 'sender.legal_name', '') }}</strong><br>
                {{ data_get($doc, 'sender.address_line_1', '') }}<br>
                {{ data_get($doc, 'sender.address_line_2', '') }}<br>
                @if(!empty(data_get($doc, 'sender.vat_number')))
                    VAT {{ data_get($doc, 'sender.vat_number') }}<br>
                @endif
                @if(!empty(data_get($doc, 'sender.iban')))
                    IBAN {{ data_get($doc, 'sender.iban') }}<br>
                @endif
            </td>
            <td>
                <div class="muted">Customer</div>
                @if(!empty($doc['customer']['name']))
                    <strong>{{ $doc['customer']['name'] }}</strong><br>
                @endif
                @if(!empty($doc['contact']['name']))
                    Attn. {{ $doc['contact']['name'] }}<br>
                @endif
                @if(!empty($doc['contact']['email']))
                    {{ $doc['contact']['email'] }}<br>
                @endif
            </td>
        </tr>
    </table>
</div>

<h1>{{ $doc['subject'] ?? '' }}</h1>
<div class="muted">Offer {{ $doc['offer_number'] ?? '' }} &middot; Valid until {{ $doc['valid_until'] ?? '—' }}</div>

@if(!empty($doc['intro_html']))
    <div style="margin-top:16px;">{!! $doc['intro_html'] !!}</div>
@endif

@foreach(($doc['sections'] ?? []) as $section)
    <h2>{{ $section['title'] }}</h2>
    @if(!empty($section['description_html']))
        <div class="muted">{!! $section['description_html'] !!}</div>
    @endif

    @if(!empty($section['is_pricing_block']) && !empty($section['categories']))
        @foreach($section['categories'] as $category)
            <h3 style="font-size:13px;margin:14px 0 6px 0;color:#374151;">{{ $category['title'] }}</h3>
            @if(!empty($category['description_html']))
                <div class="muted">{!! $category['description_html'] !!}</div>
            @endif
            <table class="lines">
                <thead>
                <tr>
                    <th>Description</th>
                    <th class="numeric">Qty</th>
                    <th class="numeric">Unit</th>
                    <th class="numeric">VAT %</th>
                    <th class="numeric">Net</th>
                </tr>
                </thead>
                <tbody>
                @foreach(($category['rows'] ?? []) as $row)
                    <tr>
                        <td>
                            {{ strip_tags((string) ($row['description_html'] ?? '')) }}
                            <div class="muted">
                                {{ $row['pricing_label'] }}
                                @if(!empty($row['custom_unit_label']))
                                    ({{ $row['custom_unit_label'] }})
                                @endif
                            </div>
                        </td>
                        <td class="numeric">{{ number_format($row['quantity'], 2, '.', '') }}</td>
                        <td class="numeric">{{ number_format($row['unit_price'], 2, '.', '') }}</td>
                        <td class="numeric">{{ number_format($row['vat_rate'], 2, '.', '') }}</td>
                        <td class="numeric">{{ number_format($row['line_net_total'], 2, '.', '') }}</td>
                    </tr>
                @endforeach
                </tbody>
            </table>
        @endforeach
    @else
    <table class="lines">
        <thead>
        <tr>
            <th>Description</th>
            <th class="numeric">Qty</th>
            <th class="numeric">Unit</th>
            <th class="numeric">VAT %</th>
            <th class="numeric">Net</th>
        </tr>
        </thead>
        <tbody>
        @foreach(($section['rows'] ?? []) as $row)
            <tr>
                <td>
                    {{ strip_tags((string) ($row['description_html'] ?? '')) }}
                    <div class="muted">
                        {{ $row['pricing_label'] }}
                        @if(!empty($row['custom_unit_label']))
                            ({{ $row['custom_unit_label'] }})
                        @endif
                    </div>
                </td>
                <td class="numeric">{{ number_format($row['quantity'], 2, '.', '') }}</td>
                <td class="numeric">{{ number_format($row['unit_price'], 2, '.', '') }}</td>
                <td class="numeric">{{ number_format($row['vat_rate'], 2, '.', '') }}</td>
                <td class="numeric">{{ number_format($row['line_net_total'], 2, '.', '') }}</td>
            </tr>
        @endforeach
        </tbody>
    </table>
    @endif
@endforeach

@php($t = $doc['totals'] ?? [])
<table class="totals">
    <tr>
        <td>Subtotal (excl. VAT)</td>
        <td class="numeric">{{ number_format($t['lines_net_subtotal'] ?? 0, 2, '.', '') }}</td>
    </tr>
    <tr>
        <td>Offer discount</td>
        <td class="numeric">-{{ number_format($t['offer_discount_amount'] ?? 0, 2, '.', '') }}</td>
    </tr>
    <tr>
        <td>Total excl. VAT</td>
        <td class="numeric">{{ number_format($t['total_excl_vat'] ?? 0, 2, '.', '') }}</td>
    </tr>
    <tr>
        <td>VAT total</td>
        <td class="numeric">{{ number_format($t['vat_total'] ?? 0, 2, '.', '') }}</td>
    </tr>
    <tr class="grand">
        <td>Grand total</td>
        <td class="numeric">{{ number_format($t['grand_total'] ?? 0, 2, '.', '') }}</td>
    </tr>
</table>

@if(!empty($doc['signer_name']) || !empty($doc['signature_url']))
    <div class="sign">
        <div class="muted">Accepted by</div>
        <strong>{{ $doc['signer_name'] ?? '' }}</strong><br>
        @if(!empty($doc['signature_url']))
            <img src="{{ $doc['signature_url'] }}" alt="Signature">
        @endif
    </div>
@endif
</div>
</body>
</html>
