@media (max-width: 768px) {
    /* ==========================================================================
       MOBILE CSS MASTER GUIDE
       ==========================================================================
       
       Strategy:
       This file contains ALL mobile overrides. It uses !important generously to
       ensure desktop styles from style.css are strictly overridden.
       
       Sections:
       1. GLOBAL LAYOUT
       2. HEADER AREA (Top Bar, Buttons, Title)
       3. GAME BOARD (History & Current Guess)
       4. STAFF SYSTEM (The Lines & Container)
       5. NOTE SIZING (Widths & Slots)
       6. NOTE VERTICAL ALIGNMENT (Pitch Offsets)
       7. VISUAL ASSETS (Icons & Buttons)
       8. ARROW INDICATORS (Precision Positioning)
       9. MODALS & UTILITIES
       ========================================================================== */


    /* ==========================================================================
       1. GLOBAL LAYOUT
       ========================================================================== */

    body {
        padding: 0.5rem;
    }

    .app-container {
        min-width: 0;
        width: 100%;
        overflow-x: hidden;
        /* Prevent horizontal scroll on body */
    }

    /* Stack History and Current Game vertically */
    .history-row {
        padding: 0.25rem;
    }

    .current-section {
        padding: 0.5rem 0;
        /* Only vertical padding */
        width: 100%;
        max-width: none;
        box-sizing: border-box;
    }

    /* Restore padding for inner containers */
    .contour-widget,
    .controls-row {
        padding: 0 0.5rem;
        box-sizing: border-box;
    }


    /* ==========================================================================
       2. HEADER AREA
       ========================================================================== */

    /* 
       The header uses absolute positioning for the Left (Stats) and Right (Help)
       buttons, while the Title is centered in the flow.
    */

    header {
        position: relative !important;
        display: flex !important;
        justify-content: center !important;
        align-items: center !important;
        width: 100%;
        height: 70px;
        /* Compact height */
        padding: 0 !important;
    }

    /* Break buttons out of their desktop container */
    .controls {
        position: static !important;
        /* Override desktop absolute */
        display: contents !important;
        /* Let children participate in header layout */
    }

    /* [LEFT] Stats Button Position */
    #btn-stats {
        position: absolute !important;
        left: 0.5rem !important;
        top: 50% !important;
        transform: translateY(-50%) !important;
        margin: 0 !important;
    }

    /* [RIGHT] Help Button Position */
    #btn-help {
        position: absolute !important;
        right: 0.5rem !important;
        top: 50% !important;
        transform: translateY(-50%) !important;
        margin: 0 !important;
    }

    /* [CENTER] Title Area */
    .logo-area {
        display: flex;
        flex-direction: column;
        align-items: center;
        text-align: center;
        margin: 0 auto;
        padding: 0 3rem;
        /* Safer padding to avoid touching buttons */
    }

    .logo-area h1 {
        font-size: 1.75rem;
        margin: 0;
        line-height: 1.2;
        /* SCALED DOWN SHADOWS: Adjusted for smaller font */
        text-shadow:
            -1.5px -1.5px 0 #000000,
            1.5px -1.5px 0 #000000,
            -1.5px 1.5px 0 #000000,
            1.5px 1.5px 0 #000000 !important;
    }

    .subtitle {
        font-size: 0.75rem;
        padding: 2px 4px;
    }


    /* ==========================================================================
       3. GAME BOARD LAYOUT
       ========================================================================== */

    /* Reorder sections: Current Guess first, then Previous Guesses */
    main {
        display: flex !important;
        flex-direction: column !important;
    }

    .current-section {
        order: 1 !important;
    }

    .history-section {
        order: 2 !important;
    }

    .feedback-message {
        order: 3 !important;
    }

    .legend {
        order: 4 !important;
    }

    .score-display {
        order: 5 !important;
    }

    .section-label {
        text-align: center;
        padding: 0 0.5rem;
    }

    .history-label {
        font-size: 1rem;
        width: 30px;
    }

    /* --- COLLAPSIBLE HISTORY SECTION --- */
    .history-section .section-label {
        display: flex;
        justify-content: center;
        align-items: center;
        gap: 0.5rem;
        cursor: pointer;
    }

    .collapse-toggle {
        background: none;
        border: none;
        font-size: 1rem;
        cursor: pointer;
        padding: 0.25rem;
        color: var(--text-secondary);
        transition: transform 0.2s ease;
    }

    .collapse-toggle:hover {
        color: var(--text-primary);
    }

    /* Arrow flips when collapsed */
    .history-section.collapsed .collapse-toggle {
        transform: rotate(180deg);
    }

    /* Show all guesses, no scrollbar */
    #guess-history {
        overflow: visible !important;
        max-height: none !important;
        transition: max-height 0.3s ease, opacity 0.3s ease;
    }

    /* Collapsed state hides content */
    .history-section.collapsed #guess-history {
        max-height: 0 !important;
        overflow: hidden !important;
        opacity: 0;
    }

    /* Contur Widget Container */
    .contour-widget {
        width: 100%;
        height: 100px;
    }

    /* Game Controls (Play/Guess Buttons) */
    .controls-row {
        flex-direction: row;
        width: 100%;
        gap: 0.5rem;
    }

    .action-btn {
        flex: 1;
        /* Equal width buttons */
        width: auto;
        padding: 0.6rem 0.8rem;
        font-size: 0.95rem;
    }


    /* ==========================================================================
       4. STAFF SYSTEM (CORE)
       ========================================================================== */

    /* 
       CRITICAL: On mobile, the staff is NOT a specific width. 
       It is display: block and width: 100% to fill the screen.
       We do NOT use the desktop scale() transform.
    */

    .staff-container {
        /* Vertical spacing between staff lines */
        --step: 14px;
        margin-top: -50px !important;
        height: 220px;
        display: block !important;
        width: 100% !important;
        max-width: 100% !important;
        min-width: 0 !important;
        overflow-x: hidden;
        padding: 0;
        position: relative;

        /* Reset Desktop Transforms */
        transform: none !important;
        transform-origin: initial !important;
        margin-bottom: -1.5rem !important;
    }

    /* The lines background */
    .staff-lines {
        width: 100% !important;
        position: absolute;
        inset: 0;
        /* Cover entire container */
    }


    /* ==========================================================================
       5. NOTE SIZING (WIDTHS & SLOTS)
       ========================================================================== */

    /* 
       The melody slots container holds the interactive notes.
       It uses justify-content: space-between to spread 7 notes across the width.
    */
    #melody-slots {
        display: flex !important;
        width: 100% !important;
        height: 100%;
        position: relative;
        z-index: 2;
        justify-content: space-between !important;
        align-items: center;
        padding-left: 4px;
        padding-right: 4px;
        /* Small buffer from screen edge */
        box-sizing: border-box;
    }

    /* 
       DYNAMIC WIDTHS:
       We use a variable usually set by JS or fallback to ~45px.
       Since we use flex space-between, the width doesn't need to be exact,
       but it should be consistent.
    */
    .note,
    .history-note,
    .note.quarter,
    .note.eighth,
    .note.sixteenth,
    .note.half,
    .note.whole,
    .history-note.quarter,
    .history-note.eighth,
    .history-note.sixteenth,
    .last-note {
        /* DYNAMIC WIDTH: Use % to guarantee they fit on screen */
        width: 12.5% !important;
        /* Forces 8 notes to fit (100% / 8) */
        max-width: 45px !important;
        /* Cap max size */
        flex-shrink: 1 !important;
        /* Allow shrinking if needed */
    }

    .note-visual {
        width: 125% !important;
        /* Increase visual width (overlap gaps) */
        margin-left: -12.5% !important;
        /* Center the oversized visual */
        max-width: none;
        height: 130px !important;
        /* Increase height slightly */
    }

    /* Hit area size */
    .note {
        --sprite-anchor: 20px;
        height: 60px;
    }

    /* History Staff (Smaller) */
    .history-staff {
        height: 120px;
        margin: -20px 0;
        /* Increased from 60px to fit 110px notes */
        margin-bottom: 0px !important;

        /* FORCE FULL WIDTH (Override style.css 140% scale hack) */
        width: 100% !important;
        transform: none !important;
        transform-origin: initial !important;
    }

    /* History Notes - Requested 3x larger */
    .history-note .note-visual.small {
        width: 55px !important;
        /* Was 20px -> ~2.75x width */
        height: 110px !important;
        /* Was 40px -> ~2.75x height */
        /* Ensure it doesn't push layout, let it overflow */
    }

    /* FORCE HISTORY NOTES EDGE-TO-EDGE */
    .history-notes {
        width: 100% !important;
        justify-content: space-between !important;
        display: flex !important;
    }


    /* ==========================================================================
       MANUAL CONFIGURATION: CURRENT GUESS NOTES
       ========================================================================== 
       Use these two variables to control EVERYTHING about current note placement.
    */
    .note {
        /* 1. SPACING: Distance between lines */
        --c-step: 14px;

        /* 2. START HEIGHT: Move ALL notes up or down together. 
           (Negative = Up, Positive = Down) */
        --c-base: -20px;
    }

    /* 
       DO NOT EDIT BELOW THIS LINE unless you want to break the pattern.
       The logic below automatically calculates position based on your variables above.
    */

    /* --- UP --- */
    .note[data-pitch="C5"] {
        transform: translateY(calc(var(--c-base) - (3 * var(--c-step)))) !important;
    }

    .note[data-pitch="B"] {
        transform: translateY(calc(var(--c-base) - (2 * var(--c-step)))) !important;
    }

    .note[data-pitch="A"] {
        transform: translateY(calc(var(--c-base) - (1 * var(--c-step)))) !important;
    }

    /* --- CENTER (G) --- */
    .note[data-pitch="G"] {
        transform: translateY(var(--c-base)) !important;
    }

    /* --- DOWN --- */
    .note[data-pitch="F"] {
        transform: translateY(calc(var(--c-base) + (1 * var(--c-step)))) !important;
    }

    .note[data-pitch="E"] {
        transform: translateY(calc(var(--c-base) + (2 * var(--c-step)))) !important;
    }

    .note[data-pitch="D"] {
        transform: translateY(calc(var(--c-base) + (3 * var(--c-step)))) !important;
    }

    .note[data-pitch="C"] {
        transform: translateY(calc(var(--c-base) + (4 * var(--c-step)))) !important;
    }

    /* ==========================================================================
       MANUAL CONFIGURATION: HISTORY NOTES
       ========================================================================== 
       Use these two variables to control EVERYTHING about history note placement.
    */
    .history-note {
        /* 1. SPACING: How far apart are the notes? (Distance between lines) */
        --h-step: 10.5px;

        /* 2. START HEIGHT: Move ALL notes up or down together. 
           (Negative = Up, Positive = Down) */
        --h-base: -41px;
    }

    /* 
       DO NOT EDIT BELOW THIS LINE unless you want to break the pattern.
       The logic below automatically calculates position based on your variables above.
    */

    /* --- UP --- */
    .history-note[data-pitch="C5"] {
        transform: translateY(calc(var(--h-base) - (3 * var(--h-step))));
    }

    .history-note[data-pitch="B"] {
        transform: translateY(calc(var(--h-base) - (2 * var(--h-step))));
    }

    .history-note[data-pitch="A"] {
        transform: translateY(calc(var(--h-base) - (1 * var(--h-step))));
    }

    /* --- CENTER (G) --- */
    .history-note[data-pitch="G"] {
        transform: translateY(var(--h-base));
    }

    /* --- DOWN --- */
    .history-note[data-pitch="F"] {
        transform: translateY(calc(var(--h-base) + (1 * var(--h-step))));
    }

    .history-note[data-pitch="E"] {
        transform: translateY(calc(var(--h-base) + (2 * var(--h-step))));
    }

    .history-note[data-pitch="D"] {
        transform: translateY(calc(var(--h-base) + (3 * var(--h-step))));
    }

    .history-note[data-pitch="C"] {
        transform: translateY(calc(var(--h-base) + (4 * var(--h-step))));
    }


    /* ==========================================================================
       7. VISUAL ASSETS & ICONS
       ========================================================================== */

    /* Force icon buttons to be uniform squares */
    .icon-btn {
        width: 40px !important;
        height: 40px !important;
        font-size: 1.5rem !important;
        padding: 0 !important;
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
    }


    /* ==========================================================================
       8. ARROW INDICATORS (PRECISION POSITIONING)
       ========================================================================== */

    /* 
       These settings control the "High/Low" hint arrows.
       EDIT HERE to move arrows for all notes at once.
    */

    /* --- CURRENT GUESS ARROWS --- */

    .arrow-hint img {
        width: 20px !important;
        height: 24px !important;
    }

    /* [UP ARROW] - "Pitch needs to be higher" */
    .arrow-hint.up {
        top: -22px !important;
        /* Distance above note */
        left: 56% !important;
        /* Horizontal center */
        margin-left: -12px !important;
        /* Center alignment (half width) */
    }

    /* [DOWN ARROW] - "Pitch needs to be lower" */
    .arrow-hint.down {
        top: 65px !important;
        /* Distance below note */
        left: 50% !important;
        margin-left: -12px !important;
    }


    .feedback-message {
        text-align: center;
        font-family: var(--font-display);
        font-size: 0.8rem;
        color: var(--text-secondary);
        min-height: 2.5rem;
        padding: 0.0rem !important;
        text-transform: uppercase;
    }

    /* --- HISTORY ARROWS (Smaller) --- */

    .history-note .arrow-hint img {
        width: 16px !important;
        height: 16px !important;
    }

    /* [UP ARROW] */
    .history-note .arrow-hint.up {
        top: 40px !important;
        left: 67% !important;
        margin-left: -8px !important;
    }

    /* [DOWN ARROW] */
    .history-note .arrow-hint.down {
        top: 45px !important;
        left: 67% !important;
        margin-left: -8px !important;
    }


    /* ==========================================================================
       9. MODALS & UTILITIES
       ========================================================================== */

    /* Stats Grid Layout */
    .stats-grid {
        grid-template-columns: 1fr 1fr;
        gap: 0.5rem;
    }

    .modal-content {
        width: 95%;
        padding: 1rem;
    }

    /* Footer Layout - Hide desktop tools */
    footer {
        flex-wrap: wrap;
    }

    #btn-dev,
    #btn-new {
        display: none !important;
    }
}