Dimuth Karunaratne holds the key to Sri Lanka salvaging the second Test with South Africa as the tourists reached 150-4 at stumps on day two - a second-innings lead of just five runs.
Having collapsed to 157 all out in their first innings, Sri Lanka saw Dean Elgar and Rassie van der Dussen build on their overnight stand of 114.
The pair added another 70, Elgar registering his 13th Test century and Van der Dussen eventually dismissed for 67.
But a maiden five-wicket haul in Tests from left-arm seamer Vishwa Fernando (5-101) in Johannesburg saw the Proteas collapse from 218-1 to 302 all out, denting their hopes of closing out a 2-0 series win.
A heavy defeat still looked the most likely outcome for Sri Lanka when Kusal Perera fell in the third over of their second innings.
Yet captain Karunaratne counter-attacked in thrilling fashion to post 91 at the close and ensure this side hold a slim advantage with six wickets still in hand.
Elgar knocked a Vishwa delivery past point to bring up three figures and at that moment there was little sign of the chaos to follow, with Van der Dussen easing to a half-century.
The opener eventually edged Dushmantha Chameera to first slip as his innings was ended on 127. Van der Dussen followed in short order, deemed to have gloved Dasun Shanaka behind on review.
Lungi Ngidi (3-26) went round the wicket to remove Perera, the seamer going on to account for Lahiru Thirimanne (31) and Kusal Mendis (0).
However, Sri Lanka - who lost Minod Bhanuka to a stunning Keshav Maharaj catch off first-innings tormenter Anrich Nortje - found hope at the top of the innings in the form of Karunaratne, whose 116-ball stay at the crease has so far yielded 17 fours.
Sri Lanka have little room left for error, though, and will hope the skipper and Niroshan Dickwella (18 not out) can guide them towards a total that Vishwa and an on-form attack at least have a chance of defending.