/* =========================================================
   ilrbsm — design tokens
   ========================================================= */
:root {
    color-scheme: light;

    /* Nền & bề mặt */
    --bg: #FFFFFF;
    --bg-soft: #FFFAFC;
    --surface: #FFF3F8;
    --surface-hover: #FFE7F1;
    --surface-active: #FFDBEB;

    /* Thương hiệu */
    --accent: #FF6FA5;
    --accent-strong: #F0568F;
    --accent-soft: rgba(255, 111, 165, 0.12);
    --on-accent: #FFFFFF;

    /* Chữ */
    --text: #1C1111;
    --text-2: #5C4B4B;
    --muted: #8A7B7B;

    /* Viền & hiệu ứng */
    --border: #F3D9E6;
    --border-strong: #EABFD5;
    --ring: 0 0 0 3px rgba(255, 111, 165, 0.35);
    --shadow-sm: 0 1px 2px rgba(28, 17, 17, 0.05);
    --shadow-md: 0 8px 24px -12px rgba(240, 86, 143, 0.45), 0 2px 6px rgba(28, 17, 17, 0.05);
    --shadow-lg: 0 28px 60px -20px rgba(28, 17, 17, 0.28);

    /* Bo góc */
    --radius-card: 14px;
    --radius-btn: 10px;
    --radius-pill: 999px;

    /* Nhịp & kích thước */
    --page-pad: 24px;
    --topbar-h: 64px;
    --ease: cubic-bezier(0.22, 1, 0.36, 1);
    --dur: 0.22s;

    /* Vùng an toàn (tai thỏ / thanh home) */
    --safe-l: env(safe-area-inset-left, 0px);
    --safe-r: env(safe-area-inset-right, 0px);
    --safe-t: env(safe-area-inset-top, 0px);
    --safe-b: env(safe-area-inset-bottom, 0px);
}

/* =========================================================
   RESET
   ========================================================= */
*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; scroll-behavior: smooth; }

body {
    font-family: 'Be Vietnam Pro', -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Helvetica Neue', Arial, sans-serif;
    font-size: 15px;
    font-weight: 400;
    line-height: 1.5;
    background-color: var(--bg);
    color: var(--text);
    min-height: 100vh;
    min-height: 100dvh;
    overflow-x: hidden;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    -webkit-tap-highlight-color: transparent;
}

button, input, select, textarea { font: inherit; color: inherit; }
button { background: none; border: none; cursor: pointer; -webkit-tap-highlight-color: transparent; }
img, video { display: block; max-width: 100%; }
::selection { background: var(--accent); color: var(--on-accent); }
[hidden] { display: none !important; }

/* Focus rõ ràng cho bàn phím, không làm phiền khi bấm chuột */
:focus-visible { outline: none; box-shadow: var(--ring); }
/* Phần tử không có bo góc riêng thì dùng bo góc mặc định của nút */
a:focus-visible, input:focus-visible { border-radius: var(--radius-btn); }

/* Thanh cuộn mảnh, hợp tông */
* { scrollbar-width: thin; scrollbar-color: var(--border-strong) transparent; }
*::-webkit-scrollbar { width: 8px; height: 8px; }
*::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: var(--radius-pill); }
*::-webkit-scrollbar-track { background: transparent; }

/* =========================================================
   TOP BAR
   ========================================================= */
.topbar {
    position: sticky;
    top: 0;
    z-index: 100;
    min-height: var(--topbar-h);
    padding: 0 calc(var(--page-pad) + var(--safe-r)) 0 calc(var(--page-pad) + var(--safe-l));
    padding-top: var(--safe-t);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    background-color: rgba(255, 255, 255, 0.82);
    backdrop-filter: saturate(180%) blur(14px);
    -webkit-backdrop-filter: saturate(180%) blur(14px);
    border-bottom: 1px solid var(--border);
}

.topbar-inner {
    width: 100%;
    max-width: 1280px;
    margin: 0 auto;
    min-height: var(--topbar-h);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
}

.logo {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    font-size: 21px;
    font-weight: 700;
    line-height: 1;
    color: var(--accent-strong);
    letter-spacing: -0.4px;
    user-select: none;
    flex-shrink: 0;
}


/* =========================================================
   Ô TÌM KIẾM
   ========================================================= */
.search {
    position: relative;
    flex: 0 1 340px;   /* chỉ dùng ở hàng ngang */
    min-width: 0;
    max-width: 100%;
}
.search-icon {
    position: absolute;
    left: 14px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 13px;
    color: var(--muted);
    pointer-events: none;
    transition: color var(--dur) var(--ease);
}
.search-input {
    width: 100%;
    height: 44px;
    padding: 0 40px 0 38px;
    font-size: 15px;
    font-weight: 400;
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: var(--radius-pill);
    background-color: var(--surface);
    outline: none;
    transition: border-color var(--dur) var(--ease), background-color var(--dur) var(--ease), box-shadow var(--dur) var(--ease);
    -webkit-appearance: none;
    appearance: none;
}
.search-input::placeholder { color: var(--muted); }
.search-input:hover { background-color: var(--surface-hover); border-color: var(--border-strong); }
.search-input:focus {
    border-color: var(--accent);
    background-color: var(--bg);
    box-shadow: var(--ring);
}
.search:focus-within .search-icon { color: var(--accent-strong); }
.search-input::-webkit-search-decoration,
.search-input::-webkit-search-cancel-button { -webkit-appearance: none; }

/* Nút xoá nội dung tìm kiếm */
.search-clear {
    position: absolute;
    right: 6px;
    top: 50%;
    transform: translateY(-50%);
    width: 32px;
    height: 32px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    color: var(--muted);
    border-radius: 50%;
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--dur) var(--ease), color var(--dur) var(--ease), background-color var(--dur) var(--ease);
}
.search.has-value .search-clear { opacity: 1; pointer-events: auto; }
.search-clear:hover { background-color: var(--surface-active); color: var(--accent-strong); }

/* =========================================================
   NÚT — hệ thống chung
   ========================================================= */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    height: 44px;
    padding: 0 20px;
    font-size: 15px;
    font-weight: 600;
    line-height: 1;
    border-radius: var(--radius-btn);
    border: 1px solid transparent;
    cursor: pointer;
    user-select: none;
    white-space: nowrap;
    transition: background-color var(--dur) var(--ease), border-color var(--dur) var(--ease),
                color var(--dur) var(--ease), box-shadow var(--dur) var(--ease), transform 0.12s var(--ease);
}
.btn:active { transform: translateY(1px) scale(0.985); }

.btn-primary {
    background-color: var(--accent);
    color: var(--on-accent);
    box-shadow: var(--shadow-md);
}
.btn-primary:hover { background-color: var(--accent-strong); }

.btn-ghost {
    background-color: var(--surface);
    color: var(--text);
    border-color: var(--border);
}
.btn-ghost:hover { background-color: var(--surface-hover); border-color: var(--accent); color: var(--accent-strong); }

/* Nút tròn chỉ có icon (quay lại) */
.btn-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 42px;
    height: 42px;
    font-size: 16px;
    color: var(--text);
    background-color: var(--surface);
    border: 1px solid var(--border);
    border-radius: 50%;
    cursor: pointer;
    flex-shrink: 0;
    transition: background-color var(--dur) var(--ease), border-color var(--dur) var(--ease),
                color var(--dur) var(--ease), box-shadow var(--dur) var(--ease), transform 0.12s var(--ease);
}
.btn-icon:hover {
    background-color: var(--surface-hover);
    border-color: var(--accent);
    color: var(--accent-strong);
    box-shadow: var(--shadow-sm);
}
.btn-icon:active { transform: scale(0.94); }

/* =========================================================
   PAGE
   ========================================================= */
.page {
    max-width: 1280px;
    margin: 0 auto;
    padding: 24px calc(var(--page-pad) + var(--safe-r)) calc(56px + var(--safe-b)) calc(var(--page-pad) + var(--safe-l));
}

.page-title {
    font-size: clamp(20px, 1.2rem + 0.6vw, 26px);
    font-weight: 700;
    line-height: 1.25;
    letter-spacing: -0.4px;
}

.home-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 16px;
    margin-bottom: 22px;
}

/* =========================================================
   LƯỚI POSTER PHIM
   ========================================================= */
.movie-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(168px, 1fr));
    gap: 24px 18px;
}

.movie-card {
    display: block;
    width: 100%;
    text-align: left;
    background: none;
    border: none;
    padding: 0;
    cursor: pointer;
    border-radius: var(--radius-card);
}
.movie-card:focus-visible { box-shadow: none; }
.movie-card:focus-visible .movie-poster { border-color: var(--accent); box-shadow: var(--ring); }

.movie-poster {
    position: relative;
    width: 100%;
    aspect-ratio: 2 / 3;
    border-radius: var(--radius-card);
    overflow: hidden;
    background-color: var(--surface);
    border: 1px solid var(--border);
    box-shadow: var(--shadow-sm);
    transition: transform var(--dur) var(--ease), border-color var(--dur) var(--ease), box-shadow var(--dur) var(--ease);
}
.movie-poster img { width: 100%; height: 100%; object-fit: cover; }

/* Lớp phủ + nút play khi rê chuột */
.movie-poster::after {
    content: "";
    position: absolute;
    inset: 0;
    background: linear-gradient(to top, rgba(28, 17, 17, 0.45), rgba(28, 17, 17, 0) 45%);
    opacity: 0;
    transition: opacity var(--dur) var(--ease);
}
.poster-play {
    position: absolute;
    left: 50%;
    top: 50%;
    z-index: 2;
    width: 46px;
    height: 46px;
    display: grid;
    place-items: center;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.94);
    color: var(--accent-strong);
    font-size: 15px;
    padding-left: 3px;
    transform: translate(-50%, -50%) scale(0.7);
    opacity: 0;
    box-shadow: var(--shadow-md);
    transition: opacity var(--dur) var(--ease), transform var(--dur) var(--ease);
}
.poster-fallback {
    position: absolute;
    inset: 0;
    display: grid;
    place-items: center;
    font-size: 12px;
    font-weight: 600;
    letter-spacing: 0.08em;
    color: var(--muted);
    background: var(--surface);
}

@media (hover: hover) {
    .movie-card:hover .movie-poster {
        transform: translateY(-4px);
        border-color: var(--accent);
        box-shadow: var(--shadow-md);
    }
    .movie-card:hover .movie-poster::after { opacity: 1; }
    .movie-card:hover .poster-play { opacity: 1; transform: translate(-50%, -50%) scale(1); }
    .movie-card:hover .movie-name { color: var(--accent-strong); }
}
.movie-card:active .movie-poster { transform: scale(0.985); }

/* Nhãn "Đang xem tập N" */
.poster-badge {
    position: absolute;
    left: 8px;
    bottom: 8px;
    z-index: 2;
    max-width: calc(100% - 16px);
    padding: 5px 10px;
    font-size: 11px;
    font-weight: 600;
    line-height: 1;
    color: var(--on-accent);
    background: var(--accent-strong);
    border-radius: var(--radius-pill);
    box-shadow: var(--shadow-sm);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.movie-name {
    margin-top: 10px;
    font-size: 15px;
    font-weight: 600;
    line-height: 1.35;
    color: var(--text);
    transition: color var(--dur) var(--ease);
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}
.movie-year { margin-top: 3px; font-size: 13px; font-weight: 400; color: var(--muted); }

.empty-state {
    grid-column: 1 / -1;
    padding: 48px 20px;
    text-align: center;
    font-size: 15px;
    color: var(--muted);
    background: var(--bg-soft);
    border: 1px dashed var(--border-strong);
    border-radius: var(--radius-card);
}

/* =========================================================
   TRANG XEM PHIM
   ========================================================= */
.watch-header {
    display: flex;
    align-items: center;
    gap: 14px;
    margin-bottom: 20px;
    min-width: 0;
}
.movie-title {
    font-size: clamp(19px, 1.1rem + 0.6vw, 26px);
    font-weight: 700;
    line-height: 1.25;
    letter-spacing: -0.4px;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.video-wrapper {
    position: relative;
    width: 100%;
    /* Giữ tỉ lệ 16:9 nhưng không cao quá màn hình, để lộ danh sách tập */
    max-width: min(100%, calc((100dvh - 260px) * 16 / 9));
    margin-inline: auto;
    aspect-ratio: 16 / 9;
    background-color: #000000;
    border-radius: var(--radius-card);
    overflow: hidden;
    box-shadow: var(--shadow-lg);
}
video { width: 100%; height: 100%; object-fit: contain; background-color: #000000; }

.player-overlay {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 16px;
    background: radial-gradient(120% 90% at 50% 45%, rgba(0, 0, 0, 0.18), rgba(0, 0, 0, 0.55));
    transition: opacity 0.28s var(--ease);
}
.player-overlay.hidden { opacity: 0; pointer-events: none; }

.btn-play {
    width: 72px;
    height: 72px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 22px;
    padding-left: 5px;
    color: var(--on-accent);
    background-color: var(--accent);
    border: none;
    border-radius: 50%;
    cursor: pointer;
    box-shadow: 0 0 0 0 rgba(255, 111, 165, 0.5), 0 10px 30px -8px rgba(0, 0, 0, 0.6);
    transition: background-color var(--dur) var(--ease), transform var(--dur) var(--ease), box-shadow var(--dur) var(--ease);
}
.btn-play:hover {
    background-color: var(--accent-strong);
    transform: scale(1.06);
    box-shadow: 0 0 0 10px rgba(255, 111, 165, 0.18), 0 14px 34px -10px rgba(0, 0, 0, 0.6);
}
.btn-play:active { transform: scale(0.96); }

.episode-heading {
    font-size: 14px;
    font-weight: 600;
    color: #FFFFFF;
    padding: 6px 14px;
    border-radius: var(--radius-pill);
    background: rgba(0, 0, 0, 0.4);
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
}

/* =========================================================
   DANH SÁCH TẬP
   ========================================================= */
.episodes-section { margin-top: 32px; }
.section-head {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 14px;
    flex-wrap: wrap;
}
.row-title { font-size: 16px; font-weight: 700; color: var(--text); letter-spacing: -0.2px; }
.episode-count {
    font-size: 12px;
    font-weight: 600;
    color: var(--accent-strong);
    background: var(--accent-soft);
    padding: 4px 10px;
    border-radius: var(--radius-pill);
}

.episodes-row {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
}

.episode-chip {
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    flex: 0 0 96px;
    width: 96px;
    min-height: 46px;
    padding: 0 12px;
    font-size: 14px;
    font-weight: 600;
    border-radius: var(--radius-btn);
    background-color: var(--surface);
    border: 1px solid var(--border);
    color: var(--text);
    cursor: pointer;
    white-space: nowrap;
    overflow: hidden;
    transition: background-color var(--dur) var(--ease), border-color var(--dur) var(--ease),
                color var(--dur) var(--ease), box-shadow var(--dur) var(--ease), transform 0.12s var(--ease);
}
.episode-chip:hover {
    background-color: var(--surface-hover);
    border-color: var(--accent);
    color: var(--accent-strong);
    box-shadow: var(--shadow-sm);
}
.episode-chip:active { transform: scale(0.97); }
.chip-num { font-size: 14px; font-weight: 600; line-height: 1; }

/* Vạch tiến độ đã xem */
.chip-progress {
    position: absolute;
    left: 0;
    bottom: 0;
    height: 3px;
    background: var(--accent);
    border-radius: var(--radius-pill);
    transition: width var(--dur) var(--ease), background-color var(--dur) var(--ease);
}

/* Tập đang phát */
.episode-chip.active,
.episode-chip.active:hover {
    background-color: var(--accent);
    border-color: var(--accent);
    color: var(--on-accent);
    box-shadow: var(--shadow-md);
}
.episode-chip.active .chip-num,
.episode-chip.active:hover .chip-num { color: var(--on-accent); }
.episode-chip.active .chip-progress { background: rgba(255, 255, 255, 0.75); }

/* Tập chưa có */
.episode-chip.disabled {
    cursor: not-allowed;
    color: var(--muted);
    background-color: var(--bg-soft);
    border-color: var(--border);
    border-style: dashed;
    box-shadow: none;
}
.episode-chip.disabled:hover {
    background-color: var(--bg-soft);
    border-color: var(--border);
    color: var(--muted);
    box-shadow: none;
    transform: none;
}

/* =========================================================
   MODAL RESUME
   ========================================================= */
.modal-overlay {
    position: fixed;
    inset: 0;
    z-index: 200;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    background-color: rgba(28, 17, 17, 0.45);
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
    transition: opacity 0.28s var(--ease), visibility 0.28s var(--ease);
}
.modal-overlay.hidden { opacity: 0; visibility: hidden; pointer-events: none; }

.modal-content {
    background-color: var(--bg);
    width: 100%;
    max-width: 420px;
    border-radius: 18px;
    padding: 26px 24px 22px;
    border: 1px solid var(--border);
    box-shadow: var(--shadow-lg);
    transform: translateY(0) scale(1);
    transition: transform 0.32s var(--ease), opacity 0.28s var(--ease);
}
.modal-overlay.hidden .modal-content { transform: translateY(16px) scale(0.97); opacity: 0; }

.modal-title { font-size: 19px; font-weight: 700; color: var(--text); margin-bottom: 8px; letter-spacing: -0.3px; }
.modal-desc { font-size: 14px; color: var(--text-2); margin-bottom: 24px; line-height: 1.55; }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; }
.modal-actions .btn { flex: 0 1 auto; }

/* =========================================================
   RESPONSIVE — TABLET
   ========================================================= */
@media (max-width: 900px) {
    :root { --page-pad: 20px; }
    .movie-grid { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 20px 16px; }
}

/* =========================================================
   RESPONSIVE — MOBILE
   ========================================================= */
@media (max-width: 640px) {
    :root { --page-pad: 16px; --topbar-h: 56px; }

    .page { padding-top: 18px; }

    .home-header {
        display: block;          /* tránh flex-basis biến thành chiều cao */
        margin-bottom: 18px;
    }
    .page-title { margin-bottom: 14px; }

    .search {
        flex: none;              /* KHÔNG để flex-basis 340px thành chiều cao */
        display: block;
        width: 100%;
        height: 44px;
    }
    .search-input {
        font-size: 16px;         /* chặn iOS tự phóng to khi focus */
        height: 44px;
    }

    .movie-grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 18px 12px; }
    .movie-name { font-size: 14px; }
    .movie-year { font-size: 12px; }

    .watch-header { gap: 10px; margin-bottom: 16px; }
    .btn-icon { width: 38px; height: 38px; font-size: 14px; }

    /* Video tràn viền cho màn hình nhỏ */
    .video-wrapper {
        margin-left: calc(var(--page-pad) * -1);
        margin-right: calc(var(--page-pad) * -1);
        width: auto;
        max-width: none;
        border-radius: 0;
        box-shadow: none;
    }

    .btn-play { width: 60px; height: 60px; font-size: 18px; }
    .episode-heading { font-size: 13px; }

    .episodes-section { margin-top: 24px; }
    .episodes-row { display: flex; flex-wrap: wrap; gap: 8px; }
    .episode-chip { flex: 0 0 calc(25% - 6px); width: auto; min-height: 44px; font-size: 13px; padding: 0 8px; }

    .modal-content { padding: 22px 20px 18px; border-radius: 16px; }
    .modal-actions { flex-direction: column-reverse; gap: 8px; }
    .modal-actions .btn { width: 100%; }
}

/* Màn hình rất hẹp */
@media (max-width: 360px) {
    .movie-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px 10px; }
    .episode-chip { flex: 0 0 calc(33.333% - 5.33px); }
    .logo { font-size: 19px; }
}

/* Điện thoại nằm ngang: ưu tiên khung hình video */
@media (max-height: 480px) and (orientation: landscape) {
    .video-wrapper { max-width: calc(82dvh * 16 / 9); }
    .btn-play { width: 52px; height: 52px; font-size: 16px; }
    .topbar { position: static; }
}

/* Màn hình rất lớn */
@media (min-width: 1600px) {
    .page, .topbar-inner { max-width: 1440px; }
    .movie-grid { grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); }
}

/* =========================================================
   TÔN TRỌNG TUỲ CHỌN NGƯỜI DÙNG
   ========================================================= */
@media (prefers-reduced-motion: reduce) {
    html { scroll-behavior: auto; }
    *, *::before, *::after {
        animation-duration: 0.001ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.001ms !important;
    }
}

@media print {
    .topbar, .player-overlay, .modal-overlay, .search { display: none !important; }
}
