/* Component-specific styles - REDESIGNED */

/* Loading Spinner */
.loading {
    display: inline-block;
    width: 20px;
    height: 20px;
    border: 3px solid #f3f3f3;
    border-top: 3px solid var(--accent-color);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

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

/* Demo Page Layout - Clean & Simple */
.demo-page {
    min-height: 100vh;
    background-color: var(--background-color);
}

.demo-header {
    background-color: var(--light-gray);
    padding: 0.75rem 0;
    border-bottom: 1px solid var(--border-color);
    position: sticky;
    top: 0;
    z-index: 100;
}

.header-horizontal {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.header-content {
    display: flex;
    align-items: baseline;
    gap: 1rem;
}

.demo-nav {
    flex-shrink: 0;
}

.demo-nav a {
    color: var(--accent-color);
    text-decoration: none;
    font-weight: 500;
}

.demo-nav a:hover {
    text-decoration: underline;
}

.demo-content {
    padding: 2rem 20px;
    max-width: 1200px;
    margin: 0 auto;
    display: grid;
    grid-template-columns: 320px 1fr;
    gap: 2rem;
    height: calc(100vh - 120px);
}

/* Left Panel - Controls */
.controls-panel {
    background: white;
    border-radius: 12px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.07);
    border: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    max-height: 100%;
    width: 320px;
    min-width: 320px;
    flex-shrink: 0;
}

.controls-header {
    padding: 1.5rem 1.5rem 1rem 1.5rem;
    border-bottom: 1px solid var(--border-color);
}

.controls-title {
    font-size: 1.2rem;
    font-weight: 600;
    color: var(--primary-color);
    margin: 0;
}

.controls-body {
    padding: 1.5rem;
    flex: 1;
    overflow-y: auto;
}

.model-selection {
    margin-bottom: 1.5rem;
    padding-bottom: 1.5rem;
    border-bottom: 1px solid var(--border-color);
}

.model-label {
    display: block;
    margin-bottom: 0.5rem;
    font-weight: 600;
    color: var(--primary-color);
    font-size: 0.9rem;
}

.model-dropdown {
    width: 100%;
    padding: 0.75rem;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    background-color: white;
    font-size: 0.9rem;
    color: var(--text-color);
    cursor: pointer;
}

.model-dropdown:focus {
    outline: none;
    border-color: var(--accent-color);
    box-shadow: 0 0 0 2px rgba(52, 152, 219, 0.2);
}

.control-buttons {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.status-area {
    min-height: 60px;
}

/* Results Container */
.results-container {
    display: flex;
    flex-direction: column;
    gap: 2rem;
    flex: 1;
}

/* Model Structure Panel */
.model-structure-panel {
    background: white;
    border-radius: 12px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.07);
    border: 1px solid var(--border-color);
    overflow: hidden;
}

/* Right Panel - Results */
.results-panel {
    background: white;
    border-radius: 12px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.07);
    border: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    max-height: 100%;
    max-width: 100%;
    overflow: hidden;
}

/* Unified Panel Header Styles */
.panel-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem 1.5rem;
    border-bottom: 1px solid var(--border-color);
    background: #f8f9fa;
}

.panel-title {
    margin: 0;
    color: var(--primary-color);
    font-size: 1.1rem;
    font-weight: 600;
}

/* Legacy styles for backwards compatibility - will be removed after migration */
.results-header {
    padding: 1.5rem 1.5rem 1rem 1.5rem;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.results-title {
    font-size: 1.2rem;
    font-weight: 600;
    color: var(--primary-color);
    margin: 0;
}

.simulation-info {
    font-size: 0.9rem;
    color: var(--accent-color);
    font-weight: 500;
}

.info-placeholder {
    color: var(--text-color);
    opacity: 0.6;
    font-style: italic;
}

.results-body {
    flex: 1;
    overflow-y: auto;
    padding: 1.5rem;
}

/* Chart Section */
.chart-section {
    margin-bottom: 2rem;
}

/* Canvas Controls and Structure Styling */

.canvas-controls {
    display: flex;
    gap: 0.25rem;
    align-items: center;
}

.btn-small {
    padding: 0.4rem 0.8rem;
    font-size: 0.8rem;
    border-radius: 6px;
    border: 1px solid var(--border-color);
    background: white;
    color: var(--primary-color);
    cursor: pointer;
    transition: all 0.2s ease;
}

.btn-small:hover {
    background: var(--light-gray);
    border-color: var(--accent-color);
}

.btn-icon {
    padding: 0;
    margin: 0;
    font-size: 14px;
    line-height: 1;
    border-radius: 4px;
    border: 1px solid var(--border-color);
    background: white;
    color: var(--primary-color);
    cursor: pointer;
    transition: all 0.2s ease;
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-family: Arial, Helvetica, sans-serif;
    box-sizing: border-box;
    text-align: center;
    font-weight: normal;
}

.btn-icon span {
    display: flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
}

.btn-icon:hover {
    background: var(--light-gray);
    border-color: var(--accent-color);
    transform: scale(1.05);
}

.canvas-container {
    position: relative;
    padding: 0;
    background: white;
    min-height: 400px;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
}

#modelCanvas {
    border: none;
    border-radius: 0;
    background: white;
    box-shadow: none;
    display: none;
    width: 100% !important;
    height: 400px !important;
}

#modelCanvas.active {
    display: block;
}

#canvasPlaceholder {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: var(--text-color);
    opacity: 0.6;
    font-style: italic;
    font-size: 1rem;
}

.chart-wrapper {
    background: var(--light-gray);
    border-radius: 8px;
    padding: 1rem;
    position: relative;
    height: 400px;
    width: 100%;
    max-width: 100%;
    overflow: hidden;
}

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

/* Table Section */

#tableContainer {
    max-height: 450px;
    width: 100% !important;
    max-width: 100% !important;
    overflow: visible !important; /* Let wrapper handle overflow */
    display: block !important;
    box-sizing: border-box !important;
    position: relative !important;
    contain: layout !important;
}

.table-wrapper {
    overflow-x: auto !important;
    overflow-y: hidden !important;
    max-height: 450px !important;
    position: relative !important;
    display: block !important;
    box-sizing: border-box !important;
}

.placeholder-message {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    min-height: 200px;
    margin: 1rem;
    color: #6c757d;
    font-style: italic;
    text-align: center;
    background: white;
}

/* DEBUG: Log when CSS is applied */
.placeholder-message::before {
    content: '';
    /* This selector will help us verify CSS is loading */
}

/* Data Table Styles - High specificity to override defaults */
.table-wrapper {
    width: 100% !important;
    max-width: 100% !important;
    max-height: 400px !important;
    overflow-x: auto !important;
    overflow-y: auto !important;
    position: relative !important;
    display: block !important;
    box-sizing: border-box !important;
    border: 1px solid #ddd !important;
    border-radius: 4px !important;
}

.table-wrapper .data-table {
    border-collapse: collapse;
    background: white;
    border-radius: 6px;
    overflow: hidden;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    width: max-content; /* Let table size itself, wrapper will constrain it */
    table-layout: auto;
    margin: 0; /* Remove any margins */
    min-width: 800px; /* Force horizontal scroll when many columns */
}

.table-wrapper .data-table th {
    background: #34495e !important; /* Force legacy header color with !important */
    color: white !important;
    padding: 0.75rem;
    text-align: left;
    font-weight: 600;
    position: sticky;
    top: 0;
    z-index: 10;
    white-space: nowrap;
    min-width: 100px; /* Ensure reasonable column width */
}

.table-wrapper .data-table td {
    padding: 0.75rem;
    border-bottom: 1px solid var(--border-color);
    white-space: nowrap;
    min-width: 100px; /* Ensure reasonable column width */
}

.data-table tr:nth-child(even) {
    background: var(--light-gray);
}

.data-table tr:hover {
    background: rgba(52, 152, 219, 0.1);
}

/* Alert Messages */
.alert {
    padding: 1rem;
    border-radius: 8px;
    margin: 1rem 0;
    border-left: 4px solid;
}

.alert-success {
    background-color: #d4edda;
    border-left-color: var(--success-color);
    color: #155724;
}

.alert-error {
    background-color: #f8d7da;
    border-left-color: var(--error-color);
    color: #721c24;
}

.alert-info {
    background-color: #d1ecf1;
    border-left-color: var(--accent-color);
    color: #0c5460;
}

/* Buttons */
.btn {
    padding: 0.75rem 1.5rem;
    border: none;
    border-radius: 6px;
    font-size: 1rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
    text-decoration: none;
    display: inline-block;
    text-align: center;
}

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

.btn-primary:hover:not(:disabled) {
    background-color: #34495e;
    transform: translateY(-1px);
}

.btn-secondary {
    background-color: var(--secondary-color);
    color: var(--text-color);
    border: 1px solid var(--border-color);
}

.btn-secondary:hover:not(:disabled) {
    background-color: #ddd;
}

.btn:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

/* Responsive Design */
@media (max-width: 1024px) {
    .demo-content {
        grid-template-columns: 1fr;
        height: auto;
        gap: 1.5rem;
    }
    
    .controls-panel,
    .results-panel {
        max-height: none;
    }
    
    .results-body {
        max-height: 80vh;
    }
}

/* Multi-Chart Layout */
.charts-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
}

.charts-header h4 {
    margin: 0;
}

#addChartBtn {
    padding: 0.2rem 0.5rem;
    font-size: 0.75rem;
    line-height: 1.1;
    height: auto;
    align-self: flex-end;
    margin-left: auto;
}

.chart-container {
    background: white;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    margin-bottom: 1rem;
    overflow: hidden;
}

.chart-header {
    background: var(--light-gray);
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.chart-title {
    font-weight: 600;
    color: var(--primary-color);
    margin: 0;
    cursor: pointer;
    padding: 2px 4px;
    border-radius: 3px;
    transition: background-color 0.2s;
}

.chart-title:hover {
    background-color: rgba(0, 0, 0, 0.05);
}

.chart-title.editing {
    background-color: white;
    border: 1px solid var(--primary-color);
    outline: none;
    cursor: text;
}

.chart-controls {
    display: flex;
    gap: 0.5rem;
    align-items: center;
    flex-shrink: 0;
}

.variable-dropdown {
    min-width: 140px;
    max-width: 200px;
    width: max-content;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chart-body {
    padding: 1rem;
}

.chart-legend {
    padding: 0.5rem 1rem;
    background: #fafafa;
    border-top: 1px solid var(--border-color);
    display: flex;
    flex-wrap: wrap;
    gap: 1rem;
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 0.25rem;
    cursor: pointer;
    padding: 0.25rem 0.5rem;
    border-radius: 4px;
    transition: background-color 0.2s;
}

.legend-item:hover {
    background: rgba(0, 0, 0, 0.05);
}

.legend-item.hidden {
    opacity: 0.5;
}

.legend-color {
    width: 16px;
    height: 3px;
    border-radius: 2px;
}

.legend-label {
    font-size: 0.875rem;
    user-select: none;
}

.btn-sm {
    padding: 0.375rem 0.75rem;
    font-size: 0.875rem;
}

.btn-danger {
    background-color: #e74c3c;
    color: white;
    border: 1px solid #e74c3c;
}

.btn-danger:hover {
    background-color: #c0392b;
    border-color: #c0392b;
}

/* Variable Popup */
.variable-popup {
    position: absolute;
    background: white;
    border: 2px solid var(--primary-color);
    border-radius: 8px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
    z-index: 1000;
    min-width: 300px;
    max-width: 500px;
    display: none;
}

.popup-content {
    padding: 0;
    cursor: default;
}

.popup-header {
    background: var(--primary-color);
    color: white;
    padding: 0.75rem 1rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-radius: 6px 6px 0 0;
    cursor: move;
}

.popup-header h4 {
    margin: 0;
    font-size: 1rem;
    font-weight: 600;
}

.popup-close {
    background: none;
    border: none;
    color: white;
    font-size: 1.2rem;
    cursor: pointer;
    padding: 0;
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 3px;
}

.popup-close:hover {
    background: rgba(255, 255, 255, 0.2);
}

.popup-body {
    padding: 1rem;
    cursor: default;
}

.popup-body p {
    margin: 0.5rem 0;
    font-size: 0.9rem;
}

.equation-box {
    background: var(--light-gray);
    border: 1px solid var(--border-color);
    border-radius: 4px;
    padding: 0.75rem;
    font-family: 'Courier New', monospace;
    font-size: 0.85rem;
    margin-top: 0.5rem;
    word-break: break-all;
    white-space: pre-wrap;
    max-height: 8em;
    overflow-y: auto;
    cursor: text;
    user-select: text;
    -webkit-user-select: text;
    -moz-user-select: text;
    -ms-user-select: text;
}

@media (max-width: 768px) {
    .demo-content {
        padding: 1rem;
        gap: 1rem;
    }
    
    .controls-header,
    .controls-body,
    .results-header,
    .results-body {
        padding: 1rem;
    }
}
