<?php
/**
 * Custom single post template (Theme Engine).
 * Renders: global header + article (category, title, meta, featured image,
 * content, tags) + global footer.
 *
 * Class hooks use conventional theme names (.entry, .entry-title,
 * .entry-content…) so the markup is indistinguishable from any hand-built
 * theme — the pushed CSS owns the visual presentation.
 */
if ( ! defined( 'ABSPATH' ) ) exit;

$lq_engine = new Linkquiver_Theme_Engine();
$lq_header = do_shortcode( $lq_engine->get_header_html() );
$lq_footer = do_shortcode( $lq_engine->get_footer_html() );
?><!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
    <meta charset="<?php bloginfo( 'charset' ); ?>">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>
<?php wp_body_open(); ?>
<?php echo $lq_header; ?>

<main class="site-main">
<?php if ( have_posts() ): while ( have_posts() ): the_post();
    $lq_cats        = get_the_category();
    $lq_primary_cat = ! empty( $lq_cats ) ? $lq_cats[0] : null;
?>
    <article class="entry" id="post-<?php the_ID(); ?>">
        <header class="entry-header">
            <div class="container">
                <?php if ( $lq_primary_cat ): ?>
                    <a class="entry-category" href="<?php echo esc_url( get_category_link( $lq_primary_cat ) ); ?>"><?php echo esc_html( $lq_primary_cat->name ); ?></a>
                <?php endif; ?>
                <h1 class="entry-title"><?php the_title(); ?></h1>
                <div class="entry-meta">
                    <time datetime="<?php echo esc_attr( get_the_date( 'c' ) ); ?>"><?php echo esc_html( get_the_date() ); ?></time>
                    <span class="entry-meta-sep" aria-hidden="true">·</span>
                    <span class="entry-author"><?php the_author(); ?></span>
                    <span class="entry-meta-sep" aria-hidden="true">·</span>
                    <span class="entry-reading-time"><?php
                        // str_word_count() splits on ASCII word chars only, so it
                        // undercounts French text (accents, apostrophes break
                        // words). Count Unicode word runs instead for an accurate
                        // reading time on accented content.
                        $lq_content    = wp_strip_all_tags( get_the_content() );
                        $lq_word_count = preg_match_all( '/[\p{L}\p{N}]+/u', $lq_content );
                        $lq_word_count = $lq_word_count ?: 0;
                        $lq_minutes    = max( 1, (int) ceil( $lq_word_count / 220 ) );
                        echo esc_html( sprintf( _n( '%d min read', '%d min read', $lq_minutes, 'linkquiver' ), $lq_minutes ) );
                    ?></span>
                </div>
            </div>
        </header>

        <?php if ( has_post_thumbnail() ): ?>
            <figure class="entry-featured">
                <?php the_post_thumbnail( 'full', array( 'loading' => 'eager', 'fetchpriority' => 'high' ) ); ?>
            </figure>
        <?php endif; ?>

        <div class="container">
            <div class="entry-content">
                <?php the_content(); ?>
            </div>

            <?php if ( has_tag() ): ?>
                <footer class="entry-tags">
                    <?php the_tags( '<ul class="entry-tag-list"><li>#', '</li><li>#', '</li></ul>' ); ?>
                </footer>
            <?php endif; ?>
        </div>
    </article>
<?php endwhile; endif; ?>
</main>

<?php echo $lq_footer; ?>
<?php wp_footer(); ?>
</body>
</html>
