/* Variables */
:root {
    --bg-color: #f0f4f9;
    --sidebar-bg: #f0f4f9;
    --chat-bg: #ffffff;
    --text-primary: #1f1f1f;
    --text-secondary: #444746;
    --primary-color: #0b57d0;
    --accent-color: #d3e3fd;
    --user-msg-bg: #f0f4f9;
    --ai-msg-bg: #ffffff;
    --border-color: #e1e3e1;
    --danger-color: #b3261e;
    --sidebar-width: 280px;
}

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

body {
    font-family: 'Roboto', sans-serif;
    background-color: var(--bg-color);
    color: var(--text-primary);
    height: 100vh;
    height: 100dvh; 
    overflow: hidden;
    -webkit-tap-highlight-color: transparent;
}

/* Layout */
.app-container {
    display: flex;
    height: 100%;
    width: 100%;
}

/* Sidebar */
.sidebar {
    width: var(--sidebar-width);
    background-color: var(--sidebar-bg);
    display: flex;
    flex-direction: column;
    padding: 1rem;
    border-right: 1px solid var(--border-color);
    transition: transform 0.3s ease;
    z-index: 100;
}

.sidebar.closed {
    transform: translateX(-100%);
    position: absolute;
    height: 100%;
}

.sidebar-header, .sidebar-content, .sidebar-footer {
    padding: 0 8px;
}

.sidebar-header {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 20px;
}

.sidebar-content {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.sidebar-footer {
    margin-top: auto;
    padding-top: 10px;
    border-top: 1px solid var(--border-color);
}

.section-title {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 0.85rem;
    font-weight: 500;
    color: var(--text-secondary);
    margin-bottom: 5px;
}

.scrollable-list {
    max-height: 40vh;
    overflow-y: auto;
    margin-bottom: 10px;
}
.scrollable-list::-webkit-scrollbar { width: 6px; }
.scrollable-list::-webkit-scrollbar-thumb { background-color: rgba(0,0,0,0.2); border-radius: 3px; }

.divider {
    height: 1px;
    background-color: var(--border-color);
    margin: 5px 0;
}

/* List Items */
.list-item {
    padding: 8px 12px;
    border-radius: 20px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 0.9rem;
    color: var(--text-primary);
    transition: background 0.2s;
    min-height: 40px;
    position: relative;
    overflow: hidden;
}
.list-item span {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}


.list-item:hover {
    background-color: #e0e4e9;
}

.list-item.active {
    background-color: var(--accent-color);
    color: var(--primary-color);
    font-weight: 500;
}

.list-item-actions {
    display: none;
    align-items: center;
    gap: 2px;
    flex-shrink: 0;
}

@media (hover: hover) {
    .list-item:hover .list-item-actions {
        display: flex;
    }
}

.list-item-actions .icon-btn {
    width: 28px;
    height: 28px;
    background-color: rgba(255,255,255,0.5);
}
.list-item-actions .icon-btn:hover {
    background-color: rgba(255,255,255,1);
    color: var(--primary-color);
}

/* Buttons */
.icon-btn {
    background: none;
    border: none;
    cursor: pointer;
    padding: 5px;
    border-radius: 50%;
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    justify-content: center;
}
.icon-btn:hover {
    background-color: rgba(0,0,0,0.05);
}
.icon-btn.small span {
    font-size: 18px;
}
.sidebar-btn {
    width: 100%;
    text-align: left;
    background: none;
    border: none;
    padding: 10px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 10px;
    border-radius: 8px;
    color: var(--text-primary);
}
.sidebar-btn:hover {
    background-color: #e0e4e9;
}

.primary-btn, .danger-btn {
    border: none;
    padding: 10px 20px;
    border-radius: 20px;
    cursor: pointer;
    font-weight: 500;
}
.primary-btn {
    background-color: var(--primary-color);
    color: white;
}
.danger-btn {
    background-color: transparent;
    color: var(--danger-color);
    border: 1px solid var(--danger-color);
}

/* Settings Input with Delete Button */
.input-with-btn {
    display: flex;
    gap: 10px;
    align-items: center;
}
.input-with-btn input {
    flex: 1;
}
.border-btn {
    border: 1px solid var(--border-color);
    border-radius: 5px;
    height: 42px;
    width: 42px;
    flex-shrink: 0;
}
.border-btn:hover {
    background-color: #ffebee;
    color: var(--danger-color);
    border-color: var(--danger-color);
}

/* Chat Area */
.chat-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    background-color: var(--chat-bg);
    position: relative;
    width: 100%;
}

.chat-header {
    padding: 10px 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid var(--border-color);
    background-color: white;
    min-height: 60px;
}

.header-info {
    display: flex;
    flex-direction: column;
}
.subtitle {
    font-size: 0.8rem;
    color: var(--text-secondary);
}

.mobile-only {
    display: none;
}

.messages-container {
    flex: 1;
    overflow-y: auto;
    padding: 20px 20px 40px 20px;
    display: flex;
    flex-direction: column;
    gap: 20px;
    scroll-behavior: smooth;
    -webkit-overflow-scrolling: touch;
}

.welcome-message {
    text-align: center;
    margin-top: 50px;
    color: var(--text-secondary);
}

/* Message Bubbles */
.message {
    display: flex;
    gap: 15px;
    max-width: 800px;
    margin: 0 auto;
    width: 100%;
    position: relative;
}

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

.avatar {
    width: 30px;
    height: 30px;
    border-radius: 50%;
    background-color: #ccc;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    flex-shrink: 0;
}
.message.ai .avatar { background-color: #e7f0fe; color: var(--primary-color); }
.message.user .avatar { background-color: var(--text-secondary); color: white; }

.msg-content {
    padding: 10px 15px;
    border-radius: 12px;
    background-color: var(--ai-msg-bg);
    line-height: 1.6;
    font-size: 0.95rem;
    flex: 1;
    overflow-wrap: break-word;
    border: 1px solid var(--border-color);
}

.message.user .msg-content {
    background-color: var(--user-msg-bg);
    border-radius: 18px 4px 18px 18px;
    border: none;
}
.message.ai .msg-content {
    border-radius: 4px 18px 18px 18px;
}

.msg-actions {
    display: none;
    position: absolute;
    bottom: -30px;
    right: 0;
    gap: 5px;
    background: white;
    box-shadow: 0 2px 5px rgba(0,0,0,0.1);
    border-radius: 15px;
    padding: 4px 8px;
    z-index: 10;
}
.message.user .msg-actions { left: 0; right: auto; }

/* Markdown Styles */
.msg-content pre {
    background: #f4f4f4;
    padding: 10px;
    border-radius: 5px;
    overflow-x: auto;
}
.msg-content code {
    font-family: monospace;
    background-color: rgba(0,0,0,0.05);
    padding: 2px 4px;
    border-radius: 3px;
}
.msg-content p:last-child {
    margin-bottom: 0;
}

/* Input Area */
.input-area {
    padding: 20px;
    padding-bottom: calc(20px + env(safe-area-inset-bottom));
    display: flex;
    justify-content: center;
    background: linear-gradient(to top, white 80%, rgba(255,255,255,0));
    flex-shrink: 0;
    z-index: 10;
}

.input-wrapper {
    max-width: 800px;
    width: 100%;
    position: relative;
    background-color: var(--user-msg-bg);
    border-radius: 25px;
    padding: 10px 15px;
    display: flex;
    align-items: flex-end;
}

textarea#user-input {
    width: 100%;
    border: none;
    background: transparent;
    resize: none;
    max-height: 200px;
    outline: none;
    padding: 10px;
    font-family: inherit;
    font-size: 16px; 
    touch-action: manipulation;
}

.send-btn {
    background-color: transparent;
    color: var(--text-secondary);
    padding: 10px;
    margin-bottom: 2px;
}
.send-btn:hover {
    color: var(--primary-color);
}
.send-btn:disabled {
    color: #ccc;
    cursor: not-allowed;
}

/* Modals */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0,0,0,0.5);
    z-index: 1000;
    align-items: center;
    justify-content: center;
}

.modal-content {
    background-color: white;
    padding: 25px;
    border-radius: 15px;
    width: 90%;
    max-width: 500px;
    position: relative;
    display: flex;
    flex-direction: column;
    gap: 15px;
    max-height: 90vh;
    overflow-y: auto;
}
.modal-content.large {
    max-width: 800px;
}
.modal-actions {
    display: flex;
    gap: 10px;
    justify-content: flex-end;
    margin-top: 10px;
}

.close-modal {
    position: absolute;
    top: 15px;
    right: 20px;
    font-size: 24px;
    cursor: pointer;
}

input[type="text"], input[type="password"], select, textarea {
    width: 100%;
    padding: 10px;
    border: 1px solid var(--border-color);
    border-radius: 5px;
    font-family: inherit;
    font-size: 16px; 
}

/* File List */
.file-list {
    margin-top: 10px;
    display: flex;
    flex-direction: column;
    gap: 5px;
}
.file-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: #f0f4f9;
    padding: 5px 10px;
    border-radius: 5px;
    font-size: 0.85rem;
}

/* Toast */
.toast {
    position: fixed;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    background-color: #323232;
    color: white;
    padding: 10px 20px;
    border-radius: 5px;
    z-index: 2000;
    opacity: 1;
    transition: opacity 0.3s, transform 0.3s;
}
.toast.hidden {
    opacity: 0;
    transform: translate(-50%, 20px);
    pointer-events: none;
}

/* Loading Spinner */
@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

.spinner {
    border: 4px solid rgba(0,0,0,0.1);
    border-left-color: var(--primary-color);
    border-radius: 50%;
    width: 30px;
    height: 30px;
    animation: spin 1s linear infinite;
    margin: 0 auto;
}
#loading-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(255, 255, 255, 0.7);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 3000;
}
.hidden { display: none !important; }

/* Responsive adjustments */
/* Tablet & Mobile */
@media (max-width: 1024px) {
    .sidebar {
        position: absolute;
        height: 100%;
        transform: translateX(-100%);
        box-shadow: 2px 0 10px rgba(0,0,0,0.1);
    }
    .sidebar.open {
        transform: translateX(0);
    }
    .mobile-only {
        display: flex;
    }
    #menu-toggle {
        display: none; 
    }
}

/* Mobile Specific */
@media (max-width: 768px) {
    .input-area {
        padding: 10px 15px;
        padding-bottom: calc(15px + env(safe-area-inset-bottom));
        background: #fff;
        border-top: 1px solid var(--border-color);
    }
    
    .input-wrapper {
        padding: 8px 12px;
        border-radius: 20px;
    }
    
    .chat-header {
        padding: 10px 15px;
    }

    .message.show-actions .msg-actions {
        display: flex;
    }
}