Australia were two wickets from victory at the end of day four of the first day-night test against Pakistan, but the tourists were buoyed by a feisty fourth innings chase and a brilliant unbeaten century from Asad Shafiq.
Pakistan were 382 for eight at stumps, 108 runs short of their victory target, but having won admiration with the highest fourth innings total at the Gabba.
Shafiq was 100 not out, after anchoring three stubborn partnerships, while Yasir Shah was on four at the end of a rain-interrupted day.
Shafiq will be key to Pakistan's faint hope of stealing a famous victory, the tourists having been set an improbable chase of 490.
Australia captain Steve Smith was berating himself for grassing two catches in the slips in the final session.
The second, off Mitchell Starc's bowling, would have dismissed Shafiq for 72 and left the tail-enders to fend for themselves.
Smith demanded an extra eight overs to push for the win and was finally rewarded when the eighth wicket fell in the final over.
He pouched a much harder chance in the slips when paceman Jackson Bird had Wahab Riaz nick behind after a swashbuckling 30 that featured two slog-swept sixes over the midwicket fence off spinner Nathan Lyon.
Shafiq was majestic in his 140-ball knock, cleaving boundaries to all corners of the ground and rotating the strike to defy Australia's fatiguing attack.
He shared in a doughty seventh wicket stand with Mohammad Amir before Bird broke the 92-run partnership when Amir nicked behind for 48.
Left-arm seamer Starc, who ended a 91-run stand between Azhar Ali and Younus Khan, also halted a 47-run effort between Shafiq and wicketkeeper Sarfraz Ahmed for the sixth wicket.
The rangy Starc fired in a gem of a delivery to bowl Sarfraz through the gate for 24.
Shafiq was dropped on 58 after smashing square to a diving Usman Khawaja and later slashed three runs to point off Bird to complete his stellar hundred.
Bringing a dreadful run of scores, Younus batted himself back into form with a fighting half-century but he fell with an appalling dismissal for 65 when attempting a flamboyant reverse sweep that gave up an easy catch to Smith behind the wicket.
The odds are stacked against Pakistan -- no team has ever successfully chased more than 418 for victory -- but the tourists head into the fifth day with pride restored after their batsmen were routed for 142 in the first innings.