/* General Reset & Root Variables */
:root {
    --bg-color: #f0f2f5;
    --surface-color: #ffffff;
    --overlay-color: #e0e2e6;
    --text-color: #333333;
    --subtext-color: #666666;
    --accent-color: #007bff;
    --ai-message-bg: #e9ecef;
    --user-message-bg: #007bff;
    --user-message-text: #ffffff;
    --font-family: 'system-ui', 'Segoe UI', 'Roboto', 'Helvetica Neue', sans-serif;
    --border-radius: 20px; /* Increased for modern rounded look */
    --shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    --text-color-rgb: 51, 51, 51; /* RGB for --text-color */
}

body.dark-mode {
    --bg-color: #1e1e2e;
    --surface-color: #313244;
    --overlay-color: #45475a;
    --text-color: #cdd6f4;
    --subtext-color: #a6adc8;
    --accent-color: #89b4fa;
    --ai-message-bg: #585b70;
    --user-message-bg: #89b4fa;
    --user-message-text: #1e1e2e;
    --shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
    --text-color-rgb: 205, 214, 244; /* RGB for --text-color */
}

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

html, body {
    height: 100%;
    overflow: hidden;
}

body {
    font-family: var(--font-family);
    background-color: var(--bg-color);
    color: var(--text-color);
    display: flex;
    overflow: hidden;
}

/* Sidebar */
#sidebar {
    width: 280px;
    background-color: var(--surface-color);
    height: 100%;
    display: flex;
    flex-direction: column;
    box-shadow: var(--shadow);
    position: relative;
    left: 0;
    top: 0;
    z-index: 1001;
    border-radius: var(--border-radius); /* Apply to sidebar */
}

#sidebar header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem;
    background-color: var(--overlay-color);
    border-bottom: 1px solid var(--bg-color);
    border-top-left-radius: var(--border-radius);
    border-top-right-radius: var(--border-radius);
}

#sidebar header h2 {
    font-size: 1.25rem;
}

#chat-list {
    list-style: none;
    padding: 0;
    margin: 0;
    flex-grow: 1;
    overflow-y: auto;
}

#chat-list li {
    padding: 0.8rem 1rem;
    cursor: pointer;
    border-bottom: 1px solid var(--overlay-color);
    transition: background-color 0.2s;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

#chat-list li:hover {
    background-color: var(--overlay-color);
}

#chat-list li.active {
    background-color: var(--accent-color);
    color: var(--bg-color);
    font-weight: bold;
}

#chat-list li.active .delete-chat-btn {
    color: var(--bg-color);
}

.delete-chat-btn {
    background: none;
    border: none;
    color: var(--subtext-color);
    cursor: pointer;
    font-size: 1.2em;
    transition: color 0.2s;
}

.delete-chat-btn:hover {
    color: var(--text-color);
}

#sidebar-footer {
    padding: 1rem;
    border-top: 1px solid var(--overlay-color);
    text-align: center;
    border-bottom-left-radius: var(--border-radius);
    border-bottom-right-radius: var(--border-radius);
}

/* Main Chat Container */
#chat-container {
    flex-grow: 1; /* Allow it to take remaining space */
    height: 100%;
    max-height: 100vh; /* Adjusted for full height */
    background-color: var(--surface-color);
    border-radius: var(--border-radius); /* Apply to chat container */
    box-shadow: var(--shadow);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    margin: 0; /* Remove auto margin */
}

header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem;
    background-color: var(--overlay-color);
    border-top-left-radius: var(--border-radius);
    border-top-right-radius: var(--border-radius);
}

header h1 {
    font-size: 1.25rem;
    margin-right: auto;
}

#current-chat-title {
    font-size: 1.25rem;
    flex-grow: 1;
    text-align: center;
}

/* Chat Box */
#chat-box {
    flex-grow: 1;
    padding: 1rem;
    padding-bottom: 100px; /* Space for fixed input area */
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.message {
    display: flex;
    max-width: 80%;
    animation: fadeIn 0.3s ease-in-out;
}

.message .text {
    padding: 0.75rem 1rem;
    border-radius: 15px; /* More rounded for bubbles */
    line-height: 1.5;
}

.ai-message {
    align-self: flex-start;
}

.ai-message .text {
    background-color: var(--ai-message-bg);
}

.user-message {
    align-self: flex-end;
}

.user-message .text {
    background-color: var(--user-message-bg);
    color: var(--user-message-text);
}

.typing-indicator {
    align-self: flex-start;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.typing-indicator .dot {
    width: 8px;
    height: 8px;
    background-color: var(--subtext-color);
    border-radius: 50%;
    animation: bounce 1.4s infinite ease-in-out both;
}
.typing-indicator .dot:nth-child(1) { animation-delay: -0.32s; }
.typing-indicator .dot:nth-child(2) { animation-delay: -0.16s; }

/* Chat Form Wrapper (for centering and fixed position) */
#chat-form-wrapper {
    position: fixed;
    bottom: 0;
    left: 280px; /* Sidebar width */
    width: calc(100% - 280px); /* Remaining width */
    background-color: var(--surface-color);
    padding: 1rem;
    border-top: 1px solid var(--overlay-color);
    display: flex;
    justify-content: center;
    z-index: 1000;
    border-bottom-right-radius: var(--border-radius);
    border-bottom-left-radius: var(--border-radius);
}

#chat-form {
    display: flex;
    align-items: flex-end;
    width: 100%;
    max-width: 700px; /* Gemini-like max-width */
}

#message-input {
    flex-grow: 1;
    border: none;
    background-color: var(--overlay-color);
    color: var(--text-color);
    border-radius: 25px; /* Pill shape */
    padding: 0.75rem 1.5rem; /* Adjusted padding for pill shape */
    font-family: inherit;
    font-size: 1rem;
    resize: none;
    max-height: 150px;
    overflow-y: auto;
}

#message-input:focus {
    outline: none;
    box-shadow: 0 0 0 2px var(--accent-color);
}

#send-btn {
    margin-left: 0.5rem;
    background-color: var(--accent-color);
    color: var(--bg-color);
    border-radius: 50%; /* Circular button */
    width: 45px; /* Fixed width for circular */
    height: 45px; /* Fixed height for circular */
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0; /* Remove padding */
}
#send-btn:hover {
    background-color: var(--accent-color);
    opacity: 0.9;
}

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

.modal-content {
    background-color: var(--surface-color);
    border-radius: var(--border-radius); /* Apply to modal content */
    box-shadow: var(--shadow);
    width: 90%;
    max-width: 500px;
    overflow: hidden;
    animation: slideIn 0.3s ease-out;
}

.modal-content header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem;
    background-color: var(--overlay-color);
    border-top-left-radius: var(--border-radius);
    border-top-right-radius: var(--border-radius);
}

.modal-content main {
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 1rem; /* Increased gap for better spacing */
}

.modal-content label {
    color: var(--subtext-color);
    font-size: 0.9rem;
}

#api-key-input {
    width: 100%;
    padding: 0.75rem;
    border: 1px solid var(--overlay-color);
    background-color: var(--bg-color);
    color: var(--text-color);
    border-radius: 8px;
    font-size: 1rem;
}

#api-key-input:focus {
    outline: none;
    border-color: var(--accent-color);
}

.theme-setting {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    padding-top: 1rem;
    border-top: 1px solid var(--overlay-color);
}

.modal-content footer {
    padding: 1rem;
    text-align: right;
    background-color: var(--overlay-color);
    border-bottom-left-radius: var(--border-radius);
    border-bottom-right-radius: var(--border-radius);
}

#save-api-key-btn {
    background-color: var(--accent-color);
    color: var(--bg-color);
    border: none;
    padding: 0.75rem 1.5rem;
    border-radius: 8px;
    cursor: pointer;
    font-weight: bold;
    transition: opacity 0.2s;
}

#save-api-key-btn:hover {
    opacity: 0.9;
}

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

@keyframes slideIn {
    from { opacity: 0; transform: translateY(-20px); }
    to { opacity: 1; transform: translateY(0); }
}

@keyframes bounce {
    0%, 80%, 100% { transform: scale(0); }
    40% { transform: scale(1.0); }
}

/* Responsive */
@media (max-width: 600px) {
    body {
        flex-direction: column;
    }
    #sidebar {
        width: 100%;
        height: auto;
        position: relative;
        transform: translateX(0%);
        box-shadow: none;
        border-bottom: 1px solid var(--overlay-color);
    }
    #chat-container {
        max-width: 100%;
        height: calc(100vh - 100px); /* Adjust based on sidebar height */
        border-radius: 0;
        box-shadow: none;
    }
    #chat-form-wrapper {
        left: 0;
        width: 100%;
        border-radius: 0;
    }
}

/* Markdown Styles */
.message .text strong {
    font-weight: 600;
    color: var(--accent-color);
}

.message .text code {
    font-family: 'Courier New', Courier, monospace;
    background-color: var(--bg-color);
    padding: 0.2em 0.4em;
    border-radius: 4px;
    font-size: 0.9em;
}

.message .text pre {
    position: relative; /* For positioning the copy button */
    background-color: var(--bg-color);
    border: 1px solid var(--overlay-color);
    border-radius: 8px;
    padding: 1rem;
    margin: 0.5rem 0;
    overflow-x: auto;
    font-size: 0.9em;
}

.message .text pre code {
    background-color: transparent;
    padding: 0;
    border-radius: 0;
    font-size: 1em;
}

.copy-btn {
    position: absolute;
    top: 0.5rem;
    right: 0.5rem;
    background-color: var(--overlay-color);
    color: var(--text-color);
    border: none;
    border-radius: 6px;
    padding: 0.3rem 0.6rem;
    cursor: pointer;
    font-size: 0.8em;
    opacity: 0; /* Hidden by default */
    transition: opacity 0.2s, background-color 0.2s;
    display: flex;
    align-items: center;
    gap: 0.2em;
}

.copy-btn:hover {
    background-color: var(--accent-color);
    color: var(--bg-color);
}

.message .text pre:hover .copy-btn {
    opacity: 1; /* Show on hover */
}

.copy-btn.copied {
    background-color: #28a745; /* Green for copied state */
    color: white;
}

/* Headings */
.message .text h1, .message .text h2, .message .text h3, .message .text h4, .message .text h5, .message .text h6 {
    margin: 0.8em 0 0.4em;
    line-height: 1.3;
}
.message .text h1 { font-size: 1.8em; }
.message .text h2 { font-size: 1.5em; }
.message .text h3 { font-size: 1.3em; }
.message .text h4 { font-size: 1.1em; }
.message .text h5 { font-size: 1.0em; }
.message .text h6 { font-size: 0.9em; color: var(--subtext-color); }
