All files / web/src/components/practice ActiveSession.tsx

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

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 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
'use client'

import { animated, useSpring } from '@react-spring/web'
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react'
import { flushSync } from 'react-dom'
import { useMyAbacus } from '@/contexts/MyAbacusContext'
import { useTheme } from '@/contexts/ThemeContext'
import { getCurrentProblemInfo, MAX_RETRY_EPOCHS } from '@/db/schema/session-plan-helpers'
import type {
  ProblemSlot,
  SessionHealth,
  SessionPart,
  SessionPartType,
  SessionPlan,
  SlotResult,
} from '@/db/schema/session-plans'

import { css } from '../../../styled-system/css'
import type { AutoPauseStats, PauseInfo } from './autoPauseCalculator'
import { calculateComplexityScaledThresholds, getComplexityCost } from './autoPauseCalculator'
import { getProgressiveAssistanceTiming, shouldUseDebugTiming } from '@/constants/helpTiming'
import { BrowseModeView, getLinearIndex } from './BrowseModeView'
import { PartTransitionScreen, TRANSITION_COUNTDOWN_MS } from './PartTransitionScreen'
import { extractTargetSkillName, PurposeBadge } from './PurposeBadge'
import { RetryTransitionScreen } from './RetryTransitionScreen'
import { SessionPausedModal } from './SessionPausedModal'

// Re-export types for consumers
export type { AutoPauseStats, PauseInfo }

/**
 * Student info needed for the pause modal
 */
export interface StudentInfo {
  id: string
  name: string
  emoji: string
  color: string
}

import { useAbacusDisplay } from '@soroban/abacus-react'
import { AbacusDock } from '../AbacusDock'
import { DecompositionProvider, DecompositionSection } from '../decomposition'
import { generateCoachHint } from './coachHintGenerator'
import { useHasPhysicalKeyboard } from './hooks/useDeviceDetection'
import { findMatchedPrefixIndex, useInteractionPhase } from './hooks/useInteractionPhase'
// Audio disabled for practice sessions (use abacus / visualize)
import { NumericKeypad } from './NumericKeypad'
import { PracticeFeedback } from './PracticeFeedback'
import { PracticeHelpOverlay } from './PracticeHelpOverlay'
import { DebugOverlay } from './DebugOverlay'
import { ProblemDebugPanel } from './ProblemDebugPanel'
import { AssistanceDebugPanel } from './AssistanceDebugPanel'
import { ProgressiveAssistanceUI } from './ProgressiveAssistanceUI'
import { useProgressiveAssistance } from './hooks/useProgressiveAssistance'
import { VerticalProblem } from './VerticalProblem'
import type { ReceivedAbacusControl } from '@/hooks/useSessionBroadcast'
import { useSetRemoteCameraSession } from '@/hooks/useSessionPlan'

/**
 * Timing data for the current problem attempt
 */
export interface AttemptTimingData {
  /** When the current attempt started */
  startTime: number
  /** Accumulated pause time in ms */
  accumulatedPauseMs: number
}

/**
 * Complexity data for broadcast (simplified for transmission)
 */
export interface BroadcastComplexity {
  /** Complexity bounds from slot constraints */
  bounds?: { min?: number; max?: number }
  /** Total complexity cost from generation trace */
  totalCost?: number
  /** Number of steps (for per-term average) */
  stepCount?: number
  /** Pre-formatted target skill name */
  targetSkillName?: string
}

/**
 * Broadcast state for session observation
 * This is sent to teachers observing the session in real-time
 */
export interface BroadcastState {
  /** Current problem being worked on */
  currentProblem: {
    terms: number[]
    answer: number
  }
  /** Unique slot ID for this problem (same ID used in answer validation) */
  slotId: string
  /** Current phase of the interaction */
  phase: 'problem' | 'feedback' | 'tutorial'
  /** Student's current answer (empty string if not yet started typing) */
  studentAnswer: string
  /** Whether the answer is correct (null if not yet submitted) */
  isCorrect: boolean | null
  /** When the current attempt started (timestamp) */
  startedAt: number
  /** Purpose of this problem slot (why this problem was selected) */
  purpose: 'focus' | 'reinforce' | 'review' | 'challenge'
  /** Complexity data for tooltip display */
  complexity?: BroadcastComplexity
  /** Current problem number (1-indexed for display) */
  currentProblemNumber: number
  /** Total problems in the session */
  totalProblems: number
  /** Session structure for progress indicator */
  sessionParts?: SessionPart[]
  /** Current part index for progress indicator */
  currentPartIndex?: number
  /** Current slot index within the part */
  currentSlotIndex?: number
  /** Accumulated results for progress indicator */
  slotResults?: SlotResult[]
}

type SubmitAdvanceDestination = 'next-problem-same-lane' | 'state-handoff'
type SubmitAdvanceIntent = 'slide-to-next-problem' | 'clear-to-loading'
type SubmitMode = 'practice' | 'redo'
type TransitionTarget = {
  problem: SlotResult['problem']
  slotIndex: number
  partIndex: number
}

type SubmitAdvanceResolution = {
  destination: SubmitAdvanceDestination
  intent: SubmitAdvanceIntent
  transitionTarget: TransitionTarget | null
}

function getSubmitAdvanceIntent(destination: SubmitAdvanceDestination): SubmitAdvanceIntent {
  return destination === 'next-problem-same-lane' ? 'slide-to-next-problem' : 'clear-to-loading'
}

interface ActiveSessionProps {
  plan: SessionPlan
  /** Historical results from prior completed sessions for threshold calculation */
  historicalResults?: SlotResult[]
  /** Student info for display in pause modal */
  student: StudentInfo
  /** Called when a problem is answered */
  onAnswer: (result: Omit<SlotResult, 'timestamp' | 'partNumber'>) => Promise<void>
  /** Called when session is ended early */
  onEndEarly: (reason?: string) => void
  /** Called when session is paused (with info about why) - for external HUD updates */
  onPause?: (pauseInfo: PauseInfo) => void
  /** Called when session is resumed - for external HUD updates */
  onResume?: () => void
  /** Called when session completes */
  onComplete: () => void
  /** Called with timing data when it changes (for external timing display) */
  onTimingUpdate?: (timing: AttemptTimingData | null) => void
  /** Called with broadcast state when it changes (for session observation) */
  onBroadcastStateChange?: (state: BroadcastState | null) => void
  /** Whether browse mode is active (controlled externally via toggle in PracticeSubNav) */
  isBrowseMode?: boolean
  /** Controlled browse index (linear problem index) */
  browseIndex?: number
  /** Called when browse index changes (for external navigation from progress indicator) */
  onBrowseIndexChange?: (index: number) => void
  /** Teacher abacus control received from observing teacher */
  teacherControl?: ReceivedAbacusControl | null
  /** Called after teacher control has been handled (to clear the state) */
  onTeacherControlHandled?: () => void
  /** Teacher-initiated pause request (from observing teacher) */
  teacherPauseRequest?: { message?: string } | null
  /** Called after teacher pause has been handled (to clear the state) */
  onTeacherPauseHandled?: () => void
  /** Teacher-initiated resume request (from observing teacher) */
  teacherResumeRequest?: boolean
  /** Called after teacher resume has been handled (to clear the state) */
  onTeacherResumeHandled?: () => void
  /** Manual pause request from parent (HUD pause button) */
  manualPauseRequest?: boolean
  /** Called after manual pause has been handled (to clear the state) */
  onManualPauseHandled?: () => void
  /** Called when a part transition starts (for broadcasting to observers) */
  onPartTransition?: (
    previousPartType: SessionPartType | null,
    nextPartType: SessionPartType,
    countdownStartTime: number,
    countdownDurationMs: number
  ) => void
  /** Called when a part transition completes (for broadcasting to observers) */
  onPartTransitionComplete?: () => void
  /** Redo state - when set, show a redo problem instead of current session problem */
  redoState?: {
    isActive: boolean
    linearIndex: number
    originalPartIndex: number
    originalSlotIndex: number
    originalResult: SlotResult
  } | null
  /** Called to record a redo result (separate from onAnswer since it doesn't advance position) */
  onRecordRedo?: (params: {
    playerId: string
    planId: string
    result: Omit<SlotResult, 'timestamp' | 'partNumber'>
    redoContext: {
      originalPartIndex: number
      originalSlotIndex: number
      originalWasCorrect: boolean
    }
  }) => Promise<SessionPlan>
  /** Called when the redo is complete or cancelled */
  onRedoComplete?: () => void
  /** Called to cancel the redo without recording */
  onCancelRedo?: () => void
  /** Called when a result is edited in browse mode (to trigger refetch) */
  onResultEdited?: () => void
  /** When true, show the debug overlay with submit buttons */
  debug?: boolean
}

/**
 * Calculate the number of abacus columns needed to compute a problem.
 *
 * This considers all intermediate running totals as we progress through the terms,
 * not just the final answer. For subtraction (e.g., 87 - 45 = 42), we need
 * enough columns for the minuend (87), not just the answer (42).
 */
function calculateAbacusColumns(terms: number[]): number {
  if (terms.length === 0) return 1

  let runningTotal = 0
  let maxAbsValue = 0

  for (const term of terms) {
    runningTotal += term
    maxAbsValue = Math.max(maxAbsValue, Math.abs(runningTotal))
  }

  return Math.max(1, String(maxAbsValue).length)
}

/**
 * Linear problem display component for Part 3
 */
function LinearProblem({
  terms,
  userAnswer,
  isFocused,
  isCompleted,
  correctAnswer,
  isDark,
  detectedPrefixIndex,
}: {
  terms: number[]
  userAnswer: string
  isFocused: boolean
  isCompleted: boolean
  correctAnswer: number
  isDark: boolean
  /** Detected prefix index - shows "..." instead of "=" for partial sums */
  detectedPrefixIndex?: number
}) {
  // Build the equation string
  const equation = terms
    .map((term, i) => {
      if (i === 0) return String(term)
      return term < 0 ? ` - ${Math.abs(term)}` : ` + ${term}`
    })
    .join('')

  // Use "..." for prefix sums (mathematically incomplete), "=" for final answer
  const isPrefixSum = detectedPrefixIndex !== undefined
  const operator = isPrefixSum ? '…' : '='

  // Use numeric comparison so "09" equals 9
  const numericUserAnswer = parseInt(userAnswer, 10)
  const answeredCorrectly = isCompleted && numericUserAnswer === correctAnswer

  return (
    <div
      data-component="linear-problem"
      data-prefix-mode={isPrefixSum ? 'true' : undefined}
      className={css({
        display: 'flex',
        alignItems: 'center',
        justifyContent: 'center',
        gap: '1rem',
        fontFamily: 'monospace',
        fontSize: '2rem',
        fontWeight: 'bold',
      })}
    >
      <span className={css({ color: isDark ? 'gray.200' : 'gray.800' })}>
        {equation}{' '}
        <span
          className={css({
            color: isPrefixSum
              ? isDark
                ? 'yellow.400'
                : 'yellow.600'
              : isDark
                ? 'gray.200'
                : 'gray.800',
          })}
        >
          {operator}
        </span>
      </span>
      <span
        className={css({
          minWidth: '80px',
          padding: '0.5rem 1rem',
          borderRadius: '8px',
          textAlign: 'center',
          backgroundColor: isCompleted
            ? answeredCorrectly
              ? isDark
                ? 'green.900'
                : 'green.100'
              : isDark
                ? 'red.900'
                : 'red.100'
            : isDark
              ? 'gray.800'
              : 'gray.100',
          color: isCompleted
            ? answeredCorrectly
              ? isDark
                ? 'green.200'
                : 'green.700'
              : isDark
                ? 'red.200'
                : 'red.700'
            : isDark
              ? 'gray.200'
              : 'gray.800',
          border: '2px solid',
          borderColor: isFocused ? 'blue.400' : isDark ? 'gray.600' : 'gray.300',
        })}
      >
        {userAnswer || (isFocused ? '?' : '')}
      </span>
    </div>
  )
}

/**
 * ActiveSession - The main practice session component
 *
 * Features:
 * - Three-part session structure (abacus, visualization, linear)
 * - Part-specific display and instructions
 * - Adaptive input (keyboard on desktop, on-screen keypad on mobile)
 * - Session health indicators
 * - On-screen abacus toggle (for abacus part only)
 * - Teacher controls (pause, end early)
 *
 * State Architecture:
 * - Uses useInteractionPhase hook for interaction state machine
 * - Single source of truth for all UI state
 * - Explicit phase transitions instead of boolean flags
 */
export function ActiveSession({
  plan,
  historicalResults,
  student,
  onAnswer,
  onEndEarly,
  onPause,
  onResume,
  onComplete,
  onTimingUpdate,
  onBroadcastStateChange,
  isBrowseMode: isBrowseModeProp = false,
  browseIndex: browseIndexProp,
  onBrowseIndexChange,
  teacherControl,
  onTeacherControlHandled,
  teacherPauseRequest,
  onTeacherPauseHandled,
  teacherResumeRequest,
  onTeacherResumeHandled,
  manualPauseRequest,
  onManualPauseHandled,
  onPartTransition,
  onPartTransitionComplete,
  redoState,
  onRecordRedo,
  onRedoComplete,
  onCancelRedo,
  onResultEdited,
  debug = false,
}: ActiveSessionProps) {
  const { resolvedTheme } = useTheme()
  const isDark = resolvedTheme === 'dark'

  // Check if abacus is docked (to force show submit button)
  const {
    isDockedByUser,
    requestDock,
    undock,
    dock,
    setDockedValue,
    visionConfig,
    captureTrainingColumns,
    setVisionRemoteSession,
  } = useMyAbacus()

  // Mutation for saving remote camera session ID to the session plan
  const setRemoteCameraSessionMutation = useSetRemoteCameraSession()

  // Track whether we've synced from DB to avoid infinite loops
  const hasInitializedRemoteSessionRef = useRef(false)
  const lastSyncedRemoteSessionRef = useRef<string | null>(null)

  // Sync remote camera session ID between session plan (DB) and vision config (context)
  useEffect(() => {
    // On mount: sync from DB to context if plan has a remote camera session
    if (!hasInitializedRemoteSessionRef.current) {
      hasInitializedRemoteSessionRef.current = true
      if (plan.remoteCameraSessionId) {
        setVisionRemoteSession(plan.remoteCameraSessionId)
        lastSyncedRemoteSessionRef.current = plan.remoteCameraSessionId
      }
      return
    }

    // After initialization: sync from context to DB when it changes
    const currentRemoteSession = visionConfig.remoteCameraSessionId
    if (currentRemoteSession !== lastSyncedRemoteSessionRef.current) {
      lastSyncedRemoteSessionRef.current = currentRemoteSession
      setRemoteCameraSessionMutation.mutate({
        playerId: student.id,
        planId: plan.id,
        remoteCameraSessionId: currentRemoteSession,
      })
    }
  }, [
    plan.id,
    plan.remoteCameraSessionId,
    student.id,
    visionConfig.remoteCameraSessionId,
    setVisionRemoteSession,
    setRemoteCameraSessionMutation,
  ])

  // Get abacus display config (for physical abacus column count in vision mode)
  const { config: abacusDisplayConfig } = useAbacusDisplay()

  // Track which problems have had training data collected (one frame per problem)
  const trainingDataCollectedRef = useRef<Set<string>>(new Set())

  // Collect training data when vision is enabled and answer is correct
  const collectVisionTrainingData = useCallback(
    async (correctAnswer: number, slotIndex: number) => {
      // Only collect if vision is enabled and docked
      if (!visionConfig.enabled || !isDockedByUser) return

      // Create a unique key for this problem to avoid duplicate collection
      const problemKey = `${plan.id}-${slotIndex}`
      if (trainingDataCollectedRef.current.has(problemKey)) return

      // Get column count from display config (physicalAbacusColumns for vision detection)
      const columnCount = abacusDisplayConfig?.physicalAbacusColumns ?? 5

      // Capture the current column images
      const columns = captureTrainingColumns(columnCount)
      if (!columns || columns.length === 0) {
        return
      }

      // Mark as collected before sending (to avoid race conditions)
      trainingDataCollectedRef.current.add(problemKey)

      // Send to the collection API (fire and forget - don't block the UI)
      try {
        const response = await fetch('/api/vision-training/collect', {
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({
            columns,
            correctAnswer,
            playerId: student.id,
            sessionId: plan.id,
          }),
        })
        if (!response.ok) {
          return
        }
      } catch {
        return
      }
    },
    [
      visionConfig.enabled,
      isDockedByUser,
      plan.id,
      student.id,
      captureTrainingColumns,
      abacusDisplayConfig,
    ]
  )

  // Compute initial problem from plan for SSR hydration (must be before useInteractionPhase)
  // Uses getCurrentProblemInfo to account for retry epochs
  const currentProblemInfo = useMemo(() => getCurrentProblemInfo(plan), [plan])

  const initialProblem = useMemo(() => {
    if (currentProblemInfo) {
      return {
        problem: currentProblemInfo.problem,
        slotIndex: currentProblemInfo.originalSlotIndex,
        partIndex: plan.currentPartIndex,
      }
    }
    return undefined
  }, [currentProblemInfo, plan.currentPartIndex])

  // ============================================================================
  // SINGLE SOURCE OF TRUTH: Which problem should be displayed right now
  // ============================================================================
  // This computed value derives the active problem from redoState + plan.
  // The hook reacts to changes in activeProblem.key to load new problems, eliminating
  // the need for synchronization effects between redoState and phase.attempt.problem.
  const activeProblem = useMemo(() => {
    // Redo mode takes priority - user clicked on a problem to redo
    if (redoState?.isActive) {
      const redoPart = plan.parts[redoState.originalPartIndex]
      const redoProblem = (redoPart?.slots[redoState.originalSlotIndex] as ProblemSlot)?.problem
      if (redoProblem) {
        return {
          problem: redoProblem,
          slotIndex: redoState.originalSlotIndex,
          partIndex: redoState.originalPartIndex,
          key: `redo-${redoState.linearIndex}`,
        }
      }
    }

    // Normal mode: use currentProblemInfo (handles epochs/retries correctly)
    if (currentProblemInfo?.problem) {
      return {
        problem: currentProblemInfo.problem,
        slotIndex: currentProblemInfo.originalSlotIndex,
        partIndex: plan.currentPartIndex,
        key: `normal-${plan.currentPartIndex}-${currentProblemInfo.originalSlotIndex}-${currentProblemInfo.epochNumber ?? 0}`,
      }
    }

    return null
  }, [redoState, plan.parts, plan.currentPartIndex, currentProblemInfo])
  const latestActiveProblemRef = useRef(activeProblem)
  const latestCurrentProblemInfoRef = useRef(currentProblemInfo)
  useEffect(() => {
    latestActiveProblemRef.current = activeProblem
    latestCurrentProblemInfoRef.current = currentProblemInfo
  }, [activeProblem, currentProblemInfo])

  // Interaction state machine - single source of truth for UI state
  const {
    phase,
    attempt,
    helpContext,
    outgoingAttempt,
    canAcceptInput,
    showAsCompleted,
    showHelpOverlay,
    showInputArea,
    showFeedback,
    inputIsFocused,
    isTransitioning,
    isPaused,
    isSubmitting,
    prefixSums,
    prefixMatch,
    matchedPrefixIndex,
    canSubmit,
    shouldAutoSubmit,
    ambiguousHelpTermIndex,
    handleDigit,
    handleBackspace,
    enterHelpMode,
    exitHelpMode,
    clearAnswer,
    setAnswer,
    startSubmit,
    completeSubmit,
    startTransition,
    completeTransition,
    clearToLoading,
    pause,
    resume,
  } = useInteractionPhase({
    initialProblem,
    activeProblem,
  })

  // Progressive assistance state machine — subsumes auto-pause timer
  const handleAutoPause = useCallback(
    (info: PauseInfo) => {
      setPauseInfo(info)
      pause()
      onPause?.(info)
    },
    [pause, onPause]
  )

  // Combine historical results from prior sessions with current session results.
  // Historical data provides the baseline for threshold calculation from session start;
  // current session results refine it as the student answers more problems.
  const thresholdResults = useMemo(
    () => [...(historicalResults ?? []), ...plan.results],
    [historicalResults, plan.results]
  )

  const assistance = useProgressiveAssistance({
    attempt: attempt
      ? {
          startTime: attempt.startTime,
          accumulatedPauseMs: attempt.accumulatedPauseMs,
          problem: {
            terms: attempt.problem.terms,
            generationTrace: attempt.problem.generationTrace,
          },
        }
      : null,
    results: thresholdResults,
    problemTermsCount: attempt?.problem?.terms?.length ?? 0,
    isPaused,
    onAutoPause: handleAutoPause,
  })

  // Debug: log complexity costs and thresholds for all problems
  // Skips the initial render before historical data loads to avoid a misleading defaults-only table.
  useEffect(() => {
    if (!historicalResults) return // still loading — skip
    if (typeof window === 'undefined') return
    try {
      if (localStorage.getItem('helpDebugTiming') !== 'true') return
    } catch {
      return
    }

    const timing = getProgressiveAssistanceTiming(shouldUseDebugTiming())
    const rows = plan.parts.flatMap((part, pi) =>
      part.slots.map((slot, si) => {
        const cost = getComplexityCost(slot.problem ?? null)
        const t = calculateComplexityScaledThresholds(thresholdResults, timing, cost)
        return {
          part: pi + 1,
          slot: si + 1,
          terms: slot.problem?.terms?.join(', ') ?? '?',
          cost: cost ?? '(none)',
          encourage: `${(t.encouragementMs / 1000).toFixed(1)}s`,
          helpOffer: `${(t.helpOfferMs / 1000).toFixed(1)}s`,
          autoPause: `${(t.autoPauseMs / 1000).toFixed(1)}s`,
        }
      })
    )
    void rows // thresholds computed for autopause timing
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [historicalResults])

  // Sync assistance machine with interaction phase transitions
  const prevPhaseRef = useRef(phase.phase)
  useEffect(() => {
    const prev = prevPhaseRef.current
    const curr = phase.phase
    prevPhaseRef.current = curr

    // Entered help mode
    if (curr === 'helpMode' && prev !== 'helpMode') {
      assistance.onHelpEntered()
    }
    // Exited help mode
    if (prev === 'helpMode' && curr !== 'helpMode') {
      assistance.onHelpExited()
    }
  }, [phase.phase, assistance.onHelpEntered, assistance.onHelpExited])

  // Notify assistance machine when problem changes
  const prevActiveProblemKeyRef = useRef(activeProblem?.key)
  useEffect(() => {
    const prevKey = prevActiveProblemKeyRef.current
    const currKey = activeProblem?.key
    prevActiveProblemKeyRef.current = currKey

    if (prevKey !== undefined && currKey !== prevKey) {
      assistance.onProblemChanged()
    }
  }, [activeProblem?.key, assistance.onProblemChanged])

  // Notify parent of timing data changes for external timing display
  useEffect(() => {
    if (onTimingUpdate) {
      if (attempt) {
        onTimingUpdate({
          startTime: attempt.startTime,
          accumulatedPauseMs: attempt.accumulatedPauseMs,
        })
      } else {
        onTimingUpdate(null)
      }
    }
  }, [onTimingUpdate, attempt?.startTime, attempt?.accumulatedPauseMs])

  // Audio disabled for practice sessions (use abacus / visualize)

  // Calculate total progress across all parts (needed for broadcast state)
  const totalProblems = useMemo(() => {
    return plan.parts.reduce((sum, part) => sum + part.slots.length, 0)
  }, [plan.parts])

  const completedProblems = useMemo(() => {
    let count = 0
    for (let i = 0; i < plan.currentPartIndex; i++) {
      count += plan.parts[i].slots.length
    }
    count += plan.currentSlotIndex
    return count
  }, [plan.parts, plan.currentPartIndex, plan.currentSlotIndex])

  // Notify parent of broadcast state changes for session observation
  useEffect(() => {
    if (!onBroadcastStateChange) return

    // Helper to extract complexity data from a slot
    const extractComplexity = (slot: ProblemSlot | undefined): BroadcastComplexity | undefined => {
      if (!slot) return undefined

      const bounds = slot.complexityBounds
      const trace = slot.problem?.generationTrace
      const hasBounds = bounds && (bounds.min !== undefined || bounds.max !== undefined)
      const hasCost = trace?.totalComplexityCost !== undefined

      if (!hasBounds && !hasCost) return undefined

      return {
        bounds: hasBounds ? { min: bounds?.min, max: bounds?.max } : undefined,
        totalCost: trace?.totalComplexityCost,
        stepCount: trace?.steps?.length,
        targetSkillName: extractTargetSkillName(slot) ?? undefined,
      }
    }

    // Get current slot's purpose from plan
    const currentPart = plan.parts[plan.currentPartIndex]
    const slot = currentPart?.slots[plan.currentSlotIndex]
    const purpose = slot?.purpose ?? 'focus'

    // During transitioning, we show the outgoing (completed) problem, not the incoming one
    // But we use the PREVIOUS slot's purpose since we're still showing feedback for it
    if (phase.phase === 'transitioning' && outgoingAttempt) {
      // During transition, use the previous slot's purpose and complexity
      const prevSlotIndex = plan.currentSlotIndex > 0 ? plan.currentSlotIndex - 1 : 0
      const prevSlot = currentPart?.slots[prevSlotIndex]
      const prevPurpose = prevSlot?.purpose ?? purpose

      onBroadcastStateChange({
        currentProblem: {
          terms: outgoingAttempt.problem.terms,
          answer: outgoingAttempt.problem.answer,
        },
        slotId: prevSlot?.slotId ?? slot?.slotId ?? '',
        phase: 'feedback',
        studentAnswer: outgoingAttempt.userAnswer,
        isCorrect: outgoingAttempt.result === 'correct',
        startedAt: attempt?.startTime ?? Date.now(),
        purpose: prevPurpose,
        complexity: extractComplexity(prevSlot),
        currentProblemNumber: completedProblems + 1,
        totalProblems,
        // Session structure for progress indicator
        sessionParts: plan.parts,
        // Use redo indices when in redo mode so observer sees correct active problem
        currentPartIndex: redoState?.isActive ? redoState.originalPartIndex : plan.currentPartIndex,
        currentSlotIndex: redoState?.isActive ? redoState.originalSlotIndex : plan.currentSlotIndex,
        slotResults: plan.results,
      })
      return
    }

    if (!attempt) {
      onBroadcastStateChange(null)
      return
    }

    // Map internal phase to broadcast phase
    let broadcastPhase: 'problem' | 'feedback' | 'tutorial'
    if (phase.phase === 'helpMode') {
      broadcastPhase = 'tutorial'
    } else if (phase.phase === 'showingFeedback') {
      broadcastPhase = 'feedback'
    } else {
      broadcastPhase = 'problem'
    }

    // Determine if answer is correct (only known in feedback phase)
    let isCorrect: boolean | null = null
    if (phase.phase === 'showingFeedback') {
      // Use the result stored in the phase, not calculated from attempt
      isCorrect = phase.result === 'correct'
    }

    onBroadcastStateChange({
      currentProblem: {
        terms: attempt.problem.terms,
        answer: attempt.problem.answer,
      },
      slotId: currentProblemInfo?.slotId ?? slot?.slotId ?? '',
      phase: broadcastPhase,
      studentAnswer: attempt.userAnswer,
      isCorrect,
      startedAt: attempt.startTime,
      purpose,
      complexity: extractComplexity(slot),
      currentProblemNumber: completedProblems + 1,
      totalProblems,
      // Session structure for progress indicator
      sessionParts: plan.parts,
      // Use redo indices when in redo mode so observer sees correct active problem
      currentPartIndex: redoState?.isActive ? redoState.originalPartIndex : plan.currentPartIndex,
      currentSlotIndex: redoState?.isActive ? redoState.originalSlotIndex : plan.currentSlotIndex,
      slotResults: plan.results,
    })
  }, [
    onBroadcastStateChange,
    attempt?.problem?.terms,
    attempt?.problem?.answer,
    attempt?.userAnswer,
    attempt?.startTime,
    phase,
    outgoingAttempt,
    plan.parts,
    plan.currentPartIndex,
    plan.currentSlotIndex,
    plan.results,
    completedProblems,
    totalProblems,
    redoState?.isActive,
    redoState?.originalPartIndex,
    redoState?.originalSlotIndex,
    currentProblemInfo?.slotId,
  ])

  // Handle teacher abacus control events
  useEffect(() => {
    if (!teacherControl) return

    // Only handle controls during problem-solving phases
    const isInputPhase =
      phase.phase === 'inputting' ||
      phase.phase === 'awaitingDisambiguation' ||
      phase.phase === 'helpMode'

    if (!isInputPhase || !attempt) {
      onTeacherControlHandled?.()
      return
    }

    switch (teacherControl.type) {
      case 'set-value':
        // Teacher sets the abacus to a specific value
        // Update both the docked abacus value AND the answer input
        setDockedValue(teacherControl.value)
        setAnswer(String(teacherControl.value))
        break
      case 'show-abacus':
        // Request dock with animation (triggers MyAbacus to animate into dock)
        if (!isDockedByUser && dock) {
          requestDock()
        }
        break
      case 'hide-abacus':
        // Undock the MyAbacus
        if (isDockedByUser) {
          undock()
        }
        break
    }

    // Clear the control after handling
    onTeacherControlHandled?.()
  }, [
    teacherControl,
    phase.phase,
    attempt,
    setAnswer,
    setDockedValue,
    isDockedByUser,
    dock,
    requestDock,
    undock,
    onTeacherControlHandled,
  ])

  // Handle teacher-initiated pause requests
  // Use a ref to track if we've handled this request to prevent duplicate handling
  const handledPauseRef = useRef(false)
  useEffect(() => {
    if (!teacherPauseRequest) {
      handledPauseRef.current = false
      return
    }
    if (handledPauseRef.current) return
    handledPauseRef.current = true

    // Pause the session with teacher reason
    const newPauseInfo: PauseInfo = {
      pausedAt: new Date(),
      reason: 'teacher',
      teacherMessage: teacherPauseRequest.message,
    }
    setPauseInfo(newPauseInfo)
    pause()
    onPause?.(newPauseInfo)

    // Clear the request after handling
    onTeacherPauseHandled?.()
  }, [teacherPauseRequest, pause, onPause, onTeacherPauseHandled])

  // Handle teacher-initiated resume requests
  // Use a ref to track if we've handled this request to prevent duplicate handling
  const handledResumeRef = useRef(false)
  useEffect(() => {
    if (!teacherResumeRequest) {
      handledResumeRef.current = false
      return
    }
    if (handledResumeRef.current) return
    handledResumeRef.current = true

    // Resume the session
    setPauseInfo(undefined)
    assistance.onResumed()
    resume()
    onResume?.()

    // Clear the request after handling
    onTeacherResumeHandled?.()
  }, [teacherResumeRequest, resume, onResume, onTeacherResumeHandled])

  // Handle manual pause requests from parent (HUD pause button)
  const handledManualPauseRef = useRef(false)
  useEffect(() => {
    if (!manualPauseRequest) {
      handledManualPauseRef.current = false
      return
    }
    if (handledManualPauseRef.current) return
    handledManualPauseRef.current = true

    // Pause the session with manual reason
    const newPauseInfo: PauseInfo = {
      pausedAt: new Date(),
      reason: 'manual',
    }
    setPauseInfo(newPauseInfo)
    pause()
    onPause?.(newPauseInfo)

    // Clear the request after handling
    onManualPauseHandled?.()
  }, [manualPauseRequest, pause, onPause, onManualPauseHandled])

  // Track which help elements have been individually dismissed
  // These reset when entering a new help session (helpContext changes)
  const [helpAbacusDismissed, setHelpAbacusDismissed] = useState(false)
  const [helpPanelDismissed, setHelpPanelDismissed] = useState(false)
  // Track when answer is fading out (for dream sequence)
  const [answerFadingOut, setAnswerFadingOut] = useState(false)
  // Capture reveal policy at submit-time so feedback is stable even if plan advances.
  const [showCorrectAnswerOnIncorrect, setShowCorrectAnswerOnIncorrect] = useState(true)

  // Track pause info for displaying in the modal (single source of truth)
  const [pauseInfo, setPauseInfo] = useState<PauseInfo | undefined>(undefined)

  // Part transition state - for showing transition screen between parts
  const [isInPartTransition, setIsInPartTransition] = useState(false)
  const [transitionData, setTransitionData] = useState<{
    previousPartType: SessionPartType | null
    nextPartType: SessionPartType
    countdownStartTime: number
  } | null>(null)
  // Track the previous part index to detect part changes
  const prevPartIndexRef = useRef<number>(plan.currentPartIndex)

  // Retry transition state - for showing transition screen between retry epochs
  const [isInRetryTransition, setIsInRetryTransition] = useState(false)
  const [retryTransitionData, setRetryTransitionData] = useState<{
    epochNumber: number
    problemCount: number
  } | null>(null)
  // Track previous epoch to detect epoch changes
  const prevEpochRef = useRef<number>(0)

  // Browse mode state - isBrowseMode is controlled via props
  // browseIndex can be controlled (browseIndexProp + onBrowseIndexChange) or internal
  const [internalBrowseIndex, setInternalBrowseIndex] = useState(0)

  // Determine if browse index is controlled
  const isControlledBrowseIndex = browseIndexProp !== undefined
  const browseIndex = isControlledBrowseIndex ? browseIndexProp : internalBrowseIndex

  // Unified setter that handles both controlled and uncontrolled modes
  const setBrowseIndex = useCallback(
    (index: number | ((prev: number) => number)) => {
      const newIndex = typeof index === 'function' ? index(browseIndex) : index
      if (isControlledBrowseIndex) {
        onBrowseIndexChange?.(newIndex)
      } else {
        setInternalBrowseIndex(newIndex)
      }
    },
    [browseIndex, isControlledBrowseIndex, onBrowseIndexChange]
  )

  // Compute current practice position as a linear index
  const currentPracticeLinearIndex = useMemo(() => {
    return getLinearIndex(plan.parts, plan.currentPartIndex, plan.currentSlotIndex)
  }, [plan.parts, plan.currentPartIndex, plan.currentSlotIndex])

  // When entering browse mode, initialize browseIndex to current problem
  const prevBrowseModeProp = useRef(isBrowseModeProp)
  useEffect(() => {
    if (isBrowseModeProp && !prevBrowseModeProp.current) {
      // Just entered browse mode - set to current practice position
      setBrowseIndex(currentPracticeLinearIndex)
    }
    prevBrowseModeProp.current = isBrowseModeProp
  }, [isBrowseModeProp, currentPracticeLinearIndex, setBrowseIndex])

  const handleDigitWithTimerReset = useCallback(
    (digit: string) => {
      assistance.onDigitTyped()
      handleDigit(digit)
    },
    [handleDigit, assistance.onDigitTyped]
  )

  // Reset dismissed states when help context changes (new help session)
  useEffect(() => {
    if (helpContext) {
      setHelpAbacusDismissed(false)
      setHelpPanelDismissed(false)
      setAnswerFadingOut(false)
    }
  }, [helpContext])

  // Exit help mode when both help elements are dismissed
  useEffect(() => {
    if (showHelpOverlay && helpAbacusDismissed && helpPanelDismissed) {
      exitHelpMode()
    }
  }, [showHelpOverlay, helpAbacusDismissed, helpPanelDismissed, exitHelpMode])

  // Refs for measuring problem widths during animation
  const outgoingRef = useRef<HTMLDivElement>(null)
  const activeRef = useRef<HTMLDivElement>(null)

  // Track if we need to apply centering offset (set true when transition starts)
  const needsCenteringOffsetRef = useRef(false)
  // Store the centering offset value for the animation end
  const centeringOffsetRef = useRef(0)

  // Spring for problem transition animation
  const [trackSpring, trackApi] = useSpring(() => ({
    x: 0,
    outgoingOpacity: 1,
    activeOpacity: 1,
    config: { tension: 200, friction: 26 },
  }))

  // Spring for submit button entrance animation
  // Show submit button when: manual submit is required OR abacus is docked (user needs way to submit)
  const showSubmitButton = attempt?.manualSubmitRequired || isDockedByUser
  const submitButtonSpring = useSpring({
    transform: showSubmitButton ? 'translateY(0px)' : 'translateY(60px)',
    opacity: showSubmitButton ? 1 : 0,
    scale: showSubmitButton ? 1 : 0.8,
    config: { tension: 280, friction: 14 },
  })

  // Apply centering offset before paint to prevent jank
  useLayoutEffect(() => {
    if (needsCenteringOffsetRef.current && outgoingRef.current) {
      const outgoingWidth = outgoingRef.current.offsetWidth
      const gap = 32 // 2rem gap
      const centeringOffset = (outgoingWidth + gap) / 2
      centeringOffsetRef.current = centeringOffset

      // Set initial position to compensate for flexbox centering
      trackApi.set({
        x: centeringOffset,
        outgoingOpacity: 1,
        activeOpacity: 0,
      })

      needsCenteringOffsetRef.current = false

      // Start fade-in of new problem
      trackApi.start({
        activeOpacity: 1,
        config: { tension: 200, friction: 26 },
      })

      // Start slide after a brief moment (150ms)
      setTimeout(() => {
        trackApi.start({
          x: -centeringOffset,
          outgoingOpacity: 0,
          config: { tension: 170, friction: 22 },
          onRest: () => {
            // Outgoing is now invisible - complete the transition
            flushSync(() => {
              completeTransition()
            })
            trackApi.set({ x: 0, outgoingOpacity: 1, activeOpacity: 1 })
          },
        })
      }, 150)
    }
  }, [outgoingAttempt, trackApi, completeTransition])

  const hasPhysicalKeyboard = useHasPhysicalKeyboard()

  // Track if keypad was ever shown - once shown, keep it visible
  // This prevents the keypad from disappearing if user uses physical keyboard
  const keypadWasShownRef = useRef(false)
  if (hasPhysicalKeyboard === false) {
    keypadWasShownRef.current = true
  }
  const showOnScreenKeypad = hasPhysicalKeyboard === false || keypadWasShownRef.current

  // Get current part and slot from plan (accounting for retry epochs)
  const parts = plan.parts
  const currentPartIndex = plan.currentPartIndex
  const currentSlotIndex = plan.currentSlotIndex
  const gameBreakTraceEnabled =
    typeof window !== 'undefined' &&
    new URLSearchParams(window.location.search).get('debugGameBreakTrace') === '1'
  const logGameBreakTrace = useCallback(
    (event: string, details: Record<string, unknown> = {}) => {
      if (!gameBreakTraceEnabled) return
      console.log('[GBTRACE][client]', event, {
        ts: new Date().toISOString(),
        planId: plan.id,
        currentPartIndex,
        currentSlotIndex,
        ...details,
      })
    },
    [gameBreakTraceEnabled, plan.id, currentPartIndex, currentSlotIndex]
  )
  const currentPart = parts[currentPartIndex] as SessionPart | undefined
  // Use getCurrentProblemInfo which handles both original slots and retry epochs
  const regularSlot = currentProblemInfo
    ? {
        slotId: currentProblemInfo.slotId,
        index: currentProblemInfo.originalSlotIndex,
        purpose: currentProblemInfo.purpose,
        problem: currentProblemInfo.problem,
        constraints: {},
      }
    : (currentPart?.slots[currentSlotIndex] as ProblemSlot | undefined)
  const sessionHealth = plan.sessionHealth as SessionHealth | null

  // Redo mode: get the redo problem slot if in redo mode
  const isInRedoMode = redoState?.isActive ?? false
  const redoSlot = useMemo(() => {
    if (!redoState?.isActive) return null
    const redoPart = parts[redoState.originalPartIndex]
    if (!redoPart) return null
    return redoPart.slots[redoState.originalSlotIndex] as ProblemSlot | undefined
  }, [redoState, parts])

  // Use redo slot when in redo mode, otherwise regular slot
  const currentSlot = isInRedoMode && redoSlot ? redoSlot : regularSlot
  // Whether the original redo problem was correct (for display and recording logic)
  const redoOriginalWasCorrect = redoState?.originalResult?.isCorrect ?? false

  // NOTE: Redo mode problem switching is now handled by the useInteractionPhase hook
  // via the activeProblem prop. The hook reacts to activeProblem.key changes and
  // loads the new problem automatically, eliminating synchronization bugs.

  // Retry epoch tracking
  const inRetryEpoch = currentProblemInfo?.isRetry ?? false
  const retryEpochNumber = currentProblemInfo?.epochNumber ?? 0

  // Check for session completion
  useEffect(() => {
    if (currentPartIndex >= parts.length) {
      onComplete()
    }
  }, [currentPartIndex, parts.length, onComplete])

  // Detect part index changes and trigger transition screen
  useEffect(() => {
    const prevIndex = prevPartIndexRef.current

    logGameBreakTrace('active-session-part-transition-effect', {
      prevIndex,
      currentPartIndex,
      partsLength: parts.length,
      willTriggerTransition: currentPartIndex !== prevIndex && currentPartIndex < parts.length,
    })

    // If part index changed and we have a valid next part
    if (currentPartIndex !== prevIndex && currentPartIndex < parts.length) {
      const prevPart = prevIndex < parts.length ? parts[prevIndex] : null
      const nextPart = parts[currentPartIndex]

      logGameBreakTrace('active-session-trigger-transition-screen', {
        previousPartType: prevPart?.type ?? null,
        nextPartType: nextPart.type,
      })

      // Trigger transition screen
      const startTime = Date.now()
      setTransitionData({
        previousPartType: prevPart?.type ?? null,
        nextPartType: nextPart.type,
        countdownStartTime: startTime,
      })
      setIsInPartTransition(true)

      // Broadcast transition to observers
      onPartTransition?.(prevPart?.type ?? null, nextPart.type, startTime, TRANSITION_COUNTDOWN_MS)
    }

    // Update ref for next comparison
    prevPartIndexRef.current = currentPartIndex
  }, [currentPartIndex, parts, onPartTransition, logGameBreakTrace])

  // Handle transition screen completion (countdown finished or user skipped)
  const handleTransitionComplete = useCallback(() => {
    logGameBreakTrace('active-session-transition-complete-callback')
    setIsInPartTransition(false)
    setTransitionData(null)
    // Broadcast transition complete to observers
    onPartTransitionComplete?.()
  }, [onPartTransitionComplete, logGameBreakTrace])

  // Detect retry epoch transitions and show retry transition screen
  useEffect(() => {
    const currentEpoch = retryEpochNumber
    const prevEpoch = prevEpochRef.current

    // If we just entered a new retry epoch (epoch increased from 0 to 1, or 1 to 2)
    if (currentEpoch > prevEpoch && currentEpoch > 0 && currentProblemInfo) {
      // Get the count of problems in this retry epoch
      const retryState = plan.retryState?.[plan.currentPartIndex]
      const problemCount = retryState?.currentEpochItems?.length ?? 0

      if (problemCount > 0) {
        setRetryTransitionData({
          epochNumber: currentEpoch,
          problemCount,
        })
        setIsInRetryTransition(true)
      }
    }

    prevEpochRef.current = currentEpoch
  }, [retryEpochNumber, currentProblemInfo, plan.retryState, plan.currentPartIndex])

  // Handle retry transition screen completion
  const handleRetryTransitionComplete = useCallback(() => {
    setIsInRetryTransition(false)
    setRetryTransitionData(null)
  }, [])

  // NOTE: Problem initialization is now handled by the useInteractionPhase hook
  // via the activeProblem prop. Initial load uses initialProblem for SSR hydration,
  // and subsequent changes (including redo mode and retry epochs) are detected via
  // activeProblem.key changes.

  // Auto-trigger help when an unambiguous prefix sum is detected
  // The awaitingDisambiguation phase handles the timer and auto-transitions to helpMode when it expires
  // This effect only handles the inputting phase case for unambiguous matches
  // DISABLED when abacus is docked - user controls when to submit, help triggers on submit if needed
  useEffect(() => {
    // Skip auto-help when abacus is docked - user has manual control
    if (isDockedByUser) return

    // Only handle unambiguous prefix matches in inputting phase
    // Ambiguous cases are handled by awaitingDisambiguation phase, which auto-transitions to helpMode
    if (phase.phase !== 'inputting') return

    // Skip ambiguous matches - they should go through awaitingDisambiguation flow
    if (prefixMatch.isAmbiguous) return

    // For unambiguous matches, trigger immediately
    if (matchedPrefixIndex >= 0 && matchedPrefixIndex < prefixSums.length - 1) {
      const newConfirmedCount = matchedPrefixIndex + 1
      if (newConfirmedCount < phase.attempt.problem.terms.length) {
        enterHelpMode(newConfirmedCount)
      }
    }
  }, [phase, prefixMatch, matchedPrefixIndex, prefixSums.length, enterHelpMode, isDockedByUser])

  // Handle when student reaches target value on help abacus
  // Sequence: show target value → dismiss abacus → show value in answer boxes → fade to empty → exit
  const handleTargetReached = useCallback(() => {
    if (phase.phase !== 'helpMode' || !helpContext) return

    // Notify assistance machine that a help term was completed
    assistance.onHelpTermCompleted(helpContext.termIndex)

    // Step 1: Set the answer to the target value (shows in answer boxes behind abacus)
    setAnswer(String(helpContext.targetValue))

    // Step 2: After a brief moment, dismiss the help abacus to reveal the answer
    setTimeout(() => {
      setHelpAbacusDismissed(true)

      // Step 3: After abacus fades out (300ms), answer boxes are visible with target value
      // Wait 1 second to let user see the result
      setTimeout(() => {
        // Step 4: Start fading out the answer
        setAnswerFadingOut(true)

        // Step 5: After fade-out completes (300ms), clear and exit
        setTimeout(() => {
          clearAnswer()
          setAnswerFadingOut(false)
          exitHelpMode()
        }, 300) // Match fade-out duration
      }, 1000) // Show target value in answer boxes for 1 second
    }, 600) // Brief pause to see success state on abacus
  }, [
    phase.phase,
    helpContext,
    setAnswer,
    clearAnswer,
    exitHelpMode,
    assistance.onHelpTermCompleted,
  ])

  // Handle value change from the docked abacus
  const handleAbacusDockValueChange = useCallback(
    (newValue: number) => {
      // When the abacus shows the correct answer, set it and auto-submit will trigger
      setAnswer(String(newValue))
    },
    [setAnswer]
  )

  const resolvePracticeTransitionTarget = useCallback(
    ({
      submittedPartIndex,
      submittedSlotIndex,
      submittedEpochNumber,
      activeProblemKeyAtSubmit,
    }: {
      submittedPartIndex: number
      submittedSlotIndex: number
      submittedEpochNumber: number
      activeProblemKeyAtSubmit: string | null
    }): TransitionTarget | null => {
      // Original lane progression can be derived synchronously from part slots.
      if (submittedEpochNumber === 0) {
        const submittedPart = plan.parts[submittedPartIndex]
        const nextSlot = submittedPart?.slots[submittedSlotIndex + 1]
        if (submittedPart && nextSlot?.problem) {
          return {
            problem: nextSlot.problem,
            slotIndex: submittedSlotIndex + 1,
            partIndex: submittedPartIndex,
          }
        }
      }

      // Retry and async mutation paths rely on the latest active problem snapshot.
      const latestActiveProblem = latestActiveProblemRef.current
      const latestCurrentProblemInfo = latestCurrentProblemInfoRef.current
      const hasAdvancedProblem =
        latestActiveProblem &&
        activeProblemKeyAtSubmit &&
        latestActiveProblem.key !== activeProblemKeyAtSubmit
      const isSamePartAsSubmitted = latestActiveProblem?.partIndex === submittedPartIndex
      const latestEpochNumber = latestCurrentProblemInfo?.epochNumber ?? submittedEpochNumber
      const isSameEpochAsSubmitted = latestEpochNumber === submittedEpochNumber

      if (hasAdvancedProblem && isSamePartAsSubmitted && isSameEpochAsSubmitted) {
        return {
          problem: latestActiveProblem.problem,
          slotIndex: latestActiveProblem.slotIndex,
          partIndex: latestActiveProblem.partIndex,
        }
      }

      return null
    },
    [plan.parts]
  )

  const enactSubmitAdvance = useCallback(
    ({ intent, transitionTarget }: SubmitAdvanceResolution) => {
      if (intent === 'slide-to-next-problem' && transitionTarget) {
        needsCenteringOffsetRef.current = true
        startTransition(
          transitionTarget.problem,
          transitionTarget.slotIndex,
          transitionTarget.partIndex
        )
        return
      }
      clearToLoading()
    },
    [startTransition, clearToLoading]
  )

  const resolveSubmitAdvanceWithWait = useCallback(
    ({
      mode,
      submittedPartIndex,
      submittedSlotIndex,
      submittedEpochNumber,
      activeProblemKeyAtSubmit,
      redoReturnTarget,
    }: {
      mode: SubmitMode
      submittedPartIndex: number
      submittedSlotIndex: number
      submittedEpochNumber: number
      activeProblemKeyAtSubmit: string | null
      redoReturnTarget: TransitionTarget | null
    }): Promise<SubmitAdvanceResolution> =>
      new Promise((resolve) => {
        if (mode === 'redo') {
          const destination: SubmitAdvanceDestination = redoReturnTarget
            ? 'next-problem-same-lane'
            : 'state-handoff'
          resolve({
            destination,
            intent: getSubmitAdvanceIntent(destination),
            transitionTarget: redoReturnTarget,
          })
          return
        }

        const maxWaitMs = 500
        const pollIntervalMs = 50
        const startedAt = Date.now()

        const settle = () => {
          const transitionTarget = resolvePracticeTransitionTarget({
            submittedPartIndex,
            submittedSlotIndex,
            submittedEpochNumber,
            activeProblemKeyAtSubmit,
          })
          const destination: SubmitAdvanceDestination = transitionTarget
            ? 'next-problem-same-lane'
            : 'state-handoff'
          resolve({
            destination,
            intent: getSubmitAdvanceIntent(destination),
            transitionTarget,
          })
        }

        const tick = () => {
          const transitionTarget = resolvePracticeTransitionTarget({
            submittedPartIndex,
            submittedSlotIndex,
            submittedEpochNumber,
            activeProblemKeyAtSubmit,
          })
          if (transitionTarget) {
            resolve({
              destination: 'next-problem-same-lane',
              intent: 'slide-to-next-problem',
              transitionTarget,
            })
            return
          }

          if (Date.now() - startedAt >= maxWaitMs) {
            settle()
            return
          }

          setTimeout(tick, pollIntervalMs)
        }

        tick()
      }),
    [resolvePracticeTransitionTarget]
  )

  // Handle submit
  const handleSubmit = useCallback(async () => {
    // Allow submitting from inputting, awaitingDisambiguation, or helpMode
    if (
      phase.phase !== 'inputting' &&
      phase.phase !== 'awaitingDisambiguation' &&
      phase.phase !== 'helpMode'
    ) {
      return
    }
    if (!phase.attempt.userAnswer) return

    const attemptData = phase.attempt
    const answerNum = parseInt(attemptData.userAnswer, 10)
    if (Number.isNaN(answerNum)) return

    // When abacus is docked and not already in help mode, check if answer is a prefix sum
    // If so, trigger help mode instead of submitting (mimic auto-help behavior on submit)
    // NOTE: We use findMatchedPrefixIndex which correctly handles the case where an
    // intermediate prefix sum equals the final answer (e.g., 65-34-15+33-18=31 where
    // prefixSums = [65, 31, 16, 49, 31] - indexOf(31) would return 1, not 4)
    if (isDockedByUser && phase.phase !== 'helpMode') {
      const prefixMatch = findMatchedPrefixIndex(attemptData.userAnswer, prefixSums)
      // helpTermIndex >= 0 means this is an intermediate prefix sum, not the final answer
      if (prefixMatch.matchedIndex >= 0 && prefixMatch.helpTermIndex >= 0) {
        enterHelpMode(prefixMatch.helpTermIndex)
        return
      }
    }

    // Transition to submitting phase
    startSubmit()

    // Subtract accumulated pause time to get actual response time
    const responseTimeMs = Date.now() - attemptData.startTime - attemptData.accumulatedPauseMs
    const isCorrect = answerNum === attemptData.problem.answer
    const epochNumberAtSubmit = currentProblemInfo?.epochNumber ?? 0
    const activeProblemKeyAtSubmit = activeProblem?.key ?? null
    const redoReturnTarget = currentProblemInfo
      ? {
          problem: currentProblemInfo.problem,
          slotIndex: currentProblemInfo.originalSlotIndex,
          partIndex: plan.currentPartIndex,
        }
      : null
    const hasRetriesRemaining = epochNumberAtSubmit < MAX_RETRY_EPOCHS
    // Only reveal the correct answer when retries are exhausted (epoch 2 wrong).
    // Never reveal during manual redo — the kid chose to retry voluntarily and
    // shouldn't get a free look at the answer.
    setShowCorrectAnswerOnIncorrect(isCorrect || !hasRetriesRemaining)

    // Notify assistance machine of incorrect answer
    if (!isCorrect) {
      assistance.onWrongAnswer()
    }

    // Collect vision training data if answer is correct and vision is enabled
    // This runs in the background (fire-and-forget) to not block the UI
    if (isCorrect) {
      collectVisionTrainingData(attemptData.problem.answer, attemptData.slotIndex)
    }

    // Record the result
    const result: Omit<SlotResult, 'timestamp' | 'partNumber'> = {
      slotId: currentProblemInfo!.slotId,
      slotIndex: attemptData.slotIndex,
      problem: attemptData.problem,
      studentAnswer: answerNum,
      isCorrect,
      responseTimeMs,
      skillsExercised: attemptData.problem.skillsRequired,
      usedOnScreenAbacus: phase.phase === 'helpMode',
      incorrectAttempts: assistance.machineState.context.wrongAttemptCount,
      hadHelp:
        phase.phase === 'helpMode' || assistance.machineState.context.helpedTermIndices.size > 0,
    }

    const submitMode: SubmitMode = isInRedoMode && redoState ? 'redo' : 'practice'

    // Handle redo recording separately - return path is still unified through submit advance resolver.
    if (submitMode === 'redo' && redoState) {
      // Only record if the original problem was incorrect
      // (Original correct = pure practice, no recording to avoid unintentional penalty)
      if (!redoOriginalWasCorrect && onRecordRedo) {
        // Add retry tracking fields
        const redoSlotId =
          plan.parts[redoState.originalPartIndex]?.slots[redoState.originalSlotIndex]?.slotId
        const redoResult: Omit<SlotResult, 'timestamp' | 'partNumber'> = {
          ...result,
          // Override slotId + slotIndex with the original slot from the redo
          slotId: redoSlotId,
          slotIndex: redoState.originalSlotIndex,
          isRetry: true,
          epochNumber: (redoState.originalResult.epochNumber ?? 0) + 1,
          originalSlotIndex: redoState.originalSlotIndex,
        }
        // Use the special redo mutation that doesn't advance session position
        await onRecordRedo({
          playerId: student.id,
          planId: plan.id,
          result: redoResult,
          redoContext: {
            originalPartIndex: redoState.originalPartIndex,
            originalSlotIndex: redoState.originalSlotIndex,
            originalWasCorrect: redoOriginalWasCorrect,
          },
        })
      }

      // Continue into unified completion path below.
    } else {
      // Optimistic submit: for epoch 0 mid-part, advance immediately without
      // waiting for the server. The mutation queue in PracticeClient serializes
      // the actual server writes.
      const nextSlotExists =
        !!plan.parts[attemptData.partIndex]?.slots[attemptData.slotIndex + 1]?.problem
      const canAdvanceOptimistically = epochNumberAtSubmit === 0 && nextSlotExists

      if (canAdvanceOptimistically) {
        void onAnswer(result)
      } else {
        await onAnswer(result)
      }
    }

    // Complete submit with result
    completeSubmit(isCorrect ? 'correct' : 'incorrect')

    // Wait for feedback display then resolve-and-enact the shared advance policy.
    setTimeout(
      () => {
        void resolveSubmitAdvanceWithWait({
          mode: submitMode,
          submittedPartIndex: attemptData.partIndex,
          submittedSlotIndex: attemptData.slotIndex,
          submittedEpochNumber: epochNumberAtSubmit,
          activeProblemKeyAtSubmit,
          redoReturnTarget,
        }).then((resolution) => {
          enactSubmitAdvance(resolution)
          if (submitMode === 'redo') {
            onRedoComplete?.()
          }
        })
      },
      isCorrect ? 500 : 1500
    )
  }, [
    phase,
    onAnswer,
    onRecordRedo,
    student.id,
    plan.id,
    plan.currentPartIndex,
    activeProblem?.key,
    startSubmit,
    completeSubmit,
    enactSubmitAdvance,
    resolveSubmitAdvanceWithWait,
    currentProblemInfo?.epochNumber,
    isDockedByUser,
    prefixSums,
    enterHelpMode,
    collectVisionTrainingData,
    isInRedoMode,
    redoState,
    redoOriginalWasCorrect,
    onRedoComplete,
    assistance.onWrongAnswer,
    assistance.machineState.context.wrongAttemptCount,
    assistance.machineState.context.helpedTermIndices,
  ])

  // Auto-submit when correct answer is entered
  useEffect(() => {
    if (shouldAutoSubmit) {
      handleSubmit()
    }
  }, [shouldAutoSubmit, handleSubmit])

  // Debug submit: set answer then submit after React commits the state update
  const debugSubmitInFlightRef = useRef(false)
  const handleSubmitRef = useRef(handleSubmit)
  handleSubmitRef.current = handleSubmit
  const debugSubmit = useCallback(
    (value: number) => {
      if (debugSubmitInFlightRef.current) return
      debugSubmitInFlightRef.current = true
      flushSync(() => {
        setAnswer(String(value))
      })
      // Use rAF to ensure React has committed the state before calling handleSubmit
      requestAnimationFrame(() => {
        handleSubmitRef.current()
        // Reset guard after submission completes (allow next submit after delay)
        setTimeout(() => {
          debugSubmitInFlightRef.current = false
        }, 2000)
      })
    },
    [setAnswer]
  )

  // Handle keyboard input
  useEffect(() => {
    if (!hasPhysicalKeyboard || !canAcceptInput) return

    const handleKeyDown = (e: KeyboardEvent) => {
      // Escape or Delete/Backspace exits help mode when in help mode
      if (e.key === 'Escape') {
        e.preventDefault()
        if (showHelpOverlay) {
          exitHelpMode()
        }
        return
      }
      if (e.key === 'Backspace' || e.key === 'Delete') {
        e.preventDefault()
        if (showHelpOverlay) {
          exitHelpMode()
        } else {
          handleBackspace()
        }
        return
      }
      if (e.key === 'Enter') {
        e.preventDefault()
        handleSubmit()
      } else if (/^[0-9]$/.test(e.key)) {
        handleDigitWithTimerReset(e.key)
      }
    }

    document.addEventListener('keydown', handleKeyDown)
    return () => document.removeEventListener('keydown', handleKeyDown)
  }, [
    hasPhysicalKeyboard,
    canAcceptInput,
    handleSubmit,
    handleDigitWithTimerReset,
    handleBackspace,
    showHelpOverlay,
    exitHelpMode,
  ])

  // Auto-pause is now handled by the progressive assistance state machine
  // (useProgressiveAssistance hook, TIMER_AUTO_PAUSE event)

  const handlePause = useCallback(
    (info?: PauseInfo) => {
      const newPauseInfo: PauseInfo = info ?? {
        pausedAt: new Date(),
        reason: 'manual',
      }
      setPauseInfo(newPauseInfo)
      pause()
      onPause?.(newPauseInfo)
    },
    [pause, onPause]
  )

  const handleResume = useCallback(() => {
    setPauseInfo(undefined)
    assistance.onResumed()
    resume()
    onResume?.()
  }, [resume, onResume, assistance.onResumed])

  // Handle skip (from progressive assistance) — show correct answer and advance
  const handleSkip = useCallback(async () => {
    if (!attempt) return

    // In redo mode, cancel the redo instead
    if (isInRedoMode && onCancelRedo) {
      onCancelRedo()
      return
    }

    // Record as incorrect with help
    const responseTimeMs = Date.now() - attempt.startTime - attempt.accumulatedPauseMs
    const result: Omit<SlotResult, 'timestamp' | 'partNumber'> = {
      slotId: currentProblemInfo!.slotId,
      slotIndex: attempt.slotIndex,
      problem: attempt.problem,
      studentAnswer: attempt.problem.answer, // Record the correct answer
      isCorrect: false, // Marked incorrect because student didn't solve it
      responseTimeMs,
      skillsExercised: attempt.problem.skillsRequired,
      usedOnScreenAbacus: false,
      incorrectAttempts: assistance.machineState.context.wrongAttemptCount,
      hadHelp: true,
      helpTrigger: 'manual' as const,
    }

    // Show the correct answer briefly
    setShowCorrectAnswerOnIncorrect(true)
    setAnswer(String(attempt.problem.answer))
    startSubmit()

    // Fire-and-forget: mutation enters the queue, 1500ms feedback delay
    // + clearToLoading provides enough time for the cache to update
    void onAnswer(result)

    completeSubmit('incorrect')

    // Advance after showing the answer
    setTimeout(() => {
      clearToLoading()
    }, 1500)
  }, [
    attempt,
    isInRedoMode,
    onCancelRedo,
    onAnswer,
    currentProblemInfo,
    setAnswer,
    startSubmit,
    completeSubmit,
    clearToLoading,
    assistance.machineState.context.wrongAttemptCount,
  ])

  // Handle help requested from progressive assistance UI
  const handleHelpFromAssistance = useCallback(() => {
    enterHelpMode(0)
  }, [enterHelpMode])

  const getHealthColor = (health: SessionHealth['overall']) => {
    switch (health) {
      case 'good':
        return 'green.500'
      case 'warning':
        return 'yellow.500'
      case 'struggling':
        return 'red.500'
      default:
        return 'gray.500'
    }
  }

  const getHealthEmoji = (health: SessionHealth['overall']) => {
    switch (health) {
      case 'good':
        return '🟢'
      case 'warning':
        return '🟡'
      case 'struggling':
        return '🔴'
      default:
        return '⚪'
    }
  }

  if (!currentPart || !attempt) {
    return (
      <div
        data-component="active-session"
        data-status="loading"
        className={css({
          display: 'flex',
          alignItems: 'center',
          justifyContent: 'center',
          minHeight: '300px',
        })}
      >
        <div
          className={css({
            fontSize: '1.25rem',
            color: isDark ? 'gray.400' : 'gray.500',
          })}
        >
          Loading next problem...
        </div>
      </div>
    )
  }

  // Browse mode - show the browse view instead of the practice view
  if (isBrowseModeProp) {
    return (
      <BrowseModeView
        plan={plan}
        browseIndex={browseIndex}
        currentPracticeIndex={currentPracticeLinearIndex}
        studentId={student.id}
        onResultEdited={onResultEdited}
      />
    )
  }

  // Check if we should show the abacus dock
  const showAbacusDock = currentPart.type === 'abacus' && !showHelpOverlay

  return (
    <div
      data-component="active-session"
      data-status={isPaused ? 'paused' : 'active'}
      data-phase={phase.phase}
      data-part-type={currentPart.type}
      className={css({
        display: 'flex',
        flexDirection: 'column',
        gap: { base: '0.75rem', md: '1rem' },
        padding: { base: '0.5rem', md: '1rem' },
        // Widen container when dock is shown to accommodate side-by-side layout
        maxWidth: showAbacusDock ? '780px' : '600px',
        margin: '0 auto',
        height: '100%',
        overflow: 'hidden',
        transition: 'max-width 0.3s ease',
      })}
    >
      {/* Problem + Dock row container */}
      <div
        data-section="problem-dock-row"
        className={css({
          display: 'flex',
          flexDirection: 'row',
          gap: '1.5rem',
          flex: 1,
          minHeight: 0,
          alignItems: 'stretch',
        })}
      >
        {/* Problem display */}
        <div
          data-section="problem-area"
          className={css({
            display: 'flex',
            flexDirection: 'column',
            alignItems: 'center',
            justifyContent: 'center',
            gap: { base: '0.75rem', md: '1rem' },
            flex: showAbacusDock ? '1 1 60%' : '1 1 100%',
            minHeight: 0,
            minWidth: 0,
            paddingTop: { base: '1rem', md: '2rem' },
            paddingRight: { base: '1rem', md: '2rem' },
            paddingBottom: { base: '1rem', md: '1.5rem' },
            paddingLeft: { base: '1rem', md: '2rem' },
            backgroundColor: isDark ? 'gray.800' : 'white',
            borderRadius: '16px',
            boxShadow: 'md',
            overflow: 'hidden',
          })}
        >
          {/* Redo mode banner */}
          {isInRedoMode && redoState && (
            <div
              data-element="redo-mode-banner"
              className={css({
                display: 'flex',
                alignItems: 'center',
                justifyContent: 'center',
                gap: '0.75rem',
                padding: '0.5rem 1rem',
                backgroundColor: isDark ? 'orange.900/80' : 'orange.100',
                borderRadius: '12px',
                border: '2px solid',
                borderColor: isDark ? 'orange.600' : 'orange.400',
                marginBottom: '0.5rem',
              })}
            >
              <span className={css({ fontSize: '1.25rem' })}>🔄</span>
              <span
                className={css({
                  fontWeight: '600',
                  fontSize: '0.875rem',
                  color: isDark ? 'orange.100' : 'orange.800',
                })}
              >
                Redoing Problem #{redoState.linearIndex + 1}
                {redoOriginalWasCorrect && (
                  <span
                    className={css({
                      marginLeft: '0.5rem',
                      fontWeight: '400',
                      fontSize: '0.75rem',
                      color: isDark ? 'orange.300' : 'orange.600',
                    })}
                  >
                    (Practice Only)
                  </span>
                )}
              </span>
              <button
                type="button"
                onClick={onCancelRedo}
                className={css({
                  padding: '0.25rem 0.5rem',
                  fontSize: '0.75rem',
                  fontWeight: '500',
                  color: isDark ? 'gray.300' : 'gray.600',
                  backgroundColor: isDark ? 'gray.700' : 'gray.200',
                  borderRadius: '6px',
                  border: 'none',
                  cursor: 'pointer',
                  transition: 'all 0.15s ease',
                  _hover: {
                    backgroundColor: isDark ? 'gray.600' : 'gray.300',
                  },
                })}
              >
                ← Back to #{completedProblems + 1}
              </button>
            </div>
          )}

          {/* Purpose badge with tooltip - shows retry indicator when in retry epoch */}
          {currentSlot && !isInRedoMode && (
            <div
              data-element="purpose-retry-container"
              className={css({
                display: 'flex',
                alignItems: 'center',
                gap: '0.5rem',
              })}
            >
              {/* Retry indicator badge */}
              {inRetryEpoch && (
                <div
                  data-element="retry-indicator"
                  data-epoch={retryEpochNumber}
                  className={css({
                    padding: '0.25rem 0.75rem',
                    borderRadius: '20px',
                    fontSize: '0.75rem',
                    fontWeight: 'bold',
                    textTransform: 'uppercase',
                    backgroundColor: isDark ? 'red.900' : 'red.100',
                    color: isDark ? 'red.200' : 'red.700',
                    border: '1px solid',
                    borderColor: isDark ? 'red.700' : 'red.300',
                  })}
                >
                  Retry {retryEpochNumber}/2
                </div>
              )}
              <PurposeBadge purpose={currentSlot.purpose} slot={currentSlot} />
            </div>
          )}

          {/* Problem display - centered, with help panel positioned outside */}
          <div
            data-element="problem-with-help"
            className={css({
              display: 'flex',
              justifyContent: 'center',
              width: '100%',
            })}
          >
            {/* Animated track for problem transitions */}
            <animated.div
              data-element="problem-track"
              style={{
                display: 'flex',
                alignItems: 'flex-start',
                transform: trackSpring.x.to((x) => `translateX(${x}px)`),
              }}
            >
              {/* Outgoing problem (slides left during transition) */}
              {outgoingAttempt && (
                <animated.div
                  ref={outgoingRef}
                  data-element="outgoing-problem"
                  style={{
                    opacity: trackSpring.outgoingOpacity,
                    marginRight: '2rem',
                    position: 'relative' as const,
                  }}
                >
                  <VerticalProblem
                    terms={outgoingAttempt.problem.terms}
                    userAnswer={outgoingAttempt.userAnswer}
                    isCompleted={true}
                    correctAnswer={outgoingAttempt.problem.answer}
                    size="large"
                    generationTrace={outgoingAttempt.problem.generationTrace}
                  />
                  {/* Feedback stays with outgoing problem */}
                  <PracticeFeedback
                    isCorrect={true}
                    correctAnswer={outgoingAttempt.problem.answer}
                    className={css({
                      position: 'absolute',
                      top: '100%',
                      left: '50%',
                      transform: 'translateX(-50%)',
                      marginTop: '0.5rem',
                      whiteSpace: 'nowrap',
                    })}
                  />
                </animated.div>
              )}

              {/* Current problem */}
              <animated.div
                ref={activeRef}
                data-element="problem-container"
                style={{
                  opacity: trackSpring.activeOpacity,
                  position: 'relative' as const,
                }}
              >
                {currentPart.format === 'vertical' ? (
                  <VerticalProblem
                    terms={attempt.problem.terms}
                    userAnswer={attempt.userAnswer}
                    isFocused={inputIsFocused}
                    isCompleted={showAsCompleted}
                    correctAnswer={attempt.problem.answer}
                    showCorrectAnswerOnIncorrect={showCorrectAnswerOnIncorrect}
                    size="large"
                    currentHelpTermIndex={helpContext?.termIndex}
                    needHelpTermIndex={
                      // Only show "need help?" prompt when not already in help mode
                      !showHelpOverlay && ambiguousHelpTermIndex >= 0
                        ? ambiguousHelpTermIndex
                        : undefined
                    }
                    rejectedDigit={attempt.rejectedDigit}
                    detectedPrefixIndex={
                      // Show vision feedback for prefix sums (not final answer)
                      matchedPrefixIndex >= 0 && matchedPrefixIndex < prefixSums.length - 1
                        ? matchedPrefixIndex
                        : undefined
                    }
                    helpOverlay={
                      // Always render overlay when in help mode (for exit transition)
                      showHelpOverlay && helpContext ? (
                        <PracticeHelpOverlay
                          currentValue={helpContext.currentValue}
                          targetValue={helpContext.targetValue}
                          columns={Math.max(
                            1,
                            Math.max(helpContext.currentValue, helpContext.targetValue).toString()
                              .length
                          )}
                          onTargetReached={handleTargetReached}
                          onDismiss={() => {
                            setHelpAbacusDismissed(true)
                            clearAnswer()
                          }}
                          visible={!helpAbacusDismissed}
                        />
                      ) : undefined
                    }
                    helpOverlayVisible={showHelpOverlay && !helpAbacusDismissed}
                    helpOverlayTransitionMs={helpAbacusDismissed ? 300 : 1000}
                    onHelpOverlayTransitionEnd={clearAnswer}
                    answerFadingOut={answerFadingOut}
                    generationTrace={attempt.problem.generationTrace}
                    complexityBudget={currentSlot?.constraints?.maxComplexityBudgetPerTerm}
                  />
                ) : (
                  <LinearProblem
                    terms={attempt.problem.terms}
                    userAnswer={attempt.userAnswer}
                    isFocused={inputIsFocused}
                    isCompleted={showAsCompleted}
                    correctAnswer={attempt.problem.answer}
                    isDark={isDark}
                    detectedPrefixIndex={
                      matchedPrefixIndex >= 0 && matchedPrefixIndex < prefixSums.length - 1
                        ? matchedPrefixIndex
                        : undefined
                    }
                  />
                )}

                {/* Help panel - absolutely positioned to the right of the problem */}
                {showHelpOverlay && helpContext && !helpPanelDismissed && (
                  <div
                    data-element="help-panel"
                    className={css({
                      position: 'absolute',
                      left: '100%',
                      top: 0,
                      marginLeft: '1.5rem',
                      display: 'flex',
                      flexDirection: 'column',
                      gap: '0.75rem',
                      padding: '1rem',
                      backgroundColor: isDark ? 'blue.900' : 'blue.50',
                      borderRadius: '12px',
                      border: '2px solid',
                      borderColor: isDark ? 'blue.700' : 'blue.200',
                      minWidth: '200px',
                      maxWidth: '280px',
                    })}
                  >
                    {/* Close button for help panel */}
                    <button
                      type="button"
                      data-action="close-help-panel"
                      onClick={() => setHelpPanelDismissed(true)}
                      className={css({
                        position: 'absolute',
                        top: '-8px',
                        right: '-8px',
                        width: '24px',
                        height: '24px',
                        display: 'flex',
                        alignItems: 'center',
                        justifyContent: 'center',
                        fontSize: '0.875rem',
                        fontWeight: 'bold',
                        color: isDark ? 'gray.400' : 'gray.500',
                        backgroundColor: isDark ? 'gray.700' : 'gray.200',
                        border: '2px solid',
                        borderColor: isDark ? 'gray.600' : 'gray.300',
                        borderRadius: '50%',
                        cursor: 'pointer',
                        zIndex: 10,
                        _hover: {
                          backgroundColor: isDark ? 'gray.600' : 'gray.300',
                          color: isDark ? 'gray.200' : 'gray.700',
                        },
                      })}
                      aria-label="Close help panel"
                    >
                      ×
                    </button>

                    {/* Coach hint */}
                    {(() => {
                      const hint = generateCoachHint(
                        helpContext.currentValue,
                        helpContext.targetValue
                      )
                      if (!hint) return null
                      return (
                        <div
                          data-element="coach-hint"
                          className={css({
                            padding: '0.5rem 0.75rem',
                            backgroundColor: isDark ? 'gray.800' : 'white',
                            borderRadius: '8px',
                            border: '1px solid',
                            borderColor: isDark ? 'blue.800' : 'blue.100',
                          })}
                        >
                          <p
                            className={css({
                              fontSize: '0.875rem',
                              color: isDark ? 'gray.300' : 'gray.700',
                              lineHeight: '1.4',
                              margin: 0,
                            })}
                          >
                            {hint}
                          </p>
                        </div>
                      )
                    })()}

                    {/* Decomposition display */}
                    <DecompositionProvider
                      startValue={helpContext.currentValue}
                      targetValue={helpContext.targetValue}
                      currentStepIndex={0}
                      abacusColumns={Math.max(
                        1,
                        Math.max(helpContext.currentValue, helpContext.targetValue).toString()
                          .length
                      )}
                    >
                      <DecompositionSection
                        className={css({
                          padding: '0.5rem 0.75rem',
                          backgroundColor: isDark ? 'gray.800' : 'white',
                          borderRadius: '8px',
                          border: '1px solid',
                          borderColor: isDark ? 'blue.800' : 'blue.100',
                          whiteSpace: 'nowrap',
                        })}
                        labelClassName={css({
                          fontSize: '0.625rem',
                          fontWeight: 'bold',
                          color: isDark ? 'blue.300' : 'blue.600',
                          marginBottom: '0.25rem',
                          textTransform: 'uppercase',
                        })}
                        contentClassName={css({
                          fontFamily: 'monospace',
                          fontSize: '0.875rem',
                          color: isDark ? 'gray.100' : 'gray.800',
                        })}
                      />
                    </DecompositionProvider>
                  </div>
                )}
              </animated.div>
            </animated.div>
          </div>

          {/* Feedback message - only show for incorrect */}
          {showFeedback && (
            <PracticeFeedback
              isCorrect={false}
              correctAnswer={attempt.problem.answer}
              showCorrectAnswer={showCorrectAnswerOnIncorrect}
            />
          )}
        </div>

        {/* Abacus dock - flex sibling of problem-area for proper layout */}
        {showAbacusDock && (
          <AbacusDock
            id="practice-abacus"
            columns={calculateAbacusColumns(attempt.problem.terms)}
            interactive={true}
            showNumbers={false}
            animated={true}
            onValueChange={handleAbacusDockValueChange}
            className={css({
              flex: '0 0 auto',
              // Responsive sizing - constrain both width and height
              // Mobile: smaller (140px-180px), Desktop: larger (up to 240px)
              width: {
                base: 'clamp(140px, 20vw, 180px)',
                md: 'clamp(180px, 18vw, 240px)',
              },
              // Mobile: shorter, Desktop: taller
              height: {
                base: 'clamp(180px, 28vh, 240px)',
                md: 'clamp(220px, 26vh, 300px)',
              },
              alignSelf: 'center',
              // Remove background/shadow - the docked content has its own styling
            })}
          />
        )}
      </div>

      {/* Progressive assistance UI (encouragement, help button, skip) */}
      {showInputArea && !isPaused && (
        <ProgressiveAssistanceUI
          machineState={assistance.machineState}
          showWrongAnswerSuggestion={assistance.showWrongAnswerSuggestion}
          isDark={isDark}
          onHelpRequested={handleHelpFromAssistance}
          onSkip={handleSkip}
          onDismissWrongAnswerSuggestion={assistance.dismissWrongAnswerSuggestion}
        />
      )}

      {/* Input area */}
      {showInputArea && !isPaused && (
        <div
          data-section="input-area"
          className={css({
            flexShrink: 0, // Don't shrink the input area
          })}
        >
          {/* Submit button - hidden on small screens when keypad is shown (integrated into keypad instead) */}
          {!showOnScreenKeypad && (
            <div
              className={css({
                display: 'flex',
                justifyContent: 'center',
                marginBottom: '0.75rem',
                minHeight: '48px',
                overflow: 'hidden',
              })}
            >
              <animated.button
                type="button"
                data-action="submit"
                data-visible={showSubmitButton}
                onClick={handleSubmit}
                disabled={!canSubmit || isSubmitting || !showSubmitButton}
                style={submitButtonSpring}
                className={css({
                  padding: '0.75rem 2rem',
                  fontSize: '1.125rem',
                  fontWeight: 'bold',
                  borderRadius: '8px',
                  border: 'none',
                  cursor: !canSubmit || !showSubmitButton ? 'not-allowed' : 'pointer',
                  backgroundColor: canSubmit ? 'blue.500' : isDark ? 'gray.700' : 'gray.300',
                  color: !canSubmit ? (isDark ? 'gray.400' : 'gray.500') : 'white',
                  _hover: {
                    backgroundColor:
                      canSubmit && showSubmitButton ? 'blue.600' : isDark ? 'gray.600' : 'gray.300',
                  },
                })}
              >
                Submit
              </animated.button>
            </div>
          )}

          {/* On-screen keypad for mobile - includes submit button */}
          {showOnScreenKeypad && (
            <NumericKeypad
              onDigit={handleDigitWithTimerReset}
              onBackspace={handleBackspace}
              onSubmit={handleSubmit}
              disabled={isSubmitting}
              currentValue={attempt.userAnswer}
              showSubmitButton={showSubmitButton}
            />
          )}
        </div>
      )}

      {/* Debug overlay - submit buttons for automated testing */}
      {debug && attempt?.problem && (
        <DebugOverlay
          correctAnswer={attempt.problem.answer}
          partIndex={currentPartIndex}
          slotIndex={currentSlotIndex}
          partType={currentPart?.type ?? 'abacus'}
          purpose={currentSlot?.purpose ?? 'focus'}
          onSubmitCorrect={() => debugSubmit(attempt.problem.answer)}
          onSubmitWrong={() => debugSubmit(attempt.problem.answer + 1)}
        />
      )}

      {/* Debug panel - shows current problem details when visual debug mode is on */}
      {currentSlot?.problem && (
        <ProblemDebugPanel
          problem={currentSlot.problem}
          slot={currentSlot}
          part={currentPart}
          partIndex={currentPartIndex}
          slotIndex={currentSlotIndex}
          userInput={attempt.userAnswer}
          phaseName={phase.phase}
        />
      )}

      {/* Assistance debug panel - shows state machine state when visual debug mode is on */}
      <AssistanceDebugPanel machineState={assistance.machineState} />

      {/* Warning when complexity cost data is missing — visible in dev, console.warn in prod */}
      {assistance.complexityCostMissing &&
        (process.env.NODE_ENV === 'production' ? (
          (() => {
            console.warn(
              '[autopause] complexityCostMissing: current problem has no generationTrace.totalComplexityCost — using flat 5-min timeout'
            )
            return null
          })()
        ) : (
          <div
            data-element="complexity-cost-warning"
            className={css({
              position: 'fixed',
              bottom: '4px',
              left: '4px',
              padding: '4px 8px',
              backgroundColor: 'rgba(255, 165, 0, 0.9)',
              color: 'black',
              fontSize: '11px',
              borderRadius: '4px',
              zIndex: 9999,
              pointerEvents: 'none',
            })}
          >
            Missing complexityCost — flat 5min timeout
          </div>
        ))}

      {/* Session Paused Modal - rendered here as single source of truth */}
      <SessionPausedModal
        isOpen={isPaused}
        student={student}
        session={plan}
        pauseInfo={pauseInfo}
        onResume={handleResume}
        onEndSession={() => onEndEarly('Session ended by user')}
      />

      {/* Part Transition Screen - full screen overlay between parts */}
      {transitionData && (
        <PartTransitionScreen
          isVisible={isInPartTransition}
          previousPartType={transitionData.previousPartType}
          nextPartType={transitionData.nextPartType}
          countdownStartTime={transitionData.countdownStartTime}
          student={student}
          onComplete={handleTransitionComplete}
        />
      )}

      {/* Retry Transition Screen - shown when entering a retry epoch */}
      {retryTransitionData && (
        <RetryTransitionScreen
          isVisible={isInRetryTransition}
          epochNumber={retryTransitionData.epochNumber}
          problemCount={retryTransitionData.problemCount}
          student={{ name: student.name, emoji: student.emoji }}
          onComplete={handleRetryTransitionComplete}
        />
      )}
    </div>
  )
}
export default ActiveSession