/* ===================================
   CSS Reset and Base Styles
   =================================== */

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

:root {
    /* Color Palette */
    --primary-color: #3388ff;
    --primary-dark: #2c7be5;
    --secondary-color: #6c757d;
    --accent-color: #ff7800;
    --success-color: #28a745;
    --danger-color: #dc3545;
    --warning-color: #ffc107;
    
    /* Neutral Colors */
    --bg-color: #ffffff;
    --bg-secondary: #f8f9fa;
    --text-color: #212529;
    --text-secondary: #6c757d;
    --border-color: #dee2e6;
    
    /* Spacing */
    --spacing-xs: 0.25rem;
    --spacing-sm: 0.5rem;
    --spacing-md: 1rem;
    --spacing-lg: 1.5rem;
    --spacing-xl: 2rem;
    
    /* Typography */
    --font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    --font-size-sm: 0.875rem;
    --font-size-base: 1rem;
    --font-size-lg: 1.125rem;
    --font-size-xl: 1.5rem;
    
    /* Layout */
    --sidebar-width: 320px;
    --header-height: 60px;
    
    /* Shadows */
    --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.12);
    --shadow-md: 0 4px 6px rgba(0, 0, 0, 0.1);
    --shadow-lg: 0 10px 25px rgba(0, 0, 0, 0.15);
    
    /* Transitions */
    --transition-fast: 150ms ease-in-out;
    --transition-base: 250ms ease-in-out;
}

body {
    font-family: var(--font-family);
    font-size: var(--font-size-base);
    line-height: 1.5;
    color: var(--text-color);
    background-color: var(--bg-color);
    overflow: hidden;
}

/* ===================================
   Loading Overlay
   =================================== */

.loading-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(255, 255, 255, 0.95);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 9999;
}

.loading-spinner {
    width: 50px;
    height: 50px;
    border: 4px solid var(--border-color);
    border-top-color: var(--primary-color);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

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

.loading-overlay p {
    margin-top: var(--spacing-md);
    color: var(--text-secondary);
}

.loading-overlay.hidden {
    display: none;
}

/* ===================================
   App Container
   =================================== */

.app-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
    width: 100vw;
}

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

.app-header {
    height: var(--header-height);
    background: var(--bg-color);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 var(--spacing-lg);
    box-shadow: var(--shadow-sm);
    z-index: 100;
}

.app-header h1 {
    font-size: var(--font-size-xl);
    font-weight: 600;
    color: var(--primary-color);
}

.header-controls {
    display: flex;
    gap: var(--spacing-sm);
}

/* ===================================
   Buttons
   =================================== */

.btn {
    padding: var(--spacing-sm) var(--spacing-md);
    border: 1px solid var(--border-color);
    border-radius: 4px;
    background: var(--bg-color);
    color: var(--text-color);
    font-size: var(--font-size-sm);
    font-weight: 500;
    cursor: pointer;
    transition: all var(--transition-fast);
    white-space: nowrap;
}

.btn:hover {
    background: var(--bg-secondary);
    border-color: var(--primary-color);
}

.btn:focus {
    outline: 2px solid var(--primary-color);
    outline-offset: 2px;
}

.btn:active {
    transform: translateY(1px);
}

.btn-primary {
    background: var(--primary-color);
    color: white;
    border-color: var(--primary-color);
}

.btn-primary:hover {
    background: var(--primary-dark);
    border-color: var(--primary-dark);
}

.btn-secondary {
    background: var(--secondary-color);
    color: white;
    border-color: var(--secondary-color);
}

.btn-secondary:hover {
    background: #5a6268;
}

.btn[aria-pressed="true"] {
    background: var(--accent-color);
    border-color: var(--accent-color);
    color: white;
}

/* ===================================
   Sidebar (Desktop)
   =================================== */

.sidebar {
    width: var(--sidebar-width);
    background: var(--bg-color);
    border-right: 1px solid var(--border-color);
    overflow-y: auto;
    flex-shrink: 0;
}

.sidebar-content {
    padding: var(--spacing-lg);
}

/* ===================================
   Search
   =================================== */

.search-container {
    position: relative;
    margin-bottom: var(--spacing-lg);
}

.search-input {
    width: 100%;
    padding: var(--spacing-sm) var(--spacing-md);
    border: 1px solid var(--border-color);
    border-radius: 4px;
    font-size: var(--font-size-base);
    transition: border-color var(--transition-fast);
}

.search-input:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(51, 136, 255, 0.1);
}

.search-suggestions {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    background: var(--bg-color);
    border: 1px solid var(--border-color);
    border-top: none;
    border-radius: 0 0 4px 4px;
    box-shadow: var(--shadow-md);
    max-height: 300px;
    overflow-y: auto;
    z-index: 1000;
    display: none;
}

.search-suggestions.visible {
    display: block;
}

.search-suggestions.hidden {
    display: none;
}

.search-suggestion {
    padding: var(--spacing-sm) var(--spacing-md);
    cursor: pointer;
    transition: background var(--transition-fast);
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid var(--bg-secondary);
}

.search-suggestion:last-child {
    border-bottom: none;
}

.search-suggestion:hover,
.search-suggestion.highlighted {
    background: var(--bg-secondary);
}

.search-suggestion:focus {
    outline: 2px solid var(--primary-color);
    outline-offset: -2px;
}

.suggestion-name {
    font-weight: 500;
    color: var(--text-color);
}

.suggestion-name mark {
    background: #fff3cd;
    color: var(--text-color);
    font-weight: 600;
    padding: 0 2px;
    border-radius: 2px;
}

.suggestion-region {
    font-size: var(--font-size-sm);
    color: var(--text-secondary);
    margin-left: var(--spacing-sm);
}

/* ===================================
   Filter
   =================================== */

.filter-container {
    margin-bottom: var(--spacing-lg);
}

.filter-container label {
    display: block;
    margin-bottom: var(--spacing-sm);
    font-weight: 500;
    color: var(--text-secondary);
}

.filter-select {
    width: 100%;
    padding: var(--spacing-sm) var(--spacing-md);
    border: 1px solid var(--border-color);
    border-radius: 4px;
    font-size: var(--font-size-base);
    background: var(--bg-color);
    cursor: pointer;
}

.filter-select:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(51, 136, 255, 0.1);
}

/* ===================================
   Info Panel
   =================================== */

.info-panel {
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: var(--spacing-lg);
    position: relative;
}

.info-panel[hidden] {
    display: none;
}

.close-btn {
    position: absolute;
    top: var(--spacing-sm);
    right: var(--spacing-sm);
    width: 32px;
    height: 32px;
    border: none;
    background: transparent;
    font-size: 24px;
    line-height: 1;
    cursor: pointer;
    color: var(--text-secondary);
    transition: color var(--transition-fast);
}

.close-btn:hover {
    color: var(--danger-color);
}

.close-btn:focus {
    outline: 2px solid var(--primary-color);
    outline-offset: 2px;
    border-radius: 4px;
}

.country-header {
    display: flex;
    align-items: center;
    gap: var(--spacing-md);
    margin-bottom: var(--spacing-lg);
    padding-right: var(--spacing-xl);
}

.country-flag {
    width: 32px;
    height: 24px;
    border: 1px solid var(--border-color);
    border-radius: 2px;
    object-fit: cover;
    flex-shrink: 0;
}

.info-content h2 {
    font-size: var(--font-size-xl);
    margin: 0;
    color: var(--primary-color);
    flex: 1;
}

.country-details {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: var(--spacing-sm) var(--spacing-md);
    margin-bottom: var(--spacing-lg);
}

.country-details dt {
    font-weight: 600;
    color: var(--text-secondary);
}

.country-details dd {
    color: var(--text-color);
    margin: 0;
}

.show-on-map-btn {
    width: 100%;
    margin-top: var(--spacing-md);
    padding: var(--spacing-md);
    font-size: var(--font-size-base);
    font-weight: 600;
}

/* ===================================
   Country Chart (SVG)
   =================================== */

.country-chart {
    margin: var(--spacing-lg) 0;
    padding: var(--spacing-md);
    background: var(--bg-color);
    border-radius: 8px;
    border: 1px solid var(--border-color);
}

.chart-title {
    font-size: var(--font-size-base);
    font-weight: 600;
    color: var(--text-color);
    margin: 0 0 var(--spacing-sm) 0;
}

.country-chart svg {
    width: 100%;
    height: auto;
}

.chart-label {
    font-size: 12px;
    fill: var(--text-secondary);
    font-weight: 500;
}

.chart-value {
    font-size: 11px;
    fill: var(--text-color);
    font-weight: 600;
}

.chart-note {
    font-size: 9px;
    fill: var(--text-secondary);
    font-style: italic;
}

/* ===================================
   Map Container
   =================================== */

.map-container {
    flex: 1;
    position: relative;
    overflow: hidden;
}

.map {
    width: 100%;
    height: 100%;
}

/* ===================================
   Choropleth Legend
   =================================== */

.legend {
    position: absolute;
    bottom: var(--spacing-lg);
    right: var(--spacing-lg);
    background: rgba(255, 255, 255, 0.95);
    padding: var(--spacing-md);
    border-radius: 8px;
    box-shadow: var(--shadow-md);
    z-index: 1000;
    min-width: 200px;
}

.legend[hidden] {
    display: none;
}

.legend h3 {
    font-size: var(--font-size-base);
    margin: 0 0 var(--spacing-sm) 0;
    color: var(--text-color);
    font-weight: 600;
}

.legend-scale {
    display: flex;
    flex-direction: column;
    gap: var(--spacing-xs);
}

.legend-item {
    display: flex;
    align-items: center;
    gap: var(--spacing-sm);
    font-size: var(--font-size-sm);
}

.legend-color {
    width: 24px;
    height: 16px;
    border: 1px solid var(--border-color);
    border-radius: 2px;
    flex-shrink: 0;
}

.legend-label {
    color: var(--text-color);
    line-height: 1.2;
}

/* ===================================
   Error Container
   =================================== */

.error-container {
    position: fixed;
    top: calc(var(--header-height) + var(--spacing-md));
    left: 50%;
    transform: translateX(-50%);
    background: var(--danger-color);
    color: white;
    padding: var(--spacing-md) var(--spacing-lg);
    border-radius: 8px;
    box-shadow: var(--shadow-lg);
    z-index: 10000;
    max-width: 500px;
}

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

/* ===================================
   Screen Reader Only
   =================================== */

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* ===================================
   Desktop Layout (≥1024px)
   =================================== */

@media (min-width: 1024px) {
    .app-container {
        flex-direction: row;
        flex-wrap: wrap;
    }
    
    .app-header {
        width: 100%;
    }
    
    .sidebar {
        height: calc(100vh - var(--header-height));
    }
    
    .map-container {
        width: calc(100% - var(--sidebar-width));
        height: calc(100vh - var(--header-height));
    }
    
    .bottom-sheet {
        display: none !important;
    }
}

/* ===================================
   Tablet Layout (768px - 1023px)
   =================================== */

@media (min-width: 768px) and (max-width: 1023px) {
    :root {
        --sidebar-width: 280px;
    }
    
    .app-header h1 {
        font-size: var(--font-size-lg);
    }
    
    .bottom-sheet {
        display: none !important;
    }
}

/* ===================================
   Mobile Layout (≤767px)
   =================================== */

@media (max-width: 767px) {
    .app-header h1 {
        font-size: var(--font-size-lg);
    }
    
    .header-controls {
        gap: var(--spacing-xs);
    }
    
    .btn {
        padding: var(--spacing-xs) var(--spacing-sm);
        font-size: 0.75rem;
    }
    
    .sidebar {
        display: none;
    }
    
    .map-container {
        width: 100%;
        height: calc(100vh - var(--header-height));
    }
    
    /* Bottom Sheet */
    .bottom-sheet {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        background: var(--bg-color);
        border-radius: 16px 16px 0 0;
        box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.15);
        z-index: 2000;
        max-height: 70vh;
        overflow-y: auto;
        transform: translateY(100%);
        transition: transform var(--transition-base);
    }
    
    .bottom-sheet.visible {
        transform: translateY(0);
    }
    
    .bottom-sheet[hidden] {
        display: none;
    }
    
    .bottom-sheet-handle {
        width: 40px;
        height: 4px;
        background: var(--border-color);
        border-radius: 2px;
        margin: var(--spacing-sm) auto;
    }
    
    .bottom-sheet-content {
        padding: var(--spacing-md) var(--spacing-lg);
    }
}

/* ===================================
   Print Styles
   =================================== */

@media print {
    .app-header,
    .sidebar,
    .search-container,
    .filter-container,
    .header-controls,
    .close-btn,
    .legend,
    .bottom-sheet,
    .leaflet-control-zoom,
    .leaflet-control-attribution {
        display: none !important;
    }
    
    .map-container {
        width: 100%;
        height: 60vh;
        page-break-after: avoid;
    }
    
    .info-panel {
        page-break-inside: avoid;
        position: static !important;
        width: 100% !important;
        margin-top: 20px;
        border: 2px solid #dee2e6;
        box-shadow: none;
    }
    
    .info-panel[hidden] {
        display: block !important;
    }
    
    .country-header {
        border-bottom: 2px solid #3388ff;
        padding-bottom: 10px;
        margin-bottom: 15px;
    }
    
    .country-details {
        font-size: 12pt;
        line-height: 1.6;
    }
    
    .country-chart {
        page-break-inside: avoid;
    }
    
    .show-on-map-btn {
        display: none !important;
    }
    
    body::before {
        content: "Country Information Map - Printed " attr(data-print-date);
        display: block;
        font-size: var(--font-size-lg);
        font-weight: 600;
        margin-bottom: var(--spacing-lg);
        padding: 10px;
        background: #f8f9fa;
        border-left: 4px solid #3388ff;
    }
    
    /* Ensure colors print */
    * {
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }
}

/* ===================================
   Focus Indicators (Accessibility)
   =================================== */

*:focus {
    outline: 2px solid var(--primary-color);
    outline-offset: 2px;
}

button:focus,
input:focus,
select:focus {
    outline-offset: 2px;
}

/* ===================================
   Leaflet Map Overrides
   =================================== */

.leaflet-container {
    font-family: var(--font-family);
}

.leaflet-popup-content-wrapper {
    border-radius: 8px;
}

.leaflet-popup-content {
    margin: var(--spacing-md);
}

/* Country hover tooltip */
.country-tooltip {
    background: rgba(0, 0, 0, 0.85);
    color: white;
    border: none;
    border-radius: 4px;
    padding: var(--spacing-xs) var(--spacing-sm);
    font-size: var(--font-size-sm);
    font-weight: 500;
    box-shadow: var(--shadow-md);
}

.country-tooltip::before {
    border-top-color: rgba(0, 0, 0, 0.85);
}

/* Ensure map takes full height */
.leaflet-container {
    height: 100%;
    width: 100%;
}

/* ===================================
   Capital Markers & Clustering
   =================================== */

/* Marker cluster styling */
.marker-cluster {
    background-color: rgba(51, 136, 255, 0.6);
    border-radius: 50%;
    text-align: center;
    font-weight: 600;
    color: white;
}

.marker-cluster div {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background-color: rgba(51, 136, 255, 0.8);
    display: flex;
    align-items: center;
    justify-content: center;
}

.marker-cluster-small {
    background-color: rgba(51, 136, 255, 0.6);
}

.marker-cluster-small div {
    background-color: rgba(51, 136, 255, 0.8);
}

.marker-cluster-medium {
    background-color: rgba(255, 120, 0, 0.6);
}

.marker-cluster-medium div {
    background-color: rgba(255, 120, 0, 0.8);
}

.marker-cluster-large {
    background-color: rgba(220, 53, 69, 0.6);
}

.marker-cluster-large div {
    background-color: rgba(220, 53, 69, 0.8);
}

/* Capital popup styling */
.capital-popup {
    text-align: center;
    padding: var(--spacing-sm);
    min-width: 150px;
}

.capital-popup h3 {
    font-size: var(--font-size-lg);
    margin: 0 0 var(--spacing-xs) 0;
    color: var(--primary-color);
    font-weight: 600;
}

.capital-country {
    font-size: var(--font-size-sm);
    color: var(--text-secondary);
    margin: 0 0 var(--spacing-md) 0;
}

.capital-info-btn {
    width: 100%;
    padding: var(--spacing-sm) var(--spacing-md);
    font-size: var(--font-size-sm);
}

/* Leaflet popup overrides for capital markers */
.leaflet-popup-content {
    margin: var(--spacing-sm);
}

.leaflet-popup-content-wrapper {
    border-radius: 8px;
    box-shadow: var(--shadow-md);
}

.leaflet-popup-tip {
    box-shadow: var(--shadow-sm);
}


/* ===================================
   Choropleth Legend
   =================================== */

.legend {
    position: absolute;
    bottom: 30px;
    right: 10px;
    background: white;
    padding: 15px;
    border-radius: 8px;
    box-shadow: var(--shadow-lg);
    z-index: 1000;
    min-width: 200px;
    max-width: 280px;
}

.legend h3 {
    margin: 0 0 10px 0;
    font-size: var(--font-size-base);
    color: var(--text-color);
    font-weight: 600;
}

.legend-scale {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 10px;
}

.legend-color {
    width: 30px;
    height: 20px;
    border-radius: 3px;
    border: 1px solid rgba(0, 0, 0, 0.1);
    flex-shrink: 0;
}

.legend-label {
    font-size: var(--font-size-sm);
    color: var(--text-secondary);
    line-height: 1.2;
}

/* Mobile adjustments for legend */
@media (max-width: 768px) {
    .legend {
        bottom: 10px;
        right: 10px;
        left: 10px;
        max-width: none;
        padding: 12px;
    }
    
    .legend h3 {
        font-size: var(--font-size-sm);
    }
    
    .legend-label {
        font-size: 0.75rem;
    }
    
    .legend-color {
        width: 25px;
        height: 18px;
    }
}
