:root {
    --bg-color: #ffffff;
    --bg-color-alt: #f8f9fa;
    --border-color: #e5e7eb;
    --primary-color: #da7756; /* Subtle terracotta/peach accent */
    --primary-hover: #c46244;
    --text-main: #1f2937;
    --text-muted: #6b7280;
    --user-msg-bg: #f3f4f6;
    --bot-msg-bg: transparent;
    --success: #10b981;
    --warning: #f59e0b;
    --error: #ef4444;
    --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
    --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
    --shadow-lg: 0 10px 25px rgb(0 0 0 / 0.15);
    --shadow-input: 0 0 15px rgba(0,0,0,0.05);
    --radius-md: 0.5rem;
    --radius-lg: 0.75rem;
    --radius-full: 9999px;
}

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

body {
    font-family: 'Outfit', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background-color: var(--bg-color);
    color: var(--text-main);
    line-height: 1.6;
    height: 100vh;
    overflow: hidden;
}

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

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

.sidebar-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.sidebar-header h2 {
    font-size: 1rem;
    font-weight: 600;
    color: var(--text-main);
}

.close-sidebar {
    display: none;
}

.status-indicator {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.75rem;
    background: var(--bg-color);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    font-size: 0.875rem;
}

.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background-color: var(--warning);
}
.status-dot.ready { background-color: var(--success); }
.status-dot.error { background-color: var(--error); }

.kb-details {
    font-size: 0.875rem;
    color: var(--text-muted);
}
.kb-details span {
    color: var(--text-main);
    font-weight: 500;
}

.kb-actions h3 {
    font-size: 0.875rem;
    margin-bottom: 0.75rem;
    font-weight: 500;
    color: var(--text-muted);
}

.upload-form {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.sr-only,
.file-input-wrapper input[type="file"] {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

.file-label {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    padding: 0.75rem;
    border: 1px dashed var(--text-muted);
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: all 0.2s ease;
    text-align: center;
    color: var(--text-muted);
    font-size: 0.875rem;
}
.file-label:hover, .file-label.has-file {
    border-color: var(--text-main);
    color: var(--text-main);
    background: var(--bg-color);
}
.file-input-wrapper input[type="file"]:focus-visible + .file-label {
    outline: 2px solid var(--primary-color);
    outline-offset: 2px;
}

.btn {
    padding: 0.6rem 1rem;
    border: none;
    border-radius: var(--radius-md);
    font-family: inherit;
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    transition: all 0.2s ease;
    width: 100%;
}

.primary-btn {
    background-color: var(--primary-color);
    color: white;
}
.primary-btn:hover:not(:disabled) {
    background-color: var(--primary-hover);
}
.primary-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.secondary-btn {
    background-color: var(--bg-color);
    color: var(--text-main);
    border: 1px solid var(--border-color);
}
.secondary-btn:hover {
    background-color: #f3f4f6;
}

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

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

.chat-header {
    height: 56px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 1.5rem;
    background-color: var(--bg-color);
    z-index: 5;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.logo {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}
.logo h1 {
    font-size: 1.125rem;
    font-weight: 600;
    color: var(--text-main);
}

.icon-btn {
    background: transparent;
    border: none;
    color: var(--text-muted);
    font-size: 1.125rem;
    cursor: pointer;
    width: 36px;
    height: 36px;
    border-radius: var(--radius-md);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
}
.icon-btn:hover {
    color: var(--text-main);
    background: var(--bg-color-alt);
}
.icon-btn:focus-visible,
.btn:focus-visible,
.suggestion-btn:focus-visible,
.send-btn:focus-visible,
.citation-badge:focus-visible,
.source-tag:focus-visible {
    outline: 2px solid var(--primary-color);
    outline-offset: 2px;
}

#openSidebarBtn {
    display: none;
}

.chat-container {
    flex: 1;
    overflow-y: auto;
    padding: 2rem 1.5rem;
    display: flex;
    flex-direction: column;
    scroll-behavior: smooth;
}

.chat-container::-webkit-scrollbar {
    width: 6px;
}
.chat-container::-webkit-scrollbar-thumb {
    background: #d1d5db;
    border-radius: 4px;
}

.welcome-screen {
    margin: auto;
    max-width: 600px;
    text-align: center;
    padding: 2rem;
    animation: fadeIn 0.3s ease;
}
@keyframes fadeIn { from { opacity: 0; transform: translateY(5px); } to { opacity: 1; transform: translateY(0); } }

.welcome-icon {
    font-size: 2.5rem;
    color: var(--primary-color);
    margin-bottom: 1rem;
}

.welcome-screen h2 {
    font-size: 1.5rem;
    margin-bottom: 0.5rem;
    color: var(--text-main);
    font-weight: 500;
}

.welcome-screen p {
    color: var(--text-muted);
    margin-bottom: 2rem;
    font-size: 0.95rem;
}

.suggestions {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    align-items: center;
}

.suggestion-btn {
    background: var(--bg-color-alt);
    border: 1px solid var(--border-color);
    color: var(--text-main);
    padding: 0.8rem 1.25rem;
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: all 0.2s ease;
    width: 100%;
    max-width: 400px;
    text-align: left;
    display: flex;
    align-items: center;
    gap: 0.75rem;
    font-size: 0.95rem;
}
.suggestion-btn i {
    color: var(--text-muted);
}
.suggestion-btn:hover {
    background: #e5e7eb;
}

.messages-wrapper {
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
    max-width: 768px;
    width: 100%;
    margin: 0 auto;
}

.message {
    display: flex;
    gap: 1rem;
    animation: slideUp 0.3s ease;
    width: 100%;
}
@keyframes slideUp { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }

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

.avatar {
    width: 28px;
    height: 28px;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    font-size: 0.85rem;
}
.message.user .avatar {
    display: none;
}
.message.bot .avatar {
    background: var(--primary-color);
    color: white;
}
.typing-container .avatar {
    background: var(--primary-color);
    color: white;
}

.message-content {
    padding: 0;
    position: relative;
    font-size: 1rem;
    line-height: 1.6;
    color: var(--text-main);
    max-width: 85%;
}

.message.user .message-content {
    background: var(--user-msg-bg);
    padding: 0.75rem 1rem;
    border-radius: var(--radius-lg);
    border-bottom-right-radius: 0.25rem;
}

.message.bot .message-content {
    padding-top: 0.15rem;
}

/* Markdown specific styles */
.message-content p { margin-bottom: 0.75rem; }
.message-content p:last-child { margin-bottom: 0; }
.message-content a { color: var(--primary-color); text-decoration: none; }
.message-content a:hover { text-decoration: underline; }
.message-content ul, .message-content ol { margin-bottom: 0.75rem; padding-left: 1.5rem; }
.message-content strong { font-weight: 600; }

.citation-badge {
    display: inline-block;
    background: #fee2e2;
    color: #b91c1c;
    font-size: 0.7rem;
    padding: 0 0.3rem;
    border-radius: 4px;
    cursor: pointer;
    margin: 0 0.1rem;
    font-weight: 500;
    vertical-align: super;
    border: 0;
    font-family: inherit;
    line-height: 1.4;
}
.citation-badge:hover {
    background: #fca5a5;
}

.sources-container {
    margin-top: 1rem;
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
}

.source-tag {
    font-size: 0.75rem;
    background: var(--bg-color-alt);
    border: 1px solid var(--border-color);
    padding: 0.25rem 0.5rem;
    border-radius: var(--radius-md);
    display: flex;
    align-items: center;
    gap: 0.4rem;
    cursor: pointer;
    color: var(--text-muted);
    font-family: inherit;
}
.source-tag:hover {
    background: #e5e7eb;
    color: var(--text-main);
}

/* Typing Indicator */
.typing-container {
    display: flex;
    align-items: flex-start;
    gap: 1rem;
    margin-bottom: 1rem;
    max-width: 768px;
    width: 100%;
    margin-left: auto;
    margin-right: auto;
}

.typing-indicator {
    display: flex;
    align-items: center;
    gap: 4px;
    padding-top: 0.5rem;
}

.typing-indicator span {
    width: 5px;
    height: 5px;
    background-color: var(--text-muted);
    border-radius: 50%;
    animation: typing 1.4s infinite ease-in-out both;
}

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

@keyframes typing {
    0%, 80%, 100% { transform: scale(0); opacity: 0.4; }
    40% { transform: scale(1); opacity: 1; }
}

/* Chat Input */
.chat-input-area {
    padding: 1rem 1.5rem;
    background: var(--bg-color);
}

.input-wrapper {
    max-width: 768px;
    margin: 0 auto;
    position: relative;
    background: var(--bg-color-alt);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-input);
}
.input-wrapper:focus-within {
    border-color: #d1d5db;
    box-shadow: 0 0 0 2px rgba(0,0,0,0.05);
}

.chat-form {
    display: flex;
    position: relative;
    padding: 0.25rem;
}

#chatInput {
    flex: 1;
    background: transparent;
    border: none;
    color: var(--text-main);
    padding: 0.8rem 3rem 0.8rem 1rem;
    font-family: inherit;
    font-size: 1rem;
    outline: none;
}

.send-btn {
    position: absolute;
    right: 0.5rem;
    top: 50%;
    transform: translateY(-50%);
    width: 32px;
    height: 32px;
    border: none;
    background: var(--primary-color);
    color: white;
    border-radius: 4px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: opacity 0.2s;
}
.send-btn:hover:not(:disabled) {
    opacity: 0.9;
}
.send-btn:disabled {
    opacity: 0.5;
    background: var(--text-muted);
    cursor: not-allowed;
}

.disclaimer {
    font-size: 0.7rem;
    color: var(--text-muted);
    text-align: center;
    padding: 0.5rem 0;
    position: absolute;
    bottom: -24px;
    width: 100%;
}

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

.modal.active {
    display: flex;
}

.modal-content {
    width: 90%;
    max-width: 600px;
    background: var(--bg-color);
    border-radius: var(--radius-md);
    overflow: hidden;
    animation: zoomIn 0.2s ease;
    max-height: 85vh;
    display: flex;
    flex-direction: column;
    box-shadow: var(--shadow-lg);
}
@keyframes zoomIn { from { opacity: 0; transform: scale(0.97); } to { opacity: 1; transform: scale(1); } }

.modal-header {
    padding: 1.25rem 1.5rem;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.modal-body {
    padding: 1.5rem;
    overflow-y: auto;
    font-size: 0.95rem;
    line-height: 1.6;
}

.source-metadata {
    display: grid;
    gap: 0.75rem;
    padding: 1rem;
    margin-bottom: 1rem;
    background: var(--bg-color-alt);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
}

.source-metadata div {
    display: grid;
    grid-template-columns: 5rem 1fr;
    gap: 0.75rem;
}

.source-metadata dt {
    color: var(--text-muted);
    font-weight: 500;
}

.source-metadata dd {
    margin: 0;
}

.source-link {
    color: var(--primary-hover);
    font-weight: 500;
}

/* Overlay for Mobile */
.overlay {
    display: none;
    position: fixed;
    top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(0,0,0,0.3);
    z-index: 8;
}

/* Responsive */
@media (max-width: 768px) {
    .sidebar {
        position: fixed;
        left: 0;
        top: 0;
        transform: translateX(-100%);
    }
    .sidebar.open {
        transform: translateX(0);
    }
    .overlay.active {
        display: block;
    }
    #openSidebarBtn, .close-sidebar {
        display: flex;
    }
    .chat-header {
        padding: 0 1rem;
    }
    .message { flex-direction: column; gap: 0.5rem; }
    .message.user { align-items: flex-end; }
    .message.bot .avatar { display: none; }
    .message-content { max-width: 95%; }
}
