/* =============================================================
   芯耕AI作物视觉分析系统 — 平板 / 手机 自适应适配
   --------------------------------------------------------------
   使用方式：必须在 style.css（以及 farm.css）之后引入。
   设计原则：
   1. 只做「断点内覆盖」，不改动桌面端默认行为
   2. 不在页面容器（.main-layout）上用 display !important（会卡死 switchPage）
   3. 触屏可用：44px 触控目标、输入框 16px 防 iOS 自动缩放、hover-only UI 补触屏显示
   断点：
     ≤1279px  导航折叠为抽屉（汉堡）
     ≤1024px  平板：三栏侧栏转为抽屉、两列网格、触控尺寸
     ≤820px   手机：单列、表格横滚、表单通栏
     ≤600px   小屏手机进一步压缩
   ============================================================= */

/* ---------- 基础：禁止横向溢出 ---------- */
html, body {
  overflow-x: hidden;
  max-width: 100%;
}

/* 以下元素由 js/responsive.js 动态创建，桌面端一律隐藏。
   注意：必须写在各断点媒体查询「之前」，否则同名规则会反过来把它永久隐藏 */
.nav-toggle { display: none; }    /* ≤1279px 显示为汉堡按钮 */
.panel-fab { display: none; }     /* ≤1024px 显示为侧栏浮动开关 */
.panel-mask { display: none; }    /* 侧栏抽屉遮罩 */

/* iOS 触摸高亮与 300ms 点击延迟 */
button, .nav-item, .nav-dd-item, .tool-btn, .btn-primary, .btn-secondary,
.tab-btn, .segment-btn, .vtab-btn, .view-toggle-btn, .multi-thumb {
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* 触屏设备：把所有「hover 才显示」的交互元素常显（否则平板上永远点不到） */
@media (hover: none) {
  .multi-thumb__del { display: block; }
  .nav-item.has-dropdown:hover .nav-dropdown { display: none; }
  .nav-item.has-dropdown.open .nav-dropdown { display: block; }
}

/* =============================================================
   ≤1279px：顶部导航折叠为抽屉（iPad Pro 11" 横屏 1194 也会命中）
   ============================================================= */
@media (max-width: 1279px) {
  .top-nav {
    height: 56px;
    padding: 0 12px;
    gap: 10px;
    justify-content: space-between;
  }
  .nav-logo .nav-logo-img { height: 30px; }
  .nav-menu { display: none; }              /* 主菜单移入抽屉 */
  .nav-right { gap: 6px; margin-left: auto; }
  .nav-time { display: none; }
  #currentUserName { display: none; }
  #siteLangSelect { margin-right: 4px; font-size: 13px; padding: 6px 8px; }
  .btn-logout { margin-left: 4px !important; padding: 6px 12px !important; font-size: 13px; }

  /* 汉堡按钮 */
  .nav-toggle {
    display: flex !important;
    flex-direction: column;
    justify-content: center;
    gap: 4px;
    width: 42px;
    height: 42px;
    padding: 0;
    background: transparent;
    border: 1px solid var(--border, rgba(212,168,67,.3));
    border-radius: 8px;
    cursor: pointer;
    flex-shrink: 0;
  }
  .nav-toggle span {
    display: block;
    width: 20px;
    height: 2px;
    margin: 0 auto;
    background: var(--gold-light, #d8c08a);
    border-radius: 2px;
  }
}

/* =============================================================
   ≤1024px：平板（含竖屏）—— 侧栏转抽屉、两列网格、放大触控
   ============================================================= */
@media (max-width: 1024px) {
  /* 页面容器高度改用 dvh（移动端浏览器工具栏会吃掉 vh） */
  .main-layout {
    height: calc(100vh - 56px - 48px);
  }
  @supports (height: 100dvh) {
    .main-layout { height: calc(100dvh - 56px - 48px); }
  }

  /* ---- AI 工作台：左右侧栏 → 抽屉（脱离文档流，中间区自动全宽） ---- */
  #page-analysis > .left-panel,
  #page-analysis > .right-panel {
    position: fixed;
    top: 56px;
    bottom: 48px;
    z-index: 1200;
    width: 320px;
    max-width: 86vw;
    padding: 16px 14px 30px;
    transform: translateX(-100%);
    transition: transform .25s ease;
    box-shadow: 0 0 40px rgba(0, 0, 0, .55);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
  #page-analysis > .right-panel {
    transform: translateX(100%);
    right: 0;
  }
  #page-analysis > .left-panel { left: 0; }
  #page-analysis > .left-panel.is-open { transform: translateX(0); }
  #page-analysis > .right-panel.is-open { transform: translateX(0); }
  #page-analysis > .center-panel { width: 100%; }

  /* 抽屉打开时的遮罩 */
  .panel-mask {
    position: fixed;
    inset: 56px 0 48px 0;
    background: rgba(0, 0, 0, .45);
    z-index: 1150;
  }
  /* 侧栏浮动开关（由 js/responsive.js 注入到 #page-analysis 内） */
  .panel-fab {
    display: flex;
    position: fixed;
    top: 66px;
    z-index: 1250;
    width: 46px;
    height: 46px;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    color: var(--gold-light, #d8c08a);
    background: rgba(10, 32, 22, .92);
    border: 1px solid rgba(212, 168, 67, .35);
    border-radius: 10px;
    cursor: pointer;
    box-shadow: 0 4px 14px rgba(0, 0, 0, .4);
  }
  .panel-fab--left { left: 10px; }
  .panel-fab--right { right: 10px; }

  /* ---- 底部时间线：允许横向滚动 ---- */
  .bottom-bar {
    height: 48px;
    padding: 0 12px;
    gap: 10px;
  }
  .bottom-title { display: none; }
  .timeline {
    flex: 1 1 auto;
    min-width: 0;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  .history-quick-link { font-size: 12px; padding: 5px 10px; }

  /* ---- 通用触控尺寸 ---- */
  .tool-btn,
  .btn-primary,
  .btn-secondary,
  .tab-btn,
  .segment-btn,
  .view-toggle-btn,
  .task-action-btn,
  .btn-logout {
    min-height: 42px;
    padding: 9px 16px;
    font-size: 14px;
  }
  .form-input,
  .form-select,
  .form-textarea {
    min-height: 42px;
    font-size: 16px;   /* ≥16px 才能避免 iOS 聚焦时自动放大页面 */
    padding: 9px 12px;
  }
  .form-label { font-size: 14px; }
  .panel-title { font-size: 15px; }

  /* ---- 工作台左侧：垂直卡片列表在窄栏里收窄内边距 ---- */
  .vtab-btn { padding: 10px 12px; }
  .vtab-desc { font-size: 11px; }

  /* ---- 驾驶舱：三栏 → 上下堆叠（地图优先） ---- */
  .cockpit-body {
    grid-template-columns: 1fr;
    grid-template-rows: auto minmax(320px, 1fr) auto;
    padding: 0 12px 12px;
    gap: 10px;
    overflow-y: auto;
  }
  .cockpit-left, .cockpit-right { max-height: none; }
  .cockpit-kpis { grid-template-columns: repeat(3, 1fr); }

  /* ---- 传感器 / 土壤等卡片网格 ---- */
  .dashboard-soil-grid,
  .dashboard-plot-grid { gap: 12px; }

  /* ---- 表格：容器横向滚动，表头保持可读 ---- */
  .table-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* ---- 农情记录页高度同步 ---- */
  #page-growth > .growth-page {
    height: calc(100vh - 104px);
    max-height: calc(100vh - 104px);
    padding: 14px 14px 60px;
  }
  @supports (height: 100dvh) {
    #page-growth > .growth-page {
      height: calc(100dvh - 104px);
      max-height: calc(100dvh - 104px);
    }
  }

  /* ---- 弹窗 ---- */
  .modal-content {
    width: 92vw;
    max-height: 84vh;
  }
  .modal-header, .modal-footer, .modal-body { padding-left: 16px; padding-right: 16px; }

  /* ---- 环形装饰缩小，避免在小屏占据主视觉 ---- */
  .ring-1 { width: 180px; height: 180px; }
  .ring-2 { width: 230px; height: 230px; }
  .ring-3 { width: 280px; height: 280px; }

  /* ---- 3D 环容器在小屏弱化 ---- */
  .rings-container { opacity: .5; }
}

/* =============================================================
   ≤820px：手机 / 小平板竖屏 —— 单列 + 通栏表单
   ============================================================= */
@media (max-width: 820px) {
  .top-nav { height: 52px; }
  /* 先给 vh 兜底，再在支持 dvh 的浏览器上覆盖（顺序不能反，否则后写的 vh 会赢） */
  .main-layout { height: calc(100vh - 52px - 44px); }
  @supports (height: 100dvh) {
    .main-layout { height: calc(100dvh - 52px - 44px); }
  }

  /* 单列化 */
  .cockpit-kpis,
  .dashboard-plot-grid,
  .dashboard-soil-grid { grid-template-columns: 1fr; }
  .sensor-grid { grid-template-columns: 1fr 1fr; }

  .stat-mini-card { padding: 12px 14px; }
  .dashboard-stats { gap: 10px; }

  /* 任务卡 / 报告卡 竖排 */
  .task-card-v2 { flex-direction: column; gap: 10px; }
  .task-card-v2 .tc-icon { width: 44px; height: 44px; }
  .report-card { padding: 14px; }
  .farm-stats { flex-wrap: wrap; }

  /* 表单：全部通栏 */
  .form-group { margin-bottom: 14px; }
  .history-filters { gap: 8px; }
  .history-filters .form-input,
  .history-filters .form-select {
    width: 100% !important;
    flex: 1 1 100%;
  }
  .history-filters .tool-btn { flex: 0 0 auto; }

  /* 表格：给最小宽度，靠容器横滚 */
  .history-table { min-width: 960px; }
  .alert-row td, .task-row td, .history-table td { white-space: nowrap; }

  /* 底部时间线进一步压缩：隐藏历史快捷入口文字 */
  .history-quick-link .history-label { display: none; }
  .history-quick-link { padding: 5px 9px; }
  .bottom-bar { height: 44px; }

  /* 内容页内边距收紧 */
  .growth-page, .history-page, .settings-page, .assistant-page,
  .tasks-page, .alerts-page, .reports-page, .models-page, .datatable-page {
    padding-left: 12px;
    padding-right: 12px;
  }

  /* 弹窗铺满近全屏 */
  .modal-content { width: 94vw; max-height: 80vh; }

  /* 图像舞台：小屏留更多展示空间 */
  .image-container { padding: 0; }
  .multi-thumb { width: 56px; height: 42px; }
  #multiImageBar { bottom: 8px; padding: 6px 8px; }
  #multiImageBar .multi-bar__hint { display: none; }

  /* 分析状态步骤文字缩短 */
  .status-step { font-size: 12px; }
}

/* =============================================================
   ≤600px：小屏手机
   ============================================================= */
@media (max-width: 600px) {
  .top-nav { padding: 0 8px; gap: 6px; }
  .nav-logo .nav-logo-img { height: 26px; }
  .btn-logout { padding: 6px 10px !important; font-size: 12px; }
  #siteLangSelect { font-size: 12px; padding: 5px 6px; }

  .sensor-grid { grid-template-columns: 1fr; }
  .cockpit-kpis { grid-template-columns: 1fr 1fr; }
  .kpi-card { padding: 10px 12px; }

  .panel-title { font-size: 14px; }
  .form-label { font-size: 13px; }

  /* 工作台抽屉按钮下移，避免与顶栏重叠 */
  .panel-fab { top: 60px; width: 42px; height: 42px; }

  .timeline-stage { font-size: 11px; }
  .tool-btn { padding: 8px 12px; font-size: 13px; }
  .analyze-btn { width: 100%; }
}

/* =============================================================
   横屏矮屏（如 iPad 横屏 + 地址栏）：压缩纵向占用
   ============================================================= */
@media (max-height: 560px) and (orientation: landscape) {
  .main-layout { height: calc(100vh - 52px - 40px); }
  .bottom-bar { height: 40px; }
  .top-nav { height: 52px; }
  .rings-container { display: none; }
}

/* =============================================================
   导航抽屉（平板/手机共用，由 js/responsive.js 构建 DOM）
   ============================================================= */
#navDrawer {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: 280px;
  max-width: 80vw;
  z-index: 2100;
  background: linear-gradient(180deg, rgba(6, 32, 24, .99), rgba(4, 20, 14, .99));
  border-right: 1px solid var(--border, rgba(212,168,67,.25));
  transform: translateX(-100%);
  transition: transform .25s ease;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 14px 0 30px;
}
#navDrawer.is-open { transform: translateX(0); }
#navDrawerMask {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .5);
  z-index: 2050;
}
.nd-brand {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 16px 14px;
  border-bottom: 1px solid rgba(212, 168, 67, .18);
  margin-bottom: 8px;
}
.nd-brand img { height: 30px; width: auto; }
.nd-close {
  background: transparent;
  border: none;
  color: var(--text-sub, #9fb8a8);
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  padding: 4px 8px;
}
.nd-group { margin: 2px 0; }
.nd-group-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 13px 16px;
  font-size: 15px;
  color: var(--text-main, #dff0e4);
  cursor: pointer;
  border-radius: 8px;
}
.nd-group-head .nd-arrow {
  font-size: 11px;
  color: var(--text-sub, #9fb8a8);
  transition: transform .2s;
}
.nd-group.is-open .nd-group-head .nd-arrow { transform: rotate(180deg); }
.nd-group-items {
  display: none;
  padding: 2px 0 6px 10px;
}
.nd-group.is-open .nd-group-items { display: block; }
.nd-link {
  display: block;
  padding: 12px 16px;
  font-size: 15px;
  color: var(--text-sub, #9fb8a8);
  cursor: pointer;
  border-radius: 8px;
}
.nd-link:active { background: rgba(212, 168, 67, .12); }
.nd-link.active {
  color: var(--gold-bright, #f0d89a);
  background: rgba(212, 168, 67, .14);
}
.nd-foot {
  margin-top: 10px;
  padding: 14px 16px 0;
  border-top: 1px solid rgba(212, 168, 67, .18);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  font-size: 13px;
  color: var(--text-sub, #9fb8a8);
}

/* 平板横屏（1025–1279）导航已是抽屉，但侧栏仍并排显示，这里只做轻微收窄 */
@media (min-width: 1025px) and (max-width: 1279px) {
  .left-panel { width: 240px; }
  .right-panel { width: 270px; }
  .cockpit-body { grid-template-columns: 250px 1fr 250px; }
}

/* =============================================================
   登录页：已有 768px 断点，这里只补 iOS 聚焦缩放防护
   （输入框字号 <16px 时 iOS Safari 会自动放大整个页面）
   ============================================================= */
@media (max-width: 820px) {
  input[type="text"], input[type="password"], input[type="email"], select {
    font-size: 16px;
    min-height: 44px;
  }
}

/* =============================================================
   登录页：已有 768px 断点，这里只补 iOS 聚焦缩放防护
   （输入框字号 <16px 时 iOS Safari 会自动放大整个页面）
   ============================================================= */
@media (max-width: 820px) {
  input[type="text"], input[type="password"], input[type="email"], select {
    font-size: 16px;
    min-height: 44px;
  }
}
