<!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>
            {% endif %}
            <h1>Version Comparison</h1>
            {% if 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 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 }}">
            <div class="project-header" onclick="toggleProject('project-body-{{ loop.index }}')">
                <h2>{{ proj.project_name }}</h2>
                <span class="project-meta">
                    {{ proj.progression|length }} versions |
                    {{ proj.progression[0].version }} &rarr; {{ proj.progression[-1].version }}
                </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">First Version</div>
                        <div class="value">{{ proj.progression[0].total }}</div>
                        <div class="label">findings</div>
                    </div>
                    <div class="mini-kpi">
                        <div class="label">Latest Version</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>Version Progression</h3>
                    </div>
                    <table class="data-table progression-table">
                        <thead>
                            <tr>
                                <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><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_version }} &rarr; {{ 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_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">Risk</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) 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_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">Risk</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) 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_version }} &rarr; {{ 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>Previous Version</th>
                                <th>Current 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">Risk</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) 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">Risk</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) 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>
        </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 }} — Version Progression',
                        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>
