:root {
    /* Light Mode (Default) */
    --bg-color: #f8fafc;
    --sidebar-bg: #ffffff;
    --chat-bg: #ffffff;
    --input-bg: #ffffff;
    --text-primary: #0f172a;
    --text-secondary: #64748b;
    --border-color: #e2e8f0;
    --accent: #6366f1;
    /* Indigo */
    --accent-hover: #4f46e5;
    --user-msg-bg: #6366f1;
    --user-msg-text: #ffffff;
    --ai-msg-bg: #f1f5f9;
    --ai-msg-text: #334155;
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
}

/* Dark Mode Overrides */
[data-theme="dark"] {
    --bg-color: #0f172a;
    --sidebar-bg: #1e293b;
    --chat-bg: #0f172a;
    --input-bg: #1e293b;
    --text-primary: #f1f5f9;
    --text-secondary: #94a3b8;
    --border-color: #334155;
    --accent: #818cf8;
    --accent-hover: #6366f1;
    --user-msg-bg: #818cf8;
    --user-msg-text: #0f172a;
    --ai-msg-bg: #1e293b;
    --ai-msg-text: #e2e8f0;
}

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

body {
    font-family: 'Inter', sans-serif;
    background-color: var(--bg-color);
    color: var(--text-primary);
    min-height: 100vh;
    overflow-x: hidden;
    transition: background-color 0.3s, color 0.3s;
}

.app-container {
    display: flex;
    min-height: 100vh;
}

/* Sidebar */
.sidebar {
    width: 310px;
    background-color: var(--sidebar-bg);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    padding: 5px;
    transition: background-color 0.3s;
}

.sidebar-right {
    border-right: none;
    border-left: 1px solid var(--border-color);
}

.sidebar-header {
    padding: 0.75rem;
}

.sidebar-header h3 {
    font-size: 1rem;
    font-weight: 600;
    color: var(--text-primary);
    margin: 0;
}

#new-chat-btn {
    width: 100%;
    padding: 0.875rem;
    background: var(--input-bg);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    border-radius: 8px;
    cursor: pointer;
    font-weight: 500;
    transition: all 0.2s;
    box-shadow: var(--shadow-sm);
    margin: 0.75rem;
    width: calc(100% - 1.5rem);
}

#new-chat-btn:hover {
    border-color: var(--accent);
    color: var(--accent);
}

.history-list {
    flex: 1;
    overflow-y: auto;
    margin-top: 1rem;
    padding: 0 0.75rem;
}

.sidebar-footer {
    margin-top: auto;
    border-top: 1px solid var(--border-color);
    padding-top: 1rem;
    padding: 1rem 0.75rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

/* Logo Styling */
.brand-link {
    display: block;
    text-decoration: none;
    margin-bottom: 0.5rem;
}

.brand-logo {
    width: 100%;
    max-width: 240px;
    /* Max width constraint */
    display: block;
    object-fit: contain;
    transition: filter 0.3s;
    margin: auto;
}

/* Invert logo in light mode so white text becomes black */
[data-theme="light"] .brand-logo {
    filter: invert(1);
}

.social-links {
    display: flex;
    justify-content: center;
    gap: 1rem;
    margin-bottom: 0.5rem;
}

.version-text {
    display: block;
    text-align: right;
    font-size: 0.7rem;
    color: var(--text-secondary);
    max-width: 240px;
    margin: 2px auto 0;
    font-weight: 500;
}

.social-link {
    color: var(--text-secondary);
    transition: color 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.social-link:hover {
    color: var(--accent);
}

.history-item {
    padding: 0.75rem;
    font-size: 0.9rem;
    color: var(--text-secondary);
    cursor: pointer;
    border-radius: 6px;
    margin-bottom: 4px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    transition: 0.2s;
}

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

.history-item.active {
    background-color: var(--ai-msg-bg);
    color: var(--accent);
    font-weight: 500;
}

.footer-nav a {
    font-size: 0.8rem;
    color: var(--text-secondary);
    text-decoration: none;
    margin-right: 10px;
}

.footer-nav a:hover {
    text-decoration: underline;
}

#theme-toggle,
#mobile-theme-toggle {
    background: none;
    border: none;
    cursor: pointer;
    font-size: 1.2rem;
    padding: 4px;
    border-radius: 4px;
}

[data-theme="dark"] .icon-moon {
    display: none;
}

[data-theme="light"] .icon-sun {
    display: none;
}

/* Main Interface */
.chat-interface {
    flex: 1;
    display: flex;
    flex-direction: column;
    position: relative;
    background-color: var(--bg-color);
    min-height: 0;
}

.mobile-header {
    display: none;
    min-height: 60px;
    padding: 0 1rem;
    border-bottom: 1px solid var(--border-color);
    align-items: center;
    justify-content: center;
    gap: 0.75rem;
}

.mobile-header .brand-link {
    flex: 1;
    display: flex;
    justify-content: center;
}

.mobile-header .nav-toggle,
.mobile-header .theme-btn-mobile {
    flex: 0 0 auto;
}

.theme-btn-mobile {
    background: none;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 6px 10px;
    font-size: 1.1rem;
    cursor: pointer;
    color: var(--text-primary);
    background-color: var(--input-bg);
}


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

.welcome-message {
    text-align: center;
    margin-top: 10vh;
    max-width: 600px;
    align-self: center;
}

.welcome-message h1 {
    font-size: 2rem;
    margin-bottom: 1rem;
    background: linear-gradient(135deg, var(--accent), #a855f7);
    background-clip: text;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

.features-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 1rem;
    margin-top: 2rem;
}

.feature-card {
    padding: 1rem;
    background: var(--ai-msg-bg);
    border-radius: 8px;
    font-size: 0.9rem;
    color: var(--text-secondary);
    border: 1px solid var(--border-color);
}

/* Message Bubbles */
.message {
    max-width: 850px;
    margin: 0 auto;
    width: 100%;
    padding: 1.5rem;
    border-radius: 12px;
    font-size: 1rem;
    line-height: 1.6;
    position: relative;
    animation: fadeIn 0.3s ease;
}

.message.user {
    background-color: var(--user-msg-bg);
    color: var(--user-msg-text);
    border-radius: 12px 12px 2px 12px;
    max-width: 80%;
    margin-right: 0;
    /* Align right */
    margin-left: auto;
}

.message.ai {
    background-color: var(--ai-msg-bg);
    color: var(--ai-msg-text);
    border: 1px solid var(--border-color);
    border-radius: 12px 12px 12px 2px;
    max-width: 100%;
    /* AI takes full width for readability */
    white-space: pre-wrap;
}

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

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Copy Button */
.copy-btn {
    position: absolute;
    top: 10px;
    right: 10px;
    background: transparent;
    border: 1px solid var(--border-color);
    color: var(--text-secondary);
    padding: 4px 8px;
    font-size: 0.75rem;
    border-radius: 4px;
    cursor: pointer;
    opacity: 0;
    transition: opacity 0.2s;
}

.message.ai:hover .copy-btn {
    opacity: 1;
}

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

.copy-btn.copied {
    border-color: var(--accent);
    color: var(--accent);
}

/* Hide copy button on mobile */
@media (max-width: 768px) {
    .copy-btn {
        display: none;
    }
}

/* Input Area */
.input-area {
    padding: 2rem;
    background: var(--bg-color);
    border-top: 1px solid var(--border-color);
}

#prompt-form {
    max-width: 850px;
    margin: 0 auto;
}

.input-wrapper {
    position: relative;
    display: flex;
    align-items: center;
}

#user-input {
    width: 100%;
    padding: 1.25rem 3.5rem 1.25rem 1.5rem;
    background-color: var(--input-bg);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    color: var(--text-primary);
    font-size: 1rem;
    outline: none;
    box-shadow: var(--shadow-sm);
    transition: border-color 0.2s;
}

#user-input:disabled {
    opacity: 0.6;
    cursor: not-allowed;
    background-color: var(--ai-msg-bg);
}

#user-input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.1);
}

#send-btn {
    position: absolute;
    right: 12px;
    background: var(--accent);
    border: none;
    width: 40px;
    height: 40px;
    border-radius: 8px;
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
}

#send-btn:hover {
    background-color: var(--accent-hover);
}

#send-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    background-color: var(--text-secondary);
}

.disclaimer {
    text-align: center;
    font-size: 0.75rem;
    color: var(--text-secondary);
    margin-top: 0.75rem;
    opacity: 0.7;
}

/* Static Page Styles (About, Terms, etc) */
.static-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    min-height: 60px;
    padding: 0.75rem 1.5rem;
    border-bottom: 1px solid var(--border-color);
    background-color: var(--sidebar-bg);
    position: relative;
    z-index: 2;
}

.static-header .brand-link {
    display: flex;
    align-items: center;
    margin: 0;
    text-decoration: none;
    color: var(--text-primary);
    font-weight: 600;
}

.static-header .brand-link img {
    max-width: 220px;
    width: 100%;
    height: auto;
    display: block;
    object-fit: contain;
}

.nav-toggle {
    display: none;
    flex-direction: column;
    justify-content: space-between;
    width: 42px;
    height: 32px;
    padding: 4px 2px;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background: var(--input-bg);
    cursor: pointer;
}

.nav-toggle span {
    display: block;
    height: 2px;
    width: 100%;
    background: var(--text-primary);
    border-radius: 999px;
}

.mobile-nav-panel {
    display: none;
    flex-direction: column;
    gap: 0.5rem;
    padding: 0.75rem 1rem 1.25rem;
    background: var(--sidebar-bg);
    border-bottom: 1px solid var(--border-color);
}

.mobile-nav-panel.open {
    display: flex;
}

.mobile-nav-panel a {
    text-decoration: none;
    color: var(--text-primary);
    font-weight: 500;
    padding: 0.3rem 0;
}

.static-nav {
    display: flex;
    align-items: center;
    gap: 1rem;
    position: relative;
}

.mobile-nav-toggle {
    display: none;
    align-items: center;
    gap: 0.4rem;
    padding: 0.5rem 0.9rem;
    border-radius: 8px;
    border: 1px solid var(--border-color);
    background: var(--input-bg);
    color: var(--text-primary);
    cursor: pointer;
    font-weight: 500;
}

.static-nav-links {
    display: flex;
    gap: 1rem;
}

.static-nav-links a {
    color: var(--text-secondary);
    text-decoration: none;
    font-weight: 500;
}

.static-nav-links a:hover,
.static-nav-links a:focus-visible {
    color: var(--accent);
}

.static-page {
    max-width: 800px;
    margin: 0 auto;
    padding: 3rem 2rem 4rem;
    line-height: 1.7;
}

.static-page h1 {
    font-size: 2.5rem;
    margin-bottom: 2rem;
    color: var(--text-primary);
}

.static-page h2 {
    font-size: 1.5rem;
    margin-top: 2rem;
    margin-bottom: 1rem;
    color: var(--text-primary);
}

.static-page p {
    margin-bottom: 1rem;
    color: var(--text-secondary);
}

.static-page a {
    color: var(--accent);
    text-decoration: none;
}

.back-link {
    display: inline-block;
    margin-bottom: 2rem;
    font-weight: 500;
}

.static-page ul {
    margin: 1rem 0 1.5rem 1.25rem;
    color: var(--text-secondary);
}

/* Rate Limit Stats */
.rate-limit-stats {
    padding: 0.75rem;
}

.stat-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.75rem;
    margin-bottom: 0.5rem;
    background: var(--ai-msg-bg);
    border-radius: 8px;
    border: 1px solid var(--border-color);
}

.stat-label {
    font-size: 0.875rem;
    color: var(--text-secondary);
}

.stat-value {
    font-size: 1rem;
    font-weight: 600;
    color: var(--accent);
}

/* Responsive */
@media (max-width: 768px) {
    .sidebar {
        display: none;
    }

    .sidebar-right {
        display: none;
    }

    .mobile-header {
        display: flex;
        margin-top: 10px;
    }

    .chat-window {
        padding: 1rem;
    }

    .input-area {
        padding: 1rem;
    }

    .welcome-message h1 {
        font-size: 1.5rem;
    }

    .mobile-header .brand-logo {
        max-height: 40px;
        width: auto;
    }

    .mobile-header .brand-link {
        margin-bottom: 0;
    }

    .nav-toggle {
        display: flex;
    }

    .static-header {
        padding: 0.5rem 1rem;
    }

    .static-header .brand-link img {
        max-width: 160px;
    }

    .static-nav-links {
        display: none;
        position: absolute;
        top: calc(100% + 0.5rem);
        right: 1rem;
        left: 1rem;
        background: var(--sidebar-bg);
        border: 1px solid var(--border-color);
        border-radius: 12px;
        padding: 1rem;
        flex-direction: column;
        box-shadow: var(--shadow-md);
        opacity: 0;
        pointer-events: none;
        transform: translateY(-10px);
        transition: opacity 0.2s ease, transform 0.2s ease;
    }

    .static-nav-links.open {
        display: flex;
        opacity: 1;
        pointer-events: auto;
        transform: translateY(0);
    }

    .static-nav-links a {
        padding: 0.4rem 0;
    }
}

@media (min-width: 769px) {
    body.app-shell {
        height: 100vh;
        overflow: hidden;
    }

    .app-shell .app-container {
        height: 100%;
    }
}