/* ============================================
   Mobile Chat Module - ChatGPT-like Experience
   ============================================ */

/* Chat Tab Content - Fixed Position */
/* CRITICAL: Don't set bottom in CSS - let JavaScript-controlled child container manage positioning */
.tab-content#chatTabContent {
    display: none;
    width: 100%;
    height: 100vh;
    max-height: 100vh;
    overflow: hidden;
    -webkit-overflow-scrolling: touch;
    background: #ffffff;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    /* Don't set bottom - let child .mobile-chat-container control its own positioning */
    padding-top: 0; /* No padding - header handles safe area */
    z-index: 1000;
}

/* Hide mobile header when chat tab is active */
.tab-content#chatTabContent.active ~ .mobile-header,
.mobile-app .tab-content#chatTabContent.active ~ * .mobile-header {
    display: none !important;
}

.tab-content#chatTabContent.active {
    display: flex;
    flex-direction: column;
}

/* Chat Page Container - Always Fixed - NEVER MOVES */
/* CRITICAL: Height and positioning are controlled by JavaScript for keyboard handling */
/* CSS only provides initial values - JavaScript will override dynamically */
.mobile-chat-container {
    display: flex;
    flex-direction: column;
    /* Initial height - JavaScript will override with dynamic height based on keyboard state */
    height: 100vh;
    height: 100dvh; /* Dynamic viewport height for better mobile support */
    /* JavaScript controls max-height dynamically - don't set it in CSS */
    width: 100%;
    max-width: 100vw;
    background: #F2F2F7; /* iOS-like light gray background */
    overflow: hidden;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    /* CRITICAL: Don't set bottom in CSS - JavaScript sets bottom: auto */
    /* bottom: 0 conflicts with JavaScript's dynamic height management */
    /* JavaScript sets bottom: auto to allow height-based positioning */
    /* Prevent any movement - no transform */
    transform: none;
    -webkit-transform: none;
    will-change: auto;
    /* Smooth height transition when keyboard opens/closes */
    transition: height 0.2s ease-out;
    /* Ensure it doesn't exceed viewport */
    box-sizing: border-box;
}

/* Chat Header - Fixed at Top - NEVER MOVES */
.mobile-chat-header {
    flex-shrink: 0;
    background: #ffffff;
    border-bottom: 1px solid #e5e7eb;
    padding: 0.75rem 1rem;
    padding-top: max(0.75rem, env(safe-area-inset-top, 0.75rem));
    z-index: 1000; /* Higher than mobile header to ensure it's on top */
    display: flex;
    align-items: center;
    gap: 0.75rem;
    /* CRITICAL: Fixed position to prevent any movement */
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    width: 100%;
    /* Minimal blur like ChatGPT */
    backdrop-filter: blur(8px);
    background: rgba(255, 255, 255, 0.98);
    /* Prevent any transform or movement */
    transform: none;
    -webkit-transform: none;
    transition: none;
}

.mobile-chat-back-btn {
    background: none;
    border: none;
    font-size: 1.5rem;
    color: #1f2937;
    cursor: pointer;
    padding: 0.5rem;
    min-width: 44px;
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 0.5rem;
    transition: background 0.2s;
}

.mobile-chat-back-btn:active {
    background: #f3f4f6;
}

.mobile-chat-header-info {
    flex: 1;
    min-width: 0;
}

.mobile-chat-header-title {
    font-weight: 600;
    color: #1f2937;
    font-size: 1.125rem;
    line-height: 1.4;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.mobile-chat-header-subtitle {
    font-size: 0.875rem;
    color: #6b7280;
    line-height: 1.4;
}

/* Chat Messages Container - Scrollable */
.mobile-chat-messages {
    /* Position will be set by JS to account for fixed header */
    position: absolute;
    left: 0;
    right: 0;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 1rem;
    background: #F2F2F7; /* iOS-like light gray background */
    min-height: 0;
    -webkit-overflow-scrolling: touch;
    /* Smooth scrolling */
    scroll-behavior: smooth;
    /* Smooth height transitions when keyboard opens/closes */
    transition: height 0.2s ease-out, top 0.2s ease-out;
}

/* Scroll to Bottom Button - iOS style */
.mobile-chat-scroll-to-bottom {
    position: absolute;
    bottom: 1rem;
    right: 1rem;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: #007AFF; /* iOS blue */
    color: white;
    border: none;
    cursor: pointer;
    display: none;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 8px rgba(0, 122, 255, 0.3);
    z-index: 100;
    transition: all 0.2s;
    font-size: 1.25rem;
}

.mobile-chat-scroll-to-bottom:active {
    transform: scale(0.95);
    background: #0051D5; /* Darker blue on press */
}

.mobile-chat-scroll-to-bottom.visible {
    display: flex;
    animation: fadeInUp 0.3s ease-out;
}

@keyframes fadeInUp {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Chat Messages */
.mobile-chat-message {
    display: flex;
    gap: 0.5rem; /* Tighter spacing like iOS */
    margin-bottom: 0.5rem; /* Tighter spacing between messages */
    animation: messageSlideIn 0.2s ease-out;
    position: relative;
}

@keyframes messageSlideIn {
    from {
        opacity: 0;
        transform: translateY(4px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.mobile-chat-message-user {
    justify-content: flex-end;
    align-items: flex-end;
}

.mobile-chat-message-avatar {
    flex-shrink: 0;
    display: flex;
    align-items: flex-start;
    width: 32px;
    height: 32px;
}

.mobile-chat-message-avatar img {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    object-fit: cover;
}

.mobile-chat-message-user .mobile-chat-message-avatar {
    order: 2;
    display: none; /* Hide avatar for user messages (iOS style) */
}

.mobile-chat-message-content {
    flex: 1;
    max-width: 75%;
    padding: 0.625rem 0.875rem; /* Slightly tighter padding like iOS */
    border-radius: 1.125rem; /* More rounded like iOS */
    font-size: 1rem;
    line-height: 1.4; /* Tighter line height */
    word-wrap: break-word;
    overflow-wrap: break-word;
    position: relative;
    /* Subtle shadow like iOS */
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.mobile-chat-message-content p {
    margin: 0 0 0.5rem 0;
    white-space: pre-wrap;
}

.mobile-chat-message-content p:last-child {
    margin-bottom: 0;
}

/* Bot Message Styling - iOS light gray */
.mobile-chat-message-bot {
    justify-content: flex-start;
    align-items: flex-start;
}

.mobile-chat-message-bot .mobile-chat-message-content {
    background: #E5E5EA; /* iOS light gray */
    color: #000000;
    border: none; /* No border like iOS */
    border-bottom-left-radius: 0.25rem; /* Tail effect */
    margin-right: auto; /* Push to left */
}

/* User Message Styling - iOS blue */
.mobile-chat-message-user .mobile-chat-message-content {
    background: #007AFF; /* iOS blue */
    color: white;
    border-bottom-right-radius: 0.25rem; /* Tail effect */
    margin-left: auto; /* Push to right */
}

/* Markdown Styling in Messages */
.mobile-chat-message-content h1,
.mobile-chat-message-content h2,
.mobile-chat-message-content h3 {
    margin: 0.75rem 0 0.5rem 0;
    font-weight: 600;
    line-height: 1.4;
}

.mobile-chat-message-content h1 {
    font-size: 1.5rem;
}

.mobile-chat-message-content h2 {
    font-size: 1.25rem;
}

.mobile-chat-message-content h3 {
    font-size: 1.125rem;
}

.mobile-chat-message-content ul,
.mobile-chat-message-content ol {
    margin: 0.5rem 0;
    padding-left: 1.5rem;
}

.mobile-chat-message-content li {
    margin: 0.25rem 0;
}

.mobile-chat-message-content code {
    background: rgba(0, 0, 0, 0.1);
    padding: 0.125rem 0.375rem;
    border-radius: 0.25rem;
    font-family: 'Courier New', monospace;
    font-size: 0.875em;
}

.mobile-chat-message-user .mobile-chat-message-content code {
    background: rgba(255, 255, 255, 0.2);
}

.mobile-chat-message-content pre {
    background: rgba(0, 0, 0, 0.05);
    padding: 0.75rem;
    border-radius: 0.5rem;
    overflow-x: auto;
    margin: 0.5rem 0;
}

.mobile-chat-message-user .mobile-chat-message-content pre {
    background: rgba(255, 255, 255, 0.15);
}

.mobile-chat-message-content pre code {
    background: none;
    padding: 0;
}

.mobile-chat-message-content a {
    color: #007AFF; /* iOS blue for links */
    text-decoration: underline;
}

.mobile-chat-message-user .mobile-chat-message-content a {
    color: rgba(255, 255, 255, 0.9);
    text-decoration: underline;
}

.mobile-chat-message-content strong {
    font-weight: 600;
}

.mobile-chat-message-content em {
    font-style: italic;
}

.mobile-chat-message-content .currency,
.mobile-chat-message-content .percentage,
.mobile-chat-message-content .number {
    font-weight: 600;
}

/* Message Actions - Copy, Regenerate */
.mobile-chat-message-actions {
    display: flex;
    gap: 0.5rem;
    margin-top: 0.5rem;
    opacity: 0;
    transition: opacity 0.2s;
}

.mobile-chat-message:hover .mobile-chat-message-actions {
    opacity: 1;
}

.mobile-chat-message-action-btn {
    background: rgba(0, 0, 0, 0.05);
    border: none;
    padding: 0.375rem 0.75rem;
    border-radius: 0.5rem;
    font-size: 0.8125rem;
    color: #6b7280;
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    gap: 0.25rem;
}

.mobile-chat-message-user .mobile-chat-message-action-btn {
    background: rgba(255, 255, 255, 0.2);
    color: rgba(255, 255, 255, 0.9);
}

.mobile-chat-message-action-btn:active {
    transform: scale(0.95);
    background: rgba(0, 0, 0, 0.1);
}

.mobile-chat-message-user .mobile-chat-message-action-btn:active {
    background: rgba(255, 255, 255, 0.3);
}

/* Typing Indicator - ChatGPT-like */
.mobile-chat-typing-indicator {
    display: flex;
    gap: 4px;
    align-items: center;
    padding: 0.5rem 0;
}

.mobile-chat-typing-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #6b7280;
    animation: typingDot 1.4s infinite;
}

.mobile-chat-typing-dot:nth-child(2) {
    animation-delay: 0.2s;
}

.mobile-chat-typing-dot:nth-child(3) {
    animation-delay: 0.4s;
}

@keyframes typingDot {
    0%, 60%, 100% {
        transform: translateY(0);
        opacity: 0.7;
    }
    30% {
        transform: translateY(-10px);
        opacity: 1;
    }
}

/* Chat Input Container - At Bottom of Container */
.mobile-chat-input-container {
    flex-shrink: 0;
    background: #ffffff;
    border-top: 1px solid #e5e7eb;
    padding: 0.75rem 1rem;
    padding-bottom: max(0.75rem, env(safe-area-inset-bottom, 0.75rem));
    position: relative; /* Relative when keyboard is closed - stays at bottom of flex container */
    z-index: 100;
    width: 100%;
    /* Smooth transitions for bottom position (when keyboard opens) */
    transition: bottom 0.15s ease-out;
}

/* When keyboard is open - input moves with keyboard (set by JS) */
.mobile-chat-container.keyboard-open .mobile-chat-input-container {
    position: fixed; /* Fixed when keyboard is open - moves with keyboard */
    left: 0;
    right: 0;
    width: 100%;
    padding-bottom: max(0.75rem, env(safe-area-inset-bottom, 0.75rem));
    margin-bottom: 0;
    /* Bottom position will be set by JS based on visual viewport */
    transition: bottom 0.15s ease-out;
}

/* Input Wrapper - iOS style */
.mobile-chat-input-wrapper {
    display: flex;
    gap: 0.5rem;
    align-items: flex-end;
    background: #ffffff;
    border: none; /* No border like iOS */
    border-radius: 1.25rem;
    padding: 0.5rem 0.75rem;
    transition: all 0.2s;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1); /* Subtle shadow */
}

.mobile-chat-input-wrapper:focus-within {
    box-shadow: 0 1px 3px rgba(0, 122, 255, 0.3); /* Blue shadow on focus */
    background: #ffffff;
}

/* Multi-line Input - Auto-expanding */
.mobile-chat-input {
    flex: 1;
    border: none;
    background: transparent;
    font-size: 1rem;
    color: #1f2937;
    resize: none;
    outline: none;
    min-height: 20px;
    max-height: 120px;
    line-height: 1.5;
    padding: 0.25rem 0;
    font-family: inherit;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.mobile-chat-input::placeholder {
    color: #9ca3af;
}

/* Send Button - iOS blue */
.mobile-chat-send-btn {
    background: #007AFF; /* iOS blue */
    color: white;
    border: none;
    border-radius: 50%;
    width: 36px;
    height: 36px;
    min-width: 36px;
    min-height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s;
    font-size: 1.125rem;
    padding: 0;
    flex-shrink: 0;
    box-shadow: 0 1px 2px rgba(0, 122, 255, 0.3); /* Subtle shadow */
}

.mobile-chat-send-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
    background: #C7C7CC; /* iOS disabled gray */
}

.mobile-chat-send-btn:not(:disabled):active {
    transform: scale(0.95);
    background: #0051D5; /* Darker blue on press */
}

/* Send button state - checkmark when message sent */
.mobile-chat-send-btn.sent {
    background: #007AFF;
}

.mobile-chat-send-btn.sent svg {
    display: none;
}

.mobile-chat-send-btn.sent::after {
    content: '✓';
    font-size: 1.25rem;
    font-weight: 600;
}

.mobile-chat-send-btn svg {
    width: 18px;
    height: 18px;
}

/* Error Message Styling */
.mobile-chat-message-error .mobile-chat-message-content {
    background: #fee2e2;
    color: #dc2626;
    border: 1px solid #fecaca;
}

/* Empty State */
.mobile-chat-empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 3rem 2rem;
    text-align: center;
    color: #6b7280;
}

.mobile-chat-empty-state-icon {
    font-size: 3rem;
    margin-bottom: 1rem;
}

.mobile-chat-empty-state-title {
    font-size: 1.25rem;
    font-weight: 600;
    color: #1f2937;
    margin-bottom: 0.5rem;
}

.mobile-chat-empty-state-message {
    font-size: 0.9375rem;
    color: #6b7280;
}

/* Responsive Adjustments */
@media (max-width: 375px) {
    .mobile-chat-message-content {
        max-width: 80%;
        font-size: 0.9375rem;
    }
    
    .mobile-chat-header {
        padding: 0.625rem 0.875rem;
    }
    
    .mobile-chat-messages {
        padding: 0.75rem;
    }
}

/* Smooth Scrolling */
.mobile-chat-messages {
    scroll-padding-bottom: 1rem;
}

/* Prevent text selection on actions */
.mobile-chat-message-actions,
.mobile-chat-scroll-to-bottom {
    user-select: none;
    -webkit-user-select: none;
}

