/* AutoTrader — Responsive Styles
   Breakpoints:
   - Desktop:      1200px+
   - Large Tablet:  1025-1199px (iPad Pro 11" portrait: 834px → tablet)
   - Tablet:        768-1024px  (iPad Air 11": 820px)
   - Mobile:        360-767px   (Galaxy S26: 360px, iPhone 17: 402px)
   - Small Mobile:  <360px      (iPhone SE: 320px, legacy)

   Target devices (2025-2026):
   - Galaxy S26/S26+: 360px CSS viewport
   - Galaxy S26 Ultra: 412px CSS viewport
   - iPhone 17/Pro: 402px CSS viewport
   - iPhone 17 Pro Max: 440px CSS viewport
   - iPad Air 11" (M4): 820px portrait
   - iPad Pro 11" (M5): 834px portrait
   - iPad Pro 13" (M5): 1032px portrait
   - Galaxy Tab S10 Ultra: 924px portrait
*/

/* ── Tablet (≤1024px) ── */
@media (max-width: 1024px) {
  .grid-4 { grid-template-columns: repeat(2, 1fr); }
  .grid-3 { grid-template-columns: repeat(2, 1fr); }
  .grid-5 { grid-template-columns: repeat(3, 1fr); }

  /* App layout — sidebar slide-in
     !important needed: app.html inline <style> defines .app-sidebar { left: 0 }
     AFTER this stylesheet link, so same-specificity cascade order wins for inline.
     See bug #8 in Reports.md — same root cause as margin-left issue. */
  .app-sidebar {
    position: fixed; left: -260px !important; top: 0; bottom: 0;
    z-index: 1001 !important; transition: left 0.3s ease;
  }
  .app-sidebar.open { left: 0 !important; }
  .sidebar-overlay {
    display: none; position: fixed; inset: 0;
    background: rgba(0,0,0,0.4); z-index: 1000;
  }
  .sidebar-overlay.active { display: block; }
  .app-main { margin-left: 0 !important; }
  .mobile-header { display: flex !important; }

  /* Hamburger button — minimum 44px touch target */
  .hamburger {
    font-size: 1.6rem;
    padding: 0.5rem;
    min-width: 44px;
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
}

/* ── Mobile (≤768px) ── */
@media (max-width: 768px) {
  /* ══════════════════════════════════════════════════════════
     수평 오버플로우 차단 전략 (Bug #15, #21)
     ──────────────────────────────────────────────────────────
     주의: overflow-x:hidden을 html/body에 적용하면
     CSS 스펙상 overflow-y가 visible→auto로 변경되어
     스크롤 컨테이너가 중첩 생성됨 → 세로 스크롤 완전 차단!

     해결: html/body는 건드리지 않고, .app-layout 하나에만
     overflow-x:hidden 적용. 나머지는 max-width로 넘침 방지.
     ══════════════════════════════════════════════════════════ */
  html { font-size: 16px; }

  .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; }
  .grid-5 { grid-template-columns: repeat(2, 1fr); }
  .form-row { flex-direction: column; }
  .container { padding: 0 0.75rem; }

  .metric-value { font-size: 1.5rem; }
  .section-title { font-size: 1.1rem; margin-bottom: 0.5rem; }
  .table { font-size: 0.88rem; }
  .table th, .table td { padding: 0.5rem 0.6rem; }

  /* Form elements */
  .form-input, select.form-input {
    max-width: 100% !important;
    width: 100%;
    font-size: 1rem;
  }

  /* ── 수평 오버플로우 격리 — .app-layout 한 곳에서만 차단 ── */
  .app-layout {
    overflow-x: hidden;          /* 유일한 overflow-x:hidden 지점 */
    max-width: 100vw;
  }
  .app-main {
    max-width: 100vw;
    padding-bottom: calc(60px + env(safe-area-inset-bottom, 0));
  }
  #content {
    padding: 0.75rem;
    max-width: 100%;
  }
  .card {
    padding: 0.75rem;
    max-width: 100%;
  }

  /* ══════════════════════════════════════════════════════════
     테이블 — 모바일에서 수평 스크롤로 모든 정보 표시
     ──────────────────────────────────────────────────────────
     hide-mobile 폐지: 정보를 숨기지 않고 증권앱처럼 수평 스크롤.
     .table-wrap이 없는 테이블도 자동으로 래핑 처리.
     ══════════════════════════════════════════════════════════ */
  .table-wrap,
  .section > .table {  /* table-wrap 없이 바로 사용된 테이블도 처리 */
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    max-width: 100%;
    scrollbar-width: thin;
  }
  .table {
    white-space: nowrap;  /* 셀 텍스트 줄바꿈 방지 — 수평 스크롤 유도 */
  }
  .table th {
    position: sticky;
    top: 0;
    z-index: 1;
  }
  /* 첫 번째 열(종목명/업종명) 고정 — 스크롤 시에도 항상 보임 */
  .table th:first-child,
  .table td:first-child {
    position: sticky;
    left: 0;
    z-index: 2;
    background: var(--bg-secondary);
  }
  .table td:first-child {
    background: var(--bg-primary);
  }
  /* 그라데이션 힌트 제거 — 스크롤 시 따라다니는 버그 + 다크/라이트 호환 불가 */

  /* ══════════════════════════════════════════════════════════
     Sidebar — 슬라이드 인/아웃 + 터치 격리
     ══════════════════════════════════════════════════════════ */
  .app-sidebar {
    display: flex !important;
    left: -280px !important;    /* 250px 너비 + 여유 30px */
    position: fixed !important;
    top: 0; bottom: 0;
    width: 250px !important;    /* 명시적 너비 고정 */
    z-index: 1001 !important;   /* !important 필수: app.html inline z-index:900 override */
    transition: left 0.3s ease;
    overflow-y: auto;
    overscroll-behavior: contain;   /* 스크롤 전파 차단 */
    touch-action: pan-y;            /* 세로 스크롤만 허용 */
    -webkit-overflow-scrolling: touch;
  }
  .app-sidebar.open { left: 0 !important; }
  /* 사이드바 내부 링크 — 터치 즉시 반응 */
  .app-sidebar .nav-link {
    touch-action: manipulation;   /* 300ms tap delay 제거 */
    -webkit-tap-highlight-color: rgba(99,102,241,0.2);
    min-height: 48px;             /* 터치 타겟 확대 */
    display: flex;
    align-items: center;
  }

  /* Sidebar overlay — 배경 터치 완전 차단 */
  .sidebar-overlay {
    display: none;
    position: fixed; inset: 0;
    background: rgba(0,0,0,0.5); z-index: 1000;
    touch-action: none;       /* 오버레이 위 모든 제스처 차단 */
    -webkit-tap-highlight-color: transparent;
  }
  .sidebar-overlay.active { display: block !important; }

  /* Body scroll lock — 사이드바 열림 시 배경 완전 고정 */
  body.sidebar-open {
    overflow: hidden !important;
    position: fixed !important;
    width: 100vw !important;
    touch-action: none !important;   /* 배경 모든 제스처 차단 */
  }
  /* 사이드바는 body.sidebar-open 상태에서도 터치 가능해야 함 */
  body.sidebar-open .app-sidebar {
    touch-action: pan-y !important;
  }

  /* ══════════════════════════════════════════════════════════
     하단 네비 — 정확히 5개 노출, 나머지 스크롤
     ──────────────────────────────────────────────────────────
     핵심: width를 100vw로 고정하여 body 수평 오버플로우와
     무관하게 항상 뷰포트 기준 20%씩 계산
     ══════════════════════════════════════════════════════════ */
  .bottom-nav {
    display: flex !important;
    position: fixed; bottom: 0; left: 0;
    width: 100vw !important;          /* right:0 대신 100vw 고정! */
    background: var(--card-bg);
    border-top: 1px solid var(--border-color);
    z-index: 900;
    height: 60px;
    padding: 0;
    padding-bottom: env(safe-area-inset-bottom, 0);
  }
  .bottom-nav-scroll {
    display: flex;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    width: 100%;
    padding: 0.3rem 0;
    touch-action: pan-x;
    overscroll-behavior-x: contain;
  }
  .bottom-nav-scroll::-webkit-scrollbar { display: none; }
  .bottom-nav a {
    flex: 0 0 20%;                    /* 뷰포트의 20% = 정확히 5개 */
    display: flex; flex-direction: column;
    align-items: center; justify-content: center;
    gap: 0.15rem;
    padding: 0.3rem 0;
    font-size: 0.78rem;
    color: var(--text-muted); text-decoration: none;
    min-height: 44px;
    white-space: nowrap;
    touch-action: manipulation;       /* 즉시 탭 반응 */
  }
  .bottom-nav a.active {
    color: var(--accent);
    font-weight: 700;
  }
  .bottom-nav .nav-icon { font-size: 1.25rem; }

  /* ── 수평 스크롤 요소 — touch-action 격리 ── */
  .tab-bar,
  .analysis-tabs {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    touch-action: pan-x;
    overscroll-behavior-x: contain;
    max-width: 100%;
  }
  .tab-bar::-webkit-scrollbar,
  .analysis-tabs::-webkit-scrollbar { display: none; }

  /* Index cards — 수평 스크롤 */
  .index-cards-grid {
    display: flex !important;
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    gap: 0.5rem !important;
    padding-bottom: 0.3rem;
    touch-action: pan-x;
    overscroll-behavior-x: contain;
    max-width: 100%;
  }
  .index-cards-grid::-webkit-scrollbar { display: none; }
  .index-cards-grid .index-card {
    flex: 0 0 140px;
    min-width: 140px;
  }

  /* Index detail panel */
  .index-detail-panel.open { max-height: 90vh; }

  /* 종목명 셀 */
  .stock-name { font-size: 0.95rem; }
  .stock-code { font-size: 0.72rem; }

  /* filter-chip 그룹 수평 스크롤 (관심그룹 시장필터 등) */
  [id="marketFilter"] {
    overflow-x: auto;
    flex-wrap: nowrap !important;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    touch-action: pan-x;
  }
  [id="marketFilter"]::-webkit-scrollbar { display: none; }

  /* Landing page mobile */
  .hero-title { font-size: 2rem !important; }
  .hero-subtitle { font-size: 1rem !important; }
  .feature-grid { grid-template-columns: 1fr !important; }
}

/* ── Small mobile (<360px — iPhone SE, legacy devices) ── */
@media (max-width: 359px) {
  html { font-size: 15px; }  /* 13 → 15px */
  .grid-5 { grid-template-columns: 1fr; }
  .metric-value { font-size: 1.2rem; }
  .btn { padding: 0.45rem 0.8rem; font-size: 0.82rem; }

  #content { padding: 0.5rem; }
  .card { padding: 0.6rem; }
  .index-cards-grid { gap: 0.3rem !important; }
  .index-cards-grid .index-card { flex: 0 0 120px; min-width: 120px; }
}

/* ── Touch device feedback (no hover capability) ── */
@media (hover: none) {
  /* Active state replaces hover for touch devices */
  .card:active { border-color: var(--accent); }
  .card-link:active { transform: translateY(-1px); box-shadow: var(--shadow-sm); }
  .index-card:active { transform: scale(0.97); }
  .tab:active, .tab-item:active { opacity: 0.7; }
  .analysis-tab:active { opacity: 0.8; }
  .table tr:active td { background: var(--card-hover); }
  .btn:active:not(:disabled) { transform: scale(0.97); }
  .nav-link:active { background: var(--sidebar-active); }

  /* Remove hover-only effects that cause sticky state on touch */
  .card:hover { border-color: var(--border-color); }
  .card-link:hover { transform: none; box-shadow: none; }
  .index-card:hover { transform: none; box-shadow: none; }
}

/* ── Touch target minimum sizes ── */
@media (pointer: coarse) {
  .btn-icon {
    min-width: 44px;
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .btn-sm { min-height: 36px; padding: 0.4rem 0.8rem; }
  .search-item { min-height: 44px; }
  .radio-label { min-height: 36px; }
}

/* ── Desktop only (≥1025px) ── */
@media (min-width: 1025px) {
  .mobile-header { display: none !important; }
  .bottom-nav { display: none !important; }
  .sidebar-overlay { display: none !important; }
}
