<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
/* ── Print-ready page setup ── */
@page {
    size: {{ style.page_format | default('A4') }};
    margin: {{ style.margin_top | default('18mm') }} {{ style.margin_right | default('18mm') }} {{ style.margin_bottom | default('18mm') }} {{ style.margin_left | default('18mm') }};
    @bottom-center {
        content: "Page " counter(page) " of " counter(pages);
        font-size: 9px;
        color: #9ca3af;
        font-family: Inter, sans-serif;
    }
}

:root {
    --primary: {{ style.primary_color | default('#2563eb') }};
    --font: {{ style.font_family | default('Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif') }};
}

*, *::before, *::after {
    -webkit-print-color-adjust: exact !important;
    -print-color-adjust: exact !important;
    color-adjust: exact !important;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: var(--font);
    font-size: 13px;
    color: #1a1a1a;
    line-height: 1.6;
    padding: 0;
    background: #fff;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* ── Page break prevention ── */
.totals, .payment-link, .footer {
    page-break-inside: avoid;
}
.line-items tbody tr {
    page-break-inside: avoid;
}

.invoice {
    max-width: 680px;
    margin: 0 auto;
    padding: 48px 48px 40px;
}

/* ── Top Accent Stripe ── */
.accent-stripe {
    height: 4px;
    background: linear-gradient(90deg, var(--primary), {{ style.primary_color | default('#2563eb') }}99);
    margin: -48px -48px 40px;
}

/* ── Header ── */
.header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 48px;
}

.logo-section {
    flex: 1;
}

.logo {
    max-width: 160px;
    max-height: 48px;
    margin-bottom: 12px;
    display: block;
}

.company-name {
    font-size: 22px;
    font-weight: 700;
    color: #111;
    letter-spacing: -0.3px;
    margin-bottom: 6px;
}

.company-detail {
    font-size: 12px;
    color: #6b7280;
    line-height: 1.7;
}

.document-title {
    text-align: right;
}

.document-type {
    font-size: 32px;
    font-weight: 800;
    color: var(--primary);
    letter-spacing: 3px;
    margin-bottom: 8px;
    text-transform: uppercase;
    line-height: 1;
}

.document-number {
    font-size: 13px;
    color: #6b7280;
    margin-bottom: 2px;
}

.document-date {
    font-size: 13px;
    color: #6b7280;
}

{% if data.document_status %}
.status-badge {
    display: inline-block;
    padding: 2px 10px;
    border-radius: 100px;
    font-size: 10px;
    font-weight: 700;
    letter-spacing: 0.8px;
    text-transform: uppercase;
    margin-top: 8px;
}

.status-draft { background: #f3f4f6; color: #6b7280; }
.status-sent { background: #dbeafe; color: #2563eb; }
.status-viewed { background: #fef3c7; color: #d97706; }
.status-paid { background: #dcfce7; color: #16a34a; }
.status-void { background: #fee2e2; color: #dc2626; }
{% endif %}

/* ── Section Divider ── */
.divider {
    height: 1px;
    background: #e5e7eb;
    margin: 32px 0;
}

/* ── Bill To / Due ── */
.meta {
    display: flex;
    justify-content: space-between;
    gap: 40px;
    margin-bottom: 8px;
}

.meta-group {
    flex: 1;
}

.meta-label {
    font-size: 10px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 1.2px;
    color: #9ca3af;
    margin-bottom: 8px;
}

.meta-name {
    font-size: 15px;
    font-weight: 600;
    color: #111;
    margin-bottom: 4px;
}

.meta-detail {
    font-size: 12px;
    color: #6b7280;
    line-height: 1.7;
}

/* ── Line Items Table ── */
.line-items {
    width: 100%;
    border-collapse: collapse;
    margin-bottom: 32px;
}

.line-items thead th {
    font-size: 10px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: #9ca3af;
    padding: 0 12px 12px;
    text-align: left;
    border-bottom: 2px solid #e5e7eb;
}

.line-items thead th:last-child,
.line-items thead th:nth-child(3),
.line-items thead th:nth-child(4) {
    text-align: right;
}

.line-items tbody tr:nth-child(odd) {
    background: #fafbfc;
}

.line-items tbody td {
    padding: 14px 12px;
    font-size: 13px;
    border-bottom: 1px solid #f3f4f6;
    vertical-align: top;
}

.line-items tbody td:last-child,
.line-items tbody td:nth-child(3),
.line-items tbody td:nth-child(4) {
    text-align: right;
}

.line-items tbody td:first-child {
    font-weight: 500;
    color: #111;
}

.line-items tbody td:not(:first-child) {
    color: #4b5563;
}

.line-items tbody tr:last-child td {
    border-bottom: none;
}

/* ── Totals ── */
.totals {
    display: flex;
    justify-content: flex-end;
    margin-bottom: 8px;
}

.totals-card {
    width: 260px;
    background: #fafbfc;
    border-radius: 8px;
    padding: 16px 20px;
    border-left: 3px solid var(--primary);
}

.totals-row {
    display: flex;
    justify-content: space-between;
    padding: 6px 0;
    font-size: 13px;
    color: #4b5563;
}

.totals-row.subtotal {
    border-bottom: 1px solid #e5e7eb;
    padding-bottom: 8px;
    margin-bottom: 4px;
}

.totals-row.grand-total {
    font-size: 17px;
    font-weight: 700;
    color: var(--primary);
    padding-top: 10px;
    margin-top: 2px;
}

.totals-row .label {
    font-weight: 500;
}

.totals-row.grand-total .label {
    font-weight: 700;
}

/* ── Payment Button ── */
.payment-link {
    text-align: center;
    margin: 32px 0;
}

.pay-btn {
    display: inline-block;
    padding: 12px 40px;
    background: var(--primary);
    color: white;
    text-decoration: none;
    font-size: 13px;
    font-weight: 600;
    border-radius: 6px;
    letter-spacing: 0.5px;
}

/* ── Footer ── */
.footer {
    margin-top: 40px;
    padding-top: 24px;
    border-top: 1px solid #e5e7eb;
}

.footer-grid {
    display: flex;
    gap: 40px;
}

.footer-col {
    flex: 1;
}

.footer-col h4 {
    font-size: 10px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: #9ca3af;
    margin-bottom: 6px;
}

.footer-col p {
    font-size: 11px;
    color: #6b7280;
    line-height: 1.6;
}

.footer-note {
    text-align: center;
    margin-top: 24px;
    font-size: 11px;
    color: #9ca3af;
    font-style: italic;
}

/* ── Watermark ── */
.watermark {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%) rotate(-45deg);
    font-size: 80px;
    font-weight: 900;
    color: var(--primary);
    opacity: 0.08;
    pointer-events: none;
    text-transform: uppercase;
    letter-spacing: 8px;
}

{% if style.show_watermark | default(false) | to_bool %}
<div class="watermark">{{ style.watermark_text | default('PAID') }}</div>
{% endif %}
</style>
</head>
<body>
<div class="invoice">

    <div class="accent-stripe"></div>

    <!-- Header -->
    <div class="header">
        <div class="logo-section">
            {% if style.logo_url %}
            <img src="{{ style.logo_url }}" alt="Logo" class="logo">
            {% endif %}
            {% if data.from.name %}
            <div class="company-name">{{ data.from.name }}</div>
            {% endif %}
            <div class="company-detail">
                {% if data.from.address %}{{ data.from.address | nl2br }}{% endif %}
                {% if data.from.email %}{% if data.from.address %}<br>{% endif %}{{ data.from.email }}{% endif %}
                {% if data.from.phone %}<br>{{ data.from.phone }}{% endif %}
            </div>
        </div>
        <div class="document-title">
            <div class="document-type">{{ title_map.get(document_type, 'INVOICE') }}</div>
            {% if data.invoice_number %}<div class="document-number">#{{ data.invoice_number }}</div>{% endif %}
            {% if data.document_number and not data.invoice_number %}<div class="document-number">#{{ data.document_number }}</div>{% endif %}
            {% if data.issue_date %}<div class="document-date">Date: {{ data.issue_date | date_format }}</div>{% endif %}
            {% if data.document_status %}
            <span class="status-badge status-{{ data.document_status | lower }}">{{ data.document_status }}</span>
            {% endif %}
        </div>
    </div>

    <div class="divider"></div>

    <!-- Bill To / Due Date -->
    <div class="meta">
        <div class="meta-group">
            <div class="meta-label">Bill To</div>
            {% if data.to.name %}<div class="meta-name">{{ data.to.name }}</div>{% endif %}
            {% if data.to.address %}<div class="meta-detail">{{ data.to.address | nl2br }}</div>{% endif %}
            {% if data.to.email %}<div class="meta-detail">{{ data.to.email }}</div>{% endif %}
            {% if data.to.phone %}<div class="meta-detail">{{ data.to.phone }}</div>{% endif %}
        </div>
        <div class="meta-group">
            {% if data.due_date %}
            <div class="meta-label">Due Date</div>
            <div class="meta-name">{{ data.due_date | date_format }}</div>
            {% endif %}
            {% if data.payment_terms %}
            <div class="meta-label" style="margin-top:12px;">Terms</div>
            <div class="meta-detail">{{ data.payment_terms }}</div>
            {% endif %}
        </div>
    </div>

    <div class="divider"></div>

    <!-- Line Items -->
    <table class="line-items">
        <thead>
            <tr>
                <th>Description</th>
                <th>Qty</th>
                <th>Unit Price</th>
                <th>Amount</th>
            </tr>
        </thead>
        <tbody>
            {% for item in data.line_items %}
            <tr>
                <td>{{ item.description }}</td>
                <td>{{ item.quantity }}</td>
                <td>{{ item.unit_price | currency }}</td>
                <td>{{ (item.amount or item.unit_price * item.qty) | currency }}</td>
            </tr>
            {% endfor %}
        </tbody>
    </table>

    <!-- Totals -->
    <div class="totals">
        <div class="totals-card">
            <div class="totals-row subtotal">
                <span class="label">Subtotal</span>
                <span>{{ data.subtotal | currency }}</span>
            </div>
            {% if data.tax_rate > 0 %}
            <div class="totals-row subtotal">
                <span class="label">Tax ({{ data.tax_rate | percent }})</span>
                <span>{{ (data.tax or data.tax_amount or 0) | currency }}</span>
            </div>
            {% endif %}
            <div class="totals-row grand-total">
                <span class="label">Total</span>
                <span>{{ data.total | currency }}</span>
            </div>
        </div>
    </div>

    <!-- Payment Button -->
    {% if data.payment_link %}
    <div class="payment-link">
        <a href="{{ data.payment_link }}" class="pay-btn">{{ data.payment_link_text | default('Pay Now') }}</a>
    </div>
    {% endif %}

    <!-- Footer -->
    <div class="footer">
        <div class="footer-grid">
            {% if data.notes %}
            <div class="footer-col">
                <h4>Notes</h4>
                <p>{{ data.notes | nl2br }}</p>
            </div>
            {% endif %}
            {% if data.payment_terms %}
            <div class="footer-col">
                <h4>Payment Terms</h4>
                <p>{{ data.payment_terms }}</p>
            </div>
            {% endif %}
            {% if data.from.name %}
            <div class="footer-col">
                <h4>Issued By</h4>
                <p>
                    {{ data.from.name }}
                    {% if data.from.email %}<br>{{ data.from.email }}{% endif %}
                </p>
            </div>
            {% endif %}
        </div>
        <div class="footer-note">Thank you for your business.</div>
    </div>

</div>
</body>
</html>
