@php
    $tz             = auth()->user()->timezone;
    $today          = now($tz)->toDateString();
    $currentMonthNr = \Carbon\Carbon::parse($currentDate, $tz)->month;

    $start  = \Carbon\Carbon::parse($currentDate, $tz)->startOfMonth()->startOfWeek(\Carbon\Carbon::MONDAY);
    $end    = \Carbon\Carbon::parse($currentDate, $tz)->endOfMonth()->endOfWeek(\Carbon\Carbon::SUNDAY);

    $days   = collect();
    $cursor = $start->copy();
    while ($cursor <= $end) {
        $days->push($cursor->copy());
        $cursor->addDay();
    }
@endphp

{{-- Wochentag-Header --}}
<div class="grid grid-cols-7 border-b border-gray-100 bg-white sticky top-0 z-10">
    @foreach(['Mo','Di','Mi','Do','Fr','Sa','So'] as $dn)
        <div class="py-2 text-center text-[11px] text-gray-400 uppercase tracking-wide">{{ $dn }}</div>
    @endforeach
</div>

{{-- Tage-Grid --}}
<div class="grid grid-cols-7 divide-x divide-y divide-gray-100">
    @foreach($days as $day)
        @php
            $dayStr          = $day->toDateString();
            $isToday         = $dayStr === $today;
            $isCurrentMonth  = $day->month === $currentMonthNr;

            // Ganztägige Events, die diesen Tag abdecken
            $allDayForDay = $events->filter(fn($e) =>
                $e->is_all_day
                && $e->starts_local->toDateString() <= $dayStr
                && ($e->ends_local ? $e->ends_local->toDateString() : $e->starts_local->toDateString()) >= $dayStr
            );

            // Zeitgebundene Events, die diesen Tag überspannen (mehrtägig: jeden Tag anzeigen)
            $timedForDay = $events->filter(function ($e) use ($dayStr) {
                if ($e->is_all_day) return false;
                $eStart = $e->starts_local->toDateString();
                $eEnd   = $e->ends_local ? $e->ends_local->toDateString() : $eStart;
                return $eStart <= $dayStr && $eEnd >= $dayStr;
            });

            $totalVisible = $allDayForDay->count() + $timedForDay->count();
            $maxShow      = 3;
            $shown        = 0;
        @endphp

        <div class="min-h-[110px] p-1.5 flex flex-col transition-colors
                    {{ $isCurrentMonth ? 'bg-white' : 'bg-gray-50/50' }}"
             x-on:dragover="onMonthDayDragOver($event)"
             x-on:dragleave="onMonthDayDragLeave($event)"
             x-on:drop="onMonthDayDrop($event, '{{ $dayStr }}')"
             x-on:dblclick.self="$wire.createEventAt('{{ $dayStr }}', 9, 0)">

            {{-- Tag-Nummer --}}
            <div class="flex justify-center mb-1">
                <span class="w-6 h-6 flex items-center justify-center rounded-full text-xs font-medium select-none
                    {{ $isToday
                        ? 'bg-primary-600 text-white'
                        : ($isCurrentMonth ? 'text-gray-700' : 'text-gray-400') }}">
                    {{ $day->day }}
                </span>
            </div>

            {{-- Ganztägige Events --}}
            @foreach($allDayForDay->take($maxShow) as $event)
                @php
                    [$bg, $border, $text] = $resolveColor($event->color);
                    $shown++;
                @endphp
                <div class="text-[10px] font-medium px-1.5 py-0.5 rounded mb-0.5 truncate cursor-grab leading-tight"
                     style="background: {{ $bg }}; color: {{ $text }}; border-left: 2px solid {{ $border }};"
                     draggable="true"
                     wire:key="month-allday-{{ $event->id }}-{{ $dayStr }}"
                     x-on:dragstart="onMonthEventDragStart($event, '{{ $event->id }}', 0, 0, true, '{{ $dayStr }}')"
                     x-on:dragend="onMonthEventDragEnd($event)"
                     x-on:dblclick.stop="$wire.openSidebar('{{ $event->id }}', '{{ $dayStr }}')">
                    {{ $event->title }}
                </div>
            @endforeach

            {{-- Zeitgebundene Events --}}
            @foreach($timedForDay as $event)
                @if($shown >= $maxShow) @break @endif
                @php
                    [$bg, $border, $text] = $resolveColor($event->color);
                    $shown++;
                @endphp
                <div class="text-[10px] font-medium px-1.5 py-0.5 rounded mb-0.5 truncate cursor-grab flex items-center gap-1 leading-tight"
                     style="background: {{ $bg }}; color: {{ $text }}; border-left: 2px solid {{ $border }};"
                     draggable="true"
                     wire:key="month-ev-{{ $event->id }}"
                     x-on:dragstart="onMonthEventDragStart($event, '{{ $event->id }}', {{ $event->starts_local->hour }}, {{ $event->starts_local->minute }}, false, '{{ $dayStr }}')"
                     x-on:dragend="onMonthEventDragEnd($event)"
                     x-on:dblclick.stop="$wire.openSidebar('{{ $event->id }}', '{{ $dayStr }}')">
                    <span class="opacity-50 shrink-0" style="font-size: 9px">{{ $event->starts_local->format('H:i') }}</span>
                    <span class="truncate">{{ $event->title }}</span>
                </div>
            @endforeach

            {{-- Overflow --}}
            @if($totalVisible > $maxShow)
                <span class="text-[10px] text-gray-400 mt-auto pt-0.5 leading-none">
                    +{{ $totalVisible - $maxShow }} mehr
                </span>
            @endif

        </div>
    @endforeach
</div>
