Katy Perry just took an L in court … a jury ruled her song “Dark Horse” stole heavily from a gospel song that came out 5 years earlier.

Monday’s verdict is the latest step in a lengthy legal battle that started back in July 2014, when Katy was sued by the Christian rapper, Flame — he claimed her 2013 hit song “Dark Horse” is a complete rip-off of his Grammy-nominated gospel song, “Joyful Noise.”

Flame’s music label uploaded the music video to YouTube way back in 2008, and it’s since racked up millions of views. He says Katy jacked the underlying beat … but she called BS, claiming there are NO meaningful similarities.

According to the suit … Flame claims Perry also destroyed his reputation in Christian music circles because he’s now associated with the anti-Christian witchcraft and imagery that’s present in her “Dark Horse” music vid.

The case will now proceed to the damages phase — AKA, when Katy finds out how much the loss will cost her.

As we reported … Katy showed up to court last week wearing a mint-colored suit and took the stand, claiming she never heard the song “Joyful Noise” before the filing of the lawsuit.

Katy told the jury she initially heard the beats from Dr. Luke while they were drinking wine in Santa Barbara, and claimed she wrote “Dark Horse” in only 4 hours. She remained calm throughout her testimony.

-TMZ