{# Jinja2 template for pairwise summary only (no table) #}
{% if colordict %}
<style type="text/css">
  {% for cand, color in colordict.items() %}
  .color-{{ cand | escape_css }} { background-color: {{ color }}; }
  {% endfor %}
</style>
{% endif %}

{% if summary_data and wltdict %}
<div class="pairwise-summary">
  <ul>
    {% set candidate_list = wltdict.items() | list %}
    {% if candidate_list %}
      <!-- Winner or Copeland tie -->
      {% if is_copeland_tie %}
        <li><b>No Condorcet winner</b>: Copeland tie between
        {%- if copewinners and candnames -%}
          {%- for winner_token in copewinners -%}
            <span class="color-box" style="background-color: {{ colordict[winner_token] | default('#ccc') }};"></span>{{ candnames[winner_token] if winner_token in candnames else winner_token }}{% if not loop.last %}, {% endif %}
          {%- endfor -%}
        {%- else -%}
          {{ copewinnerstring }}
        {%- endif -%}
        </li>
      {% else %}
        {% set winner_token = candidate_list[0][0] %}
        {% set winner_record = candidate_list[0][1] %}
        {% set winner_name = candnames[winner_token] if candnames and winner_token in candnames else winner_token %}

        <!-- Find head-to-head between winner and runner-up for winner line -->
        {% set winner_h2h_text = "" %}
        {% if candidate_list | length > 1 %}
          {% set runner_up_token = candidate_list[1][0] %}
          {% set h2h_found = false %}
          {% for victory in summary_data['victory_data'] %}
            {% if not h2h_found and not victory.get('is_tie', False) %}
              {% if (victory['winner'] == winner_token and victory['loser'] == runner_up_token) or
                    (victory['winner'] == runner_up_token and victory['loser'] == winner_token) %}
                {% set margin = victory['winner_votes'] - victory['loser_votes'] %}
                {% set winner_h2h_text = " (beats runner-up " + "{:,}".format(victory['winner_votes']) + "-" + "{:,}".format(victory['loser_votes']) + "; margin: " + "{:,}".format(margin) + ")" %}
                {% set h2h_found = true %}
              {% endif %}
            {% endif %}
          {% endfor %}
        {% endif %}

        <li><b>Winner</b>: <span class="color-box" style="background-color: {{ colordict[winner_token] | default('#ccc') }};"></span>{{ winner_name }}{{ winner_h2h_text }}</li>
      {% endif %}

      <!-- Runner-up -->
      {% if candidate_list | length > 1 %}
        {% set runner_up_token = candidate_list[1][0] %}
        {% set runner_up_record = candidate_list[1][1] %}
        {% set runner_up_name = candnames[runner_up_token] if candnames and runner_up_token in candnames else runner_up_token %}
        {% if is_copeland_tie %}
          <li>Runner-up: <span class="color-box" style="background-color: {{ colordict[runner_up_token] | default('#ccc') }};"></span>{{ runner_up_name }} (beats all other candidates except the tied Condorcet winners)</li>
        {% else %}
          <li>Runner-up: <span class="color-box" style="background-color: {{ colordict[runner_up_token] | default('#ccc') }};"></span>{{ runner_up_name }} (beats all other candidates except {{ winner_name }})</li>
        {% endif %}
      {% endif %}

      <!-- Victory margins analysis -->
      {% set decisive_victories = [] %}
      {% for victory in summary_data['victory_data'] %}
        {% if not victory.get('is_tie', False) %}
          {% set _ = decisive_victories.append(victory) %}
        {% endif %}
      {% endfor %}

      {% if decisive_victories %}
        {% set smallest_victory = decisive_victories | min(attribute='victory_size') %}
        {% set largest_victory = decisive_victories | max(attribute='victory_size') %}
        {% set method_label = "margin" if summary_data['victory_method'] == "margins" else "winning votes" %}

        {% set smallest_winner_name = candnames[smallest_victory['winner']] if candnames and smallest_victory['winner'] in candnames else smallest_victory['winner'] %}
        {% set smallest_loser_name = candnames[smallest_victory['loser']] if candnames and smallest_victory['loser'] in candnames else smallest_victory['loser'] %}
        {% set smallest_margin = smallest_victory['winner_votes'] - smallest_victory['loser_votes'] %}
        <li>Smallest margin: <span class="color-box" style="background-color: {{ colordict[smallest_victory['winner']] | default('#ccc') }};"></span>{{ smallest_winner_name }} over <span class="color-box" style="background-color: {{ colordict[smallest_victory['loser']] | default('#ccc') }};"></span>{{ smallest_loser_name }} ({{ "{:,}".format(smallest_victory['winner_votes']) }}-{{ "{:,}".format(smallest_victory['loser_votes']) }}; margin: {{ "{:,}".format(smallest_margin) }})</li>

        {% set largest_winner_name = candnames[largest_victory['winner']] if candnames and largest_victory['winner'] in candnames else largest_victory['winner'] %}
        {% set largest_loser_name = candnames[largest_victory['loser']] if candnames and largest_victory['loser'] in candnames else largest_victory['loser'] %}
        {% set largest_margin = largest_victory['winner_votes'] - largest_victory['loser_votes'] %}
        <li>Largest margin: <span class="color-box" style="background-color: {{ colordict[largest_victory['winner']] | default('#ccc') }};"></span>{{ largest_winner_name }} over <span class="color-box" style="background-color: {{ colordict[largest_victory['loser']] | default('#ccc') }};"></span>{{ largest_loser_name }} ({{ "{:,}".format(largest_victory['winner_votes']) }}-{{ "{:,}".format(largest_victory['loser_votes']) }}; margin: {{ "{:,}".format(largest_margin) }})</li>
      {% endif %}

      <!-- Ties -->
      {% set ties = [] %}
      {% for victory in summary_data['victory_data'] %}
        {% if victory.get('is_tie', False) %}
          {% set _ = ties.append(victory) %}
        {% endif %}
      {% endfor %}
      <li>Pairwise ties: {% if ties %}{{ ties | length }}{% else %}none{% endif %}</li>

      <!-- Total ballots -->
      {% if summary_data['total_ballots'] > 0 %}
        <li><b>Total ballots</b>: {{ "{:,}".format(summary_data['total_ballots']) }}</li>
      {% endif %}
    {% endif %}
  </ul>
</div>
{% endif %}
