<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Version Comparison - Finite State Report</title>
    <script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.1/dist/chart.umd.min.js"></script>

    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
            line-height: 1.6;
            color: #333;
            background-color: #f8f9fa;
        }

        .container {
            margin: 0 auto;
            padding: 20px;
            background-color: white;
            border: 1px solid #dee2e6;
            min-height: 100%;
        }

        .header {
            text-align: center;
            margin-bottom: 30px;
            padding-bottom: 20px;
            border-bottom: 2px solid #e9ecef;
        }

        .logo-container { margin-bottom: 20px; }
        .company-logo { max-height: 50px; width: auto; margin: 0 auto; display: block; }
        .header h1 { color: #2c3e50; font-size: 2.2em; margin-bottom: 10px; }
        .header .subtitle { color: #6c757d; font-size: 1.1em; }

        .metadata {
            background-color: #f8f9fa;
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 30px;
            font-size: 0.9em;
            color: #6c757d;
        }

        /* KPI Cards */
        .kpi-row {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
            gap: 16px;
            margin-bottom: 30px;
        }

        .kpi-card {
            background-color: #fff;
            border: 1px solid #dee2e6;
            border-radius: 10px;
            padding: 20px;
            text-align: center;
            box-shadow: 0 1px 3px rgba(0,0,0,0.08);
        }

        .kpi-card .kpi-title {
            font-size: 0.85em;
            color: #6c757d;
            text-transform: uppercase;
            letter-spacing: 0.5px;
            margin-bottom: 8px;
        }

        .kpi-card .kpi-values {
            display: flex;
            justify-content: center;
            align-items: baseline;
            gap: 8px;
            font-size: 1.6em;
            font-weight: 700;
            color: #2c3e50;
        }

        .kpi-card .kpi-arrow { font-size: 0.7em; color: #6c757d; }

        .kpi-card .kpi-delta {
            font-size: 0.55em;
            font-weight: 600;
            padding: 3px 8px;
            border-radius: 12px;
            margin-top: 8px;
            display: inline-block;
        }

        .kpi-delta.improved { background-color: #e8f5e9; color: #2e7d32; }
        .kpi-delta.regressed { background-color: #ffebee; color: #c62828; }
        .kpi-delta.neutral { background-color: #f5f5f5; color: #757575; }

        /* Project sections */
        .project-section {
            margin-bottom: 40px;
            border: 1px solid #dee2e6;
            border-radius: 10px;
            overflow: hidden;
        }

        .project-header {
            background-color: #f8f9fa;
            padding: 16px 20px;
            border-bottom: 1px solid #dee2e6;
            display: flex;
            justify-content: space-between;
            align-items: center;
            cursor: pointer;
        }

        .project-header h2 { font-size: 1.3em; color: #2c3e50; }

        .project-header .project-meta {
            font-size: 0.85em;
            color: #6c757d;
        }

        .project-body { padding: 20px; }

        /* Mini KPI row inside project */
        .mini-kpi-row {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
            gap: 10px;
            margin-bottom: 20px;
        }

        .mini-kpi {
            text-align: center;
            padding: 10px;
            border-radius: 8px;
            background-color: #f8f9fa;
        }

        .mini-kpi .label { font-size: 0.75em; color: #6c757d; text-transform: uppercase; }
        .mini-kpi .value { font-size: 1.4em; font-weight: 700; color: #2c3e50; }

        /* Charts */
        .chart-container {
            background-color: white;
            border-radius: 8px;
            padding: 20px;
            margin-bottom: 20px;
            border: 1px solid #dee2e6;
        }

        .chart-wrapper { position: relative; height: 300px; }

        /* Tables */
        .data-table {
            width: 100%;
            border-collapse: collapse;
            font-size: 0.9em;
        }

        .data-table thead th {
            background-color: #f8f9fa;
            border-bottom: 2px solid #dee2e6;
            padding: 10px 12px;
            text-align: left;
            font-weight: 600;
            color: #495057;
        }

        .data-table tbody td {
            padding: 8px 12px;
            border-bottom: 1px solid #e9ecef;
        }

        .data-table tbody tr:hover { background-color: #f8f9fa; }

        .data-table .num { text-align: right; font-variant-numeric: tabular-nums; }

        /* Progression table special styling */
        .progression-table .delta-positive { color: #c62828; }
        .progression-table .delta-negative { color: #2e7d32; }
        .progression-table .delta-zero { color: #757575; }

        /* Severity badges */
        .severity-badge {
            display: inline-block;
            padding: 2px 8px;
            border-radius: 4px;
            font-size: 0.8em;
            font-weight: 600;
            text-transform: uppercase;
        }

        .severity-CRITICAL { background-color: #ffebee; color: #b71c1c; }
        .severity-HIGH { background-color: #fff3e0; color: #e65100; }
        .severity-MEDIUM { background-color: #fff8e1; color: #f57f17; }
        .severity-LOW { background-color: #e8f5e9; color: #2e7d32; }
        .severity-INFO { background-color: #e3f2fd; color: #1565c0; }
        .severity-UNSPECIFIED { background-color: #f5f5f5; color: #757575; }

        /* Change type badges */
        .change-badge { display: inline-block; padding: 2px 8px; border-radius: 4px; font-size: 0.8em; font-weight: 600; }
        .change-added { background-color: #e8f5e9; color: #2e7d32; }
        .change-removed { background-color: #ffebee; color: #c62828; }
        .change-updated { background-color: #e3f2fd; color: #1565c0; }

        /* Collapsible sections */
        .section { margin-bottom: 20px; }

        .section-header {
            display: flex;
            align-items: center;
            gap: 10px;
            margin-bottom: 12px;
            padding-bottom: 8px;
            border-bottom: 1px solid #e9ecef;
        }

        .section-header h3 { font-size: 1.1em; color: #2c3e50; }

        .badge { font-size: 0.8em; padding: 3px 10px; border-radius: 12px; font-weight: 600; }
        .badge-fixed { background-color: #e8f5e9; color: #2e7d32; }
        .badge-new { background-color: #ffebee; color: #c62828; }
        .badge-churn { background-color: #e3f2fd; color: #1565c0; }

        .changes-summary { display: flex; gap: 24px; flex-wrap: wrap; margin-bottom: 16px; font-size: 0.9em; color: #495057; }
        .changes-summary-fixed { color: #2e7d32; font-weight: 500; }
        .changes-summary-new { color: #c62828; font-weight: 500; }
        .changes-two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 24px; }
        @media (max-width: 900px) { .changes-two-col { grid-template-columns: 1fr; } }
        .changes-col { min-width: 0; }
        .changes-col-title { font-size: 1em; color: #2c3e50; margin-bottom: 10px; padding-bottom: 6px; border-bottom: 1px solid #e9ecef; }
        .empty-state-inline { color: #6c757d; font-style: italic; padding: 12px 0; margin: 0; }

        .changelog-section { margin-top: 24px; }
        .changelog-section h3 { margin-bottom: 12px; }
        .changelog-entry { border: 1px solid #e9ecef; border-radius: 8px; margin-bottom: 12px; overflow: hidden; }
        .changelog-entry-header { display: flex; align-items: center; gap: 12px; padding: 12px 16px; background: #f8f9fa; cursor: pointer; font-weight: 500; }
        .changelog-entry-header:hover { background: #e9ecef; }
        .changelog-entry-header .changelog-version { color: #2c3e50; }
        .changelog-entry-body { padding: 16px; display: none; border-top: 1px solid #e9ecef; }
        .changelog-entry.expanded .changelog-entry-body { display: block; }

        .show-more-row td { text-align: center; padding: 10px; }
        .show-more-btn {
            background: none;
            border: 1px solid #dee2e6;
            border-radius: 6px;
            padding: 6px 16px;
            color: #495057;
            cursor: pointer;
            font-size: 0.9em;
        }
        .show-more-btn:hover { background-color: #f8f9fa; }
        .hidden-row { display: none; }

        .empty-state {
            text-align: center;
            padding: 40px 20px;
            color: #6c757d;
        }
        .empty-state p { font-size: 1.1em; margin-bottom: 8px; }

        .footer {
            text-align: center;
            padding: 20px 0;
            margin-top: 30px;
            border-top: 1px solid #e9ecef;
            color: #adb5bd;
            font-size: 0.85em;
        }
    </style>
</head>
<body>
    <div class="container">
        <!-- Header -->
        <div class="header">
            {% if logo_path %}
            <div class="logo-container">
                <img src="{{ logo_path }}" alt="Company Logo" class="company-logo">
            </div>
            {% else %}
            <div class="logo-container">
                <img src="data:image/webp;base64,UklGRogLAABXRUJQVlA4WAoAAAAQAAAA/wAAMAAAQUxQSJsHAAAB8EZt/zql/v9dNDjCYPdgd7cLRnfZsdgg2N0KdmF3d7did3d3dxe1ckut7YzCmNeNeb6e8xzeMe+4ExETgP/zzTe0okPn0ieRlhm5HLcm95m+4CUTejk7ZpUOkKuLQjfWxDt/OGD5Fv7gqVqAE1BsHbmvgoPlNiiFzwOBXFVRvARQ5yy/z8vrSLV6wvShHkBw7CKEmIZ7AEEv+dcAV0ep+jFyaUGg5mlyLgLJ520Az+EZfNTcISq0nDxaDSiwjLwneJbFY1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vArjTeoBsBiLk2I6tNAOI1BicVGLb008DuWG9RV0oqasjqcyxMuTd7CCLkWzXD//BS6oW/94CEbS3Pg7qi7eJwCT93iHAPhPuHcWx1dF7z+RLcx6bRqlnrqqinGaW5oYozyKstInug4w3eu9ypoHaLLwJb5D3/iADFjNPd10L2dbh9CFGP+rEjdupInLy2nUngp3wP/Z7iSiaO+SV6MF1J2ZAPsxJtXUFwVlgHcmwyqujWazTJ367dp6zib3FnY/7dHbxbZXzHEV37AnCuCiruTi/zm/9g8tnTfblrGDrDGRAl8ThBs5HWPgY8x0bDxP35ITV6015+SLai8QkDVLevEnQPXMgZadvkfPYUY22Bz1HFZj3iNRhFfL05MpXr1IKdBy07hvaFxL1IAAAAAAAAAAAAAAAAA="
                     alt="Finite State Logo" class="company-logo">
            </div>
            {% endif %}
            <h1>Version Comparison</h1>
            {% if transform_result and transform_result.is_pair_comparison and transform_result.projects %}
            {% set _p0 = transform_result.projects[0] %}
            {% set _bpn = _p0.latest_delta.baseline_project_name | default('') %}
            {% set _cpn = _p0.latest_delta.current_project_name | default('') %}
            {% if _bpn and _cpn and _bpn != _cpn %}
            <p class="subtitle">{{ _bpn }} &harr; {{ _cpn }}</p>
            {% elif _bpn %}
            <p class="subtitle">{{ _bpn }}</p>
            {% else %}
            <p class="subtitle">Two-version comparison</p>
            {% endif %}
            {% elif transform_result and transform_result.project_count %}
            <p class="subtitle">Version-over-version progression across {{ transform_result.project_count }} project{{ 's' if transform_result.project_count != 1 else '' }}</p>
            {% else %}
            <p class="subtitle">Version-over-version progression</p>
            {% endif %}
        </div>

        <!-- Metadata -->
        <div class="metadata">
            <strong>Report Generated:</strong> {{ metadata.start_date | default('N/A') }} |
            {% if transform_result and transform_result.is_pair_comparison and transform_result.projects %}
            {% set _md_p0 = transform_result.projects[0] %}
            {% set _md_bpn = _md_p0.latest_delta.baseline_project_name | default('') %}
            {% set _md_cpn = _md_p0.latest_delta.current_project_name | default('') %}
            {% if _md_bpn and _md_cpn and _md_bpn != _md_cpn %}
            <strong>Project A:</strong> {{ _md_bpn }} |
            <strong>Project B:</strong> {{ _md_cpn }} |
            {% elif _md_bpn %}
            <strong>Project:</strong> {{ _md_bpn }} |
            {% endif %}
            {% elif metadata.project_filter %}
            <strong>Project:</strong> {{ metadata.project_filter }} |
            {% endif %}
            {% if metadata.folder_filter %}
            <strong>Folder:</strong> {{ metadata.folder_filter }} |
            {% endif %}
            <strong>Generated by:</strong> Finite State Reporting Kit
        </div>

        {% if transform_result and transform_result.projects and transform_result.projects|length > 0 %}
        {% set result = transform_result %}
        {% set kpi = result.kpi %}

        <!-- Aggregate KPI Cards -->
        <div class="kpi-row">
            <div class="kpi-card">
                <div class="kpi-title">Total Findings</div>
                <div class="kpi-values">
                    <span>{{ kpi.total_findings.baseline }}</span>
                    <span class="kpi-arrow">&rarr;</span>
                    <span>{{ kpi.total_findings.current }}</span>
                </div>
                {% set tf = kpi.total_findings %}
                <span class="kpi-delta {% if tf.delta < 0 %}improved{% elif tf.delta > 0 %}regressed{% else %}neutral{% endif %}">
                    {% if tf.delta > 0 %}+{% endif %}{{ tf.delta }} ({% if tf.delta > 0 %}+{% endif %}{{ tf.pct }}%)
                </span>
            </div>

            <div class="kpi-card">
                <div class="kpi-title">Critical</div>
                <div class="kpi-values">
                    <span>{{ kpi.critical_findings.baseline }}</span>
                    <span class="kpi-arrow">&rarr;</span>
                    <span>{{ kpi.critical_findings.current }}</span>
                </div>
                {% set cf = kpi.critical_findings %}
                <span class="kpi-delta {% if cf.delta < 0 %}improved{% elif cf.delta > 0 %}regressed{% else %}neutral{% endif %}">
                    {% if cf.delta > 0 %}+{% endif %}{{ cf.delta }}
                </span>
            </div>

            <div class="kpi-card">
                <div class="kpi-title">High</div>
                <div class="kpi-values">
                    <span>{{ kpi.high_findings.baseline }}</span>
                    <span class="kpi-arrow">&rarr;</span>
                    <span>{{ kpi.high_findings.current }}</span>
                </div>
                {% set hf = kpi.high_findings %}
                <span class="kpi-delta {% if hf.delta < 0 %}improved{% elif hf.delta > 0 %}regressed{% else %}neutral{% endif %}">
                    {% if hf.delta > 0 %}+{% endif %}{{ hf.delta }}
                </span>
            </div>

            <div class="kpi-card">
                <div class="kpi-title">Components</div>
                <div class="kpi-values">
                    <span>{{ kpi.components.baseline }}</span>
                    <span class="kpi-arrow">&rarr;</span>
                    <span>{{ kpi.components.current }}</span>
                </div>
                {% set cp = kpi.components %}
                <span class="kpi-delta neutral">
                    {% if cp.delta > 0 %}+{% endif %}{{ cp.delta }}
                </span>
            </div>

            <div class="kpi-card">
                <div class="kpi-title">Fixed (latest)</div>
                <div class="kpi-values">
                    <span style="color:#2e7d32">{{ kpi.fixed_count }}</span>
                </div>
            </div>

            <div class="kpi-card">
                <div class="kpi-title">New (latest)</div>
                <div class="kpi-values">
                    <span style="color:#c62828">{{ kpi.new_count }}</span>
                </div>
            </div>
        </div>

        <!-- Per-project sections -->
        {% for proj in result.projects %}
        <div class="project-section" id="project-{{ loop.index }}">
            {% set is_pair = proj.is_pair_comparison | default(false) %}
            <div class="project-header" onclick="toggleProject('project-body-{{ loop.index }}')">
                <h2>{{ proj.project_name }}</h2>
                <span class="project-meta">
                    {% if is_pair %}
                    {{ proj.latest_delta.baseline_label }} &rarr; {{ proj.latest_delta.current_label }}
                    {% else %}
                    {{ proj.progression|length }} versions |
                    {{ proj.progression[0].version }} &rarr; {{ proj.progression[-1].version }}
                    {% endif %}
                </span>
            </div>
            <div class="project-body" id="project-body-{{ loop.index }}">
                {% set proj_idx = loop.index %}

                <!-- Mini KPI for this project -->
                <div class="mini-kpi-row">
                    <div class="mini-kpi">
                        <div class="label">{% if is_pair %}Baseline{% else %}First Version{% endif %}</div>
                        <div class="value">{{ proj.progression[0].total }}</div>
                        <div class="label">findings</div>
                    </div>
                    <div class="mini-kpi">
                        <div class="label">{% if is_pair %}Current{% else %}Latest Version{% endif %}</div>
                        <div class="value">{{ proj.progression[-1].total }}</div>
                        <div class="label">findings</div>
                    </div>
                    <div class="mini-kpi">
                        <div class="label">Net Change</div>
                        {% set net = proj.progression[-1].total - proj.progression[0].total %}
                        <div class="value" style="color:{% if net < 0 %}#2e7d32{% elif net > 0 %}#c62828{% else %}#757575{% endif %}">
                            {% if net > 0 %}+{% endif %}{{ net }}
                        </div>
                    </div>
                    <div class="mini-kpi">
                        <div class="label">Latest Fixed</div>
                        <div class="value" style="color:#2e7d32">{{ proj.latest_delta.fixed_count }}</div>
                    </div>
                    <div class="mini-kpi">
                        <div class="label">Latest New</div>
                        <div class="value" style="color:#c62828">{{ proj.latest_delta.new_count }}</div>
                    </div>
                </div>

                <!-- Progression Chart -->
                <div class="chart-container">
                    <div class="chart-wrapper">
                        <canvas id="chart-{{ loop.index }}"></canvas>
                    </div>
                </div>

                <!-- Progression Table -->
                <div class="section">
                    <div class="section-header">
                        <h3>{% if is_pair %}Version Comparison{% else %}Version Progression{% endif %}</h3>
                    </div>
                    <table class="data-table progression-table">
                        <thead>
                            <tr>
                                <th>Project</th>
                                <th>Version</th>
                                <th>Date</th>
                                <th class="num">Total</th>
                                <th class="num">Critical</th>
                                <th class="num">High</th>
                                <th class="num">Medium</th>
                                <th class="num">Low</th>
                                <th class="num">Fixed</th>
                                <th class="num">New</th>
                                <th class="num">Components</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for step in proj.progression %}
                            <tr>
                                <td>{{ step.version_project_name or proj.project_name }}</td>
                                <td><strong>{{ step.version }}</strong></td>
                                <td>{{ step.created }}</td>
                                <td class="num">{{ step.total }}</td>
                                <td class="num">{{ step.critical }}</td>
                                <td class="num">{{ step.high }}</td>
                                <td class="num">{{ step.medium }}</td>
                                <td class="num">{{ step.low }}</td>
                                <td class="num {% if step.fixed > 0 %}delta-negative{% else %}delta-zero{% endif %}">
                                    {% if step.fixed > 0 %}-{{ step.fixed }}{% elif loop.index0 == 0 %}&mdash;{% else %}0{% endif %}
                                </td>
                                <td class="num {% if step.new > 0 %}delta-positive{% else %}delta-zero{% endif %}">
                                    {% if step.new > 0 %}+{{ step.new }}{% elif loop.index0 == 0 %}&mdash;{% else %}0{% endif %}
                                </td>
                                <td class="num">{{ step.components }}</td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>

                <!-- Latest Delta: Changes (Fixed | New) — always shown -->
                <div class="section">
                    <div class="section-header">
                        <h3>Changes ({{ proj.latest_delta.baseline_label | default(proj.latest_delta.baseline_version) }} &rarr; {{ proj.latest_delta.current_label | default(proj.latest_delta.current_version) }})</h3>
                        <span class="badge badge-fixed">{{ proj.latest_delta.fixed_count }} fixed</span>
                        <span class="badge badge-new">{{ proj.latest_delta.new_count }} new</span>
                    </div>
                    <div class="changes-summary">
                        <span class="changes-summary-fixed">Fixed: {{ proj.latest_delta.fixed_severity_summary }}</span>
                        <span class="changes-summary-new">New: {{ proj.latest_delta.new_severity_summary }}</span>
                    </div>
                    <div class="changes-two-col">
                        <div class="changes-col">
                            <h4 class="changes-col-title">Fixed in {{ proj.latest_delta.current_label | default(proj.latest_delta.current_version) }}</h4>
                            {% if proj.latest_delta.fixed_findings and proj.latest_delta.fixed_findings|length > 0 %}
                            <table class="data-table" id="fixed-{{ loop.index }}">
                                <thead>
                                    <tr>
                                        <th>ID</th>
                                        <th>Severity</th>
                                        <th>Component</th>
                                        <th class="num">Score</th>
                                        <th>Title</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for row in proj.latest_delta.fixed_findings %}
                                    <tr class="{% if loop.index > 10 %}hidden-row{% endif %}">
                                        <td>{{ row.display_id if row.display_id else '—' }}</td>
                                        <td><span class="severity-badge severity-{{ row.severity }}">{{ row.severity }}</span></td>
                                        <td>{{ row.component_name }}{% if row.component_version %} {{ row.component_version }}{% endif %}</td>
                                        <td class="num">{{ "%.1f"|format(row.risk / 10.0) if row.risk else '—' }}</td>
                                        <td>{{ row.title if row.title else '—' }}</td>
                                    </tr>
                                    {% endfor %}
                                    {% if proj.latest_delta.fixed_findings|length > 10 %}
                                    <tr class="show-more-row">
                                        <td colspan="5">
                                            <button class="show-more-btn" onclick="toggleRows('fixed-{{ loop.index }}')">
                                                Show {{ proj.latest_delta.fixed_findings|length - 10 }} more...
                                            </button>
                                        </td>
                                    </tr>
                                    {% endif %}
                                </tbody>
                            </table>
                            {% else %}
                            <p class="empty-state-inline">No fixed findings.</p>
                            {% endif %}
                        </div>
                        <div class="changes-col">
                            <h4 class="changes-col-title">New in {{ proj.latest_delta.current_label | default(proj.latest_delta.current_version) }}</h4>
                            {% if proj.latest_delta.new_findings and proj.latest_delta.new_findings|length > 0 %}
                            <table class="data-table" id="new-{{ loop.index }}">
                                <thead>
                                    <tr>
                                        <th>ID</th>
                                        <th>Severity</th>
                                        <th>Component</th>
                                        <th class="num">Score</th>
                                        <th>Title</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for row in proj.latest_delta.new_findings %}
                                    <tr class="{% if loop.index > 10 %}hidden-row{% endif %}">
                                        <td>{{ row.display_id if row.display_id else '—' }}</td>
                                        <td><span class="severity-badge severity-{{ row.severity }}">{{ row.severity }}</span></td>
                                        <td>{{ row.component_name }}{% if row.component_version %} {{ row.component_version }}{% endif %}</td>
                                        <td class="num">{{ "%.1f"|format(row.risk / 10.0) if row.risk else '—' }}</td>
                                        <td>{{ row.title if row.title else '—' }}</td>
                                    </tr>
                                    {% endfor %}
                                    {% if proj.latest_delta.new_findings|length > 10 %}
                                    <tr class="show-more-row">
                                        <td colspan="5">
                                            <button class="show-more-btn" onclick="toggleRows('new-{{ loop.index }}')">
                                                Show {{ proj.latest_delta.new_findings|length - 10 }} more...
                                            </button>
                                        </td>
                                    </tr>
                                    {% endif %}
                                </tbody>
                            </table>
                            {% else %}
                            <p class="empty-state-inline">No new findings.</p>
                            {% endif %}
                        </div>
                    </div>
                </div>

                <!-- Latest Delta: Component Churn -->
                {% if proj.latest_delta.component_churn and proj.latest_delta.component_churn|length > 0 %}
                <div class="section">
                    <div class="section-header">
                        <h3>Component Changes ({{ proj.latest_delta.baseline_label | default(proj.latest_delta.baseline_version) }} &rarr; {{ proj.latest_delta.current_label | default(proj.latest_delta.current_version) }})</h3>
                        <span class="badge badge-churn">{{ proj.latest_delta.component_churn|length }} changes</span>
                    </div>
                    <table class="data-table" id="churn-{{ loop.index }}">
                        <thead>
                            <tr>
                                <th>Change</th>
                                <th>Component</th>
                                <th>Old Version</th>
                                <th>New Version</th>
                                <th>Findings Impact</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for row in proj.latest_delta.component_churn %}
                            <tr class="{% if loop.index > 15 %}hidden-row{% endif %}">
                                <td><span class="change-badge change-{{ row.change_type }}">
                                    {% if row.change_type == 'added' %}+ Added{% elif row.change_type == 'removed' %}- Removed{% else %}&uarr; Updated{% endif %}
                                </span></td>
                                <td>{{ row.name }}</td>
                                <td>{{ row.version_baseline | default('-', true) }}</td>
                                <td>{{ row.version_current | default('-', true) }}</td>
                                <td>{{ row.findings_impact | default(0) }}</td>
                            </tr>
                            {% endfor %}
                            {% if proj.latest_delta.component_churn|length > 15 %}
                            <tr class="show-more-row">
                                <td colspan="5">
                                    <button class="show-more-btn" onclick="toggleRows('churn-{{ loop.index }}')">
                                        Show {{ proj.latest_delta.component_churn|length - 15 }} more...
                                    </button>
                                </td>
                            </tr>
                            {% endif %}
                        </tbody>
                    </table>
                </div>
                {% endif %}

                <!-- Version changelog: all version-to-version changes (skip for 2-version pairs — already shown above) -->
                {% if proj.progression|length > 2 %}
                <div class="section changelog-section">
                    <div class="section-header">
                        <h3>Version changelog</h3>
                    </div>
                    {% for step in proj.progression %}
                    {% if step.from_version %}
                    {% set step_churn_list = step.component_churn | default([]) %}
                    <div class="changelog-entry{% if loop.index == 2 %} expanded{% endif %}" id="changelog-{{ proj_idx }}-{{ loop.index }}">
                        <div class="changelog-entry-header" onclick="toggleChangelog('changelog-{{ proj_idx }}-{{ loop.index }}')">
                            <span class="changelog-version">{{ step.from_version }} &rarr; {{ step.version }}</span>
                            <span class="badge badge-fixed">{{ step.fixed }} fixed</span>
                            <span class="badge badge-new">{{ step.new }} new</span>
                            <span class="badge badge-churn">{{ step_churn_list | length }} component{{ 's' if step_churn_list | length != 1 else '' }}</span>
                        </div>
                        <div class="changelog-entry-body">
                            <div class="changes-summary" style="margin-bottom: 12px;">
                                <span class="changes-summary-fixed">Fixed: {{ step.fixed_severity_summary }}</span>
                                <span class="changes-summary-new">New: {{ step.new_severity_summary }}</span>
                            </div>
                            <div class="changes-two-col">
                                <div class="changes-col">
                                    <h4 class="changes-col-title">Fixed</h4>
                                    {% if step.fixed_findings and step.fixed_findings|length > 0 %}
                                    <table class="data-table">
                                        <thead>
                                            <tr><th>ID</th><th>Severity</th><th>Component</th><th class="num">Score</th><th>Title</th></tr>
                                        </thead>
                                        <tbody>
                                            {% for row in step.fixed_findings %}
                                            <tr><td>{{ row.display_id if row.display_id else '—' }}</td><td><span class="severity-badge severity-{{ row.severity }}">{{ row.severity }}</span></td><td>{{ row.component_name }}{% if row.component_version %} {{ row.component_version }}{% endif %}</td><td class="num">{{ "%.1f"|format(row.risk / 10.0) if row.risk else '—' }}</td><td>{{ row.title if row.title else '—' }}</td></tr>
                                            {% endfor %}
                                        </tbody>
                                    </table>
                                    {% else %}
                                    <p class="empty-state-inline">No fixed findings.</p>
                                    {% endif %}
                                </div>
                                <div class="changes-col">
                                    <h4 class="changes-col-title">New</h4>
                                    {% if step.new_findings and step.new_findings|length > 0 %}
                                    <table class="data-table">
                                        <thead>
                                            <tr><th>ID</th><th>Severity</th><th>Component</th><th class="num">Score</th><th>Title</th></tr>
                                        </thead>
                                        <tbody>
                                            {% for row in step.new_findings %}
                                            <tr><td>{{ row.display_id if row.display_id else '—' }}</td><td><span class="severity-badge severity-{{ row.severity }}">{{ row.severity }}</span></td><td>{{ row.component_name }}{% if row.component_version %} {{ row.component_version }}{% endif %}</td><td class="num">{{ "%.1f"|format(row.risk / 10.0) if row.risk else '—' }}</td><td>{{ row.title if row.title else '—' }}</td></tr>
                                            {% endfor %}
                                        </tbody>
                                    </table>
                                    {% else %}
                                    <p class="empty-state-inline">No new findings.</p>
                                    {% endif %}
                                </div>
                            </div>
                            <div class="changelog-components" style="margin-top: 16px;">
                                <h4 class="changes-col-title">Component changes</h4>
                                {% if step_churn_list and step_churn_list|length > 0 %}
                                <table class="data-table">
                                    <thead>
                                        <tr>
                                            <th>Change</th>
                                            <th>Component</th>
                                            <th>Previous Version</th>
                                            <th>Current Version</th>
                                            <th class="num">Findings Impact</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        {% for row in step_churn_list %}
                                        <tr>
                                            <td><span class="change-badge change-{{ row.change_type | default('unchanged') }}">{% if row.change_type == 'added' %}+ Added{% elif row.change_type == 'removed' %}- Removed{% elif row.change_type == 'updated' %}&uarr; Updated{% else %}—{% endif %}</span></td>
                                            <td>{{ row.name | default('—') }}</td>
                                            <td>{{ row.version_baseline | default('—', true) }}</td>
                                            <td>{{ row.version_current | default('—', true) }}</td>
                                            <td class="num">{{ row.findings_impact | default(0) }}</td>
                                        </tr>
                                        {% endfor %}
                                    </tbody>
                                </table>
                                {% else %}
                                <p class="empty-state-inline">No component changes.</p>
                                {% endif %}
                            </div>
                        </div>
                    </div>
                    {% endif %}
                    {% endfor %}
                </div>
                {% endif %}

            </div>
        </div>
        {% endfor %}

        {% else %}
        <!-- No data state -->
        <div class="empty-state">
            <p><strong>No comparison data available.</strong></p>
            <p>Ensure projects have at least two scanned versions, or scope with <code>--project</code> or <code>--folder</code>.</p>
        </div>
        {% endif %}

        <div class="footer">
            <p>Generated by Finite State Reporting Kit</p>
            <p style="font-size: 0.8em; margin-top: 4px;">Copyright &copy; 2026 Finite State, Inc.</p>
        </div>
    </div>

    <script>
    // Toggle hidden rows in a table
    function toggleRows(tableId) {
        const table = document.getElementById(tableId);
        if (!table) return;
        const hiddenRows = table.querySelectorAll('.hidden-row');
        const showMoreRow = table.querySelector('.show-more-row');
        hiddenRows.forEach(row => row.classList.toggle('hidden-row'));
        if (showMoreRow) showMoreRow.style.display = 'none';
    }

    // Toggle project body visibility
    function toggleProject(bodyId) {
        const body = document.getElementById(bodyId);
        if (!body) return;
        body.style.display = body.style.display === 'none' ? 'block' : 'none';
    }

    // Toggle changelog entry expand/collapse
    function toggleChangelog(entryId) {
        const el = document.getElementById(entryId);
        if (!el) return;
        el.classList.toggle('expanded');
    }

    // Build progression charts for each project
    {% if transform_result and transform_result.projects %}
    {% for proj in transform_result.projects %}
    (function() {
        const ctx = document.getElementById('chart-{{ loop.index }}');
        if (!ctx) return;

        const labels = [{% for s in proj.progression %}'{{ s.version }}'{% if not loop.last %}, {% endif %}{% endfor %}];
        const totals = [{% for s in proj.progression %}{{ s.total }}{% if not loop.last %}, {% endif %}{% endfor %}];
        const fixed = [{% for s in proj.progression %}{{ s.fixed }}{% if not loop.last %}, {% endif %}{% endfor %}];
        const newFindings = [{% for s in proj.progression %}{{ s.new }}{% if not loop.last %}, {% endif %}{% endfor %}];

        new Chart(ctx, {
            type: 'line',
            data: {
                labels: labels,
                datasets: [
                    {
                        label: 'Total Findings',
                        data: totals,
                        borderColor: 'rgba(108, 117, 125, 0.8)',
                        backgroundColor: 'rgba(108, 117, 125, 0.15)',
                        borderWidth: 2,
                        pointBackgroundColor: 'rgba(108, 117, 125, 0.95)',
                        pointRadius: 4,
                        pointHoverRadius: 6,
                        fill: true,
                        tension: 0.3,
                    },
                ]
            },
            options: {
                responsive: true,
                maintainAspectRatio: false,
                interaction: { mode: 'index', intersect: false },
                plugins: {
                    legend: {
                        position: 'top',
                        display: true,
                    },
                    title: {
                        display: true,
                        text: '{{ proj.project_name }} — {% if proj.is_pair_comparison %}Version Comparison{% else %}Version Progression{% endif %}',
                        font: { size: 14, weight: '600' },
                        padding: { bottom: 16 }
                    },
                    tooltip: {
                        callbacks: {
                            title: function(items) {
                                if (!items.length) return '';
                                return items[0].label;
                            },
                            label: function(ctx) {
                                const i = ctx.dataIndex;
                                const lines = ['Total: ' + totals[i]];
                                if (i > 0) {
                                    const delta = totals[i] - totals[i-1];
                                    const sign = delta > 0 ? '+' : '';
                                    lines[0] += '  (' + sign + delta + ' net)';
                                    if (fixed[i] > 0 || newFindings[i] > 0) {
                                        lines.push('Fixed: -' + fixed[i] + '   New: +' + newFindings[i]);
                                    }
                                }
                                return lines;
                            }
                        }
                    }
                },
                scales: {
                    x: {
                        grid: { display: false },
                        ticks: {
                            maxRotation: 45,
                            font: { weight: '500' }
                        }
                    },
                    y: {
                        beginAtZero: true,
                        title: { display: true, text: 'Findings Count' },
                        ticks: { precision: 0 },
                        grid: { color: 'rgba(0, 0, 0, 0.06)' },
                    }
                }
            }
        });
    })();
    {% endfor %}
    {% endif %}
    </script>
</body>
</html>
