:root {
    color-scheme: dark;
    --bg-color: #121212;
    --chat-bg: #1e1e1e;
    --sidebar-bg: #171717;
    --text-color: #e0e0e0;
    --text-secondary: #a0a0a0;
    --primary-color: #bb86fc;
    --primary-variant: #3700b3;
    --secondary-color: #03dac6;
    --assistant-bg: #2c2c2c;
    --user-bg: #3700b3;
    --input-bg: #2c2c2c;
    --header-bg: rgba(30, 30, 30, 0.95);
    --border-color: #333;
    --hover-bg: rgba(255, 255, 255, 0.05);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    -webkit-tap-highlight-color: transparent;
}

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

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

/* Sidebar Styling */
.sidebar {
    width: 260px;
    background-color: var(--sidebar-bg);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    height: 100%;
    flex-shrink: 0;
    transition: width 0.3s ease, transform 0.3s ease;
    z-index: 20;
    overflow: hidden;
    white-space: nowrap;
}

.sidebar.collapsed {
    width: 0;
    border-right: none;
}

.sidebar-header {
    padding: 15px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    min-width: 260px;
}

.new-chat-btn {
    flex: 1;
    background-color: var(--border-color);
    color: var(--text-color);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 10px;
    display: flex;
    align-items: center;
    gap: 10px;
    cursor: pointer;
    font-size: 14px;
    transition: all 0.2s;
    width: 100%;
    justify-content: flex-start;
}

.new-chat-btn:hover {
    background-color: var(--hover-bg);
}

.conversation-list {
    flex: 1;
    overflow-y: auto;
    padding: 10px;
    display: flex;
    flex-direction: column;
    gap: 5px;
    min-width: 260px;
}

.conversation-item {
    padding: 10px 12px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 14px;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    transition: background-color 0.2s;
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: relative;
}

.conversation-item:hover {
    background-color: var(--hover-bg);
    color: var(--text-color);
}

.conversation-item.active {
    background-color: rgba(187, 134, 252, 0.1);
    color: var(--primary-color);
}

.conversation-item .delete-btn {
    opacity: 0;
    background: none;
    border: none;
    color: #cf6679;
    padding: 4px;
    cursor: pointer;
    border-radius: 4px;
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    transition: opacity 0.2s;
}

.conversation-item:hover .delete-btn {
    opacity: 1;
}

.conversation-item .delete-btn:hover {
    background-color: rgba(207, 102, 121, 0.1);
}

.sidebar-footer {
    padding: 15px;
    border-top: 1px solid var(--border-color);
    min-width: 260px;
}

.text-btn {
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 13px;
    padding: 5px;
}

.text-btn:hover {
    color: var(--text-color);
    text-decoration: underline;
}

/* Main Content Styling */
.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    height: 100%;
    position: relative;
    background-color: var(--chat-bg);
    min-width: 0; /* Prevents overflow issues */
}

header {
    padding: 10px 20px;
    background-color: var(--header-bg);
    backdrop-filter: blur(12px);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
    z-index: 10;
    height: 60px;
}

.header-left, .header-right {
    display: flex;
    align-items: center;
    gap: 10px;
}

header h1 {
    font-size: 18px;
    font-weight: 700;
    background: linear-gradient(90deg, var(--primary-color), var(--secondary-color));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    letter-spacing: 0.5px;
    margin: 0;
}

/* Menu button hidden on desktop by default, shown if sidebar is collapsed */
#menu-btn {
    display: none;
}

.sidebar.collapsed + .main-content #menu-btn {
    display: flex; /* Show menu button in header when sidebar is collapsed */
}

.icon-btn {
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 8px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
}

.icon-btn:hover {
    background-color: var(--hover-bg);
    color: var(--text-color);
}

.desktop-only {
    display: flex;
}

#model-select {
    background-color: rgba(255, 255, 255, 0.05);
    color: var(--text-color);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 6px 12px;
    font-size: 13px;
    outline: none;
    cursor: pointer;
    transition: all 0.2s ease;
    max-width: 200px;
}

#model-select:hover {
    background-color: rgba(255, 255, 255, 0.1);
    border-color: #555;
}

#model-select option {
    background-color: var(--chat-bg);
    color: var(--text-color);
    font-size: 12px; /* Better for small screens */
}

.chat-history {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 20px;
    scroll-behavior: smooth;
    max-width: 900px;
    margin: 0 auto;
    width: 100%;
}

.message {
    display: flex;
    max-width: 90%;
    opacity: 0;
    animation: fadeIn 0.3s forwards;
}

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

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

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

.bubble {
    padding: 12px 18px;
    border-radius: 12px;
    font-size: 15px;
    word-wrap: break-word;
    box-shadow: 0 2px 5px rgba(0,0,0,0.1);
    line-height: 1.6;
}

.message.assistant .bubble {
    background-color: var(--assistant-bg);
    color: #eee;
}

.message.user .bubble {
    background-color: var(--user-bg);
    color: #fff;
}

.input-area {
    padding: 20px;
    background-color: var(--chat-bg);
    display: flex;
    gap: 12px;
    align-items: flex-end;
    max-width: 900px;
    margin: 0 auto;
    width: 100%;
}

input[type="text"] {
    flex: 1;
    background-color: var(--input-bg);
    border: 1px solid var(--border-color);
    border-radius: 24px;
    padding: 14px 20px;
    color: white;
    font-size: 16px;
    outline: none;
    transition: all 0.2s ease;
}

input[type="text"]:focus {
    background-color: #333;
    border-color: var(--primary-color);
}

#send-btn {
    background-color: var(--primary-color);
    border: none;
    border-radius: 50%;
    width: 48px;
    height: 48px;
    display: flex;
    justify-content: center;
    align-items: center;
    color: #000;
    cursor: pointer;
    flex-shrink: 0;
    transition: transform 0.1s ease, background-color 0.2s;
}

#send-btn:hover {
    transform: scale(1.05);
    background-color: #a370f7;
}

#send-btn:disabled {
    background-color: #444;
    color: #888;
    cursor: not-allowed;
    transform: none;
}

/* Modal Styling */
.modal {
    display: none;
    position: fixed;
    z-index: 100;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0,0,0,0.6);
    backdrop-filter: blur(4px);
}

.modal-content {
    background-color: var(--chat-bg);
    margin: 5vh auto;
    padding: 0;
    border: 1px solid var(--border-color);
    border-radius: 12px;
    width: 90%;
    max-width: 500px;
    box-shadow: 0 4px 20px rgba(0,0,0,0.3);
    animation: slideDown 0.3s ease;
    max-height: 90vh;
    display: flex;
    flex-direction: column;
}

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

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

.modal-header h2 {
    font-size: 18px;
    font-weight: 600;
}

.close-btn {
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 24px;
    cursor: pointer;
    padding: 0;
    line-height: 1;
}

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

.modal-body {
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 20px;
    overflow-y: auto;
}

.setting-item {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.setting-item label {
    font-size: 14px;
    color: var(--text-secondary);
    display: flex;
    justify-content: space-between;
}

.setting-item input[type="range"] {
    width: 100%;
    accent-color: var(--primary-color);
}

.setting-item input[type="number"] {
    background-color: var(--input-bg);
    border: 1px solid var(--border-color);
    color: var(--text-color);
    padding: 8px;
    border-radius: 4px;
    width: 100%;
}

/* Citation / Details Styling */
details {
    background-color: rgba(255, 255, 255, 0.05);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    margin-top: 10px;
    overflow: hidden;
}

summary {
    padding: 8px 12px;
    cursor: pointer;
    font-size: 0.9em;
    color: var(--text-secondary);
    user-select: none;
    background-color: rgba(255, 255, 255, 0.02);
    font-weight: 500;
    display: flex;
    align-items: center;
    gap: 6px;
}

summary::marker {
    color: var(--text-secondary);
}

summary:hover {
    color: var(--text-color);
    background-color: rgba(255, 255, 255, 0.05);
}

details[open] summary {
    border-bottom: 1px solid var(--border-color);
}

details .content {
    padding: 10px 15px;
    font-size: 0.9em;
}

/* Mobile Responsive */
.mobile-only {
    display: none;
}

@media (max-width: 768px) {
    #menu-btn {
        display: flex; /* Always show menu button on mobile */
    }

    .desktop-only {
        display: none !important;
    }

    .sidebar {
        position: fixed;
        left: 0;
        top: 0;
        bottom: 0;
        transform: translateX(-100%);
        box-shadow: 2px 0 10px rgba(0,0,0,0.5);
        width: 260px !important; /* Force width on mobile */
    }

    .sidebar.show {
        transform: translateX(0);
    }
    
    .mobile-only {
        display: flex;
    }

    header h1 {
        font-size: 16px;
    }

    #model-select {
        max-width: 140px;
        font-size: 12px;
        padding: 5px 10px;
    }
    
    .input-area {
        padding: 10px;
    }
    
    #send-btn {
        width: 40px;
        height: 40px;
    }
}

/* Apple Watch & Small Screen Adaptation */
@media (max-width: 320px) {
    header {
        padding: 5px 10px;
        height: 50px;
    }

    header h1 {
        font-size: 14px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        max-width: 100px; /* Reduced to make space for model selector */
    }

    #model-select {
        display: block; /* Show model selector but make it compact */
        max-width: 100px;
        padding: 4px 8px;
        font-size: 11px;
        height: 32px;
        margin-right: 5px;
    }

    .header-right {
        gap: 5px;
    }

    .chat-history {
        padding: 10px;
    }

    .bubble {
        padding: 8px 12px;
        font-size: 14px;
    }

    .input-area {
        padding: 8px;
        gap: 8px;
    }

    input[type="text"] {
        padding: 10px 14px;
        font-size: 14px;
    }

    #send-btn {
        width: 36px;
        height: 36px;
    }
    
    /* Ensure close button in sidebar is visible/accessible */
    #close-sidebar-btn {
        padding: 10px;
    }
}

/* Scrollbar styling */
::-webkit-scrollbar {
    width: 6px;
}
::-webkit-scrollbar-track {
    background: transparent;
}
::-webkit-scrollbar-thumb {
    background: #444;
    border-radius: 3px;
}
::-webkit-scrollbar-thumb:hover {
    background: #555;
}

/* Markdown Styling */
.bubble h1, .bubble h2, .bubble h3 {
    margin-top: 0.8em;
    margin-bottom: 0.4em;
    font-weight: 600;
    line-height: 1.2;
}

.bubble h1 { font-size: 1.5em; border-bottom: 1px solid rgba(255,255,255,0.2); padding-bottom: 5px; }
.bubble h2 { font-size: 1.3em; }
.bubble h3 { font-size: 1.1em; }

.bubble p { margin-bottom: 0.8em; }
.bubble p:last-child { margin-bottom: 0; }

.bubble ul, .bubble ol { margin-bottom: 0.8em; padding-left: 20px; }

.bubble code {
    background-color: rgba(0, 0, 0, 0.3);
    padding: 2px 4px;
    border-radius: 4px;
    font-family: 'Menlo', 'Monaco', 'Courier New', monospace;
    font-size: 0.9em;
}

.bubble pre {
    background-color: #121212;
    padding: 12px;
    border-radius: 8px;
    overflow-x: auto;
    margin-bottom: 0.8em;
    border: 1px solid #333;
}

.bubble pre code {
    background-color: transparent;
    padding: 0;
    font-size: 0.9em;
    color: #f8f8f2;
}

.bubble blockquote {
    border-left: 3px solid var(--primary-color);
    margin: 0 0 10px 0;
    padding-left: 10px;
    color: #a0a0a0;
}

.bubble a { color: var(--secondary-color); text-decoration: none; }
.bubble a:hover { text-decoration: underline; }
