<!DOCTYPE html>
<html lang="en-GB">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Model Validation Report: {{ card.get_effective_model_name() }}</title>
    <style>
        /* Reset and base */
        *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
        @media print {
            body { background: #fff; font-size: 11px; }
            .no-print { display: none; }
            .section { break-inside: avoid; }
            .report-header { border-radius: 0; }
        }
        body {
            font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
            font-size: 14px;
            line-height: 1.6;
            color: #1a1a2e;
            background: #f8f9fa;
        }
        .container { max-width: 1100px; margin: 0 auto; padding: 2rem 1.5rem; }
        /* Header */
        .report-header {
            background: #1a1a2e;
            color: #fff;
            padding: 2rem 2.5rem;
            border-radius: 8px 8px 0 0;
        }
        .report-header h1 { font-size: 1.5rem; font-weight: 600; margin-bottom: 0.25rem; }
        .report-header .subtitle { font-size: 0.875rem; color: #94a3b8; margin-bottom: 1rem; }
        .header-meta {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 0.5rem;
            font-size: 0.8125rem;
        }
        .header-meta-item { color: #94a3b8; }
        .header-meta-item strong { color: #e2e8f0; }
        /* RAG status banner */
        .rag-banner {
            padding: 1rem 2.5rem;
            display: flex;
            align-items: center;
            gap: 1rem;
            font-weight: 600;
            font-size: 0.9375rem;
        }
        .rag-green { background: #d1fae5; color: #065f46; }
        .rag-amber { background: #fef3c7; color: #92400e; }
        .rag-red   { background: #fee2e2; color: #991b1b; }
        .rag-dot {
            width: 14px; height: 14px; border-radius: 50%;
            display: inline-block; flex-shrink: 0;
        }
        .dot-green { background: #059669; }
        .dot-amber { background: #d97706; }
        .dot-red   { background: #dc2626; }
        /* Sections */
        .section {
            background: #fff;
            border: 1px solid #e2e8f0;
            border-radius: 6px;
            margin-top: 1.5rem;
            overflow: hidden;
        }
        .section-header {
            background: #f1f5f9;
            padding: 0.875rem 1.5rem;
            border-bottom: 1px solid #e2e8f0;
            display: flex;
            align-items: center;
            gap: 0.75rem;
        }
        .section-header h2 {
            font-size: 0.9375rem;
            font-weight: 600;
            color: #1e293b;
        }
        .section-number {
            width: 24px; height: 24px;
            background: #1a1a2e; color: #fff;
            border-radius: 50%;
            font-size: 0.75rem; font-weight: 700;
            display: flex; align-items: center; justify-content: center;
            flex-shrink: 0;
        }
        .section-body { padding: 1.25rem 1.5rem; }
        /* Tables */
        table {
            width: 100%;
            border-collapse: collapse;
            font-size: 0.8125rem;
        }
        th {
            background: #f8fafc;
            text-align: left;
            padding: 0.5rem 0.75rem;
            border-bottom: 2px solid #e2e8f0;
            font-weight: 600;
            color: #475569;
            white-space: nowrap;
        }
        td {
            padding: 0.5rem 0.75rem;
            border-bottom: 1px solid #f1f5f9;
            vertical-align: top;
        }
        tr:last-child td { border-bottom: none; }
        tr:hover td { background: #f8fafc; }
        /* Badges */
        .badge {
            display: inline-block;
            padding: 0.125rem 0.5rem;
            border-radius: 4px;
            font-size: 0.75rem;
            font-weight: 600;
            text-transform: uppercase;
            letter-spacing: 0.025em;
        }
        .badge-pass     { background: #d1fae5; color: #065f46; }
        .badge-fail     { background: #fee2e2; color: #991b1b; }
        .badge-info     { background: #dbeafe; color: #1e40af; }
        .badge-warning  { background: #fef3c7; color: #92400e; }
        .badge-critical { background: #fee2e2; color: #991b1b; }
        .badge-green    { background: #d1fae5; color: #065f46; }
        .badge-amber    { background: #fef3c7; color: #92400e; }
        .badge-red      { background: #fee2e2; color: #991b1b; }
        /* Model card grid */
        .card-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
            gap: 1rem;
        }
        .card-label {
            font-size: 0.75rem;
            font-weight: 600;
            text-transform: uppercase;
            letter-spacing: 0.05em;
            color: #64748b;
            margin-bottom: 0.2rem;
        }
        .card-value { font-size: 0.875rem; color: #1e293b; }
        /* Metric */
        .metric-value {
            font-family: 'Consolas', 'Courier New', monospace;
            font-size: 0.8125rem;
        }
        /* Summary stats */
        .summary-stats {
            display: flex;
            gap: 2rem;
            flex-wrap: wrap;
            margin-bottom: 1.5rem;
        }
        .stat-block { text-align: center; }
        .stat-number { font-size: 2rem; font-weight: 700; line-height: 1; }
        .stat-label { font-size: 0.75rem; color: #64748b; margin-top: 0.25rem; }
        .stat-pass { color: #059669; }
        .stat-fail { color: #dc2626; }
        .stat-warn { color: #d97706; }
        /* Info box */
        .info-box {
            background: #fffbeb;
            border: 1px solid #fcd34d;
            border-radius: 6px;
            padding: 0.875rem 1.25rem;
            font-size: 0.875rem;
            margin-bottom: 1rem;
        }
        .info-box strong { color: #92400e; }
        .ibnr-box {
            background: #f0f9ff;
            border: 1px solid #7dd3fc;
            border-radius: 6px;
            padding: 0.875rem 1.25rem;
            font-size: 0.8125rem;
            margin-top: 1rem;
            color: #0c4a6e;
        }
        /* Category chip */
        .cat-chip {
            display: inline-block;
            padding: 0.1rem 0.4rem;
            border-radius: 3px;
            font-size: 0.7rem;
            font-weight: 600;
            text-transform: uppercase;
            letter-spacing: 0.04em;
            background: #e2e8f0;
            color: #475569;
        }
        /* Details text */
        .details-text { color: #475569; font-size: 0.8125rem; max-width: 600px; }
        /* Footer */
        .report-footer {
            margin-top: 2rem;
            padding: 1rem 0;
            font-size: 0.75rem;
            color: #94a3b8;
            border-top: 1px solid #e2e8f0;
            display: flex;
            justify-content: space-between;
            flex-wrap: wrap;
            gap: 0.5rem;
        }
        /* Run ID */
        .run-id {
            font-family: monospace;
            font-size: 0.7rem;
            color: #94a3b8;
        }
        /* Monitoring plan */
        .monitoring-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 1rem;
        }
        /* Tier */
        .tier-1 { background: #fee2e2; color: #991b1b; }
        .tier-2 { background: #fef3c7; color: #92400e; }
        .tier-3 { background: #d1fae5; color: #065f46; }
        /* Responsive */
        @media (max-width: 640px) {
            .report-header { padding: 1.25rem; }
            .section-body { padding: 1rem; }
            .card-grid { grid-template-columns: 1fr 1fr; }
        }
    </style>
</head>
<body>
<div class="container">

    <!-- Report header -->
    <div class="report-header">
        <h1>Model Validation Report</h1>
        <div class="subtitle">{{ card.get_effective_model_name() }} v{{ card.version }} &mdash; PRA SS1/23 Compliance</div>
        <div class="header-meta">
            <div class="header-meta-item"><strong>Generated:</strong> {{ generated_date }}</div>
            <div class="header-meta-item"><strong>Developer:</strong> {{ card.get_effective_developer() }}</div>
            {% if card.materiality_tier %}<div class="header-meta-item"><strong>Materiality Tier:</strong> {{ card.materiality_tier }}</div>{% endif %}
            <div class="header-meta-item"><strong>Model Type:</strong> {{ card.model_type or 'Not specified' }} ({{ card.get_effective_distribution() }})</div>
            {% if card.validator_name %}<div class="header-meta-item"><strong>Validator:</strong> {{ card.validator_name }}</div>{% endif %}
            {% if card.validation_date %}<div class="header-meta-item"><strong>Validation Date:</strong> {{ card.validation_date }}</div>{% endif %}
            <div class="header-meta-item"><strong>Run ID:</strong> <span class="run-id">{{ run_id }}</span></div>
        </div>
    </div>

    <!-- RAG status banner -->
    {% if rag_status == 'red' %}
    <div class="rag-banner rag-red">
        <span class="rag-dot dot-red"></span>
        VALIDATION STATUS: RED &mdash; Critical findings require resolution before production sign-off.
    </div>
    {% elif rag_status == 'amber' %}
    <div class="rag-banner rag-amber">
        <span class="rag-dot dot-amber"></span>
        VALIDATION STATUS: AMBER &mdash; Warning findings require documented management decisions.
    </div>
    {% else %}
    <div class="rag-banner rag-green">
        <span class="rag-dot dot-green"></span>
        VALIDATION STATUS: GREEN &mdash; No critical or warning findings.
    </div>
    {% endif %}

    <!-- ═══════════════════════════════════════════════════════════════ -->
    <!-- SECTION 1: Executive Summary -->
    <!-- ═══════════════════════════════════════════════════════════════ -->
    <div class="section">
        <div class="section-header">
            <div class="section-number">1</div>
            <h2>Executive Summary</h2>
        </div>
        <div class="section-body">
            {% set n_pass = results | selectattr('passed', 'equalto', true) | list | length %}
            {% set n_fail = results | selectattr('passed', 'equalto', false) | list | length %}
            {% set n_critical = results | selectattr('severity', 'equalto', 'critical') | selectattr('passed', 'equalto', false) | list | length %}
            {% set n_warn = results | selectattr('severity', 'equalto', 'warning') | selectattr('passed', 'equalto', false) | list | length %}

            <div class="summary-stats">
                <div class="stat-block">
                    <div class="stat-number" style="color:#1e293b">{{ results | length }}</div>
                    <div class="stat-label">Total Tests</div>
                </div>
                <div class="stat-block">
                    <div class="stat-number stat-pass">{{ n_pass }}</div>
                    <div class="stat-label">Passed</div>
                </div>
                <div class="stat-block">
                    <div class="stat-number stat-fail">{{ n_fail }}</div>
                    <div class="stat-label">Failed</div>
                </div>
                <div class="stat-block">
                    <div class="stat-number stat-warn">{{ n_warn }}</div>
                    <div class="stat-label">Warnings</div>
                </div>
                <div class="stat-block">
                    <div class="stat-number stat-fail">{{ n_critical }}</div>
                    <div class="stat-label">Critical</div>
                </div>
                <div class="stat-block">
                    <div class="stat-number" style="color:{% if rag_status=='green' %}#059669{% elif rag_status=='amber' %}#d97706{% else %}#dc2626{% endif %}">
                        {{ rag_status | upper }}
                    </div>
                    <div class="stat-label">RAG Status</div>
                </div>
            </div>

            <!-- Critical and warning findings summary -->
            {% set critical_results = results | selectattr('severity', 'equalto', 'critical') | selectattr('passed', 'equalto', false) | list %}
            {% set warning_results = results | selectattr('severity', 'equalto', 'warning') | selectattr('passed', 'equalto', false) | list %}

            {% if critical_results %}
            <div class="info-box" style="background:#fee2e2;border-color:#fca5a5;">
                <strong>Critical Findings ({{ critical_results | length }}):</strong>
                <ul style="margin-top:0.5rem;padding-left:1.25rem;">
                {% for r in critical_results %}
                    <li>{{ r.test_name }}: {{ r.details[:150] }}{% if r.details | length > 150 %}...{% endif %}</li>
                {% endfor %}
                </ul>
            </div>
            {% endif %}

            {% if warning_results %}
            <div class="info-box">
                <strong>Warnings ({{ warning_results | length }}):</strong>
                <ul style="margin-top:0.5rem;padding-left:1.25rem;">
                {% for r in warning_results %}
                    <li>{{ r.test_name }}: {{ r.details[:150] }}{% if r.details | length > 150 %}...{% endif %}</li>
                {% endfor %}
                </ul>
            </div>
            {% endif %}

            {% if not critical_results and not warning_results %}
            <p style="color:#059669;font-weight:500;">No critical or warning findings. Model passes all standard validation tests.</p>
            {% endif %}
        </div>
    </div>

    <!-- ═══════════════════════════════════════════════════════════════ -->
    <!-- SECTION 2: Model Card and Governance -->
    <!-- ═══════════════════════════════════════════════════════════════ -->
    <div class="section">
        <div class="section-header">
            <div class="section-number">2</div>
            <h2>Model Card and Governance</h2>
        </div>
        <div class="section-body">
            <div class="card-grid" style="margin-bottom:1.25rem">
                {% for k, v in card.summary().items() %}
                <div>
                    <div class="card-label">{{ k }}</div>
                    <div class="card-value">
                        {% if k == 'Materiality tier' and v != 'Not specified' %}
                        <span class="badge tier-{{ v }}">Tier {{ v }}</span>
                        {% else %}
                        {{ v }}
                        {% endif %}
                    </div>
                </div>
                {% endfor %}
            </div>

            <div style="margin-bottom:1rem">
                <div class="card-label">Purpose</div>
                <div class="card-value">{{ card.purpose }}</div>
            </div>

            {% if card.intended_use %}
            <div style="margin-bottom:1rem">
                <div class="card-label">Intended Use</div>
                <div class="card-value">{{ card.intended_use }}</div>
            </div>
            {% endif %}

            <div class="info-box">
                <strong>Known Limitations:</strong> {{ card.get_effective_limitations() }}
            </div>

            {% if card.alternatives_considered %}
            <div style="margin-top:1rem">
                <div class="card-label">Alternatives Considered</div>
                <div class="card-value">{{ card.alternatives_considered }}</div>
            </div>
            {% endif %}

            {% if card.model_description %}
            <div style="margin-top:1rem">
                <div class="card-label">Model Description</div>
                <div class="card-value">{{ card.model_description }}</div>
            </div>
            {% endif %}

            <div style="margin-top:1rem">
                <div class="card-label">Model Variables ({{ card.get_effective_variables() | length }})</div>
                <div class="card-value">{{ card.get_effective_variables() | join(', ') or 'None listed' }}</div>
            </div>

            {% if card.outstanding_issues %}
            <div style="margin-top:1rem">
                <div class="info-box" style="background:#fff7ed;border-color:#fed7aa;">
                    <strong>Outstanding Issues ({{ card.outstanding_issues | length }}):</strong>
                    <ul style="margin-top:0.5rem;padding-left:1.25rem;">
                    {% for issue in card.outstanding_issues %}
                        <li>{{ issue }}</li>
                    {% endfor %}
                    </ul>
                </div>
            </div>
            {% endif %}
        </div>
    </div>

    <!-- ═══════════════════════════════════════════════════════════════ -->
    <!-- SECTION 3: Data Quality -->
    <!-- ═══════════════════════════════════════════════════════════════ -->
    {% set dq_results = results | selectattr('category', 'equalto', 'data_quality') | list %}
    {% if dq_results %}
    <div class="section">
        <div class="section-header">
            <div class="section-number">3</div>
            <h2>Data Quality Assessment</h2>
        </div>
        <div class="section-body">
            <div class="ibnr-box">
                <strong>IBNR Caveat:</strong> A/E ratios on recently-incurred claims may reflect IBNR
                underdevelopment, not model error. Use fully-developed claims wherever possible.
            </div>
            <table style="margin-top:1rem">
                <thead>
                    <tr><th>Test</th><th>Result</th><th>Severity</th><th>Metric</th><th>Details</th></tr>
                </thead>
                <tbody>
                {% for r in dq_results %}
                <tr>
                    <td><code style="font-size:0.8125rem">{{ r.test_name }}</code></td>
                    <td>{% if r.passed %}<span class="badge badge-pass">Pass</span>{% else %}<span class="badge badge-fail">Fail</span>{% endif %}</td>
                    <td><span class="badge badge-{{ r.severity }}">{{ r.severity }}</span></td>
                    <td class="metric-value">{% if r.metric_value is not none %}{{ "%.4f" | format(r.metric_value) }}{% else %}-{% endif %}</td>
                    <td class="details-text">{{ r.details }}</td>
                </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
    {% endif %}

    <!-- ═══════════════════════════════════════════════════════════════ -->
    <!-- SECTION 4: Model Development Documentation -->
    <!-- ═══════════════════════════════════════════════════════════════ -->
    <div class="section">
        <div class="section-header">
            <div class="section-number">4</div>
            <h2>Model Development Documentation</h2>
        </div>
        <div class="section-body">
            <table>
                <thead>
                    <tr><th>Item</th><th>Detail</th></tr>
                </thead>
                <tbody>
                    <tr><td>Methodology</td><td>{{ card.get_effective_distribution() }}</td></tr>
                    <tr><td>Target variable</td><td>{{ card.get_effective_target() }}</td></tr>
                    <tr><td>Model type</td><td>{{ card.model_type or 'Not specified' }}</td></tr>
                    <tr><td>Features ({{ card.get_effective_variables() | length }})</td><td>{{ card.get_effective_variables() | join(', ') or 'None listed' }}</td></tr>
                    <tr><td>Development date</td><td>{{ card.development_date or 'Not specified' }}</td></tr>
                    <tr><td>Known limitations</td><td>{{ card.get_effective_limitations() }}</td></tr>
                    {% if card.alternatives_considered %}
                    <tr><td>Alternatives considered</td><td>{{ card.alternatives_considered }}</td></tr>
                    {% endif %}
                    {% if card.model_description %}
                    <tr><td>Description</td><td>{{ card.model_description }}</td></tr>
                    {% endif %}
                </tbody>
            </table>
        </div>
    </div>

    <!-- ═══════════════════════════════════════════════════════════════ -->
    <!-- SECTION 5: Performance Validation -->
    <!-- ═══════════════════════════════════════════════════════════════ -->
    {% set perf_results = results | selectattr('category', 'equalto', 'performance') | list %}
    {% if perf_results %}
    <div class="section">
        <div class="section-header">
            <div class="section-number">5</div>
            <h2>Performance Validation</h2>
        </div>
        <div class="section-body">
            <!-- Key metrics highlight -->
            {% set gini_ci = results | selectattr('test_name', 'equalto', 'gini_with_ci') | list %}
            {% set ae_ci = results | selectattr('test_name', 'equalto', 'ae_poisson_ci') | list %}
            {% set hl = results | selectattr('test_name', 'equalto', 'hosmer_lemeshow') | list %}

            {% if gini_ci %}
            <div style="display:flex;gap:1.5rem;flex-wrap:wrap;margin-bottom:1.25rem">
                {% set g = gini_ci[0] %}
                <div class="stat-block">
                    <div class="stat-number" style="font-size:1.5rem;color:#1e293b">{{ "%.3f" | format(g.metric_value) if g.metric_value is not none else '-' }}</div>
                    <div class="stat-label">Gini (point)</div>
                </div>
                {% if g.extra and g.extra.ci_lower is defined %}
                <div class="stat-block">
                    <div class="stat-number" style="font-size:1.25rem;color:#475569">
                        [{{ "%.3f" | format(g.extra.ci_lower) }}, {{ "%.3f" | format(g.extra.ci_upper) }}]
                    </div>
                    <div class="stat-label">95% Bootstrap CI</div>
                </div>
                {% endif %}
                {% if ae_ci %}
                {% set ae = ae_ci[0] %}
                <div class="stat-block">
                    <div class="stat-number" style="font-size:1.5rem;color:{% if ae.passed %}#059669{% else %}#dc2626{% endif %}">
                        {{ "%.3f" | format(ae.metric_value) if ae.metric_value is not none else '-' }}
                    </div>
                    <div class="stat-label">A/E Ratio</div>
                </div>
                {% if ae.extra and ae.extra.ci_lower is defined %}
                <div class="stat-block">
                    <div class="stat-number" style="font-size:1.25rem;color:#475569">
                        [{{ "%.3f" | format(ae.extra.ci_lower) }}, {{ "%.3f" | format(ae.extra.ci_upper) }}]
                    </div>
                    <div class="stat-label">Poisson 95% CI</div>
                </div>
                {% endif %}
                {% endif %}
                {% if hl %}
                {% set h = hl[0] %}
                <div class="stat-block">
                    <div class="stat-number" style="font-size:1.5rem;color:{% if h.passed %}#059669{% else %}#dc2626{% endif %}">
                        {% if h.extra and h.extra.p_value is defined and h.extra.p_value is not none %}
                        p={{ "%.3f" | format(h.extra.p_value) }}
                        {% else %}-{% endif %}
                    </div>
                    <div class="stat-label">H-L p-value</div>
                </div>
                {% endif %}
            </div>
            {% endif %}

            <table>
                <thead>
                    <tr><th>Test</th><th>Result</th><th>Severity</th><th>Metric</th><th>Details</th></tr>
                </thead>
                <tbody>
                {% for r in perf_results %}
                <tr>
                    <td><code style="font-size:0.8125rem">{{ r.test_name }}</code></td>
                    <td>{% if r.passed %}<span class="badge badge-pass">Pass</span>{% else %}<span class="badge badge-fail">Fail</span>{% endif %}</td>
                    <td><span class="badge badge-{{ r.severity }}">{{ r.severity }}</span></td>
                    <td class="metric-value">{% if r.metric_value is not none %}{{ "%.4f" | format(r.metric_value) }}{% else %}-{% endif %}</td>
                    <td class="details-text">{{ r.details }}</td>
                </tr>
                {% endfor %}
                </tbody>
            </table>

            <div class="ibnr-box">
                <strong>IBNR Caveat:</strong> A/E ratios on recently-incurred claims may reflect IBNR
                underdevelopment, not model error. Use fully-developed claims wherever possible.
            </div>
        </div>
    </div>
    {% endif %}

    <!-- ═══════════════════════════════════════════════════════════════ -->
    <!-- SECTION 6: Fairness and Discrimination -->
    <!-- ═══════════════════════════════════════════════════════════════ -->
    {% set disc_results = results | selectattr('category', 'equalto', 'discrimination') | list %}
    {% set fair_results = results | selectattr('category', 'equalto', 'fairness') | list %}
    {% set all_fairness = disc_results + fair_results %}
    {% if all_fairness %}
    <div class="section">
        <div class="section-header">
            <div class="section-number">6</div>
            <h2>Fairness and Discrimination (FCA Consumer Duty / TR24/2)</h2>
        </div>
        <div class="section-body">
            <table>
                <thead>
                    <tr><th>Test</th><th>Category</th><th>Result</th><th>Severity</th><th>Metric</th><th>Details</th></tr>
                </thead>
                <tbody>
                {% for r in all_fairness %}
                <tr>
                    <td><code style="font-size:0.8125rem">{{ r.test_name }}</code></td>
                    <td><span class="cat-chip">{{ r.category }}</span></td>
                    <td>{% if r.passed %}<span class="badge badge-pass">Pass</span>{% else %}<span class="badge badge-fail">Fail</span>{% endif %}</td>
                    <td><span class="badge badge-{{ r.severity }}">{{ r.severity }}</span></td>
                    <td class="metric-value">{% if r.metric_value is not none %}{{ "%.4f" | format(r.metric_value) }}{% else %}-{% endif %}</td>
                    <td class="details-text">{{ r.details }}</td>
                </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
    {% endif %}

    <!-- ═══════════════════════════════════════════════════════════════ -->
    <!-- SECTION 7: Stability -->
    <!-- ═══════════════════════════════════════════════════════════════ -->
    {% set stab_results = results | selectattr('category', 'equalto', 'stability') | list %}
    {% if stab_results %}
    <div class="section">
        <div class="section-header">
            <div class="section-number">7</div>
            <h2>Population Stability</h2>
        </div>
        <div class="section-body">
            <table>
                <thead>
                    <tr><th>Test</th><th>Result</th><th>Severity</th><th>PSI</th><th>Details</th></tr>
                </thead>
                <tbody>
                {% for r in stab_results %}
                <tr>
                    <td><code style="font-size:0.8125rem">{{ r.test_name }}</code></td>
                    <td>{% if r.passed %}<span class="badge badge-pass">Pass</span>{% else %}<span class="badge badge-fail">Fail</span>{% endif %}</td>
                    <td><span class="badge badge-{{ r.severity }}">{{ r.severity }}</span></td>
                    <td class="metric-value">{% if r.metric_value is not none %}{{ "%.4f" | format(r.metric_value) }}{% else %}-{% endif %}</td>
                    <td class="details-text">{{ r.details }}</td>
                </tr>
                {% endfor %}
                </tbody>
            </table>
            <p style="margin-top:0.75rem;font-size:0.8rem;color:#64748b">
                PSI thresholds: &lt;0.10 stable; 0.10&ndash;0.25 moderate shift (monitor); &gt;0.25 significant shift (recalibrate).
            </p>
        </div>
    </div>
    {% endif %}

    <!-- ═══════════════════════════════════════════════════════════════ -->
    <!-- SECTION 8: Feature Importance (optional) -->
    <!-- ═══════════════════════════════════════════════════════════════ -->
    {% set shap_results = results | selectattr('test_name', 'equalto', 'shap_feature_importance') | list %}
    {% if shap_results %}
    <div class="section">
        <div class="section-header">
            <div class="section-number">8</div>
            <h2>Feature Importance (SHAP)</h2>
        </div>
        <div class="section-body">
            {% for r in shap_results %}
            <p class="details-text">{{ r.details }}</p>
            {% endfor %}
        </div>
    </div>
    {% else %}
    <div class="section">
        <div class="section-header">
            <div class="section-number">8</div>
            <h2>Feature Importance (optional)</h2>
        </div>
        <div class="section-body">
            <p class="details-text">SHAP feature importance not computed for this run.
            Install <code>insurance-validation[shap]</code> and re-run to include this section.</p>
        </div>
    </div>
    {% endif %}

    <!-- ═══════════════════════════════════════════════════════════════ -->
    <!-- SECTION 9: Monitoring Plan and Sign-Off -->
    <!-- ═══════════════════════════════════════════════════════════════ -->
    {% set mon_results = results | selectattr('category', 'equalto', 'monitoring') | list %}
    <div class="section">
        <div class="section-header">
            <div class="section-number">9</div>
            <h2>Monitoring Plan and Sign-Off</h2>
        </div>
        <div class="section-body">
            {% if mon_results %}
            {% for r in mon_results %}
            <div style="margin-bottom:1rem">
                <span class="badge {% if r.passed %}badge-pass{% else %}badge-warning{% endif %}" style="margin-bottom:0.5rem">
                    {% if r.passed %}Plan Complete{% else %}Plan Incomplete{% endif %}
                </span>
                <p class="details-text" style="margin-top:0.25rem">{{ r.details }}</p>
            </div>
            {% endfor %}
            {% endif %}

            <!-- Regulatory appendix -->
            <h3 style="font-size:0.875rem;font-weight:600;color:#1e293b;margin:1.25rem 0 0.75rem">Regulatory Reference</h3>
            <table>
                <thead>
                    <tr><th>Requirement</th><th>Source</th><th>Coverage</th></tr>
                </thead>
                <tbody>
                    <tr>
                        <td>Model inventory and risk classification</td>
                        <td>PRA SS1/23 Principle 1</td>
                        <td>Section 2 &mdash; Materiality tier {{ card.materiality_tier or 'not set' }}</td>
                    </tr>
                    <tr>
                        <td>Development documentation</td>
                        <td>PRA SS1/23 Principle 3</td>
                        <td>Section 4 &mdash; purpose, methodology, limitations</td>
                    </tr>
                    <tr>
                        <td>Independent validation</td>
                        <td>PRA SS1/23 Principle 4</td>
                        <td>{% if card.validator_name %}Validator: {{ card.validator_name }}{% else %}Not yet assigned{% endif %}</td>
                    </tr>
                    <tr>
                        <td>Data quality assessment</td>
                        <td>PRA SS1/23 Principle 4</td>
                        <td>Section 3 &mdash; {{ dq_results | length }} tests</td>
                    </tr>
                    <tr>
                        <td>Performance validation (Gini, A/E, calibration)</td>
                        <td>PRA SS1/23 Principle 4</td>
                        <td>Section 5 &mdash; {{ perf_results | length }} tests</td>
                    </tr>
                    <tr>
                        <td>Proxy discrimination / fair value</td>
                        <td>FCA Consumer Duty; FCA TR24/2</td>
                        <td>Section 6 &mdash; {{ all_fairness | length }} tests</td>
                    </tr>
                    <tr>
                        <td>Renewal pricing fairness</td>
                        <td>FCA TR24/2</td>
                        <td>Section 6 &mdash; renewal cohort A/E test</td>
                    </tr>
                    <tr>
                        <td>Population stability monitoring</td>
                        <td>PRA SS1/23 Principle 5; SS1/24</td>
                        <td>Section 7 &mdash; {{ stab_results | length }} tests</td>
                    </tr>
                    <tr>
                        <td>Named monitoring owner</td>
                        <td>PRA SS1/23 Principle 5</td>
                        <td>Section 9 &mdash; {% if mon_results and mon_results[0].extra and mon_results[0].extra.owner %}{{ mon_results[0].extra.owner }}{% else %}Not assigned{% endif %}</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>

    <div class="report-footer">
        <span>Generated by insurance-validation v0.2.0 &mdash; MIT Licence &mdash; PRA SS1/23 aligned</span>
        <span>{{ card.get_effective_model_name() }} v{{ card.version }} &mdash; {{ generated_date }}</span>
        <span class="run-id">Run ID: {{ run_id }}</span>
    </div>

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