/**
 * PIONEER - Community Mobile & Tablet Fixes v4.0
 * Comprehensive fixes for mobile phones and iPads
 * Resolves conflicts between CSS and JavaScript
 */

/* ============================================================================
   MOBILE PHONES (< 600px) - CRITICAL FIXES
   ============================================================================ */

@media (max-width: 600px) {
    /* Reset any conflicting styles from force-discord-layout.js */
    .dc-layout.discord-mobile-layout {
        display: flex !important;
        flex-direction: column !important;
    }

    /* Layout container - proper height calculation */
    .dc-layout {
        display: flex !important;
        flex-direction: column !important;
        width: 100vw !important;
        max-width: 100vw !important;
        min-height: calc(100vh - 60px) !important;
        height: auto !important;
        overflow: hidden !important;
        position: relative !important;
        box-sizing: border-box !important;
    }

    /* Sidebar - Full width overlay style */
    .dc-sidebar {
        position: fixed !important;
        left: 0 !important;
        top: 60px !important;
        bottom: 60px !important;
        width: 100vw !important;
        max-width: 100vw !important;
        background: rgba(15, 20, 25, 0.98) !important;
        backdrop-filter: blur(10px) !important;
        -webkit-backdrop-filter: blur(10px) !important;
        z-index: 1000 !important;
        overflow-y: auto !important;
        overflow-x: hidden !important;
        -webkit-overflow-scrolling: touch;
        padding: 1rem !important;
        padding-bottom: calc(1rem + env(safe-area-inset-bottom, 20px)) !important;
        transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1) !important;
        transform: translateX(0) !important;
        box-sizing: border-box !important;
    }

    /* Override force-discord-layout.js inline styles */
    .dc-sidebar[style*="width: 56px"],
    .dc-sidebar.discord-mobile-sidebar {
        width: 100vw !important;
        padding: 1rem !important;
    }

    /* Hide sidebar states */
    .dc-sidebar.hidden,
    .dc-sidebar.mobile-hidden {
        transform: translateX(-100%) !important;
        pointer-events: none !important;
    }

    /* Show sidebar when explicitly opened */
    .dc-sidebar.mobile-open {
        transform: translateX(0) !important;
        pointer-events: auto !important;
    }

    /* Mobile overlay for sidebar backdrop */
    .dc-mobile-overlay {
        position: fixed !important;
        top: 60px !important;
        left: 0 !important;
        right: 0 !important;
        bottom: 60px !important;
        background: rgba(0, 0, 0, 0.7) !important;
        z-index: 999 !important;
        opacity: 0 !important;
        pointer-events: none !important;
        transition: opacity 0.3s ease !important;
    }

    .dc-mobile-overlay.active {
        opacity: 1 !important;
        pointer-events: auto !important;
    }

    /* Sidebar content - show full text and labels */
    .dc-sidebar-header {
        display: block !important;
        font-size: 1.5rem !important;
        font-weight: 800 !important;
        color: #c8a03c !important;
        text-align: left !important;
        margin-bottom: 1.5rem !important;
        padding-bottom: 1rem !important;
        border-bottom: 1px solid rgba(255, 255, 255, 0.1) !important;
    }

    .dc-section-label {
        display: flex !important;
        align-items: center !important;
        justify-content: space-between !important;
        font-size: 0.75rem !important;
        color: rgba(255, 255, 255, 0.5) !important;
        font-weight: 700 !important;
        text-transform: uppercase !important;
        margin: 1.5rem 0 0.75rem !important;
        letter-spacing: 0.5px !important;
    }

    .dc-channel-name,
    .dc-dm-item-name {
        display: block !important;
        color: rgba(255, 255, 255, 0.9) !important;
        font-size: 1rem !important;
        font-weight: 500 !important;
    }

    /* Channel and DM items - full width touch targets */
    .dc-channel-item,
    .dc-dm-item {
        width: 100% !important;
        min-height: 52px !important;
        height: auto !important;
        padding: 0.75rem 1rem !important;
        display: flex !important;
        align-items: center !important;
        gap: 0.75rem !important;
        border-radius: 8px !important;
        background: transparent !important;
        margin: 0 0 0.5rem 0 !important;
        cursor: pointer;
        transition: background 0.2s ease !important;
        box-sizing: border-box !important;
    }

    .dc-channel-item:active,
    .dc-dm-item:active {
        background: rgba(255, 255, 255, 0.05) !important;
    }

    .dc-channel-item.active,
    .dc-dm-item.active {
        background: rgba(200, 160, 60, 0.15) !important;
    }

    /* Show channel hash */
    .dc-channel-hash {
        display: inline-block !important;
        font-size: 1.2rem !important;
        color: rgba(255, 255, 255, 0.6) !important;
        flex-shrink: 0 !important;
    }

    /* Hide channel initial circles on mobile */
    .dc-channel-initial {
        display: none !important;
    }

    /* Unread badges */
    .dc-channel-badge {
        margin-left: auto !important;
        min-width: 20px !important;
        height: 20px !important;
        border-radius: 10px !important;
        background: #ff3b30 !important;
        color: white !important;
        font-size: 11px !important;
        font-weight: 700 !important;
        padding: 0 6px !important;
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
        flex-shrink: 0 !important;
    }

    /* Sidebar buttons */
    .dc-add-channel-btn,
    .dc-new-dm-btn {
        display: inline-flex !important;
        align-items: center !important;
        justify-content: center !important;
        padding: 0.5rem 1rem !important;
        border-radius: 6px !important;
        background: rgba(200, 160, 60, 0.2) !important;
        color: #c8a03c !important;
        border: none !important;
        font-weight: 600 !important;
        font-size: 0.9rem !important;
        margin-top: 0.5rem !important;
        width: auto !important;
        min-height: 44px !important;
    }

    .dc-sidebar-footer {
        display: block !important;
        margin-top: 2rem !important;
        padding-top: 1rem !important;
        border-top: 1px solid rgba(255, 255, 255, 0.1) !important;
    }

    /* Main content area */
    .dc-main {
        position: fixed !important;
        left: 0 !important;
        top: 60px !important;
        bottom: 60px !important;
        width: 100vw !important;
        max-width: 100vw !important;
        display: flex !important;
        flex-direction: column !important;
        background: #0f1419 !important;
        z-index: 100 !important;
        box-sizing: border-box !important;
    }

    /* Override force-discord-layout margin */
    .dc-main[style*="margin-left"] {
        margin-left: 0 !important;
        width: 100vw !important;
    }

    /* Main header with back button */
    .dc-main-header {
        display: flex !important;
        align-items: center !important;
        padding: 1rem !important;
        background: rgba(10, 22, 40, 0.98) !important;
        backdrop-filter: blur(10px) !important;
        -webkit-backdrop-filter: blur(10px) !important;
        border-bottom: 1px solid rgba(255, 255, 255, 0.15) !important;
        min-height: 56px !important;
        max-height: 56px !important;
        position: sticky !important;
        top: 0 !important;
        z-index: 150 !important;
        gap: 0.75rem !important;
        flex-shrink: 0 !important;
        box-sizing: border-box !important;
    }

    /* Hamburger menu button */
    .dc-hamburger-btn {
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
        width: 44px !important;
        height: 44px !important;
        border: none !important;
        background: transparent !important;
        cursor: pointer !important;
        padding: 8px !important;
        flex-shrink: 0 !important;
        z-index: 200 !important;
    }

    .dc-hamburger-icon {
        display: flex !important;
        flex-direction: column !important;
        gap: 4px !important;
        width: 24px !important;
        height: 18px !important;
    }

    .dc-hamburger-icon span {
        display: block !important;
        width: 100% !important;
        height: 2px !important;
        background: rgba(255, 255, 255, 0.8) !important;
        transition: all 0.3s ease !important;
        border-radius: 2px !important;
    }

    .dc-hamburger-btn.open .dc-hamburger-icon span:nth-child(1) {
        transform: translateY(6px) rotate(45deg) !important;
    }

    .dc-hamburger-btn.open .dc-hamburger-icon span:nth-child(2) {
        opacity: 0 !important;
    }

    .dc-hamburger-btn.open .dc-hamburger-icon span:nth-child(3) {
        transform: translateY(-6px) rotate(-45deg) !important;
    }

    /* Back buttons */
    .dc-dm-back-btn,
    .dc-channel-back-btn {
        display: flex !important;
        align-items: center !important;
        gap: 0.5rem !important;
        background: transparent !important;
        border: none !important;
        color: rgba(255, 255, 255, 0.8) !important;
        font-size: 0.9rem !important;
        font-weight: 500 !important;
        padding: 0.5rem !important;
        cursor: pointer;
        flex-shrink: 0 !important;
        min-height: 44px !important;
    }

    .dc-main-title {
        flex: 1 !important;
        font-size: 1rem !important;
        font-weight: 700 !important;
        color: white !important;
        white-space: nowrap !important;
        overflow: hidden !important;
        text-overflow: ellipsis !important;
    }

    /* Hide members sidebar on mobile */
    .dc-right,
    .dc-members-sidebar {
        display: none !important;
    }

    /* Feed area - proper scrolling */
    .dc-feed,
    .dm-messages {
        flex: 1 !important;
        overflow-y: auto !important;
        overflow-x: hidden !important;
        -webkit-overflow-scrolling: touch;
        padding: 1rem 0.75rem !important;
        padding-bottom: 180px !important;
        background: #0f1419 !important;
        box-sizing: border-box !important;
    }

    /* Post cards */
    .post-card,
    .dm-msg {
        background: rgba(255, 255, 255, 0.03) !important;
        border-radius: 12px !important;
        padding: 1rem !important;
        margin-bottom: 0.75rem !important;
        border: none !important;
        box-sizing: border-box !important;
    }

    .post-text,
    .dm-msg-body {
        font-size: 15px !important;
        line-height: 1.5 !important;
        color: rgba(255, 255, 255, 0.9) !important;
        word-wrap: break-word !important;
        overflow-wrap: break-word !important;
    }

    /* COMPOSER - Fixed at bottom */
    .dc-inline-composer {
        position: fixed !important;
        bottom: 60px !important;
        left: 0 !important;
        right: 0 !important;
        width: 100vw !important;
        max-width: 100vw !important;
        background: rgba(10, 22, 40, 0.99) !important;
        backdrop-filter: blur(10px) !important;
        -webkit-backdrop-filter: blur(10px) !important;
        border-top: 1px solid rgba(255, 255, 255, 0.15) !important;
        padding: 0.75rem !important;
        padding-bottom: calc(0.75rem + env(safe-area-inset-bottom, 10px)) !important;
        z-index: 500 !important;
        box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.3) !important;
        box-sizing: border-box !important;
    }

    /* Composer row layout */
    .dc-ic-row {
        display: flex !important;
        flex-direction: row !important;
        gap: 0.5rem !important;
        align-items: flex-end !important;
        flex-wrap: nowrap !important;
    }

    /* Tool buttons */
    .dc-ic-tool-btn {
        width: 36px !important;
        height: 36px !important;
        min-width: 36px !important;
        border-radius: 50% !important;
        background: transparent !important;
        border: none !important;
        color: rgba(255, 255, 255, 0.6) !important;
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
        flex-shrink: 0 !important;
        padding: 0 !important;
    }

    .dc-ic-tool-btn:active {
        background: rgba(255, 255, 255, 0.1) !important;
    }

    /* Textarea */
    .dc-ic-textarea,
    #dc-post-input,
    #dm-input-main {
        flex: 1 !important;
        min-height: 40px !important;
        max-height: 120px !important;
        padding: 0.65rem 1rem !important;
        font-size: 16px !important;
        border-radius: 20px !important;
        background: rgba(255, 255, 255, 0.08) !important;
        border: 1px solid rgba(255, 255, 255, 0.12) !important;
        color: white !important;
        resize: none !important;
        overflow-y: auto !important;
        box-sizing: border-box !important;
    }

    /* Send button */
    .dc-ic-post-btn {
        height: 40px !important;
        min-width: 70px !important;
        padding: 0 1rem !important;
        border-radius: 20px !important;
        background: #c8a03c !important;
        color: #0f1419 !important;
        font-weight: 700 !important;
        font-size: 0.9rem !important;
        flex-shrink: 0 !important;
        border: none !important;
    }
}

/* ============================================================================
   TABLETS (601px - 1024px) - iPad Fixes
   ============================================================================ */

@media (min-width: 601px) and (max-width: 1024px) {
    /* Layout - proper flex container */
    .dc-layout {
        display: flex !important;
        flex-direction: row !important;
        width: 100vw !important;
        max-width: 100vw !important;
        min-height: calc(100vh - 60px) !important;
        overflow: hidden !important;
        position: relative !important;
        box-sizing: border-box !important;
    }

    /* Sidebar - fixed width */
    .dc-sidebar {
        width: 240px !important;
        min-width: 240px !important;
        max-width: 240px !important;
        flex-shrink: 0 !important;
        background: rgba(10, 22, 40, 0.98) !important;
        border-right: 1px solid rgba(255, 255, 255, 0.15) !important;
        overflow-y: auto !important;
        overflow-x: hidden !important;
        padding: 1rem !important;
        padding-bottom: calc(1rem + env(safe-area-inset-bottom, 20px)) !important;
        position: relative !important;
        box-sizing: border-box !important;
    }

    /* Sidebar content */
    .dc-section-label {
        display: flex !important;
        align-items: center !important;
        justify-content: space-between !important;
        font-size: 0.75rem !important;
        color: rgba(255, 255, 255, 0.5) !important;
        font-weight: 700 !important;
        margin: 1rem 0 0.5rem !important;
    }

    .dc-channel-item,
    .dc-dm-item {
        display: flex !important;
        align-items: center !important;
        gap: 0.75rem !important;
        padding: 0.75rem !important;
        border-radius: 8px !important;
        margin-bottom: 0.25rem !important;
        cursor: pointer;
        transition: all 0.2s;
        min-height: 48px !important;
        box-sizing: border-box !important;
    }

    .dc-channel-item.active,
    .dc-dm-item.active {
        background: rgba(200, 160, 60, 0.15) !important;
    }

    .dc-channel-name,
    .dc-dm-item-name {
        display: block !important;
        color: rgba(255, 255, 255, 0.9) !important;
        font-size: 0.95rem !important;
    }

    /* Main content - flex to fill remaining space */
    .dc-main {
        flex: 1 !important;
        min-width: 0 !important;
        display: flex !important;
        flex-direction: column !important;
        overflow: hidden !important;
        box-sizing: border-box !important;
    }

    /* Main header */
    .dc-main-header {
        flex-shrink: 0 !important;
        min-height: 56px !important;
        padding: 1rem !important;
        border-bottom: 1px solid rgba(255, 255, 255, 0.1) !important;
        box-sizing: border-box !important;
    }

    /* Feed area */
    .dc-feed,
    .dm-messages {
        flex: 1 !important;
        overflow-y: auto !important;
        overflow-x: hidden !important;
        padding: 1.5rem !important;
        padding-bottom: 140px !important;
        box-sizing: border-box !important;
    }

    /* Composer - fixed at bottom, accounting for sidebar */
    .dc-inline-composer {
        position: fixed !important;
        bottom: 60px !important;
        left: 240px !important;
        right: 0 !important;
        width: calc(100vw - 240px) !important;
        max-width: calc(100vw - 240px) !important;
        background: rgba(10, 22, 40, 0.98) !important;
        backdrop-filter: blur(10px) !important;
        -webkit-backdrop-filter: blur(10px) !important;
        border-top: 1px solid rgba(255, 255, 255, 0.1) !important;
        padding: 1rem !important;
        padding-bottom: calc(1rem + env(safe-area-inset-bottom, 20px)) !important;
        z-index: 500 !important;
        box-sizing: border-box !important;
    }

    /* Composer layout */
    .dc-ic-row {
        display: flex !important;
        flex-direction: row !important;
        gap: 0.75rem !important;
        align-items: flex-end !important;
    }

    .dc-ic-textarea,
    #dc-post-input,
    #dm-input-main {
        flex: 1 !important;
        min-height: 48px !important;
        padding: 1rem !important;
        font-size: 16px !important;
        box-sizing: border-box !important;
    }

    /* Hide members sidebar on tablets too */
    .dc-right,
    .dc-members-sidebar {
        display: none !important;
    }
}

/* ============================================================================
   Common fixes for all screen sizes
   ============================================================================ */

/* Prevent text selection during touch interactions */
.dc-channel-item,
.dc-dm-item,
.dc-ic-tool-btn,
.dc-hamburger-btn {
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
    user-select: none;
}

/* Smooth scrolling */
.dc-feed,
.dm-messages,
.dc-sidebar {
    scroll-behavior: smooth;
}

/* Prevent horizontal scroll */
.dc-layout,
.dc-main,
.dc-feed,
.dm-messages {
    overflow-x: hidden !important;
}

/* Safe area support for notched devices */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
    .dc-sidebar,
    .dc-inline-composer {
        padding-bottom: calc(1rem + env(safe-area-inset-bottom)) !important;
    }
}
