/**
 * 跨浏览器兼容性全局补丁
 * 重点兼容：iOS Safari、macOS Safari、移动端浏览器
 * 原则：非侵入式加固，不改动现有排版布局和业务逻辑
 */

/* ===== 1. 苹果设备字体平滑与基础渲染优化 ===== */
* {
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

/* ===== 2. 消除苹果 Safari 原生控件的系统灰色内阴影与圆角 ===== */
/* 注意：复选框和单选框需要保留原生外观，不能设置 appearance: none */
input:not([type="checkbox"]):not([type="radio"]),
select,
textarea,
button {
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    -webkit-tap-highlight-color: transparent;
}

/* 确保控件背景色和边框不被苹果系统覆盖（复选框和单选框除外）*/
input[type="text"],
input[type="password"],
input[type="email"],
input[type="number"],
input[type="tel"],
input[type="url"],
select,
textarea {
    background-color: #ffffff;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
}

/* 复选框和单选框保留原生样式 */
input[type="checkbox"],
input[type="radio"] {
    -webkit-appearance: auto;
    -moz-appearance: auto;
    appearance: auto;
}

/* ===== 3. 优化移动端触控交互（防止双击放大和点击延迟）===== */
button,
a,
.btn-decrease,
.btn-increase,
.chip,
.tab,
.mode-btn,
.prompt-mode-tab,
.collapsible-header,
.btn-save,
.btn-logout,
.btn-get-token,
.btn-help,
.btn-generate,
.btn-secondary,
.btn-template,
.btn-retry,
.number-input-group button {
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
    user-select: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
}

/* ===== 4. 修复 iOS Safari 底部安全区与动态视口高度 ===== */
html {
    height: 100%;
    height: -webkit-fill-available;
}

body {
    min-height: 100vh;
    min-height: 100dvh;
    min-height: -webkit-fill-available;
    padding-bottom: calc(20px + env(safe-area-inset-bottom));
}

/* 容器适配安全区 */
.container {
    padding-bottom: env(safe-area-inset-bottom);
}

/* ===== 5. iOS Safari 防止输入框自动强制变焦（移动端 16px 字号补丁）===== */
@media screen and (max-width: 768px) {
    /* 全站所有输入类控件在移动端强制 16px 字号，防止 iOS 自动放大 */
    input,
    select,
    textarea {
        font-size: 16px !important;
    }

    /* 数字输入框 */
    input[type="number"] {
        font-size: 16px !important;
    }

    /* 确保按钮文字也是 16px */
    button {
        font-size: 16px !important;
    }

    /* 标签文字保持 14px */
    label {
        font-size: 14px !important;
    }

    /* 提示词输入区域 */
    #tti-prompt,
    #iti-prompt,
    #iti-merge-prompt,
    #ttv-prompt,
    #itv-prompt {
        font-size: 16px !important;
    }

    /* Token 输入框 */
    .token-input {
        font-size: 16px !important;
    }

    /* 模型参数配置组件的输入框 */
    .model-params-item select,
    .model-params-item input[type="text"],
    .model-params-item input[type="number"] {
        font-size: 16px !important;
    }

    /* 图片修改界面的输入框 */
    .iti-option-item select,
    .iti-option-item input[type="text"],
    .iti-option-item input[type="number"] {
        font-size: 16px !important;
    }
}

/* ===== 6. Safari Flex 布局兼容性修复 ===== */
.form-row,
.iti-options-row,
.model-params-row-2,
.batch-images-header {
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
}

/* ===== 7. 确保 Safari 中 number 输入框的数字居中 ===== */
input[type="number"] {
    -moz-appearance: textfield;
    text-align: center;
}

input[type="number"]::-webkit-inner-spin-button,
input[type="number"]::-webkit-outer-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

/* ===== 8. Safari 图片渲染优化 ===== */
img {
    -webkit-user-drag: none;
    user-select: none;
    -webkit-touch-callout: none;
}

/* ===== 9. Safari 滚动优化 ===== */
* {
    -webkit-overflow-scrolling: touch;
}

/* ===== 10. 防止 Safari 中文本域自动调整大小 ===== */
textarea {
    resize: vertical;
    -webkit-appearance: none;
}

/* ===== 11. Safari 中 select 下拉箭头统一 ===== */
select {
    background-position: right 10px center;
    padding-right: 32px;
}

/* ===== 12. 移动端优化按钮最小点击区域 ===== */
@media screen and (max-width: 768px) {
    button,
    a.btn-get-token,
    .btn-help {
        min-height: 44px;
        min-width: 44px;
    }

    .number-input-group .btn-decrease,
    .number-input-group .btn-increase {
        min-width: 44px;
        min-height: 44px;
    }
}

/* ===== 13. iOS 防止长按菜单弹出 ===== */
a,
button,
img {
    -webkit-touch-callout: none;
}

/* ===== 14. Safari 中确保圆角正常显示 ===== */
* {
    -webkit-background-clip: padding-box;
    background-clip: padding-box;
}

/* ===== 15. 移动端隐藏水平滚动条 ===== */
@media screen and (max-width: 768px) {
    body {
        overflow-x: hidden;
    }

    .container {
        overflow-x: hidden;
    }
}

/* ===== 16. Safari 中确保 Grid 布局正常工作 ===== */
.form-row {
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
}

/* ===== 17. 确保在 Safari 中 z-index 层级正常 ===== */
.modal,
.help-modal,
.notification {
    -webkit-transform: translateZ(0);
    transform: translateZ(0);
}

/* ===== 18. iOS Safari 修复固定定位元素 ===== */
.header {
    -webkit-transform: translateZ(0);
    transform: translateZ(0);
}

/* ===== 19. 移动端优化表单标签间距 ===== */
@media screen and (max-width: 768px) {
    .form-group {
        margin-bottom: 20px;
    }
}

/* ===== 20. Safari 确保动画流畅 ===== */
* {
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
}
