/* StellarChat Mobile Layout Reset.
   One mobile layer, loaded last. No transform-based navbar hacks. */

:root {
    --mobile-top-gap: 24px;
    --mobile-screen-x: 14px;
    --mobile-bottom-gap: 16px;
    --mobile-nav-base-height: 74px;
    --mobile-bottom-safe-buffer: 0px;
    --mobile-composer-base-lift: 14px;
    --mobile-nav-height: calc(var(--mobile-nav-base-height) + var(--mobile-bottom-safe-buffer));
    --mobile-composer-bottom-buffer: var(--mobile-bottom-safe-buffer);
    --mobile-composer-height: 64px;
    --mobile-composer-textarea-height: 40px;
    --card: var(--surface);
    --input-bg: var(--surface-soft);
}

html.navbar-rise-enabled {
    --mobile-bottom-safe-buffer: 28px;
}

@media (max-width: 900px) {
    html,
    body {
        width: 100%;
        min-height: 100dvh;
        overflow-x: hidden;
    }

    body {
        background: var(--app-gradient);
    }

    html.is-mobile-app .auth-view,
    html.is-mobile-app .app-shell {
        min-height: 100dvh !important;
        padding-top: var(--mobile-top-gap) !important;
    }

    html.is-mobile-app .app-shell {
        overflow: hidden !important;
    }

    html.is-mobile-app .app-screen {
        min-height: auto !important;
        height: calc(100dvh - var(--mobile-top-gap)) !important;
        padding: 12px var(--mobile-screen-x) calc(var(--mobile-nav-height) + var(--mobile-bottom-gap)) !important;
        overflow-y: auto !important;
        overflow-x: hidden !important;
        scrollbar-width: thin;
    }

    html.is-mobile-app .screen-header {
        padding-top: 4px !important;
    }

    html.is-mobile-app .screen-header h1 {
        font-size: 26px !important;
    }

    /* Floored app navbar. It is no longer a floating island. */
    html.is-mobile-app .bottom-nav {
        position: fixed !important;
        left: 0 !important;
        right: 0 !important;
        bottom: 0 !important;
        width: 100% !important;
        height: var(--mobile-nav-height) !important;
        display: grid !important;
        grid-template-columns: repeat(5, minmax(0, 1fr)) !important;
        margin: 0 !important;
        padding: 0 !important;
        border-radius: 0 !important;
        border-left: 0 !important;
        border-right: 0 !important;
        border-bottom: 0 !important;
        border-top: 1px solid var(--border) !important;
        background: var(--surface) !important;
        box-shadow: 0 -10px 28px rgba(15, 23, 42, 0.14) !important;
        backdrop-filter: none !important;
        transform: none !important;
        z-index: 120 !important;
        overflow: hidden !important;
    }

    html.is-mobile-app .nav-item {
        min-width: 0 !important;
        margin: 0 !important;
        padding: 13px 2px calc(6px + var(--mobile-bottom-safe-buffer)) !important;
        border-radius: 0 !important;
        box-shadow: none !important;
        display: grid !important;
        align-content: start !important;
        justify-items: center !important;
        gap: 3px !important;
    }

    html.is-mobile-app .nav-item.is-active {
        background: var(--surface-soft) !important;
    }

    html.is-mobile-app .nav-icon {
        font-size: 19px !important;
    }

    html.is-mobile-app .nav-item span:last-child {
        font-size: 10.5px !important;
    }

    /* Chat screen is its own layout: no app navbar, composer owns bottom. */
    html.is-mobile-app.is-chat-thread-open .bottom-nav {
        display: none !important;
    }

    html.is-mobile-app.is-chat-thread-open .app-screen[data-screen="chats"] {
        padding: 8px var(--mobile-screen-x) 0 !important;
        overflow: hidden !important;
    }

    html.is-mobile-app.is-chat-thread-open #chatsListView {
        display: none !important;
    }

    html.is-mobile-app.is-chat-thread-open #chatThreadView {
        height: calc(100dvh - var(--mobile-top-gap)) !important;
        min-height: 0 !important;
        display: grid !important;
        grid-template-rows: auto auto minmax(0, 1fr) !important;
        overflow: hidden !important;
        padding: 0 !important;
        background: transparent !important;
    }

    html.is-mobile-app.is-chat-thread-open .chat-header {
        grid-template-columns: auto minmax(0, 1fr) repeat(5, auto) !important;
        gap: 7px !important;
        padding: 4px 0 8px !important;
    }

    html.is-mobile-app.is-chat-thread-open .chat-header h1 {
        font-size: 18px !important;
    }

    html.is-mobile-app.is-chat-thread-open .chat-header-action,
    html.is-mobile-app.is-chat-thread-open .chat-back-button {
        width: 36px !important;
        height: 36px !important;
    }

    html.is-mobile-app.is-chat-thread-open .messages-list {
        min-height: 0 !important;
        overflow-y: auto !important;
        padding: 8px 0 calc(var(--mobile-composer-height) + var(--mobile-composer-base-lift) + var(--mobile-composer-bottom-buffer) + 18px) !important;
        scroll-padding-bottom: calc(var(--mobile-composer-height) + var(--mobile-composer-base-lift) + var(--mobile-composer-bottom-buffer) + 18px) !important;
        margin: 0 !important;
    }

    html.is-mobile-app.is-chat-thread-open .message-composer {
        position: fixed !important;
        left: var(--mobile-screen-x) !important;
        right: var(--mobile-screen-x) !important;
        bottom: calc(var(--mobile-composer-base-lift) + var(--mobile-composer-bottom-buffer)) !important;
        transform: none !important;
        width: auto !important;
        min-height: 0 !important;
        grid-template-columns: 38px minmax(0, 1fr) 38px 40px !important;
        gap: 7px !important;
        align-items: center !important;
        margin: 0 !important;
        padding: 7px 0 8px !important;
        border: 0 !important;
        border-radius: 0 !important;
        background: transparent !important;
        box-shadow: none !important;
        z-index: 280 !important;
    }

    html.is-mobile-app.is-chat-thread-open .message-composer textarea {
        height: var(--mobile-composer-textarea-height, 40px) !important;
        min-height: 40px !important;
        max-height: min(116px, 30dvh) !important;
        padding: 10px 15px 11px !important;
        line-height: 1.35 !important;
        overflow-y: auto !important;
        border-radius: 20px !important;
    }

    html.is-mobile-app.is-chat-thread-open .composer-icon-button {
        width: 38px !important;
        height: 38px !important;
    }

    html.is-mobile-app.is-chat-thread-open .send-button {
        width: 40px !important;
        height: 40px !important;
        min-width: 40px !important;
    }

    html.is-mobile-app.is-chat-thread-open .reply-context-panel,
    html.is-mobile-app.is-chat-thread-open .voice-recording-panel,
    html.is-mobile-app.is-chat-thread-open .composer-retry-panel {
        margin-bottom: 4px !important;
        background: var(--surface) !important;
        border: 1px solid var(--border) !important;
        box-shadow: 0 10px 26px rgba(15, 23, 42, 0.10) !important;
    }

    html.is-mobile-app .chat-create-button,
    html.is-mobile-app .groups-create-button {
        bottom: calc(var(--mobile-nav-height) + 14px) !important;
        transform: none !important;
        z-index: 130 !important;
    }

    /* Panels and sheets: solid, scrollable, and above the floored navbar. */
    .new-chat-sheet,
    .groups-sheet,
    .call-overlay,
    .confirm-dialog {
        position: fixed !important;
        inset: 0 !important;
        z-index: 300 !important;
        background: rgba(15, 23, 42, 0.64) !important;
        backdrop-filter: blur(8px);
    }

    .new-chat-sheet.is-hidden,
    .groups-sheet.is-hidden,
    .call-overlay.is-hidden,
    .confirm-dialog.is-hidden {
        display: none !important;
    }

    .new-chat-sheet,
    .groups-sheet {
        display: grid !important;
        align-items: end !important;
        padding: calc(var(--mobile-top-gap) + 12px) 14px calc(var(--mobile-nav-height) + 16px) !important;
    }

    html.is-mobile-app.is-chat-thread-open .new-chat-sheet,
    html.is-mobile-app.is-chat-thread-open .groups-sheet {
        padding-bottom: 16px !important;
    }

    .new-chat-panel,
    .groups-sheet-panel,
    .groups-sheet-card,
    .call-overlay-card,
    .call-panel,
    .confirm-card {
        width: 100% !important;
        max-height: 76dvh !important;
        overflow-y: auto !important;
        border: 1px solid var(--border) !important;
        border-radius: 24px !important;
        background: var(--surface) !important;
        color: var(--text) !important;
        box-shadow: 0 22px 70px rgba(15, 23, 42, 0.34) !important;
    }

    .call-overlay {
        display: grid !important;
        place-items: center !important;
        padding: calc(var(--mobile-top-gap) + 12px) 14px 18px !important;
    }

    .call-overlay-card,
    .call-panel {
        width: min(94vw, 430px) !important;
        max-height: 86dvh !important;
    }

    .call-video-stage {
        width: 100% !important;
        min-height: 210px !important;
        max-height: 42dvh !important;
        border-radius: 18px !important;
        background: #020617 !important;
        overflow: hidden !important;
    }

    .call-video-stage video,
    .call-remote-video,
    .call-local-video {
        background: #020617 !important;
        object-fit: cover !important;
    }

    /* Settings cards stack like a phone UI, not a squeezed desktop. */
    html.is-mobile-app .settings-card,
    html.is-mobile-app .profile-card,
    html.is-mobile-app .account-card {
        width: 100% !important;
    }

    html.is-mobile-app .settings-card {
        display: grid !important;
        align-items: stretch !important;
    }

    html.is-mobile-app .mobile-layout-settings-card,
    html.is-mobile-app .translation-settings-card {
        gap: 16px !important;
    }

    html.is-mobile-app .mobile-layout-settings-card > div,
    html.is-mobile-app .translation-settings-card > div {
        display: grid !important;
        gap: 4px !important;
    }

    html.is-mobile-app .translation-settings-card .field {
        margin: 0 !important;
    }

    html.is-mobile-app .translation-settings-card select {
        width: 100% !important;
        min-height: 48px !important;
        padding: 0 14px !important;
        border-radius: 16px !important;
        background: var(--surface-soft) !important;
        color: var(--text) !important;
        font-weight: 800 !important;
    }

    html.is-mobile-app .translation-settings-card .primary-button {
        width: 100% !important;
        justify-content: center !important;
    }

    html[data-theme="light"] .new-chat-panel,
    html[data-theme="light"] .groups-sheet-panel,
    html[data-theme="light"] .groups-sheet-card,
    html[data-theme="light"] .call-overlay-card,
    html[data-theme="light"] .call-panel,
    html[data-theme="light"] .confirm-card,
    html[data-theme="light"] .bottom-nav {
        background: #ffffff !important;
        color: #0f172a !important;
    }
}

@media (max-width: 900px) and (orientation: landscape) {
    :root {
        --mobile-top-gap: 10px;
        --mobile-nav-base-height: 66px;
    }

    html.is-mobile-app .app-screen {
        padding-top: 8px !important;
    }

    html.is-mobile-app.is-chat-thread-open #chatThreadView {
        height: calc(100dvh - var(--mobile-top-gap) - 12px) !important;
    }

    .new-chat-panel,
    .groups-sheet-panel,
    .groups-sheet-card,
    .call-overlay-card,
    .call-panel {
        width: min(92vw, 760px) !important;
        max-height: 84dvh !important;
    }

    .call-video-stage {
        min-height: 170px !important;
        max-height: 54dvh !important;
    }
}
/* Chat name alignment fix: mobile.css was loaded last and still assumed the old no-avatar header grid. */
@media (max-width: 900px) {
    html.is-mobile-app.is-chat-thread-open .chat-header {
        display: flex !important;
        grid-template-columns: none !important;
        align-items: center !important;
        gap: 7px !important;
        min-width: 0 !important;
        padding: 4px 0 8px !important;
    }

    html.is-mobile-app.is-chat-thread-open .chat-header > .chat-back-button,
    html.is-mobile-app.is-chat-thread-open .chat-header > .chat-header-action {
        flex: 0 0 36px !important;
        width: 36px !important;
        height: 36px !important;
        min-width: 36px !important;
    }

    html.is-mobile-app.is-chat-thread-open .chat-header > .chat-thread-avatar {
        flex: 0 0 36px !important;
        width: 36px !important;
        height: 36px !important;
        min-width: 36px !important;
    }

    html.is-mobile-app.is-chat-thread-open .chat-header > div:not(.chat-thread-avatar) {
        flex: 1 1 auto !important;
        min-width: 0 !important;
        max-width: none !important;
        overflow: hidden !important;
        text-align: left !important;
        direction: ltr !important;
    }

    html.is-mobile-app.is-chat-thread-open #chatThreadTitle,
    html.is-mobile-app.is-chat-thread-open #chatThreadSubtitle {
        display: block !important;
        min-width: 0 !important;
        max-width: 100% !important;
        overflow: hidden !important;
        text-overflow: ellipsis !important;
        white-space: nowrap !important;
        text-align: left !important;
        direction: ltr !important;
    }

    html.is-mobile-app.is-chat-thread-open #chatThreadTitle {
        font-size: 18px !important;
        line-height: 1.15 !important;
    }

    html.is-mobile-app.is-chat-thread-open #chatThreadSubtitle {
        line-height: 1.2 !important;
    }
}
