/* 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 fallback for mobile */
    height: 100vh;
    height: 100dvh; 
    height: -webkit-fill-available;
    
    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;
}

/* PC: Sidebar closed state */
.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 .item-text {
    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;
}
.primary-btn.small {
    padding: 5px 15px;
    font-size: 0.85rem;
}
.danger-btn {
    background-color: transparent;
    color: var(--danger-color);
    border: 1px solid var(--danger-color);
}

/* Settings Input */
.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;
}

/* --- FIX: Header Menu Toggle Visibility Logic --- */
/* Default (PC, Sidebar Open): Hide header toggle */
#header-menu-toggle {
    display: none; 
}

/* PC: When Sidebar is CLOSED, show header toggle */
#sidebar.closed + .chat-area #header-menu-toggle {
    display: flex;
}

.header-info {
    display: flex;
    flex-direction: column;
}
.header-actions {
    display: flex;
    align-items: center;
    gap: 10px;
}
.subtitle {
    font-size: 0.8rem;
    color: var(--text-secondary);
}

.mobile-only {
    display: none;
}
/* Memory Indicator */
.memory-indicator {
    color: var(--primary-color);
    animation: pulse 1.5s infinite;
    display: flex;
    align-items: center;
    justify-content: center;
}
@keyframes pulse {
    0% { opacity: 0.5; transform: scale(0.95); }
    50% { opacity: 1; transform: scale(1.05); }
    100% { opacity: 0.5; transform: scale(0.95); }
}

/* --- FIX: Scroll Behavior --- */
.messages-container {
    flex: 1;
    overflow-y: auto;
    /* Increased bottom padding to ensure last message clears input area visually */
    padding: 20px 20px 80px 20px;
    display: flex;
    flex-direction: column;
    gap: 20px;
    /* Disable smooth scroll for container to fix "stuck" streaming issues */
    scroll-behavior: auto; 
    -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);
    display: flex;
    flex-direction: column; 
    gap: 8px; 
    min-width: 200px; 
}

.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;
}

/* Message Actions Toolbar */
.msg-actions {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    margin-top: 5px;
    padding-top: 5px;
    border-top: 1px solid rgba(0,0,0,0.05);
}

.action-btn {
    background: none;
    border: 1px solid transparent;
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 6px;
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    gap: 4px;
    font-size: 0.75rem;
    font-weight: 500;
    transition: background 0.2s, color 0.2s;
}

.action-btn:hover {
    background-color: rgba(0,0,0,0.05);
    color: var(--primary-color);
}

.action-btn span.material-symbols-outlined {
    font-size: 16px; 
}

/* 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; 
}

/* --- Knowledge File List Scroll --- */
.scrollable-file-list {
    max-height: 150px;
    overflow-y: auto;
    border: 1px solid var(--border-color);
    border-radius: 5px;
    padding: 5px;
    background-color: #fafafa;
}

.file-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: white;
    padding: 5px 10px;
    border-radius: 5px;
    font-size: 0.85rem;
    border: 1px solid #eee;
}

/* --- Expanded Instruction Textarea --- */
.instructions-wrapper {
    position: relative;
    width: 100%;
}

textarea#gem-instructions {
    min-height: 120px;
    transition: all 0.2s ease;
    z-index: 1002;
    position: relative;
}

textarea#gem-instructions.expanded {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 90%;
    height: 60vh;
    z-index: 2100;
    box-shadow: 0 10px 30px rgba(0,0,0,0.3);
    border: 2px solid var(--primary-color);
    font-size: 1.1rem;
}

.instruction-backdrop {
    position: fixed;
    top: 0; left: 0;
    width: 100%; height: 100%;
    background: rgba(0,0,0,0.7);
    z-index: 2050;
}

#instruction-done-btn {
    position: fixed;
    bottom: 5vh;
    left: 50%;
    transform: translateX(-50%);
    z-index: 2101;
    box-shadow: 0 4px 10px rgba(0,0,0,0.3);
    min-width: 120px;
}

/* 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; }

/* Accessibility Helper */
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border-width: 0;
}

/* Responsive adjustments */
@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; 
    }
    
    /* FIX: Ensure header toggle is always visible on mobile */
    #header-menu-toggle {
        display: flex;
    }
}

@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;
    }

    textarea#gem-instructions.expanded {
        top: 0; 
        left: 0;
        transform: none;
        width: 100%;
        height: 50vh;
        border-radius: 0 0 10px 10px;
        border-left: none;
        border-right: none;
        border-top: none;
    }
    
    #instruction-done-btn {
        bottom: auto;
        top: 52vh;
    }
}