/* drum-machine.css — the drum machine on the studio kit (learning tools, task A6).
   Loaded after style.css and studio.css through base.html's page_styles block (studio_assets() first).
   style.css still carries the pre-kit .dm-* rules and the <= 640 px switch from grid to pads; the rules
   here restyle those elements in the kit's look and override the old ones where they collide.

   Layout: one hardware panel. Deck (start/stop key, tempo display, scope), a control row (swing, master,
   pattern, export), the step LEDs and the 8 x 16 grid (label, length stepper, cells, fader per track),
   the cycle readout with a level legend, then the preset bank (17 pads, coloured by family).
   Cells take the 808 colour of their beat (data-group 0..3) and a level: v1 ghost, v2 normal, v3 accent. */

.dm-wrap.st-root {
    --st-accent: var(--st-808-orange);
    --dm-lw: 5rem;            /* track label */
    --dm-nw: 4.9rem;          /* length stepper */
    --dm-vw: 3.6rem;          /* track fader */
    --dm-track: 28px;         /* cell column minimum, fine pointers (contract: 28 px) */
}
@media (pointer: coarse) {
    .dm-wrap.st-root {
        --dm-nw: 7.4rem;
        --dm-track: calc(var(--st-target) + 6px);   /* 44 px cells even where a beat gap eats 6 px */
    }
}

.dm-machine {
    display: flex;
    flex-direction: column;
    gap: 1.1rem;
}

/* ── deck ────────────────────────────────────────────────────────────── */
.dm-deck {
    display: grid;
    grid-template-columns: auto auto minmax(0, 1fr);
    align-items: center;
    gap: 0.9rem 1.4rem;
}
.dm-play {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.65rem;
    min-width: 8.4rem;
    height: 4rem;
    padding: 0 1.25rem;
    border: 0;
    border-radius: 12px;
    background: var(--st-808-orange);
    color: #17191B;
    font: inherit;
    font-family: var(--st-font);
    font-size: 1.05rem;
    font-weight: 850;
    font-stretch: 118%;
    letter-spacing: 0.06em;
    cursor: pointer;
    box-shadow: inset 0 -5px 0 rgba(0, 0, 0, 0.28), inset 0 1px 0 rgba(255, 255, 255, 0.28);
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}
.dm-play::before {                       /* play triangle; a square while playing */
    content: "";
    width: 0;
    height: 0;
    border-left: 13px solid currentColor;
    border-top: 8px solid transparent;
    border-bottom: 8px solid transparent;
}
.dm-play:hover { filter: brightness(1.06); }
.dm-play.playing {
    background: var(--st-808-red);
    color: #fff;
    transform: translateY(1px);
    box-shadow: inset 0 3px 0 rgba(0, 0, 0, 0.3);
}
.dm-play.playing::before {
    width: 13px;
    height: 13px;
    border: 0;
    border-radius: 2px;
    background: currentColor;
}

.dm-tempo {
    display: flex;
    align-items: center;
    gap: 0.9rem;
}
.dm-bpm {
    --st-display-size: clamp(2.9rem, 5.4vw, 4.2rem);
    white-space: nowrap;
    color: var(--st-panel-ink);
}
#dm-bpm-val {
    display: inline-block;
    min-width: 2.15em;                    /* 60..200 without the deck jumping */
    text-align: end;
}
.dm-nudge { display: flex; gap: 4px; }
.dm-nudge .st-btn {
    width: var(--st-target);
    padding: 0;
    font-size: 1.35rem;
    line-height: 1;
}
.dm-scope { height: 4.4rem; min-width: 0; }

/* ── control row ─────────────────────────────────────────────────────── */
.dm-controls {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) minmax(11rem, 1.3fr);
    align-items: end;
    gap: 0.6rem 1.4rem;
}
.dm-ctl { min-width: 0; }
.dm-field {
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-width: 0;
    color: var(--st-muted);
    font-size: 0.85rem;
    font-weight: 650;
    font-stretch: 92%;
}
.dm-select {
    -webkit-appearance: none;
    appearance: none;
    width: 100%;
    min-height: var(--st-target);
    padding-block: 0;
    padding-inline: 0.85rem 2.3rem;
    border: 1px solid var(--st-line);
    border-radius: 9px;
    background-color: var(--st-sunk);
    background-image: linear-gradient(45deg, transparent 50%, var(--st-muted) 50%),
        linear-gradient(135deg, var(--st-muted) 50%, transparent 50%);
    background-position: calc(100% - 19px) 52%, calc(100% - 14px) 52%;
    background-size: 5px 5px;
    background-repeat: no-repeat;
    color: var(--st-ink);
    font: inherit;
    font-family: var(--st-font);
    font-size: 0.98rem;
    font-weight: 700;
    font-stretch: 96%;
    cursor: pointer;
    box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.35);
}
[dir="rtl"] .dm-select { background-position: 14px 52%, 19px 52%; }
.dm-select option { background: #24272B; color: #ECEAE3; }
.dm-export { white-space: nowrap; }

/* ── share: Copy link, its status chip, the fallback field (A7) ──────── */
/* the export and share buttons get their own row, right-aligned under the Pattern select; on a phone they wrap */
.dm-actions {
    position: relative;
    grid-column: 1 / -1;
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-end;
    gap: 0.5rem;
}
.dm-share { white-space: nowrap; }
.dm-share-icon {
    flex: none;
    fill: none;
    stroke: currentColor;
    stroke-width: 2.2;
    stroke-linecap: round;
    stroke-linejoin: round;
}
/* a status chip that lights above the button, like a hardware readout */
.dm-toast {
    position: absolute;
    inset-inline-end: 0;
    bottom: calc(100% + 8px);
    z-index: 2;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin: 0;
    padding: 0.4rem 0.8rem 0.4rem 0.65rem;
    border: 1px solid var(--st-panel-line);
    border-radius: 999px;
    background: var(--st-well);
    color: var(--st-panel-ink);
    font-size: 0.85rem;
    font-weight: 700;
    font-stretch: 92%;
    white-space: nowrap;
    box-shadow: 0 6px 18px rgba(0, 0, 0, 0.45);
    pointer-events: none;
    opacity: 0;
    transform: translateY(4px);
    transition: opacity 0.16s ease-out, transform 0.16s ease-out;
}
.dm-toast::before {
    content: "";
    flex: none;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--st-green);
    box-shadow: 0 0 0 2px rgba(18, 168, 102, 0.25), 0 0 8px var(--st-green);
}
.dm-toast:empty { display: none; }
.dm-toast.is-shown { opacity: 1; transform: none; }
.dm-share-url {
    grid-column: 1 / -1;
    width: 100%;
    min-height: var(--st-target);
    padding: 0 0.85rem;
    border: 1px solid var(--st-line);
    border-radius: 9px;
    background: var(--st-sunk);
    color: var(--st-ink);
    font: inherit;
    font-size: 0.9rem;
    font-variant-numeric: tabular-nums;
}
.dm-share-url[hidden] { display: none; }
@media (prefers-reduced-motion: reduce) {
    .dm-toast { transition: none; transform: none; }
}

/* ── step LEDs + grid ────────────────────────────────────────────────── */
.dm-grid { gap: 6px; }                    /* style.css: column flex, hidden at <= 640 px */
.dm-row,
.dm-ledrow {
    display: grid;
    grid-template-columns: var(--dm-lw) var(--dm-nw) repeat(16, minmax(var(--dm-track), 1fr)) var(--dm-vw);
    column-gap: 4px;
    align-items: center;
}
.dm-ledrow { margin-bottom: 8px; }
.dm-ledrow .st-led {
    min-width: 0;
    min-height: 0;
    height: 6px;
    border-radius: 3px;
}
.dm-led-gap,
.dm-cell.is-beat { margin-inline-start: 6px; }

.dm-label {
    display: flex;
    align-items: center;
    gap: 0.55rem;
    width: auto;
    min-height: var(--st-target);
    padding: 0 0.3rem 0 0.15rem;
    border: 0;
    border-radius: 6px;
    background: none;
    color: var(--st-ink);
    font: inherit;
    font-family: var(--st-font);
    font-size: 0.86rem;
    font-weight: 750;
    font-stretch: 96%;
    letter-spacing: 0.02em;
    text-align: start;
    cursor: pointer;
    transition: none;
}
.dm-label:hover { color: var(--st-ink); background: rgba(255, 255, 255, 0.05); }
.dm-label-led {
    flex: none;
    width: 10px;
    height: 10px;
    border-radius: 2px;
    background: #3A3E44;
    background: color-mix(in srgb, var(--dm-c) 42%, #1A1C1F);
    box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.5);
}
.dm-label.is-hit .dm-label-led {
    background: var(--dm-c);
    box-shadow: 0 0 9px 1px var(--dm-c);
}

/* length stepper: a sunk pill like the kit's segmented control */
.dm-len {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 2px;
    min-height: var(--st-target);
    padding: 3px;
    box-sizing: border-box;
    border-radius: 9px;
    background: var(--st-sunk);
    box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.35);
}
.dm-len button {
    flex: none;
    width: 24px;
    height: 36px;
    padding: 0;
    border: 0;
    border-radius: 6px;
    background: var(--st-raised);
    color: var(--st-ink);
    font: inherit;
    font-size: 1rem;
    font-weight: 700;
    line-height: 1;
    cursor: pointer;
    box-shadow: 0 1px 0 rgba(0, 0, 0, 0.3), inset 0 1px 0 rgba(255, 255, 255, 0.06);
}
.dm-len button:hover { background: #474C53; }
.dm-len output {
    flex: 1;
    min-width: 1.5em;
    text-align: center;
    font-size: 0.92rem;
    font-weight: 750;
    font-variant-numeric: tabular-nums;
}
@media (pointer: coarse) {
    .dm-len button { width: var(--st-target); height: var(--st-target); }
    .dm-len { padding: 0; }
}

/* cells: kit .st-cell, over style.css's older .dm-cell / .dm-mini-cell rules */
.dm-cell,
.dm-mini-cell {
    flex: none;
    aspect-ratio: auto;
    max-height: none;
    transition: filter 0.08s;
}
.dm-cell.active,
.dm-mini-cell.active {
    border-color: transparent;
    box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.28);
}
.dm-cell.st-cell--v1,
.dm-mini-cell.st-cell--v1 { background: var(--st-on); background: color-mix(in srgb, var(--st-on) 36%, var(--st-key)); }
.dm-cell.st-cell--v2,
.dm-mini-cell.st-cell--v2 { background: var(--st-on); background: color-mix(in srgb, var(--st-on) 74%, var(--st-key)); }
.dm-cell.st-cell--v3,
.dm-mini-cell.st-cell--v3 {
    background: var(--st-on);
    box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.22), 0 0 12px -3px var(--st-on);
}
.dm-cell:hover,
.dm-mini-cell:hover { filter: brightness(1.18); }
.dm-cell.is-head,
.dm-mini-cell.is-head {
    box-shadow: inset 0 0 0 2px var(--st-panel-ink), inset 0 -3px 0 rgba(0, 0, 0, 0.28);
}
.dm-cell.active.is-head,
.dm-mini-cell.active.is-head { filter: brightness(1.3); }

.dm-vol-wrap { width: auto; padding-inline-start: 6px; }

/* ── readout + legend ────────────────────────────────────────────────── */
.dm-foot {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem 1.5rem;
}
.dm-cycle {
    margin: 0;
    color: var(--st-ink);
    font-size: 1rem;
}
.dm-legend {
    display: flex;
    gap: 1.1rem;
    margin: 0;
    padding: 0;
    list-style: none;
    color: var(--st-muted);
    font-size: 0.82rem;
    font-weight: 650;
    font-stretch: 92%;
}
.dm-legend li { display: inline-flex; align-items: center; gap: 0.45rem; }
.dm-swatch.st-cell {
    display: inline-block;
    width: 14px;
    height: 14px;
    border-radius: 3px;
    box-shadow: none;
    cursor: default;
}
.dm-swatch::after { display: none; }

/* ── preset bank ─────────────────────────────────────────────────────── */
.dm-bank.st-pads {
    grid-template-columns: repeat(auto-fill, minmax(4.9rem, 1fr));
    gap: 0.5rem;
    padding-top: 1.05rem;
    border-top: 1px solid var(--st-line);
}
.dm-bank .st-pad {
    min-height: 4.4rem;
    padding: 0.55rem 0.6rem 0.6rem;
    gap: 0.3rem;
}
.dm-bank .st-pad-name {
    font-size: 0.9rem;
    font-stretch: 90%;
    line-height: 1.05;
}
.dm-bank .st-pad-meta { margin-top: 0.15rem; font-size: 0.72rem; }

/* ── mobile: channel pads + mini-sequencer (style.css shows them at <= 640 px) ── */
.dm-pad.st-pad {
    aspect-ratio: auto;
    min-height: 3.7rem;
    padding: 0.55rem 0.6rem 0.6rem;
    border: 0;
    font-family: var(--st-font);
    letter-spacing: 0.02em;
}
.dm-pad.st-pad .st-pad-name { font-size: 0.95rem; }
.dm-pad.selected {
    background: var(--st-key);
    color: var(--st-key-ink);
    border-color: transparent;
    box-shadow: inset 0 3px 0 rgba(0, 0, 0, 0.28), inset 0 0 0 2px var(--st-pc);
}
.dm-pad.is-on::before {
    background: var(--st-pc);
    box-shadow: 0 0 10px 2px var(--st-pc);
}
.dm-mobile-seq { gap: 0.8rem; margin-top: 0; }
.dm-mini-seq {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 6px;
}
.dm-ch-ctrls {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr);
    align-items: end;
    gap: 0.6rem 1rem;
}
.dm-ch-len { justify-content: flex-start; }
.dm-ch-len .dm-field-label { padding: 0 0.45rem 0 0.55rem; color: var(--st-muted); font-size: 0.85rem; font-weight: 650; }
.dm-ch-len output { min-width: 2em; }

@media (max-width: 640px) {
    .dm-machine { gap: 0.95rem; }
    .dm-grid-scroll { display: none; }
    .dm-deck { grid-template-columns: auto minmax(0, 1fr); gap: 0.8rem 0.75rem; }
    .dm-tempo { justify-content: flex-end; gap: 0.5rem; min-width: 0; }
    .dm-play { min-width: 0; height: 3.4rem; padding: 0 0.9rem; gap: 0.5rem; font-size: 0.95rem; }
    .dm-bpm { --st-display-size: 2.4rem; }
    .dm-nudge .st-btn { font-size: 1.15rem; }
    .dm-scope { grid-column: 1 / -1; height: 3.4rem; }
    .dm-controls { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 0.5rem 1rem; }
    .dm-export { align-self: end; }
    .dm-controls > .dm-field,
    .dm-actions { grid-column: 1 / -1; }
    .dm-actions .st-btn { flex: 1 1 0; }
    .dm-foot { flex-direction: column; align-items: flex-start; }
    .dm-bank.st-pads {
        grid-auto-flow: column;
        grid-template-columns: none;
        grid-template-rows: repeat(2, auto);
        grid-auto-columns: 6.3rem;
        overflow-x: auto;
        overscroll-behavior-x: contain;
        scroll-snap-type: x proximity;
        padding-bottom: 6px;
    }
    .dm-bank .st-pad { scroll-snap-align: start; }
}
