/* ==================== 聊天界面移动端优化 ==================== */

/* ===== 聊天容器基础样式 ===== */
.ai-chat-area {
    display: flex;
    flex-direction: column;
    height: 100%;
}

/* ===== 聊天消息容器 ===== */
#chat-messages {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    display: flex;
    flex-direction: column;
    padding: 1rem;
    gap: 0.75rem;
    background: #f8f9fa;
    border-radius: 0.375rem;
    -webkit-overflow-scrolling: touch; /* 平滑滚动 */
}

/* ===== 单条消息样式 ===== */
.message {
    display: flex;
    align-items: flex-end;
    gap: 0.5rem;
    animation: slideIn 0.3s ease-out;
    max-width: 100%;
}

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

.message.user {
    flex-direction: row-reverse;
    justify-content: flex-start;
}

.message.assistant {
    justify-content: flex-start;
}

/* ===== 消息头像 ===== */
.message-avatar {
    flex-shrink: 0;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    color: white;
}

.message.user .message-avatar {
    background: linear-gradient(135deg, #0d6efd 0%, #0a58ca 100%);
}

.message.assistant .message-avatar {
    background: linear-gradient(135deg, #28a745 0%, #1e7e34 100%);
}

/* ===== 消息内容 ===== */
.message-content {
    max-width: 85%;
    padding: 0.75rem 1rem;
    border-radius: 1rem;
    line-height: 1.4;
    word-wrap: break-word;
    word-break: break-word;
    white-space: pre-wrap;
    overflow-wrap: break-word;
}

.message.user .message-content {
    background: #0d6efd;
    color: white;
    border-bottom-right-radius: 0.25rem;
    box-shadow: 0 1px 3px rgba(13, 110, 253, 0.3);
}

.message.assistant .message-content {
    background: white;
    color: #333;
    border: 1px solid #dee2e6;
    border-bottom-left-radius: 0.25rem;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.message-content a {
    color: inherit;
    text-decoration: underline;
    word-break: break-all;
}

/* ===== 消息时间 ===== */
.message-time {
    font-size: 0.75rem;
    color: #6c757d;
    margin-top: 0.25rem;
    text-align: center;
    width: 100%;
    opacity: 0;
    transition: opacity 0.2s ease;
}

.message:hover .message-time {
    opacity: 1;
}

/* ===== 打字指示器 ===== */
.typing-indicator {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.75rem 1rem;
    background: white;
    border: 1px solid #dee2e6;
    border-radius: 1rem;
    border-bottom-left-radius: 0.25rem;
    color: #6c757d;
    font-style: italic;
    animation: slideIn 0.3s ease-out;
}

.typing-dots {
    display: flex;
    gap: 0.25rem;
    margin-left: 0.5rem;
}

.typing-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #6c757d;
    animation: typingBounce 1.4s infinite;
}

.typing-dot:nth-child(2) {
    animation-delay: 0.2s;
}

.typing-dot:nth-child(3) {
    animation-delay: 0.4s;
}

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

/* ===== 快捷问题按钮 ===== */
#quick-questions {
    margin-top: 1rem;
    padding-top: 1rem;
    border-top: 1px solid #dee2e6;
}

#quick-questions .d-flex {
    flex-wrap: wrap;
    gap: 0.5rem;
}

.quick-question-btn {
    flex: 0 1 calc(50% - 0.25rem);
    font-size: 0.85rem;
    padding: 0.5rem 0.75rem;
    height: auto;
    min-height: 40px;
    white-space: normal;
    line-height: 1.3;
    border-radius: 0.375rem;
    transition: all 0.2s ease;
}

.quick-question-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.quick-question-btn:active {
    transform: translateY(0);
}

@media (max-width: 575px) {
    .quick-question-btn {
        flex: 0 1 calc(50% - 0.25rem);
        font-size: 0.75rem;
        padding: 0.4rem 0.6rem;
        min-height: 36px;
    }
}

/* ===== 聊天输入区域 ===== */
.chat-input-container {
    display: flex;
    gap: 0.5rem;
    margin-top: 1rem;
    flex-shrink: 0;
}

.chat-input-container .form-control {
    flex: 1;
    border-radius: 20px;
    padding: 0.75rem 1rem;
    font-size: 16px !important;
    resize: none;
    max-height: 120px;
    min-height: 44px;
}

.chat-input-container .btn {
    flex-shrink: 0;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    min-width: 44px;
}

/* ===== 聊天头部 ===== */
.chat-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.75rem 0;
    margin-bottom: 1rem;
    border-bottom: 1px solid #dee2e6;
}

.chat-header h6 {
    margin: 0;
    font-size: 1rem;
}

/* ===== 清除聊天按钮 ===== */
#clear-chat-btn {
    min-height: 36px;
    padding: 0.5rem 0.75rem;
    font-size: 0.85rem;
}

/* ===== 移动端聊天优化 ===== */
@media (max-width: 575px) {
    #chat-messages {
        max-height: calc(100vh - 400px);
        padding: 0.75rem;
        gap: 0.5rem;
    }

    .message {
        gap: 0.375rem;
    }

    .message-content {
        max-width: 85%;
        padding: 0.5rem 0.75rem;
        font-size: 0.95rem;
        border-radius: 0.75rem;
    }

    .message.user .message-content {
        border-bottom-right-radius: 0.15rem;
    }

    .message.assistant .message-content {
        border-bottom-left-radius: 0.15rem;
    }

    .message-avatar {
        width: 28px;
        height: 28px;
        font-size: 14px;
    }

    .typing-indicator {
        max-width: 85%;
        padding: 0.5rem 0.75rem;
        font-size: 0.9rem;
    }

    .chat-input-container {
        gap: 0.375rem;
        margin-top: 0.75rem;
    }

    .chat-input-container .form-control {
        border-radius: 18px;
        padding: 0.65rem 0.85rem;
        font-size: 15px !important;
        min-height: 40px;
    }

    .chat-input-container .btn {
        width: 40px;
        height: 40px;
        min-width: 40px;
    }

    .chat-header {
        padding: 0.5rem 0;
        margin-bottom: 0.75rem;
        gap: 0.5rem;
    }

    .chat-header h6 {
        font-size: 0.95rem;
    }

    #clear-chat-btn {
        min-height: 32px;
        padding: 0.4rem 0.6rem;
        font-size: 0.75rem;
    }

    #quick-questions {
        margin-top: 0.75rem;
        padding-top: 0.75rem;
    }

    #quick-questions .small {
        font-size: 0.75rem;
        margin-bottom: 0.5rem !important;
    }

    .quick-question-btn {
        flex: 0 1 calc(50% - 0.25rem);
        font-size: 0.7rem;
        padding: 0.35rem 0.5rem;
        min-height: 32px;
    }
}

/* 平板设备优化 */
@media (min-width: 576px) and (max-width: 1023px) {
    #chat-messages {
        max-height: 50vh;
    }

    .message-content {
        max-width: 80%;
        font-size: 0.95rem;
    }

    .quick-question-btn {
        flex: 0 1 calc(33% - 0.4rem);
        font-size: 0.8rem;
    }
}

/* 横屏模式优化 */
@media (orientation: landscape) and (max-height: 500px) {
    #chat-messages {
        max-height: 25vh;
    }

    .message-content {
        padding: 0.4rem 0.6rem;
        font-size: 0.85rem;
    }

    .chat-input-container {
        margin-top: 0.5rem;
    }

    .quick-question-btn {
        font-size: 0.65rem;
        padding: 0.3rem 0.5rem;
        min-height: 28px;
    }
}

/* ===== 虚拟键盘处理 ===== */
@media (hover: none) and (pointer: coarse) {
    /* 触摸设备 */
    .chat-input-container {
        position: sticky;
        bottom: 0;
        background: white;
        border-top: 1px solid #dee2e6;
        padding: 0.75rem 0;
        z-index: 10;
    }

    .message-content {
        user-select: none;
        -webkit-user-select: none;
    }

    .quick-question-btn:active {
        opacity: 0.8;
    }
}

/* ===== 消息内容格式化 ===== */
.message-content h1,
.message-content h2,
.message-content h3,
.message-content h4,
.message-content h5,
.message-content h6 {
    margin: 0.5rem 0 0.25rem 0;
    font-size: 0.95rem;
}

.message-content ul,
.message-content ol {
    margin: 0.5rem 0;
    padding-left: 1.25rem;
}

.message-content li {
    margin-bottom: 0.25rem;
}

.message-content strong {
    font-weight: 600;
    color: inherit;
}

.message-content em {
    font-style: italic;
}

/* ===== 顶部安全区域 ===== */
@supports (padding: max(0px)) {
    body {
        padding-top: max(1rem, env(safe-area-inset-top));
    }
}

/* ===== 底部安全区域 ===== */
@supports (padding: max(0px)) {
    .chat-input-container {
        padding-bottom: max(0.75rem, env(safe-area-inset-bottom));
    }
}

/* ===== 加载动画 ===== */
.loading-spinner {
    display: inline-block;
    width: 1rem;
    height: 1rem;
    border: 2px solid #f3f3f3;
    border-top: 2px solid #0d6efd;
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* ===== 无消息状态 ===== */
#ai-placeholder {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 300px;
    color: #6c757d;
}

#ai-placeholder .bi {
    font-size: 3rem;
    margin-bottom: 1rem;
    opacity: 0.5;
}

#ai-placeholder p {
    margin: 0;
    font-size: 1rem;
}
