{# The live activity pane (spec §4.9). Everything is derived from the session NDJSON
   stream and tagged telemetry; live/ is ephemeral + gitignored, never part of the gate.
   No timeline → honest idle / expired state (never a faked or blank pane). #}
{% import "macros.html" as m %}
{% if not live.timeline %}
  {{ m.empty_inline(live.title, live.reason) }}
  <p class="fw-live__telemetry mono fw-noprint">
    live/ is telemetry — ephemeral, gitignored, and never part of the freshness gate (D-LIVE-1: follow-only).
  </p>
{% else %}
<section class="live-pane">
  <div class="live-pane__head">
    <span class="live-dot {% if live.ended %}live-dot--ended{% endif %}" aria-hidden="true"></span>
    <span class="live-chip {% if live.ended %}live-chip--ended{% endif %}">
      {{ 'ended' if live.ended else ('live now' if live.active else 'last session') }}
    </span>
    <span class="live-pane__op">{{ live.op or 'session' }}{% if live.task %} · {{ live.task }}{% endif %}</span>
    <span class="fw-spacer"></span>
    <span class="live-pane__meta">
      <span>last event {{ live.age }} ago</span>
      <span>seq {{ live.last_event.seq }}</span>
      <span>{{ live.event_count }} event{{ '' if live.event_count == 1 else 's' }}</span>
    </span>
  </div>

  <div class="live-pane__body">
    {% if live.ended %}
    <div class="fw-resultbar {{ 'fw-resultbar--ok' if live.result == 'ok' else 'fw-resultbar--warn' }}">
      <span class="fw-resultbar__icon">{{ '✓' if live.result == 'ok' else '!' }}</span>
      <span class="mono">ended {{ live.result }} in {{ live.duration_s }}s{% if live.run_record %} · run-record <b>{{ live.run_record }}</b> (gated){% endif %}</span>
    </div>
    {% endif %}

    {# current step + live progress (both derived from the stream) #}
    <div class="live-pane__step"><span class="mono" aria-hidden="true">▶</span> {{ live.step or 'no named step reported' }}</div>

    <div class="progress {% if live.progress is none %}progress--indeterminate{% endif %}"
         role="progressbar" aria-valuenow="{{ live.progress if live.progress is not none else 0 }}"
         aria-valuemin="0" aria-valuemax="100">
      <div class="progress__fill {% if live.ended and live.result == 'ok' %}progress__fill--done{% endif %}"
           {% if live.progress is not none %}style="width: {{ live.progress }}%"{% endif %}></div>
    </div>
    {% if live.progress is not none %}
    <div class="live-pane__meta"><span>{{ live.progress }}% — {{ m.derived() }} from progress telemetry</span></div>
    {% endif %}

    {% if live.latest_log %}
    <div class="live-pane__latest"><span class="level-badge level-badge--{{ live.latest_log.level_class }}">{{ live.latest_log.level }}</span> {{ live.latest_log.msg }}</div>
    {% endif %}

    <div class="live-pane__timeline">
      <div class="live-pane__timeline-head">session timeline · {{ m.derived() }} telemetry</div>
      <div class="live-pane__events">
        {% for e in live.timeline %}
        <div class="live-pane__event">
          <span class="live-pane__seq">{{ '%03d'|format(e.seq) }}</span>
          <span class="live-pane__ts">{{ e.clock }}</span>
          <span class="live-pane__kind kind--{{ e.kind_class }}">{{ e.kind }}</span>
          <span class="live-pane__text">{{ e.text }}</span>
        </div>
        {% endfor %}
      </div>
    </div>

    <div class="fw-live__controls fw-noprint">
      {% if live.active %}
      <a class="button button-secondary button-sm" href="/p/{{ meta.slug }}/live">Refresh</a>
      <span class="live-pane__op">following live/CURRENT</span>
      {% else %}
      <a class="button button-secondary button-sm" href="/p/{{ meta.slug }}/live">Back to current</a>
      <span class="live-pane__op mono">session {{ live.session }}</span>
      {% endif %}
      <span class="fw-spacer"></span>
      <span class="fw-live__hint mono">follow-only · refresh to advance the stream</span>
    </div>
  </div>
</section>
{% endif %}
