#app {
    display: grid;
    /* The stage row absorbs leftover height (minmax(0,1fr)) so changes to
       any other row don't reflow it. The stack section pins itself to a
       stable height, so adding/removing slots doesn't push the canvas. */
    grid-template-rows: auto minmax(0, 1fr) auto auto auto auto;
    height: 100dvh;
    width: 100vw;
    overflow: hidden;
}

/* Portrait/mobile: #control-panel is transparent to layout — its children
   become direct grid items of #app, matching the original single-column
   row order (glitchify → stack → starter → bottom-bar). */
#control-panel { display: contents; }

#title-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--hf-space-3) var(--hf-space-4);
    padding-top: calc(var(--hf-space-3) + env(safe-area-inset-top));
    border-bottom: var(--hf-border-width) solid var(--hf-border-subtle);
    background: var(--hf-bg-surface);
}

.title {
    font-family: var(--hf-font-family);
    font-size: clamp(20px, 4vw, 26px);
    font-weight: var(--hf-weight-bold);
    letter-spacing: var(--hf-tracking-wide);
    color: var(--hf-text-bright);
    user-select: none;
}

.title-actions {
    display: flex;
    align-items: center;
    gap: var(--hf-space-2);
}

#stage {
    position: relative;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 0;
    background: var(--hf-bg-base);
}

.canvas-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
}

.canvas-wrap canvas {
    max-width: 100%;
    max-height: 100%;
    width: 100%;
    height: 100%;
    object-fit: contain;
    display: block;
}

#app.mirrored .canvas-wrap canvas { transform: scaleX(-1); }

/* ============================================================
   GLITCHIFY row
   ============================================================ */

#glitchify-row {
    display: flex;
    align-items: stretch;
    gap: var(--hf-space-2);
    padding: var(--hf-space-3) var(--hf-space-4);
    border-top: var(--hf-border-width) solid var(--hf-border-subtle);
    background: var(--hf-bg-surface);
}

/* ============================================================
   Stack section
   ============================================================ */

#stack-section {
    border-top: var(--hf-border-width) solid var(--hf-border-subtle);
    background: var(--hf-bg-surface);
    padding: var(--hf-space-2) var(--hf-space-4) var(--hf-space-3);
    position: relative;
    /* Pinned height so canvas above doesn't reflow when GLITCHIFY changes
       the slot count. The list scrolls internally when the stack overflows. */
    height: 24vh;
    min-height: 150px;
    max-height: 280px;
    overflow-y: auto;
    scrollbar-width: thin;
    scrollbar-color: var(--hf-border-subtle) transparent;
}

#stack-section::-webkit-scrollbar { width: 6px; }
#stack-section::-webkit-scrollbar-thumb {
    background: var(--hf-border-subtle);
    border-radius: 3px;
}

.stack-header {
    display: flex;
    align-items: center;
    gap: var(--hf-space-3);
    margin-bottom: var(--hf-space-2);
    position: relative;
}

.stack-label {
    color: var(--hf-text-dim);
    font-size: var(--hf-size-xs);
    letter-spacing: var(--hf-tracking-wide);
    text-transform: uppercase;
    font-family: var(--hf-font-family-mono);
}

.stack-list {
    display: flex;
    flex-direction: column;
    gap: var(--hf-space-2);
}

/* ============================================================
   Starter rail
   ============================================================ */

.starter-rail-wrap {
    display: flex;
    align-items: center;
    gap: var(--hf-space-3);
    border-top: var(--hf-border-width) solid var(--hf-border-subtle);
    background: var(--hf-bg-surface);
    padding: var(--hf-space-2) var(--hf-space-4);
    min-width: 0;
    overflow: hidden;
}

.starter-label {
    color: var(--hf-text-dim);
    font-size: var(--hf-size-xs);
    letter-spacing: var(--hf-tracking-wide);
    text-transform: uppercase;
    font-family: var(--hf-font-family-mono);
    flex-shrink: 0;
}

.starter-rail {
    display: flex;
    gap: var(--hf-space-2);
    overflow-x: auto;
    overflow-y: hidden;
    padding: 2px 0;
    scrollbar-width: none;
    scroll-snap-type: x proximity;
    -webkit-overflow-scrolling: touch;
    flex: 1;
    min-width: 0;
}

.starter-rail::-webkit-scrollbar { display: none; }

/* ============================================================
   Bottom bar
   ============================================================ */

#bottom-bar {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    gap: var(--hf-space-3);
    padding: var(--hf-space-3) var(--hf-space-4);
    padding-bottom: calc(var(--hf-space-3) + env(safe-area-inset-bottom));
    background: var(--hf-bg-surface);
    border-top: var(--hf-border-width) solid var(--hf-border-subtle);
}

.bottom-left {
    display: flex;
    align-items: center;
    gap: var(--hf-space-2);
    flex-wrap: wrap;
}

.bottom-right {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: var(--hf-space-2);
    overflow: hidden;
}

/* ============================================================
   Landscape / desktop layout — controls left, canvas right.
   Matches the noisedeck split. Mobile stays in the stacked
   layout above. Same 901px breakpoint as noisedeck.
   ============================================================ */
@media (min-width: 901px) {
    #app {
        grid-template-columns: 500px minmax(0, 1fr);
        grid-template-rows: auto minmax(0, 1fr);
        grid-template-areas:
            "title title"
            "panel stage";
    }
    #title-bar     { grid-area: title; }
    #stage         { grid-area: stage; }
    #control-panel {
        grid-area: panel;
        display: flex;
        flex-direction: column;
        min-height: 0;
        overflow: hidden;
        border-right: var(--hf-border-width) solid var(--hf-border-subtle);
    }
    /* Glitchify row sits at the top of the panel — no border-top since
       the title bar already has its own bottom border. */
    #control-panel #glitchify-row { border-top: none; }

    /* Stack section flexes to fill the available height between the
       fixed-size rows above and below it, scrolling internally. The
       portrait pin-to-vh is unnecessary in landscape — the canvas no
       longer reflows when slot count changes. */
    #control-panel #stack-section {
        flex: 1 1 0;
        height: auto;
        min-height: 0;
        max-height: none;
    }
}

@media (max-width: 600px) {
    .title { font-size: 18px; }
    .bottom-left { gap: 4px; }
    #glitchify-row { gap: var(--hf-space-2); padding: var(--hf-space-2) var(--hf-space-3); }
    #stack-section {
        padding: var(--hf-space-2) var(--hf-space-3) var(--hf-space-3);
        height: 28vh;
        min-height: 140px;
        max-height: 340px;
    }
    .starter-rail-wrap { padding: var(--hf-space-2) var(--hf-space-3); }
    #bottom-bar {
        grid-template-columns: 1fr auto 1fr;
        padding: var(--hf-space-2) var(--hf-space-2);
    }
}
