Dressed all in black with his hood pulled up, the gunman apparently took his own life as scores of police converged on the Borderline Bar and Grill in Southern California.
The motive for the rampage late Wednesday night was under investigation.
The killer , Ian David Long, 28, was a former machine gunner and Afghanistan war veteran who was interviewed by police at his home last spring after an episode of agitated behavior that authorities were told might be post-traumatic stress disorder.
Opening fire with a handgun with an illegal, extra-capacity magazine, Long shot a security guard outside the bar and then went in and took aim at employees and customers, authorities said. He also used a smoke bomb, according to a law enforcement official who was not authorized to discuss the investigation publicly and spoke on condition of anonymity.
The dead included a man who had survived last year's massacre in Las Vegas, a veteran sheriff's deputy who rushed in to confront the gunman, a 22-year-old man who planned to join the Army, a freshman at nearby Pepperdine University and a recent Cal Lutheran graduate.
"It's a horrific scene in there," Ventura County Sheriff Geoff Dean said in the parking lot. "There's blood everywhere."
Survivors of the rampage - mostly young people who had gone out for college night at the Borderline, a hangout popular with students from nearby California Lutheran University and other schools - seemed to know what to do, having come of age in an era of active-shooter drills and deadly rampages happening with terrifying frequency.
For some it was not a new experience. Survivors and their relatives said several people who were at the bar Thursday had been at the outdoor country music festival in Las Vegas last year where a gunman in a high-rise hotel killed 58 people.
"I don't want prayers. I don't want thoughts," said Susan Schmidt-Orfanos, whose son Telemachus Orfanos survived the Vegas shooting only to die less than 10 minutes from his home. "I want those bastards in Congress - they need to pass gun control so no one else has a child that doesn't come home."
Many of the estimated 150 patrons at the Borderline dived under tables, ran for exits, broke through windows or hid in the attic and bathrooms, authorities and witnesses said.
"Unfortunately our young people, people at nightclubs, have learned that this may happen, and they think about that," the sheriff said. "Fortunately it helped save a lot of lives that they fled the scene so rapidly."
Matt Wennerstrom said he instinctively pulled people behind a pool table, and he and friends shielded women with their bodies after hearing the shots. When the gunman paused to reload, Wennerstrom said, he and others shattered windows with barstools and helped about 30 people escape. He heard another volley of shots once he was safely outside.
"All I wanted to do was get as many people out of there as possible," he told KABC-TV. "I know where I'm going if I die, so I was not worried."
A video posted on Instagram after the shooting by one of the patrons shows an empty dance floor with the sound of windows shattering in the background. As a silhouetted figure comes through a doorway, the camera turns erratically and 10 gunshots ring out.
"I looked him in his eyes while he killed my friends," Dallas Knapp wrote on his post. "I hope he rots in hell for eternity."
The tragedy left a community that is annually listed as one of the safest cities in America reeling. Shootings of any kind are extremely rare in Thousand Oaks, a city of about 130,000 people about 40 miles (64 kilometers) from Los Angeles, just across the county line.
Mourners gathered for a vigil on Wednesday evening as smoke from a fast-moving, nearby wildfire billowed over them.
Earlier, people stood in line for hours to give blood. All morning, people looking for missing friends and relatives arrived at a community center where authorities and counselors were informing the next-of-kin of those who died. Many people walked past TV cameras with blank stares or tears in their eyes. In the parking lot, some comforted each other with hugs or a pat on the back.
Jason Coffman received the news that his son Cody, 22, who was about to join the Army, was dead. Coffman broke down as he told reporters how his last words to his son as he went out that night were not to drink and drive and that he loved him.
"Oh, Cody, I love you, son," Coffman sobbed.
It was the nation's deadliest such attack since 17 students and teachers were killed at a Parkland, Florida, high school nine months ago. It also came less than two weeks after a gunman massacred 11 people at a synagogue in Pittsburgh.
Democratic Gov.-elect Gavin Newsom, in his first public appearance since winning office on Tuesday, lamented the violence that has returned to California.
"It's a gun culture," he said. "You can't go to a bar or nightclub? You can't go to church or synagogue? It's insane is the only way to describe it. The normalization, that's the only way I can describe it. It's become normalized."
President Donald Trump praised police for their "great bravery" in the attack and ordered flags flown at half-staff in honor of the victims.
Authorities searched Long's home in Newbury Park, about 5 miles from the Borderline bar, for clues to what set him off.
"There's no indication that he targeted the employees. We haven't found any correlation," the sheriff said. "Maybe there was a motive for this particular night, but we have no information leading to that at all."
Long was in the Marines from 2008 to 2013, rose to the rank of corporal and served in Afghanistan in 2010-11 before he was honorably discharged, the military said. Court records show he married in 2009 and was divorced in 2013.
Authorities said he had no criminal record, but in April officers were called to his home, where deputies found him angry and acting irrationally. The sheriff said officers were told he might have PTSD because of his military service. A mental health specialist met with him and didn't feel he needed to be hospitalized.
Tom Hanson, 70, who lives next door to Long and his mother, said he called the police about six months ago when he heard "heavy-duty banging" and shouting coming from the Longs' home.
"Somebody has missed something here," his wife, Julie Hanson, said. "This woman has to know that this child needed help."
Long was armed with a Glock 21, a .45-caliber pistol designed to hold 10 rounds plus one in the chamber, according to the sheriff. But it had an extended magazine - one capable of holding more ammunition - that is illegal in California, Dean said.
Sheriff's Sgt. Ron Helus and a passing highway patrolman arrived at the club around 11:20 p.m. in response to several 911 calls, heard gunfire and went inside, the sheriff said. Helus was shot immediately, Dean said.
The highway patrolman pulled Helus out, then waited as a SWAT team and other officers arrived. Helus died at a hospital.
By the time officers entered the bar again - about 15 to 20 minutes later, according to the sheriff's office - the gunfire had stopped. They found 12 people dead inside, including the gunman, who was discovered in an office, the sheriff said.
"There's no doubt that they saved lives by going in there and engaging with the suspect," said Dean, who was set to retire Friday. He praised the slain officer - a close friend - as a hero: "He went in there to save people and paid the ultimate price."
One other person was wounded by gunfire, and as many as 15 others suffered minor injuries from jumping out windows or diving under tables, authorities said.
Five off-duty police officers who were at the bar also helped people escape, authorities said.
For several hours after the violence, survivors gathered in the dark, some sobbing and hugging as they awaited word on the fate of friends as ambulances idled nearby. Several men were bare-chested after using their shirts to plug wounds and tie tourniquets.
Around midday, the body of the slain sheriff's officer was taken by motorcade from the hospital to the coroner's office. Thousands of people stood along the route or pulled over in their vehicles to watch the hearse pass.
Helus was a 29-year veteran of the force with a wife and son and planned to retire in the coming year, said the sheriff, choking back tears.
AP journalists Andrew Dalton, Amanda Lee Myers, John Antczak and Brian Melley in Los Angeles, Michelle A. Monroe in Phoenix and Michael Balsamo in Washington contributed to this report.
Copyright 2019 The Associated Press. All rights reserved. This material may not be published, broadcast, rewritten or redistributed.