/* ==================== 移动端增强样式（增量覆盖，务必最后加载） ====================
 * 目标：在不改动既有桌面端表现的前提下，提升手机端的可读性、可点性与稳定性。
 * 说明：本项目 index.html 的聊天输入区是 .input-group（并非 .chat-input-container），
 *       故此处直接针对真实 DOM 结构编写。
 * ========================================================================== */

:root {
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-left: env(safe-area-inset-left, 0px);
    --safe-right: env(safe-area-inset-right, 0px);
    --app-vh: 100vh; /* 由 mobile-enhance.js 更新为真实可视高度 */
}

/* ---------- 全局：防横向溢出 + 触控友好 ---------- */
html,
body {
    max-width: 100%;
    overflow-x: hidden;
}

* {
    -webkit-tap-highlight-color: rgba(13, 110, 253, 0.12);
}

a,
button,
.btn,
.nav-link,
input,
select,
textarea,
label {
    touch-action: manipulation; /* 去掉 300ms 点击延迟 */
}

img,
video,
table,
pre,
code {
    max-width: 100%;
}

pre {
    white-space: pre-wrap;
    word-break: break-word;
}

/* 锚点跳转时留出呼吸空间 */
#bazi-section,
#ai-section,
#feedback-section,
#chat-history-section {
    scroll-margin-top: 0.75rem;
}

/* 卡片视觉更轻、圆角统一 */
.card {
    border-radius: 0.75rem;
}

/* ==================== 手机：≤ 767.98px ==================== */
@media (max-width: 767.98px) {

    body {
        padding-left: var(--safe-left);
        padding-right: var(--safe-right);
        line-height: 1.6;
    }

    /* ---------- 导航栏 ---------- */
    .navbar {
        padding-top: calc(0.5rem + var(--safe-top));
    }

    .navbar-brand {
        font-size: 1.05rem;
        font-weight: 600;
    }

    #navbarNav {
        padding-top: 0.5rem;
        padding-bottom: 0.25rem;
    }

    #navbarNav .nav-link,
    #navbarNav .dropdown-item {
        padding: 0.7rem 0.75rem;
        font-size: 1rem;
        border-radius: 0.5rem;
    }

    #navbarNav .dropdown-item:active {
        background-color: rgba(13, 110, 253, 0.1);
    }

    .dropdown-menu {
        border: 0;
        box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
        border-radius: 0.6rem;
    }

    /* ---------- 容器与间距 ---------- */
    .container,
    .container-fluid {
        padding-left: 0.75rem;
        padding-right: 0.75rem;
    }

    .card-body {
        padding: 0.875rem;
    }

    h5.card-title {
        font-size: 1.05rem;
    }

    /* ---------- 表格：横向可滑但不撑破布局 ---------- */
    .table-responsive {
        -webkit-overflow-scrolling: touch;
        overscroll-behavior-x: contain;
    }

    /* ---------- 聊天区：消息内滚 + 输入栏吸底 ---------- */
    #ai-chat-area {
        display: flex;
        flex-direction: column;
        min-height: 0;
    }

    #chat-messages {
        /* 用真实可视高度变量，避免移动端浏览器地址栏伸缩造成的跳动 */
        height: min(calc(var(--app-vh) * 0.5), 440px);
        max-height: none;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
        padding: 0.25rem;
    }

    #ai-chat-area .input-group {
        position: sticky;
        bottom: 0;
        z-index: 6;
        margin-top: 0.5rem;
        padding-bottom: calc(0.25rem + var(--safe-bottom));
        background: linear-gradient(to top, #fff 75%, rgba(255, 255, 255, 0));
    }

    #chat-input {
        min-height: 46px;
    }

    #send-message {
        min-width: 82px;
        min-height: 46px;
    }

    .message-content {
        max-width: 88%;
        font-size: 0.95rem;
        line-height: 1.6;
        word-break: break-word;
    }

    .message-content h3,
    .message-content h4,
    .message-content h5,
    .message-content h6 {
        font-size: 1rem;
        margin-top: 0.6rem;
    }

    .chat-header {
        gap: 0.5rem;
    }

    /* ---------- 模态框：手机端变底部抽屉 ---------- */
    .modal {
        padding: 0 !important;
    }

    .modal-dialog {
        margin: 0;
        max-width: 100%;
        min-height: 100%;
        display: flex;
        align-items: flex-end;
    }

    .modal-content {
        width: 100%;
        border: 0;
        border-radius: 1rem 1rem 0 0;
        max-height: 92vh;
        max-height: 92dvh;
        display: flex;
        flex-direction: column;
        box-shadow: 0 -8px 32px rgba(0, 0, 0, 0.18);
    }

    .modal-header {
        padding: 0.875rem 1rem;
        border-bottom: 1px solid #eef1f4;
    }

    .modal-body {
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
        padding: 1rem;
    }

    .modal-footer {
        padding: 0.75rem 1rem calc(0.75rem + var(--safe-bottom));
        gap: 0.5rem;
    }

    .modal-footer .btn {
        flex: 1 1 auto;
    }

    /* 抽屉式上滑动画 */
    .modal.fade .modal-dialog {
        transform: translateY(24px);
        transition: transform 0.28s ease-out;
    }

    .modal.show .modal-dialog {
        transform: none;
    }

    /* ---------- 提示条（toast）：铺满宽度、避开安全区 ---------- */
    .toast-container {
        left: 0.75rem;
        right: 0.75rem;
        bottom: calc(0.75rem + var(--safe-bottom));
    }

    .toast-container .toast {
        width: 100%;
    }

    /* ---------- 结果操作按钮：等宽两列 ---------- */
    .result-actions {
        display: grid !important;
        grid-template-columns: 1fr 1fr;
        width: 100%;
        gap: 0.5rem !important;
    }

    .result-actions .btn {
        width: 100%;
        justify-content: center;
    }
}

/* ==================== 超小屏：≤ 400px ==================== */
@media (max-width: 400px) {
    .navbar-brand {
        max-width: 60vw;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .message-content {
        max-width: 92%;
    }

    #chat-messages {
        height: min(calc(var(--app-vh) * 0.46), 380px);
    }
}

/* ==================== 横屏小高度：避免聊天区过高导致整页都看不到 ---------- */
@media (orientation: landscape) and (max-height: 520px) {
    #chat-messages {
        height: 34vh;
    }

    .modal-content {
        max-height: 96vh;
    }
}

/* ==================== 深色模式：与既有样式保持一致 ==================== */
@media (prefers-color-scheme: dark) {
    #ai-chat-area .input-group {
        background: linear-gradient(to top, #2a2a2a 75%, rgba(42, 42, 42, 0));
    }

    .modal-content {
        background-color: #2a2a2a;
        color: #e0e0e0;
    }

    .modal-header {
        border-bottom-color: #3a3a3a;
    }
}
