By Foo Yun Chee and Padraic Halpin

BRUSSELS/DUBLIN (Reuters) - The European Commission ordered Apple Inc to pay Ireland unpaid taxes of up to 13 billion euros ($14.5 billion) on Tuesday as it ruled the firm had received illegal state aid.

Apple and Dublin said the U.S. company's tax treatment was in line with Irish and European Union law and they would appeal the ruling, which is part of a drive against what the EU says are sweetheart tax deals that usually smaller states in the bloc offer multinational companies to lure jobs and investment.

The U.S. feels its firms are being targeted by the EU and a U.S. Treasury spokesperson warned the move threatens to undermine U.S. investment in Europe and "the important spirit of economic partnership between the U.S. and the EU."

Starbucks Corp has been ordered to pay up to 30 million euros ($33 million) to the Dutch state, while Inc and McDonald's Corp are also under investigation by the Commission, the EU's executive arm.

Apple's stock fell less than 1 percent.

EU Competition Commissioner Margrethe Vestager questioned how anyone might think an arrangement that allowed Apple to pay a tax rate of 0.005 percent, as Apple's main Irish unit did in 2014, was fair.

"Tax rulings granted by Ireland have artificially reduced Apple's tax burden for over two decades, in breach of the EU state aid rules. Apple now has to repay the benefits," Vestager told a news conference.

Analysts said the size of the claim underlined the Commission's aggressive stance, but since each case involves different circumstances and tax rules, lawyers said it was hard to see if further big claims were any more or less likely.

Apple, which had more than $200 billion in cash and readily marketable securities at the end of June, is likely to see the case drag out for years in EU and possibly Irish courts.

Apple warned investors in a July regulatory filing that the Commission's investigation could lead to "material" liability for further tax payments, but that it could not estimate the impact. On Tuesday the company said it expects to place "some amount of cash" in an escrow account.

Tax experts say the European Commission faces a tough battle to convince courts to back up its stand. While the EU has found that certain tax regulations are anti-competitive, it has never before ruled whether countries have applied tax regulations fairly in the way it has with Apple, Starbucks and others.

As a result, some lawyers and accountants said they doubted Apple would end up paying back any tax.

“I am not persuaded by the reasoning the EU has applied,” said Tim Wach, global managing director at international tax advisers Taxand.

"ARTIFICIAL" ARRANGEMENT The EU's ruling challenges the way that Ireland agreed to tax the profits of Irish-registered Apple subsidiaries, through which most of its non-U.S. profits flowed.

Apple Inc licenses the rights to technology designed in the United States to Irish subsidiaries. These then hire contract manufacturers to make devices which they sell to Apple retail subsidiaries around Europe and Asia.

Since the manufacturing cost is a small portion of device sales prices and retail subsidiaries are allocated a small operating margin, Apple Ireland is very profitable. In 2011, it earned $22 billion after paying $2 billion to its U.S. parent in relation to the rights to Apple intellectual property.

However, the Irish tax authority agreed only 50 million euros of this was taxable in Ireland, the European Commission said. Under the terms of Apple’s tax deal, first agreed in 1991 and renewed in 2007, Apple could allocate most of the profits earned by its Irish operating units to a "head office" that did not have any employees or own any premises.

“This 'head office' had no operating capacity to handle and manage the distribution business, or any other substantive business for that matter,” the Commission said.

The Commission said this agreement had no basis in tax law and was not available to others, and so represented state aid.

Irish Finance Minister Michael Noonan said he profoundly disagreed with the decision and in order to preserve Ireland’s attractiveness for investment he would appeal.

"There is no economic basis for this decision. It's bizarre and it's an exercise in politics by the Competition Commission," Noonan said.

"They don't have responsibility for taxes and they are opening a back door through state aid to influence tax policy in European countries when the European treaties say tax policy is a matter for sovereign governments," he added.

Ireland's low corporate tax rate has been a cornerstone of the country’s economic policy for decades, drawing investors from multinational companies whose staff account for almost one in 10 of the country's workers.

For many technology firms like Google and Facebook, a key attraction is that Ireland allows companies to adopt tax structures which see them pay much less than the 12.5 percent headline rate. The companies say they follow all tax rules.

Apple said it was confident of winning an appeal.

"The European Commission has launched an effort to rewrite Apple’s history in Europe, ignore Ireland’s tax laws and upend the international tax system in the process," CEO Tim Cook said in a letter to customers posted on Apple's website.

"A company’s profits should be taxed in the country where the value is created," he added.

U.S. OPPOSITION The U.S. Treasury Department published a white paper last week in which it said it was looking at possible responses to what it sees as unfair targeting of its firms, which could include extra taxes on the U.S. arms of European companies.

The paper noted that the EU executive's tax rulings could cost the U.S. exchequer money.

Under U.S. tax law, Apple's Irish profits are taxable if brought back to the United States – something the company would have to do if it wanted to use the money to pay dividends.

But any tax paid in Europe is reduced from the tax due in the United States. The Treasury has said the Commission’s approach was at odds with EU tax law and international treaties.

The Commission, which has also ruled European companies including carmaker Fiat and Swedish engineer Atlas Copco AB must pay tax claims worth over $350 million, said its application of competition law to tax rulings followed EU law and treated all companies equally.

Nonetheless, the Netherlands, Belgium and Luxembourg have already appealed EU rulings against their tax deals with multinationals.

Apple employs 5,500 people in areas such as logistics and distribution in the Irish city of Cork, which has about a quarter of Apple's Europe-based staff.

The Commission has previously said Apple's tax treatment had been "motivated by employment considerations."

Apple has said it paid Ireland's 12.5 percent rate on all the income that it generates in the country.

Apple Chief Financial Officer Luca Maestri decried the effective tax rate cited by Vestager as "a completely made-up number.

(Additional reporting by Conor Humphries in Dublin, Robin Emmott, Philip Blenkinsop, Robert-Jan Bartunek and Alastair Macdonald in Brussels, Tom Bergin in London, Eric Auchard in Frankfurt and Julia Love in San Francisco; Writing by Tom Bergin/Alastair Macdonald; Editing by Philip Blenkinsop, Alexander Smith and Bill Rigby)