/* ===== 全局重置 ===== */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

/* ===== CSS 变量 ===== */
:root {
    --header-h: 56px;
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --msg-pad-x: 24px;
    --msg-pad-y: 20px;
    --avatar-size: 36px;
    --input-pad: 16px;
    --bubble-max: 80%;
}

/* ===== 主体布局 ===== */
html {
    height: 100%;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
    background-color: #f0f5f0;
    height: 100dvh;
    display: flex;
    justify-content: center;
    align-items: stretch;
    overflow: hidden;
    -webkit-text-size-adjust: 100%;
}

.chat-container {
    width: 100%;
    max-width: 900px;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    background-color: #ffffff;
    box-shadow: 0 0 40px rgba(0, 0, 0, 0.08);
    position: relative;
}

/* ===== 顶部栏 ===== */
.chat-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px var(--msg-pad-x);
    padding-top: calc(10px + var(--safe-top));
    background: linear-gradient(135deg, #1a3c34 0%, #0d4f3c 100%);
    color: #ffffff;
    flex-shrink: 0;
    border-bottom: 2px solid #2ecc71;
    min-height: calc(var(--header-h) + var(--safe-top));
}

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

.logo {
    font-size: 28px;
    flex-shrink: 0;
}

.header-title {
    min-width: 0;
}

.header-title h1 {
    font-size: 17px;
    font-weight: 700;
    margin: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.subtitle {
    font-size: 12px;
    color: #a0c8b0;
    margin-top: 2px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.header-right {
    display: flex;
    gap: 6px;
    align-items: center;
    flex-shrink: 0;
}

.profile-btn, .clear-btn {
    padding: 6px 12px;
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 6px;
    background: transparent;
    color: #d0e8d0;
    font-size: 13px;
    cursor: pointer;
    transition: all 0.2s;
    white-space: nowrap;
}

.profile-btn:hover, .clear-btn:hover {
    background: rgba(255, 255, 255, 0.1);
    color: #ffffff;
}

/* ===== 用户画像面板 ===== */
.profile-panel {
    padding: 14px var(--msg-pad-x);
    background-color: #f5f9f5;
    border-bottom: 1px solid #d0e0d0;
    flex-shrink: 0;
    max-height: 50vh;
    overflow-y: auto;
}

.profile-panel-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
    gap: 8px;
}

.profile-panel-title {
    font-size: 14px;
    font-weight: 700;
    color: #1a3c34;
    flex-shrink: 0;
}

.profile-panel-hint {
    font-size: 12px;
    color: #999;
    text-align: right;
}

.profile-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 10px;
}

.profile-item {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.profile-item.full {
    grid-column: span 3;
}

.profile-item label {
    font-size: 12px;
    color: #666;
    font-weight: 600;
}

.profile-item input,
.profile-item select,
.profile-item textarea {
    padding: 8px 10px;
    border: 1px solid #c8d8c8;
    border-radius: 6px;
    font-size: 14px;
    outline: none;
    transition: border-color 0.2s;
    font-family: inherit;
    width: 100%;
}

.profile-item textarea {
    resize: vertical;
    min-height: 36px;
}

.profile-item input:focus,
.profile-item select:focus,
.profile-item textarea:focus {
    border-color: #2ecc71;
}

.profile-actions {
    display: flex;
    gap: 10px;
    margin-top: 12px;
}

.profile-save-btn {
    padding: 8px 20px;
    border: none;
    border-radius: 6px;
    background-color: #2ecc71;
    color: #ffffff;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
}

.profile-save-btn:hover {
    background-color: #27ae60;
}

.profile-reset-btn {
    padding: 8px 20px;
    border: 1px solid #bbb;
    border-radius: 6px;
    background-color: #ffffff;
    color: #666;
    font-size: 13px;
    cursor: pointer;
    transition: all 0.2s;
}

.profile-reset-btn:hover {
    border-color: #999;
    color: #333;
}

/* ===== 头部档案徽章 ===== */
.profile-badge {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 4px 10px;
    border: 1px solid rgba(46, 204, 113, 0.4);
    border-radius: 16px;
    background: rgba(46, 204, 113, 0.1);
    color: #2ecc71;
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    white-space: nowrap;
}

.profile-badge:hover {
    background: rgba(46, 204, 113, 0.2);
    border-color: rgba(46, 204, 113, 0.6);
}

.badge-sep {
    color: rgba(46, 204, 113, 0.4);
    margin: 0 2px;
}

/* ===== Toast 通知 ===== */
.toast {
    position: fixed;
    top: calc(16px + var(--safe-top));
    left: 50%;
    transform: translateX(-50%) translateY(-100px);
    padding: 10px 24px;
    border-radius: 8px;
    background-color: #1a3c34;
    color: #ffffff;
    font-size: 14px;
    font-weight: 500;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
    z-index: 9999;
    opacity: 0;
    transition: all 0.3s ease;
    pointer-events: none;
    max-width: 90vw;
    text-align: center;
}

.toast.show {
    transform: translateX(-50%) translateY(0);
    opacity: 1;
}

/* ===== 消息区域 ===== */
.messages-area {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: var(--msg-pad-y) var(--msg-pad-x);
    display: flex;
    flex-direction: column;
    gap: 16px;
    scroll-behavior: smooth;
    overscroll-behavior: contain;
}

.messages-area::-webkit-scrollbar {
    width: 6px;
}

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

.messages-area::-webkit-scrollbar-thumb {
    background: #c0d0c0;
    border-radius: 3px;
}

/* ===== 消息气泡 ===== */
.message {
    display: flex;
    gap: 10px;
    max-width: var(--bubble-max);
    animation: fadeInUp 0.3s ease;
}

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

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

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

.message-avatar {
    width: var(--avatar-size);
    height: var(--avatar-size);
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    flex-shrink: 0;
    background-color: #f0f8f0;
}

.user-message .message-avatar {
    background-color: #2ecc71;
}

.ai-message .message-avatar {
    background-color: #0d4f3c;
}

.message-content {
    display: flex;
    flex-direction: column;
    gap: 4px;
    min-width: 0;
}

.message-text {
    padding: 12px 16px;
    border-radius: 12px;
    font-size: 14px;
    line-height: 1.7;
    word-break: break-word;
    overflow-wrap: break-word;
}

.user-message .message-text {
    background-color: #2ecc71;
    color: #ffffff;
    border-bottom-right-radius: 4px;
}

.ai-message .message-text {
    background-color: #f5f8f5;
    color: #1a3c34;
    border-bottom-left-radius: 4px;
}

/* AI消息中的Markdown样式 */
.ai-message .message-text h1,
.ai-message .message-text h2,
.ai-message .message-text h3 {
    margin-top: 16px;
    margin-bottom: 8px;
    color: #0d4f3c;
}

.ai-message .message-text h1 { font-size: 18px; }
.ai-message .message-text h2 { font-size: 16px; }
.ai-message .message-text h3 { font-size: 15px; }

.ai-message .message-text ul,
.ai-message .message-text ol {
    margin-left: 20px;
    margin-bottom: 8px;
}

.ai-message .message-text li {
    margin-bottom: 4px;
}

.ai-message .message-text strong {
    color: #27ae60;
}

.ai-message .message-text code {
    background-color: #e0eee0;
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 13px;
    font-family: "Consolas", "Monaco", monospace;
}

.ai-message .message-text pre {
    background-color: #1a3c34;
    color: #d0e8d0;
    padding: 12px 16px;
    border-radius: 8px;
    overflow-x: auto;
    margin: 8px 0;
    -webkit-overflow-scrolling: touch;
}

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

.ai-message .message-text table {
    border-collapse: collapse;
    width: 100%;
    margin: 8px 0;
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.ai-message .message-text th,
.ai-message .message-text td {
    border: 1px solid #c8d8c8;
    padding: 6px 10px;
    text-align: left;
    font-size: 13px;
}

.ai-message .message-text th {
    background-color: #e8f0e8;
    font-weight: 600;
}

/* ===== 快捷问题 ===== */
.quick-questions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    padding: 8px 0;
}

.quick-btn {
    padding: 8px 16px;
    border: 1px solid #2ecc71;
    border-radius: 20px;
    background: transparent;
    color: #27ae60;
    font-size: 13px;
    cursor: pointer;
    transition: all 0.2s;
}

.quick-btn:hover {
    background-color: #2ecc71;
    color: #ffffff;
}

/* ===== Agent状态指示器 ===== */
.agent-status {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px var(--msg-pad-x);
    background-color: #f5f9f5;
    border-top: 1px solid #d0e0d0;
    font-size: 13px;
    color: #666;
    flex-shrink: 0;
}

.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background-color: #2ecc71;
    animation: pulse 1.5s infinite;
    flex-shrink: 0;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.3; }
}

/* ===== 输入区域 ===== */
.input-area {
    display: flex;
    gap: 10px;
    padding: var(--input-pad) var(--msg-pad-x);
    padding-bottom: calc(var(--input-pad) + var(--safe-bottom));
    background-color: #ffffff;
    border-top: 1px solid #e0e0e0;
    flex-shrink: 0;
    align-items: flex-end;
}

#messageInput {
    flex: 1;
    padding: 12px 16px;
    border: 1px solid #c8d8c8;
    border-radius: 12px;
    font-size: 16px;
    font-family: inherit;
    resize: none;
    outline: none;
    transition: border-color 0.2s;
    max-height: 120px;
    line-height: 1.5;
}

#messageInput:focus {
    border-color: #2ecc71;
}

.send-btn {
    width: 44px;
    height: 44px;
    border: none;
    border-radius: 12px;
    background-color: #2ecc71;
    color: #ffffff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    flex-shrink: 0;
}

.send-btn:hover {
    background-color: #27ae60;
    transform: scale(1.05);
}

.send-btn:disabled {
    background-color: #c0c0c0;
    cursor: not-allowed;
    transform: none;
}

/* ===== 打字动画 ===== */
.typing-indicator {
    display: flex;
    gap: 4px;
    padding: 4px 0;
}

.typing-indicator span {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background-color: #999;
    animation: typingBounce 1.4s infinite;
}

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

@keyframes typingBounce {
    0%, 60%, 100% { transform: translateY(0); }
    30% { transform: translateY(-8px); }
}


/* ===========================================
   响应式适配
   =========================================== */

/* --- 平板 (481px ~ 768px) --- */
@media (max-width: 768px) {
    :root {
        --msg-pad-x: 16px;
        --msg-pad-y: 16px;
        --bubble-max: 88%;
    }

    .logo {
        font-size: 24px;
    }

    .header-title h1 {
        font-size: 15px;
    }

    .subtitle {
        display: none;
    }

    .profile-grid {
        grid-template-columns: repeat(2, 1fr);
    }

    .profile-item.full {
        grid-column: span 2;
    }

    .profile-badge {
        display: none;
    }

    .profile-btn, .clear-btn {
        padding: 6px 10px;
        font-size: 12px;
    }

    .quick-questions {
        gap: 6px;
    }

    .quick-btn {
        font-size: 12px;
        padding: 7px 14px;
    }
}

/* --- 手机 (<= 480px) --- */
@media (max-width: 480px) {
    :root {
        --msg-pad-x: 12px;
        --msg-pad-y: 12px;
        --avatar-size: 30px;
        --input-pad: 10px;
        --bubble-max: 95%;
    }

    .chat-header {
        padding: 8px 12px;
        padding-top: calc(8px + var(--safe-top));
        min-height: calc(48px + var(--safe-top));
    }

    .logo {
        font-size: 22px;
    }

    .header-title h1 {
        font-size: 14px;
    }

    .profile-btn, .clear-btn {
        padding: 6px 8px;
        font-size: 16px;
        line-height: 1;
    }

    .profile-btn .btn-label,
    .clear-btn .btn-label {
        display: none;
    }

    .profile-panel {
        padding: 12px;
    }

    .profile-panel-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 4px;
    }

    .profile-panel-hint {
        font-size: 11px;
        text-align: left;
    }

    .profile-grid {
        grid-template-columns: 1fr;
        gap: 8px;
    }

    .profile-item.full {
        grid-column: span 1;
    }

    .profile-actions {
        flex-direction: column;
        gap: 8px;
    }

    .profile-save-btn,
    .profile-reset-btn {
        width: 100%;
        text-align: center;
        padding: 10px;
        font-size: 14px;
    }

    .messages-area {
        padding: 12px;
        gap: 12px;
    }

    .message {
        gap: 8px;
    }

    .message-avatar {
        font-size: 15px;
        border-radius: 6px;
    }

    .message-text {
        padding: 10px 14px;
        font-size: 14px;
        line-height: 1.65;
    }

    .ai-message .message-text h1 { font-size: 16px; }
    .ai-message .message-text h2 { font-size: 15px; }
    .ai-message .message-text h3 { font-size: 14px; }

    .ai-message .message-text ul,
    .ai-message .message-text ol {
        margin-left: 16px;
    }

    .quick-questions {
        flex-direction: column;
        gap: 6px;
    }

    .quick-btn {
        width: 100%;
        text-align: center;
        font-size: 13px;
        padding: 10px 14px;
    }

    .input-area {
        gap: 8px;
        padding: 10px 12px;
        padding-bottom: calc(10px + var(--safe-bottom));
    }

    #messageInput {
        padding: 10px 14px;
        font-size: 16px;
        border-radius: 10px;
    }

    .send-btn {
        width: 40px;
        height: 40px;
        border-radius: 10px;
    }

    .agent-status {
        padding: 6px 12px;
        font-size: 12px;
    }

    .toast {
        font-size: 13px;
        padding: 8px 20px;
        max-width: 92vw;
    }
}

/* --- 横屏手机 (高度 <= 500px) --- */
@media (max-height: 500px) and (orientation: landscape) {
    :root {
        --msg-pad-y: 8px;
        --msg-pad-x: 12px;
        --avatar-size: 28px;
    }

    .chat-header {
        min-height: calc(40px + var(--safe-top));
        padding: 6px 12px;
        padding-top: calc(6px + var(--safe-top));
    }

    .logo {
        font-size: 20px;
    }

    .header-title h1 {
        font-size: 13px;
    }

    .subtitle {
        display: none;
    }

    .messages-area {
        gap: 8px;
    }

    .message-text {
        padding: 8px 12px;
        font-size: 13px;
        line-height: 1.5;
    }

    .input-area {
        padding: 6px 12px;
        padding-bottom: calc(6px + var(--safe-bottom));
    }

    #messageInput {
        max-height: 60px;
        padding: 8px 12px;
    }

    .send-btn {
        width: 36px;
        height: 36px;
    }
}
