/* 
 * mobile.css 
 * Overrides for mobile devices (max-width: 768px)
 * Keeps the desktop UI completely untouched.
 */

.mobile-list-search { display: none; }

@media (max-width: 768px) {
    :root {
        --mobile-top-safe: env(safe-area-inset-top, 0px);
        --mobile-bottom-safe: env(safe-area-inset-bottom, 0px);
    }

    html, body { width: 100%; height: 100%; overflow: hidden; }
    body { overscroll-behavior: none; }
    .app-layout { display: block; width: 100%; height: 100dvh; min-height: 100dvh; overflow: hidden; }

    /* The sidebar becomes the full-screen chat list on mobile. */
    .sidebar {
        position: relative;
        inset: auto;
        display: flex !important;
        flex: 0 0 100%;
        width: 100% !important;
        min-width: 0;
        height: 100dvh;
        min-height: 0;
        transform: none;
        box-shadow: none;
        z-index: 2;
        transition: none;
    }
    .sidebar.mobile-open { transform: none; }
    .app-layout.mobile-chat-open .sidebar { display: none !important; }
    .app-layout.mobile-chat-open .chat-area { display: flex !important; width: 100%; min-width: 100%; }
    .app-layout:not(.mobile-chat-open) .chat-area { display: none !important; }

    .mobile-chat-list-header {
        display: flex;
        align-items: center;
        justify-content: space-between;
        min-height: calc(64px + var(--mobile-top-safe));
        padding: calc(var(--mobile-top-safe) + 12px) 16px 12px;
        border-bottom: 1px solid var(--border);
        background: var(--panel);
    }
    .mobile-chat-brand { display: flex; align-items: center; gap: 12px; }
    .mobile-chat-brand { font-size: 18px; }
    .mobile-chat-brand .avatar { width: 32px; height: 32px; border-radius: 9px; }
    .mobile-profile-close { width: 36px; height: 36px; border: 1px solid var(--border); border-radius: 50%; background: var(--panel-2); color: var(--text); font-size: 21px; line-height: 1; cursor: pointer; }
    .mobile-list-search { display: block; padding: 8px 16px 4px; background: var(--panel); }
    .mobile-list-search input { display: block; width: 100%; min-height: 40px; padding: 9px 12px; border: 1px solid var(--border); border-radius: 9px; outline: none; background: var(--panel-2); color: var(--text); caret-color: var(--accent); font: inherit; font-size: 13px; appearance: none; }
    .mobile-list-search input::placeholder { color: var(--muted); opacity: 1; }
    .mobile-list-search input:focus { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-glow); }
    .sidebar-header { display: none !important; }
    .desktop-dm-search { display: none; }
    .sidebar > div[style*="overflow-y"] {
        overflow-y: auto !important;
        height: auto;
        min-height: 0;
        flex: 1 1 auto !important;
        padding-bottom: calc(86px + var(--mobile-bottom-safe));
        overscroll-behavior: contain;
        -webkit-overflow-scrolling: touch;
    }
    .sidebar-section { padding-top: 16px; }
    .section-label { padding-left: 18px !important; padding-right: 18px !important; font-size: 11px; letter-spacing: 1.1px; text-transform: uppercase; }
    .sidebar-section > div[style*="padding"] { padding: 8px 16px 4px !important; }
    .sidebar-search-input { min-height: 40px; }
    .rooms-list li { min-height: 58px; padding: 10px 16px; border-bottom: 1px solid rgba(48, 54, 61, .45); touch-action: manipulation; -webkit-tap-highlight-color: transparent; }

    .chat-area { position: relative; flex: 1 1 auto; width: 100%; min-width: 0; min-height: 0; height: 100dvh; max-width: none; }
    /* The header stays put at the top of the conversation instead of being
       pushed out of view when the list scrolls or the keyboard opens. */
    #chat-view { height: 100%; min-height: 0; }
    .chat-header {
        position: sticky;
        top: 0;
        z-index: 25;
        flex: 0 0 auto;
        background: var(--panel);
        min-height: calc(64px + var(--mobile-top-safe));
        padding: calc(var(--mobile-top-safe) + 10px) 12px 10px 8px;
    }
    .chat-header > div { min-width: 0; }
    .chat-title { min-width: 0; max-width: calc(100vw - 130px); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
    .chat-subtitle { max-width: calc(100vw - 130px); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
    .mobile-chat-back { display: block; flex: 0 0 40px; border: 0; background: transparent; color: var(--text); font-size: 30px; line-height: 1; cursor: pointer; padding: 2px 8px; }
    #chat-container { min-height: 0; }
    .messages { min-height: 0; padding: 16px 12px 20px; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; }
    .message-input-wrap { flex: 0 0 auto; min-height: 56px; padding: 8px 10px calc(8px + var(--mobile-bottom-safe)); gap: 7px; }
    #msg-input { min-width: 0; max-height: 120px; line-height: 1.35; overflow-y: auto; }
    .attach-btn, .send-btn { flex: 0 0 38px; width: 38px; height: 38px; }
    .message { max-width: min(86%, 360px); min-width: 0; }
    .msg-body, .msg-body.bubble { min-width: 0; overflow-wrap: anywhere; word-break: break-word; }
    .msg-body img, .msg-body video, .msg-body iframe, .msg-body embed { max-width: 100%; height: auto; }
    .msg-body a { overflow-wrap: anywhere; }

    .mobile-profile-btn {
        display: flex;
        position: fixed;
        right: 16px;
        bottom: calc(16px + var(--mobile-bottom-safe));
        z-index: 20;
        width: 50px;
        height: 50px;
        align-items: center;
        justify-content: center;
        border: 2px solid var(--accent);
        border-radius: 50%;
        background: var(--accent);
        color: var(--bg);
        font-weight: 800;
        font-size: 18px;
        box-shadow: 0 8px 24px rgba(0,0,0,.35);
        cursor: pointer;
    }
    #app-screen[style*="display: none"] ~ .mobile-profile-btn { display: none; }
    .mobile-list-nav { position: absolute; left: 0; right: 0; bottom: 0; z-index: 5; display: grid; grid-template-columns: 1fr 1fr; gap: 8px; padding: 10px 14px calc(10px + var(--mobile-bottom-safe)); background: rgba(13, 17, 23, .96); border-top: 1px solid var(--border); }
    .mobile-list-nav-btn { min-height: 42px; border: 1px solid var(--border); border-radius: 999px; background: var(--panel-2); color: var(--muted); font: inherit; font-weight: 700; cursor: pointer; }
    .mobile-list-nav-btn.active { border-color: var(--accent); background: var(--accent); color: var(--bg); }
    .mobile-profile-backdrop[hidden], .mobile-profile-sheet[hidden] { display: none; }
    .mobile-profile-backdrop { display: block; position: fixed; inset: 0; z-index: 30; background: rgba(0,0,0,.58); backdrop-filter: blur(3px); }
    .mobile-profile-sheet { display: block; position: fixed; left: 0; right: 0; bottom: 0; z-index: 31; max-height: min(88dvh, 680px); overflow-y: auto; padding: 10px 18px calc(14px + var(--mobile-bottom-safe)); background: var(--panel); border: 1px solid var(--border); border-bottom: 0; border-radius: 22px 22px 0 0; box-shadow: 0 -16px 50px rgba(0,0,0,.4); animation: mobile-sheet-in .22s ease-out; overscroll-behavior: contain; }
    .mobile-sheet-handle { width: 42px; height: 4px; margin: 0 auto 16px; border-radius: 99px; background: var(--muted); opacity: .6; }
    .mobile-profile-heading { display: flex; align-items: center; gap: 12px; padding: 4px 0 16px; border-bottom: 1px solid var(--border); }
    .mobile-profile-heading .avatar { width: 46px; height: 46px; }
    .mobile-profile-heading h2 { font-size: 17px; }
    .mobile-profile-heading p { margin-top: 3px; color: var(--muted); font-size: 12px; }
    .mobile-profile-close { margin-left: auto; font-size: 25px; }
    .mobile-profile-menu-item { display: flex; align-items: center; gap: 14px; width: 100%; min-height: 48px; padding: 8px 2px; border: 0; background: transparent; color: var(--text); font: inherit; text-align: left; text-decoration: none; cursor: pointer; }
    .mobile-profile-menu-item:first-letter { color: var(--accent); }
    .mobile-profile-logout { margin-top: 4px; border-top: 1px solid var(--border); color: var(--danger); }
    @keyframes mobile-sheet-in { from { transform: translateY(100%); } to { transform: translateY(0); } }

    .mobile-menu-btn, .mobile-overlay { display: none !important; }
    #profile-view { padding: 16px !important; }
    .profile-summary { flex-direction: column; align-items: center; text-align: center; }
    .profile-summary-avatar { margin-right: 0; margin-bottom: 16px; }
}

@media (prefers-reduced-motion: reduce) and (max-width: 768px) {
    *, *::before, *::after { scroll-behavior: auto !important; transition-duration: 0.01ms !important; animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; }
    .mobile-profile-sheet { animation: none; }
}

@media (min-width: 769px) {
    .mobile-chat-list-header, .mobile-chat-back, .mobile-profile-btn, .mobile-profile-backdrop, .mobile-profile-sheet, .mobile-list-nav { display: none !important; }
}

/* Ensure the button is hidden on desktop (min-width: 769px) */
@media (min-width: 769px) {
    .mobile-menu-btn {
        display: none !important;
    }
}

/* Optional Overlay background when sidebar is open on mobile */
.mobile-overlay {
    display: none;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.5);
    z-index: 9998;
    backdrop-filter: blur(2px);
}

.mobile-overlay.active {
    display: block;
}
