/* 移动端响应式样式 */

/* 禁用移动端的双击缩放和长按菜单 */
* {
    -webkit-tap-highlight-color: transparent;
}

/* 防止页面缩放但允许滚动 */
html {
    touch-action: pan-x pan-y;
    -webkit-text-size-adjust: 100%;
}

/* iOS Safari 性能优化 */
body {
    -webkit-overflow-scrolling: touch;
}

/* ============== 加载状态优化 ============== */

/* 移动端整个页面初始隐藏，防止加载时尺寸跳动 */
@media screen and (max-width: 768px) {
    /* 主内容区域初始隐藏 */
    .main-content,
    .canvas-container,
    .canvas {
        opacity: 0;
        visibility: hidden;
        transition: opacity 0.3s ease, visibility 0.3s ease;
    }
    
    /* 加载完成后显示 */
    .main-content.mobile-loaded,
    .main-content.mobile-loaded .canvas-container,
    .main-content.mobile-loaded .canvas {
        opacity: 1;
        visibility: visible;
    }
    
    /* top-content 初始隐藏，加载完成后显示 */
    .top-content {
        opacity: 0;
        visibility: hidden;
        transition: opacity 0.3s ease, visibility 0.3s ease;
    }
    
    .top-content.mobile-loaded {
        opacity: 1;
        visibility: visible;
    }
}

/* ============== 移动端媒体查询 ============== */

/* 平板和大屏手机 (768px 及以下) */
@media screen and (max-width: 768px) {
    body {
        padding: 10px;
        min-height: 100vh;
        overflow-x: hidden;
    }
    
    .app-container {
        padding: 0 10px;
        max-width: 100%;
    }
    
    /* 标题缩小 */
    .title {
        font-size: 24px;
        margin-bottom: 15px;
    }
    
    /* 主内容区域改为垂直布局 */
    .main-content {
        flex-direction: column;
        gap: 15px;
        align-items: center;
    }
    
    /* 侧边栏适配 */
    .sidebar {
        width: 100%;
        max-width: 100%;
        min-width: auto;
        padding: 15px;
        border-radius: 16px;
        order: 2; /* 把菜单放到画布下面 */
        display: block;
        height: auto;
    }
    
    /* 菜单网格调整为单列 */
    .menu-grid {
        grid-template-columns: 1fr;
        grid-template-rows: auto;
        gap: 10px;
        flex: none;
    }
    
    /* 取消最后一个section横跨两列（单列布局不需要） */
    .menu-grid .menu-section:last-child {
        grid-column: auto;
    }
    
    /* 菜单区块样式调整 */
    .menu-section {
        padding: 12px;
        border-radius: 12px;
    }
    
    .menu-section h3 {
        font-size: 14px;
        margin-bottom: 12px;
    }
    
    /* 上传按钮组调整 */
    .upload-buttons {
        grid-template-columns: repeat(4, 1fr);
        gap: 8px;
    }
    
    .upload-group {
        gap: 4px;
    }
    
    .upload-slot-label {
        font-size: 11px;
    }
    
    /* 移动端合并状态适配 */
    .upload-group.merged-horizontal,
    .upload-group.merged-vertical {
        grid-column: span 2;
    }
    
    .upload-group.merged-hidden {
        display: none;
    }
    
    .upload-btn-label {
        padding: 8px;
        border-radius: 8px;
    }
    
    .upload-btn-text {
        padding: 5px 8px;
        font-size: 10px;
        min-width: 40px;
        max-width: 50px;
    }
    
    /* 颜色控制容器 */
    .color-controls {
        gap: 12px;
    }
    
    /* 背景控制 */
    .bg-control {
        flex-wrap: nowrap;
        gap: 6px;
    }
    
    .bg-control label {
        min-width: 50px;
        font-size: 12px;
    }
    
    .bg-control input[type="color"] {
        width: 28px;
        height: 28px;
    }
    
    .bg-control input[type="text"] {
        flex: 1;
        padding: 6px 8px;
        font-size: 12px;
    }
    
    /* 涂鸦上传控件 */
    .doodle-upload-controls {
        margin-top: 10px;
        padding-top: 10px;
    }
    
    .doodle-upload-group {
        gap: 6px;
    }
    
    .doodle-upload-btn {
        padding: 6px 10px;
        min-width: 80px;
    }
    
    .doodle-upload-btn span {
        font-size: 11px;
    }
    
    .doodle-reset-btn {
        padding: 6px 10px;
        font-size: 11px;
    }
    
    /* 文字编辑区域 - 简化布局 */
    .text-inputs-inline {
        flex-direction: column;
        gap: 8px;
        margin-bottom: 10px;
    }
    
    .text-inputs-inline input[type="text"] {
        padding: 10px 12px;
        font-size: 13px;
    }
    
    /* 圆角和尺寸控制 */
    .radius-control label,
    .size-control label,
    .margin-control label,
    .doodle-gap-control label {
        font-size: 12px;
        margin-bottom: 8px;
    }
    
    /* 空间合并按钮 - 手机端调整 */
    .merge-grid-middle {
        justify-content: center; /* 改为居中对齐 */
        gap: 60px; /* 设置固定间距 */
        padding: 0; /* 移除左右内边距 */
    }
    
    .merge-btn-horizontal {
        width: 100px; /* 固定宽度 */
    }
    
    .merge-btn-vertical {
        height: 80px; /* 增加高度 */
    }
    
    /* 操作按钮 */
    .action-buttons {
        gap: 8px;
        flex-direction: row;
        flex-wrap: wrap;
    }
    
    .action-btn {
        padding: 10px 12px;
        font-size: 13px;
        flex: 1 1 45%;
        min-width: 100px;
    }
    
    /* 画布容器 */
    .canvas-container {
        width: 100%;
        padding: 15px;
        border-radius: 16px;
        order: 1; /* 画布放到上面 */
        overflow: visible;
        display: block;
    }
    
    /* 画布缩放适配 - 不在CSS中设置transform，由JS动态计算 */
    .canvas {
        flex-shrink: 0;
    }
    
    /* 模态框适配 */
    .modal-content {
        width: 95%;
        margin: 10% auto;
        padding: 15px;
    }
    
    .crop-container {
        height: 300px;
    }
    
    /* 确认弹窗适配 */
    .confirm-dialog {
        min-width: 90%;
        max-width: 95%;
        padding: 24px 20px;
        margin: 0 10px;
    }
    
    .confirm-icon {
        width: 50px;
        height: 50px;
        margin-bottom: 15px;
    }
    
    .confirm-title {
        font-size: 18px;
    }
    
    .confirm-message {
        font-size: 14px;
    }
    
    .confirm-hint {
        font-size: 12px;
        padding: 10px 12px;
    }
    
    .confirm-buttons {
        gap: 12px;
    }
    
    .confirm-btn {
        padding: 10px 24px;
        font-size: 14px;
    }
}

/* 小屏手机 (480px 及以下) */
@media screen and (max-width: 480px) {
    body {
        padding: 8px;
    }
    
    .app-container {
        padding: 0 5px;
    }
    
    .title {
        font-size: 20px;
        margin-bottom: 12px;
    }
    
    .main-content {
        gap: 10px;
    }
    
    .sidebar {
        padding: 12px;
        border-radius: 12px;
    }
    
    .menu-grid {
        gap: 8px;
    }
    
    .menu-section {
        padding: 10px;
    }
    
    .menu-section h3 {
        font-size: 13px;
        margin-bottom: 10px;
        padding-left: 10px;
    }
    
    .menu-section h3::before {
        width: 3px;
        height: 14px;
    }
    
    /* 上传按钮组 - 小屏改为2列 */
    .upload-buttons {
        grid-template-columns: repeat(2, 1fr);
        gap: 6px;
    }
    
    .upload-btn-text {
        padding: 4px 6px;
        font-size: 9px;
    }
    
    /* 颜色控制容器 */
    .color-controls {
        gap: 10px;
    }
    
    /* 背景控制 */
    .bg-control label {
        min-width: 45px;
        font-size: 11px;
    }
    
    .bg-control input[type="color"] {
        width: 26px;
        height: 26px;
    }
    
    .bg-control input[type="text"] {
        flex: 1;
        padding: 5px 6px;
        font-size: 11px;
    }
    
    /* 涂鸦上传控件 */
    .doodle-upload-controls {
        margin-top: 8px;
        padding-top: 8px;
        gap: 8px;
    }
    
    .doodle-upload-btn {
        padding: 5px 8px;
        min-width: 70px;
    }
    
    .doodle-upload-btn span {
        font-size: 10px;
    }
    
    .doodle-reset-btn {
        padding: 5px 8px;
        font-size: 10px;
    }
    
    /* 空间合并按钮 - 小屏手机调整 */
    .merge-grid-middle {
        justify-content: center; /* 改为居中对齐 */
        gap: 110px; /* 设置固定间距 */
        padding: 0; /* 移除左右内边距 */
    }
    
    .merge-btn-horizontal {
        width: 100px; /* 固定宽度 */
    }
    
    .merge-btn-vertical {
        height: 80px; /* 增加高度 */
    }
    
    /* 操作按钮 - 2列布局 */
    .action-buttons {
        gap: 6px;
    }
    
    .action-btn {
        padding: 8px 10px;
        font-size: 12px;
        flex: 1 1 45%;
    }
    
    /* 画布容器 */
    .canvas-container {
        padding: 10px;
        border-radius: 12px;
    }
    
    /* 确认弹窗 */
    .confirm-dialog {
        padding: 20px 16px;
    }
    
    .confirm-title {
        font-size: 16px;
    }
    
    .confirm-btn {
        padding: 8px 20px;
        font-size: 13px;
    }
}

/* ============== 画布缩放类 ============== */

/* 画布缩放包装器 - 用于容纳缩放后的画布 */
.canvas-scale-wrapper {
    display: block;
    position: relative;
    margin: 0 auto;
    overflow: visible;
}

/* 画布缩放适配 - transform 和尺寸通过JS动态设置 */
.canvas.mobile-scaled {
    will-change: transform;
}

/* ============== 触摸优化 ============== */

/* 增大触摸目标 */
@media screen and (max-width: 768px) {
    /* 调整手柄大小以便触摸（增大触摸区域） */
    .resize-handle {
        width: 100px;
        height: 16px;
        border-radius: 8px;
        /* 阻止触摸被误判为页面滚动 */
        touch-action: none;
        /* 为伪元素定位提供参照 */
        position: absolute;
    }
    
    /* 扩展手柄触摸热区（视觉不变，触摸区域扩大到约44px高度） */
    .resize-handle::before {
        content: '';
        position: absolute;
        top: -14px;
        left: -10px;
        right: -10px;
        bottom: -14px;
        background: transparent;
    }
    
    /* 图片区域触摸优化 - 默认允许滚动 */
    .image-wrapper {
        touch-action: auto;
    }
    
    .image-preview {
        touch-action: auto;
    }
    
    /* 激活状态下禁用滚动，允许拖动 */
    .image-wrapper.mobile-active {
        touch-action: none;
    }
    
    .image-wrapper.mobile-active .image-preview {
        touch-action: none;
    }
    
    /* 位置编号在移动端稍小 */
    .position-number {
        font-size: 50px;
    }
    
    /* 上传按钮增大点击区域 */
    .upload-btn-label {
        min-height: 50px;
    }
    
    /* 输入框触摸优化 */
    input[type="text"],
    input[type="number"] {
        font-size: 16px; /* 防止iOS自动缩放 */
    }
    
    /* 滑块触摸优化 */
    input[type="range"] {
        height: 8px;
    }
    
    input[type="range"]::-webkit-slider-thumb {
        width: 24px;
        height: 24px;
    }
    
    input[type="range"]::-moz-range-thumb {
        width: 24px;
        height: 24px;
    }
}

/* ============== 拖动状态指示 ============== */

/* 正在拖动时的样式 */
.image-wrapper.is-dragging {
    opacity: 0.9;
    z-index: 100;
}

.group-c.is-dragging {
    opacity: 0.9;
}

/* ============== 移动端选中/激活状态 ============== */

@media screen and (max-width: 768px) {
    /* 图片激活状态 - 不需要特殊样式，因为手柄会显示 */
    .image-wrapper.mobile-active {
        z-index: var(--z-image-active, 50);
    }
    
    /* SVG 激活状态 - 发光效果 */
    .group-c.mobile-active {
        filter: drop-shadow(0 0 8px rgba(233, 30, 99, 0.5));
    }
}

/* ============== 横屏提示 ============== */

/* 横屏时的布局调整 */
@media screen and (max-width: 768px) and (orientation: landscape) {
    .main-content {
        flex-direction: row;
        flex-wrap: nowrap;
    }
    
    .canvas-container {
        order: 1;
        flex: 1;
        max-width: 60%;
    }
    
    .sidebar {
        order: 2;
        flex: 1;
        max-width: 40%;
        max-height: calc(100vh - 80px);
        overflow-y: auto;
    }
    
    .menu-grid {
        grid-template-columns: 1fr;
    }
}

/* ============== 滚动优化 ============== */

@media screen and (max-width: 768px) {
    /* 侧边栏可滚动 */
    .sidebar {
        max-height: none;
    }
    
    /* 平滑滚动 */
    html {
        scroll-behavior: smooth;
    }
    
    /* 防止弹性滚动 */
    body {
        overscroll-behavior: none;
    }
}

/* ============== 手柄显示优化 ============== */

@media screen and (max-width: 768px) {
    /* 移动端手柄样式 - opacity 由 JS 控制 */
    .resize-handle {
        transition: opacity 0.2s ease;
        /* iOS Safari 触摸优化 */
        -webkit-transform: translateZ(0);
        transform: translateZ(0);
    }
}

/* ============== iOS Safari 触摸性能优化 ============== */

@media screen and (max-width: 768px) {
    /* 启用 GPU 加速 */
    .image-wrapper,
    .image-preview,
    .group-c,
    .resize-handle,
    .top-content {
        -webkit-transform: translateZ(0);
        transform: translateZ(0);
        -webkit-backface-visibility: hidden;
        backface-visibility: hidden;
    }
    
    /* 拖动时启用硬件加速 */
    .image-wrapper.is-dragging,
    .image-wrapper.mobile-active,
    .group-c.is-dragging,
    .group-c.mobile-active {
        will-change: transform, left, top;
        -webkit-transform: translateZ(0);
        transform: translateZ(0);
    }
    
    /* 禁用拖动时的文字选择 */
    .image-wrapper.is-dragging,
    .group-c.is-dragging {
        -webkit-user-select: none;
        user-select: none;
    }
}

/* ============== 顶层内容移动端适配 ============== */

@media screen and (max-width: 768px) {
    .group-c {
        min-width: 150px;
        min-height: 150px;
        touch-action: auto; /* 默认允许滚动 */
    }
    
    /* 激活状态下禁用滚动 */
    .group-c.mobile-active {
        touch-action: none;
    }
    
    /* 文字大小适配 */
    .main-name {
        font-size: 60px;
        text-shadow: var(--text-shadow-outline-mobile);
    }
    
    .main-info {
        font-size: 45px;
        text-shadow: var(--text-shadow-outline-mobile);
    }
}

@media screen and (max-width: 480px) {
    .main-name {
        font-size: 50px;
    }
    
    .main-info {
        font-size: 38px;
    }
}

/* ============== 加载状态 ============== */

.canvas-loading {
    position: relative;
}

.canvas-loading::after {
    content: '加载中...';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 14px;
    color: var(--color-primary, #e91e63);
    background: rgba(255, 255, 255, 0.9);
    padding: 10px 20px;
    border-radius: 8px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}

/* ============== 折叠面板样式（可选功能） ============== */

@media screen and (max-width: 768px) {
    /* 折叠按钮 */
    .menu-section-toggle {
        display: flex;
        align-items: center;
        justify-content: space-between;
        cursor: pointer;
        user-select: none;
    }
    
    .menu-section-toggle::after {
        content: '▼';
        font-size: 10px;
        transition: transform 0.3s ease;
    }
    
    .menu-section.collapsed .menu-section-toggle::after {
        transform: rotate(-90deg);
    }
    
    .menu-section.collapsed .menu-section-content {
        display: none;
    }
    
    /* 折叠内容 */
    .menu-section-content {
        transition: all 0.3s ease;
    }
}

/* ============== 安全区域适配（刘海屏等） ============== */

@supports (padding-top: env(safe-area-inset-top)) {
    body {
        padding-top: calc(10px + env(safe-area-inset-top));
        padding-bottom: calc(10px + env(safe-area-inset-bottom));
        padding-left: calc(10px + env(safe-area-inset-left));
        padding-right: calc(10px + env(safe-area-inset-right));
    }
    
    @media screen and (max-width: 768px) {
        body {
            padding-top: calc(8px + env(safe-area-inset-top));
            padding-bottom: calc(8px + env(safe-area-inset-bottom));
        }
    }
}

/* ============== 打印样式（下载时使用） ============== */

/* ============== 移动端按钮触摸优化 ============== */

/* hover 效果已在各组件 CSS 中通过 @media (hover: hover) 限制只在桌面端生效 */
/* 这里只需要确保移除焦点样式 */
@media (hover: none) and (pointer: coarse) {
    .action-btn:focus,
    .upload-btn-label:focus,
    .confirm-btn:focus,
    .menu-section:focus {
        outline: none;
    }
}

/* 触摸反馈 - 通过 JavaScript 添加的类 */
.action-btn.touch-active {
    transform: scale(0.95) !important;
    background: linear-gradient(135deg, var(--color-primary-dark, #c2185b), var(--color-primary, #e91e63)) !important;
    box-shadow: 0 2px 8px rgba(233, 30, 99, 0.4) !important;
}

.upload-btn-label.touch-active {
    transform: scale(0.97) !important;
    border-color: var(--color-primary, #e91e63) !important;
    background-color: #ffebee !important;
}

.confirm-btn-cancel.touch-active {
    transform: scale(0.95) !important;
    background: linear-gradient(135deg, #bdbdbd 0%, #9e9e9e 100%) !important;
}

.confirm-btn-ok.touch-active {
    transform: scale(0.95) !important;
    background: linear-gradient(135deg, var(--color-primary-dark, #c2185b), var(--color-primary, #e91e63)) !important;
}

@media print {
    .sidebar,
    .title,
    .action-buttons {
        display: none !important;
    }
    
    .canvas-container {
        padding: 0;
        box-shadow: none;
        background: none;
    }
    
    .canvas {
        transform: none !important;
    }
}
