:root {
    --bg: #e8f4e8;
    --grid: #c5dfc5;
    --ink: #2a2a2a;
    --green: #4caf50;
    --lime: #8bc34a;
    --red: #f44336;
    --orange: #ff9800;
    --yellow: #ffeb3b;
    --sky: #29b6f6;
    --purple: #ab47bc;
    --white: #fffdf5;
    --border: 3px solid #2a2a2a;
    --shadow: 4px 4px 0px #2a2a2a;
}

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

body {
    font-family: 'Patrick Hand', cursive;
    background-color: var(--bg);
    background-image:
        linear-gradient(var(--grid) 1px, transparent 1px),
        linear-gradient(90deg, var(--grid) 1px, transparent 1px);
    background-size: 30px 30px;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 30px 20px 60px;
    overflow-x: hidden;
}

body::before {
    content: '';
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    pointer-events: none;
    background-image:
        radial-gradient(circle at 10% 20%, rgba(255,235,59,0.15) 0%, transparent 40%),
        radial-gradient(circle at 90% 80%, rgba(41,182,246,0.15) 0%, transparent 40%),
        radial-gradient(circle at 80% 10%, rgba(171,71,188,0.1) 0%, transparent 30%);
    z-index: 0;
}

h1 {
    font-family: 'Fredoka One', cursive;
    font-size: clamp(2rem, 5vw, 3.2rem);
    color: var(--ink);
    text-shadow: 3px 3px 0 var(--lime), 6px 6px 0 rgba(0,0,0,0.15);
    margin-bottom: 8px;
    position: relative;
    z-index: 1;
    letter-spacing: 1px;
}

.subtitle {
    font-size: 1rem;
    color: #555;
    margin-bottom: 28px;
    position: relative;
    z-index: 1;
}

.doodle-guy {
    font-size: 2.2rem;
    display: inline-block;
    animation: jump 0.7s ease-in-out infinite alternate;
    margin-right: 10px;
}
@keyframes jump {
    from { transform: translateY(0px) scaleY(1); }
    to   { transform: translateY(-14px) scaleY(1.08); }
}

#controls {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    justify-content: center;
    margin-bottom: 20px;
    position: relative;
    z-index: 1;
}

select {
    font-family: 'Fredoka One', cursive;
    font-size: 1rem;
    padding: 10px 16px;
    background: var(--white);
    border: var(--border);
    border-radius: 14px;
    box-shadow: var(--shadow);
    cursor: pointer;
    color: var(--ink);
    outline: none;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='14' height='10' viewBox='0 0 14 10'%3E%3Cpath d='M7 10L0 0h14z' fill='%232a2a2a'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 14px center;
    padding-right: 36px;
}

button {
    font-family: 'Fredoka One', cursive;
    font-size: 1rem;
    padding: 10px 20px;
    border: var(--border);
    border-radius: 14px;
    cursor: pointer;
    box-shadow: var(--shadow);
    transition: transform 0.1s, box-shadow 0.1s;
    position: relative;
    overflow: hidden;
}
button:active {
    transform: translate(3px, 3px);
    box-shadow: 1px 1px 0px #2a2a2a;
}

#btn-generate {
    background: var(--yellow);
    color: var(--ink);
}
#btn-generate:hover { background: #ffe000; }

#btn-sort {
    background: var(--lime);
    color: var(--ink);
}
#btn-sort:hover { background: #78c029; }

.speed-wrap {
    display: flex;
    align-items: center;
    gap: 8px;
    background: var(--white);
    border: var(--border);
    border-radius: 14px;
    box-shadow: var(--shadow);
    padding: 6px 14px;
    font-size: 0.9rem;
    font-family: 'Fredoka One', cursive;
    color: var(--ink);
}
input[type=range] {
    -webkit-appearance: none;
    width: 90px;
    height: 6px;
    background: #ccc;
    border-radius: 3px;
    border: 2px solid var(--ink);
    outline: none;
}
input[type=range]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 18px; height: 18px;
    background: var(--sky);
    border: 2px solid var(--ink);
    border-radius: 50%;
    cursor: pointer;
    box-shadow: 2px 2px 0 var(--ink);
}

#step-panel {
    position: relative;
    z-index: 1;
    background: var(--white);
    border: var(--border);
    border-radius: 16px;
    box-shadow: var(--shadow);
    padding: 10px 24px;
    margin-bottom: 16px;
    min-width: 260px;
    max-width: 520px;
    text-align: center;
}
#step-panel::before {
    content: '💬';
    position: absolute;
    left: -18px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 1.4rem;
}
#step-text {
    font-size: 1.05rem;
    color: var(--ink);
    line-height: 1.4;
}

#description {
    position: relative;
    z-index: 1;
    text-align: center;
    max-width: 600px;
    margin: 0 auto 20px;
    font-size: 0.95rem;
    font-family: 'Patrick Hand', cursive;
    color: #444;
    background: var(--white);
    border: var(--border);
    border-radius: 14px;
    box-shadow: var(--shadow);
    padding: 12px 20px;
    line-height: 1.6;
    min-height: 20px;
}

#arena {
    position: relative;
    z-index: 1;
    background: var(--white);
    border: 3px solid var(--ink);
    border-radius: 20px;
    box-shadow: 6px 6px 0 var(--ink);
    padding: 24px 24px 8px;
    width: min(700px, 95vw);
}

#arena::after {
    content: '';
    display: block;
    height: 4px;
    background: repeating-linear-gradient(
        90deg,
        var(--ink) 0px, var(--ink) 12px,
        transparent 12px, transparent 18px
    );
    border-radius: 2px;
    margin-top: 10px;
}

#array-container {
    display: flex;
    justify-content: center;
    align-items: flex-end;
    height: 220px;
    gap: 8px;
}

.bar {
    position: relative;
    width: 44px;
    border: 2.5px solid var(--ink);
    border-radius: 10px 10px 4px 4px;
    background: var(--sky);
    display: flex;
    justify-content: center;
    align-items: flex-start;
    padding-top: 4px;
    color: var(--ink);
    font-family: 'Fredoka One', cursive;
    font-size: 0.85rem;
    box-shadow: 3px 3px 0 var(--ink);
    transition: height 0.25s cubic-bezier(.34,1.56,.64,1),
                background-color 0.2s ease,
                transform 0.15s ease;
    transform-origin: bottom center;
}

.bar::after {
    content: '';
    position: absolute;
    bottom: -6px;
    left: -4px;
    right: -4px;
    height: 8px;
    background: var(--ink);
    border-radius: 4px;
}

.bar.comparing {
    background: var(--red) !important;
    transform: scaleX(1.06) translateY(-6px);
    animation: wobble 0.3s ease;
}
.bar.key-elem {
    background: var(--orange) !important;
    transform: scaleX(1.06) translateY(-8px);
}
.bar.sorted {
    background: var(--green) !important;
}
.bar.done {
    background: var(--lime) !important;
    animation: celebrate 0.4s ease forwards;
}

@keyframes wobble {
    0%   { transform: scaleX(1.06) translateY(-6px) rotate(-2deg); }
    50%  { transform: scaleX(1.06) translateY(-6px) rotate(2deg); }
    100% { transform: scaleX(1.06) translateY(-6px) rotate(0deg); }
}
@keyframes celebrate {
    0%   { transform: translateY(0); }
    30%  { transform: translateY(-18px) scaleY(1.1); }
    60%  { transform: translateY(-4px); }
    100% { transform: translateY(0); }
}
@keyframes popIn {
    0%   { transform: scaleY(0); opacity: 0; }
    70%  { transform: scaleY(1.1); }
    100% { transform: scaleY(1); opacity: 1; }
}
.bar.pop-in {
    animation: popIn 0.35s cubic-bezier(.34,1.56,.64,1) forwards;
}

.star {
    position: fixed;
    font-size: 1.8rem;
    pointer-events: none;
    z-index: 999;
    animation: starFly 1s ease-out forwards;
}
@keyframes starFly {
    0%   { opacity: 1; transform: translate(0,0) scale(1) rotate(0deg); }
    100% { opacity: 0; transform: translate(var(--tx), var(--ty)) scale(0.3) rotate(360deg); }
}

.legend {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    justify-content: center;
    margin-top: 22px;
    position: relative;
    z-index: 1;
}
.legend-item {
    display: flex;
    align-items: center;
    gap: 6px;
    background: var(--white);
    border: 2px solid var(--ink);
    border-radius: 20px;
    padding: 4px 12px;
    font-size: 0.82rem;
    font-family: 'Fredoka One', cursive;
    box-shadow: 2px 2px 0 var(--ink);
}
.legend-dot {
    width: 14px; height: 14px;
    border-radius: 4px;
    border: 2px solid var(--ink);
}

.cloud {
    position: fixed;
    font-size: 2.8rem;
    opacity: 0.25;
    pointer-events: none;
    z-index: 0;
    animation: drift linear infinite;
}
.cloud:nth-child(1) { top: 8%;  animation-duration: 22s; left: -80px; }
.cloud:nth-child(2) { top: 35%; animation-duration: 30s; left: -80px; animation-delay: -10s; }
.cloud:nth-child(3) { top: 65%; animation-duration: 18s; left: -80px; animation-delay: -5s; }
@keyframes drift {
    from { transform: translateX(0); }
    to   { transform: translateX(110vw); }
}




#heap-arena {
    position: relative;
    z-index: 1;
    background: var(--white);
    border: 3px solid var(--ink);
    border-radius: 20px;
    box-shadow: 6px 6px 0 var(--ink);
    padding: 18px 12px 16px;
    width: min(700px, 95vw);
    margin-top: 0;
}


#heap-arena::after {
    content: '';
    display: block;
    height: 4px;
    background: repeating-linear-gradient(
        90deg,
        var(--ink) 0px, var(--ink) 12px,
        transparent 12px, transparent 18px
    );
    border-radius: 2px;
    margin-top: 12px;
}

#heap-arena .heap-label {
    font-family: 'Fredoka One', cursive;
    font-size: 0.95rem;
    color: #888;
    text-align: center;
    margin-bottom: 6px;
    letter-spacing: 1px;
}

#heap-svg {
    width: 100%;
    height: auto;
    display: block;
    overflow: visible;
}


@keyframes heapPulse {
    from { r: 24; }
    to   { r: 28; }
}


#heap-svg circle {
    transition: fill 0.2s ease, stroke 0.15s ease;
}