/* Mobile-First Responsive Design */

/* Mobile Layout Variables */
:root {
    --mobile-keyboard-height: 50vh; /* Approximate keyboard height */
    --safe-area-inset-top: env(safe-area-inset-top, 0);
    --safe-area-inset-bottom: env(safe-area-inset-bottom, 0);
    --safe-area-inset-left: env(safe-area-inset-left, 0);
    --safe-area-inset-right: env(safe-area-inset-right, 0);
}

/* Mobile Container */
@media (max-width: 768px) {
    /* Keep connection status fixed and safe-area aware like the mobile menu button */
    #connectionStatus {
        position: fixed !important;
        top: calc(20px + env(safe-area-inset-top, 0)) !important;
        right: calc(8px + env(safe-area-inset-right, 0)) !important;
        z-index: 1001 !important; /* above content, under menu panel (1002) */
    }
    body {
        padding: 0;
        margin: 0;
        min-height: 100vh;
        overflow-y: auto; /* Allow scrolling */
        overflow-x: hidden;
        position: relative; /* Ensure proper positioning context */
        -webkit-text-size-adjust: 100%; /* Prevent font size changes on rotation */
        text-size-adjust: 100%; /* Prevent font size changes on rotation */
    }

    .container {
        max-width: 100%;
        width: 100%;
        min-height: 100vh;
        height: auto;
        /* Prefer a single scroll owner (body) to avoid nested scroll issues */
        overflow-y: visible;
        overflow-x: hidden;
        border-radius: 0;
        padding: 0;
        margin: 0;
        background: #0a0a0a;
        -webkit-overflow-scrolling: touch;
    }

    h1 {
        font-size: 1.5rem;
        margin-bottom: 1rem;
    }

    /* Screens Layout */
    .screen {
        min-height: 100vh; /* Minimum height of viewport */
        height: auto; /* Allow natural expansion */
        display: flex;
        flex-direction: column;
        overflow: visible; /* Let content flow */
    }

    .screen.active {
        display: flex;
    }

    /* Home Screen Mobile */
    #homeScreen {
        overflow-y: visible; /* Changed from auto to visible */
        height: auto; /* Allow natural height expansion */
        min-height: 100%;
        padding: 1rem;
        padding-top: calc(1rem + env(safe-area-inset-top, 20px)); /* Safe area padding */
        padding-bottom: calc(1rem + env(safe-area-inset-bottom, 0)); /* Normal bottom padding */
    }

    /* Game Screen Mobile Layout */
    #gameScreen {
        position: relative;
        height: auto; /* Natural height */
        padding: 0;
        padding-top: 0; /* Remove top padding - let children handle spacing */
        padding-bottom: 100px; /* Add padding at bottom for scrolling space */
        overflow: visible;
    }

    /* Remove the grid container on mobile */
    #gameScreen > div {
        display: block !important; /* Remove grid */
        height: auto !important;
        overflow: visible !important;
    }

    /* Hide side panel on mobile */
    .players-panel {
        display: none !important;
    }
    

    /* Game Header */
    .game-info {
        position: relative; /* Changed from sticky to prevent overlap issues */
        z-index: 50;
        background: rgba(17, 24, 39, 0.98);
        backdrop-filter: blur(10px);
        margin: 0; /* All margins removed */
        padding: 0; /* All padding removed */
        border-radius: 0;
        flex-shrink: 0;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
    

    /* Main Game Area - this is now the direct child on mobile */
    #gameScreen > div > div:first-child {
        display: block;
        height: auto;
        overflow: visible;
        position: relative;
        z-index: 1;
        padding: 0;
    }
    
    /* Ensure proper game screen layout */
    #gameScreen.active {
        display: flex;
        flex-direction: column;
        height: 100%;
    }
    
    #gameScreen > div:last-child {
        flex: 1;
        display: flex !important;
        flex-direction: column !important;
        overflow: visible !important; /* Changed from hidden */
    }

    /* Hint Box Mobile */
    .hint-box {
        margin: 0.5rem;
        padding: 0.5rem;
        font-size: 0.9rem;
        flex-shrink: 0;
    }

    /* Winner display on mobile */
    .winner-display {
        margin: 0 !important;
        margin-top: 6px !important; /* Tighter gap from input */
        padding: 0.5rem !important;
        font-size: 0.9rem;
    }
    
    .winner-display h3 {
        font-size: 1rem !important;
        margin: 0.25rem 0 !important;
    }
    
    .winner-display p {
        font-size: 0.85rem !important;
        margin: 0.25rem 0 !important;
    }
    
    /* Latest guess section on mobile */
    .latest-guess-section {
        margin: 0 !important; /* All margins removed */
        margin-top: 6px !important; /* Tighter gap from input */
        padding: 0.375rem !important; /* Half padding */
    }
    
    .latest-guess-section h4 {
        font-size: 0.8rem !important; /* Smaller heading */
        margin: 0 !important;
    }
    
    /* Override border for mobile */
    .latest-guess-section {
        border: 1px solid rgba(139, 92, 246, 0.3) !important; /* Thinner border */
    }
    
    .latest-guess-item {
        padding: 0.375rem !important; /* Half padding */
        font-size: 0.85rem !important; /* Smaller text */
    }
    
    /* All guesses section container - matches latest guess section */
    .all-guesses-section {
        margin: 0 !important;
        margin-top: 7px !important; /* Reduced gap from latest guess section */
        margin-bottom: 100px !important; /* Same as main page fix */
        padding: 0.375rem !important; /* Match latest guess padding */
        background: rgba(139, 92, 246, 0.1) !important; /* Match latest guess background */
        border: 1px solid rgba(139, 92, 246, 0.3) !important; /* Match latest guess border */
        border-radius: 12px !important; /* Match latest guess radius */
    }
    
    /* Guesses heading inside container */
    .all-guesses-section h3 {
        font-size: 0.8rem !important; /* Match h4 size */
        margin: 0 !important; /* Remove margins */
        color: #a78bfa !important; /* Match purple color */
    }
    
    /* Guesses list inside container - remove duplicate styling */
    .all-guesses-section .guesses-list {
        margin: 0 !important;
        padding: 0 !important; /* Remove padding since container has it */
        background: transparent !important; /* Remove background since container has it */
        border: none !important; /* Remove border since container has it */
        border-radius: 0 !important; /* Remove radius since container has it */
    }
    
    /* Ensure the h3 heading is visible */
    .mobile-guesses-container h3 {
        display: block !important;
        color: white !important;
        margin: 10px 0 0 0 !important; /* 10px top margin */
        font-size: 1.2rem !important;
        z-index: 1002 !important;
        position: relative;
        background: rgba(17, 24, 39, 1) !important; /* Solid background */
        padding: 0.5rem !important;
    }

    /* Override any conflicting styles */
    .guesses-list {
        overflow-y: visible !important; /* Allow infinite expansion */
        overflow-x: hidden !important;
        display: block !important;
        visibility: visible !important;
        position: relative;
        z-index: 1 !important; /* Normal z-index */
    }

    /* Floating Recent Guesses (shows above keyboard) */
    .recent-guesses-float {
        position: fixed;
        bottom: 60px; /* Above input */
        left: 0;
        right: 0;
        background: rgba(17, 24, 39, 0.95);
        backdrop-filter: blur(20px);
        border-top: 2px solid #374151;
        padding: 0.5rem;
        z-index: 90;
        max-height: 30vh;
        overflow-y: auto;
        display: none; /* Hidden by default, shown when input focused */
        box-shadow: 0 -4px 20px rgba(0,0,0,0.5);
    }

    .recent-guesses-float.show {
        display: block;
        animation: slideUp 0.3s ease-out;
    }

    @keyframes slideUp {
        from { transform: translateY(100%); }
        to { transform: translateY(0); }
    }

    .recent-guess-item {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 0.5rem;
        margin: 0.25rem 0;
        background: #111827;
        border-radius: 8px;
        font-size: 0.9rem;
    }

    /* Mobile Input Container - scrolls with content (game screen only) */
    #gameScreen .guess-input-container {
        position: static !important;
        top: auto !important;
        margin-left: 0;
        width: 60% !important; /* as requested: 60% pill width on mobile */
        background: rgba(17, 24, 39, 0.98);
        backdrop-filter: blur(20px);
        padding: 0.375rem 0.75rem; /* revert to default vertical padding */
        border: 2px solid #374151; /* Border all around */
        border-radius: 12px; /* Round all corners */
        z-index: auto;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.5); /* Shadow now points down */
        transform: none;
        will-change: auto;
        overflow-anchor: auto;
    }
    
    /* Input wrapper becomes flex container on mobile */
    .input-wrapper {
        display: flex !important;
        gap: 0.4rem;
        align-items: center;
        width: 100%;
        margin-bottom: 0.5rem;
    }
    
    /* Input field takes up appropriate space */
    .input-wrapper #guessInput {
        flex: 1 1 auto; /* Take available space */
        max-width: 60%; /* Don't get too wide */
        margin: 0;
        font-size: 16px; /* Prevents zoom on iOS */
        padding: 0.5rem;
    }
    
    /* Mobile Input Container - holds timer and counter */
    .mobile-input-container {
        display: flex !important;
        gap: 0.4rem;
        align-items: center;
        flex: 0 0 auto; /* Don't grow or shrink */
    }
    
    /* Mobile Timer Display - now inline with input */
    .mobile-only-timer {
        position: static !important;
        display: inline-block !important;
        background: linear-gradient(135deg, #1f2937 0%, #111827 100%);
        border: 2px solid #ef4444;
        border-radius: 20px; /* Pill shape */
        padding: 0.3rem 0.7rem;
        font-size: 0.75rem;
        font-weight: 700;
        color: #fff;
        backdrop-filter: blur(10px);
        min-width: 65px; /* Fixed width to prevent size changes */
        width: 65px; /* Fixed width */
        text-align: center;
        box-shadow: 0 2px 8px rgba(239, 68, 68, 0.3);
        animation: pulse 2s ease-in-out infinite;
        white-space: nowrap;
        font-variant-numeric: tabular-nums; /* Use monospace numbers */
    }

    /* (reverted) no extra tight alignment rules */
    
    /* Mobile Guess Counter */
    .mobile-guess-counter {
        display: inline-block !important;
        background: linear-gradient(135deg, #374151 0%, #1f2937 100%);
        border: 2px solid #4b5563;
        border-radius: 20px;
        padding: 0.3rem 0.7rem;
        font-size: 0.75rem;
        font-weight: 700;
        color: #e5e7eb;
        min-width: 25px;
        text-align: center;
        box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
        white-space: nowrap;
    }
    
    /* Pulse animation for urgency */
    @keyframes pulse {
        0%, 100% {
            box-shadow: 0 2px 8px rgba(239, 68, 68, 0.3);
        }
        50% {
            box-shadow: 0 2px 12px rgba(239, 68, 68, 0.5);
        }
    }
    
    /* Critical time styling */
    .mobile-only-timer[style*="color: rgb(255, 0, 0)"] {
        background: linear-gradient(135deg, #dc2626 0%, #991b1b 100%);
        border-color: #fca5a5;
        animation: pulse-critical 0.5s ease-in-out infinite;
    }
    
    @keyframes pulse-critical {
        0%, 100% {
            transform: scale(1);
            box-shadow: 0 2px 8px rgba(239, 68, 68, 0.5);
        }
        50% {
            transform: scale(1.05);
            box-shadow: 0 2px 16px rgba(239, 68, 68, 0.8);
        }
    }

    .guess-input-container .form-group {
        margin-bottom: 0 !important;
    }


    .button-group {
        margin-top: 0.5rem;
        gap: 0.5rem;
    }

    .button-group button {
        margin: 0;
        padding: 0.5rem 1rem;
        font-size: 0.9rem;
    }

    /* Guess Items Mobile */
    .guess-item {
        padding: 0.375rem !important; /* Match latest-guess-item */
        margin: 0.25rem 0;
        font-size: 0.85rem !important; /* Match latest-guess-item */
        background: rgba(31, 41, 55, 0.8) !important; /* Match latest-guess-item */
        border-radius: 8px;
        flex-wrap: wrap !important; /* Allow wrapping on mobile */
        min-height: auto !important; /* Allow vertical growth */
    }

    .guess-word {
        font-size: 0.95rem;
        word-break: break-word !important;
        overflow-wrap: break-word !important;
        hyphens: auto !important;
        min-width: 0 !important;
        flex: 1 1 auto !important;
    }

    .guess-player {
        font-size: 0.8rem;
        margin: 0 0.5rem;
        min-width: auto;
    }

    .guess-score {
        font-size: 0.9rem;
        padding: 0.2rem 0.4rem;
        min-width: 70px;
    }

    .temp-symbol {
        font-size: 1rem;
    }

    /* Compact Room Code */
    .mobile-room-display {
        background: rgba(49, 23, 77, 0.6);
        padding: 0.25rem 0.5rem;
        border-radius: 5px;
        font-size: 0.9rem;
        color: #c084fc;
        border: 1px solid rgba(192, 132, 252, 0.3);
        font-weight: 600;
    }

    /* Results Screen Mobile */
    #resultsScreen {
        padding: 1rem;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        height: 100%;
    }
    
    .winner-announcement {
        font-size: 1.5rem;
        margin: 1rem 0;
    }
    
    #resultMessage {
        margin: 1rem;
        font-size: 0.9rem;
    }
    
    /* Fix target word display on mobile */
    #resultTargetWord {
        font-size: clamp(28px, 6vw, 42px) !important;
        letter-spacing: 1px !important;
        text-shadow: 
            1px 1px 0px #f59e0b,
            0 0 15px rgba(251, 191, 36, 0.5) !important;
        padding: 0 10px !important;
        max-width: calc(100vw - 40px) !important;
        width: 100% !important;
        overflow-wrap: break-word !important;
        word-wrap: break-word !important;
        hyphens: auto !important;
        box-sizing: border-box !important;
        margin: 20px auto !important;
        display: flex !important;
        justify-content: center !important;
        align-items: center !important;
    }

    /* Waiting Room Mobile */
    #waitingScreen {
        min-height: 100vh;
        height: auto !important;
        overflow: visible !important;
        padding-bottom: 100px; /* Add padding for scrolling space */
    }
    
    #waitingScreen .players-list {
        max-height: none !important; /* Remove any height constraints */
        overflow: visible !important; /* Allow expansion */
        height: auto !important;
    }
    
    .room-code h2 {
        font-size: 1.5rem;
    }
    
    /* Room code buttons for mobile */
    .room-code-buttons {
        display: flex;
        gap: 0.5rem;
        justify-content: center;
        align-items: center;
        margin-top: 0.5rem;
        width: 100%;
    }
    
    .room-code-buttons button {
        padding: 0.4rem 0.8rem !important;
        font-size: 0.85rem !important;
        background: linear-gradient(135deg, #8b5cf6 0%, #7c3aed 100%);
        color: white;
        border: none;
        border-radius: 8px;
        cursor: pointer;
        box-shadow: 0 2px 8px rgba(139, 92, 246, 0.3);
        transition: all 0.2s ease;
        display: inline-flex;
        align-items: center;
        gap: 0.3rem;
        flex: 0 0 auto; /* Don't grow or shrink, just fit content */
        width: auto; /* Auto width to fit content */
        white-space: nowrap; /* Prevent text wrapping */
    }
    
    .room-code-buttons button:active {
        transform: scale(0.95);
        box-shadow: 0 1px 4px rgba(139, 92, 246, 0.2);
    }
    
    .room-code-buttons .copy-button {
        background: linear-gradient(135deg, #10b981 0%, #059669 100%);
        box-shadow: 0 2px 8px rgba(16, 185, 129, 0.3);
    }
    
    .room-code-buttons .share-button {
        background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%);
        box-shadow: 0 2px 8px rgba(59, 130, 246, 0.3);
    }

    /* Form Elements */
    input, button {
        font-size: 16px; /* Prevents zoom on iOS */
    }

    /* Buttons Mobile */
    button {
        padding: 0.75rem 1.5rem;
        touch-action: manipulation; /* Prevents delay */
    }

    /* Login/Signup Forms */
    .form-group {
        margin-bottom: 1rem;
    }

    /* Friends List Mobile - Match leaderboard style */
    .friend-item {
        padding: 0.6rem;
        margin: 0.4rem 0;
        font-size: 0.9rem;
    }
    
    .friend-name {
        font-size: 0.9rem;
    }
    
    .unfriend-btn {
        padding: 0.2rem 0.4rem;
        font-size: 0.75rem;
        min-width: 60px;
        min-height: 28px;
    }

    /* Leaderboard Mobile */
    .leaderboard-entry {
        padding: 0.5rem;
        font-size: 0.9rem;
    }

    /* Stats Grid Mobile */
    .stats-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 0.5rem;
    }

    .stat-value {
        font-size: 1.2rem;
    }

    /* Banner Messages Mobile */
    .message {
        top: calc(70px + env(safe-area-inset-top, 0)) !important; /* Below hamburger button */
        right: -100% !important; /* Start fully off-screen on mobile */
        width: auto !important; /* Fit content */
        max-width: calc(100vw - 40px) !important;
        font-size: 0.85rem !important;
        white-space: normal !important; /* Allow text to wrap */
        padding: 0.75rem 1rem !important; /* Slightly smaller padding on mobile */
    }
    
    .message.show {
        right: 20px !important;
    }
    
    /* Adjust stacking for mobile */
    .message:nth-of-type(2) {
        top: calc(130px + env(safe-area-inset-top, 0)) !important;
    }
    
    .message:nth-of-type(3) {
        top: calc(190px + env(safe-area-inset-top, 0)) !important;
    }
    
    /* Ensure messages don't interfere with game input */
    #gameScreen .message {
        top: calc(80px + env(safe-area-inset-top, 0)) !important; /* Account for input container */
        /* Scale down popups to half size in game screen */
        transform: scale(0.5);
        transform-origin: top right;
        right: -100% !important; /* Keep standard off-screen position */
        padding: 0.5rem 0.75rem !important; /* Even smaller padding when scaled */
    }
    
    #gameScreen .message.show {
        right: 10px !important; /* Closer to edge when scaled down */
        transform: scale(0.5);
    }
    
    /* Adjust stacking gaps for scaled messages in game screen */
    #gameScreen .message:nth-of-type(2) {
        top: calc(120px + env(safe-area-inset-top, 0)) !important; /* Smaller gap */
    }
    
    #gameScreen .message:nth-of-type(3) {
        top: calc(160px + env(safe-area-inset-top, 0)) !important; /* Smaller gap */
    }
    
    #gameScreen .message:nth-of-type(4) {
        top: calc(200px + env(safe-area-inset-top, 0)) !important; /* Smaller gap */
    }
}

/* Portrait Mode - Explicit font size reset */
@media (max-width: 768px) and (orientation: portrait) {
    html {
        font-size: 16px !important; /* Reset base font size */
    }
    
    body {
        font-size: 1rem !important; /* Ensure body uses base size */
    }
}

/* Landscape Mode Adjustments */
@media (max-width: 768px) and (orientation: landscape) {
    html {
        font-size: 16px !important; /* Keep consistent base font size */
    }
    
    .game-info {
        padding: 0; /* All padding removed */
    }

    .recent-guesses-float {
        max-height: 40vh;
    }

    h1 {
        display: none; /* Hide title in landscape to save space */
    }
    
    /* Fix results screen elements bleeding through in landscape */
    #resultsScreen:not(.active) {
        visibility: hidden !important;
        pointer-events: none !important;
    }
    
    #waffleContainer,
    #celebrationWaffle {
        z-index: 10 !important; /* Lower than mobile UI elements */
    }
    
    /* Ensure inactive screens are truly hidden */
    .screen:not(.active) {
        display: none !important;
    }
}

    /* Mini tablets (iPad Mini, etc) */
@media (min-width: 769px) and (max-width: 1024px) {
    .container {
        padding: 1.5rem;
        max-width: 90%;
    }

    #gameScreen > div {
        grid-template-columns: 1fr !important;
    }

    .players-panel {
        position: static !important;
        margin-top: 1rem;
        display: block !important;
    }

    .guess-input-container {
        position: sticky !important;
        top: calc(env(safe-area-inset-top, 0) + 8px) !important;
        margin: 0 1rem;
        width: 60%;
    }
}

/* Touch-friendly adjustments */
@media (hover: none) and (pointer: coarse) {
    button:active {
        transform: scale(0.95);
    }

    .invite-btn:active,
    .hint-button:active {
        transform: scale(0.95);
    }

    /* Larger touch targets */
    button {
        min-height: 44px;
    }

    input {
        min-height: 44px;
    }
}

/* iOS Notch Support */
@supports (padding: max(0px)) {
    .container {
        padding-top: max(1rem, env(safe-area-inset-top));
        padding-left: max(1rem, env(safe-area-inset-left));
        padding-right: max(1rem, env(safe-area-inset-right));
    }

    .guess-input-container {
        padding-left: max(0.5rem, env(safe-area-inset-left));
        padding-right: max(0.5rem, env(safe-area-inset-right));
    }
}

/* Prevent overscroll bounce on iOS */
@media (max-width: 768px) {
    html {
        height: 100%;
        width: 100%;
    }

    body {
        height: 100%;
        width: 100%;
        overflow-y: auto;
        overflow-x: hidden;
        -webkit-overflow-scrolling: touch; /* Smooth scrolling on iOS */
        overscroll-behavior-y: contain; /* Prevent pull-to-refresh but allow scroll */
        touch-action: pan-y; /* Only allow vertical scrolling */
    }
}
