<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    {% if domain %}<meta name="fs-domain" content="{{ domain }}">{% endif %}
    <title>CRA Compliance Report - Finite State Report</title>
    <script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.1/dist/chart.umd.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels@2"></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; }
        .company-logo { max-height: 50px; width: auto; margin: 0 auto; display: block; }
        .header h1 { color: #2c3e50; font-size: 2.5em; 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; }

        .alert-banner {
            background-color: #fef2f2; border: 1px solid #fecaca; border-left: 4px solid #dc2626;
            border-radius: 8px; padding: 16px 20px; margin-bottom: 30px;
            color: #991b1b; font-size: 1em; line-height: 1.5;
        }
        .alert-banner strong { color: #7f1d1d; }

        .kpi-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 15px; margin-bottom: 30px; }
        .kpi-card {
            background: white; border: 1px solid #dee2e6; border-radius: 8px;
            padding: 20px; text-align: center;
        }
        .kpi-card .kpi-title { font-size: 0.85em; color: #6c757d; text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 8px; }
        .kpi-card .kpi-value { font-size: 2.2em; font-weight: 700; color: #2c3e50; line-height: 1; }

        .table-container { background-color: white; border-radius: 8px; padding: 20px; margin-bottom: 30px; border: 1px solid #dee2e6; overflow-x: auto; }
        table { width: 100%; border-collapse: collapse; margin-top: 15px; }
        th, td { padding: 10px 12px; text-align: left; border-bottom: 1px solid #dee2e6; }
        th { background-color: #f8f9fa; font-weight: 600; color: #495057; position: sticky; top: 0; z-index: 10; }
        tr:hover { background-color: #f8f9fa; }
        td.num { text-align: right; font-variant-numeric: tabular-nums; }

        .severity-badge { display: inline-block; padding: 2px 10px; border-radius: 12px; font-size: 0.8em; font-weight: 600; color: white; }
        .severity-critical { background-color: #dc2626; }
        .severity-high { background-color: #ea580c; }
        .severity-medium { background-color: #eab308; color: #333; }
        .severity-low { background-color: #2563eb; }

        .trigger-badge { display: inline-block; padding: 2px 10px; border-radius: 12px; font-size: 0.8em; font-weight: 600; color: white; }
        .trigger-kev { background-color: #991b1b; }
        .trigger-exploit { background-color: #ea580c; }

        .footer { text-align: center; margin-top: 40px; padding-top: 20px; border-top: 1px solid #e9ecef; color: #6c757d; font-size: 0.9em; }
        .finite-state-logo { color: #007bff; font-weight: bold; }

        @media print {
            body { background-color: white; }
            .container { max-width: none; }
        }
    </style>
</head>
<body>
    <span id="fsConnectionBadge"></span>
    <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/v9dNDjCYPdgd7cLRnfZsdgg2N0KdmF3d3d3dxe1ckut7YzCmNeNeb6e8xzeMe+4ExETgP/zzTe0okPn0ieRlhm5HLcm95m+4CUTejk7ZpUOkKuLQjfWxDt/OGD5Fv7gqVqAE5BsHbmvgoPlNiiFzwOBXFVRvARQ5yy/z8vrSLV6wvShHkBw7CKEmIZ7AEEv+dcAV0ep+jFyaUGg5mlyLgLJ520Az+EZfNTcISq0nDxaDSiwjLwneJbFY1WBwivII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oBsBiLk2I6tNAOI1BicVGLb008DuWG9RV0oqasjqcyxMuTd7CCLkWzXD//BS6oW/94CEbS3Pg7qi7eJwCT93iHAPhPuHcWx1dF7z+RLcx6bRqlnrqqinGaW5oYozyKstInug4w3eu9ypoHaLLwJb5D3/iADFjNPd10L2dbh9CFGP+rEjdupInLy2nUngp3wP/Z7iSiaO+SV6MF1J2ZAPsxJtXUFwVlgHcmwyqujWazTJ367dp6zib3FnY/7dHbxbZXzHEV37AnCuCiruTi/zm/9g8tnTfblrGDrDGRAl8ThBs5HWPgY8x0bDxP35ITV6015+SLai8QkDVLevEnQPXMgZadvkfPYUY22Bz1HFZj3iNRhFfL05MpXr1IKdBy07hvaFxL1IAAAAAAAAAAAAAAAAA="
                     alt="Finite State Logo" class="company-logo">
            </div>
            {% endif %}
            <h1>CRA Compliance Report</h1>
            <div class="subtitle">
                EU Cyber Resilience Act &mdash; Exploited Vulnerability Assessment
            </div>
        </div>

        <!-- Metadata -->
        <div class="metadata">
            <strong>Report:</strong> CRA Compliance |
            <strong>Generated:</strong> {{ generated_at }} |
            <strong>Scope:</strong> {{ scope_label | default('All Projects') }}
        </div>

        <!-- Alert Banner -->
        {% if summary.total_count | default(0) > 0 %}
        <div class="alert-banner">
            <strong>&#9888; CRA Notification Obligation:</strong> {{ summary.total_count }} actively exploited vulnerabilit{{ 'y' if summary.total_count == 1 else 'ies' }} detected.
            EU CRA Article 14 requires notification to ENISA within 24 hours.
        </div>
        {% endif %}

        <!-- KPI Cards -->
        <div class="kpi-row">
            <div class="kpi-card">
                <div class="kpi-title">Total Findings</div>
                <div class="kpi-value" {% if summary.total_count | default(0) > 0 %}style="color: #dc2626;"{% endif %}>{{ summary.total_count | default(0) }}</div>
            </div>
            <div class="kpi-card">
                <div class="kpi-title">KEV Listed</div>
                <div class="kpi-value" {% if summary.kev_count | default(0) > 0 %}style="color: #dc2626;"{% endif %}>{{ summary.kev_count | default(0) }}</div>
            </div>
            <div class="kpi-card">
                <div class="kpi-title">Known Exploits</div>
                <div class="kpi-value" {% if summary.known_exploit_count | default(0) > 0 %}style="color: #ea580c;"{% endif %}>{{ summary.known_exploit_count | default(0) }}</div>
            </div>
            <div class="kpi-card">
                <div class="kpi-title">Critical</div>
                <div class="kpi-value" {% if summary.critical_count | default(0) > 0 %}style="color: #7f1d1d;"{% endif %}>{{ summary.critical_count | default(0) }}</div>
            </div>
            <div class="kpi-card">
                <div class="kpi-title">High</div>
                <div class="kpi-value" {% if summary.high_count | default(0) > 0 %}style="color: #ea580c;"{% endif %}>{{ summary.high_count | default(0) }}</div>
            </div>
            <div class="kpi-card">
                <div class="kpi-title">Open</div>
                <div class="kpi-value">{{ summary.open_count | default(0) }}</div>
            </div>
            <div class="kpi-card">
                <div class="kpi-title">Triaged</div>
                <div class="kpi-value">{{ summary.triaged_count | default(0) }}</div>
            </div>
        </div>

        <!-- Affected Projects (primary view) -->
        {% if project_dossiers and project_dossiers | length > 0 %}
        <div class="table-container">
            <h2>Affected Projects</h2>
            <p style="font-size:0.9em;color:#6c757d;margin-bottom:16px;">
                {{ project_dossiers | length }} project(s) contain actively exploited vulnerabilities requiring CRA notification assessment.
            </p>
            {% for dossier in project_dossiers %}
            <details style="border:1px solid #dee2e6;border-radius:6px;margin-bottom:10px;background:#fff;" {% if loop.index <= 3 %}open{% endif %}>
                <summary style="padding:12px 16px;cursor:pointer;display:flex;align-items:center;gap:12px;list-style:none;outline:none;">
                    <span style="font-weight:600;font-size:1em;">{{ dossier.project_name }}</span>
                    <span style="margin-left:auto;display:flex;gap:8px;flex-wrap:wrap;align-items:center;">
                        {% if dossier.critical_count > 0 %}
                        <span style="background:#dc2626;color:#fff;padding:1px 8px;border-radius:10px;font-size:0.75em;font-weight:600;">{{ dossier.critical_count }} Critical</span>
                        {% endif %}
                        {% if dossier.high_count > 0 %}
                        <span style="background:#ea580c;color:#fff;padding:1px 8px;border-radius:10px;font-size:0.75em;font-weight:600;">{{ dossier.high_count }} High</span>
                        {% endif %}
                        {% if dossier.kev_count > 0 %}
                        <span style="background:#991b1b;color:#fff;padding:1px 8px;border-radius:10px;font-size:0.75em;font-weight:600;">{{ dossier.kev_count }} KEV</span>
                        {% endif %}
                        {% if dossier.exploit_count > 0 %}
                        <span style="background:#7c3aed;color:#fff;padding:1px 8px;border-radius:10px;font-size:0.75em;font-weight:600;">{{ dossier.exploit_count }} Exploit</span>
                        {% endif %}
                        <span style="font-size:0.8em;color:#6c757d;">{{ dossier.finding_count }} finding(s)</span>
                    </span>
                </summary>
                <div style="padding:0 16px 14px;">
                    {% if dossier.top_cves %}
                    <table style="width:100%;border-collapse:collapse;font-size:0.85em;margin-top:8px;">
                        <thead>
                            <tr style="background:#f8f9fa;">
                                <th style="padding:6px 8px;text-align:left;border-bottom:1px solid #dee2e6;">CVE ID</th>
                                <th style="padding:6px 8px;text-align:left;border-bottom:1px solid #dee2e6;">Severity</th>
                                <th style="padding:6px 8px;text-align:right;border-bottom:1px solid #dee2e6;">CVSS</th>
                                <th style="padding:6px 8px;text-align:left;border-bottom:1px solid #dee2e6;">Component</th>
                                <th style="padding:6px 8px;text-align:left;border-bottom:1px solid #dee2e6;">CRA Trigger</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for cve in dossier.top_cves %}
                            <tr>
                                <td style="padding:5px 8px;border-bottom:1px solid #f1f3f4;"><code>{{ cve.cve_id }}</code></td>
                                <td style="padding:5px 8px;border-bottom:1px solid #f1f3f4;">
                                    <span class="severity-badge {% if cve.severity == 'CRITICAL' %}severity-critical{% elif cve.severity == 'HIGH' %}severity-high{% elif cve.severity == 'MEDIUM' %}severity-medium{% elif cve.severity == 'LOW' %}severity-low{% endif %}">{{ cve.severity }}</span>
                                </td>
                                <td style="padding:5px 8px;border-bottom:1px solid #f1f3f4;text-align:right;">{{ "%.1f"|format(cve.cvss_score) if cve.cvss_score else "—" }}</td>
                                <td style="padding:5px 8px;border-bottom:1px solid #f1f3f4;">{{ cve.component }}</td>
                                <td style="padding:5px 8px;border-bottom:1px solid #f1f3f4;">
                                    <span class="trigger-badge {% if cve.cra_trigger == 'KEV' %}trigger-kev{% else %}trigger-exploit{% endif %}">{{ cve.cra_trigger }}</span>
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                    {% if dossier.finding_count > 5 %}
                    <p style="font-size:0.78em;color:#6c757d;margin-top:6px;">Showing top 5 of {{ dossier.finding_count }} findings. See full findings table below for details.</p>
                    {% endif %}
                    {% endif %}
                </div>
            </details>
            {% endfor %}
        </div>
        {% endif %}

        <!-- Full Findings Table (secondary — detail reference) -->
        {% if cra_findings and cra_findings | length > 0 %}
        <div class="table-container">
            <h2>All CRA-Relevant Findings</h2>
            <table>
                <thead>
                    <tr>
                        <th>CVE ID</th>
                        <th>Severity</th>
                        <th class="num">CVSS</th>
                        <th>Component</th>
                        <th>Project</th>
                        <th>CRA Trigger</th>
                        <th>Status</th>
                        <th class="num">EPSS Score</th>
                    </tr>
                </thead>
                <tbody>
                    {% for row in cra_findings %}
                    <tr>
                        <td><code>{{ row.cve_id }}</code></td>
                        <td>
                            <span class="severity-badge {% if row.severity == 'CRITICAL' %}severity-critical{% elif row.severity == 'HIGH' %}severity-high{% elif row.severity == 'MEDIUM' %}severity-medium{% elif row.severity == 'LOW' %}severity-low{% endif %}">{{ row.severity }}</span>
                        </td>
                        <td class="num">{{ "%.1f"|format(row.cvss_score) if row.cvss_score else "—" }}</td>
                        <td>{{ row.component }}{% if row.component_version %} {{ row.component_version }}{% endif %}</td>
                        <td>{{ row.project }}{% if row.project_version %} ({{ row.project_version }}){% endif %}</td>
                        <td>
                            <span class="trigger-badge {% if row.cra_trigger == 'KEV' %}trigger-kev{% else %}trigger-exploit{% endif %}">{{ row.cra_trigger }}</span>
                        </td>
                        <td>{{ row.status }}</td>
                        <td class="num">{% if row.epss_score is not none %}{{ '%.4f' | format(row.epss_score) }}{% else %}&mdash;{% endif %}</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
        {% endif %}

        <div class="footer">
            <p>Generated by <span class="finite-state-logo">Finite State</span> | {{ generated_at }}</p>
            <p style="font-size: 0.8em; margin-top: 4px;">Copyright &copy; 2026 Finite State, Inc.</p>
        </div>
    </div>

    {% include '_action_buttons.html' %}
</body>
</html>
