The past 10 years have been cruel to Lego, the famous Danish toy company. There was once a time when its iconic plastic-studded bricks were a part of every childhood. But in the age of Game Boy, Xbox, web chat, texting and fast-burning fads such as Pokemon, Lego has struggled to cut it with generations of increasingly sophisticated young consumers.

In 1998, for the first time, the company made a loss. The following year, 1,000 people were laid off (another first). In January, Lego posted another set of dismal figures - this time a loss of 1.4bn Danish kroner (about £140m). A few weeks ago, announcing a plan to stabilise its financial situation, another 500 jobs were cut. And all this during good times in the market for toys, which is growing at around 4% a year, not including video games and electronics.

This downturn in Lego's fortunes was taking place just as the scale of its achievements was being recognised. In 2000, when it came to choosing Toy of the Century, the British Association of Toy Retailers, Fortune and Forbes magazines all agreed: the Lego brick was it.

It is not that Lego hasn't tried to move with the times. Toy store shelves feature dozens of new Lego products that bear little resemblance to the sets of bricks that were a childhood staple for 50 years. Today, there are familiar licences in kit form: Harry Potter, Bob the Builder and Winnie the Pooh. There have been successful home-grown innovations, too - Clikits for girls and programmable bricks called Mindstorms (as popular with adults as kids).

The biggest of the recent hits, and number one Lego product in 2003, is an action figure range called Bionicles, which first appeared in virtual form on the web in December 2000. Today, the epic struggle between Toa heroes and Makuta villains for control of Mata Nui, their tropical island world, features in comics and books, on the web, in CD-Roms and even movies, as well as in millions of snap-together kits sold in the shops. Bionicle may not have sold as fast as Beyblades or Yu-Gi-Oh, but they seem certain to outlast them. Three years old and still growing, Bionicle accounted for roughly a quarter of Lego's turnover last year. But the product's real value may prove to be in showing a company steeped in its own traditions the way out of troubled waters.

When the idea for Bionicle was proposed back in 2000, there was resistance from company traditionalists. "The correct term for what we encountered is scepticism," says Lars Kaae, who has been marketing manager on Bionicle since day one. The company had no experience of creating the kind of story-based, multichannel brand that was being proposed. The Bionicle characters' war-like appearance also ran up against the company's values. Every product decision is checked against the vaunted "Lego values": high-quality products, an emphasis on free play and encouraging the imagination, and no modern warfare or violence.

Internal debate about new products can be intense. The Star Wars range launched in 1999 upset some company traditionalists because the kits came with a story built in. Two years later, Bionicle went far beyond this - it made the story the most important thing that Lego was selling.

"We started telling the story on the website before the product was launched," recalled Leah Weston, senior producer for Lego Virtual, the company's web division.

"Then as the product rolled out, we developed it on the website through character bios and an episodic adventure game." New story elements were also included with the toy figures' assembly instructions. Later, CD-Roms featuring games and movie clips were given away with the toys. Comics and books followed, and last autumn, Bionicle: The Mask of Light was released, quickly climbing to near the top of Billboard's Top Kid Video chart.

"The main storyline is developed by a team of eight spread around the world who meet three times a year," says Kaae. "And between them, they decide which media gets which bit of the story." With the story being developed across several media, the role of the website is to pull the story strands together. The strategy of making every Bionicle product from toy to toothbrush promote the website has paid off: traffic has averaged at more than a million unique page views per month since last August. The website is promoted in other ways: toy packaging features special access codes and each purchase earns Kanoka points - the Bionicle equivalent of a loyalty scheme - that can be used to play web games.

Lego is in the world of Bionicle for the long-term, so things such as loyalty count. A second movie in what could be a long series is due in autumn. "The Bionicle story is planned out for the next 20 years, and we have an outline that maps out the overall direction the product is heading in," said Weston.

The success of Lego's first home-grown, story-based product bodes well for the company's future. It says Lego knows how the world of play has changed in recent years.

"We haven't been good enough at keeping up with how children's ways of expressing their creativity have changed over time," says Anne Flemmert, senior researcher at the Lego Learning Institute. The institute, which funds research into play and learning, and Vision Lab, an internal thinktank, were recently set up to help Lego evolve in a fast-changing world. Just like our society, children's play is much more complex than it has ever been, and toys are increasingly knowledge and rule-based - like Pokemon, Beyblade and Yu-Gi-Oh." And now Bionicle. "Bionicle is one of the first examples of this company starting to loosen up a bit," says Flemmert.

The loosening-up applies not just to products, but also to ways of working. "When we started Bionicle, designers, engineers and marketing people sat down as a team and worked on the concept and developed it sequentially together," says Kaae. "This project-orientated way of working was new for us - it was successful and now all new projects are set up in this way."

The latest cuts and upheavals in the company have reached Kaae's team but the mood is optimistic. "We are confident now that we are moving in the right direction."