:root {
  color-scheme: dark;
  --bg-deep: #08080a;
  --bg-surface: #101116;
  --bg-surface-elevated: #161820;
  --bg-surface-hover: #1c1f2a;
  --bg-card: #121319;
  --border-subtle: rgba(255, 255, 255, 0.08);
  --border-focus: rgba(255, 208, 0, 0.5);
  --yellow: #ffd000;
  --yellow-glow: rgba(255, 208, 0, 0.28);
  --yellow-hover: #ffe14d;
  --yellow-gradient: linear-gradient(135deg, #ffd000 0%, #ffb300 100%);
  --green: #4ade80;
  --text-main: #f4f4f6;
  --text-muted: #8e92a4;
  --text-dim: #606374;
  --danger: #ff4757;
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;
  --radius-full: 9999px;
  --shadow-card: 0 10px 30px -10px rgba(0, 0, 0, 0.7);
  --font-sans: 'Plus Jakarta Sans', system-ui, -apple-system, sans-serif;
  --transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
  --topbar-sticky-offset: 60px;
  --subbar-bottom-height: 76px;
}

body.theme-rou {
  --yellow: #f5c400;
  --yellow-hover: #ffd84d;
  --yellow-gradient: linear-gradient(135deg, #f5c400 0%, #e8a800 100%);
  --yellow-glow: rgba(245, 196, 0, 0.32);
  --rou-accent: #f5c400;
  --rou-tag-bg: rgba(245, 196, 0, 0.14);
  font-family: 'Noto Sans SC', 'Plus Jakarta Sans', system-ui, sans-serif;
}

*, *::before, *::after { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg-deep);
  color: var(--text-main);
  font-family: var(--font-sans);
  -webkit-font-smoothing: antialiased;
  line-height: 1.5;
  overflow-x: hidden;
}
a { color: inherit; text-decoration: none; }
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: var(--bg-deep); }
::-webkit-scrollbar-thumb { background: #232634; border-radius: var(--radius-full); }
::-webkit-scrollbar-thumb:hover { background: var(--yellow); }

.top-loader {
  position: fixed; top: 0; left: 0; width: 100%; height: 3px;
  background: var(--yellow-gradient); box-shadow: 0 0 12px var(--yellow);
  z-index: 1000; transform: translateX(-100%); pointer-events: none;
}
.top-loader.is-loading { animation: loadAnim 1.4s infinite cubic-bezier(0.65,0,0.35,1); }
@keyframes loadAnim { 0%{transform:translateX(-100%);} 50%{transform:translateX(0);} 100%{transform:translateX(100%);} }

.topbar {
  position: sticky; top: 0; z-index: 99;
  background: rgba(8,8,10,0.85); backdrop-filter: blur(16px);
  border-bottom: 1px solid var(--border-subtle);
}
.topbar-inner {
  max-width: 1600px; margin: 0 auto; padding: 0.85rem 1.5rem;
  display: flex; align-items: center; justify-content: space-between;
}
.brand { display: flex; align-items: center; gap: 2px; font-weight: 800; font-size: 1.6rem; letter-spacing: -0.04em; transition: transform 0.15s; }
.brand:hover { transform: scale(1.02); }
.brand-text { color: #fff; }
.brand-badge {
  background: var(--yellow-gradient); color: #000; font-size: 1.15rem;
  font-weight: 800; padding: 0.12rem 0.5rem; border-radius: var(--radius-sm);
  margin-left: 2px; box-shadow: 0 2px 10px rgba(255,208,0,0.35);
}
.brand-lead-rou { color: var(--rou-accent, var(--yellow)); }
.brand-badge-rou {
  letter-spacing: 0.02em;
  font-size: 1rem;
  padding: 0.16rem 0.55rem;
}
.header-right { display: flex; align-items: center; gap: 0.85rem; }
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 38px; height: 38px; border-radius: var(--radius-md);
  background: var(--bg-surface); border: 1px solid var(--border-subtle);
  color: var(--text-muted); cursor: pointer; transition: var(--transition);
}
.icon-btn svg { width: 18px; height: 18px; }
.icon-btn:hover { background: var(--bg-surface-hover); color: var(--yellow); border-color: var(--border-focus); transform: translateY(-1px); }

.subbar-bottom {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 98;
  background: rgba(16, 17, 22, 0.92);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-top: 1px solid var(--border-subtle);
  box-shadow: 0 -8px 32px rgba(0, 0, 0, 0.45);
  padding-bottom: env(safe-area-inset-bottom, 0);
}
.subbar-inner {
  max-width: 1600px; margin: 0 auto; padding: 0.75rem 1.5rem;
  display: flex; align-items: center; justify-content: center;
  min-width: 0;
}

/* Region Tabs */
.region-tabs {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  background: var(--bg-surface-elevated);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-full);
  padding: 0.3rem;
  max-width: 100%;
  min-width: 0;
  margin: 0 auto;
}
.region-tab {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-family: inherit;
  font-size: 0.82rem;
  font-weight: 700;
  padding: 0.45rem 0.95rem;
  border-radius: var(--radius-full);
  cursor: pointer;
  transition: var(--transition);
  position: relative;
  flex-shrink: 0;
  white-space: nowrap;
}
.region-tab:hover:not(:disabled):not(.is-active) {
  color: var(--text-main);
  background: rgba(255, 255, 255, 0.04);
}
.region-tab.is-active {
  background: var(--yellow-gradient);
  color: #000;
  box-shadow: 0 2px 10px rgba(255, 208, 0, 0.3);
}
.region-tab:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}
.flag-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}
.flag-mm { background: #fecb00; }
.flag-jp { background: #bc002d; }
.flag-cn { background: #de2910; }
.tag-soon {
  font-size: 0.58rem;
  font-weight: 800;
  padding: 0.05rem 0.3rem;
  background: rgba(255, 255, 255, 0.08);
  color: var(--text-dim);
  border-radius: var(--radius-full);
  letter-spacing: 0.05em;
  text-transform: uppercase;
}
.region-tab.is-active .tag-soon { display: none; }

.ad-slot {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  max-width: 100%;
  margin: 0.5rem 0 0.75rem;
  min-height: 0;
  overflow: hidden;
  border-radius: var(--radius-md);
  background: rgba(255, 255, 255, 0.02);
  border: 1px dashed transparent;
}
.ad-slot:not(.ad-slot--loaded) {
  display: none;
}
.ad-slot-watch {
  margin-top: 0.75rem;
}
.ad-slot-sidebar {
  margin-bottom: 1rem;
}
@media (max-width: 1024px) {
  .ad-slot-sidebar {
    display: none !important;
  }
}

.app-container {
  max-width: 1600px;
  margin: 0 auto;
  padding: 1.75rem 1.5rem calc(var(--subbar-bottom-height) + env(safe-area-inset-bottom, 0) + 1.5rem);
}
.main { min-height: 60vh; }

.rou-catalog-tabs {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  margin: 0 0 1.1rem;
  padding: 0.28rem;
  border-radius: var(--radius-full);
  background: var(--bg-surface-elevated);
  border: 1px solid var(--border-subtle);
}
.rou-catalog-tab {
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-family: inherit;
  font-size: 0.8rem;
  font-weight: 800;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  padding: 0.42rem 1rem;
  border-radius: var(--radius-full);
  cursor: pointer;
  transition: var(--transition);
}
.rou-catalog-tab:hover:not(.is-active) {
  color: var(--text-main);
  background: rgba(255, 255, 255, 0.04);
}
.rou-catalog-tab.is-active {
  color: #000;
  background: var(--yellow-gradient);
  box-shadow: 0 2px 10px rgba(255, 208, 0, 0.28);
}

.video-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 1.5rem 1.15rem; }
.video-grid-rou { grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 1.35rem 1rem; }

.video-card { display: flex; flex-direction: column; padding: 0; background: transparent; border: none; text-align: left; cursor: pointer; transition: var(--transition); }
.video-card:hover { transform: translateY(-3px); }

.thumb-wrap {
  position: relative; aspect-ratio: 16/9; width: 100%; background: #090a0d;
  overflow: hidden; border-radius: var(--radius-md); border: 1px solid var(--border-subtle); transition: var(--transition);
}
.video-card:hover .thumb-wrap { border-color: var(--yellow); box-shadow: 0 0 0 1px var(--yellow), 0 8px 24px rgba(255,208,0,0.18); }
.thumb-wrap img { width: 100%; height: 100%; object-fit: cover; display: block; transition: transform 0.35s, filter 0.3s; }
.video-card:hover .thumb-wrap img { transform: scale(1.05); filter: brightness(1.06); }
.thumb-wrap-rou { aspect-ratio: 16/10; }
.thumb-wrap.thumb-missing { background: linear-gradient(145deg, #12131a, #0a0b10); }
.thumb-badges {
  position: absolute; inset: 0; pointer-events: none; z-index: 2;
}
.thumb-badge {
  position: absolute; font-size: 0.68rem; font-weight: 800; line-height: 1;
  padding: 0.22rem 0.42rem; border-radius: 4px; color: #fff;
}
.thumb-badge-quality {
  top: 8px; left: 8px; background: var(--yellow-gradient); color: #111;
  box-shadow: 0 2px 8px rgba(0,0,0,0.35);
}
.thumb-badge-duration {
  right: 8px; bottom: 8px; background: rgba(7, 16, 21, 0.82);
  backdrop-filter: blur(4px);
}

.card-info { padding: 0.7rem 0.2rem 0; display: flex; flex-direction: column; gap: 0.35rem; }
.card-title-row { display: flex; align-items: flex-start; gap: 0.5rem; }
.card-title { margin: 0; flex: 1; font-size: 0.9rem; font-weight: 600; line-height: 1.4; color: var(--text-main); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; transition: color 0.15s; }
.video-card:hover .card-title { color: var(--yellow); }
.card-menu { flex-shrink: 0; width: 24px; height: 24px; display: flex; align-items: center; justify-content: center; color: var(--text-dim); background: transparent; border: none; border-radius: 4px; cursor: pointer; opacity: 0; transition: opacity 0.15s; }
.card-menu svg { width: 16px; height: 16px; }
.video-card:hover .card-menu { opacity: 1; }
.card-menu:hover { color: var(--yellow); background: rgba(255,208,0,0.1); }
.card-meta { margin: 0; display: flex; align-items: center; gap: 0.4rem; font-size: 0.76rem; color: var(--text-muted); }
.card-meta-chips { flex-wrap: wrap; gap: 0.35rem; }
.meta-chip {
  display: inline-flex; align-items: center; max-width: 100%;
  font-size: 0.7rem; font-weight: 600; color: var(--text-muted);
  padding: 0.15rem 0.45rem; border-radius: var(--radius-full);
  background: rgba(255,255,255,0.04); border: 1px solid var(--border-subtle);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.meta-chip-tag {
  color: var(--rou-accent, var(--yellow));
  background: var(--rou-tag-bg, rgba(255,208,0,0.12));
  border-color: rgba(245, 196, 0, 0.22);
}
.meta-chip-dim { color: var(--text-dim); border-color: transparent; background: transparent; padding-left: 0; }
.video-card-rou .card-title { font-weight: 650; letter-spacing: 0.01em; }

.skeleton-card { display: flex; flex-direction: column; gap: 0.7rem; }
.skeleton-thumb { aspect-ratio: 16/9; border-radius: var(--radius-md); background: linear-gradient(90deg,#13141a 25%,#1d1f2a 50%,#13141a 75%); background-size: 200% 100%; animation: shimmer 1.5s infinite; }
.skeleton-line { height: 12px; border-radius: 4px; background: linear-gradient(90deg,#161722 25%,#222535 50%,#161722 75%); background-size: 200% 100%; animation: shimmer 1.5s infinite; }
.skeleton-line.short { width: 60%; }
.skeleton-line.mini { width: 40%; height: 10px; }
@keyframes shimmer { 0%{background-position:200% 0;} 100%{background-position:-200% 0;} }

.empty-state { grid-column: 1/-1; padding: 4rem 1.5rem; text-align: center; background: var(--bg-surface); border-radius: var(--radius-lg); border: 1px solid var(--border-subtle); }
.empty-state svg { width: 48px; height: 48px; color: var(--yellow); margin-bottom: 1rem; }
.empty-title { font-size: 1.15rem; font-weight: 700; margin: 0 0 0.5rem; color: #fff; }
.empty-desc { font-size: 0.88rem; color: var(--text-muted); margin: 0; }

.sentinel { height: 1px; width: 100%; }
.load-status { text-align: center; padding: 1.5rem; color: var(--text-muted); font-size: 0.85rem; display: flex; align-items: center; justify-content: center; gap: 0.65rem; }
.spinner { width: 16px; height: 16px; border: 2px solid rgba(255,208,0,0.25); border-top-color: var(--yellow); border-radius: 50%; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.toast-container {
  position: fixed;
  right: 24px;
  bottom: calc(var(--subbar-bottom-height) + env(safe-area-inset-bottom, 0) + 16px);
  z-index: 10000;
  display: flex;
  flex-direction: column;
  gap: 10px;
  pointer-events: none;
}
.toast { pointer-events: auto; display: flex; align-items: center; gap: 0.75rem; padding: 0.85rem 1.25rem; background: #151720; border: 1px solid var(--border-subtle); border-left: 4px solid var(--yellow); border-radius: var(--radius-md); color: var(--text-main); font-size: 0.88rem; font-weight: 600; box-shadow: 0 12px 30px rgba(0,0,0,0.8); animation: toastIn 0.3s cubic-bezier(0.16,1,0.3,1); }
.toast.toast-error { border-left-color: var(--danger); }
@keyframes toastIn { from{transform:translateY(20px);opacity:0;} to{transform:translateY(0);opacity:1;} }
.hidden { display: none !important; }

@media (max-width: 900px) {
  .subbar-inner {
    padding: 0.65rem 1rem;
  }

  .region-tabs {
    display: inline-flex;
    width: auto;
    max-width: 100%;
    justify-content: center;
    margin: 0 auto;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    border-radius: var(--radius-full);
    padding: 0.35rem;
  }

  .region-tabs::-webkit-scrollbar {
    display: none;
  }

  .region-tab {
    padding: 0.42rem 0.8rem;
    font-size: 0.78rem;
  }
}

@media (max-width: 768px) {
  .topbar-inner { padding: 0.75rem 1rem; gap: 0.5rem; }
  .brand { font-size: 1.35rem; }
  .subbar-inner { padding: 0.85rem 1rem; }
  .app-container {
    padding: 1.25rem 1rem calc(var(--subbar-bottom-height) + env(safe-area-inset-bottom, 0) + 1.25rem);
  }
  .toast-container {
    left: 1rem;
    right: 1rem;
    bottom: calc(var(--subbar-bottom-height) + env(safe-area-inset-bottom, 0) + 12px);
  }
  .video-grid { grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 1rem 0.6rem; }
  .card-title { font-size: 0.82rem; }
  .toast-container { left: 1rem; right: 1rem; bottom: 1rem; }
}

/* ===================== Watch Page ===================== */
body.watch-page {
  overflow-x: clip;
}
.watch-container {
  max-width: 1600px;
  margin: 0 auto;
  padding: 1.5rem 1.5rem 3.5rem;
  width: 100%;
  min-width: 0;
}
.watch-layout {
  display: grid;
  grid-template-columns: 1fr 380px;
  gap: 2rem;
  align-items: start;
  min-width: 0;
}
.watch-main { min-width: 0; max-width: 100%; }

.player-frame-wrapper {
  position: relative;
  width: 100%;
  max-width: 100%;
  background: #000;
  border-radius: var(--radius-lg);
  overflow: hidden;
  border: 1px solid var(--border-subtle);
  aspect-ratio: 16/9;
}
.player-frame-wrapper.is-buffering::after {
  content: '';
  position: absolute; inset: 0; pointer-events: none;
  background: radial-gradient(circle at center, rgba(0,0,0,0.1), rgba(0,0,0,0.45));
}
.player-frame-wrapper.is-buffering::before {
  content: '';
  position: absolute; left: 50%; top: 50%; width: 42px; height: 42px; margin: -21px 0 0 -21px;
  border: 3px solid rgba(255,255,255,0.18); border-top-color: var(--yellow);
  border-radius: 50%; animation: spin 0.8s linear infinite; z-index: 2;
}
.player-frame-wrapper #player {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  max-width: 100%;
  display: block;
  outline: none;
  background: #000;
  object-fit: contain;
}
.player-status {
  display: flex; align-items: center; justify-content: center; gap: 0.75rem;
  padding: 1.5rem; font-size: 0.9rem; color: var(--text-muted); background: #060608;
  border-radius: var(--radius-md); margin-top: 1rem;
}
.player-status.error { color: #ff9999; background: rgba(255,71,87,0.1); }

.watch-info { padding: 1.25rem 0 0; }
.watch-title-row { display: flex; align-items: flex-start; gap: 0.65rem; flex-wrap: wrap; }
.watch-title { margin: 0; font-size: 1.25rem; font-weight: 700; line-height: 1.35; color: #fff; flex: 1; min-width: 0; }
.watch-tag {
  flex-shrink: 0; font-size: 0.72rem; font-weight: 700; color: #111;
  background: var(--yellow-gradient); padding: 0.2rem 0.55rem; border-radius: var(--radius-full);
}
body.watch-rou .watch-title { font-weight: 650; letter-spacing: 0.01em; }
body.watch-rou .watch-sidebar { top: 84px; }
.watch-meta {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 0.75rem;
  margin-top: 0.75rem;
  min-width: 0;
}
.meta-stats {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.65rem 1rem;
  min-width: 0;
}
.stat-item {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.85rem;
  color: var(--text-muted);
  font-weight: 600;
  min-width: 0;
}
.stat-item span {
  overflow-wrap: anywhere;
}
.stat-item svg { width: 16px; height: 16px; color: var(--yellow); }
.watch-time {
  font-size: 0.82rem;
  color: var(--text-dim);
  flex: 1 1 100%;
  overflow-wrap: anywhere;
}

/* Sidebar Related Videos */
.watch-sidebar {
  position: sticky;
  top: 90px;
  min-width: 0;
  max-width: 100%;
}
.sidebar-title { margin: 0 0 1rem; font-size: 1rem; font-weight: 800; color: #fff; letter-spacing: -0.01em; }
.related-list { display: flex; flex-direction: column; gap: 0.85rem; }

.related-card { display: flex; gap: 0.65rem; padding: 0; background: transparent; border: none; cursor: pointer; text-align: left; transition: var(--transition); }
.related-card:hover { transform: translateX(-2px); }
.related-thumb { position: relative; flex-shrink: 0; width: 140px; aspect-ratio: 16/9; background: #090a0d; border-radius: var(--radius-sm); overflow: hidden; border: 1px solid var(--border-subtle); transition: var(--transition); }
.related-card:hover .related-thumb { border-color: var(--yellow); }
.related-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.related-dur { position: absolute; right: 4px; bottom: 4px; background: rgba(0,0,0,0.85); color: #fff; font-size: 0.62rem; font-weight: 700; padding: 0.1rem 0.3rem; border-radius: 3px; }
.related-info { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 0.25rem; padding-top: 0.1rem; }
.related-title { margin: 0; font-size: 0.82rem; font-weight: 600; line-height: 1.35; color: var(--text-main); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; transition: color 0.15s; }
.related-card:hover .related-title { color: var(--yellow); }
.related-meta { margin: 0; font-size: 0.72rem; color: var(--text-muted); }
.related-empty { margin: 0; color: var(--text-muted); font-size: 0.85rem; }
.related-card-rou .related-title { font-weight: 650; }

/* Watch page — mobile (must follow desktop watch rules) */
@media (max-width: 1024px) {
  .watch-layout {
    grid-template-columns: 1fr;
    gap: 1.5rem;
  }

  .watch-sidebar {
    position: static;
    top: auto;
  }
}

@media (max-width: 768px) {
  body.watch-page .topbar-inner {
    padding: 0.65rem 0.85rem;
  }

  .watch-container {
    padding: 0 0 2rem;
    padding-bottom: max(2rem, env(safe-area-inset-bottom));
  }

  .watch-layout {
    gap: 1.25rem;
  }

  .player-frame-wrapper {
    border-radius: 0;
    border-left: none;
    border-right: none;
    width: 100%;
    max-width: 100%;
  }

  .watch-info {
    padding: 1rem 1rem 0;
    padding-left: max(1rem, env(safe-area-inset-left));
    padding-right: max(1rem, env(safe-area-inset-right));
  }

  .watch-title {
    font-size: 1.05rem;
    line-height: 1.4;
    overflow-wrap: anywhere;
  }

  .watch-meta {
    flex-direction: column;
    align-items: stretch;
    gap: 0.5rem;
  }

  .watch-time {
    flex: none;
  }

  .player-status {
    margin-left: max(1rem, env(safe-area-inset-left));
    margin-right: max(1rem, env(safe-area-inset-right));
    padding: 1rem;
    font-size: 0.82rem;
    text-align: center;
  }

  .watch-sidebar {
    padding: 0 max(1rem, env(safe-area-inset-right)) 0 max(1rem, env(safe-area-inset-left));
    border-top: 1px solid var(--border-subtle);
    padding-top: 1.25rem;
  }

  .related-card {
    width: 100%;
    min-width: 0;
  }

  .related-thumb {
    width: 118px;
  }

  .related-title {
    font-size: 0.8rem;
  }

  body.watch-page .toast-container {
    left: max(0.75rem, env(safe-area-inset-left));
    right: max(0.75rem, env(safe-area-inset-right));
    bottom: max(1rem, env(safe-area-inset-bottom));
  }
}

@media (max-width: 480px) {
  .related-thumb {
    width: 100px;
  }

  .meta-stats {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.35rem;
  }

  .stat-item {
    font-size: 0.8rem;
  }
}
