/* ========================================
   Variables et Réinitialisation CSS
   ======================================== */

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html, body {
    height: 100%;
    overflow: hidden;
}

:root {
    --primary-color: #2c3e50;
    --secondary-color: #3498db;
    --success-color: #27ae60;
    --danger-color: #e74c3c;
    --warning-color: #f39c12;
    --light-bg: #ecf0f1;
    --white: #ffffff;
    --text-dark: #2c3e50;
    --text-light: #7f8c8d;
    --border-color: #bdc3c7;
    --shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    --shadow-lg: 0 4px 16px rgba(0, 0, 0, 0.15);
    --transition: all 0.3s ease;
}

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background-color: var(--light-bg);
    color: var(--text-dark);
    line-height: 1.6;
}

/* ========================================
   Layout Principal (Flexbox)
   ======================================== */

.container {
    display: flex;
    min-height: 100vh;
    height: 100vh;
    gap: 0;
}

.sidebar {
    width: 280px;
    background-color: var(--primary-color);
    color: var(--white);
    padding: 1.5rem;
    overflow: hidden;
    box-shadow: var(--shadow);
    display: flex;
    flex-direction: column;
}

.sidebar h2 {
    margin-bottom: 1.5rem;
    font-size: 1.5rem;
    border-bottom: 2px solid var(--secondary-color);
    padding-bottom: 0.75rem;
}

.sidebar-help {
    margin-top: auto;
    align-self: flex-start;
}

.favorites-list {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
}

.help-link {
    display: inline-block;
    padding: 0.5rem 1rem;
    background-color: rgba(52, 152, 219, 0.2);
    color: var(--white);
    text-decoration: none;
    border-radius: 4px;
    font-size: 0.85rem;
    transition: var(--transition);
    border: 1px solid var(--secondary-color);
}

/* Garder le lien "Aide" blanc même après visite (override de a:visited global) */
.help-link:visited,
.help-link:hover,
.help-link:active,
.help-link:focus,
.help-link:focus-visible {
    color: var(--white);
}

.help-link:hover {
    background-color: var(--secondary-color);
    box-shadow: 0 0 8px rgba(52, 152, 219, 0.5);
}

.main-content {
    flex: 1;
    padding: 1.25rem;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    min-height: 0;
}

/* ========================================
   Header
   ======================================== */

header {
    margin-bottom: 1rem;
    padding-bottom: 0.75rem;
    border-bottom: 3px solid var(--secondary-color);
}

header h1 {
    font-size: 2.1rem;
    color: var(--primary-color);
    margin-bottom: 0.5rem;
}

.subtitle {
    color: var(--text-light);
    font-size: 1rem;
}

/* ========================================
   Section de Recherche
   ======================================== */

.search-section {
    margin-bottom: 1rem;
}

.search-container {
    display: flex;
    gap: 0.75rem;
}

#select-year {
    padding: 0.55rem 0.9rem;
    border: 2px solid var(--border-color);
    border-radius: 4px;
    font-size: 1rem;
    background-color: var(--white);
    color: var(--text-dark);
    transition: var(--transition);
    min-width: 110px;

}

#select-year:focus {
    outline: none;
    border-color: var(--secondary-color);
    box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.1);
}

#input-search {
    flex: 1;
    padding: 0.55rem 0.9rem;
    border: 2px solid var(--border-color);
    border-radius: 4px;
    font-size: 1rem;
    transition: var(--transition);
}

#input-search:focus {
    outline: none;
    border-color: var(--secondary-color);
    box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.1);
}

#btn-search {
    padding: 0.55rem 1.4rem;
    background-color: var(--secondary-color);
    color: var(--white);
    border: none;
    border-radius: 4px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: var(--transition);
}

#btn-search:hover:not(:disabled) {
    background-color: #2980b9;
    box-shadow: var(--shadow-lg);
    transform: translateY(-2px);
}

#btn-search:disabled {
    background-color: var(--border-color);
    cursor: not-allowed;
    opacity: 0.6;
}

#btn-search:active:not(:disabled) {
    transform: translateY(0);
}

/* ========================================
   Loader
   ======================================== */

.loader-container {
    text-align: center;
    padding: 2rem;
    margin-bottom: 2rem;
}

.loader-container.hidden {
    display: none;
}

.spinner {
    border: 4px solid var(--light-bg);
    border-top: 4px solid var(--secondary-color);
    border-radius: 50%;
    width: 40px;
    height: 40px;
    animation: spin 0.8s linear infinite;
    margin: 0 auto 1rem;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

.loader-container p {
    color: var(--text-light);
    font-size: 0.95rem;
}

/* ========================================
   Section Résultats
   ======================================== */

.results-section {
    margin-bottom: 1rem;
    flex: 1 1 auto;
    min-height: 0;
}

.results-container {
    background-color: var(--white);
    border-radius: 8px;
    padding: 1rem;
    box-shadow: var(--shadow);
    height: 100%;
    display: flex;
    flex-direction: column;
    min-height: 0;
    overflow: hidden;
}

#visualizations-container {
    flex: 1 1 auto;
    min-height: 0;
    overflow: hidden;
}

.message {
    color: var(--text-light);
    text-align: center;
    padding: 1rem;
    font-style: italic;
}

/* ========================================
   Carte Résultat
   ======================================== */

.result-card {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    margin-bottom: 0.75rem;
    transition: var(--transition);
    background-color: var(--white);
}

.result-card:last-child {
    margin-bottom: 0;
}

.result-info {
    flex: 1;
}

.result-country-name {
    font-size: 1.2rem;
    font-weight: 600;
    color: var(--primary-color);
    margin-bottom: 0.25rem;
}

.result-stats {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 0.6rem;
}

.result-population,
.result-year,
.result-birth-rate,
.result-death-rate,
.result-births,
.result-deaths {
    font-size: 0.9rem;
    color: var(--text-dark);
    padding: 0.35rem;
    border-left: 3px solid var(--secondary-color);
    padding-left: 0.75rem;
}

.result-year {
    border-left-color: var(--text-light);
    color: var(--text-light);
    font-size: 0.85rem;
}

.result-birth-rate {
    border-left-color: #27ae60;
}

.result-death-rate {
    border-left-color: var(--danger-color);
}

.result-births,
.result-deaths {
    border-left-color: var(--warning-color);
}

.result-population strong,
.result-birth-rate strong,
.result-death-rate strong,
.result-births strong,
.result-deaths strong {
    color: var(--primary-color);
    font-weight: 700;
}

.result-actions {
    display: flex;
    gap: 1rem;
    align-items: center;
}

.star-btn {
    background: none;
    border: none;
    font-size: 1.8rem;
    cursor: pointer;
    transition: var(--transition);
    padding: 0.25rem;
    display: flex;
    align-items: center;
    justify-content: center;
}

.star-btn:hover {
    transform: scale(1.2);
}

.star-empty {
    color: var(--warning-color);
}

.star-full {
    color: #f1c40f;
}

/* ========================================
   Zone d'Erreur
   ======================================== */

.error-container {
    background-color: #fadbd8;
    border-left: 4px solid var(--danger-color);
    color: #c0392b;
    padding: 1rem 1.5rem;
    border-radius: 4px;
    margin-bottom: 1.5rem;
    display: none;
}

.error-container:not(.hidden) {
    display: block;
    animation: slideInDown 0.3s ease;
}

.error-container.hidden {
    display: none;
}

#error-message {
    margin: 0;
}

@keyframes slideInDown {
    from {
        opacity: 0;
        transform: translateY(-10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* ========================================
   Favoris (Sidebar)
   ======================================== */

.favorites-list {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.favorites-list p {
    color: var(--light-bg);
    font-style: italic;
    opacity: 0.8;
}

.favorites-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: rgba(255, 255, 255, 0.1);
    padding: 0.75rem 1rem;
    border-radius: 4px;
    transition: var(--transition);
    cursor: pointer;
}

.favorites-item:hover {
    background-color: rgba(255, 255, 255, 0.2);
}

.fav-country-name {
    flex: 1;
    font-weight: 500;
}

.fav-delete-btn {
    background: none;
    border: none;
    color: var(--white);
    cursor: pointer;
    font-size: 1rem;
    padding: 0;
    transition: var(--transition);
}

.fav-delete-btn:hover {
    color: var(--danger-color);
    transform: scale(1.2);
}

/* ========================================
   Responsive Design (Mobile First)
   ======================================== */

@media (max-width: 768px) {
    .container {
        flex-direction: column;
    }

    .sidebar {
        width: 100%;
        padding: 1.5rem;
        max-height: 30vh;
        border-bottom: 2px solid var(--secondary-color);
    }

    .main-content {
        padding: 1.5rem;
    }

    header h1 {
        font-size: 1.8rem;
    }

    .subtitle {
        font-size: 0.9rem;
    }

    .search-container {
        flex-direction: column;
    }

    #input-search,
    #select-year,
    #btn-search {
        width: 100%;
    }

    .result-card {
        flex-direction: column;
        align-items: flex-start;
    }

    .result-actions {
        width: 100%;
        margin-top: 1rem;
    }
}

@media (max-width: 480px) {
    .container {
        min-height: auto;
    }

    .sidebar {
        padding: 1rem;
        max-height: 25vh;
    }

    .main-content {
        padding: 1rem;
    }

    header h1 {
        font-size: 1.4rem;
    }

    .sidebar h2 {
        font-size: 1.1rem;
    }

    .result-country-name {
        font-size: 1.1rem;
    }

    .result-population {
        font-size: 0.9rem;
    }

    .star-btn {
        font-size: 1.5rem;
    }
}

/* ========================================
   Accessibilité
   ======================================== */

:focus-visible {
    outline: 2px solid var(--secondary-color);
    outline-offset: 2px;
}

/* Respect du contraste WCAG AA */
body {
    color: #212121;
}

.sidebar {
    background-color: #1a252f;
    color: #ffffff;
}

a, button {
    color: var(--secondary-color);
}

a:visited {
    color: #1d5fa3;
}

/* Réduction des animations pour les utilisateurs qui la préfèrent */
@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        transition-duration: 0.01ms !important;
    }
}

/* ========================================
   Visualisations Graphiques
   ======================================== */

.visualizations-section {
    background-color: var(--white);
    border-radius: 8px;
    padding: 0.75rem;
    box-shadow: var(--shadow-lg);
    animation: fadeIn 0.4s ease;
    height: 100%;
    min-height: 0;
    display: grid;
    grid-template-columns: 1fr minmax(240px, 320px);
    grid-template-rows: auto auto 1fr;
    grid-template-areas:
        "header header"
        "tabsHeader ."
        "tabsContent pyramid";
    gap: 0.5rem;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.visualization-header {
    margin-bottom: 0.5rem;
    padding-bottom: 0.5rem;
    border-bottom: 2px solid var(--secondary-color);
    grid-area: header;
}

.visualization-header h2 {
    font-size: 1.25rem;
    color: var(--primary-color);
    margin-bottom: 0.25rem;
}

.analysis-year {
    color: var(--text-light);
    font-size: 0.8rem;
}

.visualizations-main {
    display: contents;
}

.chart-wrapper {
    background-color: var(--white);
    border-radius: 6px;
    padding: 0.75rem;
    border: 1px solid var(--border-color);
    box-shadow: var(--shadow);
    transition: var(--transition);
    position: relative;
    height: 100%;
    min-height: 0;
    width: 100%;
    margin-bottom: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}
.chart-wrapper:hover {
    box-shadow: var(--shadow);
    border-color: var(--secondary-color);
}

.chart-pyramid {
    grid-area: pyramid;
    justify-self: end;
    max-width: 320px;
    max-height: clamp(320px, 48vh, 520px);
}

.chart-evolution {
    grid-area: evolution;
}


.chart-canvas {
    position: relative;
    flex: 1 1 auto;
    min-height: 0;
}

.chart-canvas canvas {
    display: block;
    width: 100% !important;
    height: 100% !important;
}

.chart-title {
    margin-bottom: 0.5rem;
}

.chart-title h3 {
    font-size: 1rem;
    color: var(--primary-color);
    margin-bottom: 0.15rem;
}

.chart-subtitle {
    color: var(--text-light);
    font-size: 0.8rem;
    font-style: italic;
}

/* ========================================
   Système d'Onglets
   ======================================== */

.visualizations-tabs {
    display: contents;
}

.tabs-header {
    display: flex;
    gap: 0.75rem;
    margin-bottom: 0;
    flex-wrap: wrap;
    grid-area: tabsHeader;
}

.tab-btn {
    padding: 0.45rem 1rem;
    background-color: var(--light-bg);
    border: 2px solid var(--border-color);
    border-radius: 4px 4px 0 0;
    cursor: pointer;
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-dark);
    transition: var(--transition);
}

.tab-btn:hover:not(.active) {
    background-color: #d5dbdb;
    border-color: var(--secondary-color);
}

.tab-btn.active {
    background-color: var(--secondary-color);
    color: var(--white);
    border-color: var(--secondary-color);
    box-shadow: 0 -2px 8px rgba(52, 152, 219, 0.2);
}

.tab-content {
    display: none;
    animation: fadeIn 0.3s ease;
    grid-area: tabsContent;
}

.tab-content.active {
    display: flex;
    flex: 1 1 auto;
    min-height: 0;
}

.tab-content .chart-wrapper {
    height: 100%;
    min-height: 0;
    max-height: clamp(360px, 52vh, 620px);
}

/* Desktop compact (Full HD et +) */
@media (min-width: 1200px) and (min-height: 800px) {
    .container {
        min-height: 100dvh;
        height: 100dvh;
    }

    .sidebar {
        width: 260px;
    }

    header h1 {
        font-size: 2rem;
    }
}

/* ========================================
   Graphiques - Responsive
   ======================================== */

@media (max-width: 1024px) {
    .visualizations-section {
        grid-template-columns: 1fr;
        grid-template-rows: auto auto auto 1fr;
        grid-template-areas:
            "header"
            "pyramid"
            "tabsHeader"
            "tabsContent";
    }

    .chart-pyramid {
        justify-self: stretch;
        max-width: none;
    }
}

@media (max-width: 768px) {
    .visualizations-section {
        padding: 1rem;
    }

    .visualization-header h2 {
        font-size: 1.2rem;
    }

    .chart-pyramid {
        min-height: 320px;
    }

    .chart-evolution {
        min-height: 250px;
    }

    .tabs-header {
        gap: 0.5rem;
    }

    .tab-btn {
        padding: 0.5rem 0.9rem;
        font-size: 0.8rem;
    }

    .chart-title h3 {
        font-size: 1rem;
    }
}

@media (max-width: 480px) {
    .visualizations-section {
        padding: 0.8rem;
    }

    .visualization-header h2 {
        font-size: 1.1rem;
    }

    .chart-pyramid {
        min-height: 280px;
    }

    .chart-evolution {
        min-height: 220px;
    }

    .chart-wrapper {
        padding: 0.8rem;
    }

    .chart-title h3 {
        font-size: 0.95rem;
    }

    .chart-subtitle {
        font-size: 0.75rem;
    }

    .tabs-header {
        flex-direction: column;
        gap: 0.5rem;
    }

    .tab-btn {
        width: 100%;
        padding: 0.5rem 0.8rem;
        border-radius: 4px;
    }

    .tab-btn.active {
        border-radius: 4px;
    }
}
