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

:root {
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --bg: #f8fafc;
    --surface: #ffffff;
    --sidebar-bg: #1e293b;
    --sidebar-text: #e2e8f0;
    --sidebar-hover: #334155;
    --text: #1e293b;
    --text-secondary: #64748b;
    --border: #e2e8f0;
    --user-msg: #2563eb;
    --user-text: #ffffff;
    --assistant-msg: #f1f5f9;
    --shadow: 0 1px 3px rgba(0,0,0,0.1);
    --radius: 8px;
}

html, body {
    height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Noto Sans SC", sans-serif;
    font-size: 15px;
    color: var(--text);
    background: var(--bg);
}

/* ── Screen management ── */
.screen { display: flex; height: 100vh; }
.screen.hidden { display: none; }

/* ── Login Screen ── */
.login-container {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    background: linear-gradient(135deg, #1e293b 0%, #334155 100%);
}

.login-card {
    background: var(--surface);
    padding: 48px 40px;
    border-radius: 16px;
    box-shadow: 0 20px 60px rgba(0,0,0,0.3);
    text-align: center;
    max-width: 420px;
    width: 90%;
}

.login-title {
    font-size: 28px;
    font-weight: 700;
    margin-bottom: 8px;
    color: var(--text);
}

.login-subtitle {
    font-size: 14px;
    color: var(--text-secondary);
    margin-bottom: 32px;
}

.login-form {
    display: flex;
    gap: 8px;
    margin-bottom: 16px;
}

.login-form .input {
    flex: 1;
    padding: 12px 16px;
    border: 2px solid var(--border);
    border-radius: var(--radius);
    font-size: 16px;
    outline: none;
    transition: border-color 0.2s;
}

.login-form .input:focus {
    border-color: var(--primary);
}

.login-hint {
    font-size: 12px;
    color: var(--text-secondary);
}

/* ── Buttons ── */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: none;
    cursor: pointer;
    font-size: 14px;
    border-radius: var(--radius);
    transition: background 0.2s, opacity 0.2s;
    white-space: nowrap;
}

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

.btn-primary {
    background: var(--primary);
    color: white;
    padding: 12px 24px;
    font-weight: 600;
}

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

.btn-small {
    padding: 6px 12px;
    font-size: 13px;
}

.btn-ghost {
    background: transparent;
    color: var(--sidebar-text);
}

.btn-ghost:hover {
    background: var(--sidebar-hover);
}

.send-btn {
    padding: 10px 20px;
    align-self: flex-end;
}

/* ── Layout ── */
.sidebar {
    width: 260px;
    min-width: 260px;
    background: var(--sidebar-bg);
    color: var(--sidebar-text);
    display: flex;
    flex-direction: column;
    height: 100vh;
}

.sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px;
    border-bottom: 1px solid rgba(255,255,255,0.1);
}

.sidebar-title {
    font-size: 16px;
    font-weight: 600;
}

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

.conversation-item {
    padding: 10px 12px;
    border-radius: 6px;
    cursor: pointer;
    margin-bottom: 2px;
    font-size: 14px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    transition: background 0.15s;
}

.conversation-item:hover {
    background: var(--sidebar-hover);
}

.conversation-item.active {
    background: var(--primary);
    color: white;
}

.sidebar-footer {
    padding: 12px 16px;
    border-top: 1px solid rgba(255,255,255,0.1);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.user-name {
    font-size: 13px;
    opacity: 0.8;
}

/* ── Chat Main ── */
.chat-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    height: 100vh;
    min-width: 0;
}

.top-bar {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 20px;
    border-bottom: 1px solid var(--border);
    background: var(--surface);
}

.menu-btn {
    display: none;
    font-size: 20px;
}

.model-selector {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 13px;
    color: var(--text-secondary);
}

.select {
    padding: 6px 10px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 13px;
    background: var(--surface);
    color: var(--text);
    outline: none;
}

.select:focus {
    border-color: var(--primary);
}

/* ── Messages ── */
.message-list {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.empty-state {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: var(--text-secondary);
    font-size: 16px;
}

.message {
    display: flex;
    flex-direction: column;
    max-width: 80%;
}

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

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

.message-content {
    padding: 12px 16px;
    border-radius: 12px;
    line-height: 1.6;
    word-wrap: break-word;
}

.message.user .message-content {
    white-space: pre-wrap;
}

.message.user .message-content {
    background: var(--user-msg);
    color: var(--user-text);
    border-bottom-right-radius: 4px;
}

.message.assistant .message-content {
    background: var(--assistant-msg);
    color: var(--text);
    border-bottom-left-radius: 4px;
}

/* ── Markdown 内容样式 ── */
.message-content code {
    background: #e2e8f0;
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 0.9em;
    font-family: 'Consolas', 'Monaco', monospace;
}

.message-content pre {
    background: #1e293b;
    color: #e2e8f0;
    padding: 12px 16px;
    border-radius: 8px;
    overflow-x: auto;
    margin: 8px 0;
    font-size: 13px;
    line-height: 1.5;
}

.message-content pre code {
    background: transparent;
    padding: 0;
    color: inherit;
}

.message-content ul, .message-content ol {
    padding-left: 20px;
    margin: 6px 0;
}

.message-content li {
    margin: 3px 0;
}

.message-content blockquote {
    border-left: 3px solid var(--primary);
    padding-left: 12px;
    color: var(--text-secondary);
    margin: 8px 0;
}

.message-content h1, .message-content h2, .message-content h3,
.message-content h4, .message-content h5, .message-content h6 {
    margin: 12px 0 6px 0;
    line-height: 1.3;
}

.message-content h1 { font-size: 1.3em; }
.message-content h2 { font-size: 1.2em; }
.message-content h3 { font-size: 1.1em; }

.message-content p {
    margin: 4px 0;
}

.message-content strong {
    font-weight: 700;
}

.message-content table {
    border-collapse: collapse;
    margin: 8px 0;
    font-size: 0.9em;
    width: 100%;
}

.message-content th, .message-content td {
    border: 1px solid var(--border);
    padding: 6px 10px;
    text-align: left;
}

.message-content th {
    background: #e2e8f0;
    font-weight: 600;
}

.message-content a {
    color: var(--primary);
    text-decoration: underline;
}

.message-time {
    font-size: 11px;
    color: var(--text-secondary);
    margin-top: 4px;
    padding: 0 4px;
}

.message.user .message-time {
    text-align: right;
}

/* ── Input Area ── */
.input-area {
    display: flex;
    gap: 8px;
    padding: 16px 20px;
    border-top: 1px solid var(--border);
    background: var(--surface);
}

.textarea {
    flex: 1;
    padding: 10px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 14px;
    font-family: inherit;
    resize: none;
    outline: none;
    line-height: 1.5;
}

.textarea:focus {
    border-color: var(--primary);
}

/* ── Loading indicator ── */
.loading-dots {
    display: flex;
    gap: 4px;
    padding: 4px 0;
}

.loading-dots span {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--text-secondary);
    animation: bounce 1.4s infinite;
}

.loading-dots span:nth-child(2) { animation-delay: 0.2s; }
.loading-dots span:nth-child(3) { animation-delay: 0.4s; }

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

/* ── Error message ── */
.message.error .message-content {
    background: #fef2f2;
    color: #dc2626;
    border: 1px solid #fecaca;
}

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

    .sidebar.open {
        left: 0;
    }

    .menu-btn {
        display: block;
    }

    .message {
        max-width: 90%;
    }

    .login-card {
        padding: 32px 24px;
    }
}
