@if(!empty($post->videolink))
    @php
        // Extract YouTube ID inline without redeclaring a function
        preg_match("/(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/watch\?v=|youtu\.be\/)([\w\-]+)/", $post->videolink, $matches);
        $videoID = $matches[1] ?? '';
    @endphp

    @if($videoID)
        <div class="videoiframe-container">
            <iframe 
                src="https://www.youtube.com/embed/{{ $videoID }}" 
                title="YouTube video player" 
                frameborder="0" 
                allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" 
                allowfullscreen>
            </iframe>
        </div>
    @endif
@endif

{{-- Styles for responsive video --}}
<style>
.videoiframe-container {
    position: relative;
    padding-bottom: 56.25%; /* 16:9 ratio */
    height: 0;
    overflow: hidden;
    max-width: 100%;
    margin: 20px 0;
}

.videoiframe-container iframe {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}
</style>
