/*
 * 모바일 서브 목록만 — 연재 ‘전체 연재 시리즈’, 신작 ‘전체 신작 공개작’,
 * 영화·영상 ‘전체 영화·영상’.
 * 메인·버티컬·상단 추천 레일·태블릿·PC 는 이 파일을 읽지 않거나 선택자 밖에 둔다.
 *
 * 기준은 화면 이름이 아니라 목록이 실제로 가진 너비다.
 * 좌우 여백 16px·카드 간격 12px 일 때 카드 한 장이 190px 이상이면 2열.
 * (190 × 2) + 12 = 392px. 뷰포트 424px(392 + 16 + 16)부터 2열이다.
 * 그보다 좁으면 왼쪽 썸네일 + 오른쪽 정보.
 *
 * 정보 묶음 상자는 PC·2열에서 레이아웃에 끼어들지 않는다.
 * 연재 회차 배지도 좁은 목록에서만 보이고, 그 외에는 메타 줄의 회차를 쓴다.
 */

body[data-page="series"] #seriesCatalogGrid .catalog-info-cluster,
body[data-page="new-episodes"] #seriesCatalogGrid .catalog-info-cluster,
body[data-page="shorts"][data-catalog="films"] .catalog-info-cluster {
  display: contents;
}

body[data-page="series"] #seriesCatalogGrid .series-ep-badge {
  display: none;
}

@media (max-width: 768px) {
  body[data-page="series"] .section-space:has(> #seriesCatalogGrid),
  body[data-page="new-episodes"] .section-space:has(> #seriesCatalogGrid),
  body[data-page="shorts"][data-catalog="films"] #shortsCatalogSection {
    container-type: inline-size;
    container-name: mobile-catalog;
  }
}

/* ── 좁은 목록: 가로 한 줄 ── */
@container mobile-catalog (max-width: 391px) {
  body[data-page="series"] #seriesCatalogGrid,
  body[data-page="new-episodes"] #seriesCatalogGrid,
  body[data-page="shorts"][data-catalog="films"] #shortsCatalogGrid {
    display: flex !important;
    flex-direction: column !important;
    grid-template-columns: none !important;
    gap: 12px !important;
    width: 100% !important;
  }

  body[data-page="series"] #seriesCatalogGrid > .series-card,
  body[data-page="new-episodes"] #seriesCatalogGrid > .series-card,
  body[data-page="shorts"][data-catalog="films"] #shortsCatalogGrid > .shorts-catalog-card {
    display: flex !important;
    flex-direction: row !important;
    /* 썸네일은 16:9 크기를 유지한 채 글 묶음의 세로 가운데에 둔다. */
    align-items: center !important;
    gap: 12px !important;
    width: 100% !important;
    min-width: 0 !important;
    max-width: 100% !important;
    height: auto !important;
  }

  /* 썸네일은 목록 너비의 48%, 170px 을 넘기지 않는다. 16:9 와 레터박스는 기존 규칙을 유지한다. */
  body[data-page="series"] #seriesCatalogGrid > .series-card > .poster-wrap,
  body[data-page="new-episodes"] #seriesCatalogGrid > .series-card > .poster-wrap,
  body[data-page="shorts"][data-catalog="films"] #shortsCatalogGrid > .shorts-catalog-card > .poster-wrap {
    width: min(48%, 170px) !important;
    max-width: 170px !important;
    flex: 0 0 min(48%, 170px) !important;
    height: auto !important;
    aspect-ratio: 16 / 9 !important;
  }

  body[data-page="series"] #seriesCatalogGrid > .series-card > .series-body,
  body[data-page="new-episodes"] #seriesCatalogGrid > .series-card > .ne-catalog-card-body,
  body[data-page="shorts"][data-catalog="films"] #shortsCatalogGrid > .shorts-catalog-card > .shorts-card-body {
    flex: 1 1 auto !important;
    width: auto !important;
    min-width: 0 !important;
    max-width: none !important;
    /* 윗선에 붙지 않도록 상단만 2px 더 준다. */
    padding: 4px 10px 8px 0 !important;
    gap: 4px !important;
  }
}

/* ── 카드가 190px 이상인 모바일: 2열. 썸네일은 카드 전체 폭의 16:9 ── */
@container mobile-catalog (min-width: 392px) {
  body[data-page="series"] #seriesCatalogGrid,
  body[data-page="new-episodes"] #seriesCatalogGrid,
  body[data-page="shorts"][data-catalog="films"] #shortsCatalogGrid {
    display: grid !important;
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    gap: 12px !important;
    width: 100% !important;
  }

  body[data-page="series"] #seriesCatalogGrid > .series-card,
  body[data-page="new-episodes"] #seriesCatalogGrid > .series-card,
  body[data-page="shorts"][data-catalog="films"] #shortsCatalogGrid > .shorts-catalog-card {
    display: flex !important;
    flex-direction: column !important;
    align-items: stretch !important;
    width: 100% !important;
    min-width: 0 !important;
    max-width: 100% !important;
    height: auto !important;
    gap: 0 !important;
  }

  body[data-page="series"] #seriesCatalogGrid > .series-card > .poster-wrap,
  body[data-page="new-episodes"] #seriesCatalogGrid > .series-card > .poster-wrap,
  body[data-page="shorts"][data-catalog="films"] #shortsCatalogGrid > .shorts-catalog-card > .poster-wrap {
    width: 100% !important;
    max-width: none !important;
    flex: 0 0 auto !important;
    height: auto !important;
    aspect-ratio: 16 / 9 !important;
  }
}

/*
 * 두 배치 공통 — 제목은 최대 두 줄.
 * 신작은 회차 제목을 위로 올리고, 시리즈명은 그 아래 작은 글씨로 둔다.
 * 평점·조회수 칸에 내용이 없으면 줄을 만들지 않는다.
 */
@container mobile-catalog (min-width: 0px) {
  body[data-page="series"] #seriesCatalogGrid .series-card-title,
  body[data-page="series"] #seriesCatalogGrid .title-assist-wrapper > .series-card-title,
  body[data-page="shorts"][data-catalog="films"] #shortsCatalogGrid .shorts-card-title,
  body[data-page="shorts"][data-catalog="films"] #shortsCatalogGrid .title-assist-wrapper > .shorts-card-title {
    display: -webkit-box !important;
    -webkit-box-orient: vertical !important;
    -webkit-line-clamp: 2 !important;
    white-space: normal !important;
    overflow: hidden !important;
    text-overflow: ellipsis !important;
    line-height: 1.35 !important;
    max-height: calc(1.35em * 2) !important;
    width: auto !important;
    max-width: 100% !important;
    flex: 1 1 auto !important;
    min-width: 0 !important;
  }

  body[data-page="series"] #seriesCatalogGrid .title-assist-wrapper,
  body[data-page="shorts"][data-catalog="films"] #shortsCatalogGrid .title-assist-wrapper {
    display: flex !important;
    flex-direction: row !important;
    flex-wrap: nowrap !important;
    align-items: flex-start !important;
    width: 100% !important;
    max-width: 100% !important;
    min-width: 0 !important;
    margin: 0 !important;
  }

  /* 신작의 줄 순서·회차 번호 배치는 아래 좁은 목록 규칙에서만 바꾼다. */

  /* 연재 2열 — 시즌·회차와 평점·조회수를 같은 보조 줄에 둔다 */
  body[data-page="series"] #seriesCatalogGrid .series-card .series-meta-row,
  body[data-page="series"] #seriesCatalogGrid .series-card.style-wide-quad .series-meta-row {
    flex-direction: row !important;
    align-items: center !important;
    justify-content: space-between !important;
    gap: 6px !important;
    width: 100% !important;
    margin: 0 !important;
  }

  body[data-page="series"] #seriesCatalogGrid .series-card .series-meta-row > span:first-child {
    flex: 1 1 auto !important;
    min-width: 0 !important;
    overflow: hidden !important;
    text-overflow: ellipsis !important;
    white-space: nowrap !important;
  }

  body[data-page="series"] #seriesCatalogGrid .rating-views-combined.catalog-rating-stats,
  body[data-page="series"] #seriesCatalogGrid .series-card.style-wide-quad .rating-views-combined.catalog-rating-stats {
    flex: 0 0 auto !important;
    width: auto !important;
    max-width: 55% !important;
    margin: 0 !important;
    justify-content: flex-end !important;
  }

  /* 값이 없는 평점·조회수 칸은 빈 줄로 남기지 않는다 */
  body[data-page="series"] #seriesCatalogGrid .catalog-rating-stats:not(:has(*)),
  body[data-page="new-episodes"] #seriesCatalogGrid .ne-card-parent-stats:not(:has(*)),
  body[data-page="new-episodes"] #seriesCatalogGrid .catalog-rating-stats:not(:has(*)),
  body[data-page="shorts"][data-catalog="films"] #shortsCatalogGrid .shorts-rating-views:not(:has(*)),
  body[data-page="shorts"][data-catalog="films"] #shortsCatalogGrid .shorts-card-subline:not(:has(*)) {
    display: none !important;
    margin: 0 !important;
    padding: 0 !important;
    height: 0 !important;
    min-height: 0 !important;
  }
}

/*
 * 좁은 가로 목록 최종 보정.
 * 2열(목록 너비 392px 이상) 규칙보다 뒤에 두어, 같은 조건이면 이쪽이 이긴다.
 * 한 줄 제목에 빈 둘째 줄을 두지 않고, 제목·메타 묶음만 크리에이터 줄 위에서 가운데에 둔다.
 */
@container mobile-catalog (max-width: 391px) {
  /*
   * -webkit-box 줄수 제한은 한 줄 제목에도 빈 높이를 남긴다.
   * 일반 블록으로 두고 두 줄 높이만 잘라, 한 줄은 글자 높이만큼만 차지하게 한다.
   */
  body[data-page="series"] #seriesCatalogGrid .series-card .series-card-title,
  body[data-page="series"] #seriesCatalogGrid .series-card .title-assist-wrapper > .series-card-title,
  body[data-page="shorts"][data-catalog="films"] #shortsCatalogGrid .shorts-catalog-card .shorts-card-title,
  body[data-page="shorts"][data-catalog="films"] #shortsCatalogGrid .shorts-catalog-card .title-assist-wrapper > .shorts-card-title {
    display: block !important;
    -webkit-line-clamp: unset !important;
    -webkit-box-orient: unset !important;
    white-space: normal !important;
    overflow: hidden !important;
    height: auto !important;
    min-height: 0 !important;
    max-height: calc(1.35em * 2) !important;
    line-height: 1.35 !important;
    align-self: flex-start !important;
    flex: 0 0 auto !important;
  }

  body[data-page="series"] #seriesCatalogGrid > .series-card > .series-body,
  body[data-page="new-episodes"] #seriesCatalogGrid > .series-card > .ne-catalog-card-body,
  body[data-page="shorts"][data-catalog="films"] #shortsCatalogGrid > .shorts-catalog-card > .shorts-card-body {
    display: flex !important;
    flex-direction: column !important;
    align-self: stretch !important;
    justify-content: flex-start !important;
    gap: 0 !important;
  }

  /* 제목과 바로 아래 메타는 4px. 남는 높이 안에서 이 묶음만 세로 가운데. */
  body[data-page="series"] #seriesCatalogGrid .catalog-info-cluster,
  body[data-page="new-episodes"] #seriesCatalogGrid .catalog-info-cluster,
  body[data-page="shorts"][data-catalog="films"] #shortsCatalogGrid .catalog-info-cluster {
    display: flex !important;
    flex: 1 1 auto !important;
    flex-direction: column !important;
    justify-content: center !important;
    align-items: stretch !important;
    gap: 4px !important;
    min-width: 0 !important;
    min-height: 0 !important;
    width: 100% !important;
  }

  body[data-page="series"] #seriesCatalogGrid .series-card-footer,
  body[data-page="new-episodes"] #seriesCatalogGrid .ne-card-footer-row,
  body[data-page="shorts"][data-catalog="films"] #shortsCatalogGrid .shorts-card-footer {
    flex: 0 0 auto !important;
    width: 100% !important;
    margin-top: 0 !important;
  }

  /*
   * 신작 — 시리즈명 → 회차 제목(전체 폭) → S1·EP + 지표.
   * 크리에이터·플랫폼은 이 묶음 밖, 정보칸 하단에 둔다.
   * 회차 번호는 제목 왼쪽 칸이 아니라 지표 줄에 둔다.
   */
  body[data-page="new-episodes"] #seriesCatalogGrid .catalog-info-cluster:has(.ne-card-ep-prefix) {
    display: grid !important;
    grid-template-columns: minmax(0, 1fr) auto !important;
    grid-template-areas:
      "series series"
      "title title"
      "ep stats" !important;
    column-gap: 8px !important;
    row-gap: 4px !important;
    align-items: center !important;
    align-content: center !important;
  }

  body[data-page="new-episodes"] #seriesCatalogGrid .catalog-info-cluster:has(.ne-card-ep-prefix) .ne-card-parent-row,
  body[data-page="new-episodes"] #seriesCatalogGrid .catalog-info-cluster:has(.ne-card-ep-prefix) .ne-card-main-title {
    display: contents !important;
  }

  body[data-page="new-episodes"] #seriesCatalogGrid .ne-card-parent-line {
    grid-area: series !important;
    font-size: 0.7rem !important;
    font-weight: 700 !important;
    line-height: 1.3 !important;
    height: auto !important;
  }

  body[data-page="new-episodes"] #seriesCatalogGrid .ne-card-main-title .title-assist-wrapper,
  body[data-page="new-episodes"] #seriesCatalogGrid .ne-card-main-title > .ne-card-title-text {
    grid-area: title !important;
    width: 100% !important;
    max-width: 100% !important;
    min-width: 0 !important;
  }

  body[data-page="new-episodes"] #seriesCatalogGrid .ne-card-main-title > .ne-card-title-text,
  body[data-page="new-episodes"] #seriesCatalogGrid .ne-card-main-title .title-assist-wrapper .ne-card-title-text {
    display: block !important;
    -webkit-box-orient: unset !important;
    -webkit-line-clamp: unset !important;
    white-space: normal !important;
    overflow: hidden !important;
    height: auto !important;
    min-height: 0 !important;
    max-height: calc(1.35em * 2) !important;
    line-height: 1.35 !important;
    font-size: 14px !important;
    font-weight: 800 !important;
    flex: 0 0 auto !important;
    min-width: 0 !important;
  }

  body[data-page="new-episodes"] #seriesCatalogGrid .ne-card-main-title .title-assist-wrapper {
    display: flex !important;
    flex-direction: row !important;
    align-items: flex-start !important;
    -webkit-line-clamp: unset !important;
    max-height: none !important;
  }

  body[data-page="new-episodes"] #seriesCatalogGrid .ne-card-ep-prefix {
    grid-area: ep !important;
    font-size: 12px !important;
    font-weight: 700 !important;
    line-height: 1.3 !important;
    color: var(--accent-orange) !important;
    white-space: nowrap !important;
    padding: 0 !important;
  }

  body[data-page="new-episodes"] #seriesCatalogGrid .ne-card-parent-stats {
    grid-area: stats !important;
    justify-self: end !important;
    width: auto !important;
    margin: 0 !important;
  }

  /*
   * 연재 — 회차는 썸네일 오른쪽 아래 배지. 메타 줄에는 상태와 통계만 둔다.
   * 글자를 줄이거나 값을 자르지 않는다. 한 줄에 다 안 들어가면 통계 묶음 전체가 내려간다.
   */
  body[data-page="series"] #seriesCatalogGrid .series-card .genre-badge-absolute {
    top: 2px !important;
  }

  body[data-page="series"] #seriesCatalogGrid .series-ep-badge {
    display: inline-block !important;
    position: absolute !important;
    right: 6px !important;
    bottom: 6px !important;
    z-index: 4 !important;
    max-width: calc(100% - 12px) !important;
    padding: 2px 5px !important;
    border-radius: 3px !important;
    background: rgba(0, 0, 0, 0.72) !important;
    color: rgba(255, 255, 255, 0.9) !important;
    font-size: 10px !important;
    font-weight: 700 !important;
    line-height: 1.2 !important;
    letter-spacing: 0 !important;
    white-space: nowrap !important;
    pointer-events: none !important;
  }

  body[data-page="series"] #seriesCatalogGrid .series-meta-episode,
  body[data-page="series"] #seriesCatalogGrid .series-meta-dot {
    display: none !important;
  }

  body[data-page="series"] #seriesCatalogGrid .series-card .series-meta-row,
  body[data-page="series"] #seriesCatalogGrid .series-card.style-wide-quad .series-meta-row {
    flex-direction: row !important;
    flex-wrap: wrap !important;
    align-items: center !important;
    justify-content: flex-start !important;
    column-gap: 6px !important;
    row-gap: 2px !important;
  }

  body[data-page="series"] #seriesCatalogGrid .series-card .series-meta-row > span.series-meta-lead {
    flex: 0 0 auto !important;
    max-width: 100% !important;
    overflow: visible !important;
    text-overflow: clip !important;
    white-space: nowrap !important;
  }

  body[data-page="series"] #seriesCatalogGrid .series-card .rating-views-combined.catalog-rating-stats,
  body[data-page="series"] #seriesCatalogGrid .series-card.style-wide-quad .rating-views-combined.catalog-rating-stats {
    flex: 0 0 auto !important;
    width: auto !important;
    max-width: 100% !important;
    margin-left: auto !important;
    white-space: nowrap !important;
  }

  body[data-page="series"] #seriesCatalogGrid .rating-views-combined.catalog-rating-stats:not(:has(*)) {
    display: none !important;
    margin: 0 !important;
  }

  /*
   * 영화·영상 — ‘by ’가 폭을 먼저 먹어 이름만 말줄임되지 않게 한다.
   * 이름이 없는 칸은 접두사도 보이지 않는다.
   */
  body[data-page="shorts"][data-catalog="films"] #shortsCatalogGrid .shorts-card-footer > .creator-name-update {
    display: flex !important;
    align-items: baseline !important;
    justify-content: flex-start !important;
    min-width: 0 !important;
    margin: 0 !important;
    padding: 0 !important;
    overflow: visible !important;
    text-overflow: clip !important;
  }

  /* ‘by ’를 숨긴 뒤 이름이 제목과 같은 왼쪽 선에서 시작하게 한다. */
  body[data-page="shorts"][data-catalog="films"] #shortsCatalogGrid .shorts-card-footer .creator-profile-link {
    flex: 1 1 auto !important;
    min-width: 0 !important;
    max-width: none !important;
    margin: 0 !important;
    padding: 0 !important;
    overflow: hidden !important;
    text-overflow: ellipsis !important;
    white-space: nowrap !important;
  }

  body[data-page="shorts"][data-catalog="films"] #shortsCatalogGrid .shorts-card-footer .creator-by {
    display: none !important;
  }

  /* 이름이 없으면 함수가 빈 칸을 돌려준다. ‘by ’만 남지 않게 그 칸을 접는다. */
  body[data-page="shorts"][data-catalog="films"] #shortsCatalogGrid .shorts-card-footer > .creator-name-update:empty {
    display: none !important;
  }

  /*
   * 제목 오른쪽 🌐(한영 원제 마크).
   * 제목이 칸 폭을 다 쓰면 아이콘이 카드 밖으로 밀려 잘린다.
   * 아이콘 16px과 간격 6px을 남기고, 짧은 제목은 글자 바로 옆에 둔다.
   */
  body[data-page="series"] #seriesCatalogGrid .series-card .title-assist-wrapper,
  body[data-page="new-episodes"] #seriesCatalogGrid .ne-card-main-title .title-assist-wrapper,
  body[data-page="shorts"][data-catalog="films"] #shortsCatalogGrid .shorts-catalog-card .title-assist-wrapper {
    display: flex !important;
    flex-direction: row !important;
    flex-wrap: nowrap !important;
    align-items: flex-start !important;
    justify-content: flex-start !important;
    gap: 6px !important;
    width: 100% !important;
    max-width: 100% !important;
    min-width: 0 !important;
    overflow: visible !important;
  }

  body[data-page="series"] #seriesCatalogGrid .series-card .title-assist-wrapper > .series-card-title,
  body[data-page="shorts"][data-catalog="films"] #shortsCatalogGrid .shorts-catalog-card .title-assist-wrapper > .shorts-card-title,
  body[data-page="new-episodes"] #seriesCatalogGrid .ne-card-main-title .title-assist-wrapper .ne-card-title-text {
    flex: 0 1 auto !important;
    min-width: 0 !important;
    width: auto !important;
    max-width: calc(100% - 22px) !important;
  }

  body[data-page="series"] #seriesCatalogGrid .series-card .title-assist-wrapper .title-assist-icon,
  body[data-page="new-episodes"] #seriesCatalogGrid .ne-card-main-title .title-assist-wrapper .title-assist-icon,
  body[data-page="shorts"][data-catalog="films"] #shortsCatalogGrid .shorts-catalog-card .title-assist-wrapper .title-assist-icon {
    flex: 0 0 16px !important;
    width: 16px !important;
    min-width: 16px !important;
    height: 16px !important;
    margin: 1px 0 0 0 !important;
    align-self: flex-start !important;
    position: relative !important;
    z-index: 4 !important;
  }
}
