/* ==========================================================================
   tender_web · 响应式适配层（非侵入 v60）
   --------------------------------------------------------------------------
   · 本文件 + static/js/responsive.js 是本次改造的全部内容，
     不修改任何现有业务页的 CSS / JS / HTML。
   · 回滚方式：删掉 index.html 里本文件的 <link> 与 responsive.js 的 <script>。
   · 断点：1440(紧凑) / 1200(次紧凑) / 1024(侧栏抽屉化) / 768(表格转卡片流、
     弹窗全屏) / 480(极窄)
   · 表格策略：≤768px 由 JS 给宽表打 data-cardify=1 转卡片流；
     其余宽度保留横向滚动。
   · 逃生开关：URL 带 ?nocard=1 或 localStorage.wbForceTable='1' 时，
     表格强制回到横向滚动模式（body.wb-force-table）。
   ========================================================================== */

html { -webkit-text-size-adjust: 100%; }

/* ── 由 responsive.js 注入的三个控件：默认隐藏，由媒体查询按需显示 ── */
#wbNavToggle, #wbDrawerClose, #wbCardToggle { display: none; }

/* flex 主区允许收缩，防止宽内容把整个布局撑出横向滚动 */
.main-content { min-width: 0; }

/* 状态徽章禁止字内换行（窄列下「新标讯」曾竖排） */
.status-badge { white-space: nowrap; }

/* 抽屉打开时隐藏右下角切换按钮，避免压住抽屉 */
body.wb-drawer-open #wbCardToggle { display: none !important; }

/* iOS 惯性滚动 */
.wb-xscroll, .table-wrap, .page > .content { -webkit-overflow-scrolling: touch; }

/* ══════════════════════════════════════════════════════════════════════════
   ≤1440：笔记本紧凑模式（1366×768 之类小屏本的主要痛点是内边距太占宽）
   ══════════════════════════════════════════════════════════════════════════ */
@media (max-width: 1440px) {
  html:root { --sidebar-w: 184px; }

  .sidebar-logo { padding: 16px 12px; }
  .sidebar-logo h2 { font-size: 15px; }
  .nav-item { padding: 10px 14px; font-size: 13px; gap: 8px; }
  .nav-group-header { padding: 9px 14px; font-size: 12px; gap: 8px; }
  .nav-group-items .nav-item { padding-left: 30px; }

  .topbar { padding: 10px 16px; }
  .page > .content { padding: 16px; }
  .page-toolbar { padding: 10px 16px; }
  .table-wrap { padding: 0 16px 16px; }
  .filters, .admin-filters { padding: 10px 16px; gap: 8px; }
  .quick-tags { padding: 0 16px 6px; }
  .stats { padding: 12px 16px; gap: 10px; }
  .stat-card { padding: 12px 14px; }
  .stat-card .value { font-size: 21px; }
  .pagination { padding: 12px 16px; }
  .sys-monitor { padding: 0 16px 16px; gap: 10px; }
  .filters input[type="search"] { min-width: 180px; }
  .title-cell { max-width: 280px; }
  thead th { padding: 9px 8px; }
  tbody td { padding: 7px 8px; }
}

/* ══════════════════════════════════════════════════════════════════════════
   ≤1200：进一步压缩 + 宽表格容器允许横向滚动（不改动 DOM）
   ══════════════════════════════════════════════════════════════════════════ */
@media (max-width: 1200px) {
  html:root { --sidebar-w: 176px; }

  .page > .content { padding: 12px; overflow-x: auto; }
  .page-toolbar { padding: 8px 12px; }
  .table-wrap { padding: 0 12px 12px; }
  .filters, .admin-filters { padding: 8px 12px; }
  .quick-tags { padding: 0 12px 6px; }
  .stats { padding: 10px 12px; }
  .pagination { padding: 10px 12px; }
  .sys-monitor { padding: 0 12px 12px; }
  .title-cell { max-width: 240px; }
  .filters input[type="search"] { min-width: 150px; }
  .dash-panel-body { max-height: 240px; }

  /* 双列面板改成单列，避免两列都挤成窄条 */
  #dashTwoCol { grid-template-columns: 1fr !important; }

  /* 表格整体允许横向滚动兜底：表格比容器宽时不会撑破布局 */
  table { min-width: 0; }
}

/* ══════════════════════════════════════════════════════════════════════════
   ≤1024：侧边栏抽屉化（平板竖屏 / 小笔记本极窄窗口）
   ══════════════════════════════════════════════════════════════════════════ */
@media (max-width: 1024px) {
  /* ── 抽屉本体 ── */
  .sidebar {
    width: 264px !important;
    max-width: 86vw;
    transform: translateX(-100%);
    transition: transform .26s cubic-bezier(.4, 0, .2, 1);
    box-shadow: 6px 0 28px rgba(0, 0, 0, .35);
    will-change: transform;
    padding-bottom: env(safe-area-inset-bottom, 0);
  }
  body.wb-drawer-open .sidebar { transform: translateX(0); }

  /* 抽屉打开时锁定背景滚动 */
  body.wb-drawer-open { overflow: hidden; }

  /* ── 遮罩 ── */
  .wb-drawer-backdrop {
    position: fixed; inset: 0;
    background: rgba(0, 0, 0, .45);
    z-index: 148;
    display: none;
    animation: wbFadeIn .18s ease;
  }
  body.wb-drawer-open .wb-drawer-backdrop { display: block; }
  @keyframes wbFadeIn { from { opacity: 0; } to { opacity: 1; } }

  /* ── 主区与选项卡栏回到整宽 ── */
  .main-content { margin-left: 0 !important; }
  .wb-tab-bar { left: 0 !important; }

  /* ── 汉堡按钮（由 responsive.js 注入到 .wb-tab-bar 首位） ── */
  #wbNavToggle {
    display: inline-flex;
    align-items: center; justify-content: center;
    width: 42px; flex-shrink: 0;
    background: none; border: none;
    border-right: 1px solid var(--border-light);
    color: var(--muted);
    font-size: 17px; line-height: 1;
    cursor: pointer;
    transition: all .15s;
  }
  #wbNavToggle:hover, #wbNavToggle:active { color: var(--accent); background: var(--bg-hover); }

  /* ── 抽屉顶部关闭按钮（JS 注入到 .sidebar 内） ── */
  #wbDrawerClose {
    position: absolute; top: 10px; right: 10px;
    width: 32px; height: 32px;
    display: flex;
    align-items: center; justify-content: center;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 50%;
    color: var(--muted); font-size: 18px; line-height: 1;
    cursor: pointer;
  }
  #wbDrawerClose:hover { color: var(--red); border-color: var(--red); }

  /* ── 触控友好输入框：≥16px 防止 iOS 聚焦自动放大 ── */
  input:not([type="checkbox"]):not([type="radio"]):not([type="file"]):not([type="range"])
        :not([type="color"]):not([type="date"]):not([type="time"]):not([type="month"])
        :not([type="week"]):not([type="datetime-local"]):not(.sop-note):not(.todo-done-note),
  select, textarea {
    font-size: 16px !important;
  }
  .sop-note, .todo-done-note { font-size: 16px !important; }

  /* 表单控件最小触控高度 */
  .filters input, .filters select,
  .admin-filters input, .admin-filters select,
  .page-toolbar input, .page-toolbar select,
  .remark-area, .inline-edit-input {
    min-height: 38px;
  }
  select, input[type="search"], input[type="text"], input[type="number"], textarea {
    max-width: 100%;
  }

  /* 顶部栏压缩并允许换行（topbar 是不换行 flex，min-content 曾把页面撑到 900+px） */
  .topbar { padding: 9px 12px !important; flex-wrap: wrap; }
  .topbar-actions { flex-wrap: wrap; }
  .topbar h1 { font-size: 15px; }
  .topbar-actions { gap: 6px; }

  /* 标签组在多列窄表里放开最大宽度 */
  .tag-group, .tag-group.has-more { max-width: none !important; }
}

/* ══════════════════════════════════════════════════════════════════════════
   ≤768：手机模式 —— 表格转卡片流 + 弹窗全屏 + 表单单列
   ══════════════════════════════════════════════════════════════════════════ */
@media (max-width: 768px) {

  /* ───────── 1. 布局 ───────── */
  .sidebar { width: 82vw !important; }
  .page > .content { padding: 12px !important; }
  .page-toolbar { padding: 10px 12px !important; gap: 8px; }
  .table-wrap { padding: 0 12px 14px !important; }
  .quick-tags { padding: 0 12px 6px !important; }
  .pagination { padding: 12px !important; flex-wrap: wrap; gap: 6px; }
  .pagination button { padding: 8px 11px !important; }

  /* ───────── 2. 弹窗全屏（覆盖业务行内 max-width） ───────── */
  .modal-overlay { padding: 0 !important; overflow-x: hidden; }
  .modal {
    width: 100% !important;
    max-width: 100% !important;
    border-radius: 0 !important;
    border-left: none; border-right: none; border-top: none;
  }
  .modal-header { padding: 13px 14px !important; border-radius: 0 !important; gap: 10px; }
  .modal-header h2 { font-size: 15px; }
  .modal-body { padding: 14px !important; }
  .modal-section { margin-bottom: 16px; }
  .modal-actions { flex-wrap: wrap; gap: 8px; }
  .modal-actions > .btn, .modal-actions > button { flex: 1 1 auto; justify-content: center; }
  .info-grid { grid-template-columns: 92px 1fr !important; gap: 6px 10px; }

  /* ───────── 3. 表格 → 卡片流 ───────── */
  body:not(.wb-force-table) table[data-cardify="1"] { display: block; width: 100%; }
  body:not(.wb-force-table) table[data-cardify="1"] > thead { display: none !important; }
  body:not(.wb-force-table) table[data-cardify="1"] > tbody { display: block; width: 100%; }
  body:not(.wb-force-table) table[data-cardify="1"] > tbody > tr {
    display: block;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 10px;
    margin: 0 0 10px;
    padding: 4px 12px;
    overflow: hidden;
    transition: border-color .2s;
  }
  body:not(.wb-force-table) table[data-cardify="1"] > tbody > tr:hover {
    border-color: rgba(var(--accent-rgb), .45);
    background: var(--card);
  }
  body:not(.wb-force-table) table[data-cardify="1"] > tbody > tr.row-viewed { opacity: 1; }
  body:not(.wb-force-table) table[data-cardify="1"] > tbody > tr > td {
    display: flex !important;
    align-items: flex-start;
    justify-content: space-between;
    gap: 10px;
    padding: 7px 0 !important;
    border-bottom: 1px solid var(--border-light);
    max-width: none !important;
    min-width: 0 !important;
    width: auto !important;
    white-space: normal !important;
    text-align: left;
    word-break: break-word;
  }
  body:not(.wb-force-table) table[data-cardify="1"] > tbody > tr > td:last-child { border-bottom: none; }
  body:not(.wb-force-table) table[data-cardify="1"] > tbody > tr > td[colspan] {
    display: block !important;
    text-align: center;
    border-bottom: none;
    color: var(--muted);
    padding: 14px 0 !important;
  }
  /* 列名标签 */
  body:not(.wb-force-table) table[data-cardify="1"] > tbody > tr > td::before {
    content: attr(data-label);
    flex: 0 0 auto;
    max-width: 42%;
    color: var(--muted);
    font-size: 11px;
    font-weight: 600;
    letter-spacing: .3px;
    padding-top: 2px;
  }
  body:not(.wb-force-table) table[data-cardify="1"] > tbody > tr > td[data-label=""]::before { content: none; }
  body:not(.wb-force-table) table[data-cardify="1"] > tbody > tr > td[colspan]::before { content: none; }
  body:not(.wb-force-table) table[data-cardify="1"] .title-cell { max-width: none !important; }
  body:not(.wb-force-table) table[data-cardify="1"] .tag-group { max-width: none !important; }

  /* ───────── 4. 行内写死的多列网格 → 单列（覆盖行内 style） ───────── */
  [style*="grid-template-columns: 1fr 1fr"],
  [style*="grid-template-columns:1fr 1fr"],
  [style*="grid-template-columns: 1fr 1fr 1fr"],
  [style*="grid-template-columns:1fr 1fr 1fr"],
  [style*="grid-template-columns: 1fr 1fr 1fr 1fr"],
  [style*="grid-template-columns:1fr 1fr 1fr 1fr"],
  [style*="grid-template-columns: repeat(2, 1fr)"],
  [style*="grid-template-columns:repeat(2,1fr)"],
  [style*="grid-template-columns: repeat(3, 1fr)"],
  [style*="grid-template-columns:repeat(3,1fr)"],
  [style*="grid-template-columns: repeat(4, 1fr)"],
  [style*="grid-template-columns:repeat(4,1fr)"],
  [style*="grid-template-columns: repeat(5, 1fr)"],
  [style*="grid-template-columns:repeat(5,1fr)"],
  [style*="grid-template-columns: repeat(6, 1fr)"],
  [style*="grid-template-columns:repeat(6,1fr)"] {
    grid-template-columns: 1fr !important;
  }

  /* 仪表盘两块带行内 padding 的网格，顺手收窄内边距 */
  #timelineStats { padding: 8px 12px 0 12px !important; }
  #dashTwoCol { padding: 0 12px 0 12px !important; }

  /* ───────── 5. 筛选/工具栏 ───────── */
  .filters, .admin-filters { gap: 8px !important; padding: 10px 12px !important; }
  .filters input[type="search"], .filters input[type="text"],
  .admin-filters input[type="search"], .admin-filters input[type="text"] {
    flex: 1 1 100% !important;
    width: 100% !important;
    min-width: 0 !important;
  }
  .filters select, .admin-filters select,
  .filters input:not([type="search"]):not([type="text"]),
  .admin-filters input:not([type="search"]):not([type="text"]) {
    flex: 1 1 calc(50% - 4px) !important;
    min-width: 0 !important;
    width: auto !important;
  }
  .page-toolbar > * { flex: 1 1 auto; min-width: 0; }
  .page-toolbar > input[type="search"], .page-toolbar > input[type="text"] { flex: 1 1 100% !important; }

  /* ───────── 6. 统计卡 / 看板卡 ───────── */
  .stats {
    padding: 12px 12px !important;
    gap: 10px !important;
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)) !important;
  }
  .stat-card { padding: 12px 14px !important; }
  .stat-card .value { font-size: 20px !important; }
  .sys-monitor { grid-template-columns: 1fr !important; padding: 0 12px 12px !important; }
  .todo-stats { flex-wrap: wrap; gap: 10px; margin-bottom: 14px; }
  .todo-stat-card { flex: 1 1 28%; padding: 12px 8px !important; }
  .todo-stat-num { font-size: 22px !important; }
  .todo-done-note { width: 100% !important; }
  .todo-item { padding: 12px; gap: 10px; }
  .memo-grid { grid-template-columns: 1fr !important; }

  /* ───────── 7. 按钮触控区 ───────── */
  .btn { padding: 10px 15px !important; font-size: 14px; }
  .btn-sm, .btn-xs { padding: 7px 11px !important; font-size: 12px; }
  .qs-tag { padding: 5px 11px; font-size: 12px; }

  /* ───────── 8. 表格模式切换小按钮（JS 注入到 body） ───────── */
  #wbCardToggle {
    display: inline-flex;
    position: fixed;
    right: 12px; bottom: calc(14px + env(safe-area-inset-bottom, 0px));
    z-index: 160;
    align-items: center; gap: 4px;
    padding: 8px 12px;
    font-size: 12px; line-height: 1;
    color: var(--text);
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 20px;
    box-shadow: 0 4px 14px rgba(0, 0, 0, .22);
    cursor: pointer;
    opacity: .92;
  }
  #wbCardToggle:active { transform: scale(.96); }
}

/* ══════════════════════════════════════════════════════════════════════════
   ≤480：极小屏（iPhone SE 等）
   ══════════════════════════════════════════════════════════════════════════ */
@media (max-width: 480px) {
  .page > .content { padding: 10px !important; }
  .table-wrap { padding: 0 10px 12px !important; }
  .page-toolbar { padding: 8px 10px !important; }
  .filters, .admin-filters { padding: 8px 10px !important; }
  .stats { grid-template-columns: repeat(2, 1fr) !important; gap: 8px !important; }
  .stat-card { padding: 10px 12px !important; }
  .stat-card .value { font-size: 18px !important; }
  .stat-card .label { font-size: 10px; }
  .info-grid { grid-template-columns: 1fr !important; }
  .info-grid .info-label { white-space: normal; margin-top: 4px; }
  .topbar { padding: 8px 10px !important; }
  .topbar h1 { font-size: 14px; }
  .wb-tab-item { padding: 8px 10px !important; font-size: 12px; }
  .todo-stat-card { flex: 1 1 100%; }
  .modal-body { padding: 12px !important; }
  body:not(.wb-force-table) table[data-cardify="1"] > tbody > tr > td { font-size: 12.5px; }
  body:not(.wb-force-table) table[data-cardify="1"] > tbody > tr > td::before { max-width: 46%; }
  #wbCardToggle { right: 10px; padding: 7px 10px; font-size: 11px; }
}

/* ══════════════════════════════════════════════════════════════════════════
   横屏矮屏（手机横屏 / 分屏）：抽屉不要占满屏
   ══════════════════════════════════════════════════════════════════════════ */
@media (max-width: 900px) and (orientation: landscape) {
  .sidebar { width: 300px !important; }
  body:not(.wb-force-table) table[data-cardify="1"] > tbody > tr { padding: 2px 10px; }
}
