/* ========== LAYOUT REGRESSION FIX (CORRECTED SELECTORS) ========== */

/* 1. Modal Overlay & Container */
/* The card itself must be the flex container and NOT scroll */
#modal-community .community-card {
    display: flex !important;
    flex-direction: column !important;
    height: 90vh !important;
    max-height: 90vh !important;
    padding: 0 !important;
    overflow: hidden !important;
    /* CRITICAL: Prevents the card (and headers) from scrolling */
    position: relative !important;
}

/* 2. Fixed Header Elements */
/* These are direct children of .community-card */
#modal-community .community-header,
#modal-community .community-user-bar,
#modal-community .community-tabs {
    flex-shrink: 0 !important;
    /* Prevent shrinking */
    flex-grow: 0 !important;
    /* Prevent growing */
    width: 100% !important;
    background: white !important;
    z-index: 50;
    position: relative;
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.05) !important;
}

#modal-community .community-header {
    padding: 15px 20px;
    border-bottom: 1px solid rgba(0, 0, 0, 0.05);
    /* Visual separator */
}

/* 3. Community Content Wrapper */
/* This fills the remaining space in the card */
.community-content {
    display: none !important;
    flex: 1 1 auto !important;
    /* Grow to fill space */
    flex-direction: column !important;
    overflow: hidden !important;
    /* Prevent this wrapper from scrolling */
    position: relative !important;
    height: 100% !important;
    /* Occupy full available height */
    min-height: 0 !important;
    /* Crucial for nested scrolling */
}

.community-content.active {
    display: flex !important;
}

/* 4. Scrollable Lists */
/* This is the ONLY element that should scroll */
.chat-messages-list,
.mural-posts-list,
#challenges-list {
    flex: 1 1 auto !important;
    overflow-y: auto !important;
    overflow-x: hidden;
    padding: 15px;
    padding-bottom: 90px !important;
    /* Space for the absolute input */
    scroll-behavior: smooth;
    -webkit-overflow-scrolling: touch;
    /* Momentum scrolling */
    width: 100%;
    height: 100% !important;
}

/* 5. Fixed Input Area */
/* Positioned slightly above the bottom of the content area */
#community-mural .input-area-container,
#community-chat .input-area-container {
    position: absolute !important;
    bottom: 20px;
    left: 20px;
    right: 20px;
    width: auto !important;
    margin: 0 !important;
    z-index: 100 !important;
    background: rgba(255, 255, 255, 0.95) !important;
    -webkit-backdrop-filter: blur(20px) !important;
    backdrop-filter: blur(20px) !important;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2) !important;
    border-radius: 50px;
    display: flex !important;
    align-items: center !important;
    padding: 8px 12px !important;
    flex-shrink: 0 !important;
}

/* Fix input field expansion */
#chat-input,
#mural-input {
    flex: 1 !important;
}


/* ========== MOBILE OPTIMIZATIONS (REMOVE "POP-UP" FEEL) ========== */
@media (max-width: 768px) {

    /* 1. Make Community Modal Full Screen */
    /* Remove the card look and make it fill the screen */
    #modal-community .community-card {
        width: 100% !important;
        height: 100% !important;
        max-height: 100% !important;
        max-width: 100% !important;
        margin: 0 !important;
        border-radius: 0 !important;
        border: none !important;
        box-shadow: none !important;
        background: #f4f4f9 !important;
        /* Match body bg */
    }

    /* 2. Remove Overlay Padding/Margins */
    #modal-community.modal-overlay {
        padding: 0 !important;
        align-items: flex-start !important;
        /* Start from top */
    }

    /* 3. Adjust Header Padding */
    #modal-community .community-header {
        padding: 12px 15px !important;
    }

    #modal-community .community-tabs {
        padding: 0 5px !important;
    }

    #modal-community .community-tab {
        padding: 12px 10px !important;
        font-size: 0.9rem !important;
    }

    /* 4. Content Area Tweaks */
    .chat-messages-list,
    .mural-posts-list {
        padding: 10px !important;
        padding-bottom: 80px !important;
        /* Keep space for input */
    }

    /* 5. Streamline Input Area */
    #community-mural .input-area-container,
    #community-chat .input-area-container {
        left: 10px !important;
        right: 10px !important;
        bottom: 10px !important;
        width: auto !important;
        border-radius: 30px !important;
    }

    /* 6. Main Screens: Full Screen Pages */
    #screen-setup,
    #screen-analysis,
    #screen-wean,
    #screen-freedom {
        padding: 20px !important;
        width: 100% !important;
        max-width: 100% !important;
        margin-left: 0 !important;
        margin-right: 0 !important;
        border-radius: 0 !important;
        box-shadow: none !important;
        border: none !important;
        min-height: 100vh !important;
        display: flex;
        flex-direction: column;
        background: transparent !important;
        /* Matches body bg */
    }

    /* Target the Settings Modal specifically to match */
    #modal-settings .modal-card {
        width: 100% !important;
        height: 100% !important;
        max-height: 100% !important;
        border-radius: 0 !important;
        margin: 0 !important;
        display: flex;
        flex-direction: column;
    }

    #modal-settings.modal-overlay {
        padding: 0 !important;
        align-items: flex-start !important;
    }
}

/* ========== DARK MODE FIXES (CHAT & GENERAL) ========== */
[data-theme="dark"] .community-close-btn {
    color: white !important;
    background: rgba(255, 255, 255, 0.1) !important;
    border: 1px solid rgba(255, 255, 255, 0.2) !important;
}

[data-theme="dark"] #community-user-name {
    color: #f1f2f6 !important;
    /* Light text for dark mode */
}

/* Ensure header background matches dark theme */
[data-theme="dark"] .modal-community-header,
[data-theme="dark"] #modal-community .community-header,
[data-theme="dark"] #modal-community .community-user-bar,
[data-theme="dark"] #modal-community .community-tabs {
    background: #1e272e !important;
    /* Dark background */
    color: white !important;
    border-bottom-color: rgba(255, 255, 255, 0.1) !important;
}

/* Tabs in Dark Mode */
[data-theme="dark"] .community-tab {
    color: rgba(255, 255, 255, 0.6) !important;
}

[data-theme="dark"] .community-tab.active {
    color: var(--primary) !important;
    border-bottom-color: var(--primary) !important;
}

/* Main Card Background in Dark Mode */
[data-theme="dark"] #modal-community .community-card {
    background: #1e272e !important;
}

/* Fix Input Background in Dark Mode */
[data-theme="dark"] #community-mural .input-area-container,
[data-theme="dark"] #community-chat .input-area-container {
    background: rgba(30, 39, 46, 0.95) !important;
    border: 1px solid rgba(255, 255, 255, 0.1) !important;
}

[data-theme="dark"] #chat-input,
[data-theme="dark"] #mural-input {
    background: rgba(0, 0, 0, 0.2) !important;
    color: white !important;
    border: 1px solid rgba(255, 255, 255, 0.1) !important;
}

[data-theme="dark"] #chat-input:focus,
[data-theme="dark"] #mural-input:focus {
    background: rgba(0, 0, 0, 0.4) !important;
    border-color: var(--primary) !important;
}

/* ========== CRITICAL FIX: HIDDEN STATE VISIBILITY ========== */
/* 
   The above full-screen rules (display: flex !important) were overriding
   the .hidden utility class. This restores the correct behavior.
*/
.screen.hidden,
.card.hidden,
.hidden {
    display: none !important;
    visibility: hidden !important;
    opacity: 0 !important;
    pointer-events: none !important;
}

/* ========== FLATTEN DESMAME SCREEN (REMOVE "WINDOWS") ========== */
/* User requested to remove the "card" look from Graph and Reward on this screen */
#screen-wean .evo-plan-card,
#screen-wean .reward-card {
    background: transparent !important;
    box-shadow: none !important;
    border: none !important;
    border-radius: 0 !important;
    padding-left: 0 !important;
    padding-right: 0 !important;
    padding-bottom: 0 !important;
    margin-top: 10px !important;
    /* Reduced from 30px */
    margin-bottom: 10px !important;
    /* Reduced from 30px */
    min-height: 0 !important;
    /* EXPLICIT RESET to prevent stretching */
    flex: none !important;
    /* Prevent stretching */
}

/* Add a subtle divider instead of a box */
#screen-wean .evo-plan-card::after {
    content: "";
    display: block;
    width: 80%;
    height: 1px;
    background: rgba(0, 0, 0, 0.1);
    margin: 10px auto 0 auto;
    /* Reduced from 30px */
}

/* Dark mode divider */
[data-theme="dark"] #screen-wean .evo-plan-card::after {
    background: rgba(255, 255, 255, 0.1);
}

/* ========== FIX POPUP MODALS (Prevent Full Screen on Mobile) ========== */
/* The global .card rule above forces full screen. We must override it here for specific "popup" modals. */
@media (max-width: 768px) {

    #modal-analysis-confirm.modal-overlay,
    #modal-panic.modal-overlay,
    #modal-health.modal-overlay,
    #modal-meta.modal-overlay,
    #modal-sleep-edit.modal-overlay {
        align-items: center !important;
        justify-content: center !important;
        padding: 20px !important;
        background: rgba(0, 0, 0, 0.8) !important;
        /* Ensure dark backdrop */
    }

    #modal-analysis-confirm .card,
    #modal-panic .card,
    #modal-health .card,
    #modal-meta .card,
    #modal-sleep-edit .card {
        width: 90% !important;
        max-width: 320px !important;
        height: auto !important;
        min-height: 0 !important;
        /* Override 100vh */
        margin: auto !important;
        border-radius: 20px !important;
        border: 1px solid rgba(255, 255, 255, 0.1) !important;
        box-shadow: 0 10px 40px rgba(0, 0, 0, 0.4) !important;
        display: block !important;
        /* Reset flex if needed */
    }

    [data-theme="dark"] #modal-analysis-confirm .card,
    [data-theme="dark"] #modal-panic .card,
    [data-theme="dark"] #modal-health .card,
    [data-theme="dark"] #modal-meta .card,
    [data-theme="dark"] #modal-sleep-edit .card {
        /* background: #1e272e !important; Removed for glass effect */
    }
}