﻿/**
 * Mirror Ai - Premium Mobile Chat Experience
 * ============================================
 * Complete mobile-first redesign for a premium, polished interface.
 *
 * Architecture:
 * - Single @media(max-width:767px) block per section (no fragmentation)
 * - Uses html body prefix for specificity over desktop chat.css
 * - Uses html body.immersive-mode prefix to beat immersive-mode.css
 * - ZERO dead selectors — every rule targets elements in chat_view.html
 * - The "nuclear fix" (position:absolute on .chat-main) is preserved
 *
 * Sections:
 * 1. CSS Variables
 * 2. Tablet (768-1024px)
 * 3. Mobile Layout Foundation
 * 4. Mobile Header Bar
 * 5. Sidebar Drawer
 * 6. Chat Messages
 * 7. Welcome Card
 * 8. Conversation Starters
 * 9. Input Area
 * 10. Typing Indicator
 * 11. Responsive Tweaks (small/large phones, landscape)
 * 12. Accessibility
 * 13. Performance
 * 14. Immersive Mode Overrides
 */

/* ============================================================================
   1. CSS VARIABLES
   ============================================================================ */
:root {
    /* Colors — inherit from design-system.css */
    --m-bg: var(--color-bg-primary, #0a0e27);
    --m-bg-elevated: var(--color-bg-secondary, #151a35);
    --m-surface: var(--sidebar-bg, rgba(21, 26, 53, 0.97));
    --m-surface-elevated: rgba(30, 37, 64, 0.98);

    /* Accents */
    --m-accent: var(--color-accent-cyan, #00f2fe);
    --m-accent-purple: var(--color-accent-purple, #764ba2);
    --m-gradient-user: var(--gradient-user-message, linear-gradient(135deg, #667eea 0%, #764ba2 100%));

    /* Text */
    --m-text: var(--color-text-primary, #ffffff);
    --m-text-secondary: var(--color-text-secondary, rgba(255, 255, 255, 0.8));
    --m-text-muted: var(--color-text-muted, rgba(255, 255, 255, 0.5));
    --m-text-dim: rgba(255, 255, 255, 0.35);

    /* Spacing */
    --m-gap-xs: 4px;
    --m-gap-sm: 8px;
    --m-gap-md: 16px;
    --m-gap-lg: 24px;
    --m-gap-xl: 32px;

    /* Layout */
    --m-header-h: 56px;
    --m-input-h: 68px;
    --m-nav-h: var(--floating-nav-height-mobile, 58px);
    --m-tap: var(--touch-target-min, 44px);
    --m-avatar: 34px;
    --m-sidebar-w: 300px;

    /* Radii */
    --m-radius-sm: 8px;
    --m-radius-md: 12px;
    --m-radius-lg: 18px;
    --m-radius-xl: 24px;
    --m-radius-pill: 999px;

    /* Shadows */
    --m-shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.15);
    --m-shadow-md: 0 4px 20px rgba(0, 0, 0, 0.25);
    --m-shadow-lg: 0 8px 32px rgba(0, 0, 0, 0.35);
    --m-shadow-glow: 0 0 24px rgba(0, 242, 254, 0.15);

    /* Transitions */
    --m-fast: 150ms ease;
    --m-base: 250ms cubic-bezier(0.4, 0, 0.2, 1);
    --m-slow: 350ms cubic-bezier(0.4, 0, 0.2, 1);

    /* Z-index (from design-system.css scale) */
    --m-z-content: 1;
    --m-z-messages: 5;
    --m-z-header: 100;
    --m-z-input: var(--z-input-area, 650);
    --m-z-nav: var(--z-floating-nav, 600); /* Floating nav z-index */
    --m-z-menu: 700;
    --m-z-overlay: var(--z-overlay, 800);
    --m-z-sidebar: var(--z-sidebar-mobile, 850);

    /* Safe areas */
    --m-safe-top: env(safe-area-inset-top, 0px);
    --m-safe-bottom: env(safe-area-inset-bottom, 0px);
    --m-safe-left: env(safe-area-inset-left, 0px);
    --m-safe-right: env(safe-area-inset-right, 0px);

    /* Computed padding for fixed elements clearance */
    --m-pad-top: calc(var(--m-nav-h) + var(--m-safe-top) + 8px);
    --m-pad-bottom: calc(var(--m-input-h) + var(--m-safe-bottom) + 12px);
}


/* ============================================================================
   2. TABLET (768px - 1024px)
   ============================================================================ */
@media (max-width: 1024px) {
    .chat-messages-container {
        padding: 24px 0;
    }
    .messages-wrapper {
        padding: 0 20px;
        max-width: 100%;
    }
    .message-content {
        max-width: 90% !important;
    }
    .message-bubble {
        padding: 18px 22px;
        font-size: 15.5px;
        line-height: 1.7;
    }
    .message-text {
        font-size: 15.5px;
        line-height: 1.7;
    }
    /* Assistant text readability on dark surface */
    .assistant-message .message-bubble {
        color: rgba(255, 255, 255, 0.95);
    }
    .assistant-message .message-text {
        color: rgba(255, 255, 255, 0.92);
    }
    .sidebar-header {
        padding: 16px;
    }
    /* Input area centered and full width on tablet */
    .chat-input-area-enhanced {
        padding-left: 20px;
        padding-right: 20px;
    }
    .chat-input-area-enhanced .input-wrapper-enhanced {
        max-width: 100%;
    }

    /* Code blocks — wrap on tablet too */
    .message-text pre {
        white-space: pre-wrap;
        word-break: break-word;
        max-width: 100%;
    }

    /* Images — constrain on tablet */
    .message-text img {
        max-width: 100%;
        height: auto;
    }

    /* Links — overflow containment */
    .message-text a {
        overflow-wrap: anywhere;
        word-break: break-word;
    }
}


/* ============================================================================
   3. MOBILE LAYOUT FOUNDATION  (< 768px)
   ============================================================================ */
@media screen and (max-width: 767px) {

    /* --- Force visibility of all key elements (beat animation opacity:0) --- */
    html body .chat-input-area-enhanced,
    html body .chat-messages-container,
    html body .chat-main,
    html body .chat-mobile-menu-toggle {
        display: flex !important;
        visibility: visible !important;
        opacity: 1 !important;
    }

    /* Force chat-view-wrapper visible on mobile (when not explicitly hidden).
       Prevents cinematic transition classes (transitioning-in, transitioned)
       from setting opacity:0 or transform:scale() on the outermost wrapper. */
    html body .chat-view-wrapper:not(.is-hidden) {
        display: flex !important;
        visibility: visible !important;
        opacity: 1 !important;
    }

    /* --- Allow .is-hidden to work on chat-main (for Chat History toggle) --- */
    html body .chat-main.is-hidden,
    html body.immersive-mode .chat-main.is-hidden,
    html body.chat-mode .chat-main.is-hidden,
    html body.chat-history-mode .chat-main.is-hidden {
        display: none !important;
        visibility: hidden !important;
        opacity: 0 !important;
    }

    /* NOTE: .chat-input-area-enhanced is a CHILD of .chat-main (not sibling),
       so hiding .chat-main already hides the input area.
       Chat-history-mode input hiding is handled in section 14 below. */

    /* --- Chat History / Sessions Content — mobile layout --- */
    html body #sessions-content,
    html body.immersive-mode #sessions-content,
    html body.chat-history-mode #sessions-content {
        flex: 1 1 0%;
        display: flex;
        flex-direction: column;
        width: 100%;
        max-width: 100%;
        min-height: 0;
        padding: var(--m-gap-md);
        padding-top: var(--m-pad-top);
        padding-bottom: var(--m-gap-lg);
        gap: var(--m-gap-md);
        overflow-y: auto;
        overflow-x: hidden;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
        background: var(--m-bg);
        box-sizing: border-box;
        z-index: var(--m-z-content);
    }

    /* Sessions content children — full width on mobile */
    html body #sessions-content > * {
        max-width: 100%;
        margin-left: 0;
        margin-right: 0;
    }

    /* Sessions header — compact on mobile */
    html body #sessions-content .sessions-header {
        flex-direction: column;
        text-align: center;
        padding: var(--m-gap-md);
        gap: var(--m-gap-sm);
        border-radius: var(--m-radius-md);
    }

    /* Stats grid — horizontal scroll on mobile */
    html body #sessions-content .sessions-stats {
        gap: var(--m-gap-sm);
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
        flex-wrap: nowrap;
    }
    html body #sessions-content .sessions-stats::-webkit-scrollbar {
        display: none;
    }
    html body #sessions-content .stat-item {
        min-width: 0;
        flex: 1 1 0%;
    }

    /* Session cards — full width stacked */
    html body #sessions-content #session-list {
        gap: var(--m-gap-sm);
    }
    html body #sessions-content .session-item {
        border-radius: var(--m-radius-md);
    }

    /* Force visibility when not hidden (beat animation/opacity issues) */
    html body #sessions-content:not(.is-hidden) {
        visibility: visible !important;
        opacity: 1 !important;
    }

    /* Force visibility on key layout elements (prevent animation opacity:0 flash).
       The visibility/opacity overrides guarantee elements are always visible
       even when entry animations start at opacity:0 with fill:forwards. */
    html body .messages-wrapper,
    html body .welcome-card-container,
    html body .welcome-card,
    html body .conversation-starters-container,
    html body .chat-message,
    html body .starters-grid,
    html body .starter-card,
    html body .starters-header,
    html body .welcome-content,
    html body .welcome-avatar-container,
    html body .message-bubble,
    html body .message-content,
    html body .message-text {
        visibility: visible !important;
        opacity: 1 !important;
    }

    /* Disable only heavy/decorative animations on mobile.
       Lightweight animations (messageEntrance, ripple) are preserved. */
    html body .welcome-card-container,
    html body .welcome-avatar-container,
    html body .avatar-glow-ring,
    html body .starters-grid,
    html body .starter-card,
    html body .starters-header {
        animation: none !important;
    }

    /* --- Messages wrapper layout --- */
    html body .messages-wrapper {
        display: flex !important;
        flex-direction: column !important;
    }
    html body .welcome-card-container {
        display: flex !important;
        justify-content: center !important;
    }
    html body .conversation-starters-container {
        display: flex !important;
        flex-direction: column !important;
    }

    /* --- Hide ambient background on mobile --- */
    .ambient-background {
        display: none;
    }

    /* === LAYOUT CHAIN === */

    /* Root wrapper — fixed fullscreen */
    html body .chat-view-wrapper {
        position: fixed;
        inset: 0;
        height: 100vh;   /* fallback for older browsers */
        height: 100dvh;  /* preferred — respects dynamic viewport on iOS */
        padding-top: 0;
        display: flex;
        flex-direction: column;
        overflow: visible;
        z-index: var(--z-feature-view, 400);
    }

    /* Chat container — fills wrapper.
       height: 100% explicitly overrides desktop height: calc(100% - 74px) from chat.css
       which leaks into mobile and can cause layout collapse in WebKit. */
    html body .modern-chat-container {
        position: relative;
        width: 100%;
        height: 100%;
        flex: 1;
        min-height: 0;
        display: flex;
        flex-direction: column;
        overflow: visible;
        background: var(--m-bg);
        -webkit-overflow-scrolling: touch;
    }

    /* Chat main area — flex child that fills container */
    html body .chat-main {
        flex: 1 1 0%;
        display: flex;
        flex-direction: column;
        width: 100%;
        min-height: 100px !important;
        position: relative;
        overflow: visible;
        z-index: var(--m-z-content);
    }


    /* ================================================================
       4. MOBILE HEADER BAR
       Hamburger button positioned below floating nav
       ================================================================ */

    html body .chat-mobile-menu-toggle {
        position: fixed;
        top: calc(var(--m-nav-h) + var(--m-safe-top) + 10px);
        left: 14px;
        right: auto;
        z-index: var(--m-z-menu);
        display: flex !important;
        align-items: center;
        justify-content: center;
        width: 44px;
        height: 44px;
        padding: 0;
        margin: 0;
        border: none;
        border-radius: var(--m-radius-md);
        background: rgba(15, 21, 41, 0.85);
        backdrop-filter: blur(12px);
        -webkit-backdrop-filter: blur(12px);
        color: var(--m-text);
        cursor: pointer;
        box-shadow: var(--m-shadow-md), 0 0 0 1px rgba(255,255,255,0.06);
        transition: all var(--m-fast);
        -webkit-tap-highlight-color: transparent;
        touch-action: manipulation;
    }

    html body .chat-mobile-menu-toggle i {
        font-size: 20px;
        transition: transform var(--m-fast);
    }

    html body .chat-mobile-menu-toggle:active {
        transform: scale(0.92);
        background: rgba(0, 242, 254, 0.15);
    }


    /* ================================================================
       5. SIDEBAR DRAWER
       Full-height slide-in from left with proper overlay
       ================================================================ */

    /* Hide desktop collapse button */
    html body .sidebar-collapse-btn {
        display: none !important;
    }

    /* Sidebar — off-canvas left */
    html body .chat-sidebar {
        position: fixed;
        top: 0;
        left: 0;
        bottom: 0;
        width: var(--m-sidebar-w);
        max-width: 85vw;
        z-index: var(--m-z-sidebar);
        transform: translateX(-100%);
        visibility: hidden;
        background: linear-gradient(180deg,
            rgba(15, 21, 48, 0.92) 0%,
            rgba(12, 16, 38, 0.95) 100%);
        backdrop-filter: blur(12px) saturate(160%);
        -webkit-backdrop-filter: blur(12px) saturate(160%);
        border-right: 1px solid rgba(0, 242, 254, 0.1);
        box-shadow:
            var(--m-shadow-lg),
            4px 0 24px rgba(0, 0, 0, 0.2);
        transition: transform var(--m-base), visibility 0s linear 250ms;
        overflow-y: auto;
        overflow-x: hidden;
        overscroll-behavior: contain;
        -webkit-overflow-scrolling: touch;
    }

    html body .chat-sidebar.open {
        transform: translateX(0);
        visibility: visible;
        transition: transform var(--m-base), visibility 0s linear 0s;
    }

    /* Overlay — uses the actual HTML .sidebar-overlay element */
    html body .sidebar-overlay {
        display: none;
        position: fixed;
        inset: 0;
        z-index: calc(var(--m-z-sidebar) - 1);
        background: rgba(0, 0, 0, 0.35);
        opacity: 0;
        transition: opacity var(--m-base);
    }

    html body .chat-sidebar.open ~ .sidebar-overlay,
    html body .sidebar-overlay.active {
        display: block;
        opacity: 1;
    }

    /* Force mobile z-index even in immersive mode */
    html body.immersive-mode .chat-sidebar {
        z-index: var(--m-z-sidebar) !important;
    }

    html body.immersive-mode .chat-sidebar.open {
        z-index: var(--m-z-sidebar) !important;
    }

    /* Animated glow accent line on right edge (matches desktop) */
    html body .chat-sidebar::before {
        content: '' !important;
        display: block !important;
        position: absolute;
        top: 0;
        right: 0;
        width: 2px;
        height: 100%;
        background: linear-gradient(180deg,
            rgba(0, 242, 254, 0.5) 0%,
            rgba(118, 75, 162, 0.45) 50%,
            rgba(0, 242, 254, 0.35) 100%);
        z-index: 10;
        animation: sidebarGlow 4s ease-in-out infinite;
        pointer-events: none;
    }

    /* Secondary soft glow bleed (matches desktop ::after) */
    html body .chat-sidebar::after {
        content: '';
        position: absolute;
        top: 0;
        right: 1px;
        width: 6px;
        height: 100%;
        background: linear-gradient(180deg,
            rgba(0, 242, 254, 0.08) 0%,
            rgba(118, 75, 162, 0.06) 50%,
            rgba(0, 242, 254, 0.04) 100%);
        filter: blur(3px);
        opacity: 0.6;
        z-index: 9;
        pointer-events: none;
    }

    /* --- Sidebar internals --- */
    html body .chat-sidebar .sidebar-header {
        padding: var(--m-gap-md);
        border-bottom: 1px solid rgba(0, 242, 254, 0.08);
        background: rgba(0, 0, 0, 0.15);
    }

    html body .chat-sidebar .new-chat-btn {
        width: 100%;
        padding: 12px 16px;
        font-size: 0.9rem;
        border-radius: var(--m-radius-md);
        display: flex;
        align-items: center;
        gap: var(--m-gap-sm);
        background: linear-gradient(135deg, #764ba2 0%, #667eea 50%, #00f2fe 100%);
        background-size: 200% 200%;
        animation: immersiveGradientShift 3s ease infinite;
        border: none;
        color: white;
        font-weight: 600;
        box-shadow:
            0 4px 15px rgba(118, 75, 162, 0.3),
            0 0 20px rgba(0, 242, 254, 0.08);
    }

    html body .chat-sidebar .sidebar-service-switcher {
        padding: 12px var(--m-gap-md);
        border-bottom: 1px solid rgba(0, 242, 254, 0.06);
    }

    html body .chat-sidebar .service-tab {
        padding: 8px 12px;
        font-size: 0.85rem;
        gap: 6px;
        border-radius: var(--m-radius-sm);
    }

    html body .chat-sidebar .service-tab span {
        display: none;
    }

    html body .chat-sidebar .service-tab i {
        font-size: 1rem;
    }

    html body .chat-sidebar .sidebar-back-btn {
        padding: 10px 14px;
        font-size: 0.85rem;
        margin-top: 8px;
        border-radius: var(--m-radius-sm);
    }

    html body .chat-sidebar .sidebar-nav {
        padding: var(--m-gap-md) 0;
    }

    html body .chat-sidebar .nav-section-title {
        padding: 0 var(--m-gap-md) var(--m-gap-sm);
        font-size: 0.7rem;
        text-transform: uppercase;
        letter-spacing: 1.5px;
        color: var(--m-text-dim);
    }

    html body .chat-sidebar .nav-item {
        padding: 12px var(--m-gap-md);
        font-size: 0.9rem;
        min-height: var(--m-tap);
        display: flex;
        align-items: center;
        gap: var(--m-gap-sm);
        transition: background var(--m-fast);
        border-radius: 0;
    }

    html body .chat-sidebar .nav-item:active {
        background: rgba(0, 242, 254, 0.08);
    }

    html body .chat-sidebar .nav-item.active {
        background: rgba(0, 242, 254, 0.06);
        border-left: 3px solid var(--m-accent);
    }

    html body .chat-sidebar .sidebar-sessions {
        padding: var(--m-gap-md);
        border-top: 1px solid rgba(0, 242, 254, 0.06);
    }

    html body .chat-sidebar .sidebar-sessions-header {
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-bottom: var(--m-gap-sm);
    }

    html body .chat-sidebar .sidebar-sessions-title {
        font-size: 0.8rem;
        color: var(--m-text-muted);
    }

    html body .chat-sidebar .chat-item {
        padding: 12px var(--m-gap-sm);
        min-height: var(--m-tap);
        border-radius: var(--m-radius-sm);
        transition: background var(--m-fast);
    }

    html body .chat-sidebar .chat-item:active {
        background: rgba(0, 242, 254, 0.06);
    }

    html body .chat-sidebar .chat-title {
        font-size: 0.85rem;
        color: var(--m-text);
    }

    html body .chat-sidebar .chat-preview {
        font-size: 0.75rem;
        color: var(--m-text-muted);
    }


    /* ================================================================
       6. CHAT MESSAGES AREA
       Scrollable container between header and input
       ================================================================ */

    html body .chat-messages-container {
        flex: 1;
        min-height: 200px;
        overflow-y: auto;
        overflow-x: hidden;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
        scroll-behavior: smooth;

        /* Clearance for fixed top nav + fixed bottom input */
        padding-top: var(--m-pad-top);
        padding-bottom: var(--m-pad-bottom);
        padding-left: var(--m-gap-md);
        padding-right: var(--m-gap-md);

        display: flex;
        flex-direction: column;
        gap: 8px;
        z-index: var(--m-z-messages);
    }

    /* Thin scrollbar */
    html body .chat-messages-container::-webkit-scrollbar {
        width: 3px;
    }
    html body .chat-messages-container::-webkit-scrollbar-track {
        background: transparent;
    }
    html body .chat-messages-container::-webkit-scrollbar-thumb {
        background: rgba(0, 242, 254, 0.2);
        border-radius: 2px;
    }

    /* Messages wrapper */
    html body .messages-wrapper {
        display: flex;
        flex-direction: column;
        gap: var(--m-gap-md);
        padding: 0;
        min-height: 200px;
        flex: 1;
        width: 100%;
    }

    /* Center welcome content vertically when no chat messages exist */
    html body .messages-wrapper:has(.welcome-card-container):not(:has(.chat-message)) {
        justify-content: center;
    }

    /* Loading fallback */
    html body .messages-wrapper:empty::before {
        content: '';
        display: flex;
        justify-content: center;
        align-items: center;
        min-height: 200px;
    }

    /* --- Message bubbles --- */
    html body .chat-message {
        display: flex;
        align-items: flex-start;
        gap: 10px;
        max-width: 100%;
        margin-bottom: 4px;
        opacity: 1;
        visibility: visible;
    }

    /* Avatar */
    html body .message-avatar {
        width: var(--m-avatar);
        height: var(--m-avatar);
        min-width: var(--m-avatar);
        border-radius: 50%;
        flex-shrink: 0;
        overflow: hidden;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    html body .message-avatar.user-avatar {
        background: var(--m-gradient-user);
        color: white;
        font-size: 13px;
        font-weight: 600;
    }

    html body .message-avatar.assistant-avatar {
        background: var(--chat-bg-tertiary, rgba(15, 23, 42, 0.9));
        border: 1px solid var(--chat-glass-border-hover, rgba(0, 242, 254, 0.3));
        padding: 4px;
    }

    html body .message-avatar img {
        width: 100%;
        height: 100%;
        object-fit: cover;
    }

    /* Message content */
    html body .message-content {
        display: flex;
        flex-direction: column;
        max-width: calc(100% - 50px);
        min-width: 0;
    }

    /* Message bubble — premium glass design */
    html body .message-bubble {
        padding: 14px 16px;
        border-radius: var(--m-radius-lg);
        word-wrap: break-word;
        overflow-wrap: break-word;
        line-height: 1.6;
        font-size: 15px;
        position: relative;
        letter-spacing: 0.1px;
    }

    /* User bubble — Phase 1: restrained branded tint (matches desktop calm surface) */
    html body .user-message .message-bubble {
        background: linear-gradient(135deg, rgba(102, 126, 234, 0.20) 0%, rgba(118, 75, 162, 0.22) 100%);
        color: white;
        border: 1px solid rgba(118, 75, 162, 0.32);
        border-bottom-right-radius: 6px;
        box-shadow: 0 2px 12px rgba(0, 0, 0, 0.22);
    }

    /* Assistant bubble — Phase 1: calm text on the canvas (no bubble) */
    html body .assistant-message .message-bubble {
        background: transparent;
        color: rgba(255, 255, 255, 0.92);
        border: none;
        box-shadow: none;
        padding: 2px 0 0;
        border-radius: 0;
    }

    /* Phase 1: bubble tails removed — calm surface + restrained user bubble use
       an asymmetric radius (a notch), not speech-bubble tails. */
    html body .user-message .message-bubble::after,
    html body .assistant-message .message-bubble::after {
        display: none;
    }

    /* Timestamp */
    html body .message-time {
        font-size: 11px;
        color: var(--m-text-dim);
        margin-top: 4px;
        padding: 0 4px;
        display: flex;
        align-items: center;
        gap: 4px;
    }
    html body .user-message .message-time {
        justify-content: flex-end;
    }
    html body .assistant-message .message-time {
        justify-content: flex-start;
    }

    /* Message text */
    html body .message-text {
        font-size: 15px;
        line-height: 1.7;
        color: var(--chat-text-secondary);
        word-break: break-word;
    }

    /* Headings in AI responses */
    html body .assistant-message .message-text h1,
    html body .assistant-message .message-text h2,
    html body .assistant-message .message-text h3,
    html body .assistant-message .message-text h4 {
        color: #ffffff;
        margin-top: 1em;
        margin-bottom: 0.5em;
        line-height: 1.4;
    }

    /* Paragraphs in AI responses */
    html body .assistant-message .message-text p {
        margin-bottom: 0.75em;
        line-height: 1.7;
    }

    /* Lists in AI responses */
    html body .assistant-message .message-text ul,
    html body .assistant-message .message-text ol {
        padding-left: 1.25em;
        margin-bottom: 0.75em;
    }

    html body .assistant-message .message-text li {
        margin-bottom: 0.4em;
        line-height: 1.65;
    }

    /* Bold text stand out more */
    html body .assistant-message .message-text strong,
    html body .assistant-message .message-text b {
        color: #ffffff;
        font-weight: 700;
    }

    /* Code blocks (shared) */
    html body .message-text pre,
    html body .message-text code {
        font-size: 13px;
        border-radius: var(--m-radius-sm);
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    /* Pre blocks — mobile-specific */
    html body .message-text pre {
        padding: 12px;
        margin: 10px 0;
        max-width: 100%;
        white-space: pre-wrap;
        word-break: break-word;
    }

    /* Inline code — prevent long tokens from overflowing */
    html body .message-text code {
        overflow-wrap: anywhere;
        word-break: break-word;
    }

    /* Pre > code reset (don't double-wrap) */
    html body .message-text pre code {
        white-space: pre-wrap;
        word-break: break-word;
        overflow-wrap: anywhere;
    }

    /* ---- Blockquotes in AI responses ---- */
    html body .message-text blockquote {
        border-left: 3px solid rgba(0, 242, 254, 0.35);
        padding: 8px 0 8px 12px;
        margin: 10px 0;
        background: rgba(0, 242, 254, 0.04);
        border-radius: 0 6px 6px 0;
        color: rgba(255, 255, 255, 0.85);
        font-style: italic;
        font-size: 14px;
        line-height: 1.6;
    }

    html body .message-text blockquote p {
        margin-bottom: 0.5em;
    }

    html body .message-text blockquote p:last-child {
        margin-bottom: 0;
    }

    /* Nested blockquotes */
    html body .message-text blockquote blockquote {
        margin-left: 0;
        border-left-color: rgba(0, 242, 254, 0.2);
    }

    /* ---- Images in AI responses ---- */
    html body .message-text img {
        max-width: 100%;
        height: auto;
        border-radius: 6px;
        margin: 8px 0;
        display: block;
    }

    /* ---- Horizontal rules ---- */
    html body .message-text hr {
        border: none;
        border-top: 1px solid rgba(255, 255, 255, 0.1);
        margin: 12px 0;
    }

    /* ---- h5/h6 in AI responses ---- */
    html body .assistant-message .message-text h5 {
        font-size: 0.9rem;
        font-weight: 600;
        color: #ffffff;
        margin-top: 0.8em;
        margin-bottom: 0.4em;
    }

    html body .assistant-message .message-text h6 {
        font-size: 0.8rem;
        font-weight: 600;
        color: rgba(255, 255, 255, 0.85);
        margin-top: 0.8em;
        margin-bottom: 0.4em;
        text-transform: uppercase;
        letter-spacing: 0.5px;
    }

    /* ---- Nested lists — cap indentation for mobile ---- */
    html body .message-text ul ul,
    html body .message-text ol ol,
    html body .message-text ul ol,
    html body .message-text ol ul {
        padding-left: 1em;
        margin-bottom: 0.2em;
        margin-top: 0.2em;
    }

    html body .message-text ul ul ul,
    html body .message-text ol ol ol {
        padding-left: 0.75em;
    }

    /* ---- Links — prevent long URL overflow ---- */
    html body .message-text a {
        overflow-wrap: anywhere;
        word-break: break-word;
    }

    /* ---- Tables in AI responses ---- */
    /* Make the message bubble allow overflow so the table can scroll */
    html body .assistant-message .message-bubble:has(table) {
        overflow: visible;
        padding-left: 0;
        padding-right: 0;
    }

    /* Scrollable table wrapper */
    html body .message-text table,
    html body .message-bubble table {
        display: block;
        width: 100%;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        margin: 0.75em 0;
        border-collapse: separate;
        border-spacing: 0;
        border: 1px solid rgba(148, 163, 184, 0.15);
        border-radius: var(--m-radius-sm);
        font-size: 13.5px;
        line-height: 1.5;
    }

    html body .message-text thead,
    html body .message-bubble thead,
    html body .message-text tbody,
    html body .message-bubble tbody {
        display: table;
        width: 100%;
        table-layout: auto;
    }

    html body .message-text tr,
    html body .message-bubble tr {
        display: table-row;
    }

    html body .message-text th,
    html body .message-bubble th {
        background: rgba(0, 180, 220, 0.15);
        color: #ffffff;
        font-weight: 700;
        font-size: 13px;
        text-transform: uppercase;
        letter-spacing: 0.5px;
        padding: 10px 14px;
        text-align: left;
        white-space: nowrap;
        border-bottom: 1px solid rgba(0, 180, 220, 0.25);
    }

    html body .message-text td,
    html body .message-bubble td {
        padding: 10px 14px;
        color: rgba(255, 255, 255, 0.9);
        border-bottom: 1px solid rgba(255, 255, 255, 0.06);
        white-space: normal;
        min-width: 100px;
        word-break: normal;
    }

    /* Alternating row stripes for readability */
    html body .message-text tr:nth-child(even),
    html body .message-bubble tr:nth-child(even) {
        background: rgba(255, 255, 255, 0.03);
    }

    /* Scroll hint shadow on right side */
    html body .assistant-message .message-bubble:has(table)::before {
        content: none;
    }

    /* Links in messages */
    html body .message-text a {
        color: var(--m-accent);
        text-decoration: underline;
        text-decoration-color: rgba(0, 242, 254, 0.35);
        text-underline-offset: 2px;
    }

    /* Message grouping — tighter spacing for consecutive same-sender */
    html body .user-message + .user-message,
    html body .assistant-message + .assistant-message {
        margin-top: -2px;
    }
    html body .user-message + .user-message .message-bubble::after,
    html body .assistant-message + .assistant-message .message-bubble::after {
        display: none;
    }
    html body .user-message + .user-message .message-bubble {
        border-top-right-radius: 6px;
    }
    html body .assistant-message + .assistant-message .message-bubble {
        border-top-left-radius: 6px;
    }

    /* Follow-up questions */
    html body .follow-up-questions-list {
        gap: 6px;
    }
    html body .follow-up-question-btn {
        padding: 10px 14px;
        font-size: 0.8rem;
        border-radius: var(--m-radius-sm);
        min-height: var(--m-tap);
    }


    /* ================================================================
       7. WELCOME CARD — Premium centered hero
       ================================================================ */

    html body .welcome-card-container {
        padding: var(--m-gap-lg) var(--m-gap-md) var(--m-gap-md);
        display: flex !important;
        justify-content: center;
    }

    html body .welcome-card {
        max-width: 100%;
        width: 100%;
        padding: var(--m-gap-lg) 20px;
        border-radius: var(--m-radius-xl);
        border: 1px solid rgba(255, 255, 255, 0.07);
        background: linear-gradient(145deg,
            rgba(20, 26, 50, 0.9) 0%,
            rgba(12, 17, 38, 0.94) 100%);
        box-shadow:
            var(--m-shadow-md),
            inset 0 1px 0 rgba(255, 255, 255, 0.04);
    }

    html body .welcome-avatar-container {
        width: 60px;
        height: 60px;
        margin: 0 auto 14px;
    }

    html body .welcome-avatar {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        box-shadow:
            0 6px 22px rgba(0, 180, 220, 0.18),
            0 0 0 1px rgba(255, 255, 255, 0.07);
    }

    html body .avatar-glow-ring {
        inset: -5px;
        border-width: 2px;
    }

    html body .welcome-content {
        text-align: center;
    }

    html body .welcome-title {
        font-size: 1.3rem;
        font-weight: 700;
        margin: 0 0 10px;
        line-height: 1.3;
        color: #ffffff;
        -webkit-text-fill-color: #ffffff;
    }

    html body .welcome-description {
        font-size: 0.88rem;
        line-height: 1.65;
        margin: 0 0 16px;
        color: var(--m-text-secondary);
    }

    html body .welcome-badges {
        display: flex;
        gap: 8px;
        justify-content: center;
        flex-wrap: wrap;
    }

    html body .welcome-badge {
        padding: 7px 12px;
        font-size: 0.72rem;
        border-radius: 9px;
        white-space: nowrap;
        background: rgba(255, 255, 255, 0.03);
        border: 1px solid rgba(255, 255, 255, 0.08);
        color: var(--m-text-secondary);
    }

    html body .welcome-badge i {
        font-size: 0.68rem;
        margin-right: 4px;
        color: var(--m-accent);
    }


    /* ================================================================
       8. CONVERSATION STARTERS — touch-friendly cards
       ================================================================ */

    html body .conversation-starters-container {
        padding: 0 0 var(--m-gap-md);
        gap: var(--m-gap-md);
        max-width: 100%;
    }

    html body .starters-header {
        padding: 0 4px;
        gap: 10px;
        display: flex;
        align-items: center;
    }

    html body .starters-icon {
        font-size: 1.2rem;
    }

    html body .starters-title {
        font-size: 0.9rem;
        font-weight: 600;
        color: var(--m-text-secondary);
    }

    /* Stack vertically */
    html body .starters-grid {
        display: flex !important;
        flex-direction: column !important;
        gap: 10px;
    }

    html body .starter-card {
        display: flex;
        align-items: center;
        gap: 14px;
        padding: 13px 16px;
        min-height: var(--m-tap);
        background: rgba(255, 255, 255, 0.025);
        border: 1px solid rgba(255, 255, 255, 0.07);
        border-radius: var(--m-radius-md);
        cursor: pointer;
        -webkit-tap-highlight-color: transparent;
        touch-action: manipulation;
        transition: background 0.18s ease, border-color 0.18s ease, transform 0.18s ease;
    }

    html body .starter-card:active {
        transform: scale(0.98);
        background: rgba(255, 255, 255, 0.05);
        border-color: rgba(0, 242, 254, 0.25);
    }

    /* No hover effects on touch */
    html body .starter-card:hover {
        transform: none;
        box-shadow: none;
    }

    /* Emoji in a calm tinted chip (matches desktop) */
    html body .starter-icon {
        font-size: 1.05rem;
        flex-shrink: 0;
        width: 34px;
        height: 34px;
        display: flex;
        align-items: center;
        justify-content: center;
        background: rgba(0, 242, 254, 0.08);
        border-radius: 9px;
    }

    /* Hide the desktop hover chevron on touch */
    html body .starter-card::after { display: none; }

    html body .starter-text {
        font-size: 0.84rem;
        line-height: 1.5;
        color: var(--m-text);
        font-weight: 500;
    }

    html body .starter-ripple {
        position: absolute;
        width: 200px;
        height: 200px;
        border-radius: 50%;
        background: rgba(0, 242, 254, 0.25);
        transform: scale(0);
        pointer-events: none;
    }

    html body .starter-card.ripple-active .starter-ripple {
        animation: m-ripple 0.6s ease-out forwards;
    }

    @keyframes m-ripple {
        0% { transform: scale(0); opacity: 0.6; }
        100% { transform: scale(2.5); opacity: 0; }
    }


    /* ================================================================
       9. INPUT AREA — fixed bottom glass bar
       ================================================================ */

    html body .chat-input-area-enhanced {
        display: flex !important;
        visibility: visible !important;
        opacity: 1 !important;
        position: fixed !important;
        bottom: 0 !important;
        left: 0 !important;
        right: 0 !important;
        width: 100%;
        z-index: var(--m-z-input) !important;
        padding: 10px 14px;
        padding-bottom: max(14px, var(--m-safe-bottom));
        background: linear-gradient(to top,
            rgba(10, 14, 31, 0.99) 0%,
            rgba(10, 14, 31, 0.96) 70%,
            rgba(10, 14, 31, 0.88) 100%);
        backdrop-filter: blur(16px) saturate(160%);
        -webkit-backdrop-filter: blur(16px) saturate(160%);
        border-top: 1px solid rgba(0, 242, 254, 0.08);
        box-shadow: 0 -2px 20px rgba(0, 0, 0, 0.3);
    }

    /* Glass container */
    html body .chat-input-area-enhanced .input-container-glass {
        background: transparent;
        border: none;
        padding: 0;
        margin: 0;
        box-shadow: none;
        max-width: 100%;
        width: 100%;
    }

    /* Input wrapper — pill shape */
    html body .chat-input-area-enhanced .input-wrapper-enhanced {
        display: flex;
        align-items: flex-end;
        gap: var(--m-gap-sm);
        background: var(--m-bg-elevated);
        border: 1px solid rgba(148, 163, 184, 0.15);
        border-radius: var(--m-radius-pill);
        padding: 4px 4px 4px 16px;
        transition: border-color var(--m-fast), box-shadow var(--m-fast);
    }

    html body .chat-input-area-enhanced .input-wrapper-enhanced:focus-within {
        border-color: var(--m-accent);
        box-shadow: 0 0 0 3px rgba(0, 242, 254, 0.08);
    }

    /* Textarea */
    html body .chat-input-area-enhanced .chat-input-enhanced {
        flex: 1;
        min-height: 42px;
        max-height: 120px;
        padding: 10px 0;
        background: transparent;
        border: none;
        color: var(--m-text);
        font-size: 16px; /* Prevents iOS zoom */
        line-height: 1.5;
        font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
        resize: none;
        outline: none;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        -webkit-appearance: none;
    }

    html body .chat-input-area-enhanced .chat-input-enhanced::placeholder {
        color: var(--m-text-dim);
        font-size: 14px;
    }

    /* Send button — circular gradient */
    html body .chat-input-area-enhanced .send-button-enhanced {
        position: relative;
        width: 42px;
        height: 42px;
        min-width: 42px;
        min-height: 42px;
        padding: 0;
        display: flex;
        align-items: center;
        justify-content: center;
        background: linear-gradient(135deg, #00f2fe, #58b9ff);
        border: none;
        border-radius: 50%;
        color: #04121a;
        cursor: pointer;
        box-shadow: 0 2px 10px rgba(0, 242, 254, 0.28);
        transition: all 200ms cubic-bezier(0.4, 0, 0.2, 1);
        -webkit-tap-highlight-color: transparent;
        touch-action: manipulation;
        flex-shrink: 0;
        overflow: hidden;
    }

    html body .chat-input-area-enhanced .send-button-enhanced:active:not(:disabled) {
        transform: scale(0.9);
        box-shadow: 0 1px 6px rgba(0, 242, 254, 0.45);
    }

    html body .chat-input-area-enhanced .send-button-enhanced:disabled {
        opacity: 0.35;
        cursor: not-allowed;
        background: linear-gradient(135deg, #4a5568 0%, #2d3748 100%);
        box-shadow: none;
    }

    html body .chat-input-area-enhanced .send-button-enhanced i {
        font-size: 15px;
    }

    /* Hide desktop-only elements */
    html body .chat-input-area-enhanced .input-hint {
        display: none;
    }
    html body .chat-input-area-enhanced .send-button-glow {
        display: none;
    }




    /* ================================================================
       10b. SCROLL-TO-BOTTOM FAB
       Appears when user scrolls up in chat
       ================================================================ */

    html body .scroll-to-bottom-fab {
        position: fixed;
        bottom: calc(var(--m-input-h) + var(--m-safe-bottom) + 20px);
        right: 16px;
        z-index: calc(var(--m-z-input) - 1);
        width: 44px;
        height: 44px;
        border-radius: 50%;
        border: none;
        background: linear-gradient(135deg, rgba(15, 21, 41, 0.95) 0%, rgba(10, 14, 31, 0.98) 100%);
        border: 1px solid rgba(0, 242, 254, 0.2);
        color: var(--m-accent);
        font-size: 16px;
        cursor: pointer;
        display: flex;
        align-items: center;
        justify-content: center;
        box-shadow:
            0 4px 16px rgba(0, 0, 0, 0.4),
            0 0 20px rgba(0, 242, 254, 0.1);
        backdrop-filter: blur(12px);
        -webkit-backdrop-filter: blur(12px);
        -webkit-tap-highlight-color: transparent;
        touch-action: manipulation;
        transition: all var(--m-base);
        transform: translateY(80px);
        opacity: 0;
        pointer-events: none;
    }

    html body .scroll-to-bottom-fab.visible {
        transform: translateY(0);
        opacity: 1;
        pointer-events: auto;
    }

    html body .scroll-to-bottom-fab:active {
        transform: scale(0.9);
        background: rgba(0, 242, 254, 0.15);
    }

    /* Unread message count badge */
    html body .scroll-to-bottom-fab .fab-badge {
        position: absolute;
        top: -4px;
        right: -4px;
        min-width: 18px;
        height: 18px;
        border-radius: 9px;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: white;
        font-size: 10px;
        font-weight: 700;
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 0 4px;
        opacity: 0;
        transform: scale(0);
        transition: all 0.2s ease;
    }

    html body .scroll-to-bottom-fab .fab-badge.has-count {
        opacity: 1;
        transform: scale(1);
    }


    /* ================================================================
       10c. iOS VIRTUAL KEYBOARD HANDLING
       The input area adjusts when keyboard is open
       ================================================================ */

    html body .chat-input-area-enhanced.keyboard-open {
        /* Position is dynamically set via JS visualViewport API */
        transition: bottom 0.1s ease;
    }


    /* ================================================================
       11. RESPONSIVE TWEAKS
       ================================================================ */

    /* --- Responsive tweaks --- */
    .ambient-orb {
        animation-duration: 40s;
        filter: blur(15px);
        opacity: 0.3;
    }

    /* --- Touch device optimization --- */
    @media (hover: none) and (pointer: coarse) {
        /* Fix double-tap issue: force chat actions visible (beats .chat-item-header .chat-actions opacity:0) */
        .chat-actions,
        .chat-item-header .chat-actions,
        .session-card-actions {
            opacity: 1 !important;
            transform: none !important;
        }

        /* Remove hover transforms on touch devices */
        .starter-card:hover,
        .follow-up-question-btn:hover,
        .new-chat-btn:hover,
        .send-button-enhanced:hover:not(:disabled),
        .chat-item:hover,
        .session-card:hover,
        .stat-item:hover,
        .nav-item:hover {
            transform: none;
        }

        /* Active states for touch feedback */
        .starter-card:active { background: rgba(0, 242, 254, 0.06); }
        .new-chat-btn:active { transform: scale(0.98); }
        .send-button-enhanced:active:not(:disabled) { transform: scale(0.92); }
        .chat-item:active { background: rgba(0, 242, 254, 0.08); }
        .session-card:active { transform: scale(0.98); }
    }

    /* Eliminate 300ms tap delay on interactive sidebar elements */
    html body .chat-sidebar .chat-item,
    html body .chat-sidebar .nav-item,
    html body .chat-sidebar .new-chat-btn,
    html body .chat-sidebar .sidebar-back-btn,
    html body .session-card {
        touch-action: manipulation;
        -webkit-tap-highlight-color: transparent;
    }
}

/* Small phones (≤ 374px) */
@media screen and (max-width: 374px) {
    :root {
        --m-header-h: 52px;
        --m-avatar: 28px;
    }
    html body .message-bubble {
        font-size: 14px;
        padding: 12px 14px;
    }
    html body .message-content {
        max-width: calc(100% - 46px);
    }
    html body .chat-mobile-menu-toggle {
        width: 44px;
        height: 44px;
        left: 10px;
    }

    /* Tighter typography for small screens */
    html body .assistant-message .message-text h1 {
        font-size: 1.25rem;
    }
    html body .assistant-message .message-text h2 {
        font-size: 1.1rem;
    }
    html body .assistant-message .message-text h3 {
        font-size: 1rem;
    }
    html body .message-text pre {
        padding: 10px;
        font-size: 12px;
    }
    html body .message-text blockquote {
        padding: 6px 0 6px 10px;
        font-size: 13px;
    }
    html body .message-text table {
        font-size: 12.5px;
    }
    html body .message-text th,
    html body .message-text td {
        padding: 8px 10px;
    }

}

/* Large phones (414-767px) */
@media screen and (min-width: 414px) and (max-width: 767px) {
    html body .message-content {
        max-width: calc(100% - 50px);
    }
    html body .message-bubble {
        font-size: 15.5px;
        padding: 14px 18px;
    }
}

/* Landscape */
@media screen and (max-width: 767px) and (orientation: landscape) {
    :root {
        --m-header-h: 48px;
        --m-input-h: 56px;
        --m-pad-top: calc(52px + var(--m-safe-top));
        --m-pad-bottom: calc(64px + var(--m-safe-bottom));
    }
    html body .chat-messages-container {
        padding-top: var(--m-pad-top);
        padding-bottom: var(--m-pad-bottom);
        padding-left: 12px;
        padding-right: 12px;
    }
    html body .chat-input-area-enhanced {
        padding: 8px 14px;
        padding-bottom: max(8px, var(--m-safe-bottom));
    }
    html body .chat-input-area-enhanced .input-wrapper-enhanced {
        padding: 3px 3px 3px 14px;
    }
    html body .chat-input-area-enhanced .chat-input-enhanced {
        min-height: 36px;
        max-height: 60px;
        padding: 6px 0;
        font-size: 15px;
    }
    html body .chat-input-area-enhanced .send-button-enhanced {
        width: 44px;
        height: 44px;
        min-width: 44px;
        min-height: 44px;
    }
}

/* (Floating nav adjustments handled in floating-nav.css) */


/* ============================================================================
   12. ACCESSIBILITY
   ============================================================================ */
@media screen and (max-width: 767px) {
    /* Focus visible */
    html body button:focus-visible,
    html body .chat-input-enhanced:focus-visible,
    html body .starter-card:focus-visible,
    html body .nav-item:focus-visible,
    html body a:focus-visible {
        outline: 2px solid var(--m-accent);
        outline-offset: 2px;
        box-shadow: 0 0 0 4px rgba(0, 242, 254, 0.15);
    }

    /* Remove focus ring for touch */
    html body button:focus:not(:focus-visible),
    html body .chat-input-enhanced:focus:not(:focus-visible),
    html body .starter-card:focus:not(:focus-visible) {
        outline: none;
        box-shadow: none;
    }

    /* Touch target enforcement — 44×44px minimum per WCAG 2.5.5 */
    html body .chat-mobile-menu-toggle,
    html body .send-button-enhanced,
    html body .starter-card,
    html body .floating-nav-back,
    html body .floating-nav-tab,
    html body .nav-item,
    html body .chat-action-btn,
    html body .sidebar-sessions-refresh,
    html body .sidebar-session-delete,
    html body .session-menu-btn {
        min-width: 44px;
        min-height: 44px;
    }

    /* Screen reader only */
    .sr-only,
    .visually-hidden {
        position: absolute;
        width: 1px;
        height: 1px;
        padding: 0;
        margin: -1px;
        overflow: hidden;
        clip: rect(0, 0, 0, 0);
        white-space: nowrap;
        border: 0;
    }

    /* Allow text selection in messages */
    .message-bubble {
        -webkit-user-select: text;
        user-select: text;
    }

    /* Copy buttons — always visible on mobile (no hover) */
    html body .message-copy-btn {
        opacity: 0.6;
        width: 36px;
        height: 36px;
        font-size: 14px;
        touch-action: manipulation;
        -webkit-tap-highlight-color: transparent;
    }

    html body .code-copy-btn {
        opacity: 0.6;
        width: 34px;
        height: 34px;
        touch-action: manipulation;
        -webkit-tap-highlight-color: transparent;
    }

    html body .message-action-menu {
        right: 4px;
        bottom: calc(100% + 4px);
    }

    html body .message-action-btn {
        min-height: 44px;
        padding: 10px 14px;
    }

    /* Reduced motion */
    @media (prefers-reduced-motion: reduce) {
        html body .chat-message,
        html body .welcome-card-container,
        html body .conversation-starters-container {
            animation: none !important;
            transition: none !important;
        }
        html body .send-button-enhanced {
            animation: none !important;
        }
        html body .typing-dots span {
            animation: none !important;
            opacity: 1;
        }
    }

    /* High contrast */
    @media (prefers-contrast: high) {
        html body .message-bubble {
            border: 2px solid currentColor;
        }
        html body .user-message .message-bubble {
            background: #4a0080;
        }
        html body .assistant-message .message-bubble {
            background: #1a1a2e;
        }
        html body .chat-input-area-enhanced {
            border-top: 2px solid #ffffff;
        }
        html body .input-wrapper-enhanced {
            border: 2px solid #ffffff;
        }
        html body .starter-card {
            border: 2px solid #ffffff;
        }
    }

    /* Forced colors (Windows HC) */
    @media (forced-colors: active) {
        html body .send-button-enhanced {
            forced-color-adjust: none;
            background: ButtonFace;
            border: 2px solid ButtonText;
        }
        html body .message-bubble {
            forced-color-adjust: none;
            border: 2px solid CanvasText;
        }
    }
}


/* ============================================================================
   13. PERFORMANCE
   ============================================================================ */
@media screen and (max-width: 767px) {
    /* Optimized will-change — only applied during sidebar transition.
       Permanent will-change wastes GPU memory when sidebar is closed.
       JS should add .sidebar-transitioning class during open/close. */
    html body .chat-sidebar.sidebar-transitioning {
        will-change: transform;
    }
    html body .chat-sidebar:not(.sidebar-transitioning) {
        will-change: auto;
    }
    html body .chat-messages-container {
        will-change: scroll-position;
    }

    /* Backdrop filter fallback for unsupported browsers */
    @supports not (backdrop-filter: blur(1px)) {
        html body .chat-input-area-enhanced {
            background: rgba(10, 14, 31, 0.99);
        }
        html body .chat-mobile-menu-toggle {
            background: rgba(15, 21, 41, 0.98);
        }
    }

    /* Reduce backdrop-filter blur radius on mobile for better performance
       on low-end devices. Desktop uses 20-30px; 10px is visually similar
       but significantly cheaper to composite. */
    html body .chat-input-area-enhanced,
    html body .chat-mobile-menu-toggle,
    html body .chat-sidebar {
        backdrop-filter: blur(10px);
        -webkit-backdrop-filter: blur(10px);
    }

    /* Disable backdrop-filter entirely when user prefers reduced motion */
    @media (prefers-reduced-motion: reduce) {
        html body .chat-input-area-enhanced,
        html body .chat-mobile-menu-toggle,
        html body .chat-sidebar {
            backdrop-filter: none;
            -webkit-backdrop-filter: none;
        }
    }
}


/* ============================================================================
   14. IMMERSIVE MODE — MOBILE OVERRIDES
   ============================================================================
   These rules use html body.immersive-mode prefix (specificity 0,2,2)
   to beat immersive-mode.css body.immersive-mode rules (specificity 0,2,1).
   ============================================================================ */
@media screen and (max-width: 767px) {

    /* chat-view-wrapper — override desktop immersive 100vh, add dvh + flex.
       Also disable cinematic transitions on mobile for instant, reliable show. */
    html body.immersive-mode .chat-view-wrapper,
    html body.chat-mode .chat-view-wrapper,
    html body.chat-history-mode .chat-view-wrapper {
        display: flex;
        flex-direction: column;
        position: fixed;
        inset: 0;
        height: 100vh;   /* fallback for older browsers */
        height: 100dvh;  /* preferred — respects dynamic viewport on iOS */
        padding-top: 0;
        overflow: visible;
        transition: none !important;
        transform: none !important;
    }

    /* Kill desktop cinematic transition classes on mobile — instant show.
       transitioning-in sets opacity:0 on desktop which can strand the wrapper
       invisible if RAF is delayed. transitioned sets transform:scale(1) which
       creates a containing-block for position:fixed children.
       Include chat-history-mode for when entering directly from dashboard. */
    html body.immersive-mode .chat-view-wrapper.transitioning-in,
    html body.chat-mode .chat-view-wrapper.transitioning-in,
    html body.chat-history-mode .chat-view-wrapper.transitioning-in {
        opacity: 1 !important;
        transform: none !important;
    }

    html body.immersive-mode .chat-view-wrapper.transitioned,
    html body.chat-mode .chat-view-wrapper.transitioned,
    html body.chat-history-mode .chat-view-wrapper.transitioned {
        opacity: 1 !important;
        transform: none !important;
    }

    /* modern-chat-container — override desktop height:calc(100%-74px), force column.
       Target all three body modes so layout works even if immersive-mode class
       is absent due to transition timing or popstate. */
    html body.immersive-mode .modern-chat-container,
    html body.chat-mode .modern-chat-container,
    html body.chat-history-mode .modern-chat-container {
        height: 100%;
        min-height: 0;
        flex: 1;
        overflow: visible;
        display: flex;
        flex-direction: column;
        position: relative;
        background: var(--m-bg);
    }

    /* chat-main — absolute positioning to reliably fill parent on mobile.
       flex:1 with height:auto can collapse in some mobile browsers,
       so absolute positioning is the proven fallback.
       Target chat-mode alongside immersive-mode for resilience. */
    html body.immersive-mode .chat-main,
    html body.chat-mode .chat-main {
        position: absolute !important;
        top: 0 !important;
        left: 0 !important;
        right: 0 !important;
        bottom: 0 !important;
        width: 100% !important;
        height: 100% !important;
        display: flex !important;
        flex-direction: column !important;
        overflow: visible;
        z-index: var(--m-z-content);
        background: transparent;
    }

    /* chat-messages-container — restore proper padding in immersive.
       Also target chat-mode for resilience when immersive-mode is absent. */
    html body.immersive-mode .chat-messages-container,
    html body.immersive-mode #chat-messages-container,
    html body.chat-mode .chat-messages-container,
    html body.chat-mode #chat-messages-container {
        padding-top: var(--m-pad-top);
        padding-bottom: var(--m-pad-bottom);
        padding-left: var(--m-gap-md);
        padding-right: var(--m-gap-md);
        flex: 1;
        overflow-y: auto;
        overflow-x: hidden;
        -webkit-overflow-scrolling: touch;
    }

    /* ---- Chat History mode overrides ----
       Force-hide chat elements that have unconditional display:flex !important
       rules earlier in this file, and ensure sessions-content is visible.

       CRITICAL: These rules use body.chat-history-mode as source of truth
       instead of relying solely on .is-hidden class, which can lose specificity
       battles against the immersive-mode .chat-main absolute positioning rule
       above (both at 0,2,2 !important — later rule wins). */

    /* Force .chat-sidebar to position:fixed in chat-history-mode.
       The desktop immersive-mode rule (body.immersive-mode .chat-sidebar)
       sets position:relative with specificity (0,2,1), beating the mobile
       position:fixed rule at (0,1,2). This causes the sidebar to stay in
       normal flex flow, consuming all parent height and starving
       #sessions-content. Forcing position:fixed removes it from flex layout
       while still allowing the slide-in hamburger menu to work. */
    html body.chat-history-mode .chat-sidebar,
    html body.immersive-mode.chat-history-mode .chat-sidebar {
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        bottom: 0 !important;
        width: var(--m-sidebar-w) !important;
        max-width: 85vw !important;
        transform: translateX(-100%);
        visibility: hidden;
        z-index: var(--m-z-sidebar) !important;
        height: auto !important;
        overflow-y: auto !important;
        overflow-x: hidden !important;
    }

    /* Allow sidebar to slide in when .open is added (hamburger toggle) */
    html body.chat-history-mode .chat-sidebar.open,
    html body.immersive-mode.chat-history-mode .chat-sidebar.open {
        transform: translateX(0) !important;
        visibility: visible !important;
        display: flex !important;
    }

    /* Force-hide .chat-main in chat-history-mode.
       Uses body class + higher specificity to guarantee .chat-main is hidden
       regardless of the immersive-mode display:flex !important rule above. */
    html body.chat-history-mode .chat-main,
    html body.immersive-mode.chat-history-mode .chat-main {
        display: none !important;
        visibility: hidden !important;
        opacity: 0 !important;
        position: static !important;
        height: 0 !important;
        width: 0 !important;
        overflow: hidden !important;
        pointer-events: none !important;
    }

    /* Sessions content — visible and scrollable in chat-history-mode.
       Uses body class as primary condition (not :not(.is-hidden)) for reliability. */
    html body.chat-history-mode #sessions-content,
    html body.immersive-mode.chat-history-mode #sessions-content {
        display: flex !important;
        flex-direction: column !important;
        visibility: visible !important;
        opacity: 1 !important;
        position: relative !important;
        z-index: 5 !important;
        flex: 1 1 0% !important;
        min-height: 0 !important;
        width: 100% !important;
        max-width: 100% !important;
        overflow-y: auto !important;
        overflow-x: hidden !important;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
        padding-top: var(--m-pad-top) !important;
        padding-left: var(--m-gap-md) !important;
        padding-right: var(--m-gap-md) !important;
        padding-bottom: var(--m-gap-lg) !important;
        background: var(--m-bg) !important;
        box-sizing: border-box !important;
    }

    /* When is-hidden IS present, still hide (e.g. during hideAllViews transition) */
    html body.chat-history-mode #sessions-content.is-hidden,
    html body.immersive-mode.chat-history-mode #sessions-content.is-hidden {
        display: none !important;
        visibility: hidden !important;
        opacity: 0 !important;
    }

    /* Force-hide input area in chat-history-mode */
    html body.chat-history-mode .chat-input-area-enhanced,
    html body.immersive-mode.chat-history-mode .chat-input-area-enhanced {
        display: none !important;
        visibility: hidden !important;
    }

    /* Force-hide messages container in chat-history-mode */
    html body.chat-history-mode .chat-messages-container,
    html body.immersive-mode.chat-history-mode .chat-messages-container {
        display: none !important;
        visibility: hidden !important;
    }

    /* Show hamburger menu button in chat-history-mode so sidebar is accessible */
    html body.chat-history-mode .chat-mobile-menu-toggle,
    html body.immersive-mode.chat-history-mode .chat-mobile-menu-toggle {
        display: flex !important;
        visibility: visible !important;
        opacity: 1 !important;
        z-index: var(--m-z-header) !important;
    }

    /* Force-hide scroll-to-bottom FAB in chat-history-mode */
    html body.chat-history-mode .scroll-to-bottom-fab,
    html body.immersive-mode.chat-history-mode .scroll-to-bottom-fab {
        display: none !important;
    }

    /* Session items & cards — force visible on mobile.
       Desktop CSS sets opacity:0 with a fadeInUp animation; on mobile
       this animation can fail to fire, leaving items permanently invisible. */
    html body #sessions-content .session-item,
    html body #sessions-content .session-card {
        opacity: 1 !important;
        visibility: visible !important;
        transform: none !important;
        animation: none !important;
    }

    /* Session list grid — single column on mobile */
    html body #sessions-content #session-list {
        grid-template-columns: 1fr !important;
    }

    /* Sessions header children — full width */
    html body #sessions-content > * {
        max-width: 100% !important;
        margin-left: 0 !important;
        margin-right: 0 !important;
    }
}


/* ============================================================================
   SAFE AREA HANDLING (Notched devices)
   ============================================================================ */
@supports (padding: max(0px)) {
    @media (max-width: 767px) {
        html body .chat-sidebar {
            padding-left: env(safe-area-inset-left);
        }
        /* Floating nav safe area handling is in floating-nav.css */
    }
}


/* ============================================================================
   iOS SPECIFIC
   ============================================================================ */
@supports (-webkit-touch-callout: none) {
    @media screen and (max-width: 767px) {
        .chat-input-enhanced {
            font-size: 16px !important;
        }
        body {
            overscroll-behavior-y: none;
        }
        .chat-messages-container {
            -webkit-overflow-scrolling: touch;
        }
    }
}


/* ============================================================================
   HIGH DPI / RETINA
   ============================================================================ */
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
    .chat-glass-border {
        border-width: 0.5px;
    }
}


/* ============================================================================
   PRINT
   ============================================================================ */
@media print {
    .chat-sidebar,
    .chat-input-area-enhanced,
    .chat-mobile-menu-toggle,
    .floating-nav,
    .conversation-starters-container,
    .follow-up-questions-container,
    .sidebar-overlay {
        display: none !important;
    }
    .chat-main {
        width: 100% !important;
    }
    .chat-messages-container {
        padding: 0 !important;
        overflow: visible !important;
    }
    .message-bubble {
        background: #f5f5f5 !important;
        border: 1px solid #ddd !important;
        color: #000 !important;
        box-shadow: none !important;
    }
    .user-message .message-bubble {
        background: #e3f2fd !important;
    }
    .message-text {
        color: #000 !important;
    }
}
