/* Mobile (≤768px) — viewport containment first, not desktop leftovers */

@media (max-width: 768px) {
    :root {
        --mobile-gutter: 16px;
        --mobile-topbar: 46px;
        --mobile-nav: 40px;
        --keyboard-inset: 0px;
        --composer-offset: 0px;
        --bottom-nav-height: 40px;
    }

    /* Hard viewport lock — fixes MIUI / Android horizontal blowout */
    html {
        width: 100%;
        max-width: 100%;
        overflow-x: clip;
        overflow-x: hidden;
    }

    body {
        width: 100%;
        max-width: 100%;
        min-height: 100dvh;
        min-height: -webkit-fill-available;
        overflow-x: clip;
        overflow-x: hidden;
        padding-left: 0;
        padding-right: 0;
    }

    .ambient-bg {
        max-width: 100vw;
        overflow: hidden;
    }

    .ambient-bg::before {
        width: 100%;
        max-width: 100vw;
        left: 0;
        transform: none;
        animation: none;
    }

    #app,
    .app {
        display: flex;
        flex-direction: column;
        width: 100%;
        max-width: 100%;
        min-width: 0;
        height: 100dvh;
        height: -webkit-fill-available;
        max-height: 100dvh;
        overflow: hidden;
        overflow-x: clip;
        overflow-x: hidden;
        padding-left: var(--safe-left);
        padding-right: var(--safe-right);
        box-sizing: border-box;
    }

    /* Kill desktop fixed/centered composer */
    .composer,
    .composer.composer--float {
        position: relative !important;
        left: auto !important;
        right: auto !important;
        bottom: auto !important;
        transform: none !important;
        width: 100% !important;
        max-width: 100% !important;
        min-width: 0 !important;
        margin: 0 !important;
        padding: 0 !important;
        pointer-events: auto !important;
        flex-shrink: 0;
        z-index: 30;
        background: transparent !important;
        border-top: none !important;
        box-sizing: border-box;
    }

    .topbar {
        flex: 0 0 auto;
        min-width: 0;
        max-width: 100%;
        min-height: calc(var(--mobile-topbar) + var(--safe-top));
        padding: calc(6px + var(--safe-top)) var(--mobile-gutter) 6px;
        box-sizing: border-box;
    }

    .topbar__left,
    .topbar__right {
        min-width: 0;
    }

    .topbar__left .nav { display: none; }

    .topbar__right #api-status,
    .topbar__right #usage-label,
    .topbar__right .pill--user { display: none; }

    .brand__name { font-size: .9rem; }

    .main {
        flex: 1 1 auto;
        min-height: 0;
        min-width: 0;
        width: 100%;
        max-width: 100%;
        overflow: hidden;
        overflow-x: clip;
        overflow-x: hidden;
        padding: 0 !important;
        box-sizing: border-box;
    }

    .view--studio,
    .view--gallery,
    .view--admin,
    .view--chat {
        height: 100%;
        min-height: 0;
        min-width: 0;
        max-width: 100%;
        overflow: hidden;
        overflow-x: clip;
        overflow-x: hidden;
        display: flex;
        flex-direction: column;
        width: 100%;
        box-sizing: border-box;
    }

    .view--studio {
        min-height: 0 !important;
    }

    /* ── Studio canvas ── */
    .studio-stage {
        flex: 1;
        min-height: 0;
        min-width: 0;
        width: 100%;
        max-width: 100%;
        overflow-x: clip;
        overflow-x: hidden;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        padding: 8px var(--mobile-gutter) 12px;
        justify-content: flex-start;
        box-sizing: border-box;
    }

    .studio-canvas,
    .studio-layout,
    .studio-layout__main,
    .studio-hero,
    .empty-state {
        width: 100%;
        max-width: 100%;
        min-width: 0;
        box-sizing: border-box;
    }

    .studio-layout {
        flex-direction: column;
        gap: 10px;
    }

    .studio-hero__media {
        border-radius: 14px;
        overflow: hidden;
        max-width: 100%;
    }

    .studio-hero__media img,
    .studio-hero__media video {
        display: block;
        width: 100%;
        max-width: 100%;
        height: auto;
        max-height: 52dvh;
        object-fit: contain;
    }

    .studio-hero__actions {
        opacity: 1;
        transform: none;
    }

    .studio-hero__action {
        width: 36px;
        height: 36px;
    }

    .studio-hero__prompt {
        font-size: .82rem;
        padding: 0 4px;
        word-break: break-word;
    }

    .studio-thread__strip {
        flex-direction: row;
        max-width: 100%;
        overflow-x: auto;
        scrollbar-width: none;
    }

    .studio-thread__item {
        flex: 0 0 44px;
        width: 44px;
        height: 44px;
    }

    .empty-state {
        padding: 24px var(--mobile-gutter);
    }

    .empty-state__title {
        font-size: 1.5rem;
    }

    .empty-state__sub {
        font-size: .86rem;
    }

    .empty-state__prompts {
        max-width: 100%;
        padding: 0;
    }

    /* ── Composer: inset pill, not full-bleed bar ── */
    .composer__drawer {
        display: none !important;
    }

    .composer.composer--drawer-open .composer__drawer {
        display: block !important;
        position: fixed;
        left: var(--safe-left);
        right: var(--safe-right);
        bottom: var(--mobile-nav);
        z-index: 50;
        max-height: 70dvh;
        max-width: 100%;
        margin: 0;
        padding: 14px var(--mobile-gutter) calc(14px + var(--safe-bottom));
        border-radius: 16px 16px 0 0;
        border: 1px solid rgba(255, 255, 255, 0.1);
        border-bottom: none;
        background: #111;
        overflow-x: hidden;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        box-sizing: border-box;
    }

    .composer.composer--drawer-open::before {
        content: "";
        position: fixed;
        inset: 0;
        z-index: 49;
        background: rgba(0, 0, 0, 0.55);
    }

    .composer__drawer .modes {
        flex-wrap: wrap;
        justify-content: flex-start;
        gap: 6px;
        margin-bottom: 12px;
        padding: 4px;
        max-width: 100%;
    }

    .composer__drawer .mode {
        font-size: .68rem;
        padding: 4px 9px;
    }

    .composer__drawer .aspect-picker {
        gap: 6px;
    }

    .composer__drawer .aspect-option {
        flex: 1 1 calc(33.333% - 6px);
        min-width: 0;
    }

    .composer__drawer .settings-grid,
    .composer__drawer .style-bar,
    .composer__drawer .upload-bar,
    .composer__drawer .attachment-bar,
    .composer__drawer .edit-options-bar,
    .composer__drawer .negative-prompt-bar,
    .composer__drawer .preset-bar {
        max-width: 100%;
        overflow-x: hidden;
    }

    .negative-prompt-bar {
        margin: 0 var(--mobile-gutter) 8px;
        max-width: calc(100% - 2 * var(--mobile-gutter));
        box-sizing: border-box;
    }

    .negative-prompt-chip {
        font-size: .66rem;
        padding: 4px 8px;
    }

    .composer__prompt-shell {
        padding: 6px var(--mobile-gutter) 8px;
        width: 100%;
        max-width: 100%;
        min-width: 0;
        box-sizing: border-box;
    }

    .prompt-row,
    .prompt-row--grok {
        display: flex;
        flex-wrap: nowrap;
        align-items: center;
        gap: 6px;
        width: 100%;
        max-width: 100%;
        min-width: 0;
        margin: 0;
        padding: 5px 6px 5px 10px;
        border-radius: 24px;
        background: #1a1a1a;
        border: 1px solid rgba(255, 255, 255, 0.1);
        box-shadow: none;
        overflow: hidden;
        box-sizing: border-box;
    }

    .prompt-row .cost-estimate,
    .prompt-row .spicy-toggle,
    .prompt-row #enhance-btn,
    .prompt-row #settings-btn {
        display: none !important;
    }

    #prompt {
        flex: 1 1 auto;
        order: 0;
        width: 0;
        min-width: 0;
        min-height: 24px;
        max-height: 96px;
        padding: 8px 0;
        font-size: 16px;
        line-height: 1.4;
        overflow: hidden;
    }

    .prompt-row #attach-btn,
    .prompt-row .composer__drawer-btn {
        order: 1;
        width: 34px;
        height: 34px;
        flex-shrink: 0;
    }

    .prompt-row .send-btn {
        order: 2;
        width: 38px;
        height: 38px;
        flex-shrink: 0;
        margin-left: 0;
    }

    .composer__drawer-btn {
        display: inline-grid !important;
    }

    .attachment-bar,
    .edit-options-bar {
        margin: 0 var(--mobile-gutter) 8px;
        max-width: calc(100% - 2 * var(--mobile-gutter));
        box-sizing: border-box;
    }

    /* ── Bottom nav (compact tab bar) ── */
    .bottom-nav {
        display: flex;
        align-items: stretch;
        flex: 0 0 auto;
        width: 100%;
        max-width: 100%;
        min-width: 0;
        min-height: calc(var(--mobile-nav) + var(--safe-bottom));
        padding: 2px 4px calc(2px + var(--safe-bottom));
        gap: 2px;
        background: #000;
        border-top: 1px solid rgba(255, 255, 255, 0.07);
        box-sizing: border-box;
    }

    .bottom-nav__btn {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 1px;
        flex: 1 1 0;
        min-width: 0;
        min-height: 0;
        height: var(--mobile-nav);
        padding: 2px 0;
        border: none;
        border-radius: 8px;
        background: transparent;
        color: var(--text-subtle);
        cursor: pointer;
        -webkit-tap-highlight-color: transparent;
    }

    .bottom-nav__btn--active {
        color: var(--text);
        background: rgba(255, 255, 255, 0.06);
    }

    .bottom-nav__icon {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 20px;
        height: 20px;
        flex-shrink: 0;
        line-height: 0;
    }

    .bottom-nav__icon svg {
        width: 18px;
        height: 18px;
        display: block;
    }

    .bottom-nav__label {
        font-size: .48rem;
        font-weight: 500;
        line-height: 1;
        letter-spacing: 0;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        max-width: 100%;
    }

    /* ── Gallery ── */
    .view--gallery {
        max-width: 100%;
        padding-top: .5rem;
    }

    .gallery-toolbar {
        padding: 8px var(--mobile-gutter) 0;
        gap: 8px;
        max-width: 100%;
        box-sizing: border-box;
    }

    .gallery-search {
        min-width: 0;
        flex: 1 1 auto;
    }

    .gallery-search input {
        font-size: 16px;
        min-height: 42px;
        min-width: 0;
    }

    .gallery-grid {
        flex: 1;
        min-width: 0;
        overflow-y: auto;
        overflow-x: clip;
        overflow-x: hidden;
        padding: 0 var(--mobile-gutter) 8px;
        box-sizing: border-box;
    }

    .history-card__body {
        opacity: 1;
        max-height: none;
    }

    .history-card__actions {
        opacity: 1;
    }

    /* ── Chat ── */
    .chat-layout {
        grid-template-columns: 1fr;
        grid-template-rows: auto minmax(0, 1fr);
        height: 100%;
        min-width: 0;
        max-width: 100%;
    }

    .chat-sidebar {
        flex-direction: row;
        min-width: 0;
        max-width: 100%;
        padding: 8px var(--mobile-gutter);
        border-bottom: 1px solid rgba(255, 255, 255, 0.07);
        box-sizing: border-box;
    }

    .chat-sidebar__title { display: none; }

    #chat-new {
        font-size: .72rem;
        padding: 6px 10px;
        flex-shrink: 0;
    }

    #chat-sessions {
        flex-direction: row;
        min-width: 0;
        overflow-x: auto;
        scrollbar-width: none;
    }

    .chat-session-wrap {
        min-width: 88px;
        max-width: 120px;
        flex-shrink: 0;
    }

    .chat-main {
        display: flex;
        flex-direction: column;
        min-height: 0;
        min-width: 0;
        max-width: 100%;
    }

    .chat-messages {
        flex: 1;
        min-width: 0;
        overflow-y: auto;
        overflow-x: clip;
        overflow-x: hidden;
        padding: 10px var(--mobile-gutter);
        box-sizing: border-box;
    }

    .chat-msg {
        max-width: 100%;
    }

    .chat-msg__bubble {
        max-width: min(480px, 92%);
    }

    .chat-msg__media img {
        max-height: 45dvh;
        width: 100%;
        max-width: 100%;
        object-fit: contain;
    }

    .chat-composer {
        padding: 8px var(--mobile-gutter) calc(8px + var(--safe-bottom));
        border-top: 1px solid rgba(255, 255, 255, 0.07);
        background: #000;
        max-width: 100%;
        box-sizing: border-box;
    }

    .chat-mode-hint { display: none; }

    .chat-modes {
        overflow-x: auto;
        scrollbar-width: none;
        margin-bottom: 8px;
        max-width: 100%;
    }

    .chat-mode {
        font-size: .72rem;
        padding: 5px 10px;
        flex-shrink: 0;
    }

    .chat-input-row {
        display: flex;
        flex-wrap: nowrap;
        align-items: center;
        gap: 6px;
        width: 100%;
        max-width: 100%;
        min-width: 0;
        padding: 5px 6px 5px 10px;
        border-radius: 24px;
        background: #1a1a1a;
        border: 1px solid rgba(255, 255, 255, 0.1);
        overflow: hidden;
        box-sizing: border-box;
    }

    #chat-input {
        flex: 1 1 auto;
        width: 0;
        min-width: 0;
        min-height: 24px;
        max-height: 96px;
        font-size: 16px;
        padding: 8px 0;
    }

    .chat-input-row .icon-btn {
        width: 34px;
        height: 34px;
        flex-shrink: 0;
    }

    .chat-input-row .send-btn {
        width: 38px;
        height: 38px;
        flex-shrink: 0;
    }

    /* Viewer */
    .viewer__panel {
        width: 100%;
        max-width: 100%;
        border-radius: 0;
        padding: calc(10px + var(--safe-top)) var(--mobile-gutter) calc(10px + var(--safe-bottom));
        box-sizing: border-box;
    }

    .viewer__image,
    .viewer__video {
        max-height: 50dvh;
        width: 100%;
        max-width: 100%;
        object-fit: contain;
    }

    .viewer__actions {
        display: flex;
        flex-direction: column;
        gap: 8px;
    }

    .viewer__actions .btn {
        width: 100%;
        min-height: 42px;
        font-size: .8rem;
    }

    /* Login */
    .login-screen {
        padding: var(--mobile-gutter);
        box-sizing: border-box;
    }

    .login-card {
        width: 100%;
        max-width: min(360px, 100%);
        padding: 28px 20px;
        box-sizing: border-box;
    }

    .login-form .field input {
        font-size: 16px;
        min-height: 46px;
    }

    body.composer-drawer-open {
        overflow: hidden;
    }
}

@media (max-width: 480px) {
    :root {
        --mobile-gutter: 14px;
    }

    .settings-grid,
    .admin-stats {
        grid-template-columns: 1fr;
    }

    .prompt-row #attach-btn,
    .prompt-row .composer__drawer-btn {
        width: 32px;
        height: 32px;
    }

    .prompt-row .send-btn {
        width: 36px;
        height: 36px;
    }
}

/* Keyboard: pin dock above keyboard (Android / MIUI) */
@media (max-width: 768px) {
    body.is-keyboard-open .composer {
        position: fixed !important;
        left: var(--safe-left) !important;
        right: var(--safe-right) !important;
        bottom: var(--keyboard-inset) !important;
        width: auto !important;
        max-width: none !important;
        z-index: 60;
        background: #000 !important;
    }

    body.is-keyboard-open .bottom-nav {
        display: none;
    }
}