<?php get_header(); ?>

<!-- Post Hero -->
<section class="post-hero">
    <?php if (has_post_thumbnail()) : ?>
        <div class="post-hero-image">
            <?php the_post_thumbnail('full'); ?>
        </div>
    <?php endif; ?>
    <div class="post-hero-content">
        <div class="container">
            <div class="post-meta">
                <span class="date">
                    <i class="far fa-calendar"></i>
                    <?php echo get_the_date('M j, Y'); ?>
                </span>
                <?php
                $categories = get_the_category();
                if ($categories) :
                ?>
                <span class="category">
                    <i class="far fa-folder"></i>
                    <?php echo esc_html($categories[0]->name); ?>
                </span>
                <?php endif; ?>
                <span class="author">
                    <i class="far fa-user"></i>
                    <?php the_author(); ?>
                </span>
            </div>
            <h1><?php the_title(); ?></h1>
        </div>
    </div>
</section>

<!-- Post Content -->
<section class="post-content">
    <div class="container">
        <div class="content-wrapper">
            <?php
            while (have_posts()) :
                the_post();
                the_content();
                
                // If comments are open or we have at least one comment, load up the comment template.
                if (comments_open() || get_comments_number()) :
                    comments_template();
                endif;
            endwhile;
            ?>
        </div>
        
        <!-- Post Navigation -->
        <div class="post-navigation">
            <?php
            $prev_post = get_previous_post();
            $next_post = get_next_post();
            
            if ($prev_post || $next_post) :
            ?>
                <div class="nav-links">
                    <?php if ($prev_post) : ?>
                        <a href="<?php echo get_permalink($prev_post); ?>" class="nav-previous">
                            <span class="nav-subtitle">Previous Post</span>
                            <span class="nav-title"><?php echo get_the_title($prev_post); ?></span>
                        </a>
                    <?php endif; ?>
                    
                    <?php if ($next_post) : ?>
                        <a href="<?php echo get_permalink($next_post); ?>" class="nav-next">
                            <span class="nav-subtitle">Next Post</span>
                            <span class="nav-title"><?php echo get_the_title($next_post); ?></span>
                        </a>
                    <?php endif; ?>
                </div>
            <?php endif; ?>
        </div>
        
        <!-- Related Posts -->
        <?php
        $categories = get_the_category();
        if ($categories) :
            $category_ids = array();
            foreach ($categories as $category) {
                $category_ids[] = $category->term_id;
            }
            
            $related_query = new WP_Query(array(
                'category__in' => $category_ids,
                'post__not_in' => array(get_the_ID()),
                'posts_per_page' => 3,
                'orderby' => 'rand'
            ));
            
            if ($related_query->have_posts()) :
        ?>
            <div class="related-posts">
                <h2>Related Posts</h2>
                <div class="related-grid">
                    <?php
                    while ($related_query->have_posts()) :
                        $related_query->the_post();
                        $image = get_the_post_thumbnail_url(get_the_ID(), 'medium_large');
                        if (!$image) {
                            $image = get_theme_file_uri('images/placeholder.jpg');
                        }
                    ?>
                        <article class="related-card">
                            <div class="related-image">
                                <img src="<?php echo esc_url($image); ?>" 
                                     alt="<?php echo esc_attr(get_the_title()); ?>" 
                                     loading="lazy">
                            </div>
                            <div class="related-content">
                                <h3><?php the_title(); ?></h3>
                                <a href="<?php the_permalink(); ?>" class="btn-read">Read More →</a>
                            </div>
                        </article>
                    <?php
                    endwhile;
                    wp_reset_postdata();
                    ?>
                </div>
            </div>
        <?php
            endif;
        endif;
        ?>
    </div>
</section>

<style>
/* Single Post Styles */
.post-hero {
    position: relative;
    height: 60vh;
    margin-bottom: 4rem;
    background-color: var(--dark);
}

.post-hero-image {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

.post-hero-image img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.post-hero-content {
    position: relative;
    height: 100%;
    display: flex;
    align-items: flex-end;
    padding-bottom: 3rem;
    background: linear-gradient(
        to bottom,
        rgba(0, 0, 0, 0) 0%,
        rgba(0, 0, 0, 0.7) 100%
    );
}

.post-meta {
    display: flex;
    gap: 1.5rem;
    margin-bottom: 1rem;
    color: rgba(255, 255, 255, 0.9);
}

.post-meta span {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.95rem;
}

.post-meta i {
    color: var(--primary);
}

.post-hero-content h1 {
    color: white;
    font-size: 3rem;
    margin: 0;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
    max-width: 800px;
}

.post-content {
    padding-bottom: 4rem;
}

.content-wrapper {
    max-width: 800px;
    margin: 0 auto;
    font-size: 1.1rem;
    line-height: 1.8;
    color: #444;
}

.content-wrapper p {
    margin-bottom: 1.5rem;
}

.content-wrapper h2,
.content-wrapper h3 {
    color: var(--dark);
    margin: 2rem 0 1rem;
}

.content-wrapper img {
    max-width: 100%;
    height: auto;
    border-radius: 8px;
    margin: 2rem 0;
}

.content-wrapper ul,
.content-wrapper ol {
    margin: 1.5rem 0;
    padding-left: 1.5rem;
}

.content-wrapper li {
    margin-bottom: 0.5rem;
}

.post-navigation {
    max-width: 800px;
    margin: 4rem auto;
    border-top: 1px solid #eee;
    border-bottom: 1px solid #eee;
    padding: 2rem 0;
}

.nav-links {
    display: flex;
    justify-content: space-between;
    gap: 2rem;
}

.nav-previous,
.nav-next {
    flex: 1;
    text-decoration: none;
    color: inherit;
}

.nav-next {
    text-align: right;
}

.nav-subtitle {
    display: block;
    font-size: 0.9rem;
    color: #666;
    margin-bottom: 0.5rem;
}

.nav-title {
    color: var(--dark);
    font-weight: 500;
    transition: color 0.3s ease;
}

.nav-previous:hover .nav-title,
.nav-next:hover .nav-title {
    color: var(--accent);
}

.related-posts {
    max-width: 1200px;
    margin: 4rem auto 0;
}

.related-posts h2 {
    text-align: center;
    margin-bottom: 2rem;
    color: var(--dark);
}

.related-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 2rem;
}

.related-card {
    background: white;
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 2px 15px rgba(0, 0, 0, 0.1);
    transition: transform 0.3s ease;
}

.related-card:hover {
    transform: translateY(-5px);
}

.related-image {
    width: 100%;
    height: 200px;
    position: relative;
    overflow: hidden;
}

.related-image img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: transform 0.3s ease;
}

.related-card:hover .related-image img {
    transform: scale(1.1);
}

.related-content {
    padding: 1.5rem;
}

.related-content h3 {
    font-size: 1.2rem;
    margin-bottom: 1rem;
    color: var(--dark);
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.btn-read {
    display: inline-block;
    padding: 0.8rem 1.5rem;
    background-color: var(--accent);
    color: white;
    text-decoration: none;
    border-radius: 4px;
    transition: background-color 0.3s ease;
}

.btn-read:hover {
    background-color: var(--accent-dark);
}

@media (max-width: 768px) {
    .post-hero {
        height: 50vh;
    }
    
    .post-hero-content h1 {
        font-size: 2rem;
    }
    
    .post-meta {
        flex-wrap: wrap;
        gap: 1rem;
    }
    
    .content-wrapper {
        padding: 0 1.5rem;
        font-size: 1rem;
    }
    
    .nav-links {
        flex-direction: column;
        gap: 1.5rem;
    }
    
    .nav-next {
        text-align: left;
    }
    
    .related-grid {
        grid-template-columns: 1fr;
        gap: 1.5rem;
    }
}
</style>

<?php get_footer(); ?> 