@php
    $width = $layout['svg']['width'] ?? 1080;
    $height = $layout['svg']['height'] ?? 1520;
    $background = $layout['background'] ?? '#FFFFFF';
    $gradient = $layout['background_gradient'] ?? null;
    $gradientId = $gradient ? 'bg-gradient-'.uniqid() : null;
    $accent = $layout['accent'] ?? '#000000';
    $secondary = $layout['secondary'] ?? '#E5E7EB';
    $textColor = $layout['text'] ?? '#111827';
    $badgeColor = $layout['badge'] ?? $accent;
    $instructions = $layout['instructions'] ?? [];
    $description = $layout['description'] ?? '';
    $subtitle = $layout['subtitle'] ?? '';
    $headline = $layout['headline'] ?? $eventName;
    $badgeLabel = $layout['badge_label'] ?? 'Digitale Gästebox';
    $instructionsHeading = $layout['instructions_heading'] ?? "So funktioniert's";
    $linkHeading = $layout['link_heading'] ?? 'Alternative zum Einscannen';
    $ctaLabel = $layout['cta_label'] ?? 'Scan mich & starte direkt';
    $linkLabel = $layout['link_label'] ?? $tokenUrl;
    $logoUrl = $layout['logo_url'] ?? null;
    $qrSize = $layout['qr']['size_px'] ?? 500;
    $qrX = max($width - $qrSize - 120, 520);
    $qrY = 420;
    $qrPanelX = $qrX - 40;
    $qrPanelY = $qrY - 40;
    $qrPanelWidth = $qrSize + 80;
    $qrPanelHeight = $qrSize + 200;
    $linkBoxY = $qrY + $qrSize + 30;
    $ctaTextY = $qrPanelY + $qrPanelHeight - 50;
    $titleLines = explode("\n", wordwrap($headline, 18, "\n", true));
    $subtitleLines = $subtitle !== '' ? explode("\n", wordwrap($subtitle, 36, "\n", true)) : [];
    $descriptionLines = $description !== '' ? explode("\n", wordwrap($description, 40, "\n", true)) : [];
    $instructionStartY = 870;
    $instructionSpacing = 56;
    if ($gradient) {
        $angle = (float) ($gradient['angle'] ?? 180);
        $angleRad = deg2rad($angle);
        $x1 = 0.5 - cos($angleRad) / 2;
        $y1 = 0.5 - sin($angleRad) / 2;
        $x2 = 0.5 + cos($angleRad) / 2;
        $y2 = 0.5 + sin($angleRad) / 2;
        $x1Attr = number_format($x1, 4, '.', '');
        $y1Attr = number_format($y1, 4, '.', '');
        $x2Attr = number_format($x2, 4, '.', '');
        $y2Attr = number_format($y2, 4, '.', '');
    }
@endphp
<svg width="{{ $width }}" height="{{ $height }}" viewBox="0 0 {{ $width }} {{ $height }}" xmlns="http://www.w3.org/2000/svg">
    <defs>
        @if($gradientId)
            <linearGradient id="{{ $gradientId }}" x1="{{ $x1Attr }}" y1="{{ $y1Attr }}" x2="{{ $x2Attr }}" y2="{{ $y2Attr }}">
                @php
                    $stops = $gradient['stops'] ?? [];
                    $stopCount = max(count($stops) - 1, 1);
                @endphp
                @foreach($stops as $index => $stopColor)
                    @php
                        $offset = $stopCount > 0 ? ($index / $stopCount) * 100 : 0;
                    @endphp
                    <stop offset="{{ number_format($offset, 2, '.', '') }}%" stop-color="{{ $stopColor }}"/>
                @endforeach
            </linearGradient>
        @endif
    </defs>

    <style>
        .title-line {
            font-family: 'Montserrat', 'Helvetica Neue', Arial, sans-serif;
            font-size: 82px;
            font-weight: 700;
            letter-spacing: -1px;
        }
        .subtitle-line {
            font-family: 'Lora', 'Georgia', serif;
            font-size: 32px;
            font-weight: 500;
        }
        .description-line {
            font-family: 'Montserrat', 'Helvetica Neue', Arial, sans-serif;
            font-size: 30px;
            font-weight: 400;
            line-height: 1.4;
        }
        .badge-text {
            font-family: 'Montserrat', 'Helvetica Neue', Arial, sans-serif;
            font-size: 28px;
            font-weight: 600;
            letter-spacing: 4px;
            text-transform: uppercase;
        }
        .instruction-bullet {
            font-family: 'Montserrat', 'Helvetica Neue', Arial, sans-serif;
            font-size: 30px;
            font-weight: 600;
        }
        .instruction-text {
            font-family: 'Montserrat', 'Helvetica Neue', Arial, sans-serif;
            font-size: 30px;
            font-weight: 400;
        }
        .small-label {
            font-family: 'Montserrat', 'Helvetica Neue', Arial, sans-serif;
            font-size: 28px;
            font-weight: 600;
            letter-spacing: 3px;
            text-transform: uppercase;
        }
        .link-text {
            font-family: 'Courier New', monospace;
            font-size: 30px;
        }
        .footer-text {
            font-family: 'Montserrat', 'Helvetica Neue', Arial, sans-serif;
            font-size: 26px;
            font-weight: 400;
        }
        .footer-strong {
            font-weight: 700;
        }
    </style>

    <rect x="0" y="0" width="{{ $width }}" height="{{ $height }}" fill="{{ $gradientId ? 'url(#'.$gradientId.')' : $background }}" />

    <rect x="70" y="380" width="500" height="600" rx="46" fill="rgba(255,255,255,0.78)" />

    <rect x="{{ $qrPanelX }}" y="{{ $qrPanelY }}" width="{{ $qrPanelWidth }}" height="{{ $qrPanelHeight }}" rx="40" fill="rgba(255,255,255,0.88)" />
    <rect x="{{ $qrPanelX + 20 }}" y="{{ $linkBoxY + 80 }}" width="{{ $qrPanelWidth - 40 }}" height="8" rx="4" fill="{{ $accent }}" opacity="0.45" />

    <rect x="80" y="120" width="250" height="70" rx="35" fill="{{ $badgeColor }}" />
    <text x="205" y="165" text-anchor="middle" fill="#FFFFFF" class="badge-text">{{ e($badgeLabel) }}</text>

    @if($logoUrl)
        <image href="{{ $logoUrl }}" x="840" y="90" width="180" height="120" preserveAspectRatio="xMidYMid meet" />
    @endif

    @foreach($titleLines as $index => $line)
        <text x="80" y="{{ 260 + $index * 88 }}" fill="{{ $textColor }}" class="title-line">{{ e($line) }}</text>
    @endforeach

    @php
        $subtitleOffset = 260 + count($titleLines) * 88 + 40;
    @endphp
    @foreach($subtitleLines as $index => $line)
        <text x="80" y="{{ $subtitleOffset + $index * 44 }}" fill="{{ $secondary }}" class="subtitle-line">{{ e($line) }}</text>
    @endforeach

    @php
        $descriptionOffset = $subtitleOffset + (count($subtitleLines) ? count($subtitleLines) * 44 + 60 : 40);
    @endphp
    @foreach($descriptionLines as $index => $line)
        <text x="110" y="{{ $descriptionOffset + $index * 48 }}" fill="{{ $textColor }}" class="description-line">{{ e($line) }}</text>
    @endforeach

    <text x="120" y="760" fill="{{ $accent }}" class="small-label">{{ e(mb_strtoupper($instructionsHeading)) }}</text>

    @foreach($instructions as $index => $step)
        @php
            $lineY = $instructionStartY + $index * $instructionSpacing;
        @endphp
        <circle cx="120" cy="{{ $lineY - 18 }}" r="10" fill="{{ $accent }}" />
        <text x="150" y="{{ $lineY }}" fill="{{ $textColor }}" class="instruction-text">{{ e($step) }}</text>
    @endforeach

    <text x="{{ $qrPanelX + 40 }}" y="{{ $linkBoxY }}" fill="{{ $accent }}" class="small-label">{{ e(mb_strtoupper($linkHeading)) }}</text>
    <rect x="{{ $qrPanelX + 30 }}" y="{{ $linkBoxY + 30 }}" width="{{ $qrPanelWidth - 60 }}" height="140" rx="26" fill="rgba(0,0,0,0.08)" />
    <text x="{{ $qrPanelX + 50 }}" y="{{ $linkBoxY + 118 }}" fill="{{ $textColor }}" class="link-text">{{ e($linkLabel) }}</text>

    <image href="{{ $qrPngDataUri }}" x="{{ $qrX }}" y="{{ $qrY }}" width="{{ $qrSize }}" height="{{ $qrSize }}" />

    <text x="{{ $qrPanelX + ($qrPanelWidth / 2) }}" y="{{ $ctaTextY }}" text-anchor="middle" fill="{{ $accent }}" class="small-label">{{ e(mb_strtoupper($ctaLabel)) }}</text>

    <text x="120" y="{{ $height - 160 }}" fill="rgba(17,24,39,0.6)" class="footer-text">
        <tspan class="footer-strong" fill="{{ $accent }}">{{ e(config('app.name', 'Fotospiel')) }}</tspan>
        &nbsp;– Gästebox & Fotochallenges
    </text>
    <text x="{{ $width - 120 }}" y="{{ $height - 160 }}" text-anchor="end" fill="rgba(17,24,39,0.6)" class="footer-text">
        Einladung gültig: {{ $token->expires_at ? $token->expires_at->locale(app()->getLocale())->isoFormat('LLL') : 'bis Widerruf' }}
    </text>
</svg>
