/* --- Mobile Optimization (Max Width 768px) --- */
@media (max-width: 768px) {
    body {
        /* Prevent elastic scrolling on iOS */
        overscroll-behavior: none;
        position: fixed;
        /* Lock body scroll */
        width: 100%;
        height: 100%;
    }

    #page-wrapper {
        padding: 0;
        flex-direction: column;
    }

    #app-container {
        display: block;
        /* Disable flex to use absolute positioning logic */
        width: 100vw;
        height: 100vh;
        overflow: hidden;
    }

    /* --- 1. Middle Panel (Board) - Always Visible Top Area --- */
    #middle-panel {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 60%;
        /* Top 60% for board */
        padding: 5px;
        z-index: 1;
        border-radius: 0;
        border: none;
        box-shadow: none;
    }

    /* Adjust Board Title Layout */
    #middle-panel h2 {
        font-size: 14px;
        padding: 5px;
        margin-bottom: 5px;
    }

    #clock-container {
        font-size: 10px;
    }

    /* Compact Board Controls */
    .board-controls {
        flex-wrap: wrap;
        gap: 5px;
    }

    .board-controls button,
    #import-input {
        padding: 4px 6px;
        font-size: 11px;
    }

    .board-hint {
        display: none;
        /* Save space on mobile */
    }

    /* --- 2. Left Panel (Piece List) - Bottom Drawer --- */
    #left-panel {
        position: absolute;
        bottom: 50px;
        /* Leave space for toolbar */
        left: 0;
        width: 100%;
        height: 40%;
        /* Bottom 40% for list */
        box-sizing: border-box;
        border-radius: 12px 12px 0 0;
        box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
        z-index: 100;
        display: flex;
        flex-direction: column;
        transition: transform 0.3s ease;
        padding: 10px;
    }

    /* When hidden (if we implement toggle), it can slide down */
    #left-panel.hidden {
        transform: translateY(100%);
    }

    /* Adjust Grid for Mobile */
    #piece-list {
        grid-template-columns: repeat(auto-fill, minmax(50px, 1fr));
        gap: 5px;
        padding-bottom: 20px;
    }

    /* Smaller Pieces in List */
    .piece {
        width: 50px;
        height: 65px;
    }

    .piece-avatar {
        width: 40px;
        height: 40px;
    }

    .piece-img-placeholder {
        width: 40px;
        height: 40px;
        font-size: 20px;
    }

    /* --- 3. Right Panel (Synergies) - Full Screen Overlay --- */
    #right-panel {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        z-index: 200;
        display: none;
        /* Hidden by default */
        border-radius: 0;
    }

    #right-panel.active {
        display: flex;
    }

    /* Add a close button logic for synergy panel if needed, 
       or rely on the bottom toolbar to toggle it off */

    /* --- 4. Mobile Toolbar --- */
    #mobile-toolbar {
        position: fixed;
        bottom: 0;
        left: 0;
        width: 100%;
        height: 50px;
        background: #fff;
        border-top: 1px solid #e0e0e0;
        display: flex;
        justify-content: space-around;
        align-items: center;
        z-index: 300;
    }

    .mobile-tool-btn {
        flex: 1;
        height: 100%;
        border: none;
        background: transparent;
        font-size: 14px;
        font-weight: bold;
        color: #6c757d;
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 5px;
    }

    .mobile-tool-btn.active {
        color: #007bff;
        background-color: #f8f9fa;
    }

    /* --- 5. Touch Improvements --- */
    /* Hide hover tolltips on mobile */
    #piece-tooltip,
    #synergy-tooltip {
        display: none !important;
    }

    /* Drag Ghost Element Style */
    .drag-ghost {
        position: fixed;
        pointer-events: none;
        z-index: 9999;
        opacity: 0.8;
        transform: scale(1.1);
        width: 50px;
        height: 50px;
        /* Only show the avatar part */
        border-radius: 4px;
        overflow: hidden;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    }

    .drag-ghost .piece-name {
        display: none;
    }

    .drag-ghost .piece-avatar,
    .drag-ghost .piece-img-placeholder {
        width: 100%;
        height: 100%;
    }
}

/* Desktop default: Hide mobile elements */
@media (min-width: 769px) {
    #mobile-toolbar {
        display: none;
    }
}