/* CSS Reset & Variables */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

:root {
    --bg-primary: #212121;
    --bg-secondary: #171717;
    --bg-tertiary: #2f2f2f;
    --bg-hover: #3f3f3f;
    --text-primary: #ececec;
    --text-secondary: #b4b4b4;
    --text-muted: #8e8e8e;
    --accent: #6366f1;
    --accent-hover: #5558e3;
    --border: #3f3f3f;
    --error: #ef4444;
    --success: #22c55e;
    --user-bg: #2f2f2f;
    --assistant-bg: transparent;
    --sidebar-width: 260px;
    --input-max-width: 768px;
    --message-max-width: 768px;
}

html, body {
    height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', sans-serif;
    font-size: 16px;
    line-height: 1.5;
    background: var(--bg-primary);
    color: var(--text-primary);
}

#app {
    height: 100%;
}

.view {
    height: 100%;
}

.hidden {
    display: none !important;
}

/* Login View */
.login-container {
    max-width: 400px;
    margin: 0 auto;
    padding: 2rem;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    justify-content: center;
}

.logo {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 8px;
}

.logo h1 {
    font-size: 1.75rem;
    font-weight: 600;
    background: linear-gradient(135deg, #6366f1, #8b5cf6);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.tagline {
    color: var(--text-secondary);
    margin-bottom: 2rem;
}

.form-group {
    margin-bottom: 1rem;
}

.form-group label {
    display: block;
    margin-bottom: 0.5rem;
    font-size: 0.875rem;
    color: var(--text-secondary);
}

.form-group input {
    width: 100%;
    padding: 0.75rem 1rem;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--bg-secondary);
    color: var(--text-primary);
    font-size: 1rem;
    transition: border-color 0.2s, box-shadow 0.2s;
}

.form-group input:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.2);
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 0.75rem 1.5rem;
    border: none;
    border-radius: 8px;
    font-size: 1rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
}

.btn-primary {
    width: 100%;
    background: var(--accent);
    color: white;
    margin-top: 0.5rem;
}

.btn-primary:hover {
    background: var(--accent-hover);
}

.btn-primary:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.error {
    color: var(--error);
    font-size: 0.875rem;
    margin-top: 1rem;
    text-align: center;
}

.login-footer {
    margin-top: 2rem;
    text-align: center;
    color: var(--text-secondary);
    font-size: 0.875rem;
}

.login-footer a {
    color: var(--accent);
    text-decoration: none;
}

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

/* Chat View Layout */
#chat-view {
    display: flex;
    height: 100%;
}

/* Sidebar */
.sidebar {
    width: var(--sidebar-width);
    background: var(--bg-secondary);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
}

.sidebar-header {
    padding: 12px;
}

.btn-new-chat {
    width: 100%;
    padding: 12px 16px;
    background: transparent;
    border: 1px solid var(--border);
    border-radius: 8px;
    color: var(--text-primary);
    font-size: 0.875rem;
    cursor: pointer;
    transition: background 0.2s;
}

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

.btn-new-chat svg {
    width: 16px;
    height: 16px;
}

.conversation-list {
    flex: 1;
    overflow-y: auto;
    padding: 0 8px;
}

.conversation-item {
    display: flex;
    align-items: center;
    padding: 10px 12px;
    margin: 2px 0;
    border-radius: 8px;
    cursor: pointer;
    color: var(--text-secondary);
    font-size: 0.875rem;
    text-overflow: ellipsis;
    white-space: nowrap;
    overflow: hidden;
    transition: background 0.15s;
}

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

.conversation-item.active {
    background: var(--bg-tertiary);
    color: var(--text-primary);
}

.conversation-item .title {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
}

.conversation-item .delete-btn {
    opacity: 0;
    padding: 4px;
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    border-radius: 4px;
    transition: opacity 0.15s, color 0.15s;
}

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

.conversation-item .delete-btn:hover {
    color: var(--error);
}

.sidebar-footer {
    padding: 12px;
    border-top: 1px solid var(--border);
}

.user-info {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 12px;
    background: var(--bg-tertiary);
    border-radius: 8px;
}

#user-display-name {
    font-size: 0.875rem;
    color: var(--text-secondary);
}

.btn-icon {
    padding: 6px;
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    border-radius: 6px;
    transition: background 0.15s, color 0.15s;
}

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

/* Main Chat Area */
.chat-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* Empty State */
.empty-state {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 2rem;
}

.empty-icon {
    margin-bottom: 1.5rem;
}

.empty-state h2 {
    font-size: 1.5rem;
    font-weight: 600;
    margin-bottom: 0.5rem;
}

.empty-state p {
    color: var(--text-secondary);
}

/* Messages Container */
.messages-container {
    flex: 1;
    overflow-y: auto;
    padding: 1rem 0;
}

.messages {
    max-width: var(--message-max-width);
    margin: 0 auto;
    padding: 0 1rem;
}

.message {
    padding: 1.25rem 0;
    display: flex;
    gap: 1rem;
}

.message-avatar {
    width: 32px;
    height: 32px;
    border-radius: 6px;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.875rem;
    font-weight: 600;
}

.message.user .message-avatar {
    background: var(--accent);
    color: white;
}

.message.assistant .message-avatar {
    background: linear-gradient(135deg, #6366f1, #8b5cf6);
    color: white;
}

.message-content {
    flex: 1;
    min-width: 0;
}

.message-role {
    font-weight: 600;
    font-size: 0.875rem;
    margin-bottom: 0.25rem;
}

.message-text {
    color: var(--text-primary);
    line-height: 1.6;
    white-space: pre-wrap;
    word-break: break-word;
}

.message-text p {
    margin-bottom: 0.75rem;
}

.message-text p:last-child {
    margin-bottom: 0;
}

.message-text code {
    background: var(--bg-tertiary);
    padding: 2px 6px;
    border-radius: 4px;
    font-family: 'SF Mono', Monaco, 'Courier New', monospace;
    font-size: 0.875em;
}

.message-text pre {
    background: var(--bg-secondary);
    padding: 1rem;
    border-radius: 8px;
    overflow-x: auto;
    margin: 0.75rem 0;
}

.message-text pre code {
    background: none;
    padding: 0;
}

.message.streaming .message-text::after {
    content: '|';
    animation: blink 1s infinite;
}

@keyframes blink {
    0%, 50% { opacity: 1; }
    51%, 100% { opacity: 0; }
}

/* Input Area */
.input-area {
    padding: 1rem;
    background: var(--bg-primary);
}

.chat-form {
    max-width: var(--input-max-width);
    margin: 0 auto;
}

.input-wrapper {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 8px 12px;
    transition: border-color 0.2s, box-shadow 0.2s;
}

.input-wrapper:focus-within {
    border-color: var(--accent);
    box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.15);
}

#message-input {
    flex: 1;
    background: transparent;
    border: none;
    color: var(--text-primary);
    font-size: 1rem;
    line-height: 1.5;
    resize: none;
    max-height: 200px;
    padding: 4px 0;
}

#message-input:focus {
    outline: none;
}

#message-input::placeholder {
    color: var(--text-muted);
}

.btn-send {
    width: 32px;
    height: 32px;
    padding: 0;
    background: var(--accent);
    border: none;
    border-radius: 8px;
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s, opacity 0.2s;
    flex-shrink: 0;
}

.btn-send:hover:not(:disabled) {
    background: var(--accent-hover);
}

.btn-send:disabled {
    opacity: 0.3;
    cursor: not-allowed;
}

.input-hint {
    text-align: center;
    font-size: 0.75rem;
    color: var(--text-muted);
    margin-top: 8px;
}

/* Loading states */
.loading {
    display: flex;
    align-items: center;
    gap: 4px;
}

.loading-dot {
    width: 8px;
    height: 8px;
    background: var(--text-muted);
    border-radius: 50%;
    animation: bounce 1.4s infinite ease-in-out;
}

.loading-dot:nth-child(1) { animation-delay: -0.32s; }
.loading-dot:nth-child(2) { animation-delay: -0.16s; }

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

/* Scrollbar */
::-webkit-scrollbar {
    width: 8px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--text-muted);
}

/* Mobile responsiveness */
@media (max-width: 768px) {
    .sidebar {
        position: fixed;
        left: -100%;
        top: 0;
        bottom: 0;
        z-index: 100;
        transition: left 0.3s;
    }

    .sidebar.open {
        left: 0;
    }

    .messages {
        padding: 0 0.75rem;
    }

    .input-area {
        padding: 0.75rem;
    }
}
