{% load i18n static jazzmin admin_list %}
{% get_jazzmin_ui_tweaks as jazzmin_ui %}

<div id="toolbar" class="clearfix mb-3">
    <form id="changelist-search" method="get" class="form-inline" style="display: flex; align-items: center; flex-wrap: wrap; gap: 10px;">
        
        {# --- RESTORED FILTERS (Sorting Panel) --- #}
        {% if cl.has_filters %}
            <div id="filters-container" style="display: flex; flex-wrap: wrap; gap: 5px; align-items: center;">
                {% for spec in cl.filter_specs %}
                    <div class="filter-wrapper" style="display: inline-flex; align-items: center;">
                        {% jazzmin_list_filter cl spec %}
                    </div>
                {% endfor %}
            </div>
        {% endif %}
        {# ---------------------------------------- #}

        {% if cl.search_fields %}
        <div class="search-container" style="display: flex; gap: 5px; align-items: center;">
            <div class="input-group" style="width: auto; max-width: 300px; display: flex; flex-wrap: nowrap;">
                <input type="text" size="40" name="{{ search_var }}" value="{{ cl.query }}" id="searchbar" class="form-control masar-search-input" placeholder="{% trans 'Search' %}" autofocus style="flex: 1;">
                <div class="input-group-append">
                    <button type="submit" class="btn {{ jazzmin_ui.button_classes.primary|default:'btn-primary' }} masar-search-btn" style="white-space: nowrap;">
                        <i class="fas fa-search"></i> {% trans 'Search' %}
                    </button>
                </div>
            </div>
            
            {# --- RESET BUTTON --- #}
            {% if cl.query or cl.has_filters %}
                <a href="." class="btn {{ jazzmin_ui.button_classes.secondary|default:'btn-secondary' }}" style="white-space: nowrap;">
                    <i class="fas fa-undo"></i> {% trans 'Reset' %}
                </a>
            {% endif %}
        </div>
        {% endif %}

        {# Keep hidden fields for other params to preserve filter state #}
        {% for pair in cl.params.items %}
            {% if pair.0 != search_var %}<input type="hidden" name="{{ pair.0 }}" value="{{ pair.1 }}"/>{% endif %}
        {% endfor %}
        
    </form>
</div>