:root {
    --bg-main: #111;
    --rack-bg: #222;
    --panel-303: linear-gradient(180deg, #c8c8c8 0%, #a8a8a8 100%);
    --panel-909: #e6e2d6;
    --knob-body: linear-gradient(135deg, #ddd 0%, #999 100%);
    --knob-top: #ccc;
    --knob-indicator: #333;
    --switch-bg: #222;
    --switch-handle: #ddd;
    --led-on: #ff3333;
    --led-off: #4a0000;
    --lcd-bg: #2b0000;
    --lcd-text: #ff4444;
    --btn-grey: linear-gradient(to bottom, #eee, #ccc);
    --btn-grey-active: linear-gradient(to bottom, #ccc, #eee);
    --shadow-inset: inset 1px 1px 2px rgba(0, 0, 0, 0.6), inset -1px -1px 2px rgba(255, 255, 255, 0.1);
    --shadow-out: 1px 1px 3px rgba(0, 0, 0, 0.5);
}

* {
    box-sizing: border-box;
}

@font-face {
    font-family: 'DSEG7Classic';
    src: url('assets/DSEG7Classic-Bold.woff2') format('woff2');
    font-weight: bold;
    font-style: normal;
}

html {
    touch-action: manipulation;
    -webkit-text-size-adjust: 100%;
}

body {
    background-color: var(--bg-main);
    color: #222;
    font-family: 'Arial Narrow', 'Helvetica Neue', Arial, sans-serif;
    display: flex;
    flex-direction: column;
    align-items: center;
    margin: 0;
    padding: 20px;
    user-select: none;
    touch-action: manipulation;
    /* Prevent double-tap zoom */
    -webkit-user-select: none;
    -webkit-touch-callout: none;
}

h1,
h2,
h3 {
    margin: 0;
    text-transform: uppercase;
    letter-spacing: -0.5px;
    font-weight: 800;
    color: #222;
    text-shadow: 0 1px 0 rgba(255, 255, 255, 0.4);
}

.rack {
    display: flex;
    flex-direction: column;
    gap: 15px;
    width: 100%;
    max-width: 920px;
    padding: 20px;
    background: var(--rack-bg);
    border-radius: 8px;
    box-shadow: inset 0 0 30px rgba(0, 0, 0, 0.8), 0 0 0 4px #444;
    border: 1px solid #000;
    padding-bottom: 50px;
}

/* Version Display */
.version-display {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 10px;
    color: #888;
    font-family: 'Courier New', monospace;
    font-weight: bold;
    background: rgba(0, 0, 0, 0.3);
    padding: 4px 8px;
    border-radius: 3px;
    user-select: none;
    min-width: 44px;
    height: 44px;
}

/* --- Global Controls --- */
.top-bar {
    position: sticky;
    top: 0;
    z-index: 100;
    background: #333;
    background: linear-gradient(180deg, #444 0%, #222 100%);
    color: #ddd;
    padding: 10px;
    border-radius: 4px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border: 1px solid #000;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.5), inset 0 1px 0 rgba(255, 255, 255, 0.1);
    flex-wrap: wrap;
    gap: 15px;
}

.transport-controls {
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
}

/* Transport Icon Buttons */
.transport-icon-btn {
    width: 44px;
    height: 44px;
    background: linear-gradient(to bottom, #ddd, #bbb);
    border: 1px solid #000;
    border-radius: 4px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.4), inset 0 1px 0 rgba(255, 255, 255, 0.6);
    transition: all 0.15s ease;
    color: #333;
}

.transport-icon-btn:hover {
    background: linear-gradient(to bottom, #eee, #ccc);
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.6), inset 0 1px 0 rgba(255, 255, 255, 0.7);
    transform: translateY(-1px);
}

.transport-icon-btn:active {
    transform: translateY(0);
    box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.5);
}

.transport-icon-btn.play {
    background: linear-gradient(to bottom, #cfc, #aea);
    color: #060;
}

.transport-icon-btn.play:hover {
    background: linear-gradient(to bottom, #dfd, #bfb);
}

.transport-icon-btn.stop {
    background: linear-gradient(to bottom, #fcc, #caa);
    color: #600;
}

.transport-icon-btn.stop:hover {
    background: linear-gradient(to bottom, #fdd, #dbb);
}

.transport-icon-btn.share {
    background: linear-gradient(to bottom, #ccf, #aac);
    color: #006;
}

.transport-icon-btn.share:hover {
    background: linear-gradient(to bottom, #ddf, #bbd);
}

.transport-icon-btn svg {
    pointer-events: none;
}

/* Tempo Display - 7 Segment */
.led-display {
    background: #000;
    padding: 4px 8px;
    border: 2px solid #555;
    border-radius: 2px;
    box-shadow: inset 0 0 10px rgba(0, 0, 0, 1);
    display: flex;
    gap: 4px;
    align-items: center;
    justify-content: center;
    height: 38px;
    /* Slightly taller to match visual weight of large knob */
    box-sizing: border-box;
}

.digit {
    position: relative;
    width: 18px;
    height: 30px;
    margin: 0 1px;
}

.segment {
    position: absolute;
    background-color: #300;
    /* Off state: dark red */
    border-radius: 1px;
    transition: background-color 0.05s;
    box-shadow: 0 0 2px rgba(0, 0, 0, 0.5);
}

.segment.on {
    background-color: #ff0000;
    box-shadow: 0 0 8px #ff0000, 0 0 3px #ffaaaa;
    z-index: 2;
}

/* Segment Positions */
/* 
     A
   F   B
     G
   E   C
     D
*/
.segment.a {
    top: 0;
    left: 3px;
    right: 3px;
    height: 3px;
}

.segment.b {
    top: 3px;
    right: 0;
    height: 11px;
    width: 3px;
}

.segment.c {
    bottom: 3px;
    right: 0;
    height: 11px;
    width: 3px;
}

.segment.d {
    bottom: 0;
    left: 3px;
    right: 3px;
    height: 3px;
}

.segment.e {
    bottom: 3px;
    left: 0;
    height: 11px;
    width: 3px;
}

.segment.f {
    top: 3px;
    left: 0;
    height: 11px;
    width: 3px;
}

.segment.g {
    top: 13.5px;
    left: 3px;
    right: 3px;
    height: 3px;
}

/* --- Mobile Responsive Layout --- */
.rack {
    width: 100%;
    max-width: 900px;
    background: var(--rack-bg);
    margin: 20px auto;
    padding: 20px;
    border-radius: 10px;
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.8), inset 0 0 0 2px rgba(255, 255, 255, 0.1);
    position: relative;
    border: 1px solid #000;
}

@media (max-width: 768px) {
    .rack {
        margin: 0;
        padding: 0;
        border-radius: 0;
        border: none;
    }

    body {
        padding: 0;
    }

    .top-bar {
        flex-direction: column;
        align-items: stretch;
        gap: 10px;
    }

    .transport-controls {
        justify-content: center;
    }

    .tempo-group {
        justify-content: center;
        width: 100%;
        padding: 0 !important;
    }
}

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

.machine-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 2px solid rgba(0, 0, 0, 0.3);
    border-top: 1px solid rgba(255, 255, 255, 0.3);
    padding-bottom: 10px;
    margin-bottom: 20px;
}

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

/* --- ROTARY KNOB STYLES --- */
.knob-group {
    display: flex;
    gap: 20px;
    justify-content: space-between;
    background: rgba(0, 0, 0, 0.1);
    padding: 15px;
    border-radius: 6px;
    margin-bottom: 20px;
    box-shadow: inset 0 2px 5px rgba(0, 0, 0, 0.2);
    border-bottom: 1px solid rgba(255, 255, 255, 0.2);
}

.knob-group-section {
    display: flex;
    gap: 6px;
    align-items: center;
    padding: 0 6px;
    position: relative;
    flex-wrap: wrap;
    margin-top: 10px;
    padding-right: 0;
}

.knob-group-section::before {
    content: attr(data-label);
    position: absolute;
    top: -12px;
    left: 50%;
    transform: translateX(-50%);
    font-size: 9px;
    color: #555;
    font-weight: bold;
    letter-spacing: 1px;
}

.knob-wrapper {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 50px;
    position: relative;
}

.knob-label {
    font-size: 9px;
    font-weight: bold;
    margin-bottom: 6px;
    text-align: center;
    pointer-events: none;
    text-transform: uppercase;
    color: #333;
    text-shadow: 0 1px 0 rgba(255, 255, 255, 0.3);
}

.rotary-knob {
    width: 40px;
    height: 40px;
    background: var(--knob-body);
    border-radius: 50%;
    position: relative;
    cursor: ns-resize;
    box-shadow: 0 3px 5px rgba(0, 0, 0, 0.4), inset 0 1px 0 rgba(255, 255, 255, 0.8);
    border: 1px solid #666;
    touch-action: none;
}

.rotary-knob.large {
    width: 60px;
    height: 60px;
}

/* Static gloss overlay */
.knob-wrapper::after {
    content: '';
    position: absolute;
    top: 20px;
    left: 50%;
    transform: translateX(-50%);
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: radial-gradient(circle at 30% 30%, rgba(255, 255, 255, 0.4) 0%, rgba(255, 255, 255, 0) 50%);
    pointer-events: none;
    z-index: 10;
}

.knob-wrapper.large::after {
    width: 60px;
    height: 60px;
    top: 0;
}

/* Knob Indicator Line */
.rotary-knob::after {
    content: '';
    position: absolute;
    top: 4px;
    left: 50%;
    width: 3px;
    height: 14px;
    background: var(--knob-indicator);
    transform-origin: bottom center;
    transform: translateX(-50%);
    border-radius: 1px;
}

.rotary-knob.large::after {
    width: 4px;
    height: 20px;
    top: 5px;
}

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

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

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

.waveform-switch {
    position: relative;
    width: 60px;
    height: 24px;
    background: #111;
    border-radius: 2px;
    display: flex;
    box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.8);
    border: 1px solid #555;
}

/* 303 Clear Button Positioning */
.clear-303-btn {
    margin-left: 0;
}

/* Waveform Switch Logic */
.waveform-switch input[type="radio"] {
    display: none;
}

.waveform-switch label {
    width: 50%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 2;
    cursor: pointer;
}

.switch-handle {
    position: absolute;
    top: 2px;
    left: 2px;
    width: calc(50% - 2px);
    height: calc(100% - 4px);
    background: linear-gradient(to bottom, #eee, #ccc);
    border-radius: 2px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
    transition: transform 0.2s ease;
    z-index: 1;
}

/* Use attribute selector for ID suffix matching to support multiple units */
.waveform-switch input[id*="wave-sq"]:checked~.switch-handle {
    transform: translateX(100%);
}

.icon-saw,
.icon-sq {
    width: 16px;
    height: 16px;
    background-repeat: no-repeat;
    background-position: center;
    opacity: 0.5;
    transition: opacity 0.2s;
}

.icon-saw {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23333' stroke-width='2'%3E%3Cpath d='M22 22L12 2L2 22'/%3E%3C/svg%3E");
}

.icon-sq {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23333' stroke-width='2'%3E%3Crect x='2' y='2' width='20' height='20'/%3E%3C/svg%3E");
}

.waveform-switch input:checked+label .icon-saw,
.waveform-switch input:checked+label .icon-sq {
    opacity: 1;
    filter: drop-shadow(0 0 2px rgba(255, 255, 255, 0.8));
}

/* Mode Switch Styles */
.mode-switch-group {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    margin: -3px 0 6px;
}

.mode-switch {
    position: relative;
    width: 80px;
    height: 28px;
    background: #111;
    border-radius: 2px;
    display: flex;
    box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.8);
    border: 1px solid #555;
}

.mode-switch input[type="radio"] {
    display: none;
}

.mode-switch label {
    width: 50%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 2;
    cursor: pointer;
}

.mode-switch input[value="song"]:checked~.switch-handle {
    transform: translateX(100%);
}

.icon-pattern,
.icon-song {
    width: 18px;
    height: 18px;
    background-repeat: no-repeat;
    background-position: center;
    opacity: 0.5;
    transition: opacity 0.2s;
}

.icon-pattern {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23333' stroke-width='2'%3E%3Crect x='3' y='3' width='18' height='18' rx='2'/%3E%3Cpath d='M3 9h18M3 15h18M9 3v18M15 3v18'/%3E%3C/svg%3E");
}

.icon-song {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23333' stroke-width='2'%3E%3Cpath d='M9 18V5l12-2v13'/%3E%3Ccircle cx='6' cy='18' r='3'/%3E%3Ccircle cx='18' cy='16' r='3'/%3E%3C/svg%3E");
}

.mode-switch input:checked+label .icon-pattern,
.mode-switch input:checked+label .icon-song {
    opacity: 1;
    filter: drop-shadow(0 0 2px rgba(255, 255, 255, 0.8));
}

/* Tempo Group */
.tempo-group {
    display: flex;
    align-items: center;
    gap: 15px;
    /* Increased gap for better separation */
    background: rgba(0, 0, 0, 0.2);
    padding: 10px 20px;
    /* Increased padding */
    border-radius: 4px;
    border: 1px solid rgba(255, 255, 255, 0.1);
    /* Removed fixed height to allow content to fit */
    min-height: 80px;
    box-sizing: border-box;
}

.tempo-display-wrapper {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
}

.tempo-label {
    font-size: 10px;
    font-weight: bold;
    color: #888;
    letter-spacing: 1px;
    text-shadow: 0 1px 0 rgba(0, 0, 0, 1);
}

.knob-input {
    display: none;
}

/* --- 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);
}

.sequencer-303 {
    display: grid;
    grid-template-columns: repeat(16, 1fr);
    gap: 4px;
    background: #222;
    padding: 6px;
    border-radius: 4px;
    box-shadow: inset 0 2px 5px rgba(0, 0, 0, 0.5);
    border: 1px solid #444;
}

@media (max-width: 768px) {
    .sequencer-303 {
        display: grid;
        grid-template-columns: repeat(8, 1fr);
        gap: 4px;
        overflow-x: visible;
        padding-bottom: 0;
    }

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

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

    .step-303 {
        min-width: 0;
        /* Reset min-width for grid */
        width: 100%;
    }
}


.step-303 {
    box-sizing: border-box;
    background: linear-gradient(to bottom, #ccc, #aaa);
    border: 1px solid #000;
    padding: 4px 2px;
    display: flex;
    flex-direction: column;
    align-items: center;
    border-radius: 2px;
    min-height: 55px;
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.4), 0 2px 2px rgba(0, 0, 0, 0.5);
    position: relative;
}

.step-303.current {
    background: linear-gradient(to bottom, #ffeb3b, #fbc02d);
    box-shadow: 0 0 5px #ff3333;
}

/* Knob Tooltip */
.knob-tooltip {
    position: absolute;
    top: -25px;
    left: 50%;
    transform: translateX(-50%);
    background: #000;
    color: #f00;
    font-family: 'DSEG7Classic', monospace;
    font-size: 12px;
    padding: 2px 4px;
    border: 1px solid #555;
    border-radius: 2px;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.1s;
    z-index: 100;
    white-space: nowrap;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.5);
}

.knob-tooltip.visible {
    opacity: 1;
}

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

.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;
    /* LED Red */
    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-303 select:focus {
    outline: none;
    border-color: #666;
}

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

.mini-btn {
    flex: 1;
    font-size: 8px;
    /* Reduced font size */
    text-align: center;
    cursor: pointer;
    background: linear-gradient(to bottom, #444, #222);
    color: #888;
    border: 1px solid #111;
    padding: 3px 0;
    /* Reduced padding */
    border-radius: 2px;
    box-shadow: 0 1px 1px rgba(0, 0, 0, 0.5), inset 0 1px 0 rgba(255, 255, 255, 0.1);
    font-family: 'Arial', sans-serif;
    font-weight: bold;
    text-shadow: 0 -1px 0 rgba(0, 0, 0, 1);
    transition: all 0.1s;
}

.mini-btn:active {
    transform: translateY(1px);
    box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.6);
    background: #222;
}

.mini-btn.active {
    color: #fff;
    text-shadow: 0 0 2px #f00;
    background: linear-gradient(to bottom, #d00, #900);
    box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.3), 0 0 5px #f00;
    border-color: #500;
}

.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;
    /* 909 Orange */
}

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

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

.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);
}

.track-header {
    width: 220px;
    /* Wider for extra knob */
    display: flex;
    align-items: center;
    margin-right: 10px;
    justify-content: space-between;
    border-right: 2px solid #d85c2e;
}

/* 909 Knob Style 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;
    /* Adjust wrapper width */
}

.tr-909 .knob-wrapper::after {
    width: 30px;
    height: 30px;
    top: 15px;
    /* Adjust gloss position */
}

.tr-909 .rotary-knob::after {
    background: #ff6600;
    /* Orange indicator */
    width: 2px;
    height: 10px;
    /* Shorter indicator */
    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;
}

@media (max-width: 600px) {
    .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(16, 1fr);
    gap: 4px;
    flex-grow: 1;
    background: #444;
    padding: 4px;
    border-radius: 2px;
}

@media (max-width: 768px) {
    .drum-track-row {
        flex-direction: column;
        align-items: stretch;
    }

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

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

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

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

    .step-909 {
        min-width: 0;
        width: 100%;
        height: 35px;
        /* Slightly taller for touch */
    }
}

.step-909 {
    box-sizing: border-box;
    height: 40px;
    background: #e6e2d6;
    /* Match panel */
    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;
}

/* Classic 909 coloring: First 4 grey/white, next 4 orange, etc. */
.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);
    /* Orange-ish */
}

.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;
}

.step-909.current {
    border: 2px solid #ffff00;
    box-shadow: 0 0 8px #ffff00;
    z-index: 10;
}

/* --- Advanced Note Editor Popover --- */
.piano-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    z-index: 2000;
    display: flex;
    align-items: center;
    justify-content: center;
    backdrop-filter: blur(5px);
}

.note-editor {
    width: 90%;
    max-width: 400px;
    background: #222;
    border: 1px solid #555;
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.9);
    border-radius: 8px;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    animation: popIn 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

@keyframes popIn {
    from {
        transform: scale(0.9);
        opacity: 0;
    }

    to {
        transform: scale(1);
        opacity: 1;
    }
}

.editor-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 15px;
    background: #333;
    border-bottom: 1px solid #444;
}

.step-nav {
    display: flex;
    align-items: center;
    gap: 10px;
}

.step-nav button {
    background: linear-gradient(to bottom, #444, #333);
    border: 1px solid #222;
    color: #ddd;
    width: 36px;
    height: 36px;
    border-radius: 4px;
    cursor: pointer;
    box-shadow: 0 2px 2px rgba(0, 0, 0, 0.3);
    font-weight: bold;
}

.step-nav button:active {
    transform: translateY(1px);
    box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.5);
}

.step-indicator {
    font-family: 'DSEG7 Classic', monospace;
    color: #ff3333;
    font-size: 20px;
    background: #000;
    padding: 6px 10px;
    border-radius: 2px;
    border: 1px solid #555;
    box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.8);
    min-width: 40px;
    text-align: center;
}

.close-btn {
    background: transparent;
    border: none;
    color: #888;
    font-size: 24px;
    cursor: pointer;
    line-height: 1;
    padding: 0 10px;
}

.close-btn:hover {
    color: #fff;
}

.editor-controls {
    padding: 15px;
    display: grid;
    grid-template-columns: 1fr;
    gap: 15px;
    background: #2a2a2a;
}

.control-row {
    display: flex;
    gap: 15px;
    align-items: center;
}

.control-group {
    display: flex;
    flex-direction: column;
    gap: 6px;
    flex: 1;
}

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

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

.octave-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:hover {
    color: #999;
}

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

.toggle-row {
    display: flex;
    gap: 10px;
}

.toggle-btn {
    flex: 1;
    padding: 12px;
    background: linear-gradient(to bottom, #3a3a3a, #2a2a2a);
    border: 1px solid #111;
    color: #888;
    border-radius: 4px;
    cursor: pointer;
    font-weight: bold;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    transition: all 0.1s;
    box-shadow: 0 2px 2px rgba(0, 0, 0, 0.3);
    font-size: 12px;
}

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

.toggle-btn.active {
    color: white;
    text-shadow: 0 0 5px rgba(255, 255, 255, 0.5);
    border-color: rgba(255, 255, 255, 0.2);
}

.toggle-btn.accent.active {
    background: linear-gradient(to bottom, #e53935, #b71c1c);
    box-shadow: 0 0 10px #e53935;
}

.toggle-btn.slide.active {
    background: linear-gradient(to bottom, #43a047, #2e7d32);
    box-shadow: 0 0 10px #43a047;
}

.preview-toggle {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 5px;
    font-size: 12px;
    color: #aaa;
    cursor: pointer;
    user-select: none;
}

.preview-checkbox {
    width: 18px;
    height: 18px;
    accent-color: #d85c2e;
    cursor: pointer;
}

.piano-container {
    position: relative;
    height: 160px;
    background: #1a1a1a;
    border-top: 1px solid #444;
    margin-top: auto;
    padding: 0 10px;
    /* Spacing for keys */
}

.piano-key-new {
    position: absolute;
    top: 0;
    height: 100%;
    border: 1px solid #888;
    border-top: none;
    border-radius: 0 0 4px 4px;
    cursor: pointer;
    box-shadow: inset 0 -5px 10px rgba(0, 0, 0, 0.1), 2px 2px 5px rgba(0, 0, 0, 0.3);
    transition: background 0.05s;
    display: flex;
    align-items: flex-end;
    justify-content: center;
    padding-bottom: 10px;
    font-size: 10px;
    font-weight: bold;
    color: #444;
}

.piano-key-new.active.disabled {
    background: #555 !important;
    color: #888 !important;
    box-shadow: inset 0 0 10px #333 !important;
    border-color: #444 !important;
}

.piano-key-new:active {
    transform: translateY(2px);
    box-shadow: inset 0 -2px 5px rgba(0, 0, 0, 0.2);
}

.piano-key-new.white {
    background: linear-gradient(to bottom, #fff 0%, #eee 100%);
    z-index: 1;
}

.piano-key-new.white.active {
    background: #d85c2e;
    border-color: #a33;
    color: white;
    box-shadow: 0 0 15px #d85c2e;
}

.piano-key-new.black {
    background: linear-gradient(to bottom, #333 0%, #000 100%);
    z-index: 2;
    height: 60%;
    border: 1px solid #000;
    border-bottom: 1px solid #333;
    color: #ccc;
}

.piano-key-new.black.active {
    background: #d85c2e;
    border-color: #fff;
    color: white;
    box-shadow: 0 0 15px #d85c2e;
}

.mute-btn {
    width: 100%;
    padding: 15px;
    background: #222;
    color: #888;
    border: none;
    border-top: 1px solid #444;
    cursor: pointer;
    font-weight: bold;
    text-transform: uppercase;
    letter-spacing: 1px;
    transition: background 0.2s;
    font-size: 12px;
}

.mute-btn:hover {
    background: #333;
    color: #fff;
}

.mute-btn.active {
    background: #333;
    color: #d85c2e;
    box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.5);
}

@media (max-width: 600px) {
    .piano-overlay {
        align-items: flex-end;
        /* Align to bottom on mobile */
        background: rgba(0, 0, 0, 0.5);
        /* Lighter overlay to see behind */
    }

    .note-editor {
        width: 100%;
        max-width: none;
        border-radius: 4px 4px 0 0;
        /* Position it to cover the sequencer area roughly, or just stick to bottom */
        /* User asked to match .sequencer-303 size so top-bar and knob-group are visible. 
           The sequencer is at the bottom of the machine div. 
           However, the overlay is fixed full screen. 
           To achieve the effect of "not covering top-bar", we can just make it a bottom sheet 
           that doesn't go too high. 
        */
        height: auto;
        max-height: 60vh;
        /* Ensure it doesn't cover the whole screen */
        border-bottom: none;
        box-shadow: 0 -5px 20px rgba(0, 0, 0, 0.5);
    }

    .piano-container {
        flex-grow: 0;
        height: 140px;
        /* Fixed height for keys on mobile */
    }

    .editor-controls {
        gap: 10px;
        padding: 10px;
    }

    .toggle-btn {
        padding: 10px;
        font-size: 12px;
    }

    .octave-btn {
        padding: 10px 0;
        font-size: 12px;
    }
}

/* Toast Notification */
#toast {
    visibility: hidden;
    min-width: 250px;
    background-color: #222;
    color: #fff;
    text-align: center;
    border-radius: 4px;
    padding: 16px;
    position: fixed;
    z-index: 100;
    left: 50%;
    bottom: 30px;
    transform: translateX(-50%);
    font-size: 14px;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.8);
    border: 1px solid #444;
}

#toast.show {
    visibility: visible;
    animation: fadein 0.5s, fadeout 0.5s 2.5s;
}

@keyframes fadein {
    from {
        bottom: 0;
        opacity: 0;
    }

    to {
        bottom: 30px;
        opacity: 1;
    }
}

@keyframes fadeout {
    from {
        bottom: 30px;
        opacity: 1;
    }

    to {
        bottom: 0;
        opacity: 0;
    }
}

/* --- Mode Controls --- */
.mode-controls {
    background: #2a2a2a;
    border: 1px solid #000;
    border-radius: 4px;
    padding: 10px;
    box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.5);
    color: #ddd;
}

.mode-switch-row {
    display: flex;
    gap: 10px;
    margin-bottom: 10px;
    justify-content: center;
}

.mode-switch-row button {
    background: #444;
    color: #888;
    border: 1px solid #111;
    padding: 8px 20px;
    font-weight: bold;
    cursor: pointer;
    border-radius: 3px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
    transition: all 0.2s;
}

.mode-switch-row button.active {
    background: #ddd;
    color: #111;
    box-shadow: 0 0 5px #fff;
}

.mode-context-controls {
    background: #222;
    padding: 10px;
    border-radius: 4px;
    border: 1px solid #000;
}

/* Pattern Mode UI */
.pattern-select-row {
    display: flex;
    gap: 3px;
    justify-content: center;
    margin-bottom: 10px;
    flex-wrap: wrap;
}

.pat-btn {
    width: 40px;
    height: 40px;
    background: #444;
    color: #aaa;
    border: 1px solid #000;
    border-radius: 4px;
    font-weight: bold;
    cursor: pointer;
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.1);
}

.pat-btn.active {
    background: #ff3333;
    color: #fff;
    box-shadow: 0 0 10px #ff3333;
    border-color: #ff0000;
}

.pattern-actions,
.song-actions {
    display: flex;
    gap: 3px;
    justify-content: center;
    height: 50px;
    align-items: center;
}

/* Icon Action Buttons */
.icon-action-btn {
    width: 40px;
    height: 40px;
    background: linear-gradient(to bottom, #555, #333);
    border: 1px solid #000;
    border-radius: 4px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.4), inset 0 1px 0 rgba(255, 255, 255, 0.2);
    transition: all 0.15s ease;
}

.icon-action-btn:hover {
    background: linear-gradient(to bottom, #666, #444);
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.6), inset 0 1px 0 rgba(255, 255, 255, 0.3);
    transform: translateY(-1px);
}

.icon-action-btn:active {
    transform: translateY(0);
    box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.5);
}

.icon-copy,
.icon-paste {
    width: 20px;
    height: 20px;
    background-repeat: no-repeat;
    background-position: center;
    background-size: contain;
    opacity: 0.8;
}

.icon-copy {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23fff' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Crect x='9' y='9' width='13' height='13' rx='2' ry='2'/%3E%3Cpath d='M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1'/%3E%3C/svg%3E");
}

.icon-paste {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23fff' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M16 4h2a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h2'/%3E%3Crect x='8' y='2' width='8' height='4' rx='1' ry='1'/%3E%3C/svg%3E");
}

.icon-action-btn:hover .icon-copy,
.icon-action-btn:hover .icon-paste {
    opacity: 1;
}

/* Song Mode UI */
.song-timeline {
    display: flex;
    gap: 5px;
    flex-wrap: wrap;
    background: #111;
    padding: 10px;
    border: 1px solid #444;
    border-radius: 4px;
    align-items: center;
    overflow-y: auto;
    margin-top: 12px;
}

.song-block {
    background: #444;
    color: #aaa;
    padding: 5px 10px;
    border-radius: 2px;
    font-size: 0.8em;
    border: 1px solid #222;
    min-width: 30px;
    text-align: center;
    transition: all 0.2s;
}

.song-block:hover {
    background: #555;
    border-color: #ff3333;
    transform: translateY(-2px);
}

.song-block.playing {
    background: #ffcc00;
    color: #000;
    border-color: #ffaa00;
    box-shadow: 0 0 5px #ffcc00;
    font-weight: bold;
    transform: scale(1.1);
}



/* --- Track Controls (909) --- */
.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 .mini-btn.icon-btn {
    margin: 0 auto;
    width: 24px;
    height: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* --- Header Clear Button (909) --- */
.header-clear-btn {
    background: linear-gradient(to bottom, #444, #222);
    border: 1px solid #111;
    border-radius: 2px;
    padding: 3px 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.1s;
    margin-left: auto;
    box-shadow: 0 1px 1px rgba(0, 0, 0, 0.5), inset 0 1px 0 rgba(255, 255, 255, 0.1);
    margin-bottom: 1px;
}

.header-clear-btn:active {
    transform: translateY(1px);
    box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.6);
    background: #222;
}

.header-clear-btn:hover {
    background: linear-gradient(to bottom, #555, #333);
}

.header-clear-btn svg {
    stroke: #888;
    transition: stroke 0.2s;
}

.header-clear-btn:hover svg {
    stroke: #ff3333;
}