<div>
  <ul>
    {% set total_votes = resblob.FPTP_result.total_votes | default(0) %}
    {% if total_votes == 0 %}
      <li><b>Total ballots:</b> 0</li>
      <li>No first-place votes recorded for this contest.</li>
    {% else %}
    {% set winners = resblob['FPTP_result']['winners'] %}
    {% set winnerstr = ", ".join(winners) %}
    {% set winnerintro = "FPTP Winners (tie)" if winners | length > 1 else "FPTP Winner" %}
    <li><b>{{winnerintro}}</b>:
      {% if FPTP_candnames %}
        {%- for winner in winners -%}
          <span class="color-box" style="background-color: {{ colordict[winner] | default('#ccc') }};"></span>{{ FPTP_candnames[winner] if winner in FPTP_candnames else winner }}{% if not loop.last %}, {% endif %}
        {%- endfor -%}
      {% else %}
        {{ winnerstr }}
      {% endif %}
      {% if winners and resblob.FPTP_result.toppicks %}
        {% set winner_votes = resblob.FPTP_result.toppicks[winners[0]] %}
        with {{ "{:,}".format(winner_votes) }} first-place votes ({{ "%0.1f"|format((winner_votes / resblob.FPTP_result.total_votes) * 100) if resblob.FPTP_result.total_votes > 0 else 0 }}%)
      {% endif %}
    </li>
    {% if winners and resblob.FPTP_result.toppicks %}
      {% set winner_votes = resblob.FPTP_result.toppicks[winners[0]] %}
      {# Build candidate list excluding None for runner-up computation #}
      {% set sorted_candidates = resblob.FPTP_result.toppicks.items() | list %}
      {% set sorted_candidates = sorted_candidates | selectattr('0', 'ne', None) | list %}
      {% set sorted_candidates = sorted_candidates | sort(attribute='1', reverse=true) %}
      {% if sorted_candidates|length > 1 %}
        {% set runner_up = sorted_candidates[1] %}
        <li>Runner-up: <span class="color-box" style="background-color: {{ colordict[runner_up[0]] | default('#ccc') }};"></span>{{ FPTP_candnames[runner_up[0]] if FPTP_candnames and runner_up[0] in FPTP_candnames else runner_up[0] }} with {{ "{:,}".format(runner_up[1]) }} first-place votes ({{ "%0.1f"|format((runner_up[1] / resblob.FPTP_result.total_votes) * 100) if resblob.FPTP_result.total_votes > 0 else 0 }}%)</li>
        <li>Margin of victory: {{ "{:,}".format(winner_votes - runner_up[1]) }} votes ({{ "%0.1f"|format(((winner_votes - runner_up[1]) / resblob.FPTP_result.total_votes) * 100) if resblob.FPTP_result.total_votes > 0 else 0 }} percentage points)</li>
      {% endif %}
    {% endif %}
    <li>First-place votes
    <ul>
    {% for candidate, votes in resblob.FPTP_result.toppicks|dictsort(reverse=true, by='value') %}
    {% if candidate is not none %}
      <li>
        <span class="color-box" style="background-color: {{ colordict[candidate] | default('#ccc') }};"></span>
        {{ FPTP_candnames[candidate] if FPTP_candnames and candidate in FPTP_candnames else candidate }}: {{ "{:,}".format(votes) }} votes
        {% if resblob.FPTP_result.total_votes > 0 %}
        ({{ "%0.1f"|format((votes / resblob.FPTP_result.total_votes) * 100) }} %)
        {% endif %}
      </li>
    {% endif %}
    {% endfor %}
    </ul></li>
    {% set overvotes = resblob.FPTP_result.get('overvote_ballots', resblob.FPTP_result.get('invalid_ballots', 0)) %}
    {% set none_total = resblob.FPTP_result.toppicks.get(None, 0) %}
    {% set blanks = (none_total - overvotes) if (none_total is not none) else 0 %}
    {% if overvotes > 0 %}
      <li><b>Overvotes</b>: {{ "{:,}".format(overvotes) }}{% if resblob.FPTP_result.total_votes > 0 %} ({{ "%0.1f"|format((overvotes / resblob.FPTP_result.total_votes) * 100) }} %){% endif %}</li>
    {% endif %}
    {% if blanks > 0 %}
      <li><b>Blanks</b>: {{ "{:,}".format(blanks) }}{% if resblob.FPTP_result.total_votes > 0 %} ({{ "%0.1f"|format((blanks / resblob.FPTP_result.total_votes) * 100) }} %){% endif %}</li>
    {% endif %}
    <li><b>Total ballots:</b> {{ "{:,}".format(resblob.FPTP_result.total_votes) }}</li>
    {% endif %}
  </ul>
</div>
