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

html, body {
    margin: 0;
    padding: 0;
    overflow-x: hidden; /* Prevent horizontal scroll */
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    background-color: #1a1a1a;
    color: #ffffff;
    min-height: 100vh;
    min-height: 100dvh; /* Dynamic viewport height with fallback */
    -webkit-user-select: none;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
}

.container {
    width: 100%;
    height: 100vh; /* Fallback for older browsers */
    height: 100dvh; /* Dynamic viewport height for mobile browsers */
    background-color: #2a2a2a;
    padding: 2rem;
    padding-top: max(2rem, env(safe-area-inset-top));
    padding-bottom: max(2rem, env(safe-area-inset-bottom));
    padding-left: max(2rem, env(safe-area-inset-left));
    padding-right: max(2rem, env(safe-area-inset-right));
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
    display: flex;
    flex-direction: column;
}


h2 {
    font-size: 1.2rem;
    margin-bottom: 1rem;
    color: #cccccc;
}

h3 {
    font-size: 1rem;
    margin-bottom: 0.5rem;
    color: #aaaaaa;
}

h3.track-title {
    color: #ffffff;
    font-weight: 500;
    transition: all 0.3s ease;
}

h3.track-title.loading {
    color: #ffaa00;
    animation: pulse 1s infinite;
    cursor: not-allowed;
    opacity: 0.6;
}

h3.track-title.loading:hover {
    transform: none;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

h3.track-title.ready {
    color: #44ff44;
    animation: readyFlash 0.5s ease;
}

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

@keyframes readyFlash {
    0% { transform: scale(1); }
    50% { transform: scale(1.1); }
    100% { transform: scale(1); }
}

.controls-section {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.master-controls {
    background-color: #333333;
    padding: 0.75rem;
    border-radius: 8px;
    margin-bottom: 1rem;
    display: flex;
    align-items: center;
    gap: 1rem;
}

.control-button {
    background-color: #4CAF50;
    color: white;
    border: none;
    padding: 0.5rem;
    font-size: 1.2rem;
    border-radius: 50%;
    cursor: pointer;
    transition: all 0.3s;
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.control-button:hover {
    background-color: #45a049;
    transform: scale(1.1);
}

.control-button.playing {
    background-color: #f44336;
}

.control-button.playing:hover {
    background-color: #da190b;
}

.control-button.touching {
    transform: scale(0.95);
    box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.3);
}

.tempo-control {
    display: flex;
    align-items: center;
    gap: 1rem;
    width: 100%;
}

.tempo-control label {
    font-size: 1rem;
    white-space: nowrap;
    min-width: 80px;
}

#tempo-slider {
    flex: 1;
}

.tracks-container {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: repeat(2, 1fr);
    gap: 1rem;
    padding: 1rem;
    background-color: #1a1a1a;
    border-radius: 8px;
    flex: 1;
    width: 100%;
}

.track {
    position: relative;
    isolation: isolate;
}

.track-title {
    cursor: pointer;
    user-select: none;
    transition: all 0.2s ease;
    margin: 0;
    text-align: center;
    padding: 1rem;
    border-radius: 8px;
    width: 100%;
    height: 100%;
    background-color: #444444;
    border: 2px solid #555555;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    font-size: 1rem;
    position: relative;
    overflow: hidden;
    z-index: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 40px; /* Reduced minimum height */
}

.track-title:hover {
    background-color: #555555;
    border-color: #666666;
    transform: translateY(-1px);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
}

.track-title:active,
.track-title.touching {
    transform: translateY(0);
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}

.track-title.touching {
    background-color: #666666;
    border-color: #888888;
    animation: touchPulse 0.3s ease;
}

@keyframes touchPulse {
    0% { transform: scale(0.95); }
    50% { transform: scale(0.98); }
    100% { transform: scale(1); }
}

.track-title.muted {
    background-color: #3a3a3a;
    border-color: #444444;
    color: #666666;
    opacity: 0.8;
}

.track-title.muted:hover {
    background-color: #444444;
    border-color: #555555;
}

/* Level meter background effect */
.track-title::before {
    content: '';
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: var(--level-height, 0%);
    background: linear-gradient(to top, rgba(68, 175, 80, 0.3), rgba(68, 175, 80, 0.1));
    transition: height 0.05s ease-out;
    z-index: -1;
}

/* Green level (0-50%) */
.track-title.has-signal::before {
    background: linear-gradient(to top, rgba(68, 255, 68, 0.4), rgba(68, 255, 68, 0.1));
}

/* Yellow level (50-70%) */
.track-title.level-yellow::before {
    background: linear-gradient(to top, rgba(255, 235, 59, 0.5), rgba(255, 235, 59, 0.1));
}

/* Orange level (70-85%) */
.track-title.level-orange::before {
    background: linear-gradient(to top, rgba(255, 152, 0, 0.6), rgba(255, 152, 0, 0.1));
}

/* Red level (85-100%) */
.track-title.level-red::before {
    background: linear-gradient(to top, rgba(244, 67, 54, 0.7), rgba(244, 67, 54, 0.1));
}

/* Legacy controls - hidden */
.vertical-controls,
.level-meter,
.volume-slider {
    display: none;
}




.info-section {
    text-align: center;
    padding: 0.5rem;
    background-color: #333333;
    border-radius: 8px;
    font-size: 0.9rem;
    margin-top: 1rem;
}

.debug-counters {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1rem;
    text-align: left;
}

.debug-counters p {
    background-color: #2a2a2a;
    padding: 0.5rem;
    border-radius: 4px;
    font-family: monospace;
    font-size: 0.9rem;
    border-left: 3px solid #4CAF50;
}

input[type="range"] {
    -webkit-appearance: none;
    appearance: none;
    height: 6px;
    background: #555555;
    outline: none;
    opacity: 0.7;
    transition: opacity 0.2s;
    border-radius: 3px;
}

input[type="range"]:hover {
    opacity: 1;
}

input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 16px;
    height: 16px;
    background: #4CAF50;
    cursor: pointer;
    border-radius: 50%;
}

input[type="range"]::-moz-range-thumb {
    width: 16px;
    height: 16px;
    background: #4CAF50;
    cursor: pointer;
    border-radius: 50%;
    border: none;
}

/* Mobile landscape optimization */
@media (max-width: 768px) and (orientation: landscape) {
    .container {
        padding: 0.5rem;
        padding-top: max(0.5rem, env(safe-area-inset-top));
        padding-bottom: max(0.5rem, env(safe-area-inset-bottom));
        padding-left: max(0.5rem, env(safe-area-inset-left));
        padding-right: max(0.5rem, env(safe-area-inset-right));
    }
    
    
    .control-button {
        width: 35px;
        height: 35px;
        font-size: 1rem;
    }
    
    .master-controls {
        padding: 0.4rem;
        margin-bottom: 0.4rem;
    }
    
    .tempo-control label {
        font-size: 0.8rem;
    }
    
    .tracks-container {
        gap: 0.3rem;
        padding: 0.3rem;
        grid-template-rows: 1fr 1fr; /* Force equal height rows */
        min-height: 0; /* Allow grid to shrink */
    }
    
    .track {
        min-height: 0; /* Allow tracks to shrink to fit */
    }
    
    .track-title {
        padding: 0.5rem;
        font-size: 0.75rem;
        min-height: 35px; /* Smaller minimum for tight spaces */
    }
    
    .info-section {
        display: none;
    }
}

/* Mobile portrait */
@media (max-width: 600px) {
    .container {
        width: 100%;
        padding: 1rem;
        padding-top: max(1rem, env(safe-area-inset-top));
        padding-bottom: max(1rem, env(safe-area-inset-bottom));
        padding-left: max(1rem, env(safe-area-inset-left));
        padding-right: max(1rem, env(safe-area-inset-right));
    }
    
    
    .tracks-container {
        grid-template-columns: repeat(2, 1fr);
        grid-template-rows: repeat(4, 1fr);
        width: 100%;
    }
    
    .track-title {
        padding: 1.5rem 1rem;
        font-size: 0.9rem;
    }
    
    .info-section {
        display: none;
    }
}

/* Input styles for better touch support */
input[type="range"] {
    -webkit-user-select: auto;
    user-select: auto;
    cursor: pointer;
}

input[type="range"]::-webkit-slider-thumb {
    cursor: pointer;
    -webkit-appearance: none;
    width: 20px;
    height: 20px;
}

input[type="range"]::-moz-range-thumb {
    cursor: pointer;
}