By Ali Kucukgocmen and Orhan Coskun
ISTANBUL/ANKARA (Reuters) - It was midnight on election night when Turkey's main opposition party finally believed it had overturned a decade and a half of failure at the polls and delivered a stinging defeat to the country's master campaigner, President Tayyip Erdogan.
The candidate of the secularist Republican People's Party (CHP) for mayor of Ankara was comfortably ahead in the capital, but in Istanbul - the biggest prize in Sunday's local elections - Erdogan's ruling AK Party candidate had declared victory.
CHP politicians and supporters, who had endured more than a dozen election and referendum losses at the hands of Erdogan's Islamist-rooted AKP since 2002, were well accustomed to crushing disappointment once the ballots were counted.
This time was different.
"At around 12 a.m. we realized that we were clearly the winners of the election, that we had won," said Suleyman Celebi, an aide to CHP Istanbul candidate Ekrem Imamoglu.
The party's own data, he said, showed they had defeated Erdogan's close ally and former prime minister Binali Yildirim, despite an exhaustive and bitter two-month campaign waged by the president and backed by an overwhelmingly pro-government media.
Once they saw the numbers, Imamoglu and his team refused to back down. "We believed after that that there was no turning back, and that we would win," Celebi told Reuters.
That newfound confidence, supported by initial results the next morning showing Imamoglu had narrowly beaten Yildirim, was forged in an opposition campaign that contrasted sharply with Erdogan's high-profile, confrontational election strategy.
The softly-spoken Imamoglu, spurning the kind of mass rallies which Erdogan addressed daily, sought to win over a broad spectrum of voters with a positive message, meeting many of them face-to-face in a campaign which even his opponents acknowledged struck a chord with the electorate.
Celebi said Turks, tiring of "divisive and polarizing language" from their leaders, warmed to a new style of politics. "This time we saw an open will to vote for Ekrem Imamoglu across all parts of society," he said.
The CHP made a formal alliance with the breakaway nationalist Iyi (Good) Party and also won votes in Istanbul from supporters of the pro-Kurdish HDP party, which chose not to field a mayoral candidate there to boost Imamoglu's chances.
Confirmation of defeat in Istanbul - a city of 15 million people - and Ankara would not reduce Erdogan's sweeping presidential powers, but would seriously dent the aura of invincibility he has built up during 16 years of ever-tighter control over the country.
His AKP, which has challenged results in both Ankara and Istanbul, still won 51.6 percent of the national vote together with its nationalist MHP alliance partner, down from 53.7 percent in parliamentary elections last year.
Three party sources who spoke to Reuters all said there were flaws in a campaign which struck a relentlessly belligerent tone, linking Erdogan's opponents to terrorists and painting the vote bleakly as a "matter of survival" for Turkey.
"Maybe we should have given another message instead of 'survival'," said one source at AKP headquarters in Ankara who described it as ill-suited for voters worried about living standards and local services.
As early as January, AKP officials were sounding the alarm over falling public support amid high inflation and rising unemployment.
Authorities responded to soaring food prices by setting up stalls in Ankara and Istanbul selling subsidized vegetables.
That too may have backfired, the same source said, as TV footage of people queueing for food revived images of "old Turkey" and the chronic inflation of the 1990s. "Long lines being formed disrupted the perception of the AK Party," he said.
Another party source said the AKP was still missing former campaign manager Erol Olcok, a close friend of Erdogan who masterminded AKP election victories before he was killed on an Istanbul bridge during a failed military coup in 2016.
In recent elections, the AKP could not "find its groove", he said.
Praising the CHP's strategy of focusing on "direct contact with the people", a third source said: "If we had paid attention to the messages from the start and then taken necessary steps, the results could have been different today."
For Imamoglu, now preparing to take over Turkey's largest city and govern 15 million people with a projected expenditure of 24 billion lira ($4.3 billion), the campaign is over but the battle not yet won.
The AKP has appealed results of all 39 districts in Istanbul. A party official said on Thursday that once the recount was complete, the AKP's candidate would emerge on top.
Imamoglu says the electoral board "must do its job" and declare him victor but, in keeping with the tone of his campaign, he has stressed consensus over confrontation.
"We really need to help each other," he said a day after the vote, as it became clear the close result could trigger a bitter dispute. "I would like to remind you that we walk side by side on the same path, that we are in the same boat."
($1 = 5.5907 liras)
(Additional reporting by Ece Toksabay in Ankara; Editing by Dominic Evans and Gareth Jones)