:root {
    /* Dynamic variables are kept in the HTML file */
}

body {
    font-family: 'Mate SC', serif;
    margin: 0;
    padding-top: var(--top-bar-height); /* Push content down */
    background: linear-gradient(135deg, #0c0c0c 0%, #1a1a2e 50%, #16213e 100%);
    height: 100vh;
    overflow: hidden;
    position: relative;
    box-sizing: border-box;
}

/* Étoiles en arrière-plan */
body::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-image: 
        radial-gradient(2px 2px at 20px 30px, #eee, transparent),
        radial-gradient(2px 2px at 40px 70px, rgba(255,255,255,0.8), transparent),
        radial-gradient(1px 1px at 90px 40px, #fff, transparent),
        radial-gradient(1px 1px at 130px 80px, rgba(255,255,255,0.6), transparent),
        radial-gradient(2px 2px at 160px 30px, #fff, transparent);
    background-repeat: repeat;
    background-size: 200px 100px;
    animation: twinkle 4s ease-in-out infinite alternate;
    z-index: -1; /* Ensure it's behind everything */
}

@keyframes twinkle {
    0% { opacity: 0.3; }
    100% { opacity: 1; }
}

@keyframes burning-sun {
    0% {
        filter: drop-shadow(0 0 4px rgba(255, 215, 0, 0.8)) 
                drop-shadow(0 0 8px rgba(255, 140, 0, 0.6)) 
                drop-shadow(0 0 12px rgba(255, 0, 0, 0.5));
    }
    100% {
        filter: drop-shadow(0 0 6px rgba(255, 215, 0, 1)) 
                drop-shadow(0 0 12px rgba(255, 140, 0, 0.7)) 
                drop-shadow(0 0 20px rgba(255, 0, 0, 0.5));
    }
}

.top-bar {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: auto;
    min-height: var(--top-bar-height);
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 15px;
    flex-wrap: nowrap; /* Prevents wrapping */
    gap: 15px; /* Increased gap for better spacing */
    
    /* --- Vintage Papyrus Background --- */
    background-color: #f5eeda; /* Base papyrus color */
    background-image: 
        /* Subtle grain texture */
        linear-gradient(90deg, rgba(210, 180, 140, 0.15) 1px, transparent 1px),
        linear-gradient(rgba(210, 180, 140, 0.15) 1px, transparent 1px);
    background-size: 4px 4px, 4px 4px;
    background-repeat: repeat, repeat;

    border-bottom: 1px solid rgba(101, 67, 33, 0.3); /* Sepia border */
    z-index: 100;
    
    /* --- Shadow for Papyrus Effect --- */
    box-shadow: 
        0 3px 5px rgba(0,0,0,0.3), /* Main drop shadow */
        inset 0 3px 4px -2px rgba(101, 67, 33, 0.2), /* Inner top shadow */
        inset 0 -3px 4px -2px rgba(101, 67, 33, 0.2); /* Inner bottom shadow */

    color: #5d4037; /* Dark sepia text */
    font-family: 'IM Fell English SC', serif;
    box-sizing: border-box;
}

/* The media query that caused wrapping has been removed. */

.top-bar-left, .top-bar-right {
    display: flex;
    align-items: center;
    gap: 15px;
    flex-shrink: 1; /* Allow sections to shrink */
    flex-wrap: nowrap; /* Prevent internal wrapping */
    min-width: 0; /* Allow shrinking below content size */
}

.top-bar-center {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    flex-grow: 1;
    flex-shrink: 0; /* Do not allow the title to shrink */
    line-height: 1.1;
    text-align: center;
    pointer-events: none;
    margin: 0 20px; /* Add horizontal margin */
}

.top-bar-center > div {
    pointer-events: auto;
}

.top-bar-title {
    font-size: 28px;
    text-shadow: 1px 1px 2px rgba(255, 255, 255, 0.7);
    font-weight: bold;
    color: #4e342e;
    display: block;
}

.top-bar-subtitle {
    font-size: 14px;
    font-family: 'Mate SC', serif;
    color: #6d4c41;
    display: block;
    margin-top: -2px;
}

.top-bar-button {
    background: rgba(255,255,255,0.2);
    border: 1px solid rgba(101, 67, 33, 0.4);
    color: #4e342e;
    padding: 8px 15px;
    border-radius: 3px;
    cursor: pointer;
    font-family: 'Mate SC', serif;
    font-size: 14px;
    transition: all 0.3s ease;
}

.top-bar-button:hover {
    background: rgba(255,255,255,0.5);
    border-color: rgba(101, 67, 33, 0.7);
    box-shadow: 0 0 8px rgba(101, 67, 33, 0.2);
}

.top-bar-button.toggled {
    background: rgba(101, 67, 33, 0.2); /* A sepia-like color */
    border-color: rgba(101, 67, 33, 0.7);
    box-shadow: inset 0 1px 3px rgba(0,0,0,0.2);
}

.story-mode-prominent {
    background-color: #6495ED; /* Cornflower Blue, similar to sea area */
    border: 1px solid #4682B4; /* SteelBlue */
    color: white;
    font-weight: bold;
    text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5);
    box-shadow: 0 2px 4px rgba(0,0,0,0.2);
}

.story-mode-prominent:hover {
    background-color: #87CEEB; /* SkyBlue */
    border-color: #6495ED;
}

.story-controls-container {
    display: flex;
    gap: 5px;
}

.story-controls-container.hidden {
    display: none;
}


.info-display {
    display: flex;
    align-items: center;
    gap: 15px;
    font-size: 16px;
    background: rgba(255,255,255,0.2);
    padding: 5px 15px;
    border-radius: 4px;
    border: 1px solid rgba(101, 67, 33, 0.3);
}

.info-display .separator {
    color: rgba(101, 67, 33, 0.4);
}

#totalPopulation {
    font-family: 'Courier New', Courier, monospace;
    font-weight: bold;
}

#tooltip {
    position: absolute;
    display: none;
    background: rgba(0, 0, 0, 0.85);
    color: white;
    padding: 10px;
    border-radius: 8px;
    border: 1px solid rgba(255, 255, 255, 0.3);
    font-size: 14px;
    pointer-events: none; /* Important pour ne pas interférer avec les autres événements de la souris */
    z-index: 1000;
    width: 220px;
    box-shadow: 0 4px 15px rgba(0,0,0,0.5);
}

#tooltip h3 {
    margin: 0 0 8px 0;
    font-size: 16px;
    display: flex;
    align-items: center;
}

#tooltip img {
    width: 30px;
    margin-right: 10px;
    border: 1px solid #555;
}

#tooltip p {
    margin: 4px 0;
}

#visualization {
    position: absolute;
    top: var(--top-bar-height);
    left: 0;
    width: 100vw;
    height: calc(100vh - var(--top-bar-height));
    z-index: 1;
}

#canvas {
    width: 100%;
    height: 100%;
    background: radial-gradient(ellipse at center, rgba(25,25,50,0.8) 0%, rgba(0,0,0,0.9) 100%);
    box-shadow: 0 0 50px rgba(100,150,255,0.3);
    cursor: grab;
}

#canvas.grabbing {
    cursor: grabbing;
}

#resetZoomButton {
    position: absolute;
    top: 20px;
    left: 20px;
    z-index: 10;
    display: none; /* Hidden by default */
    background: rgba(0,0,0,0.5);
    color: white;
    border: 1px solid rgba(255,255,255,0.3);
    padding: 5px 10px;
    border-radius: 5px;
    cursor: pointer;
}

#resetZoomButton:hover {
    background: rgba(0,0,0,0.7);
}

.year-selector {
    position: relative;
    z-index: 10;
    text-align: center;
    margin: 10px 0;
}

.controls {
    position: fixed;
    top: calc(var(--top-bar-height) + 20px); /* Position below top bar */
    right: 20px;
    display: flex;
    flex-direction: column;
    gap: 20px;
    background: rgba(0, 0, 0, 0.7);
    padding: 20px;
    border-radius: 10px;
    z-index: 10;
    border: 1px solid rgba(255, 255, 255, 0.2);
    width: 300px;
    transition: transform 0.3s ease-in-out;
}

.controls.hidden {
    transform: translateX(calc(100% + 40px)); /* Move it off-screen */
}

#helpContent {
    position: fixed;
    top: var(--top-bar-height);
    left: 0;
    width: 100%;
    height: calc(100vh - var(--top-bar-height));
    z-index: 99; /* Below top bar but above visualization */
    
    /* --- Parchment/Papyrus Background --- */
    background-color: #f5eeda; /* Base papyrus color */
    background-image: 
        linear-gradient(90deg, rgba(210, 180, 140, 0.15) 1px, transparent 1px),
        linear-gradient(rgba(210, 180, 140, 0.15) 1px, transparent 1px);
    background-size: 4px 4px, 4px 4px;
    
    /* --- Unfurling Animation --- */
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.7s cubic-bezier(0.86, 0, 0.07, 1);
    
    box-shadow: 0 5px 15px rgba(0,0,0,0.5);
    border-top: 1px solid rgba(101, 67, 33, 0.4);
}

#helpContent.is-open {
    max-height: 100vh; /* A value larger than the actual height */
}

#helpContent iframe {
    width: 100%;
    height: 100%;
    border: none;
}

.control-group {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
    width: 100%;
}

.control-group label, .control-group span {
    color: white;
}

.control-group input[type="range"] {
    width: 100%;
}

.country-name-label {
    font-size: var(--country-label-font-size);
    fill: #333;
    font-weight: bold;
    text-anchor: start;
    dominant-baseline: hanging;
    pointer-events: none;
    user-select: none;
}

.country-circle-total {
    stroke: rgba(100, 150, 255, 0.4);
    stroke-width: 1;
    fill: radial-gradient(circle, rgba(100, 150, 255, 0.3) 0%, rgba(100, 150, 255, 0.1) 70%, rgba(100, 150, 255, 0.05) 100%);
    opacity: 0.8;
    filter: drop-shadow(0 0 10px rgba(100,150,255,0.3));
    cursor: pointer;
}

.country-point {
    stroke: none; /* Le dégradé gère la couleur */
    cursor: pointer;
    animation: burning-sun 3s ease-in-out infinite alternate;
}

.planet {
    cursor: pointer;
    opacity: 0.8;
    filter: drop-shadow(0 0 3px rgba(255,255,255,0.4));
}

.planet:hover {
    opacity: 1;
    filter: drop-shadow(0 0 8px rgba(255,255,255,0.8));
}

.highlighted {
    opacity: 1 !important;
    filter: drop-shadow(0 0 15px rgba(255, 255, 100, 1)) !important;
    stroke: yellow !important;
    stroke-width: 2px !important;
    stroke-opacity: 1 !important;
}

.country-name-label {
    font-size: 11px;
    fill: white;
    font-weight: bold;
    text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.8);
    pointer-events: none;
    text-anchor: start;
    dominant-baseline: hanging;
}

@keyframes rotate {
    from {
        transform: rotate(0deg);
    }
    to {
        transform: rotate(360deg);
    }
}

.legend {
    margin-top: 20px;
    background: rgba(0, 0, 0, 0.8);
    color: white;
    padding: 15px;
    border-radius: 10px;
    font-size: 12px;
    border: 1px solid rgba(255, 255, 255, 0.2);
    width: 100%;
    box-sizing: border-box;
}

.legend-item {
    display: flex;
    align-items: center;
    margin: 5px 0;
}

.legend-color {
    width: 15px;
    height: 15px;
    margin-right: 8px;
    border-radius: 50%;
}

.legend-stroke {
    border: 2px solid;
    background: transparent;
}

#distance-scale-container {
    margin-top: 10px;
    padding-top: 5px;
    border-top: 1px solid rgba(255, 255, 255, 0.2);
}

.scale-label {
    font-size: 10px;
    text-align: center;
    color: #ccc;
}

.scale-bar {
    height: 2px;
    background: white;
    margin: 2px auto 0;
    border-bottom: 1px solid #555;
    border-right: 1px solid #555;
}

.legend-container {
    position: fixed;
    bottom: 20px;
    left: 20px;
    z-index: 10;
    transition: transform 0.3s ease-in-out;
}

.legend-container.hidden {
    transform: translateX(calc(-100% - 20px));
}

/* --- Statistics Panel --- */
#statsContainer {
    position: fixed;
    top: 32%;
    left: 65%;
    transform: translate(-50%, -50%);
    width: 30%; /* Smaller width */
    max-width: 700px;
    height: 50vh; /* Smaller height */
    background: transparent;
    border: none;
    z-index: 101;
    display: none;
    flex-direction: column;
    padding: 20px;
    color: white;
}

#statsHeader {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    flex-shrink: 0;
}

#statsHeader h2 {
    font-family: 'IM Fell English SC', serif;
    margin: 0;
    font-size: 1.5em;
    color: #f5eeda;
}

#statsMetricButtons {
    display: flex;
    gap: 10px;
    width: 100%;
}

.stats-metric-button {
    flex-grow: 1;
    background: rgba(255, 255, 255, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.4);
    color: white;
    padding: 8px 5px;
    cursor: pointer;
    border-radius: 4px;
    transition: background-color 0.2s;
    font-family: 'Mate SC', serif;
    font-size: 12px;
}

.stats-metric-button:hover {
    background: rgba(255, 255, 255, 0.2);
}

.stats-metric-button.active {
    background: rgba(255, 255, 255, 0.3);
    border-color: white;
    font-weight: bold;
}

#statsChartContainer {
    flex-grow: 1;
    display: flex;
    flex-direction: row;
    justify-content: space-around;
    align-items: flex-end; /* Align bars to the bottom */
    gap: 4px;
    overflow-x: hidden; /* Prevent horizontal scrolling */
    overflow-y: hidden;
    padding-bottom: 10px;
}

.stats-bar-container {
    flex-grow: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-end;
    position: relative;
    cursor: pointer;
    height: 100%; /* Ensure container takes full height */
    min-width: 0; /* Allow shrinking */
}

#statsTooltip {
    display: none;
    position: absolute;
    left: 50%;
    top: 70px; 
    transform: translateX(-50%);
    background-color: rgba(0, 0, 0, 0.9);
    color: white;
    padding: 5px 15px;
    border-radius: 4px;
    font-size: 14px;
    font-weight: bold;
    white-space: nowrap;
    z-index: 110; /* Above other elements */
    border: 1px solid rgba(255,255,255,0.3);
}

.stats-bar-visual {
    width: 100%;
    background: linear-gradient(to top, #FF8C00, #FFD700);
    border-radius: 3px 3px 0 0;
    transition: all 0.2s ease-in-out;
}

.stats-bar-container:hover .stats-bar-visual, .stats-bar-container.highlighted .stats-bar-visual {
    filter: brightness(1.2);
}

.stats-bar-flag {
    width: 100%;
    max-width: 20px;
    height: 15px;
    margin-top: 5px;
    border: 1px solid rgba(255,255,255,0.5);
}

#navigation-controls {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

#navigation-controls button {
    background: rgba(0,0,0,0.5);
    border: 1px solid rgba(255,255,255,0.3);
    color: white;
    border-radius: 5px;
    cursor: pointer;
    font-family: sans-serif;
    font-size: 18px;
    width: 40px;
    height: 40px;
    transition: background 0.2s;
}

#navigation-controls button:hover {
    background: rgba(0,0,0,0.7);
}

.zoom-controls {
    display: flex;
    flex-direction: column;
    gap: 5px;
    background: rgba(0, 0, 0, 0.7);
    padding: 10px;
    border-radius: 10px;
    border: 1px solid rgba(255, 255, 255, 0.2);
}

.pan-controls {
    display: grid;
    grid-template-areas:
        ". up ."
        "left center right"
        ". down .";
    gap: 5px;
    background: rgba(0, 0, 0, 0.7);
    padding: 10px;
    border-radius: 10px;
    border: 1px solid rgba(255, 255, 255, 0.2);
}

#pan-up { grid-area: up; }
#pan-down { grid-area: down; }
#pan-left { grid-area: left; }
#pan-right { grid-area: right; }
#pan-center { grid-area: center; }

/* --- Story Mode Panel --- */
#storyModePanel {
    position: fixed;
    top: 90px; /* Adjusted from 50% */
    right: 20px; /* Changed from left: 50% */
    left: auto;
    transform: scale(1); /* Removed translate */
    width: 35%; /* Increased from 90% with max-width */
    max-width: 1200px; /* Increased from 900px */
    height: 60vh; /* Increased from 60vh */
    background: rgba(20, 20, 35, 0.95);
    border-radius: 15px;
    z-index: 102;
    display: flex;
    flex-direction: column;
    color: #f5eeda;
    box-shadow: 0 10px 30px rgba(0,0,0,0.5);
    transition: transform 0.3s ease, opacity 0.3s ease;
    opacity: 1;
}

#storyModePanel.hidden {
    transform: scale(0.8); /* Simplified from translate + scale */
    opacity: 0;
    pointer-events: none;
}

.story-content {
    padding: 20px;
    display: flex;
    flex-direction: column;
    height: 100%;
}

#storyDecadeTitle {
    font-family: 'IM Fell English SC', serif;
    text-align: center;
    font-size: 2em;
    margin: 0 0 15px 0;
    color: #FFD700;
    text-shadow: 1px 1px 3px rgba(0,0,0,0.5);
}

.story-columns {
    display: flex;
    flex-grow: 1;
    gap: 20px;
    overflow: hidden;
}

.story-facts-container {
    flex: 0 0 250px; /* Fixed width for the facts list */
    background: rgba(0,0,0,0.3);
    border-radius: 8px;
    padding: 15px;
    display: flex;
    flex-direction: column;
}

.story-charts-grid {
    flex-grow: 1;
    display: grid;
    /* A 2-column grid */
    grid-template-columns: repeat(2, 1fr);
    /* Let rows size automatically */
    grid-auto-rows: minmax(0, 1fr);
    gap: 15px;
    overflow-y: auto;
}

.story-chart-container {
    border-radius: 8px;
    padding: 15px;
    display: flex;
    flex-direction: column;
    position: relative; /* For Chart.js responsiveness */
}

/* Make the population evolution chart span both columns as it's the most complex */
.story-charts-grid .story-chart-container:nth-child(3) {
    grid-column: 1 / -1;
}

.story-chart-container p, .story-facts-container p {
    margin: 0 0 10px 0;
    font-weight: bold;
    text-align: center;
    border-bottom: 1px solid rgba(245, 238, 218, 0.3);
    padding-bottom: 5px;
    flex-shrink: 0;
}

.story-chart-container canvas {
    flex-grow: 1;
    min-height: 0;
}

.story-facts-container ul {
    list-style-type: none;
    padding: 0;
    margin: 0;
    overflow-y: auto;
    font-family: 'Mate SC', serif;
}

.story-facts-container li {
    padding: 8px 5px;
    border-bottom: 1px solid rgba(245, 238, 218, 0.1);
}

.story-facts-container li:last-child {
    border-bottom: none;
}

.story-navigation {
    display: flex;
    justify-content: center;
    gap: 20px;
    padding-top: 15px;
    border-top: 1px solid rgba(245, 238, 218, 0.2);
}

.story-navigation button {
    background: rgba(245, 238, 218, 0.2);
    border: 1px solid rgba(245, 238, 218, 0.5);
    color: #f5eeda;
    padding: 10px 20px;
    border-radius: 5px;
    cursor: pointer;
    font-family: 'IM Fell English SC', serif;
    font-size: 1em;
    transition: all 0.2s ease;
}

.story-navigation button:hover {
    background: rgba(245, 238, 218, 0.4);
    border-color: #FFD700;
}

.story-navigation button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.story-charts-container {
    flex-grow: 1;
    display: flex;
    flex-direction: column;
    border-radius: 8px;
    padding: 15px;
}

.story-charts-wrapper {
    flex-grow: 1;
    position: relative;
}

.story-chart-container {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex; /* Use flex to help canvas resize */
    flex-direction: column;
}

.story-chart-container p {
    margin: 0 0 10px 0;
    font-weight: bold;
    text-align: center;
    border-bottom: 1px solid rgba(245, 238, 218, 0.3);
    padding-bottom: 5px;
    flex-shrink: 0;
}

.story-chart-container.active-chart {
    /* This class is kept for consistency but no longer hides/shows */
    opacity: 1;
    visibility: visible;
}

.story-chart-container canvas {
    flex-grow: 1;
    min-height: 0;
}

.fact-label-group .fact-line {
    stroke: yellow;
    stroke-width: 1.5;
    stroke-dasharray: 4, 2;
    opacity: 0.8;
}

.fact-label-group .fact-text {
    fill: yellow;
    font-weight: bold;
    font-size: 14px;
    text-anchor: middle;
    filter: drop-shadow(1px 1px 2px rgba(0,0,0,0.8));
}

#factScroller {
    position: absolute;
    top: calc(var(--top-bar-height) + 20px);
    left: 50%;
    transform: translateX(-50%);
    width: 80%;
    max-width: 900px;
    height: 150px; /* Adjust height to fit multiple facts */
    z-index: 1020;
    pointer-events: none;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
}

.story-fact-item {
    background-color: rgba(0, 0, 0, 0.5);
    color: yellow;
    text-shadow: 1px 1px 3px rgba(0, 0, 0, 1);
    text-align: center;
    padding: 10px 20px;
    border-radius: 8px;
    font-family: 'Mate SC', serif;
    font-size: 1.2em;
    box-sizing: border-box;
    transition: opacity 0.8s ease-in-out, transform 0.8s ease-in-out;
    opacity: 0;
    transform: translateY(20px);
}

.story-fact-item.visible {
    opacity: 1;
    transform: translateY(0);
}

.story-highlight {
    stroke: yellow !important;
    stroke-width: 3px !important;
    stroke-opacity: 1 !important;
    filter: drop-shadow(0 0 15px yellow) !important;
}

/* Styles pour le curseur d'année */
.year-slider-container input[type="range"] {
    -webkit-appearance: none;
    width: 100%;
    height: 6px;
    border-radius: 3px;
    background: #4a6fa5; /* Fond bleu clair */
    outline: none;
    margin: 10px 0;
}

/* Style du curseur (le bouton que l'on déplace) */
.year-slider-container input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: #1e3a8a; /* Bleu plus foncé */
    cursor: pointer;
    border: 2px solid #fff;
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.5);
}

.year-slider-container input[type="range"]::-moz-range-thumb {
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: #1e3a8a; /* Bleu plus foncé */
    cursor: pointer;
    border: 2px solid #fff;
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.5);
}

/* Style de la barre de progression (partie remplie) */
.year-slider-container input[type="range"]::-webkit-slider-runnable-track {
    background: #4a6fa5; /* Même couleur que le fond */
    border-radius: 3px;
}

.year-slider-container input[type="range"]::-moz-range-track {
    background: #4a6fa5; /* Même couleur que le fond */
    border-radius: 3px;
    height: 6px;
}

/* Style pour le texte de l'année */
#yearValue {
    color: #1e3a8a; /* Bleu foncé pour le texte */
    font-weight: bold;
    white-space: nowrap;
    font-size: 2em; /* Taille de police plus grande */
    min-width: 60px; /* Largeur minimale pour éviter les sauts de mise en page */
    display: inline-block;
    text-align: center;
    margin: 0 10px; /* Marge horizontale pour l'espacement */
}

/* Style pour le conteneur du slider d'année */
.year-slider-container {
    display: flex;
    align-items: center;
    gap: 10px; /* Espacement entre l'année et le slider */
}
