/* Reset and base styles */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html {
    /* Fill entire viewport including safe areas */
    height: 100%;
    height: 100dvh;
    width: 100%;
    margin: 0;
    padding: 0;
    /* Solid dark background as base */
    background: #0a0a0a;
    -webkit-tap-highlight-color: transparent;
    overflow: hidden;
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    color: #ffffff;
    /* Body fills html */
    margin: 0;
    padding: 0;
    min-height: 100%;
    min-height: 100dvh;
    height: 100%;
    height: 100dvh;
    width: 100%;
    overflow-x: hidden;
    overflow-y: hidden;
    position: relative;
}

/* Background layer that extends into safe areas for PWA */
body::before {
    content: '';
    position: fixed;
    /* Extend beyond viewport into safe areas */
    top: calc(-1 * env(safe-area-inset-top, 0px));
    left: calc(-1 * env(safe-area-inset-left, 0px));
    right: calc(-1 * env(safe-area-inset-right, 0px));
    bottom: calc(-1 * env(safe-area-inset-bottom, 0px));
    background: linear-gradient(180deg, #0f1419 0%, #0a0a0a 100%);
    z-index: -1;
    pointer-events: none;
}

.container {
    max-width: 1400px;
    margin: 0 auto;
    /* Base padding */
    padding: 32px;
    /* Add safe area insets so content doesn't overlap system UI */
    padding-top: calc(32px + env(safe-area-inset-top, 0px));
    padding-bottom: calc(32px + env(safe-area-inset-bottom, 0px));
    padding-left: calc(32px + env(safe-area-inset-left, 0px));
    padding-right: calc(32px + env(safe-area-inset-right, 0px));
    /* Container fills body - with border-box, padding is included in height */
    min-height: 100%;
    min-height: 100dvh;
    height: 100%;
    height: 100dvh;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    overflow: hidden;
    background: transparent;
    position: relative;
}

/* Header/Controls Section */
.controls {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 48px;
    padding: 24px 0;
    flex-wrap: wrap;
}

.control-group {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 16px;
}

.control-label {
    font-size: 12px;
    font-weight: 600;
    letter-spacing: 0.12em;
    color: #aabbcc;
    text-transform: uppercase;
    font-family: 'Inter', sans-serif;
}

/* BPM Control */
.bpm-control {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0;
}

.bpm-display-container {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
}

.bpm-input {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 120px;
    height: 80px;
    text-align: center;
    font-size: 64px;
    font-weight: 700;
    font-family: 'Inter', sans-serif;
    background: transparent;
    border: none;
    color: #ffffff;
    outline: none;
    opacity: 0;
    visibility: hidden; /* Hidden by default */
    pointer-events: none;
    z-index: 10;
}

.bpm-input:focus {
    opacity: 1;
    visibility: visible; /* Show when focused */
    pointer-events: auto;
}

.bpm-display-container.input-focused .bpm-display {
    opacity: 0;
    visibility: hidden; /* Completely hide to prevent overlap */
}

.bpm-display {
    font-size: 64px;
    font-weight: 700;
    letter-spacing: -0.03em;
    color: #ffffff;
    text-shadow: 0 0 20px rgba(255, 255, 255, 0.3);
    line-height: 1.0;
    font-family: 'Inter', sans-serif;
    min-width: 120px;
    text-align: center;
    cursor: default;
    user-select: none;
    -webkit-user-select: none;
    transition: opacity 0.2s ease;
    pointer-events: none; /* Disable clicking on display, use buttons instead */
}

.bpm-display:focus {
    outline: 2px solid rgba(0, 170, 255, 0.5);
    outline-offset: 4px;
    border-radius: 4px;
}

/* Subdivision Control */
.subdivision-control {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0;
}

.subdivision-display {
    font-size: 32px;
    font-weight: 600;
    letter-spacing: -0.01em;
    color: #ffffff;
    font-family: 'Inter', sans-serif;
    min-width: 60px;
    text-align: center;
    cursor: default;
    user-select: none;
    -webkit-user-select: none;
    transition: opacity 0.2s ease;
    pointer-events: none; /* Disable clicking on display, use buttons instead */
}

.subdivision-display[tabindex="-1"] {
    cursor: not-allowed;
}

.bpm-display[tabindex="-1"] {
    cursor: not-allowed;
}

/* Triangle Buttons */
.triangle-btn {
    width: 44px;
    height: 44px;
    border: none;
    background: transparent;
    color: #ffffff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    border-radius: 8px;
    transition: all 0.2s ease;
    -webkit-tap-highlight-color: transparent;
}

.triangle-btn:hover:not(:disabled) {
    background: rgba(255, 255, 255, 0.1);
    transform: scale(1.1);
}

.triangle-btn:active:not(:disabled) {
    background: rgba(255, 255, 255, 0.2);
    transform: scale(0.95);
}

.triangle-btn:disabled {
    opacity: 0.3;
    cursor: not-allowed;
}

.triangle-btn svg {
    width: 100%;
    height: 100%;
    pointer-events: none;
}

/* BPM Control - show buttons */
.bpm-control {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 16px;
}

/* Subdivision Control - show buttons */
.subdivision-control {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
}

/* Start/Stop Button */
.start-stop-btn {
    width: 140px;
    height: 56px;
    border-radius: 28px;
    border: none;
    background: linear-gradient(135deg, #00aa44 0%, #008833 100%);
    color: #ffffff;
    font-size: 18px;
    font-weight: 600;
    letter-spacing: 0.05em;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.3s ease;
    box-shadow: 0 4px 20px rgba(0, 170, 68, 0.4);
    font-family: 'Inter', sans-serif;
}

.start-stop-btn:hover {
    transform: scale(1.05);
    box-shadow: 0 6px 30px rgba(0, 170, 68, 0.6);
}

.start-stop-btn:active {
    transform: scale(0.98);
}

.start-stop-btn.playing {
    background: linear-gradient(135deg, #ff3333 0%, #cc0000 100%);
    box-shadow: 0 4px 20px rgba(255, 51, 51, 0.4);
}

.start-stop-btn.playing:hover {
    box-shadow: 0 6px 30px rgba(255, 51, 51, 0.6);
}

.btn-text {
    user-select: none;
}

/* Canvas Container */
.canvas-container {
    width: 100%;
    flex: 1;
    min-height: 400px;
    max-width: 1200px;
    margin: 0 auto;
    position: relative;
    border-radius: 12px;
    overflow: hidden;
    background: linear-gradient(180deg, #0f1419 0%, #0a0a0a 100%);
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
    border: 1px solid rgba(85, 102, 119, 0.2);
}

/* Start/Stop Button Container */
.start-stop-container {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 24px 0;
}

#metronome-canvas {
    width: 100%;
    height: 100%;
    display: block;
}

.hit-flash {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.15s ease-out;
}

.hit-flash.active {
    opacity: 0.3;
    background: radial-gradient(circle at center, rgba(255, 170, 170, 0.5) 0%, transparent 70%);
}

/* Scroll-based input styling */
input[type="number"]::-webkit-inner-spin-button,
input[type="number"]::-webkit-outer-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

input[type="number"] {
    -moz-appearance: textfield;
}

/* Responsive Design */
@media (max-width: 768px) {
    .container {
        /* Reduced base padding on mobile, plus safe area insets */
        padding-top: calc(16px + env(safe-area-inset-top, 0px));
        padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px));
        padding-left: calc(12px + env(safe-area-inset-left, 0px));
        padding-right: calc(12px + env(safe-area-inset-right, 0px));
    }

    .controls {
        flex-direction: column;
        gap: 20px; /* Reduced gap */
        padding: 12px 0; /* Reduced padding */
    }

    .control-group {
        gap: 8px; /* Reduced gap */
    }

    .bpm-display {
        font-size: 48px;
    }

    .subdivision-display {
        font-size: 24px;
    }

    .triangle-btn {
        width: 40px;
        height: 40px;
    }
    
    .triangle-btn svg {
        width: 10px;
        height: 10px;
    }
    
    .bpm-control {
        gap: 12px;
    }
    
    .subdivision-control {
        gap: 8px;
    }

    .start-stop-btn {
        width: 120px;
        height: 48px;
        font-size: 16px;
    }

    .start-stop-container {
        padding: 12px 0; /* Reduced padding */
    }

    .canvas-container {
        min-height: 0; /* Remove fixed min-height, let flex handle it */
        flex: 1 1 auto; /* Allow canvas to shrink if needed */
    }

    .control-label {
        font-size: 11px;
    }
}

/* Hide scrollbars but allow scrolling */
.bpm-display-container,
.subdivision-control {
    scrollbar-width: none;
    -ms-overflow-style: none;
}

.bpm-display-container::-webkit-scrollbar,
.subdivision-control::-webkit-scrollbar {
    display: none;
}
