THOUSAND OAKS, Calif. -- Another mass shooting in America has left another region stricken with grief, sorrow and questions about what could have possibly motivated someone to gun down a dozen people at a country-western bar.

The massacre that left 12 people dead -- including an 18-year-old who had just started college, a Marine and a sergeant who rushed in to confront the shooter -- also left behind uncertainty regarding how a gunman neighbors knew as troubled and who prompted a mental-health check from police was able to legally obtain the handgun used in the attack. Even as the community reckoned with the violence, it also quickly turned to another threat, as roaring wildfires prompted extensive evacuations in the area and elsewhere in the state.

The shooting in Thousand Oaks, a city of about 130,000 near Los Angeles, followed on a seemingly endless series of rampages in churches, movie theaters, schools, community centers and offices. Police said Ian David Long, a 28-year-old from the area and a former Marine, first shot a security guard outside with a .45-caliber handgun, then marched inside, shooting staff and then patrons of the Borderline Bar & Grill who had gathered for its college night.

When Sgt. Ron Helus of the Ventura County Sheriff’s Office headed inside to confront him, the veteran officer was shot multiple times and mortally wounded. Officers later found Long inside an office in the bar, dead of an apparently self-inflicted gunshot wound.

Ventura County Sheriff Geoff Dean said it didn’t appear that Long was targeting any particular people inside the bar.

“Obviously he had something going on in his head that would cause him to do something like this," Dean told reporters. "So he obviously had some sort of issues.”

Dean, who had previously announced his plans to retire at the end of Friday, said of spending his final days in office responding to the shooting: “It can’t be any worse."

The shooter apparently posted messages on social media during the rampage in the bar, according to investigators.

“While he was inside the bar, and in between volleys of shots, apparently, based on the timestamps, he was posting on Instagram,” said Sgt. Eric Buschow of the Ventura County Sheriff’s Office.

Buschow said he did not have details about what was said in the messages. He said investigators contacted Instagram -- which is owned by Facebook -- and that the company took the page down from public view but saved it for authorities to review. A Facebook spokesperson said Friday that the company had “removed the shooter’s accounts from Facebook and Instagram and will remove any praise or support for the crime or the shooter as soon as we’re aware.”

Buschow said the investigation would look at “every social media” avenue possible to try to determine more about a motive and the shooter’s mindset. Mass shootings are often followed by a painful search for answers about what could have motivated the bloodshed. In some cases, answers remain out of reach; more than a year after a gunman in Las Vegas killed 58 people, his motive remains an unnerving mystery.

“I don’t have any details about motive at this point,” Buschow said in an interview Friday afternoon. “I know that’s one of the big frustration with Vegas -- there’s no answer to the why. Hopefully we will be able to determine whatever the motive was.”

People who escaped the rampage -- among them some who had also survived the massacre of 58 people at a country-music festival in Las Vegas a year earlier -- described the gunman as methodical and silent.

“It was a very stern, straight-faced, focused face,” said David Anderson, 23, of Newbury Park, who lived through both the Las Vegas shooting and the Thousand Oaks attack. “Didn’t say anything.”

People who had encountered Long in the past described him as angry and troubled. Attempts to reach Long’s mother and other relatives have been unsuccessful. Neighbors of the home where Long lived with his mother, Colleen Long, recounted a pattern of unnerving behavior that left some unsurprised to hear he was named as the shooter.

Carol Richardson, who lived nearby, said a friend went to the home multiple times to “calm him down,” and her 19-year-old daughter, Morgan, said: “We always knew he had problems.”

What followed the rampage at the Borderline has become a grimly familiar ritual across the country. Reports of the gunfire give way to accounts of multiple people struck or killed. Then tales of chaos, horror and heroism emerge from those who escaped. And, as occurred after attacks from Parkland, Fla., to Sutherland Springs, Tex., questions began to emerge about the red flags before the bloodshed and what, if anything, law enforcement officials could or should have done.

Todd Stratton, who was friends with Long in high school, said he was in the Borderline listening to country music when he saw his friend walk in with a gun.

"I can’t believe he killed my friends and he was my friend too,” an emotional Stratton said.

Stratton said it was a “real mystery” why Long targeted the club, but described him long having been angry and aggressive.

“He had troubles long before the military,” Stratton said. “He was a hot head. He would drink and get into fights but nothing that weird.”

In at least one instance, Long’s issues came to the attention of law enforcement. Dean said police had encountered Long in April when they were called out for a disturbance.

Long was acting irrationally, so they brought out a crisis-intervention team to meet with him, Dean said. They discussed the possibility that “he might be suffering from PTSD,” Dean said, noting Long’s service in the Marines. (Long deployed to Afghanistan between 2010 and 2011 and left the Marines as a corporal in March 2013.)

That team “met with him, talked to him and cleared him” and did not feel they could take him into custody under a so-called 5150 order, Dean told reporters. Under a 5150 order, a person who is deemed to be “a danger to others, or to himself or herself" can be taken into custody for up to 72 hours -- which would have barred him from owning or possessing firearms.

Police have not released more information regarding the April encounter and how Long was cleared, but neighbors recalled hearing alarming banging noises and yelling from within the house in the middle of the night. That gave way to a standoff with police, as officers aimed their guns and rifles at the home during the situation, one neighbor recalled. Ultimately, Long was cleared, and Dean said it appeared the Glock handgun used in the attack was purchased legally.

That also echoes findings from previous shooting rampages. An FBI study of active shootings between 2000 and 2013 found that most of the attackers, usually men or boys, wielded legally obtained guns. The study also said that shooters had concerned people around them prior to the attacks, and it noted that law enforcement officials were only able to verify that 25 percent of these attackers had diagnosed mental health issues.

In Thousand Oaks and beyond, the shooting prompted vigils and renewed calls for new gun control measures. Schools were closed and a center in the city set aside for mourning families to gather.

Dean, the sheriff, had planned to attend his own retirement party on Saturday, capping off eight years as the county’s sheriff and 41 years in the department. He had just gotten home Wednesday night from someone else’s retirement party when he got a text message about the bar shooting. He put his uniform back on and headed to the Borderline for what wound up being a sleepless night.

On Thursday, he went to tell the families of people killed in the Borderline that their loved ones were among those slain. His last day in the job would be spent responding to the massacre, while his retirement party planned for Saturday has been called off.

“That won’t be happening,” Dean said while standing on the street near the Borderline on Thursday afternoon. “I’ll be here tomorrow, and I don’t know where I’ll be Saturday.”

Berman reported from Washington. Katie Zezima in Thousand Oaks, Calif., and Emily Wax-Thibodeaux, William Wan, Frances Stead Sellers, Alice Crites, Julie Tate and Jennifer Jenkins in Washington contributed to this report.