<!-- Reservations List -->
{% if reservations %}
<div class="space-y-2">
    {% for item in reservations %}
    {% set res = item.reservation %}
    {% set card_id = "res-card-" ~ res.id %}
    {% set detail_id = "res-detail-" ~ res.id %}

    <div class="rounded-lg border border-gray-200 dark:border-gray-700"
         style="border-left: 4px solid {{ res.color }};">

        <!-- Header row (always visible, clickable) -->
        <div class="res-card-header flex items-center justify-between p-4 cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-700/50 transition-colors select-none"
             data-res-id="{{ res.id }}"
             onclick="toggleResCard('{{ res.id }}')">

            <div class="flex-1 min-w-0">
                <div class="flex items-center gap-2 flex-wrap">
                    <h3 class="font-semibold text-gray-900 dark:text-white">{{ res.name }}</h3>
                    {% if res.device_type == 'slm' %}
                    <span class="px-2 py-0.5 text-xs font-medium bg-purple-100 dark:bg-purple-900/30 text-purple-700 dark:text-purple-400 rounded">SLM</span>
                    {% else %}
                    <span class="px-2 py-0.5 text-xs font-medium bg-blue-100 dark:bg-blue-900/30 text-blue-700 dark:text-blue-400 rounded">Seismograph</span>
                    {% endif %}
                    {% if item.has_conflicts %}
                    <span class="px-2 py-0.5 text-xs font-medium bg-amber-100 dark:bg-amber-900/30 text-amber-700 dark:text-amber-400 rounded"
                          title="{{ item.conflict_count }} unit(s) will need a calibration swap during this job">
                        {{ item.conflict_count }} cal swap{{ 's' if item.conflict_count != 1 else '' }}
                    </span>
                    {% endif %}
                </div>
                <p class="text-sm text-gray-500 dark:text-gray-400 mt-0.5">
                    {{ res.start_date.strftime('%b %d, %Y') }} –
                    {% if res.end_date %}
                        {{ res.end_date.strftime('%b %d, %Y') }}
                    {% elif res.end_date_tbd %}
                        <span class="text-yellow-600 dark:text-yellow-400 font-medium">TBD</span>
                        {% if res.estimated_end_date %}
                        <span class="text-gray-400">(est. {{ res.estimated_end_date.strftime('%b %d, %Y') }})</span>
                        {% endif %}
                    {% else %}
                        <span class="text-yellow-600 dark:text-yellow-400">Ongoing</span>
                    {% endif %}
                </p>
            </div>

            <!-- Counts -->
            <div class="flex flex-col items-end gap-1 mx-4 flex-shrink-0">
                {% set full = item.assigned_count == item.location_count and item.location_count > 0 %}
                {% set remaining = item.location_count - item.assigned_count %}
                <!-- Number row -->
                <div class="flex items-baseline gap-2">
                    <span class="text-xs text-gray-400 dark:text-gray-500">est. {% if res.estimated_units %}{{ res.estimated_units }}{% else %}—{% endif %}</span>
                    <span class="text-gray-300 dark:text-gray-600">·</span>
                    <span class="text-base font-bold {% if full %}text-green-600 dark:text-green-400{% elif item.assigned_count == 0 %}text-gray-400 dark:text-gray-500{% else %}text-amber-500 dark:text-amber-400{% endif %}">
                        {{ item.assigned_count }}/{{ item.location_count }}
                    </span>
                    {% if remaining > 0 %}
                    <span class="text-xs text-amber-500 dark:text-amber-400 whitespace-nowrap">({{ remaining }} more)</span>
                    {% endif %}
                </div>
                <!-- Progress squares -->
                {% if item.location_count > 0 %}
                <div class="flex gap-0.5">
                    {% for i in range(item.location_count) %}
                    <span class="w-3 h-3 rounded-sm {% if i < item.assigned_count %}{% if full %}bg-green-500{% else %}bg-amber-500{% endif %}{% else %}bg-gray-300 dark:bg-gray-600{% endif %}"></span>
                    {% endfor %}
                </div>
                {% endif %}
            </div>

            <!-- Action buttons -->
            <div class="flex items-center gap-1 flex-shrink-0">
                <!-- Assign units (always visible) -->
                <button onclick="event.stopPropagation(); openPlanner('{{ res.id }}')"
                        class="p-2 text-gray-400 hover:text-green-600 dark:hover:text-green-400 rounded-lg hover:bg-gray-100 dark:hover:bg-gray-700"
                        title="Assign units">
                    <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-3 7h3m-3 4h3m-6-4h.01M9 16h.01"/>
                    </svg>
                </button>

                <!-- "..." overflow menu -->
                <div class="relative" onclick="event.stopPropagation()">
                    <button onclick="toggleResMenu('{{ res.id }}')"
                            class="p-2 text-gray-400 hover:text-gray-600 dark:hover:text-gray-300 rounded-lg hover:bg-gray-100 dark:hover:bg-gray-700"
                            title="More options">
                        <svg class="w-4 h-4" fill="currentColor" viewBox="0 0 24 24">
                            <circle cx="5" cy="12" r="1.5"/><circle cx="12" cy="12" r="1.5"/><circle cx="19" cy="12" r="1.5"/>
                        </svg>
                    </button>
                    <div id="res-menu-{{ res.id }}"
                         class="hidden absolute right-0 top-8 z-20 w-44 bg-white dark:bg-slate-800 border border-gray-200 dark:border-gray-700 rounded-lg shadow-lg py-1">
                        <button onclick="openPromoteModal('{{ res.id }}', '{{ res.name }}'); toggleResMenu('{{ res.id }}')"
                                class="w-full text-left px-4 py-2 text-sm text-gray-700 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-slate-700 flex items-center gap-2">
                            <svg class="w-4 h-4 text-emerald-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 10l7-7m0 0l7 7m-7-7v18"/>
                            </svg>
                            Promote to Project
                        </button>
                        <button onclick="editReservation('{{ res.id }}'); toggleResMenu('{{ res.id }}')"
                                class="w-full text-left px-4 py-2 text-sm text-gray-700 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-slate-700 flex items-center gap-2">
                            <svg class="w-4 h-4 text-blue-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"/>
                            </svg>
                            Edit
                        </button>
                        <div class="border-t border-gray-100 dark:border-gray-700 my-1"></div>
                        <button onclick="deleteReservation('{{ res.id }}', '{{ res.name }}'); toggleResMenu('{{ res.id }}')"
                                class="w-full text-left px-4 py-2 text-sm text-red-600 dark:text-red-400 hover:bg-red-50 dark:hover:bg-red-900/20 flex items-center gap-2">
                            <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/>
                            </svg>
                            Delete
                        </button>
                    </div>
                </div>

                <!-- Chevron -->
                <svg id="chevron-{{ res.id }}" class="w-4 h-4 text-gray-400 transition-transform duration-200 ml-1 pointer-events-none" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"/>
                </svg>
            </div>
        </div>

        <!-- Expandable detail panel -->
        <div id="{{ detail_id }}" class="hidden border-t border-gray-100 dark:border-gray-700 bg-gray-50 dark:bg-slate-800/60 px-4 py-3">

            {% if res.notes %}
            <p class="text-sm text-gray-500 dark:text-gray-400 mb-3 italic">{{ res.notes }}</p>
            {% endif %}

            <div class="grid grid-cols-2 gap-x-6 gap-y-1 text-sm mb-3">
                <div class="text-gray-500 dark:text-gray-400">Estimated</div>
                <div class="font-medium {% if res.estimated_units %}text-gray-800 dark:text-gray-200{% else %}text-gray-400 dark:text-gray-500 italic{% endif %}">
                    {% if res.estimated_units %}{{ res.estimated_units }} unit{{ 's' if res.estimated_units != 1 else '' }}{% else %}not specified{% endif %}
                </div>
                <div class="text-gray-500 dark:text-gray-400">Locations</div>
                <div class="font-medium text-gray-800 dark:text-gray-200">{{ item.assigned_count }} of {{ item.location_count }} filled</div>
                {% if item.assigned_count < item.location_count %}
                <div class="text-gray-500 dark:text-gray-400">Still needed</div>
                <div class="font-medium text-amber-600 dark:text-amber-400">{{ item.location_count - item.assigned_count }} location{{ 's' if (item.location_count - item.assigned_count) != 1 else '' }} remaining</div>
                {% endif %}
                {% if item.has_conflicts %}
                <div class="text-gray-500 dark:text-gray-400">Cal swaps</div>
                <div class="font-medium text-amber-600 dark:text-amber-400">{{ item.conflict_count }} unit{{ 's' if item.conflict_count != 1 else '' }} will need swapping during job</div>
                {% endif %}
            </div>

            {% if item.assigned_units %}
            <p class="text-xs font-semibold uppercase tracking-wide text-gray-400 dark:text-gray-500 mb-2">Monitoring Locations</p>
            <div class="flex flex-col gap-1">
                {% for u in item.assigned_units %}
                <div class="rounded bg-white dark:bg-slate-700 border border-gray-100 dark:border-gray-600 text-sm">
                    <div class="flex items-center gap-3 px-3 py-1.5">
                        <span class="text-gray-400 dark:text-gray-500 text-xs w-12 flex-shrink-0">Loc. {{ loop.index }}</span>
                        <div class="flex flex-col min-w-0">
                            {% if u.location_name %}
                            <span class="text-xs font-semibold text-gray-700 dark:text-gray-300 truncate">{{ u.location_name }}</span>
                            {% endif %}
                            <button onclick="openUnitDetailModal('{{ u.id }}')"
                                    class="font-medium text-blue-600 dark:text-blue-400 hover:underline text-left text-sm">{{ u.id }}</button>
                        </div>
                        <span class="flex-1"></span>
                        {% if u.power_type == 'ac' %}
                        <span class="text-xs px-1.5 py-0.5 bg-blue-50 dark:bg-blue-900/20 text-blue-600 dark:text-blue-400 rounded">A/C</span>
                        {% elif u.power_type == 'solar' %}
                        <span class="text-xs px-1.5 py-0.5 bg-yellow-50 dark:bg-yellow-900/20 text-yellow-600 dark:text-yellow-400 rounded">Solar</span>
                        {% endif %}
                        {% if u.deployed %}
                        <span class="text-xs px-1.5 py-0.5 bg-green-50 dark:bg-green-900/20 text-green-600 dark:text-green-400 rounded">Deployed</span>
                        {% else %}
                        <span class="text-xs px-1.5 py-0.5 bg-gray-100 dark:bg-gray-600 text-gray-500 dark:text-gray-400 rounded">Benched</span>
                        {% endif %}
                        {% if u.last_calibrated %}
                        <span class="text-xs text-gray-400 dark:text-gray-500">Cal: {{ u.last_calibrated.strftime('%b %d, %Y') }}</span>
                        {% endif %}
                    </div>
                    {% if u.notes %}
                    <p class="px-3 pb-1.5 text-xs text-gray-400 dark:text-gray-500 italic">{{ u.notes }}</p>
                    {% endif %}
                </div>
                {% endfor %}
            </div>
            {% else %}
            <p class="text-sm text-gray-400 dark:text-gray-500 italic">No units assigned yet. Click the clipboard icon to plan.</p>
            {% endif %}
        </div>

    </div>
    {% endfor %}
</div>

<!-- toggleResCard, deleteReservation, editReservation, openUnitDetailModal defined in fleet_calendar.html -->
{% else %}
<div class="text-center py-8">
    <svg class="w-12 h-12 mx-auto text-gray-400 dark:text-gray-500 mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z"/>
    </svg>
    <p class="text-gray-500 dark:text-gray-400">No jobs yet</p>
    <p class="text-sm text-gray-400 dark:text-gray-500 mt-1">Click "New Job" to start planning a deployment</p>
</div>
{% endif %}
