Afghanistan require a further 118 runs with nine wickets remaining to win their one-off Test with Ireland, after star leg-spinner Rashid Khan took five-for on day three in Dehra Dun.
Rashid (5-82) became the first Afghanistan player to claim a five-wicket haul in Test cricket as Ireland were bowled out for 288 in their second innings.
Trailing by 142 runs after the two teams had batted once, a century stand for the third wicket between Andy Balbirnie (82) and James McCollom (39) had helped reduce the arrears, only for both to be dismissed within an over of each other.
Rashid then returned in the last session to claim three wickets in three overs that reduced Ireland to 230-9 - a lead of only 88 runs - before their total was bolstered somewhat by last-wicket pair James Cameron-Dow (32no) and Tim Murtagh (27) putting on 58.
Set a target of 147 to win, Afghanistan closed the day 29-1, with Andy McBrine (1-8) striking early to remove Mohammad Shahzad (2), but Ihsanullah (16no) and Rahmat Shah (11no) surviving through to stumps.
Starting the day 22-1 in their second dig, Ireland dominated the opening session thanks to Balbirnie and McCollom's century stand, and with Rashid - nursing an injury to his spinning finger going into the Test match - struggling, in particular.
But Waqar Salamkheil (2-66) broke partnership, with Balbirnie smartly caught behind, and then McCollum was trapped lbw in the following over by Rashid - his first wicket.
Stuart Poynter (1) and Stuart Thompson (1) - Rashid's second victim - came and went cheaply, before Kevin O'Brien (58) and George Dockrell (25) put on another half-century stand that had Ireland ambitious once more of setting a testing target.
However, Rashid then ran through the lower order in the last session, claiming three wickets in three overs, and though Cameron-Dow and Murtagh - for the second time in the match, following his first-innings fifty - lifted the score with an enterprising final-wicket stand, Afghanistan look on course for a maiden Test triumph heading into day four.
Both teams are competing in only their second Test match since gaining Test status in 2017. Last year, Afghanistan lost their debut Test to India, and Ireland were beaten by Pakistan in their inaugural Test.