#!/usr/bin/env python3
"""The whole of Revelation as one vertical diagram, from the throne room outward (03:34). Writes REVELATION-DIAGRAM.html. Every verse reference is checked against the seated Greek."""
import re, html, pathlib
D = pathlib.Path(__file__).resolve().parent; R = D.parents[2]
ALL = {l.split("\t", 1)[0][4:] for l in (R/"data/corpora/gnt-nestle1904/text/Rev.txt").read_text().split("\n") if "\t" in l}
def chk(r):
    for part in r.split(","):
        m = re.fullmatch(r"\s*(\d+):(\d+)(?:-(\d+))?\s*", part); assert m, r
        for v in range(int(m[2]), int(m[3] or m[2]) + 1): assert f"{m[1]}:{v}" in ALL, r
    return r
e = html.escape
def card(t, s="", r="", k=""):
    return f'<div class="card {k}"><b>{e(t)}</b>' + (f'<span>{e(s)}</span>' if s else "") + (f'<i>{e(chk(r))}</i>' if r else "") + '</div>'
def stack(items): return '<div class="stack">' + "".join(card(*i) for i in items) + '</div>'
def cols(heads, columns, n=None):
    n = n or len(heads); out = f'<div class="scroll"><div class="cols c{n}">'
    for h, col in zip(heads, columns): out += '<div class="col">' + card(*h, k="head") + "".join(card(*c) for c in col) + '</div>'
    return out + '</div></div>'
def sec(eyebrow, title, lead, body): return f'<section><p class="eyebrow">{e(eyebrow)}</p><h2>{e(title)}</h2><p class="lead">{e(lead)}</p>{body}</section><div class="down" aria-hidden="true"></div>'
A = "acts"
S = []
S.append(sec("Centre", "The throne room", "Five places, each named by the book's own preposition. Everything else is sent from one of them.", stack([
 ("On the throne", "One seated, like jasper and carnelian. In the right hand a book, sealed seven times.", "4:2-3, 5:1", A),
 ("In the midst", "A Lamb, standing, as slain: seven horns, seven eyes. He takes the book. Sends: the seven seals.", "5:6-7", A),
 ("Around", "A rainbow. Twenty-four elders on thrones, white, gold crowns. Four living creatures, all eyes: lion, calf, face of a man, eagle. Angels without number. Sends: Come, four times; the seven bowls.", "4:3-8, 5:11, 15:7", A),
 ("Before", "Seven lamps of fire. A sea of glass. The golden altar. Those who are brought to stand. Sends: the seven trumpets; fire off the altar.", "4:5-6, 8:2-5", A),
 ("Out of it", "Lightning, voices, thunder. A voice at each ending. At the last, a river.", "4:5, 16:17, 22:1", A)])))
seals = [("1 White horse", "bow, crown · a creature: Come", "6:1-2", A), ("2 Red horse", "great sword · a creature: Come", "6:3-4", A), ("3 Black horse", "a balance · a creature: Come", "6:5-6", A), ("4 Pale horse", "Death, Hades · a creature: Come", "6:7-8", A),
 ("5 The slain", "under the altar: How long? White robes.", "6:9-11", A), ("6 Sky rolled up", "sun black, moon blood, stars fall; they hide from the face", "6:12-17"), ("Between", "four winds held; the sealed; the multitude", "7:1-17", "gap"), ("7 Silence", "half an hour; then fire off the altar", "8:1-5", A)]
trump = [("1 Hail, fire, blood", "a third of the earth", "8:7"), ("2 Burning mountain", "a third of the sea", "8:8-9"), ("3 Wormwood", "a third of the rivers", "8:10-11"), ("4 A third of light", "an eagle: Woe, woe, woe", "8:12-13"),
 ("5 The shaft", "smoke; locusts like horses; Abaddon", "9:1-11"), ("6 Euphrates", "a voice from the altar; two hundred million", "9:13-21", A), ("Between", "the little book; the reed; two witnesses", "10:1-11, 11:1-13", "gap"), ("7 The kingdom is his", "elders fall; temple open; the ark seen", "11:15-19", A)]
bowls = [("1 Sores", "on those with the mark", "16:2"), ("2 Sea, blood", "every living thing dies", "16:3"), ("3 Rivers, blood", "the altar: True and just", "16:4-7", A), ("4 Sun scorches", "they curse", "16:8-9"),
 ("5 Dark", "on the beast's throne", "16:10-11"), ("6 Euphrates dry", "frogs; Harmagedon", "16:12-16"), ("Between", "nothing", "", "gap"), ("7 It is done", "a voice out of the throne; the city in three", "16:17-21", A)]
S.append(sec("First split", "Three sevens, side by side", "Each leaves the room from a different place and lands on the same earth. Tinted steps are those where the room itself acts.",
 cols([("Seals", "from the midst: the Lamb opens"), ("Trumpets", "from before: seven angels"), ("Bowls", "from around: a creature hands them")], [seals, trump, bowls])))
S.append(sec("Inside the pauses", "What stands between the sixth and the seventh", "The seals and the trumpets each stop before their last. The bowls do not stop.", cols(
 [("Before the seventh seal", "who can stand?"), ("Before the seventh trumpet", "the seer is given things to do")],
 [[("Four angels", "at the four corners, holding the four winds", "7:1"), ("The seal of the living God", "an angel from the sunrise", "7:2-3"), ("A hundred and forty-four thousand", "twelve thousand from each of twelve tribes", "7:4-8"),
   ("A multitude no one can count", "every nation; white; palms", "7:9-10"), ("An elder asks: who are these?", "out of the great affliction; robes washed white in the Lamb's blood", "7:13-14"), ("Every tear wiped", "no hunger, no thirst; the Lamb shepherds them", "7:15-17")],
  [("A strong angel comes down", "cloud, rainbow, face as the sun, feet as fire; one foot on sea, one on land", "10:1-2"), ("Seven thunders", "they speak; seal it, do not write", "10:3-4"), ("An oath", "by the maker of heaven, earth and sea: no more delay", "10:5-7"),
   ("The little book", "taken and eaten: sweet in the mouth, bitter in the belly. Prophesy again.", "10:8-11"), ("A reed", "measure the temple and the altar; leave the court; forty-two months", "11:1-2"), ("Two witnesses", "see the next part", "11:3-13", A)]])))
S.append(sec("Inside the second pause", "The two witnesses", "One line, start to end.", stack([
 ("In sackcloth", "twelve hundred and sixty days", "11:3"), ("Two olive trees, two lampstands", "standing before the Lord of the earth", "11:4"), ("Fire from their mouth", "on whoever would harm them", "11:5"),
 ("Power over sky and water", "to shut heaven so that no rain falls; to turn water to blood", "11:6"), ("Killed", "by the beast that comes up out of the abyss, when their witness is finished", "11:7"),
 ("Their bodies in the street", "of the great city, called in spirit Sodom and Egypt, where their Lord was crucified", "11:8"), ("Three days and a half", "unburied; those on earth send gifts", "11:9-10"),
 ("Breath of life from God", "they stand on their feet", "11:11"), ("Come up here", "they go up in the cloud; their enemies watch", "11:12"), ("Earthquake", "a tenth of the city falls; the rest give glory", "11:13")])))
lamb = [("A woman", "the sun, the moon under her feet, twelve stars; in labour", "12:1-2"), ("A child", "caught up to God and to his throne", "12:5", A), ("War in heaven", "Michael; they overcame by the blood of the Lamb", "12:7-11"),
 ("The wilderness", "a place made ready; eagle's wings; 1,260 days", "12:6, 12:14"), ("The Lamb on Zion", "a hundred and forty-four thousand", "14:1-5"), ("A name on the foreheads", "his, and his Father's", "14:1"),
 ("Three angels", "fear God; fallen is Babylon; do not take the mark", "14:6-11"), ("One like a son of man", "on a white cloud; a sickle; the earth reaped", "14:14-16"), ("The glass sea", "the song of Moses and of the Lamb", "15:2-4", A),
 ("A white horse", "Faithful and True; the Word of God; a sword from the mouth", "19:11-16"), ("The supper of the marriage", "fine linen, bright and clean", "19:7-9", A), ("A thousand years", "they live and reign", "20:4-6"), ("A city coming down", "a bride", "21:2")]
drag = [("A great red dragon", "seven heads, ten horns; a third of the stars", "12:3-4"), ("Thrown down", "to the earth, and his angels with him", "12:9"), ("He hunts the woman", "a river from his mouth; the earth swallows it", "12:13-16"),
 ("The sand of the sea", "he stands, and makes war on the rest of her seed", "12:17-18"), ("A beast from the sea", "leopard, bear, lion; one head as slain, and healed", "13:1-8"), ("A mark", "on the right hand or the forehead; 666", "13:16-18"),
 ("A beast from the earth", "two horns like a lamb; an image that speaks", "13:11-15"), ("The winepress", "outside the city; blood to the bridles", "14:19-20"), ("The beast's throne", "darkened; frogs from three mouths", "16:10-14"),
 ("The kings of the earth", "gathered against the rider", "19:19"), ("The supper of the birds", "the flesh of kings", "19:17-18, 19:21"), ("Bound a thousand years", "then loosed; Gog and Magog", "20:1-3, 20:7-9"), ("A city thrown down", "Babylon; a millstone", "18:21")]
S.append(sec("Second split", "Two sides, side by side", "Read across a row: each thing on one side has its double on the other.", cols([("The Lamb's side", ""), ("The dragon's side", "")], [lamb, drag])))
S.append(sec("On the dragon's side", "The great city", "A woman, and what she sits on, and how it ends.", stack([
 ("A woman in a wilderness", "purple and scarlet; a gold cup; on her forehead: Mystery, Babylon the great", "17:3-5"), ("Drunk", "with the blood of the holy ones", "17:6"),
 ("The beast she sits on", "was, and is not, and is about to come up; seven heads, seven mountains, seven kings; ten horns, ten kings", "17:8-12"), ("The horns turn", "they hate her, strip her, burn her", "17:16"),
 ("Fallen, fallen", "Come out of her, my people", "18:2-4"), ("In one hour", "kings, merchants and shipmasters stand far off and weep", "18:9-19"), ("The cargo", "gold, pearls, linen, spice, wheat, horses, and bodies, and the lives of people", "18:12-13"),
 ("A millstone", "thrown into the sea; no harp, no mill, no lamp, no bride", "18:21-23")])))
S.append(sec("Under", "The abyss and the lake", "What is below the earth.", cols([("The abyss", "opened, and shut"), ("The lake of fire", "nothing comes back")],
 [[("A star with the key", "the shaft opened; smoke", "9:1-2"), ("Abaddon", "king of the locusts", "9:11"), ("The beast comes up", "out of it", "11:7, 17:8"), ("A chain", "the dragon sealed in, a thousand years", "20:1-3")],
  [("The beast and the false prophet", "alive", "19:20"), ("The devil", "after Gog and Magog", "20:10"), ("Death and Hades", "the second death", "20:14"), ("Whoever is not in the book of life", "", "20:15")]])))
fig = stack([("One like a son of man", "in the midst of seven gold lampstands", "1:13"), ("Clothed", "a robe to the feet; gold at the breast", "1:13"), ("Head and hair", "white as wool, as snow", "1:14"), ("Eyes, feet, voice", "a flame; bronze fired in a furnace; many waters", "1:14-15"),
 ("Right hand, mouth, face", "seven stars; a sharp two-edged sword; the sun", "1:16"), ("The secret told", "the seven stars are the angels of the seven assemblies; the seven lampstands are the seven assemblies", "1:20", A)])
S.append(sec("Edge", "The figure among the lampstands", "The first thing seen, on the ground, before any door opens.", fig))
msgs = [("Ephesus", "Holds the seven stars; walks among the lampstands", "You tested the false apostles. You left your first love.", "The tree of life", "2:1-7"),
 ("Smyrna", "The first and the last, who was dead and lived", "Affliction, poverty, and you are rich. Ten days. Be faithful to death.", "The crown of life; no hurt from the second death", "2:8-11"),
 ("Pergamum", "Has the sharp two-edged sword", "You live where the adversary's throne is. Antipas, my witness. Balaam's teaching.", "Hidden manna; a white stone; a new name", "2:12-17"),
 ("Thyatira", "The Son of God; eyes as flame, feet as bronze", "Your last works more than the first. You put up with Jezebel.", "Authority over the nations; the morning star", "2:18-29"),
 ("Sardis", "Has the seven spirits and the seven stars", "A name that you live, and you are dead. Wake.", "White garments; a name not wiped from the book of life", "3:1-6"),
 ("Philadelphia", "The holy, the true; has the key of David", "An open door. Little power, and you kept my word.", "A pillar in the temple; the name of the new Jerusalem", "3:7-13"),
 ("Laodicea", "The Amen; the faithful witness; the beginning of God's creation", "Neither cold nor hot. You say, I am rich. Buy gold refined by fire. I stand at the door.", "To sit with me in my throne", "3:14-22")]
mh = '<div class="msgs">' + "".join(f'<div class="msg"><b>{i}. {e(c)}</b><dl><dt>Who speaks</dt><dd>{e(w)}</dd><dt>What is known</dt><dd>{e(k)}</dd><dt>To the one who overcomes</dt><dd>{e(p)}</dd></dl><i>{chk(r)}</i></div>' for i, (c, w, k, p, r) in enumerate(msgs, 1)) + '</div>'
S.append(sec("Edge", "Seven lampstands, seven messages", "Each speaker is named by a piece of the figure. Each promise is a piece of the end.", mh))
S.append(sec("Edge", "Jesus and John", "How the book reaches the reader, and where the one who sends it is seen.", cols(
 [("The handing down", "one line"), ("John", "what he does"), ("Jesus", "where he is seen")],
 [[("God", "gave the revelation", "1:1"), ("Jesus Christ", "to show his slaves", "1:1"), ("His angel", "sent and signified", "1:1, 22:16"), ("John", "who witnessed all he saw", "1:2"), ("The seven assemblies", "in Asia", "1:4, 1:11"), ("The one who reads aloud", "and those who hear and keep", "1:3")],
  [("On Patmos", "hears a voice behind; turns", "1:9-12"), ("Falls as dead", "a right hand is laid on him: Do not fear", "1:17"), ("Goes up", "through a door open in heaven", "4:1-2"), ("Weeps much", "no one can open the book", "5:4"), ("Eats", "the little book", "10:10"), ("Measures", "with a reed", "11:1"), ("Wonders", "at the woman", "17:6"), ("Falls before the angel", "twice. Do not. Worship God.", "19:10, 22:8-9")],
  [("Among the lampstands", "as one like a son of man", "1:13"), ("In the midst of the throne", "as a Lamb, slain", "5:6", A), ("On Mount Zion", "as the Lamb", "14:1"), ("On a white cloud", "as one like a son of man", "14:14"), ("On a white horse", "as the Word of God", "19:11-13"), ("In the city", "as its lamp, and on its throne", "21:23, 22:3", A), ("At the close", "I, Jesus. I come quickly.", "22:16, 22:20")]])))
S.append(sec("End", "The centre, come down", "The throne on the ground, and no rings around it.", stack([
 ("A great white throne", "earth and heaven flee from the face", "20:11", A), ("The dead, great and small", "books opened; another book, of life", "20:12-13"), ("A new heaven and a new earth", "the sea is no more", "21:1"),
 ("The city coming down", "the tent of God is with humankind; every tear wiped; death no more", "21:2-4"), ("The seated one speaks", "Behold, I make all things new. It is done.", "21:5-6", A),
 ("Foursquare", "twelve thousand stadia each way; twelve gates, twelve pearls; twelve foundations; the street gold as glass", "21:12-21"), ("No temple, no sun, no night", "God and the Lamb are its temple; its lamp is the Lamb; the gates are never shut", "21:22-25"),
 ("The river", "out of the throne of God and of the Lamb, down the middle of the street", "22:1", A), ("The tree of life", "twelve fruits; leaves for the healing of the nations", "22:2"), ("His face", "and his name on their foreheads", "22:4")])))
CSS = """/* one column running down from the centre; side-by-side grids where the book runs in parallel */
:root{--paper:#f3f4f1;--ink:#1c2226;--muted:#5d676d;--line:#c9cfcc;--card:#ffffff;--centre:#8a1f2d;--centre-bg:#f6e4e6;--gap-bg:#e9ecea;--display:'Newsreader',Georgia,serif;--body:'IBM Plex Sans',system-ui,sans-serif}
@media (prefers-color-scheme:dark){:root:not([data-theme="light"]){color-scheme:dark;--paper:#14181a;--ink:#e6e9e7;--muted:#9aa5a8;--line:#333c40;--card:#1c2225;--centre:#e58a96;--centre-bg:#3a1c21;--gap-bg:#20272a}}
:root[data-theme="dark"]{color-scheme:dark;--paper:#14181a;--ink:#e6e9e7;--muted:#9aa5a8;--line:#333c40;--card:#1c2225;--centre:#e58a96;--centre-bg:#3a1c21;--gap-bg:#20272a}
*{box-sizing:border-box}body{margin:0;background:var(--paper);color:var(--ink);font-family:var(--body);font-size:15px;line-height:1.45}
main{max-width:860px;margin:0 auto;padding:28px 16px 64px}h1{font-family:var(--display);font-weight:500;font-size:34px;line-height:1.1;margin:0 0 8px;text-wrap:balance}
.intro{color:var(--muted);max-width:62ch;margin:0 0 8px}.key{display:flex;gap:8px;align-items:center;color:var(--muted);font-size:13px;margin:0 0 28px}.key span{width:14px;height:14px;border-radius:3px;background:var(--centre-bg);border:1px solid var(--centre)}
section{margin:0}.eyebrow{font-size:12px;letter-spacing:.08em;text-transform:uppercase;color:var(--centre);margin:0 0 2px}h2{font-family:var(--display);font-weight:500;font-size:24px;margin:0 0 4px;text-wrap:balance}.lead{color:var(--muted);margin:0 0 12px;max-width:62ch}
.down{width:1px;height:34px;background:var(--line);margin:10px auto 14px;position:relative}.down::after{content:"";position:absolute;left:-4px;bottom:0;width:8px;height:8px;border-right:1px solid var(--line);border-bottom:1px solid var(--line);transform:rotate(45deg)}
.stack{display:flex;flex-direction:column;gap:6px;max-width:560px;margin:0 auto}.scroll{overflow-x:auto}.cols{display:grid;gap:8px;align-items:start}.c2{grid-template-columns:repeat(2,minmax(150px,1fr))}.c3{grid-template-columns:repeat(3,minmax(132px,1fr))}.col{display:flex;flex-direction:column;gap:6px;min-width:0}
.card{background:var(--card);border:1px solid var(--line);border-radius:6px;padding:8px 10px;display:flex;flex-direction:column;gap:2px;min-width:0}.card b{font-weight:500}.card span{color:var(--muted);font-size:13px}.card i{font-style:normal;font-size:11.5px;color:var(--muted);font-variant-numeric:tabular-nums}
.card.acts{background:var(--centre-bg);border-color:var(--centre)}.card.acts b{color:var(--centre)}.card.head{background:transparent;border-style:dashed}.card.head b{font-family:var(--display);font-size:18px}.card.gap{background:var(--gap-bg);border-style:dotted}
.msgs{display:grid;gap:8px;grid-template-columns:repeat(auto-fit,minmax(240px,1fr))}.msg{background:var(--card);border:1px solid var(--line);border-radius:6px;padding:10px 12px}.msg b{font-family:var(--display);font-weight:500;font-size:18px}.msg dl{margin:6px 0 4px}.msg dt{font-size:11.5px;letter-spacing:.06em;text-transform:uppercase;color:var(--centre);margin-top:6px}.msg dd{margin:0}.msg i{font-style:normal;font-size:11.5px;color:var(--muted)}
footer{color:var(--muted);font-size:13px;max-width:62ch;margin-top:8px}"""
doc = f"""<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Revelation from the throne outward</title>
<link rel="preconnect" href="https://fonts.googleapis.com"><link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Sans:wght@400;500&family=Newsreader:opsz,wght@6..72,500&display=swap" rel="stylesheet"><style>{CSS}</style></head><body><main>
<h1>Revelation from the throne outward</h1><p class="intro">The whole book as one tree, read down from its centre. Where the book runs in parallel, the parts stand side by side. Each box carries its verses.</p><p class="key"><span></span>the throne room itself acts or is touched here</p>
{''.join(S)}<footer>Compressed from the Greek of Nestle 1904 as seated in the archive. It is a map and not a translation. Working draft, 29 November 2026.</footer></main></body></html>"""
doc = doc.rsplit('<div class="down" aria-hidden="true"></div>', 1); doc = doc[0] + doc[1]
(D/"REVELATION-DIAGRAM.html").write_text(doc); print(len(doc), doc.count('class="card'), doc.count("<section>"))
