@media (max-width: 400px) {
    .game-type-grid {
        grid-template-columns: repeat(3, 1fr);
        gap: 6px;
    }
    .game-type-card {
        padding: 10px 4px 8px;
    }
    .game-type-icon {
        font-size: 22px;
    }
    .game-type-name {
        font-size: 11px;
    }
}

@media (max-width: 768px) {
    .chat-main.has-game-sidebar > .messages-container,
    .chat-main.has-game-sidebar > .live-controls {
        margin-right: 0;
    }

    /* Floating compact game overlay — toggled via a small button */
    #game-overlay {
        width: auto;
        min-width: 200px;
        max-width: 85vw;
        height: auto;
        max-height: 55vh;
        bottom: calc(90px + env(safe-area-inset-bottom));
        top: auto;
        left: 10px;
        right: auto;
        border-radius: 16px;
        border: none;
        box-shadow: none;
        backdrop-filter: none;
        -webkit-backdrop-filter: none;
        background: transparent !important;
        padding: 0;
        gap: 10px;
    }

    /* Compact role card inside floating overlay */
    #game-overlay .game-role-card {
        padding: 10px 14px;
        border-radius: 12px;
    }

    #game-overlay .role-card-inner {
        flex-direction: row;
        gap: 10px;
    }

    #game-overlay .role-badge {
        font-size: 24px;
    }

    #game-overlay .role-name {
        font-size: 14px;
    }

    #game-overlay .role-team {
        font-size: 11px;
    }

    /* Compact log panel */
    #game-overlay .game-log-panel {
        max-height: 120px;
    }

    .game-over-title {
        font-size: 24px;
    }
}

@media (max-width: 768px) {
    /* 1. Prevent chat/controls from shrinking */
    .chat-main.has-game-sidebar > .messages-container {
        margin-right: 0 !important;
    }
    .chat-main.has-game-sidebar + .live-controls {
        margin-right: 0 !important;
    }

    /* 2. Make the overlay an invisible boundary canvas */
    #game-overlay {
        position: absolute; /* Absolute within chat-main */
        top: 0;
        left: 0;
        right: 0;
        bottom: 0 !important;
        height: 100%;
        max-height: none !important;
        z-index: 500;
        background: transparent !important;
        box-shadow: none !important;
        border: none;
        padding: 0; /* Clear padding, let children float */
        pointer-events: none; /* Let touches pass through the blank space */
        overflow: hidden; /* Prevent scrolling the overlay itself */
    }

    /* Make internal boxes clickable */
    #game-overlay > * {
        pointer-events: auto;
    }

    /* 3. Top Anchor: Status Header anchored beneath avatars */
    .game-header {
        position: absolute;
        top: 130px; /* Lock beneath the host avatars and action bar */
        left: 10px;
        right: 10px;
        padding: 6px 12px;
        min-height: auto;
        margin: 0;
    }
    
    .game-phase-indicator {
        padding: 4px 10px;
        font-size: 12px;
    }

    /* 4. Bottom Anchor: Role Card locked securely above live-controls */
    .game-role-card {
        position: absolute;
        bottom: 180px; /* Must clear the 150px tall live-controls container */
        left: 10px;
        right: 10px;
        padding: 6px 14px;
        border-radius: 12px;
        margin: 0;
    }
    .role-card-inner {
        flex-direction: row;
        align-items: center;
        justify-content: space-between;
        gap: 10px;
    }
    .role-badge {
        font-size: 20px;
        animation: none; /* remove big pulse */
    }
    .role-name {
        font-size: 14px;
        flex: 1;
        text-align: left;
    }
    .role-team {
        text-align: right;
    }

    /* 5. Keep players horizontal and scrollable */
    .game-players-bar {
        padding: 6px;
        display: flex;
        flex-wrap: nowrap;
        overflow-x: auto;
        scrollbar-width: none; /* hide scrollbars */
        -webkit-overflow-scrolling: touch;
        gap: 6px;
    }

    /* 6. Hide Game Log (too tall for mobile, chat history suffices) */
    .game-log-panel {
        display: none !important;
    }

    /* 7. Action Center: Centered popup on mobile */
    .game-action-panel,
    .game-vote-panel {
        position: fixed;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        width: 90vw;
        box-shadow: 0 10px 40px rgba(0,0,0,0.6);
        z-index: 9999;
        max-height: 60vh;
        overflow-y: auto;
        margin: 0;
        bottom: auto;
    }
    
    /* 8. Ensure messages container extends past the action panel */
    .messages-container {
        padding-bottom: 220px !important;
    }
    
    .action-options,
    .vote-options {
        display: grid;
        grid-template-columns: repeat(2, 1fr); /* 2 columns for voting */
        gap: 6px;
    }

    .game-action-btn.pass-btn,
    .vote-btn.pass-btn {
        grid-column: span 2; /* Make skip full width */
    }
    
    /* Make the action buttons inside human speech wrap nicely */
    .action-options > div[style*="display: flex"] {
        grid-column: span 2;
        flex-direction: row;
        width: 100%;
    }
}