Brisbane: Australia were two wickets from victory at the end of day four of the first day-night Test against Pakistan on Sunday, 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 Mohammed 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 Younis 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, Younis 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.
Australia 1st innings: 429 (S. Smith 130, P. Handscomb 105, M. Renshaw 71; W. Riaz 4-89, M. Amir 4-97)
Pakistan 1st innings: 142 (S. Ahmed 59no)
Australia 2nd innings 202 for 5 decl (U. Khawaja 74, S. Smith 63)
Pakistan 2nd innings:
S. Aslam c Renshaw b Starc 15
A. Ali c Wade b Starc 71
B. Azam c Smith b Lyon 14
Y. Khan c Smith b Lyon 65
Misbah-ul Haq c Wade b Bird 5
A. Shafiq not out 100
S. Ahmed b Starc 24
M. Amir c Wade b Bird 48
W. Riaz c Smith b Bird 30
Y. Shah not out 4
Extras (lb-4, w-2) 6
Total (for 8 wickets, 123 overs) 382
Fall of wickets: 1-31, 2-54, 3-145, 4-165, 5-173, 6-220, 7-312, 8-378
To bat: R. Ali
Bowling: M. Starc 30-8-97-3 (w-1), J. Hazlewood 35-11-78-0, J. Bird 29-6-94-3 (w-1), N. Lyon 26-3-100-2, N. Maddinson 3-0-9-0
Umpires: I. Gould & R. Illingworth
TV umpire: S. Ravi
Match referee: Ranjan Madugalle