/* ============================================================
   챌린지 화면 모바일 보정
   폭 639px 이하에서만 적용된다. 640px 이상에서는 이 파일의 규칙이
   전혀 적용되지 않으므로 PC·태블릿 화면은 이전과 완전히 동일하다.

   왜 필요했나 (390px 기준 실측)
     - 리더보드가 11열 table-fixed + w-full 이라 가로 스크롤이 생기지 않고
       열이 짜부라졌다. 방문자(시작) 열은 25px, 좌우 패딩 빼면 숫자 자리가 9px.
     - 달력 칸이 44px인데 날짜와 방문자·애드포스트를 한 줄에 옆으로 붙이고
       whitespace-nowrap 을 걸어 숫자가 잘렸다.
     - 상단 통계 카드 4개가 2열이라 라벨과 값이 좌우로 붙어 값이 줄바꿈됐다.

   주의: 이 파일은 dashboard.html 의 렌더 결과를 문자열로 세는 테스트
   (tests/test_adpost_render.py)가 있어 템플릿 인라인 <style> 로 넣으면 안 된다.
   visitor-display / adpost-display 같은 클래스명이 HTML 본문에 추가로 등장해
   칸 수 카운트가 어긋난다.
   ============================================================ */

@media (max-width: 639px) {

  /* ── ① 리더보드: 11열 표를 사람당 작은 카드 하나로 접는다 ──
     마크업은 그대로 두고 표시만 바꾼다. 데이터를 만드는 곳이 한 군데라
     PC와 모바일 숫자가 어긋날 수 없다. 열을 숨기지 않으므로 모든 값이 보인다.

     10-21 개편: 전에는 값마다 한 줄(라벨 왼쪽·값 오른쪽)이라 카드 하나가
     화면을 다 먹어 상위권이 한눈에 안 들어왔다. 지금은
       1줄  순위 · 사진 · 닉네임 · 점수
       2줄  주제 / 방문자(시작) / 방문자(현재)      ← 라벨 위·값 아래, 3칸
       3줄  누적 방문자 / 애드포스트 / 누적 애드포스트
       4줄  완료일 · 진행률 막대
     로 접어 390px 화면에 4~5명이 들어온다. */
  .lb-table { table-layout: auto !important; }
  .lb-table colgroup,
  .lb-table thead { display: none; }
  .lb-table,
  .lb-table tbody { display: block; width: 100%; }

  .lb-table tr {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    background: #fff;
    border: 1px solid #e5e7eb;
    border-radius: 12px;
    margin-bottom: 8px;
    padding: 2px 0 4px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, .04);
  }

  /* 기본 칸 = 지표 칸. 라벨 위·값 아래로 세로 쌓고 폭은 3분의 1 */
  .lb-table td {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    justify-content: center;
    gap: 1px;
    flex: 0 0 33.333%;
    min-width: 0;
    padding: 3px 10px !important;
    text-align: left;
    border-top: 0;
    font-size: 12px;
    line-height: 1.25;
    white-space: normal;
  }
  .lb-table td::before {
    content: attr(data-label);
    font-size: 10px;
    font-weight: 500;
    color: #9ca3af;
    white-space: nowrap;
  }
  .lb-table td > span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 100%; }

  /* 첫 줄: 순위 · 닉네임 · 점수 — 가로 한 줄, 라벨 없음 */
  .lb-table td.lb-rank,
  .lb-table td.lb-nick,
  .lb-table td:nth-child(3) {
    flex-direction: row;
    align-items: center;
    padding-top: 6px !important;
    padding-bottom: 4px !important;
  }
  .lb-table td.lb-rank::before,
  .lb-table td.lb-nick::before,
  .lb-table td:nth-child(3)::before { content: none; }
  .lb-table td.lb-rank {
    flex: 0 0 auto;
    font-size: 16px;
    font-weight: 700;
    padding-right: 2px !important;
  }
  /* 닉네임 기준폭 = 한 줄 폭 − 순위·점수 몫. 순위(≤40px)+닉네임+점수(≤80px)가
     딱 한 줄을 채워 다음 칸(주제)이 반드시 둘째 줄로 내려간다.
     flex 1 1 0 으로 두면 주제 칸이 첫 줄 끝에 끼어들었다(10-21 실측). */
  .lb-table td.lb-nick {
    flex: 1 1 calc(100% - 150px);
    min-width: 0;
    font-size: 14px;
    font-weight: 600;
    padding-left: 4px !important;
  }
  .lb-table td.lb-nick > div { justify-content: flex-start !important; width: 100%; }
  .lb-table td.lb-nick img,
  .lb-table td.lb-nick > div > div { width: 28px; height: 28px; flex: 0 0 auto; }
  .lb-table td.lb-nick a,
  .lb-table td.lb-nick > div > span {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .lb-table td:nth-child(3) { flex: 0 0 auto; }
  .lb-table td:nth-child(3) > span { font-size: 12px; padding: 2px 8px; }

  /* 지표 줄 위에 얇은 선 */
  .lb-table td:nth-child(4),
  .lb-table td:nth-child(5),
  .lb-table td:nth-child(6) { border-top: 1px solid #f3f4f6; padding-top: 5px !important; }

  /* 마지막 줄: 완료일 · 진행률 막대 */
  .lb-table td:nth-child(10) { flex: 0 0 auto; flex-direction: row; gap: 5px; align-items: baseline; }
  .lb-table td.lb-prog {
    flex: 1 1 0;
    flex-direction: row;
    align-items: center;
    gap: 6px;
  }
  .lb-table td.lb-prog > div { flex: 1 1 auto; justify-content: flex-end; }
  .lb-table td.lb-prog .w-12 { width: auto; flex: 1 1 auto; max-width: 160px; }

  /* 좁은 칸에 걸려 있던 nowrap 을 풀어 값이 잘리지 않게 한다 */
  .lb-table td .whitespace-nowrap { white-space: normal; }

  /* 카드 모드에서는 표를 감싼 가로 스크롤 상자가 필요 없다 */
  .lb-scroll { overflow-x: visible !important; }

  /* 머리글 오른쪽 참가자 수가 세로로 쪼개지지 않게 */
  #may-section > div:first-child > div:last-child { white-space: nowrap; margin-left: 10px; }
  #may-section > div:first-child { padding: 14px 16px !important; }
  #may-section > div:first-child h2 { font-size: 17px; line-height: 1.3; }

  /* ── ② 달력 칸: 날짜 아래로 지표를 쌓아 숫자가 다 보이게 한다 ── */
  .cal-grid { gap: 4px !important; }

  .day-cell-head {
    flex-direction: column !important;
    align-items: flex-start !important;
    gap: 1px;
    padding: 3px 4px !important;
  }
  .day-cell-head > span:first-child { font-size: 14px !important; line-height: 1.1; }
  .day-cell-head > div {
    width: 100%;
    flex-direction: column !important;
    align-items: flex-start !important;
    gap: 0 !important;
  }
  .day-cell-head .flex-col { width: 100%; align-items: flex-start !important; }
  .day-cell-head .visitor-display,
  .day-cell-head .adpost-display {
    font-size: 10px !important;
    white-space: normal !important;
    word-break: break-all;
    line-height: 1.2;
  }
  /* 아이콘과 '명' 단위가 폭을 많이 먹는다 — 숫자를 살리려고 모바일에서만 감춘다 */
  .day-cell-head .visitor-display i,
  .day-cell-head .adpost-display i { display: none; }
  .day-cell-head .visitor-display .unit { display: none; }

  /* 44px 칸에서 URL 주소는 읽을 수 없다. 날짜를 눌러 뜨는 창에 전체 목록이 있으므로
     칸에서는 글자 대신 링크 아이콘만 남긴다. */
  .calendar-day-cell .url-container {
    display: flex;
    flex-wrap: wrap;
    gap: 3px;
    max-height: none !important;
    padding: 3px 4px !important;
  }
  .calendar-day-cell .url-container a { font-size: 0; padding: 0; }
  .calendar-day-cell .url-container a i { font-size: 11px; margin: 0 !important; }
  .calendar-day-cell .url-container p { font-size: 9px !important; margin-top: 0 !important; }
  .calendar-day-cell { min-height: 92px; }
  .calendar-day-cell .count-badge { font-size: 10px; padding: 1px 4px; }

  /* ── ③ 상단 통계 카드 4개: 라벨 위 · 값 아래로 쌓는다 ── */
  .stat-cards > div {
    flex-direction: column !important;
    align-items: flex-start !important;
    gap: 1px;
    padding: 8px 10px !important;
  }
  .stat-cards > div > span:first-child { font-size: 11px; }
  .stat-cards > div > span:last-child { font-size: 15px; }
}
