<?php
/**
 * Single Post Template - Last Day Last Trump Ministries (LDLT)
 * Theme: Twenty Twenty-One Child  (file name: single.php)
 *
 * Features: hero, article, share buttons, author box, FAQ accordion,
 * prev/next, comments, sidebar with Recent Posts / Categories / Book CTA.
 */

get_header();

/* ------------------------------------------------------------
   FAQ DATA
   Option A (per post): add a Custom Field named  ldlt_faqs  and write
   one FAQ per line in this format:   Question? | Answer text
   (Enable it in the post editor: top-right menu > Preferences >
   Panels > Custom Fields.)
   Option B (all posts): edit the default list below. It is shown
   when a post has no ldlt_faqs custom field.
   ------------------------------------------------------------ */
$default_faqs = array(
	array( 'What is the central idea of this commentary?', 'Daniel\'s prophecies, the Olivet Discourse and the Book of Revelation are read side by side as one chronology of events, sequence and time.' ),
	array( 'Do you present other schools of interpretation?', 'Yes. Idealism, Preterism, Historicism and Futurism are each outlined fairly before the LDLT view is made.' ),
	array( 'Where can I get the book?', 'The commentary is available as an instant EPUB or PDF download from the Shop page.' ),
);

function ldlt_get_faqs( $post_id, $default ) {
	$raw  = get_post_meta( $post_id, 'ldlt_faqs', true );
	$faqs = array();
	if ( $raw ) {
		foreach ( preg_split( '/\r\n|\r|\n/', $raw ) as $line ) {
			$parts = explode( '|', $line, 2 );
			if ( count( $parts ) === 2 && trim( $parts[0] ) !== '' && trim( $parts[1] ) !== '' ) {
				$faqs[] = array( trim( $parts[0] ), trim( $parts[1] ) );
			}
		}
	}
	return ! empty( $faqs ) ? $faqs : $default;
}

function ldlt_read_time( $post_id ) {
	$words = str_word_count( wp_strip_all_tags( get_post_field( 'post_content', $post_id ) ) );
	return max( 1, (int) ceil( $words / 200 ) );
}
function ldlt_first_cat_name( $post_id ) {
	$cats = get_the_category( $post_id );
	return ! empty( $cats ) ? $cats[0]->name : 'Article';
}

while ( have_posts() ) :
	the_post();
	$pid       = get_the_ID();
	$faqs      = ldlt_get_faqs( $pid, $default_faqs );
	$blog_page = get_page_by_path( 'blog' );
	$blog_url  = $blog_page ? get_permalink( $blog_page ) : home_url( '/' );
	$share_url = rawurlencode( get_permalink() );
	$share_ttl = rawurlencode( get_the_title() );
	?>

<style>
/* ============================================================
   LDLT SINGLE POST (scoped to .ldlt-single)
   Fonts are inherited from your site/theme (same headings and
   paragraph fonts as the rest of the website).
   ============================================================ */
.ldlt-single{
	--navy-900:#0a1428; --navy-800:#0f1d3a; --navy-700:#16284d;
	--gold:#c9a24b; --gold-light:#e3c776;
	--cream:#f6f1e6; --ink:#1b2438; --muted:#6b7488; --white:#fff;
	--radius:14px;
	background:var(--cream); color:var(--ink); line-height:1.7; width:100%;
}
.ldlt-single *{box-sizing:border-box}
.ldlt-single a{text-decoration:none}
.ldlt-single img{max-width:100%;height:auto}
.ldlt-single button{font-family:inherit}
.ls-container{width:min(1180px,92%);margin-inline:auto}

/* Hero */
.ls-hero{position:relative;overflow:hidden;text-align:center;color:#fff;padding:90px 0 120px;
	background:radial-gradient(circle at 70% 20%,#1d3566 0%,var(--navy-800) 45%,var(--navy-900) 100%)}
.ls-hero::before{content:"";position:absolute;width:420px;height:420px;border-radius:50%;right:-90px;top:-110px;
	background:radial-gradient(circle,rgba(227,199,118,.35),rgba(227,199,118,0) 70%)}
.ls-crumbs{font-size:.8rem;color:#b9c3dc;margin-bottom:20px}
.ls-crumbs a{color:var(--gold-light)}
.ls-eyebrow{display:block;font-size:.78rem;letter-spacing:.35em;text-transform:uppercase;color:var(--gold-light);font-weight:600;margin-bottom:16px}
.ldlt-single .ls-hero h1{color:#fff;font-size:clamp(2.2rem,5vw,3.9rem);line-height:1.12;max-width:900px;margin:0 auto 22px;font-weight:700}
.ls-meta{display:flex;justify-content:center;flex-wrap:wrap;gap:8px 20px;color:#b9c3dc;font-size:.88rem}
.ls-meta span+span::before{content:"·";margin-right:20px;color:var(--gold)}

/* Layout */
.ls-wrap{display:grid;grid-template-columns:minmax(0,1fr) 340px;gap:48px;margin-top:-60px;padding-bottom:90px;position:relative}
.ls-article{background:var(--white);border-radius:var(--radius);padding:50px 56px;box-shadow:0 20px 50px rgba(10,20,40,.12)}
.ls-feature-img{margin:-50px -56px 36px;border-radius:var(--radius) var(--radius) 0 0;overflow:hidden}
.ls-feature-img img{width:100%;display:block;max-height:440px;object-fit:cover}

/* Article content */
.ls-content{font-size:1.06rem;color:#2b3550}
.ls-content p{margin:0 0 1.4em}
.ls-content h2{font-size:2.1rem;line-height:1.2;color:var(--navy-800);margin:1.8em 0 .6em}
.ls-content h3{font-size:1.55rem;color:var(--navy-700);margin:1.6em 0 .5em}
.ls-content a{color:var(--gold);text-decoration:underline}
.ls-content ul,.ls-content ol{margin:0 0 1.4em 1.3em}
.ls-content li{margin-bottom:.5em}
.ls-content blockquote{margin:32px 0;padding:26px 30px;background:var(--cream);border-left:4px solid var(--gold);border-radius:0 var(--radius) var(--radius) 0;font-style:italic;color:var(--navy-800)}
.ls-content blockquote p:last-child{margin-bottom:0}
.ls-content img{border-radius:12px;margin:10px 0}
.ls-content figure{margin:28px 0}
.ls-content figcaption{font-size:.85rem;color:var(--muted);text-align:center;margin-top:8px}
.ls-content table{width:100%;border-collapse:collapse;margin-bottom:1.4em}
.ls-content th,.ls-content td{border:1px solid #e3dcc7;padding:10px 14px;text-align:left}
.ls-content th{background:var(--cream)}

/* Tags + share */
.ls-tags{display:flex;flex-wrap:wrap;gap:10px;margin-top:36px}
.ls-tags a{font-size:.78rem;padding:6px 16px;border:1px solid #d9cfb4;border-radius:50px;color:var(--navy-700);transition:.2s}
.ls-tags a:hover{background:var(--gold);border-color:var(--gold);color:var(--navy-900)}
.ls-share{display:flex;align-items:center;flex-wrap:wrap;gap:10px;margin-top:28px;padding-top:24px;border-top:1px solid #eee}
.ls-share strong{font-size:.8rem;letter-spacing:.2em;text-transform:uppercase;color:var(--navy-800);margin-right:6px}
.ls-share a,.ls-share button{font-size:.78rem;padding:8px 16px;border-radius:50px;border:1px solid #d9cfb4;background:var(--white);color:var(--navy-700);cursor:pointer;transition:.2s}
.ls-share a:hover,.ls-share button:hover{background:var(--navy-800);border-color:var(--navy-800);color:var(--gold-light)}

/* Author */
.ls-author{display:flex;gap:20px;align-items:center;margin-top:36px;padding:26px;border-radius:var(--radius);background:var(--cream)}
.ls-author img{border-radius:50%;width:72px;height:72px;flex:0 0 72px}
.ldlt-single .ls-author h4{font-size:1.3rem;color:var(--navy-800);margin:0}
.ls-author p{margin:4px 0 0;font-size:.92rem;color:var(--muted)}

/* FAQ accordion */
.ls-faq{margin-top:56px}
.ls-faq-label{display:block;font-size:.72rem;letter-spacing:.3em;color:var(--gold);font-weight:600;text-transform:uppercase}
.ldlt-single .ls-faq h2{font-size:2.1rem;color:var(--navy-800);margin:6px 0 24px}
.ls-faq-item{border:1px solid #e3dcc7;border-radius:12px;margin-bottom:12px;overflow:hidden;background:var(--white);transition:.25s}
.ls-faq-item.open{border-color:var(--gold);box-shadow:0 10px 26px rgba(10,20,40,.08)}
.ls-faq-q{width:100%;display:flex;justify-content:space-between;align-items:center;gap:16px;text-align:left;padding:18px 22px;background:transparent;border:0;cursor:pointer;font-size:1.05rem;font-weight:600;color:var(--navy-800)}
.ls-faq-q:hover{color:var(--gold)}
.ls-faq-icon{flex:0 0 30px;height:30px;border-radius:50%;background:var(--cream);position:relative;transition:.25s}
.ls-faq-icon::before,.ls-faq-icon::after{content:"";position:absolute;left:50%;top:50%;width:12px;height:2px;background:var(--navy-800);transform:translate(-50%,-50%);transition:.25s}
.ls-faq-icon::after{transform:translate(-50%,-50%) rotate(90deg)}
.ls-faq-item.open .ls-faq-icon{background:var(--gold)}
.ls-faq-item.open .ls-faq-icon::after{transform:translate(-50%,-50%) rotate(0)}
.ls-faq-a{max-height:0;overflow:hidden;transition:max-height .35s ease}
.ls-faq-a-inner{padding:0 22px 20px;color:#2b3550}

/* Prev / Next */
.ls-nav{display:grid;grid-template-columns:1fr 1fr;gap:18px;margin-top:48px}
.ls-nav a{display:block;padding:20px 22px;border:1px solid #e3dcc7;border-radius:12px;transition:.25s;color:var(--navy-800)}
.ls-nav a:hover{border-color:var(--gold);transform:translateY(-3px)}
.ls-nav small{display:block;font-size:.7rem;letter-spacing:.22em;text-transform:uppercase;color:var(--gold);font-weight:600;margin-bottom:4px}
.ls-nav .next{text-align:right}

/* Comments */
.ls-comments{margin-top:50px}

/* Sidebar */
.ls-side{display:flex;flex-direction:column;gap:26px;align-self:start;position:sticky;top:30px}
.ls-card{background:var(--white);border-radius:var(--radius);padding:28px;box-shadow:0 14px 36px rgba(10,20,40,.08)}
.ldlt-single .ls-card h3{font-size:1.5rem;color:var(--navy-800);margin:0 0 16px;padding-bottom:12px;border-bottom:2px solid var(--gold);display:inline-block}
.ls-search{display:flex;gap:8px}
.ls-search input{flex:1;min-width:0;padding:11px 16px;border-radius:50px;border:1px solid #d9cfb4;outline:none;font-family:inherit}
.ls-search input:focus{border-color:var(--gold)}
.ls-search button{padding:0 18px;border:0;border-radius:50px;background:var(--gold);color:var(--navy-900);font-weight:600;cursor:pointer}
.ls-recent{list-style:none;margin:0;padding:0}
.ls-recent li{display:flex;gap:14px;padding:14px 0;border-bottom:1px solid #eee}
.ls-recent li:last-child{border:0;padding-bottom:0}
.ls-recent-thumb{flex:0 0 76px;height:76px;border-radius:10px;overflow:hidden;background:linear-gradient(135deg,var(--navy-700),var(--navy-900));display:flex;align-items:center;justify-content:center;color:var(--gold-light);font-size:.7rem;text-align:center;padding:4px}
.ls-recent-thumb img{width:100%;height:100%;object-fit:cover;margin:0}
.ls-recent h4{font-size:1.02rem;line-height:1.3;margin:0 0 4px}
.ldlt-single .ls-recent h4 a{color:var(--navy-800)}
.ldlt-single .ls-recent h4 a:hover{color:var(--gold)}
.ls-recent small{color:var(--muted);font-size:.78rem}
.ls-cats{list-style:none;margin:0;padding:0}
.ls-cats li{display:flex;justify-content:space-between;padding:10px 0;border-bottom:1px solid #eee;font-size:.95rem}
.ls-cats li:last-child{border:0}
.ls-cats a{color:var(--navy-800)}
.ls-cats a:hover{color:var(--gold)}
.ls-cats span{color:var(--muted);font-size:.82rem}
.ls-book{background:linear-gradient(160deg,var(--navy-700),var(--navy-900));color:#fff;text-align:center}
.ldlt-single .ls-book h3{color:#fff;border-color:var(--gold)}
.ls-book p{font-size:.9rem;color:#c3cde6;margin:0 0 16px}
.ls-price{display:block;color:var(--gold-light);font-size:.85rem;letter-spacing:.08em;margin-bottom:14px}
.ls-btn{display:inline-block;padding:12px 28px;border-radius:50px;background:var(--gold);color:var(--navy-900)!important;font-weight:600;font-size:.82rem;letter-spacing:.08em;text-transform:uppercase;transition:.25s}
.ls-btn:hover{background:var(--gold-light);transform:translateY(-2px)}

/* CTA band */
.ls-cta{background:linear-gradient(135deg,var(--gold),var(--gold-light));padding:64px 0;text-align:center;color:var(--navy-900)}
.ldlt-single .ls-cta h2{font-size:clamp(1.8rem,4vw,2.6rem);margin:0 0 10px;color:var(--navy-900)}
.ls-cta p{max-width:540px;margin:0 auto 24px}
.ls-cta .ls-btn{background:var(--navy-900);color:#fff!important}
.ls-cta .ls-btn:hover{background:var(--navy-700)}

/* Responsive */
@media(max-width:1020px){
	.ls-wrap{grid-template-columns:1fr}
	.ls-side{position:static}
}
@media(max-width:640px){
	.ls-article{padding:30px 22px}
	.ls-feature-img{margin:-30px -22px 26px}
	.ls-nav{grid-template-columns:1fr}
	.ls-nav .next{text-align:left}
	.ls-author{flex-direction:column;text-align:center}
	.ls-hero{padding:70px 0 100px}
}
</style>

<div class="ldlt-single">

	<!-- ============ HERO ============ -->
	<section class="ls-hero">
		<div class="ls-container">
			<div class="ls-crumbs">
				<a href="<?php echo esc_url( home_url( '/' ) ); ?>">Home</a> &nbsp;/&nbsp;
				<a href="<?php echo esc_url( $blog_url ); ?>">Blog</a>
			</div>
			<span class="ls-eyebrow"><?php echo esc_html( ldlt_first_cat_name( $pid ) ); ?></span>
			<h1><?php the_title(); ?></h1>
			<div class="ls-meta">
				<span>By <?php the_author(); ?></span>
				<span><?php echo esc_html( get_the_date( 'd F Y' ) ); ?></span>
				<span><?php echo (int) ldlt_read_time( $pid ); ?> min read</span>
			</div>
		</div>
	</section>

	<!-- ============ ARTICLE + SIDEBAR ============ -->
	<div class="ls-container ls-wrap">

		<article class="ls-article" id="post-<?php the_ID(); ?>">

			<?php if ( has_post_thumbnail() ) : ?>
				<div class="ls-feature-img"><?php the_post_thumbnail( 'large' ); ?></div>
			<?php endif; ?>

			<div class="ls-content">
				<?php the_content(); ?>
				<?php wp_link_pages( array( 'before' => '<p><strong>Pages:</strong> ', 'after' => '</p>' ) ); ?>
			</div>

			<?php $tags = get_the_tags(); if ( $tags ) : ?>
				<div class="ls-tags">
					<?php foreach ( $tags as $t ) : ?>
						<a href="<?php echo esc_url( get_tag_link( $t->term_id ) ); ?>">#<?php echo esc_html( $t->name ); ?></a>
					<?php endforeach; ?>
				</div>
			<?php endif; ?>

			<div class="ls-share">
				<strong>Share</strong>
				<a href="https://www.facebook.com/sharer/sharer.php?u=<?php echo $share_url; ?>" target="_blank" rel="noopener">Facebook</a>
				<a href="https://twitter.com/intent/tweet?url=<?php echo $share_url; ?>&text=<?php echo $share_ttl; ?>" target="_blank" rel="noopener">X</a>
				<a href="https://wa.me/?text=<?php echo $share_ttl . '%20' . $share_url; ?>" target="_blank" rel="noopener">WhatsApp</a>
				<button type="button" id="lsCopy" data-url="<?php echo esc_attr( get_permalink() ); ?>">Copy Link</button>
			</div>

			<div class="ls-author">
				<?php echo get_avatar( get_the_author_meta( 'ID' ), 72 ); ?>
				<div>
					<h4><?php the_author(); ?></h4>
					<p><?php echo esc_html( get_the_author_meta( 'description' ) ?: 'Author at Last Day Last Trump Ministries.' ); ?></p>
				</div>
			</div>

			<!-- ============ FAQ ACCORDION ============ -->
			<?php if ( ! empty( $faqs ) ) : ?>
			<section class="ls-faq">
				<span class="ls-faq-label">Questions &amp; Answers</span>
				<h2>Frequently Asked Questions</h2>
				<div id="lsFaq">
					<?php foreach ( $faqs as $i => $faq ) : ?>
						<div class="ls-faq-item">
							<button type="button" class="ls-faq-q" aria-expanded="false" aria-controls="faq-a-<?php echo (int) $i; ?>">
								<span><?php echo esc_html( $faq[0] ); ?></span>
								<span class="ls-faq-icon" aria-hidden="true"></span>
							</button>
							<div class="ls-faq-a" id="faq-a-<?php echo (int) $i; ?>" role="region">
								<div class="ls-faq-a-inner"><?php echo wp_kses_post( wpautop( $faq[1] ) ); ?></div>
							</div>
						</div>
					<?php endforeach; ?>
				</div>
			</section>

			<?php
			/* FAQ structured data for Google rich results */
			$schema = array(
				'@context'   => 'https://schema.org',
				'@type'      => 'FAQPage',
				'mainEntity' => array(),
			);
			foreach ( $faqs as $faq ) {
				$schema['mainEntity'][] = array(
					'@type'          => 'Question',
					'name'           => wp_strip_all_tags( $faq[0] ),
					'acceptedAnswer' => array( '@type' => 'Answer', 'text' => wp_strip_all_tags( $faq[1] ) ),
				);
			}
			?>
			<script type="application/ld+json"><?php echo wp_json_encode( $schema, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ); ?></script>
			<?php endif; ?>

			<!-- ============ PREV / NEXT ============ -->
			<?php
			$prev = get_previous_post();
			$next = get_next_post();
			if ( $prev || $next ) : ?>
				<div class="ls-nav">
					<?php if ( $prev ) : ?>
						<a href="<?php echo esc_url( get_permalink( $prev ) ); ?>"><small>&larr; Previous</small><?php echo esc_html( get_the_title( $prev ) ); ?></a>
					<?php else : ?><span></span><?php endif; ?>
					<?php if ( $next ) : ?>
						<a class="next" href="<?php echo esc_url( get_permalink( $next ) ); ?>"><small>Next &rarr;</small><?php echo esc_html( get_the_title( $next ) ); ?></a>
					<?php endif; ?>
				</div>
			<?php endif; ?>

			<!-- ============ COMMENTS ============ -->
			<?php if ( comments_open() || get_comments_number() ) : ?>
				<div class="ls-comments"><?php comments_template(); ?></div>
			<?php endif; ?>
		</article>

		<!-- ============ SIDEBAR ============ -->
		<aside class="ls-side">

			<div class="ls-card">
				<h3>Search</h3>
				<form class="ls-search" role="search" method="get" action="<?php echo esc_url( home_url( '/' ) ); ?>">
					<input type="search" name="s" placeholder="Search articles..." value="<?php echo esc_attr( get_search_query() ); ?>">
					<button type="submit">Go</button>
				</form>
			</div>

			<div class="ls-card">
				<h3>Recent Blogs</h3>
				<ul class="ls-recent">
					<?php
					$recent = new WP_Query( array(
						'posts_per_page'      => 5,
						'post_status'         => 'publish',
						'post__not_in'        => array( $pid ),
						'ignore_sticky_posts' => true,
					) );
					if ( $recent->have_posts() ) :
						while ( $recent->have_posts() ) : $recent->the_post(); ?>
							<li>
								<a class="ls-recent-thumb" href="<?php the_permalink(); ?>">
									<?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'thumbnail' ); } else { echo esc_html( ldlt_first_cat_name( get_the_ID() ) ); } ?>
								</a>
								<div>
									<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
									<small><?php echo esc_html( get_the_date( 'd M Y' ) ); ?></small>
								</div>
							</li>
						<?php endwhile;
					else : ?>
						<li>No other posts yet.</li>
					<?php endif; wp_reset_postdata(); ?>
				</ul>
			</div>

			<div class="ls-card">
				<h3>Categories</h3>
				<ul class="ls-cats">
					<?php
					$cats = get_categories( array( 'hide_empty' => true ) );
					foreach ( $cats as $c ) :
						if ( 'uncategorized' === $c->slug ) { continue; } ?>
						<li><a href="<?php echo esc_url( get_category_link( $c->term_id ) ); ?>"><?php echo esc_html( $c->name ); ?></a><span><?php echo (int) $c->count; ?></span></li>
					<?php endforeach; ?>
				</ul>
			</div>

			<div class="ls-card ls-book">
				<h3>Get the Commentary</h3>
				<p>About 280 pages. Full exegesis, all 12 questions and complete cross-references between Daniel, Olivet and Revelation.</p>
				<span class="ls-price">e-Book · $12.00 AUD</span>
				<a class="ls-btn" href="<?php echo esc_url( home_url( '/shop/' ) ); ?>">Buy the Book</a>
			</div>

		</aside>
	</div>

	<!-- ============ CTA ============ -->
	<section class="ls-cta">
		<div class="ls-container">
			<h2>Available Instantly in EPUB and PDF</h2>
			<p>Trace the full chronology with original diagrams and decide for yourself.</p>
			<a class="ls-btn" href="<?php echo esc_url( home_url( '/shop/' ) ); ?>">Buy The Book</a>
		</div>
	</section>
</div>

<script>
(function () {
	/* FAQ accordion: one open at a time */
	var items = document.querySelectorAll('#lsFaq .ls-faq-item');
	items.forEach(function (item) {
		var btn = item.querySelector('.ls-faq-q');
		var panel = item.querySelector('.ls-faq-a');
		btn.addEventListener('click', function () {
			var isOpen = item.classList.contains('open');
			items.forEach(function (o) {
				o.classList.remove('open');
				o.querySelector('.ls-faq-q').setAttribute('aria-expanded', 'false');
				o.querySelector('.ls-faq-a').style.maxHeight = null;
			});
			if (!isOpen) {
				item.classList.add('open');
				btn.setAttribute('aria-expanded', 'true');
				panel.style.maxHeight = panel.scrollHeight + 'px';
			}
		});
	});
	/* Open the first FAQ by default */
	if (items.length) { items[0].querySelector('.ls-faq-q').click(); }

	/* Copy link button */
	var copy = document.getElementById('lsCopy');
	if (copy) {
		copy.addEventListener('click', function () {
			var url = copy.getAttribute('data-url');
			var done = function () { copy.textContent = 'Copied!'; setTimeout(function () { copy.textContent = 'Copy Link'; }, 1800); };
			if (navigator.clipboard) { navigator.clipboard.writeText(url).then(done); }
			else { var t = document.createElement('textarea'); t.value = url; document.body.appendChild(t); t.select(); document.execCommand('copy'); document.body.removeChild(t); done(); }
		});
	}
})();
</script>

<?php endwhile; ?>

<?php get_footer(); ?>