:root {
    --app-bg: #09090b;
    /* Zinc 950 */
    --panel-bg: #18181b;
    /* Zinc 900 */
    --border-color: #27272a;
    /* Zinc 800 */
    --text-main: #fafafa;
    --text-muted: #a1a1aa;

    /* Piano sizing variables */
    --key-width: 56px;
    --key-height: calc(var(--key-width) * 240 / 56);
    --black-key-width: calc(var(--key-width) * 34 / 56);
    --black-key-height: calc(var(--key-width) * 150 / 56);
    --black-key-margin: calc(var(--black-key-width) / -2);
}

/* Lock scroll state */
.piano-wrapper.locked {
    overflow-x: hidden !important;
    touch-action: none;
}

/* 
   FORCE LANDSCAPE HACK 
   CSS configuration to rotate the app horizontally when the user holds the phone vertically.
*/
html,
body {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
    background-color: var(--app-bg);
    font-family: 'Inter', sans-serif;
    color: var(--text-main);
    overscroll-behavior: none;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
}

#app-container {
    width: 100vw;
    height: 100vh;
    display: flex;
    flex-direction: column;
    position: absolute;
    top: 0;
    left: 0;
}

/* Mobile Portrait Mode - Rotate 90 degrees & Optimize UI sizes */
@media screen and (orientation: portrait) {
    :root {
        --key-width: 48px;
        --key-height: 100%;
        --black-key-height: 60%;
    }

    #app-container {
        /* Rotate 90 degrees, swap Width and Height */
        width: 100vh;
        height: 100vw;
        transform: rotate(90deg);
        transform-origin: left top;
        /* Translate position back after rotation */
        top: 0;
        left: 100%;
    }

    /* Completely hide the empty main workspace to give 100% space to the piano */
    main {
        display: none !important;
    }

    /* Make footer expand to fill the remaining screen space */
    footer {
        flex: 1 !important;
        display: flex !important;
        flex-direction: column !important;
        height: 0 !important; /* Force correct flexbox layout calculation */
    }

    /* 1. Header optimization with Safe Area insets */
    .app-header {
        height: calc(3.25rem + env(safe-area-inset-right)) !important;
        padding-top: calc(0.25rem + env(safe-area-inset-right)) !important; /* Shift away from physical right edge/notch */
        padding-left: calc(1rem + env(safe-area-inset-top)) !important;     /* Shift away from physical top status bar */
        padding-right: calc(1rem + env(safe-area-inset-bottom)) !important; /* Shift away from physical bottom navigation bar */
    }
    .app-header h1 {
        font-size: 0.95rem !important;
    }
    .app-header .w-8.h-8 {
        width: 1.75rem !important;
        height: 1.75rem !important;
    }
    .app-header .gap-6 {
        gap: 0.5rem !important;
    }
    .app-header #sustain-btn {
        padding-left: 0.6rem !important;
        padding-right: 0.6rem !important;
        padding-top: 0.2rem !important;
        padding-bottom: 0.2rem !important;
        font-size: 10px !important;
    }

    /* 2. Piano Control Panel optimization */
    .piano-control-panel {
        flex-wrap: nowrap !important;
        padding-left: 1rem !important;
        padding-right: 1rem !important;
        padding-top: 0.35rem !important;
        padding-bottom: 0.35rem !important;
        gap: 1rem !important;
    }
    .piano-control-panel > div.flex-wrap {
        flex-wrap: nowrap !important;
        gap: 1rem !important;
        min-width: unset !important;
    }
    .piano-control-panel #lock-btn,
    .piano-control-panel #restore-btn {
        padding-left: 0.65rem !important;
        padding-right: 0.65rem !important;
        padding-top: 0.25rem !important;
        padding-bottom: 0.25rem !important;
        font-size: 10px !important;
    }
    .piano-control-panel svg {
        width: 12px !important;
        height: 12px !important;
    }

    /* 3. Piano Wrapper & Container optimization with Safe Area insets */
    .piano-wrapper {
        flex: 1 !important;
        padding-top: 8px !important;
        padding-bottom: calc(4px + env(safe-area-inset-left)) !important;   /* Shift away from physical left edge */
        padding-left: calc(10px + env(safe-area-inset-top)) !important;     /* Shift away from physical top status bar */
        padding-right: calc(10px + env(safe-area-inset-bottom)) !important; /* Shift away from physical bottom navigation bar */
        display: flex !important;
        flex-direction: column !important;
        overflow-y: hidden !important;
    }

    .piano {
        height: 100% !important;
        width: max-content !important;
    }
}

/* Hide scrollbars for app-like interface */
::-webkit-scrollbar {
    display: none;
}

* {
    -ms-overflow-style: none;
    scrollbar-width: none;
}

/* Customize horizontal scrollbar of the Piano */
.piano-wrapper::-webkit-scrollbar {
    display: block;
    height: 4px;
}

.piano-wrapper::-webkit-scrollbar-track {
    background: transparent;
}

.piano-wrapper::-webkit-scrollbar-thumb {
    background: #3f3f46;
    border-radius: 4px;
}

.piano-wrapper {
    position: relative;
    padding-top: 12px;
    background: #111;
    border-top: 1px solid #333;
    box-shadow: inset 0 10px 20px rgba(0, 0, 0, 0.5);
    padding: 12px 16px 16px 16px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    display: flex;
    justify-content: flex-start;
}

.piano {
    display: flex;
    align-items: flex-start;
    margin: 0 auto;
    padding-bottom: 5px;
    touch-action: none;
}

.key {
    flex-shrink: 0;
    position: relative;
    cursor: pointer;
    transition: transform 0.05s ease, box-shadow 0.05s ease, background 0.1s ease;
    transform-origin: top center;
}

/* White keys */
.white-key {
    width: var(--key-width);
    height: var(--key-height);
    background: #ffffff;
    z-index: 1;
    border: 1px solid #e4e4e7;
    border-bottom: 4px solid #d4d4d8;
    border-radius: 0 0 6px 6px;
    box-shadow:
        -1px 0 0px rgba(255, 255, 255, 0.8),
        1px 0 0px rgba(255, 255, 255, 0.8),
        0 4px 5px rgba(0, 0, 0, 0.2);
    display: flex;
    align-items: flex-end;
    justify-content: center;
    padding-bottom: 12px;
}

.white-key.active {
    background: #f4f4f5;
    border-bottom: 2px solid #d4d4d8;
    transform: rotateX(1deg) translateY(2px);
    box-shadow:
        inset 0 2px 5px rgba(0, 0, 0, 0.1),
        0 1px 2px rgba(0, 0, 0, 0.2);
}

/* Black keys */
.black-key {
    width: var(--black-key-width);
    height: var(--black-key-height);
    background: linear-gradient(to bottom, #27272a 0%, #09090b 100%);
    z-index: 2;
    margin-left: var(--black-key-margin);
    margin-right: var(--black-key-margin);
    border-radius: 0 0 4px 4px;
    box-shadow:
        inset 1px 0px 2px rgba(255, 255, 255, 0.1),
        inset -1px 0px 2px rgba(255, 255, 255, 0.1),
        -2px 0px 4px rgba(0, 0, 0, 0.4),
        2px 0px 4px rgba(0, 0, 0, 0.4),
        0 6px 8px rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: flex-end;
    justify-content: center;
    padding-bottom: 15px;
}

.black-key.active {
    background: linear-gradient(to bottom, #18181b 0%, #000000 100%);
    transform: rotateX(1.5deg) translateY(2px);
    box-shadow:
        inset 0px -1px 1px rgba(255, 255, 255, 0.05),
        0 2px 3px rgba(0, 0, 0, 0.8);
}

/* Labels on keys */
.key-label {
    width: 100%;
    text-align: center;
    pointer-events: none;
}

.solfege-text {
    display: block;
    font-size: clamp(8px, calc(var(--key-width) * 11 / 56), 14px);
    font-weight: 600;
    color: #a1a1aa;
    margin-bottom: 2px;
    letter-spacing: 0.5px;
}

.pc-key-text {
    display: block;
    font-size: clamp(6px, calc(var(--key-width) * 8 / 56), 11px);
    font-weight: 500;
    color: #71717a;
    letter-spacing: 1px;
}

.black-key .pc-key-text {
    color: #52525b;
    font-size: clamp(6px, calc(var(--key-width) * 9 / 56), 12px);
    margin-bottom: 6px;
}

.active .solfege-text {
    color: #09090b;
    /* Dark color when pressed (white keys) */
}

.active .pc-key-text {
    color: #3f3f46;
}

.black-key.active .pc-key-text {
    color: #fafafa;
    /* Light color when pressed (black keys) */
}

/* Custom Range Input (Slider) */
input[type=range] {
    -webkit-appearance: none;
    background: transparent;
}

input[type=range]::-webkit-slider-thumb {
    -webkit-appearance: none;
    height: 16px;
    width: 16px;
    border-radius: 50%;
    background: #fafafa;
    cursor: pointer;
    margin-top: -6px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
}

input[type=range]::-webkit-slider-runnable-track {
    width: 100%;
    height: 4px;
    cursor: pointer;
    background: #3f3f46;
    border-radius: 2px;
}

/* Range Slider Disabled state */
input[type=range]:disabled::-webkit-slider-thumb {
    background: #52525b;
    cursor: not-allowed;
}
input[type=range]:disabled::-webkit-slider-runnable-track {
    background: #27272a;
    cursor: not-allowed;
}

/* Flying notes (Monochrome) */
.flying-note {
    position: absolute;
    font-size: 24px;
    color: #fafafa;
    pointer-events: none;
    animation: floatUp 1.5s cubic-bezier(0.2, 0.8, 0.2, 1) forwards;
    text-shadow: 0 0 12px rgba(255, 255, 255, 0.4);
    z-index: 10;
}

@keyframes floatUp {
    0% {
        transform: translateY(0) scale(0.8);
        opacity: 0;
    }

    20% {
        opacity: 1;
    }

    100% {
        transform: translateY(-120px) scale(1.2);
        opacity: 0;
    }
}

/* Customize Instrument Buttons */
.hide-scrollbar::-webkit-scrollbar {
    display: none;
}

.hide-scrollbar {
    -ms-overflow-style: none;
    scrollbar-width: none;
}

.inst-btn {
    padding: 10px 20px;
    border-radius: 9999px;
    color: #a1a1aa;
    background: transparent;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    text-align: center;
    min-width: 120px;
    border: 1px solid transparent;
}

.inst-btn.active {
    background: #fafafa;
    color: #09090b;
    box-shadow: 0 4px 15px rgba(255, 255, 255, 0.15);
}

.inst-btn.active span:last-child {
    color: #52525b;
}

.inst-btn:hover:not(.active) {
    background: rgba(255, 255, 255, 0.05);
    color: #e4e4e7;
}

/* Loading Screen */
#loading-overlay {
    backdrop-filter: blur(12px);
    z-index: 9999;
}

.spinner {
    border: 3px solid rgba(255, 255, 255, 0.1);
    border-left-color: #fafafa;
    border-radius: 50%;
    width: 48px;
    height: 48px;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    100% {
        transform: rotate(360deg);
    }
}

/* Custom UI elements for Mobile Select and Fullscreen */
#inst-select-mobile {
    display: none;
}

.block.md\:hidden {
    display: none;
}

#fullscreen-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border-radius: 9999px;
    background-color: #18181b;
    border: 1px solid #27272a;
    color: #a1a1aa;
    transition: all 0.2s ease;
    cursor: pointer;
}

#fullscreen-btn:hover {
    border-color: #3f3f46;
    color: #ffffff;
    background-color: #27272a;
}

@media screen and (orientation: portrait) {
    .block.md\:hidden {
        display: block !important;
    }
    
    #inst-select-mobile {
        display: block !important;
        background-color: #18181b;
        border: 1px solid #27272a;
        color: #d4d4d8;
        font-size: 11px;
        font-weight: 600;
        border-radius: 9999px;
        padding: 4px 28px 4px 10px;
        outline: none;
        cursor: pointer;
        transition: all 0.2s ease;
    }
    
    #inst-select-mobile:hover {
        border-color: #3f3f46;
        color: #ffffff;
    }
}
