/* ==========================================================================
   MusiCK — Design tokens (bảng màu "Refined Neon" đã chốt)
   ========================================================================== */

:root {
    --bg-deep:   #0b1220;      /* nền ngoài cùng, tối nhất (desktop) */
    --bg-main:   #0f172a;      /* nền chính */
    --bg-card:   #1e293b;      /* thẻ, panel */
    --bg-hover:  #334155;

    --accent-purple: #a78bfa;
    --accent-cyan:   #22d3ee;
    --accent-grad: linear-gradient(90deg, var(--accent-purple), var(--accent-cyan));

    --text-primary:   #f8fafc;
    --text-secondary: #94a3b8;
    --danger: #f87171;

    --line: rgba(255, 255, 255, 0.06);
    --track: rgba(255, 255, 255, 0.12);

    --radius-sm:   12px;
    --radius-md:   16px;
    --radius-lg:   24px;
    --radius-full: 9999px;

    --speed: 0.3s;
    --ease: cubic-bezier(0.4, 0, 0.2, 1);

    /* Khoảng an toàn cho tai thỏ / thanh home trên iPhone */
    --safe-top:    env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

body {
    font-family: 'Inter', system-ui, -apple-system, sans-serif;
    background-color: var(--bg-main);
    color: var(--text-primary);
    overflow: hidden;                 /* scroll xảy ra trong .playlist-view */
    overscroll-behavior: none;
}

/* Chỉ ẩn khỏi màn hình, vẫn đọc được bằng trình đọc màn hình */
.visually-hidden {
    position: absolute;
    width: 1px; height: 1px;
    padding: 0; margin: -1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
    border: 0;
}

.hidden { display: none; }

/* Viền focus rõ ràng cho điều hướng bàn phím */
:focus-visible {
    outline: 2px solid var(--accent-cyan);
    outline-offset: 2px;
    border-radius: 6px;
}

.icon { width: 24px; height: 24px; display: block; flex: none; }
.icon-lg { width: 32px; height: 32px; }

::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--bg-hover); border-radius: 10px; }

/* ==========================================================================
   Layout
   ========================================================================== */

.app-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
    max-width: 600px;
    margin: 0 auto;
    position: relative;
}

/* Cột thư viện — chứa cả header và filters, nên khi container chuyển sang
   flex-row ở desktop, cả cụm này di chuyển thành một cột duy nhất. */
.library-pane {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;               /* cho phép con bên trong scroll được */
    padding-top: var(--safe-top);
}

/* ==========================================================================
   Header + tìm kiếm
   ========================================================================== */

.app-header { padding: 24px 20px 16px; }

.brand {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 7px;
    margin-bottom: 18px;
}

.logo {
    font-size: 32px;
    font-weight: 700;
    letter-spacing: -0.8px;
    line-height: 1.05;
    /* Dự phòng: nếu trình duyệt không cắt được nền theo hình chữ thì chữ vẫn
       hiện màu bình thường, thay vì trong suốt và biến mất. */
    color: var(--text-primary);
}

@supports (-webkit-background-clip: text) or (background-clip: text) {
    .logo {
        background: linear-gradient(96deg, #c4b5fd 0%, var(--accent-purple) 42%, var(--accent-cyan) 100%);
        -webkit-background-clip: text;
        background-clip: text;
        color: transparent;
    }
}

.logo-wave {
    display: block;
    opacity: 0.8;
    transform-origin: center;
    transition: opacity var(--speed);
}

/* Đang phát thì sóng thở nhẹ — đủ để nhận ra app đang chạy, không đủ để gây rối */
.brand.is-playing .logo-wave {
    animation: wave-breathe 2.6s ease-in-out infinite;
}

@keyframes wave-breathe {
    0%, 100% { transform: scaleY(0.85); opacity: 0.75; }
    50%      { transform: scaleY(1.5);  opacity: 1; }
}

.search-bar {
    display: flex;
    align-items: center;
    gap: 12px;
    background-color: var(--bg-card);
    padding: 12px 16px;
    border-radius: var(--radius-full);
    border: 1px solid var(--line);
    transition: border-color var(--speed);
}

.search-bar:focus-within { border-color: rgba(167, 139, 250, 0.5); }
.search-bar .icon { width: 20px; height: 20px; color: var(--text-secondary); }

.search-bar input {
    background: transparent;
    border: none;
    color: var(--text-primary);
    font: inherit;
    font-size: 16px;              /* < 16px sẽ khiến iOS tự zoom khi focus */
    width: 100%;
    outline: none;
}

.search-bar input::placeholder { color: var(--text-secondary); }
.search-bar input::-webkit-search-cancel-button { filter: invert(0.6); }

/* ==========================================================================
   Filter tabs
   ========================================================================== */

.filters {
    display: flex;
    gap: 10px;
    padding: 0 20px 16px;
    overflow-x: auto;
    scrollbar-width: none;
}
.filters::-webkit-scrollbar { display: none; }

.filter-btn {
    background: transparent;
    color: var(--text-secondary);
    border: 1px solid var(--track);
    padding: 9px 20px;
    border-radius: var(--radius-full);
    font: inherit;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    white-space: nowrap;
    transition: all var(--speed);
}

.filter-btn:hover { color: var(--text-primary); border-color: var(--text-secondary); }

.filter-btn.active {
    background-color: var(--accent-purple);
    color: var(--bg-main);
    border-color: var(--accent-purple);
    font-weight: 600;
    box-shadow: 0 4px 18px rgba(167, 139, 250, 0.35);
}

/* ==========================================================================
   Danh sách bài hát
   ========================================================================== */

.playlist-view {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 0 20px calc(112px + var(--safe-bottom));  /* chừa chỗ cho mini player */
}

.playlist { list-style: none; display: flex; flex-direction: column; gap: 12px; }

.song-item {
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 12px;
    background-color: var(--bg-card);
    border: 1px solid var(--line);
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: background-color 0.2s, transform 0.1s, border-color 0.2s;
}

.song-item:hover { background-color: #24334a; }
.song-item:active { transform: scale(0.985); }

/* Bài đang phát: viền sáng + quầng tím, đúng như mockup */
.song-item.playing {
    border-color: rgba(167, 139, 250, 0.55);
    background: linear-gradient(90deg, rgba(167, 139, 250, 0.14), rgba(34, 211, 238, 0.08));
    box-shadow: 0 0 22px rgba(167, 139, 250, 0.18);
}

.song-item.playing .song-info h4 { color: var(--accent-purple); }

.song-cover {
    width: 56px; height: 56px;
    border-radius: var(--radius-sm);
    object-fit: cover;
    background-color: var(--bg-main);
    flex: none;
}

.song-info { flex: 1; overflow: hidden; }

.song-info h4 {
    font-size: 16px;
    font-weight: 600;
    margin-bottom: 4px;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

.song-info p {
    font-size: 13px;
    color: var(--text-secondary);
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

.song-meta { display: flex; align-items: center; gap: 10px; flex: none; }
.song-duration { font-size: 13px; color: var(--text-secondary); font-variant-numeric: tabular-nums; }
.song-fav { color: var(--accent-purple); width: 16px; height: 16px; }
.song-offline { color: var(--accent-cyan); width: 16px; height: 16px; }

/* Cột sóng nhỏ cạnh bài đang phát, theo mockup mobile.
   Dựng bằng pseudo-element nên không phải thêm node vào mỗi item. */
.song-item.playing .song-meta::before {
    content: '';
    width: 14px;
    height: 14px;
    flex: none;
    background:
        linear-gradient(var(--accent-purple), var(--accent-purple)) 0    100% / 3px 45% no-repeat,
        linear-gradient(var(--accent-cyan),   var(--accent-cyan))   50%  100% / 3px 100% no-repeat,
        linear-gradient(var(--accent-purple), var(--accent-purple)) 100% 100% / 3px 65% no-repeat;
    animation: eq-mini 0.85s ease-in-out infinite alternate;
}

@keyframes eq-mini {
    from { background-size: 3px 30%, 3px 100%, 3px 45%; }
    to   { background-size: 3px 100%, 3px 40%, 3px 90%; }
}

.empty-state {
    text-align: center;
    color: var(--text-secondary);
    padding: 48px 20px;
    font-size: 15px;
}

/* ==========================================================================
   Mini player (chỉ mobile)
   ========================================================================== */

.mini-player {
    position: absolute;
    left: 16px; right: 16px;
    bottom: calc(16px + var(--safe-bottom));
    background-color: var(--bg-card);
    border: 1px solid var(--line);
    border-radius: var(--radius-lg);
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.55);
    overflow: hidden;
    cursor: pointer;
    transform: translateY(calc(100% + 24px));
    transition: transform var(--speed) var(--ease);
    z-index: 10;
}

.mini-player.visible { transform: translateY(0); }

.mini-progress-bar { width: 100%; height: 3px; background-color: var(--track); }

.mini-progress {
    height: 100%; width: 0%;
    background: var(--accent-grad);
    transition: width 0.15s linear;
}

.mini-player-content { display: flex; align-items: center; gap: 12px; padding: 12px 16px; }

.mini-cover {
    width: 44px; height: 44px;
    border-radius: 50%;
    object-fit: cover;
    background-color: var(--bg-main);
    flex: none;
}

.mini-info { flex: 1; overflow: hidden; }

.mini-info h3 {
    font-size: 15px; font-weight: 600; margin-bottom: 2px;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

.mini-info p {
    font-size: 13px; color: var(--text-secondary);
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

/* ==========================================================================
   Nút bấm
   ========================================================================== */

.control-btn, .icon-btn {
    background: transparent;
    border: none;
    color: var(--text-primary);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 8px;
    transition: color 0.2s, opacity 0.2s;
    /* Flex item mặc định co được. Thiếu dòng này, hàng điều khiển chật một chút
       là nút tròn bị bóp thành hình bầu dục. */
    flex: none;
}

.icon-btn.secondary { color: var(--text-secondary); }
.icon-btn.secondary:hover { color: var(--text-primary); }

/* Trạng thái bật cho shuffle / repeat / favorite */
.icon-btn.is-on { color: var(--accent-cyan); }
.icon-btn.is-on::after {
    content: '';
    position: absolute;
    transform: translateY(18px);
    width: 4px; height: 4px;
    border-radius: 50%;
    background: var(--accent-cyan);
}

#favorite-btn.is-on { color: var(--accent-purple); }
#favorite-btn.is-on .icon { fill: currentColor; }
#favorite-btn.is-on::after { display: none; }

/* Nhóm nút bên phải tiêu đề trình phát */
.header-actions { display: flex; align-items: center; gap: 4px; }

#download-btn { position: relative; }
#download-btn.is-on { color: var(--accent-cyan); }
#download-btn.is-on::after { display: none; }
#download-btn.is-busy { color: var(--accent-cyan); cursor: progress; }
#download-btn.is-busy .icon { animation: pulse 1.1s ease-in-out infinite; }
#download-btn.is-busy::after { display: none; }

/* Phần trăm nằm đè lên nút, chỉ hiện khi máy chủ gửi Content-Length */
.download-pct {
    position: absolute;
    bottom: -6px;
    left: 50%;
    transform: translateX(-50%);
    font-size: 9px;
    font-weight: 700;
    color: var(--accent-cyan);
    font-variant-numeric: tabular-nums;
    pointer-events: none;
}

@keyframes pulse {
    from { opacity: 1; }
    to   { opacity: 0.35; }
}

.play-btn-small {
    width: 40px; height: 40px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.1);
    flex: none;
    position: relative;
}

/* ---- Trạng thái đang tải ----
   Tải nhạc từ máy chủ ở xa mất một tới hai giây; không có chỉ báo thì cú bấm
   trông như bị trượt. */

.play-btn-large.is-loading .icon,
.play-btn-small.is-loading .icon {
    opacity: 0;
}

.play-btn-large.is-loading::after,
.play-btn-small.is-loading::after {
    content: '';
    position: absolute;
    width: 52%;
    height: 52%;
    border-radius: 50%;
    border: 3px solid currentColor;
    border-top-color: transparent;
    opacity: 0.85;
    animation: spin 0.8s linear infinite;
}

.play-btn-large.is-loading,
.play-btn-small.is-loading {
    cursor: progress;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

.play-btn-large {
    width: 72px; height: 72px;
    flex: none;
    position: relative;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--accent-purple), var(--accent-cyan));
    border: none;
    color: var(--bg-main);
    display: flex; align-items: center; justify-content: center;
    cursor: pointer;
    box-shadow: 0 10px 28px rgba(167, 139, 250, 0.45);
    transition: transform 0.2s;
}

.play-btn-large:active { transform: scale(0.95); }

/* ==========================================================================
   Trình phát đầy đủ
   ========================================================================== */

.full-player-modal {
    position: fixed;
    inset: 0;
    background-color: var(--bg-main);
    z-index: 100;
    display: flex;
    flex-direction: column;
    padding: calc(20px + var(--safe-top)) 24px calc(20px + var(--safe-bottom));
    transform: translateY(0);
    transition: transform 0.4s var(--ease), visibility 0s;
}

/* Ghi đè .hidden để giữ được hiệu ứng trượt thay vì biến mất đột ngột */
.full-player-modal.hidden {
    display: flex;
    visibility: hidden;
    transform: translateY(100%);
    transition: transform 0.4s var(--ease), visibility 0s 0.4s;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 24px;
    font-weight: 600;
    font-size: 14px;
    color: var(--text-secondary);
}

.album-art-container {
    flex: 1;
    min-height: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 28px;
}

/* Chiều cao dẫn dắt, chiều rộng chạy theo aspect-ratio. Nếu để width:100%
   thì max-height sẽ cắt bớt chiều cao và ảnh bìa mất dáng vuông. */
.full-cover {
    height: 100%;
    width: auto;
    max-width: 100%;
    max-height: 320px;
    aspect-ratio: 1 / 1;
    border-radius: var(--radius-lg);
    object-fit: cover;
    background-color: var(--bg-card);
    /* Quầng sáng tím→cyan quanh ảnh bìa như mockup */
    box-shadow:
        0 0 0 1px rgba(255, 255, 255, 0.12),
        0 0 60px rgba(167, 139, 250, 0.35),
        0 0 90px rgba(34, 211, 238, 0.18),
        0 20px 50px rgba(0, 0, 0, 0.5);
    transition: box-shadow var(--speed);
}

.song-info-container {
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    gap: 16px;
    margin-bottom: 20px;
}

.song-text { flex: 1; overflow: hidden; }

.song-text h2 {
    font-size: 24px; font-weight: 700; margin-bottom: 6px;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

.song-text p {
    font-size: 16px; color: var(--text-secondary);
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

/* ==========================================================================
   Visualizer (CSS — nhánh Web Audio sẽ đến ở giai đoạn 3)
   ========================================================================== */

.visualizer {
    display: flex;
    align-items: flex-end;
    gap: 4px;
    height: 26px;
    opacity: 0;
    transition: opacity var(--speed);
    flex: none;
}

.visualizer.active { opacity: 1; }

.visualizer .bar {
    display: block;
    width: 4px;
    border-radius: 2px;
    background: var(--accent-purple);
    transform-origin: bottom;
}

/* Nhánh CSS: bốn cột nhấp nhô bằng keyframes, lệch pha cho tự nhiên */
.visualizer.mode-css.active .bar { animation: eq 0.9s ease-in-out infinite alternate; }

.visualizer.mode-css .bar:nth-child(1) { height: 40%;  animation-delay: 0.10s; }
.visualizer.mode-css .bar:nth-child(2) { height: 80%;  animation-delay: 0.30s; background: var(--accent-cyan); }
.visualizer.mode-css .bar:nth-child(3) { height: 60%;  animation-delay: 0.50s; }
.visualizer.mode-css .bar:nth-child(4) { height: 100%; animation-delay: 0.20s; background: var(--accent-cyan); }

@keyframes eq {
    from { transform: scaleY(0.3); }
    to   { transform: scaleY(1); }
}

/* Nhánh Web Audio: chiều cao do AnalyserNode điều khiển qua scaleY.
   Transition ngắn làm mượt giữa các khung mà không tạo cảm giác trễ. */
.visualizer.mode-webaudio {
    gap: 2px;
    height: 30px;
}

.visualizer.mode-webaudio .bar {
    width: 3px;
    height: 100%;
    transform: scaleY(0.08);
    transition: transform 0.06s linear;
    animation: none;
}

/* ==========================================================================
   Thanh trượt (seek + volume)
   ========================================================================== */

.progress-container { margin-bottom: 20px; }

.time-info {
    display: flex;
    justify-content: space-between;
    font-size: 12px;
    color: var(--text-secondary);
    margin-top: 10px;
    font-variant-numeric: tabular-nums;
}

/* Phần đã phát được tô bằng lớp background thứ nhất, chiều rộng do biến
   --progress điều khiển. Cách này chạy đồng nhất trên WebKit lẫn Firefox,
   khác với ::-moz-range-progress vốn chỉ có ở Firefox. */
.range-fill {
    -webkit-appearance: none;
    appearance: none;
    width: 100%;
    height: 6px;
    border-radius: 3px;
    outline: none;
    cursor: pointer;
    background:
        var(--accent-grad) 0 / var(--progress, 0%) 100% no-repeat,
        var(--track);
}

.range-fill::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 16px; height: 16px;
    border-radius: 50%;
    background: var(--text-primary);
    border: none;
    cursor: pointer;
    box-shadow: 0 0 10px rgba(255, 255, 255, 0.6);
    transition: transform 0.15s;
}

.range-fill::-webkit-slider-thumb:active { transform: scale(1.25); }

.range-fill::-moz-range-thumb {
    width: 16px; height: 16px;
    border-radius: 50%;
    background: var(--text-primary);
    border: none;
    cursor: pointer;
    box-shadow: 0 0 10px rgba(255, 255, 255, 0.6);
}

.range-fill::-moz-range-track { background: transparent; }

/* ==========================================================================
   Hàng điều khiển
   ========================================================================== */

.controls-main {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
    margin-bottom: 16px;
}

.controls-main .icon-btn { position: relative; }

.volume-row {
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 0 8px;
}

.volume-row .range-fill { height: 4px; max-width: 220px; }

/* ==========================================================================
   Toast
   ========================================================================== */

.toast {
    position: fixed;
    left: 50%;
    bottom: calc(96px + var(--safe-bottom));
    transform: translateX(-50%);
    background: var(--bg-card);
    color: var(--text-primary);
    border: 1px solid var(--line);
    border-left: 3px solid var(--danger);
    padding: 12px 18px;
    border-radius: var(--radius-sm);
    font-size: 14px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
    z-index: 200;
    max-width: min(90vw, 420px);
}

/* ==========================================================================
   Desktop — hai panel dạng thẻ, đúng bố cục mockup

   Ngưỡng đặt ở 900px chứ không phải 768px: ở đúng 768px (iPad dọc), cột trái
   380px khiến cột phải chỉ còn ~246px, không đủ cho hàng điều khiển. Máy tính
   bảng dựng đứng dùng bố cục mobile vừa vặn hơn.
   ========================================================================== */

@media (min-width: 900px) {
    body { background-color: var(--bg-deep); }

    .app-container {
        max-width: 1400px;
        flex-direction: row;
        gap: 20px;
        padding: 20px;
    }

    /* Cột trái: logo + tìm kiếm + filters + danh sách, tất cả trong một thẻ.
       Cho phép co (flex-shrink 1) để cột phải luôn đủ chỗ cho hàng nút. */
    .library-pane {
        flex: 0 1 380px;
        min-width: 280px;
        background-color: var(--bg-main);
        border: 1px solid var(--line);
        border-radius: var(--radius-lg);
        overflow: hidden;
        padding-top: 0;
    }

    .app-header { padding: 24px 20px 16px; }

    .playlist-view { padding-bottom: 20px; }

    /* Cột phải: trình phát luôn hiển thị, không còn là modal */
    .full-player-modal,
    .full-player-modal.hidden {
        position: relative;
        inset: auto;
        flex: 1;
        min-width: 0;
        visibility: visible;
        transform: none;
        transition: none;
        background-color: var(--bg-main);
        border: 1px solid var(--line);
        border-radius: var(--radius-lg);
        padding: 32px 40px;
        justify-content: center;
    }

    /* Nút đóng chỉ có nghĩa ở mobile */
    #close-modal-btn { visibility: hidden; pointer-events: none; }

    .mini-player { display: none; }

    .full-cover { max-height: 300px; }
    .song-info-container { justify-content: center; text-align: center; }
    .song-text { flex: 0 1 auto; }
    /* Khoảng cách co theo bề rộng cột thay vì để các nút bị bóp méo */
    .controls-main { justify-content: center; gap: clamp(10px, 2.5vw, 28px); }
    .volume-row { justify-content: center; }
    .toast { bottom: 32px; }
}

@media (min-width: 1100px) {
    .library-pane { flex: 0 0 420px; }
    .full-cover { max-height: 340px; }
}

/* ==========================================================================
   Tôn trọng thiết lập giảm chuyển động của hệ điều hành
   ========================================================================== */

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
    .visualizer.active .bar { animation: none; }
    .brand.is-playing .logo-wave { animation: none; opacity: 1; }

    /* Spinner là ngoại lệ: nó truyền đạt trạng thái chứ không trang trí.
       Đứng yên thì mất hẳn ý nghĩa, nên chỉ quay chậm lại. */
    .play-btn-large.is-loading::after,
    .play-btn-small.is-loading::after {
        animation: spin 1.6s linear infinite !important;
    }
}
