/* ============================================================
   YX_MOBILE_PATCH_V1 + YX_MOBILE_CARDS_V2 + YX_MOBILE_MATRIX_V3
   + YX_MOBILE_STICKY_HEAD_V4 + YX_MOBILE_STICKY_COLS_V5
   易星无涯 · 全站移动端统一表格体验
   ------------------------------------------------------------
   设计原则（Extension · Preserve）：
   1. 仅在 <= 900px 生效；桌面端此文件等于空，网页版零改动。
   2. 不新增任何品牌色 / 字体 / 圆角 / 阴影，全部沿用站点既有 token。
   3. V1 兜底规则包在 :where() 内，特异性 0；只补页面「没有声明」
      的属性，不覆盖各页已有响应式规则。
   4. V2 卡片化规则只作用于显式标记 data-mobile-cards 的数据表，
      局部提升特异性以压过 financial.css 的 th/td 基线，但绝不
      影响未标记表格、后台布局表、JS 动态生成的表。
   5. V3 排盘矩阵只作用于显式标记 data-mobile-matrix 的表：保留完整
      行列、横向可滑动、首列固定；刻意不改 border-collapse，避免
      改变各页原有边框模型（已在 Chromium 实测 collapse 下 sticky 生效）。
   6. 明确排除已自行适配的组件（fw-table 卡片化表格、dlp-spec 规格表）。
   7. V4 长矩阵表头吸顶只作用于显式标记 data-mobile-sticky-head 的
      V3 矩阵：短表、普通表格、桌面端完全不变；表头与首列可同时固定。
  8. V5 超宽矩阵（列数 >= 10）固定前两列：首列 left:0，第二列 sticky 到
     var(--yx-col1-w)（由 mobile-patch.js 测量首列宽写入）；
     分组表头（含 colspan）用 "2-grouped" 显式固定首行表头与表体的第 1、2 列，
     其中首列（代码/名称 等 rowspan=2 续列到第二行）显式 sticky left:0，
     避开花式第二行的首 DOM 子（价/开 等子表头）；桌面端 / 短表 / 单列矩阵完全不变。
   ============================================================ */

/* ---------------- V1 · 全站基础适配 ---------------- */
@media (max-width: 900px) {

  /* --- 1. 基础：文本不自动放大，媒体与表单控件不撑破视口 --- */
  :where(html) { -webkit-text-size-adjust: 100%; }
  :where(img, svg, canvas, video, iframe) { max-width: 100%; }
  :where(input, select, textarea) { max-width: 100%; }
  :where(pre) { max-width: 100%; overflow-x: auto; }

  /* --- 2. 未标记卡片化的多列表格：整表在自身容器内横向滚动 -------
     表头 nowrap 抬高表格最小内容宽度，配合 display:block +
     overflow-x:auto，浏览器会保留完整列宽并给出横向滚动条；
     thead/tbody 仍按表格算法布局，列宽严格对齐（已实测）。 */
  :where(table:not(.fw-table):not(.dlp-spec):not([data-mobile-keep]):not([data-mobile-cards])) {
    display: block;
    overflow-x: auto;
    max-width: 100%;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-x: contain;
  }
  :where(table:not(.fw-table):not(.dlp-spec):not([data-mobile-keep]):not([data-mobile-cards])) th {
    white-space: nowrap;
  }
  :where(table:not(.fw-table):not(.dlp-spec):not([data-mobile-keep]):not([data-mobile-cards])) td {
    overflow-wrap: break-word;
  }
  :where(table:not(.fw-table):not(.dlp-spec):not([data-mobile-keep]):not([data-mobile-cards])) td button {
    white-space: nowrap;
  }

  /* --- 3. 顶部栏目导航：单行横滑，不堆成多行挤压正文 --- */
  :where(header, .topnav) { max-width: 100%; }
  :where(.topnav) {
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  :where(.topnav > a, .topnav > button) {
    flex: 0 0 auto;
    white-space: nowrap;
  }

  /* --- 4. 触控反馈（不改变任何尺寸，避免破坏紧凑布局） --- */
  :where(a, button) { -webkit-tap-highlight-color: rgba(156, 42, 42, .12); }

  /* --- 5. 主站导航：单行横滑 ---------------------------------
     app.css 里 nav.main 声明了 flex-wrap:wrap，会在手机上堆 4 行。
     此条必须使用常规特异性（与 nav.main 相同），并依赖本补丁在
     app.css 之后加载才能生效；min-width:0 让 flex 子项可收缩。 */
  nav.main {
    flex-wrap: nowrap;
    overflow-x: auto;
    min-width: 0;
    max-width: 100%;
    -webkit-overflow-scrolling: touch;
  }
  nav.main a { flex: 0 0 auto; white-space: nowrap; }

  /* --- 6. 最后防线：任何遗漏的宽元素都不允许撑破文档宽度 --- */
  :where(body) { overflow-x: clip; }
}

/* ---------------- V2 · 全站统一表格卡片化 ----------------
   提炼自 financial_watch.html 的 fw-resp-v1，泛化为属性驱动：
   - table[data-mobile-cards]      标记可卡片化的数据表
   - td[data-label]                卡片内的字段名（::before 渲染）
   - td[data-mobile-full]          图表 / 操作 / 长文本独占整行
   - tr[data-mobile-head]          无 thead 时的表头行，窄屏隐藏
   只在 <= 900px 生效，桌面端这些属性不改变任何布局。 */
@media (max-width: 900px) {

  table[data-mobile-cards] {
    display: block;
    width: 100%;
    max-width: 100%;
    overflow: visible;
    border: 0;
    border-collapse: separate;
    border-spacing: 0;
    background: transparent;
  }
  table[data-mobile-cards] thead,
  table[data-mobile-cards] tr[data-mobile-head] { display: none; }
  /* colgroup/col 在 display:block 的表格里会退化成普通元素占位 */
  table[data-mobile-cards] colgroup,
  table[data-mobile-cards] col { display: none; }

  table[data-mobile-cards] tbody {
    display: grid;
    gap: 12px;
    width: 100%;
  }
  table[data-mobile-cards] tbody tr {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0 12px;
    min-width: 0;
    margin: 0;
    padding: 10px 12px;
    background: var(--card, #fff);
    border: 1px solid var(--line, #d9c9a3);
    border-radius: 8px;
    box-shadow: var(--shadow, 0 1px 3px rgba(60, 40, 10, .06));
  }
  table[data-mobile-cards] tbody td {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    justify-content: center;
    box-sizing: border-box;
    min-width: 0;
    min-height: 44px;
    padding: 6px 2px;
    border: 0;
    border-bottom: 1px dashed var(--line, #d9c9a3);
    font-size: 13px;
    line-height: 1.5;
    text-align: left;
    overflow: hidden;
    overflow-wrap: break-word;
    word-break: break-word;
  }
  table[data-mobile-cards] tbody td[data-label]::before {
    content: attr(data-label);
    display: block;
    max-width: 100%;
    margin-bottom: 1px;
    color: var(--ink2, #8a8275);
    font-size: 11px;
    font-weight: 600;
    letter-spacing: .3px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  table[data-mobile-cards] tbody td[data-mobile-full] {
    grid-column: 1 / -1;
    display: block;
    margin-top: 4px;
    padding: 9px 2px 4px;
    border-top: 1px solid var(--line, #d9c9a3);
    border-bottom: 0;
  }
  table[data-mobile-cards] tbody td[data-mobile-full] > * { max-width: 100%; }
  table[data-mobile-cards] tbody td[data-mobile-full] svg { max-width: 100%; height: auto; }
  table[data-mobile-cards] tbody td button { white-space: nowrap; }
  table[data-mobile-cards] tbody td[colspan] { text-align: left; }
}

/* ---------------- V3 · 排盘矩阵（保留行列 + 横滑 + 首列固定）----
   适用：四柱 / 六爻 / 紫微 / 五格 / 分组表头行情表等「行列对应不可拆」
   的矩阵。卡片化会打散行列语义，因此这类表保持 table 结构：
   - table[data-mobile-matrix]  自身即滚动容器，行列与列宽完整保留
   - tr > :first-child          首列（行标题 / 标签列）横滑时固定
   - thead tr:first-child      左上角交叉单元格层级最高
   - thead tr:not(:first-child) 分组表头第二行无真实首列，撤销固定，
     避免与 rowspan 占位的首列重叠（特异性对齐 + 源码顺序胜出）
   - 不设 border-collapse / border-spacing：沿用各页原有边框模型。
   只在 <= 900px 生效；桌面端所有属性均不应用。 */
@media (max-width: 900px) {

  table[data-mobile-matrix] {
    display: block;
    overflow-x: auto;
    max-width: 100%;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-x: contain;
  }
  /* 列头不换行：抬高表格最小内容宽度，横向滚动才有意义，列不会被压成竖排 */
  table[data-mobile-matrix] th { white-space: nowrap; }
  table[data-mobile-matrix] td { overflow-wrap: break-word; }
  table[data-mobile-matrix] td button { white-space: nowrap; }

  /* 首列固定：不透明背景遮挡滑过的列，细线标示固定列边界 */
  table[data-mobile-matrix] tr > :first-child:not([colspan]) {
    position: sticky;
    left: 0;
    z-index: 2;
    background: var(--paper2, #f6efdd);
    box-shadow: 1px 0 0 var(--line, #d9c9a3);
  }
  table[data-mobile-matrix] thead tr:first-child > :first-child { z-index: 3; }
  table[data-mobile-matrix] thead tr:not(:first-child) > :first-child {
    position: static;
    box-shadow: none;
  }
}

/* ---------------- V4 · 长矩阵表头纵向吸顶 ----------------
   仅作用于 V3 矩阵中显式标记 data-mobile-sticky-head 的长表：
   - 表格自身成为纵向滚动容器，短表不设 max-height、不会出现滚动条
   - thead 整组吸顶；表头单元格同时 sticky，兼容更多 WebView 版本
   - 首列表头 / 首列数据列继续横向固定，左上角层级最高
   - 分组表头第二行用 --yx-head-row1-h 下移，避免覆盖第一行
   只在 <= 900px 生效；桌面端 display:table、max-height:none、static。
   ============================================================ */
@media (max-width: 900px) {

  table[data-mobile-matrix][data-mobile-sticky-head] {
    max-height: 68vh;
    max-height: min(68vh, 620px);
    overflow-y: auto;
    overscroll-behavior-y: contain;
    -webkit-overflow-scrolling: touch;
  }

  table[data-mobile-matrix][data-mobile-sticky-head] > thead {
    position: sticky;
    top: 0;
    z-index: 4;
  }

  table[data-mobile-matrix][data-mobile-sticky-head] > thead > tr > th,
  table[data-mobile-matrix][data-mobile-sticky-head] > thead > tr > td {
    position: sticky;
    top: 0;
    z-index: 4;
    background: var(--paper2, #f6efdd);
    box-shadow: inset 0 -1px 0 var(--line, #d9c9a3);
  }

  table[data-mobile-matrix][data-mobile-sticky-head] > thead > tr:not(:first-child) > th,
  table[data-mobile-matrix][data-mobile-sticky-head] > thead > tr:not(:first-child) > td {
    top: var(--yx-head-row1-h, 2.6em);
    z-index: 3;
  }

  /* 左上角：同时纵向吸顶 + 横向固定，保留右侧分隔线 */
  table[data-mobile-matrix][data-mobile-sticky-head] > thead > tr:first-child > :first-child:not([colspan]) {
    left: 0;
    z-index: 6;
    box-shadow: inset 0 -1px 0 var(--line, #d9c9a3),
                1px 0 0 var(--line, #d9c9a3);
  }

  /* 分组表头第二行不是真实首列：吸顶但不抢左固定列 */
  table[data-mobile-matrix][data-mobile-sticky-head] > thead > tr:not(:first-child) > :first-child:not([colspan]) {
    left: auto;
    z-index: 3;
    box-shadow: inset 0 -1px 0 var(--line, #d9c9a3);
  }

  /* Vue 模板常见写法：没有 thead，第一行全是 th。仅对已被注入器
     标记为长矩阵的表启用，避免把「首列 th + 数据 td」误当表头。 */
  table[data-mobile-matrix][data-mobile-sticky-head] > tbody > tr:first-child > th,
  table[data-mobile-matrix][data-mobile-sticky-head] > tbody > tr:first-child > td {
    position: sticky;
    top: 0;
    z-index: 4;
    background: var(--paper2, #f6efdd);
    box-shadow: inset 0 -1px 0 var(--line, #d9c9a3);
  }
  table[data-mobile-matrix][data-mobile-sticky-head] > tbody > tr:first-child > :first-child:not([colspan]) {
    left: 0;
    z-index: 6;
    box-shadow: inset 0 -1px 0 var(--line, #d9c9a3),
                1px 0 0 var(--line, #d9c9a3);
  }
}

/* ---------------- V5 · 超宽矩阵固定前两列 ----------------
   仅作用于显式标记 data-mobile-sticky-cols 的超宽矩阵（列数 >= 10）：
   - 第二列 position:sticky; left:var(--yx-col1-w)，首列宽由 mobile-patch.js
     测量写入（含 CSS 回退值 92px）
   - 非分组表用 "2"：每个 tr 的第 2 列都固定，首列沿用 V3 left:0
   - 分组表头（含 colspan）用 "2-grouped"：固定首行表头与表体的第 1、2 列，
     避开花式第二行的首 DOM 子（价/开 等子表头）；首列（代码/名称 等
     rowspan=2 续列到第二行）显式 sticky left:0，使 2-grouped 模式自包含，
     不依赖 V3 隐式首列规则，避免后续调整 V3 时漏掉该场景
   桌面端 / 短表 / 单列矩阵完全不变；仅 <=900px 生效。 */
@media (max-width: 900px) {

  /* --- 非分组：所有 tr 的第 2 列 --- */
  table[data-mobile-matrix][data-mobile-sticky-cols="2"] tr > :nth-child(2):not([colspan]) {
    position: sticky;
    left: var(--yx-col1-w, 92px);
    z-index: 2;
    background: var(--paper2, #f6efdd);
    box-shadow: 1px 0 0 var(--line, #d9c9a3);
  }
  table[data-mobile-matrix][data-mobile-sticky-cols="2"] thead tr:first-child > :nth-child(2):not([colspan]) {
    z-index: 3;
  }
  table[data-mobile-matrix][data-mobile-sticky-cols="2"][data-mobile-sticky-head] > thead > tr:first-child > :nth-child(2):not([colspan]) {
    z-index: 6;
    box-shadow: inset 0 -1px 0 var(--line, #d9c9a3),
                1px 0 0 var(--line, #d9c9a3);
  }

  /* --- 分组：首行表头与表体的第 1、2 列固定（避开花式第二行的首 DOM 子） --- */
  /* 首列（代码/名称 等 rowspan=2，续列到分组表头第二行）：显式 sticky left:0，
     不依赖 V3 隐式首列规则，使 2-grouped 模式自包含、可独立成立 */
  table[data-mobile-matrix][data-mobile-sticky-cols="2-grouped"] thead tr:first-child > :first-child:not([colspan]) {
    position: sticky;
    left: 0;
    z-index: 3;
    background: var(--paper2, #f6efdd);
    box-shadow: 1px 0 0 var(--line, #d9c9a3);
  }
  table[data-mobile-matrix][data-mobile-sticky-cols="2-grouped"] thead tr:first-child > :nth-child(2):not([colspan]),
  table[data-mobile-matrix][data-mobile-sticky-cols="2-grouped"] tbody tr > :nth-child(2):not([colspan]) {
    position: sticky;
    left: var(--yx-col1-w, 92px);
    z-index: 2;
    background: var(--paper2, #f6efdd);
    box-shadow: 1px 0 0 var(--line, #d9c9a3);
  }
  /* 叠加 V4 长矩阵表头吸顶：首列同时顶 + 左固定，层级与 V4 对齐 */
  table[data-mobile-matrix][data-mobile-sticky-cols="2-grouped"][data-mobile-sticky-head] thead tr:first-child > :first-child:not([colspan]) {
    top: 0;
    left: 0;
    z-index: 6;
    box-shadow: inset 0 -1px 0 var(--line, #d9c9a3),
                1px 0 0 var(--line, #d9c9a3);
  }
  table[data-mobile-matrix][data-mobile-sticky-cols="2-grouped"][data-mobile-sticky-head] > thead > tr:first-child > :nth-child(2):not([colspan]) {
    z-index: 6;
    box-shadow: inset 0 -1px 0 var(--line, #d9c9a3),
                1px 0 0 var(--line, #d9c9a3);
  }
}
