File size: 33,727 Bytes
d3a7a1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
576c6b1
 
 
 
 
 
 
ce5cab3
 
 
 
576c6b1
 
 
d3a7a1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
485b68d
 
 
d3a7a1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
import os
import json
import time
import random
import html
import gradio as gr
import agents
import voice
import transcribe
import events
import router
import world_state
import campaign
from worlds import get_world, WORLDS, list_worlds

MOCK = os.environ.get("TINYWORLD_MOCK", "0") == "1"

# On a ZeroGPU Space (TINYWORLD_INFER=local) the @spaces.GPU functions must be
# registered at startup or ZeroGPU shuts the Space down. Import inference eagerly
# in that mode only (loads torch but NOT the models β€” those stay lazy). Skipped on
# mock/modal so dev and tests never need torch/spaces.
if not MOCK and os.environ.get("TINYWORLD_INFER", "modal").lower() == "local":
    try:
        import inference  # noqa: F401  (registers ZeroGPU GPU tasks)
        import threading
        # warm the model into CPU RAM in the background so startup isn't blocked
        threading.Thread(target=inference.warmup, daemon=True).start()
        print("[app] ZeroGPU inference module loaded; warming model in background")
    except Exception as e:
        print(f"[app] inference preimport failed: {e}")

MOOD_EMOJI = {
    "happy": "😊", "stressed": "😰", "bored": "😐", "excited": "🀩",
    "hungry": "πŸ•", "tired": "😴", "nostalgic": "πŸ₯Ή", "curious": "🧐",
    "proud": "😀", "embarrassed": "🫣",
}

VIBE_COLORS = {
    "energy": ("#ff6b6b", "#ffb15f"),
    "hunger": ("#ffd76a", "#ff8a5f"),
    "social": ("#38e8ff", "#9b6bff"),
}

CHAR_COLORS = {
    "Marta Voss": "#ff5fa2", "Jay Park": "#ff8a5f", "Nia Okafor": "#7CFFB2",
    "Luca Bell": "#38e8ff", "Priya Raman": "#9b6bff",
}
PALETTE = ["#ff5fa2", "#ff8a5f", "#7CFFB2", "#38e8ff", "#9b6bff"]


def esc(value):
    return html.escape(str(value), quote=True)


def char_color(world, name):
    for i, c in enumerate(world["cast"]):
        if c["name"] == name:
            return c.get("color") or CHAR_COLORS.get(name) or PALETTE[i % len(PALETTE)]
    return "#9b6bff"


# ---------------------------------------------------------------- canvas payloads
def build_world_payload(world_id):
    world = get_world(world_id)
    board = world["board"]
    cast = []
    for c in world["cast"]:
        home_key = c.get("home", "square")
        tile = board["hotspots_tile"].get(home_key, [6, 6])
        cast.append({
            "name": c["name"], "short": c["name"].split()[0],
            "emoji": c.get("emoji", "πŸ‘€"),
            "color": char_color(world, c["name"]),
            "home": tile,
        })
    payload = {
        "cols": board["cols"], "rows": board["rows"],
        "roads": board["roads"], "plaza": board["plaza"],
        "plaza_center": board.get("plaza_center", [board["cols"] / 2, board["rows"] / 2]),
        "buildings": board["buildings"], "trees": board.get("trees", []),
        "props": board.get("props", []), "cast": cast,
        "hotspots": board.get("hotspots_tile", {}), "ambient": board.get("ambient", 12),
    }
    return json.dumps(payload)


def build_reactions_payload(world_id, reactions):
    world = get_world(world_id)
    board = world["board"]
    hs = board["hotspots_tile"]
    acts = board.get("activities", {})
    out = {"ts": time.time(), "reactions": []}
    for r in reactions:
        name = r["name"]
        key = r.get("moved_to") or world_state.get_position(world_id, name)
        act = acts.get(name, {})
        action = (r.get("action") or "").strip()
        short_action = (action[:24].rstrip() + "…") if len(action) > 25 else action
        label = short_action or act.get("label", "")
        vehicle = act.get("vehicle", "")
        if key and key in hs:
            tgt = hs[key]
        else:
            home = next((c.get("home") for c in world["cast"] if c["name"] == name), None)
            tgt = hs.get(home) or hs.get("square") or [0, 0]
        out["reactions"].append({
            "name": name, "short": name.split()[0],
            "mood": r["mood"], "moodEmoji": MOOD_EMOJI.get(r["mood"], "😐"),
            "text": r["text"], "target": tgt, "activity": label, "vehicle": vehicle,
            "running": False,
        })
    return json.dumps(out)


def build_state_payload(world_id):
    world = get_world(world_id)
    board = world["board"]
    hs = board["hotspots_tile"]
    out = {"ts": time.time(), "silent": True, "reactions": []}
    for c in world["cast"]:
        name = c["name"]
        key = world_state.get_position(world_id, name) or c.get("home", "square")
        tile = hs.get(key) or hs.get(c.get("home")) or hs.get("square") or [0, 0]
        mood = world_state.get_mood(world_id, name)
        out["reactions"].append({
            "name": name,
            "short": name.split()[0],
            "mood": mood,
            "moodEmoji": MOOD_EMOJI.get(mood, "😐"),
            "text": "",
            "target": tile,
            "activity": world_state.get_activity(world_id, name),
            "vehicle": "",
            "running": False,
        })
    return json.dumps(out)


# ---------------------------------------------------------------- roster / ticker / log
def render_vibe_bar(label, value, colors):
    c1, c2 = colors
    pct = int(value * 100)
    return (
        f'<div class="vibe-row"><span class="vibe-label">{label}</span>'
        f'<div class="vibe-track"><div class="vibe-fill" style="width:{pct}%;'
        f'background:linear-gradient(90deg,{c1},{c2});color:{c1}"></div></div>'
        f'<span class="vibe-val">{pct}</span></div>'
    )


def render_roster(world_id):
    world = get_world(world_id)
    world_state.init_cast(world)
    state = world_state.get_state(world_id)
    needs = world_state.get_needs(world_id)
    cards = []
    for c in world["cast"]:
        name = c["name"]
        mood = state["moods"].get(name, "curious")
        mood_e = MOOD_EMOJI.get(mood, "😐")
        n = needs.get(name, {"energy": 60, "hunger": 35, "social": 50})
        cards.append(
            f'<div class="roster-card" style="border-left-color:{char_color(world, name)}">'
            f'<div class="roster-emoji">{c.get("emoji", "πŸ‘€")}</div>'
            f'<div class="roster-name">{name.split()[0]}</div>'
            f'<div class="roster-mood">{mood_e} {mood}</div>'
            f'<div class="roster-meters">{render_vibe_bar("⚑", n["energy"] / 100, VIBE_COLORS["energy"])}'
            f'{render_vibe_bar("🍽", n["hunger"] / 100, VIBE_COLORS["hunger"])}'
            f'{render_vibe_bar("πŸ’¬", n["social"] / 100, VIBE_COLORS["social"])}</div>'
            f'</div>'
        )
    return f'<div class="roster-rail">{"".join(cards)}</div>'


def render_ticker(world_id):
    state = world_state.get_state(world_id)
    day, game_time, paused = world_state.get_game_time(world_id)
    chaos = state["chaos"]
    filled = min(int(chaos * 5), 5)
    bar = "β–°" * filled + "β–±" * (5 - filled)
    unlocked = " πŸ”“" if chaos >= 0.5 else ""
    return (
        f'<div class="progress-ticker">'
        f'<span class="ticker-day">DAY {day} Β· {world_state.format_time(game_time)}</span><span class="ticker-sep">Β·</span>'
        f'<span class="ticker-events">{"PAUSED" if paused else "LIVE TIME"}</span><span class="ticker-sep">Β·</span>'
        f'<span class="ticker-events">⚑ {state["event_count"]}</span><span class="ticker-sep">·</span>'
        f'<span class="ticker-chaos">CHAOS {bar}{unlocked}</span></div>'
    )


def render_daily_log(world_id):
    world = get_world(world_id)
    timeline = world_state.get_timeline(world_id)
    rows = []
    for c in world["cast"]:
        name = c["name"]
        entries = timeline.get(name, [])[-4:]
        text = " Β· ".join(esc(e) for e in entries) if entries else "No entries yet."
        rows.append(
            f'<div class="daily-row"><b style="color:{char_color(world, name)}">{esc(name.split()[0])}</b> '
            f'<span>{text}</span></div>'
        )
    return f'<div class="daily-log-inner">{"".join(rows)}</div>'


def render_mode_badge(status=None):
    status = status or agents.get_runtime_status()
    mode = status.get("mode")
    model = status.get("model") or "unknown"
    latency = status.get("latency")
    error = status.get("error")
    if mode == "live":
        label = f"🟒 Live · {model.split('/')[-1]}"
        if latency is not None:
            label += f" Β· {latency:.1f}s"
        cls = "mode-live"
    elif mode == "waking":
        label = f"🟑 Waking · {model.split('/')[-1]}"
        cls = "mode-waking"
    elif mode == "error":
        msg = f" Β· {error}" if error else ""
        label = f"πŸ”΄ LLM error{msg}"
        cls = "mode-error"
    else:
        label = "🟑 Offline demo (mock)"
        cls = "mode-mock"
    return f'<div class="mode-badge {cls}">{esc(label)}</div>'


def render_town_log(world_id, reactions=None, followup=None):
    world = get_world(world_id)
    if not reactions:
        return ('<div class="town-log-inner"><div class="town-log-empty">'
                'The town log is empty.<br>Throw an event to start the story.</div></div>')
    lines = []
    for r in reactions:
        char = next((c for c in world["cast"] if c["name"] == r["name"]), None)
        emoji = char.get("emoji", "πŸ‘€") if char else "πŸ‘€"
        name = esc(r["name"])
        text = esc(r["text"])
        mood = esc(r["mood"])
        lines.append(
            f'<div class="log-entry"><div class="log-head">'
            f'<span class="log-emoji">{emoji}</span><span class="log-name">{name}</span>'
            f'<span class="log-mood">{MOOD_EMOJI.get(r["mood"], "😐")}</span></div>'
            f'<div class="log-text" data-mood="{mood}">"{text}"</div></div>'
        )
    if followup:
        lines.append(f'<div class="log-entry log-followup"><div class="log-text">πŸ”— <em>{esc(followup["text"])}</em></div></div>')
    gossip_from = [r for r in reactions if random.random() < 0.3]
    if gossip_from:
        source = random.choice(gossip_from)
        others = [c["name"] for c in world["cast"] if c["name"] != source["name"]]
        if others:
            target = random.choice(others)
            snippet = source["text"][:60] + ("…" if len(source["text"]) > 60 else "")
            lines.append(f'<div class="log-entry log-gossip"><div class="log-text">πŸ—£οΈ '
                         f'<em>Word spreads: {esc(target.split()[0])} hears "{esc(snippet)}"</em></div></div>')
            world_state.add_gossip(world_id, target, snippet)
    return f'<div class="town-log-inner">{"".join(lines)}</div>'


def render_explainer(world_id, reactions=None, focus=None):
    world = get_world(world_id)
    if not reactions:
        return ('<div class="explain-inner"><div class="explain-empty">'
                'Pick a <b>scenario</b> or throw an event, then this panel explains '
                '<b>why each person reacted the way they did</b> β€” great for teaching how '
                'different people respond to the same situation.</div></div>')
    parts = []
    if focus:
        parts.append(f'<div class="explain-focus">πŸŽ“ <b>Think about:</b> {esc(focus)}</div>')
    else:
        parts.append('<div class="explain-focus">πŸŽ“ <b>Why did they react this way?</b></div>')
    for r in reactions:
        c = next((x for x in world["cast"] if x["name"] == r["name"]), None)
        if not c:
            continue
        trait = (c.get("traits") or ["distinct"])[0]
        hint = c.get("catchphrase_hint", "responds in their own way")
        col = char_color(world, r["name"])
        understanding = esc((r.get("understanding") or "").strip())
        action = esc((r.get("action") or "").strip())
        first_name = esc(r["name"].split()[0])
        mood = esc(r["mood"])
        trait_text = esc(trait)
        if understanding or action:
            reason = (
                f'reads it as: <em>{understanding}</em> ' if understanding else ''
            ) + (
                f'so they <b>{action}</b>.' if action else ''
            )
            parts.append(
                f'<div class="explain-row"><b style="color:{col}">{first_name}</b> '
                f'(feeling <em>{mood}</em>, naturally <em>{trait_text}</em>) {reason}</div>'
            )
        else:
            parts.append(
                f'<div class="explain-row"><b style="color:{col}">{first_name}</b> '
                f'feels <em>{mood}</em> and is naturally <em>{trait_text}</em>, '
                f'so they {esc(hint)}.</div>'
            )
    return f'<div class="explain-inner">{"".join(parts)}</div>'


# ---------------------------------------------------------------- crisis mode UI
def render_crisis_hud(world_id):
    v = campaign.view(get_world(world_id))
    cr = v["crisis"]
    if not v["active"] or not cr:
        return '<div class="crisis-hud crisis-hud-idle">No crisis active β€” switch to 🎬 Campaign and press <b>Start Campaign</b>.</div>'
    chips = []
    for req in cr["requirements"]:
        cls = "met" if req["met"] else "open"
        icon = "βœ…" if req["met"] else "⏳"
        opt = " (optional)" if req["optional"] else ""
        chips.append(f'<span class="crisis-chip {cls}">{icon} {esc(req["label"])}{esc(opt)}</span>')
    chaos_pct = int(v["chaos"] * 100)
    res = int(v["town_resilience"])
    return (
        f'<div class="crisis-hud">'
        f'<div class="crisis-top"><span class="crisis-title">🚨 {esc(cr["title"])}</span>'
        f'<span class="crisis-meta">Chapter {v["chapter_index"] + 1}/{v["chapter_count"]} Β· '
        f'Round {cr["round"]}/{cr["time_limit"]}</span></div>'
        f'<div class="crisis-chips">{"".join(chips)}</div>'
        f'<div class="crisis-bars">'
        f'<div class="crisis-bar"><span>πŸ”₯ Chaos</span><div class="cbar-track">'
        f'<div class="cbar-fill chaos" style="width:{chaos_pct}%"></div></div><b>{chaos_pct}</b></div>'
        f'<div class="crisis-bar"><span>πŸ›‘ Resilience</span><div class="cbar-track">'
        f'<div class="cbar-fill res" style="width:{res}%"></div></div><b>{res}</b></div>'
        f'</div></div>'
    )


def render_story_card(kind="dispatch", title="", body="", extra=""):
    if not title and not body and not extra:
        return '<div class="story-card story-hidden"></div>'
    title_html = f'<div class="story-title">{esc(title)}</div>' if title else ""
    body_html = f'<div class="story-body">{esc(body)}</div>' if body else ""
    return f'<div class="story-card story-{kind}">{title_html}{body_html}{extra}</div>'


def build_crisis_payload(world_id):
    world = get_world(world_id)
    v = campaign.view(world)
    cr = v["crisis"]
    out = {"ts": time.time(), "active": bool(v["active"] and cr),
           "status": v["status"], "chaos": v["chaos"]}
    if cr:
        hs = world["board"]["hotspots_tile"]
        out.update({
            "kind": cr["kind"], "title": cr["title"],
            "tile": hs.get(cr["affected_hotspot"]) if cr["affected_hotspot"] else None,
            "round": cr["round"], "time_limit": cr["time_limit"],
            "requirements": [{"id": r["id"], "label": r["label"], "met": r["met"]}
                             for r in cr["requirements"]],
        })
    return json.dumps(out)


def _outcome_card(res):
    oc = res.get("outcome")
    grade = res.get("grade")
    if oc == "won":
        return render_story_card("win", f"βœ… Chapter cleared Β· Grade {grade}", res.get("outro", ""))
    if oc == "lost_crisis":
        return render_story_card("loss", f"❌ Chapter failed · Grade {grade}", res.get("outro", ""))
    if oc == "campaign_lost":
        return render_story_card("gameover", "🏚 The town fell",
                                 f'{res.get("outro", "")} Town Resilience hit zero.')
    if oc == "finale":
        extra = f'<div class="story-reveal">{esc(res.get("reveal", ""))}</div>'
        return render_story_card("finale", "🎬 The Long Summer β€” Finale", res.get("outro", ""), extra)
    return render_story_card("dispatch", "", res.get("dispatch", ""))


def scenario_choices(world_id):
    world = get_world(world_id)
    return [(s["title"], s["id"]) for s in world.get("scenarios", [])]


def render_stage_shell():
    return '<div class="stage" id="tw-stage"><canvas id="tw-canvas"></canvas><div class="stage-vignette"></div></div>'


# ---------------------------------------------------------------- assets
def _read(path):
    full = os.path.join(os.path.dirname(__file__), path)
    if os.path.exists(full):
        with open(full) as f:
            return f.read()
    return ""


css_content = _read("assets/style.css")
js_content = _read("assets/game.js")

# Force dark mode on load. HF Spaces otherwise loads light mode, which renders
# Gradio's dark text on top of our neon-dark panels β€” unreadable. With ?__theme=dark
# Gradio uses its light-on-dark palette and everything is legible.
THEME_JS = (
    "(function(){try{var u=new URL(window.location.href);"
    "if(u.searchParams.get('__theme')!=='dark'){"
    "u.searchParams.set('__theme','dark');window.location.replace(u.href);}}catch(e){}})();\n"
)
js_content = THEME_JS + js_content

DEFAULT_WORLD = os.environ.get("TW_DEFAULT_WORLD", "maple_street")
world_list = list_worlds()
world_names = {w["id"]: w["name"] for w in world_list}


# ---------------------------------------------------------------- UI
with gr.Blocks(title="TinyWorld β€” AI Neighborhood Game") as demo:
    current_world_id = gr.State(DEFAULT_WORLD)
    last_reactions_state = gr.State([])

    with gr.Row():
        with gr.Column(scale=7):
            gr.HTML('<div class="tw-topbar"><div class="tw-logo">TINYWORLD</div>'
                    '<div class="tw-tagline">the town that remembers</div></div>')
        with gr.Column(scale=2, min_width=160):
            world_picker = gr.Dropdown(
                choices=[(world_names.get(w["id"], w["id"]), w["id"]) for w in world_list],
                value=DEFAULT_WORLD, label="World")
        with gr.Column(scale=3, min_width=210):
            ticker_html = gr.HTML(render_ticker(DEFAULT_WORLD))
        with gr.Column(scale=2, min_width=210):
            mode_badge_html = gr.HTML(render_mode_badge())

    # the canvas stage (rendered once, never re-rendered β†’ game loop persists)
    gr.HTML(render_stage_shell())

    # hidden data channels for the canvas
    world_box = gr.Textbox(value=build_world_payload(DEFAULT_WORLD), elem_id="tw-world",
                           elem_classes="tw-data", label="", interactive=True)
    reactions_box = gr.Textbox(value="", elem_id="tw-reactions", elem_classes="tw-data",
                               label="", interactive=True)
    crisis_box = gr.Textbox(value=build_crisis_payload(DEFAULT_WORLD), elem_id="tw-crisis",
                            elem_classes="tw-data", label="", interactive=True)

    with gr.Row(elem_id="tw-console"):
        with gr.Column(scale=8):
            event_input = gr.Textbox(
                placeholder="Type an event to throw at the neighborhood…  (e.g. a UFO lands in the park)",
                label="", show_label=False, lines=1)
        with gr.Column(scale=2, min_width=150):
            trigger_btn = gr.Button("⚑ THROW", variant="primary", elem_id="throw-btn")

    with gr.Row(elem_id="tw-actions"):
        with gr.Column(scale=6, min_width=260):
            scenario_dd = gr.Dropdown(choices=scenario_choices(DEFAULT_WORLD), value=None,
                                      label="πŸŽ“ Teaching scenario β€” a real situation to explore")
        with gr.Column(scale=2, min_width=140):
            run_scenario_btn = gr.Button("πŸŽ“ Run Scenario")
        with gr.Column(scale=2, min_width=140):
            random_btn = gr.Button("🎲 Random Chaos")
        with gr.Column(scale=1, min_width=120):
            pause_btn = gr.Button("⏯ Time")
        with gr.Column(scale=1, min_width=120):
            step_btn = gr.Button("⏭ Step")

    with gr.Row(elem_id="tw-mode"):
        with gr.Column(scale=5, min_width=240):
            mode_radio = gr.Radio(
                ["πŸ§ͺ Sandbox", "🎬 Campaign"], value="πŸ§ͺ Sandbox",
                label="Mode", show_label=True)
        with gr.Column(scale=2, min_width=160):
            start_campaign_btn = gr.Button("🎬 Start Campaign", variant="primary")
        with gr.Column(scale=2, min_width=160):
            next_round_btn = gr.Button("β–Ά Play Round")

    crisis_hud_html = gr.HTML(render_crisis_hud(DEFAULT_WORLD))
    story_card_html = gr.HTML(render_story_card())

    with gr.Row(equal_height=False):
        with gr.Column(scale=3, min_width=220):
            gr.HTML('<div class="panel-title">πŸ§‘β€πŸ€β€πŸ§‘ Townsfolk</div>')
            roster_html = gr.HTML(render_roster(DEFAULT_WORLD))
        with gr.Column(scale=4, min_width=240):
            gr.HTML('<div class="panel-title">πŸ“œ Town Log</div>')
            town_log_html = gr.HTML(render_town_log(DEFAULT_WORLD))
        with gr.Column(scale=4, min_width=240):
            gr.HTML('<div class="panel-title">πŸŽ“ Learn β€” Why did they react?</div>')
            explainer_html = gr.HTML(render_explainer(DEFAULT_WORLD))

    gr.HTML('<div class="panel-title" style="margin-top:6px">πŸ•’ Daily Log</div>')
    daily_log_html = gr.HTML(render_daily_log(DEFAULT_WORLD))

    gr.HTML('<div class="panel-title" style="margin-top:6px">πŸ”Š Voice</div>')
    with gr.Row(elem_id="tw-audio", equal_height=False):
        with gr.Column(scale=4, min_width=240):
            mic_input = gr.Audio(sources=["microphone"], type="filepath", label="πŸŽ™οΈ 1 Β· Record your event")
            transcribe_btn = gr.Button("πŸ“ 2 Β· Transcribe to text")
            mic_status = gr.HTML('<div class="audio-cap">Record β†’ press <b>Transcribe</b>: your words fill the '
                                 'event box, then press <b>⚑ THROW</b>. <i>Mic only works on '
                                 'http://localhost:7860 (blocked on 0.0.0.0).</i></div>')
        with gr.Column(scale=4, min_width=240):
            voice_output = gr.Audio(label="πŸ”Š Auto-voice (plays after each event)", autoplay=True)
            gr.HTML('<div class="audio-cap">The <b>most dramatic</b> reaction is read aloud automatically.</div>')
        with gr.Column(scale=4, min_width=240):
            hear_name = gr.Dropdown(choices=[], label="🎧 Replay a character's voice")
            hear_btn = gr.Button("β–Ά Play their last line")
            gr.HTML('<div class="audio-cap">Pick any character and hear their <b>last line</b> again.</div>')

    gr.HTML('<div class="tw-footer">Built for <strong>Build Small Hackathon 2026</strong> Β· '
            'Hugging Face Γ— Gradio Β· Thousand Token Wood<br>'
            'Models: <strong>Nemotron-Mini-4B</strong> (NVIDIA) + <strong>VoxCPM2</strong> (OpenBMB) + '
            '<strong>Whisper</strong> Β· running on <strong>Hugging Face ZeroGPU</strong><br>'
            'Built with <strong>OpenAI Codex</strong><br>'
            '<span class="footer-muted">Switch maps with the <b>World</b> picker, top-right Β· '
            'open at <b>http://localhost:7860</b> for microphone access.</span></div>')
    tick_timer = gr.Timer(value=float(os.environ.get("TINYWORLD_TIME_SCALE", "6")), active=True)

    # ---------------------------------------------------------- handlers
    def _run_event(world_id, event_text, focus=None, route=None):
        world = get_world(world_id)
        world_state.init_cast(world)
        result = agents.react(world_id, event_text, route=route)
        reactions = result["reactions"]
        runtime = result.get("runtime") or agents.get_runtime_status()
        followup = agents.generate_followup(reactions, event_text)
        top = max(reactions, key=lambda r: r["drama"])
        audio = None
        try:
            cd = next(c for c in world["cast"] if c["name"] == top["name"])
            audio = voice.generate_voice(top["text"], cd["voice_description"])
        except Exception as e:
            print(f"[app] voice failed: {e}")
        names = [r["name"] for r in reactions]
        return (render_town_log(world_id, reactions, followup), render_roster(world_id),
                render_ticker(world_id), audio,
                gr.Dropdown(choices=names, value=names[0] if names else None),
                reactions, build_reactions_payload(world_id, reactions),
                render_explainer(world_id, reactions, focus), render_mode_badge(runtime),
                render_daily_log(world_id))

    def _sandbox_tail(world_id):
        # the three campaign outputs in their idle/passthrough state for sandbox events
        return (render_crisis_hud(world_id), render_story_card(), build_crisis_payload(world_id))

    def _empty_sandbox(world_id):
        return (render_town_log(world_id), render_roster(world_id), render_ticker(world_id),
                None, gr.Dropdown(choices=[], value=None), [], "", render_explainer(world_id),
                render_mode_badge(), render_daily_log(world_id)) + _sandbox_tail(world_id)

    def _sandbox_throw(event_text, world_id):
        if not event_text or not event_text.strip():
            return _empty_sandbox(world_id)
        world = get_world(world_id)
        route = router.classify(event_text.strip(), world)
        if route["type"] == "noop":
            return _empty_sandbox(world_id)
        return _run_event(world_id, event_text.strip(), route=route) + _sandbox_tail(world_id)

    def do_throw(event_text, world_id, mode):
        v = campaign.view(get_world(world_id))
        if str(mode).startswith("🎬") and v["active"] and v["status"] == "playing":
            return _run_crisis_round(world_id, event_text)
        return _sandbox_throw(event_text, world_id)

    def start_campaign(world_id):
        world = get_world(world_id)
        world_state.reset_world(world_id)
        world_state.init_cast(world)
        v = campaign.start(world)
        card = render_story_card("intro", f"🎬 {v['title']}", v.get("chapter_intro", ""))
        return (render_town_log(world_id), render_roster(world_id), render_ticker(world_id),
                None, gr.Dropdown(choices=[], value=None), [], build_state_payload(world_id),
                render_explainer(world_id), render_mode_badge(), render_daily_log(world_id),
                render_crisis_hud(world_id), card, build_crisis_payload(world_id))

    def _run_crisis_round(world_id, player_text):
        world = get_world(world_id)
        v0 = campaign.view(world)
        if not v0["active"] or not v0["crisis"] or v0["status"] != "playing":
            return (render_town_log(world_id), render_roster(world_id), render_ticker(world_id),
                    None, gr.Dropdown(choices=[], value=None), [], build_state_payload(world_id),
                    render_explainer(world_id), render_mode_badge(), render_daily_log(world_id),
                    render_crisis_hud(world_id),
                    render_story_card("dispatch", "", "No active crisis β€” press Start Campaign."),
                    build_crisis_payload(world_id))

        cr = campaign.current_crisis(world)
        focus = cr.focus if cr else ""
        event_text = campaign.round_event_text(world)
        route = None
        if player_text and player_text.strip():
            r = router.classify(player_text.strip(), world)
            if r["type"] != "noop":
                route = r

        result = agents.react(world_id, event_text, route=route)
        reactions = result["reactions"]
        runtime = result.get("runtime") or agents.get_runtime_status()
        res = campaign.resolve_round(world, reactions)

        audio = None
        if reactions:
            top = max(reactions, key=lambda r: r["drama"])
            try:
                cd = next(c for c in world["cast"] if c["name"] == top["name"])
                audio = voice.generate_voice(top["text"], cd["voice_description"])
            except Exception as e:
                print(f"[app] voice failed: {e}")

        names = [r["name"] for r in reactions]
        dispatch = {"text": res.get("dispatch", "")} if res.get("dispatch") else None
        return (render_town_log(world_id, reactions, dispatch), render_roster(world_id),
                render_ticker(world_id), audio,
                gr.Dropdown(choices=names, value=names[0] if names else None),
                reactions, build_reactions_payload(world_id, reactions),
                render_explainer(world_id, reactions, focus), render_mode_badge(runtime),
                render_daily_log(world_id), render_crisis_hud(world_id),
                _outcome_card(res), build_crisis_payload(world_id))

    def run_scenario(scenario_id, world_id):
        world = get_world(world_id)
        scen = next((s for s in world.get("scenarios", []) if s["id"] == scenario_id), None)
        if not scen:
            return (gr.update(), gr.update(), gr.update(), gr.update(), gr.update(),
                    gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update())
        route = {"type": "world_event", "text": scen["event"], "instruction": scen["event"], "addressees": [], "goto": None}
        return (scen["event"],) + _run_event(world_id, scen["event"], scen.get("focus"), route)

    def random_chaos(world_id):
        world = get_world(world_id)
        if world and world.get("events"):
            return random.choice(world["events"])
        return events.random_event()

    def switch_world(world_id):
        world = get_world(world_id)
        if world:
            world_state.reset_world(world_id)
            world_state.init_cast(world)
        return (build_world_payload(world_id), "", render_town_log(world_id),
                render_roster(world_id), render_ticker(world_id),
                gr.Dropdown(choices=scenario_choices(world_id), value=None),
                render_explainer(world_id), world_id, render_mode_badge(), render_daily_log(world_id),
                render_crisis_hud(world_id), render_story_card(), build_crisis_payload(world_id))

    def sim_tick(world_id):
        world = get_world(world_id)
        if world:
            world_state.tick(world, hours=1.0)
        return (render_ticker(world_id), render_roster(world_id),
                build_state_payload(world_id), render_daily_log(world_id))

    def toggle_time(world_id):
        _, _, paused = world_state.get_game_time(world_id)
        world_state.set_paused(world_id, not paused)
        return sim_tick(world_id)

    def step_time(world_id):
        world = get_world(world_id)
        if world:
            world_state.tick(world, hours=1.0, force=True)
        return (render_ticker(world_id), render_roster(world_id),
                build_state_payload(world_id), render_daily_log(world_id))

    def transcribe_audio(audio_path):
        if not audio_path:
            return ("", '<div class="audio-cap">⚠ No recording found β€” press the mic ● record button first. '
                    '<i>The mic only works on http://localhost:7860.</i></div>')
        text = transcribe.transcribe(audio_path)
        if not text:
            return ("", '<div class="audio-cap">⚠ Could not transcribe. Try again, or just type the event.</div>')
        return (text, f'<div class="audio-cap">βœ… Heard: "<b>{esc(text)}</b>" β€” now press <b>⚑ THROW</b>.</div>')

    def hear_reaction(name, world_id, reactions):
        if not reactions or not name:
            return None
        rx = next((r for r in reactions if r["name"] == name), None)
        if not rx:
            return None
        world = get_world(world_id)
        try:
            cd = next(c for c in world["cast"] if c["name"] == name)
            return voice.generate_voice(rx["text"], cd["voice_description"])
        except Exception as e:
            print(f"[app] hear failed: {e}")
            return None

    def play_round(world_id):
        return _run_crisis_round(world_id, "")

    trig_out = [town_log_html, roster_html, ticker_html, voice_output,
                hear_name, last_reactions_state, reactions_box, explainer_html, mode_badge_html,
                daily_log_html]
    full_out = trig_out + [crisis_hud_html, story_card_html, crisis_box]
    trigger_btn.click(do_throw, [event_input, current_world_id, mode_radio], full_out)
    event_input.submit(do_throw, [event_input, current_world_id, mode_radio], full_out)
    start_campaign_btn.click(start_campaign, [current_world_id], full_out)
    next_round_btn.click(play_round, [current_world_id], full_out)
    run_scenario_btn.click(run_scenario, [scenario_dd, current_world_id], [event_input] + trig_out)
    random_btn.click(random_chaos, [current_world_id], [event_input])
    transcribe_btn.click(transcribe_audio, [mic_input], [event_input, mic_status])
    hear_btn.click(hear_reaction, [hear_name, current_world_id, last_reactions_state], [voice_output])
    world_picker.change(switch_world, [world_picker],
                        [world_box, reactions_box, town_log_html, roster_html, ticker_html,
                         scenario_dd, explainer_html, current_world_id, mode_badge_html, daily_log_html,
                         crisis_hud_html, story_card_html, crisis_box])
    pause_btn.click(toggle_time, [current_world_id], [ticker_html, roster_html, reactions_box, daily_log_html])
    step_btn.click(step_time, [current_world_id], [ticker_html, roster_html, reactions_box, daily_log_html])
    tick_timer.tick(sim_tick, [current_world_id], [ticker_html, roster_html, reactions_box, daily_log_html])


if __name__ == "__main__":
    port = int(os.environ.get("GRADIO_SERVER_PORT", "7860"))
    print("\n  TinyWorld is starting…")
    print(f"  β–Ά Open  http://localhost:{port}   (use localhost, not 0.0.0.0, so the mic works)\n")
    demo.launch(server_name="0.0.0.0", server_port=port, css=css_content, js=js_content or None)