{% if scorestardict and scorestardict['scoremodel'] and scorestardict['starscale'] %}
{% set scoredata = scorestardict['scoremodel'] %}
{% set stardata = scorestardict['starscale'] %}
{% set ranklist = scoredata['ranklist'] %}
{% set colordict = stardata['colordict'] %}
{% set starscaled = stardata['starscaled'] %}
{% set colorlines = stardata['colorlines'] %}
<style>
  {% for cand in ranklist %}
  {{colorlines[cand]}}
  {% endfor %}
</style>

    <ul>
      {% if scoredata['totalvoters'] == 0 %}
      <li>No ballots recorded for this contest.</li>
      {% endif %}
      <li><b>STAR winner</b>:
        {% if scoredata['winner'] and "tie " in scoredata['winner'] %}
          {# Handle STAR ties: show color boxes for all candidates mentioned in tie string #}
          {%- set winners = [] -%}
          {%- for cand in ranklist -%}
            {%- set j = scoredata['scores'][cand] -%}
            {%- if j['candname'] in scoredata['winner'] -%}
              {%- set _ = winners.append((cand, j['candname'])) -%}
            {%- endif -%}
          {%- endfor -%}
          {%- for cand_token, candname in winners -%}
            <span class="color-box" style="background-color: {{ colordict[cand_token] | default('#ccc') }};"></span>{{ candname }}{% if not loop.last %}, {% endif %}
          {%- endfor -%}
        {% else %}
          {# Handle single winner #}
          {% for cand in ranklist %}
            {% set j = scoredata['scores'][cand] %}
            {% if j['candname'] == scoredata['winner'] %}
              <span class="color-box" style="background-color: {{ colordict[cand] | default('#ccc') }};"></span>
            {% endif %}
          {% endfor %}
          {{ scoredata['winner']}}
        {% endif %}
      </li>
      {% if scoredata['scores'] and ranklist %}
        {% set top_scorer = ranklist[0] %}
        {% set top_score_data = scoredata['scores'][top_scorer] %}
        <li>Finalists: {% for cand in ranklist %}{% set j = scoredata['scores'][cand] %}{% if j['candname'] == scoredata['fin1n'] %}<span class="color-box" style="background-color: {{ colordict[cand] | default('#ccc') }};"></span>{% endif %}{% endfor %}{{ scoredata['fin1n'] }} vs {% for cand in ranklist %}{% set j = scoredata['scores'][cand] %}{% if j['candname'] == scoredata['fin2n'] %}<span class="color-box" style="background-color: {{ colordict[cand] | default('#ccc') }};"></span>{% endif %}{% endfor %}{{ scoredata['fin2n'] }}</li>
      <li>Runoff result:
        <ul>
          <li>{% for cand in ranklist %}{% set j = scoredata['scores'][cand] %}{% if j['candname'] == scoredata['fin1n'] %}<span class="color-box" style="background-color: {{ colordict[cand] | default('#ccc') }};"></span>{% endif %}{% endfor %}{{ scoredata['fin1n'] }}: {{ "{:,}".format(scoredata['fin1votes']) }} votes ({{ scoredata['fin1votes_pct_str'] }})</li>
          <li>{% for cand in ranklist %}{% set j = scoredata['scores'][cand] %}{% if j['candname'] == scoredata['fin2n'] %}<span class="color-box" style="background-color: {{ colordict[cand] | default('#ccc') }};"></span>{% endif %}{% endfor %}{{ scoredata['fin2n'] }}: {{ "{:,}".format(scoredata['fin2votes']) }} votes ({{ scoredata['fin2votes_pct_str'] }})</li>
          <li>No preference: {{ scoredata['final_abstentions'] }} voters ({{ scoredata['final_abstentions_pct_str'] }})</li>
        </ul>
      </li>
      {% endif %}
      <li>Total stars allocated: {{ "{:,}".format(scoredata['total_all_scores']) }}</li>
      <li><b>Total ballots</b>: {{ "{:,}".format(scoredata['totalvoters']) }}</li>
    </ul>


<div class="flex-container">
  <div class="container">
    <div class="stars star">
      <div class="r6star">
        <span class="star s01">★</span><span class="star s02">★</span><span class="star s03">★</span><span class="star s04">★</span><span class="star s05">★</span><span class="star s06">★</span>
      </div><br>
      <div class="r5star">
        <span class="star s07">★</span><span class="star s08">★</span><span class="star s09">★</span><span class="star s11">★</span><span class="star s11">★</span>
      </div><br>
      <div class="r6star">
        <span class="star s12">★</span><span class="star s13">★</span><span class="star s14">★</span><span class="star s15">★</span><span class="star s16">★</span><span class="star s17">★</span>
      </div>
      <div class="r5star">
        <span class="star s18">★</span><span class="star s19">★</span><span class="star s20">★</span><span class="star s21">★</span><span class="star s22">★</span>
      </div>
      <div class="r6star">
        <span class="star s23">★</span><span class="star s24">★</span><span class="star s25">★</span><span class="star s26">★</span><span class="star s27">★</span><span class="star s28">★</span>
      </div>
      <div class="r5star">
        <span class="star s29">★</span><span class="star s30">★</span><span class="star s31">★</span><span class="star s32">★</span><span class="star s33">★</span>
      </div>
      <div class="r6star">
        <span class="star s34">★</span><span class="star s35">★</span><span class="star s36">★</span><span class="star s37">★</span><span class="star s38">★</span><span class="star s39">★</span>
      </div>
      <div class="r5star">
        <span class="star s40">★</span><span class="star s41">★</span><span class="star s42">★</span><span class="star s43">★</span><span class="star s44">★</span>
      </div>
      <div class="r6star">
        <span class="star s45">★</span><span class="star s46">★</span><span class="star s47">★</span><span class="star s48">★</span><span class="star s49">★</span><span class="star s50">★</span>
      </div>
    </div>
    <div class="star-caption">(one star above is approximately {{ "{:,}".format(stardata['starratio']) }} stars allocated by voters)
    </div>
  </div>
  <div>
    <ul>
      <li>First round:
        <ul>
          {% for cand in ranklist %}
          {% set j = scoredata['scores'][cand] %}
          <li><span class="star g{{ loop.index }}">★</span>#{{ j['rank'] }}: {{ j['candname'] }} received {{ "{:,}".format(j['score']) }} stars ({{ j['score_pct_str'] }}) from {{ "{:,}".format(j['votercount']) }} voters ({{ j['voter_pct_str'] }})</li>
          {% endfor %}
        </ul>
      </li>
      <li>Finalists:
        <ul>
          {% set fin1n = scoredata['fin1n'] %}
          {% set fin2n = scoredata['fin2n'] %}
          <li><span class="star g1">★</span><b>{{ fin1n }}</b> (✅ winner) preferred by {{ "{:,}".format(scoredata['fin1votes']) }} of {{ "{:,}".format(scoredata['totalvoters']) }} voters
          <li><span class="star g2">★</span>{{ fin2n }} preferred by {{ "{:,}".format(scoredata['fin2votes']) }} of  {{ "{:,}".format(scoredata['totalvoters']) }} voters
          <li><span class="star g0">★</span>No preference between the finalists: {{ scoredata['final_abstentions']}} </li>
        </ul>
      </li>
    </ul>
  </div>
</div>
{% else %}
<p>STAR voting data not available.</p>
{% endif %}
