BUENOS AIRES, Argentina -- Argentina's Senate on Thursday rejected a bill to legalize elective abortion in the first 14 weeks of pregnancy, an issue that has divided the homeland of Pope Francis. Lawmakers debated for more than 15 hours, from Wednesday into Thursday, then voted 31 in favor to 38 against.

The decision could echo across Latin America, where the Roman Catholic Church has lost influence and moral authority due to secularization, an out-of-touch clerical caste and an avalanche of sex abuse scandals.

For long hours, thousands of supporters wearing green handkerchiefs that represent the effort to legalize abortion and opponents of the measure wearing light blue, braved the heavy rain and cold temperatures in Argentina's winter to watch the debate on large screens set up outside Congress.

Get Breaking News Delivered to Your Inbox

The demonstrations were largely peaceful, but after the vote, small groups of protesters clashed with police, throwing firebombs and setting up flaming barricades. Police officers responded with tear gas.

The lower house had already passed the measure and conservative President Mauricio Macri had said that he would sign it, even though he is anti-abortion.

In Argentina, abortion is only allowed in cases of rape and risks to a woman's health. Thousands of women, most of them poor, are hospitalized each year for complications linked due to unsafe abortions - the main cause of maternal death.

Backers of the measure said legalizing abortion would save the life of many women who now turn to dangerous illegal abortions. The Health Ministry estimated in 2016 that the country sees as many as half a million clandestine abortions each year, with dozens of women dying as a result.

The Catholic Church and other groups opposed it, saying it violated Argentine law, which guarantees life from the moment of conception.

"There are positive points that have come out of this, first of all, that even when there are differing ways of thinking, there's a square in peace right now, with thousands of people defending their convictions," said Buenos Aires provincial Gov. Maria Eugenia Vidal, who was against the measure.

"There was talk of a green square and a light blue square. But the truth is that these are people who are right there next to each other, coexisting and defending what they believe to be the truth."

But the contentious issue has divided Argentines, pitting conservative doctors and the Catholic Church against feminist groups and physicians. While thousands of people waited for the decision on the streets guarded by umbrellas, others gathered Wednesday night at a "Mass for Life" at the capital city's Metropolitan Cathedral, the church of Pope Francis during his tenure as the archbishop of Buenos Aires.

"It's not about religious beliefs but about a humanitarian reason," Cardinal Mario Poli, the archbishop of Buenos Aires, told churchgoers. "Caring for life is the first human right and the duty of the State."

Pope Francis this year had denounced abortion as the "white glove" equivalent of the Nazi-era eugenics program and urged families "to accept the children that God gives them."

Activists estimate that 3,000 women in Argentina have died of illegal abortions since 1983.

"Let's recognize that we're facing a public health tragedy because 3,030 women who have died is a tragedy," said Magdalena Odarda, a senator for Rio Negro province.

"We're not deciding abortion yes or now. We're deciding abortion in a hospital, or illegal abortion, with a clothes hanger, or anything else that puts a woman in a humiliating, degrading situation - a real torture," she said.

Many women in Argentina use misoprostol to end first-trimester pregnancies. The drug is only sold under prescription, but for the poorest women the cost of the drug is out of reach.

For many of them, the methods used to induce an abortion include using an IV tube with a sharp wire clothes hanger or a knitting needle to try to break the amniotic sac inside womb. Others drink herbs, insert dubious non-abortive pills in the vagina, or pump toxic mixtures, which can cause ulcers, hemorrhage and ultimately severe infections, and death.

For months, hundreds of doctors in Argentina had staged anti-abortion protests, in one case laying their white medical coats on the ground outside the presidential palace. Feminists and other groups led even larger demonstrations in support of the measure, often wearing green that symbolizes the pro-abortion movement, or red cloaks and white bonnets like the characters from the novel-turned-TV series "The Handmaid's Tale."

International human rights and women's groups have been closely following the vote, and figures such as U.S. actress Susan Sarandon and "The Handmaid's Tale" author Margaret Atwood supported the pro-abortion cause in Argentina.

Jose Miguel Vivanco, director for the Americas at Human Rights Watch, said that Argentina had a "historic opportunity" to protect the rights of women. Amnesty International told Argentine legislators that "the world is watching."

Catholic and evangelical groups protested abortion with the slogan, "Argentina, filicide (child murder) will be your ruin."

Women's movements across South America have been pushing against decades-old abortion prohibitions.

In neighboring Brazil, supporters and opponents of abortion recently testified before the Supreme Federal Tribunal in an extraordinary session on whether to allow elective abortions during the first 12 weeks of pregnancy.

In Brazil, which is home to the world's largest population of Catholics and fast-growing evangelical faiths, abortion carries a punishment of up to three years in prison. There are three exceptions: if a woman is raped, pregnancy puts her life in danger, or a fetus is brain-dead.

In neighboring Chile, the Constitutional Court last year upheld a measure that would end that country's absolute ban on abortions, permitting abortions when a woman's life is in danger, when a fetus is not viable and in cases of rape. Chile had been the last country in South America to ban abortion in all cases, though several nations in Central America still have absolute prohibitions.

Small groups rallied in other countries across the region to voice support for the Argentine abortion measure, including in Mexico, Brazil, Uruguay, Ecuador, Paraguay and Peru.

Cuba, Guyana, Puerto Rico and Uruguay permit early-term elective abortions, as does Mexico City.

"It's a shame that we were not able to reach a consensus because the problem will continue to exist exactly as it did before we began this debate," said former President Cristina Fernandez, who is an opposition senator.

"We need to make an effort to resolve this," she said.