<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
/* ═══════════════════════════════════════════════
   ACCOUNT STATEMENT — Clean Financial 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: #94a3b8;
        font-family: {{ style.font_family | default('Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif') }};
    }
}

:root {
    --primary: {{ style.primary_color | default('#0f172a') }};
    --primary-dark: {{ style.primary_dark | default('#1e293b') }};
    --primary-light: {{ style.primary_light | default('#e2e8f0') }};
    --primary-faint: {{ style.primary_faint | default('#f8fafc') }};
    --accent: {{ style.accent_color | default('#2563eb') }};
    --font: {{ style.font_family | default('Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif') }};
    --text-heading: {{ style.text_primary | default('#0f172a') }};
    --text-body: {{ style.text_secondary | default('#475569') }};
    --text-muted: {{ style.text_tertiary | default('#94a3b8') }};
    --border: {{ style.border_color | default('#e2e8f0') }};
    --bg-subtle: {{ style.bg_subtle | default('#f8fafc') }};
    --bg-card: #ffffff;
    --danger: #dc2626;
    --danger-faint: #fef2f2;
    --success: #059669;
    --success-faint: #ecfdf5;
}

/* ── Print color preservation ── */
*, *::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: 11px;
    color: var(--text-body);
    line-height: 1.6;
    background: #eef2f7;
    padding: 20px;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* ═══════════════════════════════════════════════
   DOCUMENT SHELL
   ═══════════════════════════════════════════════ */
.document {
    max-width: 740px;
    margin: 0 auto;
    background: var(--bg-card);
    overflow: hidden;
    box-shadow: 0 0.5px 0 rgba(0,0,0,0.08);
}

/* ═══════════════════════════════════════════════
   HEADER
   ═══════════════════════════════════════════════ */
.header {
    padding: 36px 44px;
    border-bottom: 3px solid var(--primary);
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
}

.header-left {
    flex: 1;
}

.header-logo {
    max-width: 140px;
    max-height: 40px;
    margin-bottom: 12px;
    display: block;
}

.header-company {
    font-size: 17px;
    font-weight: 700;
    color: var(--text-heading);
    letter-spacing: 0.2px;
    margin-bottom: 4px;
}

.header-company-detail {
    font-size: 10.5px;
    color: var(--text-muted);
    line-height: 1.55;
}

.header-right {
    text-align: right;
    min-width: 200px;
}

.header-title-badge {
    display: inline-block;
    background: var(--primary);
    color: #ffffff;
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 2.5px;
    text-transform: uppercase;
    padding: 6px 18px;
    border-radius: 4px;
    margin-bottom: 10px;
}

.header-doc-number {
    font-size: 11px;
    color: var(--text-muted);
    font-weight: 400;
    margin-bottom: 4px;
}

.header-period {
    font-size: 11px;
    color: var(--text-heading);
    font-weight: 600;
}

.header-generated {
    font-size: 9.5px;
    color: var(--text-muted);
    margin-top: 4px;
}

/* ═══════════════════════════════════════════════
   CONTENT AREA
   ═══════════════════════════════════════════════ */
.content {
    padding: 32px 44px 36px;
}

/* ═══════════════════════════════════════════════
   SECTION HEADER
   ═══════════════════════════════════════════════ */
.section {
    margin-bottom: 28px;
}

.section:last-child {
    margin-bottom: 0;
}

.section-header {
    font-size: 10px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 1.2px;
    color: var(--text-muted);
    padding-bottom: 8px;
    margin-bottom: 14px;
    border-bottom: 1px solid var(--border);
}

/* ═══════════════════════════════════════════════
   CUSTOMER INFO CARD
   ═══════════════════════════════════════════════ */
.customer-card {
    display: flex;
    gap: 20px;
    background: var(--bg-subtle);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 18px 22px;
    align-items: flex-start;
}

.customer-info {
    flex: 1;
}

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

.customer-info .name {
    font-size: 14px;
    font-weight: 600;
    color: var(--text-heading);
    margin-bottom: 4px;
}

.customer-info .detail {
    font-size: 10.5px;
    color: var(--text-body);
    line-height: 1.55;
}

.customer-account {
    text-align: right;
    min-width: 140px;
    border-left: 1px solid var(--border);
    padding-left: 20px;
}

.customer-account .label {
    font-size: 9px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--text-muted);
    margin-bottom: 4px;
}

.customer-account .account-num {
    font-family: 'SF Mono', 'Cascadia Code', 'Consolas', monospace;
    font-size: 12px;
    font-weight: 600;
    color: var(--text-heading);
    letter-spacing: 1px;
}

.customer-account .stmt-date {
    font-size: 10px;
    color: var(--text-muted);
    margin-top: 4px;
}

/* ═══════════════════════════════════════════════
   ACCOUNT SUMMARY — 2x2 Grid
   ═══════════════════════════════════════════════ */
.account-summary {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
    page-break-inside: avoid;
}

.summary-card {
    border-radius: 8px;
    padding: 16px 20px;
    border: 1px solid var(--border);
    page-break-inside: avoid;
}

.summary-card .label {
    font-size: 9px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.8px;
    margin-bottom: 6px;
}

.summary-card .amount {
    font-size: 22px;
    font-weight: 700;
    letter-spacing: -0.5px;
    font-variant-numeric: tabular-nums;
    line-height: 1.2;
}

/* Opening balance — neutral */
.summary-card.opening {
    background: var(--bg-subtle);
}
.summary-card.opening .label { color: var(--text-muted); }
.summary-card.opening .amount { color: var(--text-heading); }

/* Total charges — red */
.summary-card.charges {
    background: var(--danger-faint);
    border-color: #fecaca;
}
.summary-card.charges .label { color: var(--danger); opacity: 0.75; }
.summary-card.charges .amount { color: var(--danger); }

/* Total payments — green */
.summary-card.payments {
    background: var(--success-faint);
    border-color: #a7f3d0;
}
.summary-card.payments .label { color: var(--success); opacity: 0.75; }
.summary-card.payments .amount { color: var(--success); }

/* Closing balance — primary */
.summary-card.closing {
    background: var(--primary);
    border-color: var(--primary);
}
.summary-card.closing .label { color: rgba(255,255,255,0.7); }
.summary-card.closing .amount { color: #ffffff; }

/* ═══════════════════════════════════════════════
   TRANSACTION TABLE
   ═══════════════════════════════════════════════ */
.txn-table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    border-radius: 8px;
    overflow: hidden;
    border: 1px solid var(--border);
}

.txn-table thead th {
    background: var(--primary);
    color: #ffffff;
    padding: 10px 14px;
    font-size: 9px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.8px;
    text-align: left;
    border: none;
}

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

.txn-table tbody td {
    padding: 10px 14px;
    border-bottom: 1px solid var(--border);
    font-size: 11px;
}

.txn-table tbody td.right {
    text-align: right;
    font-variant-numeric: tabular-nums;
    min-width: 80px;
}

.txn-table tbody tr:last-child td { border-bottom: none; }
.txn-table tbody tr:nth-child(even) { background: var(--bg-subtle); }

.txn-table .date-cell {
    color: var(--text-muted);
    font-size: 10px;
    white-space: nowrap;
}

.txn-table .desc-cell {
    color: var(--text-heading);
    font-weight: 400;
}

.txn-table .ref-cell {
    font-family: 'SF Mono', 'Cascadia Code', 'Consolas', monospace;
    font-size: 10px;
    color: var(--text-muted);
}

.txn-table .debit-cell { color: var(--danger); }
.txn-table .credit-cell { color: var(--success); }
.txn-table .balance-cell { color: var(--text-heading); font-weight: 600; }
.txn-table .cell-dash { color: var(--text-muted); }

/* ═══════════════════════════════════════════════
   SUMMARY TOTALS — Bottom Row
   ═══════════════════════════════════════════════ */
.summary-totals {
    margin-top: 16px;
    border-top: 2px solid var(--primary);
    padding-top: 12px;
    page-break-inside: avoid;
}

.summary-totals table {
    width: 100%;
    border-collapse: collapse;
}

.summary-totals td {
    padding: 4px 14px;
    font-size: 11px;
}

.summary-totals td.right {
    text-align: right;
    font-variant-numeric: tabular-nums;
}

.summary-totals .total-row td {
    font-weight: 600;
    color: var(--text-heading);
}

.summary-totals .grand-total-row td {
    font-size: 14px;
    font-weight: 700;
    color: var(--primary);
    border-top: 1px solid var(--border);
    padding-top: 8px;
    padding-bottom: 4px;
}

/* ═══════════════════════════════════════════════
   NOTES
   ═══════════════════════════════════════════════ */
.notes-block {
    margin-top: 24px;
    padding: 14px 18px;
    background: var(--bg-subtle);
    border-radius: 6px;
    border: 1px solid var(--border);
    font-size: 10.5px;
    color: var(--text-body);
    line-height: 1.65;
}

.notes-block .notes-label {
    font-size: 9px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.8px;
    color: var(--text-muted);
    margin-bottom: 6px;
}

/* ═══════════════════════════════════════════════
   FOOTER
   ═══════════════════════════════════════════════ */
.footer {
    padding: 18px 44px;
    background: var(--bg-subtle);
    border-top: 1px solid var(--border);
    text-align: center;
    font-size: 10px;
    color: var(--text-muted);
    line-height: 1.5;
}

.footer .footer-brand {
    font-weight: 600;
    color: var(--text-heading);
}

/* ═══════════════════════════════════════════════
   PAGE BREAKS
   ═══════════════════════════════════════════════ */
.account-summary, .summary-card, .summary-totals {
    page-break-inside: avoid;
}

.txn-table tbody tr {
    page-break-inside: avoid;
}

/* ═══════════════════════════════════════════════
   WATERMARK
   ═══════════════════════════════════════════════ */
.watermark {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%) rotate(-35deg);
    font-size: 72px;
    font-weight: 900;
    color: var(--primary);
    opacity: 0.06;
    pointer-events: none;
    text-transform: uppercase;
    letter-spacing: 8px;
}

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

    <!-- ════════════════════════════════════════
         HEADER
         ════════════════════════════════════════ -->
    <div class="header">
        <div class="header-left">
            {% if style.logo_url %}
            <img src="{{ style.logo_url }}" alt="Logo" class="header-logo">
            {% endif %}
            <div class="header-company">{{ data.from.name }}</div>
            {% if data.from.address %}
            <div class="header-company-detail">{{ data.from.address | nl2br }}</div>
            {% endif %}
            {% if data.from.email or data.from.phone %}
            <div class="header-company-detail">
                {% if data.from.email %}{{ data.from.email }}{% endif %}
                {% if data.from.email and data.from.phone %} &middot; {% endif %}
                {% if data.from.phone %}{{ data.from.phone }}{% endif %}
            </div>
            {% endif %}
        </div>
        <div class="header-right">
            <div class="header-title-badge">{{ title_map.get(document_type, 'ACCOUNT STATEMENT') }}</div>
            {% if data.document_number %}
            <div class="header-doc-number">#{{ data.document_number }}</div>
            {% endif %}
            {% if data.period_start or data.period_end %}
            <div class="header-period">
                {{ data.period_start | default('') | date_format }}
                {% if data.period_end %} — {{ data.period_end | date_format }}{% endif %}
            </div>
            {% endif %}
            <div class="header-generated">Generated: {{ data.issue_date | date_format }}</div>
        </div>
    </div>

    <div class="content">

        <!-- ════════════════════════════════════
             CUSTOMER INFO
             ════════════════════════════════════ -->
        <div class="section">
            <div class="section-header">Customer Information</div>
            <div class="customer-card">
                <div class="customer-info">
                    <div class="label">Statement For</div>
                    <div class="name">{{ data.to.name }}</div>
                    {% if data.to.address %}
                    <div class="detail">{{ data.to.address | nl2br }}</div>
                    {% endif %}
                    {% if data.to.email %}
                    <div class="detail">{{ data.to.email }}</div>
                    {% endif %}
                </div>
                {% if data.account_number %}
                <div class="customer-account">
                    <div class="label">Account Number</div>
                    <div class="account-num">{{ data.account_number }}</div>
                    <div class="stmt-date">Statement Date: {{ data.issue_date | date_format }}</div>
                </div>
                {% endif %}
            </div>
        </div>

        <!-- ════════════════════════════════════
             ACCOUNT SUMMARY
             ════════════════════════════════════ -->
        <div class="section">
            <div class="section-header">Account Summary</div>
            <div class="account-summary">
                <div class="summary-card opening">
                    <div class="label">Opening Balance</div>
                    <div class="amount">{{ data.opening_balance | default(0) | currency }}</div>
                </div>
                <div class="summary-card charges">
                    <div class="label">Total Charges</div>
                    <div class="amount">{{ data.total_debits | default(0) | currency }}</div>
                </div>
                <div class="summary-card payments">
                    <div class="label">Total Payments</div>
                    <div class="amount">{{ data.total_credits | default(0) | currency }}</div>
                </div>
                <div class="summary-card closing">
                    <div class="label">Closing Balance</div>
                    <div class="amount">{{ data.closing_balance | default(0) | currency }}</div>
                </div>
            </div>
        </div>

        <!-- ════════════════════════════════════
             TRANSACTION TABLE
             ════════════════════════════════════ -->
        <div class="section">
            <div class="section-header">Transaction Details</div>
            {% if data.transactions %}
            <table class="txn-table">
                <thead>
                    <tr>
                        <th style="width:14%;">Date</th>
                        <th style="width:42%;">Description</th>
                        {% if data.transactions[0] and data.transactions[0].reference %}
                        <th style="width:16%;">Reference</th>
                        {% endif %}
                        <th class="right" style="width:14%;">Debit</th>
                        <th class="right" style="width:14%;">Credit</th>
                        <th class="right" style="width:14%;">Balance</th>
                    </tr>
                </thead>
                <tbody>
                    {% for txn in data.transactions %}
                    <tr>
                        <td class="date-cell">{{ txn.date | date_format }}</td>
                        <td class="desc-cell">{{ txn.description | default('') }}</td>
                        {% if data.transactions[0] and data.transactions[0].reference %}
                        <td class="ref-cell">{{ txn.reference | default('') }}</td>
                        {% endif %}
                        <td class="right debit-cell">
                            {% if txn.debit and txn.debit > 0 %}
                            {{ txn.debit | currency }}
                            {% else %}
                            <span class="cell-dash">-</span>
                            {% endif %}
                        </td>
                        <td class="right credit-cell">
                            {% if txn.credit and txn.credit > 0 %}
                            {{ txn.credit | currency }}
                            {% else %}
                            <span class="cell-dash">-</span>
                            {% endif %}
                        </td>
                        <td class="right balance-cell">{{ txn.balance | default(0) | currency }}</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>

            <!-- Summary Totals at Bottom -->
            <div class="summary-totals">
                <table>
                    <tr class="total-row">
                        <td>Total Debits</td>
                        <td class="right debit-cell">{{ data.total_debits | default(0) | currency }}</td>
                    </tr>
                    <tr class="total-row">
                        <td>Total Credits</td>
                        <td class="right credit-cell">{{ data.total_credits | default(0) | currency }}</td>
                    </tr>
                    <tr class="grand-total-row">
                        <td>Closing Balance</td>
                        <td class="right">{{ data.closing_balance | default(0) | currency }}</td>
                    </tr>
                </table>
            </div>
            {% else %}
            <div style="text-align:center;padding:48px 0;color:var(--text-muted);font-size:12px;line-height:1.6;">
                <div style="font-size:24px;margin-bottom:8px;opacity:0.4;">&#9632;</div>
                No transactions recorded in this period.
            </div>
            {% endif %}
        </div>

        <!-- ════════════════════════════════════
             NOTES
             ════════════════════════════════════ -->
        {% if data.notes %}
        <div class="notes-block">
            <div class="notes-label">Notes</div>
            {{ data.notes | nl2br }}
        </div>
        {% endif %}

    </div>

    <!-- ════════════════════════════════════════
         FOOTER
         ════════════════════════════════════════ -->
    <div class="footer">
        <div class="footer-brand">{{ data.from.name }}</div>
        <div>For questions about this statement, contact {{ data.from.email | default(data.from.phone | default('your accounts department')) }}</div>
    </div>

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