.rack {
    display: flex;
    flex-direction: column;
    gap: 1px;
    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;
    position: relative;
    margin: 20px auto;
}

.version-display {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 10px;
    color: #888;
    font-family: 'Courier New', monospace;
    font-weight: bold;
    user-select: none;
    position: absolute;
    top: 5px;
    right: 5px;
}

/* --- Global Controls --- */
.top-bar {
    position: sticky;
    top: 0;
    z-index: 100;
    background: #333;
    background: linear-gradient(180deg, #444 0%, #222 100%);
    color: #ddd;
    border-radius: 4px;
    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);
}

.controls-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px;
    gap: 10px;
    flex-wrap: wrap;
}

.transport-controls {
    display: flex;
    gap: 4px;
    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:active {
    box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.4);
    transform: translateY(1px);
}

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

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

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

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

.bmc {
    background: linear-gradient(to bottom, #ffd, #fa0);
}

.bmc:active {
    background: linear-gradient(to bottom, #fa0, #ffd);
}

/* Swing Control Panel */
.swing-control-panel {
    width: 100%;
    height: 0;
    overflow: hidden;
    transition: height 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    box-sizing: border-box;
    opacity: 0;
    background: rgba(0, 0, 0, 0.2);
    border-top: 1px solid transparent;
}

.swing-control-panel.open {
    height: 36px;
    opacity: 1;
    border-top: 1px solid #333;
}

.swing-label {
    color: #888;
    font-size: 11px;
    font-weight: bold;
    letter-spacing: 1px;
    white-space: nowrap;
}

.swing-display {
    font-family: 'DSEG7Classic', monospace;
    font-weight: bold;
    color: #ffcc00;
    font-size: 14px;
    background: #000;
    padding: 3px 6px;
    border-radius: 2px;
    border: 1px solid #333;
    width: 48px;
    text-align: center;
    box-shadow: inset 0 0 5px rgba(255, 204, 0, 0.2);
    white-space: nowrap;
}

.ribbon-controller {
    width: 200px;
    height: 24px;
    background: #333;
    position: relative;
    cursor: ew-resize;
    border-radius: 2px;
    overflow: hidden;
    border: 1px solid #000;
    box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.5);
}

.swing-track {
    position: absolute;
    top: 50%;
    left: 0;
    width: 100%;
    height: 2px;
    background: #555;
    transform: translateY(-50%);
    z-index: 0;
    box-shadow: 0 1px 0 rgba(255, 255, 255, 0.1);
}

.swing-guide {
    position: absolute;
    top: 25%;
    bottom: 25%;
    width: 1px;
    background: rgba(255, 255, 255, 0.2);
    transform: translateX(-50%);
    z-index: 0;
    pointer-events: none;
}

.swing-guide.swing-guide-25 {
    left: 25%;
}

.swing-guide.swing-guide-75 {
    left: 75%;
}

.swing-dot {
    position: absolute;
    top: 50%;
    transform: translate(-50%, -50%);
    z-index: 1;
    border-radius: 50%;
}

.swing-dot.fixed {
    background: #888;
    width: 10px;
    height: 10px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
}

.swing-dot.fixed.swing-dot-0 {
    left: 0%;
}

.swing-dot.fixed.swing-dot-50 {
    left: 50%;
}

.swing-dot.moving {
    background: #eee;
    width: 8px;
    height: 8px;
    border: 2px solid #333;
    box-shadow: 0 0 4px rgba(255, 255, 255, 0.3);
    pointer-events: none;
}

/* Tempo & Oscilloscope */
.tempo-group {
    display: flex;
    align-items: center;
    gap: 15px;
    background: rgba(0, 0, 0, 0.2);
    padding: 10px 20px;
    border-radius: 4px;
    border: 1px solid rgba(255, 255, 255, 0.1);
    min-height: 80px;
    box-sizing: border-box;
}

.tempo-container {
    display: flex;
    align-items: center;
    gap: 15px;
}

.oscilloscope-container {
    width: 120px;
    height: 44px;
    background: #001100;
    border: 1px solid #333;
    border-radius: 4px;
    box-shadow: inset 0 0 10px rgba(0, 255, 0, 0.1);
    position: relative;
    overflow: hidden;
    cursor: pointer;
    transition: opacity 0.3s;
}

.oscilloscope-container.disabled {
    opacity: 0.4;
    background: #000;
}

.scope-toggle {
    position: absolute;
    top: 4px;
    right: 4px;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #0f0;
    border: none;
    padding: 0;
    box-shadow: 0 0 5px #0f0;
    cursor: pointer;
    z-index: 10;
    transition: all 0.2s;
}

.oscilloscope-container.disabled .scope-toggle {
    background: #333;
    box-shadow: none;
}

.oscilloscope-container::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-image: linear-gradient(rgba(0, 255, 0, 0.1) 1px, transparent 1px),
        linear-gradient(90deg, rgba(0, 255, 0, 0.1) 1px, transparent 1px);
    background-size: 10px 10px;
    pointer-events: none;
}

#oscilloscope {
    width: 100%;
    height: 100%;
    display: block;
}

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

/* 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-select-row {
    display: grid;
    grid-template-columns: repeat(8, 38px);
    gap: 3px;
    justify-content: center;
    margin-bottom: 15px;
}

@media (min-width: 700px) {
    .pattern-select-row {
        grid-template-columns: repeat(16, 38px);
    }
}

.pat-btn,
.song-block {
    width: 38px;
    height: 38px;
    background: #444;
    color: #aaa;
    border: 1px solid #000;
    border-radius: 4px;
    font-weight: bold;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.1);
    font-size: 14px;
}

.pat-btn {
    transition: all 0.1s;
}

.song-block {
    transition: background-color 0.1s, color 0.1s, border-color 0.1s, box-shadow 0.1s, transform 0.1s;
    user-select: none;
    touch-action: none;
    position: relative;
    overflow: visible;
}

.song-block.dragging-source {
    opacity: 0.45;
}

.song-drag-ghost {
    position: fixed;
    left: 0;
    top: 0;
    margin: 0;
    z-index: 9999;
    opacity: 0.58;
    pointer-events: none;
    transition: none !important;
    will-change: transform;
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.5);
}

.pat-btn.active,
.song-block.playing {
    background: #ff3333;
    color: #fff;
    box-shadow: 0 0 10px rgba(255, 51, 51, 0.5);
    border-color: #ff0000;
}

.pat-btn.queued {
    border-color: #ffe066;
    box-shadow: 0 0 6px rgba(255, 224, 102, 0.25), inset 0 1px 0 rgba(255, 255, 255, 0.1);
    animation: pattern-queue-blink var(--pattern-queue-blink-duration, 0.48s) infinite;
}

@keyframes pattern-queue-blink {
    0%,
    44% {
        border-color: #a08a32;
        box-shadow: 0 0 4px rgba(255, 224, 102, 0.18), inset 0 1px 0 rgba(255, 255, 255, 0.08);
    }
    45%,
    100% {
        border-color: #fff2a6;
        box-shadow: 0 0 12px rgba(255, 224, 102, 0.75), 0 0 18px rgba(255, 224, 102, 0.4), inset 0 1px 0 rgba(255, 255, 255, 0.18);
    }
}

.song-block.playing {
    background: #ffcc00;
    color: #000;
    border-color: #ffaa00;
    box-shadow: 0 0 10px rgba(255, 204, 0, 0.5);
}

.song-block.queued {
    border-color: #ffe066;
    box-shadow: 0 0 6px rgba(255, 224, 102, 0.25), inset 0 1px 0 rgba(255, 255, 255, 0.1);
    animation: pattern-queue-blink var(--pattern-queue-blink-duration, 0.48s) infinite;
}

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

.mode-switch-group {
    display: flex;
    align-items: center;
    width: 100%;
    margin: -3px 0 6px;
}

.mode-switch-group .spacer {
    flex: 1;
}

.mode-switch-group .switch-group-item.mode-switch-spacer {
    visibility: hidden;
    width: 40px;
}

.switch-group-item {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.mode-switch-group .switch-group-item:nth-child(2) {
    flex: 1;
    align-items: center;
}

.mode-switch-group .switch-group-item:nth-child(1) {
    flex: 1;
    align-items: flex-start;
}

.mode-switch-group .switch-group-item:nth-child(1) .switch-label {
    width: 80px;
    text-align: center;
}

.mode-switch-group .switch-group-item:nth-child(3) {
    flex: 1;
    align-items: flex-end;
}

.mode-switch-group .switch-group-item:nth-child(3) .switch-label {
    width: 40px;
    text-align: center;
}

#shuffleBtn {
    height: 28px;
}

.song-timeline {
    display: grid;
    grid-template-columns: repeat(8, 38px);
    gap: 3px;
    justify-content: center;
    position: relative;
    padding: 15px 10px;
    border-top: 1px solid #333;
    margin-top: 5px;
    min-height: 38px;
    overflow-y: auto;
    overflow-x: hidden;
    max-height: 200px;
}

.song-drop-indicator {
    position: absolute;
    width: 2px;
    transform: translateX(-1px);
    background: linear-gradient(to bottom, #fffbcc 0%, #ff3333 45%, #ff0000 100%);
    border-radius: 2px;
    pointer-events: none;
    z-index: 20;
}

.song-block.drop-neighbor-left::after,
.song-block.drop-neighbor-right::before {
    content: '';
    position: absolute;
    top: 0;
    bottom: 0;
    width: 50%;
    pointer-events: none;
    z-index: 21;
}

.song-block.drop-neighbor-left::after {
    right: 0;
    background: linear-gradient(to left, rgba(255, 70, 70, 0.8) 0%, rgba(255, 70, 70, 0) 100%);
}

.song-block.drop-neighbor-right::before {
    left: 0;
    background: linear-gradient(to right, rgba(255, 70, 70, 0.8) 0%, rgba(255, 70, 70, 0) 100%);
}

.song-block.song-drop-landed {
    border-color: #ff6666;
    box-shadow: 0 0 12px rgba(255, 70, 70, 0.9), inset 0 0 6px rgba(255, 160, 160, 0.35);
}

.song-empty-msg {
    grid-column: 1 / -1;
    text-align: center;
    color: #666;
    font-size: 0.9em;
    font-style: italic;
    padding: 10px 0;
}

@media (min-width: 700px) {
    .song-timeline {
        grid-template-columns: repeat(16, 38px);
    }
}

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

/* --- RESPONSIVE MEDIA QUERIES (Global) --- */
@media (max-width: 768px) {
    .rack {
        margin: 0;
        padding: 0;
        border-radius: 0;
        border: none;
    }

    body {
        padding: 0;
    }

    .top-bar {
        flex-direction: row;
    }

    /* Ensure top-bar doesn't stack too early */
}

@media (max-width: 600px) {
    .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;
    }
}

@media (max-width: 400px) {
    .transport-controls {
        gap: 2px;
    }

    .transport-icon-btn {
        width: 42px;
        height: 42px;
    }
}

@media (max-width: 600px) and (orientation: portrait) {
    .tempo-group {
        width: 100%;
        justify-content: space-around;
    }
}
