:root {
    --bg: #1d2021;
    --bg1: #282828;
    --bg2: #3c3836;
    --bg3: #504945;
    --fg: #ebdbb2;
    --fg-dim: #a89984;
    --muted: #665c54;
    --red: #cc241d;
    --red-bright: #fb4934;
    --green: #b8bb26;
    --yellow: #fabd2f;
    --blue: #83a598;
    --orange: #fe8019;
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

html, body { height: 100%; }

body {
    background: var(--bg);
    color: var(--fg);
    font: 16px/1.4 system-ui, sans-serif;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

main { flex: 1; overflow: hidden; position: relative; }

.screen {
    position: absolute;
    inset: 0;
    display: none;
    flex-direction: column;
    padding: 16px;
    padding-bottom: calc(16px + env(safe-area-inset-bottom));
    overflow-y: auto;
}

.screen[data-active="true"] { display: flex; }

.screen-header { display: flex; align-items: center; gap: 12px; margin-bottom: 16px; }
.screen-header h1 { font-size: 18px; font-weight: 600; }

.tab-bar {
    display: flex;
    border-top: 1px solid var(--bg2);
    background: var(--bg);
    padding-bottom: env(safe-area-inset-bottom);
}

.tab-bar button {
    flex: 1;
    background: none;
    border: none;
    color: var(--fg-dim);
    font: inherit;
    font-size: 13px;
    padding: 14px 0;
}

.tab-bar button[data-active="true"] { color: var(--yellow); }

.chips { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; margin-bottom: 8px; }

.chip {
    background: none;
    border: 1px solid var(--muted);
    border-radius: 16px;
    color: var(--fg);
    font: inherit;
    font-size: 13px;
    padding: 5px 14px;
}

.chip[data-on="true"] { border-color: var(--green); color: var(--green); }

.record-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 16px;
    min-height: 220px;
}

#record-btn {
    width: 120px;
    height: 120px;
    border-radius: 50%;
    border: none;
    background: var(--red);
    box-shadow: 0 0 0 10px color-mix(in srgb, var(--red) 20%, transparent);
}

#record-btn[data-recording="true"] {
    background: var(--red-bright);
    animation: pulse 1s infinite alternate;
    border-radius: 24px;
    width: 90px;
    height: 90px;
}

@keyframes pulse { from { box-shadow: 0 0 0 6px color-mix(in srgb, var(--red-bright) 25%, transparent); }
                   to   { box-shadow: 0 0 0 16px color-mix(in srgb, var(--red-bright) 10%, transparent); } }

.hint { color: var(--fg-dim); font-size: 13px; text-align: center; }
.hint.warn { color: var(--yellow); }

.recent h2, .label { font-size: 12px; text-transform: uppercase; color: var(--fg-dim); margin-bottom: 8px; }

.clip-list { list-style: none; display: flex; flex-direction: column; gap: 8px; }

.clip-row {
    display: flex;
    align-items: center;
    gap: 10px;
    background: var(--bg1);
    border-radius: 8px;
    padding: 10px;
}

.clip-row button { background: none; border: none; color: var(--fg); font: inherit; font-size: 18px; }
.clip-row .clip-main { flex: 1; min-width: 0; }
.clip-row .clip-name { font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.clip-row canvas { display: block; width: 100%; height: 18px; margin: 4px 0; }
.clip-row .clip-meta { font-size: 12px; color: var(--fg-dim); }

#library-search {
    flex: 1;
    background: var(--bg1);
    border: 1px solid var(--bg2);
    border-radius: 8px;
    color: var(--fg);
    font: inherit;
    padding: 8px 12px;
}

.menu-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, .5);
    display: flex;
    align-items: flex-end;
    /* Lift the bottom-anchored sheet above the soft keyboard (set by
       js/keyboard-inset.js from the visual viewport). */
    padding-bottom: var(--kb-inset, 0px);
    box-sizing: border-box;
    transition: padding-bottom .12s ease;
}

.menu-sheet {
    background: var(--bg1);
    border-radius: 12px 12px 0 0;
    width: 100%;
    padding: 8px 0 calc(8px + env(safe-area-inset-bottom));
}

.menu-sheet button {
    display: block;
    width: 100%;
    background: none;
    border: none;
    color: var(--fg);
    font: inherit;
    padding: 14px 20px;
    text-align: left;
}

.menu-sheet button.danger { color: var(--red-bright); }

/* Add-to-song destination picker: options flow left→right and wrap,
   while the disabled title heading spans the full width on its own row. */
.menu-sheet--grid {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    padding: 8px 12px calc(8px + env(safe-area-inset-bottom));
}

.menu-sheet--grid button[disabled] {
    flex: 1 0 100%;
    width: auto;
    padding: 8px 8px;
    color: var(--muted);
    text-align: left;
}

.menu-sheet--grid button:not([disabled]) {
    flex: 1 1 46%;
    min-width: 120px;
    width: auto;
    min-height: 44px;
    background: var(--bg2);
    border: 1px solid var(--bg3);
    border-radius: 8px;
    padding: 12px 14px;
    text-align: center;
}

.menu-msg {
    margin: 0;
    padding: 14px 20px 8px;
    color: var(--fg-dim, var(--fg));
    line-height: 1.4;
}

.menu-sheet input {
    width: calc(100% - 40px);
    margin: 8px 20px;
    background: var(--bg2);
    border: 1px solid var(--bg3);
    border-radius: 6px;
    color: var(--fg);
    font: inherit;
    font-size: 16px;   /* ≥16px stops iOS from zooming (and shifting) on focus */
    padding: 8px 10px;
}

.song-header { gap: 10px; }

/* Header actions (mix / export / click) styled as tabs, matching the bottom
   tab bar: dim text with an underline indicator on press / when toggled on. */
.song-header button {
    background: none;
    border: none;
    border-bottom: 2px solid transparent;
    border-radius: 0;
    color: var(--fg-dim);
    font: inherit;
    font-size: 13px;
    padding: 6px 4px;
}
.song-header button:active { color: var(--yellow); border-bottom-color: var(--yellow); }
.song-header button[data-on="true"] { color: var(--green); border-bottom-color: var(--green); }

.song-grid {
    display: grid;
    gap: 6px;
    align-items: center;
    margin-bottom: 12px;
}

.song-grid .grid-corner, .song-grid .scene-head, .song-grid .track-head { font-size: 12px; color: var(--fg-dim); text-align: center; }
.song-grid .scene-head { padding: 6px 0; background: none; border: none; color: var(--fg-dim); font: inherit; }
.song-grid .track-head { display: flex; align-items: center; justify-content: center; text-align: center; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; background: none; border: none; color: var(--fg-dim); font: inherit; cursor: pointer; }

.grid-cell {
    height: 44px;
    border-radius: 8px;
    border: 1px dashed var(--muted);
    background: none;
    padding: 0;
}
.grid-cell[data-state="filled"]   { border: none; background: color-mix(in srgb, var(--blue) 45%, var(--bg1)); }
.grid-cell[data-state="armed"]    { border: none; background: var(--blue); animation: pulse 0.5s infinite alternate; }
.grid-cell[data-state="playing"]  { border: none; background: var(--blue); box-shadow: 0 0 8px var(--blue); }
.grid-cell[data-state="stopping"] { border: none; background: color-mix(in srgb, var(--blue) 60%, var(--bg1)); animation: pulse 0.5s infinite alternate; }

/* midi cells tint green where audio is blue */
.grid-cell[data-kind="midi"][data-state="filled"]   { background: color-mix(in srgb, var(--green) 40%, var(--bg1)); }
.grid-cell[data-kind="midi"][data-state="armed"]    { background: var(--green); }
.grid-cell[data-kind="midi"][data-state="playing"]  { background: var(--green); box-shadow: 0 0 8px var(--green); }
.grid-cell[data-kind="midi"][data-state="stopping"] { background: color-mix(in srgb, var(--green) 55%, var(--bg1)); }

.grid-add { height: 44px; border-radius: 8px; border: 1px dashed var(--bg3); background: none; color: var(--fg-dim); font: inherit; }

.chain { border-top: 1px solid var(--bg2); padding-top: 10px; }
.chain .label { display: flex; justify-content: space-between; align-items: center; }
.chain-blocks { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 8px; }
.chain-block {
    background: var(--blue);
    color: var(--bg);
    border: none;
    border-radius: 6px;
    padding: 6px 12px;
    font: inherit;
    font-size: 13px;
}
.chain-block[data-active="true"] { box-shadow: 0 0 8px var(--blue); }
.chain-add { background: none; border: 1px dashed var(--muted); border-radius: 6px; color: var(--fg-dim); padding: 6px 12px; font: inherit; }

.loopify {
    position: fixed;
    inset: 0;
    background: var(--bg);
    z-index: 10;
    display: flex;
    flex-direction: column;
    padding: 16px;
    gap: 12px;
}
.loopify header { display: flex; justify-content: space-between; align-items: center; }
.loopify header .save { color: var(--green); background: none; border: none; font: inherit; }
.loopify header .close { color: var(--fg); background: none; border: none; font: inherit; font-size: 18px; }

.loopify-wave {
    position: relative;
    background: var(--bg1);
    border-radius: 8px;
    height: 120px;
    touch-action: none;       /* we handle pan/pinch ourselves */
    overflow: hidden;
}
.loopify-wave canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
.loopify-handle {
    position: absolute;
    top: 0; bottom: 0;
    width: 28px;              /* generous touch target */
    margin-left: -14px;
    cursor: ew-resize;
    touch-action: none;       /* not inherited from .loopify-wave; the handles sit at
                                 the screen edges, so without this the drag triggers
                                 the phone's edge-swipe back gesture */
}
.loopify-handle::after {
    content: '';
    position: absolute;
    top: 0; bottom: 0; left: 13px;
    width: 3px;
    background: var(--yellow);
}
.loopify-region { position: absolute; top: 0; bottom: 0; background: color-mix(in srgb, var(--blue) 18%, transparent); }

.loopify-row { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; align-items: center; }
.loopify-card { background: var(--bg1); border-radius: 8px; padding: 10px; display: flex; flex-direction: column; gap: 8px; }
.loopify-card .row { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.loopify input[type="range"] { flex: 1; accent-color: var(--blue); }

/* ---- piano-roll overlay ---- */
.piano-roll {
    position: fixed;
    inset: 0;
    z-index: 20;
    background: var(--bg);
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: 16px;
    padding-bottom: calc(16px + env(safe-area-inset-bottom));
}
.piano-roll header { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.piano-roll header .clip-name { flex: 1; text-align: center; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.piano-roll header button { background: none; border: none; color: var(--fg); font: inherit; font-size: 18px; }
.piano-roll header .save { color: var(--green); }
.pr-chips { display: flex; gap: 8px; flex-wrap: wrap; }
.pr-scroll {
    flex: 1;
    overflow: auto;
    border: 1px solid var(--bg2);
    border-radius: 8px;
    overscroll-behavior: contain;
}
.pr-canvas-wrap { position: relative; }
.pr-grid { display: block; }
.pr-note {
    position: absolute;
    background: var(--green);
    border: 1px solid color-mix(in srgb, var(--green) 55%, black);
    border-radius: 3px;
    touch-action: none;   /* notes drag; the background scrolls */
}
.pr-note[data-selected="true"] { background: var(--yellow); border-color: var(--yellow); box-shadow: 0 0 6px var(--yellow); }
.pr-playhead { position: absolute; top: 0; bottom: 0; width: 2px; background: var(--orange); pointer-events: none; }
.pr-foot { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.pr-foot input[type="range"] { flex: 1; min-width: 80px; }
.pr-message { color: var(--yellow); font-size: 13px; }

/* ---- mixer overlay ---- */
.mixer {
    position: fixed;
    inset: 0;
    z-index: 20;
    background: var(--bg);
    display: flex;
    flex-direction: column;
    padding: 16px;
    padding-bottom: calc(16px + env(safe-area-inset-bottom));
    gap: 10px;
}
.mixer header { display: flex; justify-content: space-between; align-items: center; }
.mixer header .clip-name { flex: 1; text-align: center; }
.mixer header button { background: none; border: none; color: var(--fg); font: inherit; font-size: 18px; }
.mixer-body { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 8px; }
.mixer-label { font-size: 12px; text-transform: uppercase; color: var(--fg-dim); margin-top: 8px; }
.mix-strip { background: var(--bg1); border-radius: 8px; padding: 10px; display: flex; flex-direction: column; gap: 8px; }
.mix-row { display: flex; align-items: center; gap: 10px; }
.mix-row input[type="range"] { flex: 1; min-width: 60px; }
.mix-row .clip-meta { min-width: 64px; }
.mix-name { flex: 1; text-align: left; background: none; border: none; color: var(--fg); font: inherit; }
.mix-expand, .mix-expand-static { display: flex; flex-direction: column; gap: 8px; }
.mix-toggle { width: 32px; height: 32px; border-radius: 6px; border: 1px solid var(--muted); background: none; color: var(--fg-dim); font: inherit; }
.mix-toggle[data-on="true"] { border-color: var(--green); color: var(--green); }
.mix-static { color: var(--green); }
.mix-del { align-self: flex-start; background: none; border: 1px solid var(--red-bright); border-radius: 6px; color: var(--red-bright); font: inherit; font-size: 12px; padding: 6px 12px; }

.mixer-tabs { display: flex; gap: 8px; }
.mixer-tab {
    flex: 1;
    background: none;
    border: 1px solid var(--muted);
    border-radius: 16px;
    color: var(--fg-dim);
    font: inherit;
    font-size: 13px;
    padding: 6px 12px;
}
.mixer-tab[data-on="true"] { border-color: var(--orange); color: var(--orange); }
