/*
 * Mobile layout for Satbayev AI chat.
 * Loaded only on /llm/ — safe to override .rms-* classes.
 *
 * Topbar heights (style.css):
 *   ≤ 900px  →  min-height: 64px   (mobile flex)
 *   > 900px  →  min-height: 72px   (desktop grid)
 */
:root {
    --su-topbar-h: 72px;
}

@media (max-width: 900px) {
    :root { --su-topbar-h: 64px; }
}

/* Drawer-toggle elements: hidden on desktop, shown via media query below */
.su-chat-drawer-open,
.su-chat-drawer-close,
.su-chat-backdrop {
    display: none;
}

/* ============================================================
   MOBILE LAYOUT  ≤ 960px
   ============================================================ */
@media (max-width: 960.98px) {

    /* ── rms overrides ── */
    .rms-content {
        padding: 0 !important;
        margin: 0 !important;
        flex: none !important;
    }

    .rms-footer {
        display: none !important;
    }

    /* ── Chat page: fixed below the site topbar ──
     *  overflow: visible so the dropdown can extend into the chat area.
     */
    .su-chat-page {
        position: fixed !important;
        top: var(--su-topbar-h) !important;
        left: 0 !important;
        right: 0 !important;
        bottom: 0 !important;
        width: 100% !important;
        height: auto !important;
        min-height: 0 !important;
        overflow: visible !important;
        background: #ffffff;
        z-index: 10 !important;
    }

    /* Main flex column — fills the entire fixed chat page */
    .su-chat-main {
        display: flex !important;
        flex-direction: column !important;
        width: 100% !important;
        height: 100% !important;
        min-height: 0 !important;
        overflow: hidden !important;
        position: relative !important;
        z-index: 1 !important;
    }

    /* ── Chat header ── */
    .su-chat-header {
        flex: 0 0 auto !important;
        padding: 10px 12px 8px !important;
        border-bottom: 1px solid #e2e8f0;
        background: #ffffff;
        position: relative !important;
        z-index: 2 !important;
    }

    /* Single horizontal row: [☰] [title] [⚙] [↗] */
    .su-chat-header-top {
        display: flex !important;
        flex-direction: row !important;
        align-items: center !important;
        gap: 8px !important;
        width: 100% !important;
    }

    .su-chat-header-copy {
        flex: 1 1 auto !important;
        min-width: 0 !important;
        text-align: left !important;
        align-items: flex-start !important;
    }

    .su-chat-header-title {
        margin: 0 !important;
        font-size: 16px !important;
        font-weight: 700 !important;
        line-height: 1.25 !important;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .su-chat-header-subtitle {
        display: none !important;
    }

    .su-chat-share-status {
        margin: 4px 0 0 !important;
        font-size: 12px !important;
    }

    /* ── Hamburger open button ── */
    .su-chat-drawer-open {
        display: inline-flex;
        flex-shrink: 0;
        align-items: center;
        justify-content: center;
        width: 40px;
        height: 40px;
        border: 1px solid #dbe4ef;
        border-radius: 12px;
        background: #ffffff;
        color: #132848;
        font-size: 22px;
        cursor: pointer;
        transition: background 0.15s, border-color 0.15s;
    }

    .su-chat-drawer-open:hover,
    .su-chat-drawer-open:active {
        background: #f1f5f9;
        border-color: #b6c8de;
    }

    /* ── Settings button — icon only ── */
    .su-chat-settings-button {
        flex-shrink: 0 !important;
        width: 40px !important;
        height: 40px !important;
        padding: 0 !important;
        border-radius: 12px !important;
        justify-content: center !important;
    }

    .su-chat-settings-button span {
        display: none !important;
    }

    /* ── Share button — icon only ── */
    .su-chat-share-button {
        flex-shrink: 0 !important;
        width: 40px !important;
        height: 40px !important;
        padding: 0 !important;
        display: inline-flex !important;
        justify-content: center !important;
        align-items: center !important;
        border-radius: 12px !important;
    }

    .su-chat-share-button span {
        display: none !important;
    }

    /* Desktop @640px rule sets width: 100% on .chat-share-btn — reset it */
    .su-chat-settings-button.chat-share-btn,
    .su-chat-share-button.chat-share-btn {
        width: 40px !important;
        justify-content: center !important;
    }

    /* ── Settings dropdown ── */
    .su-chat-settings-dropdown {
        position: absolute !important;
        top: calc(100% + 6px) !important;
        right: 0 !important;
        left: auto !important;
        width: min(320px, calc(100vw - 20px)) !important;
        z-index: 1070 !important;
        border-radius: 16px !important;
        box-shadow: 0 8px 32px rgba(15, 23, 42, 0.18) !important;
    }

    .su-chat-model-select {
        font-size: 15px !important;
        padding: 10px 12px !important;
        height: 44px;
    }

    /* ── Messages ── */
    .su-chat-messages {
        flex: 1 1 auto !important;
        min-height: 0 !important;
        width: 100% !important;
        overflow-y: auto !important;
        overscroll-behavior: contain;
        padding: 14px 12px 20px !important;
        background: #ffffff;
    }

    .su-chat-messages .message-bubble {
        max-width: 92% !important;
    }

    /* Tables — horizontal scroll */
    .su-chat-messages .chat-md-table {
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch;
    }

    .su-chat-messages .chat-md-table table {
        min-width: 440px;
    }

    /* Follow-up buttons — horizontal scroll */
    .su-chat-messages .chat-followup-buttons {
        flex-wrap: nowrap !important;
        overflow-x: auto !important;
        overflow-y: hidden !important;
        -webkit-overflow-scrolling: touch;
        padding-bottom: 4px !important;
        gap: 8px !important;
        scrollbar-width: none;
    }

    .su-chat-messages .chat-followup-buttons::-webkit-scrollbar {
        display: none;
    }

    .su-chat-messages .chat-followup-btn {
        flex-shrink: 0 !important;
        font-size: 13px !important;
        padding: 7px 13px !important;
        white-space: nowrap !important;
    }

    /* ── Input area ── */
    .su-chat-input-wrap {
        flex: 0 0 auto !important;
        position: relative !important;
        bottom: auto !important;
        width: 100% !important;
        padding: 8px 10px calc(8px + env(safe-area-inset-bottom)) !important;
        background: #ffffff;
        border-top: 1px solid #e8eef6;
    }

    .su-chat-form {
        display: flex !important;
        align-items: flex-end !important;
        gap: 8px !important;
        width: 100% !important;
        grid-template-columns: unset !important;
    }

    .su-chat-textarea {
        flex: 1 1 auto !important;
        min-width: 0 !important;
        max-height: 130px !important;
        font-size: 16px !important;
        line-height: 1.45 !important;
    }

    .su-chat-form-actions {
        display: flex !important;
        align-items: center !important;
        gap: 6px !important;
        flex: 0 0 auto !important;
        grid-template-columns: unset !important;
        width: auto !important;
    }

    .su-chat-send-button,
    .su-chat-stop-button {
        width: auto !important;
        flex-shrink: 0 !important;
        min-width: 44px !important;
        min-height: 44px !important;
    }

    /* ============================================================
       HISTORY DROPDOWN
       Opens as a panel that drops DOWN over the chat messages.
       position: absolute → contained inside .su-chat-page (overflow: visible).
       ============================================================ */

    .su-chat-drawer {
        /* Geometry */
        position: absolute !important;
        z-index: 300 !important;
        top: 0 !important;
        left: 0 !important;
        right: 0 !important;
        width: 100% !important;
        height: auto !important;
        max-height: 68vh !important;

        /* Appearance */
        background: #ffffff !important;
        border-radius: 0 0 20px 20px !important;
        box-shadow: 0 12px 36px rgba(15, 23, 42, 0.16) !important;
        overflow: hidden !important;

        /* Hidden: translated up so it sits just off-screen */
        transform: translateY(-103%) !important;
        visibility: hidden !important;
        transition:
            transform 0.22s cubic-bezier(0.4, 0, 0.2, 1),
            visibility 0s linear 0.22s !important;
    }

    .su-chat-drawer.su-chat-drawer--open {
        transform: translateY(0) !important;
        visibility: visible !important;
        transition:
            transform 0.22s cubic-bezier(0.4, 0, 0.2, 1),
            visibility 0s linear 0s !important;
    }

    /* Drawer header */
    .su-chat-drawer-header {
        display: flex !important;
        align-items: center !important;
        justify-content: space-between !important;
        flex: 0 0 auto !important;
        padding: 14px 14px 10px !important;
        border-bottom: 1px solid #e2e8f0;
        background: #ffffff;
    }

    .su-chat-drawer-title {
        margin: 0 !important;
        font-size: 16px !important;
        font-weight: 700 !important;
        color: #0f172a !important;
    }

    .su-chat-drawer-actions {
        display: flex !important;
        align-items: center !important;
        gap: 6px !important;
    }

    /* Close ✕ button (inside drawer) */
    .su-chat-drawer-close {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        width: 36px;
        height: 36px;
        border: 1px solid #dbe4ef;
        border-radius: 10px;
        background: #ffffff;
        color: #334155;
        cursor: pointer;
        transition: background 0.15s;
    }

    .su-chat-drawer-close:hover {
        background: #f1f5f9;
    }

    /* Scrollable chat list */
    .su-chat-history {
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
        padding: 8px !important;
        max-height: calc(68vh - 56px) !important;
    }

    /* ── Backdrop: dims messages while dropdown is open ── */
    .su-chat-backdrop {
        display: block;
        position: absolute !important;   /* inside su-chat-page */
        z-index: 200 !important;
        inset: 0 !important;
        border: 0;
        padding: 0;
        background: rgba(15, 23, 42, 0.3);
        opacity: 0;
        visibility: hidden;
        pointer-events: none;
        transition:
            opacity 0.22s ease,
            visibility 0s linear 0.22s;
    }

    /* backdrop is the next sibling of drawer in the DOM */
    .su-chat-drawer--open + .su-chat-backdrop {
        opacity: 1;
        visibility: visible;
        pointer-events: auto;
        transition:
            opacity 0.22s ease,
            visibility 0s linear 0s;
    }

    /* Share dialog */
    .su-chat-share-dialog {
        width: calc(100% - 24px) !important;
        max-width: 420px !important;
        padding: 18px !important;
        border-radius: 18px !important;
    }
}

/* ============================================================
   SMALL PHONES  ≤ 420px
   ============================================================ */
@media (max-width: 420px) {
    .su-chat-header {
        padding-left: 8px !important;
        padding-right: 8px !important;
    }

    .su-chat-messages {
        padding-left: 8px !important;
        padding-right: 8px !important;
    }

    .su-chat-messages .message-bubble {
        max-width: 96% !important;
    }

    .su-chat-header-title {
        font-size: 15px !important;
    }
}

/* ============================================================
   REDUCED MOTION
   ============================================================ */
@media (prefers-reduced-motion: reduce) {
    .su-chat-drawer,
    .su-chat-backdrop {
        transition: none !important;
    }
}
