/* ===== CSS Variables & Reset ===== */
:root {
    --bg: #f5f7fa;
    --card-bg: #ffffff;
    --card-border: #dde1e8;
    --accent: #1e3a8a;
    --accent-hover: #2d5bbf;
    --fx-color: #e17055;
    --fpx-color: #00b894;
    --text: #1a1a2e;
    --text-muted: #6b7280;
    --success: #00b894;
    --error: #e17055;
    --warning: #fdcb6e;
    --sidebar-width: 260px;
    --font: 'Segoe UI', system-ui, -apple-system, sans-serif;
}

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

html {
    scroll-behavior: smooth;
    overflow-x: hidden;
}

body {
    font-family: var(--font);
    background: var(--bg);
    color: var(--text);
    line-height: 1.7;
    min-height: 100vh;
    overflow-x: hidden;
}

/* ===== Sidebar ===== */
.sidebar {
    position: fixed;
    top: 0;
    left: 0;
    width: var(--sidebar-width);
    height: 100vh;
    background: var(--card-bg);
    border-right: 1px solid var(--card-border);
    padding: 24px 16px;
    overflow-y: auto;
    z-index: 100;
    transition: transform 0.3s ease;
}

.sidebar-header {
    margin-bottom: 24px;
}

.sidebar-header h2 {
    color: var(--accent);
    font-size: 1.3rem;
    margin-bottom: 4px;
}

.sidebar-subtitle {
    color: var(--text-muted);
    font-size: 0.85rem;
    margin-bottom: 12px;
}

.progress-bar-container {
    width: 100%;
    height: 6px;
    background: var(--bg);
    border-radius: 3px;
    overflow: hidden;
    margin-bottom: 4px;
}

.progress-bar {
    width: 0%;
    height: 100%;
    background: linear-gradient(90deg, var(--accent), var(--success));
    border-radius: 3px;
    transition: width 0.5s ease;
}

.progress-text {
    font-size: 0.75rem;
    color: var(--text-muted);
}

.nav-list {
    list-style: none;
}

.nav-link {
    display: block;
    padding: 10px 14px;
    color: var(--text-muted);
    text-decoration: none;
    border-radius: 8px;
    font-size: 0.9rem;
    transition: all 0.2s ease;
    margin-bottom: 2px;
}

.nav-link:hover {
    color: var(--text);
    background: rgba(26, 115, 232, 0.15);
}

.nav-link.active {
    color: #fff;
    background: var(--accent);
}

.sidebar-toggle {
    display: none;
    position: fixed;
    top: 12px;
    left: 12px;
    z-index: 200;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: 8px;
    width: 44px;
    height: 44px;
    font-size: 1.3rem;
    cursor: pointer;
}

/* ===== Main Content ===== */
.main-content {
    margin-left: var(--sidebar-width);
    padding: 40px 48px;
    max-width: 1000px;
}

.section {
    margin-bottom: 60px;
}

.section > h1 {
    font-size: 1.8rem;
    color: #0a0a1a;
    margin-bottom: 24px;
    padding-bottom: 12px;
    border-bottom: 2px solid var(--accent);
}

/* ===== Cards ===== */
.card {
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: 12px;
    padding: 24px;
    margin-bottom: 20px;
}

.card h3 {
    color: var(--accent);
    font-size: 1.15rem;
    margin-bottom: 12px;
}

.card p {
    margin-bottom: 10px;
}

.card ul, .card ol {
    padding-left: 24px;
    margin-bottom: 10px;
}

.card li {
    margin-bottom: 6px;
}

.warning-card {
    border-color: var(--error);
    background: rgba(225, 112, 85, 0.08);
}

.warning-card h3 {
    color: var(--error);
}

/* ===== Formula block ===== */
.formula-block {
    background: rgba(26, 115, 232, 0.08);
    border-left: 3px solid var(--accent);
    padding: 12px 16px;
    margin: 12px 0;
    border-radius: 0 8px 8px 0;
    overflow-x: auto;
}

/* ===== Tables ===== */
.styled-table {
    width: 100%;
    border-collapse: collapse;
    margin: 12px 0;
}

.styled-table th, .styled-table td {
    padding: 10px 16px;
    text-align: left;
    border-bottom: 1px solid var(--card-border);
}

.styled-table th {
    color: var(--accent);
    font-weight: 600;
    background: rgba(26, 115, 232, 0.1);
}

.styled-table tr:hover td {
    background: rgba(26, 115, 232, 0.05);
}

/* ===== Algorithm Steps ===== */
.algorithm-steps {
    margin: 12px 0;
}

.step {
    display: flex;
    align-items: flex-start;
    gap: 14px;
    margin-bottom: 14px;
}

.step-number {
    flex-shrink: 0;
    width: 36px;
    height: 36px;
    background: var(--accent);
    color: #fff;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 1rem;
}

.step-text {
    padding-top: 6px;
    font-size: 1rem;
}

/* ===== Interactive Controls ===== */
.interactive-card {
    padding: 20px;
}

.controls-row {
    display: flex;
    flex-wrap: wrap;
    gap: 16px;
    align-items: center;
    margin-bottom: 14px;
}

.controls-row label {
    color: var(--text-muted);
    font-size: 0.9rem;
    display: flex;
    align-items: center;
    gap: 8px;
}

.controls-row select {
    background: var(--bg);
    color: var(--text);
    border: 1px solid var(--card-border);
    border-radius: 6px;
    padding: 6px 10px;
    font-size: 0.9rem;
}

.small-input {
    width: 60px;
    background: var(--bg);
    color: var(--text);
    border: 1px solid var(--card-border);
    border-radius: 6px;
    padding: 4px 8px;
    text-align: center;
}

.slider {
    width: 120px;
    accent-color: var(--accent);
}

.slider-group {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
}

.slider-item {
    display: flex;
    align-items: center;
    gap: 6px;
    color: var(--text-muted);
    font-size: 0.85rem;
}

.slider-item input[type="range"] {
    width: 100px;
    accent-color: var(--accent);
}

.slider-val {
    min-width: 36px;
    text-align: right;
    color: var(--text);
    font-weight: 600;
}

/* ===== Canvas ===== */
.canvas-container {
    background: var(--bg);
    border-radius: 10px;
    overflow: hidden;
    margin: 12px 0;
    border: 1px solid var(--card-border);
}

.canvas-container canvas {
    display: block;
    width: 100%;
    height: auto;
}

/* ===== Legend ===== */
.legend {
    display: flex;
    flex-wrap: wrap;
    gap: 16px;
    margin-top: 8px;
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 0.85rem;
    color: var(--text-muted);
}

.legend-color {
    width: 14px;
    height: 14px;
    border-radius: 3px;
    display: inline-block;
}

.graph-info {
    margin-top: 10px;
    font-size: 0.9rem;
    color: var(--text-muted);
    min-height: 24px;
}

/* ===== Animation ===== */
.step-label {
    color: var(--text-muted);
    font-size: 0.9rem;
}

.anim-description {
    margin-top: 10px;
    padding: 12px;
    background: rgba(26, 115, 232, 0.08);
    border-radius: 8px;
    font-size: 0.95rem;
    min-height: 48px;
}

/* ===== Buttons ===== */
.btn {
    padding: 8px 20px;
    border: none;
    border-radius: 8px;
    font-size: 0.9rem;
    cursor: pointer;
    transition: all 0.2s ease;
    font-weight: 600;
}

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

.btn-primary:hover {
    background: var(--accent-hover);
    transform: translateY(-1px);
}

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

.btn-secondary {
    background: var(--card-border);
    color: var(--text);
}

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

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

/* ===== Problems ===== */
.problem-card {
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: 12px;
    margin-bottom: 16px;
    overflow: hidden;
}

.problem-header {
    padding: 16px 20px;
    cursor: pointer;
    display: flex;
    justify-content: space-between;
    align-items: center;
    transition: background 0.2s;
}

.problem-header:hover {
    background: rgba(26, 115, 232, 0.08);
}

.problem-number {
    color: var(--accent);
    font-weight: 700;
    margin-right: 10px;
}

.problem-type {
    font-size: 0.8rem;
    color: var(--text-muted);
    background: rgba(26, 115, 232, 0.15);
    padding: 2px 10px;
    border-radius: 12px;
}

.problem-toggle {
    font-size: 1.2rem;
    color: var(--text-muted);
    transition: transform 0.3s;
}

.problem-toggle.open {
    transform: rotate(180deg);
}

.problem-body {
    display: none;
    padding: 0 20px 20px;
    border-top: 1px solid var(--card-border);
}

.problem-body.open {
    display: block;
}

.problem-hint {
    background: rgba(253, 203, 110, 0.1);
    border-left: 3px solid var(--warning);
    padding: 10px 14px;
    border-radius: 0 8px 8px 0;
    margin: 10px 0;
    font-size: 0.9rem;
}

.problem-hint-btn {
    background: none;
    border: none;
    color: var(--warning);
    cursor: pointer;
    font-size: 0.9rem;
    margin-bottom: 6px;
}

.problem-solution {
    margin: 12px 0;
}

.solution-step {
    margin-bottom: 8px;
    padding-left: 12px;
    border-left: 2px solid var(--card-border);
}

.solution-step strong {
    color: var(--accent);
}

.problem-answer {
    background: rgba(0, 184, 148, 0.1);
    border: 1px solid var(--success);
    border-radius: 8px;
    padding: 10px 14px;
    font-weight: 700;
    color: var(--success);
    margin-top: 10px;
}

/* ===== Trainer ===== */
.trainer-problem {
    font-size: 1.15rem;
    margin-bottom: 14px;
    min-height: 32px;
}

.trainer-input-row {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    align-items: center;
    margin-bottom: 12px;
}

.trainer-input {
    background: var(--bg);
    color: var(--text);
    border: 1px solid var(--card-border);
    border-radius: 8px;
    padding: 8px 14px;
    font-size: 1rem;
    min-width: 200px;
}

.trainer-input:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 2px rgba(26, 115, 232, 0.25);
}

.feedback {
    min-height: 28px;
    font-size: 1rem;
    font-weight: 600;
    margin-bottom: 8px;
    padding: 12px 16px;
    border-radius: 8px;
    transition: all 0.3s ease;
}

.feedback.correct {
    color: #00b894;
    background: rgba(0, 184, 148, 0.18);
    border: 2px solid rgba(0, 184, 148, 0.5);
    box-shadow: 0 0 15px rgba(0, 184, 148, 0.2);
    animation: correctPulse 0.5s ease;
}

.feedback.wrong {
    color: #e17055;
    background: rgba(225, 112, 85, 0.18);
    border: 2px solid rgba(225, 112, 85, 0.5);
    box-shadow: 0 0 15px rgba(225, 112, 85, 0.2);
    animation: wrongShake 0.4s ease;
}

@keyframes correctPulse {
    0% { transform: scale(0.95); opacity: 0; }
    50% { transform: scale(1.02); }
    100% { transform: scale(1); opacity: 1; }
}

@keyframes wrongShake {
    0%, 100% { transform: translateX(0); }
    20% { transform: translateX(-6px); }
    40% { transform: translateX(6px); }
    60% { transform: translateX(-4px); }
    80% { transform: translateX(4px); }
}

.trainer-score {
    color: var(--text-muted);
    font-size: 0.9rem;
}

.hint-text {
    color: var(--warning);
    font-size: 0.9rem;
    min-height: 20px;
    margin-bottom: 8px;
}

/* ===== Exam Mode ===== */
.exam-header {
    display: flex;
    align-items: center;
    gap: 20px;
    margin-bottom: 16px;
}

.timer {
    font-size: 2rem;
    font-weight: 700;
    color: var(--accent);
    font-variant-numeric: tabular-nums;
}

.timer.warning {
    color: var(--warning);
}

.timer.danger {
    color: var(--error);
    animation: pulse 1s infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

/* ===== Cheatsheet ===== */
.cheatsheet-card {
    background: linear-gradient(135deg, var(--card-bg), rgba(26, 115, 232, 0.08));
}

.cheatsheet-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
    gap: 12px;
    margin-top: 12px;
}

.cheatsheet-item {
    background: var(--bg);
    padding: 10px 14px;
    border-radius: 8px;
    border: 1px solid var(--card-border);
    text-align: center;
}

.cheatsheet-algo {
    padding-left: 24px;
    margin-top: 8px;
}

.cheatsheet-algo li {
    margin-bottom: 6px;
}

/* ===== Responsive ===== */
@media (max-width: 900px) {
    .sidebar {
        transform: translateX(-100%);
    }

    .sidebar.open {
        transform: translateX(0);
        z-index: 200;
        box-shadow: 4px 0 20px rgba(0, 0, 0, 0.15);
    }

    .sidebar-toggle {
        display: block;
    }

    .main-content {
        margin-left: 0;
        padding: 60px 16px 40px;
    }

    .controls-row {
        flex-direction: column;
        align-items: flex-start;
    }

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

    .exam-header {
        flex-direction: column;
        align-items: flex-start;
    }

    /* Tables: horizontal scroll on mobile */
    .styled-table {
        display: block;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    /* Sprint header stack */
    .sprint-header {
        flex-direction: column;
        align-items: flex-start;
    }

    /* Algorithm pipeline: allow scroll if needed */
    .algo-pipeline {
        overflow-x: auto;
    }
}

@media (max-width: 500px) {
    .main-content {
        padding: 50px 12px 30px;
    }

    .section > h1 {
        font-size: 1.4rem;
    }

    .card {
        padding: 16px;
    }

    /* Formula blocks: prevent overflow */
    .formula-block {
        padding: 10px 12px;
        font-size: 0.9rem;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    /* Touch targets: minimum 44px */
    .btn, .nav-link, button, .problem-header {
        min-height: 44px;
    }

    .nav-link {
        padding: 12px 14px;
    }

    /* Trainer inputs: full width on mobile */
    .trainer-input-row {
        flex-direction: column;
        align-items: stretch;
    }

    .trainer-input {
        min-width: unset;
        width: 100%;
    }

    /* Slider: full width */
    .slider {
        width: 100%;
    }

    .slider-item input[type="range"] {
        width: 100%;
        flex: 1;
    }

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

    /* Problem answer input: full width */
    .problem-answer-input {
        min-width: unset;
        width: 100%;
    }

    /* Sprint timer smaller */
    .sprint-timer {
        font-size: 2rem;
    }

    .sprint-stats {
        flex-direction: column;
        gap: 8px;
    }

    /* Algo steps: smaller on mobile */
    .algo-step {
        padding: 10px 14px;
        gap: 10px;
    }

    .algo-step-num {
        width: 34px;
        height: 34px;
        font-size: 1rem;
    }

    /* Step numbers: smaller */
    .step-number {
        width: 32px;
        height: 32px;
        font-size: 0.9rem;
    }
}

/* ===== iOS input zoom prevention ===== */
@media (max-width: 900px) {
    input, select, textarea {
        font-size: 16px;
    }
}

/* ===== KaTeX overrides ===== */
.katex {
    font-size: 1.08em;
}

.formula-block .katex {
    font-size: 1.15em;
}

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

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

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

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

/* ===== Algorithm Hero Card ===== */
.algo-hero-card {
    background: linear-gradient(135deg, rgba(26, 115, 232, 0.15), rgba(0, 184, 148, 0.10));
    border: 2px solid var(--accent);
    position: relative;
    overflow: hidden;
}

.algo-hero-card h3 {
    font-size: 1.3rem;
    color: #0a0a1a;
    text-align: center;
    margin-bottom: 4px;
}

.algo-hero-subtitle {
    text-align: center;
    color: var(--text-muted);
    font-size: 0.95rem;
    margin-bottom: 20px !important;
}

.algo-pipeline {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0;
}

.algo-step {
    display: flex;
    align-items: center;
    gap: 16px;
    width: 100%;
    max-width: 520px;
    padding: 14px 20px;
    border-radius: 12px;
    transition: transform 0.2s, box-shadow 0.2s;
}

.algo-step:hover {
    transform: translateX(6px);
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
}

.algo-step-1 { background: rgba(26, 115, 232, 0.20); border-left: 4px solid #1a73e8; }
.algo-step-2 { background: rgba(0, 184, 148, 0.15); border-left: 4px solid #00b894; }
.algo-step-3 { background: rgba(253, 203, 110, 0.15); border-left: 4px solid #fdcb6e; }
.algo-step-4 { background: rgba(225, 112, 85, 0.15); border-left: 4px solid #e17055; }
.algo-step-5 { background: rgba(116, 185, 255, 0.15); border-left: 4px solid #74b9ff; }

.algo-step-num {
    flex-shrink: 0;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 800;
    font-size: 1.2rem;
    color: #fff;
}

.algo-step-1 .algo-step-num { background: #1a73e8; }
.algo-step-2 .algo-step-num { background: #00b894; }
.algo-step-3 .algo-step-num { background: #fdcb6e; color: #333; }
.algo-step-4 .algo-step-num { background: #e17055; }
.algo-step-5 .algo-step-num { background: #74b9ff; color: #333; }

.algo-step-title {
    font-weight: 700;
    font-size: 1.05rem;
    color: #0a0a1a;
}

.algo-step-desc {
    font-size: 0.85rem;
    color: var(--text-muted);
    margin-top: 2px;
}

.algo-arrow {
    font-size: 1.4rem;
    color: var(--text-muted);
    line-height: 1;
    padding: 4px 0;
    text-align: center;
}

/* ===== Mistake Cards ===== */
.mistake-card {
    border-color: var(--warning);
    background: rgba(253, 203, 110, 0.04);
}

.mistake-card h3 {
    color: var(--warning);
}

.mistake-block {
    margin-bottom: 20px;
    padding: 16px;
    border-radius: 10px;
    border: 1px solid var(--card-border);
}

.mistake-block-1 { background: rgba(225, 112, 85, 0.08); border-left: 4px solid #e17055; }
.mistake-block-2 { background: rgba(26, 115, 232, 0.08); border-left: 4px solid #1a73e8; }
.mistake-block-3 { background: rgba(253, 203, 110, 0.08); border-left: 4px solid #fdcb6e; }

.mistake-label {
    font-weight: 700;
    font-size: 1.05rem;
    color: #0a0a1a;
    margin-bottom: 10px;
}

.mistake-example {
    font-size: 0.95rem;
    line-height: 1.8;
}

.mistake-example p {
    margin-bottom: 4px;
}

.mistake-stop {
    color: #e17055;
    font-weight: 700;
    font-size: 1rem;
    margin-top: 8px !important;
}

.mistake-wrong {
    color: #e17055;
    text-decoration: line-through;
    opacity: 0.8;
}

.mistake-right {
    color: #00b894;
    font-weight: 700;
}

.mistake-result {
    color: #00b894;
    font-weight: 600;
}

.mistake-note {
    color: var(--text-muted);
    font-style: italic;
    font-size: 0.9rem;
    margin-top: 6px !important;
    padding-top: 6px;
    border-top: 1px solid var(--card-border);
}

/* ===== Solved counter ===== */
.solved-counter-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 16px;
}

.solved-counter {
    background: rgba(0, 184, 148, 0.15);
    border: 1px solid var(--success);
    color: var(--success);
    padding: 6px 16px;
    border-radius: 20px;
    font-weight: 700;
    font-size: 0.95rem;
}

/* ===== Solved problem check mark ===== */
.solved-check {
    color: #00b894;
    font-weight: 900;
    font-size: 1.2rem;
    min-width: 20px;
    text-align: center;
}

.solved-check.unsolved {
    color: transparent;
}

.problem-card.solved {
    border-color: rgba(0, 184, 148, 0.4);
}

.problem-card.solved .problem-header {
    border-left: 3px solid #00b894;
}

/* ===== Progressive hints ===== */
.progressive-hints {
    margin: 12px 0;
}

.hint-levels {
    margin-top: 8px;
}

.hint-level {
    background: rgba(253, 203, 110, 0.10);
    border-left: 3px solid var(--warning);
    padding: 8px 14px;
    margin-bottom: 6px;
    border-radius: 0 8px 8px 0;
    font-size: 0.9rem;
    animation: hintReveal 0.3s ease;
}

.hint-level-label {
    font-weight: 700;
    color: var(--warning);
}

@keyframes hintReveal {
    from { opacity: 0; transform: translateY(-8px); }
    to { opacity: 1; transform: translateY(0); }
}

/* ===== Problem answer input ===== */
.problem-answer-input {
    flex: 1;
    min-width: 160px;
}

.problem-check-btn {
    white-space: nowrap;
}

/* ===== Chain Rule Section ===== */
.chain-rule-example {
    margin-top: 8px;
}

.chain-rule-steps {
    margin-top: 12px;
    padding-left: 4px;
}

.chain-step {
    margin-bottom: 8px;
    padding: 6px 12px;
    border-left: 2px solid var(--accent);
    font-size: 0.95rem;
}

.chain-step-label {
    font-weight: 700;
    color: var(--accent);
    margin-right: 4px;
}

/* ===== Sprint Trainer ===== */
.sprint-header {
    display: flex;
    align-items: center;
    gap: 20px;
    margin-bottom: 16px;
    flex-wrap: wrap;
}

.sprint-timer {
    font-size: 2.5rem;
    font-weight: 800;
    color: var(--accent);
    font-variant-numeric: tabular-nums;
    min-width: 60px;
    text-align: center;
    transition: color 0.3s;
}

.sprint-timer-danger {
    color: var(--error) !important;
    animation: pulse 0.8s infinite;
}

.sprint-stats {
    display: flex;
    gap: 20px;
    font-size: 0.95rem;
    color: var(--text-muted);
}

.sprint-result-box {
    background: rgba(26, 115, 232, 0.1);
    border: 1px solid var(--accent);
    border-radius: 12px;
    padding: 20px;
    text-align: center;
    font-size: 1.1rem;
}

.sprint-result-box p {
    margin-bottom: 8px;
}

/* ===== Sidebar Stats ===== */
.sidebar-stats {
    margin-top: 24px;
    padding-top: 16px;
    border-top: 1px solid var(--card-border);
}

.sidebar-stats h4 {
    color: var(--accent);
    font-size: 0.95rem;
    margin-bottom: 10px;
}

.stat-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 0.82rem;
    color: var(--text-muted);
    padding: 3px 0;
}

.stat-row span:last-child {
    color: var(--text);
    font-weight: 600;
}
