* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --bg-dark: #0a0a0f;
    --bg-panel: #151520;
    --bg-module: #1a1a28;
    --border-color: #2a2a3a;
    --cyan: #00f5ff;
    --purple: #bf00ff;
    --pink: #ff00aa;
    --yellow: #ffee00;
    --green: #00ff88;
    --orange: #ff8800;
    --red: #ff3366;
    --text: #e0e0e0;
    --text-dim: #888;
    --jack-audio: #00ff88;
    --jack-cv: #ffee00;
    --jack-gate: #ff3366;
    --cable-audio: #00ff88;
    --cable-cv: #ffee00;
    --cable-gate: #ff3366;
}

body {
    font-family: 'Orbitron', -apple-system, BlinkMacSystemFont, sans-serif;
    background: var(--bg-dark);
    color: var(--text);
    min-height: 100vh;
    overflow: hidden;
}

#app {
    display: flex;
    flex-direction: column;
    height: 100vh;
}

#header {
    display: flex;
    flex-direction: column;
    padding: 6px 10px;
    background: linear-gradient(180deg, #1a1a28 0%, #0a0a0f 100%);
    border-bottom: 1px solid var(--border-color);
    gap: 6px;
    flex-shrink: 0;
}

.header-top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    flex-wrap: wrap;
}

.header-controls {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-wrap: wrap;
}

.header-bottom {
    display: flex;
    justify-content: center;
}

.app-title {
    font-size: 1.1rem;
    font-weight: 800;
    background: linear-gradient(135deg, var(--cyan) 0%, var(--purple) 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    letter-spacing: 1px;
    white-space: nowrap;
}

.preset-controls {
    display: flex;
    align-items: center;
    gap: 6px;
}

.preset-controls select {
    background: var(--bg-panel);
    border: 1px solid var(--border-color);
    color: var(--text);
    padding: 4px 8px;
    border-radius: 4px;
    font-family: inherit;
    font-size: 0.75rem;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 6px;
}

.separator {
    width: 1px;
    height: 20px;
    background: var(--border-color);
    margin: 0 4px;
}

.module-palette {
    display: flex;
    gap: 4px;
    flex-wrap: wrap;
    justify-content: center;
}

.add-module-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    padding: 8px 12px;
    min-height: 44px;
    min-width: 44px;
    background: linear-gradient(180deg, rgba(40, 40, 60, 0.8) 0%, rgba(20, 20, 30, 0.9) 100%);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    color: var(--text);
    cursor: pointer;
    transition: all 0.15s;
    font-family: inherit;
    touch-action: manipulation;
}

.add-module-btn:hover,
.add-module-btn:active {
    border-color: var(--cyan);
    background: linear-gradient(180deg, rgba(0, 245, 255, 0.2) 0%, rgba(20, 20, 30, 0.9) 100%);
    transform: translateY(-2px);
}

.add-module-btn .module-icon {
    font-size: 1rem;
}

.add-module-btn .module-name {
    font-size: 0.55rem;
    font-weight: 600;
    letter-spacing: 0.5px;
}

.add-module-btn.fx-btn {
    background: linear-gradient(180deg, rgba(191, 0, 255, 0.2) 0%, rgba(20, 20, 30, 0.9) 100%);
}

.add-module-btn.fx-btn:hover,
.add-module-btn.fx-btn:active {
    border-color: var(--purple);
    background: linear-gradient(180deg, rgba(191, 0, 255, 0.4) 0%, rgba(20, 20, 30, 0.9) 100%);
}

.palette-divider {
    color: var(--border-color);
    font-size: 1.2rem;
    line-height: 44px;
    margin: 0 4px;
}

.midi-section {
    display: flex;
    align-items: center;
    gap: 4px;
}

.midi-section label {
    font-size: 0.6rem;
    color: var(--text-dim);
    letter-spacing: 0.5px;
}

.midi-section select {
    background: var(--bg-panel);
    border: 1px solid var(--border-color);
    color: var(--text);
    padding: 3px 4px;
    border-radius: 4px;
    font-family: inherit;
    font-size: 0.65rem;
    max-width: 80px;
}

#preset-select {
    background: var(--bg-panel);
    border: 1px solid var(--border-color);
    color: var(--text);
    padding: 3px 6px;
    border-radius: 4px;
    font-family: inherit;
    font-size: 0.65rem;
}

.bus-indicator {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--text-dim);
    transition: background 0.1s;
}

.bus-indicator.active {
    background: var(--green);
    box-shadow: 0 0 8px var(--green);
}

.bus-indicator.hub-routing {
    background: var(--purple);
    box-shadow: 0 0 8px var(--purple);
}

.bus-indicator.hub-routing.active {
    background: var(--cyan);
    box-shadow: 0 0 12px var(--cyan);
}

.action-btn {
    background: var(--bg-panel);
    border: 1px solid var(--border-color);
    color: var(--text);
    padding: 4px 8px;
    border-radius: 4px;
    cursor: pointer;
    font-family: inherit;
    font-size: 0.75rem;
    transition: all 0.2s;
}

.action-btn:hover {
    border-color: var(--cyan);
    background: rgba(0, 245, 255, 0.1);
}

.action-btn.danger:hover {
    border-color: var(--red);
    background: rgba(255, 51, 102, 0.1);
}

.action-btn.midi-learn-btn {
    font-size: 0.65rem;
    letter-spacing: 1px;
}

.action-btn.midi-learn-btn.active {
    background: var(--purple);
    border-color: var(--purple);
    animation: pulse 1s infinite;
}

.action-btn.demo-btn {
    background: linear-gradient(135deg, var(--purple), var(--cyan));
    border: none;
    color: white;
    font-weight: 600;
    letter-spacing: 1px;
    padding: 6px 14px;
}

.action-btn.demo-btn:hover {
    background: linear-gradient(135deg, var(--cyan), var(--purple));
    transform: scale(1.05);
    box-shadow: 0 0 15px rgba(0, 245, 255, 0.4);
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.6; }
}

#canvas-container {
    flex: 1;
    position: relative;
    background: 
        radial-gradient(circle at center, rgba(0, 245, 255, 0.03) 0%, transparent 70%),
        linear-gradient(rgba(30, 30, 50, 0.5) 1px, transparent 1px),
        linear-gradient(90deg, rgba(30, 30, 50, 0.5) 1px, transparent 1px);
    background-size: 100% 100%, 20px 20px, 20px 20px;
    overflow: auto;
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
}

#canvas-container::-webkit-scrollbar {
    width: 10px;
    height: 10px;
}

#canvas-container::-webkit-scrollbar-track {
    background: #0a0a12;
}

#canvas-container::-webkit-scrollbar-thumb {
    background: #1a1a28;
    border-radius: 5px;
    border: 1px solid #252535;
}

#canvas-container::-webkit-scrollbar-thumb:hover {
    background: #252535;
}

#canvas-container::-webkit-scrollbar-corner {
    background: #0a0a12;
}

.cables-layer {
    position: absolute;
    top: 0;
    left: 0;
    min-width: 2000px;
    min-height: 2000px;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 1000;
    overflow: visible;
}

#cables-svg {
    pointer-events: none;
    touch-action: none;
    -webkit-user-select: none;
    user-select: none;
    overflow: visible;
}

/* Prevent text selection on touch hold */
.cables-layer .cable,
.cables-layer .cable-hitarea {
    -webkit-touch-callout: none;
}

.cables-layer .cable {
    pointer-events: none;
    cursor: default;
    stroke-width: 3;
    stroke-linecap: round;
    transition: stroke-width 0.15s, opacity 0.15s;
}

/* Invisible hit area for easier touch targeting */
.cables-layer .cable-hitarea {
    pointer-events: none;
    cursor: default;
    stroke-width: 24;
    stroke: transparent;
    fill: none;
}

/* Cable delete mode - enable pointer events on cables and SVG */
body.cable-delete-mode .cables-layer,
body.cable-delete-mode #cables-svg {
    pointer-events: all;
}

body.cable-delete-mode .cables-layer .cable,
body.cable-delete-mode .cables-layer .cable-hitarea {
    pointer-events: all;
    cursor: pointer;
}

/* Visual indicator for delete mode */
.delete-cables-btn.active {
    background: var(--red) !important;
    border-color: var(--red) !important;
    animation: pulse-delete 1s infinite;
}

@keyframes pulse-delete {
    0%, 100% { box-shadow: 0 0 5px var(--red); }
    50% { box-shadow: 0 0 15px var(--red); }
}

.cables-layer .cable:hover,
.cables-layer .cable.selected {
    stroke-width: 5;
}

.cables-layer .cable.selected {
    opacity: 0.7;
    animation: cable-pulse 0.3s ease-in-out;
}

@keyframes cable-pulse {
    0%, 100% { stroke-width: 5; }
    50% { stroke-width: 7; }
}

#modules-container {
    position: relative;
    min-width: 2000px;
    min-height: 2000px;
    z-index: 999;
}

.module {
    position: absolute;
    background: linear-gradient(180deg, var(--bg-module) 0%, rgba(15, 15, 25, 0.95) 100%);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    min-width: 160px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.5);
    user-select: none;
    z-index: 1001;
}

.module.selected {
    border-color: var(--cyan);
    box-shadow: 0 0 20px rgba(0, 245, 255, 0.3);
}

.module-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 12px;
    background: linear-gradient(180deg, rgba(60, 60, 80, 0.5) 0%, transparent 100%);
    border-bottom: 1px solid var(--border-color);
    border-radius: 7px 7px 0 0;
    cursor: move;
    touch-action: none;
    user-select: none;
    position: relative;
    z-index: 1002;
    -webkit-user-select: none;
    min-height: 44px;
    pointer-events: auto;
}

.module-title {
    font-size: 0.75rem;
    font-weight: 700;
    letter-spacing: 1px;
}

.module-close {
    background: none;
    border: none;
    color: var(--text-dim);
    cursor: pointer;
    font-size: 1.2rem;
    padding: 8px 10px;
    line-height: 1;
    min-width: 44px;
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: -8px -10px -8px 0;
    touch-action: manipulation;
}

.module-close:hover,
.module-close:active {
    color: var(--red);
    background: rgba(255, 0, 0, 0.1);
}

.module-body {
    padding: 12px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.module-row {
    display: flex;
    align-items: center;
    gap: 10px;
}

.module-jacks {
    display: flex;
    justify-content: space-between;
    padding: 8px 0;
}

.jack-column {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.jack-column.inputs {
    align-items: flex-start;
}

.jack-column.outputs {
    align-items: flex-end;
}

.jack-row {
    display: flex;
    align-items: center;
    gap: 6px;
}

.jack-row.input {
    flex-direction: row;
}

.jack-row.output {
    flex-direction: row-reverse;
}

.jack {
    width: 18px;
    height: 18px;
    border-radius: 50%;
    border: 2px solid;
    cursor: pointer;
    transition: all 0.15s;
    position: relative;
    touch-action: none;
    pointer-events: auto;
}

/* Invisible touch target extension - 44px minimum for accessibility */
.jack::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 44px;
    height: 44px;
    border-radius: 50%;
    z-index: 1;
}

.jack.audio {
    border-color: var(--jack-audio);
    background: radial-gradient(circle, var(--jack-audio) 30%, transparent 70%);
}

.jack.cv {
    border-color: var(--jack-cv);
    background: radial-gradient(circle, var(--jack-cv) 30%, transparent 70%);
}

.jack.gate {
    border-color: var(--jack-gate);
    background: radial-gradient(circle, var(--jack-gate) 30%, transparent 70%);
}

.jack:hover,
.jack.active {
    transform: scale(1.4);
    box-shadow: 0 0 12px currentColor;
}

.jack.connected {
    background: currentColor;
}

.jack.highlight {
    animation: jack-pulse 0.5s ease-in-out infinite;
}

@keyframes jack-pulse {
    0%, 100% { transform: scale(1); box-shadow: 0 0 5px currentColor; }
    50% { transform: scale(1.3); box-shadow: 0 0 15px currentColor; }
}

.jack-label {
    font-size: 0.55rem;
    color: var(--text-dim);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.knob-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    min-width: 50px;
    flex-shrink: 0;
}

.knob {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: linear-gradient(145deg, #2a2a3a, #1a1a28);
    border: 2px solid var(--border-color);
    position: relative;
    cursor: pointer;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    transform-origin: center center;
    will-change: transform;
    flex-shrink: 0;
    pointer-events: auto;
}

.knob::after {
    content: '';
    position: absolute;
    top: 4px;
    left: 50%;
    transform: translateX(-50%);
    width: 3px;
    height: 12px;
    background: var(--cyan);
    border-radius: 2px;
}

.knob.learning {
    border-color: var(--purple);
    animation: pulse 0.5s infinite;
}

.knob-label {
    font-size: 0.55rem;
    color: var(--text-dim);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.knob-value {
    font-size: 0.6rem;
    color: var(--cyan);
    font-weight: 600;
}

.select-container {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.select-container label {
    font-size: 0.55rem;
    color: var(--text-dim);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.select-container select {
    background: var(--bg-panel);
    border: 1px solid var(--border-color);
    color: var(--text);
    padding: 4px 6px;
    border-radius: 4px;
    font-family: inherit;
    font-size: 0.7rem;
    pointer-events: auto;
}

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

.fader {
    width: 8px;
    height: 60px;
    background: var(--bg-panel);
    border: 1px solid var(--border-color);
    border-radius: 4px;
    position: relative;
    cursor: pointer;
    pointer-events: auto;
}

.fader-track {
    position: absolute;
    bottom: 4px;
    left: 50%;
    transform: translateX(-50%);
    width: 2px;
    height: calc(100% - 8px);
    background: var(--border-color);
    border-radius: 1px;
}

.fader-fill {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    background: linear-gradient(180deg, var(--cyan), var(--purple));
    border-radius: 1px;
}

.fader-handle {
    position: absolute;
    left: -4px;
    width: 16px;
    height: 10px;
    background: linear-gradient(180deg, #555, #333);
    border: 1px solid var(--border-color);
    border-radius: 2px;
    transform: translateY(50%);
}

.fader-label {
    font-size: 0.55rem;
    color: var(--text-dim);
    text-transform: uppercase;
}

.fader-value {
    font-size: 0.6rem;
    color: var(--cyan);
    font-weight: 600;
}

.module.midi-input .module-header { border-top: 3px solid var(--pink); }
.module.oscillator .module-header { border-top: 3px solid var(--cyan); }
.module.noise .module-header { border-top: 3px solid var(--text-dim); }
.module.filter .module-header { border-top: 3px solid var(--orange); }
.module.envelope .module-header { border-top: 3px solid var(--yellow); }
.module.lfo .module-header { border-top: 3px solid var(--purple); }
.module.vca .module-header { border-top: 3px solid var(--green); }
.module.mixer .module-header { border-top: 3px solid var(--cyan); }
.module.output .module-header { border-top: 3px solid var(--red); }
.module.scope .module-header { border-top: 3px solid var(--purple); }
.module.delay .module-header { border-top: 3px solid #00aaff; }
.module.reverb .module-header { border-top: 3px solid #88aaff; }
.module.distortion .module-header { border-top: 3px solid #ff6600; }
.module.chorus .module-header { border-top: 3px solid #00ffaa; }

.scope-display {
    padding: 8px;
    display: flex;
    justify-content: center;
}

.scope-canvas {
    background: #0a0a12;
    border: 1px solid var(--border-color);
    border-radius: 4px;
    pointer-events: auto;
}

.modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.8);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

.modal.hidden {
    display: none;
}

.modal-content {
    background: var(--bg-module);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    max-width: 600px;
    max-height: 80vh;
    overflow: auto;
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    border-bottom: 1px solid var(--border-color);
}

.modal-header h2 {
    font-size: 1.2rem;
    background: linear-gradient(135deg, var(--cyan), var(--purple));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.modal-close {
    background: none;
    border: none;
    color: var(--text-dim);
    font-size: 1.5rem;
    cursor: pointer;
}

.modal-close:hover {
    color: var(--red);
}

.modal-body {
    padding: 20px;
}

.modal-body section {
    margin-bottom: 20px;
}

.modal-body h3 {
    color: var(--cyan);
    font-size: 0.9rem;
    margin-bottom: 10px;
}

.modal-body p,
.modal-body li {
    color: var(--text);
    font-size: 0.85rem;
    line-height: 1.6;
}

.modal-body ol,
.modal-body ul {
    padding-left: 20px;
}

.modal-body li {
    margin-bottom: 6px;
}

@media (max-width: 900px) {
    #header {
        flex-wrap: wrap;
        gap: 8px;
    }
    
    .header-center {
        order: 3;
        width: 100%;
    }
    
    .module-palette {
        justify-content: flex-start;
    }
}
