<div class="monitor-header">
  <div class="dispatcher-status {% if dispatcher.alive %}alive{% else %}dead{% endif %}">
    <span class="dot"></span>
    dispatcher
    {% if dispatcher.alive %}
      <span class="muted">· il y a {{ dispatcher.age_s }}s</span>
    {% elif dispatcher.age_s is not none %}
      <span class="muted">· last seen {{ dispatcher.age_s }}s ago</span>
    {% else %}
      <span class="muted">· non démarré</span>
    {% endif %}
  </div>
</div>

{% if orchestrator %}
<div class="worker {% if not orchestrator.online %}offline{% endif %}" style="margin-bottom:0.75rem">
  <div class="hdr">
    <b>{{ orchestrator.role }}</b>
    <span class="gpu">orchestrateur</span>
    <span class="state">{% if orchestrator.online %}online{% else %}offline{% endif %}</span>
  </div>
  {% if orchestrator.online %}
    <div class="bar">
      <span>CPU</span>
      <span style="font-size:0.8rem;color:var(--accent)">{{ orchestrator.cpu_load or '?' }}</span>
    </div>
    <div class="bar">
      <span>RAM</span>
      <progress value="{{ orchestrator.ram_used_mib or 0 }}" max="{{ orchestrator.ram_total_mib or 1 }}"></progress>
      <small>{{ orchestrator.ram_used_mib or '?' }} / {{ orchestrator.ram_total_mib or '?' }} MiB</small>
    </div>
    <div class="worker-meta">
      <span class="tag muted">SSD {{ orchestrator.ssd_avail }} dispo</span>
      <span class="tag muted">{{ orchestrator.ssd_used_pct }} utilise</span>
    </div>
  {% else %}
    <div class="err">{{ orchestrator.error or "unreachable" }}</div>
  {% endif %}
</div>
{% endif %}

<div class="worker-grid">
  {% for w in workers %}
    <div class="worker {% if not w.online %}offline{% endif %}">
      <div class="hdr">
        <b>{{ w.host }}</b>
        <span class="gpu">{{ w.gpu }}</span>
        <span class="state">{% if w.online %}online{% else %}offline{% endif %}</span>
      </div>
      {% if w.online %}
        <div class="bar">
          <span>VRAM</span>
          <progress value="{{ w.vram_used_mib or 0 }}" max="{{ w.vram_total_mib or 1 }}"></progress>
          <small>{{ w.vram_used_mib }} / {{ w.vram_total_mib }} MiB</small>
        </div>
        <div class="bar">
          <span>GPU</span>
          <progress value="{{ w.gpu_util_pct or 0 }}" max="100"></progress>
          <small>{{ w.gpu_util_pct }}%</small>
        </div>
        <div class="worker-meta">
          {% if w.gpu_temp_c is not none %}<span class="tag {% if w.gpu_temp_c > 80 %}warn{% endif %}">{{ w.gpu_temp_c }}°C</span>{% endif %}
          {% if w.gpu_power_w is not none %}<span class="tag">{{ w.gpu_power_w }}W</span>{% endif %}
          <span class="tag muted">/ {{ w.disk_avail }} dispo</span>
          <span class="tag muted">{{ w.disk_used_pct }} utilisé</span>
        </div>
      {% else %}
        <div class="err">{{ w.error or "unreachable" }}</div>
      {% endif %}
    </div>
  {% endfor %}
</div>
