{# Aggregated response summary partial for anatomy questions (HTMX-loadable) #}
<div id="anatomy-response-summary-content">
    {% if exam_response_total %}
        <div class="small text-muted mb-2">Total responses: {{ exam_response_total }}</div>
        <div class="row mb-2">
            <div class="col-auto">Correct:</div>
            <div class="col">
                {% if exam_response_correct_count is not None %}
                    <strong>{{ exam_response_correct_count }}</strong>
                    {% if exam_response_correct_pct is not None %}
                        <span class="text-muted">({{ exam_response_correct_pct }}%)</span>
                    {% endif %}
                {% else %}
                    <span class="text-muted">N/A</span>
                {% endif %}
            </div>
        </div>

        <div class="list-group mb-2">
            {# Correct (2) #}
            <div class="list-group-item">
                <div class="d-flex w-100 justify-content-between align-items-center">
                    <div>Correct</div>
                    <div class="text-end"><strong>{{ exam_response_score_counts|get_item:'2'|default:0 }}</strong> <small class="text-muted">{{ exam_response_score_pcts|get_item:'2'|default:0 }}%</small></div>
                </div>
                <div class="progress mt-2" style="height:12px;">
                    <div class="progress-bar bg-success" role="progressbar" style="width: {{ exam_response_score_pcts.2|default:0 }}%;" aria-valuenow="{{ exam_response_score_pcts.2|default:0 }}" aria-valuemin="0" aria-valuemax="100"></div>
                </div>
            </div>

            {# Half mark (1) #}
            <div class="list-group-item">
                <div class="d-flex w-100 justify-content-between align-items-center">
                    <div>Half mark</div>
                    <div class="text-end"><strong>{{ exam_response_score_counts|get_item:'1'|default:0 }}</strong> <small class="text-muted">{{ exam_response_score_pcts|get_item:'1'|default:0 }}%</small></div>
                </div>
                <div class="progress mt-2" style="height:12px;">
                    <div class="progress-bar bg-warning" role="progressbar" style="width: {{ exam_response_score_pcts.1|default:0 }}%;" aria-valuenow="{{ exam_response_score_pcts.1|default:0 }}" aria-valuemin="0" aria-valuemax="100"></div>
                </div>
            </div>

            {# Incorrect (0) #}
            <div class="list-group-item">
                <div class="d-flex w-100 justify-content-between align-items-center">
                    <div>Incorrect</div>
                    <div class="text-end"><strong>{{ exam_response_score_counts|get_item:'0'|default:0 }}</strong> <small class="text-muted">{{ exam_response_score_pcts|get_item:'0'|default:0 }}%</small></div>
                </div>
                <div class="progress mt-2" style="height:12px;">
                    <div class="progress-bar bg-danger" role="progressbar" style="width: {{ exam_response_score_pcts.0|default:0 }}%;" aria-valuenow="{{ exam_response_score_pcts.0|default:0 }}" aria-valuemin="0" aria-valuemax="100"></div>
                </div>
            </div>

            {# Unmarked (empty) #}
            <div class="list-group-item">
                <div class="d-flex w-100 justify-content-between align-items-center">
                    <div>Unmarked</div>
                    <div class="text-end"><strong>{{ exam_response_score_counts|get_item:'empty'|default:0 }}</strong> <small class="text-muted">{{ exam_response_score_pcts|get_item:'empty'|default:0 }}%</small></div>
                </div>
                <div class="progress mt-2" style="height:12px;">
                    <div class="progress-bar bg-secondary" role="progressbar" style="width: {{ exam_response_score_pcts.empty|default:0 }}%;" aria-valuenow="{{ exam_response_score_pcts.empty|default:0 }}" aria-valuemin="0" aria-valuemax="100"></div>
                </div>
            </div>
        </div>

        <div>
            {# Collapsible Top submitted answers - closed by default. #}
            <div class="d-flex justify-content-between align-items-center">
                <h6 class="mb-1">Top submitted answers</h6>
                <button class="btn btn-sm btn-outline-primary" type="button" data-bs-toggle="collapse" data-bs-target="#top-answers-{{ q_index }}" aria-expanded="false" aria-controls="top-answers-{{ q_index }}">
                    Show answers
                </button>
            </div>

            <div class="collapse mt-2" id="top-answers-{{ q_index }}">
                {% if top_answers %}
                    <ul class="list-group">
                        {% for a in top_answers %}
                            <li class="list-group-item d-flex justify-content-between align-items-center">
                                <div class="flex-grow-1">{{ a.answer_compare }}</div>
                                <div class="d-flex align-items-center gap-2">
                                    {% if a.dominant_score == '2' %}
                                        <span class="badge bg-success">Correct</span>
                                    {% elif a.dominant_score == '1' %}
                                        <span class="badge bg-warning text-dark">Half</span>
                                    {% elif a.dominant_score == '0' %}
                                        <span class="badge bg-danger">Incorrect</span>
                                    {% else %}
                                        <span class="badge bg-secondary">Unmarked</span>
                                    {% endif %}
                                    <div class="small text-muted badge bg-primary" title="Submission Count">{{ a.count }}</div>
                                </div>
                            </li>
                        {% endfor %}
                    </ul>
                {% else %}
                    <div class="small text-muted">No submitted answers yet.</div>
                {% endif %}
            </div>
        </div>
    {% else %}
        <div class="small text-muted">No responses recorded for this question in the exam yet.</div>
    {% endif %}
</div>
