<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
@page {
    size: A4;
    margin: 0;
    @bottom-center {
        content: "Page " counter(page) " of " counter(pages);
        font-family: 'Inter', sans-serif;
        font-size: 8px;
        color: #9CA3AF;
    }
}

:root {
    --primary: {{ style.primary_color | default('#2383E2') }};
    --primary-light: {{ style.primary_light | default('#E8F4FD') }};
    --text-primary: #37352F;
    --text-secondary: #787774;
    --text-tertiary: #ADB5BD;
    --border: #E5E7EB;
    --bg-subtle: #F7F7F5;
    --bg-white: #FFFFFF;
    --radius: 8px;
    --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-print-color-adjust: exact !important;
    -print-color-adjust: exact !important;
    color-adjust: exact !important;
}

body {
    font-family: var(--font);
    font-size: 11px;
    color: var(--text-primary);
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
    counter-reset: page;
}

/* ── Page break prevention ── */
.totals-section, .payment-info, .payment-link { page-break-inside: avoid; }

/* ── Layout ── */
.page-layout {
    display: flex;
    min-height: 100vh;
}

.sidebar {
    width: 180px;
    background: var(--primary);
    padding: 40px 24px;
    color: white;
    flex-shrink: 0;
}

.main-content {
    flex: 1;
    padding: 40px 32px;
}

/* ── Sidebar ── */
.sidebar-brand {
    margin-bottom: 40px;
}

.sidebar-logo {
    width: 32px;
    height: 32px;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 14px;
    margin-bottom: 16px;
}

.sidebar-company {
    font-size: 13px;
    font-weight: 600;
    letter-spacing: -0.01em;
}

.sidebar-details {
    font-size: 10px;
    opacity: 0.85;
    margin-top: 4px;
    line-height: 1.5;
}

.sidebar-section {
    margin-bottom: 24px;
}

.sidebar-label {
    font-size: 8px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 1px;
    opacity: 0.7;
    margin-bottom: 8px;
}

.sidebar-value {
    font-size: 11px;
    line-height: 1.5;
}

/* ── Document Header ── */
.document-header {
    margin-bottom: 32px;
}

.document-title {
    font-size: 28px;
    font-weight: 700;
    color: var(--text-primary);
    letter-spacing: -0.02em;
    margin-bottom: 4px;
}

.document-meta {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-top: 8px;
}

.document-number {
    font-size: 12px;
    font-weight: 500;
    color: var(--text-secondary);
}

.document-date {
    font-size: 12px;
    color: var(--text-secondary);
}

.status-badge {
    display: inline-block;
    padding: 3px 8px;
    border-radius: 4px;
    font-size: 9px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.status-badge.paid {
    background: #ECFDF5;
    color: #059669;
}

.status-badge.pending {
    background: #FEF3C7;
    color: #D97706;
}

.status-badge.overdue {
    background: #FEE2E2;
    color: #DC2626;
}

.status-badge.draft {
    background: var(--bg-subtle);
    color: var(--text-secondary);
}

/* ── Info Grid ── */
.info-grid {
    display: flex;
    gap: 24px;
    margin-bottom: 32px;
    padding: 20px;
    background: var(--bg-subtle);
    border-radius: var(--radius);
}

.info-block {
    flex: 1;
}

.info-label {
    font-size: 9px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--text-tertiary);
    margin-bottom: 8px;
}

.info-name {
    font-size: 14px;
    font-weight: 600;
    color: var(--text-primary);
    letter-spacing: -0.01em;
    margin-bottom: 4px;
}

.info-detail {
    font-size: 11px;
    color: var(--text-secondary);
    line-height: 1.5;
}

/* ── Line Items ── */
.line-items-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
}

.line-items-title {
    font-size: 10px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--text-tertiary);
}

.line-items-table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    margin-bottom: 24px;
    border-radius: var(--radius);
    overflow: hidden;
    border: 1px solid var(--border);
}

.line-items-table thead th {
    background: var(--bg-subtle);
    padding: 12px 16px;
    font-size: 9px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--text-tertiary);
    text-align: left;
    border-bottom: 1px solid var(--border);
}

.line-items-table thead th.right {
    text-align: right;
}

.line-items-table tbody td {
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
    font-size: 11px;
}

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

.line-items-table tbody tr:nth-child(odd) {
    background: var(--bg-subtle);
}

.line-items-table tbody td.right {
    text-align: right;
    font-variant-numeric: tabular-nums;
}

.line-items-table tbody td.description {
    font-weight: 500;
    color: var(--text-primary);
}

.line-items-table tbody td.quantity {
    color: var(--text-secondary);
    font-variant-numeric: tabular-nums;
}

.line-items-table tbody td.price {
    color: var(--text-secondary);
    font-variant-numeric: tabular-nums;
}

.line-items-table tbody td.amount {
    font-weight: 500;
    font-variant-numeric: tabular-nums;
}

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

.totals-container {
    width: 240px;
    background: var(--primary-light);
    border-radius: var(--radius);
    padding: 16px 20px;
}

.totals-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 6px 0;
    font-size: 11px;
}

.totals-row .label {
    color: var(--text-secondary);
}

.totals-row .value {
    font-weight: 500;
    font-variant-numeric: tabular-nums;
}

.totals-row.grand-total {
    padding-top: 12px;
    margin-top: 8px;
    border-top: 2px solid var(--primary);
}

.totals-row.grand-total .label {
    font-size: 13px;
    font-weight: 600;
    color: var(--text-primary);
}

.totals-row.grand-total .value {
    font-size: 20px;
    font-weight: 700;
    color: var(--primary);
    font-variant-numeric: tabular-nums;
    letter-spacing: -0.02em;
}

/* ── Payment Info ── */
.payment-info {
    display: flex;
    gap: 24px;
    margin-bottom: 24px;
}

.payment-block {
    flex: 1;
    background: var(--bg-subtle);
    border-radius: var(--radius);
    padding: 16px 20px;
}

.payment-label {
    font-size: 9px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--text-tertiary);
    margin-bottom: 8px;
}

.payment-value {
    font-size: 11px;
    color: var(--text-secondary);
    line-height: 1.5;
}

/* ── Notes ── */
.notes-section {
    margin-bottom: 24px;
}

.notes-label {
    font-size: 9px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--text-tertiary);
    margin-bottom: 8px;
}

.notes-text {
    font-size: 11px;
    color: var(--text-secondary);
    line-height: 1.5;
}

/* ── Payment Link ── */
.payment-link {
    text-align: center;
    padding: 24px 0;
}

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

/* ── Footer ── */
.footer {
    text-align: center;
    padding: 32px 0;
    border-top: 1px solid var(--border);
}

.footer-text {
    font-size: 11px;
    color: var(--text-tertiary);
    margin-bottom: 4px;
}

.footer-company {
    font-size: 10px;
    color: var(--text-tertiary);
}

/* ── Watermark ── */
{% if style.show_watermark | default(false) | to_bool %}
.watermark {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%) rotate(-45deg);
    font-size: 120px;
    font-weight: 900;
    color: var(--primary);
    opacity: 0.06;
    pointer-events: none;
    text-transform: uppercase;
    letter-spacing: 20px;
    z-index: -1;
}
{% endif %}
</style>
</head>
<body>
{% if style.show_watermark | default(false) | to_bool %}
<div class="watermark">{{ style.watermark_text | default('PAID') }}</div>
{% endif %}

<div class="page-layout">
    <div class="sidebar">
        <div class="sidebar-brand">
            {% if style.logo_url %}
            <img src="{{ style.logo_url }}" alt="Logo" class="sidebar-logo" style="background: rgba(255,255,255,0.2); border-radius: 6px; object-fit: contain;">
            {% else %}
            <div class="sidebar-logo">{{ data.from.name | default('') | truncate(1, True, '') }}</div>
            {% endif %}
            <div class="sidebar-company">{{ data.from.name }}</div>
        </div>
        
        {% if data.from.address %}
        <div class="sidebar-section">
            <div class="sidebar-label">Address</div>
            <div class="sidebar-value">{{ data.from.address | nl2br }}</div>
        </div>
        {% endif %}
        
        {% if data.from.email or data.from.phone %}
        <div class="sidebar-section">
            <div class="sidebar-label">Contact</div>
            <div class="sidebar-value">
                {% if data.from.email %}{{ data.from.email }}<br>{% endif %}
                {% if data.from.phone %}{{ data.from.phone }}{% endif %}
            </div>
        </div>
        {% endif %}
    </div>
    
    <div class="main-content">
        <div class="document-header">
            <div class="document-title">{{ title_map.get(document_type, 'INVOICE') }}</div>
            <div class="document-meta">
                {% if data.document_number %}
                <div class="document-number">#{{ data.document_number }}</div>
                {% endif %}
                {% if data.issue_date %}
                <div class="document-date">{{ data.issue_date | date_format }}</div>
                {% endif %}
                {% if data.status %}
                <div class="status-badge {{ data.status | lower | replace(' ', '-') }}">
                    {{ data.status | upper }}
                </div>
                {% endif %}
            </div>
        </div>
        
        <div class="info-grid">
            <div class="info-block">
                <div class="info-label">Bill To</div>
                <div class="info-name">{{ data.to.name }}</div>
                {% if data.to.email %}
                <div class="info-detail">{{ data.to.email }}</div>
                {% endif %}
                {% if data.to.phone %}
                <div class="info-detail">{{ data.to.phone }}</div>
                {% endif %}
                {% if data.to.address %}
                <div class="info-detail">{{ data.to.address | nl2br }}</div>
                {% endif %}
            </div>
            <div class="info-block">
                <div class="info-label">Payment Details</div>
                {% if data.due_date %}
                <div class="info-name">Due {{ data.due_date | date_format }}</div>
                {% endif %}
                {% if data.payment_terms %}
                <div class="info-detail">{{ data.payment_terms }}</div>
                {% endif %}
                {% if data.currency %}
                <div class="info-detail">Currency: {{ data.currency | upper }}</div>
                {% endif %}
            </div>
        </div>
        
        <div class="line-items-header">
            <div class="line-items-title">Description</div>
            <div class="line-items-title">Amount</div>
        </div>
        
        <table class="line-items-table">
            <thead>
                <tr>
                    <th style="flex: 2;">Description</th>
                    <th class="right" style="width: 60px;">Qty</th>
                    <th class="right" style="width: 100px;">Unit</th>
                    <th class="right" style="width: 100px;">Amount</th>
                </tr>
            </thead>
            <tbody>
                {% for item in data.line_items %}
                <tr>
                    <td class="description">{{ item.description }}</td>
                    <td class="quantity">{{ item.quantity }}</td>
                    <td class="price">{{ item.unit_price | currency }}</td>
                    <td class="amount">{{ (item.amount or item.unit_price * item.qty) | currency }}</td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
        
        <div class="totals-section">
            <div class="totals-container">
                <div class="totals-row">
                    <span class="label">Subtotal</span>
                    <span class="value">{{ data.subtotal | currency }}</span>
                </div>
                {% if data.tax_rate > 0 %}
                <div class="totals-row">
                    <span class="label">Tax ({{ data.tax_rate | percent }})</span>
                    <span class="value">{{ (data.tax or data.tax_amount or 0) | currency }}</span>
                </div>
                {% endif %}
                {% if data.discount > 0 %}
                <div class="totals-row">
                    <span class="label">Discount</span>
                    <span class="value">-{{ data.discount | currency }}</span>
                </div>
                {% endif %}
                <div class="totals-row grand-total">
                    <span class="label">Total</span>
                    <span class="value">{{ data.total | currency }}</span>
                </div>
            </div>
        </div>
        
        {% if data.notes %}
        <div class="notes-section">
            <div class="notes-label">Notes</div>
            <div class="notes-text">{{ data.notes | nl2br }}</div>
        </div>
        {% endif %}
        
        {% 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 %}
        
        <div class="footer">
            <div class="footer-text">Thank you for your business.</div>
            <div class="footer-company">{{ data.from.name }}</div>
        </div>
    </div>
</div>
</body>
</html>