:root {
    --primary-color: #3b82f6;
    --secondary-color: #8b5cf6;
    --success-color: #22c55e;
    --warning-color: #f97316;
    --danger-color: #ef4444;
    --cyan-color: #06b6d4;
    --purple-color: #a855f7;
    --bg-color: #f8fafc;
    --card-bg: #ffffff;
    --text-color: #1e293b;
    --border-color: #e2e8f0;
    --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
    --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1);
    --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
    background: var(--bg-color);
    color: var(--text-color);
    line-height: 1.6;
}

.container {
    max-width: 1400px;
    margin: 0 auto;
    padding: 2rem;
}

/* Header */
header {
    text-align: center;
    margin-bottom: 2rem;
}

header h1 {
    font-size: 2.5rem;
    color: var(--text-color);
    margin-bottom: 0.5rem;
}

.subtitle {
    color: #64748b;
    font-size: 1.1rem;
}

/* Controls Section */
.controls-section {
    background: var(--card-bg);
    border-radius: 12px;
    padding: 1.5rem;
    margin-bottom: 2rem;
    box-shadow: var(--shadow-md);
}

.controls-section h2 {
    margin-bottom: 1rem;
    font-size: 1.5rem;
}

.control-group {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    gap: 1.5rem;
    margin-bottom: 1.5rem;
}

.control-item {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.control-item label {
    font-weight: 500;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.value-display {
    background: var(--primary-color);
    color: white;
    padding: 0.25rem 0.75rem;
    border-radius: 6px;
    font-family: monospace;
    font-size: 1rem;
    min-width: 60px;
    text-align: center;
}

.control-item input[type="range"] {
    width: 100%;
    height: 8px;
    border-radius: 4px;
    background: var(--border-color);
    outline: none;
    -webkit-appearance: none;
}

.control-item input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: var(--primary-color);
    cursor: pointer;
    transition: all 0.2s;
}

.control-item input[type="range"]::-webkit-slider-thumb:hover {
    transform: scale(1.1);
    box-shadow: 0 0 0 4px rgba(59, 130, 246, 0.2);
}

.checkbox-item {
    flex-direction: row;
    align-items: center;
}

.checkbox-item input[type="checkbox"] {
    width: 20px;
    height: 20px;
    margin-right: 0.75rem;
    cursor: pointer;
}

.checkbox-item label {
    cursor: pointer;
    display: flex;
    align-items: center;
}

/* Buttons */
.button-group {
    display: flex;
    flex-wrap: wrap;
    gap: 1rem;
    margin-bottom: 1.5rem;
    align-items: center;
}

.btn {
    padding: 0.75rem 1.5rem;
    border: none;
    border-radius: 8px;
    font-size: 1rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    font-family: inherit;
}

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

.btn-primary:hover {
    background: #2563eb;
    transform: translateY(-2px);
    box-shadow: var(--shadow-md);
}

.btn-secondary {
    background: #e2e8f0;
    color: var(--text-color);
}

.btn-secondary:hover {
    background: #cbd5e1;
}

.preset-buttons {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    align-items: center;
}

.preset-buttons span {
    font-weight: 500;
    margin-right: 0.5rem;
}

.view-toggle {
    display: flex;
    gap: 0.5rem;
    justify-content: center;
}

.btn-view {
    background: #e2e8f0;
    color: var(--text-color);
    flex: 1;
    max-width: 200px;
}

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

/* Visualization Section */
.visualization-section {
    background: var(--card-bg);
    border-radius: 12px;
    padding: 1.5rem;
    margin-bottom: 2rem;
    box-shadow: var(--shadow-md);
}

.canvas-container {
    position: relative;
    width: 100%;
    display: flex;
    justify-content: center;
}

#mainCanvas {
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background: #fafafa;
    cursor: crosshair;
    max-width: 100%;
}

.tooltip {
    position: absolute;
    background: rgba(0, 0, 0, 0.9);
    color: white;
    padding: 0.75rem;
    border-radius: 6px;
    font-size: 0.875rem;
    pointer-events: none;
    display: none;
    z-index: 100;
    font-family: monospace;
    white-space: pre;
}

/* Metrics Section */
.metrics-section {
    background: var(--card-bg);
    border-radius: 12px;
    padding: 1.5rem;
    margin-bottom: 2rem;
    box-shadow: var(--shadow-md);
}

.metrics-section h2 {
    margin-bottom: 1rem;
    font-size: 1.5rem;
}

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

.metric-card {
    background: #f8fafc;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 1rem;
}

.metric-card h3 {
    font-size: 1rem;
    font-weight: 600;
    margin-bottom: 0.75rem;
    color: #475569;
}

/* Matrix Display */
.matrix-display {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    font-family: monospace;
    font-size: 1.1rem;
}

.matrix-row {
    display: flex;
    gap: 0.5rem;
}

.matrix-elem {
    background: white;
    padding: 0.5rem;
    border-radius: 4px;
    min-width: 70px;
    text-align: center;
    border: 1px solid var(--border-color);
}

/* Eigenvalue Display */
.eigenvalue-display {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.eigenvalue-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.5rem;
    background: white;
    border-radius: 4px;
    border: 1px solid var(--border-color);
}

.eigenvalue-item.total {
    margin-top: 0.5rem;
    border-color: var(--primary-color);
    background: #eff6ff;
}

.eigen-label {
    font-weight: 500;
    font-family: monospace;
}

.eigen-value {
    font-family: monospace;
    font-size: 1.1rem;
    color: var(--primary-color);
}

/* Eigenvector Display */
.eigenvector-display {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.eigenvector-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.5rem;
    background: white;
    border-radius: 4px;
    border: 1px solid var(--border-color);
}

.eigenvector-item.pc1 {
    border-left: 4px solid var(--success-color);
}

.eigenvector-item.pc2 {
    border-left: 4px solid var(--warning-color);
}

.vec-label {
    font-weight: 500;
    font-family: monospace;
}

.vec-value {
    font-family: monospace;
    font-size: 1rem;
}

/* Variance Bars */
.variance-bars {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.variance-bar-container {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.variance-label {
    font-weight: 500;
    min-width: 50px;
    font-family: monospace;
}

.variance-bar-wrapper {
    flex: 1;
    height: 24px;
    background: #e2e8f0;
    border-radius: 4px;
    overflow: hidden;
}

.variance-bar {
    height: 100%;
    transition: width 0.5s ease;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    padding-right: 0.5rem;
    color: white;
    font-size: 0.75rem;
    font-weight: 600;
}

.pc1-bar {
    background: linear-gradient(90deg, var(--success-color), #16a34a);
}

.pc2-bar {
    background: linear-gradient(90deg, var(--warning-color), #ea580c);
}

.variance-percent {
    font-family: monospace;
    font-weight: 600;
    min-width: 50px;
    text-align: right;
}

/* Error Display */
.error-display {
    font-size: 2rem;
    font-weight: 700;
    color: var(--danger-color);
    text-align: center;
    font-family: monospace;
}

/* Educational Content */
.education-section {
    background: var(--card-bg);
    border-radius: 12px;
    padding: 1.5rem;
    margin-bottom: 2rem;
    box-shadow: var(--shadow-md);
}

.tabs {
    display: flex;
    gap: 0.5rem;
    border-bottom: 2px solid var(--border-color);
    margin-bottom: 1.5rem;
    overflow-x: auto;
}

.tab-btn {
    padding: 0.75rem 1.5rem;
    background: none;
    border: none;
    font-size: 1rem;
    font-weight: 500;
    cursor: pointer;
    color: #64748b;
    border-bottom: 2px solid transparent;
    margin-bottom: -2px;
    transition: all 0.2s;
    white-space: nowrap;
}

.tab-btn:hover {
    color: var(--primary-color);
}

.tab-btn.active {
    color: var(--primary-color);
    border-bottom-color: var(--primary-color);
}

.tab-content {
    min-height: 300px;
}

.tab-pane {
    display: none;
}

.tab-pane.active {
    display: block;
}

/* Concept Grid */
.concept-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
    gap: 1rem;
}

.concept-card {
    background: #f8fafc;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 1rem;
}

.concept-card h3 {
    color: var(--primary-color);
    margin-bottom: 0.5rem;
    font-size: 1.1rem;
}

.concept-card p {
    color: #475569;
    line-height: 1.6;
}

/* Formula List */
.formula-list {
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

.formula-item {
    background: #f8fafc;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 1.25rem;
}

.formula-item h4 {
    color: var(--primary-color);
    margin-bottom: 0.75rem;
    font-size: 1.1rem;
}

.formula {
    font-size: 1.25rem;
    padding: 1rem;
    background: white;
    border-radius: 6px;
    margin-bottom: 0.75rem;
    overflow-x: auto;
}

.formula-description {
    color: #64748b;
    font-style: italic;
}

/* Step List */
.step-list {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.step-item {
    display: flex;
    gap: 1rem;
    background: #f8fafc;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 1.25rem;
}

.step-number {
    flex-shrink: 0;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: var(--primary-color);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 1.25rem;
}

.step-content h4 {
    color: var(--primary-color);
    margin-bottom: 0.5rem;
    font-size: 1.1rem;
}

.step-content p {
    color: #475569;
    line-height: 1.6;
}

/* Application List */
.application-list {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
    gap: 1rem;
}

.application-item {
    background: #f8fafc;
    border: 1px solid var(--border-color);
    border-left: 4px solid var(--primary-color);
    border-radius: 8px;
    padding: 1.25rem;
}

.application-item h3 {
    color: var(--primary-color);
    margin-bottom: 0.5rem;
    font-size: 1.1rem;
}

.application-item p {
    color: #475569;
    line-height: 1.6;
}

/* Visual Explanations */
.visual-explanations {
    background: var(--card-bg);
    border-radius: 12px;
    padding: 1.5rem;
    margin-bottom: 2rem;
    box-shadow: var(--shadow-md);
}

.visual-explanations h2 {
    margin-bottom: 1rem;
    font-size: 1.5rem;
}

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

.explanation-card {
    background: #f8fafc;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 1rem;
}

.explanation-card h3 {
    color: var(--secondary-color);
    margin-bottom: 0.5rem;
    font-size: 1rem;
}

.explanation-card p {
    color: #64748b;
    line-height: 1.5;
    font-size: 0.95rem;
}

/* Legend Section */
.legend-section {
    background: var(--card-bg);
    border-radius: 12px;
    padding: 1.5rem;
    margin-bottom: 2rem;
    box-shadow: var(--shadow-md);
}

.legend-section h2 {
    margin-bottom: 1rem;
    font-size: 1.5rem;
}

.legend-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 0.75rem;
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.5rem;
    background: #f8fafc;
    border-radius: 6px;
}

.legend-color {
    width: 20px;
    height: 20px;
    border-radius: 4px;
    flex-shrink: 0;
}

.legend-arrow {
    width: 40px;
    height: 6px;
    border-radius: 3px;
    flex-shrink: 0;
}

.legend-ellipse {
    width: 30px;
    height: 20px;
    border: 2px solid #a855f7;
    border-radius: 50%;
    flex-shrink: 0;
}

/* Responsive Design */
@media (max-width: 768px) {
    .container {
        padding: 1rem;
    }

    header h1 {
        font-size: 1.75rem;
    }

    .subtitle {
        font-size: 1rem;
    }

    .control-group {
        grid-template-columns: 1fr;
    }

    .button-group {
        flex-direction: column;
    }

    .preset-buttons {
        flex-direction: column;
        align-items: flex-start;
    }

    #mainCanvas {
        max-width: 100%;
        height: auto;
    }

    .metrics-grid {
        grid-template-columns: 1fr;
    }

    .concept-grid,
    .application-list,
    .explanation-grid {
        grid-template-columns: 1fr;
    }

    .tabs {
        flex-wrap: nowrap;
        overflow-x: auto;
    }

    .tab-btn {
        padding: 0.5rem 1rem;
        font-size: 0.9rem;
    }

    .legend-grid {
        grid-template-columns: 1fr;
    }
}

/* Animations */
@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.metric-card,
.concept-card,
.application-item,
.explanation-card {
    animation: fadeIn 0.3s ease;
}

/* KaTeX Override */
.katex-display {
    margin: 0.5rem 0;
    overflow-x: auto;
}
