<div class="container">
    <?php
    // Check if the Featured Post section is enabled
    $is_featured_enabled = get_theme_mod('nino_enable_featured_post', true);

    if ($is_featured_enabled) {
        // Get the selected category ID from the Customizer
        $selected_category_id = get_theme_mod('nino_selected_post_category');

        if ($selected_category_id) {
            // Fetch the selected category details
            $category = get_category($selected_category_id);

            if ($category) {
                // Get the custom category title from the Customizer
                $custom_title = get_theme_mod('nino_custom_category_title', '');

                // Display the custom title if available, otherwise display the category name
                echo '<h2 class="category-title">' . esc_html(!empty($custom_title) ? $custom_title : $category->name) . '</h2>';

                // Query posts from the selected category
                $args = array(
                    'posts_per_page' => 5,
                    'post_type'      => 'post',
                    'cat'            => $category->term_id,
                );

                $category_query = new WP_Query($args);

                if ($category_query->have_posts()): ?>
                    <div class="row custom-loop">
                        <?php
                        $count = 1;
                        while ($category_query->have_posts()):
                            $category_query->the_post();

                            // First post with a larger column size
                            if ($count == 1): ?>
                                <div class="col-md-6 featured-posts">
                                    <figure>
                                        <a href="<?php the_permalink(); ?>">
                                            <?php if (has_post_thumbnail()):
                                                the_post_thumbnail();
                                            else: ?>
                                                <img src="<?php echo esc_url(get_template_directory_uri() . "/img/dummy-750.png"); ?>">
                                            <?php endif; ?>
                                        </a>
                                        <div class="info">
                                            <h3>
                                                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                            </h3>
                                        </div><!--info-->
                                    </figure>
                                    <div class="box-shadow"></div>
                                </div><!--Featured-Posts-->
                            <?php endif; ?>

                            <?php
                            // Open wrapper for posts 2–5
                            if ($count == 2): ?>
                                <div class="col-md-6 additional-posts-container d-flex flex-wrap">
                            <?php endif; ?>

                            <?php if ($count > 1): ?>
                                <div class="col-md-6 col-lg-6 col-sm-12 featured-posts">
                                    <figure>
                                        <a href="<?php the_permalink(); ?>">
                                            <?php if (has_post_thumbnail()):
                                                the_post_thumbnail();
                                            else: ?>
                                                <img src="<?php echo esc_url(get_template_directory_uri() . "/img/dummy-750.png"); ?>">
                                            <?php endif; ?>
                                        </a>
                                        <div class="info">
                                            <h3>
                                                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                            </h3>
                                        </div><!--info-->
                                    </figure>
                                    <div class="box-shadow"></div>
                                </div><!--Featured-Posts-->
                            <?php endif; ?>

                            <?php
                            // Close wrapper after post 5
                            if ($count == 5): ?>
                                </div><!--additional-posts-container-->
                            <?php endif;

                            $count++;
                        endwhile; ?>
                    </div>
                <?php else: ?>
                    <p>No posts available in this category.</p>
                <?php endif;

                wp_reset_postdata();
            }
        }
    }
    ?>
</div>