<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{ contract.title }}</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
            font-size: 11pt;
            line-height: 1.6;
            color: #1f2937;
            padding: 0;
        }

        .header {
            border-bottom: 3px solid #2563eb;
            padding-bottom: 20px;
            margin-bottom: 30px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .header-left {
            flex: 1;
        }

        .header-right {
            text-align: right;
        }

        .logo {
            max-width: 180px;
            max-height: 60px;
            margin-bottom: 10px;
        }

        .business-name {
            font-size: 24pt;
            font-weight: 700;
            color: #1f2937;
            margin-bottom: 5px;
        }

        .contract-title {
            font-size: 20pt;
            font-weight: 600;
            color: #1f2937;
            margin: 30px 0 20px 0;
            text-align: center;
        }

        .metadata {
            background-color: #f9fafb;
            border: 1px solid #e5e7eb;
            border-radius: 8px;
            padding: 15px;
            margin-bottom: 30px;
        }

        .metadata-row {
            display: flex;
            margin-bottom: 8px;
        }

        .metadata-row:last-child {
            margin-bottom: 0;
        }

        .metadata-label {
            font-weight: 600;
            color: #6b7280;
            width: 180px;
        }

        .metadata-value {
            color: #1f2937;
        }

        .content {
            margin: 30px 0;
            line-height: 1.8;
        }

        .content h1 {
            font-size: 16pt;
            margin-top: 24px;
            margin-bottom: 12px;
            color: #1f2937;
        }

        .content h2 {
            font-size: 14pt;
            margin-top: 20px;
            margin-bottom: 10px;
            color: #374151;
        }

        .content p {
            margin-bottom: 12px;
        }

        .content ul, .content ol {
            margin-left: 20px;
            margin-bottom: 12px;
        }

        .signature-section {
            margin-top: 40px;
            padding: 20px;
            background-color: #f0f9ff;
            border: 2px solid #2563eb;
            border-radius: 8px;
        }

        .signature-section h2 {
            font-size: 16pt;
            color: #1e40af;
            margin-bottom: 15px;
            text-align: center;
        }

        .signature-grid {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 10px;
            margin-bottom: 15px;
        }

        .signature-field {
            margin-bottom: 10px;
        }

        .signature-label {
            font-weight: 600;
            color: #1e40af;
            font-size: 9pt;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }

        .signature-value {
            font-size: 11pt;
            color: #1f2937;
            margin-top: 3px;
        }

        .signature-checkmark {
            font-size: 14pt;
            color: #059669;
            font-weight: 700;
        }

        .audit-trail {
            margin-top: 40px;
            page-break-inside: avoid;
        }

        .audit-trail h2 {
            font-size: 14pt;
            color: #1f2937;
            margin-bottom: 15px;
            border-bottom: 2px solid #e5e7eb;
            padding-bottom: 8px;
        }

        .audit-table {
            width: 100%;
            border-collapse: collapse;
            font-size: 9pt;
            margin-bottom: 20px;
        }

        .audit-table th {
            background-color: #f3f4f6;
            color: #374151;
            font-weight: 600;
            text-align: left;
            padding: 10px;
            border: 1px solid #d1d5db;
        }

        .audit-table td {
            padding: 8px 10px;
            border: 1px solid #e5e7eb;
            color: #1f2937;
        }

        .audit-table tr:nth-child(even) {
            background-color: #f9fafb;
        }

        .legal-notice {
            margin-top: 30px;
            padding: 15px;
            background-color: #fef3c7;
            border-left: 4px solid #f59e0b;
            font-size: 9pt;
            color: #78350f;
            line-height: 1.5;
            page-break-inside: avoid;
        }

        .legal-notice-title {
            font-weight: 700;
            margin-bottom: 8px;
            font-size: 10pt;
        }

        .footer {
            margin-top: 40px;
            padding-top: 20px;
            border-top: 1px solid #e5e7eb;
            text-align: center;
            font-size: 9pt;
            color: #6b7280;
        }

        .verification-code {
            font-family: "Courier New", monospace;
            background-color: #f3f4f6;
            padding: 2px 6px;
            border-radius: 4px;
            font-size: 10pt;
        }
    </style>
</head>
<body>
    <div class="header">
        <div class="header-left">
            {% if business_logo_url %}
            <img src="{{ business_logo_url }}" alt="{{ business_name }}" class="logo">
            {% else %}
            <div class="business-name">{{ business_name }}</div>
            {% endif %}
        </div>
        <div class="header-right">
            <div style="font-size: 10pt; color: #6b7280;">
                <div>Document ID: <strong>{{ contract.id }}</strong></div>
                <div>Generated: <strong>{{ signature.signed_at|date:"M d, Y g:i A" }}</strong></div>
            </div>
        </div>
    </div>

    <h1 class="contract-title">{{ contract.title }}</h1>

    <div class="metadata">
        <div class="metadata-row">
            <span class="metadata-label">Customer:</span>
            <span class="metadata-value">{{ customer.get_full_name|default:customer.email }}</span>
        </div>
        <div class="metadata-row">
            <span class="metadata-label">Customer Email:</span>
            <span class="metadata-value">{{ customer.email }}</span>
        </div>
        {% if event %}
        <div class="metadata-row">
            <span class="metadata-label">Appointment:</span>
            <span class="metadata-value">{{ event.title }} - {{ event.start_time|date:"M d, Y g:i A" }}</span>
        </div>
        {% endif %}
        <div class="metadata-row">
            <span class="metadata-label">Contract Created:</span>
            <span class="metadata-value">{{ contract.created_at|date:"M d, Y g:i A" }}</span>
        </div>
        <div class="metadata-row">
            <span class="metadata-label">Signed:</span>
            <span class="metadata-value">{{ signature.signed_at|date:"M d, Y g:i A" }}</span>
        </div>
    </div>

    <div class="content">
        {{ contract.content_html|safe }}
    </div>

    <div class="signature-section">
        <h2><span class="signature-checkmark">✓</span> Electronically Signed</h2>
        
        <div class="signature-grid">
            <div class="signature-field">
                <div class="signature-label">Signer Name</div>
                <div class="signature-value">{{ signature.signer_name }}</div>
            </div>
            <div class="signature-field">
                <div class="signature-label">Signer Email</div>
                <div class="signature-value">{{ signature.signer_email }}</div>
            </div>
            <div class="signature-field">
                <div class="signature-label">Signed At</div>
                <div class="signature-value">{{ signature.signed_at|date:"F d, Y g:i:s A T" }}</div>
            </div>
            <div class="signature-field">
                <div class="signature-label">Verification Code</div>
                <div class="signature-value">
                    <span class="verification-code">{{ contract.content_hash|slice:":16" }}</span>
                </div>
            </div>
        </div>

        <div class="signature-field" style="margin-top: 15px;">
            <div class="signature-label">Electronic Consent</div>
            <div class="signature-value">
                <span class="signature-checkmark">✓</span> 
                {{ signature.consent_text|truncatewords:20 }}
            </div>
        </div>
    </div>

    <div class="audit-trail">
        <h2>Audit Trail & Verification</h2>
        
        <table class="audit-table">
            <thead>
                <tr>
                    <th>Field</th>
                    <th>Value</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>Document ID</td>
                    <td>{{ contract.id }}</td>
                </tr>
                <tr>
                    <td>Document Hash (SHA-256)</td>
                    <td style="font-family: 'Courier New', monospace; font-size: 8pt; word-break: break-all;">
                        {{ signature.document_hash_at_signing }}
                    </td>
                </tr>
                <tr>
                    <td>Signed At (UTC)</td>
                    <td>{{ signature.signed_at|date:"Y-m-d H:i:s" }} UTC</td>
                </tr>
                <tr>
                    <td>Signer IP Address</td>
                    <td>{{ signature.ip_address }}</td>
                </tr>
                {% if geolocation %}
                <tr>
                    <td>Geolocation</td>
                    <td>{{ geolocation }}</td>
                </tr>
                {% endif %}
                <tr>
                    <td>User Agent</td>
                    <td style="font-size: 8pt; word-break: break-word;">{{ signature.user_agent|truncatewords:15 }}</td>
                </tr>
                <tr>
                    <td>Verification Code</td>
                    <td><span class="verification-code">{{ contract.content_hash|slice:":16" }}</span></td>
                </tr>
            </tbody>
        </table>
    </div>

    <div class="legal-notice">
        <div class="legal-notice-title">Legal Notice - Electronic Signature</div>
        <p>{{ esign_notice }}</p>
        <p style="margin-top: 10px;">
            This document has been cryptographically signed and verified. 
            The document hash (SHA-256) ensures the content has not been tampered with since signing.
            Any modification to the content would result in a different hash value.
        </p>
    </div>

    <div class="footer">
        <p>
            This is a legally binding document executed electronically.<br>
            For verification, contact {{ business_name }}{% if tenant.contact_email %} at {{ tenant.contact_email }}{% endif %}.
        </p>
    </div>
</body>
</html>
