/* =========================================================
   移动端适配增强 (Mobile-First Ergonomics & PWA Safe Area)
   ========================================================= */
@media (max-width: 640px) {
  .app {
    padding:
      max(8px, env(safe-area-inset-top, 0px))
      max(12px, env(safe-area-inset-right, 0px))
      calc(84px + env(safe-area-inset-bottom, 0px))
      max(12px, env(safe-area-inset-left, 0px));
  }
  html[data-view="medium"] .app { max-width: 100%; }

  /* 未登录鉴权页移动端紧凑适配 */
  .auth-guard {
    padding:
      max(16px, env(safe-area-inset-top, 0px))
      14px
      max(16px, env(safe-area-inset-bottom, 0px));
  }
  .auth-card {
    padding: 22px 18px 20px;
    border-radius: 18px;
    gap: 14px;
  }
  .auth-brand .logo {
    width: 48px;
    height: 48px;
    border-radius: 14px;
    font-size: 24px;
  }
  .auth-brand h2 { font-size: 19px; }
  .auth-tab { min-height: 38px; font-size: 13.5px; }

  /* 顶栏：移动端双行对齐结构（第 1 行：品牌 + 搜索框；第 2 行：单行快捷工具栏） */
  .topbar {
    gap: 8px;
    padding: 6px 0 10px;
  }
  .brand {
    order: 1;
    gap: 7px;
  }
  .brand .logo {
    width: 36px;
    height: 36px;
    border-radius: 11px;
    font-size: 18px;
  }
  .brand h1 { font-size: 16px; letter-spacing: .8px; }
  .brand p { font-size: 9.5px; letter-spacing: 1px; }

  .search-wrap {
    order: 2;
    flex: 1 1 130px;
    min-width: 0;
  }
  .search-wrap input {
    height: 36px;
    padding: 0 32px 0 32px;
    font-size: 16px; /* >=16px 防止 iOS Safari 聚焦自动放大页面 */
    border-radius: 11px;
  }
  .search-wrap::before { left: 11px; font-size: 12px; }
  .search-clear {
    width: 28px;
    height: 28px;
    right: 4px;
    display: grid;
    place-items: center;
  }

  .top-actions {
    order: 3;
    flex: 0 0 100%;
    width: 100%;
    margin-left: 0;
    gap: 3.5px;
    flex-wrap: nowrap;
    overflow-x: auto;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
    justify-content: flex-start;
    padding: 2px 0;
  }
  .top-actions::-webkit-scrollbar { display: none; }
  .top-actions > * { flex: 0 0 auto; }
  .top-actions #btnNew { margin-left: auto; }

  .view-switch { display: none; }
  .user-name-label { display: none; }
  .theme-switch { height: 32px; padding: 2px; border-radius: 9px; gap: 1px; }
  .theme-switch button {
    width: 22px;
    height: 26px;
    font-size: 11.5px;
    border-radius: 7px;
  }
  .top-actions .btn,
  html[data-view="medium"] .top-actions .btn.icon-collapse {
    height: 32px;
    min-width: 29px;
    padding: 0 5.5px;
    font-size: 11.5px;
    border-radius: 9px;
    gap: 2px;
    position: relative;
  }
  .top-actions .sync-btn .sync-dot {
    position: absolute;
    top: 3px;
    right: 3px;
    width: 5.5px;
    height: 5.5px;
  }
  .top-actions .btn.primary { padding: 0 8.5px; font-weight: 700; font-size: 11.5px; }
  .coffee-btn { padding: 0 6px; gap: 2.5px; font-size: 12px; }
  .coffee-btn .c-emoji { font-size: 13px; }
  .pomo-mini { font-size: 9.5px; padding: 1.5px 3.5px; border-radius: 5px; }

  /* 时间轴：扩大移动端触控热区 */
  .timeline {
    padding: 6px 7px;
    gap: 5px;
    border-radius: 14px;
    margin-bottom: 10px;
  }
  .tl-nav {
    width: 34px;
    height: 34px;
    font-size: 15px;
    border-radius: 9px;
  }
  #tlToday {
    height: 34px;
    padding: 0 10px;
    font-size: 12px;
    border-radius: 9px;
    flex: 0 0 auto;
  }
  .tl-strip { gap: 5px; padding: 2px 1px; }
  .tl-day {
    width: 48px;
    padding: 5px 0;
    border-radius: 10px;
  }
  .tl-w { font-size: 9.5px; }
  .tl-d { font-size: 14px; }
  .tl-m { font-size: 9px; }
  .btn.sm { padding: 7px 11px; font-size: 12px; border-radius: 9px; }

  /* 统计栏：单行横向滑动，避免窄屏折行挤压首屏高度 */
  .statsbar {
    flex-wrap: nowrap;
    overflow-x: auto;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
    white-space: nowrap;
    gap: 12px;
    font-size: 12px;
    padding: 0 2px 10px;
  }
  .statsbar::-webkit-scrollbar { display: none; }
  .stat { flex: 0 0 auto; }
  .stat b { font-size: 13px; }

  /* 筛选与排序工具栏 */
  .toolbar { gap: 8px; padding-bottom: 14px; }
  .toolbar .spacer { display: none; }
  .filter-group {
    width: 100%;
    flex-wrap: nowrap;
    overflow-x: auto;
    padding-bottom: 2px;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
  }
  .filter-group::-webkit-scrollbar { display: none; }
  .chip {
    padding: 7px 12px;
    min-height: 34px;
    font-size: 12.5px;
    flex: 0 0 auto;
    display: inline-flex;
    align-items: center;
  }

  .sel {
    padding: 8px 10px;
    min-height: 38px;
    font-size: 16px; /* 防止 iOS 选择器触发页面缩放 */
    border-radius: 10px;
    flex: 1 1 calc(50% - 4px);
    min-width: 0;
  }

  /* 看板与卡片 */
  .board { gap: 24px; }
  .sec-head { padding-left: 10px; gap: 8px; margin-bottom: 11px; }
  .sec-head::before { width: 3.5px; }
  .sec-head h2 { font-size: 14.5px; }
  .sec-count { font-size: 11px; padding: 1px 8px; }
  .sec-desc { font-size: 11.5px; flex-basis: 100%; line-height: 1.5; }
  .card-grid { grid-template-columns: 1fr; gap: 12px; }
  .empty { padding: 22px 16px; font-size: 12.5px; }

  .card { padding: 13px 14px 11px 16px; border-radius: 14px; gap: 8px; }
  .card::before { width: 3.5px; }
  .badge { font-size: 10.5px; padding: 2.5px 8px; }
  .card-title { font-size: 15px; line-height: 1.38; }
  .card-sub { font-size: 12.5px; }
  .card-text { font-size: 13px; -webkit-line-clamp: 2; line-height: 1.6; }

  /* 子任务触控增强（防止误触打开卡片编辑弹窗） */
  .card-subtasks { padding: 8px 10px; gap: 4px; }
  .subtask-item {
    padding: 6px 4px;
    min-height: 34px;
    gap: 9px;
    font-size: 13px;
  }
  .subtask-item input[type="checkbox"] {
    width: 18px;
    height: 18px;
    flex: 0 0 18px;
  }

  .card-dates { gap: 5px; font-size: 11px; }
  .date-item { padding: 3px 7px; font-size: 11px; gap: 4px; }
  .date-item i { font-size: 10px; }
  .rem { font-size: 11px; padding: 3px 8px; }
  .card-prog { padding: 8px 10px; border-radius: 9px; }
  .prog-head { font-size: 10.5px; }
  .prog-text { font-size: 12.5px; }
  .card-note { padding: 8px 10px; font-size: 12.5px; }

  /* 卡片底部操作栏：等分撑满宽度，加大触控面积 */
  .card-actions { gap: 6px; padding-top: 9px; }
  .card-actions .act {
    flex: 1 1 auto;
    justify-content: center;
    min-height: 34px;
    padding: 6px 8px;
    font-size: 12px;
    border-radius: 9px;
  }
  .card:not(.done) .card-actions .act[data-act="edit"] {
    flex: 0 0 36px;
    padding: 6px 0;
  }

  /* 底部抽屉式弹窗 (Bottom Sheet) 与下拉关闭指示条 */
  .overlay {
    padding: 0;
    align-items: flex-end;
  }
  .modal, .modal.wide {
    border-radius: 20px 20px 0 0;
    max-height: 90vh;
    max-height: 90dvh;
    width: 100%;
    scrollbar-width: none;
    transform: translateY(100%);
    transition: transform .28s cubic-bezier(.2, .9, .3, 1.05);
  }
  .modal::-webkit-scrollbar { display: none; }
  .overlay.show .modal { transform: none; }
  .modal-head {
    padding: 20px 18px 12px;
    border-radius: 20px 20px 0 0;
    touch-action: none;
  }
  .modal-head::before {
    content: "";
    position: absolute;
    top: 7px;
    left: 50%;
    transform: translateX(-50%);
    width: 36px;
    height: 4px;
    border-radius: 999px;
    background: var(--line);
  }
  .modal-head h3 { font-size: 15.5px; }
  .icon-btn { width: 34px; height: 34px; font-size: 16px; }
  .modal-body { padding: 14px 18px; gap: 13px; }
  .modal-foot {
    padding: 12px 18px calc(14px + env(safe-area-inset-bottom, 0px));
    gap: 8px;
    flex-wrap: wrap;
  }
  .modal-foot .btn {
    flex: 1 1 auto;
    min-width: 78px;
    min-height: 40px;
    padding: 10px 12px;
    font-size: 13.5px;
  }
  .row > .field { flex: 1 1 100% !important; }
  .field input,
  .field textarea,
  .field select,
  .subtask-edit-row input[type="text"],
  .subtask-add-bar input {
    padding: 10px 12px;
    font-size: 16px; /* >=16px 防止 iOS 自动缩放 */
    border-radius: 10px;
  }
  .subtask-edit-row input[type="checkbox"] {
    width: 18px;
    height: 18px;
    flex: 0 0 18px;
  }
  .subtask-add-bar .btn.tiny {
    min-height: 40px;
    padding: 0 14px;
    font-size: 13px;
  }
  .quick-notes button { padding: 7px 12px; font-size: 12px; min-height: 32px; }
  .prog-list { max-height: 220px; }
  .prog-item { padding: 9px 10px; font-size: 13px; }
  .prog-ops { opacity: 1; }
  .op { width: 32px; height: 32px; font-size: 13.5px; }

  /* 数据管理与管理后台弹窗移动端布局 */
  .data-actions .btn {
    flex: 1 1 calc(50% - 5px);
    min-height: 38px;
    justify-content: center;
  }
  .arch-row { flex-wrap: wrap; gap: 6px; padding: 10px 11px; }
  .arch-meta { flex: 1 1 100%; order: 3; padding-top: 2px; }
  .arch-row .btn.tiny { margin-left: auto; min-height: 32px; padding: 6px 11px; }
  .arch-row .btn.tiny + .btn.tiny { margin-left: 0; }

  .admin-sec { padding: 12px; }
  #admRegModeSel {
    flex: 1 1 100% !important;
    width: 100%;
    font-size: 16px;
  }
  #admSaveModeBtn,
  #admCreateInviteBtn,
  #accSaveBtn {
    width: 100%;
    min-height: 38px;
    justify-content: center;
  }
  .admin-item {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
    padding: 10px 12px;
  }
  .admin-item-info { min-width: 0; width: 100%; }
  .admin-item-ops { width: 100%; justify-content: flex-end; }
  .admin-item-ops .btn.tiny {
    min-height: 32px;
    padding: 6px 11px;
    flex: 1 1 auto;
  }

  /* 专注模式移动端适配 */
  .focus-overlay {
    padding:
      max(16px, env(safe-area-inset-top, 0px))
      14px
      calc(20px + env(safe-area-inset-bottom, 0px));
  }
  .focus-stage { max-width: 100%; gap: 18px; }
  .focus-head { gap: 10px; align-items: flex-start; }
  #focusExitBtn { height: 34px; padding: 0 11px; font-size: 12.5px; }
  .focus-title { font-size: 19px; line-height: 1.35; }
  .focus-sub { font-size: 12.5px; margin-top: 5px; }
  .focus-clock-block { padding: 10px 0 0; }
  .focus-clock { font-size: clamp(40px, 15vw, 72px); }
  .focus-clock-sub { font-size: 12px; gap: 8px; margin-top: 12px; }
  .focus-body { gap: 12px; }
  .focus-input { padding: 10px 12px; border-radius: 13px; }
  .focus-input textarea { font-size: 16px; min-height: 58px; }
  .focus-input-foot { gap: 6px; }
  .focus-input-foot .btn {
    flex: 1 1 auto;
    min-width: 78px;
    min-height: 36px;
    padding: 8px 10px;
    font-size: 12.5px;
  }
  .focus-edit-hint { flex-basis: 100%; margin-right: 0; text-align: center; }
  .focus-log-item { padding: 10px 12px; font-size: 13px; gap: 10px; }
  .focus-log-ops { opacity: 1; }

  /* 休息模式移动端适配 */
  .rest-emoji { font-size: 50px; }
  .rest-title { font-size: 18px; letter-spacing: 2px; }
  .rest-sub { font-size: 12.5px; }
  .rest-clock { font-size: clamp(40px, 13.5vw, 72px); }
  .rest-clock-label { font-size: 11.5px; letter-spacing: 2.5px; margin-top: 10px; }
  .rest-pomo-paused { font-size: 11.5px; padding: 6px 12px; margin-top: 11px; }
  .rest-tips { gap: 7px; }
  .rest-tip { padding: 7px 12px; font-size: 12px; border-radius: 999px; }
  .rest-settings { padding: 12px 14px; gap: 10px; border-radius: 13px; }
  .rest-set-row { gap: 10px 8px; justify-content: space-between; }
  .rest-set-row .spacer { display: none; }
  .rest-set-row .lbl { font-size: 12.5px; }
  .rest-min-input { width: 68px; padding: 7px 8px; font-size: 16px; }
  .rest-set-hint { font-size: 11.5px; }
  .rest-actions .btn { flex: 1 1 auto; min-height: 42px; font-size: 14px; max-width: 260px; justify-content: center; }

  /* 番茄钟提醒弹窗 */
  .remind-overlay { padding: 16px; }
  .remind-card {
    padding: 22px 20px 18px;
    border-radius: 18px;
    transform: translateY(10px) scale(.96);
  }
  .remind-emoji { font-size: 44px; margin-bottom: 10px; }
  .remind-card h3 { font-size: 16px; letter-spacing: .5px; }
  .remind-card p { font-size: 13px; margin-bottom: 16px; }
  .remind-actions { gap: 8px; }
  .remind-actions .btn { flex: 1 1 auto; min-width: 0; min-height: 40px; justify-content: center; padding: 10px 12px; font-size: 13.5px; }

  /* 日历弹出框 */
  .cal-pop {
    width: min(310px, calc(100vw - 20px));
    padding: 12px 12px 10px;
    border-radius: 16px;
  }
  .cal-d { font-size: 13px; border-radius: 10px; min-height: 36px; }
  .cal-nav { width: 34px; height: 34px; font-size: 16px; }

  .switch { width: 42px; height: 24px; }
  .switch .slider::before { height: 18px; width: 18px; }
  .switch input:checked + .slider::before { transform: translateX(18px); }

  /* 移动端右下角悬浮新建按钮 (FAB) */
  .mobile-fab {
    position: fixed;
    right: max(16px, env(safe-area-inset-right, 16px));
    bottom: calc(20px + env(safe-area-inset-bottom, 0px));
    z-index: 350;
    width: 52px;
    height: 52px;
    border-radius: 50%;
    border: none;
    background: var(--accent);
    color: var(--accent-text);
    font-size: 26px;
    font-weight: 400;
    line-height: 1;
    display: grid;
    place-items: center;
    box-shadow: var(--shadow-lg);
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    transition: transform .18s ease, box-shadow .18s ease;
  }
  .mobile-fab:active {
    transform: scale(.92);
  }

  /* Toast 避让底部 FAB 悬浮按钮 */
  .toast-container {
    right: 12px;
    left: 12px;
    bottom: calc(82px + env(safe-area-inset-bottom, 0px));
  }
}

/* 超小屏手机（如 iPhone SE 320px~375px）精细微调 */
@media (max-width: 380px) {
  .brand h1 { font-size: 15px; }
  .brand p { display: none; }
  .top-actions { gap: 4px; }
  .theme-switch button { width: 26px; height: 26px; font-size: 12px; }
  .top-actions .btn { padding: 0 8px; font-size: 12px; }
  .top-actions .btn.primary { padding: 0 10px; }
  .tl-day { width: 44px; }
  .tl-d { font-size: 13px; }
  .card-actions .act { padding: 6px 6px; font-size: 11.5px; }
  .focus-clock { font-size: clamp(36px, 16vw, 60px); }
  .rest-clock { font-size: clamp(36px, 15vw, 60px); }
  .rest-emoji { font-size: 44px; }
}

/* 手机横屏模式紧凑布局 */
@media (max-height: 520px) and (orientation: landscape) {
  .focus-overlay { padding: 12px 18px; }
  .focus-stage { gap: 12px; }
  .focus-clock-block { padding: 4px 0 0; }
  .focus-clock { font-size: clamp(34px, 9vw, 56px); }
  .focus-clock-sub { margin-top: 6px; }
  .rest-emoji { font-size: 36px; }
  .rest-clock { font-size: clamp(34px, 9vw, 56px); }
  .modal, .modal.wide { max-height: 94vh; max-height: 94dvh; }
}

/* 触屏设备移除悬停粘滞态 */
@media (hover: none) {
  .card:hover { transform: none; box-shadow: var(--shadow); }
  .tl-day:hover { transform: none; box-shadow: none; }
  .btn:hover { transform: none; box-shadow: none; }
  .act:hover { background: transparent; color: var(--text-2); border-color: var(--line); }
  .act.focus:hover { background: transparent; color: var(--accent); }
  .chip:hover { transform: none; border-color: var(--line); }
  .prog-item:hover { background: var(--surface-2); }
  .prog-ops { opacity: 1; }
}
