<!DOCTYPE html>
<html lang="tr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    
    {{-- Dynamic SEO Meta Tags --}}
    @php
        $pageType        = $__env->hasSection('page_type')       ? $__env->getSection('page_type')       : 'home';
        $metaTitle       = $__env->hasSection('meta_title')      ? $__env->getSection('meta_title')      : null;
        $metaDescription = $__env->hasSection('meta_description')? $__env->getSection('meta_description'): null;
        $ogType          = $__env->hasSection('og_type')         ? $__env->getSection('og_type')         : 'website';
        $ogImage         = $__env->hasSection('og_image')        ? $__env->getSection('og_image')        : null;
        $canonicalUrl    = $__env->hasSection('canonical_url')   ? $__env->getSection('canonical_url')   : null;
        $noindex         = $__env->hasSection('noindex')         ? (bool)$__env->getSection('noindex')   : false;

        // Article/Breadcrumb/FAQ structured data (passed from child views via @php)
        // These are set as PHP variables directly in child view scope (outside sections),
        // so we access them from the view data bag if present.
        $seoArticleData    = $articleData    ?? null;
        $seoBreadcrumbData = $breadcrumbData ?? null;
        $seoFaqData        = $faqData        ?? null;

        $appSettings    = \App\Models\AppSetting::getAllCached();
        $siteName       = $appSettings['site_name']        ?? config('app.name', 'AskeriPlatform');
        $siteLogo       = $appSettings['site_logo']        ?? null;
        $siteFavicon    = $appSettings['site_favicon']     ?? null;
        $siteUrl        = $appSettings['site_url']         ?? config('app.url');
        $siteDescription= $appSettings['site_description'] ?? 'Askeri kariyer, KPSS hazırlık ve üniversite bilgileri için kapsamlı rehber.';
        $contactEmail   = $appSettings['contact_email']    ?? null;
        $contactPhone   = $appSettings['contact_phone']    ?? null;
        $contactAddress = $appSettings['contact_address']  ?? null;
    @endphp
    <x-seo-meta
        :pageType="$pageType"
        :title="$metaTitle"
        :description="$metaDescription"
        :type="$ogType"
        :image="$ogImage"
        :url="$canonicalUrl"
        :noindex="$noindex"
        :articleData="$seoArticleData"
        :breadcrumbData="$seoBreadcrumbData"
        :faqData="$seoFaqData"
    />
    
    {{-- Favicon --}}
    @if($siteFavicon)
        <link rel="icon" type="image/x-icon" href="{{ asset($siteFavicon) }}">
    @endif
    
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
    @vite(['resources/css/app.css', 'resources/js/app.js'])

    {{-- Global Theme CSS Variables --}}
    @include('components.theme-styles')
    
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }

        body {
            font-family: var(--font-family, 'Inter'), -apple-system, BlinkMacSystemFont, sans-serif;
            background: var(--background-color, #f8fafc);
            color: var(--text-color, #111827);
            line-height: 1.6;
            -webkit-font-smoothing: antialiased;
        }

        a { color: var(--primary-color, #2563eb); text-decoration: none; }
        a:hover { text-decoration: none; }
        img { max-width: 100%; }

        /* ─── MAIN CONTENT ────────────────────────────── */
        main { min-height: 60vh; min-width: 0; overflow: hidden; }

        /* ─── PAGE WRAPPER ─────────────────────────────── */
        .page-wrapper {
            max-width: 1280px;
            margin: 0 auto;
            padding: 48px 24px;
            display: grid;
            grid-template-columns: 1fr 320px;
            gap: 40px;
            align-items: start;
            min-width: 0;
        }

        .page-wrapper.fullwidth {
            grid-template-columns: 1fr;
            max-width: 100%;
            padding: 0;
        }

        @media (max-width: 1024px) {
            .page-wrapper { grid-template-columns: 1fr; }
            .sidebar { display: none; }
        }

        /* ─── SIDEBAR ──────────────────────────────────── */
        .sidebar {
            position: sticky;
            top: 90px;
        }

        .sidebar-widget {
            background: #ffffff;
            border: 1px solid var(--border-color, #e2e8f0);
            border-radius: 12px;
            padding: 24px;
            margin-bottom: 24px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.06);
        }

        .widget-title {
            font-size: 12px;
            font-weight: 700;
            color: #6b7280;
            text-transform: uppercase;
            letter-spacing: 1px;
            margin-bottom: 16px;
            padding-bottom: 12px;
            border-bottom: 1px solid #f1f5f9;
        }

        .category-item {
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 10px 12px;
            color: #374151;
            text-decoration: none;
            font-size: 14px;
            border-radius: 8px;
            margin-bottom: 4px;
            transition: all 0.2s;
        }
        .category-item:hover {
            background: rgba(30, 64, 175, 0.05);
            color: var(--primary-color, #2563eb);
        }
        .category-item-name {
            display: flex;
            align-items: center;
            gap: 8px;
        }
        .category-dot {
            width: 8px;
            height: 8px;
            border-radius: 50%;
            background: #3b82f6;
        }
        .category-count {
            background: #f8fafc;
            padding: 2px 10px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: 600;
            color: #6b7280;
        }

        .user-widget {
            background: var(--primary-color, linear-gradient(135deg, #1d4ed8 0%, #2563eb 100%));
            color: white;
        }
        .user-widget .widget-title {
            color: rgba(255,255,255,0.7);
            border-bottom-color: rgba(255,255,255,0.1);
        }
        .user-avatar-lg {
            width: 56px;
            height: 56px;
            background: rgba(255,255,255,0.2);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 24px;
            font-weight: 700;
            margin-bottom: 12px;
        }
        .user-name {
            font-size: 18px;
            font-weight: 700;
            margin-bottom: 4px;
        }
        .user-role {
            font-size: 13px;
            opacity: 0.8;
        }

        .quick-links {
            margin-top: 16px;
            padding-top: 16px;
            border-top: 1px solid rgba(255,255,255,0.15);
        }
        .quick-link {
            display: flex;
            align-items: center;
            gap: 8px;
            padding: 8px 0;
            color: rgba(255,255,255,0.85);
            text-decoration: none;
            font-size: 13px;
            font-weight: 500;
            transition: color 0.2s;
        }
        .quick-link:hover {
            color: white;
        }
    <style>
        .sidebar-recent-post {
            display: flex;
            gap: 10px;
            padding: 10px 0;
            text-decoration: none;
            border-bottom: 1px solid #f1f5f9;
            align-items: flex-start;
        }
        .sidebar-recent-post:last-child { border-bottom: none; }
        .sidebar-recent-post-body {
            flex: 1;
            min-width: 0;
        }
        .sidebar-recent-post-title {
            font-size: 13px;
            font-weight: 600;
            color: #111827;
            line-height: 1.4;
            margin-bottom: 3px;
            overflow: hidden;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
        }
        .sidebar-recent-post-meta {
            font-size: 11px;
            color: #6b7280;
        }

        .sidebar-recent-topic {
            display: flex;
            gap: 10px;
            padding: 10px 0;
            text-decoration: none;
            border-bottom: 1px solid #f1f5f9;
            align-items: flex-start;
        }
        .sidebar-recent-topic:last-child { border-bottom: none; }
        .sidebar-recent-topic-icon {
            width: 32px;
            height: 32px;
            background: rgba(37,99,235,0.08);
            border-radius: 8px;
            display: flex;
            align-items: center;
            justify-content: center;
            flex-shrink: 0;
            font-size: 14px;
        }
        .sidebar-recent-topic-body {
            flex: 1;
            min-width: 0;
        }
        .sidebar-recent-topic-title {
            font-size: 13px;
            font-weight: 600;
            color: #111827;
            line-height: 1.4;
            margin-bottom: 3px;
            overflow: hidden;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
        }
        .sidebar-recent-topic-meta {
            font-size: 11px;
            color: #6b7280;
        }

        .sidebar-more-link {
            display: block;
            text-align: center;
            margin-top: 12px;
            font-size: 13px;
            font-weight: 600;
            color: #2563eb;
            text-decoration: none;
        }

        /* Flash messages */
        .flash-success {
            background: #d1fae5;
            border: 1px solid #6ee7b7;
            color: #065f46;
            padding: 14px 20px;
            border-radius: 10px;
            font-size: 14px;
            margin-bottom: 0;
        }
        .flash-error {
            background: #fee2e2;
            border: 1px solid #fca5a5;
            color: #991b1b;
            padding: 14px 20px;
            border-radius: 10px;
            font-size: 14px;
            margin-bottom: 0;
        }
        .flash-wrapper {
            max-width: 1200px;
            margin: 16px auto;
            padding: 0 24px;
        }

        /* Sidebar search */
        .sidebar-search-wrap {
            display: flex;
            gap: 8px;
        }
        .sidebar-search-input {
            flex: 1;
            padding: 10px 14px;
            border: 1px solid #e2e8f0;
            border-radius: 8px;
            font-size: 14px;
            font-family: inherit;
            outline: none;
            transition: border-color 0.15s;
        }
        .sidebar-search-input:focus { border-color: #2563eb; }
        .sidebar-search-btn {
            padding: 10px 14px;
            background: #2563eb;
            color: white;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            display: flex;
            align-items: center;
            transition: background 0.15s;
        }
        .sidebar-search-btn:hover { background: #1d4ed8; }

        /* Profile delete danger card */
        .profile-danger-card { border-color: #fecaca !important; }
        .profile-danger-title { color: #dc2626 !important; }
        .profile-danger-text { color: #6b7280; font-size: 14px; margin-bottom: 20px; }
    </style>
    @yield('styles')
</head>
<body>

    {{-- Shared Navigation and Footer --}}
    @include('layouts.shared-navbar')

    {{-- Header AdSense Banner — fullwidth sayfalar kendi header reklamlarını yönetiyor --}}
    @if(!$__env->hasSection('fullwidth'))
        <x-adsense location="header" />
    @endif

    {{-- Main Content --}}
    @if($__env->hasSection('fullwidth'))
    <main>
        @if(session('success'))
            <div class="flash-wrapper">
                <div class="flash-success">{{ session('success') }}</div>
            </div>
        @endif
        @if(session('error'))
            <div class="flash-wrapper">
                <div class="flash-error">{{ session('error') }}</div>
            </div>
        @endif
        @yield('content')
    </main>
    @else
    <div class="page-wrapper">
        <main>
            @if(session('success'))
                <div class="flash-success">{{ session('success') }}</div>
            @endif
            @if(session('error'))
                <div class="flash-error">{{ session('error') }}</div>
            @endif
            @yield('content')
        </main>

        @php
            $sidebarSettings = \App\Models\SidebarSetting::getAllCached();
            // Determine current module from available variables
            $currentModule = 'global';
            if (isset($blogCategories) || isset($recentBlogPosts)) $currentModule = 'blog';
            elseif (isset($forumCategories) || isset($recentForumTopics)) $currentModule = 'forum';
            elseif (isset($libraryCategories)) $currentModule = 'library';
            elseif (isset($interviewCategories)) $currentModule = 'interview';
            elseif (isset($chatCategories)) $currentModule = 'chat';
            $moduleSetting = $sidebarSettings[$currentModule] ?? null;
            $globalSetting = $sidebarSettings['global'] ?? null;
            $showSidebar = ($moduleSetting['is_enabled'] ?? true);
        @endphp

        @if($showSidebar)
        <aside class="sidebar">
            {{-- Search Widget --}}
            @if($moduleSetting['show_search'] ?? $globalSetting['show_search'] ?? true)
            <div class="sidebar-widget">
                <div class="widget-title">{{ $moduleSetting['title'] ?? $globalSetting['title'] ?? 'Site İçi Arama' }}</div>
                <form action="/search" method="GET">
                    <div class="sidebar-search-wrap">
                        <input type="text" name="q" value="{{ request('q') }}" placeholder="Blog, forum, sohbet ara..." class="sidebar-search-input">
                        <button type="submit" class="sidebar-search-btn">
                            <svg style="width:16px;height:16px" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"/>
                            </svg>
                        </button>
                    </div>
                </form>
            </div>
            @endif

            {{-- Dynamic Categories Widget --}}
            @if($moduleSetting['show_categories'] ?? $globalSetting['show_categories'] ?? true)
            @isset($chatCategories)
                <div class="sidebar-widget">
                    <div class="widget-title">Sohbet Kategorileri</div>
                    @foreach($chatCategories as $cat)
                        <a href="{{ route('chat.index') }}#{{ $cat->slug }}" class="category-item">
                            <div class="category-item-name">
                                <div class="category-dot" style="background: {{ $cat->color ?? '#3b82f6' }};"></div>
                                <span>{{ $cat->name }}</span>
                            </div>
                            <span class="category-count">{{ $cat->rooms_count ?? $cat->rooms?->count() ?? 0 }}</span>
                        </a>
                    @endforeach
                </div>
            @endisset

            @isset($blogCategories)
                <div class="sidebar-widget">
                    <div class="widget-title">Blog Kategorileri</div>
                    @foreach($blogCategories as $cat)
                        <a href="{{ route('blog.category', $cat->slug) }}" class="category-item">
                            <div class="category-item-name">
                                <div class="category-dot"></div>
                                <span>{{ $cat->name }}</span>
                            </div>
                            <span class="category-count">{{ $cat->posts_count ?? 0 }}</span>
                        </a>
                    @endforeach
                </div>
            @endisset

            @isset($forumCategories)
                <div class="sidebar-widget">
                    <div class="widget-title">Forum Kategorileri</div>
                    @foreach($forumCategories as $cat)
                        <a href="{{ route('forum.category', $cat) }}" class="category-item">
                            <div class="category-item-name">
                                <div class="category-dot"></div>
                                <span>{{ $cat->name }}</span>
                            </div>
                            <span class="category-count">{{ $cat->topics_count ?? 0 }}</span>
                        </a>
                    @endforeach
                </div>
            @endisset

            @isset($libraryCategories)
                <div class="sidebar-widget">
                    <div class="widget-title">Kütüphane Kategorileri</div>
                    @foreach($libraryCategories as $cat)
                        <a href="{{ route('library.category', $cat) }}" class="category-item">
                            <div class="category-item-name">
                                <div class="category-dot"></div>
                                <span>{{ $cat->name }}</span>
                            </div>
                            <span class="category-count">{{ $cat->contents_count ?? 0 }}</span>
                        </a>
                    @endforeach
                </div>
            @endisset

            @isset($interviewCategories)
                <div class="sidebar-widget">
                    <div class="widget-title">Mülakat Kategorileri</div>
                    @foreach($interviewCategories as $cat)
                        <a href="{{ route('interview.category', $cat) }}" class="category-item">
                            <div class="category-item-name">
                                <div class="category-dot"></div>
                                <span>{{ $cat->name }}</span>
                            </div>
                            <span class="category-count">{{ $cat->questions_count ?? 0 }}</span>
                        </a>
                    @endforeach
                </div>
            @endisset
            @endif

            {{-- Recent Blog Posts Widget --}}
            @if($moduleSetting['show_popular'] ?? $globalSetting['show_popular'] ?? false)
            @isset($recentBlogPosts)
                @if($recentBlogPosts->count() > 0)
                <div class="sidebar-widget">
                    <div class="widget-title">Son Blog Yazıları</div>
                    @foreach($recentBlogPosts as $post)
                        <a href="{{ route('blog.show', $post->slug) }}" class="sidebar-recent-post">
                            <div class="sidebar-recent-post-body">
                                <div class="sidebar-recent-post-title">{{ $post->title }}</div>
                                <div class="sidebar-recent-post-meta">{{ $post->published_at?->diffForHumans() }}</div>
                            </div>
                        </a>
                    @endforeach
                    <a href="{{ route('blog.index') }}" class="sidebar-more-link">Tüm Yazılar →</a>
                </div>
                @endif
            @endisset

            @isset($recentForumTopics)
                @if($recentForumTopics->count() > 0)
                <div class="sidebar-widget">
                    <div class="widget-title">Son Forum Konuları</div>
                    @foreach($recentForumTopics as $topic)
                        @if($topic->category)
                        <a href="{{ route('forum.topic', [$topic->category, $topic]) }}" class="sidebar-recent-topic">
                            <div class="sidebar-recent-topic-icon">💬</div>
                            <div class="sidebar-recent-topic-body">
                                <div class="sidebar-recent-topic-title">{{ $topic->title }}</div>
                                <div class="sidebar-recent-topic-meta">{{ $topic->user?->name }} · {{ $topic->reply_count ?? 0 }} yanıt</div>
                            </div>
                        </a>
                        @endif
                    @endforeach
                    <a href="{{ route('forum.index') }}" class="sidebar-more-link">Tüm Konular →</a>
                </div>
                @endif
            @endisset
            @endif

            {{-- User Widget --}}
            @if($moduleSetting['show_user_widget'] ?? $globalSetting['show_user_widget'] ?? true)
            @auth
            <div class="sidebar-widget user-widget">
                <div class="widget-title">Hesabım</div>
                <div class="user-avatar-lg">{{ substr(auth()->user()->name, 0, 1) }}</div>
                <div class="user-name">{{ auth()->user()->name }}</div>
                <div class="user-role">{{ ucfirst(auth()->user()->role ?? 'Üye') }}</div>
                <div class="quick-links">
                    <a href="{{ route('dashboard') }}" class="quick-link">
                        <svg style="width:16px;height:16px" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/>
                        </svg>
                        Dashboard
                    </a>
                    <a href="{{ route('profile.edit') }}" class="quick-link">
                        <svg style="width:16px;height:16px" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z"/>
                        </svg>
                        Profil Düzenle
                    </a>
                    <a href="{{ route('notifications.index') }}" class="quick-link">
                        <svg style="width:16px;height:16px" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 17h5l-1.405-1.405A2.032 2.032 0 0118 14.158V11a6.002 6.002 0 00-4-5.659V5a2 2 0 10-4 0v.341C7.67 6.165 6 8.388 6 11v3.159c0 .538-.214 1.055-.595 1.436L4 17h5m6 0v1a3 3 0 11-6 0v-1m6 0H9"/>
                        </svg>
                        Bildirimler
                    </a>
                    @if(auth()->user()->isAdmin())
                    <hr style="margin:8px 0;border:none;border-top:1px solid rgba(255,255,255,0.15);">
                    <a href="{{ route('admin.dashboard') }}" class="quick-link">
                        <svg style="width:16px;height:16px" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 12l2-2m0 0l7-7 7 7M5 10v10a1 1 0 001 1h3m10-11l2 2m-2-2v10a1 1 0 01-1 1h-3m-6 0a1 1 0 001-1v-4a1 1 0 011-1h2a1 1 0 011 1v4a1 1 0 001 1m-6 0h6"/>
                        </svg>
                        Admin Paneli
                    </a>
                    <a href="{{ route('admin.blog.index') }}" class="quick-link">
                        <svg style="width:16px;height:16px" 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>
                        Blog Yönet
                    </a>
                    @endif
                </div>
            </div>
            @endauth
            @endif

            {{-- Custom HTML Widget --}}
            @if(!empty($moduleSetting['custom_html'] ?? $globalSetting['custom_html'] ?? null))
            <div class="sidebar-widget">
                {!! $moduleSetting['custom_html'] ?? $globalSetting['custom_html'] !!}
            </div>
            @endif
        </aside>
    </div>
    @endif
    @endif

    {{-- Shared Footer --}}
    @include('layouts.shared-footer')

    @yield('scripts')

    @auth
    <script>
        // Poll for unread notification count every 60 seconds
        async function updateNotificationBadge() {
            try {
                const response = await fetch('/api/notifications/unread-count');
                const data = await response.json();
                const badge = document.getElementById('notification-badge');
                if (badge) {
                    if (data.count > 0) {
                        badge.textContent = data.count > 99 ? '99+' : data.count;
                        badge.style.display = 'inline';
                    } else {
                        badge.style.display = 'none';
                    }
                }
            } catch (e) {}
        }

        updateNotificationBadge();
        // Poll for unread notifications
        setInterval(updateNotificationBadge, 60000); // 60 saniye
    </script>
    @endauth
</body>
</html>
