:root {
    --primary: #2c3e50;
    --correct: #27ae60;
    --wrong: #e74c3c;
    --accent: #2980b9;
    /* Professional blue */
    --accent-text: #ffffff;
    --correct-text: #ffffff;
    /* Blue for Start/Action */
    --key-width: 40px;
    --key-height: 180px;

    /* Theme Colors */
    --bg-color: #f4f7f6;
    --text-color: #2c3e50;
    --card-bg: white;
    --btn-bg: #e2e8f0;
    --btn-text: #2c3e50;
    --key-white: white;
    --key-border: #333;
    --shadow: rgba(0, 0, 0, 0.1);
}

/* Dark Mode Overrides */
[data-theme="dark"] {
    --primary: #34495e;
    --accent: #2471a3;
    /* Deeper, more comfortable blue for dark mode */
    --correct: #2ecc71;
    /* Brighter green for dark background */
    --accent-text: #ffffff;
    --correct-text: #1a1a1a;
    /* Dark text on bright green for better contrast */
    --bg-color: #1a1a1a;
    --text-color: #ecf0f1;
    --card-bg: #2c3e50;
    --btn-bg: #3e4e5f;
    --btn-text: #ecf0f1;
    --key-white: #dcdcdc;
    --key-border: #111;
    --shadow: rgba(0, 0, 0, 0.4);
}

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    display: flex;
    flex-direction: column;
    align-items: center;
    background: var(--bg-color);
    color: var(--text-color);
    margin: 0;
    padding: 20px;
    touch-action: manipulation;
    overflow-x: hidden;
    transition: background 0.3s, color 0.3s;
}

#current-key-display {
    color: var(--text-color);
}

/* Statistics UI */
.stats {
    background: var(--card-bg);
    padding: 15px 30px;
    border-radius: 12px;
    box-shadow: 0 4px 6px var(--shadow);
    margin-bottom: 20px;
    text-align: center;
    width: 90%;
    max-width: 400px;
    position: relative;
    /* For toggle positioning */
    display: flex;
    flex-direction: column;
    align-items: center;
}

.stats-controls {
    position: absolute;
    right: 15px;
    top: 50%;
    transform: translateY(-50%);
    display: flex;
    gap: 8px;
    align-items: center;
}

.stats h2 {
    margin: 0;
    font-size: 1.2rem;
    color: var(--text-color);
    /* Use theme text color */
}

.score {
    font-size: 2rem;
    font-weight: bold;
}

/* MIDI Controls */
.midi-controls {
    background: var(--card-bg);
    padding: 8.25px 16.5px;
    border-radius: 7px;
    box-shadow: 0 2px 4px var(--shadow);
    display: flex;
    align-items: center;
    gap: 8.25px;
    font-size: 0.74rem;
    width: auto;
    max-width: 330px;
    margin: 20px auto 0;
    z-index: 1;
}

.midi-controls label {
    font-weight: bold;
    color: var(--text-color);
    /* Use theme text color */
}

.midi-controls select {
    flex: 1;
    padding: 4.125px 8.25px;
    border: 1px solid #ccc;
    border-radius: 3px;
    font-size: 0.74rem;
    background: var(--card-bg);
    color: var(--text-color);
}

.midi-indicator {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: #ccc;
    transition: background 0.1s;
}

.midi-indicator.active {
    background: #27ae60;
    /* Bright green when receiving MIDI */
    box-shadow: 0 0 8px #27ae60;
}

/* Controls */
.controls {
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
    justify-content: center;
    max-width: 800px;
    margin-bottom: 30px;
}

button {
    padding: 12px 20px;
    border: none;
    border-radius: 8px;
    background: var(--btn-bg);
    color: var(--btn-text);
    cursor: pointer;
    font-weight: 600;
    font-size: 0.95rem;
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    box-shadow: 0 2px 4px var(--shadow);
    display: flex;
    align-items: center;
    justify-content: center;
}

button:hover:not(:disabled) {
    transform: translateY(-2px);
    box-shadow: 0 4px 8px var(--shadow);
    filter: brightness(1.05);
}

button:active:not(:disabled) {
    transform: translateY(0);
    box-shadow: 0 1px 2px var(--shadow);
}

/* Start Button Specifics */
#start-btn {
    background: var(--accent);
    color: var(--accent-text);
}

/* Submit Button Specifics */
button[onclick="submitAnswer()"] {
    background: var(--correct);
    color: var(--correct-text);
}

button.active {
    background: var(--correct);
    color: var(--correct-text);
}

button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* Keyboard UI */
#game-container {
    transition: transform 0.1s;
    margin-bottom: 20px;
}

.keyboard {
    display: flex;
    position: relative;
    background: #222;
    padding: 10px;
    border-radius: 8px;
    user-select: none;
}

.key {
    box-sizing: border-box;
    width: var(--key-width);
    height: var(--key-height);
    border: 1px solid var(--key-border);
    /* Fallback */
    border-right: none;
    /* Remove right border to avoid double thickness */
    background: var(--key-white);
    border-radius: 0 0 4px 4px;
    position: relative;
    cursor: pointer;
    z-index: 1;
}

.key:last-child {
    border-right: 1px solid var(--key-border);
    /* restore right border for the last key */
}

.key.black {
    width: calc(var(--key-width) * 0.7);
    height: calc(var(--key-height) * 0.6);
    background: black;
    /* Darker background */
    border: 1px solid var(--key-border);
    /* Full border for black keys */
    margin-left: calc(var(--key-width) * -0.35);
    margin-right: calc(var(--key-width) * -0.35);
    z-index: 2;
}

.key.active {
    background: #3498db !important;
}

.key.highlight {
    background: #f1c40f !important;
}

/* Shake Effect */
.shake {
    animation: shake 0.5s cubic-bezier(.36, .07, .19, .97) both;
}

@keyframes shake {

    10%,
    90% {
        transform: translate3d(-1px, 0, 0);
    }

    20%,
    80% {
        transform: translate3d(2px, 0, 0);
    }

    30%,
    50%,
    70% {
        transform: translate3d(-4px, 0, 0);
    }

    40%,
    60% {
        transform: translate3d(4px, 0, 0);
    }
}

/* Correct Answer Flash */
.correct-flash {
    animation: correctFlash 0.3s ease-out;
}

@keyframes correctFlash {
    0% {
        background-color: var(--bg-color);
    }

    50% {
        background-color: #00ff00;
        /* Bright green */
    }

    100% {
        background-color: var(--bg-color);
    }
}

/* Toggle & Status Buttons */
.control-toggle,
.theme-toggle {
    background: var(--card-bg);
    border: 1px solid var(--key-border);
    border-radius: 50%;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: 0 2px 5px var(--shadow);
    transition: transform 0.2s, background 0.2s;
    font-size: 1.1rem;
    padding: 0;
    line-height: 1;
    position: static;
    /* Let the container handle positioning */
    transform: none;
}

.control-toggle:active,
.theme-toggle:active {
    transform: scale(0.9);
}

@media (max-width: 900px) and (orientation: landscape) {
    .theme-toggle {
        width: 30px;
        height: 30px;
        font-size: 1rem;
    }
}

/* Keyboard Shortcuts Bubble */
.shortcuts-bubble {
    position: fixed;
    bottom: 20px;
    right: 20px;
    background: rgba(44, 62, 80, 0.95);
    color: white;
    padding: 15px 20px;
    border-radius: 12px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
    font-size: 0.85rem;
    line-height: 1.5;
    z-index: 1000;
}

.shortcuts-bubble strong {
    display: block;
    margin-bottom: 8px;
    font-size: 0.95rem;
}

.shortcuts-bubble div {
    opacity: 0.9;
}

/* Responsive Handling */
@media (max-width: 600px) {
    :root {
        --key-width: 30px;
        --key-height: 140px;
    }

    .midi-controls {
        display: none;
        /* Hide MIDI menu on mobile */
    }

    .shortcuts-bubble {
        display: none;
        /* Hide on mobile - no keyboard */
    }
}

/* Portrait Warning Overlay */
#portrait-warning {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: #2c3e50;
    color: white;
    z-index: 9999;
    align-items: center;
    justify-content: center;
    text-align: center;
    flex-direction: column;
    gap: 30px;
}

#portrait-warning .message {
    font-size: 1.2rem;
    font-weight: bold;
    padding: 20px;
    line-height: 1.4;
}

.phone-icon {
    width: 50px;
    height: 90px;
    border: 3px solid white;
    border-radius: 6px;
    position: relative;
    animation: rotatePhone 2s ease-in-out infinite;
}

.phone-icon::after {
    content: '';
    position: absolute;
    bottom: 5px;
    left: 50%;
    transform: translateX(-50%);
    width: 8px;
    height: 8px;
    border: 2px solid white;
    border-radius: 50%;
}

@keyframes rotatePhone {
    0% {
        transform: rotate(0deg);
    }

    40%,
    60% {
        transform: rotate(-90deg);
    }

    100% {
        transform: rotate(0deg);
    }
}

/* Mobile Landscape Layout */
@media (max-width: 900px) and (max-height: 600px) and (orientation: landscape) {
    body {
        --key-width: calc((100vw - 20px) / 15);
        /* Exact width for 15 white keys */
        display: grid;
        grid-template-rows: auto auto 1fr;
        /* Header, Controls, Keyboard (expands) */
        grid-template-columns: auto 1fr auto auto;
        grid-template-areas:
            "header-left header-center header-right header-controls"
            "controls controls controls controls"
            "keyboard keyboard keyboard keyboard";
        height: 100vh;
        width: 100vw;
        /* Add space for toggle */
        padding: 10px;
        box-sizing: border-box;
        overflow: hidden;
    }

    .stats-controls {
        grid-area: header-controls;
        position: static;
        transform: none;
        margin-left: 10px;
        gap: 5px;
    }

    .theme-toggle,
    .control-toggle {
        width: 28px;
        height: 28px;
        font-size: 0.9rem;
    }

    /* Redistribute Header Items */
    .stats {
        display: contents;
        /* Allows children to be placed in grid */
    }

    .stats h2 {
        grid-area: header-left;
        font-size: 1rem;
        text-align: left;
        align-self: center;
    }

    .score {
        grid-area: header-center;
        font-size: 1.5rem;
        margin: 0;
    }

    #current-key-display {
        grid-area: header-right;
        justify-self: end;
        align-self: center;
        background: var(--card-bg);
        /* Use theme card background */
        padding: 5px 10px;
        border-radius: 15px;
        border: 1px solid var(--key-border);
        /* Use theme border */
        font-size: 0.9rem;
        opacity: 1;
        /* Ensure full visibility */
    }

    /* Controls Bar */
    .controls {
        grid-area: controls;
        width: 100%;
        max-width: none;
        margin: 10px 0;
        justify-content: space-between;
        gap: 5px;
    }

    .controls button {
        flex: 1;
        padding: 8px 4px;
        font-size: 0.8rem;
        white-space: nowrap;
        min-width: 0;
        /* Allows shrinking below content size */
    }

    /* Keyboard Area */
    #game-container {
        grid-area: keyboard;
        display: flex;
        flex-direction: column;
        height: 100%;
        /* Fill available space */
        margin: 0;
    }

    .keyboard {
        flex: 1;
        /* Grow to fill container */
        width: fit-content;
        /* Ensure it wraps the fixed-width keys */
        margin: 0 auto;
        /* Center the keyboard */
        padding: 5px;
        box-sizing: border-box;
        overflow: hidden;
        /* Prevent spillover */
    }

    .key {
        width: var(--mobile-w-key);
        flex: 0 0 var(--mobile-w-key);
        /* Fix width to integer pixels */
        height: auto;
        /* Fill vertically */
        border-radius: 0 0 4px 4px;
    }

    .key.black {
        width: var(--mobile-b-key);
        flex: 0 0 var(--mobile-b-key);
        height: 50%;
        margin-left: var(--mobile-b-offset);
        margin-right: var(--mobile-b-offset);
    }

    /* Floating elements cleanup for mobile */
    .shortcuts-bubble,
    .midi-controls {
        display: none;
    }
}

/* Show Warning on Mobile Portrait */
@media (max-width: 600px) and (orientation: portrait) {
    #portrait-warning {
        display: flex;
        /* Show the warning */
    }

    /* Hide everything else to prevent interaction */
    .stats,
    .controls,
    #game-container,
    .shortcuts-bubble {
        display: none !important;
    }
}

/* Navigation */
.back-home {
    position: absolute;
    top: 10px;
    left: 10px;
    font-size: 0.85rem;
    color: var(--text-color);
    text-decoration: none;
    padding: 6px 12px;
    background: var(--btn-bg);
    border-radius: 6px;
    z-index: 1001;
    box-shadow: 0 2px 4px var(--shadow);
    transition: all 0.2s;
}

.back-home:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 6px var(--shadow);
}

.portrait-back-link {
    color: #4fd1c5;
    /* Bright modern teal */
    text-decoration: none;
    font-weight: bold;
    font-size: 1.1rem;
    display: inline-block;
    margin-top: 15px;
    border: 1px solid #4fd1c5;
    padding: 8px 16px;
    border-radius: 8px;
}

@media (max-width: 600px) {
    .back-home {
        display: none !important;
    }
}