(CNN) — The view from the castle is so magnificent that it's easy to understand why warring empires didn't want to concede this gentle green valley, tucked between the snow-capped peaks of the Catalan Pyrenees.
Although it's hard to tell from this vantage point just why this piece of land was once the last missing piece in a geopolitical puzzle of continental proportions.
Welcome to Llívia, a little patch of Spain stranded in France and one of Europe's oldest enclaves.
A continent in conflict
At first glance, the present-day border between France and Spain seems to be defined by obvious natural features, with the imposing Pyrenees mountain range -- including several peaks that top 10,000 feet -- forming a clear divide between the neighboring countries.
Yet, the history of this border owes as much to war and diplomacy as it does to geology.
Close to 400 years ago, Central Europe was caught up in a decades-long conflict that would come to be known as the Thirty Years' War. As part of this, the Franco-Spanish War ran on from 1635 to 1659.
When the Principality of Catalonia, which for centuries had straddled both sides of the Pyrenees, came under the rule of Spanish monarchs, so did its lands to the north.
This was a challenge to France's own long-term goal of securing what it perceived as its natural borders. Discussions were not going to be easy.
A royal wedding
Finally, France and Spain were close to reaching an agreement, with even a royal wedding arranged to seal the deal: Louis XIV of France was to marry a Spanish princess.
There was just one issue left on the table: the demarcation of the common border.
Negotiations dragged on for months on end and, yet, the resulting Treaty of the Pyrenees -- signed November 1659 -- outlined a general agreement, but still left some open points when it came to the border. It would take a long time and some creative arrangements to settle it completely.
Take first of all Pheasant Island, the patch of land in the middle of the Bidasoa River upon which the treaty was signed. It was for centuries a sort of "no man's land" where international treaties were signed and royal grooms and brides were exchanged between the two kingdoms.
This remained the status of this uninhabited island on the westernmost section of the border, just 6,820 square meters in size, until 1866, when it became what is known in international law as a "condominium." It switches sovereignty between France and Spain every six months, with Spain owning it from February to July and France taking over from August to January.
France built the Mont-Louis citadel in the 17th century to protect the new border.
How green was my valley
A more contentious case was that of the Cerdanya valley, several hundred miles east. The lack of obvious natural barriers in this section of the border created a dilemma for Spanish and French negotiators.
"Contrary to what is often believed, the border in the Cerdanya area was not settled at the Treaty of the Pyrenees, but a bit later, because France and Spain could not agree on where the border should be," explains historian and author Michel Bougain, an expert on this period. "Both sides wanted to keep the whole valley for themselves."
The water table divide is actually north of the present-day border, but this wasn't an option the French favored. They wanted a line much farther south, which would have left most of the Pyrenees inside France. And this was a claim that France was ready to back with the considerable military forces it had on the ground at the time.
Negotiations dragged on to the point that the Spanish delegate, Luis Méndez de Haro, threatened to call off the imminent wedding between Louis XIV and the Infanta Maria Theresa, the eldest daughter of the Spanish king, if an agreement could not be reached on the issue of the Cerdanya border.
Following the replacement of hawkish De Haro, the two delegations -- the locals were, of course, not consulted -- could come up with a formula for the partition.
That was far from the end of the story, though.
What's in a word?
Diplomats took a King Solomon-style approach to carving up the territory.
The problem was that the wording of the final agreement referred to "33 villages" in the disputed area that would go to France.
Right in the middle of this France-assigned portion was the ancient settlement of Llívia which, the Spanish side argued, was not a village but a town, since Catalan law accorded it this status, one step up the urban pecking order.
It was a small detail that would have long-lasting consequences.
The French negotiators cried foul, while the Spanish side stuck to their guns. Ultimately, the King of Spain would keep Llívia on the back of this technicality, but this settlement, which traces its origins back to Roman times, would remain an enclave, surrounded on all sides by French territory.
In order to placate the French, Spain gave them, in return, the adjacent valley of Carol, of strategic importance since it controls the road to Toulouse.
It was a solution worthy of King Solomon: splitting the territory down the middle. It sounds simple, yet it overlooked local complexities.
The new border divided communities that, to this day, share to some degree a common language and sense of identity.
How Llívia got cut off
"At the time it was not obvious that Llívia would end up being an enclave," explains Bougain. "The maps the delegations were using were not that accurate and it was only later, once the border had been settled, that they realized a narrow strip of land, belonging to two nearby villages ceded to France, cut Llívia off from the rest of Spain".
Those borders were ratified in the Treaty of Bayonne in 1866. The road linking Llívia to the Spanish mainland was also designated as of "free passage." Little they imagined that a century later this special status would lead to the so-called "War of the Stops."
In the early 1970s, France built two roads intersecting with the international road and granted them right of way. Llívia residents reacted with indignation and started to tear down the "Stop" road signs installed by the French authorities, since they were seen as a breach of the international treaty.
The situation was sorted years later by building an overpass and a traffic circle, but until the implementation of the EU's Schengen Agreement in 1985, only cars with Spanish car plates could drive on this road. The main access road to Llívia is considered part of Spain's national road network, even if part of it runs through French territory.
Another more serious conflict reached Llívia in 1939.
In the last stages of the Spanish Civil War, when the Francoist army reached the border, it had to request permission from the French government to march on to Llívia. Photographs of the time show how soldiers then covered one and a half miles through French territory under the watchful eye of French gendarmes, lined along the road.
Membrane, not a barrier
This photo was taken in Spain, but France lies beyond the border marker.
A rather different panorama greets visitors today. With internal EU borders mostly gone, links between both sides were reestablished (if they ever went away). Locals may hold different passports, but many share their Catalan language and culture.
"The border has also become part of the local identity," explains François Mancebo, professor of sustainability and urban planning at the University of Reims and an expert on the region's cross-border relations. "It is an obstacle but it has often been also a way to earn money for those that could game the system, for example by trading, whether legally or illegally.
"People of both sides kept close links, often they married... I would define it as a sort of membrane rather than a barrier."
Another pivotal moment for the whole region was the 1980s inauguration of Cadí tunnel, running underneath the Serra de Moixeró mountain range, which greatly enhanced communications with the Barcelona area. An influx of visitors and money followed suit.
"At first, when the hard border was gone it created a bit of disorientation," says Professor Mancebo, "but both sides soon benefited from new economic opportunities.
"The French side, for example, gravitated towards the south. It has attracted plenty of private investment and there has been a resurgence of Catalan identity too."
Perhaps the most tangible symbol of this coming together is the 2014 opening of a new modern cross-border hospital. Located almost literally on the border line, it is the first hospital facility in the EU conceived to seamlessly treat patients from two different member states.
Today, the valley is a vacation destination, with tourists attracted to its multiple ski resorts and mountain trails. Vacation homes, shops and businesses have sprung seamlessly on both sides of the border, reviving what were, not so long ago, languishing rural villages.