All files / web/src/components/toys/number-line NumberLine.tsx

0% Statements 0/1880
0% Branches 0/1
0% Functions 0/1
0% Lines 0/1880

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
'use client'

import { useRef, useEffect, useCallback, useState, useMemo } from 'react'
import { useTheme } from '../../../contexts/ThemeContext'
import type {
  NumberLineState,
  TickThresholds,
  CollisionFadeMap,
  RenderConstant,
  PrimeTickInfo,
} from './types'
import { DEFAULT_TICK_THRESHOLDS } from './types'
import { renderNumberLine } from './renderNumberLine'
import { useNumberLineTouch } from './useNumberLineTouch'
import { NumberLineDebugPanel } from './NumberLineDebugPanel'
import { VoiceDebugPanels } from './VoiceDebugPanels'
import { DemoRecommendations } from './DemoRecommendations'
import { KeyboardShortcutsOverlay } from './KeyboardShortcutsOverlay'
import { useDemoKeyboardShortcuts } from './useDemoKeyboardShortcuts'
import { DemoScrubberControls } from './DemoScrubberControls'
import { useFindTheNumberGame } from './useFindTheNumberGame'
import { createHandleExploreConstant } from './createHandleExploreConstant'
import { MATH_CONSTANTS } from './constants/constantsData'
import { computeAllConstantVisibilities } from './constants/computeConstantVisibility'
import { updateConstantMarkerDOM } from './constants/updateConstantMarkerDOM'
import { ConstantInfoCard } from './constants/ConstantInfoCard'
import { useConstantDemo } from './constants/demos/useConstantDemo'
import { CONSTANT_IDS, DEMO_RECOMMENDATIONS } from './talkToNumber/explorationRegistry'
import { GAME_MAP } from './talkToNumber/gameRegistry'
import { renderGoldenRatioOverlay, computeSweepTransform } from './constants/demos/goldenRatioDemo'
import { DEMO_OVERLAY_RENDERERS } from './constants/demos/demoOverlayRegistry'
import { useAudioManagerInstance } from '@/contexts/AudioManagerContext'
import { useConstantDemoNarration } from './constants/demos/useConstantDemoNarration'
import { NARRATION_CONFIGS } from './constants/demos/narrationConfigs'
import { usePhiExploreImage } from './constants/demos/usePhiExploreImage'
import { renderPhiExploreImage } from './constants/demos/renderPhiExploreImage'
import { usePhiCenteringMode } from './constants/demos/usePhiCenteringMode'
import { computePrimeInfos, smallestPrimeFactor } from './primes/sieve'
import { PrimeTooltip } from './primes/PrimeTooltip'
import {
  computePrimePairArcs,
  getSpecialPrimeLabels,
  LABEL_COLORS,
  PRIME_TYPE_DESCRIPTIONS,
} from './primes/specialPrimes'
import { computeInterestingPrimes } from './primes/interestingness'
import type { InterestingPrime } from './primes/interestingness'
import { usePrimeTour } from './primes/usePrimeTour'
import { renderTourSpotlight } from './primes/renderTourSpotlight'
import { renderSieveOverlay } from './primes/renderSieveOverlay'
import { PrimeTourOverlay } from './primes/PrimeTourOverlay'
import { computeTickMarks, numberToScreenX, screenXToNumber } from './numberLineTicks'
import { useRealtimeVoice } from './talkToNumber/useRealtimeVoice'
import type { CallState, SceneSnapshot } from './talkToNumber/useRealtimeVoice'
import { PhoneCallOverlay, updateCallBoxPositions } from './talkToNumber/PhoneCallOverlay'
import { useZoomWash } from './useZoomWash'
import { syncDemoScrubberDOM } from './syncDemoScrubberDOM'
import {
  computeTourHighlights,
  computeSieveState,
  computeIndicatorFade,
  computeEffectiveHovered,
} from './drawComputations'
import { useViewportFly } from './useViewportFly'
import { syncVoiceNarration } from './syncVoiceNarration'
import {
  renderLcmHopperOverlay,
  setActiveCombo,
  getActiveCombo,
  setGuess,
  clearGuess,
  getGuessPosition,
  getGuessResult,
  EARLY_HOP_END,
  GUESS_END,
} from './lcmHopper/renderLcmHopperOverlay'
import { LcmHopperOverlay } from './lcmHopper/LcmHopperOverlay'
import { useLcmHopperParty } from './lcmHopper/useLcmHopperParty'
import { HoppingPartyBar } from './lcmHopper/HoppingPartyBar'
import { evaluateGuess } from './lcmHopper/lcmHopperState'
import { useUserPlayers } from '@/hooks/useUserPlayers'
import { useVisualDebugSafe } from '@/contexts/VisualDebugContext'

// ── URL param persistence for demo playback state ────────────────────
let lastUrlUpdateMs = 0
const URL_UPDATE_INTERVAL_MS = 1000

function updateDemoUrlParams(constantId: string | null, progress: number) {
  const now = performance.now()
  // Debounce writes, but always allow immediate clears
  if (constantId !== null && now - lastUrlUpdateMs < URL_UPDATE_INTERVAL_MS) return
  lastUrlUpdateMs = now
  const url = new URL(window.location.href)
  if (constantId) {
    url.searchParams.set('demo', constantId)
    url.searchParams.set('p', progress.toFixed(3))
  } else {
    url.searchParams.delete('demo')
    url.searchParams.delete('p')
  }
  if (url.href !== window.location.href) {
    window.history.replaceState(window.history.state, '', url.href)
  }
}

const INITIAL_STATE: NumberLineState = {
  center: 0,
  pixelsPerUnit: 100,
}

/** Determine decimal precision of a target number (for hint phrasing). */
export type NumberLineMode = 'standalone' | 'exploration-break'

interface NumberLineProps {
  playerId?: string
  onPlayerIdentified?: (playerId: string) => void
  onCallStateChange?: (state: CallState) => void
  /** When 'exploration-break', suppresses voice/game/debug UI and auto-plays a demo */
  mode?: NumberLineMode
  /** Constant ID to auto-play on mount (e.g. 'pi', 'phi'). Only used in exploration-break mode. */
  autoPlayDemo?: string
  /** Fires when the auto-played demo's narration completes (revealProgress >= 1) */
  onDemoComplete?: (constantId: string) => void
}

export function NumberLine({
  playerId,
  onPlayerIdentified,
  onCallStateChange,
  mode = 'standalone',
  autoPlayDemo,
  onDemoComplete,
}: NumberLineProps = {}) {
  const isBreakMode = mode === 'exploration-break'
  const canvasRef = useRef<HTMLCanvasElement | null>(null)
  const stateRef = useRef<NumberLineState>({ ...INITIAL_STATE })
  const rafRef = useRef<number>(0)
  const { resolvedTheme } = useTheme()
  const { isVisualDebugEnabled, isDevelopment } = useVisualDebugSafe()
  const audioManager = useAudioManagerInstance()
  const phiExploreRef = usePhiExploreImage(resolvedTheme)
  const centering = usePhiCenteringMode(resolvedTheme)
  // Ref so the memoized draw callback can always read latest centering state
  const centeringRef = useRef(centering)
  centeringRef.current = centering

  // Tick thresholds — owned by NumberLineDebugPanel, synced via ref
  const thresholdsRef = useRef<TickThresholds>({ ...DEFAULT_TICK_THRESHOLDS })

  // Track CSS dimensions for rendering
  const cssWidthRef = useRef(0)
  const cssHeightRef = useRef(0)

  // Forward ref to break circular dependency: hooks need draw(), but draw() is defined later
  const drawFnRef = useRef<() => void>(() => {})

  // Zoom velocity + background color wash (extracted hook)
  const {
    displayVelocityRef,
    displayHueRef,
    zoomFocalXRef,
    decayRafRef,
    wrapperRef,
    pageRef,
    handleZoomVelocity,
    updateDisplayValues,
  } = useZoomWash({ resolvedTheme, drawRef: drawFnRef })

  // Persistent collision fade state for smooth label show/hide transitions
  const collisionFadeMapRef = useRef<CollisionFadeMap>(new Map())
  const collisionRafRef = useRef<number>(0)

  // --- Mathematical constants (Number Safari) state ---
  const [constantsEnabled, setConstantsEnabled] = useState(true)
  const constantsEnabledRef = useRef(true)
  constantsEnabledRef.current = constantsEnabled
  const [tappedConstantId, setTappedConstantId] = useState<string | null>(null)
  // Persist discovered constants in localStorage
  const [discoveredIds, setDiscoveredIds] = useState<Set<string>>(() => {
    if (typeof window === 'undefined') return new Set()
    try {
      const stored = localStorage.getItem('number-line-discovered-constants')
      return stored ? new Set(JSON.parse(stored) as string[]) : new Set()
    } catch {
      return new Set()
    }
  })
  const discoveredIdsRef = useRef(discoveredIds)
  discoveredIdsRef.current = discoveredIds
  // Last computed render constants (for hit-testing taps)
  const renderConstantsRef = useRef<RenderConstant[]>([])
  // Container for DOM-rendered MathML constant symbols
  const constantMarkersRef = useRef<HTMLDivElement>(null)

  // --- Constant demo state ---
  const demoRedraw = useCallback(() => drawFnRef.current(), [])
  const {
    demoState: demoStateRef,
    startDemo,
    restoreDemo,
    tickDemo,
    cancelDemo,
    setRevealProgress,
    markUserInteraction,
  } = useConstantDemo(stateRef, cssWidthRef, cssHeightRef, demoRedraw)
  // --- Constant demo narration (all constants) ---
  const narration = useConstantDemoNarration(demoStateRef, setRevealProgress, NARRATION_CONFIGS)

  // --- Demo URL restore (runs once on first resize) ---
  const hasRestoredRef = useRef(false)
  const [restoredFromUrl, setRestoredFromUrl] = useState(false)
  // Track whether onDemoComplete has been fired (fire once per demo)
  const demoCompleteFireRef = useRef(false)

  // Scene snapshot function ref — set after all hooks, read by mark_moment at runtime
  const sceneSnapshotFnRef = useRef<() => SceneSnapshot>(() => ({
    viewport: { center: stateRef.current.center, pixelsPerUnit: stateRef.current.pixelsPerUnit },
  }))

  // --- Demo scrubber state ---
  const scrubberTrackRef = useRef<HTMLDivElement>(null)
  const scrubberFillRef = useRef<HTMLDivElement>(null)
  const scrubberThumbRef = useRef<HTMLDivElement>(null)
  const scrubberGapRef = useRef<HTMLDivElement>(null)
  const playPauseBtnRef = useRef<HTMLButtonElement>(null)
  const timeDisplayRef = useRef<HTMLDivElement>(null)
  const segmentTicksRef = useRef<HTMLDivElement>(null)
  const segmentLabelRef = useRef<HTMLDivElement>(null)
  const lastTickConstantIdRef = useRef<string | null>(null)
  const [demoActive, setDemoActive] = useState(false)
  const isDraggingScrubberRef = useRef(false)
  const scrubberHoverProgressRef = useRef<number | null>(null)

  // --- Demo Refine mode state (dev-only) ---
  // Initialize from sessionStorage so an active task survives HMR (dev only)
  const isDev = process.env.NODE_ENV === 'development'
  const [refineMode, setRefineMode] = useState(() => {
    if (!isDev) return false
    try {
      return sessionStorage.getItem('refine-active-task') !== null
    } catch {
      return false
    }
  })
  const [refineRange, setRefineRange] = useState<{ start: number; end: number } | null>(null)
  const refineStartRef = useRef<number | null>(null)
  const [refineTaskActive, setRefineTaskActive] = useState(() => {
    if (!isDev) return false
    try {
      return sessionStorage.getItem('refine-active-task') !== null
    } catch {
      return false
    }
  })

  // --- Prime Tour state ---
  // Uses the same drawFnRef/demoRedraw pattern as useConstantDemo
  const {
    tourState: tourStateRef,
    currentStopIndex: tourStopIndex,
    totalStops: tourTotalStops,
    currentStop: tourCurrentStop,
    startTour,
    nextStop: tourNextStop,
    prevStop: tourPrevStop,
    exitTour,
    tickTour,
    forcedHoverValue,
  } = usePrimeTour(stateRef, cssWidthRef, cssHeightRef, demoRedraw)

  // --- Primes (Sieve of Eratosthenes) state ---
  const [primesEnabled, setPrimesEnabled] = useState(true)
  const primesEnabledRef = useRef(true)
  primesEnabledRef.current = primesEnabled
  const [hoveredValue, setHoveredValue] = useState<number | null>(null)
  const hoveredValueRef = useRef<number | null>(null)
  hoveredValueRef.current = hoveredValue
  // When true, the mouse is over the PrimeTooltip — don't clear hoveredValue
  const tooltipHoveredRef = useRef(false)
  // Tapped non-prime integer (click to show factorization)
  const [tappedIntValue, setTappedIntValue] = useState<number | null>(null)
  // Last computed prime infos (for tooltip data lookup)
  const primeInfosRef = useRef<Map<number, PrimeTickInfo>>(new Map())
  // Visible primes set for fast lookup in hover handler
  const visiblePrimesSetRef = useRef<Set<number>>(new Set())
  // Rendered interesting prime positions for hit-testing at low zoom
  const renderedPrimePositionsRef = useRef<{ value: number; screenX: number; note?: string }[]>([])
  // Current interesting primes for the visible range
  const interestingPrimesRef = useRef<InterestingPrime[]>([])

  // --- LCM Hopper / Hopping Party ---
  const dismissTooltip = useCallback(() => {
    setTappedIntValue(null)
    setHoveredValue(null)
    tooltipHoveredRef.current = false
  }, [])
  const {
    partyInvitees,
    setPartyInvitees,
    startLcmHopperDemo,
    handleToggleInvite,
    startHoppingParty,
    handleDismissPartyDemo,
    getPartyState,
  } = useLcmHopperParty({
    demoStateRef,
    narration,
    startDemo,
    cancelDemo,
    onDismissTooltip: dismissTooltip,
  })

  // --- Talk to a Number state ---
  const [callingNumber, setCallingNumber] = useState<number | null>(null)
  const handleVoiceTransfer = useCallback((targetNumber: number) => {
    setCallingNumber(targetNumber)
  }, [])
  // Tracks whether a demo exploration is active (for call timer freeze)
  const isExplorationActiveRef = useRef(false)

  // Ref-based forward declarations so useRealtimeVoice can control exploration
  const exploreFnRef = useRef<(constantId: string) => void>(() => {})
  const pauseFnRef = useRef<() => void>(() => {})
  const resumeFnRef = useRef<() => void>(() => {})
  const seekFnRef = useRef<(segmentIndex: number) => void>(() => {})
  const lookAtFnRef = useRef<(center: number, range: number) => void>(() => {})
  const indicateFnRef = useRef<
    (
      numbers: number[],
      range?: { from: number; to: number },
      durationSeconds?: number,
      persistent?: boolean
    ) => void
  >(() => {})
  const startFindNumberFnRef = useRef<(target: number) => void>(() => {})
  const stopFindNumberFnRef = useRef<() => void>(() => {})
  // When true, indicate's auto-zoom is suppressed (game controls the viewport)
  const suppressIndicateZoomRef = useRef(false)
  const handleVoiceGameStart = useCallback(
    (gameId: string, params: Record<string, unknown>) => {
      setActiveGameId(gameId)
      if (gameId === 'find_number') {
        const target = Number(params.target)
        if (isFinite(target)) startFindNumberFnRef.current(target)
      }
      if (gameId === 'guess_my_number') {
        // Lock viewport to full game range so the child sees the indicate band shrink
        const min = params.min !== undefined ? Number(params.min) : 1
        const max = params.max !== undefined ? Number(params.max) : 100
        const center = (min + max) / 2
        const range = (max - min) * 1.15 // 15% margin
        lookAtFnRef.current(center, range)
        suppressIndicateZoomRef.current = true
      }
      if (gameId === 'nim') {
        const stones =
          typeof params.stones === 'number' && isFinite(params.stones as number)
            ? (params.stones as number)
            : 15
        const center = (1 + stones) / 2
        const range = stones * 1.15 // 15% margin
        lookAtFnRef.current(center, range)
        suppressIndicateZoomRef.current = true
        // Boost label visibility — wide zoom makes individual numbers nearly invisible
        labelScaleRef.current = 1.8
        labelMinOpacityRef.current = 0.9
      }
      if (gameId === 'lcm_hopper') {
        startLcmHopperDemo()
      }
    },
    [startLcmHopperDemo]
  )
  const handleVoiceGameEnd = useCallback((gameId: string) => {
    setActiveGameId(null)
    suppressIndicateZoomRef.current = false
    if (gameId === 'find_number') {
      stopFindNumberFnRef.current()
    }
  }, [])
  const handleVoiceExploration = useCallback((constantId: string) => {
    exploreFnRef.current(constantId)
  }, [])
  const handleVoicePause = useCallback(() => {
    pauseFnRef.current()
  }, [])
  const handleVoiceResume = useCallback(() => {
    resumeFnRef.current()
  }, [])
  const handleVoiceSeek = useCallback((segIdx: number) => {
    seekFnRef.current(segIdx)
  }, [])
  const handleVoiceEndExploration = useCallback(() => {
    cancelDemo()
    narration.stop()
  }, [cancelDemo, narration])
  const handleVoiceLookAt = useCallback((center: number, range: number) => {
    lookAtFnRef.current(center, range)
  }, [])
  const handleVoiceIndicate = useCallback(
    (
      numbers: number[],
      range?: { from: number; to: number },
      durationSeconds?: number,
      persistent?: boolean
    ) => {
      indicateFnRef.current(numbers, range, durationSeconds, persistent)
    },
    []
  )
  const handleVoiceSetLabelStyle = useCallback((scale: number, minOpacity: number) => {
    labelScaleRef.current = scale
    labelMinOpacityRef.current = minOpacity
  }, [])
  // Fetch players for mid-call identification
  const { data: allPlayers } = useUserPlayers()
  const availablePlayerSummaries = useMemo(() => {
    if (!allPlayers) return []
    return allPlayers
      .filter((p) => p.isActive && !p.isArchived)
      .map((p) => ({ id: p.id, name: p.name, emoji: p.emoji || '👤' }))
  }, [allPlayers])

  const {
    state: voiceState,
    error: voiceError,
    errorCode: voiceErrorCode,
    dial,
    hangUp,
    timeRemaining,
    isSpeaking,
    transferTarget,
    conferenceNumbers,
    currentSpeaker,
    removeFromCall,
    sendSystemMessage,
    setNarrationPlaying,
    profileFailed,
    currentInstructions,
    modeDebug,
  } = useRealtimeVoice({
    onTransfer: handleVoiceTransfer,
    onStartExploration: handleVoiceExploration,
    onPauseExploration: handleVoicePause,
    onResumeExploration: handleVoiceResume,
    onSeekExploration: handleVoiceSeek,
    onEndExploration: handleVoiceEndExploration,
    onLookAt: handleVoiceLookAt,
    onIndicate: handleVoiceIndicate,
    onGameStart: handleVoiceGameStart,
    onGameEnd: handleVoiceGameEnd,
    onSetLabelStyle: handleVoiceSetLabelStyle,
    isExplorationActiveRef,
    onPlayerIdentified,
    getSceneSnapshot: () => sceneSnapshotFnRef.current(),
  })
  const callBoxContainerRef = useRef<HTMLDivElement>(null)
  const voiceStateRef = useRef<CallState>('idle')
  voiceStateRef.current = voiceState
  // Notify parent of call state changes
  useEffect(() => {
    onCallStateChange?.(voiceState)
  }, [voiceState, onCallStateChange])
  // Pending tour — queued during a call, launched after hangup
  const pendingTourRef = useRef<string | null>(null)
  const conferenceNumbersRef = useRef<number[]>([])
  conferenceNumbersRef.current = conferenceNumbers
  // Debug: log overlay render condition changes
  useEffect(() => {
    const shouldShow = callingNumber !== null && voiceState !== 'idle'
    console.log(
      '[NumberLine] overlay condition — callingNumber:',
      callingNumber,
      'voiceState:',
      voiceState,
      'shouldShow:',
      shouldShow
    )
  }, [callingNumber, voiceState])
  // Clear callingNumber when call ends (e.g. model hangs up).
  // Only clear when voiceState *transitions* to idle (not on initial mount
  // or when callingNumber is set before dial() goes async).
  const prevVoiceStateRef = useRef<CallState>('idle')
  useEffect(() => {
    const prev = prevVoiceStateRef.current
    prevVoiceStateRef.current = voiceState
    // Only clear if we transitioned TO idle FROM a non-idle state
    if (voiceState === 'idle' && prev !== 'idle') {
      if (callingNumber !== null) {
        console.log(
          '[NumberLine] clearing callingNumber because voiceState transitioned',
          prev,
          '→ idle'
        )
        setCallingNumber(null)
      }
      // Clear any active game when the call ends
      setActiveGameId(null)
      suppressIndicateZoomRef.current = false
      // Reset label style overrides
      labelScaleRef.current = 1
      labelMinOpacityRef.current = 0
      // Clear any lingering indicator
      indicatorRef.current = null
      // Launch pending tour after hangup
      const tourId = pendingTourRef.current
      if (tourId) {
        pendingTourRef.current = null
        console.log('[NumberLine] launching pending tour after hangup:', tourId)
        // Small delay so the call UI clears first
        setTimeout(() => {
          cancelDemo()
          setTappedConstantId(null)
          setTappedIntValue(null)
          setHoveredValue(null)
          startTour()
        }, 500)
      }
    }
  }, [voiceState, callingNumber, cancelDemo, startTour])
  // TTS narration plays during voice calls — the pre-recorded narrator handles
  // content while the voice agent acts as a silent companion.

  // --- Game session state (generic, across all games) ---
  const [activeGameId, setActiveGameId] = useState<string | null>(null)

  // --- Find the Number game ---
  const drawRef = useRef<() => void>(() => {})
  const {
    gameState,
    renderTargetRef,
    proximityRef,
    handleGameStart,
    handleGameGiveUp,
    computeGameProximity,
    gameStartedByModel,
    labelScaleRef,
    labelMinOpacityRef,
    gameRafRef,
  } = useFindTheNumberGame({
    stateRef,
    cssWidthRef,
    drawRef,
    voiceState,
    sendSystemMessage,
    setActiveGameId,
    startFindNumberFnRef,
    stopFindNumberFnRef,
  })

  const draw = useCallback(() => {
    const canvas = canvasRef.current
    if (!canvas) return
    const ctx = canvas.getContext('2d')
    if (!ctx) return

    const dpr = window.devicePixelRatio || 1
    const cssWidth = cssWidthRef.current
    const cssHeight = cssHeightRef.current

    // Compute proximity for Find the Number game
    computeGameProximity()

    // Compute constant visibilities
    let renderConstants: RenderConstant[] | undefined
    if (constantsEnabledRef.current) {
      renderConstants = computeAllConstantVisibilities(
        MATH_CONSTANTS,
        stateRef.current,
        cssWidth,
        discoveredIdsRef.current
      )
      renderConstantsRef.current = renderConstants
    } else {
      renderConstantsRef.current = []
    }

    // Compute prime infos, interesting primes, and pair arcs
    let primeInfos: Map<number, PrimeTickInfo> | undefined
    let interestingPrimes: InterestingPrime[] | undefined
    let primePairArcs: ReturnType<typeof computePrimePairArcs> | undefined
    if (primesEnabledRef.current) {
      const ticks = computeTickMarks(stateRef.current, cssWidth, thresholdsRef.current)
      primeInfos = computePrimeInfos(ticks)
      primeInfosRef.current = primeInfos

      // Compute interesting primes for axis-level markers (works at any zoom)
      const halfWidth = cssWidth / 2
      const ppu = stateRef.current.pixelsPerUnit
      const leftValue = stateRef.current.center - halfWidth / ppu
      const rightValue = stateRef.current.center + halfWidth / ppu
      interestingPrimes = computeInterestingPrimes(leftValue, rightValue, cssWidth)
      interestingPrimesRef.current = interestingPrimes
      visiblePrimesSetRef.current = new Set(interestingPrimes.map((ip) => ip.value))

      // Store rendered positions for hover hit-testing at low zoom
      const positions: { value: number; screenX: number; note?: string }[] = []
      for (const ip of interestingPrimes) {
        // Skip if already visible as a tick-based dot
        if (primeInfos.has(ip.value)) continue
        const sx = numberToScreenX(ip.value, stateRef.current.center, ppu, cssWidth)
        if (sx >= -5 && sx <= cssWidth + 5) {
          positions.push({ value: ip.value, screenX: sx, note: ip.note })
        }
      }
      renderedPrimePositionsRef.current = positions

      // Compute pair arcs from the raw prime values
      const primeValues = interestingPrimes.map((ip) => ip.value)
      primePairArcs = computePrimePairArcs(primeValues)
    } else {
      primeInfosRef.current = new Map()
      visiblePrimesSetRef.current = new Set()
      interestingPrimesRef.current = []
      renderedPrimePositionsRef.current = []
    }

    // Rate-limit display values before rendering
    updateDisplayValues()

    // Tick the constant demo state machine (updates viewport during animation)
    tickDemo()

    // Auto-start narration when viewport fly-in is ~60% done (revealProgress > 0)
    {
      const ds = demoStateRef.current
      if (ds.phase === 'animating' && ds.revealProgress > 0) {
        narration.startIfNeeded(ds.constantId)
      }
    }

    // Fire onDemoComplete when narration finishes (once per demo)
    {
      const ds = demoStateRef.current
      if (
        onDemoComplete &&
        !demoCompleteFireRef.current &&
        ds.phase === 'presenting' &&
        ds.revealProgress >= 1 &&
        ds.constantId
      ) {
        demoCompleteFireRef.current = true
        onDemoComplete(ds.constantId)
      }
      // Reset the flag when demo returns to idle (so next demo can fire again)
      if (ds.phase === 'idle') {
        demoCompleteFireRef.current = false
      }
    }

    // Write demo state to URL params (debounced).
    // Skip until restore has been attempted — otherwise the first draw()
    // (which runs before restore) clears the params while demo is still idle.
    // Also skip in break mode — no URL persistence for embedded explorations.
    if (hasRestoredRef.current && !isBreakMode) {
      const ds = demoStateRef.current
      if (ds.phase !== 'idle' && ds.constantId) {
        updateDemoUrlParams(ds.constantId, ds.revealProgress)
      } else {
        updateDemoUrlParams(null, 0)
      }
    }

    // Track whether an exploration is active (used by call timer to freeze countdown)
    isExplorationActiveRef.current =
      demoStateRef.current.phase !== 'idle' && demoStateRef.current.phase !== 'fading'

    // Feed narration segment updates to voice agent during active call
    {
      const ds = demoStateRef.current
      syncVoiceNarration(
        {
          voiceState: voiceStateRef.current,
          demoPhase: ds.phase,
          constantId: ds.constantId,
          revealProgress: ds.revealProgress,
          isNarrating: !!narration.isNarrating.current,
          segmentIndex: narration.segmentIndexRef.current,
          lastReportedSegment: voiceExplorationSegmentRef.current,
        },
        {
          sendSystemMessage,
          setNarrationPlaying,
          updateLastReportedSegment: (idx) => {
            voiceExplorationSegmentRef.current = idx
          },
        },
        ds.constantId ? NARRATION_CONFIGS[ds.constantId] : undefined
      )
    }

    // Tick the prime tour state machine (updates viewport during flights)
    tickTour()

    // Effective hovered value: tour forced hover overrides user hover
    const effectiveHovered = computeEffectiveHovered(tourStateRef.current, hoveredValueRef.current)

    // Compute tour spotlight highlight set (phase-aware)
    const tourTs = tourStateRef.current
    const { highlightSet, highlightedArcSet, dimAmount } = computeTourHighlights(tourTs)

    // Compute sieve tick transforms during ancient-trick tour stop
    const { sieveTransforms, sieveUniformity, sieveDwellElapsed } = computeSieveState(
      tourTs,
      stateRef.current,
      cssWidth,
      cssHeight
    )

    // Compute indicator fade lifecycle
    const { renderIndicator, expired: indicatorExpired } = computeIndicatorFade(
      indicatorRef.current
    )
    if (indicatorExpired) indicatorRef.current = null

    ctx.save()
    ctx.setTransform(1, 0, 0, 1, 0, 0) // reset any existing transform
    ctx.scale(dpr, dpr)
    const fadeAnimating = renderNumberLine(
      ctx,
      stateRef.current,
      cssWidth,
      cssHeight,
      resolvedTheme === 'dark',
      thresholdsRef.current,
      displayVelocityRef.current,
      displayHueRef.current,
      zoomFocalXRef.current,
      renderTargetRef.current,
      collisionFadeMapRef.current,
      renderConstants,
      primeInfos,
      effectiveHovered,
      interestingPrimes,
      primePairArcs,
      highlightSet,
      highlightedArcSet,
      sieveTransforms,
      sieveUniformity,
      renderIndicator
    )

    // Render constant demo overlay (golden ratio, etc.)
    const ds = demoStateRef.current

    // Render phi explore image behind the spiral (fades in during final 25%)
    // When centering mode is active, override with centering data and force full opacity
    const ct = centeringRef.current
    if (ct.enabled && ct.image) {
      renderPhiExploreImage(
        ctx,
        stateRef.current,
        cssWidth,
        cssHeight,
        1.0,
        1.0,
        ct.image,
        ct.alignment
      )
    } else if (ds.phase !== 'idle' && ds.constantId === 'phi' && ds.revealProgress > 0.75) {
      const pe = phiExploreRef.current
      if (pe) {
        const t = (ds.revealProgress - 0.75) / 0.25 // 0→1 over final quarter
        const imageOpacity = t * t * (3 - 2 * t) * ds.opacity // smoothstep × demo opacity
        renderPhiExploreImage(
          ctx,
          stateRef.current,
          cssWidth,
          cssHeight,
          ds.revealProgress,
          imageOpacity,
          pe.image,
          pe.alignment
        )
      }
    }

    // Render golden spiral overlay — always at full reveal when centering
    if (ct.enabled) {
      renderGoldenRatioOverlay(
        ctx,
        stateRef.current,
        cssWidth,
        cssHeight,
        resolvedTheme === 'dark',
        1.0,
        1.0
      )
    } else if (ds.phase !== 'idle' && ds.constantId === 'phi') {
      renderGoldenRatioOverlay(
        ctx,
        stateRef.current,
        cssWidth,
        cssHeight,
        resolvedTheme === 'dark',
        ds.revealProgress,
        ds.opacity
      )
    }
    // Render standard demo overlays (pi, tau, e, gamma, sqrt2, sqrt3, ln2, ramanujan, feigenbaum)
    if (ds.phase !== 'idle' && ds.constantId) {
      const renderer = DEMO_OVERLAY_RENDERERS[ds.constantId]
      if (renderer) {
        renderer(
          ctx,
          stateRef.current,
          cssWidth,
          cssHeight,
          resolvedTheme === 'dark',
          ds.revealProgress,
          ds.opacity
        )
      }
    }

    // Render sieve animation during ancient-trick tour stop
    if (sieveTransforms) {
      renderSieveOverlay(
        ctx,
        stateRef.current,
        cssWidth,
        cssHeight,
        resolvedTheme === 'dark',
        sieveDwellElapsed,
        tourTs.opacity
      )
    }

    // Render tour spotlight (dim overlay + pulse glow)
    if (tourTs.phase !== 'idle' && highlightSet && highlightSet.size > 0 && dimAmount > 0) {
      renderTourSpotlight(
        ctx,
        stateRef.current,
        cssWidth,
        cssHeight,
        resolvedTheme === 'dark',
        [...highlightSet],
        dimAmount,
        tourTs.opacity
      )
    }

    // Render LCM Hopper overlay (emojis, arcs, landing marks, celebration)
    if (ds.constantId === 'lcm_hopper' && ds.phase !== 'idle') {
      renderLcmHopperOverlay(
        ctx,
        stateRef.current,
        cssWidth,
        cssHeight,
        resolvedTheme === 'dark',
        ds.revealProgress,
        ds.opacity
      )
    }

    ctx.restore()

    // --- Sync demo scrubber DOM ---
    syncDemoScrubberDOM(
      {
        scrubberTrackRef,
        scrubberFillRef,
        scrubberThumbRef,
        scrubberGapRef,
        playPauseBtnRef,
        timeDisplayRef,
        segmentTicksRef,
        segmentLabelRef,
        lastTickConstantIdRef,
        isDraggingScrubberRef,
        scrubberHoverProgressRef,
      },
      ds,
      !!narration.isNarrating.current,
      resolvedTheme
    )
    // Sync React state for conditional rendering (batch with rAF)
    const isActive = ds.phase !== 'idle'
    if (isActive && !demoActive) setDemoActive(true)
    else if (!isActive && demoActive) setDemoActive(false)

    // Sync MathML DOM overlays with canvas
    if (constantMarkersRef.current && renderConstants) {
      const maxTickHeight = Math.min(40, cssHeight * 0.3)
      updateConstantMarkerDOM(
        constantMarkersRef.current,
        renderConstants,
        MATH_CONSTANTS,
        cssHeight / 2,
        maxTickHeight,
        resolvedTheme === 'dark'
      )
    } else if (constantMarkersRef.current) {
      // Constants disabled — clear any remaining DOM elements
      updateConstantMarkerDOM(
        constantMarkersRef.current,
        [],
        MATH_CONSTANTS,
        cssHeight / 2,
        0,
        resolvedTheme === 'dark'
      )
    }

    // Position call boxes at their number's screen-X
    if (callBoxContainerRef.current && voiceStateRef.current === 'active') {
      updateCallBoxPositions(
        callBoxContainerRef.current,
        stateRef.current.center,
        stateRef.current.pixelsPerUnit,
        cssWidth,
        cssHeight
      )
    }

    // Keep redrawing while collision fades are in progress
    if (fadeAnimating && !collisionRafRef.current) {
      collisionRafRef.current = requestAnimationFrame(() => {
        collisionRafRef.current = 0
        draw()
      })
    }
  }, [resolvedTheme])

  // Keep draw refs pointing at the latest draw function
  drawFnRef.current = draw
  drawRef.current = draw

  const scheduleRedraw = useCallback(() => {
    if (rafRef.current) return
    rafRef.current = requestAnimationFrame(() => {
      rafRef.current = 0
      draw()
    })
  }, [draw])

  // Redraw when centering mode state changes
  useEffect(() => {
    if (centering.enabled) scheduleRedraw()
  }, [
    centering.enabled,
    centering.image,
    centering.alignment,
    centering.subjectId,
    centering.theme,
    scheduleRedraw,
  ])

  // --- Hover handler for prime tooltips (primes only) ---
  const handleHover = useCallback(
    (hoverScreenX: number, _hoverScreenY: number) => {
      // Suppress user hover during prime tour (forced hover handles it)
      if (tourStateRef.current.phase !== 'idle') return

      if (hoverScreenX < 0) {
        if (hoveredValueRef.current !== null && !tooltipHoveredRef.current) {
          setHoveredValue(null)
          scheduleRedraw()
        }
        return
      }
      if (!primesEnabledRef.current) {
        if (hoveredValueRef.current !== null && !tooltipHoveredRef.current) setHoveredValue(null)
        return
      }

      const cssWidth = cssWidthRef.current
      const state = stateRef.current
      const value = screenXToNumber(hoverScreenX, state.center, state.pixelsPerUnit, cssWidth)
      const nearest = Math.round(value)

      // Check if cursor is within ~20px of the nearest integer's screen position
      const nearestScreenX = numberToScreenX(nearest, state.center, state.pixelsPerUnit, cssWidth)
      const dist = Math.abs(hoverScreenX - nearestScreenX)

      // Standard approach: snap to nearest integer prime
      if (dist < 20 && nearest >= 2 && smallestPrimeFactor(nearest) === nearest) {
        if (hoveredValueRef.current !== nearest) {
          setHoveredValue(nearest)
          scheduleRedraw()
        }
        return
      }

      // At low zoom, snap to nearest rendered interesting prime dot instead
      const renderedPrimes = renderedPrimePositionsRef.current
      if (renderedPrimes.length > 0 && renderedPrimes.length < 5000) {
        let closest: (typeof renderedPrimes)[number] | null = null
        let closestDist = Infinity
        for (const rp of renderedPrimes) {
          const d = Math.abs(hoverScreenX - rp.screenX)
          if (d < closestDist) {
            closest = rp
            closestDist = d
          }
        }
        if (closest && closestDist < 20) {
          if (hoveredValueRef.current !== closest.value) {
            setHoveredValue(closest.value)
            scheduleRedraw()
          }
          return
        }
      }

      // No prime nearby — but keep tooltip alive if mouse is over it
      if (hoveredValueRef.current !== null && !tooltipHoveredRef.current) {
        setHoveredValue(null)
        scheduleRedraw()
      }
    },
    [scheduleRedraw]
  )

  // --- Constants + non-prime integer tap handler ---
  const handleCanvasTap = useCallback(
    (screenX: number, _screenY: number) => {
      // LCM Hopper guess interception — active during guess phase of the demo
      {
        const ds = demoStateRef.current
        if (
          ds.constantId === 'lcm_hopper' &&
          ds.phase !== 'idle' &&
          ds.revealProgress >= EARLY_HOP_END &&
          ds.revealProgress < GUESS_END
        ) {
          const combo = getActiveCombo()
          if (combo) {
            const value = screenXToNumber(
              screenX,
              stateRef.current.center,
              stateRef.current.pixelsPerUnit,
              cssWidthRef.current
            )
            const snapped = Math.round(value)
            const result = evaluateGuess(snapped, combo.lcm)
            setGuess(snapped, result)
            return
          }
        }
      }

      // Check constants first
      if (constantsEnabledRef.current) {
        const HIT_RADIUS = 30
        const constants = renderConstantsRef.current
        let closest: RenderConstant | null = null
        let closestDist = Infinity

        for (const c of constants) {
          if (c.opacity < 0.3) continue
          const dist = Math.abs(c.screenX - screenX)
          if (dist < HIT_RADIUS && dist < closestDist) {
            closest = c
            closestDist = dist
          }
        }

        if (closest) {
          setDiscoveredIds((prev) => {
            const next = new Set(prev)
            next.add(closest!.id)
            try {
              localStorage.setItem('number-line-discovered-constants', JSON.stringify([...next]))
            } catch {
              /* ignore */
            }
            return next
          })
          setTappedConstantId(closest.id)
          setTappedIntValue(null)
          draw()
          return
        }
      }

      // Check for integer tap (all integers ≥ 2 — primes and composites)
      if (primesEnabledRef.current) {
        const cssWidth = cssWidthRef.current
        const state = stateRef.current
        const value = screenXToNumber(screenX, state.center, state.pixelsPerUnit, cssWidth)
        const nearest = Math.round(value)
        const nearestScreenX = numberToScreenX(nearest, state.center, state.pixelsPerUnit, cssWidth)
        const dist = Math.abs(screenX - nearestScreenX)

        if (dist < 20 && nearest >= 2) {
          setTappedConstantId(null)
          setTappedIntValue(nearest)
          return
        }
      }

      // Tap on empty space dismisses everything
      setTappedConstantId(null)
      setTappedIntValue(null)
    },
    [draw]
  )

  // Helper: get currently visible exploration recommendations (if any)
  const getVisibleRecommendations = useCallback((): string[] | undefined => {
    const ds = demoStateRef.current
    if (ds.revealProgress >= 1 && ds.constantId) {
      return DEMO_RECOMMENDATIONS[ds.constantId]
    }
    return undefined
  }, [demoStateRef])

  // --- Long-press handler for "Talk to a Number" ---
  const handleCanvasLongPress = useCallback(
    (screenX: number, _screenY: number) => {
      console.log(
        '[NumberLine] handleCanvasLongPress fired, screenX:',
        screenX,
        'current callingNumber:',
        callingNumber,
        'voiceState:',
        voiceStateRef.current
      )
      const cssWidth = cssWidthRef.current
      const state = stateRef.current
      const value = screenXToNumber(screenX, state.center, state.pixelsPerUnit, cssWidth)
      // Snap to the nearest visible tick mark — that's the number the user sees
      const ticks = computeTickMarks(state, cssWidth, thresholdsRef.current)
      let numberToCall = value
      let bestDist = Infinity
      for (const tick of ticks) {
        const d = Math.abs(value - tick.value)
        if (d < bestDist) {
          bestDist = d
          numberToCall = tick.value
        }
      }
      // Clean floating point noise (e.g. 399 * 0.01 = 3.9900000000000002 → 3.99)
      numberToCall = parseFloat(numberToCall.toPrecision(12))
      console.log(
        '[NumberLine] calling setCallingNumber(%s) and dial(%s)',
        numberToCall,
        numberToCall
      )
      setCallingNumber(numberToCall)
      dial(numberToCall, {
        recommendedExplorations: getVisibleRecommendations(),
        playerId,
        availablePlayers: !playerId ? availablePlayerSummaries : undefined,
      })
    },
    [dial, callingNumber, getVisibleRecommendations, playerId, availablePlayerSummaries]
  )

  // Touch/mouse/wheel handling
  const handleStateChange = useCallback(() => {
    setRestoredFromUrl(false)
    markUserInteraction()
    scheduleRedraw()
  }, [markUserInteraction, scheduleRedraw])

  // Capture current canvas at CSS resolution (1x, not DPR-scaled) for screenshot annotation
  const captureScreenshot = useCallback((): string | null => {
    const canvas = canvasRef.current
    if (!canvas) return null
    const cssW = cssWidthRef.current
    const cssH = cssHeightRef.current
    if (!cssW || !cssH) return null
    const offscreen = document.createElement('canvas')
    offscreen.width = cssW
    offscreen.height = cssH
    const ctx = offscreen.getContext('2d')
    if (!ctx) return null
    ctx.drawImage(canvas, 0, 0, cssW, cssH)
    return offscreen.toDataURL('image/png')
  }, [])

  useNumberLineTouch({
    stateRef,
    canvasRef,
    onStateChange: handleStateChange,
    onZoomVelocity: handleZoomVelocity,
    onTap: handleCanvasTap,
    onHover: handleHover,
    onLongPress: handleCanvasLongPress,
  })

  // ResizeObserver for responsive canvas sizing
  useEffect(() => {
    const canvas = canvasRef.current
    if (!canvas) return

    const resize = () => {
      const rect = canvas.getBoundingClientRect()
      const dpr = window.devicePixelRatio || 1
      cssWidthRef.current = rect.width
      cssHeightRef.current = rect.height
      canvas.width = rect.width * dpr
      canvas.height = rect.height * dpr
      draw()

      // Restore demo from URL params on first resize (canvas dimensions now set)
      if (!hasRestoredRef.current) {
        hasRestoredRef.current = true
        if (isBreakMode && autoPlayDemo) {
          // In exploration-break mode, auto-start the specified demo
          startDemo(autoPlayDemo)
        } else {
          const params = new URLSearchParams(window.location.search)
          const demoId = params.get('demo')
          const progressStr = params.get('p')
          if (demoId && CONSTANT_IDS.has(demoId)) {
            const progress = progressStr !== null ? parseFloat(progressStr) : 0
            if (!isNaN(progress)) {
              restoreDemo(demoId, progress)
              narration.markTriggered(demoId)
              setRestoredFromUrl(true)
            }
          }
        }
      }
    }

    const observer = new ResizeObserver(resize)
    observer.observe(canvas)

    // Initial size
    resize()

    return () => {
      observer.disconnect()
      if (rafRef.current) {
        cancelAnimationFrame(rafRef.current)
      }
      if (decayRafRef.current) {
        cancelAnimationFrame(decayRafRef.current)
      }
      if (gameRafRef.current) {
        cancelAnimationFrame(gameRafRef.current)
      }
      if (collisionRafRef.current) {
        cancelAnimationFrame(collisionRafRef.current)
      }
      // Restore page background on unmount
      if (pageRef.current) {
        pageRef.current.style.backgroundColor = ''
      }
    }
  }, [draw])

  // Redraw when theme changes
  useEffect(() => {
    draw()
  }, [draw])

  // Viewport fly animations (conference calls, look_at, indicate)
  const {
    lookAt: viewportLookAt,
    indicate: viewportIndicate,
    indicatorRef,
  } = useViewportFly({
    stateRef,
    cssWidthRef,
    draw,
    conferenceNumbers,
    voiceState,
    suppressIndicateZoomRef,
  })
  // Wire up forward refs so voice agent callbacks use the hook's implementations
  lookAtFnRef.current = viewportLookAt
  indicateFnRef.current = viewportIndicate

  // Update scene snapshot function now that all refs (indicator, renderTarget, demoState) are available
  sceneSnapshotFnRef.current = () => ({
    viewport: {
      center: stateRef.current.center,
      pixelsPerUnit: stateRef.current.pixelsPerUnit,
    },
    indicator: indicatorRef.current
      ? { numbers: indicatorRef.current.numbers, range: indicatorRef.current.range }
      : undefined,
    gameTarget: renderTargetRef.current
      ? { value: renderTargetRef.current.value, emoji: renderTargetRef.current.emoji }
      : undefined,
    activeExplorationId:
      demoStateRef.current.phase !== 'idle' ? demoStateRef.current.constantId : null,
    demoProgress:
      demoStateRef.current.phase !== 'idle' ? demoStateRef.current.revealProgress : undefined,
  })

  // Find tapped constant data for info card
  const tappedConstant = useMemo(
    () =>
      tappedConstantId ? (MATH_CONSTANTS.find((c) => c.id === tappedConstantId) ?? null) : null,
    [tappedConstantId]
  )
  // Find screen position of tapped constant from last render
  const tappedScreenX = useMemo(() => {
    if (!tappedConstantId) return 0
    const rc = renderConstantsRef.current.find((c) => c.id === tappedConstantId)
    return rc?.screenX ?? 0
  }, [tappedConstantId])

  const handleDismissInfoCard = useCallback(() => {
    console.log('[NumberLine] handleDismissInfoCard — calling audioManager.stop()')
    audioManager.stop()
    setTappedConstantId(null)
  }, [audioManager])

  const handleCallNumber = useCallback(
    (n: number) => {
      setCallingNumber(n)
      setTappedConstantId(null)
      setTappedIntValue(null)
      dial(n, {
        recommendedExplorations: getVisibleRecommendations(),
        playerId,
        availablePlayers: !playerId ? availablePlayerSummaries : undefined,
      })
    },
    [dial, getVisibleRecommendations, playerId, availablePlayerSummaries]
  )

  // Track which narration segment was last reported to the voice agent
  const voiceExplorationSegmentRef = useRef(-1)
  // Ref for speed reset — assigned after keyboard hook provides setDisplaySpeed/setShowSpeedBadge
  const speedResetRef = useRef(() => {})

  const handleExploreConstant = useCallback(
    createHandleExploreConstant({
      audioManager,
      voiceStateRef,
      pendingTourRef,
      voiceExplorationSegmentRef,
      narration,
      cancelDemo,
      exitTour,
      startTour,
      startDemo,
      restoreDemo,
      sendSystemMessage,
      onSpeedReset: () => speedResetRef.current(),
      setTappedConstantId,
      setTappedIntValue,
      setHoveredValue,
      tooltipHoveredRef,
    }),
    [
      audioManager,
      startDemo,
      restoreDemo,
      exitTour,
      startTour,
      cancelDemo,
      narration,
      sendSystemMessage,
    ]
  )
  // Keep refs pointed at latest implementations for voice agent callbacks
  exploreFnRef.current = handleExploreConstant
  pauseFnRef.current = () => {
    if (narration.isNarrating.current) narration.stop()
  }
  resumeFnRef.current = () => {
    const ds = demoStateRef.current
    console.log(
      '[exploration] resumeFnRef called — constantId:',
      ds.constantId,
      'revealProgress:',
      ds.revealProgress
    )
    if (ds.constantId && ds.revealProgress < 1) {
      console.log('[exploration] narration.resume() — TTS narration starts NOW')
      narration.resume(ds.constantId)
    }
  }
  seekFnRef.current = (segmentIndex: number) => {
    const ds = demoStateRef.current
    if (!ds.constantId) return
    const cfg = NARRATION_CONFIGS[ds.constantId]
    if (!cfg || segmentIndex < 0 || segmentIndex >= cfg.segments.length) return
    // Stop current narration, seek to segment start, leave paused
    if (narration.isNarrating.current) narration.stop()
    setRevealProgress(cfg.segments[segmentIndex].startProgress)
    // Update the voice agent's segment tracker so the segment cue re-fires on resume
    voiceExplorationSegmentRef.current = segmentIndex - 1
  }

  // --- Phi centering drag/scroll handlers ---
  const centeringDragRef = useRef<{
    startX: number
    startY: number
    startAlignment: { offsetX: number; offsetY: number; rotation: number }
    shift: boolean
  } | null>(null)
  const phiFinalEffScale = useMemo(() => computeSweepTransform(1.0).effScale, [])

  const handleCenteringMouseDown = useCallback((e: React.MouseEvent) => {
    e.preventDefault()
    e.stopPropagation()
    const al = centeringRef.current.alignment
    centeringDragRef.current = {
      startX: e.clientX,
      startY: e.clientY,
      startAlignment: { offsetX: al.offsetX, offsetY: al.offsetY, rotation: al.rotation },
      shift: e.shiftKey,
    }
  }, [])

  const handleCenteringMouseMove = useCallback(
    (e: React.MouseEvent) => {
      const drag = centeringDragRef.current
      if (!drag) return
      e.preventDefault()
      e.stopPropagation()
      const dx = e.clientX - drag.startX
      const dy = e.clientY - drag.startY
      const boxH = phiFinalEffScale * stateRef.current.pixelsPerUnit
      if (boxH < 1) return

      if (drag.shift) {
        // Shift+drag: rotate. Horizontal movement → degrees.
        // Renderer negates rotation, so subtract to make visual match drag direction.
        const degreeDelta = dx * 0.5
        centering.updateAlignment({ rotation: drag.startAlignment.rotation - degreeDelta })
      } else {
        // Normal drag: offset. Renderer negates offsetY, so subtract dy.
        const offsetDeltaX = dx / boxH
        const offsetDeltaY = -dy / boxH
        centering.updateAlignment({
          offsetX: drag.startAlignment.offsetX + offsetDeltaX,
          offsetY: drag.startAlignment.offsetY + offsetDeltaY,
        })
      }
    },
    [centering, phiFinalEffScale]
  )

  const handleCenteringMouseUp = useCallback(() => {
    centeringDragRef.current = null
  }, [])

  const handleCenteringWheel = useCallback(
    (e: React.WheelEvent) => {
      e.preventDefault()
      e.stopPropagation()
      const delta = -e.deltaY
      const factor = 1.01 ** delta
      const al = centeringRef.current.alignment
      centering.updateAlignment({ scale: al.scale * factor })
    },
    [centering]
  )

  // --- Prime Tour handlers ---
  const handleStartTour = useCallback(() => {
    cancelDemo() // mutual exclusion: cancel demo when starting tour
    setTappedConstantId(null)
    setTappedIntValue(null)
    setHoveredValue(null)
    tooltipHoveredRef.current = false
    startTour()
  }, [startTour, cancelDemo])

  // --- Scrubber pointer handlers ---
  /** Snap progress to a segment boundary if within 20px on screen. */
  const handlePlayPauseClick = useCallback(() => {
    setRestoredFromUrl(false)
    const ds = demoStateRef.current
    if (ds.phase === 'idle') return
    if (narration.isNarrating.current) {
      narration.stop()
    } else if (ds.constantId && ds.revealProgress >= 1) {
      // Replay from the beginning
      narration.reset()
      speedResetRef.current()
      startDemo(ds.constantId)
    } else if (ds.constantId && ds.revealProgress < 1) {
      narration.resume(ds.constantId)
    }
  }, [demoStateRef, narration, startDemo])

  // --- Keyboard shortcuts + speed display (extracted hook) ---
  const {
    showShortcuts,
    setShowShortcuts,
    displaySpeed,
    setDisplaySpeed,
    showSpeedBadge,
    setShowSpeedBadge,
    speedFadeTimerRef,
  } = useDemoKeyboardShortcuts({
    demoStateRef,
    narration,
    setRevealProgress,
    handlePlayPauseClick,
    audioManager,
    refineMode,
    refineTaskActive,
    setRefineMode,
    setRefineRange,
    refineStartRef,
    isVisualDebugEnabled,
    isDevelopment,
  })
  speedResetRef.current = () => {
    setDisplaySpeed(1)
    setShowSpeedBadge(false)
  }

  return (
    <div
      ref={wrapperRef}
      data-component="number-line-wrapper"
      style={{ display: 'flex', flexDirection: 'column', width: '100%', height: '100%' }}
    >
      {/* FindTheNumberBar hidden — the voice agent starts find_number via start_game tool */}
      <div data-element="canvas-wrapper" style={{ flex: 1, minHeight: 0, position: 'relative' }}>
        <canvas
          ref={canvasRef}
          data-component="number-line"
          style={{
            width: '100%',
            height: '100%',
            display: 'block',
            touchAction: 'none',
          }}
        />
        {!isBreakMode && centering.enabled && (
          <div
            data-element="centering-overlay"
            onMouseDown={handleCenteringMouseDown}
            onMouseMove={handleCenteringMouseMove}
            onMouseUp={handleCenteringMouseUp}
            onMouseLeave={handleCenteringMouseUp}
            onWheel={handleCenteringWheel}
            style={{
              position: 'absolute',
              inset: 0,
              cursor: centeringDragRef.current ? 'grabbing' : 'grab',
              zIndex: 5,
            }}
          />
        )}
        {!isBreakMode && (
          <div
            ref={constantMarkersRef}
            data-element="constant-markers"
            style={{
              position: 'absolute',
              inset: 0,
              pointerEvents: 'none',
              overflow: 'hidden',
            }}
          />
        )}
        {/* Prime Tour button removed — tour is now offered contextually via PrimeTooltip */}
        {!isBreakMode && tappedConstant && (
          <ConstantInfoCard
            constant={tappedConstant}
            screenX={tappedScreenX}
            containerWidth={cssWidthRef.current}
            containerHeight={cssHeightRef.current}
            centerY={cssHeightRef.current / 2}
            isDark={resolvedTheme === 'dark'}
            onDismiss={handleDismissInfoCard}
            onExplore={handleExploreConstant}
            onCallNumber={handleCallNumber}
          />
        )}
        {!isBreakMode &&
          activeGameId &&
          voiceState === 'active' &&
          (() => {
            const game = GAME_MAP.get(activeGameId)
            return (
              <div
                data-component="game-session-banner"
                style={{
                  position: 'absolute',
                  top: 56,
                  left: '50%',
                  transform: 'translateX(-50%)',
                  zIndex: 14,
                  display: 'flex',
                  alignItems: 'center',
                  gap: 8,
                  padding: '6px 16px',
                  borderRadius: 20,
                  background:
                    resolvedTheme === 'dark'
                      ? 'rgba(99, 102, 241, 0.25)'
                      : 'rgba(99, 102, 241, 0.15)',
                  backdropFilter: 'blur(8px)',
                  WebkitBackdropFilter: 'blur(8px)',
                  border: `1px solid ${resolvedTheme === 'dark' ? 'rgba(129, 140, 248, 0.3)' : 'rgba(99, 102, 241, 0.25)'}`,
                  color: resolvedTheme === 'dark' ? '#c7d2fe' : '#4338ca',
                  fontSize: '0.8rem',
                  fontWeight: 600,
                  letterSpacing: '0.02em',
                  pointerEvents: 'none',
                  userSelect: 'none',
                  whiteSpace: 'nowrap',
                }}
              >
                <span style={{ fontSize: '0.9rem' }}>{'🎮'}</span>
                {game?.name ?? activeGameId}
              </div>
            )
          })()}
        {!isBreakMode && callingNumber !== null && voiceState !== 'idle' && (
          <PhoneCallOverlay
            number={callingNumber}
            state={voiceState}
            timeRemaining={timeRemaining}
            error={voiceError}
            errorCode={voiceErrorCode}
            transferTarget={transferTarget}
            conferenceNumbers={conferenceNumbers}
            currentSpeaker={currentSpeaker}
            isSpeaking={isSpeaking}
            onHangUp={() => {
              hangUp()
              setCallingNumber(null)
            }}
            onRemoveFromCall={removeFromCall}
            onRetry={() => dial(callingNumber)}
            onDismiss={() => {
              hangUp()
              setCallingNumber(null)
            }}
            containerWidth={cssWidthRef.current}
            containerHeight={cssHeightRef.current}
            isDark={resolvedTheme === 'dark'}
            callBoxContainerRef={callBoxContainerRef}
          />
        )}
        {/* LCM Hopper overlay (guess prompt + celebration card) */}
        {!isBreakMode &&
          demoStateRef.current.constantId === 'lcm_hopper' &&
          demoStateRef.current.phase !== 'idle' && (
            <LcmHopperOverlay
              progress={demoStateRef.current.revealProgress}
              combo={getActiveCombo()}
              guessResult={getGuessResult()}
              guessPosition={getGuessPosition()}
              isDark={resolvedTheme === 'dark'}
              onDismiss={handleDismissPartyDemo}
            />
          )}
        {/* Hopping Party bar — shown when invitees selected and demo idle */}
        {!isBreakMode &&
          partyInvitees.length > 0 &&
          demoStateRef.current.phase === 'idle' &&
          tourStateRef.current.phase === 'idle' && (
            <HoppingPartyBar
              partyInvitees={partyInvitees}
              isDark={resolvedTheme === 'dark'}
              onToggleInvite={handleToggleInvite}
              onStartParty={startHoppingParty}
              onClearParty={() => setPartyInvitees([])}
            />
          )}
        {!isBreakMode &&
          demoActive &&
          demoStateRef.current.revealProgress >= 1 &&
          demoStateRef.current.constantId && (
            <DemoRecommendations
              constantId={demoStateRef.current.constantId}
              isDark={resolvedTheme === 'dark'}
              onExplore={handleExploreConstant}
            />
          )}
        {demoActive && (
          <DemoScrubberControls
            demoStateRef={demoStateRef}
            narration={narration}
            setRevealProgress={setRevealProgress}
            startDemo={startDemo}
            resolvedTheme={resolvedTheme}
            restoredFromUrl={restoredFromUrl}
            setRestoredFromUrl={setRestoredFromUrl}
            isDevelopment={isDev}
            onCaptureScreenshot={captureScreenshot}
            scrubberTrackRef={scrubberTrackRef}
            scrubberFillRef={scrubberFillRef}
            scrubberThumbRef={scrubberThumbRef}
            scrubberGapRef={scrubberGapRef}
            playPauseBtnRef={playPauseBtnRef}
            timeDisplayRef={timeDisplayRef}
            segmentTicksRef={segmentTicksRef}
            segmentLabelRef={segmentLabelRef}
            isDraggingScrubberRef={isDraggingScrubberRef}
            scrubberHoverProgressRef={scrubberHoverProgressRef}
            refineMode={refineMode}
            refineRange={refineRange}
            setRefineRange={setRefineRange}
            refineStartRef={refineStartRef}
            refineTaskActive={refineTaskActive}
            setRefineTaskActive={setRefineTaskActive}
            setRefineMode={setRefineMode}
            displaySpeed={displaySpeed}
            setDisplaySpeed={setDisplaySpeed}
            showSpeedBadge={showSpeedBadge}
            setShowSpeedBadge={setShowSpeedBadge}
            handlePlayPauseClick={handlePlayPauseClick}
          />
        )}
        {!isBreakMode &&
          (() => {
            const tooltipValue = forcedHoverValue ?? hoveredValue
            if (tooltipValue === null || !primesEnabled) return null
            const spf = tooltipValue >= 2 ? smallestPrimeFactor(tooltipValue) : 0
            const isPrime = tooltipValue >= 2 && spf === tooltipValue
            const ip = interestingPrimesRef.current.find((p) => p.value === tooltipValue)
            const tourAvailable = tourStateRef.current.phase === 'idle' && !demoActive
            const hoverPartyState = getPartyState(tooltipValue)
            return (
              <PrimeTooltip
                value={tooltipValue}
                primeInfo={{
                  value: tooltipValue,
                  smallestPrimeFactor: spf,
                  isPrime,
                  classification: tooltipValue === 1 ? 'one' : isPrime ? 'prime' : 'composite',
                }}
                screenX={numberToScreenX(
                  tooltipValue,
                  stateRef.current.center,
                  stateRef.current.pixelsPerUnit,
                  cssWidthRef.current
                )}
                tooltipY={cssHeightRef.current / 2 + Math.min(40, cssHeightRef.current * 0.3) + 30}
                containerWidth={cssWidthRef.current}
                isDark={resolvedTheme === 'dark'}
                landmarkNote={ip?.note}
                onStartTour={tourAvailable ? handleStartTour : undefined}
                partyState={hoverPartyState}
                onToggleInvite={hoverPartyState ? handleToggleInvite : undefined}
                onMouseEnter={() => {
                  tooltipHoveredRef.current = true
                }}
                onMouseLeave={() => {
                  tooltipHoveredRef.current = false
                }}
              />
            )
          })()}
        {!isBreakMode &&
          hoveredValue === null &&
          forcedHoverValue === null &&
          tappedIntValue !== null &&
          primesEnabled &&
          (() => {
            const v = tappedIntValue
            const spf = v >= 2 ? smallestPrimeFactor(v) : 0
            const isPrime = v >= 2 && spf === v
            const info: PrimeTickInfo =
              v === 1
                ? { value: 1, smallestPrimeFactor: 0, isPrime: false, classification: 'one' }
                : {
                    value: v,
                    smallestPrimeFactor: spf,
                    isPrime,
                    classification: isPrime ? 'prime' : 'composite',
                  }
            const tappedPartyState = getPartyState(v)
            return (
              <PrimeTooltip
                value={v}
                primeInfo={info}
                screenX={numberToScreenX(
                  v,
                  stateRef.current.center,
                  stateRef.current.pixelsPerUnit,
                  cssWidthRef.current
                )}
                tooltipY={cssHeightRef.current / 2 + Math.min(40, cssHeightRef.current * 0.3) + 30}
                containerWidth={cssWidthRef.current}
                isDark={resolvedTheme === 'dark'}
                partyState={tappedPartyState}
                onToggleInvite={tappedPartyState ? handleToggleInvite : undefined}
                onMouseEnter={() => {
                  tooltipHoveredRef.current = true
                }}
                onMouseLeave={() => {
                  tooltipHoveredRef.current = false
                }}
              />
            )
          })()}
        {/* Prime Tour overlay card */}
        {!isBreakMode && tourCurrentStop !== null && tourStopIndex !== null && (
          <PrimeTourOverlay
            stop={tourCurrentStop}
            stopIndex={tourStopIndex}
            totalStops={tourTotalStops}
            isDark={resolvedTheme === 'dark'}
            onNext={tourNextStop}
            onPrev={tourPrevStop}
            onClose={exitTour}
          />
        )}
        {!isBreakMode && !refineMode && (
          <NumberLineDebugPanel
            thresholdsRef={thresholdsRef}
            scheduleRedraw={scheduleRedraw}
            constantsEnabled={constantsEnabled}
            setConstantsEnabled={setConstantsEnabled}
            primesEnabled={primesEnabled}
            setPrimesEnabled={setPrimesEnabled}
            centering={centering}
            resolvedTheme={resolvedTheme}
          />
        )}
        {!isBreakMode && !refineMode && isVisualDebugEnabled && (
          <VoiceDebugPanels
            voiceState={voiceState}
            modeDebug={modeDebug}
            activeGameId={activeGameId}
            currentInstructions={currentInstructions}
            isDark={resolvedTheme === 'dark'}
          />
        )}
        {!isBreakMode &&
          (() => {
            const fv = forcedHoverValue ?? hoveredValue
            if (fv === null || !primesEnabled || fv < 2) return null
            const spf = smallestPrimeFactor(fv)
            if (spf !== fv) return null // not prime
            const labels = getSpecialPrimeLabels(fv)
            if (labels.length === 0) return null
            const isDark = resolvedTheme === 'dark'
            // Deduplicate types (a prime can have e.g. two twin pairs but one footnote)
            const seenTypes = new Set<string>()
            const uniqueLabels = labels.filter((l) => {
              if (seenTypes.has(l.type)) return false
              seenTypes.add(l.type)
              return true
            })
            return (
              <div
                data-element="prime-footnotes"
                style={{
                  position: 'absolute',
                  bottom: 'max(8px, env(safe-area-inset-bottom, 0px))',
                  left: 12,
                  right: 12,
                  display: 'flex',
                  flexWrap: 'wrap',
                  gap: '4px 14px',
                  pointerEvents: 'none',
                }}
              >
                {uniqueLabels.map((label) => (
                  <span
                    key={label.type}
                    data-element="prime-footnote"
                    style={{
                      fontSize: 10,
                      lineHeight: 1.4,
                      color: LABEL_COLORS[label.type][isDark ? 'dark' : 'light'],
                      opacity: 0.85,
                    }}
                  >
                    {PRIME_TYPE_DESCRIPTIONS[label.type]}
                  </span>
                ))}
              </div>
            )
          })()}

        {/* Keyboard shortcuts overlay */}
        {!isBreakMode && showShortcuts && demoStateRef.current.phase !== 'idle' && (
          <KeyboardShortcutsOverlay
            isDark={resolvedTheme === 'dark'}
            showRefineMode={isVisualDebugEnabled}
            onClose={() => setShowShortcuts(false)}
          />
        )}
      </div>
    </div>
  )
}