@php
    $fmt = fn ($value, $decimals = 2) => is_numeric($value) ? number_format((float) $value, $decimals, ',', '.') : '-';
    $int = fn ($value) => is_numeric($value) ? number_format((float) $value, 0, ',', '.') : '-';
    $overview = $summary['overview'] ?? [];
    $settings = $templateSettings ?? [];
    $sections = $templateSections ?? [];
    $primary = $settings['primary_color'] ?? '#111827';
    $accent = $settings['accent_color'] ?? '#ef5f86';
    $secondary = $settings['secondary_color'] ?? '#f8fafc';
    $text = $settings['text_color'] ?? '#111827';
    $muted = $settings['muted_color'] ?? '#6b7280';
    $coverStyle = $settings['cover_style'] ?? 'gradient';
    $coverBg = $settings['cover_background_url'] ?? '';
    $coverCss = match($coverStyle) {
        'minimal' => "background:#fff;color:{$text};border-bottom:6px solid {$accent};",
        'image' => $coverBg ? "background:linear-gradient(135deg, rgba(17,24,39,.82), rgba(17,24,39,.52)), url('{$coverBg}') center/cover no-repeat;color:#fff;" : "background:linear-gradient(135deg, {$primary}, {$accent});color:#fff;",
        'dark' => "background:#050816;color:#fff;border-bottom:6px solid {$accent};",
        default => "background:linear-gradient(135deg, {$primary}, {$accent});color:#fff;",
    };
    $tableLimit = (int) ($settings['table_limit'] ?? 20);
@endphp
<!doctype html>
<html lang="tr">
<head>
    <meta charset="utf-8">
    <title>{{ $settings['agency_name'] ?? 'DW Ads Reporter' }} - {{ $report->client?->company_name }}</title>
    <style>
        *{box-sizing:border-box}body{font-family:Arial,Helvetica,sans-serif;color:{{ $text }};margin:0;background:{{ $secondary }}}.page{max-width:1120px;margin:0 auto;background:#fff;min-height:100vh}.cover{padding:42px 48px;{{ $coverCss }}}.brand{font-size:13px;letter-spacing:.08em;text-transform:uppercase;color:{{ $coverStyle === 'minimal' ? $accent : '#fff' }};font-weight:700}.title{font-size:34px;margin:12px 0 6px}.subtitle{font-size:15px;color:{{ $coverStyle === 'minimal' ? $muted : '#e5e7eb' }};line-height:1.55}.intro{margin-top:18px;max-width:780px;color:{{ $coverStyle === 'minimal' ? $muted : '#f3f4f6' }}}.content{padding:34px 48px}.grid{display:grid;grid-template-columns:repeat(4,1fr);gap:14px}.card{border:1px solid #e5e7eb;border-radius:12px;padding:16px;background:#fff}.label{font-size:12px;color:{{ $muted }};margin-bottom:8px}.value{font-size:24px;font-weight:700;color:{{ $primary }}}.section{margin-top:32px}.section h2{font-size:22px;margin:0 0 12px;color:{{ $text }}}.notes{background:#fffbeb;border:1px solid #fde68a;border-radius:12px;padding:16px}.ai{background:#eef2ff;border:1px solid #c7d2fe;border-radius:12px;padding:18px}.ai h3{margin:16px 0 8px;font-size:16px}.notes li,.ai li{margin-bottom:8px}.table{width:100%;border-collapse:collapse;font-size:12px}.table th,.table td{border-bottom:1px solid #e5e7eb;padding:9px 8px;text-align:left;vertical-align:top}.table th{background:#f9fafb;color:#374151}.muted{color:{{ $muted }}}.footer{padding:20px 48px;color:{{ $muted }};font-size:11px;border-top:1px solid #e5e7eb}.logo{max-height:54px;max-width:180px;margin-bottom:18px}.client-logo{max-height:46px;max-width:150px;margin-left:18px}.compact .section{margin-top:22px}.compact .card{padding:12px}.compact .value{font-size:20px}@media print{body{background:#fff}.page{max-width:none}.card{break-inside:avoid}.section{break-inside:avoid}}
    </style>
</head>
<body>
<div class="page {{ !empty($settings['compact_mode']) ? 'compact' : '' }}">
    @if(($sections['cover'] ?? true) && ($settings['include_pdf_cover'] ?? true))
        <div class="cover">
            <div>
                @if(($settings['show_agency_logo'] ?? true) && !empty($settings['agency_logo_url']))<img class="logo" src="{{ $settings['agency_logo_url'] }}" alt="Ajans Logo">@endif
                @if(($settings['show_client_logo'] ?? true) && !empty($settings['client_logo_url']))<img class="client-logo" src="{{ $settings['client_logo_url'] }}" alt="Müşteri Logo">@endif
            </div>
            <div class="brand">{{ $settings['agency_name'] ?? 'DW Ads Reporter' }}</div>
            <div class="title">{{ $settings['cover_title'] ?? 'Dijital Performans Raporu' }}</div>
            <div class="subtitle">{{ $report->client?->company_name }} · {{ $report->date_from?->format('d.m.Y') }} - {{ $report->date_to?->format('d.m.Y') }}</div>
            @if(!empty($settings['cover_subtitle']))<div class="intro">{{ $settings['cover_subtitle'] }}</div>@endif
        </div>
    @endif

    <div class="content">
        @if(!empty($settings['intro_text']))<p class="muted">{{ $settings['intro_text'] }}</p>@endif

        @if($sections['overview_cards'] ?? true)
            <div class="grid">
                <div class="card"><div class="label">Toplam Reklam Harcaması</div><div class="value">{{ $fmt($overview['total_ad_spend'] ?? 0) }}</div></div>
                <div class="card"><div class="label">Reklam Tıklaması</div><div class="value">{{ $int($overview['total_ad_clicks'] ?? 0) }}</div></div>
                <div class="card"><div class="label">Toplam Dönüşüm</div><div class="value">{{ $fmt($overview['total_conversions'] ?? 0) }}</div></div>
                <div class="card"><div class="label">Organik Tıklama</div><div class="value">{{ $int($overview['organic_clicks'] ?? 0) }}</div></div>
            </div>
        @endif

        @if(($sections['ai_summary'] ?? true) && $report->hasAiInsights())
            <div class="section ai">
                <h2>AI Yönetici Özeti</h2>
                <p>{{ $report->ai_executive_summary }}</p>
                @if($report->ai_customer_note)<p><strong>Müşteri Notu:</strong> {{ $report->ai_customer_note }}</p>@endif
                <h3>Riskler</h3><ul>@foreach(($report->ai_risks ?? []) as $item)<li>{{ $item }}</li>@endforeach</ul>
                <h3>Fırsatlar</h3><ul>@foreach(($report->ai_opportunities ?? []) as $item)<li>{{ $item }}</li>@endforeach</ul>
                <h3>Aksiyon Listesi</h3><ol>@foreach(($report->ai_actions ?? []) as $item)<li>{{ $item }}</li>@endforeach</ol>
            </div>
        @elseif($sections['executive_summary'] ?? true)
            <div class="section notes">
                <h2>Yönetici Özeti</h2>
                <ul>@foreach(($summary['notes'] ?? []) as $note)<li>{{ $note }}</li>@endforeach</ul>
            </div>
        @endif

        @include('admin.reports.print.sections', ['dataset' => $dataset, 'fmt' => $fmt, 'int' => $int, 'sections' => $sections, 'tableLimit' => $tableLimit])
    </div>

    @if($sections['footer'] ?? true)
        <div class="footer">
            {{ $settings['footer_note'] ?? 'Bu rapor DW Ads Reporter tarafından otomatik oluşturulmuştur.' }}
            @if(!empty($settings['contact_email'])) · {{ $settings['contact_email'] }} @endif
            @if(!empty($settings['website_url'])) · {{ $settings['website_url'] }} @endif
            @if($settings['show_data_timestamp'] ?? true) Veri çekim zamanı: {{ $dataset['generated_at'] ?? now()->toDateTimeString() }} @endif
        </div>
    @endif
</div>
</body>
</html>
