/* ============================================================================
   DESIGN SYSTEM & CSS VARIABLES
   ============================================================================ */

:root {
    /* --- Colors --- */
    --color-background-light: #f8f9fa;
    --color-background-gray: #f0f0f0;
    --color-background-white: #ffffff;
    --color-background-dark: #343a40;
    --color-border: #ddd;
    --color-border-light: #ccc;
    --color-text-dark: #333;
    --color-text-muted: #555;
    --color-shadow: rgba(0, 0, 0, 0.1);
    --color-shadow-strong: rgba(0, 0, 0, 0.2);

    /* --- Spacing & Sizing --- */
    --spacing-xs: 5px;
    --spacing-sm: 10px;
    --spacing-md: 15px;
    --spacing-lg: 20px;
    --spacing-xl: 30px;

    --border-radius: 4px;
    --box-shadow: 0 2px 5px var(--color-shadow);
    --box-shadow-strong: 0 2px 10px var(--color-shadow-strong);
    --opacity-overlay: 0.9;
    --opacity-tooltip: 0.95;

    /* --- Layout Dimensions --- */
    --sidebar-width: 250px;
    --header-height: 80px;

    /* --- Component Dimensions --- */
    --filter-box-width: clamp(200px, 20%, 300px);
    --filter-box-height: clamp(300px, 60%, 700px);
    --filter-box-background: var(--color-background-light);

    --regression-box-width: clamp(250px, 25%, 400px);
    --regression-box-height: clamp(200px, 35%, 350px);
    --regression-box-background: var(--color-background-white);

    --legend-box-width: clamp(250px, 25%, 400px);
    --legend-box-height: clamp(150px, 30%, 350px);
    --legend-box-background: var(--color-background-white);

    --timeline-box-width: clamp(250px, 25%, 400px);
    --timeline-box-height: clamp(100px, 20%, 200px);
    --timeline-box-background: var(--color-background-white);

    --navigation-box-height: 80px;
    --title-box-height: 80px;

    --center-div-background: var(--color-background-gray);
}

/* ============================================================================
   MAP CONTAINER
   ============================================================================ */

#map {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 1;
}


/* ============================================================================
   MAIN LAYOUT COMPONENTS
   ============================================================================ */

/* --- Header Container (holds Navigation + Title side by side) --- */
.header-container {
    position: absolute;
    top: 0;
    left: var(--sidebar-width);
    width: calc(100% - var(--sidebar-width));
    height: var(--navigation-box-height);
    display: flex;
    align-items: center;
    background-color: var(--color-background-white);
    border-bottom: 1px solid var(--color-border);
    z-index: 100;
}

/* --- Navigation Bar --- */
.navigation {
    flex-shrink: 0;
    padding: 0 var(--spacing-lg);
    color: black;
}

.navigation h1 {
    margin: 0;
    font-size: clamp(14px, 2vw, 24px);
    white-space: nowrap;
}

/* --- Page Title --- */
.title {
    flex: 1;
    overflow: hidden;
    padding: 0 var(--spacing-lg);
    display: flex;
    justify-content: flex-end;
    align-items: center;
}

.title h2 {
    max-width: 100%;
    margin: 0;
    font-size: clamp(14px, 2vw, 24px);
    text-align: center;
    line-height: 1.2;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

/* --- Main Content Area --- */
.center-div {
    position: fixed;
    top: var(--navigation-box-height);
    left: var(--sidebar-width);
    width: calc(100vw - var(--sidebar-width));
    height: calc(100vh - var(--navigation-box-height));
    background-color: var(--center-div-background);
    display: flex;
}

/* ============================================================================
   FLOATING PANELS (Filter, Regression, Legend, Timeline)
   ============================================================================ */

/* Shared panel styling for consistent appearance and behavior */
.filter-box,
.regression-box,
.legend-box,
.timeline-box {
    border: 1px solid var(--color-border);
    border-radius: var(--border-radius);
    box-shadow: var(--box-shadow);
    padding: var(--spacing-sm);
    opacity: var(--opacity-overlay);
    z-index: 1000;
}

/* --- Filter Panel (Left, Top) --- */
.filter-box {
    position: absolute;
    top: var(--spacing-lg);
    left: var(--spacing-lg);
    width: var(--filter-box-width);
    height: auto;
    max-height: var(--filter-box-height);
    background-color: var(--filter-box-background);
    overflow-y: auto;
}

.filter-box h3 {
    margin: 5px 0 10px 0;
    font-size: clamp(12px, 1.2vw, 16px);
}

/* --- Regression Plot Panel (Right, Bottom) --- */
.regression-box {
    position: absolute;
    width: var(--regression-box-width);
    height: var(--regression-box-height);
    right: var(--spacing-lg);
    bottom: var(--spacing-lg);
    background-color: var(--regression-box-background);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* --- Legend Panel (Right, Top) --- */
.legend-box {
    position: absolute;
    width: var(--legend-box-width);
    height: fit-content;
    max-height: var(--legend-box-height);
    right: var(--spacing-lg);
    top: var(--spacing-lg);
    background-color: var(--legend-box-background);
    display: flex;
    flex-direction: column;
    overflow-y: auto;
}

#legend {
    flex: 1;
    display: flex;
    flex-direction: column;
}

/* --- Timeline / Year Selection Panel (Left, Bottom) --- */
.timeline-box {
    position: absolute;
    width: fit-content;
    max-width: var(--timeline-box-width);
    height: fit-content;
    max-height: var(--timeline-box-height);
    left: var(--spacing-lg);
    bottom: var(--spacing-lg);
    background-color: var(--timeline-box-background);
}

.timeline-box h3 {
    margin: 5px 0;
    font-size: clamp(11px, 1.1vw, 14px);
}

.timeline-box select {
    width: 100%;
    padding: 4px;
    font-size: clamp(10px, 1vw, 12px);
    margin-bottom: 8px;
}

/* ============================================================================
   FORM CONTROLS & FILTERS
   ============================================================================ */

/* --- Filter Group Container --- */
.filter-group {
    margin-bottom: 8px;
}

/* --- Filter Labels --- */
.filter-group label {
    display: block;
    margin-bottom: 2px;
    font-size: clamp(10px, 1vw, 13px);
    font-weight: bold;
    color: var(--color-text-dark);
}

/* --- Filter Dropdowns & Select Elements --- */
.filter-group select {
    width: 100%;
    padding: 4px;
    border: 1px solid var(--color-border-light);
    border-radius: var(--border-radius);
    font-size: clamp(10px, 1vw, 12px);
    color: var(--color-text-dark);
}

/* --- Factor Multi-select (Extended height) --- */
#factor-select {
    height: 15vh;
}

/* --- Filter Group Disabled State --- */
.filter-group.disabled select {
    opacity: 0.6;
    cursor: not-allowed;
    pointer-events: none;
}

/* --- Hidden Filter Group (PCA Selection) --- */
#pc-selection-group {
    display: none;
}

#pc-selection-group.visible {
    display: block;
}

#pc-select {
    min-height: 10vh;
}

#biplot-selection-group {
    display: none;
}

#biplot-selection-group.visible {
    display: block;
}

.biplot-selects {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: var(--spacing-sm);
}

/* ============================================================================
   TOOLTIPS & POPUPS
   ============================================================================ */

/* --- Map Tooltip (for feature information) --- */
.map-tooltip {
    background-color: rgba(255, 255, 255, var(--opacity-tooltip));
    border: 1px solid var(--color-border);
    border-radius: var(--border-radius);
    box-shadow: var(--box-shadow-strong);
    padding: var(--spacing-sm);
    font-family: Arial, sans-serif;
    max-width: 350px;
    min-width: 200px;
    width: max-content;
    white-space: normal;
    word-wrap: break-word;
}

.map-tooltip h4 {
    margin: 0 0 8px 0;
    font-size: 16px;
    color: var(--color-text-dark);
}

.map-tooltip p {
    margin: 0;
    font-size: 14px;
    color: var(--color-text-muted);
}

/* --- Generic Tooltip (for hover information) --- */
.tooltip {
    position: absolute;
    background-color: rgba(255, 255, 255, var(--opacity-tooltip));
    border: 1px solid var(--color-border);
    border-radius: var(--border-radius);
    box-shadow: var(--box-shadow-strong);
    padding: 8px;
    font-family: Arial, sans-serif;
    pointer-events: none;
    z-index: 1500;
}

/* ============================================================================
   INTERACTIVE ELEMENTS
   ============================================================================ */

/* --- Map Feature Hover --- */
.leaflet-interactive:hover {
    cursor: pointer;
}


/* ============================================================================
   PLOT CONTAINERS
   ============================================================================ */

/* --- Regression Plot Area --- */
#regression-plot {
    margin-top: var(--spacing-sm);
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
}

#regression-plot svg {
    width: 100%;
    height: 100%;
    display: block;
}

/* --- Map Wrapper Container --- */
.map-wrapper {
    position: relative;
    height: 100%;
    width: 100%;
}

/* --- Bottom Dashboard Section (Table & Heatmap) --- */
.dashboard-bottom {
    display: flex;
    gap: var(--spacing-lg);
    padding: var(--spacing-lg);
    flex-wrap: wrap;
    align-items: flex-start;
}

/* --- Table Container --- */
#table-container {
    background-color: var(--color-background-white);
    padding: var(--spacing-lg);
    position: relative;
    z-index: 10;
    min-height: 400px;
    flex: 1 1 30%;
    min-width: 320px;
}

/* --- PCA/BiPlot Container Wrapper --- */
#PCA-container-wrapper {
    background-color: var(--color-background-white);
    min-height: 400px;
    flex: 1 1 35%;
    min-width: 420px;
    max-width: 48%;
    display: flex;
    flex-direction: column;
    border: 1px solid var(--color-border);
    border-radius: var(--border-radius);
    box-shadow: var(--box-shadow);
}

/* --- PCA View Toggle Buttons --- */
.pca-view-toggle {
    display: flex;
    border-bottom: 1px solid var(--color-border);
    background-color: #f5f5f5;
}

.view-btn {
    flex: 1;
    padding: var(--spacing-sm) var(--spacing-md);
    background: none;
    border: none;
    border-bottom: 3px solid transparent;
    cursor: pointer;
    font-size: 13px;
    font-weight: 500;
    color: var(--color-text-muted);
    transition: all 0.2s ease;
}

.view-btn:hover {
    background-color: #efefef;
    color: var(--color-text-dark);
}

.view-btn.active {
    color: #2196F3;
    border-bottom-color: #2196F3;
    background-color: var(--color-background-white);
}

/* --- Heatmap & BiPlot Views --- */
.pca-view {
    flex: 1;
    padding: var(--spacing-lg);
    display: none;
    flex-direction: column;
    align-items: center;
    overflow-y: auto;
}

.pca-view.active {
    display: flex;
}

#PCA-container,
#biplot-container {
    min-height: 300px;
}

/* --- Loading State Messages --- */
.loading-message,
.error-message,
.empty-state-message {
    text-align: center;
    padding: var(--spacing-lg);
}

.loading-message {
    color: var(--color-text-muted);
}

.error-message {
    color: #d32f2f;
}

.empty-state-message {
    color: var(--color-text-muted);
}

/* --- Center Div Content Area --- */
.center-div {
    display: block;
    overflow-y: auto;
}


/* ============================================================================
   DATA TABLE STYLES
   ============================================================================ */

/* --- Table Card Container --- */
.table-card {
    width: 100%;
    border: 1px solid var(--color-border);
    background: var(--color-background-white);
    border-radius: var(--border-radius);
    box-shadow: var(--box-shadow);
    margin: var(--spacing-md) 0;
    display: flex;
    flex-direction: column;
}

/* --- Table Header --- */
.table-header {
    background: var(--color-background-light);
    padding: var(--spacing-sm) var(--spacing-md);
    border-bottom: 1px solid var(--color-border);
    font-weight: bold;
    color: var(--color-text-dark);
}

.table-header strong {
    display: block;
    margin-bottom: 4px;
}

.table-header-info {
    font-weight: normal;
    margin-left: var(--spacing-sm);
    color: var(--color-text-muted);
    font-size: 13px;
}

/* --- Table Body Scrolling --- */
.table-body {
    max-height: 500px;
    overflow-y: auto;
}

/* --- Data Table Base Styles --- */
.data-table {
    width: 100%;
    border-collapse: collapse;
    font-family: sans-serif;
    font-size: 14px;
}

/* --- Table Cells --- */
.data-table th,
.data-table td {
    padding: var(--spacing-sm) var(--spacing-md);
    border-bottom: 1px solid #eee;
    text-align: left;
    color: var(--color-text-dark);
}

/* --- Table Headers (Sticky) --- */
.data-table th {
    background: var(--color-background-white);
    position: sticky;
    top: 0;
    border-bottom: 2px solid var(--color-border);
    font-weight: 600;
    z-index: 1;
    box-shadow: 0 2px 2px -1px rgba(0, 0, 0, 0.1);
    cursor: pointer;
    user-select: none;
    transition: background-color 0.2s ease;
}

.data-table th:hover {
    background-color: #f0f0f0;
}

/* --- Table Rows (Alternating Background) --- */
.data-table tr:nth-child(even) {
    background-color: #f9f9f9;
}

.data-table tr:hover {
    background-color: #f0f0f0;
}

/* --- Sort Icon --- */
.sort-icon {
    margin-left: var(--spacing-xs);
    font-size: 0.8em;
    color: #888;
}


/* ============================================================================
   BUTTONS & ACTION CONTROLS
   ============================================================================ */

/* --- Button Base Styles --- */
button {
    padding: 4px 8px;
    cursor: pointer;
    border: none;
    border-radius: var(--border-radius);
    font-size: clamp(11px, 1vw, 13px);
    font-weight: 500;
    transition: background-color 0.3s ease, opacity 0.3s ease;
}

/* --- Primary Action Button (Visualize) --- */
#visualize-btn {
    background-color: #4CAF50;
    color: white;
    margin-right: var(--spacing-sm);
}

#visualize-btn:hover {
    background-color: #45a049;
}

/* --- Secondary Action Button (Run PCA) --- */
#run-pca-btn {
    background-color: #2196F3;
    color: white;
}

#run-pca-btn:hover {
    background-color: #0b7dda;
}

/* --- Button Disabled State --- */
#run-pca-btn.disabled {
    background-color: #f44336;
    cursor: not-allowed;
    opacity: 0.7;
}

/* --- Button Alignment Group --- */
.button-group {
    text-align: right;
}
