“Weird Science” meets “Stranger Than Fiction” in “Ruby Sparks,” a romantic fable that begins as a genial slapstick comedy but finds its way to darker dramatic territory. Written by Zoe Kazan, who also takes the title role alongside Paul Dano, it’s the rare that’s not just smart and funny but takes risks as well.

The premise is one of those simple magical oddities that are never explained, and none the worse for that. Calvin Weir-Fields (Dano) is a shy, hesitant one-hit-wonder novelist suffering from a terrible case of writer’s block. He breaks through, on the advice of his avuncular shrink (Elliot Gould), by banging out the beginnings of a romance on his old typewriter (which contrasts markedly with his modernist apartment), starring a chipper young woman he names Ruby Sparks—his dream girl, of course. And when he comes down from his bedroom the next morning, he finds the red-haired free spirit (Kazan) happily eating cereal and treating him like her boyfriend. Dazed, Calvin thinks he’s going crazy, but when other people can see Ruby too, he happily settles into life with a woman who meets his every expectation—and whom, as it turns out, he can control when she shows any hint of independence by taking to his typewriter and altering her to conform to his desires.

Calvin’s encouraged in all this by his initially skeptical brother (Chris Messina), a married guy who looks upon his hapless sibling’s good fortune with a mixture of wonderment and envy. Even the boys’ blissfully freewheeling mother (Annette Bening), who’s now living an unconventional life in a homemade rustic estate with a gregarious, hippie-style artist (Antonio Banderas), approves heartily of Ruby when they go to visit the happy pair—though Calvin keeps her origin a secret, of course.

The fly in the ointment occurs when Ruby shows signs of wanting to make her own decisions about her life, which sends Calvin into paroxysms of fear and insecurity. In a scene remarkable for turning what had seemed a light comedy in a pretty dark direction, he reveals that she’s his creation, whom he can control by the simple act of sitting down at his keyboard. The question is whether he loves her enough to set her free, despite the loneliness that will bring.

It’s that turn from frothy to serious that gives “Ruby Sparks” a special tang, and co-directors Jonathan Dayton and Valerie Faris, who managed a delicate balance between comic and serious in “Little Miss Sunshine,” are equally dexterous here. But they’re fortunate to be working with Kazan and Dano. Her ebullient, pixie-like persona fits Ruby perfectly, but she conveys a firmness beneath the surface that’s also part of the character. And no one does diffidence better nowadays than Dano, who also exhibits a strong feel for slapstick, as in the scene when he first encounters Ruby and scampers around his house hoping she’ll simply disappear. His performance is good enough to warrant comparison to that of an earlier gangly fellow who played off against another figment of the imagination, Jimmy Stewart’s Elwood P. Dowd.

The stars are abetted by a strong supporting cast. Messina invests a character who might have descended to Apatow-level frat-boyishness with an amiable combination of raunchiness and sensitivity, while Bening and Banderas have enormous fun in their sequence. Gould effortlessly nails the part of Calvin’s concerned psychiatrist, and Steve Coogan gets a few moments to shine as his smarmy publisher. One also must mention the pooch named Oscar, who plays Calvin’s dog Scotty—a curious canine that matches his owner’s peculiarities with a few of his own. He may not get as much screen time as the dog in “The Artist,” but what he does have is choice.

Technically “Ruby Sparks” is the very model of a first-class indie production, with Judy Becker’s production design a standout, abetted by Alexander Wei’s art direction, Matthew Flood Ferguson’s set decoration and Nancy Steiner’s costumes, all nicely captured in Matthew Libatique’s widescreen lensing. The picture also benefits from Pamela Martin’s crisp editing and a distinctive score that melds classical themes with Nick Urata’s newly-composed cuts.

In an era of mindless Hollywood romantic comedies, the intelligent, carefully crafted “Ruby Sparks” stands out like a charming breath of fresh air.