/*
 * 모바일 / 세로 화면 레이아웃
 * 다른 스타일시트보다 뒤에 로드되어야 같은 선택자의 기본값을 덮어쓸 수 있다.
 */

/* 좁은 화면 공통: 사이드바가 화면을 넘지 않도록 */
@media (max-width: 900px) {
    .sidebar-drawer { width: min(88vw, 420px); }
}

/*
 * 세로(포트레이트) 화면
 * 상단: 큼직한 앨범 커버 + 곡 정보 + 진행바 + 재생 컨트롤
 * 하단: 현재 가사 한 줄만 표시
 */
@media (orientation: portrait) and (max-width: 1024px) {
    .viewport {
        flex-direction: column;
        justify-content: flex-start;
        align-items: center;
        gap: clamp(10px, 2vh, 20px);
        /* 상단 버튼(top-actions) 영역만큼 위쪽을 비운다 */
        padding:
            calc(max(16px, var(--safe-top)) + 58px)
            max(18px, env(safe-area-inset-right, 18px))
            max(18px, env(safe-area-inset-bottom, 18px))
            max(18px, env(safe-area-inset-left, 18px));
    }

    .top-actions {
        top: max(14px, var(--safe-top));
        right: max(16px, env(safe-area-inset-right, 16px));
        gap: 8px;
    }
    .top-actions .btn-ctrl { width: 40px; height: 40px; }
    .top-actions .btn-ctrl svg { width: 18px; height: 18px; }

    .activity-pill { top: max(66px, calc(var(--safe-top) + 66px)); }

    .left-panel {
        width: 100%;
        max-width: 520px;
        margin-left: 0;
        gap: clamp(8px, 1.6vh, 14px);
        flex-shrink: 0;
    }

    /* 커버는 화면 폭을 거의 채우되, 세로 공간의 일정 비율을 넘지 않게 */
    .cover-box {
        width: min(86vw, 42vh, 460px);
        width: min(86vw, 42dvh, 460px);
        max-width: none;
        border-radius: 16px;
    }

    .track-title { font-size: clamp(19px, 5.4vw, 26px); }
    .track-artist { font-size: clamp(14px, 3.8vw, 17px); }

    .progress-wrap { width: min(100%, 460px); }

    .control-bar { gap: clamp(12px, 5vw, 22px); }
    .control-bar .btn-ctrl { width: 48px; height: 48px; }
    .control-bar .btn-ctrl.play-btn { width: 62px; height: 62px; }
    .control-bar .btn-ctrl.play-btn svg { width: 26px; height: 26px; }

    /* 하단 가사 영역: 남은 공간을 모두 사용하고 한 줄을 가운데에 */
    .right-panel {
        flex: 1;
        width: 100%;
        max-width: 640px;
        height: auto;
        min-height: 0;
        margin-left: 0;
        padding-right: 0;
        align-items: center;
        justify-content: center;
        mask-image: none;
        -webkit-mask-image: none;
    }

    .lyrics-stream {
        height: 100%;
        padding: 0;
        align-items: center;
        justify-content: center;
        gap: 0;
        scroll-behavior: auto;
    }

    .lyric-item {
        width: 100%;
        max-width: 100%;
        align-items: center;
        text-align: center;
        transform-origin: center;
        gap: 6px;
        padding: 0 4px;
    }
    .lyric-item.active { transform: none; }

    /* 현재 가사(active)가 있으면 그 한 줄만 보여준다.
       싱크가 없는 가사처럼 active 줄이 없으면 기존처럼 목록으로 스크롤.
       가사 화면(lyrics-view-open)에서는 적용하지 않는다. */
    body:not(.lyrics-view-open) .right-panel { cursor: pointer; }
    /* 하단 한 줄 모드는 별도 표시 항목 설정(compact)을 쓴다 */
    body:not(.lyrics-view-open) .lyric-layers-full { display: none; }
    body:not(.lyrics-view-open) .lyric-layers-compact { display: flex; }
    body:not(.lyrics-view-open) .lyrics-stream:has(.lyric-item.active) {
        overflow-y: hidden;
        /* clip은 자동 스크롤(scrollTo)도 막아 넘친 가사의 윗부분이 밀려 올라가지 않게 한다 */
        overflow: clip;
        /* 가운데 정렬은 아래 margin:auto가 맡는다. 넘치면 첫 줄부터 보이도록 위쪽 기준 */
        justify-content: flex-start;
    }
    body:not(.lyrics-view-open) .lyrics-stream:has(.lyric-item.active) .lyric-item:not(.active) {
        display: none;
    }
    body:not(.lyrics-view-open) .lyrics-stream:has(.lyric-item.active) .lyric-item.active {
        margin: auto 0;
        animation: lyric-line-in 0.38s cubic-bezier(0.16, 1, 0.3, 1);
    }
    body:not(.lyrics-view-open) .lyrics-stream:not(:has(.lyric-item.active)) {
        justify-content: flex-start;
        padding: 12px 0 30vh;
        gap: 22px;
    }

    /* ---- 가사 화면: 가사만 전체 화면으로 ---- */
    /* left-panel은 설정(커버 표시)에서 inline style로 display를 지정하므로 !important 필요 */
    body.lyrics-view-open .left-panel,
    body.lyrics-view-open .top-actions { display: none !important; }

    body.lyrics-view-open .viewport {
        padding-top: max(16px, var(--safe-top));
        padding-bottom: 0;
    }
    body.lyrics-view-open .right-panel {
        height: 100%;
        mask-image: linear-gradient(to bottom, transparent 0%, black 14%, black 86%, transparent 100%);
        -webkit-mask-image: linear-gradient(to bottom, transparent 0%, black 14%, black 86%, transparent 100%);
        animation: lyrics-view-in 0.32s cubic-bezier(0.16, 1, 0.3, 1);
    }
    body.lyrics-view-open .lyrics-stream {
        justify-content: flex-start;
        overflow-y: auto;
        padding: 42vh 0;
        gap: calc(var(--lyrics-gap) * 0.8);
        scroll-behavior: smooth;
    }
    body.lyrics-view-open .lyric-item.active { transform: scale(1.04); }

    body.lyrics-view-open .lyrics-view-close {
        display: flex;
        position: fixed;
        top: max(14px, var(--safe-top));
        right: max(16px, env(safe-area-inset-right, 16px));
        z-index: 100;
        width: 42px;
        height: 42px;
    }
    .lyrics-view-close svg { width: 24px; height: 24px; }


    .loading-card { align-items: center; text-align: center; margin: auto 0; }
}

/*
 * 세로(모바일) 화면: 사이드바와 설정창을 전체 화면으로
 * - 사이드바(대기열/플레이리스트/검색)는 아래에서 위로 올라온다
 * - 설정창은 화면 전체를 채운다
 */
@media (orientation: portrait) and (max-width: 1024px) {
    .sidebar-drawer {
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        width: 100%;
        height: 100%;
        height: 100dvh;
        border-right: none;
        background: rgba(14, 15, 20, 0.94);
        transform: translateY(105%);
        transition: transform 0.38s cubic-bezier(0.16, 1, 0.3, 1), box-shadow 0.38s ease;
    }
    .sidebar-drawer.open {
        transform: translateY(0);
        box-shadow: 0 -20px 50px rgba(0, 0, 0, 0.6);
    }
    .sidebar-header {
        padding-left: max(16px, env(safe-area-inset-left, 16px));
        padding-right: max(16px, env(safe-area-inset-right, 16px));
    }
    .sidebar-body {
        padding-left: max(12px, env(safe-area-inset-left, 12px));
        padding-right: max(18px, env(safe-area-inset-right, 18px));
    }

    /* 설정창·가사 수동 검색창은 화면 전체를 채운다 */
    #modal-settings,
    #modal-search { padding: 0; }
    #modal-settings .modal-card,
    #modal-search .modal-card {
        width: 100%;
        max-width: none;
        height: 100%;
        height: 100dvh;
        max-height: none;
        border: none;
        border-radius: 0;
        background: rgba(14, 15, 20, 0.96);
        box-shadow: none;
    }
    #modal-settings.active .modal-card,
    #modal-search.active .modal-card {
        animation: sheet-up 0.38s cubic-bezier(0.16, 1, 0.3, 1);
    }
    #modal-settings .modal-header,
    #modal-search .modal-header {
        padding-top: max(16px, var(--safe-top));
        padding-left: max(18px, env(safe-area-inset-left, 18px));
        padding-right: max(18px, env(safe-area-inset-right, 18px));
    }
    #modal-settings .modal-body {
        padding-left: max(14px, env(safe-area-inset-left, 14px));
        padding-right: max(18px, env(safe-area-inset-right, 18px));
        padding-bottom: 20px;
    }
    #modal-settings .modal-footer {
        padding-bottom: max(14px, env(safe-area-inset-bottom, 14px));
    }

    /* iOS는 16px 미만 입력칸을 누르면 화면을 확대하므로 키운다 */
    .form-input { font-size: 16px; }

    /* 검색창: 입력칸은 위에 두고, 결과 목록이 남은 높이를 모두 쓴다 */
    #modal-search .modal-body {
        flex: 1;
        min-height: 0;
        overflow: hidden;
        padding-left: max(14px, env(safe-area-inset-left, 14px));
        padding-right: max(14px, env(safe-area-inset-right, 14px));
        padding-bottom: max(14px, env(safe-area-inset-bottom, 14px));
    }
    /* 결과 영역은 HTML에 inline으로 max-height가 있어 !important로 덮는다 */
    #modal-search #search-results-wrap {
        flex: 1;
        min-height: 0;
        max-height: none !important;
    }
    #modal-search #search-results {
        max-height: none !important;
        height: 100%;
    }
}

/* 모바일: 색상 팔레트는 아래에서 올라오는 시트 */
@media (orientation: portrait) and (max-width: 1024px) {
    #modal-color-picker { align-items: flex-end; padding: 0; }
    #modal-color-picker .color-picker-card {
        max-width: none;
        border-radius: 22px 22px 0 0;
        border-bottom: none;
        animation: sheet-up 0.32s cubic-bezier(0.16, 1, 0.3, 1);
    }
    #modal-color-picker .modal-footer {
        padding-bottom: max(14px, env(safe-area-inset-bottom, 14px));
    }
    #modal-color-picker .modal-footer .btn-action { flex: 1; justify-content: center; }
}

@keyframes sheet-up {
    from { transform: translateY(40px); opacity: 0; }
    to   { transform: none; opacity: 1; }
}

/* 세로지만 높이가 매우 낮은 화면(작은 폰 등)에서는 커버를 조금 줄인다 */
@media (orientation: portrait) and (max-width: 1024px) and (max-height: 640px) {
    .cover-box {
        width: min(80vw, 36vh, 460px);
        width: min(80vw, 36dvh, 460px);
    }
    .control-bar .btn-ctrl { width: 42px; height: 42px; }
    .control-bar .btn-ctrl.play-btn { width: 54px; height: 54px; }
}

/* 가사 화면 닫기 버튼은 가사 화면에서만 보인다 */
.lyrics-view-close { display: none; }

@keyframes lyrics-view-in {
    from { opacity: 0; transform: translateY(24px); }
    to   { opacity: 1; transform: none; }
}

@keyframes lyric-line-in {
    from { opacity: 0; transform: translateY(14px); }
    to   { opacity: 1; transform: none; }
}
