It has been an Olympic fiesta like never before for Britain: their best medal haul in 108 years, second in the medal table, the only host nation to go on to win more medals at the next Olympics.

Never before has a Briton won a diving gold. Never before has a Briton won a gymnastics gold. There have been champions across 15 different sports, a spread no other country can get close to touching.

It enabled Liz Nicholl, chief executive of UK Sport, the body responsible for distributing funds from national government to Olympic sports, to declare on the final day of competition in Rio that Britain was now a "sporting superpower".

Only 20 years ago, GB were languishing 36th in the Atlanta Olympics medal table, their entire team securing only a single gold between them. This is the story of a remarkable transformation.

Money talks

Rio Olympics 2016: Team GB stars draw how they feel

As that nadir was being reached back in 1996, the most pivotal change of all had already taken place.

The advent of the National Lottery in 1994, and the decision of John Major's struggling government to allocate significant streams of its revenue to elite Olympic sport, set in motion a funding spree unprecedented in British sport.

From just £5m per year before Atlanta, UK Sport's spending leapt to £54m by Sydney 2000, where Britain won 28 medals to leap to 10th on the medal table. By the time of London 2012 - third in the medal table, 65 medals - that had climbed to £264m. Between 2013 and 2017, almost £350m in public funds will have been lavished on Olympic and Paralympic sports.

It has reinvigorated some sports and altered others beyond recognition.

Gymnastics, given nothing at all before Atlanta, received £5.9m for Sydney and £14.6m in the current cycle. In Rio, Max Whitlock won two gymnastics golds; his team-mates delivered another silver and three bronzes.

As a talented teenage swimmer, Adam Peaty relied on fundraising events laid on by family and friends to pay for his travel and training costs. That changed in 2012, when he was awarded a grant of £15,000 and his coach placed on an elite coaching programme. In Rio he became the first British male to win a swimming gold in 28 years.

There are ethical and economic debates raised by this maximum sum game. Team GB's 67 medals won here in Brazil cost an average of £4,096,500 each in lottery and exchequer funding over the past four years.

As determined by the Sport Industry Research Centre

At a time of austerity, that is profligate to some. To others, the average cost of this Olympic programme to each Briton - a reported £1.09 per year - represents extraordinary financial and emotional value. Joe Joyce's super-heavyweight silver medal on Sunday was the 700th Olympic and Paralympic medal won by his nation since lottery funding came on tap.

"The funding is worth its weight in gold," says Nicholl.

"It enables us to strategically plan for the next Games even before this one has started and makes sure we don't lose any time. We can maintain the momentum of success for every athlete with medal potential through to the next Games."

All in the detail

The idea of marginal gains has gone from novelty to cliche over the past three Olympic cycles, but three examples from Rio underline how essential to British success it remains.

In the build-up to these Olympics, a PhD student at the English Institute of Sport named Luke Gupta examined the sleep quality of more than 400 elite GB athletes, looking at the duration of their average sleep, issues around deprivation and then individual athletes' perception of their sleep quality.

His findings resulted in an upgrading of the 'sleep environment' in the Team GB boxing training base in Sheffield - 37 single beds replaced by 33 double and four extra-long singles; sheets, duvets and pillows switched to breathable, quick drying fabrics; materials selected to create a hypo-allergenic barrier to allergens in each bedroom.

"On average, the boxers are sleeping for 24 minutes longer each night," says former Olympic bronze medallist and now consultant coach Richie Woodhall.

"When you add it up over the course of a cycle it could be as much as 29 or 30 days' extra sleep. That can be the difference between winning a medal or going out in the first round."

In track cycling, GB physio Phil Burt and team doctor Richard Freeman realised saddle sores were keeping some female riders out of training.

Their response? To bring together a panel of experts - friction specialist, reconstructive surgeons, a consultant in vulval health - to advise on the waxing and shaving of pubic hair. In the six months before Rio not a single rider complained of saddle sores.

Then there is the lateral thinking of Danny Kerry, performance director to the Great Britain women's hockey team that won gold in such spectacular fashion on Friday.

"Everyone puts a lot of time into the physiological effects of hockey, but what we've done in this Olympic cycle is put our players in an extremely fatigued state, and then ask them to think very hard at the same time," Kerry told BBC Sport.

"We call that Thinking Thursday - forcing them to consistently make excellent decisions under that fatigue. We've done that every Thursday for a year."

Britain won that gold on a penalty shootout, standing firm as their Dutch opponents, clear favourites for gold, missed every one of their four attempts.

Virtuous circles

Success has bred British success.

That hockey team featured Helen and Kate Richardson-Walsh, in their fifth Olympic cycle, mentoring 21-year-old Lily Owsley, who scored the first goal in the final. A squad that won bronze in London were ready to go two better in Brazil.

"We've retained eight players who had medals around their necks already," says Kerry. "We added another eight who have no fear.

"It gave us a great combination of those who know what it's all about, and those who have no concept at all of what it's all about, and have just gone out and played in ruthless fashion.

"We get carried away with some of the hard science around sport, but there's so much value in how you use characters and how you bring those qualities and traits to the fore. You see that on the pitch. Leverage on the human beings as much as the science."

Inspired to try a new sport? Find out how to get into just about any sport with our special guides

In the velodrome, experience and expertise is being recycled with each successive Games.

Paul Manning was part of the team pursuit quartet that won bronze in Sydney, silver in Athens and gold in Beijing. As his riding career came towards the end, he was one of the first to graduate through the Elite Coaching Apprenticeship Programme, a two-year scheme that offered an accelerated route into high-performance coaching for athletes already in British Cycling's system.

In Rio he coached the women's pursuit team to their second gold in two Olympics, his young charge Laura Trott also winning omnium gold for the second Games in a row.

Then there is Heiko Salzwedel, head of the men's endurance squad, back for his third spell with British Cycling having worked under the visionary Peter Keen from 2000 to 2002 and then Sir Dave Brailsford between 2008 and 2010.

Expertise developed, expertise retained. A culture where winning is expected, not just hoped for.

"We have got the talent in this country and we know that we can recruit and keep the very best coaches, sports scientists and sports medics," says Nicholl.

"It is now a system that provides the very best support for that talent."

Competitive advantages

Funding has not flowed to all British sports equally, because in some there is a greater chance of success than others.

On Lagoa Rodrigo de Freitas, Britain's rowers dominated the regatta, winning three gold medals and two silvers.

With 43 athletes they also had the biggest team of any nation there. Forty-nine of the nations there qualified teams of fewer than 10 athletes. Thirty-two had a team of just one or two rowers.

Only nine other nations won gold. In comparison, 204 nations were represented in track and field competition at Rio's Estadio Olimpico, and 47 nations won medals.

British efforts in the velodrome, where for the third Olympics on the bounce they ruled the boards, were fuelled by a budget over the four years from London of £30.2m, up even from the £26m they received in funding up to 2012.

In comparison, the US track cycling team - which won team pursuit silver behind Britain's women, and saw Sarah Hammer once again push Trott hard for omnium gold, has only one full-time staff member, director Andy Sparks.

Then there is the decline of other nations who once battled with Britain for the upper reaches of the medal table, and frequently sat far higher.

In 2012, Russia finished fourth with 22 golds. They were third in 2008 and third again in 2004.

This summer, despite escaping a total ban on their athletes in the wake of the World Anti-Doping Agency's McLaren Report, they finished with 19 golds for fourth, permitted to enter only one track and field athlete, Darya Klishina.

Australia, Britain's traditional great rivals? Eighth in 2012, sixth in Beijing, fourth in Athens, 10th here in Brazil.

As Team GB have risen, others have fallen back.

Talent on tap

In Rio, 129 different British athletes have won an Olympic medal.

It is a remarkable depth and breadth of talent - a Games where 58-year-old Nick Skelton won a gold and 16-year-old gymnast Amy Tinkler grabbed a bronze, a fortnight where Jason Kenny won his sixth gold at the age of 28 and Mo Farah won his ninth successive global track title.

The abilities of those men and women has been backed up by similar aptitude in coaching and support.

In swimming there is Rebecca Adlington's former mentor, Bill Furniss, who has taken a programme that won just one silver and two bronzes in London and, with a no-compromise strategy, taken them to their best haul at an Olympics since 1908.

In cycling, there has been the key hire of New Zealand sprint specialist Justin Grace, the coach behind Francois Pervis' domination at the World Championships, a critical influence on Kenny, Callum Skinner, Becky James and Katy Marchant.

"We have got the talent in this country, and we know we can recruit and keep the very best coaches, sports scientists and sports medics," says Nicholl.

"It is a system that provides the very best support for that talent. We do a lot in terms of people development. We are conscious when people are recruited to key positions as coaches they are not necessarily the finished article in their broader skills.

"We provide support so that coaches across sports can network and learn from each other. That improves their knowledge expertise and the support systems they've got."

It is an intimidating thought for Britain's competitors. After two decades of consistent improvement, Rio may not even represent the peak.

Subscribe to the BBC Sport newsletter to get our pick of news, features and video sent to your inbox.