:root {
    --bg-primary: #FFFFFF;
    --bg-secondary: #F5F5F7;
    --bg-tertiary: #E8E8ED;
    --accent: #007AFF;
    --accent-hover: #0056CC;
    --text-primary: #1D1D1F;
    --text-secondary: #6E6E73;
    --text-tertiary: #AEAEB2;
    --border: #E5E5EA;
    --shadow-sm: 0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.04);
    --shadow-md: 0 4px 12px rgba(0,0,0,0.1), 0 2px 4px rgba(0,0,0,0.06);
    --shadow-lg: 0 8px 24px rgba(0,0,0,0.12), 0 4px 8px rgba(0,0,0,0.06);
    --radius: 12px;
    --radius-sm: 8px;
    --radius-lg: 20px;
}

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

html {
    font-size: 16px;
    -webkit-text-size-adjust: 100%;
}

body {
    font-family: 'Inter', 'Noto Sans TC', -apple-system, BlinkMacSystemFont, 'SF Pro Display', 'Segoe UI', sans-serif;
    background: var(--bg-secondary);
    color: var(--text-primary);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    line-height: 1.5;
}

/* Chat layout */
.chat-app {
    display: flex;
    flex-direction: column;
    height: 100dvh;
    max-width: 640px;
    margin: 0 auto;
    background: var(--bg-primary);
    position: relative;
}

@media (min-width: 641px) {
    .chat-app {
        border-left: 1px solid var(--border);
        border-right: 1px solid var(--border);
    }
}

/* Header */
.chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    background: rgba(255,255,255,0.85);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border-bottom: 0.5px solid var(--border);
    flex-shrink: 0;
    z-index: 10;
}

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

.header-logo svg {
    width: 36px;
    height: 36px;
    display: block;
}

.header-info {
    display: flex;
    flex-direction: column;
}

.header-title {
    font-size: 17px;
    font-weight: 600;
    color: var(--text-primary);
    line-height: 1.2;
}

.header-status {
    font-size: 12px;
    color: var(--text-secondary);
    line-height: 1.3;
}

.header-lang {
    font-size: 13px;
    color: var(--text-secondary);
    padding: 4px 10px;
    background: var(--bg-secondary);
    border-radius: 12px;
}

/* Messages */
.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    -webkit-overflow-scrolling: touch;
    scroll-behavior: smooth;
}

.message {
    display: flex;
    gap: 8px;
    max-width: 85%;
    animation: messageIn 0.25s ease;
}

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

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

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

.message-avatar {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-top: 2px;
}

.message.assistant .message-avatar {
    background: var(--accent);
}

.message.assistant .message-avatar svg {
    width: 16px;
    height: 16px;
}

.message-bubble {
    padding: 10px 14px;
    border-radius: var(--radius-lg);
    font-size: 15px;
    line-height: 1.5;
    word-break: break-word;
}

.message.user .message-bubble {
    background: var(--accent);
    color: #FFFFFF;
    border-bottom-right-radius: 4px;
}

.message.assistant .message-bubble {
    background: var(--bg-secondary);
    color: var(--text-primary);
    border-bottom-left-radius: 4px;
}

.message-actions {
    display: flex;
    gap: 4px;
    margin-top: 4px;
    padding-left: 36px;
}

.message.user .message-actions {
    justify-content: flex-end;
    padding-left: 0;
    padding-right: 36px;
}

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

.msg-action-btn:hover {
    color: var(--accent);
    background: var(--bg-secondary);
}

/* Typing indicator */
.typing-indicator {
    display: none;
    align-self: flex-start;
    gap: 8px;
    max-width: 85%;
}

.typing-indicator.visible {
    display: flex;
}

.typing-dots {
    padding: 12px 16px;
    background: var(--bg-secondary);
    border-radius: var(--radius-lg);
    border-bottom-left-radius: 4px;
    display: flex;
    gap: 4px;
    align-items: center;
}

.typing-dots span {
    width: 7px;
    height: 7px;
    background: var(--text-tertiary);
    border-radius: 50%;
    animation: typingBounce 1.4s infinite;
}

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

@keyframes typingBounce {
    0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
    30% { transform: translateY(-4px); opacity: 1; }
}

/* Input area */
.chat-input-area {
    padding: 8px 12px;
    padding-bottom: max(8px, env(safe-area-inset-bottom));
    background: rgba(255,255,255,0.85);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border-top: 0.5px solid var(--border);
    flex-shrink: 0;
}

.input-row {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    max-width: 100%;
}

.mic-btn, .send-btn {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: background 0.15s, transform 0.1s;
}

.mic-btn {
    background: var(--bg-secondary);
    color: var(--text-secondary);
}

.mic-btn:hover {
    background: var(--bg-tertiary);
}

.mic-btn.recording {
    background: #FF3B30;
    color: #FFFFFF;
    animation: micPulse 1.5s ease infinite;
}

@keyframes micPulse {
    0%, 100% { box-shadow: 0 0 0 0 rgba(255,59,48,0.4); }
    50% { box-shadow: 0 0 0 8px rgba(255,59,48,0); }
}

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

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

.send-btn:active {
    transform: scale(0.95);
}

.send-btn:disabled {
    background: var(--bg-tertiary);
    color: var(--text-tertiary);
    cursor: not-allowed;
}

.input-wrapper {
    flex: 1;
    position: relative;
}

.chat-input {
    width: 100%;
    padding: 8px 14px;
    border: 1px solid var(--border);
    border-radius: 20px;
    font-size: 15px;
    font-family: inherit;
    line-height: 1.4;
    resize: none;
    outline: none;
    transition: border-color 0.15s;
    max-height: 120px;
    background: var(--bg-primary);
    color: var(--text-primary);
}

.chat-input:focus {
    border-color: var(--accent);
}

.chat-input::placeholder {
    color: var(--text-tertiary);
}

/* Footer */
.chat-footer {
    padding: 8px 16px;
    padding-bottom: max(8px, env(safe-area-inset-bottom));
    text-align: center;
    flex-shrink: 0;
}

.end-chat-btn {
    font-size: 14px;
    color: #FF3B30;
    background: none;
    border: none;
    cursor: pointer;
    padding: 8px 20px;
    border-radius: var(--radius-sm);
    transition: background 0.15s;
    font-family: inherit;
    font-weight: 500;
}

.end-chat-btn:hover {
    background: rgba(255,59,48,0.08);
}

/* Welcome message */
.welcome-message {
    text-align: center;
    padding: 32px 20px;
}

.welcome-icon {
    margin-bottom: 12px;
}

.welcome-icon svg {
    width: 48px;
    height: 48px;
}

.welcome-title {
    font-size: 20px;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 6px;
}

.welcome-desc {
    font-size: 14px;
    color: var(--text-secondary);
    max-width: 280px;
    margin: 0 auto;
}

/* Scrollbar */
.chat-messages::-webkit-scrollbar {
    width: 4px;
}

.chat-messages::-webkit-scrollbar-track {
    background: transparent;
}

.chat-messages::-webkit-scrollbar-thumb {
    background: var(--text-tertiary);
    border-radius: 2px;
}

/* End dialog */
.dialog-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.3);
    backdrop-filter: blur(4px);
    z-index: 100;
    align-items: center;
    justify-content: center;
}

.dialog-overlay.visible {
    display: flex;
}

.dialog-box {
    background: var(--bg-primary);
    border-radius: 14px;
    padding: 24px;
    width: min(320px, calc(100vw - 48px));
    text-align: center;
    box-shadow: var(--shadow-lg);
}

.dialog-title {
    font-size: 17px;
    font-weight: 600;
    margin-bottom: 8px;
}

.dialog-desc {
    font-size: 14px;
    color: var(--text-secondary);
    margin-bottom: 20px;
}

.dialog-actions {
    display: flex;
    gap: 8px;
}

.dialog-btn {
    flex: 1;
    padding: 10px 16px;
    border-radius: var(--radius-sm);
    border: none;
    font-size: 15px;
    font-weight: 500;
    cursor: pointer;
    font-family: inherit;
    transition: background 0.15s;
}

.dialog-btn.cancel {
    background: var(--bg-secondary);
    color: var(--text-primary);
}

.dialog-btn.cancel:hover {
    background: var(--bg-tertiary);
}

.dialog-btn.confirm {
    background: #FF3B30;
    color: #FFFFFF;
}

.dialog-btn.confirm:hover {
    background: #D63029;
}