{# Shared render macros. State language + cards reuse mgf-brand-web instrument
   classes (.status-chip, .level-badge, .pill, .detail-block, .empty); the small
   app-specific bits (.tstate task-state chip, .derived-tag) live in app.css. #}

{# Sync-state chip — the brand state-chip; CHECKING/SYNCING swap the dot for a spinner. #}
{%- macro sync_chip(state) -%}
<span class="status-chip status-chip--{{ state|lower|replace('_','-') }}">
{%- if state in ('CHECKING', 'SYNCING') %}<span class="spinner"></span>{% else %}<span class="status-chip__dot"></span>{% endif -%}
{{ state }}</span>
{%- endmacro -%}

{# The violet "derived" tag — web-inferred, never a project-reported fact (spec §5.2). #}
{%- macro derived() -%}
<span class="derived-tag" title="inferred by mgf-fed-web — not a project-reported fact">derived</span>
{%- endmacro -%}

{# Task / stage workflow-state chip (done, in_progress, next, blocked, …). #}
{%- macro tstate(state) -%}
<span class="tstate tstate--{{ state }}"><span class="tstate__dot"></span>{{ state|replace('_', ' ') }}</span>
{%- endmacro -%}

{# A work-unit card (In progress / Decisions). `age` is derived (now − started_at). #}
{%- macro task_card(t, age=None) -%}
<article class="card fw-task">
  <header class="fw-task__h">
    <span class="pill">{{ t.id }}</span>
    <span class="fw-task__t">{{ t.title }}</span>
    {% if t.owner %}<span class="pill">{{ t.owner }}</span>{% endif %}
  </header>
  <div class="fw-task__meta">
    {{ tstate(t.state) }}
    {% if t.impact %}<span class="level-badge level-badge--{{ t.impact }}">impact {{ t.impact }}</span>{% endif %}
    {% if t.effort %}<span class="level-badge level-badge--effort">effort {{ t.effort }}</span>{% endif %}
    {% if t.eta %}<span class="pill">eta {{ t.eta }}</span>{% endif %}
    {% if t.stage %}<span class="pill">{{ t.stage }}</span>{% endif %}
    {% if age %}<span class="pill pill--age">started {{ age }} ago</span>{{ derived() }}
    {% elif t.state == 'in_progress' %}<span class="pill">age not reported</span>{% endif %}
  </div>
  {% if t.reason %}<div class="detail-block detail-block--warning"><div class="detail-block__head">⚠ why</div>{{ t.reason }}</div>{% endif %}
  {% if t.evidence %}<p class="evidence">evidence · {{ t.evidence }}</p>{% endif %}
</article>
{%- endmacro -%}

{# Honest-absence block — every absence is an explicit state + reason (spec §1.6). #}
{%- macro absence(view) -%}
{%- set kind = 'unreadable' if view.state == 'UNREADABLE' else ('unknown' if view.state == 'UNKNOWN' else 'no-data') -%}
<div class="empty empty--{{ kind }} fw-absence">
  <div class="empty__icon">{{ '!' if view.state == 'UNREADABLE' else '?' }}</div>
  <p class="empty__title">{{ view.title }}</p>
  <p class="empty__desc">{{ view.reason }}</p>
  <p class="fw-absence__state">{{ sync_chip(view.state) }}{% if view.action %} <span class="muted">{{ view.action }}</span>{% endif %}</p>
  {% if view.errors %}<ul class="fw-absence__errs mono">{% for e in view.errors %}<li>{{ e }}</li>{% endfor %}</ul>{% endif %}
</div>
{%- endmacro -%}

{# An inline "nothing here" empty card (data present but the slice is empty). #}
{%- macro empty_inline(title, reason) -%}
<div class="empty empty--no-data fw-absence">
  <div class="empty__icon">∅</div>
  <p class="empty__title">{{ title }}</p>
  <p class="empty__desc">{{ reason }}</p>
</div>
{%- endmacro -%}
