/* =============================================================================
   SCENARIO ANALYSIS - MAIN STYLES
   ============================================================================= */

.scenario-analysis-wrapper {
    max-width: 1400px;
    margin: 0 auto;
    padding: 20px;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
}

/* Header */
.sa-header {
    text-align: center;
    margin-bottom: 40px;
    padding: 30px;
    background: linear-gradient(135deg, #10b981 0%, #059669 100%);
    color: white;
    border-radius: 10px;
}

.sa-header h1 {
    margin: 0 0 10px 0;
    font-size: 2.5em;
}

.sa-subtitle {
    margin: 0;
    font-size: 1.1em;
    opacity: 0.9;
}

/* Section Structure */
.sa-section {
    background: white;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
    margin-bottom: 30px;
    overflow: hidden;
}

.section-title {
    background: #f8f9fa;
    padding: 20px 25px;
    cursor: pointer;
    display: flex;
    justify-content: space-between;
    align-items: center;
    user-select: none;
    border-bottom: 1px solid #e0e0e0;
    transition: background 0.2s;
}

.section-title:hover {
    background: #e9ecef;
}

.section-title h2 {
    margin: 0;
    font-size: 1.5em;
    color: #333;
}

.toggle-icon {
    font-size: 1.2em;
    transition: transform 0.3s;
}

.section-title.collapsed .toggle-icon {
    transform: rotate(-90deg);
}

.section-content {
    padding: 25px;
    transition: max-height 0.3s ease-out;
}

.section-title.collapsed + .section-content {
    display: none;
}

/* Control Panels */
.sa-control-panel,
.mc-control-panel {
    display: flex;
    flex-direction: column;
    gap: 20px;
    margin-bottom: 30px;
    padding: 20px;
    background: #f8f9fa;
    border-radius: 8px;
}

.control-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.control-group label {
    font-weight: 600;
    margin-bottom: 0;
    color: #333;
    font-size: 0.95em;
}

/* Compact row layout for filters */
.control-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 20px;
    align-items: start;
}

/* Scenario Selector */
.scenario-selector-wrapper {
    display: flex;
    gap: 10px;
    align-items: stretch;
}

#scenario-selector {
    flex: 1;
    padding: 8px;
    border: 1px solid #ddd;
    border-radius: 4px;
    font-size: 0.9em;
    min-height: 120px;
}

#vessel-type-filter {
    width: 100%;
    max-width: 100%;
    padding: 8px 12px;
    border: 1px solid #ddd;
    border-radius: 4px;
    font-size: 0.9em;
    height: 38px;
    cursor: pointer;
    background-color: white;
}

#vessel-type-filter:focus {
    outline: none;
    border-color: #10b981;
    box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.1);
}

#scenario-selector option {
    padding: 8px;
    cursor: pointer;
}

#scenario-selector option:hover {
    background: #e3f2fd;
}

.selector-buttons {
    display: flex;
    flex-direction: column;
    gap: 8px;
    min-width: 80px;
}

.selector-buttons button {
    padding: 6px 12px;
    font-size: 0.85em;
}

/* Variable Checkboxes */
.variable-selector-wrapper {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.variable-hint {
    padding: 8px 12px;
    background: #fff3cd;
    border: 1px solid #ffc107;
    border-radius: 4px;
    color: #856404;
    font-size: 0.85em;
    line-height: 1.4;
}

.variable-checkboxes {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
    gap: 8px;
    padding: 12px;
    background: white;
    border: 1px solid #ddd;
    border-radius: 4px;
    max-height: 150px;
    overflow-y: auto;
}

.variable-checkboxes label {
    display: flex;
    align-items: center;
    gap: 6px;
    cursor: pointer;
    padding: 4px;
    border-radius: 3px;
    transition: background 0.2s;
    font-size: 0.85em;
}

.variable-checkboxes label:hover {
    background: #f0f0f0;
}

.variable-checkboxes input[type="checkbox"] {
    cursor: pointer;
    width: 16px;
    height: 16px;
    flex-shrink: 0;
}

/* Date Range Filter */
.date-range-wrapper {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
}

.date-range-wrapper input[type="month"] {
    flex: 0 1 150px;
    padding: 8px 12px;
    border: 1px solid #ddd;
    border-radius: 4px;
    font-size: 0.9em;
    height: 38px;
}

.date-range-wrapper span {
    flex-shrink: 0;
    color: #666;
    font-size: 0.9em;
}

.date-range-wrapper button {
    flex-shrink: 0;
}

/* Buttons */
.btn-primary,
.btn-secondary {
    padding: 8px 16px;
    border: none;
    border-radius: 4px;
    font-size: 0.9em;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    white-space: nowrap;
    height: 38px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.btn-primary {
    background: #10b981;
    color: white;
}

.btn-primary:hover {
    background: #5568d3;
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
}

.btn-secondary {
    background: #6c757d;
    color: white;
}

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

.btn-large {
    padding: 15px 40px;
    font-size: 1.1em;
}

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

/* Loaded Scenarios Info */
.loaded-scenarios-info {
    margin-bottom: 20px;
    padding: 15px;
    background: #e3f2fd;
    border-left: 4px solid #10b981;
    border-radius: 4px;
}

.loaded-scenarios-info h3 {
    margin: 0 0 10px 0;
    font-size: 1.1em;
    color: #333;
}

#scenario-badges {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
}

.scenario-badge {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 6px 12px;
    background: white;
    border: 1px solid #10b981;
    border-radius: 20px;
    font-size: 0.9em;
}

.scenario-badge .remove-btn {
    cursor: pointer;
    color: #dc3545;
    font-weight: bold;
}

/* Chart Container */
.chart-container {
    position: relative;
    width: 100%;
    min-height: 500px;  /* Ensure minimum height */
    height: 60vh;       /* Use viewport height for responsiveness */
    max-height: 700px;  /* Prevent it from getting too tall */
    margin: 20px 0;
    padding: 30px 20px 40px 20px;  /* Extra bottom padding */
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    display: flex;
    flex-direction: column;
}

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

/* Tables */
.table-container {
    margin: 30px 0;
}

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

.table-container h3 {
    margin-bottom: 15px;
    color: #333;
}

#freight-rate-table,
#risk-variables-table {
    overflow-x: auto;
}

/* Ensure canvas fills container properly */
.chart-container canvas {
    flex: 1;
    min-height: 0;  /* Important for flex child */
}

/* Monte Carlo chart containers */
/* Monte Carlo section headers */
.mc-distribution-section h3,
.mc-confidence-section h3,
.mc-sensitivity-section h3,
.mc-correlation-section h3,
.mc-timeseries-section h3,
.mc-metrics-section h3 {
    margin-bottom: 20px;
    color: #333;
    font-size: 1.5em;
}

/* Chart sections with canvas elements */
.mc-distribution-section,
.mc-confidence-section,
.mc-sensitivity-section,
.mc-timeseries-section {
    margin: 40px 0;
    padding: 30px 20px;
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

/* Chart wrappers - contain the canvas */
.chart-wrapper {
    position: relative;
    width: 100%;
    height: 500px;
    min-height: 400px;
}

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

/* Distribution chart - slightly taller for histogram */
.mc-distribution-section .chart-wrapper {
    height: 450px;
}

/* Confidence bands - taller for time series */
.mc-confidence-section .chart-wrapper {
    height: 550px;
}

/* Sensitivity - horizontal bar chart */
.mc-sensitivity-section .chart-wrapper {
    height: 400px;
}

/* Time series - tallest for multiple paths */
.mc-timeseries-section .chart-wrapper {
    height: 600px;
}

/* Correlation matrix section - no canvas, just table */
.mc-correlation-section {
    margin: 40px 0;
}

.mc-correlation-section #correlation-matrix {
    overflow-x: auto;
    background: #fff;
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

/* Metrics section */
.mc-metrics-section {
    margin: 40px 0;
}

.mc-metrics-section h3 {
    margin-bottom: 20px;
    color: #333;
    font-size: 1.5em;
}

/* Responsive adjustments */
@media (max-width: 1200px) {
    .chart-container {
        min-height: 400px;
        height: 50vh;
    }
}

@media (max-width: 768px) {
    .chart-container {
        min-height: 350px;
        height: 45vh;
    }
    
    .chart-wrapper {
        height: 350px !important;
        min-height: 300px;
    }
}

@media (max-width: 480px) {
    .chart-container {
        min-height: 300px;
        height: 40vh;
        padding: 10px;
    }
    
    .chart-wrapper {
        height: 300px !important;
        min-height: 250px;
    }
}

/* Fix for section content */
.section-content {
    padding: 20px;
    overflow: visible; /* Ensure charts aren't clipped */
}

/* Ensure parent containers don't restrict height */
.sa-section {
    overflow: visible;
}

.comparison-section,
.monte-carlo-section {
    overflow: visible;
}

/* Fix any potential flex/grid issues */
.scenario-analysis-wrapper {
    display: block;
    width: 100%;
}

.table-container h3 {
    margin-bottom: 15px;
    color: #333;
}

#freight-rate-table,
#risk-variables-table {
    overflow-x: auto;
}

table.comparison-table,
table.risk-config-table {
    width: 100%;
    border-collapse: collapse;
    background: white;
}

table.comparison-table th,
table.comparison-table td,
table.risk-config-table th,
table.risk-config-table td {
    padding: 12px;
    text-align: left;
    border-bottom: 1px solid #e0e0e0;
}

table.comparison-table th,
table.risk-config-table th {
    background: #f8f9fa;
    font-weight: 600;
    color: #333;
    position: sticky;
    top: 0;
}

table.comparison-table tr:hover,
table.risk-config-table tr:hover {
    background: #f8f9fa;
}

table.comparison-table td.number,
table.risk-config-table td.number {
    text-align: right;
    font-family: 'Courier New', monospace;
}

/* Monte Carlo Specific */
.mc-variables-config {
    margin: 30px 0;
    padding: 20px;
    background: #f8f9fa;
    border-radius: 8px;
}

.mc-variables-config h3 {
    margin-top: 0;
    margin-bottom: 10px;
    color: #333;
}

.config-hint {
    margin: 0 0 20px 0;
    color: #666;
    font-size: 0.9em;
}

.risk-config-table input[type="number"],
.risk-config-table select {
    width: 100%;
    padding: 6px 10px;
    border: 1px solid #ddd;
    border-radius: 4px;
    font-size: 0.9em;
}

.mc-actions {
    display: flex;
    align-items: center;
    gap: 20px;
    margin: 30px 0;
    padding: 20px;
    background: #f8f9fa;
    border-radius: 8px;
}

.mc-actions.hidden {
    display: none;
}

.progress-indicator {
    flex: 1;
}

.progress-bar {
    width: 100%;
    height: 8px;
    background: #e0e0e0;
    border-radius: 4px;
    overflow: hidden;
    margin-bottom: 5px;
}

.progress-bar::after {
    content: '';
    display: block;
    width: 0;
    height: 100%;
    background: linear-gradient(90deg, #10b981, #059669);
    animation: progress 2s ease-in-out infinite;
}

@keyframes progress {
    0% { width: 0; }
    50% { width: 70%; }
    100% { width: 100%; }
}

.progress-text {
    font-size: 0.9em;
    color: #666;
}

/* Monte Carlo Results */
.mc-results {
    margin-top: 40px;
    padding-top: 40px;
    border-top: 2px solid #e0e0e0;
}

.mc-summary {
    margin-bottom: 40px;
}

.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 20px;
    margin-top: 20px;
}

.stat-card {
    padding: 20px;
    background: white;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
    text-align: center;
}

.stat-label {
    display: block;
    font-size: 0.9em;
    color: #666;
    margin-bottom: 10px;
}

.stat-value {
    display: block;
    font-size: 2em;
    font-weight: 700;
    color: #10b981;
}

.risk-metrics-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
    gap: 20px;
}

.metric-card {
    padding: 25px;
    background: #f8f9fa;
    border-radius: 8px;
    min-width: 280px;
     max-height: none;
    overflow-y: auto;
     min-height: 350px;
}

.metric-card h4 {
    margin-top: 0;
    margin-bottom: 20px;
    color: #333;
    font-size: 1.1em;
}

.metric-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 0;
    border-bottom: 1px solid #e0e0e0;
    gap: 15px;
}

.metric-item:last-child {
    border-bottom: none;
}

.metric-label {
    color: #666;
    flex-shrink: 0;
    min-width: 120px;
}

.metric-value {
    font-weight: 600;
    color: #333;
    text-align: right;
    word-break: break-word;
    flex: 1;
}

/* Correlation Matrix */
#correlation-matrix {
    overflow-x: auto;
}

.correlation-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.85em;
}

.correlation-table th,
.correlation-table td {
    padding: 8px;
    text-align: center;
    border: 1px solid #e0e0e0;
}

.correlation-table th {
    background: #f8f9fa;
    font-weight: 600;
}

.correlation-cell {
    font-family: 'Courier New', monospace;
}

.correlation-strong-positive {
    background: rgba(76, 175, 80, 0.3);
}

.correlation-positive {
    background: rgba(76, 175, 80, 0.1);
}

.correlation-strong-negative {
    background: rgba(244, 67, 54, 0.3);
}

.correlation-negative {
    background: rgba(244, 67, 54, 0.1);
}

/* Responsive Design */
@media (max-width: 768px) {
    .sa-control-panel,
    .mc-control-panel {
        grid-template-columns: 1fr;
    }
    
    .scenario-selector-wrapper {
        flex-direction: column;
    }
    
    .stats-grid {
        grid-template-columns: 1fr;
    }
    
    .variable-checkboxes {
        grid-template-columns: 1fr;
    }
    
    .mc-actions {
        flex-direction: column;
        align-items: stretch;
    }
}

/* Loading States */
.loading {
    text-align: center;
    padding: 40px;
    color: #666;
}

.loading::after {
    content: '...';
    animation: dots 1.5s steps(4, end) infinite;
}

@keyframes dots {
    0%, 20% { content: '.'; }
    40% { content: '..'; }
    60%, 100% { content: '...'; }
}

/* Error States */
.error-message {
    padding: 15px;
    background: #ffebee;
    border-left: 4px solid #f44336;
    border-radius: 4px;
    color: #c62828;
    margin: 20px 0;
}

.success-message {
    padding: 15px;
    background: #e8f5e9;
    border-left: 4px solid #4caf50;
    border-radius: 4px;
    color: #2e7d32;
    margin: 20px 0;
}

/* =============================================================================
   HISTORICAL STATISTICS STYLING - ADD TO main-sa.css
   Enhanced styling for the risk configuration table with historical data
   ============================================================================= */

/* Historical statistics cells */
.historical-stat {
    background-color: #f0f8ff;
    font-size: 0.85em;
    color: #555;
    border-left: 2px solid #e3f2fd;
}

.historical-stat br {
    line-height: 1.6;
}

/* Historical info message */
.historical-info {
    margin-top: 15px;
    padding: 12px 15px;
    background: #e3f2fd;
    border-left: 4px solid #2196f3;
    border-radius: 4px;
    font-size: 0.9em;
    line-height: 1.5;
}

.historical-info.warning {
    background: #fff3cd;
    border-left-color: #ffc107;
    color: #856404;
}

.historical-info p {
    margin: 5px 0;
}

.historical-info strong {
    color: #1976d2;
}

.historical-info.warning strong {
    color: #856404;
}

/* Enhanced risk configuration table */
.risk-config-table {
    font-size: 0.9em;
}

.risk-config-table th {
    font-size: 0.85em;
    padding: 10px 8px;
    text-align: center;
    white-space: nowrap;
}

.risk-config-table td {
    vertical-align: middle;
}

/* Input fields in risk table */
.risk-config-table input[type="number"] {
    text-align: center;
    font-weight: 600;
}

.risk-config-table input[type="number"]:focus {
    background-color: #fffde7;
    border-color: #10b981;
}

/* Highlight auto-populated values */
.risk-config-table input[type="number"][title*="Suggested"] {
    border-left: 3px solid #4caf50;
}

.risk-config-table input[type="number"][title*="Suggested"]:focus {
    box-shadow: 0 0 0 2px rgba(76, 175, 80, 0.2);
}

/* Distribution type selector */
.risk-config-table select.distribution-type {
    font-size: 0.85em;
    padding: 6px;
}

/* Responsive adjustments for risk table */
@media (max-width: 1200px) {
    .risk-config-table {
        font-size: 0.8em;
    }
    
    .risk-config-table th,
    .risk-config-table td {
        padding: 6px 4px;
    }
    
    .risk-config-table input[type="number"],
    .risk-config-table select {
        padding: 4px 6px;
        font-size: 0.9em;
    }
}

@media (max-width: 768px) {
    /* Stack historical columns on mobile */
    .risk-config-table thead th:nth-child(3),
    .risk-config-table thead th:nth-child(4),
    .risk-config-table thead th:nth-child(5) {
        display: none;
    }
    
    .risk-config-table tbody td.historical-stat {
        display: none;
    }
    
    .historical-info {
        font-size: 0.8em;
    }
}

/* Tooltip styling for inputs with suggestions */
.risk-config-table input[title]:hover::after {
    content: attr(title);
    position: absolute;
    background: #333;
    color: white;
    padding: 5px 10px;
    border-radius: 4px;
    font-size: 0.8em;
    white-space: nowrap;
    z-index: 1000;
    margin-top: 25px;
    margin-left: -50px;
}

/* Loading state for risk table */
.risk-config-table.loading {
    opacity: 0.6;
    pointer-events: none;
}

.risk-config-table.loading::after {
    content: 'Loading historical data...';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: white;
    padding: 20px 40px;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.2);
    font-weight: 600;
    color: #10b981;
}

/* Highlight table header groups */
.risk-config-table thead tr th {
    border-bottom: 2px solid #e0e0e0;
}

/* Group historical columns visually */
.risk-config-table thead th:nth-child(3),
.risk-config-table thead th:nth-child(4),
.risk-config-table thead th:nth-child(5) {
    background-color: #f0f8ff;
    border-left: 2px solid #e3f2fd;
}

/* Group risk config columns visually */
.risk-config-table thead th:nth-child(6),
.risk-config-table thead th:nth-child(7),
.risk-config-table thead th:nth-child(8) {
    background-color: #fff8e1;
    border-left: 2px solid #ffecb3;
}

/* Alternating row colors for better readability */
.risk-config-table tbody tr:nth-child(even) {
    background-color: #fafafa;
}

.risk-config-table tbody tr:hover {
    background-color: #f5f5f5;
}

/* Variable name column */
.risk-config-table tbody td:first-child {
    font-weight: 600;
    color: #333;
    position: sticky;
    left: 0;
    background-color: inherit;
    z-index: 1;
}

/* Current value column */
.risk-config-table tbody td:nth-child(2) {
    font-family: 'Courier New', monospace;
    color: #1976d2;
    font-weight: 600;
}

/* Success indicator for loaded historical data */
.historical-info.success {
    background: #e8f5e9;
    border-left-color: #4caf50;
    color: #2e7d32;
}

.historical-info.success strong {
    color: #2e7d32;
}

/* Icon indicators */
.historical-info::before {
    content: 'â„¹ï¸';
    margin-right: 8px;
    font-size: 1.2em;
}

.historical-info.warning::before {
    content: 'âš ï¸';
}

.historical-info.success::before {
    content: 'âœ“';
}

/* Enhanced table container */
.mc-variables-config {
    position: relative;
}

/* Data quality indicator */
.data-quality-badge {
    display: inline-block;
    padding: 3px 8px;
    border-radius: 12px;
    font-size: 0.75em;
    font-weight: 600;
    margin-left: 10px;
}

.data-quality-badge.high {
    background: #e8f5e9;
    color: #2e7d32;
}

.data-quality-badge.medium {
    background: #fff3cd;
    color: #856404;
}

.data-quality-badge.low {
    background: #ffebee;
    color: #c62828;
}

/* Percentile indicator bars (optional enhancement) */
.percentile-bar {
    height: 4px;
    background: linear-gradient(to right, #f44336 0%, #ffc107 25%, #4caf50 50%, #ffc107 75%, #f44336 100%);
    border-radius: 2px;
    margin: 5px 0;
    position: relative;
}

.percentile-bar::before {
    content: '';
    position: absolute;
    width: 2px;
    height: 8px;
    background: #333;
    top: -2px;
    left: 50%;
    transform: translateX(-50%);
}

/* Print styles */
@media print {
    .historical-info {
        page-break-inside: avoid;
    }
    
    .risk-config-table {
        page-break-inside: avoid;
    }
    
    .risk-config-table input,
    .risk-config-table select {
        border: 1px solid #333;
    }
}