:root {
  --bg: #0b0b0b;
  --surface: #1f1f1f;
  --surface-2: #2a2a2a;
  --text: #f5f5f5;
  --muted: #b3b3b3;
  --red: #e50914;
  --warn: #f5a623;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --nav-h: 56px;
  --gutter: 16px;
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { background: var(--bg); }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
  overflow-x: hidden;
}
a { color: inherit; text-decoration: none; }
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; padding: 0; }
img { display: block; }
h1, h2, h3 { margin: 0; line-height: 1.2; }
.muted { color: var(--muted); }
.pad { padding: 0 var(--gutter); }

#view {
  min-height: 100vh;
  padding: 0 var(--safe-right) calc(var(--nav-h) + var(--safe-bottom) + 16px) var(--safe-left);
}

/* ---- Thanh điều hướng dưới ---- */
#nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; height: calc(var(--nav-h) + var(--safe-bottom));
  padding: 0 var(--safe-right) var(--safe-bottom) var(--safe-left);
  background: var(--bg); /* nền đặc: hiệu ứng mờ (backdrop-filter) làm iPhone giật khi cuộn */
  border-top: 1px solid #222;
}
#nav a {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  font-size: 11px; color: var(--muted); gap: 2px;
}
#nav a span { font-size: 20px; line-height: 1; }
#nav a.on { color: var(--text); }
body.playing #nav { display: none; }
body.playing #view { padding: 0; }

/* ---- Nút ---- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 40px; padding: 0 16px; border-radius: 6px;
  background: rgba(109, 109, 110, 0.7); color: #fff; font-weight: 600; white-space: nowrap;
}
.btn.primary { background: #fff; color: #000; }
.btn.fav.on { background: rgba(229, 9, 20, 0.85); }
.actions { display: flex; gap: 10px; flex-wrap: wrap; }

/* ---- Banner trang chủ ---- */
.hero { position: relative; min-height: min(72vh, 560px); display: flex; align-items: flex-end; overflow: hidden; }
.hero-bg { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; animation: fade .6s ease; }
.hero::after {
  content: ""; position: absolute; inset: 0;
  background: linear-gradient(to top, var(--bg) 4%, rgba(11,11,11,.55) 45%, rgba(11,11,11,.2) 70%, rgba(11,11,11,.6));
}
.hero-body { position: relative; z-index: 1; padding: calc(var(--safe-top) + 24px) var(--gutter) 20px; width: 100%; }
.hero h1 { font-size: clamp(26px, 7vw, 44px); font-weight: 800; text-shadow: 0 2px 12px rgba(0,0,0,.6); }
.origin { margin: 4px 0 0; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; font-size: 12px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 10px 0 14px; padding: 0; }
.chips span { font-size: 12px; padding: 2px 8px; border-radius: 4px; background: rgba(255,255,255,.12); }
.chips span.warn { background: rgba(245,166,35,.25); color: var(--warn); }
.dots { display: flex; gap: 6px; margin-top: 14px; }
.dots i { width: 6px; height: 6px; border-radius: 50%; background: rgba(255,255,255,.35); }
.dots i.on { background: #fff; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

.hint {
  display: flex; align-items: center; gap: 10px; margin: 8px var(--gutter);
  padding: 10px 12px; border-radius: 8px; background: var(--surface); font-size: 13px;
}
.hint p { margin: 0; flex: 1; }
.hint button { font-size: 16px; color: var(--muted); padding: 4px 6px; }

/* ---- Hàng phim ---- */
.row { margin: 18px 0 4px; content-visibility: auto; contain-intrinsic-size: auto 230px; } /* hàng ngoài màn hình: không vẽ */
.row-head { display: flex; align-items: baseline; justify-content: space-between; padding: 0 var(--gutter); margin-bottom: 8px; }
.row-head h2 { font-size: 17px; font-weight: 700; }
.row-head .more { font-size: 13px; color: var(--muted); }
.scroller {
  display: flex; gap: 8px; overflow-x: auto; padding: 0 var(--gutter);
  scroll-snap-type: x proximity; scrollbar-width: none; -webkit-overflow-scrolling: touch;
}
.scroller::-webkit-scrollbar { display: none; }
.scroller .card { flex: 0 0 auto; width: 112px; scroll-snap-align: start; }
.scroller .card.ranked { width: 150px; display: flex; align-items: flex-end; }
.card { display: block; min-width: 0; }
.card .poster {
  position: relative; display: block; aspect-ratio: 2 / 3; border-radius: 6px; overflow: hidden; background: var(--surface);
}
.card.ranked .poster { flex: 0 0 104px; }
.card .poster img { width: 100%; height: 100%; object-fit: cover; }
.card .name {
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
  margin-top: 5px; font-size: 12px; line-height: 1.3; color: #ddd;
}
.card.ranked .name { display: none; }
.card .rank {
  flex: 0 0 46px; font-size: 76px; font-weight: 900; line-height: .8; color: var(--bg);
  -webkit-text-stroke: 2px #777; text-align: right; margin-right: -2px; letter-spacing: -6px;
}
.badge {
  position: absolute; top: 6px; left: 6px; max-width: calc(100% - 12px);
  font-size: 10px; font-weight: 700; padding: 2px 5px; border-radius: 3px;
  background: var(--red); white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.progress { position: absolute; left: 0; right: 0; bottom: 0; height: 4px; background: rgba(255,255,255,.25); }
.progress i { display: block; height: 100%; background: var(--red); }
.skeleton .poster { animation: pulse 1.2s ease-in-out infinite alternate; }
@keyframes pulse { from { opacity: .45; } to { opacity: .9; } }

.grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(104px, 1fr));
  gap: 14px 8px; padding: 0 var(--gutter);
}

/* ---- Đầu trang ---- */
.page-head {
  position: sticky; top: 0; z-index: 10; display: flex; align-items: center; gap: 8px;
  padding: calc(var(--safe-top) + 10px) var(--gutter) 10px; background: var(--bg); margin-bottom: 8px;
}
.page-head h1 { font-size: 22px; font-weight: 800; }
.page-head .back { position: static; width: 32px; height: 32px; margin-left: -6px; background: none; }
.search-head input {
  flex: 1; height: 42px; padding: 0 14px; border-radius: 8px; border: 1px solid #333;
  background: var(--surface); color: var(--text); font-size: 16px; outline: none;
}
.search-head input:focus { border-color: #666; }
.results .row:first-child { margin-top: 4px; }
h2.sub { font-size: 17px; margin: 22px var(--gutter) 10px; }
.genres { display: flex; flex-wrap: wrap; gap: 8px; padding: 0 var(--gutter); }
.genre { padding: 9px 14px; border-radius: 20px; background: var(--surface); font-size: 14px; }
.settings { padding: 24px var(--gutter); display: flex; flex-direction: column; align-items: flex-start; gap: 10px; }
.settings .muted { font-size: 12px; margin-top: 10px; }

.state { display: flex; flex-direction: column; align-items: center; gap: 12px; padding: 48px var(--gutter); text-align: center; color: var(--muted); }
.state p { margin: 0; }
.spinner {
  width: 36px; height: 36px; border-radius: 50%;
  border: 3px solid rgba(255,255,255,.2); border-top-color: var(--red); animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---- Chi tiết ---- */
.detail { position: relative; }
.backdrop { position: relative; width: 100%; height: min(56.25vw, 60vh); overflow: hidden; }
.backdrop img { width: 100%; height: 100%; object-fit: cover; }
.backdrop::after { content: ""; position: absolute; inset: 0; background: linear-gradient(to top, var(--bg), transparent 60%); }
.back {
  position: absolute; z-index: 5; top: calc(var(--safe-top) + 10px); left: calc(var(--safe-left) + 10px);
  width: 38px; height: 38px; border-radius: 50%; background: rgba(0,0,0,.55);
  display: flex; align-items: center; justify-content: center; font-size: 30px; line-height: 1; padding-bottom: 4px;
}
.detail-body { position: relative; padding: 0 var(--gutter); margin-top: -36px; }
.detail-body h1 { font-size: 24px; font-weight: 800; }
.desc {
  margin: 14px 0 0; color: #ddd; font-size: 14px; cursor: pointer;
  display: -webkit-box; -webkit-line-clamp: 4; -webkit-box-orient: vertical; overflow: hidden;
}
.desc.open { -webkit-line-clamp: unset; display: block; }
.facts { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; margin: 12px 0 0; font-size: 13px; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; }
.tabs { display: flex; gap: 8px; overflow-x: auto; margin: 18px calc(-1 * var(--gutter)) 0; padding: 0 var(--gutter); scrollbar-width: none; }
.tabs::-webkit-scrollbar { display: none; }
.tab { flex: 0 0 auto; padding: 7px 14px; border-radius: 18px; background: var(--surface); font-size: 13px; white-space: nowrap; }
.tab.on { background: #fff; color: #000; font-weight: 700; }
.episodes { display: grid; grid-template-columns: repeat(auto-fill, minmax(76px, 1fr)); gap: 8px; margin-top: 14px; }
.ep {
  display: flex; align-items: center; justify-content: center; min-height: 40px; padding: 4px;
  border-radius: 6px; background: var(--surface); font-size: 13px; text-align: center; border: 2px solid transparent;
}
.ep.current { border-color: var(--red); }
.detail .row { margin-top: 26px; }

#toast {
  position: fixed; left: 50%; bottom: calc(var(--nav-h) + var(--safe-bottom) + 16px); z-index: 50;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none; transition: .25s;
  background: #333; color: #fff; padding: 10px 16px; border-radius: 20px; font-size: 14px; max-width: 90vw; text-align: center;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
body.playing #toast { bottom: 90px; }

/* ---- Trình phát ---- */
.player {
  position: fixed; inset: 0; z-index: 30; background: #000; overflow: hidden; outline: none;
  touch-action: manipulation; user-select: none; -webkit-user-select: none;
}
.player video, .player .embed { position: absolute; inset: 0; width: 100%; height: 100%; border: 0; background: #000; }
.player .tapzone { position: absolute; inset: 0; }
.player.embed-on .tapzone, .player.embed-on .subs, .player.embed-on .hud-center,
.player.embed-on .hud-bottom .cur, .player.embed-on .hud-bottom .seek, .player.embed-on .hud-bottom .dur,
.player.embed-on [data-act="fullscreen"], .player.embed-on .loading { display: none; }
.player.embed-on .hud { pointer-events: none; background: none; opacity: 1; }
.player.embed-on .hud-top { background: linear-gradient(rgba(0,0,0,.7), transparent); pointer-events: none; }
.player.embed-on .hud-top button, .player.embed-on .hud-bottom .pill { pointer-events: auto; }
.player.embed-on .hud-bottom { justify-content: flex-end; background: none; pointer-events: none; }

.hud {
  position: absolute; inset: 0; display: flex; flex-direction: column; justify-content: space-between;
  pointer-events: none; opacity: 0; transition: opacity .25s;
  background: linear-gradient(rgba(0,0,0,.65), transparent 22%, transparent 70%, rgba(0,0,0,.75));
}
.player.hud-on .hud { opacity: 1; }
.player.hud-on .hud button, .player.hud-on .hud input { pointer-events: auto; }
.hud-top, .hud-bottom {
  display: flex; align-items: center; gap: 10px;
  padding-left: calc(var(--safe-left) + 12px); padding-right: calc(var(--safe-right) + 12px);
}
.hud-top { padding-top: calc(var(--safe-top) + 8px); }
.hud-bottom { padding-bottom: calc(var(--safe-bottom) + 10px); }
.hud .title { flex: 1; min-width: 0; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hud-center { display: flex; align-items: center; justify-content: center; gap: 48px; }
.icon {
  width: 44px; height: 44px; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center;
  font-size: 24px; line-height: 1; position: relative; text-shadow: 0 1px 6px rgba(0,0,0,.6);
}
.icon.big { width: 56px; height: 56px; font-size: 34px; }
.icon.big small { position: absolute; font-size: 10px; font-weight: 700; top: 50%; left: 50%; transform: translate(-50%, -40%); }
.icon.huge { width: 72px; height: 72px; font-size: 34px; background: rgba(0,0,0,.4); }
.cur, .dur { font-size: 12px; font-variant-numeric: tabular-nums; min-width: 38px; text-align: center; }
.pill { padding: 7px 12px; border-radius: 16px; background: rgba(255,255,255,.18); font-size: 13px; font-weight: 600; white-space: nowrap; }

.seek {
  --p: 0%; --b: 0%;
  flex: 1; min-width: 0; height: 28px; margin: 0; background: transparent; -webkit-appearance: none; appearance: none;
}
.seek::-webkit-slider-runnable-track {
  height: 4px; border-radius: 2px;
  background: linear-gradient(to right, var(--red) var(--p), rgba(255,255,255,.5) var(--p), rgba(255,255,255,.5) var(--b), rgba(255,255,255,.22) var(--b));
}
.seek::-webkit-slider-thumb { -webkit-appearance: none; width: 16px; height: 16px; margin-top: -6px; border-radius: 50%; background: var(--red); border: 0; }
.seek::-moz-range-track { height: 4px; border-radius: 2px; background: rgba(255,255,255,.22); }
.seek::-moz-range-progress { height: 4px; border-radius: 2px; background: var(--red); }
.seek::-moz-range-thumb { width: 16px; height: 16px; border-radius: 50%; background: var(--red); border: 0; }

.loading { position: absolute; inset: 0; display: none; align-items: center; justify-content: center; pointer-events: none; }
.player.loading-on .loading { display: flex; }

.subs {
  position: absolute; left: 6%; right: 6%; bottom: calc(var(--safe-bottom) + 6%); z-index: 1;
  display: flex; flex-direction: column; align-items: center; gap: 4px; text-align: center; pointer-events: none;
  transition: bottom .25s; --sub: clamp(15px, 3.6vmin + 4px, 30px);
}
.player.hud-on .subs { bottom: calc(var(--safe-bottom) + 72px); }
.subs[data-size="s"] { --sub: clamp(13px, 3vmin + 2px, 24px); }
.subs[data-size="l"] { --sub: clamp(18px, 4.6vmin + 4px, 38px); }
.sub-block { font-size: var(--sub); line-height: 1.3; font-weight: 600; }
.sub-block.second { font-size: calc(var(--sub) * .78); font-weight: 500; color: #ffe9a8; }
.subs[data-bg="shadow"] span { text-shadow: 0 0 4px #000, 0 0 4px #000, 1px 1px 2px #000, -1px -1px 2px #000; }
.subs[data-bg="box"] span { background: rgba(0,0,0,.72); padding: 1px 6px; -webkit-box-decoration-break: clone; box-decoration-break: clone; }

.ripple {
  position: absolute; top: 0; bottom: 0; width: 38%; display: flex; align-items: center; justify-content: center;
  font-weight: 700; opacity: 0; pointer-events: none; background: rgba(255,255,255,.08);
}
.ripple.left { left: 0; border-radius: 0 50% 50% 0; }
.ripple.right { right: 0; border-radius: 50% 0 0 50%; }
.ripple.show { animation: ripple .6s ease; }
@keyframes ripple { 0%, 40% { opacity: 1; } 100% { opacity: 0; } }

.skip-intro {
  position: absolute; z-index: 4; right: calc(var(--safe-right) + 16px); bottom: calc(var(--safe-bottom) + 84px);
  padding: 10px 16px; border-radius: 6px; background: rgba(255,255,255,.92); color: #000; font-weight: 700;
}
.next-card, .message {
  position: absolute; z-index: 5; right: calc(var(--safe-right) + 16px); bottom: calc(var(--safe-bottom) + 84px);
  max-width: min(420px, calc(100% - 32px)); padding: 14px 16px; border-radius: 10px; background: rgba(24,24,24,.95);
}
.message { left: 50%; right: auto; top: 50%; bottom: auto; transform: translate(-50%, -50%); text-align: center; width: max-content; }
.message p { margin: 0 0 12px; }
.message .actions { justify-content: center; }
.next-card .label { margin: 0; font-size: 12px; color: var(--muted); }
.next-card h3 { font-size: 17px; margin: 2px 0 12px; }
.next-movie { display: flex; gap: 12px; align-items: center; }
.next-movie img { width: 56px; aspect-ratio: 2 / 3; object-fit: cover; border-radius: 4px; margin-bottom: 12px; }

.sheet { position: absolute; inset: 0; z-index: 6; background: rgba(0,0,0,.45); display: flex; justify-content: flex-end; }
.sheet[hidden] { display: none; }
.sheet-body {
  width: min(380px, 100%); height: 100%; overflow-y: auto; background: rgba(20,20,20,.97);
  padding: calc(var(--safe-top) + 16px) calc(var(--safe-right) + 16px) calc(var(--safe-bottom) + 16px) 16px;
}
.group { margin-bottom: 18px; }
.group h3 { font-size: 13px; color: var(--muted); font-weight: 600; margin-bottom: 8px; text-transform: uppercase; letter-spacing: .04em; }
.group .muted { font-size: 12px; margin: 8px 0 0; }
.opts { display: flex; flex-wrap: wrap; gap: 8px; }
.opt { padding: 8px 12px; border-radius: 6px; background: var(--surface-2); font-size: 14px; }
.opt.on { background: #fff; color: #000; font-weight: 700; }

[hidden] { display: none !important; }

@media (min-width: 900px) {
  :root { --gutter: 32px; }
  .scroller .card { width: 150px; }
  .scroller .card.ranked { width: 190px; }
  .card.ranked .poster { flex-basis: 136px; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
  .hero-body { max-width: 640px; padding-bottom: 36px; }
  .detail-body { max-width: 960px; margin-top: -80px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
