/**
 * 全局前端交互优化
 * 解决：点击延迟、响应速度、触摸反馈、滚动性能
 */

/* ============ 1. 消除300ms点击延迟 ============ */
html {
    -webkit-tap-highlight-color: transparent;
    /* 注意：不在html上设置touch-action，会影响iOS滚动 */
}

/* 仅对可点击元素消除延迟，保留滚动能力 */
a, button, input, select, textarea,
[onclick], [role="button"], .clickable {
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

/* ============ 2. 点击反馈增强 ============ */

/* 通用按钮点击效果 */
button, .btn, 
input[type="submit"], 
input[type="button"],
[role="button"],
.clickable {
    cursor: pointer;
    user-select: none;
    -webkit-user-select: none;
    transition: transform 0.1s ease, opacity 0.1s ease, box-shadow 0.15s ease;
    transform: translateZ(0); /* GPU加速 */
    will-change: transform;
}

/* 按钮按下效果 - 更明显的视觉反馈 */
button:active, .btn:active,
input[type="submit"]:active,
input[type="button"]:active,
[role="button"]:active,
.clickable:active {
    transform: scale(0.96) translateZ(0);
    opacity: 0.9;
}

/* 禁用状态 */
button:disabled, .btn:disabled,
input[type="submit"]:disabled,
input[type="button"]:disabled {
    opacity: 0.6;
    cursor: not-allowed;
    transform: none !important;
}

/* 链接点击效果 */
a {
    cursor: pointer;
    transition: opacity 0.1s ease;
}

a:active {
    opacity: 0.7;
}

/* 卡片点击效果 */
.card, .algorithm-card, .formulaCard,
.algo-card, .ranking-item, .my-menu-item,
[data-clickable="true"] {
    cursor: pointer;
    transition: transform 0.15s ease, box-shadow 0.15s ease, border-color 0.15s ease;
    transform: translateZ(0);
}

.card:active, .algorithm-card:active, .formulaCard:active,
.algo-card:active, .ranking-item:active, .my-menu-item:active,
[data-clickable="true"]:active {
    transform: scale(0.98) translateZ(0);
}

/* ============ 3. 输入框优化 ============ */

input, textarea, select {
    /* 消除iOS输入框阴影 */
    -webkit-appearance: none;
    appearance: none;
    /* 防止缩放 */
    font-size: 16px !important;
    /* 流畅过渡 */
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

/* 输入框获得焦点效果 */
input:focus, textarea:focus, select:focus {
    outline: none;
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.15);
}

/* 输入框placeholder优化 */
input::placeholder, textarea::placeholder {
    color: #9ca3af;
    opacity: 1;
}

/* ============ 4. 滚动性能优化 ============ */

html {
    scroll-behavior: smooth;
    /* iOS Safari 滚动修复 */
    height: 100%;
}

body {
    /* 流畅滚动 - iOS关键 */
    -webkit-overflow-scrolling: touch;
    /* 确保body可滚动 */
    min-height: 100%;
    /* 不限制滚动 */
    overflow-x: hidden;
    overflow-y: auto;
}

/* 可滚动容器优化 */
.scroll-container,
.scrollable,
[style*="overflow"],
.modal-content,
.recommend-history-list,
.history-list {
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    /* 隐藏滚动条但保留功能 */
    scrollbar-width: thin;
}

/* 自定义滚动条（桌面端） */
::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: rgba(0, 0, 0, 0.2);
    border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
    background: rgba(0, 0, 0, 0.3);
}

/* ============ 5. 骨架屏加载动画 ============ */

/* 骨架屏占位符 */
.skeleton {
    background: linear-gradient(
        90deg,
        rgba(255, 255, 255, 0.1) 25%,
        rgba(255, 255, 255, 0.2) 50%,
        rgba(255, 255, 255, 0.1) 75%
    );
    background-size: 200% 100%;
    animation: skeleton-loading 1.5s infinite;
    border-radius: 4px;
}

.skeleton-light {
    background: linear-gradient(
        90deg,
        #f0f0f0 25%,
        #e0e0e0 50%,
        #f0f0f0 75%
    );
    background-size: 200% 100%;
    animation: skeleton-loading 1.5s infinite;
    border-radius: 4px;
}

@keyframes skeleton-loading {
    0% { background-position: 200% 0; }
    100% { background-position: -200% 0; }
}

/* 加载旋转动画 */
.loading-spin {
    animation: spin 1s linear infinite;
}

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

/* 脉冲加载效果 */
.loading-pulse {
    animation: pulse 1.5s ease-in-out infinite;
}

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

/* ============ 6. 页面过渡动画 ============ */

/* 淡入效果 */
.fade-in {
    animation: fadeIn 0.3s ease-out;
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

/* 从下滑入 */
.slide-up {
    animation: slideUp 0.3s ease-out;
}

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

/* 从上滑入 */
.slide-down {
    animation: slideDown 0.3s ease-out;
}

@keyframes slideDown {
    from { 
        opacity: 0;
        transform: translateY(-20px);
    }
    to { 
        opacity: 1;
        transform: translateY(0);
    }
}

/* 缩放进入 */
.scale-in {
    animation: scaleIn 0.2s ease-out;
}

@keyframes scaleIn {
    from { 
        opacity: 0;
        transform: scale(0.9);
    }
    to { 
        opacity: 1;
        transform: scale(1);
    }
}

/* ============ 7. 快速点击优化（防止双击缩放） ============ */

/* 仅对可点击元素禁用双击缩放，不影响滚动 */
/* 注意：不要对 * 设置 touch-action，会影响iOS滚动 */

/* 按钮区域禁用文本选择 */
button, .btn, a, [role="button"] {
    -webkit-user-select: none;
    user-select: none;
}

/* ============ 8. 性能优化 ============ */

/* GPU加速层 */
.gpu-accelerate {
    transform: translateZ(0);
    will-change: transform;
    backface-visibility: hidden;
}

/* 减少重绘区域 */
.contain-layout {
    contain: layout;
}

.contain-paint {
    contain: paint;
}

/* 固定元素优化 */
.site-header,
header,
nav,
.fixed-bottom,
.floating-btn,
.refresh-btn {
    transform: translateZ(0);
    will-change: transform;
    backface-visibility: hidden;
}

/* ============ 9. 弹窗/模态框优化 ============ */

.modal, .popup, .overlay {
    /* 点击背景关闭时的反馈 */
    cursor: pointer;
}

.modal-content, .popup-content {
    cursor: default;
}

/* 弹窗打开动画 */
.modal.show .modal-content,
.popup.show .popup-content {
    animation: modalIn 0.25s ease-out;
}

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

/* ============ 10. Toast提示优化 ============ */

.toast {
    pointer-events: none;
    touch-action: none;
    transform: translateX(-50%) translateY(20px) translateZ(0);
    opacity: 0;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

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

/* ============ 11. 图片优化 ============ */

img {
    /* 防止图片拖拽 */
    -webkit-user-drag: none;
    /* 优化渲染 */
    image-rendering: -webkit-optimize-contrast;
}

/* 图片懒加载占位 */
img[loading="lazy"] {
    background: linear-gradient(
        90deg,
        #f0f0f0 25%,
        #e8e8e8 50%,
        #f0f0f0 75%
    );
    background-size: 200% 100%;
    animation: skeleton-loading 1.5s infinite;
}

img[loading="lazy"].loaded {
    animation: none;
    background: none;
}

/* ============ 12. 导航标签页优化 ============ */

.nav-tab, .tab-item, .filter-tab,
.algo-filter-btn, .sort-btn, .category-btn {
    position: relative;
    overflow: hidden;
    /* 点击涟漪准备 */
}

/* 涟漪效果 */
.ripple {
    position: absolute;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.4);
    transform: scale(0);
    animation: ripple 0.6s linear;
    pointer-events: none;
}

@keyframes ripple {
    to {
        transform: scale(4);
        opacity: 0;
    }
}

/* ============ 13. 暗色主题下的优化 ============ */

/* 针对暗色背景页面 */
.dark-theme input,
.dark-theme textarea,
.dark-theme select,
body[style*="background: linear-gradient"] input,
body[style*="background: #0f"] input {
    background: rgba(255, 255, 255, 0.1);
    border-color: rgba(255, 255, 255, 0.2);
    color: #fff;
}

/* ============ 14. 移动端特定优化 ============ */

@media (max-width: 768px) {
    /* 增大触摸目标 - 仅限主要操作按钮，不影响筛选按钮 */
    .btn-submit,
    .btn-primary,
    .vip-btn,
    .auth-submit-btn,
    input[type="submit"] {
        min-height: 44px;
    }
    
    /* 输入框高度 */
    input[type="text"],
    input[type="password"],
    input[type="email"],
    textarea,
    select {
        min-height: 44px;
    }
    
    /* 更明显的按下效果 */
    button:active, .btn:active {
        transform: scale(0.95) translateZ(0);
    }
}

/* ============ 15. 减少动画（尊重用户偏好） ============ */

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}
