/* Mobile overrides are kept together so later desktop themes cannot compress tables. */
.mobile-filter-toggle,
.mobile-detail-back { display: none; }

@media (max-width: 1080px) {
  /* hidden on one axis creates a scroll container and prevents the header sticking. */
  .app-shell { overflow: clip; }
}

@media (min-width: 641px) and (max-width: 1080px) {
  .metrics-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .lead-table { min-width: 900px; }
  .lead-table-wrap { overflow-x: auto; }
}

@media (max-width: 640px) {
  html { scroll-padding-top: 80px; }
  body { -webkit-text-size-adjust: 100%; }
  .main { padding: 12px 12px calc(88px + env(safe-area-inset-bottom)); gap: 16px; }
  .sidebar, .app-shell.sidebar-collapsed .sidebar {
    padding: 8px 12px;
    padding-top: max(8px, env(safe-area-inset-top));
    gap: 8px;
  }
  .brand-lockup { border: 0; }
  .brand-logo { width: 148px; max-width: calc(100vw - 190px); }
  .sidebar-account-card { order: 1; }
  .sidebar-collapse-toggle { order: 2; border-radius: 6px; }
  .app-shell.mobile-nav-open .nav-list,
  .app-shell.mobile-nav-open .sidebar-settings-btn { order: 3; }
  .app-shell.mobile-nav-open .nav-list { max-height: calc(100dvh - 160px); gap: 8px; }
  .nav-group-title { min-height: 52px; box-shadow: none; }
  .nav-item { min-height: 48px; }

  .topbar { padding: 4px 0 14px; border: 0; border-bottom: 1px solid var(--line); border-radius: 0; background: none; box-shadow: none; gap: 12px; }
  .topbar h1 { font-size: 24px; line-height: 1.3; margin: 0; }
  .topbar .eyebrow { display: none; }
  .page-subtitle { margin-top: 6px; font-size: 13px; line-height: 1.5; }
  .topbar-actions { display: flex; width: 100%; justify-content: flex-end; gap: 8px; }
  .app-status-line:empty { display: none; }
  .metrics-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .metric-card { padding: 12px; min-height: 88px; gap: 4px; box-shadow: none; }
  .metric-card strong { font-size: 24px; }
  .metric-card span, .metric-card small { font-size: 12px; }
  .workspace-grid { gap: 12px; }

  .queue-tabs { flex-wrap: nowrap; overflow-x: auto; padding: 10px 12px; gap: 8px; scrollbar-width: thin; }
  .queue-tab { flex: 0 0 auto; min-height: 40px; font-size: 13px; }
  .lead-toolbar { grid-template-columns: minmax(0, 1fr) auto !important; gap: 10px; padding: 12px; }
  .lead-toolbar .search-wrap { grid-column: auto; }
  .lead-toolbar .search-wrap label { display: none; }
  .mobile-filter-toggle { display: block; align-self: end; min-height: 44px; }
  .mobile-filter-toggle[aria-expanded="true"] { background: var(--soft); border-color: var(--brand); }
  .lead-toolbar:not(.mobile-filters-open) > :not(.search-wrap):not(.mobile-filter-toggle) { display: none; }
  .lead-toolbar.mobile-filters-open { grid-template-columns: repeat(2, minmax(0, 1fr)) !important; }
  .lead-toolbar.mobile-filters-open .search-wrap { grid-column: 1; }
  .lead-toolbar .lead-date-picker { grid-column: 1 / -1; }
  .date-range-popover { max-width: calc(100vw - 52px); }
  .bulk-toolbar { padding: 8px 12px; gap: 8px; }
  .bulk-toolbar:not(.has-selection) .bulk-status-controls,
  .bulk-toolbar:not(.has-selection) .bulk-action-controls { display: none; }
  .bulk-toolbar #selectedCount { font-size: 12px; color: var(--muted); }
  .bulk-action-controls { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .bulk-action-controls > button { min-width: 0; white-space: normal; }
  .bulk-action-controls .ai-model-badge { grid-column: 1 / -1; justify-self: start; }

  /* Reuse the existing row controls and handlers, without a second copy of lead data. */
  .lead-table, .lead-table tbody { display: block; width: 100%; min-width: 0; }
  .lead-table thead, .lead-table thead tr { display: block; }
  .lead-table thead th { display: none; }
  .lead-table thead th.select-col { display: flex; align-items: center; gap: 8px; width: auto; padding: 4px 12px; min-height: 44px; }
  .lead-table thead th.select-col::after { content: "选择当前页"; font-size: 12px; font-weight: 500; }
  .lead-table .lead-row { display: grid; position: relative; grid-template-columns: 32px repeat(2, minmax(0, 1fr)); gap: 6px 8px; padding: 12px; border-top: 1px solid var(--line); }
  .lead-table .lead-row > td { display: block; width: auto; min-width: 0; padding: 0; border: 0; font-size: 13px; line-height: 1.5; overflow-wrap: anywhere; }
  .lead-table .lead-row > .select-cell { grid-column: 1; grid-row: 1 / 5; align-self: start; }
  .lead-table input[type="checkbox"] { width: 20px; height: 20px; margin: 10px 4px; }
  .lead-table .lead-row > .company-cell { grid-column: 2 / -1; }
  .lead-table .lead-row > td:nth-child(3) { grid-column: 2 / -1; }
  .lead-table .lead-row > td:nth-child(4),
  .lead-table .lead-row > td:nth-child(6) { grid-column: 2; }
  .lead-table .lead-row > td:nth-child(5),
  .lead-table .lead-row > td:nth-child(7) { grid-column: 3; }
  .lead-table .lead-row > td:nth-child(8) { grid-column: 2 / -1; display: flex; align-items: center; justify-content: space-between; gap: 8px; }
  .lead-table .lead-row > td:nth-child(4)::before { content: "国家 "; color: var(--muted); }
  .lead-table .lead-row > td:nth-child(5)::before { content: "来源 "; color: var(--muted); }
  .lead-table .lead-row > td:nth-child(6)::before { content: "评分 "; color: var(--muted); }
  .lead-table .company-title-line { flex-wrap: wrap; gap: 4px 8px; }
  .lead-table .company-title-line strong { max-width: 100%; white-space: normal; overflow: visible; font-size: 15px; }
  .lead-table .contact-title-line { flex-wrap: wrap; gap: 6px; }
  .lead-table .lead-url { display: block; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 12px; }
  .lead-table .lead-entered-cell time { font-size: 12px; color: var(--muted); }
  .lead-table .country-cell strong { display: inline; }
  .lead-pagination { flex-direction: column; align-items: stretch; padding: 12px; gap: 10px; }
  .lead-pagination-controls { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .lead-pagination-controls label { min-width: 0; }
  .lead-pagination-controls input { width: 100%; min-width: 0; }
  .workspace-grid:not(.mobile-detail-open) > .detail-panel,
  .workspace-grid.mobile-detail-open > .lead-panel { display: none; }
  .mobile-detail-back { display: block; margin-bottom: 14px; }
  .detail-panel { padding: 14px; }
  .detail-panel h2 { font-size: 20px; overflow-wrap: anywhere; }
  .detail-tab { min-height: 44px; }

  .internal-maps-panel > .module-header > div:first-child,
  .cleaning-panel > .module-header > div:first-child,
  .social-collector-panel > .module-header > div:first-child { display: none; }
  .module-header { margin-bottom: 12px; gap: 12px; }
  .module-header h2 { font-size: 20px; }
  .internal-maps-actions, .cleaning-actions { width: 100%; justify-content: flex-start; gap: 8px; flex-wrap: wrap; }
  .internal-maps-grid, .cleaning-grid { gap: 12px; }
  .internal-maps-grid > * { min-width: 0; }

  input:not([type="checkbox"]):not([type="radio"]), select, textarea,
  .agent-memory-form input:not([type="checkbox"]), .agent-learning-form > input,
  .agent-learning-form > select, .agent-learning-form > textarea,
  .agent-prompt-form textarea, .agent-memory-form textarea { font-size: 16px; }
  :is(.primary-btn, .ghost-btn, .danger-btn, .icon-btn, .agent-icon-btn) { min-height: 44px; }
  dialog { max-width: calc(100vw - 24px); max-height: calc(100dvh - 24px - env(safe-area-inset-top) - env(safe-area-inset-bottom)); overscroll-behavior: contain; }
  .lead-form, .auth-dialog-form { padding: 16px; }
  .dialog-header { gap: 10px; }
  .dialog-header h2 { font-size: 20px; }
  .dialog-header button { flex-shrink: 0; }
  .agent-panel { display: flex; flex-direction: column; height: 100dvh; padding-top: env(safe-area-inset-top); }
  .agent-panel[hidden] { display: none; }
  .agent-panel-header, .agent-tabs { flex-shrink: 0; }
  .agent-panel-body { height: auto; min-height: 0; flex: 1; padding-bottom: env(safe-area-inset-bottom); }
  .agent-tabs { padding: 0 8px; }
  .agent-tabs button { padding: 8px 2px; font-size: 13px; }
  .agent-launcher { top: max(8px, env(safe-area-inset-top)); right: 116px; bottom: auto; z-index: 101; width: 44px; height: 44px; padding: 0; justify-content: center; box-shadow: none; }
  .agent-launcher strong { position: absolute; top: -5px; right: -5px; min-width: 16px; height: 16px; padding: 0 3px; font-size: 10px; }
}
