/* CSS Variables */
:root {
    --bg: #f5f5f5;
    --surface: #ffffff;
    --surface-secondary: #f0f0f0;
    --text: #1a1a1a;
    --text-secondary: #666666;
    --border: #e0e0e0;
    --primary: #4f46e5;
    --primary-hover: #4338ca;
    --user-bubble: #4f46e5;
    --user-text: #ffffff;
    --assistant-bubble: #ffffff;
    --assistant-text: #1a1a1a;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    --shadow-lg: 0 4px 12px rgba(0, 0, 0, 0.1);
    --radius: 12px;
    --radius-sm: 8px;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #1a1a2e;
        --surface: #16213e;
        --surface-secondary: #0f3460;
        --text: #e4e4e4;
        --text-secondary: #a0a0a0;
        --border: #2a2a4a;
        --assistant-bubble: #16213e;
        --assistant-text: #e4e4e4;
        --shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
        --shadow-lg: 0 4px 12px rgba(0, 0, 0, 0.4);
    }
}

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

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

/* Layout */
#app {
    display: flex;
    flex-direction: column;
    height: 100vh;
    max-width: 800px;
    margin: 0 auto;
}

/* Header */
#header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

#header h1 {
    font-size: 18px;
    font-weight: 600;
}

#new-chat-btn {
    padding: 6px 16px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--surface);
    color: var(--text);
    cursor: pointer;
    font-size: 13px;
    transition: background 0.15s, border-color 0.15s;
}

#new-chat-btn:hover {
    background: var(--surface-secondary);
    border-color: var(--primary);
}

/* Chat Container */
#chat-container {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    scroll-behavior: smooth;
}

/* Messages */
#messages {
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.message {
    display: flex;
    flex-direction: column;
    max-width: 85%;
    padding: 12px 16px;
    border-radius: var(--radius);
    animation: fadeIn 0.2s ease-out;
    word-break: break-word;
}

.message.user {
    align-self: flex-end;
    background: var(--user-bubble);
    color: var(--user-text);
    border-bottom-right-radius: 4px;
}

.message.assistant {
    align-self: flex-start;
    background: var(--assistant-bubble);
    color: var(--assistant-text);
    border: 1px solid var(--border);
    border-bottom-left-radius: 4px;
}

.message.error {
    align-self: center;
    background: #fef2f2;
    color: #dc2626;
    border: 1px solid #fecaca;
    font-size: 13px;
}

@media (prefers-color-scheme: dark) {
    .message.error {
        background: #3b1111;
        color: #fca5a5;
        border-color: #7f1d1d;
    }
}

.message .label {
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 4px;
    opacity: 0.7;
}

.message .content {
    white-space: pre-wrap;
    line-height: 1.7;
}

/* Markdown rendered content */
.message.assistant .content pre {
    margin: 8px 0;
    padding: 12px;
    background: var(--surface-secondary);
    border-radius: var(--radius-sm);
    overflow-x: auto;
}
.message.assistant .content pre code {
    padding: 0;
    background: none;
}
.message.assistant .content h2 {
    font-size: 16px;
    font-weight: 600;
    margin: 12px 0 6px;
    padding-bottom: 4px;
    border-bottom: 1px solid var(--border);
}
.message.assistant .content h3 {
    font-size: 14px;
    font-weight: 600;
    margin: 10px 0 4px;
}
.message.assistant .content ul,
.message.assistant .content ol {
    margin: 6px 0;
    padding-left: 20px;
}
.message.assistant .content li {
    margin-bottom: 2px;
}
.message.assistant .content hr {
    border: none;
    border-top: 1px solid var(--border);
    margin: 12px 0;
}
.message.assistant .content strong {
    font-weight: 600;
}
.message.assistant .content p {
    margin-bottom: 8px;
}
.message.assistant .content p:last-child {
    margin-bottom: 0;
}
.message.assistant .content code {
    font-family: "SF Mono", "Fira Code", "Consolas", monospace;
    font-size: 13px;
    background: var(--surface-secondary);
    padding: 2px 6px;
    border-radius: 4px;
}

/* Streaming cursor */
.message.assistant.streaming .content::after {
    content: "▊";
    animation: blink 1s step-end infinite;
    margin-left: 1px;
    opacity: 0.6;
}

/* Welcome */
#welcome {
    text-align: center;
    padding: 60px 20px;
    color: var(--text-secondary);
}

#welcome h2 {
    font-size: 24px;
    margin-bottom: 8px;
    color: var(--text);
}

#welcome.hidden {
    display: none;
}

/* Loading */
#loading {
    display: flex;
    justify-content: center;
    padding: 12px;
}

#loading.hidden {
    display: none;
}

.dot-pulse {
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--primary);
    animation: dotPulse 1.4s infinite ease-in-out both;
}

.dot-pulse::before,
.dot-pulse::after {
    content: "";
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--primary);
    position: absolute;
    animation: dotPulse 1.4s infinite ease-in-out both;
}

.dot-pulse::before {
    left: -20px;
    animation-delay: -0.32s;
}

.dot-pulse::after {
    left: 20px;
    animation-delay: 0.32s;
}

/* Input Area */
#input-area {
    display: flex;
    align-items: flex-end;
    gap: 10px;
    padding: 16px 20px;
    background: var(--surface);
    border-top: 1px solid var(--border);
    flex-shrink: 0;
}

#message-input {
    flex: 1;
    resize: none;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 10px 14px;
    font-family: inherit;
    font-size: 14px;
    line-height: 1.5;
    background: var(--bg);
    color: var(--text);
    outline: none;
    max-height: 150px;
    transition: border-color 0.15s;
}

#message-input:focus {
    border-color: var(--primary);
}

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

#send-btn {
    padding: 10px 20px;
    border: none;
    border-radius: var(--radius-sm);
    background: var(--primary);
    color: #ffffff;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.15s, opacity 0.15s;
    flex-shrink: 0;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

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

#send-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

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

@keyframes blink {
    50% { opacity: 0; }
}

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

/* ---- 手机适配 ---- */
@media (max-width: 768px) {
    #app {
        max-width: 100%;
    }

    #header {
        padding: 12px 14px;
    }
    #header h1 {
        font-size: 16px;
    }

    #new-chat-btn {
        padding: 5px 12px;
        font-size: 12px;
    }

    #chat-container {
        padding: 12px;
    }

    .message {
        max-width: 92%;
        padding: 10px 14px;
        font-size: 14px;
    }

    .message .label {
        font-size: 10px;
    }

    #welcome h2 {
        font-size: 20px;
    }

    #input-area {
        padding: 10px 12px;
        gap: 8px;
    }

    #message-input {
        padding: 8px 10px;
        font-size: 15px; /* 防止 iOS 自动缩放 */
    }

    #send-btn {
        padding: 8px 16px;
        font-size: 14px;
    }

    .message.assistant .content pre {
        padding: 8px;
        font-size: 12px;
    }
    .message.assistant .content h2 {
        font-size: 15px;
    }
    .message.assistant .content h3 {
        font-size: 13px;
    }
}

/* Scrollbar */
#chat-container::-webkit-scrollbar {
    width: 6px;
}

#chat-container::-webkit-scrollbar-track {
    background: transparent;
}

#chat-container::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 3px;
}

#chat-container::-webkit-scrollbar-thumb:hover {
    background: var(--text-secondary);
}
