/* CSS Variables */
:root {
    --bg-primary: #0d1117;
    --bg-secondary: #161b22;
    --bg-tertiary: #21262d;
    --bg-hover: #30363d;

    --text-primary: #e6edf3;
    --text-secondary: #8b949e;
    --text-muted: #6e7681;

    --accent-primary: #58a6ff;
    --accent-success: #3fb950;
    --accent-warning: #d29922;
    --accent-danger: #f85149;

    --border-color: #30363d;
    --border-radius: 6px;

    --font-family: 'Segoe UI', -apple-system, BlinkMacSystemFont, 'Roboto', sans-serif;
    --font-mono: 'SF Mono', 'Consolas', 'Liberation Mono', monospace;

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.4);

    --transition-fast: 150ms ease;
    --transition-normal: 250ms ease;
}

/* Reset & Base */
*,
*::before,
*::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html,
body {
    height: 100%;
    font-family: var(--font-family);
    font-size: 14px;
    line-height: 1.5;
    color: var(--text-primary);
    background: var(--bg-primary);
    overflow: hidden;
}

/* App Layout */
.app-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
}

.app-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 20px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-color);
}

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

.main-content {
    display: flex;
    flex: 1;
    overflow: hidden;
}

/* Panels */
.control-panel,
.tuning-panel {
    width: 320px;
    min-width: 280px;
    background: var(--bg-secondary);
    border-right: 1px solid var(--border-color);
    overflow-y: auto;
    padding: 16px;
}

.tuning-panel {
    border-right: none;
    border-left: 1px solid var(--border-color);
}

.panel-section {
    margin-bottom: 20px;
    padding-bottom: 16px;
    border-bottom: 1px solid var(--border-color);
}

.panel-section:last-child {
    border-bottom: none;
    margin-bottom: 0;
}

.panel-section h2 {
    font-size: 14px;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 12px;
}

.panel-section h3 {
    font-size: 12px;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin: 16px 0 8px 0;
}

/* Parameter Rows */
.param-row {
    margin-bottom: 12px;
}

.param-row label {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 13px;
    color: var(--text-secondary);
    margin-bottom: 4px;
}

/* Slider + Input Group */
.slider-input-group {
    display: flex;
    gap: 8px;
    align-items: center;
}

.param-slider {
    flex: 1;
    height: 4px;
    background: var(--bg-tertiary);
    border-radius: 2px;
    outline: none;
    -webkit-appearance: none;
    appearance: none;
}

.param-slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 14px;
    height: 14px;
    background: var(--accent-primary);
    border-radius: 50%;
    cursor: pointer;
    transition: transform var(--transition-fast);
}

.param-slider::-webkit-slider-thumb:hover {
    transform: scale(1.2);
}

.param-slider::-moz-range-thumb {
    width: 14px;
    height: 14px;
    background: var(--accent-primary);
    border: none;
    border-radius: 50%;
    cursor: pointer;
}

.param-input {
    width: 70px;
    padding: 6px 8px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius);
    color: var(--text-primary);
    font-size: 13px;
    font-family: var(--font-mono);
    text-align: right;
}

.param-input:focus {
    outline: none;
    border-color: var(--accent-primary);
}

.param-input.invalid {
    border-color: var(--accent-danger);
}

.param-textarea {
    width: 100%;
    padding: 8px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius);
    color: var(--text-primary);
    font-size: 12px;
    font-family: var(--font-mono);
    resize: vertical;
}

.param-textarea:focus {
    outline: none;
    border-color: var(--accent-primary);
}

/* Buttons */
.btn {
    padding: 8px 16px;
    font-size: 13px;
    font-weight: 500;
    border: none;
    border-radius: var(--border-radius);
    cursor: pointer;
    transition: all var(--transition-fast);
}

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

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

.btn-primary:hover:not(:disabled) {
    filter: brightness(1.1);
}

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

.btn-secondary:hover:not(:disabled) {
    background: var(--bg-hover);
}

.button-group {
    display: flex;
    gap: 8px;
    margin-top: 12px;
}

/* Toggle Labels */
.toggle-label {
    display: flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    font-size: 13px;
}

.toggle-label input[type="checkbox"] {
    width: 16px;
    height: 16px;
    accent-color: var(--accent-primary);
}

/* Checkbox & Radio Groups */
.checkbox-group,
.radio-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-bottom: 12px;
}

.checkbox-group {
    flex-direction: row;
}

.checkbox-label,
.radio-label {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 13px;
    cursor: pointer;
}

.checkbox-label input,
.radio-label input {
    accent-color: var(--accent-primary);
}

/* Conditional Inputs */
.conditional-input {
    margin-left: 22px;
    margin-top: 8px;
}

.conditional-section {
    margin-left: 8px;
    padding-left: 12px;
    border-left: 2px solid var(--border-color);
}

/* Input Hints & Validation */
.input-hint {
    display: block;
    font-size: 11px;
    color: var(--text-muted);
    margin-top: 4px;
}

.validation-error {
    display: block;
    font-size: 11px;
    color: var(--accent-danger);
    margin-top: 4px;
    min-height: 16px;
}

/* Help Tooltip */
.help-tooltip {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 14px;
    height: 14px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: 50%;
    font-size: 10px;
    color: var(--text-muted);
    cursor: help;
    position: relative;
}

.help-tooltip::after {
    content: attr(data-tooltip);
    position: absolute;
    left: 50%;
    bottom: calc(100% + 8px);
    transform: translateX(-50%);
    width: max-content;
    max-width: 250px;
    padding: 8px 12px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius);
    font-size: 12px;
    color: var(--text-primary);
    white-space: normal;
    opacity: 0;
    visibility: hidden;
    transition: opacity var(--transition-fast);
    z-index: 1000;
    box-shadow: var(--shadow-md);
    pointer-events: none;
}

.help-tooltip:hover::after {
    opacity: 1;
    visibility: visible;
}

/* Collapsible Sections */
.collapsible-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    cursor: pointer;
    user-select: none;
}

.collapse-icon {
    font-size: 16px;
    font-weight: bold;
    color: var(--text-muted);
    transition: transform var(--transition-fast);
}

.collapsible-content {
    max-height: 0;
    overflow: hidden;
    transition: max-height var(--transition-normal);
}

.collapsible.expanded .collapsible-content {
    max-height: 2000px;
}

.collapsible.expanded .collapse-icon {
    transform: rotate(45deg);
}

/* Progress Bar */
.progress-container {
    margin-top: 12px;
}

.progress-bar {
    height: 8px;
    background: var(--bg-tertiary);
    border-radius: 4px;
    overflow: hidden;
}

.progress-fill {
    height: 100%;
    width: 0%;
    background: var(--accent-primary);
    transition: width var(--transition-fast);
}

.progress-text {
    display: block;
    font-size: 12px;
    color: var(--text-muted);
    margin-top: 4px;
    text-align: center;
}

/* Results Tables */
.results-container {
    margin-top: 16px;
}

.results-table-wrapper {
    max-height: 200px;
    overflow-y: auto;
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius);
    margin-bottom: 8px;
}

.results-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 12px;
    font-family: var(--font-mono);
}

.results-table th,
.results-table td {
    padding: 6px 8px;
    text-align: right;
    border-bottom: 1px solid var(--border-color);
}

.results-table th {
    background: var(--bg-tertiary);
    color: var(--text-secondary);
    font-weight: 600;
    position: sticky;
    top: 0;
}

.results-table tbody tr {
    cursor: pointer;
    transition: background var(--transition-fast);
}

.results-table tbody tr:hover {
    background: var(--bg-hover);
}

.results-table tbody tr.best-result {
    background: rgba(63, 185, 80, 0.15);
}

.results-table .status-unstable {
    color: var(--accent-danger);
}

.results-table .status-stable {
    color: var(--accent-success);
}

/* Visualization Area */
.visualization-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--bg-primary);
    overflow: hidden;
}

.viz-container {
    flex: 1;
    display: flex;
    gap: 16px;
    padding: 16px;
    overflow: hidden;
}

.scene-3d {
    flex: 1;
    min-width: 300px;
    min-height: 300px;
    background: var(--bg-secondary);
    border-radius: var(--border-radius);
    border: 1px solid var(--border-color);
}

.plots-container {
    width: 300px;
    min-width: 250px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.plot-canvas {
    flex: 1;
    min-height: 120px;
    width: 100%;
    background: var(--bg-secondary);
    border-radius: var(--border-radius);
    border: 1px solid var(--border-color);
}

/* Status Bar */
.status-bar {
    display: flex;
    gap: 24px;
    padding: 8px 16px;
    background: var(--bg-secondary);
    border-top: 1px solid var(--border-color);
    font-size: 12px;
    font-family: var(--font-mono);
    color: var(--text-secondary);
}

/* Download History */
.download-history {
    max-height: 150px;
    overflow-y: auto;
}

/* Mode Tabs */
.mode-tabs {
    display: flex;
    gap: 4px;
    margin-bottom: 16px;
    background: var(--bg-tertiary);
    border-radius: var(--border-radius);
    padding: 4px;
}

.mode-tab {
    flex: 1;
    padding: 8px 12px;
    background: transparent;
    border: none;
    border-radius: calc(var(--border-radius) - 2px);
    color: var(--text-secondary);
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
    transition: all var(--transition-fast);
}

.mode-tab:hover {
    color: var(--text-primary);
    background: var(--bg-hover);
}

.mode-tab.active {
    background: var(--accent-primary);
    color: #000;
}

/* Status classes for results */
.status-stable {
    color: var(--accent-success);
}

.status-marginal {
    color: var(--accent-warning);
}

.status-unstable {
    color: var(--accent-danger);
}

.best-result {
    background: rgba(88, 166, 255, 0.1);
    border-left: 3px solid var(--accent-primary);
}

/* Playback Controls */
.playback-controls {
    padding: 12px 16px;
    background: var(--bg-tertiary);
    border-top: 1px solid var(--border-color);
}

.playback-row {
    display: flex;
    align-items: center;
    gap: 16px;
}

.timeline-container {
    flex: 1;
}

.timeline-slider {
    width: 100%;
    height: 8px;
    -webkit-appearance: none;
    appearance: none;
    background: var(--bg-primary);
    border-radius: 4px;
    outline: none;
    cursor: pointer;
}

.timeline-slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: var(--accent-primary);
    cursor: pointer;
    border: 2px solid var(--bg-secondary);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.timeline-slider::-moz-range-thumb {
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: var(--accent-primary);
    cursor: pointer;
    border: 2px solid var(--bg-secondary);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.timeline-slider:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.timeline-slider:disabled::-webkit-slider-thumb {
    cursor: not-allowed;
}

.speed-control {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}

.speed-control label {
    font-size: 0.875rem;
    color: var(--text-secondary);
}

.speed-select {
    padding: 4px 8px;
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius);
    color: var(--text-primary);
    font-size: 0.875rem;
    cursor: pointer;
}

.speed-select:hover {
    border-color: var(--accent-primary);
}

.speed-select:focus {
    outline: none;
    border-color: var(--accent-primary);
    box-shadow: 0 0 0 2px rgba(88, 166, 255, 0.2);
}

.download-history .empty-state {
    color: var(--text-muted);
    font-size: 12px;
    font-style: italic;
}

.download-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 6px 0;
    border-bottom: 1px solid var(--border-color);
    font-size: 12px;
}

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

.download-item a {
    color: var(--accent-primary);
    text-decoration: none;
}

.download-item a:hover {
    text-decoration: underline;
}

/* File Name Display */
.file-name {
    display: block;
    font-size: 11px;
    color: var(--text-muted);
    margin-top: 4px;
    font-style: italic;
}

/* Power User Section */
.power-user-section {
    margin-top: 16px;
    padding-top: 16px;
    border-top: 1px dashed var(--border-color);
}

/* Scrollbar Styling */
::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

::-webkit-scrollbar-track {
    background: var(--bg-primary);
}

::-webkit-scrollbar-thumb {
    background: var(--bg-hover);
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--text-muted);
}

/* Animations */
@keyframes fadeIn {
    from {
        opacity: 0;
    }

    to {
        opacity: 1;
    }
}

@keyframes pulse {

    0%,
    100% {
        opacity: 1;
    }

    50% {
        opacity: 0.5;
    }
}

.running .progress-fill {
    animation: pulse 1s ease-in-out infinite;
}

/* ==============================================
   Mobile Navigation (hidden on desktop)
   ============================================== */
.mobile-nav {
    display: none;
}

/* ==============================================
   RESPONSIVE: Tablet (≤1100px)
   ============================================== */
@media (max-width: 1100px) {

    .control-panel,
    .tuning-panel {
        width: 260px;
        min-width: 220px;
    }

    .plots-container {
        width: 240px;
        min-width: 200px;
    }

    .status-bar {
        flex-wrap: wrap;
        gap: 12px 20px;
    }
}

/* ==============================================
   RESPONSIVE: Small Tablet / Large Phone (≤900px)
   ============================================== */
@media (max-width: 900px) {

    html,
    body {
        overflow: auto;
    }

    .app-container {
        height: auto;
        min-height: 100vh;
    }

    /* Header: show mobile nav, hide desktop toggle */
    .app-header {
        flex-wrap: wrap;
        padding: 10px 12px;
        gap: 8px;
    }

    .app-header h1 {
        font-size: 16px;
        flex: 1;
    }

    .header-controls {
        order: 2;
    }

    .mobile-nav {
        display: flex;
        order: 3;
        width: 100%;
        gap: 4px;
        background: var(--bg-tertiary);
        border-radius: var(--border-radius);
        padding: 4px;
    }

    .mobile-nav-btn {
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 6px;
        padding: 8px 10px;
        background: transparent;
        border: none;
        border-radius: calc(var(--border-radius) - 2px);
        color: var(--text-secondary);
        font-size: 12px;
        font-weight: 500;
        cursor: pointer;
        transition: all var(--transition-fast);
    }

    .mobile-nav-btn svg {
        flex-shrink: 0;
    }

    .mobile-nav-btn:hover {
        color: var(--text-primary);
        background: var(--bg-hover);
    }

    .mobile-nav-btn.active {
        background: var(--accent-primary);
        color: #000;
    }

    .mobile-nav-btn.active svg {
        stroke: #000;
    }

    /* Main content: stack vertically */
    .main-content {
        flex-direction: column;
        overflow: visible;
    }

    /* Panels: full width, hidden by default on mobile */
    .control-panel,
    .tuning-panel {
        width: 100%;
        min-width: unset;
        border-right: none;
        border-left: none;
        border-bottom: 1px solid var(--border-color);
        display: none;
    }

    .tuning-panel {
        border-left: none;
    }

    /* Show active panel */
    .control-panel.mobile-active,
    .tuning-panel.mobile-active {
        display: block;
    }

    .visualization-area.mobile-active {
        display: flex;
    }

    .visualization-area.mobile-hidden {
        display: none;
    }

    /* Visualization area */
    .visualization-area {
        flex: none;
    }

    .viz-container {
        flex-direction: column;
        padding: 12px;
        gap: 12px;
    }

    .scene-3d {
        min-width: unset;
        min-height: 300px;
        height: 320px;
        max-height: 320px;
    }

    .plots-container {
        width: 100%;
        min-width: unset;
        flex-direction: column;
    }

    .plot-canvas {
        width: 100%;
        min-height: 140px;
        height: 140px;
    }

    /* Status bar: wrap */
    .status-bar {
        flex-wrap: wrap;
        gap: 8px 16px;
        font-size: 11px;
        padding: 8px 12px;
    }

    /* Playback */
    .playback-controls {
        padding: 10px 12px;
    }

    .playback-row {
        flex-wrap: wrap;
        gap: 10px;
    }

    .timeline-container {
        min-width: 100%;
    }

    .speed-control {
        width: 100%;
        justify-content: flex-end;
    }
}

/* ==============================================
   RESPONSIVE: Phone (≤600px)
   ============================================== */
@media (max-width: 600px) {
    .app-header {
        padding: 8px 10px;
    }

    .app-header h1 {
        font-size: 14px;
    }

    .header-controls {
        font-size: 12px;
    }

    .header-controls span {
        display: none;
    }

    .mobile-nav-btn {
        padding: 8px 6px;
        font-size: 11px;
    }

    .mobile-nav-btn span {
        display: inline;
    }

    /* Panels */
    .control-panel,
    .tuning-panel {
        padding: 12px;
    }

    .panel-section h2 {
        font-size: 13px;
    }

    /* Sliders: make touch-friendly */
    .param-slider::-webkit-slider-thumb {
        width: 20px;
        height: 20px;
    }

    .param-slider::-moz-range-thumb {
        width: 20px;
        height: 20px;
    }

    .param-slider {
        height: 6px;
    }

    .param-input {
        width: 65px;
        padding: 8px 6px;
        font-size: 13px;
    }

    /* Buttons: larger touch targets */
    .btn {
        padding: 10px 16px;
        font-size: 14px;
    }

    .button-group {
        flex-wrap: wrap;
    }

    .button-group .btn {
        flex: 1;
        min-width: 80px;
    }

    /* Scene */
    .scene-3d {
        min-height: 320px;
        height: 320px;
        max-height: 320px;
    }

    .viz-container {
        padding: 8px;
        gap: 8px;
    }

    .plot-canvas {
        min-height: 110px;
        height: 140px;
    }

    /* Status bar */
    .status-bar {
        font-size: 10px;
        gap: 6px 12px;
        padding: 6px 10px;
    }

    /* Results table: horizontal scroll */
    .results-table-wrapper {
        max-height: 180px;
    }

    .results-table {
        font-size: 11px;
    }

    .results-table th,
    .results-table td {
        padding: 5px 6px;
    }

    /* Mode tabs */
    .mode-tab {
        font-size: 0.8rem;
        padding: 7px 8px;
    }

    /* Tooltips: position below on mobile (avoid offscreen) */
    .help-tooltip::after {
        left: auto;
        right: 0;
        transform: none;
        bottom: auto;
        top: calc(100% + 8px);
        max-width: 200px;
    }

    /* Playback speed */
    .speed-select {
        font-size: 0.8rem;
    }

    /* Toggle labels */
    .toggle-label {
        font-size: 12px;
    }

    .toggle-label input[type="checkbox"] {
        width: 18px;
        height: 18px;
    }

    /* Checkbox group: stack vertically on very small screens */
    .checkbox-group {
        flex-direction: column;
        gap: 6px;
    }
}

/* ==============================================
   RESPONSIVE: Very Small Phone (≤400px)
   ============================================== */
@media (max-width: 400px) {
    .app-header h1 {
        font-size: 13px;
    }

    .mobile-nav-btn span {
        display: none;
    }

    .scene-3d {
        min-height: 300px;
        height: 300px;
        max-height: 300px;
    }

    .plot-canvas {
        min-height: 100px;
        height: 130px;
    }

    .param-input {
        width: 58px;
        font-size: 12px;
    }

    .param-row label {
        font-size: 12px;
    }

    .slider-input-group {
        gap: 6px;
    }
}

/* ==============================================
   Touch-friendly utilities
   ============================================== */
@media (pointer: coarse) {

    /* Larger touch targets for touch devices */
    .param-slider::-webkit-slider-thumb {
        width: 22px;
        height: 22px;
    }

    .param-slider::-moz-range-thumb {
        width: 22px;
        height: 22px;
    }

    .timeline-slider::-webkit-slider-thumb {
        width: 22px;
        height: 22px;
    }

    .timeline-slider::-moz-range-thumb {
        width: 22px;
        height: 22px;
    }

    .collapsible-header {
        padding: 4px 0;
    }

    /* Prevent zoom on input focus (iOS) */
    input[type="number"],
    input[type="text"],
    select,
    textarea {
        font-size: 16px;
    }
}

/* Landscape phone: limit scene height */
@media (max-width: 900px) and (orientation: landscape) {
    .scene-3d {
        min-height: 300px;
        height: 300px;
        max-height: 300px;
    }

    .plot-canvas {
        min-height: 140px;
        height: 140px;
    }
}