#!/usr/bin/env python3
# Book 38 (Bajaj Auto) — THE LITTLE ENGINE OF A BILLION JOURNEYS. Science->business: the tiny four-stroke
# engine (SUCK, SQUEEZE, BANG, BLOW -> a tiny explosion turned into spin) that is small and light, so it SIPS
# fuel — a rupee of petrol carries you far -> the maker of motorcycles and three-wheelers that move a billion
# everyday journeys and ride out to many countries.
import os
ART="/root/omahaindia.com/comics/site/book/bajajauto/art"
OUT="/root/omahaindia.com/comics/site/book/bajajauto/index.html"
STYLE=("children's storybook manga illustration, warm painted animation style, soft cinematic light, "
"clean linework, NO text NO letters NO words")
CHARLOTTE=("Charlotte, the SAME young Indian CHILD girl from the reference, round young face, warm tan skin, "
"freckles, glossy black hair in two low twin ponytails, big curious dark eyes, bright TEAL t-shirt, NO facial hair")
GRAHAM="Ben, a kind scholarly elderly American gentleman, round wire spectacles, tweed suit and bow tie"
BUFFETT="Warren, a folksy cheerful elderly American man, thinning white hair, big friendly glasses, grey suit"
MUNGER="Charlie, a stocky wise elderly American man, very thick black-framed glasses, dark suit"
HANK=("Hank, a warm friendly Western ENGINE BUILDER, NOT Indian, sturdy build, blue coveralls, a shiny metal "
"piston in his palm, a flat cap, silver stubble, warm proud smile")

# name -> (w,h,seed,prompt,ref). ref set -> Seedream character; None -> flux scene. Ritual panels copied by mkbook.
NEW={
 "00_cover":(1024,1180,3800,f"{STYLE}. BOOK COVER, {CHARLOTTE} standing with {HANK} in a bright engine workshop, holding up a tiny gleaming motorcycle engine that glows warmly from within like a captured spark, rows of shiny little motorcycles and cheerful three-wheelers behind them, a road curling to a horizon of many far countries, wonder, cinematic","charlotte"),
 "_ref_hank":(1024,1024,3801,f"{STYLE}. clean friendly character portrait, {HANK}, plain soft warm background, kind smile, head and shoulders",None),
 # Ch1
 "ba_c1_coin":(1024,720,3810,f"{STYLE}. {CHARLOTTE} at home, Grandpa Ben's glowing brass compass twitching between a tiny toy motorcycle and a single glowing golden drop of fuel on the table, EXPRESSION puzzled and intrigued, cozy lamp light","charlotte"),
 "ba_c1_diary":(1024,720,3811,f"{STYLE}. an open magical diary glowing warm gold on a desk, a faint ribbon of light rising from the page and curling into a tiny spinning engine wheel, no people",None),
 "ba_c1_swirl":(1024,720,3812,f"{STYLE}. {CHARLOTTE} pulled into a swirling spiral of golden light and drifting sparks and gears from a glowing diary, EXPRESSION thrilled, magical","charlotte"),
 # Ch2 — arrive at the engine workshop, meet Hank
 "ba_c2_arrive":(1024,720,3813,f"{STYLE}. {CHARLOTTE} landing wide-eyed in a bright sunlit engine workshop, gleaming little motorcycles and cheerful three-wheelers lined up in neat rows, tools and shiny engines everywhere, EXPRESSION awe and delight, warm daylight","charlotte"),
 "ba_c2_hank":(1024,720,3814,f"{STYLE}. {HANK} warmly welcoming {CHARLOTTE} in the workshop, tipping his flat cap and holding out a shiny piston, EXPRESSION Charlotte shy happy smile; keep the SAME engine builder from the reference, same blue coveralls, same flat cap, same piston","hank"),
 "ba_c2_yard":(1024,720,3815,f"{STYLE}. a big cheerful workshop yard full of shiny new motorcycles and bright three-wheeler auto-rickshaws lined up ready to roll out, sunbeams and clean floors, no faces close up",None),
 # Ch3 — Suck & Squeeze: the first two strokes
 "ba_c3_suck":(1024,720,3816,f"{STYLE}. {HANK} kneeling to show {CHARLOTTE} a see-through toy engine, the piston sliding DOWN and drinking in a swirling puff of glowing fuel-and-air mist through an open valve, EXPRESSION Charlotte fascinated leaning in; SAME engine builder, blue coveralls, flat cap","hank"),
 "ba_c3_squeeze":(1024,720,3817,f"{STYLE}. a magical cutaway of one engine cylinder, the piston pushing UP and squeezing the glowing fuel-air mist into a tiny tight bright pocket at the top, arrows of pressure, no people",None),
 "ba_c3_valve":(1024,720,3818,f"{STYLE}. a friendly close up of an engine valve opening like a little round door to let a puff of glowing fuel-air mist breathe into the cylinder, playful science, no people",None),
 # Ch4 — Bang: the tiny explosion that pushes
 "ba_c4_spark":(1024,720,3819,f"{STYLE}. {CHARLOTTE} watching in wonder as a tiny spark plug flashes and the squeezed fuel goes BANG in a burst of warm orange light inside the see-through cylinder, EXPRESSION amazed and thrilled, dramatic glow","charlotte"),
 "ba_c4_push":(1024,720,3820,f"{STYLE}. a magical cutaway of a cylinder, a small bright orange explosion shoving the piston straight DOWN with force, glowing energy lines, no people",None),
 "ba_c4_crank":(1024,720,3821,f"{STYLE}. a beautiful close up of a piston's downward push turning a crankshaft, straight motion becoming round spinning motion, golden gleaming metal, no people",None),
 # Ch5 — Blow: clear out, then the spin turns the wheel
 "ba_c5_blow":(1024,720,3822,f"{STYLE}. {HANK} showing {CHARLOTTE} how the piston rises again to BLOW the smoky burnt air out through the exhaust valve in a little puff, EXPRESSION Charlotte delighted 'aha'; SAME engine builder, blue coveralls, flat cap","hank"),
 "ba_c5_wheel":(1024,720,3823,f"{STYLE}. a spinning crankshaft turning a bright chain that turns a motorcycle's back wheel round and round, gleaming mechanism, warm workshop light, no people",None),
 # Ch6 — small & light means it sips fuel, a rupee of petrol carries you far
 "ba_c6_light":(1024,720,3824,f"{STYLE}. {CHARLOTTE} easily lifting a small light motorcycle engine in both hands beside a huge heavy truck engine she cannot budge, EXPRESSION surprised and pleased, bright workshop","charlotte"),
 "ba_c6_sip":(1024,720,3825,f"{STYLE}. a single tiny drop of golden fuel carrying a happy little motorcycle along a very long winding road over hills into the distance, playful, no people",None),
 # Ch7 — a billion everyday journeys, and exports to many countries
 "ba_c7_journeys":(1024,720,3826,f"{STYLE}. a joyful bustling scene of motorcycles and cheerful three-wheeler rickshaws carrying people to work, to school, to market, everyday journeys everywhere, warm togetherness, no faces close up",None),
 "ba_c7_exports":(1024,720,3827,f"{STYLE}. a warm storybook world map, cheerful little motorcycles and three-wheelers riding out from India along glowing routes to many far countries across the seas, gentle and grand, no people",None),
 # Ch8 — reveal Bajaj Auto
 "ba_c8_reveal":(1024,720,3828,f"{STYLE}. {CHARLOTTE} and {HANK} standing proudly before a vast bright vehicle factory with motorcycles and three-wheelers rolling off the line against a blue sky, EXPRESSION proud recognition; SAME engine builder, blue coveralls, flat cap","charlotte"),
 "ba_c8_history":(1024,720,3829,f"{STYLE}. warm sepia mid-century scene, workers proudly building an early scooter and a small three-wheeler in an old workshop, hopeful faces and old machines, inspiring, no text",None),
 "ba_c8_scale":(1024,720,3830,f"{STYLE}. a mighty modern vehicle factory at golden hour, robotic arms and long rows of gleaming motorcycles, ships and trucks carrying them out to the world, proud and grand, no people",None),
 # Ch10 (ch9 uses ritual castle/invert/verdict; ch10 uses ritual wake/diary)
 "ba_c10_look":(1024,720,3831,f"{STYLE}. {CHARLOTTE} outside at dusk watching motorcycles and little three-wheelers hum happily past her on the road, knowing each one hides a tiny four-stroke engine sipping fuel, EXPRESSION delighted wonder","charlotte"),
}

def P(img,cap=None,b1=None,b2=None,sfx=None,sfxc="#e8425a",tall=False): return ("panel",img,cap,b1,b2,sfx,sfxc,tall)
def SUB(d,t): return ("sub",d,t)
def WOW(t): return ("wow",t)
def NOTE(t,b): return ("note",t,b)
def TL(r): return ("timeline",r)
def MOAT(c): return ("moat",c)

CHAPTERS=[
 ("c1","CHAPTER 1","The Toy That Wanted Fuel","pages 1-8",[
   P("ba_c1_coin","Back home, Grandpa Ben's compass twitched again — pointing between a little toy motorcycle and a single golden drop of fuel.","A tiny bike… and a drop of petrol? What's the link?",None,"twitch"),
   P("ba_c1_diary","The old diary fell open. A new line shimmered in Grandpa Ben's hand.",None,'"Find the little engine that turns one small bang into a long, long ride."'),
   WOW("A toy motorcycle and a drop of fuel don't look like much — yet millions of people climb onto machines just like this every morning and ride to work, school and market. What tiny thing is hiding inside that turns a drop of petrol into a whole journey? That's the puzzle of this adventure."),
   P("ba_c1_swirl","Golden light swirled up and pulled Charlotte in.","Here we GO!",None,"WHOOSH","#c8621f"),
   NOTE("Charlotte's Secret Notebook","Two BIG questions:<br>➊ How can one little drop of fuel push a motorcycle so far?<br>➋ Grandpa Ben says a tiny <i>engine</i> does it with a little <i>bang</i>. How does a bang turn into a smooth ride? 🛵"),
 ]),
 ("c2","CHAPTER 2","The Workshop of Little Engines","pages 9-16",[
   P("ba_c2_arrive","One blink, and she stood in a bright, busy workshop — rows of gleaming motorcycles and cheerful little three-wheelers as far as she could see.","It's a whole world of tiny machines!"),
   P("ba_c2_hank","A cheerful man in blue coveralls tipped his cap. \"I'm Hank, an engine builder. Down here we make one small explosion do a very big job.\"","A small… EXPLOSION?!"),
   P("ba_c2_yard","All around, shiny new motorcycles and bright three-wheelers waited in neat rows, ready to roll out into the world.","So many of them — and they all run on that little bang?","Stick with me, kiddo."),
   NOTE("Charlotte's Secret Notebook","Hank is an engine builder, and he grins like he has a secret — just like the other guides did. He says a tiny engine turns one small explosion into a whole ride. Okay Hank… show me how! ✨"),
 ]),
 ("c3","CHAPTER 3","Suck, Then Squeeze","pages 17-26",[
   P("ba_c3_suck","Hank opened a see-through engine. \"First, SUCK. The piston slides down and the engine breathes IN a puff of fuel mixed with air.\"","The engine takes a big breath — like me!"),
   P("ba_c3_squeeze","\"Then, SQUEEZE. The piston pushes back up and packs that fuel-and-air into a tiny, tight little pocket.\"","Squished into a teeny space!",None,"squeeze","#37414d"),
   P("ba_c3_valve","\"Little doors called valves open and shut to let the breath in — and later, let the smoke out.\"","So the engine has doors AND lungs!"),
   WOW("Here are the first two strokes! 💨 <b>SUCK</b>: the piston drops and the engine breathes in fuel + air. <b>SQUEEZE</b>: the piston pushes up and packs it into a tiny tight pocket. Squeezing fuel and air together is what makes the next step so powerful."),
   ("diagram",),
   NOTE("Charlotte's Secret Notebook","Stroke ➊ SUCK — piston down, engine breathes in fuel + air. Stroke ➋ SQUEEZE — piston up, packs it tight. Little valves are the doors that open and close. 🫁"),
 ]),
 ("c4","CHAPTER 4","BANG! The Little Explosion","pages 27-36",[
   P("ba_c4_spark","A tiny spark plug flashed. The squeezed fuel went BANG in a burst of orange light. Charlotte jumped.","A real explosion — a TINY one!"),
   P("ba_c4_push","\"That little bang shoves the piston down HARD,\" said Hank. \"That's the push that does all the work.\"","The explosion is doing the pushing!",None,"BANG","#c8621f"),
   P("ba_c4_crank","\"And a clever bent rod — the crankshaft — turns that straight push into round, spinning motion.\"","Straight push… turns into SPIN!"),
   WOW("Here's the heart of it! 💥 <b>BANG</b>: a spark lights the squeezed fuel and it explodes, shoving the piston down hard. A bent rod called the <b>crankshaft</b> turns that straight shove into <b>spinning</b> — and spinning is what a wheel needs. One tiny explosion, turned into turning."),
   NOTE("Charlotte's Secret Notebook · SOLVED (part 1)","Stroke ➌ BANG — a spark lights the packed fuel, it explodes, and the blast shoves the piston down. The crankshaft turns that push into SPIN. A bang becomes a wheel-turn! 💥"),
 ]),
 ("c5","CHAPTER 5","Blow It Out, and Round It Goes","pages 37-44",[
   P("ba_c5_blow","\"Last, BLOW,\" said Hank. \"The piston rises again and puffs the burnt smoke out the door — then it starts all over: suck, squeeze, bang, blow.\"","Four little steps, round and round!"),
   P("ba_c5_wheel","The spinning crankshaft pulled a chain, and the chain spun the motorcycle's wheel round and round.","So THAT'S how the wheel turns!"),
   WOW("The fourth stroke is <b>BLOW</b>: the piston pushes the burnt smoke out so fresh fuel can come in — then the whole cycle repeats, hundreds of times a minute. The spin runs a chain, the chain spins the wheel, and off you ride. Suck → squeeze → bang → blow → spin → GO!"),
   NOTE("Charlotte's Secret Notebook · SOLVED!","Stroke ➍ BLOW — piston up, puffs out the smoke. Then it repeats super fast! The spinning crankshaft turns a chain, the chain turns the wheel. SUCK → SQUEEZE → BANG → BLOW → SPIN → GO! 🛞"),
 ]),
 ("c6","CHAPTER 6","Small, Light, and Thirsty for Almost Nothing","pages 45-52",[
   P("ba_c6_light","Charlotte lifted the little engine easily — then tried the giant truck engine and couldn't budge it.","This one's so LITTLE and light!"),
   P("ba_c6_sip","\"Because it's small and light,\" said Hank, \"it only SIPS fuel. One little drop carries you a long, long way.\"","A tiny sip — and you go for miles!"),
   WOW("Here's the clever part: a <b>small, light</b> engine doesn't need much fuel to move a light motorcycle. It <b>sips</b> instead of gulps — so a single rupee of petrol carries you far. That's called being <b>efficient</b>: getting a lot of journey out of a little fuel."),
   NOTE("Charlotte's Secret Notebook","A small light engine SIPS fuel instead of gulping it — so a rupee of petrol carries you far. That's efficiency: lots of journey from a little fuel. Perfect for a whole country on the move. ⛽"),
 ]),
 ("c7","CHAPTER 7","A Billion Everyday Journeys","pages 53-60",[
   P("ba_c7_journeys","Hank swept his arm across a bustling town. Motorcycles and little three-wheelers carried people to work, to school, to market.","EVERYONE'S ride has a little engine in it?!"),
   P("ba_c7_exports","\"And it doesn't stop here,\" said Hank. \"These little machines ride out to many, many countries across the sea.\"","Our little engines travel the whole WORLD!"),
   WOW("This isn't a small thing. Cheap, thirsty-for-almost-nothing motorcycles and three-wheelers move a <b>billion everyday journeys</b> — and the very same machines are shipped to <b>many countries</b> around the world. A tiny four-stroke engine, quietly carrying a whole planet of people to where they need to go."),
   NOTE("Charlotte's Secret Notebook","The little engine moves a BILLION everyday journeys — work, school, market — and rides out to many countries as EXPORTS. Small, cheap, efficient… and everywhere. 🌏🛵"),
 ]),
 ("c8","CHAPTER 8 · THE BIG DISCOVERY","The Maker of the Little Engine","pages 61-70",[
   P("ba_c8_reveal","Hank led Charlotte to a viewpoint over an enormous factory, motorcycles and three-wheelers rolling out by the thousand.","Who MAKES all of these?"),
   P("ba_c8_history","\"Long ago,\" said Hank, \"a company here started building simple little scooters and three-wheelers for ordinary people to afford.\"","Made for everyone — not just the rich!"),
   P("ba_c8_scale","\"It grew and grew — making so many, so cheaply, that its little machines now sell across the world.\"","From a small workshop… to the whole planet!"),
   WOW("A \"company\" can be a giant shared job. <b>Bajaj Auto</b> grew from building simple, affordable little machines into one of the world's great makers of <b>motorcycles and three-wheelers</b> — selling them cheaply at home and shipping them to many countries abroad."),
   TL([("Long ago","A company begins making simple, affordable scooters and three-wheelers."),
       ("Over decades","It makes so many, so cheaply, that ordinary families across India can ride."),
       ("Today","Its motorcycles and three-wheelers ride out as exports to many countries."),
       ("Tomorrow","It learns new journeys too — quieter, cleaner electric machines. ⚡")]),
   NOTE("Charlotte's Secret Notebook","Bajaj Auto makes motorcycles and three-wheelers — cheap enough for everyday families, and good enough to sell all over the world. From one workshop to a billion journeys. 🏆"),
 ]),
 ("c9","CHAPTER 9","The Castle Built of Little Engines","pages 71-80",[
   P("ba_c9_castle","Two kind old men appeared with a glow. Warren pointed at a castle standing over a busy road of motorcycles. \"What keeps rivals out, Charlotte?\"","Walls! But what kind?"),
   MOAT([("🏭","Wall One: Cheap at Giant Scale","It makes so many engines and machines that each one costs very little to build. A newcomer making just a few can never match that low price.",False),
         ("🌍","Wall Two: Riding the World","It sells to many countries, not just one. If one market slows down, the others keep the wheels turning.",False),
         ("🤝","Wall Three: A Trusted Name","Families have trusted this name for their rides for generations. Trust like that takes a very long time to earn.",False),
         ("⚠️","The Crack in the Wall","Everyone else wants the very same rider. Many rival makers chase the same customers, so it must keep its machines cheap, reliable — and keep improving — or riders will choose someone else.",True)]),
   P("ba_c9_munger","Charlie tapped his head. \"Don't just ask how to WIN. Ask how you'd LOSE — then don't do that. Invert!\"","Think backwards?"),
   WOW("<b>Charlie's Inversion:</b> to keep an engine-maker strong, imagine how it could FADE — letting its machines get costly or unreliable, ignoring rivals, or missing the switch to cleaner electric rides — then carefully avoid each one, and keep making little engines cheaper and better."),
   P("ba_c9_verdict","Warren nodded. \"It builds cheaply at giant scale, sells around the world, and carries a trusted name — a mighty castle, so long as it keeps out-riding its rivals.\"","I SEE the whole picture!"),
   NOTE("Charlotte's Secret Notebook · The Big Secret","Bajaj Auto is hard to copy: it makes engines CHEAP at giant scale, it EXPORTS to many countries, and it has a TRUSTED name. But everyone wants the same rider, so it must stay cheap and keep improving. Charlie's trick: picture how you'd lose (costly, careless, too slow), then never do it. Invert! 🏰"),
 ]),
 ("c10","CHAPTER 10","The Little Engine Everywhere","pages 81-88",[
   P("ba_c10_wake","Charlotte woke in her own bed. The busy workshop was gone… but the little toy motorcycle on her desk gleamed softly.","Was it real? It FELT real…"),
   P("ba_c10_diary","On the diary's page, a new line glowed — as if Grandpa Ben wrote it just for her.",None,'"Now you hear the little bang inside every ride. Well done."'),
   P("ba_c10_look","She ran outside at dusk. Every motorcycle and every little three-wheeler humming past held the same tiny four-stroke heart — suck, squeeze, bang, blow.","There's a little explosion in EVERY one — I just couldn't hear it!"),
   WOW("Next time a motorcycle hums past, you'll know its secret: a <b>tiny four-stroke engine</b> — suck, squeeze, bang, blow — turning one small explosion into spin, sipping barely any fuel so a rupee carries you far. That's <b>the little engine of a billion journeys.</b> 🛵💨"),
   NOTE("Charlotte's Secret Notebook · What I Learned","➊ A tiny engine works in four strokes: SUCK, SQUEEZE, BANG, BLOW.<br>➋ One small explosion pushes a piston; the crankshaft turns it into SPIN → the wheel turns.<br>➌ Small and light means it SIPS fuel — a rupee of petrol carries you far.<br>➍ These little machines move a BILLION journeys and export to many countries.<br>➎ Bajaj Auto builds them cheap at giant scale; but Charlie's trick — picture how you'd lose (costly, careless, too slow), then keep out-riding rivals. 💛"),
 ]),
]

HOOK=("ba_c1_swirl","EVERY ORDINARY THING HIDES A SECRET.",
 "Charlotte heard the little bang inside every ride. But the compass is twitching again — toward a quiet new scooter that hums along with no fuel and no bang at all. Where does THAT silent power come from, and can it carry us just as far?",
 "NEXT ADVENTURE · The Quiet Engine →")

DIAGRAM='''<div class="panel wide" style="background:#fbf3ec;">
<svg viewBox="0 0 820 380"><rect width="820" height="380" fill="#fbf3ec"/>
<text x="410" y="36" text-anchor="middle" font-family="Bangers" font-size="24" fill="#d64541">THE FOUR STROKES OF A TINY ENGINE</text>
<g><rect x="70" y="70" width="80" height="150" rx="10" fill="#fff" stroke="#171717" stroke-width="4"/><rect x="76" y="168" width="68" height="28" rx="4" fill="#8fb3d9" stroke="#37414d" stroke-width="2"/><text x="110" y="62" text-anchor="middle" font-family="Baloo 2" font-size="15" fill="#2aa15a">SUCK</text><text x="110" y="245" text-anchor="middle" font-family="Nunito" font-size="12" fill="#555">piston drops,</text><text x="110" y="262" text-anchor="middle" font-family="Nunito" font-size="12" fill="#555">breathes in fuel+air</text></g>
<g stroke="#37414d" stroke-width="5" fill="#37414d"><line x1="158" y1="140" x2="200" y2="140" stroke-dasharray="8 6"/><path d="M196 132 L212 140 L196 148Z"/></g>
<g><rect x="220" y="70" width="80" height="150" rx="10" fill="#fff" stroke="#171717" stroke-width="4"/><rect x="226" y="86" width="68" height="28" rx="4" fill="#8fb3d9" stroke="#37414d" stroke-width="2"/><text x="260" y="62" text-anchor="middle" font-family="Baloo 2" font-size="15" fill="#37414d">SQUEEZE</text><text x="260" y="245" text-anchor="middle" font-family="Nunito" font-size="12" fill="#555">piston up, packs it</text><text x="260" y="262" text-anchor="middle" font-family="Nunito" font-size="12" fill="#555">into a tight pocket</text></g>
<g stroke="#37414d" stroke-width="5" fill="#37414d"><line x1="308" y1="140" x2="350" y2="140" stroke-dasharray="8 6"/><path d="M346 132 L362 140 L346 148Z"/></g>
<g><rect x="370" y="70" width="80" height="150" rx="10" fill="#fff" stroke="#171717" stroke-width="4"/><circle cx="410" cy="100" r="16" fill="#f6b53f" stroke="#c8621f" stroke-width="3"/><rect x="376" y="176" width="68" height="28" rx="4" fill="#8fb3d9" stroke="#37414d" stroke-width="2"/><text x="410" y="62" text-anchor="middle" font-family="Bangers" font-size="20" fill="#c8621f">BANG!</text><text x="410" y="245" text-anchor="middle" font-family="Nunito" font-size="12" fill="#555">spark! it explodes,</text><text x="410" y="262" text-anchor="middle" font-family="Nunito" font-size="12" fill="#555">shoves piston DOWN</text></g>
<g stroke="#37414d" stroke-width="5" fill="#37414d"><line x1="458" y1="140" x2="500" y2="140" stroke-dasharray="8 6"/><path d="M496 132 L512 140 L496 148Z"/></g>
<g><rect x="520" y="70" width="80" height="150" rx="10" fill="#fff" stroke="#171717" stroke-width="4"/><rect x="526" y="86" width="68" height="28" rx="4" fill="#8fb3d9" stroke="#37414d" stroke-width="2"/><text x="560" y="62" text-anchor="middle" font-family="Baloo 2" font-size="15" fill="#2aa15a">BLOW</text><text x="560" y="245" text-anchor="middle" font-family="Nunito" font-size="12" fill="#555">piston up, puffs</text><text x="560" y="262" text-anchor="middle" font-family="Nunito" font-size="12" fill="#555">out the smoke</text></g>
<g stroke="#d64541" stroke-width="5" fill="#d64541"><line x1="608" y1="140" x2="650" y2="140" stroke-dasharray="8 6"/><path d="M646 132 L662 140 L646 148Z"/></g>
<g><circle cx="720" cy="140" r="52" fill="none" stroke="#171717" stroke-width="6"/><circle cx="720" cy="140" r="10" fill="#37414d"/><text x="720" y="62" text-anchor="middle" font-family="Bangers" font-size="20" fill="#d64541">SPIN</text><text x="720" y="245" text-anchor="middle" font-family="Nunito" font-size="12" fill="#555">the wheel turns</text><text x="720" y="262" text-anchor="middle" font-family="Nunito" font-size="12" fill="#555">— and off you go!</text></g>
<rect x="70" y="300" width="680" height="52" rx="9" fill="#fff" stroke="#171717" stroke-width="3"/>
<text x="410" y="322" text-anchor="middle" font-family="Nunito" font-size="14" fill="#123">SUCK &#8594; SQUEEZE &#8594; BANG &#8594; BLOW &#8594; a bent crankshaft turns the push into SPIN &#8594; the wheel turns.</text>
<text x="410" y="343" text-anchor="middle" font-family="Nunito" font-size="13" fill="#555">Small and light, the little engine only sips fuel — so a rupee of petrol carries you far.</text>
</svg><div class="figcap">One small explosion, four times a cycle, turned into steady spin — the tiny four-stroke engine that moves a billion journeys.</div></div>'''

try:
    from book_extra import EXTRA, NEW2
    NEW.update(NEW2)
except Exception:
    EXTRA={}

def render_panel(pn,pageno=None):
    _,img,cap,b1,b2,sfx,sfxc,tall=pn
    pg=f'<div class="pageno">{pageno}</div>' if pageno else ''
    p=[f'<div class="panel wide">{pg}<img src="art/{img}.jpg" alt="" loading="lazy">']
    if cap: p.append(f'<div class="cap tl tilt">{cap}</div>')
    if b1: p.append(f'<div class="bubble dl" style="left:12px;bottom:12px;">{b1}</div>')
    if b2: p.append(f'<div class="bubble dr" style="right:12px;top:12px;">{b2}</div>')
    if sfx: p.append(f'<div class="sfx" style="left:40%;top:14px;font-size:32px;color:{sfxc};">{sfx}</div>')
    p.append('</div>'); return "".join(p)

def render_item(it,pageno=None):
    t=it[0]
    if t=="panel": return render_panel(it,pageno)
    if t=="diagram": return DIAGRAM
    if t=="sub": return f'<div class="sub"><span class="dot">{it[1]}</span><b>{it[2]}</b></div>'
    if t=="wow": return f'<div class="wow">{it[1]}</div>'
    if t=="note": return f'<div class="note"><div class="nt">{it[1]}</div><p>{it[2]}</p></div>'
    if t=="timeline":
        rows="".join(f'<div class="tl"><div class="yr">{y}</div><p>{x}</p></div>' for y,x in it[1])
        return f'<div class="timeline">{rows}</div>'
    if t=="moat":
        cards="".join(f'<div class="mc{" bad" if bad else ""}"><h4>{ic} {ti}</h4><p>{tx}</p></div>' for ic,ti,tx,bad in it[1])
        return f'<div class="moat">{cards}</div>'
    return ""

def referenced_images():
    imgs=set()
    for cls,_,_,_,items in CHAPTERS:
        for it in items:
            if it[0]=="panel": imgs.add(it[1])
        for it in EXTRA.get(cls,[]):
            if it[0]=="panel": imgs.add(it[1])
    imgs.add(HOOK[0]); imgs.add("00_cover"); imgs.add("_ref_hank"); return imgs

def build():
    body=[]; pageno=0
    for cls,num,title,pglabel,items in CHAPTERS:
        items=list(items); extra=EXTRA.get(cls,[])
        if extra:
            idx=[i for i,it in enumerate(items) if it[0]=="panel"]
            at=(idx[-1]+1) if idx else len(items)
            for ex in reversed(extra): items.insert(at,ex)
        start=pageno+1
        inner=[f'<div class="chapbar {cls}"><div class="n">{num}</div><h2>{title}</h2></div>']
        for it in items:
            if it[0]=="panel": pageno+=1; inner.append(render_item(it,pageno))
            else: inner.append(render_item(it))
        lbl=f"pages {start}-{pageno}" if pageno>=start else pglabel
        body.append(f'<div class="sheet" data-pg="{lbl}">{"".join(inner)}</div>')
    hook=f'''<div class="sheet" data-pg="the end… for now"><div class="hook"><img src="art/{HOOK[0]}.jpg" alt="">
<div class="hc"><h3>{HOOK[1]}</h3><p>{HOOK[2]}</p><div class="b2">{HOOK[3]}</div></div></div></div>'''
    head=open("/root/omahaindia.com/comics/site/book/bajajauto/_head.html").read()
    tail=open("/root/omahaindia.com/comics/site/book/bajajauto/_tail.html").read()
    open(OUT,"w").write(head+"".join(body)+hook+tail)
    print("built",OUT,"| chapters:",len(CHAPTERS),"| images:",len(referenced_images()))

if __name__=="__main__":
    build()
