/* ===============================================
 * 移动端适配 v1.0（开发三典 · 航海日志）
 * 设计目标：
 * 1. iOS 安全区适配（iPhone 刘海/底部 Home Indicator）
 * 2. 触摸目标最小 44×44px（Apple HIG）
 * 3. 模态弹窗移动端全屏化（slide-up 抽屉式）
 * 4. 输入框 iOS 缩放防护（font-size ≥ 16px）
 * 5. 长按/滑动操作优化
 * 6. 横屏适配
 * 7. 防止橡皮筋滚动 & 触摸优化
 * =============================================== */

/* ---------- 1. 基础重置（移动端） ---------- */
* {
    /* 触摸反馈 - 点击高亮禁用，自定义 */
    -webkit-tap-highlight-color: transparent;
}

/* 让 body 在 iOS 弹性滚动中表现更自然 */
html {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
    overscroll-behavior-y: contain;
}

body {
    /* iOS 安全区 */
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
    /* 防止文本被选中 */
    -webkit-user-select: none;
    user-select: none;
}

/* 允许文本选择的元素 */
input, textarea, [contenteditable="true"], .task-title, .task-desc, .empty-desc {
    -webkit-user-select: text;
    user-select: text;
}

/* ---------- 2. 触摸目标最小尺寸 ---------- */
@media (max-width: 768px) {
    /* 所有按钮最小 44×44 触摸区 */
    .btn,
    button:not(.no-tap-target),
    .task-action-btn,
    .mobile-tab-item,
    .nav-item {
        min-height: 44px;
    }

    /* 任务项的删除/重要按钮加大 */
    .task-action-btn {
        width: 36px;
        height: 36px;
    }

    /* 侧边栏 nav-item 加大间距 */
    .nav-item {
        min-height: 48px;
        padding: 14px 16px;
    }

    /* 主按钮加大 */
    .btn-primary {
        min-height: 48px;
        padding: 14px 24px;
        font-size: 16px;
    }
}

/* ---------- 3. iOS 输入框缩放防护 ---------- */
@media (max-width: 768px) {
    input[type="text"],
    input[type="search"],
    input[type="email"],
    input[type="tel"],
    input[type="url"],
    input[type="password"],
    input[type="datetime-local"],
    input[type="date"],
    input[type="time"],
    input[type="number"],
    select,
    textarea {
        font-size: 16px !important; /* iOS 自动缩放触发线为 < 16px */
        border-radius: 10px;
    }

    /* 输入框聚焦时平滑过渡 */
    input:focus, textarea:focus, select:focus {
        transition: border-color 200ms ease, box-shadow 200ms ease;
    }

    /* 防止 input 点击时页面自动缩放 */
    .form-group input,
    .form-group textarea,
    .form-group select {
        transform: translateZ(0);
    }
}

/* ---------- 4. 模态弹窗 - 移动端底部抽屉式 ---------- */
@media (max-width: 768px) {
    .modal {
        align-items: flex-end !important;
    }

    .modal-content {
        width: 100% !important;
        max-width: 100% !important;
        max-height: 90vh !important;
        border-radius: 16px 16px 0 0 !important;
        margin: 0 !important;
        animation: modalSlideUp 360ms cubic-bezier(0.16, 1, 0.3, 1) both;
        /* iOS 底部安全区 */
        padding-bottom: calc(16px + env(safe-area-inset-bottom));
    }

    /* 抽屉拉把（视觉提示） */
    .modal-content::before {
        content: '';
        display: block;
        width: 40px;
        height: 4px;
        background: var(--border-soft, rgba(0, 0, 0, 0.15));
        border-radius: 2px;
        margin: 8px auto 0;
    }

    .modal-content > .modal-header {
        margin-top: 8px;
    }
}

@keyframes modalSlideUp {
    0%   { transform: translateY(100%); }
    100% { transform: translateY(0); }
}

/* 设置弹窗 - 移动端全屏 */
@media (max-width: 768px) {
    #modal-settings .modal-content,
    #modal-add-task .modal-content {
        max-height: 95vh !important;
    }
}

/* ---------- 5. 移动端 TabBar 安全区适配 ---------- */
@media (max-width: 768px) {
    .mobile-tabbar {
        padding-bottom: env(safe-area-inset-bottom);
        background: var(--bg-card);
        backdrop-filter: blur(20px) saturate(180%);
        -webkit-backdrop-filter: blur(20px) saturate(180%);
        background-color: rgba(255, 255, 255, 0.85);
    }

    [data-theme="todolist-dark"] .mobile-tabbar {
        background-color: rgba(31, 36, 31, 0.85);
    }

    /* FAB 位置也加上安全区偏移 */
    .mobile-fab {
        bottom: calc(32px + env(safe-area-inset-bottom));
    }
}

/* ---------- 6. 任务项移动端优化 ---------- */
@media (max-width: 768px) {
    .task-item {
        padding: 14px 12px;
        gap: 10px;
        border-radius: 10px;
    }

    .task-checkbox {
        width: 26px;
        height: 26px;
        flex-shrink: 0;
    }

    .task-title {
        font-size: 15px;
        line-height: 1.4;
    }

    .task-desc {
        font-size: 13px;
        -webkit-line-clamp: 2;
    }

    .task-info {
        font-size: 12px;
        gap: 6px;
    }

    .task-priority,
    .task-repeat {
        font-size: 11px;
        padding: 1px 6px;
    }
}

/* ---------- 7. 工具栏移动端优化 ---------- */
@media (max-width: 768px) {
    .toolbar {
        padding: 10px 12px;
        gap: 8px;
        flex-wrap: nowrap;
    }

    .toolbar h2 {
        font-size: 17px;
        flex-shrink: 0;
    }

    .toolbar-right {
        gap: 6px;
        flex-shrink: 1;
        min-width: 0;
    }

    .search-input {
        min-width: 0;
        flex: 1;
        height: 38px;
        padding: 0 10px;
        font-size: 14px;
    }

    /* 移动端隐藏次要按钮文字 */
    .btn-sort,
    .btn-pomodoro span,
    .btn-text {
        font-size: 14px;
        min-height: 38px;
        padding: 8px 12px;
    }
}

/* ---------- 8. 状态卡移动端 2×2 网格 ---------- */
@media (max-width: 768px) {
    .stats-row {
        display: grid !important;
        grid-template-columns: repeat(2, 1fr);
        gap: 10px;
    }

    .stat-card {
        padding: 14px;
        min-height: 80px;
    }

    .stat-icon {
        width: 32px;
        height: 32px;
    }

    .stat-icon .icon {
        width: 18px;
        height: 18px;
    }

    .stat-number {
        font-size: 24px;
    }

    .stat-label {
        font-size: 12px;
    }
}

/* ---------- 9. 倒计时卡移动端紧凑化 ---------- */
@media (max-width: 768px) {
    .countdown-card {
        padding: 16px 20px;
        border-radius: 12px;
    }

    .countdown-time {
        font-size: 32px;
    }

    .countdown-label {
        font-size: 11px;
    }

    .countdown-task {
        font-size: 12px;
    }
}

/* ---------- 10. 日期头移动端紧凑化 ---------- */
@media (max-width: 768px) {
    .today-day {
        font-size: 56px;
    }

    .today-day-suffix {
        font-size: 18px;
    }

    .today-info {
        font-size: 13px;
    }

    .current-time {
        font-size: 20px;
    }
}

/* ---------- 11. 横屏适配 ---------- */
@media (max-width: 768px) and (orientation: landscape) {
    .countdown-time {
        font-size: 28px;
    }

    .today-day {
        font-size: 48px;
    }

    .stat-number {
        font-size: 20px;
    }
}

/* ---------- 12. 超小屏幕 (< 360px) ---------- */
@media (max-width: 360px) {
    .toolbar h2 {
        font-size: 15px;
    }

    .btn-primary {
        font-size: 14px;
        padding: 10px 16px;
    }

    .task-title {
        font-size: 14px;
    }

    .stat-number {
        font-size: 20px;
    }
}

/* ---------- 13. 列表/滚动优化 ---------- */
@media (max-width: 768px) {
    .task-list {
        /* 滚动时启用硬件加速 */
        -webkit-overflow-scrolling: touch;
        /* 防止列表空白处滑动穿透 */
        overscroll-behavior: contain;
    }

    /* 章节标题加粗 */
    .section-title {
        font-size: 13px;
        margin: 16px 0 8px;
        padding: 0 4px;
    }
}

/* ---------- 14. 设置页移动端分组卡片化 ---------- */
@media (max-width: 768px) {
    .settings-group {
        background: var(--bg-card);
        border-radius: 12px;
        padding: 12px 16px;
        margin-bottom: 12px;
    }

    .settings-group h4 {
        font-size: 12px;
        text-transform: uppercase;
        letter-spacing: 0.06em;
        color: var(--text-tertiary);
        margin: 0 0 8px;
    }

    .setting-item {
        padding: 12px 0;
        min-height: 48px;
        border-bottom: 1px solid var(--border-soft, rgba(0, 0, 0, 0.06));
    }

    .setting-item:last-child {
        border-bottom: none;
    }

    .setting-item select,
    .setting-item input {
        max-width: 160px;
    }
}

/* ---------- 15. 触摸长按效果 ---------- */
@media (hover: none) and (pointer: coarse) {
    /* 触摸设备上，按钮按下时缩小反馈 */
    .btn:active,
    button:active {
        transform: scale(0.97);
        transition: transform 80ms ease-out;
    }

    .task-item:active {
        transform: scale(0.99);
        transition: transform 80ms ease-out;
    }
}

/* ---------- 16. 暗色模式安全区 ---------- */
@supports (padding: max(0px)) {
    .mobile-tabbar {
        padding-bottom: max(env(safe-area-inset-bottom), 0px);
    }
}

/* ---------- 17. 防止页面缩放时字号变化 ---------- */
@media (max-width: 768px) {
    html {
        -webkit-text-size-adjust: 100%;
    }
}
