/* ===== CSS Variables / Themes ===== */
:root {
    --bg-primary: #0d1117;
    --bg-secondary: #161b22;
    --bg-tertiary: #21262d;
    --border-color: #30363d;
    --text-primary: #e6edf3;
    --text-secondary: #8b949e;
    --accent: #58a6ff;
    --accent-hover: #79c0ff;
    --accent-emphasis: #1f6feb;
    --success: #3fb950;
    --warning: #d29922;
    --danger: #f85149;
    --sphere-wire: #3d444d;
    --sphere-wire-opacity: 0.4;
    --label-color: #58a6ff;
}

[data-theme="light"] {
    --bg-primary: #f6f8fa;
    --bg-secondary: #ffffff;
    --bg-tertiary: #f0f3f6;
    --border-color: #d0d7de;
    --text-primary: #1f2328;
    --text-secondary: #656d76;
    --accent: #0969da;
    --accent-hover: #0550ae;
    --accent-emphasis: #0550ae;
    --success: #1a7f37;
    --warning: #9a6700;
    --danger: #cf222e;
    --sphere-wire: #b0b8c0;
    --sphere-wire-opacity: 0.5;
    --label-color: #0550ae;
}

/* ===== Base Styles ===== */
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html, body {
    height: 100%;
    overflow: hidden;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
}

/* ===== Layout ===== */
.app-container {
    display: flex;
    height: 100vh;
    width: 100vw;
}

#canvas-container {
    flex: 1;
    position: relative;
    min-width: 0;
}

/* ===== Control Panel ===== */
#ui-container {
    width: 320px;
    height: 100vh;
    background: var(--bg-secondary);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    transition: width 0.3s ease, transform 0.3s ease;
    position: relative;
    z-index: 10;
}

#ui-container.collapsed {
    width: 0;
    border-right: none;
}

#ui-container.collapsed .panel-content {
    display: none;
}

.panel-content {
    padding: 20px;
    overflow-y: auto;
    flex: 1;
    display: flex;
    flex-direction: column;
}

/* Collapse Toggle Button */
.collapse-btn {
    position: absolute;
    top: 50%;
    right: -32px;
    transform: translateY(-50%);
    width: 32px;
    height: 64px;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-left: none;
    border-radius: 0 8px 8px 0;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-secondary);
    transition: color 0.15s, background 0.15s;
}

.collapse-btn:hover {
    color: var(--accent);
    background: var(--bg-tertiary);
}

.collapse-btn svg {
    width: 16px;
    height: 16px;
    transition: transform 0.3s ease;
}

#ui-container.collapsed .collapse-btn svg {
    transform: rotate(180deg);
}

/* ===== Panel Header ===== */
.panel-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 20px;
    padding-bottom: 16px;
    border-bottom: 1px solid var(--border-color);
}

.panel-title h1 {
    font-size: 1.25rem;
    font-weight: 600;
    margin-bottom: 4px;
    letter-spacing: -0.02em;
}

.panel-subtitle {
    font-size: 0.75rem;
    color: var(--text-secondary);
}

/* Theme Toggle */
.theme-toggle {
    width: 36px;
    height: 36px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-secondary);
    transition: all 0.15s;
    flex-shrink: 0;
}

.theme-toggle:hover {
    border-color: var(--accent);
    color: var(--accent);
}

.theme-toggle svg {
    width: 18px;
    height: 18px;
}

.theme-toggle .icon-moon { display: none; }
[data-theme="light"] .theme-toggle .icon-sun { display: none; }
[data-theme="light"] .theme-toggle .icon-moon { display: block; }

/* ===== Control Groups ===== */
.control-group {
    margin-bottom: 16px;
}

.control-label {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;
}

.label-text {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.875rem;
    color: var(--text-secondary);
}

.param-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 28px;
    height: 24px;
    padding: 0 6px;
    background: rgba(88, 166, 255, 0.1);
    border: 1px solid rgba(88, 166, 255, 0.2);
    border-radius: 4px;
    color: var(--accent);
    font-size: 0.875rem;
    font-weight: 500;
}

.value-display {
    font-family: 'SF Mono', Consolas, monospace;
    font-size: 0.8rem;
    color: var(--text-primary);
    background: var(--bg-tertiary);
    padding: 4px 8px;
    border-radius: 4px;
}

/* Value Input */
.value-input {
    width: 90px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: 4px;
    padding: 5px 8px;
    font-family: 'SF Mono', Consolas, monospace;
    font-size: 0.8rem;
    color: var(--text-primary);
    text-align: right;
    outline: none;
    transition: border-color 0.15s, box-shadow 0.15s;
}

.value-input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 2px rgba(88, 166, 255, 0.15);
}

.value-input.error {
    border-color: var(--danger);
    box-shadow: 0 0 0 2px rgba(248, 81, 73, 0.15);
}

.value-input::placeholder {
    color: var(--text-secondary);
    opacity: 0.6;
}

/* Input Hint */
.input-hint {
    font-size: 0.7rem;
    color: var(--text-secondary);
    margin-top: 4px;
    margin-bottom: 12px;
    padding: 6px 10px;
    background: rgba(88, 166, 255, 0.05);
    border-radius: 4px;
    border-left: 2px solid var(--accent);
}

.input-hint strong {
    color: var(--accent);
}

/* ===== Range Slider ===== */
input[type="range"] {
    -webkit-appearance: none;
    width: 100%;
    height: 6px;
    background: var(--bg-tertiary);
    border-radius: 3px;
    cursor: pointer;
    border: 1px solid var(--border-color);
}

input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 18px;
    height: 18px;
    background: var(--accent);
    border-radius: 50%;
    cursor: grab;
    transition: transform 0.1s, box-shadow 0.1s;
    box-shadow: 0 2px 4px rgba(0,0,0,0.2);
}

input[type="range"]::-webkit-slider-thumb:hover {
    transform: scale(1.15);
}

input[type="range"]::-webkit-slider-thumb:active {
    cursor: grabbing;
}

input[type="range"]::-moz-range-thumb {
    width: 18px;
    height: 18px;
    background: var(--accent);
    border-radius: 50%;
    border: none;
    cursor: grab;
    box-shadow: 0 2px 4px rgba(0,0,0,0.2);
}

/* ===== Section Title ===== */
.section-title {
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-secondary);
    margin: 16px 0 10px;
}

/* ===== Button Grid ===== */
.btn-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
}

.state-btn {
    background: var(--bg-tertiary);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
    padding: 10px 8px;
    cursor: pointer;
    border-radius: 6px;
    font-family: inherit;
    font-size: 0.9rem;
    transition: all 0.15s;
}

.state-btn:hover {
    border-color: var(--accent);
    background: rgba(88, 166, 255, 0.1);
}

.state-btn.active {
    background: var(--accent-emphasis);
    border-color: var(--accent);
    color: white;
}

/* ===== Readout Section ===== */
.readout {
    margin-top: auto;
    padding-top: 16px;
    border-top: 1px solid var(--border-color);
}

.readout-card {
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    padding: 12px;
    margin-bottom: 10px;
}

.readout-card:last-child {
    margin-bottom: 0;
}

.readout-label {
    font-size: 0.65rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-secondary);
    margin-bottom: 6px;
}

.readout-value {
    font-family: 'SF Mono', Consolas, monospace;
    font-size: 0.85rem;
    color: var(--accent);
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 24px;
}

.readout-value.state-vector {
    font-size: 0.8rem;
    line-height: 1.4;
}

.readout-value mjx-container {
    font-size: 0.9rem !important;
}

/* MathJax in buttons */
.state-btn mjx-container {
    font-size: 1rem !important;
}

.param-badge mjx-container {
    font-size: 0.85rem !important;
}

/* ===== Info Button ===== */
.info-btn {
    position: fixed;
    bottom: 20px;
    right: 20px;
    width: 40px;
    height: 40px;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-secondary);
    transition: all 0.15s;
    z-index: 100;
}

.info-btn:hover {
    border-color: var(--accent);
    color: var(--accent);
    transform: scale(1.05);
}

.info-btn svg {
    width: 20px;
    height: 20px;
}

/* ===== Modal ===== */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.6);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.2s, visibility 0.2s;
}

.modal-overlay.active {
    opacity: 1;
    visibility: visible;
}

.modal {
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 28px 32px;
    max-width: 380px;
    width: 90%;
    text-align: center;
    transform: scale(0.95);
    transition: transform 0.2s;
}

.modal-overlay.active .modal {
    transform: scale(1);
}

.modal-title {
    font-size: 1.1rem;
    color: var(--text-primary);
    margin-bottom: 20px;
}

.modal-links {
    display: flex;
    gap: 12px;
    justify-content: center;
}

.modal-link {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 10px 18px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    color: var(--text-primary);
    text-decoration: none;
    font-size: 0.875rem;
    transition: all 0.15s;
}

.modal-link:hover {
    border-color: var(--accent);
    color: var(--accent);
}

.modal-link svg {
    width: 18px;
    height: 18px;
}

/* ===== Mobile Responsive ===== */
@media (max-width: 768px) {
    .app-container {
        flex-direction: column;
    }
    
    #canvas-container {
        flex: 1;
        min-height: 50vh;
    }
    
    #ui-container {
        width: 100%;
        height: auto;
        max-height: 50vh;
        border-right: none;
        border-top: 1px solid var(--border-color);
        order: 1;
    }
    
    #ui-container.collapsed {
        width: 100%;
        height: 48px;
        max-height: 48px;
        overflow: hidden;
    }
    
    #ui-container.collapsed .panel-content {
        display: none;
    }
    
    .collapse-btn {
        position: static;
        transform: none;
        width: 100%;
        height: 48px;
        border-radius: 0;
        border: none;
        border-bottom: 1px solid var(--border-color);
        right: auto;
    }
    
    .collapse-btn svg {
        transform: rotate(90deg);
    }
    
    #ui-container.collapsed .collapse-btn svg {
        transform: rotate(-90deg);
    }
    
    .panel-content {
        padding: 16px;
    }
    
    .panel-header {
        margin-bottom: 14px;
        padding-bottom: 12px;
    }
    
    .btn-grid {
        grid-template-columns: repeat(3, 1fr);
        gap: 6px;
    }
    
    .state-btn {
        padding: 8px 6px;
        font-size: 0.85rem;
    }
    
    .readout {
        padding-top: 12px;
    }
    
    .readout-card {
        padding: 10px;
        margin-bottom: 8px;
    }
    
    .info-btn {
        bottom: auto;
        top: 12px;
        right: 12px;
        width: 36px;
        height: 36px;
    }
}

@media (max-width: 400px) {
    .panel-content {
        padding: 12px;
    }
    
    .modal {
        padding: 20px;
    }
    
    .modal-links {
        flex-direction: column;
    }
}

