/* ==========================================================================
   视康 · 移动端 / 平板端专属适配
   --------------------------------------------------------------------------
   为什么单独一层：
     桌面端的布局假设「有鼠标、有物理键盘、屏幕宽」，
     在平板与手机上这三条都不成立。这里按「手指操作」重排：
       · 触控目标不小于 44pt（Apple HIG）/ 48dp（Material）
       · 主要操作上移到拇指可达区（屏幕下半部）
       · 视标舞台优先占据可视高度，其余内容滚动
       · 适配刘海屏 / 手势条的安全区
       · 平板不简单放大桌面版，而是走双栏

   通过 <html data-device="mobile|tablet"> 或视口宽度区分：
     · 竖屏手机（< 600px）→ 单列 + 底部固定操作条
     · 横屏手机          → 舞台与作答面板左右分栏
     · 平板（600–1079）  → 双栏，保留侧栏导航
   ========================================================================== */

/* ==========================================================================
   一、触控目标放大（全局，任何窄屏都生效）
   ========================================================================== */
@media (hover: none) and (pointer: coarse) {
  /* 触摸设备没有 hover，所有可点元素都要够大 */
  .btn { min-height: 46px; padding-left: 18px; padding-right: 18px; }
  .btn-sm { min-height: 40px; }
  .btn-lg { min-height: 54px; font-size: 17px; }
  .btn-xs { min-height: 40px; }

  /* 图标按钮：点击热区放大 */
  .icon-btn { width: 46px; height: 46px; }
  .search-trigger { min-height: 46px; }

  /* 分类切换按钮（专项检查页） */
  .group-tabs button { min-height: 42px; padding: 9px 14px; }

  /* 选项卡 / 芯片 */
  .chip { min-height: 42px; padding-left: 15px; padding-right: 15px; }
  .segmented button { min-height: 44px; }
  .tabs button { min-height: 44px; }

  /* 表单控件 */
  .input { min-height: 48px; font-size: 16px; }  /* 16px 可避免 iOS 自动放大 */
  select.input { min-height: 48px; }

  /* 表格里的操作按钮 */
  .table .btn-xs { min-height: 38px; }

  /* 复选框 / 单选框加大命中区 */
  .check { min-height: 48px; }
}

/* ==========================================================================
   二、安全区（刘海屏 / 手势条）
   ========================================================================== */
@media (max-width: 1079px) {
  .safe-b { padding-bottom: env(safe-area-inset-bottom); }
  .safe-t { padding-top: env(safe-area-inset-top); }
}

/* ==========================================================================
   三、竖屏手机：单列 + 底部固定操作条
   ========================================================================== */
@media (max-width: 599px) {

  .container { padding: 0 14px; }

  /* 顶栏更紧凑 */
  .topbar { padding-top: env(safe-area-inset-top); }
  .topbar-inner { padding: 8px 12px; gap: 8px; }
  .topbar-actions { gap: 4px; }
  .search-trigger .st-text { display: none; }
  .search-trigger { width: 42px; padding: 0; justify-content: center; }

  /* 页面标题缩小，节省纵向空间 */
  .page-title { font-size: 21px; }
  .page-head { margin-bottom: 18px; }
  .ph-sub { font-size: 13px; line-height: 1.65; }

  /* 底部导航：留出手势条空间 */
  .bottom-nav { padding-bottom: calc(8px + env(safe-area-inset-bottom)); }
  .bottom-nav a { min-height: 52px; }

  /* ---- 测试执行页：视标舞台 + 底部作答 ---- */
  .test-shell { gap: 10px; }
  /* 作答区外层的 mt-4 在「一屏装下」的布局里是多余间距 */
  .test-shell > .mt-4 { margin-top: 0 !important; }
  /* 「第 1 题 · 已答对 0 题」这行在小屏上会被底部导航压住，且信息在顶部进度条里已有 */
  .test-shell > .hint.text-center { display: none; }

  /* 舞台：视标是主角，但必须有高度下限 ——
     小屏手机（如 664px 高）上一旦允许压到 0，视标就会被挤成一条、
     甚至和提示文字叠在一起。这里用 flex 分配 + 140px 下限。 */
  .stage {
    flex: 1 1 auto;
    min-height: 130px;
    height: auto;
    max-height: min(46vh, 380px);
    padding: 18px 14px 30px;
    border-radius: var(--r-lg);
  }
  .stage-hint { font-size: 12px; bottom: 10px; }

  /* 进度条压缩 */
  .test-bar { gap: 8px; padding: 9px 12px; flex-wrap: wrap; }
  .test-meta { font-size: 12px; }

  /* 方向键：64px 兼顾「拇指好按」与「一屏放得下」 */
  .dpad {
    grid-template-columns: repeat(3, 60px);
    grid-template-rows: repeat(3, 60px);
    gap: 6px;
  }
  .dpad button { min-height: 60px; }
  .dpad button svg { width: 26px; height: 26px; }
  .dpad .dpad-mid { font-size: 12px; }

  /* 字符键盘：加大点击区，横向可滚动 */
  .charpad { gap: 8px; }
  .charpad-btn { min-height: 62px; min-width: 52px; font-size: 20px; }
  .charpad--shape .charpad-btn { min-width: 62px; }

  /* 操作按钮：改为两列网格，拇指可达 */
  .test-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    margin-top: 14px;
  }
  .test-actions .btn { width: 100%; }
  /* 「看不清」占满一行，它是主要按钮之一 */
  .test-actions .btn-cantsee { grid-column: 1 / -1; order: -1; }

  /* 标定卡：手机上把读数改成两列，避免横向滚动 */
  .calib-readout { gap: 8px; }
  .readout { min-width: 0; flex: 1 1 30%; padding: 8px 6px; }
  .readout .r-value { font-size: 15px; }
  .calib-stage { min-height: 170px; padding: 12px; }

  /* 投屏 / 全屏演示的触控面板 */
  .present-pad { padding: 8px 10px calc(10px + env(safe-area-inset-bottom)); gap: 10px; }
  .pp-dpad { grid-template-columns: repeat(3, 62px); grid-template-rows: repeat(3, 62px); gap: 6px; }
  .pp-key { font-size: 24px; }
  .pp-cant { padding: 14px 18px; font-size: 16px; }
  .pp-char { min-width: 46px; height: 46px; font-size: 17px; }

  /* 表格：横向滚动而不是压缩列 */
  .table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .data-table { min-width: 560px; }

  /* 首页宫格：两列 */
  .feature-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
  .opt-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }

  /* 底部固定 CTA 留出安全区 */
  body:has(.sticky-cta) .page { padding-bottom: calc(168px + env(safe-area-inset-bottom)); }

  /* 弹窗贴底，更像原生 */
  .modal { border-radius: 18px 18px 0 0; max-height: 88vh; }
}

/* ==========================================================================
   四、横屏手机：舞台与作答左右分栏
   （横屏时纵向空间极稀缺，必须把作答面板放到侧边）
   ========================================================================== */
@media (max-width: 899px) and (orientation: landscape) and (max-height: 520px) {
  .container { padding: 0 12px; }
  .page-head { margin-bottom: 12px; }
  .page-title { font-size: 19px; }

  .test-shell {
    display: grid;
    grid-template-columns: 1fr auto;
    grid-template-areas: "bar bar" "stage pad";
    align-items: start;
    gap: 12px;
  }
  .test-bar { grid-area: bar; }
  .stage {
    grid-area: stage;
    height: auto;
    min-height: 200px;
    padding: 16px 10px 26px;
  }
  /* 作答面板移到右侧 */
  .test-shell > .mt-4 { grid-area: pad; margin-top: 0 !important; }
  .test-actions { grid-area: pad; margin-top: 10px; display: flex; flex-wrap: wrap; }
  .test-actions .btn { flex: 1 1 40%; }
  .test-actions .btn-cantsee { flex-basis: 100%; }

  .dpad { grid-template-columns: repeat(3, 52px); grid-template-rows: repeat(3, 52px); gap: 5px; }
  .dpad button { min-height: 52px; }
  .dpad button svg { width: 20px; height: 20px; }

  .itemLabel, .hint.text-center.mt-3 { display: none; }
}

/* ==========================================================================
   五、平板：双栏 + 保留侧边导航
   ========================================================================== */
@media (min-width: 600px) and (max-width: 1079px) {

  .container { padding: 0 20px; }

  /* 触控目标整体放大一档 */
  .btn { min-height: 48px; }
  .btn-lg { min-height: 56px; }
  .icon-btn { width: 48px; height: 48px; }
  .input { min-height: 50px; font-size: 16px; }

  /* 侧边导航保留（比底部导航更适合平板） */
  .side-nav { display: block; }

  /* 功能宫格三列 */
  .feature-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 16px; }

  /* 视标类型选择：两列，卡片更大更好点 */
  .optotype-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .opt-card--dense { padding: 14px 15px; }

  /* 测试舞台：平板屏幕大，视标可以更大 */
  .stage { min-height: 440px; padding: 40px 24px; }
  .dpad { grid-template-columns: repeat(3, 72px); grid-template-rows: repeat(3, 72px); gap: 9px; }
  .dpad button { min-height: 72px; }
  .dpad button svg { width: 26px; height: 26px; }
  .charpad-btn { min-height: 62px; min-width: 54px; }

  /* 标定读数三列 */
  .calib-readout { gap: 12px; }
  .readout { min-width: 130px; padding: 11px 14px; }
  .calib-stage { min-height: 300px; }

  /* 准备页：左侧步骤 + 右侧摘要（平板正好放得下）
     ⚠ 1fr 必须写成 minmax(0, 1fr)：默认的 1fr 最小尺寸是 min-content，
       摘要卡里「3.7795 px/mm（约 96 DPI）」这类长值会把整列撑破，
       导致整页出现横向滚动。 */
  .layout-split {
    grid-template-columns: minmax(0, 1fr) minmax(240px, 290px);
    gap: 18px;
  }
  .layout-split > .layout-main,
  .layout-split > .layout-side { min-width: 0; }
  /* 摘要里的长值允许换行，不要撑破侧栏 */
  .layout-side .kv-grid { grid-template-columns: minmax(0, 1fr); }
  .layout-side .kv .v { overflow-wrap: anywhere; }

  /* 表格完整显示 */
  .data-table { min-width: 0; }
}

/* ==========================================================================
   六、触摸设备的按压反馈（替代 hover）
   ========================================================================== */
@media (hover: none) and (pointer: coarse) {
  .btn:active:not(:disabled) { transform: scale(.97); }
  .opt-card:active { transform: scale(.985); }
  .chip:active { transform: scale(.97); }
  .dpad button:active { transform: scale(.94); }
  .icon-btn:active { background: var(--surface-3); }

  /* 触摸时高亮当前元素，让用户知道按到了 */
  .dpad button.hit { background: var(--primary); color: #fff; }
}

/* ==========================================================================
   七、横屏时的视标舞台优先
   （平板横屏是投屏演示与实测的主场景，纵向空间要留给视标）
   ========================================================================== */
@media (orientation: landscape) and (max-height: 600px) and (min-width: 600px) {
  .stage { min-height: 300px; padding: 26px 18px 32px; }
  .present-stage { min-height: 0; }
  .page-head { margin-bottom: 14px; }
  .ph-sub { display: none; }
}

/* ==========================================================================
   八、App 内（原生外壳）微调
   （App 没有浏览器地址栏，顶栏可以更省空间）
   ========================================================================== */
html[data-shell="app"] .topbar { position: static; }
html[data-shell="app"] .topbar-inner { padding-top: calc(8px + env(safe-area-inset-top)); }
