:root {
    --bg-color: #0f172a;
    --panel-bg: #1e293b;
    --text-primary: #f8fafc;
    --text-secondary: #94a3b8;

    --primary-color: #06b6d4;
    /* Cyan */
    --secondary-color: #f59e0b;
    /* Amber */
    --accent-color: #f43f5e;
    /* Rose/Red */
    --success-color: #10b981;
    /* Emerald/Green */

    --bar-default: #06b6d4;
    --bar-compare: #f59e0b;
    --bar-swap: #f43f5e;
    --bar-sorted: #10b981;

    --header-height: 70px;
    --bottom-bar-height: 80px;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Outfit', sans-serif;
}

body {
    background-color: var(--bg-color);
    color: var(--text-primary);
    height: 100vh;
    /* Fallback for browsers that don't support dvh */
    height: 100dvh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    /* Prevent body scroll */
}

/* --- Header --- */
header {
    height: var(--header-height);
    padding: 0 20px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    background-color: rgba(15, 23, 42, 0.95);
    backdrop-filter: blur(10px);
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
    z-index: 10;
    margin-bottom: 25px;
}

.header-content {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

header h1 {
    font-size: 1.5rem;
    font-weight: 600;
    background: linear-gradient(135deg, var(--primary-color), var(--success-color));
    background-clip: text;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    text-align: center;
    flex-grow: 1;
}

.subtitle {
    font-size: 0.75rem;
    color: var(--text-secondary);
    text-align: center;
    margin-top: 2px;
}

.icon-btn {
    background: none;
    border: none;
    color: var(--text-primary);
    cursor: pointer;
    padding: 8px;
    border-radius: 8px;
    transition: background-color 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.icon-btn:hover {
    background-color: rgba(255, 255, 255, 0.1);
}

.header-spacer {
    width: 40px;
    /* Balances the menu button */
}

/* --- Container & Array Area --- */
.container {
    flex: 1;
    display: flex;
    flex-direction: column;
    position: relative;
    padding: 20px 20px var(--bottom-bar-height) 20px;
    /* Space for bottom bar */
    overflow: hidden;
}

/* Old Stats Bar Removed - styling for frame stats below */


.visualizer-grid {
    flex: 1;
    display: grid;
    grid-template-columns: 1fr;
    grid-auto-rows: 400px;
    gap: 20px;
    overflow-y: auto;
    padding-bottom: 10px;
}

/* 2 columns on larger screens */
@media (min-width: 900px) {
    .visualizer-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}

.visualizer-frame {
    background-color: rgba(30, 41, 59, 0.5);
    border-radius: 16px;
    border: 1px solid rgba(255, 255, 255, 0.05);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    position: relative;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.frame-header {
    background-color: rgba(15, 23, 42, 0.6);
    padding: 10px 15px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}

.frame-header h3 {
    font-size: 1rem;
    font-weight: 600;
    color: var(--primary-color);
}

.frame-stats {
    display: flex;
    gap: 15px;
    font-size: 0.8rem;
    color: var(--text-secondary);
}

.frame-stat span {
    color: var(--text-primary);
    font-weight: 600;
    font-variant-numeric: tabular-nums;
}

.frame-container {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: flex-end;
    padding: 10px 15px;
    gap: 1px;
    min-height: 200px;
}

.array-bar {
    /* width calculated in JS */
    background-color: var(--bar-default);
    border-radius: 4px 4px 0 0;
    transition: height 0.2s ease, transform 0.2s ease;
}

.bar-compare {
    background-color: var(--bar-compare) !important;
}

.bar-swap {
    background-color: var(--bar-swap) !important;
}

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

/* --- Settings Panel (Side Menu) --- */
.settings-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(2px);
    z-index: 90;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s ease, visibility 0.3s;
}

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

.settings-panel {
    position: fixed;
    top: 0;
    left: 0;
    height: 100%;
    width: 320px;
    max-width: 85%;
    background-color: var(--panel-bg);
    z-index: 100;
    transform: translateX(-100%);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    box-shadow: 10px 0 30px rgba(0, 0, 0, 0.5);
    display: flex;
    flex-direction: column;
    padding: 20px;
    overflow-y: auto;
}

.settings-panel.active {
    transform: translateX(0);
}

.panel-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 30px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
    padding-bottom: 15px;
}

.panel-header h2 {
    font-size: 1.25rem;
    font-weight: 600;
}

.control-group {
    margin-bottom: 25px;
}

label {
    display: block;
    margin-bottom: 8px;
    font-size: 0.9rem;
    color: var(--text-secondary);
    font-weight: 500;
}

select,
input[type="number"] {
    width: 100%;
    background-color: rgba(15, 23, 42, 0.5);
    color: var(--text-primary);
    border: 1px solid rgba(255, 255, 255, 0.1);
    padding: 12px;
    border-radius: 8px;
    font-size: 1rem;
    outline: none;
}

input[type="range"] {
    width: 100%;
    margin-top: 5px;
}

.panel-footer {
    margin-top: auto;
    padding-top: 20px;
    border-top: 1px solid rgba(255, 255, 255, 0.05);
    text-align: center;
}

.panel-footer p {
    font-size: 0.75rem;
    color: var(--text-secondary);
    line-height: 1.5;
}

/* --- Bottom Bar --- */
.bottom-bar {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: var(--bottom-bar-height);
    background-color: var(--panel-bg);
    border-top: 1px solid rgba(255, 255, 255, 0.05);
    display: flex;
    justify-content: space-around;
    align-items: center;
    padding: 0 20px;
    z-index: 50;
    box-shadow: 0 -5px 20px rgba(0, 0, 0, 0.2);
}

.btn {
    border: none;
    border-radius: 12px;
    font-weight: 600;
    font-size: 1rem;
    cursor: pointer;
    transition: transform 0.2s, opacity 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    height: 50px;
    flex: 1;
    max-width: 45%;
}

.btn:active {
    transform: scale(0.96);
}

.secondary-btn {
    background-color: transparent;
    border: 2px solid var(--primary-color);
    color: var(--primary-color);
}

.secondary-btn:hover {
    background-color: rgba(6, 182, 212, 0.1);
}

.primary-action-btn {
    background: linear-gradient(135deg, var(--primary-color), var(--success-color));
    color: #fff;
    box-shadow: 0 4px 15px rgba(16, 185, 129, 0.3);
}

.primary-action-btn:hover {
    box-shadow: 0 6px 20px rgba(16, 185, 129, 0.4);
}

.primary-action-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    background: #334155;
    color: #94a3b8;
    box-shadow: none;
}

/* --- Footer --- */
footer {
    display: none;
    /* Hidden in main view, info moved to panel/modal */
}

/* --- Mobile / Responsive --- */
@media (max-width: 600px) {
    header h1 {
        font-size: 1.25rem;
    }

    .subtitle {
        display: none;
    }

    .btn {
        font-size: 0.9rem;
        padding: 0 10px;
    }

    .bottom-bar {
        gap: 10px;
    }
}

/* --- Modal & Leaderboard --- */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(4px);
    z-index: 200;
    display: flex;
    justify-content: center;
    align-items: center;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s ease, visibility 0.3s;
}

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

.modal-content {
    background-color: var(--panel-bg);
    width: 90%;
    max-width: 600px;
    border-radius: 16px;
    border: 1px solid rgba(255, 255, 255, 0.1);
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    transform: scale(0.9);
    transition: transform 0.3s cubic-bezier(0.18, 0.89, 0.32, 1.28);
}

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

.modal-header {
    background-color: rgba(15, 23, 42, 0.8);
    padding: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}

.modal-header h2 {
    font-size: 1.5rem;
    background: linear-gradient(135deg, var(--secondary-color), var(--accent-color));
    background-clip: text;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

.close-modal {
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 2rem;
    line-height: 1;
    cursor: pointer;
    padding: 0 10px;
    transition: color 0.2s;
}

.close-modal:hover {
    color: var(--text-primary);
}

.modal-body {
    padding: 20px;
    max-height: 70vh;
    overflow-y: auto;
}

.leaderboard-table-wrapper {
    overflow-x: auto;
}

#leaderboard-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.95rem;
}

#leaderboard-table th,
#leaderboard-table td {
    padding: 12px 15px;
    text-align: left;
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}

#leaderboard-table th {
    background-color: rgba(0, 0, 0, 0.2);
    font-weight: 600;
    color: var(--primary-color);
    text-transform: uppercase;
    font-size: 0.8rem;
    letter-spacing: 1px;
}

#leaderboard-table tr:last-child td {
    border-bottom: none;
}

#leaderboard-table tr:hover {
    background-color: rgba(255, 255, 255, 0.02);
}

.rank-1 {
    color: #FFD700;
    font-weight: bold;
}

/* Gold */
.rank-2 {
    color: #C0C0C0;
    font-weight: bold;
}

/* Silver */
.rank-3 {
    color: #CD7F32;
    font-weight: bold;
}

/* Bronze */