/* AI Chat - Mobile-First Responsive Layout */

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: #f7f7f8;
    color: #374151;
    line-height: 1.6;
    height: 100vh;
    overflow: hidden;
    /* Prevent zoom on iOS */
    -webkit-text-size-adjust: 100%;
    -moz-text-size-adjust: 100%;
    -ms-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

/* Allow text selection everywhere by default */
* {
    -webkit-user-select: text;
    -moz-user-select: text;
    -ms-user-select: text;
    user-select: text;
}

/* Disable selection only for UI elements */
button, .sidebar, .model-dropdown-btn, .new-chat-btn {
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

.app-container {
    height: 100vh;
    display: flex;
    position: relative;
}

/* Mobile hamburger menu */
.mobile-menu-btn {
    display: none;
    position: fixed;
    top: 1rem;
    left: 1rem;
    z-index: 1001;
    background: #171717;
    color: white;
    border: none;
    padding: 0.5rem;
    border-radius: 6px;
    cursor: pointer;
    font-size: 1.2rem;
}

/* Sidebar */
.sidebar {
    width: 240px; /* Reduced from 260px */
    background: #171717;
    color: white;
    display: flex;
    flex-direction: column;
    border-right: 1px solid #4d4d4f;
    transition: transform 0.3s ease;
}

/* Mobile responsive styles */
@media (max-width: 768px) {
    .mobile-menu-btn {
        display: block;
    }

    .sidebar {
        position: fixed;
        top: 0;
        left: 0;
        height: 100vh;
        z-index: 1000;
        transform: translateX(-100%);
        width: 280px;
    }

    .sidebar.open {
        transform: translateX(0);
    }

    .sidebar-overlay {
        display: none;
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.5);
        z-index: 999;
    }

    .sidebar-overlay.show {
        display: block;
    }

    .main-content {
        width: 100%;
        margin-left: 0;
    }

    /* Mobile model dropdown fixes */
    .model-dropdown {
        position: relative;
    }

    .model-dropdown-menu {
        position: fixed !important;
        top: auto !important;
        bottom: 0 !important;
        left: 0 !important;
        right: 0 !important;
        max-height: 70vh !important;
        overflow-y: auto !important;
        border-radius: 16px 16px 0 0 !important;
        z-index: 9999 !important;
        box-shadow: 0 -8px 32px rgba(0, 0, 0, 0.4) !important;
        transform: translateY(100%) !important;
        transition: transform 0.3s ease !important;
        background: #2d2d30 !important;
        border: 1px solid #4d4d4f !important;
    }

    .model-dropdown.open .model-dropdown-menu {
        display: block !important;
        transform: translateY(0) !important;
    }

    .model-dropdown-btn {
        font-size: 0.85rem;
        padding: 0.7rem;
        min-height: 44px; /* Touch target */
        display: flex;
        align-items: center;
        justify-content: space-between;
    }

    .model-section-title {
        font-size: 0.75rem;
        padding: 0.6rem 1rem 0.3rem;
        color: #9ca3af;
        font-weight: 600;
    }

    .model-option {
        padding: 1rem;
        font-size: 0.85rem;
        min-height: 44px; /* Touch target */
        display: flex;
        align-items: center;
        cursor: pointer;
        border-bottom: 1px solid #3d3d40;
    }

    .model-option:last-child {
        border-bottom: none;
    }

    .model-option small {
        font-size: 0.75rem;
        margin-top: 0.2rem;
        display: block;
    }

    .model-option:hover,
    .model-option:active {
        background: #3d3d40 !important;
    }

    .model-option.selected {
        background: #10a37f !important;
    }

    /* Mobile chat history */
    .chat-history {
        padding: 0.5rem;
    }

    .history-item {
        padding: 0.75rem 0.6rem;
        font-size: 0.8rem;
        margin-bottom: 0.3rem;
        border-radius: 8px;
    }

    .history-item-title {
        font-size: 0.8rem;
        margin-bottom: 0.3rem;
    }

    .history-item-preview {
        font-size: 0.7rem;
        line-height: 1.3;
    }

    .history-item-date {
        font-size: 0.65rem;
        margin-top: 0.3rem;
    }

    .history-header h3 {
        font-size: 0.8rem;
    }

    .clear-history-btn {
        padding: 0.3rem;
    }

    /* Mobile image upload */
    .upload-btn {
        min-width: 32px;
        min-height: 32px;
        padding: 6px;
    }

    #imagePreview {
        margin: 6px 0;
    }

    #previewImg {
        max-width: 150px;
        max-height: 150px;
    }

    #removeImage {
        width: 28px;
        height: 28px;
        font-size: 16px;
    }
}

.sidebar-header {
    padding: 0.75rem; /* Reduced from 1rem */
    border-bottom: 1px solid #4d4d4f;
}

.new-chat-btn {
    background: transparent;
    border: 1px solid #4d4d4f;
    color: white;
    padding: 0.75rem 1rem;
    border-radius: 6px;
    cursor: pointer;
    width: 100%;
    font-size: 0.875rem;
    transition: background-color 0.2s;
}

.new-chat-btn:hover {
    background: #2d2d30;
}

/* Model Selector Dropdown */
.model-selector {
    padding: 0.75rem; /* Reduced from 1rem */
    border-bottom: 1px solid #4d4d4f;
}

.model-dropdown {
    position: relative;
}

.model-dropdown-btn {
    width: 100%;
    background: #2d2d30;
    border: 1px solid #4d4d4f;
    color: #ececf1;
    padding: 0.75rem;
    border-radius: 6px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 0.875rem;
    transition: background-color 0.2s;
}

.model-dropdown-btn:hover {
    background: #3d3d40;
}

.dropdown-arrow {
    transition: transform 0.2s;
}

.model-dropdown.open .dropdown-arrow {
    transform: rotate(180deg);
}

.model-dropdown-menu {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    background: #2d2d30;
    border: 1px solid #4d4d4f;
    border-radius: 6px;
    margin-top: 0.25rem;
    z-index: 1000;
    display: none;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}

.model-dropdown.open .model-dropdown-menu {
    display: block;
}

.model-section {
    margin-bottom: 0.5rem;
}

.model-section:last-child {
    margin-bottom: 0;
}

.model-section-title {
    font-size: 0.75rem;
    font-weight: 600;
    color: #9ca3af;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    padding: 0.5rem 0.75rem 0.25rem;
    border-bottom: 1px solid #4d4d4f;
    margin-bottom: 0.25rem;
}

.model-option {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    padding: 0.75rem;
    cursor: pointer;
    transition: background-color 0.2s;
    color: #ececf1;
    font-size: 0.875rem;
    border-bottom: 1px solid #4d4d4f;
    gap: 0.25rem;
}

.model-option span {
    font-weight: 500;
}

.model-option small {
    font-size: 0.75rem;
    color: #9ca3af;
    font-weight: 400;
}

.model-option:last-child {
    border-bottom: none;
}

.model-option:hover {
    background: #3d3d40;
}

.model-option.selected {
    background: #10a37f;
    color: white;
}

/* Chat History */
.chat-history {
    flex: 1;
    overflow-y: auto;
    padding: 0.75rem;
    border-bottom: 1px solid #4d4d4f;
}

.history-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.75rem;
}

.history-header h3 {
    font-size: 0.875rem;
    font-weight: 600;
    color: #ececf1;
    margin: 0;
}

.clear-history-btn {
    background: transparent;
    border: none;
    color: #9ca3af;
    cursor: pointer;
    padding: 0.25rem;
    border-radius: 4px;
    transition: color 0.2s, background-color 0.2s;
}

.clear-history-btn:hover {
    color: #ef4444;
    background: rgba(239, 68, 68, 0.1);
}

.history-list {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.history-item {
    padding: 0.5rem 0.75rem;
    background: #2d2d30;
    border: 1px solid #4d4d4f;
    border-radius: 6px;
    cursor: pointer;
    transition: background-color 0.2s;
    font-size: 0.8rem;
    color: #ececf1;
    line-height: 1.4;
}

.history-item:hover {
    background: #3d3d40;
}

.history-item.active {
    background: #10a37f;
    border-color: #10a37f;
}

.history-item-title {
    font-weight: 500;
    margin-bottom: 0.25rem;
    display: -webkit-box;
    -webkit-line-clamp: 1;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.history-item-preview {
    color: #9ca3af;
    font-size: 0.75rem;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.history-item-date {
    color: #6b7280;
    font-size: 0.7rem;
    margin-top: 0.25rem;
}

.model-option input[type="radio"] {
    display: none;
}

.model-option span {
    margin-left: 0.5rem;
}

/* Connection Status */
.connection-status {
    padding: 1rem;
    border-top: 1px solid #4d4d4f;
    margin-top: auto;
}

.status-item {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-bottom: 0.5rem;
    font-size: 0.75rem;
    color: #ececf1;
}

.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #6b7280;
}

.status-item.online .status-dot {
    background: #10b981;
}

.status-item.offline .status-dot {
    background: #ef4444;
}

/* Hide original header */
.header {
    display: none;
}

/* Main Content */
.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: #ffffff;
}

/* Chat Container */
.chat-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: #ffffff;
    position: relative;
}

/* Welcome Screen */
.welcome-screen {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    text-align: center;
    color: #374151;
}

.welcome-screen h1 {
    font-size: 2rem;
    font-weight: 600;
    margin-bottom: 2rem;
}

/* Messages Area */
.messages-container {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    background: #ffffff;
    min-height: 400px;
    max-height: calc(100vh - 200px); /* Ensure it doesn't exceed viewport */
    position: relative;
    /* Enable smooth scrolling */
    scroll-behavior: smooth;
    /* Fix iOS scrolling */
    -webkit-overflow-scrolling: touch;
}

.messages {
    max-width: 768px;
    margin: 0 auto;
    padding: 0.75rem; /* Reduced from 1rem */
    padding-bottom: 1.5rem; /* Reduced from 2rem */
}

/* Mobile scrolling improvements */
@media (max-width: 768px) {
    .messages-container {
        max-height: calc(100vh - 160px); /* Adjust for mobile header/input */
        padding: 0;
    }

    .messages {
        padding: 0.75rem;
        padding-bottom: 1.5rem;
    }
}

/* Message Styles - True ChatGPT Style */
.message {
    display: flex;
    gap: 1rem;
    padding: 1.5rem 0;
    word-wrap: break-word;
    animation: fadeIn 0.3s ease-in;
    border-bottom: 1px solid #f0f0f0;
}

.message.user {
    background: #f7f7f8;
    margin: 0 -1rem;
    padding: 1.5rem 1rem;
}

.message.assistant {
    background: #ffffff;
}

.message.system {
    background: #f9f9f9;
    color: #6b7280;
    font-style: italic;
    text-align: center;
    padding: 0.75rem;
    font-size: 0.875rem;
}

/* Avatar */
.message-avatar {
    width: 30px;
    height: 30px;
    border-radius: 2px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    font-weight: 600;
    flex-shrink: 0;
}

.message.user .message-avatar {
    background: #10a37f;
    color: white;
}

.message.assistant .message-avatar {
    background: #19c37d;
    color: white;
}

/* Message Content */
.message-content {
    flex: 1;
    min-width: 0;
    font-size: 1rem;
    line-height: 1.75;
}

.message-content p {
    margin-bottom: 1rem;
}

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

/* Input Area */
.input-area {
    padding: 1rem;
    background: #ffffff;
    border-top: 1px solid #e5e7eb;
}

.input-container {
    max-width: 768px;
    margin: 0 auto;
    position: relative;
}

/* Mobile input styles */
@media (max-width: 768px) {
    .input-area {
        padding: 0.75rem;
    }

    .input-container {
        max-width: none;
        margin: 0;
    }

    .input-wrapper {
        padding: 10px 14px;
    }

    #messageInput {
        font-size: 16px; /* Prevent zoom on iOS */
    }

    #sendButton {
        padding: 6px;
        min-width: 32px;
        min-height: 32px;
    }
}

.input-wrapper {
    display: flex;
    align-items: flex-end;
    background: #f4f4f4;
    border: 1px solid #d1d5db;
    border-radius: 24px;
    padding: 12px 16px;
    box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
    transition: all 0.2s;
    gap: 8px;
}

.upload-btn {
    background: transparent;
    border: none;
    color: #6b7280;
    cursor: pointer;
    padding: 4px;
    border-radius: 6px;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 24px;
    min-height: 24px;
}

.upload-btn:hover {
    background: #e5e7eb;
    color: #374151;
}

.upload-btn:active {
    background: #d1d5db;
}

/* Image Preview */
#imagePreview {
    position: relative;
    margin: 8px 0;
    display: none;
}

#imagePreview.show {
    display: block;
}

#previewImg {
    max-width: 200px;
    max-height: 200px;
    border-radius: 8px;
    border: 1px solid #d1d5db;
}

#removeImage {
    position: absolute;
    top: -8px;
    right: -8px;
    background: #ef4444;
    color: white;
    border: none;
    border-radius: 50%;
    width: 24px;
    height: 24px;
    cursor: pointer;
    font-size: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

#removeImage:hover {
    background: #dc2626;
}

.input-wrapper:focus-within {
    border-color: #10a37f;
    box-shadow: 0 0 0 2px rgba(16, 163, 127, 0.2);
}

#messageInput {
    flex: 1;
    border: none;
    outline: none;
    resize: none;
    font-family: inherit;
    font-size: 16px; /* 16px prevents iOS zoom */
    line-height: 1.5;
    max-height: 200px;
    min-height: 24px;
    background: transparent;
    color: #374151;
    /* Prevent iOS zoom and styling */
    -webkit-text-size-adjust: 100%;
    -webkit-appearance: none;
    border-radius: 0;
}

#messageInput::placeholder {
    color: #8e8ea0;
}

#sendButton {
    background: #10a37f;
    color: white;
    border: none;
    border-radius: 50%;
    padding: 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    transition: all 0.2s;
    margin-left: 8px;
}

#sendButton:hover:not(:disabled) {
    background: #0d8f6b;
    transform: scale(1.05);
}

#sendButton:disabled {
    background: #d1d5db;
    cursor: not-allowed;
    transform: none;
}

/* Loading Animation */
.typing-indicator {
    display: flex;
    align-items: center;
    gap: 0.25rem;
    padding: 1rem;
    color: #6b7280;
}

.typing-dots {
    display: flex;
    gap: 0.25rem;
}

.typing-dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: #9ca3af;
    animation: typing 1.4s infinite ease-in-out;
}

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

@keyframes typing {
    0%, 80%, 100% { opacity: 0.3; }
    40% { opacity: 1; }
}

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

/* Responsive */
@media (max-width: 768px) {
    .app-container {
        max-width: 100%;
    }
    
    .message {
        padding: 1rem 0.75rem;
        gap: 0.75rem;
        flex-direction: row; /* Keep row layout */
        align-items: flex-start;
    }

    .message.user {
        flex-direction: row-reverse; /* Reverse for user messages */
    }

    .message-avatar {
        flex-shrink: 0;
        width: 32px;
        height: 32px;
        font-size: 0.9rem;
    }

    .message-content {
        flex: 1;
        min-width: 0; /* Allow shrinking */
        margin: 0;
        padding: 0.75rem;
        font-size: 0.9rem;
        line-height: 1.5;
        word-break: break-word;
        overflow-wrap: break-word;
        border-radius: 12px;
    }

    .message.user .message-content {
        background: #10a37f;
        color: white;
        margin-left: 2rem;
    }

    .message.assistant .message-content {
        background: #f1f1f1;
        color: #374151;
        margin-right: 2rem;
    }

    .message-content pre {
        white-space: pre-wrap;
        word-break: break-word;
        overflow-x: auto;
        background: rgba(0, 0, 0, 0.1);
        padding: 0.5rem;
        border-radius: 6px;
        margin: 0.5rem 0;
    }

    .message-content code {
        word-break: break-word;
        background: rgba(0, 0, 0, 0.1);
        padding: 0.2rem 0.4rem;
        border-radius: 4px;
    }
    
    .input-area {
        padding: 0.75rem;
    }
}
