It may be a fanciful tale set inside the mind of an 11-year-old girl, but Inside Out is based on hard science.
Its writer-director Pete Docter, producer Jonas Rivera and the rest of the creative team behind the Pixar film spoke to scientists and psychologists to come up with as accurate a portrait of the human mind as possible.
But the starting point of the story - about the inner turmoil experienced by a girl named Riley as she moves to a new city and leaves behind her old life, friends and childhood - was Docter's own daughter.
"I noticed my daughter growing up and being a little less goofy and wacky and funny and a little more shy and quiet because she had turned 11.
"And at the same time, I was looking at different ideas for a film and thought about emotions as characters."
As he tried to think of what the core emotions would be for such a tale, he realised how much he and his co-writers did not know about the subject.
"In the very beginning, I think I had pitched optimism, which we learnt later is not really an emotion. I had fear and anger and some other ones, and we realised, man, we don't really know anything about this. So we did a lot of research," Docter says.
"There is no consensus among scientists about how many emotions there actually are. Some say three, some say 27; most are somewhere in the middle. So we realised, well, we get to kind of make this up.
One of the resources the team drew on was the work of Dr Paul Ekman, the famed psychologist and lying expert who was the inspiration for the television series Lie To Me (2009-2011), starring Tim Roth.
Dr Ekman believes there are seven core emotions - joy, sadness, fear, disgust, anger, surprise and contempt - but for the purposes of simplicity and good story-telling, the film-makers settled on the first five.
"It felt like a good crowd, with enough contrast and conflict between them, but not so big that you're like, 'Wait, who's that again?'" says Docter, who reveals that this meant ditching some potentially interesting emotions such as pride and schadenfreude, a German word for pleasure derived from another's misfortune.
The five core emotions are shown vying for influence over Riley's mind at a futuristic console in what looks like the control room of a ship.
"From the very beginning, we thought of the emotions as being some sort of controls because I feel like that's probably how they would work to affect us emotionally,'' Docter says.
Translating this into a workable story involving characters inside a child's mind was another matter, though, and initially it was a tough sell even with fans of the idea, including Pixar studio chief John Lasseter.
Docter remembers Lasseter saying: "This is such a cool idea. It's got a lot of complexity and a lot of potential. It's also gonna be really hard."
Adds the director, who won the 2010 Best Animated Feature Oscar for his movie Up: "And I didn't really see that at the time. I was too taken by the fun of it."
Another concept the film-makers wanted to bring to life was the notion of abstract thought and how there might be a separate area of the mind devoted to that. In the film, it is depicted as a kind of no man's land filled with deconstructed shapes.
Docter says: "It was one of those things that, as soon as we came up with that concept, I was, like, we have to do abstract thought. And it was one of the early sequences that, even as the rest of the film didn't work, even John (Lasseter) was, like, 'That was gold.'"
It is no accident that the little girl's old imaginary friend Bing Bong - whom Joy and Sadness encounter as they go on a quest to retrieve some of her lost memories - is unable to understand these abstract thoughts.
"As we did the research, we found out that abstraction is something that is a little more advanced thinking. It's usually at age six or seven, when kids are slowly able to conceive concepts that are not physical objects in front of them," Docter says.
"So we figured that's a more recent development in Riley's brain. Bing Bong doesn't really know what it does because he's like a three-year-old."
While this and other story elements were based on experts' theories about how the mind works, the film-makers did not lose sight of the fact that they were trying to create a fun story.
"We didn't want it to be too science-fiction," says Rivera. "And you can imagine, in this movie, without any visuals and just reading it, it did sort of lean that way. So we worked really hard to make it whimsical and fun, like the mind of a little girl."