{{-- 캘린더 그리드 (HTMX로 로드) --}}
@php
    use Carbon\Carbon;

    $firstDay = Carbon::create($year, $month, 1);
    $lastDay = $firstDay->copy()->endOfMonth();
    $startOfWeek = $firstDay->copy()->startOfWeek(Carbon::SUNDAY);
    $endOfWeek = $lastDay->copy()->endOfWeek(Carbon::SATURDAY);

    $today = Carbon::today();
@endphp

<div class="overflow-x-auto">
    <table class="w-full border-collapse">
        <thead>
            <tr class="bg-gray-50">
                <th class="px-2 py-3 text-center text-sm font-semibold text-red-500 border-b w-[14.28%]">일</th>
                <th class="px-2 py-3 text-center text-sm font-semibold text-gray-600 border-b w-[14.28%]">월</th>
                <th class="px-2 py-3 text-center text-sm font-semibold text-gray-600 border-b w-[14.28%]">화</th>
                <th class="px-2 py-3 text-center text-sm font-semibold text-gray-600 border-b w-[14.28%]">수</th>
                <th class="px-2 py-3 text-center text-sm font-semibold text-gray-600 border-b w-[14.28%]">목</th>
                <th class="px-2 py-3 text-center text-sm font-semibold text-gray-600 border-b w-[14.28%]">금</th>
                <th class="px-2 py-3 text-center text-sm font-semibold text-blue-500 border-b w-[14.28%]">토</th>
            </tr>
        </thead>
        <tbody>
            @php
                $currentDate = $startOfWeek->copy();
            @endphp

            @while($currentDate <= $endOfWeek)
            <tr>
                @for($i = 0; $i < 7; $i++)
                    @php
                        $dateKey = $currentDate->format('Y-m-d');
                        $isCurrentMonth = $currentDate->month === $month;
                        $isToday = $currentDate->isSameDay($today);
                        $isSunday = $currentDate->dayOfWeek === Carbon::SUNDAY;
                        $isSaturday = $currentDate->dayOfWeek === Carbon::SATURDAY;
                        $daySchedules = $calendarData[$dateKey] ?? [];
                        $holidayName = $holidayMap[$dateKey] ?? null;
                        $isHoliday = $holidayName !== null;
                    @endphp

                    <td class="border border-gray-100 align-top {{ !$isCurrentMonth ? 'bg-gray-50' : '' }}">
                        <div class="p-1 min-h-[6rem]">
                            {{-- 날짜 --}}
                            <div class="flex items-center justify-between mb-1">
                                <div class="flex items-center gap-1.5">
                                    <span class="{{ $isHoliday && $isCurrentMonth ? 'text-base font-bold' : 'text-sm font-medium' }}
                                        {{ !$isCurrentMonth ? 'text-gray-300' : '' }}
                                        {{ $isCurrentMonth && ($isSunday || $isHoliday) ? 'text-red-500' : '' }}
                                        {{ $isCurrentMonth && $isSaturday && !$isHoliday ? 'text-blue-500' : '' }}
                                        {{ $isCurrentMonth && !$isSunday && !$isSaturday && !$isHoliday ? 'text-gray-700' : '' }}
                                        {{ $isToday ? 'bg-emerald-500 !text-white rounded-full w-6 h-6 flex items-center justify-center' : '' }}
                                    ">
                                        {{ $currentDate->day }}
                                    </span>
                                    @if($isHoliday && $isCurrentMonth)
                                        <span class="text-sm text-red-500 font-bold truncate max-w-[80px]" title="{{ $holidayName }}">{{ $holidayName }}</span>
                                    @endif
                                </div>

                                @if($isCurrentMonth)
                                <button type="button" onclick="openCreateModal('{{ $dateKey }}')"
                                   class="text-gray-300 hover:text-emerald-600 hover:bg-emerald-50 rounded transition-colors"
                                   title="{{ $currentDate->format('m/d') }} 일정 추가">
                                    <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="M12 4v16m8-8H4"/>
                                    </svg>
                                </button>
                                @endif
                            </div>

                            {{-- 일정 목록 --}}
                            <div class="space-y-1">
                                @foreach($daySchedules as $schedule)
                                    <button type="button" onclick="openEditModal({{ $schedule['id'] }})"
                                       class="block w-full text-left px-1.5 py-0.5 rounded text-xs truncate border cursor-pointer
                                           {{ $schedule['schedule_type'] === 'income' ? 'bg-green-50 text-green-700 border-green-200 hover:bg-green-100' : 'bg-red-50 text-red-700 border-red-200 hover:bg-red-100' }}
                                           {{ $schedule['status'] === 'completed' ? 'opacity-60 line-through' : '' }}
                                           {{ $schedule['status'] === 'cancelled' ? 'opacity-40 line-through' : '' }}
                                       "
                                       title="{{ $schedule['title'] }} - {{ number_format($schedule['amount']) }}원">
                                        <div class="font-medium truncate">{{ $schedule['title'] }}</div>
                                        <div class="text-[10px]">{{ number_format($schedule['amount']) }}원</div>
                                    </button>
                                @endforeach

                            </div>
                        </div>
                    </td>

                    @php
                        $currentDate->addDay();
                    @endphp
                @endfor
            </tr>
            @endwhile
        </tbody>
    </table>
</div>
