/* ===================================================================
   mobile.css — 모바일 반응형 스타일
   구조:
   ├── 공통 모바일 유틸리티 (Safe Area, dvh)
   ├── @media (max-width: 767px)    ← 모바일
   ├── @media (min-width: 768px) and (max-width: 1023px)  ← 태블릿 (task 2.3)
   └── @media (min-width: 1024px)   ← 데스크톱 (task 2.3)
   =================================================================== */

/* ===== 공통 모바일 유틸리티 ===== */

/* Safe Area 변수 적용 — iOS notch/홈 인디케이터 대응 */
body {
  padding-top: env(safe-area-inset-top);
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}

/* PWA 가로 모드: 좌우 safe area 대칭 적용 (노치 비대칭 보정) */
@media (orientation: landscape) {
  body {
    padding-left: max(env(safe-area-inset-left), env(safe-area-inset-right));
    padding-right: max(env(safe-area-inset-left), env(safe-area-inset-right));
  }
}

/* 100dvh 적용 — iOS Safari 주소 바 높이 변화 대응 */
body {
  height: 100dvh;
}

#app {
  height: 100dvh;
}

/* ===== 모바일 브레이크포인트 (max-width: 767px) ===== */
@media (max-width: 767px) {

  /* --- 단일 컬럼 레이아웃 --- */
  .view {
    padding: 0.75rem;
    max-width: 100%;
  }

  /* 덱 그리드: 단일 컬럼 */
  .deck-grid,
  #study-deck-list,
  #deck-list {
    grid-template-columns: 1fr;
  }

  /* 카드 그리드: 단일 컬럼 */
  .card-grid {
    grid-template-columns: 1fr;
  }

  /* --- 플래시카드 전체 너비 + 3:2 비율 --- */
  .flashcard {
    width: 100%;
    max-width: calc(100vw - 2rem);
    height: auto;
    aspect-ratio: 3 / 2;
  }

  .flashcard-inner {
    width: 100%;
    height: 100%;
  }

  /* Fit/Full/Resize 모드도 모바일에서 전체 너비 */
  .flashcard.sizeFit {
    width: 100%;
    max-width: calc(100vw - 2rem);
    height: auto;
    aspect-ratio: 3 / 2;
  }

  .flashcard.sizeFull {
    width: 100%;
    max-width: calc(100vw - 2rem);
    height: auto;
    aspect-ratio: 3 / 2;
  }

  .flashcard.sizeResize {
    width: 100%;
    max-width: calc(100vw - 2rem);
    height: auto;
    aspect-ratio: 3 / 2;
  }

  /* --- 통계 그리드 단일 컬럼 --- */
  .stats-grid {
    grid-template-columns: 1fr;
  }

  /* --- 카드 관리 테이블: 가로 스크롤 --- */
  .card-table-scroll {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* 카드 레이아웃: 세로 배치 */
  .cards-layout {
    flex-direction: column;
  }

  .card-preview-panel {
    width: 100%;
    position: static;
  }

  /* --- 설정 패널 전체 화면 오버레이 --- */
  #settingsPanel {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    max-height: 100%;
    min-width: 100%;
    border-radius: 0;
    z-index: 200;
    transform: none;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    padding: 0;
    padding-top: env(safe-area-inset-top);
  }

  #settingsPanel.open {
    transform: none;
  }

  .settings-close-btn {
    display: inline-block;
  }

  /* 설정 타이틀 고정 헤더 */
  #settingsPanel .settingsTitle {
    flex-shrink: 0;
    padding: 0.8rem 1rem 0.5rem;
    background: var(--bgPrimary);
    border-bottom: 1px solid var(--cardBorder);
    margin: 0;
  }

  /* 설정 옵션 스크롤 영역 */
  #settingsPanel .settings-body {
    flex: 1;
    overflow-y: auto;
    padding: 1rem;
    padding-bottom: calc(4rem + env(safe-area-inset-bottom));
  }

  /* 스크롤 인디케이터 숨김 (모바일은 네이티브 스크롤) */
  #settingsPanel .settings-scroll-up,
  #settingsPanel .settings-scroll-down {
    display: none !important;
  }

  /* 학습 탭: 현황/히트맵 숨김 (통계 탭에서 확인) */
  #study-overview-header,
  #stats-overview,
  #heatmap-container {
    display: none !important;
  }

  /* 도움말: 데스크톱 전용 항목 숨김 */
  .help-desktop-only {
    display: none !important;
  }

  /* Platform-specific help content — mobile */
  .help-mobile-only { display: list-item !important; }

  /* 카드 관리 탭 숨김 (데스크톱에서 관리) */
  .mobile-hide-manage {
    display: none !important;
  }

  /* 통계 초기화 버튼 숨김 */
  #btn-reset-stats {
    display: none !important;
  }

  /* 사용자 버튼: 이메일 대신 아바타 아이콘 */
  #btn-user {
    font-size: 0 !important;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: var(--colorPrimary);
    display: flex !important;
    align-items: center;
    justify-content: center;
    padding: 0 !important;
    min-width: unset !important;
    min-height: unset !important;
  }
  #btn-user::before {
    content: '👤';
    font-size: 1rem;
  }

  /* --- 키보드 힌트 숨김 --- */
  #keyboard-hints,
  .keyboard-hints {
    display: none !important;
  }

  /* --- 카드 사이즈 컨트롤 숨김 (모바일은 자동 fit) --- */
  #cardSizeControl {
    display: none !important;
  }

  /* --- 퀴즈 컴팩트 레이아웃 --- */
  .quiz-question-text {
    padding: 0.8rem;
    margin-bottom: 0.8rem;
    font-size: 1rem;
    border-radius: 8px;
  }
  .quiz-choices {
    gap: 0.35rem;
  }
  .quiz-choice-btn {
    padding: 0.5rem 0.7rem;
    font-size: 0.85rem;
    border-radius: 6px;
  }
  .quiz-pass-area {
    margin-top: 0.3rem;
  }
  .quiz-feedback {
    margin-top: 0.3rem;
  }
  .quiz-hint-area {
    margin-top: 0.3rem;
  }

  /* --- 퀴즈 결과 컴팩트 레이아웃 --- */
  .quiz-result {
    margin: 0.5rem auto !important;
  }
  .quiz-result-title {
    font-size: 1.1rem !important;
    margin-bottom: 0.5rem !important;
  }
  .quiz-result-stats {
    padding: 0.5rem !important;
    margin-bottom: 0.5rem !important;
  }
  .quiz-result-row {
    padding: 0.25rem 0 !important;
    font-size: 0.82rem !important;
  }
  .quiz-result-actions {
    margin-top: 0.8rem !important;
    gap: 0.4rem;
  }
  .quiz-wrong-list {
    margin-top: 0.5rem !important;
  }
  .quiz-wrong-toggle {
    padding: 0.5rem 0.6rem;
    font-size: 0.82rem;
  }

  /* --- 섹션 헤더 조정 --- */
  .section-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.5rem;
  }

  /* 학습 뷰 헤더: 한 줄 가로 배치 */
  #view-review > .section-header {
    flex-direction: row !important;
    align-items: center !important;
    flex-wrap: nowrap !important;
    gap: 0.3rem !important;
    margin-bottom: 0.5rem;
  }
  #view-review > .section-header h2 {
    font-size: 0.85rem;
    white-space: nowrap;
    margin: 0;
  }
  #view-review > .section-header #btn-back-study,
  #view-review > .section-header #review-direction-toggle,
  #view-review > .section-header #tts-speak-btn,
  #view-review > .section-header .review-immersive-btn,
  #view-review > .section-header .immersive-exit-btn {
    font-size: 0.65rem;
    padding: 0.15rem 0.3rem;
    min-height: unset;
    min-width: unset;
    white-space: nowrap;
    line-height: 1.2;
  }
  #view-review > .section-header #review-progress {
    font-size: 0.75rem;
    white-space: nowrap;
  }
  #view-review > .section-header #review-limit-info {
    font-size: 0.65rem !important;
    white-space: nowrap;
  }
  #view-review > .section-header #review-direction-toggle {
    margin-left: auto;
  }
  }

  /* 퀴즈 뷰 헤더: 학습 뷰와 동일한 한 줄 가로 배치 */
  #view-quiz > .section-header {
    flex-direction: row !important;
    align-items: center !important;
    flex-wrap: nowrap !important;
    gap: 0.3rem !important;
    margin-bottom: 0.5rem;
  }
  #view-quiz > .section-header h2 {
    font-size: 0.85rem;
    white-space: nowrap;
    margin: 0;
  }
  #view-quiz > .section-header #btn-back-quiz {
    font-size: 0.7rem;
    padding: 0.2rem 0.4rem;
    min-height: unset;
    min-width: unset;
    white-space: nowrap;
  }
  #view-quiz > .section-header #quiz-progress {
    font-size: 0.75rem;
    white-space: nowrap;
    margin-left: auto;
  }

  /* 학습 카드 세로 중앙 배치 */
  #view-review {
    display: flex;
    flex-direction: column;
    height: calc(100dvh - 40px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  }
  #view-review #review-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
  }

  /* --- 모달 모바일 최적화 --- */
  .modal {
    min-width: auto;
    max-width: calc(100vw - 2rem);
    width: 100%;
  }

  /* --- 인증 박스 모바일 --- */
  .auth-box {
    margin: 2rem auto;
    padding: 1.5rem;
  }

  /* --- 히트맵 가로 스크롤 --- */
  #heatmap-container,
  .heatmap-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* --- 카드 사이즈 컨트롤 모바일 위치 --- */
  .cardSizeControl {
    bottom: 0.5rem;
    right: 0.5rem;
  }
}

/* ===== 하단 탭 바 — 기본 스타일 (모든 뷰포트) ===== */

/* 기본: 숨김 (태블릿/데스크톱) */
.mobile-tab-bar {
  display: none;
}

/* ===== 모바일에서 하단 탭 바 표시 + 상단 탭 바 숨김 ===== */
@media (max-width: 767px) {

  /* 상단 탭 바 숨김 */
  #tab-bar {
    display: none !important;
  }

  /* 하단 탭 바 표시 */
  .mobile-tab-bar {
    display: flex;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 100;
    min-height: 40px;
    padding-bottom: env(safe-area-inset-bottom);
    background: var(--bgSecondary);
    border-top: 1px solid var(--colorSecondary);
    box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.08);
    justify-content: space-around;
    align-items: stretch;
  }

  [data-theme="dark"] .mobile-tab-bar {
    box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.3);
  }

  /* 탭 버튼: 아이콘 + 라벨 세로 배치 */
  .mobile-tab-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    flex: 1;
    gap: 1px;
    padding: 4px 0;
    border: none;
    background: none;
    cursor: pointer;
    color: var(--colorPrimary);
    opacity: 0.4;
    font-family: var(--fontFamily);
    transition: color 0.2s, opacity 0.2s;
    -webkit-tap-highlight-color: transparent;
  }

  .mobile-tab-icon {
    font-size: 1rem;
    line-height: 1;
  }

  .mobile-tab-label {
    font-size: 0.6rem;
    font-weight: 600;
    line-height: 1;
    color: var(--colorPrimary);
  }

  /* 활성 탭 강조 */
  .mobile-tab-btn.active {
    opacity: 1;
    color: var(--colorPrimary);
  }

  .mobile-tab-btn.active .mobile-tab-label {
    font-weight: 700;
  }

  .mobile-tab-btn:focus-visible {
    outline: 3px solid var(--focusRing);
    outline-offset: -2px;
  }

  /* 메인 콘텐츠 하단 여백 — 탭 바와 겹침 방지 */
  #main-content {
    padding-bottom: calc(40px + env(safe-area-inset-bottom));
  }
}

/* ===== 모바일 터치 친화적 UI (task 2.3) ===== */
@media (max-width: 767px) {

  /* --- 터치 타겟 최소 44×44px --- */
  .btn,
  .btn-sm,
  a,
  select,
  .toolbarBtn {
    min-height: 44px;
    min-width: 44px;
  }

  /* 탭 버튼은 탭 바 높이에 맞춤 (별도 터치 영역) */
  .mobile-tab-btn {
    min-height: 40px;
    min-width: unset;
    background: transparent !important;
  }

  /* 체크박스/라디오: 시각적 크기는 유지, 터치 영역은 패딩으로 확보 */
  input[type="checkbox"],
  input[type="radio"] {
    min-height: unset;
    min-width: unset;
    width: 18px;
    height: 18px;
    padding: 0;
    margin: 0;
    cursor: pointer;
  }

  /* 체크박스를 감싸는 label의 터치 영역 확보 */
  label > input[type="checkbox"],
  label > input[type="radio"] {
    /* 부모 label에 flex 레이아웃 적용을 위한 마커 — JS에서 .label-input 클래스 추가 */
  }
  .label-input,
  .wp-check,
  .quiz-rule-check-card {
    min-height: 44px;
    display: flex;
    align-items: center;
    gap: 0.4rem;
  }

  /* --- 평가 버튼 48px 이상 --- */
  #rating-buttons .btn {
    min-height: 48px;
  }

  /* --- 인접 버튼 간격 8px --- */
  #rating-buttons {
    gap: 0.5rem; /* 8px */
  }

  .modal-actions {
    gap: 0.5rem;
  }

  .deck-item .deck-actions {
    gap: 0.5rem;
  }

  /* --- 모달 버튼 전체 너비 + 최소 높이 --- */
  .modal .btn,
  .modal .btn-sm {
    width: 100%;
    min-height: 44px;
  }

  .modal-actions {
    flex-direction: column;
  }
}

/* ===== 태블릿 브레이크포인트 (task 2.3) ===== */
@media (min-width: 768px) and (max-width: 1023px) {

  /* --- 뷰 패딩 1.5rem --- */
  .view {
    padding: 1.5rem;
  }

  /* --- 2컬럼 덱 그리드 --- */
  .deck-grid,
  #study-deck-list,
  #deck-list {
    grid-template-columns: repeat(2, 1fr);
  }

  /* --- 하단 탭 바 숨김 (기본 display:none 유지) --- */
  .mobile-tab-bar {
    display: none !important;
  }
}

/* 데스크톱에서도 하단 탭 바 확실히 숨김 */
@media (min-width: 1024px) {
  .mobile-tab-bar {
    display: none !important;
  }
}

/* ===== 터치 디바이스: 가로 모드에서도 모바일 UI 통일 ===== */
@media (hover: none) and (pointer: coarse) {
  #keyboard-hints,
  .keyboard-hints {
    display: none !important;
  }

  #cardSizeControl {
    display: none !important;
  }

  /* 상단 탭 바 숨김, 하단 탭 바 표시 (가로 모드 포함) */
  #tab-bar {
    display: none !important;
  }

  /* 카드 관리 탭 숨김 */
  .mobile-hide-manage {
    display: none !important;
  }

  /* 통계 초기화 버튼 숨김 */
  #btn-reset-stats {
    display: none !important;
  }

  /* 사용자 버튼: 이메일 대신 아바타 아이콘 */
  #btn-user {
    font-size: 0 !important;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: var(--colorPrimary);
    display: flex !important;
    align-items: center;
    justify-content: center;
    padding: 0 !important;
    min-width: unset !important;
    min-height: unset !important;
  }
  #btn-user::before {
    content: '👤';
    font-size: 1rem;
  }

  /* 학습 탭: 현황/히트맵 숨김 */
  #study-overview-header,
  #stats-overview,
  #heatmap-container {
    display: none !important;
  }

  /* 도움말: 데스크톱 전용 항목 숨김 */
  .help-desktop-only {
    display: none !important;
  }

  /* Platform-specific help content — mobile */
  .help-mobile-only { display: list-item !important; }

  /* 메인 콘텐츠 하단 여백 — 탭 바와 겹침 방지 (가로 모드 포함) */
  #main-content {
    padding-bottom: calc(44px + env(safe-area-inset-bottom));
  }

  .mobile-tab-bar {
    display: flex !important;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 100;
    min-height: 40px;
    padding-bottom: env(safe-area-inset-bottom);
    background: var(--bgSecondary);
    border-top: 1px solid var(--colorSecondary);
    box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.08);
    justify-content: space-around;
    align-items: stretch;
  }

  [data-theme="dark"] .mobile-tab-bar {
    box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.3);
  }

  /* 탭 버튼 배경 투명 + 크기 맞춤 */
  .mobile-tab-btn {
    background: transparent !important;
    min-height: 40px;
    min-width: unset;
    border: none;
    color: var(--colorPrimary);
    opacity: 0.4;
    font-family: var(--fontFamily);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 1px;
    padding: 4px 0;
    transition: color 0.2s, opacity 0.2s;
  }

  .mobile-tab-btn .mobile-tab-label {
    color: var(--colorPrimary);
    font-weight: 600;
    font-size: 0.6rem;
  }

  .mobile-tab-btn .mobile-tab-icon {
    font-size: 1rem;
  }

  .mobile-tab-btn.active {
    opacity: 1;
    color: var(--colorPrimary);
  }

  .mobile-tab-btn.active .mobile-tab-label {
    font-weight: 700;
  }

  /* 몰입 모드 버튼 표시 */
  .review-immersive-btn {
    display: inline-block !important;
    font-size: 0.75rem;
    padding: 0.15rem 0.4rem;
    min-height: unset;
    min-width: unset;
    opacity: 0.6;
  }

  /* 몰입 모드 활성 시 */
  body.immersive-mode {
    overflow: hidden !important;
  }

  body.immersive-mode #navbar,
  body.immersive-mode .mobile-tab-bar {
    display: none !important;
  }

  /* 집중모드에서 헤더: 모든 버튼 유지, 나가기 버튼만 추가 표시, 진입 버튼 숨김 */
  body.immersive-mode .section-header .immersive-exit-btn {
    display: inline-block !important;
    font-size: 0.65rem;
    padding: 0.15rem 0.3rem;
    min-height: unset;
    min-width: unset;
  }
  body.immersive-mode .section-header .review-immersive-btn {
    display: none !important;
  }
  body.immersive-mode .section-header {
    position: relative !important;
  }

  body.immersive-mode #view-review,
  body.immersive-mode #view-quiz {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    height: 100dvh;
    padding: calc(0.5rem + env(safe-area-inset-top)) 0.5rem 0.5rem;
    margin: 0;
    z-index: 250;
    background: var(--bgPrimary);
    display: flex;
    flex-direction: column;
  }

  body.immersive-mode #view-review #review-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    height: 100%;
    overflow: visible;
    padding: 0 0.5rem;
    position: relative;
  }

  /* 카드: sizeFit 높이를 몰입 모드에 맞게 오버라이드 */
  body.immersive-mode #view-review .flashcard.sizeFit {
    height: calc(100dvh - 6rem) !important;
    margin: 0 auto 0.5rem !important;
  }

  /* 세로 모드: 카드 높이 더 확대 */
  @media (orientation: portrait) {
    body.immersive-mode #view-review .flashcard.sizeFit {
      height: calc(100dvh - 6rem) !important;
    }
  }

  /* 평가 버튼 위로 올리기 */
  body.immersive-mode #view-review #rating-buttons {
    flex-shrink: 0;
    width: 95%;
    min-height: 2rem;
    align-items: center;
    gap: 0.25rem;
    padding: 0.1rem 0 0.2rem;
    margin-top: -0.5rem;
  }

  body.immersive-mode #view-review #rating-buttons .btn {
    min-height: 36px;
    padding: 0.2rem 0.4rem;
    font-size: 0.8rem;
  }

  body.immersive-mode #main-content {
    padding-bottom: 0;
  }

  /* 가로 모드: 카드 높이 축소 + 간격 최소화 */
  @media (orientation: landscape) {
    body.immersive-mode #view-review,
    body.immersive-mode #view-quiz {
      padding: calc(0.25rem + env(safe-area-inset-top)) 0.5rem 0.25rem 0.5rem;
    }

    body.immersive-mode #view-review .flashcard.sizeFit {
      height: calc(100dvh - 8rem - env(safe-area-inset-top)) !important;
      margin: 0 auto 0.2rem !important;
    }

    body.immersive-mode #view-review #review-area {
      padding: 0 0.5rem;
      gap: 0;
      align-items: center;
    }

    body.immersive-mode #view-review #rating-buttons {
      padding: 0;
      margin-top: 0;
    }
  }

  /* 퀴즈 몰입 모드: quiz-area가 남은 공간 채움 */
  body.immersive-mode #view-quiz #quiz-area {
    flex: 1;
    overflow-y: auto;
  }

  /* 몰입 모드 해제 버튼: 헤더 인라인 토글로 이동 — fixed 오버레이 제거 */
}

/* ===== 터치 디바이스 가로 모드: 퀴즈 결과 컴팩트 ===== */
@media (hover: none) and (pointer: coarse) and (orientation: landscape) {
  .quiz-result {
    margin: 0.3rem auto !important;
  }
  .quiz-result-title {
    font-size: 0.95rem !important;
    margin-bottom: 0.3rem !important;
  }
  .quiz-result-stats {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.3rem;
    padding: 0.4rem !important;
    margin-bottom: 0.3rem !important;
  }
  .quiz-result-row {
    padding: 0.2rem 0.4rem !important;
    font-size: 0.78rem !important;
    border: 1px solid rgba(128,128,128,0.15);
    border-radius: 4px;
    background: rgba(128,128,128,0.04);
  }
  .quiz-result-row-wide {
    grid-column: 1 / -1;
  }
  .quiz-result-actions {
    margin-top: 0.4rem !important;
    gap: 0.3rem;
  }
  .quiz-result-actions .btn,
  .quiz-result-actions .btn-sm {
    padding: 0.25rem 0.5rem;
    font-size: 0.78rem;
  }
  .quiz-wrong-list {
    margin-top: 0.3rem !important;
  }
  .quiz-wrong-toggle {
    padding: 0.4rem 0.5rem;
    font-size: 0.78rem;
  }
}
