<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
  @page {
    size: A4;
    margin: 2cm;
    @bottom-right {
      content: "Powered by ResolutionFlow";
      font-size: 8pt;
      color: #999;
    }
    @bottom-left {
      content: "Generated {{ generated_at }}";
      font-size: 8pt;
      color: #999;
    }
  }

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

  body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    color: #1a1a2e;
    background: #ffffff;
    font-size: 10pt;
    line-height: 1.5;
  }

  /* --- Header --- */
  .header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 24px;
    padding-bottom: 16px;
    border-bottom: 3px solid #06b6d4;
  }

  .header-left {
    flex: 1;
  }

  .report-type {
    font-size: 8pt;
    text-transform: uppercase;
    letter-spacing: 0.1em;
    color: #06b6d4;
    font-weight: 600;
    margin-bottom: 4px;
  }

  .flow-title {
    font-size: 18pt;
    font-weight: 700;
    color: #1a1a2e;
    margin-bottom: 4px;
  }

  .company-name {
    font-size: 10pt;
    color: #666;
  }

  .header-logo {
    flex-shrink: 0;
    margin-left: 24px;
  }

  .header-logo img {
    max-width: 120px;
    max-height: 60px;
    object-fit: contain;
  }

  /* --- Metadata Grid --- */
  .metadata-grid {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 12px;
    margin-bottom: 24px;
    padding: 16px;
    background: #f8fafc;
    border-radius: 8px;
    border: 1px solid #e2e8f0;
  }

  .meta-item {
    display: flex;
    flex-direction: column;
  }

  .meta-label {
    font-size: 7pt;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: #94a3b8;
    font-weight: 600;
    margin-bottom: 2px;
  }

  .meta-value {
    font-size: 10pt;
    color: #1a1a2e;
    font-weight: 500;
  }

  .outcome-resolved {
    color: #059669;
    font-weight: 700;
  }

  .outcome-escalated {
    color: #d97706;
    font-weight: 700;
  }

  .outcome-workaround {
    color: #2563eb;
    font-weight: 700;
  }

  .outcome-unresolved {
    color: #dc2626;
    font-weight: 700;
  }

  /* --- Section Headers --- */
  .section-title {
    font-size: 12pt;
    font-weight: 700;
    color: #1a1a2e;
    margin-bottom: 12px;
    padding-bottom: 6px;
    border-bottom: 2px solid #06b6d4;
  }

  /* --- Summary --- */
  .summary {
    margin-bottom: 24px;
  }

  .summary-text {
    font-size: 10pt;
    color: #334155;
    line-height: 1.6;
    white-space: pre-wrap;
  }

  /* --- Troubleshooting Path Timeline --- */
  .timeline {
    margin-bottom: 24px;
  }

  .timeline-step {
    display: flex;
    align-items: flex-start;
    margin-bottom: 8px;
    position: relative;
    padding-left: 24px;
  }

  .timeline-step::before {
    content: "";
    position: absolute;
    left: 5px;
    top: 6px;
    width: 10px;
    height: 10px;
    background: #06b6d4;
    border-radius: 50%;
  }

  .timeline-step::after {
    content: "";
    position: absolute;
    left: 9px;
    top: 18px;
    width: 2px;
    height: calc(100% + 0px);
    background: #e2e8f0;
  }

  .timeline-step:last-child::after {
    display: none;
  }

  .step-content {
    flex: 1;
    padding-bottom: 8px;
  }

  .step-title {
    font-size: 10pt;
    font-weight: 600;
    color: #1a1a2e;
  }

  .step-decision {
    font-size: 9pt;
    color: #06b6d4;
    font-weight: 500;
    margin-top: 2px;
  }

  .step-notes {
    font-size: 9pt;
    color: #64748b;
    font-style: italic;
    margin-top: 2px;
  }

  .step-duration {
    font-size: 8pt;
    color: #94a3b8;
    margin-top: 2px;
  }

  /* --- Supporting Data --- */
  .supporting-data {
    margin-bottom: 24px;
  }

  .supporting-item {
    margin-bottom: 16px;
    break-inside: avoid;
    border: 1px solid #e2e8f0;
    border-radius: 8px;
    overflow: hidden;
  }

  .supporting-item-label {
    font-size: 9pt;
    font-weight: 600;
    color: #1a1a2e;
    padding: 8px 12px;
    background: #f8fafc;
    border-bottom: 1px solid #e2e8f0;
  }

  .supporting-item-content {
    padding: 12px;
  }

  .code-block {
    font-family: "JetBrains Mono", "Fira Code", "Consolas", monospace;
    font-size: 8pt;
    line-height: 1.5;
    background: #f1f5f9;
    padding: 12px;
    border-radius: 4px;
    white-space: pre-wrap;
    word-break: break-all;
    color: #334155;
  }

  .screenshot-img {
    max-width: 100%;
    height: auto;
    border-radius: 4px;
  }
</style>
</head>
<body>

  <!-- Header -->
  <div class="header">
    <div class="header-left">
      <div class="report-type">{{ report_type }}</div>
      <div class="flow-title">{{ flow_title }}</div>
      {% if company_name %}
      <div class="company-name">{{ company_name }}</div>
      {% endif %}
    </div>
    {% if logo_data %}
    <div class="header-logo">
      <img src="data:{{ logo_content_type }};base64,{{ logo_data }}" alt="Logo">
    </div>
    {% endif %}
  </div>

  <!-- Metadata Grid -->
  <div class="metadata-grid">
    <div class="meta-item">
      <span class="meta-label">Engineer</span>
      <span class="meta-value">{{ engineer_name or "N/A" }}</span>
    </div>
    <div class="meta-item">
      <span class="meta-label">Client</span>
      <span class="meta-value">{{ client_name or "N/A" }}</span>
    </div>
    <div class="meta-item">
      <span class="meta-label">Ticket #</span>
      <span class="meta-value">{{ ticket_number or "N/A" }}</span>
    </div>
    <div class="meta-item">
      <span class="meta-label">Date</span>
      <span class="meta-value">{{ session_date }}</span>
    </div>
    <div class="meta-item">
      <span class="meta-label">Duration</span>
      <span class="meta-value">{{ duration }}</span>
    </div>
    <div class="meta-item">
      <span class="meta-label">Outcome</span>
      <span class="meta-value {{ outcome_class }}">{{ outcome_display }}</span>
    </div>
  </div>

  <!-- Summary -->
  {% if summary %}
  <div class="summary">
    <div class="section-title">Summary</div>
    <div class="summary-text">{{ summary }}</div>
  </div>
  {% endif %}

  <!-- Troubleshooting Path -->
  {% if steps %}
  <div class="timeline">
    <div class="section-title">Troubleshooting Path</div>
    {% for step in steps %}
    <div class="timeline-step">
      <div class="step-content">
        <div class="step-title">{{ loop.index }}. {{ step.title }}</div>
        {% if step.decision %}
        <div class="step-decision">{{ step.decision }}</div>
        {% endif %}
        {% if step.notes %}
        <div class="step-notes">{{ step.notes }}</div>
        {% endif %}
        {% if step.duration %}
        <div class="step-duration">{{ step.duration }}</div>
        {% endif %}
      </div>
    </div>
    {% endfor %}
  </div>
  {% endif %}

  <!-- Supporting Data -->
  {% if supporting_data %}
  <div class="supporting-data">
    <div class="section-title">Supporting Data</div>
    {% for item in supporting_data %}
    <div class="supporting-item">
      <div class="supporting-item-label">{{ item.label }}</div>
      <div class="supporting-item-content">
        {% if item.data_type == "screenshot" %}
        <img class="screenshot-img" src="data:{{ item.content_type or 'image/png' }};base64,{{ item.content }}" alt="{{ item.label }}">
        {% else %}
        <div class="code-block">{{ item.content }}</div>
        {% endif %}
      </div>
    </div>
    {% endfor %}
  </div>
  {% endif %}

  <!-- File Upload Evidence -->
  {% if uploads %}
  <div class="supporting-data">
    <div class="section-title">Evidence</div>
    <div style="display: flex; flex-wrap: wrap; gap: 12px;">
      {% for upload in uploads %}
      <div style="margin-bottom: 8px;">
        {% if upload.is_image %}
        <img src="{{ upload.url }}" alt="{{ upload.filename }}" style="max-width: 400px; border-radius: 8px; display: block; margin-bottom: 4px;" />
        <div style="font-size: 0.8em; color: #666;">{{ upload.filename }}</div>
        {% else %}
        <a href="{{ upload.url }}" style="color: #06b6d4;">{{ upload.filename }}</a>
        {% endif %}
      </div>
      {% endfor %}
    </div>
  </div>
  {% endif %}

</body>
</html>
