/* ===== BizAI Mobile Optimization v2.0 ===== */

@media (max-width: 768px) {
  body {
    font-size: 14px;
    -webkit-text-size-adjust: 100%;
    -webkit-tap-highlight-color: transparent;
  }

  /* --- 모든 컨테이너 overflow 잘림 방지 --- */
  #companyDetailContent,
  #companyDetailContent *,
  .crm-detail-tab-content,
  .da-sub-content,
  .da-sub-content > div,
  .pd-result,
  .pd-result > div,
  #companyHomeSection,
  #companyHomeSection *,
  .container {
    overflow-x: visible !important;
    max-width: 100% !important;
    box-sizing: border-box !important;
  }

  /* --- 카테고리 탭 (데이터/진단/컨설팅/매칭/관리) 줄바꿈 --- */
  #companyCategoryBar,
  div[data-testid='company-category-bar'] {
    flex-wrap: wrap !important;
    overflow-x: visible !important;
  }
  .company-cat-btn {
    flex: 0 0 auto !important;
    min-width: 18% !important;
    font-size: 11px !important;
    padding: 8px 2px !important;
    gap: 2px !important;
  }

  /* --- 서브 탭 (기본정보/업로드분석/기업데이터 등) 줄바꿈 --- */
  #companySubTabBar,
  div[data-testid='company-sub-tab-bar'] {
    overflow-x: visible !important;
  }
  #companySubTabBar > div,
  div[data-testid='company-sub-tab-bar'] > div {
    flex-wrap: wrap !important;
    overflow-x: visible !important;
  }
  .company-sub-tab-btn {
    font-size: 11px !important;
    padding: 6px 10px !important;
    white-space: nowrap !important;
  }

  /* --- CRM Detail Tabs 줄바꿈 --- */
  .crm-detail-tabs {
    flex-wrap: wrap !important;
    overflow-x: visible !important;
    gap: 3px !important;
    padding: 4px !important;
  }
  .crm-detail-tab {
    font-size: 11px !important;
    padding: 6px 10px !important;
    white-space: nowrap !important;
    border-radius: 8px !important;
  }

  /* --- 서브탭 바 줄바꿈 --- */
  div[data-testid*='sub-tabs'],
  .da-sub-tabs,
  .ti-sub-tabs {
    flex-wrap: wrap !important;
    overflow-x: visible !important;
    gap: 3px !important;
  }
  div[data-testid*='sub-tabs'] button,
  .da-sub-tabs button,
  .ti-sub-tabs button {
    font-size: 11px !important;
    padding: 6px 10px !important;
    white-space: nowrap !important;
  }

  /* --- AI 분석 서브탭 줄바꿈 --- */
  div[data-testid='ai-analysis-subtabs'] {
    flex-wrap: wrap !important;
    overflow-x: visible !important;
  }
  div[data-testid='ai-analysis-subtabs'] button {
    font-size: 10px !important;
    padding: 6px 8px !important;
    white-space: nowrap !important;
  }

  /* --- 프로세스 탭바 줄바꿈 --- */
  div[data-testid='process-tab-bar'] {
    flex-wrap: wrap !important;
    overflow-x: visible !important;
  }
  div[data-testid='process-tab-bar'] button {
    font-size: 11px !important;
    padding: 8px 12px !important;
  }

  /* --- 모달 탭 줄바꿈 --- */
  .modal-tabs {
    flex-wrap: wrap !important;
    overflow-x: visible !important;
  }
  .modal-tab {
    font-size: 12px !important;
    padding: 8px 10px !important;
    white-space: nowrap !important;
  }

  /* --- Hero Section --- */
  .hero {
    padding: 16px 12px !important;
    gap: 8px !important;
  }
  .hero h1 {
    font-size: 20px !important;
    line-height: 1.3 !important;
  }
  .hero p, .hero-subtitle {
    font-size: 12px !important;
  }

  /* --- Action Cards --- */
  .action-cards {
    grid-template-columns: 1fr 1fr !important;
    gap: 8px !important;
    padding: 0 8px !important;
  }
  .action-card {
    padding: 12px 8px !important;
    font-size: 11px !important;
    border-radius: 12px !important;
    min-height: 70px !important;
  }

  /* --- CRM Detail Panel --- */
  #companyDetailContent {
    padding: 8px !important;
  }
  .crm-detail-header {
    padding: 10px !important;
    flex-direction: column !important;
    gap: 6px !important;
  }
  .crm-detail-title {
    font-size: 16px !important;
  }
  .crm-detail-info {
    grid-template-columns: 1fr !important;
    gap: 4px !important;
    font-size: 11px !important;
  }
  .crm-detail-tab-content {
    padding: 8px !important;
  }

  /* --- Grid 레이아웃 모바일 대응 --- */
  div[style*='grid-template-columns: 1fr 1fr 1fr'],
  div[style*='grid-template-columns:1fr 1fr 1fr'],
  div[style*='grid-template-columns: repeat(3'],
  div[style*='grid-template-columns:repeat(3'],
  div[style*='grid-template-columns: repeat(4'],
  div[style*='grid-template-columns:repeat(4'] {
    grid-template-columns: 1fr 1fr !important;
  }

  /* --- Bottom Tab Bars --- */
  #consultantBottomTabBar,
  #adminBottomTabBar,
  #enterpriseBottomTabBar {
    padding: 4px 0 !important;
    padding-bottom: calc(4px + env(safe-area-inset-bottom)) !important;
  }
  .c-bottom-tab,
  .a-bottom-tab {
    font-size: 10px !important;
    padding: 4px 2px !important;
  }

  /* --- Enterprise Tab --- */
  .ent-tab-bar { gap: 0 !important; }
  .ent-tab {
    padding: 8px 10px !important;
    font-size: 12px !important;
  }
  .ent-form { grid-template-columns: 1fr !important; }

  /* --- Tables 가로 스크롤 허용 --- */
  table {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    max-width: 100%;
  }
  table th, table td {
    font-size: 11px !important;
    padding: 4px 6px !important;
  }

  /* --- Buttons 최소 터치 영역 --- */
  button { min-height: 36px; }

  /* --- Input iOS 자동줌 방지 --- */
  input[type='text'],
  input[type='search'],
  input[type='number'],
  textarea,
  select {
    font-size: 16px !important;
  }

  /* --- 패딩 축소 --- */
  div[style*='padding:24px'],
  div[style*='padding: 24px'],
  div[style*='padding:20px'],
  div[style*='padding: 20px'] {
    padding: 12px !important;
  }

  /* --- 갭 축소 --- */
  div[style*='display:flex'][style*='gap:16px'],
  div[style*='display:flex'][style*='gap:20px'] {
    gap: 8px !important;
  }

  /* --- Charts --- */
  canvas {
    max-width: 100% !important;
    height: auto !important;
  }

  /* --- 스크롤바 --- */
  ::-webkit-scrollbar { width: 3px; height: 3px; }
  ::-webkit-scrollbar-thumb { background: rgba(0,0,0,0.15); border-radius: 4px; }
}

/* --- 400px 이하 --- */
@media (max-width: 400px) {
  .action-cards {
    grid-template-columns: 1fr 1fr !important;
    gap: 6px !important;
  }
  .hero h1 { font-size: 18px !important; }
  .company-cat-btn {
    font-size: 10px !important;
    min-width: 30% !important;
  }
}

/* --- iPhone Safe Area --- */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
  #consultantBottomTabBar,
  #adminBottomTabBar,
  #enterpriseBottomTabBar {
    padding-bottom: calc(8px + env(safe-area-inset-bottom)) !important;
  }
}

/* === Bigdata Enhance 차트/KPI 모바일 대응 v2.1 === */
@media (max-width: 768px) {
  /* KPI 카드 3열 → 2열 */
  div[style*='grid-template-columns:repeat(3,1fr)'] {
    grid-template-columns: 1fr 1fr !important;
  }

  /* 차트 2열 → 1열 세로 */
  div[style*='grid-template-columns:1fr 1fr'][style*='gap:10px'] {
    grid-template-columns: 1fr !important;
  }

  /* 빈 차트 canvas 높이 제한 */
  canvas[id*='bde'] {
    max-height: 200px !important;
  }

  /* 인증 보유현황 카드 */
  div[data-testid='section-certifications'] {
    padding: 10px !important;
  }
  div[data-testid='section-certifications'] div[style*='grid'] {
    grid-template-columns: 1fr !important;
  }

  /* pd-section 패딩 */
  .pd-section {
    padding: 12px !important;
  }

  /* 통합조회 결과 영역 */
  .pd-result,
  .pd-result > div {
    overflow-x: visible !important;
  }

  /* pd-kv-grid 2열 → 1열 */
  .pd-kv-grid {
    grid-template-columns: 1fr !important;
  }
}

/* === 빅데이터 비연도 카테고리 2열→1열 v2.2 === */
@media (max-width: 768px) {
  /* 비재무/기술 등 key-value 2열 그리드 → 1열 */
  div[style*='grid-template-columns:1fr 1fr'][style*='gap:1px'] {
    grid-template-columns: 1fr !important;
  }
}
