/* Chat Interface Styles */
/* Extends existing styles.css with enhanced chat functionality */

/* Chat Container Enhancements */
/* FIXED: Added mobile-friendly layout with proper height handling */
.chat-container {
    display: flex;
    flex-direction: column;
    height: 100%;
    max-height: 100vh; /* Prevent overflow on mobile */
    max-height: 100dvh; /* Use dynamic viewport height for modern browsers */
    background: linear-gradient(135deg, var(--chat-bg) 0%, rgba(36, 36, 36, 0.95) 100%);
    border-radius: var(--border-radius);
    box-shadow: var(--box-shadow);
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.1);
    overflow: hidden; /* Prevent content from affecting parent layout */
    position: relative; /* For absolute positioned children */
}

/* Chat Header */
.chat-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 15px 20px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(67, 97, 238, 0.1);
    border-radius: var(--border-radius) var(--border-radius) 0 0;
}

.chat-header h3 {
    color: var(--accent-color);
    font-size: 1.1rem;
    font-weight: 600;
    margin: 0;
}

.chat-action-btn {
    background: transparent;
    border: 1px solid rgba(255, 255, 255, 0.2);
    color: var(--text-color);
    padding: 5px 12px;
    border-radius: 15px;
    font-size: 0.8rem;
    cursor: pointer;
    transition: all 0.3s ease;
}

.chat-action-btn:hover {
    background: rgba(255, 255, 255, 0.1);
    border-color: var(--accent-color);
    color: var(--accent-color);
}

/* Enhanced Message Styles */
.message {
    display: flex;
    align-items: flex-start;
    gap: 12px;
    margin-bottom: 16px;
    opacity: 1;
    transform: translateY(0);
    transition: opacity 0.3s ease, transform 0.3s ease;
}

/* Bot messages with structured content - use block display for proper text flow */
.message.bot .message-text {
    display: block;
    line-height: 1.6;
}

/* Birth details and structured content in bot messages */
.message.bot .birth-details,
.message.bot .info-section {
    display: block;
    padding: 10px;
    background: rgba(255, 255, 255, 0.05);
    border-radius: 8px;
    margin-top: 8px;
    line-height: 1.8;
}

.message.bot .birth-details strong,
.message.bot .info-section strong {
    color: var(--accent-color);
    display: inline;
    margin-right: 4px;
}

.message.user {
    flex-direction: row-reverse;
}

.message-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.2rem;
    flex-shrink: 0;
    background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
    color: white;
    box-shadow: 0 2px 8px rgba(67, 97, 238, 0.3);
}

.message.user .message-avatar {
    background: linear-gradient(135deg, var(--accent-color), var(--primary-color));
    box-shadow: 0 2px 8px rgba(76, 201, 240, 0.3);
}

/* Error message styling */
.message.error .message-avatar {
    background: linear-gradient(135deg, #ff6b6b, #ee5a6f);
    box-shadow: 0 2px 8px rgba(255, 107, 107, 0.3);
}

.message.error .message-text {
    background: rgba(255, 107, 107, 0.1);
    border: 1px solid rgba(255, 107, 107, 0.3);
    color: #ff6b6b;
}

.message.error .message-name {
    color: #ff6b6b;
    font-weight: 600;
}

.message-content {
    flex: 1;
    max-width: calc(100% - 60px);
}

.message-text {
    background: var(--message-bot-bg);
    padding: 12px 16px;
    border-radius: 18px 18px 18px 4px;
    color: var(--text-color);
    line-height: 1.5;
    word-wrap: break-word;
    border: 1px solid rgba(255, 255, 255, 0.1);
    /* Use block display for natural text flow */
    display: block;
}

/* Assistant message styling (welcome messages use this class) */
.message.assistant-message {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    padding: 20px;
    border-radius: 12px;
    margin: 20px;
    display: block !important; /* Override flex display for welcome messages */
}

/* Ensure assistant messages display properly - only direct children */
.assistant-message > div {
    display: block !important;
    width: 100%;
}

/* Fix inline elements in assistant messages to respect line breaks */
.assistant-message br {
    display: block;
    content: "";
    margin: 0;
    padding: 0;
}

/* Structured data display */
.message-text .data-row {
    display: flex;
    flex-direction: column;
    gap: 4px;
    margin: 4px 0;
}

.message-text .data-label {
    font-weight: 600;
    color: var(--accent-color);
    font-size: 0.9rem;
}

.message-text .data-value {
    color: var(--text-primary);
    padding-left: 12px;
}

.message.user .message-text {
    background: linear-gradient(135deg, var(--message-user-bg), var(--secondary-color));
    border-radius: 18px 18px 4px 18px;
    color: white;
    border: 1px solid rgba(255, 255, 255, 0.2);
}

.message-timestamp {
    font-size: 0.75rem;
    color: #888;
    margin-top: 4px;
    padding-left: 16px;
}

.message.user .message-timestamp {
    text-align: right;
    padding-left: 0;
    padding-right: 16px;
}

/* Markdown Styling in Messages */
.message-text h1, .message-text h2, .message-text h3 {
    color: var(--accent-color);
    margin: 8px 0 4px 0;
    font-size: 1rem;
}

.message-text h2 {
    font-size: 0.95rem;
}

.message-text h3 {
    font-size: 0.9rem;
}

.message-text strong {
    color: var(--accent-color);
    font-weight: 600;
}

.message-text em {
    color: #ccc;
    font-style: italic;
}

.message-text code {
    background: rgba(0, 0, 0, 0.3);
    padding: 2px 6px;
    border-radius: 4px;
    font-family: 'Monaco', 'Consolas', monospace;
    font-size: 0.85rem;
}

.message-text blockquote {
    border-left: 3px solid var(--accent-color);
    padding-left: 12px;
    margin: 8px 0;
    font-style: italic;
    color: #ccc;
    background: rgba(76, 201, 240, 0.1);
    padding: 8px 12px;
    border-radius: 0 8px 8px 0;
}

.message-text ul, .message-text ol {
    margin: 8px 0;
    padding-left: 20px;
}

.message-text li {
    margin: 4px 0;
}

/* Enhanced Chat Input */
.chat-input {
    display: flex;
    align-items: flex-end;
    padding: 15px 20px;
    padding-bottom: calc(15px + env(safe-area-inset-bottom)); /* Add safe area for iPhone */
    background: rgba(26, 26, 26, 0.8);
    border-top: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 0 0 var(--border-radius) var(--border-radius);
    gap: 12px;
    position: relative; /* Ensure proper stacking */
    z-index: 100; /* Keep input above other content */
}

.chat-input input {
    flex: 1;
    background: rgba(51, 51, 51, 0.8);
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 20px;
    padding: 12px 16px;
    color: var(--text-color);
    font-size: 0.95rem;
    resize: none;
    min-height: 20px;
    max-height: 120px;
    outline: none;
    transition: all 0.3s ease;
}

.chat-input input:focus {
    border-color: var(--accent-color);
    background: rgba(51, 51, 51, 1);
    box-shadow: 0 0 0 2px rgba(76, 201, 240, 0.2);
}

.chat-input input::placeholder {
    color: #888;
}

.chat-input button {
    width: 44px;
    height: 44px;
    border: none;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.3s ease;
    box-shadow: 0 2px 8px rgba(67, 97, 238, 0.4);
}

.chat-input button:hover {
    transform: scale(1.05);
    box-shadow: 0 4px 12px rgba(67, 97, 238, 0.6);
}

.chat-input button:active {
    transform: scale(0.95);
}

.chat-input button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

/* Typing Indicator */
.typing-indicator {
    opacity: 0.8;
}

.typing-animation {
    display: flex;
    gap: 4px;
    padding: 12px 16px;
}

.typing-animation span {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--accent-color);
    display: inline-block;
    animation: typing-bounce 1.4s infinite ease-in-out both;
}

.typing-animation span:nth-child(1) {
    animation-delay: -0.32s;
}

.typing-animation span:nth-child(2) {
    animation-delay: -0.16s;
}

@keyframes typing-bounce {
    0%, 80%, 100% {
        transform: scale(0.8);
        opacity: 0.5;
    }
    40% {
        transform: scale(1);
        opacity: 1;
    }
}

/* Streaming Message Styles */
.message.streaming .message-text {
    position: relative;
    border: 1px solid var(--accent-color);
    background: rgba(76, 201, 240, 0.1);
    min-height: 24px; /* Prevent layout shifts during content updates */
    overflow-wrap: break-word; /* Handle long words properly */
    word-break: break-word; /* Prevent horizontal overflow */
}

.streaming-indicator {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-top: 8px;
    padding: 4px 0;
}

.streaming-indicator .dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--accent-color);
    animation: streaming-pulse 1.5s infinite;
}

.streaming-indicator .dot:nth-child(2) {
    animation-delay: 0.5s;
}

.streaming-indicator .dot:nth-child(3) {
    animation-delay: 1s;
}

@keyframes streaming-pulse {
    0%, 100% {
        opacity: 0.3;
        transform: scale(0.8);
    }
    50% {
        opacity: 1;
        transform: scale(1);
    }
}

/* Chat Status Bar */
.chat-status {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 20px;
    background: rgba(26, 26, 26, 0.6);
    border-radius: 0 0 var(--border-radius) var(--border-radius);
    font-size: 0.75rem;
    color: #888;
    border-top: 1px solid rgba(255, 255, 255, 0.05);
}

#session-indicator {
    font-family: 'Monaco', 'Consolas', monospace;
}

#connection-status {
    font-weight: 500;
}

.status-connected {
    color: #4CAF50;
}

.status-error {
    color: #f44336;
}

.status-connecting {
    color: #FF9800;
}

/* Enhanced Scrollbar */
.chat-messages::-webkit-scrollbar {
    width: 6px;
}

.chat-messages::-webkit-scrollbar-track {
    background: rgba(255, 255, 255, 0.05);
    border-radius: 3px;
}

.chat-messages::-webkit-scrollbar-thumb {
    background: linear-gradient(135deg, var(--primary-color), var(--accent-color));
    border-radius: 3px;
}

.chat-messages::-webkit-scrollbar-thumb:hover {
    background: linear-gradient(135deg, var(--accent-color), var(--primary-color));
}

/* Animation Classes */
.message-enter {
    animation: messageSlideIn 0.3s ease-out;
}

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

/* Mobile Responsive Improvements */
/* FIXED: Enhanced mobile responsiveness for chat interface */
@media (max-width: 768px) {
    /* Fix chat container for mobile viewport */
    .chat-container {
        height: 100vh;
        height: 100dvh; /* Dynamic viewport height */
        max-height: 100vh;
        max-height: 100dvh;
        border-radius: 0; /* Remove rounded corners on mobile */
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        z-index: 100;
        display: flex;
        flex-direction: column;
    }
    
    .chat-header {
        padding: 10px 12px;
        position: sticky;
        top: 0;
        z-index: 10;
        background: rgba(26, 26, 26, 0.95);
    }
    
    /* Fix messages container for proper scrolling */
    .chat-messages {
        flex: 1 1 auto;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch; /* Smooth scrolling on iOS */
        padding: 10px;
        min-height: 0; /* Important for flexbox children */
    }
    
    .chat-input {
        padding: 10px 12px;
        padding-bottom: calc(10px + env(safe-area-inset-bottom)); /* iPhone safe area */
        position: relative; /* Changed from sticky to relative */
        background: rgba(26, 26, 26, 0.98);
        border-top: 2px solid rgba(255, 255, 255, 0.1);
        flex-shrink: 0; /* Prevent shrinking */
        z-index: 100;
    }
    
    .message {
        margin-bottom: 10px;
        max-width: 100%;
    }
    
    .message-avatar {
        width: 32px;
        height: 32px;
        font-size: 0.9rem;
    }
    
    .message-content {
        max-width: calc(100% - 44px);
        word-break: break-word;
    }
    
    .message-text {
        padding: 8px 12px;
        font-size: 0.9rem;
        max-width: 100%;
        overflow-wrap: break-word;
        word-wrap: break-word;
        hyphens: auto;
        /* FIXED: Maintain vertical layout on mobile */
        display: flex;
        flex-direction: column;
        gap: 6px;
    }
    
    /* Ensure bot messages stay vertical on mobile */
    .message.bot .message-text {
        flex-direction: column;
    }
    
    /* Adjust welcome message for mobile */
    .message.bot.welcome .message-text {
        padding: 12px;
    }
    
    .message.bot.welcome h2,
    .message.bot.welcome h3 {
        font-size: 1rem;
    }
    
    /* Fix profile selector for mobile */
    .profile-selector {
        width: 100%;
        max-width: 100%;
        font-size: 14px;
    }
    
    .chat-input input {
        padding: 10px 12px;
        font-size: 16px; /* Prevent zoom on iOS */
        min-height: 44px;
    }
    
    .chat-input button {
        width: 44px;
        height: 44px;
        min-width: 44px;
    }
}

/* FIXED: Enhanced support for very small mobile screens */
@media (max-width: 480px) {
    .chat-container {
        height: 100vh;
        height: 100dvh; /* Dynamic viewport height */
        max-height: 100vh;
        max-height: 100dvh;
    }
    
    .chat-header {
        padding: 8px 10px;
    }
    
    .chat-header h3 {
        font-size: 0.95rem;
    }
    
    /* Optimize messages for small screens */
    .chat-messages {
        padding: 8px;
        flex: 1 1 auto;
        min-height: 0; /* Important for flexbox */
    }
    
    .message {
        gap: 8px;
    }
    
    .message-avatar {
        width: 28px;
        height: 28px;
        font-size: 0.8rem;
    }
    
    .message-content {
        max-width: calc(100% - 36px);
    }
    
    .message-text {
        padding: 6px 10px;
        font-size: 0.85rem;
        border-radius: 12px;
        /* FIXED: Ensure vertical layout on very small screens */
        display: flex;
        flex-direction: column;
        gap: 4px;
    }
    
    /* Keep structured content vertical */
    .message.bot .birth-details,
    .message.bot .info-section {
        padding: 6px 8px;
        gap: 4px;
    }
    
    .message-text .data-label {
        font-size: 0.8rem;
    }
    
    .message-text .data-value {
        font-size: 0.85rem;
        padding-left: 8px;
    }
    
    /* Optimize input area */
    .chat-input {
        padding: 8px 10px;
        padding-bottom: calc(8px + env(safe-area-inset-bottom)); /* iPhone safe area */
        gap: 8px;
        flex-shrink: 0;
    }
    
    .chat-input input {
        padding: 8px 10px;
        font-size: 16px; /* Critical: prevents zoom on iOS */
        border-radius: 18px;
    }
    
    .chat-input button {
        width: 40px;
        height: 40px;
        min-width: 40px;
    }
    
    .chat-status {
        padding: 4px 10px;
        font-size: 0.7rem;
    }
    
    /* Hide less important elements on very small screens */
    .message-timestamp {
        font-size: 0.65rem;
    }
}

/* High contrast mode support */
@media (prefers-contrast: high) {
    .message-text {
        border: 2px solid var(--accent-color);
    }
    
    .chat-input input {
        border: 2px solid var(--accent-color);
    }
    
    .chat-action-btn {
        border: 2px solid var(--accent-color);
    }
}

/* Reduced motion support */
@media (prefers-reduced-motion: reduce) {
    .message,
    .chat-input button,
    .typing-animation span,
    .streaming-indicator .dot {
        animation: none;
        transition: none;
    }
}

/* Dark mode enhancements */
@media (prefers-color-scheme: dark) {
    .message-text code {
        background: rgba(255, 255, 255, 0.1);
    }
}

/* Focus improvements for accessibility */
.chat-input input:focus,
.chat-action-btn:focus,
.chat-input button:focus {
    outline: 2px solid var(--accent-color);
    outline-offset: 2px;
}

/* Loading state for messages */
.message.loading {
    opacity: 0.6;
    pointer-events: none;
}

.message.loading .message-text::after {
    content: "⏳";
    margin-left: 8px;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}