/* ============================================
   恒光科技 ERP V5.1 — 移动端全面适配
   断点: 768px (平板/手机) / 480px (小屏手机)
   策略: 覆盖内联样式 + 桌面样式，实现完整移动端体验
   ============================================ */

/* ========================================
   1. 全局变量 & 基础重置
   ======================================== */
@media (max-width: 768px) {
  :root {
    --sidebar-width: 260px;
    --header-height: 52px;
  }

  * {
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
  }

  body {
    font-size: 13px;
    overscroll-behavior: none;
    -webkit-overflow-scrolling: touch;
    overflow: hidden;
    height: 100%;
  }

  html { height: 100%; }

  /* 防止水平溢出，强制 viewport 内布局 */
  .app-container {
    overflow-x: hidden;
    height: 100vh;
    overflow: hidden;
  }

  .main-content {
    height: 100vh;
  }

  .page-content {
    min-height: 0; /* 关键：flex 子元素 overflow 必需 */
  }

  /* 图表高度限制 */
  #dashChartArea {
    height: 200px !important;
  }

  /* ========================================
     2. 侧边栏 — 抽屉式
     ======================================== */
  .sidebar {
    position: fixed !important;
    top: 0;
    left: 0;
    bottom: 0;
    width: var(--sidebar-width) !important;
    max-width: 80vw;
    transform: translateX(-100%);
    transition: transform 0.3s ease;
    z-index: 1001;
    box-shadow: 2px 0 16px rgba(0,0,0,0.15);
  }

  .sidebar.open {
    transform: translateX(0);
  }

  /* 侧边栏内部恢复正常显示 */
  .sidebar .nav-item span:not(.icon) {
    display: inline !important;
  }
  .sidebar .nav-section-title {
    display: block !important;
  }
  .sidebar-logo h2 {
    font-size: 17px;
  }

  /* 侧边栏遮罩 */
  .sidebar-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 1000;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s ease;
  }
  .sidebar-overlay.active {
    opacity: 1;
    pointer-events: auto;
  }

  /* 汉堡按钮 */
  .hamburger-btn {
    display: flex !important;
    flex-direction: column;
    justify-content: center;
    gap: 4px;
    width: 36px;
    height: 36px;
    border: none;
    background: transparent;
    cursor: pointer;
    padding: 0 6px;
    border-radius: 6px;
    flex-shrink: 0;
  }
  .hamburger-btn span {
    display: block;
    width: 20px;
    height: 2px;
    background: var(--text);
    border-radius: 1px;
    transition: all 0.2s;
  }

  /* ========================================
     3. 主内容区
     ======================================== */
  .main-content {
    margin-left: 0 !important;
    max-width: 100vw !important;
    width: 100%;
  }

  /* 顶部栏 */
  .topbar {
    height: var(--header-height);
    padding: 0 12px !important;
    gap: 8px;
  }

  .topbar-title {
    display: none !important;
  }

  .topbar-actions {
    gap: 6px;
  }

  .topbar-actions .topbar-user-name {
    display: none;
  }

  .topbar-actions .btn {
    padding: 6px 10px;
    font-size: 12px;
  }

  /* 顶部搜索 */
  .topbar-search-wrap {
    flex: 1 !important;
    max-width: none;
  }

  .topbar-search-box {
    height: 34px;
    padding: 0 8px;
    gap: 6px;
  }

  .topbar-search-box input {
    font-size: 13px;
  }

  .topbar-search-box input::placeholder {
    font-size: 12px;
  }

  .topbar-search-kbd {
    display: none !important;
  }

  /* 搜索下拉面板 */
  .topbar-search-dropdown {
    min-width: 0 !important;
    width: 100%;
    max-width: calc(100vw - 24px);
    position: fixed !important;
    top: var(--header-height) !important;
    left: 12px !important;
    right: 12px;
    max-height: 60vh;
  }

  /* ========================================
     4. 页面内容区
     ======================================== */
  .page-content {
    padding: 10px 12px !important;
    padding-bottom: 70px !important; /* 给底部导航留空间 */
  }

  /* ========================================
     5. 卡片
     ======================================== */
  .card {
    padding: 12px !important;
    margin-bottom: 10px !important;
    border-radius: 8px !important;
  }

  .card-header {
    margin-bottom: 10px;
    flex-wrap: wrap;
    gap: 8px;
  }

  .card-title {
    font-size: 14px;
  }

  /* ========================================
     6. 网格布局覆盖 (核心!)
     覆盖所有内联 grid-template-columns
     ======================================== */

  /* 4列 → 2列 */
  .page-content [style*="grid-template-columns:repeat(4"],
  .page-content [style*="grid-template-columns: repeat(4"],
  .page-content [style*="grid-template-columns:1fr 1fr 1fr 1fr"] {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 8px !important;
  }

  /* 5列 → 2列 */
  .page-content [style*="grid-template-columns:repeat(5"],
  .page-content [style*="grid-template-columns: repeat(5"] {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 8px !important;
  }

  /* 3列 → 1列 */
  .page-content [style*="grid-template-columns:repeat(3"],
  .page-content [style*="grid-template-columns: repeat(3"],
  .page-content [style*="grid-template-columns:1fr 1fr 1fr"] {
    grid-template-columns: 1fr !important;
    gap: 8px !important;
  }

  /* 2列 → 1列 (统计卡片等) */
  .page-content [style*="grid-template-columns:1fr 1fr"]:not([style*="repeat(2"]) {
    grid-template-columns: 1fr !important;
    gap: 8px !important;
  }

  /* 但是 .page-content 内的子元素的子元素如果是 1fr 1fr 保留 (小格子) */
  .page-content .card [style*="grid-template-columns:1fr 1fr"],
  .page-content .card [style*="grid-template-columns:repeat(2"] {
    grid-template-columns: repeat(2, 1fr) !important;
  }

  /* 统计数字缩小 */
  .page-content [style*="font-size:28px"],
  .page-content [style*="font-size: 28px"] {
    font-size: 20px !important;
  }

  .page-content [style*="font-size:24px"],
  .page-content [style*="font-size: 24px"] {
    font-size: 18px !important;
  }

  .page-content [style*="font-size:20px"],
  .page-content [style*="font-size: 20px"] {
    font-size: 16px !important;
  }

  /* ========================================
     7. 表单
     ======================================== */
  .form-row {
    flex-direction: column !important;
    gap: 0 !important;
  }

  .form-row .form-group {
    min-width: 0 !important;
    width: 100%;
  }

  .form-input,
  .form-select,
  .form-textarea {
    padding: 10px 12px !important;
    font-size: 14px !important; /* 16px+ 防止 iOS 缩放 */
    min-height: 40px;
  }

  .form-input-sm {
    padding: 8px 10px !important;
    font-size: 13px !important;
  }

  .form-label {
    font-size: 12px;
    margin-bottom: 3px;
  }

  .form-group {
    margin-bottom: 10px;
  }

  /* ========================================
     8. 按钮
     ======================================== */
  .btn {
    padding: 9px 14px !important;
    font-size: 13px !important;
    min-height: 38px;
  }

  .btn-sm {
    padding: 6px 10px !important;
    font-size: 12px !important;
    min-height: 32px;
  }

  .btn-lg {
    padding: 12px 20px !important;
    font-size: 15px !important;
    min-height: 44px;
  }

  /* ========================================
     9. 表格
     ======================================== */
  .table-container {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  table {
    font-size: 12px !important;
    min-width: 600px;
  }

  table th {
    padding: 8px 10px !important;
    font-size: 12px;
  }

  table td {
    padding: 8px 10px !important;
  }

  /* 表格行增加触控高度 */
  table tbody tr {
    min-height: 44px;
  }

  /* ========================================
     10. 弹窗/模态框 — 全屏化
     ======================================== */
  .modal-overlay {
    align-items: stretch !important;
    padding: 0 !important;
  }

  .modal,
  .modal-content {
    width: 100% !important;
    max-width: 100% !important;
    max-height: 100vh !important;
    height: 100vh !important;
    border-radius: 0 !important;
    margin: 0 !important;
  }

  .modal-lg {
    max-width: 100% !important;
  }

  .modal-header {
    padding: 12px 14px !important;
  }

  .modal-title {
    font-size: 15px;
  }

  .modal-body {
    padding: 12px 14px !important;
  }

  .modal-footer {
    padding: 10px 14px !important;
    flex-wrap: wrap;
  }

  .modal-footer .btn {
    flex: 1;
    min-width: 80px;
  }

  /* 弹窗内表单行也变单列 */
  .modal .form-row,
  .modal-content .form-row {
    flex-direction: column !important;
  }

  /* picker 下拉全宽 */
  .picker-dropdown {
    min-width: 0 !important;
    width: 100% !important;
    max-width: 100% !important;
  }

  /* ========================================
     11. 标签页/Tabs
     ======================================== */
  .tabs {
    overflow-x: auto;
    justify-content: flex-start !important;
    flex-wrap: nowrap !important;
    -webkit-overflow-scrolling: touch;
    gap: 8px !important;
    padding-bottom: 4px;
  }

  .tabs .tab {
    padding: 8px 16px !important;
    font-size: 13px !important;
    white-space: nowrap;
    flex-shrink: 0;
  }

  /* ========================================
     12. 底部导航
     ======================================== */
  .mobile-bottom-nav {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: var(--bg-white);
    border-top: 1px solid var(--border);
    z-index: 900;
    box-shadow: 0 -2px 10px rgba(0,0,0,0.06);
    padding-bottom: env(safe-area-inset-bottom, 0px);
  }

  .mobile-bottom-nav .nav-tabs {
    display: flex;
    overflow-x: hidden;
  }

  .mobile-bottom-nav .nav-tab {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    padding: 6px 4px;
    flex: 1;
    min-width: 0;
    text-decoration: none;
    color: var(--text-light);
    font-size: 10px;
    transition: color 0.15s;
    cursor: pointer;
    position: relative;
  }

  .mobile-bottom-nav .nav-tab .nav-icon {
    font-size: 18px;
    line-height: 1;
  }

  .mobile-bottom-nav .nav-tab .nav-label {
    font-size: 10px;
    white-space: nowrap;
  }

  .mobile-bottom-nav .nav-tab.active {
    color: var(--primary);
    font-weight: 600;
  }

  .mobile-bottom-nav .nav-tab.active::after {
    content: '';
    position: absolute;
    top: 0;
    left: 50%;
    transform: translateX(-50%);
    width: 24px;
    height: 2px;
    background: var(--primary);
    border-radius: 0 0 2px 2px;
  }

  /* 更多菜单按钮 */
  .mobile-bottom-nav .nav-tab.more-tab {
    border-left: 1px solid var(--border-light);
  }

  /* 更多菜单面板 */
  .mobile-more-panel {
    position: fixed;
    bottom: 56px;
    left: 0;
    right: 0;
    background: var(--bg-white);
    border-top: 1px solid var(--border);
    border-radius: 12px 12px 0 0;
    box-shadow: 0 -4px 20px rgba(0,0,0,0.1);
    z-index: 899;
    padding: 12px;
    transform: translateY(100%);
    transition: transform 0.25s ease;
    max-height: 50vh;
    overflow-y: auto;
    padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
  }

  .mobile-more-panel.open {
    transform: translateY(0);
  }

  .mobile-more-panel-title {
    font-size: 13px;
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: 10px;
    padding: 0 4px;
  }

  .mobile-more-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 8px;
  }

  .mobile-more-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    padding: 10px 4px;
    border-radius: 8px;
    text-decoration: none;
    color: var(--text);
    font-size: 11px;
    background: var(--bg);
    cursor: pointer;
    transition: background 0.15s;
  }

  .mobile-more-item:active {
    background: var(--primary-light);
  }

  .mobile-more-item .icon {
    font-size: 22px;
  }

  /* 更多面板遮罩 */
  .mobile-more-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.3);
    z-index: 898;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s;
  }

  .mobile-more-overlay.open {
    opacity: 1;
    pointer-events: auto;
  }

  /* ========================================
     13. 回到顶部按钮
     ======================================== */
  .back-to-top {
    position: fixed;
    right: 16px;
    bottom: 72px;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: var(--primary);
    color: #fff;
    border: none;
    font-size: 18px;
    cursor: pointer;
    box-shadow: 0 2px 10px rgba(0,0,0,0.2);
    z-index: 800;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s, transform 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .back-to-top.show {
    opacity: 1;
    pointer-events: auto;
  }

  .back-to-top:active {
    transform: scale(0.9);
  }

  /* ========================================
     14. Toast 提示
     ======================================== */
  .toast {
    top: 10px !important;
    right: 10px !important;
    left: 10px !important;
    max-width: none !important;
    padding: 10px 14px !important;
    font-size: 13px !important;
    border-radius: 8px !important;
    text-align: center;
  }

  /* ========================================
     15. 登录页
     ======================================== */
  .app-container > div[style*="min-height:100vh"] {
    padding: 20px !important;
  }

  .app-container > div[style*="min-height:100vh"] > div[style*="width:380px"] {
    width: 100% !important;
    max-width: 360px;
    padding: 24px 20px !important;
  }

  /* ========================================
     16. AI 侧边栏 — 全屏化
     ======================================== */
  #aiSidebar {
    width: 100% !important;
    max-width: 100vw !important;
  }

  #aiFab {
    bottom: 64px !important;
    right: 16px !important;
    width: 48px !important;
    height: 48px !important;
    font-size: 22px !important;
  }

  /* ========================================
     17. 报价模块特定适配
     ======================================== */

  /* 报价参数面板 */
  .param-panel {
    width: 100% !important;
  }

  .page.active {
    flex-direction: column !important;
  }

  /* 报价表格内输入框 */
  .quote-table input,
  .quote-table select {
    font-size: 12px !important;
    padding: 6px 4px !important;
  }

  /* ========================================
     18. 空状态 & 加载
     ======================================== */
  .empty-state {
    padding: 40px 16px !important;
  }

  .empty-state .icon {
    font-size: 36px !important;
  }

  .loading {
    padding: 30px 16px !important;
  }

  /* ========================================
     19. Badge & 标签
     ======================================== */
  .badge {
    font-size: 10px !important;
    padding: 2px 6px !important;
  }

  /* ========================================
     20. 工具栏 / 操作栏
     ======================================== */

  /* 页面顶部操作栏 */
  .page-content > .card:first-child [style*="display:flex"],
  .page-content > div[style*="display:flex"][style*="justify-content"] {
    flex-wrap: wrap !important;
    gap: 8px !important;
  }

  /* 按钮组横排可滚动 */
  .page-content [style*="display:flex"][style*="gap"] {
    flex-wrap: wrap !important;
  }

  /* ========================================
     21. 搜索框 & 筛选区
     ======================================== */
  .page-content [style*="display:flex"][style*="align-items:center"][style*="gap"]:not(.nav-tabs) {
    flex-wrap: wrap !important;
  }

  /* ========================================
     22. 图片/附件网格
     ======================================== */
  .page-content [style*="display:grid"][style*="grid-template-columns"][style*="auto-fill"] {
    grid-template-columns: repeat(auto-fill, minmax(80px, 1fr)) !important;
    gap: 6px !important;
  }

  /* ========================================
     23. 通用内联样式覆盖
     ======================================== */

  /* 所有 display:flex 在 page-content 内允许换行 */
  .page-content [style*="display:flex"]:not(.nav-tabs):not(.nav-tab) {
    flex-wrap: wrap;
  }

  /* 固定宽度的内联样式解除 */
  .page-content [style*="width:"] {
    max-width: 100%;
  }

  /* padding 过大的内联样式 */
  .page-content [style*="padding:20px"],
  .page-content [style*="padding: 20px"] {
    padding: 12px !important;
  }

  .page-content [style*="padding:24px"],
  .page-content [style*="padding: 24px"] {
    padding: 12px !important;
  }

  .page-content [style*="padding:16px 24px"],
  .page-content [style*="padding: 16px 24px"] {
    padding: 10px 12px !important;
  }

  /* margin 过大 */
  .page-content [style*="margin-bottom:16px"],
  .page-content [style*="margin-bottom: 16px"] {
    margin-bottom: 10px !important;
  }

  .page-content [style*="margin-bottom:24px"],
  .page-content [style*="margin-bottom: 24px"] {
    margin-bottom: 12px !important;
  }

  /* gap 过大 */
  .page-content [style*="gap:16px"],
  .page-content [style*="gap: 16px"] {
    gap: 8px !important;
  }

  .page-content [style*="gap:24px"],
  .page-content [style*="gap: 24px"] {
    gap: 12px !important;
  }
}

/* ========================================
   超小屏 (<=480px) 进一步优化
   ======================================== */
@media (max-width: 480px) {
  body {
    font-size: 12px;
  }

  .page-content {
    padding: 8px 10px !important;
    padding-bottom: 68px !important;
  }

  .card {
    padding: 10px !important;
  }

  /* 4列 → 1列 (超小屏) */
  .page-content [style*="grid-template-columns:repeat(4"],
  .page-content [style*="grid-template-columns: repeat(4"],
  .page-content [style*="grid-template-columns:1fr 1fr 1fr 1fr"] {
    grid-template-columns: repeat(2, 1fr) !important;
  }

  .page-content [style*="grid-template-columns:repeat(5"],
  .page-content [style*="grid-template-columns: repeat(5"] {
    grid-template-columns: repeat(3, 1fr) !important;
  }

  /* 商机统计5列 → 3列 */
  .page-content .card [style*="grid-template-columns:repeat(5"] {
    grid-template-columns: repeat(3, 1fr) !important;
  }

  /* 底部导航每个Tab更小 */
  .mobile-bottom-nav .nav-tab {
    padding: 5px 2px;
  }

  .mobile-bottom-nav .nav-tab .nav-icon {
    font-size: 16px;
  }

  .mobile-bottom-nav .nav-tab .nav-label {
    font-size: 9px;
  }

  /* 更多面板 grid → 3列 */
  .mobile-more-grid {
    grid-template-columns: repeat(3, 1fr);
  }

  /* 表格更紧凑 */
  table th,
  table td {
    padding: 6px 8px !important;
  }

  /* 按钮 */
  .btn {
    padding: 8px 12px !important;
    font-size: 12px !important;
  }

  /* 顶部栏高度减小 */
  .topbar {
    height: 48px;
  }

  .topbar-search-box {
    height: 32px;
  }

  /* FAB按钮位置调整 */
  #aiFab {
    bottom: 60px !important;
    right: 12px !important;
  }

  /* 回到顶部 */
  .back-to-top {
    bottom: 68px;
    right: 12px;
  }
}

/* ========================================
   桌面端: 隐藏移动端专用元素
   ======================================== */
@media (min-width: 769px) {
  .hamburger-btn {
    display: none !important;
  }

  .mobile-bottom-nav {
    display: none !important;
  }

  .sidebar-overlay {
    display: none !important;
  }

  .back-to-top {
    display: none !important;
  }

  .mobile-more-panel,
  .mobile-more-overlay {
    display: none !important;
  }
}

/* ========================================
   横屏适配 (手机横屏)
   ======================================== */
@media (max-width: 768px) and (orientation: landscape) {
  .modal,
  .modal-content {
    height: 100vh !important;
    max-height: 100vh !important;
  }

  .modal-body {
    max-height: calc(100vh - 100px);
  }

  /* 横屏时底部导航可以更紧凑 */
  .mobile-bottom-nav .nav-tab {
    padding: 4px 2px;
  }
}

/* ========================================
   安全区域 (刘海屏/底部横条)
   ======================================== */
@supports (padding: env(safe-area-inset-top)) {
  .topbar {
    padding-top: env(safe-area-inset-top, 0px);
    height: calc(var(--header-height) + env(safe-area-inset-top, 0px));
  }

  .mobile-bottom-nav {
    padding-bottom: env(safe-area-inset-bottom, 0px);
  }
}
