@php
    $trendingPosts = AleshaPost::ofType('post')->published()->latest('published_at')->take(8)->get();
    $sidebarCats   = AleshaTaxonomy::where('type','category')
        ->withCount(['posts' => fn($q) => $q->ofType('post')->published()])
        ->having('posts_count','>',0)->orderBy('posts_count','desc')->get();
    $sidebarTags   = AleshaTaxonomy::where('type','tag')
        ->withCount(['posts' => fn($q) => $q->ofType('post')->published()])
        ->having('posts_count','>',0)->orderBy('posts_count','desc')->take(20)->get();
@endphp

<aside>
    {{-- Categories --}}
    @if($sidebarCats->count())
    <div style="background:#fff; border:1px solid #e5e7eb; border-radius:10px; padding:16px; margin-bottom:20px;">
        <div class="section-title">Topics</div>
        @foreach($sidebarCats as $cat)
        <a href="/category/{{ $cat->slug }}"
           style="display:flex; align-items:center; justify-content:space-between; padding:8px 10px; border-radius:6px; text-decoration:none; color:#374151; font-size:13px; font-weight:600; border-bottom:1px solid #f9fafb; transition:all 0.15s;"
           onmouseover="this.style.background='var(--news-red-bg)'; this.style.color='var(--news-red)'" onmouseout="this.style.background='transparent'; this.style.color='#374151'">
            <span>{{ $cat->name }}</span>
            <span style="background:#f3f4f6; color:#9ca3af; font-size:11px; font-weight:700; padding:1px 6px; border-radius:10px;">{{ $cat->posts_count }}</span>
        </a>
        @endforeach
    </div>
    @endif

    {{-- Tags --}}
    @if($sidebarTags->count())
    <div style="background:#fff; border:1px solid #e5e7eb; border-radius:10px; padding:16px; margin-bottom:20px;">
        <div class="section-title">Tags</div>
        <div style="display:flex; flex-wrap:wrap; gap:6px;">
            @foreach($sidebarTags as $tag)
            <a href="/tag/{{ $tag->slug }}"
               style="display:inline-flex; align-items:center; gap:4px; padding:4px 10px; background:#f3f4f6; color:#374151; font-size:12px; font-weight:600; border-radius:20px; text-decoration:none; transition:all 0.15s;"
               onmouseover="this.style.background='var(--news-red)'; this.style.color='#fff'" onmouseout="this.style.background='#f3f4f6'; this.style.color='#374151'">
                #{{ $tag->name }}
                <span style="font-size:10px; color:#9ca3af; font-weight:400;" class="tag-count">{{ $tag->posts_count }}</span>
            </a>
            @endforeach
        </div>
    </div>
    @endif

    {{-- Trending --}}
    @if($trendingPosts->count())
    <div style="background:#fff; border:1px solid #e5e7eb; border-radius:10px; padding:16px; margin-bottom:20px;">
        <div class="section-title">Most Read</div>
        @foreach($trendingPosts as $t)
        <div style="display:flex; gap:10px; padding:10px 0; {{ !$loop->last ? 'border-bottom:1px solid #f9fafb;' : '' }} align-items:flex-start;">
            <span style="font-size:20px; font-weight:900; color:var(--news-red-light); font-family:'Playfair Display',serif; line-height:1; flex-shrink:0; min-width:20px;">{{ $loop->iteration }}</span>
            <div>
                <a href="/{{ $t->content_type }}/{{ $t->slug }}" style="text-decoration:none;">
                    <p style="font-size:12px; font-weight:700; color:#111; margin:0 0 3px; line-height:1.4; transition:color 0.15s;"
                       onmouseover="this.style.color='var(--news-red)'" onmouseout="this.style.color='#111'">{{ $t->title }}</p>
                </a>
                <p class="article-meta">{{ $t->published_at?->diffForHumans() }}</p>
            </div>
        </div>
        @endforeach
    </div>
    @endif

</aside>
