/* 响应式设计 */

/* 平板设备 */
@media (max-width: 768px) {
    .app {
        height: 100vh;
        /* iOS Safari 视口修复 */
        height: calc(var(--vh, 1vh) * 100);
        min-height: -webkit-fill-available;
        max-width: 100%;
        border-radius: 0;
        box-shadow: none;
    }

    .app-header {
        padding: 1.2rem 1rem;
    }

    .app-header h1 {
        font-size: 1.4rem;
    }

    .device-info {
        font-size: 0.8rem;
        flex-direction: row;
        gap: 1rem;
    }

    .connection-status {
        font-size: 0.75rem;
    }

    .message {
        max-width: 85%;
        margin-bottom: 0.5rem;
        padding: 0 0.25rem;
    }

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

    .message-list {
        padding: 1rem 0.5rem;
        gap: 0;
    }

    .input-container {
        padding: 8px 12px;
        min-height: 56px;
        /* iOS 平板特殊处理 */
        padding-bottom: calc(8px + var(--safe-area-inset-bottom));
        position: -webkit-sticky;
        position: sticky;
    }

    .upload-area {
        padding: 1.5rem 1rem;
    }

    .input-wrapper {
        gap: 8px;
    }

    .file-button {
        width: 42px;
        height: 42px;
        font-size: 18px;
    }

    .input-field-container {
        min-height: 42px;
    }

    .input-field-container textarea {
        font-size: 16px;
        padding: 11px 13px;
    }
}

/* 手机设备 */
@media (max-width: 480px) {
    .app-header {
        padding: 1rem 0.75rem;
    }

    .app-header h1 {
        font-size: 1.3rem;
        margin-bottom: 0.3rem;
    }

    .device-info {
        font-size: 0.75rem;
        flex-direction: column;
        gap: 0.4rem;
    }

    .connection-status {
        font-size: 0.7rem;
        padding: 0.15rem 0.4rem;
    }

    .message-list {
        padding: 0.75rem 0.5rem;
        gap: 0;
    }

    .message {
        max-width: 88%; /* 稍微减小最大宽度，确保在小屏幕上有足够边距 */
        margin-bottom: 0.75rem; /* 增加消息间距 */
        padding: 0 0.5rem; /* 增加水平内边距 */
        /* 确保消息容器有最小宽度 */
        min-width: 120px;
    }

    .message-content {
        padding: 0.5rem 0.75rem; /* 减少内边距，更紧凑 */
        font-size: 1rem !important; /* 确保字体大小足够 */
        border-radius: 0.5rem; /* 保持适中的圆角 */
        /* 确保内容容器有最小高度 */
        min-height: 2rem; /* 减少最小高度 */
        /* 改善文字渲染 */
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
        /* 确保文字布局 */
        display: flex;
        flex-direction: column;
        justify-content: center;
        /* 强化移动端换行设置 */
        word-wrap: break-word !important;
        overflow-wrap: break-word !important;
        word-break: break-word !important;
        /* 确保容器不会超出父元素 */
        max-width: 100% !important;
        box-sizing: border-box !important;
        /* 防止内容溢出 */
        overflow: hidden !important;
        /* 优化行高 */
        line-height: 1.4 !important;
    }

    /* 手机设备上的文本消息优化 */
    .message-content .text-message:not(.markdown-rendered) {
        padding: 0.25rem 0 !important; /* 减少垂直内边距，更紧凑 */
        margin: 0 !important;
        font-weight: 400 !important;
        letter-spacing: 0.01em !important;
        font-size: 1rem !important; /* 确保字体大小足够 */
        line-height: 1.4 !important; /* 优化行高 */
        min-height: 1.2em !important; /* 减少最小高度 */
        display: block !important;
        /* 移动端强化换行设置 */
        white-space: pre-wrap !important;
        word-wrap: break-word !important;
        overflow-wrap: break-word !important;
        word-break: break-word !important;
        /* 确保长URL和字符串换行 */
        hyphens: auto !important;
        /* 确保宽度限制 */
        max-width: 100% !important;
        box-sizing: border-box !important;
    }

    .message-content .text-message.markdown-rendered {
        padding: 0.25rem 24px 0.25rem 0 !important; /* 减少内边距 */
        font-weight: 400 !important;
        letter-spacing: 0.01em !important;
        font-size: 1rem !important;
        line-height: 1.4 !important; /* 优化行高 */
    }

    .message-meta {
        font-size: 0.65rem;
        margin-bottom: 0.3rem;
    }

    .message-time {
        font-size: 0.6rem;
        padding: 0.15rem 0.4rem;
    }

    .input-container {
        padding: 8px 12px;
        min-height: 56px;
        /* iOS 手机特殊处理 */
        padding-bottom: calc(8px + var(--safe-area-inset-bottom));
        position: -webkit-sticky;
        position: sticky;
    }

    .input-wrapper {
        gap: 8px;
    }

    .file-button {
        width: 40px;
        height: 40px;
        font-size: 18px;
    }

    .input-field-container {
        min-height: 40px;
    }

    .input-field-container textarea {
        font-size: 16px;
        padding: 10px 12px;
    }

    .send-button {
        width: 34px;
        height: 34px;
    }

    .send-icon {
        width: 17px;
        height: 17px;
    }
    
    .file-upload {
        margin-bottom: 0.75rem;
    }
    
    .upload-area {
        padding: 0.75rem;
    }
    
    .upload-prompt {
        gap: 0.25rem;
    }
    
    .upload-icon {
        font-size: 1.2rem;
    }
    
    .file-info {
        gap: 0.5rem;
    }
    
    .file-icon {
        font-size: 1.2rem;
    }
    
    .download-btn {
        padding: 0.4rem 0.6rem;
        font-size: 0.75rem;
    }
    
    .app-footer {
        padding: 0.5rem;
    }
    
    .refresh-button {
        padding: 0.4rem 0.8rem;
        font-size: 0.8rem;
    }
}

/* 超小屏幕 */
@media (max-width: 320px) {
    .app-header h1 {
        font-size: 1.1rem;
    }
    
    .message-content {
        padding: 0.5rem 0.75rem; /* 减少内边距，更紧凑 */
        font-size: 0.95rem !important; /* 确保字体不会太小 */
        /* 确保在超小屏幕上也有足够的最小高度 */
        min-height: 1.8rem; /* 减少最小高度 */
        /* 改善布局 */
        display: flex;
        flex-direction: column;
        justify-content: center;
        /* 优化行高 */
        line-height: 1.4 !important;
    }

    /* 超小屏幕的文本消息优化 */
    .message-content .text-message:not(.markdown-rendered) {
        padding: 0.2rem 0 !important; /* 进一步减少内边距 */
        margin: 0 !important;
        font-weight: 400 !important;
        letter-spacing: 0.01em !important;
        font-size: 0.95rem !important; /* 稍微减小字体但保持可读性 */
        line-height: 1.4 !important; /* 优化行高 */
        min-height: 1.2em !important; /* 减少最小高度 */
        display: block !important;
        /* 超小屏幕强化换行设置 */
        white-space: pre-wrap !important;
        word-wrap: break-word !important;
        overflow-wrap: break-word !important;
        word-break: break-word !important;
        hyphens: auto !important;
        max-width: 100% !important;
        box-sizing: border-box !important;
    }

    .message-content .text-message.markdown-rendered {
        padding: 0.2rem 20px 0.2rem 0 !important; /* 减少内边距 */
        font-weight: 400 !important;
        letter-spacing: 0.01em !important;
        font-size: 0.95rem !important;
        line-height: 1.4 !important; /* 优化行高 */
    }
    
    .file-name {
        font-size: 0.85rem;
    }
    
    .file-size {
        font-size: 0.75rem;
    }
    
    .input-field-container textarea {
        font-size: 15px;
        padding: 8px 10px;
    }

    .file-button {
        width: 38px;
        height: 38px;
        font-size: 16px;
    }

    .send-button {
        width: 30px;
        height: 30px;
    }

    .send-icon {
        width: 15px;
        height: 15px;
    }
}

/* 横屏适配 */
@media (max-height: 500px) and (orientation: landscape) {
    .app-header {
        padding: 0.5rem;
    }
    
    .app-header h1 {
        font-size: 1.1rem;
        margin-bottom: 0.2rem;
    }
    
    .device-info {
        font-size: 0.7rem;
    }
    
    .message-list {
        padding: 0.5rem;
    }
    
    .input-container {
        padding: 0.5rem;
    }
    
    .app-footer {
        padding: 0.4rem;
    }
}

/* iOS Safari 专用修复 */
@supports (-webkit-touch-callout: none) {
    .app {
        height: -webkit-fill-available;
        min-height: -webkit-fill-available;
    }

    .input-container {
        /* 强制硬件加速，确保在iOS上正确渲染 */
        -webkit-transform: translate3d(0, 0, 0);
        transform: translate3d(0, 0, 0);
        /* 确保在虚拟键盘弹出时保持可见 */
        position: -webkit-sticky;
        position: sticky;
        bottom: 0;
        /* iOS 特殊的z-index处理 */
        z-index: 9999;
    }
}

/* iOS 设备特定修复 */
@media screen and (-webkit-min-device-pixel-ratio: 2) and (orientation: portrait) {
    .app {
        height: calc(var(--vh, 1vh) * 100);
        min-height: calc(var(--vh, 1vh) * 100);
    }

    .input-container {
        /* 确保输入框在iOS上始终可见 */
        position: fixed;
        bottom: var(--safe-area-inset-bottom, 0);
        left: 0;
        right: 0;
        width: 100%;
        max-width: 800px;
        margin: 0 auto;
    }

    .message-list {
        /* 为固定的输入框留出空间 */
        padding-bottom: calc(72px + var(--safe-area-inset-bottom, 0));
    }
}

/* iPhone X 及以上设备的安全区域适配 */
@media screen and (device-width: 375px) and (device-height: 812px) and (-webkit-device-pixel-ratio: 3),
       screen and (device-width: 414px) and (device-height: 896px) and (-webkit-device-pixel-ratio: 2),
       screen and (device-width: 414px) and (device-height: 896px) and (-webkit-device-pixel-ratio: 3),
       screen and (device-width: 390px) and (device-height: 844px) and (-webkit-device-pixel-ratio: 3),
       screen and (device-width: 428px) and (device-height: 926px) and (-webkit-device-pixel-ratio: 3) {
    .input-container {
        padding-bottom: calc(8px + 34px); /* 34px 是 iPhone X 系列的底部安全区域 */
    }
}

/* iOS 虚拟键盘弹出时的样式调整 */
body.keyboard-open .app {
    height: 100vh;
    height: calc(var(--vh, 1vh) * 100);
}

body.keyboard-open .input-container {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    max-width: 800px;
    margin: 0 auto;
    z-index: 10000;
    /* 确保在虚拟键盘上方 */
    transform: translateY(0);
}

body.keyboard-open .message-list {
    /* 为固定输入框预留更多空间 */
    padding-bottom: 80px;
}

/* 高分辨率屏幕 */
@media (min-width: 1200px) {
    .app {
        max-width: 900px;
    }

    .message {
        max-width: 60%;
    }

    .app-header h1 {
        font-size: 1.6rem;
    }

    .message-content {
        font-size: 1.05rem;
        padding: 1rem 1.25rem;
    }

    /* 高分辨率屏幕的文本消息优化 */
    .message-content .text-message:not(.markdown-rendered) {
        padding: 0.5rem 0 !important; /* 保持一致的内边距 */
        margin: 0 !important;
        font-weight: 400 !important;
        letter-spacing: 0.01em !important;
        display: block !important;
    }

    .message-content .text-message.markdown-rendered {
        padding: 0.5rem 28px 0.5rem 0 !important; /* 统一内边距 */
        font-weight: 400 !important;
        letter-spacing: 0.01em !important;
    }
}

/* 打印样式 */
@media print {
    .app-header,
    .input-container,
    .app-footer {
        display: none;
    }
    
    .app {
        height: auto;
        box-shadow: none;
    }
    
    .message-list {
        padding: 0;
    }
    
    .message-content {
        border: 1px solid #ddd;
        background-color: white !important;
        color: black !important;
    }
}
