/* One text row, with independent reference and action rows. Desktop only;
 * phones continue to use their keyboard-aware composer sizing. */
@media (min-width: 921px) {
    html body.chat-landing[data-mode="chat"]:has(#ai-chat-model)
        .workspace .composer:has(#chat-input):has(.fuuvo-desktop-autogrow) {
        max-height: var(--fuuvo-landing-available-height, calc(100vh - 144px)) !important;
    }

    html body:is(.chat-landing, .chat-detail-page, .chat-detail-open)[data-mode="chat"]:has(#ai-chat-model)
        .workspace #chat-compose-field:has(#chat-input) > .chat-input-wrap.fuuvo-desktop-autogrow {
        --fuuvo-desktop-text-min: 58px;
        --fuuvo-desktop-toolbar-height: 54px;
        display: flex !important;
        flex-direction: column !important;
        align-items: stretch !important;
        height: auto !important;
        min-height: 0 !important;
        max-height: none !important;
        padding: 0 !important;
        overflow: visible !important;
        box-sizing: border-box !important;
    }

    html body.chat-landing[data-mode="chat"]:has(#ai-chat-model)
        .workspace #chat-compose-field:has(#chat-input) > .chat-input-wrap.fuuvo-desktop-autogrow {
        --fuuvo-desktop-text-min: 104px;
        --fuuvo-desktop-toolbar-height: 64px;
    }

    html body:is(.chat-landing, .chat-detail-page, .chat-detail-open)[data-mode="chat"]:has(#ai-chat-model)
        .workspace #chat-compose-field:has(#chat-input) > .chat-input-wrap.fuuvo-desktop-autogrow > #chat-input {
        position: relative !important;
        inset: auto !important;
        order: 2 !important;
        flex: 0 0 auto !important;
        width: 100% !important;
        min-width: 0 !important;
        height: var(--fuuvo-desktop-text-height, var(--fuuvo-desktop-text-min)) !important;
        min-height: 0 !important;
        max-height: none !important;
        margin: 0 !important;
        padding: 12px 16px 4px !important;
        box-sizing: border-box !important;
        overflow-x: hidden !important;
        overflow-y: var(--fuuvo-desktop-text-overflow, hidden) !important;
        resize: none !important;
    }

    html body.chat-landing[data-mode="chat"]:has(#ai-chat-model)
        .workspace #chat-compose-field:has(#chat-input) > .chat-input-wrap.fuuvo-desktop-autogrow > #chat-input {
        padding: 20px 20px 8px !important;
    }

    html body:is(.chat-landing, .chat-detail-page, .chat-detail-open)[data-mode="chat"]:has(#ai-chat-model)
        .workspace #chat-compose-field:has(#chat-input) > .chat-input-wrap.fuuvo-desktop-autogrow > .chat-input-tools {
        position: relative !important;
        inset: auto !important;
        order: 3 !important;
        flex: 0 0 var(--fuuvo-desktop-toolbar-height) !important;
        width: auto !important;
        min-width: 0 !important;
        height: var(--fuuvo-desktop-toolbar-height) !important;
        min-height: var(--fuuvo-desktop-toolbar-height) !important;
        max-height: none !important;
        margin: 0 12px !important;
        padding: 0 !important;
        box-sizing: border-box !important;
        align-items: center !important;
    }

    html body:is(.chat-landing, .chat-detail-page, .chat-detail-open)[data-mode="chat"]:has(#ai-chat-model)
        .workspace #chat-compose-field:has(#chat-input) > .chat-input-wrap.fuuvo-desktop-autogrow.has-attachments > .chat-attachment-bar:not(.hidden) {
        position: relative !important;
        inset: auto !important;
        order: 1 !important;
        flex: 0 0 auto !important;
        width: auto !important;
        min-width: 0 !important;
        height: 69px !important;
        min-height: 69px !important;
        margin: 0 12px !important;
        padding: 8px 0 0 !important;
        box-sizing: border-box !important;
        overflow: hidden !important;
    }

    html body:is(.chat-landing, .chat-detail-page, .chat-detail-open)[data-mode="chat"]:has(#ai-chat-model)
        .workspace #chat-compose-field:has(#chat-input) > .chat-input-wrap.fuuvo-desktop-autogrow.has-attachments > .chat-input-hint {
        top: 81px !important;
    }
}

/* Upload now belongs to the model/options rail. Reserve one independent
 * action column on home, detail and in-page new-chat transitions; the legacy
 * detail grid still reserves an obsolete upload column. */
@media (min-width: 921px) {
    html body:is(.chat-landing, .chat-detail-page, .chat-detail-open)[data-mode="chat"]:has(#ai-chat-model)
        .workspace #chat-compose-field:has(#chat-input) > .chat-input-wrap.has-chat-input > .chat-input-tools {
        display: grid !important;
        grid-template-columns: minmax(0, 1fr) max-content !important;
        grid-template-rows: auto !important;
        column-gap: 10px !important;
        align-items: center !important;
        min-width: 0 !important;
    }

    html body:is(.chat-landing, .chat-detail-page, .chat-detail-open)[data-mode="chat"]:has(#ai-chat-model)
        .workspace #chat-compose-field:has(#chat-input) > .chat-input-wrap.has-chat-input > .chat-input-tools > .ai-chat-mode-controls {
        position: static !important;
        inset: auto !important;
        transform: none !important;
        grid-column: 1 !important;
        grid-row: 1 !important;
        width: auto !important;
        min-width: 0 !important;
        max-width: 100% !important;
        margin: 0 !important;
        overflow-x: auto !important;
        overflow-y: hidden !important;
        scrollbar-width: none;
    }

    html body:is(.chat-landing, .chat-detail-page, .chat-detail-open)[data-mode="chat"]:has(#ai-chat-model)
        .workspace #chat-compose-field:has(#chat-input) > .chat-input-wrap.has-chat-input > .chat-input-tools > #chat-input-action-group {
        position: relative !important;
        inset: auto !important;
        transform: none !important;
        grid-column: 2 !important;
        grid-row: 1 !important;
        display: flex !important;
        flex: 0 0 auto !important;
        width: max-content !important;
        min-width: max-content !important;
        max-width: none !important;
        margin: 0 !important;
        align-items: center !important;
        justify-content: flex-end !important;
        justify-self: end !important;
    }

    html body:is(.chat-landing, .chat-detail-page, .chat-detail-open)[data-mode="chat"]:has(#ai-chat-model)
        .workspace #chat-compose-field:has(#chat-input) > .chat-input-wrap.has-chat-input > .chat-input-tools > #chat-input-action-group > button {
        position: relative !important;
        inset: auto !important;
        transform: none !important;
        flex-basis: auto !important;
        flex-shrink: 0 !important;
        margin: 0 !important;
    }

    /* Older desktop Safari versions ignore relational selectors. The initial
     * HTML already carries these stable classes, so no delayed JS is needed.
     * The repeated field ID also beats the legacy :is(#chat-input, ...) rail
     * selector, whose ID specificity otherwise moves the toolbar upward. */
    @supports not selector(:has(*)) {
        html body[data-mode="chat"] .workspace #chat-compose-field#chat-compose-field.has-chat-input > .chat-input-wrap.has-chat-input > .chat-input-tools {
            position: absolute !important;
            inset: auto 12px 8px !important;
            width: auto !important;
            height: 46px !important;
            min-height: 46px !important;
            max-height: 46px !important;
            min-width: 0 !important;
            margin: 0 !important;
            padding: 0 !important;
            transform: none !important;
            flex: none !important;
            box-sizing: border-box !important;
            display: grid !important;
            grid-template-columns: minmax(0, 1fr) max-content !important;
            grid-template-rows: 46px !important;
            column-gap: 10px !important;
            align-items: center !important;
            pointer-events: auto !important;
            z-index: 30 !important;
        }
        html body[data-mode="chat"] .workspace #chat-compose-field#chat-compose-field.has-chat-input > .chat-input-wrap.has-chat-input > .chat-input-tools > .ai-chat-mode-controls {
            position: static !important;
            inset: auto !important;
            transform: none !important;
            grid-column: 1 !important;
            grid-row: 1 !important;
            width: auto !important;
            min-width: 0 !important;
            max-width: 100% !important;
            margin: 0 !important;
            overflow-x: auto !important;
        }
        html body[data-mode="chat"] .workspace #chat-compose-field#chat-compose-field.has-chat-input > .chat-input-wrap.has-chat-input > .chat-input-tools > #chat-input-action-group {
            position: relative !important;
            inset: auto !important;
            transform: none !important;
            grid-column: 2 !important;
            grid-row: 1 !important;
            display: flex !important;
            width: max-content !important;
            min-width: max-content !important;
            flex: 0 0 auto !important;
            margin: 0 !important;
            justify-self: end !important;
        }
        html body[data-mode="chat"] .workspace #chat-compose-field#chat-compose-field.has-chat-input > .chat-input-wrap.has-chat-input > .chat-input-tools > #chat-input-action-group > button {
            position: relative !important;
            inset: auto !important;
            transform: none !important;
            flex-basis: auto !important;
            flex-shrink: 0 !important;
            margin: 0 !important;
        }
    }
}

/* A narrow Malay catalogue uses two rows. Keep every tool reachable without
 * using all of a short laptop's height before the text has room to grow. */
@media (min-width: 921px) and (max-height: 700px) {
    html body.chat-landing[data-mode="chat"]:has(#ai-chat-model)
        .workspace .composer:has(#chat-input):has(.fuuvo-desktop-autogrow) .composer-capability-shell {
        max-height: clamp(112px, 23vh, 200px) !important;
        min-height: 0 !important;
        flex-shrink: 0 !important;
        overflow-x: hidden !important;
        overflow-y: auto !important;
        overscroll-behavior-y: contain;
        scrollbar-width: thin;
        scrollbar-gutter: stable;
    }
}
