/* --- Machine Panel Common --- */
.machine {
    padding: 20px;
    border-radius: 4px;
    border: 1px solid #000;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.6), inset 0 1px 0 rgba(255, 255, 255, 0.3);
    position: relative;
}

.machine-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
    user-select: none;
}

.machine-header h2 {
    cursor: pointer;
    transition: opacity 0.2s;
}

.machine-header h2:active {
    opacity: 0.7;
}

.machine-header h2::after {
    content: ' ▼';
    font-size: 0.5em;
    opacity: 0.5;
    margin-left: 8px;
    transition: transform 0.2s ease;
    display: inline-block;
}

.machine.collapsed .machine-header h2::after {
    transform: rotate(-90deg);
}

.machine-header h2 small {
    font-size: 0.6em;
    opacity: 0.7;
    font-weight: normal;
}

/* --- TB-303 Specific --- */
.tb-303 {
    background: var(--panel-303);
}

.sequencer-303 {
    display: grid;
    grid-template-columns: repeat(16, 1fr);
    gap: 4px;
    padding: 10px;
    background: #222;
    border-radius: 4px;
    border: 2px solid #111;
    box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.5);
    max-height: 1000px;
    overflow: hidden;
    opacity: 1;
    transition: max-height 0.2s ease, opacity 0.2s ease, padding 0.2s ease, margin 0.2s ease;
    box-sizing: border-box;
    width: 100%;
}

.machine.collapsed .sequencer-303 {
    max-height: 0;
    opacity: 0;
    padding: 0 10px;
    margin: 0;
}

.step-303 {
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    align-items: center;
    border-radius: 3px;
    min-height: 70px;
    /* Made slightly taller to accommodate physical feel */
    position: relative;
    border: 1px solid #7a6332;
    margin-bottom: 4px;
    /* Space for the 3D shadow */
    transition: all 0.1s ease;
    background: #e6e2d6;
    box-shadow: 0 2px 2px rgba(0, 0, 0, 0.2), inset 0 1px 0 white;
}

/* Beat grouping for 303: matches 909 step grouping colors */
.sequencer-303 .step-303:nth-child(-n+4),
.sequencer-303 .step-303:nth-child(n+9):nth-child(-n+12) {
    background: linear-gradient(to bottom, #f0f0f0, #dcdcdc);
}

.sequencer-303 .step-303:nth-child(n+5):nth-child(-n+8),
.sequencer-303 .step-303:nth-child(n+13) {
    background: linear-gradient(to bottom, #ffccaa, #ffaa88);
}



/* Active State (Pressed down - applies to both groups) */
.sequencer-303 .step-303:active,
.sequencer-303 .step-303.pressed {
    box-shadow: var(--key-shadow-active);
    transform: translateY(3px);
    background: var(--key-white-active);
}

.sequencer-303 .step-303.current {
    /* For current, we keep the physical appearance but glow the border heavily */
    border-color: #ffeb3b;
    box-shadow: 0 0 10px #ff3333, inset 0 0 10px rgba(255, 255, 255, 0.6), var(--key-shadow);
    z-index: 10;
}

.led {
    width: 10px;
    height: 10px;
    background: var(--led-off);
    border-radius: 50%;
    margin-bottom: 6px;
    border: 1px solid #333;
    box-shadow: inset 0 2px 3px rgba(0, 0, 0, 0.8), 0 1px 0 rgba(255, 255, 255, 0.2);
}

.step-303.active .led {
    background: var(--led-on);
    box-shadow: 0 0 8px #ff0000, inset 0 1px 2px rgba(255, 255, 255, 0.5);
}

.step-303 select {
    appearance: none;
    -webkit-appearance: none;
    background: #000;
    color: #f00;
    border: 1px solid #444;
    border-top-color: #222;
    border-bottom-color: #555;
    font-family: 'Courier New', monospace;
    font-size: 10px;
    font-weight: bold;
    width: 100%;
    margin-bottom: 2px;
    text-align: center;
    padding: 2px 0;
    border-radius: 1px;
    box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.8);
    cursor: pointer;
}

.step-ctrls {
    display: flex;
    gap: 2px;
    width: 100%;
    margin-top: 2px;
}

/* --- Note Editor (Piano UI) --- */
.note-editor {
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding: 10px;
    background: #222;
    border-radius: 4px;
    border: 2px solid #111;
    box-shadow: inset 0 20px 50px rgba(0, 0, 0, 0.9);
    margin-bottom: 4px;
    box-sizing: border-box;
    width: 100%;
    position: relative;
    max-height: 1000px;
    overflow: hidden;
    opacity: 1;
    transition: max-height 0.2s ease, opacity 0.2s ease, padding 0.2s ease, margin 0.2s ease, border-width 0.2s ease;
}

.note-editor.live-mode {
    background: linear-gradient(180deg, #1b2a1f 0%, #141d17 100%);
    border-color: #2c5034;
    box-shadow: inset 0 20px 50px rgba(0, 0, 0, 0.9), 0 0 10px rgba(102, 255, 153, 0.2);
}

.machine.collapsed .note-editor {
    max-height: 0;
    opacity: 0;
    padding: 0 10px;
    margin: 0;
    border-width: 0;
    pointer-events: none;
}

.editor-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 4px;
}

.editor-header .nav-group {
    display: flex;
    align-items: center;
    gap: 6px;
}

.editor-header .step-indicator {
    color: #ff3333;
    font-family: 'DSEG7Classic', monospace;
    font-weight: normal;
    font-size: 14px;
    letter-spacing: 0.5px;
    min-width: 25px;
    text-align: center;
    text-shadow: 0 0 5px rgba(255, 0, 0, 0.5);
}

.note-editor.live-mode .editor-header .step-indicator {
    color: #7dff9e;
    text-shadow: 0 0 6px rgba(78, 255, 138, 0.65);
}

.note-editor.live-mode .prev-btn,
.note-editor.live-mode .next-btn {
    display: none;
}

.note-editor.live-mode .preview-toggle-btn {
    display: none;
}

.note-editor.live-mode .editor-header .nav-group:first-child {
    gap: 0;
}

/* Base button style for editor controls */
.editor-btn {
    background: linear-gradient(to bottom, #444, #222);
    border: 1px solid #111;
    border-radius: 2px;
    color: #aaa;
    font-size: 10px;
    font-weight: bold;
    padding: 0 6px;
    cursor: pointer;
    min-width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 2px rgba(0, 0, 0, 0.5), inset 0 1px 1px rgba(255, 255, 255, 0.1);
    box-sizing: border-box;
    touch-action: manipulation;
}

.editor-btn:active {
    transform: translateY(2px);
    box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.6);
}

.editor-btn.active {
    background: linear-gradient(to bottom, #ff9900, #cc7700);
    color: white;
    border-color: #aa5500;
    box-shadow: 0 0 5px rgba(255, 153, 0, 0.5);
}

.editor-btn.live-toggle-btn.active {
    background: linear-gradient(to bottom, #7cff9f, #2f8f4b);
    border-color: #1f5f31;
    color: #0b2512;
    box-shadow: 0 0 8px rgba(102, 255, 153, 0.45);
}

.editor-btn.accent-btn.active {
    background: linear-gradient(to bottom, #ff5555, #cc0000);
    border-color: #900;
}

.editor-btn.slide-btn.active {
    background: linear-gradient(to bottom, #81c784, #2e7d32);
    border-color: #1b5e20;
}

.editor-btn .icon {
    width: 18px;
    height: 18px;
}

.editor-header .close-editor-btn {
    color: #888;
    background: none;
    border: none;
    font-size: 22px;
    cursor: pointer;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: color 0.1s;
}

.editor-header .close-editor-btn:active {
    color: #fff;
    transform: scale(0.9);
}

/* Single line control row */
.editor-controls {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: rgba(0, 0, 0, 0.3);
    padding: 6px 12px;
    border-radius: 4px;
    margin-bottom: 8px;
    border: 1px solid #111;
}

.note-editor.live-mode .editor-controls {
    background: rgba(9, 27, 14, 0.45);
    border-color: #27462f;
}

.note-editor.live-mode .control-label {
    color: #92b199;
}

.control-group {
    display: flex;
    align-items: center;
    gap: 6px;
}

.control-label {
    font-size: 9px;
    color: #888;
    text-transform: uppercase;
    font-weight: bold;
    letter-spacing: 0.5px;
}

.octave-selector,
.modifier-selector {
    display: flex;
    background: #111;
    border-radius: 4px;
    padding: 3px;
    box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.5);
}

.octave-btn,
.mod-btn {
    flex: 1;
    background: transparent;
    border: none;
    color: #666;
    padding: 10px 0;
    font-size: 12px;
    cursor: pointer;
    border-radius: 2px;
    transition: all 0.2s;
    font-weight: bold;
}

.octave-btn:active,
.mod-btn:active {
    color: #999;
}

.octave-btn.active,
.mod-btn.active {
    background: #444;
    color: #fff;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.5);
}

/* Piano container and keys */
.piano-container {
    position: relative;
    height: 110px;
    overflow: hidden;
    background: #000;
    border-radius: 2px;
    border: 1px solid #111;
}

.piano-keys-wrapper {
    display: flex;
    height: 100%;
    position: relative;
    width: 100%;
    --total-white-keys: 21;
    /* 3 octaves */
}

.piano-key {
    flex: 1;
    background: var(--key-white);
    border: 1px solid #000;
    border-radius: 0 0 4px 4px;
    position: relative;
    cursor: pointer;
    z-index: 1;
    display: flex;
    align-items: flex-end;
    justify-content: center;
    padding-bottom: 6px;
    font-size: 10px;
    font-weight: 800;
    color: rgba(0, 0, 0, 0.4);
    box-shadow: var(--key-shadow);
    transition: all 0.05s;
}

.piano-key.white-index-0 { --white-index: 0; }

.piano-key.white-index-1 { --white-index: 1; }

.piano-key.white-index-2 { --white-index: 2; }

.piano-key.white-index-3 { --white-index: 3; }

.piano-key.white-index-4 { --white-index: 4; }

.piano-key.white-index-5 { --white-index: 5; }

.piano-key.white-index-6 { --white-index: 6; }

.piano-key.white-index-7 { --white-index: 7; }

.piano-key.white-index-8 { --white-index: 8; }

.piano-key.white-index-9 { --white-index: 9; }

.piano-key.white-index-10 { --white-index: 10; }

.piano-key.white-index-11 { --white-index: 11; }

.piano-key.white-index-12 { --white-index: 12; }

.piano-key.white-index-13 { --white-index: 13; }

.piano-key.white-index-14 { --white-index: 14; }

.piano-key.white-index-15 { --white-index: 15; }

.piano-key.white-index-16 { --white-index: 16; }

.piano-key.white-index-17 { --white-index: 17; }

.piano-key.white-index-18 { --white-index: 18; }

.piano-key.white-index-19 { --white-index: 19; }

.piano-key.white-index-20 { --white-index: 20; }

.piano-key.black {
    background: var(--key-black);
    height: 60%;
    width: calc((100% / var(--total-white-keys)) * 0.7);
    left: calc((var(--white-index) * (100% / var(--total-white-keys))) - ((100% / var(--total-white-keys)) * 0.35));
    position: absolute;
    z-index: 2;
    border-radius: 0 0 3px 3px;
    color: rgba(255, 255, 255, 0.4);
    box-shadow: var(--key-black-shadow);
    border: 1px solid #000;
}

.piano-key:active {
    background: var(--key-white-active);
    box-shadow: var(--key-shadow-active);
    transform: translateY(2px);
}

.piano-key.black:active {
    background: var(--key-black-active);
    box-shadow: var(--key-black-shadow-active);
    transform: translateY(1px);
}

.piano-key.active {
    background: #ffcc00 !important;
    /* Design guide accent color */
    color: #000;
    box-shadow: 0 0 15px rgba(255, 204, 0, 0.6);
}

/* Responsive Logic */
@media (max-width: 600px) and (orientation: portrait) {
    .piano-keys-wrapper {
        --total-white-keys: 7;
        /* Override for single octave */
    }

    /* Hide octaves 2 and 3 and show single octave keys wider */
    .piano-key:nth-child(n+13) {
        display: none !important;
    }
}

@media (min-width: 601px) {

    /* Hide octave selectors in landscape/desktop as 3 octaves are visible */
    .octave-selector-group {
        display: none !important;
    }
}

/* 303 Waveform Styles */
.waveform-control {
    display: flex;
    flex-direction: row;
    align-items: flex-end;
    gap: 6px;
    margin-left: auto;
}

.waveform-control .header-lock-btn {
    margin-left: 0;
}

.waveform-switch-group {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
}

.switch-label {
    font-size: 8px;
    font-weight: bold;
    color: #555;
}

.clear-303-btn {
    margin-left: 0;
}

/* Mini Button Machine Overrides */
.mini-btn.sld.active {
    background: linear-gradient(to bottom, #ddd, #aaa);
    color: #111;
    text-shadow: none;
    box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.3), 0 0 5px #fff;
    border-color: #888;
}

.mini-btn.acc.active {
    background: linear-gradient(to bottom, #e57373, #c62828);
    color: white;
}

.mini-btn.sld.active {
    background: linear-gradient(to bottom, #81c784, #2e7d32);
    color: white;
}

.mini-btn.oct.active {
    background: linear-gradient(to bottom, #64b5f6, #1976d2);
    color: white;
    box-shadow: 0 0 5px #2196f3;
}

/* --- TR-909 Specific --- */
.tr-909 {
    background: var(--panel-909);
    color: #333;
    border: 1px solid #bbb;
}

.tr-909 .machine-header {
    border-bottom: 2px solid #d85c2e;
}

.tr-909 h2 {
    color: #333;
    font-family: 'Helvetica Neue', Arial, sans-serif;
    letter-spacing: 0;
}

.tr-909 h2 small {
    color: #d85c2e;
    font-weight: bold;
}

#tracks909 {
    width: 100%;
    display: flex;
    flex-direction: column;
}

.drum-track-row {
    display: flex;
    align-items: center;
    margin-bottom: 10px;
    background: transparent;
    padding: 8px;
    border-radius: 0;
    border-bottom: 1px solid rgba(0, 0, 0, 0.1);
}

.machine.collapsed .drum-track-row {
    margin-bottom: 0;
}

.machine.collapsed .sequencer-909 {
    width: 0;
}

@media (min-width: 601px) {
    .machine.collapsed #tracks909 {
        flex-direction: row;
        flex-wrap: wrap;
        justify-content: space-between;
    }

    .machine.collapsed .track-header {
        margin-right: 0;
    }

    .machine.collapsed .manage-909-track-btn {
        width: 224px;
    }
}

.track-header {
    width: 216px;
    display: flex;
    align-items: center;
    margin-right: 10px;
    justify-content: space-between;
    border-right: 2px solid #d85c2e;
}

/* 909 Knob Override */
.tr-909 .rotary-knob {
    width: 30px;
    height: 30px;
    background: radial-gradient(circle at 30% 30%, #444, #222);
    border: 1px solid #111;
}

.tr-909 .knob-wrapper {
    width: 34px;
}

.tr-909 .knob-wrapper::after {
    width: 30px;
    height: 30px;
    top: 15px;
}

.tr-909 .rotary-knob::after {
    background: #ff6600;
    width: 2px;
    height: 10px;
    top: 3px;
}

.track-knobs {
    display: flex;
    gap: 6px;
    align-items: center;
}

.track-name {
    font-weight: bold;
    font-size: 10px;
    width: 50px;
    text-align: right;
    color: #d85c2e;
    font-family: 'Arial', sans-serif;
    display: block;
}

.drum-track-row.custom-track {
    background: rgba(255, 204, 0, 0.05);
    border-left: 3px solid #ffcc00;
}

.drum-track-row.custom-track .track-name {
    color: #ffcc00;
}

.sequencer-909 {
    display: grid;
    grid-template-columns: repeat(16, 1fr);
    gap: 4px;
    flex-grow: 1;
    background: #444;
    padding: 4px;
    border-radius: 2px;
    max-height: 1000px;
    overflow: hidden;
    opacity: 1;
}

.machine.collapsed .sequencer-909 {
    max-height: 0;
    opacity: 0;
    padding: 0 4px;
}

.step-909 {
    box-sizing: border-box;
    height: 40px;
    background: #e6e2d6;
    border: 1px solid #999;
    border-bottom-color: #777;
    border-radius: 2px;
    cursor: pointer;
    box-shadow: 0 2px 2px rgba(0, 0, 0, 0.2), inset 0 1px 0 white;
    position: relative;
}

.sequencer-909 .step-909:nth-child(-n+4),
.sequencer-909 .step-909:nth-child(n+9):nth-child(-n+12) {
    background: linear-gradient(to bottom, #f0f0f0, #dcdcdc);
}

.sequencer-909 .step-909:nth-child(n+5):nth-child(-n+8),
.sequencer-909 .step-909:nth-child(n+13) {
    background: linear-gradient(to bottom, #ffccaa, #ffaa88);
}

.step-909:active,
.step-909.pressed {
    box-shadow: var(--key-shadow-active);
    transform: translateY(3px);
    background: var(--key-white-active);
}

.step-909.active {
    background: linear-gradient(to bottom, #ff5555, #cc0000) !important;
    box-shadow: 0 0 8px #ff0000, inset 0 1px 0 rgba(255, 255, 255, 0.4);
    border-color: #900;
}

.step-909.active::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 6px;
    height: 6px;
    background: #fff;
    border-radius: 50%;
    box-shadow: 0 0 5px #fff;
}

.sequencer-909 .step-909.current {
    border: 2px solid #ffff00;
    box-shadow: 0 0 10px #ffff00, inset 0 0 5px #ffff00;
    z-index: 20;
}

/* Track Controls & Buttons */
.track-controls {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-width: 40px;
}

.track-controls .track-name {
    margin-bottom: 4px;
    text-align: center;
    width: 100%;
}

.track-controls .remove-track-btn {
    color: #ff4444;
    font-size: 16px;
    font-weight: bold;
    cursor: pointer;
    background: rgba(255, 0, 0, 0.1);
    border: 1px solid rgba(255, 0, 0, 0.2);
}

.manage-909-track-btn {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 8px 12px;
    background: linear-gradient(to bottom, #d85c2e 0%, #a33e1c 100%);
    /* Retro red plastic */
    border: 1px solid #7a2910;
    border-radius: 3px;
    color: #fff;
    font-size: 11px;
    font-weight: bold;
    letter-spacing: 1px;
    cursor: pointer;
    box-shadow: 0 4px 0 #7a2910, 0 5px 5px rgba(0, 0, 0, 0.4), inset 0 1px 1px rgba(255, 255, 255, 0.4);
    transition: all 0.1s;
    text-shadow: 0 1px 1px rgba(0, 0, 0, 0.5);
}

.manage-909-track-btn:active {
    background: linear-gradient(to bottom, #c44a20 0%, #873010 100%);
    box-shadow: 0 1px 0 #7a2910, 0 2px 2px rgba(0, 0, 0, 0.4), inset 0 2px 4px rgba(0, 0, 0, 0.6);
    transform: translateY(3px);
    color: #ffdcb0;
}

.header-clear-btn,
.header-lock-btn {
    background: linear-gradient(to bottom, #444, #222);
    border: 1px solid #111;
    border-radius: 2px;
    width: 32px;
    height: 24px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.1s;
    box-shadow: 0 1px 1px rgba(0, 0, 0, 0.5), inset 0 1px 0 rgba(255, 255, 255, 0.1);
    margin-bottom: 1px;
    color: #888;
    padding: 0;
}

.header-clear-btn {
    margin-left: auto;
}

.header-lock-btn {
    margin-left: 6px;
}

.header-clear-btn:active,
.header-lock-btn:active {
    transform: translateY(1px);
    box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.6);
    background: linear-gradient(to bottom, #555, #333);
}

.header-lock-btn.locked {
    background: linear-gradient(to bottom, #ff9900, #cc7700);
    border-color: #aa5500;
}

.header-lock-btn.locked svg {
    stroke: #fff;
}

.header-clear-btn svg,
.header-lock-btn svg {
    width: 14px;
    height: 14px;
    stroke-width: 2.5px;
}

/* --- RESPONSIVE MACHINE QUERIES --- */
@media (max-width: 768px) {
    .sequencer-303 {
        grid-template-columns: repeat(8, 1fr);
        padding-bottom: 0;
    }

    .step-303 {
        min-width: 0;
        width: 100%;
    }

    .sequencer-909 {
        grid-template-columns: repeat(16, 1fr);
    }

    .drum-track-row {
        flex-direction: row;
        flex-wrap: wrap;
    }

    .track-header {
        width: auto;
        min-width: 180px;
        border-right: 2px solid #d85c2e;
        margin-right: 10px;
    }
}

@media (max-width: 600px) {
    .sequencer-303 {
        grid-template-columns: repeat(4, 1fr);
        padding-bottom: 6px;
    }

    .step-303 {
        min-width: 0;
        width: 100%;
    }

    .track-header {
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        align-items: center;
        gap: 10px;
    }

    .track-name {
        width: auto;
        flex-shrink: 0;
        font-size: 12px;
        font-weight: bold;
    }

    .track-knobs {
        flex-wrap: wrap;
        justify-content: flex-start;
    }

    .sequencer-909 {
        display: grid;
        grid-template-columns: repeat(8, 1fr);
        overflow-x: visible;
        gap: 2px;
    }

    .step-909 {
        min-width: 0;
        width: 100%;
        height: 35px;
    }
}

@media (max-width: 600px) and (orientation: portrait) {
    .drum-track-row {
        flex-direction: column;
        align-items: stretch;
        border-bottom: none;
        margin-bottom: 0;
        padding: 4px;
    }

    .track-header {
        width: 100%;
        border-right: none;
        border-bottom: 1px solid rgba(0, 0, 0, 0.1);
        margin-bottom: 3px;
        padding-bottom: 3px;
        margin-right: 0;
    }

    .track-controls {
        min-width: 0;
    }

    .step-909 {
        height: 40px;
    }
}

@media (min-width: 601px) and (max-width: 920px) {
    .drum-track-row {
        display: flex;
        flex-direction: column;
        align-items: stretch;
        margin-bottom: 10px;
        border: 1px solid rgba(255, 255, 255, 0.1);
        border-radius: 4px;
        background: rgba(0, 0, 0, 0.2);
    }

    .machine.collapsed .drum-track-row {
        margin: 3px;
    }

    .track-header {
        margin-right: 0;
        margin-bottom: 10px;
        border-right: none;
    }

    .machine.collapsed .track-header {
        border: none;
        margin-bottom: 0;
    }

    .sequencer-909 {
        min-width: 0;
        width: 100%;
    }
}

/* Original tablet styles */
@media (max-width: 768px) and (min-width: 601px) {
    .drum-track-row {
        flex-direction: row;
        flex-wrap: wrap;
    }

    .track-header {
        width: auto;
        min-width: 180px;
        border-right: 2px solid #d85c2e;
        border-bottom: none;
        margin-bottom: 0;
        padding-bottom: 0;
        margin-right: 10px;
    }

    .sequencer-909 {
        display: grid;
        grid-template-columns: repeat(16, 1fr);
        gap: 3px;
    }

    .step-909 {
        min-width: 0;
        width: 100%;
        height: 35px;
    }
}
