/* ===== CSS Variables / Theme ===== */
:root {
    --bg-primary: #0f0f13;
    --bg-secondary: #1a1a24;
    --bg-card: #1e1e2e;
    --bg-hover: #2a2a3c;
    --text-primary: #e8e8f0;
    --text-secondary: #9898b0;
    --text-muted: #6a6a82;
    --accent: #8b5cf6;
    --accent-hover: #7c3aed;
    --border: #2e2e42;

    /* Bar colors */
    --bar-default: #6366f1;
    --bar-comparing: #facc15;
    --bar-swapping: #ef4444;
    --bar-pivot: #f97316;
    --bar-sorted: #22c55e;
    --bar-active: #38bdf8;

    /* Misc */
    --radius: 10px;
    --radius-sm: 6px;
    --transition: 0.2s ease;
}

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

html {
    font-size: 16px;
    -webkit-font-smoothing: antialiased;
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    min-height: 100vh;
    overflow-x: hidden;
}

/* ===== App Container ===== */
.app-container {
    max-width: 1100px;
    margin: 0 auto;
    padding: 2rem 1.5rem 3rem;
}

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

.title {
    font-size: 2.4rem;
    font-weight: 800;
    background: linear-gradient(135deg, #8b5cf6 0%, #6366f1 50%, #38bdf8 100%);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    letter-spacing: -0.02em;
}

.subtitle {
    color: var(--text-secondary);
    font-size: 1rem;
    margin-top: 0.4rem;
    font-weight: 400;
}

/* ===== Controls ===== */
.controls {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-end;
    gap: 1.2rem;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.2rem 1.5rem;
    margin-bottom: 1.2rem;
}

.control-group {
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
    flex: 1;
    min-width: 140px;
}

.control-group label {
    font-size: 0.78rem;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.06em;
}

/* Select */
select {
    appearance: none;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    color: var(--text-primary);
    padding: 0.55rem 0.8rem;
    border-radius: var(--radius-sm);
    font-size: 0.9rem;
    font-family: inherit;
    cursor: pointer;
    transition: border-color var(--transition);
    background-image: url("data:image/svg+xml,%3Csvg width='10' height='6' viewBox='0 0 10 6' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M1 1L5 5L9 1' stroke='%239898b0' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 0.7rem center;
    padding-right: 2rem;
}

select:hover,
select:focus {
    border-color: var(--accent);
    outline: none;
}

/* Range Slider */
input[type="range"] {
    -webkit-appearance: none;
    appearance: none;
    height: 6px;
    border-radius: 3px;
    background: var(--bg-secondary);
    outline: none;
    margin-top: 0.3rem;
}

input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: var(--accent);
    cursor: pointer;
    border: 2px solid var(--bg-primary);
    transition: transform var(--transition);
}

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

input[type="range"]::-moz-range-thumb {
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: var(--accent);
    cursor: pointer;
    border: 2px solid var(--bg-primary);
}

/* Buttons */
.control-buttons {
    display: flex;
    gap: 0.6rem;
    flex-shrink: 0;
}

.btn {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.55rem 1.1rem;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 0.85rem;
    font-weight: 600;
    font-family: inherit;
    cursor: pointer;
    transition: all var(--transition);
    white-space: nowrap;
}

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

.btn-icon {
    background: var(--bg-secondary);
    color: var(--text-primary);
    border: 1px solid var(--border);
    padding: 0.45rem 0.55rem;
    line-height: 1;
}

.btn-icon:hover {
    background: var(--bg-hover);
    border-color: var(--text-muted);
}

.btn-icon.muted {
    color: var(--text-muted);
    border-color: rgba(239, 68, 68, 0.4);
}

/* Audio controls */
.audio-controls {
    min-width: 100px !important;
    flex: 0.6 !important;
}

.audio-row {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.audio-row input[type="range"] {
    flex: 1;
    margin-top: 0;
}

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

.btn-primary:hover:not(:disabled) {
    background: var(--accent-hover);
    transform: translateY(-1px);
    box-shadow: 0 4px 15px rgba(139, 92, 246, 0.35);
}

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

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

.btn-danger {
    background: #dc2626;
    color: #fff;
}

.btn-danger:hover:not(:disabled) {
    background: #b91c1c;
}

/* ===== Algorithm Info Card ===== */
.algo-info {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1rem 1.5rem;
    margin-bottom: 1.2rem;
}

.algo-info-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 0.8rem;
}

.algo-info-header h2 {
    font-size: 1.15rem;
    font-weight: 700;
}

.complexity-badges {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
}

.badge {
    font-size: 0.75rem;
    padding: 0.25rem 0.65rem;
    border-radius: 20px;
    font-weight: 500;
    letter-spacing: 0.02em;
}

.badge strong {
    font-weight: 700;
}

.badge-time {
    background: rgba(99, 102, 241, 0.15);
    color: #a5b4fc;
    border: 1px solid rgba(99, 102, 241, 0.3);
}

.badge-worst {
    background: rgba(239, 68, 68, 0.12);
    color: #fca5a5;
    border: 1px solid rgba(239, 68, 68, 0.25);
}

.badge-space {
    background: rgba(34, 197, 94, 0.12);
    color: #86efac;
    border: 1px solid rgba(34, 197, 94, 0.25);
}

.algo-description {
    color: var(--text-secondary);
    font-size: 0.88rem;
    margin-top: 0.6rem;
    line-height: 1.5;
}

/* ===== Visualizer ===== */
.visualizer-container {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.5rem;
    margin-bottom: 1.2rem;
    min-height: 380px;
    display: flex;
    align-items: flex-end;
}

.bars-container {
    display: flex;
    align-items: flex-end;
    justify-content: center;
    gap: 2px;
    width: 100%;
    height: 340px;
    position: relative;
}

/* Wrapper column: owns the flex sizing, label sits here (outside transform) */
.bar-col {
    flex: 1;
    min-width: 2px;
    max-width: 60px;
    height: 100%;
    position: relative;
}

/* Visual bar: GPU-composited via transform: scaleY() — no reflow on update */
.bar {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 100%;
    border-radius: 3px 3px 0 0;
    background: var(--bar-default);
    transform-origin: bottom center;
    will-change: transform;
}

.bar.comparing {
    background: var(--bar-comparing);
    box-shadow: 0 0 12px rgba(250, 204, 21, 0.4);
}

.bar.swapping {
    background: var(--bar-swapping);
    box-shadow: 0 0 12px rgba(239, 68, 68, 0.4);
}

.bar.pivot {
    background: var(--bar-pivot);
    box-shadow: 0 0 12px rgba(249, 115, 22, 0.4);
}

.bar.sorted {
    background: var(--bar-sorted);
}

.bar.active {
    background: var(--bar-active);
    box-shadow: 0 0 12px rgba(56, 189, 248, 0.4);
}

/* Value label – sits in .bar-col (outside .bar's scaleY transform) */
.bar-col .bar-value {
    position: absolute;
    /* bottom is set dynamically via JS to track the bar's visual top */
    left: 50%;
    transform: translateX(-50%);
    font-size: 0.65rem;
    font-weight: 600;
    color: var(--text-secondary);
    white-space: nowrap;
    pointer-events: none;
    z-index: 1;
}

/* ===== Stats ===== */
.stats {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 0.8rem;
    margin-bottom: 1.2rem;
}

.stat-item {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 0.8rem 1rem;
    text-align: center;
}

.stat-label {
    display: block;
    font-size: 0.72rem;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-bottom: 0.25rem;
}

.stat-value {
    display: block;
    font-size: 1.3rem;
    font-weight: 700;
    color: var(--text-primary);
    font-variant-numeric: tabular-nums;
}

/* ===== Legend ===== */
.legend {
    display: flex;
    justify-content: center;
    flex-wrap: wrap;
    gap: 1.2rem;
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    font-size: 0.8rem;
    color: var(--text-secondary);
}

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

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

    .title {
        font-size: 1.7rem;
    }

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

    .control-buttons {
        justify-content: center;
    }

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

    .stats {
        grid-template-columns: repeat(2, 1fr);
    }

    .bars-container {
        height: 260px;
    }

    .visualizer-container {
        min-height: 300px;
    }
}

@media (max-width: 480px) {
    .stats {
        grid-template-columns: 1fr 1fr;
    }

    .legend {
        gap: 0.8rem;
    }
}

/* ===== Completion animation ===== */
@keyframes sortedPulse {
    0% { filter: brightness(1); }
    50% { filter: brightness(1.4); }
    100% { filter: brightness(1); }
}

.bar.sorted-anim {
    animation: sortedPulse 0.4s ease;
}

/* ===== Footer ===== */
.footer {
    margin-top: 2rem;
    padding-top: 1.5rem;
    border-top: 1px solid var(--border);
    text-align: center;
}

.footer p {
    font-size: 0.8rem;
    color: var(--text-muted);
}

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

.footer a:hover {
    color: var(--accent-hover);
    text-decoration: underline;
}
