Jennifer Crawford (Embeth Davidtz) has already had a long day of hotel sex with her illicit lover when her husband, an aeronautics engineer named Ted (Anthony Hopkins), corners her into a honey-we-need-to-talk discussion. “I don’t think I can do one of these tonight,” she says wearily, “one of these” apparently being an intense, unwinnable conversation relating to Ted’s emotional neediness. Ted says he loves her; she clearly does not love him back. She loves Rob Nunnally (Billy Burke), her cop boyfriend, and Ted knows about the affair. So Ted shoots her in the head.

Many a film has ended there, but “Fracture,” a tauntingly clever cat-and-mouse thriller, uses it as the jumping-off point. Ted the engineer has engineered an elaborate scheme. He knew it would be his wife’s lover who would come to the house to arrest him after the gardener heard shots fired, and he knew the fact that the arresting officer was sleeping with the victim would screw things up in court. He’s also worked out a very neat trick: Somehow, Ted’s gun, the one he shot Jennifer with, the one that was taken from him when he was arrested — somehow, ballistics reports indicate that gun has never been fired.

What at first appeared to be an open-and-shut case for a cocky, up-and-coming deputy district attorney named Willy Beachum (Ryan Gosling) has now turned into a headache. Jennifer is in a coma (an apparent snag in her husband’s plan; he wanted her dead) and can’t testify to what happened. The attempted-murder weapon was apparently not the weapon after all, but the cops can’t find any other guns in the house, and they know Ted never left after he fired the shots.

It’s a mystery worthy of Agatha Christie — WHERE THE HELL IS THE GUN?! — and my sleuthing cap is off to “Fracture” writers Daniel Pyne and Glenn Gers for eventually answering that maddening question with a resolution that’s simple, elegant, and relatively believable.

In the meantime, the focus is on Willy’s frustration with Ted — he insists you call him Ted — who’s representing himself at trial and behaving far more coyly than you want your attempted-murder suspects to be. Willy was supposed to be ditching the public sector for a cushy corporate-law gig (complete with a hot new boss played by Rosamund Pike), but now personal pride — and a little scorn from his current boss, district attorney Joe Lobruto (David Strathairn) — has forced him to stay put and see this thing through. No one makes a monkey out of the district attorney’s office, not on Willy Beachum’s watch!

Willy is played by recent Oscar-nominee Ryan Gosling with a dash of arrogance and a mild streak of Southern-gentleman charm. You can picture him 30 years from now, dressed in a white seersucker suit, holding onto his lapels while talkin’ all folksy to a jury about how he’s just a simple country boy, and then moving in for the kill. Willy is smart and crafty, and he’s annoyed to have encountered a criminal who’s just as sly.

Anthony Hopkins, meanwhile, channels Hannibal Lecter a little (Ted knows a lot about Willy’s personal life) but mostly casts off the shackles of playing a monster and just has fun. He’s playful, masterful, completely in control of his performance, with a wink or a slight smile all he needs to convey Ted’s sinister nature. Hopkins is most famous for playing someone who gives you the creeps; Ted Crawford, on the other hand, gives you the giggles, like the haunted house at a carnival.

Directed by Gregory Hoblit (2000’s under-appreciated “Frequency”), “Fracture” wisely shows us Ted’s initial actions from a neutral point of view. There is no question that he did, in fact, shoot his wife. Willy is certain of that, and we can share his certainty: We saw it happen. So what happened to the gun? And how will the D.A.’s office stop Ted from getting away with it all?

I have it on good authority — I asked my friend Mike, and he’s a lawyer — that some of the legal maneuvering in the last act is bogus, which is disappointing, considering the movie thinks it’s being very clever. I take issue, too, with the inclusion of the Rosamund Pike character at all, let alone the perfunctory fling she and Willy have. The film would be much leaner without her, and without that spurious Constitution-evading bit in the end. But the central mystery of how Ted managed to do something obvious and then make it look like he didn’t is a tidy piece of prestidigitation, and the movie that surrounds it is smart enough to be worth paying attention to.

B (1 hr., 52 min.; R, a dozen or so F-bombs, brief moderate violence.)