:root {
    --sidebar-width: 280px;
    --input-max-height: 150px;
    --primary: #4f46e5;
    --primary-hover: #4338ca;
    --bg: #f8f9fa;
    --bg-chat: #ffffff;
    --bg-sidebar: #1e1e2e;
    --bg-sidebar-hover: #2a2a3e;
    --bg-user-msg: #4f46e5;
    --bg-assistant-msg: #f1f3f5;
    --text-sidebar: #cdd6f4;
    --text-user-msg: #ffffff;
    --text-assistant-msg: #1e1e2e;
    --border-color: #e5e7eb;
    --mobile-header-height: 56px;
}

* {
    box-sizing: border-box;
}

html, body {
    height: 100%;
    margin: 0;
    overflow: hidden;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

/* Loading Screen */
.loading-screen {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100vh;
    background: var(--bg);
}

/* App Container */
.app-container {
    display: flex;
    height: 100vh;
    overflow: hidden;
    background: var(--bg);
}

/* Mobile Header */
.mobile-header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: var(--mobile-header-height);
    background: var(--bg-sidebar);
    color: white;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 8px;
    z-index: 1040;
}

/* Sidebar */
.sidebar {
    width: var(--sidebar-width);
    min-width: var(--sidebar-width);
    background: var(--bg-sidebar);
    color: var(--text-sidebar);
    display: flex;
    flex-direction: column;
    height: 100vh;
    overflow: hidden;
}

.sidebar-header {
    padding: 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid rgba(255,255,255,0.1);
    flex-shrink: 0;
}

.sidebar-header h5 {
    color: #ffffff;
}

.conversation-list {
    flex: 1;
    overflow-y: auto;
    padding: 8px;
}

.conversation-item {
    display: flex;
    align-items: center;
    padding: 10px 12px;
    border-radius: 8px;
    cursor: pointer;
    margin-bottom: 2px;
    transition: background 0.15s;
    color: var(--text-sidebar);
    text-decoration: none;
    gap: 8px;
}

.conversation-item:hover {
    background: var(--bg-sidebar-hover);
}

.conversation-item.active {
    background: var(--bg-sidebar-hover);
}

.conversation-item .conv-title {
    flex: 1;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-size: 0.9rem;
}

.conversation-item .conv-delete {
    opacity: 0;
    transition: opacity 0.15s;
    background: none;
    border: none;
    color: #888;
    padding: 2px 4px;
    cursor: pointer;
    flex-shrink: 0;
}

.conversation-item:hover .conv-delete {
    opacity: 1;
}

.conversation-item .conv-delete:hover {
    color: #ef4444;
}

.sidebar-footer {
    padding: 12px 16px;
    border-top: 1px solid rgba(255,255,255,0.1);
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
}

.user-info {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.85rem;
    overflow: hidden;
}

.user-info span {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Sidebar Backdrop (mobile) */
.sidebar-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 1049;
    display: none;
}

.sidebar-backdrop.show {
    display: block;
}

/* Chat Main */
.chat-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: var(--bg-chat);
    min-width: 0;
}

/* Welcome Screen */
.welcome-screen {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
}

.welcome-content {
    text-align: center;
    padding: 24px;
}

.welcome-content h3 {
    margin-top: 16px;
    margin-bottom: 8px;
}

/* Chat View */
.chat-view {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* Messages */
.messages-container {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    scroll-behavior: smooth;
}

.message {
    display: flex;
    margin-bottom: 16px;
    gap: 10px;
    animation: fadeIn 0.2s ease;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(4px); }
    to { opacity: 1; transform: translateY(0); }
}

.message.user {
    flex-direction: row-reverse;
}

.message-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    font-size: 0.85rem;
}

.message.user .message-avatar {
    background: var(--bg-user-msg);
    color: white;
}

.message.assistant .message-avatar {
    background: #e0e7ff;
    color: var(--primary);
}

.message-bubble {
    max-width: 75%;
    padding: 10px 14px;
    border-radius: 16px;
    line-height: 1.5;
    word-wrap: break-word;
    overflow-wrap: break-word;
}

.message.user .message-bubble {
    background: var(--bg-user-msg);
    color: var(--text-user-msg);
    border-bottom-right-radius: 4px;
}

.message.assistant .message-bubble {
    background: var(--bg-assistant-msg);
    color: var(--text-assistant-msg);
    border-bottom-left-radius: 4px;
}

.message-bubble p {
    margin: 0 0 8px 0;
}

.message-bubble p:last-child {
    margin-bottom: 0;
}

.message-bubble pre {
    background: #1e1e2e;
    color: #cdd6f4;
    padding: 12px;
    border-radius: 8px;
    overflow-x: auto;
    font-size: 0.85rem;
    margin: 8px 0;
}

.message-bubble code {
    font-family: 'Fira Code', 'Consolas', monospace;
    font-size: 0.85em;
}

.message-bubble :not(pre) > code {
    background: rgba(0,0,0,0.06);
    padding: 2px 5px;
    border-radius: 4px;
}

.message-bubble ul, .message-bubble ol {
    padding-left: 20px;
    margin: 4px 0;
}

.message-bubble table {
    border-collapse: collapse;
    margin: 8px 0;
    width: 100%;
    font-size: 0.9rem;
}

.message-bubble th, .message-bubble td {
    border: 1px solid var(--border-color);
    padding: 6px 10px;
    text-align: left;
}

.message-bubble th {
    background: rgba(0,0,0,0.04);
}

/* Tool indicator */
.tool-indicator {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 6px 10px;
    background: #f0f9ff;
    border-radius: 8px;
    font-size: 0.8rem;
    color: #0369a1;
    margin: 4px 0;
}

.tool-indicator .spinner-border {
    width: 14px;
    height: 14px;
    border-width: 2px;
}

/* Typing indicator */
.typing-indicator {
    display: flex;
    gap: 4px;
    padding: 4px 0;
}

.typing-indicator span {
    width: 6px;
    height: 6px;
    background: #94a3b8;
    border-radius: 50%;
    animation: bounce 1.4s infinite;
}

.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }

@keyframes bounce {
    0%, 60%, 100% { transform: translateY(0); }
    30% { transform: translateY(-6px); }
}

/* Input Area */
.input-area {
    padding: 12px 16px;
    border-top: 1px solid var(--border-color);
    background: var(--bg-chat);
    flex-shrink: 0;
}

.input-wrapper {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    max-width: 800px;
    margin: 0 auto;
    background: #f1f3f5;
    border-radius: 24px;
    padding: 6px 6px 6px 16px;
}

.input-wrapper textarea {
    flex: 1;
    border: none;
    background: transparent;
    resize: none;
    outline: none;
    font-size: 0.95rem;
    line-height: 1.5;
    max-height: var(--input-max-height);
    padding: 6px 0;
    font-family: inherit;
}

.send-btn {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    flex-shrink: 0;
}

.send-btn:disabled {
    opacity: 0.4;
}

/* Messages centered max-width */
.messages-container > .message {
    max-width: 800px;
    margin-left: auto;
    margin-right: auto;
}

/* Mobile Responsive */
@media (max-width: 767.98px) {
    .sidebar {
        position: fixed;
        top: 0;
        left: 0;
        bottom: 0;
        z-index: 1050;
        transform: translateX(-100%);
        transition: transform 0.3s ease;
    }

    .sidebar.open {
        transform: translateX(0);
    }

    .chat-main {
        padding-top: var(--mobile-header-height);
    }

    .message-bubble {
        max-width: 85%;
    }

    .input-area {
        padding: 8px 10px;
        padding-bottom: max(8px, env(safe-area-inset-bottom));
    }

    .welcome-content {
        padding: 16px;
    }

    .welcome-content h3 {
        font-size: 1.3rem;
    }
}

/* Scrollbar styling */
.messages-container::-webkit-scrollbar,
.conversation-list::-webkit-scrollbar {
    width: 6px;
}

.messages-container::-webkit-scrollbar-track,
.conversation-list::-webkit-scrollbar-track {
    background: transparent;
}

.messages-container::-webkit-scrollbar-thumb {
    background: #d1d5db;
    border-radius: 3px;
}

.conversation-list::-webkit-scrollbar-thumb {
    background: rgba(255,255,255,0.15);
    border-radius: 3px;
}

/* Empty state for conversations */
.conv-empty {
    padding: 24px 16px;
    text-align: center;
    color: rgba(255,255,255,0.4);
    font-size: 0.85rem;
}

/* Error message */
.message-error {
    color: #ef4444;
    font-size: 0.85rem;
    display: flex;
    align-items: center;
    gap: 6px;
    margin-top: 4px;
}

.message-error button {
    background: none;
    border: none;
    color: var(--primary);
    cursor: pointer;
    padding: 0;
    font-size: 0.85rem;
    text-decoration: underline;
}
