Sussex 258 (Haines 108*, Tear 56) and 312 for 0 (Orr 185*, Haines 121*) lead Glamorgan 533 for 9 dec (Cooke 141, Gill 119, Lloyd 56, Patel 51*) by 37 runs
Captain Tom Haines completed two unbeaten hundreds in the same day and opening partner Ali Orr thrashed a magnificent 185 not out to transform Sussex's fortunes and end Glamorgan's promotion hopes in the LV= County Championship.
Haines, who had resumed on 34, became the first Sussex batsman since 2014 to carry his bat with 108 but Sussex were still bowled out for 258 in their first innings and followed on 275 behind at the 1st Central County Ground.
At that stage of the third day Glamorgan were firmly on course for the victory that would have kept their hopes of climbing out of Division Two alive.
But as it emerged that rivals Middlesex were getting themselves into an unbeatable position against Worcestershire to claim second spot, Glamorgan spirits seemed to sag and after tea and Orr and Haines took full advantage.
By stumps they had put on a magnificent 311 together in only 53 overs, with 190 of their runs coming in boundaries. Orr finished 185 not out (163 balls, 18 fours, nine sixes) and Haines will resume on 121 (157 balls, 16 fours) with Sussex now 37 runs in front after a remarkable day produced 481 runs.
Earlier, Haines and 18-year-old wicketkeeper-batsman Charlie Tear had provided the only resistance as Sussex twice collapsed, losing three wickets for 18 runs in the first hour before their last four went for six runs in just four overs.
James Harris sparked the first collapse when he picked up two wickets in four balls when Tom Clark was caught behind off a flat-footed waft and Dan Ibrahim unwisely tried to pull a ball outside off stump which cannoned into his stumps. Fynn Hudson-Prentice was also guilty of a poor shot as he gave Ajaz Patel an easy return catch before Haines and Tear came together.
They added 95 for the seventh wicket with Haines reaching his second hundred of the season, having made 234 against Derbyshire back in April.
Tear caught the eye too, and at one stage he hit six successive balls from New Zealand Test spinner Patel to the boundary as he made his maiden half-century in only his second appearance.
But when Tear played back to Tim van der Gugten Sussex folded again with the last three batters all out for ducks. Haines, who offered one sharp chance to David Lloyd at slip, walked off to a standing ovation having faced 169 balls and hit 14 fours.
Ten minutes later he was walking out to bat again and soon he and Orr were hurtling along at seven runs an over as Glamorgan's five-man attack toiled for a breakthrough.
Orr reached his fifty by hoisting Patel out of the ground for six and he needed just 38 more balls to get to his century, hitting Patel for three successive sixes at one stage. Just 41 deliveries were needed for his next 50 runs and the next milestone ticked off was his career-best 141 which he passed with a reverse sweep to the boundary off Patel, whose eight overs so far have gone for 78.
He wasn't the only bowler to suffer on a pitch offering little assistance. Orr passed 1,000 runs for the season when he got to 153 and shortly afterwards Haines was raising his bat for the second time in the day to acknowledge a standing ovation after reaching his century. He is the first Sussex batter to score two hundreds in the same match since Mike Yardy against Yorkshire in 2011.
At stumps the two left-handers had set a record partnership for any wicket against Glamorgan after passing Luke Wells and Ben Brown's 294 at Hove in 2016 while there have only been three bigger opening stands in the county's history.