<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
/* ── Print-ready page setup ── */
@page {
    size: {{ style.page_format | default('Letter') }};
    margin: {{ style.margin_top | default('10mm') }} {{ style.margin_right | default('10mm') }} {{ style.margin_bottom | default('10mm') }} {{ style.margin_left | default('10mm') }};
    @bottom-center {
        content: "Page " counter(page) " of " counter(pages);
        font-size: 9px;
        color: #999;
        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;
}

body {
    font-family: var(--font);
    font-size: 12px;
    color: #1a1a1a;
    line-height: 1.4;
    padding: 15px;
    -webkit-font-smoothing: antialiased;
}

.totals { page-break-inside: avoid; }

/* ── Receipt body ── */
.receipt {
    max-width: 400px;
    margin: 0 auto;
    border: 2px dashed #ddd;
    border-radius: 4px;
    padding: 20px;
}

/* ── Header ── */
.header {
    text-align: center;
    margin-bottom: 15px;
    padding-bottom: 15px;
    border-bottom: 1px dashed #ddd;
}

.logo {
    max-width: 120px;
    max-height: 36px;
    margin-bottom: 6px;
    display: block;
    margin-left: auto;
    margin-right: auto;
}

.company-name {
    font-size: 16px;
    font-weight: 700;
    margin-bottom: 2px;
}

.company-detail {
    font-size: 11px;
    color: #666;
}

.doc-title {
    font-size: 14px;
    font-weight: 700;
    letter-spacing: 1px;
    margin-top: 8px;
    color: var(--primary);
}

/* ── Meta ── */
.meta {
    display: flex;
    justify-content: space-between;
    font-size: 11px;
    color: #666;
    margin-bottom: 15px;
    padding-bottom: 10px;
    border-bottom: 1px dashed #ddd;
}

/* ── Info ── */
.info {
    margin-bottom: 10px;
    font-size: 11px;
}

.info .label {
    font-weight: 600;
    color: #999;
    font-size: 9px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* ── Table ── */
.items {
    width: 100%;
    border-collapse: collapse;
    margin-bottom: 10px;
}

.items th {
    font-size: 9px;
    font-weight: 600;
    text-transform: uppercase;
    color: #999;
    text-align: left;
    padding: 6px 0;
    border-bottom: 1px solid #eee;
}

.items th.right {
    text-align: right;
}

.items td {
    padding: 6px 0;
    border-bottom: 1px solid #f5f5f5;
}

.items td.right {
    text-align: right;
}

/* ── Totals ── */
.totals {
    margin-top: 10px;
    padding-top: 10px;
    border-top: 2px solid #1a1a1a;
}

.totals-row {
    display: flex;
    justify-content: space-between;
    padding: 2px 0;
    font-size: 12px;
}

.totals-row.grand-total {
    font-size: 16px;
    font-weight: 700;
    margin-top: 4px;
    padding-top: 6px;
    border-top: 2px solid #1a1a1a;
}

/* ── Footer ── */
.footer {
    margin-top: 15px;
    padding-top: 15px;
    border-top: 1px dashed #ddd;
    text-align: center;
    font-size: 11px;
    color: #999;
}

.notes {
    font-size: 11px;
    color: #666;
    margin-bottom: 10px;
    padding: 8px;
    background: #f8f9fa;
    border-radius: 4px;
}

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

{% if style.show_watermark | default(false) | to_bool %}
<div class="watermark">{{ style.watermark_text | default('PAID') }}</div>
{% endif %}
</style>
</head>
<body>
<div class="receipt">
    <div class="header">
        {% if style.logo_url %}
        <img src="{{ style.logo_url }}" alt="Logo" class="logo">
        {% endif %}
        <div class="company-name">{{ data.from.name }}</div>
        {% if data.from.address %}<div class="company-detail">{{ data.from.address | nl2br }}</div>{% endif %}
        {% if data.from.email %}<div class="company-detail">{{ data.from.email }}</div>{% endif %}
        {% if data.from.phone %}<div class="company-detail">{{ data.from.phone }}</div>{% endif %}
        <div class="doc-title">{{ title_map.get(document_type, 'RECEIPT') }}</div>
    </div>

    <div class="meta">
        <div>
            {% if data.document_number %}<div>#{{ data.document_number }}</div>{% endif %}
        </div>
        <div>
            {% if data.issue_date %}<div>{{ data.issue_date | date_format }}</div>{% endif %}
        </div>
    </div>

    <div class="info">
        <div class="label">Customer</div>
        <div>{{ data.to.name }}</div>
        {% if data.to.address %}<div>{{ data.to.address | nl2br }}</div>{% endif %}
        {% if data.to.email %}<div>{{ data.to.email }}</div>{% endif %}
        {% if data.to.phone %}<div>{{ data.to.phone }}</div>{% endif %}
    </div>

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

    <div class="totals">
        <div class="totals-row">
            <span>Subtotal</span>
            <span>{{ data.subtotal | currency }}</span>
        </div>
        {% if data.tax_rate > 0 %}
        <div class="totals-row">
            <span>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>TOTAL</span>
            <span>{{ data.total | currency }}</span>
        </div>
    </div>

    {% if data.notes %}
    <div class="notes">{{ data.notes | nl2br }}</div>
    {% endif %}

    <div class="footer">
        Thank you for your business!
    </div>
</div>
</body>
</html>
