/* ============================================
   ORBITLINE - COMPREHENSIVE RESPONSIVE FIXES
   Mobile & Desktop Compatibility
   ============================================ */

/* ===== BASE MOBILE FIXES ===== */
@media screen and (max-width: 768px) {
    /* Prevent horizontal scrolling */
    body {
        overflow-x: hidden;
        padding: 8px;
    }
    
    .container {
        max-width: 100%;
        padding: 0;
        margin: 0;
    }
    
    /* Header fixes */
    .header {
        padding: 12px;
        margin-bottom: 12px;
    }
    
    .header h1 {
        font-size: 1.2em;
        margin-bottom: 8px;
    }
    
    /* Resources header - horizontal scroll prevention */
    .resources-header {
        display: flex;
        flex-wrap: wrap;
        gap: 8px;
        justify-content: space-between;
        padding: 12px;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
    
    .resource-item {
        min-width: calc(25% - 8px);
        text-align: center;
    }
    
    /* Tab navigation - proper wrapping */
    .tabs {
        display: flex;
        flex-wrap: wrap;
        gap: 6px;
        margin-bottom: 12px;
        justify-content: center;
        padding: 0 4px;
    }
    
    .tab {
        flex: 1 1 auto;
        min-width: 80px;
        max-width: calc(33.333% - 6px);
        padding: 8px 6px;
        font-size: 0.8em;
        text-align: center;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    
    /* Cards - mobile optimized */
    .card {
        padding: 12px;
        margin-bottom: 12px;
        border-radius: 8px;
    }
    
    .card h3 {
        font-size: 1em;
        margin-bottom: 10px;
    }
    
    /* Buttons - touch friendly */
    .button {
        padding: 10px 16px;
        font-size: 0.85em;
        min-height: 44px;
        touch-action: manipulation;
    }
    
    /* Input fields - prevent zoom on iOS */
    input[type="text"],
    input[type="number"],
    input[type="email"],
    input[type="password"],
    select,
    textarea {
        font-size: 16px !important;
        padding: 12px;
        border-radius: 8px;
    }
    
    /* Chat panel - full screen on mobile */
    .chat-panel {
        width: 95vw;
        height: 85vh;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
    }
    
    .chat-input-wrapper {
        display: flex;
        gap: 8px;
    }
    
    .chat-input {
        flex: 1;
        font-size: 16px !important;
    }
    
    .chat-send-btn,
    .chat-refresh-btn {
        min-width: 50px;
        padding: 10px;
    }
    
    /* Modal fixes */
    .modal-content {
        width: 95%;
        max-width: none;
        max-height: 85vh;
        margin: 10px;
    }
    
    .building-modal .modal-content {
        width: 95%;
        padding: 40px 20px 20px;
    }
    
    /* Floating buttons - MUST match exactly */
    .floating-refresh,
    .floating-chat {
        width: 50px;
        height: 50px;
        font-size: 18px; /* Smaller for mobile */
        bottom: 15px;
        right: 15px;
    }
    
    /* Chat button positioned ABOVE refresh button and SMALLER */
    .floating-chat {
        bottom: 75px; /* 50px button + 10px gap + 15px from bottom */
        font-size: 16px !important; /* Even smaller than refresh for mobile */
    }
    
    /* Chat panel - full screen on mobile */
    .chat-panel {
        width: 95vw;
        height: 85vh;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
    }
    
    /* Chat input area - prevent overflow */
    .chat-input-area {
        padding: 12px;
        width: 100%;
        box-sizing: border-box;
    }
    
    /* Chat input wrapper - critical fix for overflow */
    .chat-input-wrapper {
        display: flex;
        gap: 6px;
        align-items: center;
        width: 100%;
        max-width: 100%;
        box-sizing: border-box;
    }
    
    .chat-input {
        flex: 1;
        font-size: 16px !important;
        min-height: 44px;
        padding: 10px;
        box-sizing: border-box;
        max-width: calc(100% - 110px); /* Account for both buttons */
    }
    
    .chat-send-btn,
    .chat-refresh-btn {
        min-width: 44px;
        max-width: 44px;
        min-height: 44px;
        max-height: 44px;
        padding: 8px;
        font-size: 14px;
        flex-shrink: 0;
        box-sizing: border-box;
    }
    
    /* Building/Unit cards - stack properly */
    .building-card,
    .unit-card {
        margin-bottom: 12px;
    }
    
    .building-main-info {
        flex-direction: column;
        align-items: flex-start;
        gap: 8px;
    }
    
    .building-icon-main {
        font-size: 1.5em;
    }
    
    /* Settings modal */
    .settings-content {
        width: 95%;
        max-width: none;
        padding: 20px;
    }
    
    /* Footer */
    .footer-content {
        flex-direction: column;
        gap: 12px;
        text-align: center;
    }
}

/* ===== EXTRA SMALL DEVICES (480px) ===== */
@media screen and (max-width: 480px) {
    body {
        padding: 6px;
        font-size: 13px;
    }
    
    .tab {
        min-width: 70px;
        font-size: 0.75em;
        padding: 7px 5px;
    }
    
    .button {
        padding: 9px 14px;
        font-size: 0.8em;
    }
    
    .card {
        padding: 10px;
    }
    
    .chat-panel {
        width: 98vw;
        height: 90vh;
    }
}

/* ===== LANDSCAPE MODE FIXES ===== */
@media screen and (max-height: 500px) and (orientation: landscape) {
    .chat-panel {
        width: 90vw;
        height: 95vh;
    }
    
    .modal-content {
        max-height: 90vh;
    }
    
    .header {
        padding: 8px;
        margin-bottom: 8px;
    }
    
    .tabs {
        margin-bottom: 8px;
    }
    
    /* Fix chat buttons in landscape */
    .chat-input-wrapper {
        gap: 6px;
    }
    
    .chat-send-btn,
    .chat-refresh-btn {
        min-width: 45px;
        min-height: 45px;
        padding: 10px;
    }
    
    /* Chat mod dropdown - keep on screen in landscape */
    .chat-mod-dropdown {
        max-height: 150px;
        max-width: 180px;
    }
}

/* ===== PORTRAIT MODE SPECIFIC FIXES ===== */
@media screen and (max-width: 768px) and (orientation: portrait) {
    /* Chat buttons properly sized in portrait - NO OVERFLOW */
    .chat-input-area {
        padding: 10px !important;
    }
    
    .chat-input-wrapper {
        gap: 6px !important;
        padding: 0 !important;
    }
    
    .chat-input {
        min-height: 48px !important;
        padding: 12px !important;
        flex: 1 !important;
        width: auto !important;
        max-width: none !important;
    }
    
    .chat-send-btn,
    .chat-refresh-btn {
        min-width: 44px !important;
        max-width: 44px !important;
        min-height: 44px !important;
        max-height: 44px !important;
        padding: 10px !important;
        font-size: 14px !important;
        border-radius: 50%;
        flex-shrink: 0 !important;
    }
    
    /* Ensure chat panel doesn't cause horizontal scroll */
    .chat-panel {
        width: 95vw !important;
        max-width: 95vw !important;
        overflow-x: hidden !important;
    }
    
    /* Chat mod dropdown in portrait */
    .chat-mod-dropdown {
        max-width: 200px;
        font-size: 14px;
        position: fixed !important;
    }
}

/* ===== DESKTOP OPTIMIZATIONS ===== */
@media screen and (min-width: 769px) {
    /* Ensure desktop layout is clean */
    .tabs {
        display: flex;
        flex-wrap: nowrap;
        gap: 10px;
        overflow-x: auto;
    }
    
    .tab {
        flex: 0 0 auto;
        white-space: nowrap;
    }
    
    .chat-panel {
        width: 600px;
        height: 700px;
        max-width: 90vw;
        max-height: 85vh;
    }
    
    .modal-content {
        max-width: 600px;
    }
}

/* ===== PWA SAFE AREA SUPPORT ===== */
@supports (padding: env(safe-area-inset-top)) {
    .pwa-mode .container {
        padding-top: env(safe-area-inset-top);
        padding-bottom: env(safe-area-inset-bottom);
        padding-left: env(safe-area-inset-left);
        padding-right: env(safe-area-inset-right);
    }
    
    .pwa-mode .header {
        margin-top: env(safe-area-inset-top);
    }
    
    .pwa-mode .floating-refresh,
    .pwa-mode .floating-chat {
        bottom: calc(env(safe-area-inset-bottom) + 15px);
        right: calc(env(safe-area-inset-right) + 15px);
    }
    
    .pwa-mode .floating-chat {
        bottom: calc(env(safe-area-inset-bottom) + 75px);
    }
}

/* ===== PWA MODE - MATCH BROWSER VERSION EXACTLY ===== */
.pwa-mode {
    /* Remove any special PWA styling - should look identical to browser */
    background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 50%, #cbd5e1 100%) !important;
}

.pwa-mode body {
    background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 50%, #cbd5e1 100%) !important;
    color: #1e293b !important;
}

.pwa-mode .header,
.pwa-mode .card,
.pwa-mode .tab,
.pwa-mode .button {
    /* No special PWA styling - use same as browser */
}

/* Display mode detection - force same styles */
@media (display-mode: standalone) {
    body {
        background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 50%, #cbd5e1 100%) !important;
        color: #1e293b !important;
    }
    
    /* Keep mobile responsive padding */
    .container {
        padding: 8px;
    }
}

/* ===== FIX COMMON WRAPPING ISSUES ===== */
.resource-overview {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
    gap: 15px;
}

@media screen and (max-width: 768px) {
    .resource-overview {
        grid-template-columns: repeat(2, 1fr);
        gap: 10px;
        padding: 12px;
    }
}

/* ===== PREVENT TEXT OVERFLOW ===== */
.building-details-main h4,
.unit-name,
.card-title {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 100%;
}

/* ===== GRID LAYOUTS - RESPONSIVE ===== */
.buildings-grid,
.units-grid,
#unit-types-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 15px;
}

@media screen and (max-width: 768px) {
    .buildings-grid,
    .units-grid,
    #unit-types-grid {
        grid-template-columns: 1fr;
        gap: 12px;
    }
}

/* ===== SCROLLBAR STYLING ===== */
.chat-messages::-webkit-scrollbar,
.modal-content::-webkit-scrollbar {
    width: 8px;
}

.chat-messages::-webkit-scrollbar-track {
    background: rgba(0, 0, 0, 0.1);
}

.chat-messages::-webkit-scrollbar-thumb {
    background: rgba(139, 92, 246, 0.5);
    border-radius: 4px;
}

.chat-messages::-webkit-scrollbar-thumb:hover {
    background: rgba(139, 92, 246, 0.7);
}

/* ===== UTILITY CLASSES ===== */
.mobile-only {
    display: none;
}

.desktop-only {
    display: block;
}

@media screen and (max-width: 768px) {
    .mobile-only {
        display: block;
    }
    
    .desktop-only {
        display: none;
    }
}

/* ===== ACCESSIBILITY IMPROVEMENTS ===== */
.button:focus,
.tab:focus,
input:focus,
select:focus,
textarea:focus {
    outline: 2px solid #3b82f6;
    outline-offset: 2px;
}

/* ===== ANIMATION PERFORMANCE ===== */
.tab,
.button,
.card {
    will-change: transform;
    backface-visibility: hidden;
}

/* ===== CONSISTENT SPACING ===== */
@media screen and (max-width: 768px) {
    .status-item,
    .resource-item,
    .building-item {
        margin-bottom: 8px;
    }
    
    .section-divider {
        margin: 15px 0;
    }
}
