/* ================================================================
   天天看剧 (ttkj.tv) — Adaptive streaming theme (dark default, light optional)
   ================================================================ */

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

/* Dark theme (default) */
:root {
  --bg: #0f0f0f;
  --surface: #1a1a1a;
  --card: #222222;
  --card-hover: #2a2a2a;
  --border: #333333;
  --text: #f0f0f0;
  --text-secondary: #a0a0a0;
  --accent: #e63946;
  --accent-bright: #ff4d5a;
  --gold: #f5c518;
  --danger: #ff4444;
  --radius: 8px;
  --radius-sm: 4px;
  --shadow: 0 2px 12px rgba(0,0,0,.6);
  --font: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB",
          "Microsoft YaHei", "Noto Sans CJK SC", "Segoe UI", Roboto, sans-serif;
}

/* Light theme — auto-follows system preference */
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg: #f5f5f5;
    --surface: #ffffff;
    --card: #ffffff;
    --card-hover: #f0f0f0;
    --border: #e0e0e0;
    --text: #1a1a1a;
    --text-secondary: #666666;
    --accent: #e63946;
    --accent-bright: #d32f2f;
    --gold: #d4a810;
    --danger: #d32f2f;
    --shadow: 0 2px 8px rgba(0,0,0,.1);
  }
}

/* Manual override: force light */
:root[data-theme="light"] {
  --bg: #f5f5f5;
  --surface: #ffffff;
  --card: #ffffff;
  --card-hover: #f0f0f0;
  --border: #e0e0e0;
  --text: #1a1a1a;
  --text-secondary: #666666;
  --accent: #e63946;
  --accent-bright: #d32f2f;
  --gold: #d4a810;
  --danger: #d32f2f;
  --shadow: 0 2px 8px rgba(0,0,0,.1);
}

/* Manual override: force dark */
:root[data-theme="dark"] {
  --bg: #0f0f0f;
  --surface: #1a1a1a;
  --card: #222222;
  --card-hover: #2a2a2a;
  --border: #333333;
  --text: #f0f0f0;
  --text-secondary: #a0a0a0;
  --accent: #e63946;
  --accent-bright: #ff4d5a;
  --gold: #f5c518;
  --danger: #ff4444;
  --shadow: 0 2px 12px rgba(0,0,0,.6);
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

a { color: var(--text); text-decoration: none; }
a:hover { color: var(--accent); }

/* ================================================================
   Navigation
   ================================================================ */

.nav {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0 1.25rem;
  display: flex;
  align-items: center;
  height: 56px;
  position: sticky;
  top: 0;
  z-index: 200;
}

.nav-brand {
  font-size: 1.3rem;
  font-weight: 800;
  color: var(--accent);
  letter-spacing: -0.5px;
  margin-right: 1.5rem;
  flex-shrink: 0;
}

.nav-links {
  display: flex;
  gap: 2px;
  align-items: center;
}

.nav-links a {
  color: var(--text-secondary);
  font-size: .9rem;
  padding: .45rem .75rem;
  border-radius: var(--radius-sm);
  transition: color .15s, background .15s;
  white-space: nowrap;
}

.nav-links a:hover {
  color: var(--text);
  background: var(--card);
}

.nav-links a.active {
  color: #fff;
  background: var(--accent);
}

.nav-search {
  margin-left: auto;
  flex-shrink: 0;
}

.nav-search input {
  background: var(--card);
  border: 1px solid var(--border);
  color: var(--text);
  padding: .45rem .9rem;
  border-radius: 20px;
  font-size: .85rem;
  width: 200px;
  transition: border-color .2s, width .2s;
}

.nav-search input:focus {
  outline: none;
  border-color: var(--accent);
  width: 260px;
}

/* Theme toggle button */
.theme-toggle {
  background: none;
  border: 1px solid var(--border);
  color: var(--text);
  width: 36px;
  height: 36px;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  margin-left: .5rem;
  flex-shrink: 0;
  transition: border-color .2s;
}
.theme-toggle:hover { border-color: var(--accent); }

/* Hamburger */
.nav-toggle {
  display: none;
  background: none;
  border: none;
  cursor: pointer;
  width: 36px;
  height: 36px;
  border-radius: var(--radius-sm);
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 5px;
  padding: 6px;
  margin-right: .5rem;
  transition: background .15s;
}

.nav-toggle:hover { background: var(--card); }

.nav-toggle span {
  display: block;
  width: 22px;
  height: 2px;
  background: var(--text);
  border-radius: 2px;
  transition: transform .2s, opacity .2s;
}

/* Mobile */
@media (max-width: 768px) {
  .nav {
    height: auto;
    min-height: 52px;
    flex-wrap: wrap;
    padding: .65rem 1rem;
    gap: .5rem;
  }

  .nav-toggle { display: flex; }

  .nav-links {
    display: none;
    width: 100%;
    flex-direction: column;
    gap: 0;
    order: 10;
    background: var(--surface);
    border-top: 1px solid var(--border);
    margin: .5rem -1rem -.65rem;
    padding: .5rem 0;
  }

  .nav-links.open { display: flex; }

  .nav-links a {
    padding: .7rem 1.25rem;
    border-radius: 0;
    font-size: .95rem;
  }

  .nav-links a:hover,
  .nav-links a.active {
    background: var(--card);
  }

  .nav-search {
    order: 5;
    flex: 1;
    min-width: 0;
  }

  .nav-search input {
    width: 100%;
    border-radius: var(--radius-sm);
  }

  .nav-search input:focus { width: 100%; }
}

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

.container {
  max-width: 1320px;
  margin: 0 auto;
  padding: 1.5rem 1.25rem;
}

@media (max-width: 768px) {
  .container { padding: 1rem .75rem; }
}

/* ================================================================
   Typography & Sections
   ================================================================ */

.section-title {
  font-size: 1.2rem;
  font-weight: 700;
  margin-bottom: 1rem;
  color: var(--text);
  padding-left: .75rem;
  border-left: 3px solid var(--accent);
}

.row-section { margin-bottom: 2.5rem; }

/* ================================================================
   Card Grid
   ================================================================ */

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
  gap: .6rem;
}

@media (min-width: 480px) {
  .card-grid { grid-template-columns: repeat(auto-fill, minmax(145px, 1fr)); gap: .75rem; }
}
@media (min-width: 768px) {
  .card-grid { grid-template-columns: repeat(auto-fill, minmax(165px, 1fr)); gap: 1rem; }
}
@media (min-width: 1100px) {
  .card-grid { grid-template-columns: repeat(auto-fill, minmax(175px, 1fr)); }
}

.card {
  background: var(--card);
  border-radius: var(--radius);
  overflow: hidden;
  transition: transform .2s, box-shadow .2s;
}

.card:hover {
  transform: translateY(-4px);
  box-shadow: var(--shadow);
  background: var(--card-hover);
}

.card a { display: block; color: inherit; text-decoration: none; }

.card-img {
  width: 100%;
  aspect-ratio: 2 / 3;
  object-fit: cover;
  display: block;
  background: var(--surface);
}

.card-body { padding: .5rem .6rem .6rem; }

.card-title {
  font-size: .82rem;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  line-height: 1.3;
}

.card-meta {
  font-size: .72rem;
  color: var(--text-secondary);
  margin-top: .2rem;
}

/* ================================================================
   Tabs
   ================================================================ */

.tabs {
  display: flex;
  gap: .4rem;
  margin-bottom: 1.25rem;
  flex-wrap: wrap;
}

.tab {
  padding: .4rem .85rem;
  border-radius: 20px;
  font-size: .85rem;
  cursor: pointer;
  color: var(--text-secondary);
  background: transparent;
  border: 1px solid var(--border);
  transition: all .15s;
}

.tab:hover {
  color: var(--text);
  border-color: var(--accent);
  text-decoration: none;
}

.tab.active {
  color: #fff;
  background: var(--accent);
  border-color: var(--accent);
  text-decoration: none;
}

/* ================================================================
   Filters
   ================================================================ */

.filter-section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  margin-bottom: 1.5rem;
}

.filter-row {
  display: flex;
  align-items: flex-start;
  gap: .75rem;
  margin-bottom: .6rem;
}

.filter-row:last-child { margin-bottom: 0; }

.filter-label {
  font-size: .85rem;
  color: var(--text-secondary);
  min-width: 2.5rem;
  padding-top: .4rem;
  flex-shrink: 0;
}

.filter-row .tabs {
  margin-bottom: 0;
}

.filter-row .tab {
  padding: .25rem .6rem;
  font-size: .8rem;
}

@media (max-width: 768px) {
  .filter-section { padding: .75rem; }
  .filter-row { gap: .5rem; }
  .filter-label { min-width: 2rem; font-size: .8rem; }
  .filter-row .tab { padding: .2rem .45rem; font-size: .75rem; }
}

/* ── Sort bar (separate from filters) ── */

.sort-bar {
  display: flex;
  gap: .5rem;
  margin-bottom: 1.25rem;
  align-items: center;
  border-bottom: 1px solid var(--border);
  padding-bottom: .75rem;
}

.sort-btn {
  padding: .4rem .75rem;
  font-size: .85rem;
  color: var(--text-secondary);
  cursor: pointer;
  transition: color .15s;
  border: none;
  background: none;
  font-family: var(--font);
}

.sort-btn:hover { color: var(--text); text-decoration: none; }

.sort-btn.active {
  color: var(--accent);
  font-weight: 600;
  text-decoration: none;
}

.sort-arrow {
  font-size: .7rem;
  margin-left: .15rem;
}

/* ── Clickable tags on play page ── */

.tag.clickable {
  cursor: pointer;
  transition: background .15s, border-color .15s;
  text-decoration: none;
  color: var(--text);
}

.tag.clickable:hover {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  text-decoration: none;
}

/* ================================================================
   Pagination
   ================================================================ */

.pagination {
  display: flex;
  gap: .3rem;
  justify-content: center;
  margin-top: 2rem;
  flex-wrap: wrap;
}

.page-btn {
  padding: .4rem .7rem;
  border-radius: var(--radius-sm);
  font-size: .85rem;
  color: var(--text-secondary);
  background: var(--card);
  border: 1px solid var(--border);
  cursor: pointer;
  transition: all .15s;
}

.page-btn:hover {
  color: var(--text);
  border-color: var(--accent);
  text-decoration: none;
}

.page-btn.active {
  color: #fff;
  background: var(--accent);
  border-color: var(--accent);
  pointer-events: none;
  text-decoration: none;
}

/* ================================================================
   Player
   ================================================================ */

.player-wrap {
  background: #000;
  border-radius: var(--radius);
  overflow: hidden;
  margin-bottom: 1.5rem;
}

.player-wrap video {
  width: 100%;
  display: block;
  max-height: 70vh;
}

.show-info {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 1.25rem;
  margin-bottom: 1.5rem;
  border: 1px solid var(--border);
}

.show-info h1 {
  font-size: 1.35rem;
  margin-bottom: .4rem;
  line-height: 1.3;
}

.show-info .meta {
  color: var(--text-secondary);
  font-size: .85rem;
  margin-bottom: .75rem;
  display: flex;
  flex-wrap: wrap;
  gap: .4rem;
  align-items: center;
}

.show-info .tag {
  background: var(--card);
  border: 1px solid var(--border);
  padding: .1rem .5rem;
  border-radius: 4px;
  font-size: .78rem;
}

.show-info .rating {
  color: var(--gold);
  font-weight: 700;
  font-size: 1.05rem;
}

.show-info .credits {
  font-size: .88rem;
  color: var(--text-secondary);
  line-height: 1.6;
  margin-top: .3rem;
}
.show-info .credits-label {
  color: var(--text);
  font-weight: 500;
}
.show-info .desc {
  font-size: .88rem;
  color: var(--text-secondary);
  line-height: 1.7;
}

/* ================================================================
   Episodes
   ================================================================ */

.ep-section { margin-bottom: 1.5rem; }

.ep-section h3 {
  font-size: .95rem;
  margin-bottom: .6rem;
  color: var(--text-secondary);
}

.ep-grid {
  display: flex;
  flex-wrap: wrap;
  gap: .35rem;
}

.ep-btn {
  padding: .4rem .65rem;
  border-radius: var(--radius-sm);
  font-size: .82rem;
  color: var(--text-secondary);
  background: var(--card);
  border: 1px solid var(--border);
  cursor: pointer;
  min-width: 2.8rem;
  text-align: center;
  transition: all .12s;
}

.ep-btn:hover {
  color: var(--text);
  border-color: var(--accent);
  text-decoration: none;
}

.ep-btn.active {
  color: #fff;
  background: var(--accent);
  border-color: var(--accent);
  text-decoration: none;
}

/* ================================================================
   Search
   ================================================================ */

.search-box { margin-bottom: 1.5rem; }

.search-box input {
  background: var(--card);
  border: 1px solid var(--border);
  color: var(--text);
  padding: .7rem 1.1rem;
  border-radius: var(--radius);
  font-size: 1rem;
  font-family: var(--font);
  width: 100%;
  max-width: 520px;
  transition: border-color .2s;
}

.search-box input:focus {
  outline: none;
  border-color: var(--accent);
}

/* ================================================================
   Ranking
   ================================================================ */

.rank-list { list-style: none; }

.rank-item {
  display: flex;
  align-items: center;
  gap: .85rem;
  padding: .7rem .5rem;
  border-bottom: 1px solid var(--border);
  transition: background .12s;
}

.rank-item:last-child { border-bottom: none; }
.rank-item:hover { background: var(--card); border-radius: var(--radius-sm); }

.rank-num {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--text-secondary);
  min-width: 2rem;
  text-align: center;
}

.rank-item:nth-child(1) .rank-num,
.rank-item:nth-child(2) .rank-num,
.rank-item:nth-child(3) .rank-num {
  color: var(--gold);
  font-size: 1.3rem;
}

.rank-img {
  width: 52px;
  height: 72px;
  object-fit: cover;
  border-radius: var(--radius-sm);
  background: var(--surface);
  flex-shrink: 0;
}

.rank-info { flex: 1; min-width: 0; }

.rank-title {
  font-size: .9rem;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--text);
}

.rank-title:hover { color: var(--accent-bright); }

.rank-meta {
  font-size: .78rem;
  color: var(--text-secondary);
  margin-top: .15rem;
}

.rank-score {
  font-size: 1rem;
  font-weight: 700;
  color: var(--gold);
  flex-shrink: 0;
}

@media (max-width: 768px) {
  .rank-item { gap: .6rem; padding: .55rem .25rem; }
  .rank-img { width: 44px; height: 60px; }
}

/* ================================================================
   Error & Empty states
   ================================================================ */

.error-msg {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2rem 1rem;
  color: var(--text-secondary);
  text-align: center;
  margin: 2rem 0;
}

.not-found {
  text-align: center;
  padding: 5rem 1rem;
}

.not-found h1 {
  font-size: 5rem;
  color: var(--accent);
  margin-bottom: .5rem;
  line-height: 1;
}

.not-found p {
  color: var(--text-secondary);
  font-size: 1.1rem;
}

/* ================================================================
   HTMX loading states
   ================================================================ */

.htmx-indicator { opacity: 0; transition: opacity .15s; }
.htmx-request .htmx-indicator { opacity: 1; }
.htmx-request { opacity: .6; pointer-events: none; transition: opacity .15s; }

/* ================================================================
   Scrollbar (webkit)
   ================================================================ */

::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: var(--bg); }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-secondary); }

/* ================================================================
   UX Improvements (from UI/UX audit)
   ================================================================ */

/* Focus states for keyboard navigation */
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.tab:focus-visible,
.ep-btn:focus-visible,
.page-btn:focus-visible,
.sort-btn:focus-visible,
.card:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Minimum touch targets (44px) */
.tab { min-height: 36px; display: inline-flex; align-items: center; }
.ep-btn { min-height: 36px; min-width: 36px; display: inline-flex; align-items: center; justify-content: center; }
.sort-btn { min-height: 40px; }

@media (max-width: 768px) {
  .tab { min-height: 40px; padding: .35rem .65rem; }
  .ep-btn { min-height: 40px; min-width: 40px; }
}

/* Filter row horizontal scroll on mobile (instead of wrapping 29 genres) */
@media (max-width: 768px) {
  .filter-row .tabs {
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    padding-bottom: 4px;
  }
  .filter-row .tabs::-webkit-scrollbar { display: none; }
  .filter-row .tab { flex-shrink: 0; }
}

/* Card hover overlay with extra info (desktop) */
.card { position: relative; }
.card-overlay {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: linear-gradient(transparent, rgba(0,0,0,.85));
  padding: .5rem .6rem;
  opacity: 0;
  transition: opacity .2s;
}
.card:hover .card-overlay { opacity: 1; }
.card-overlay-text { font-size: .7rem; color: var(--text-secondary); }

/* Section "see more" link */
.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1rem;
}

.section-more {
  font-size: .85rem;
  color: var(--text-secondary);
  transition: color .15s;
}

.section-more:hover {
  color: var(--accent-bright);
  text-decoration: none;
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
  .card:hover { transform: none; }
}

/* Video poster placeholder */
.player-wrap video {
  background: var(--surface);
}

/* Cursor pointer on all clickable things */
.tab, .ep-btn, .page-btn, .sort-btn, .card a, .tag.clickable, .nav-toggle, .search-tag {
  cursor: pointer;
}

/* ── Search empty state ── */

.search-empty {
  text-align: center;
  padding: 4rem 1rem;
  color: var(--text-secondary);
}

.search-empty-icon {
  font-size: 3rem;
  margin-bottom: 1rem;
  opacity: .5;
}

.search-empty p {
  font-size: 1rem;
  margin-bottom: 1.5rem;
}

.search-suggestions {
  display: flex;
  gap: .5rem;
  justify-content: center;
  flex-wrap: wrap;
}

.search-tag {
  padding: .4rem .8rem;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 20px;
  font-size: .85rem;
  color: var(--text-secondary);
  transition: all .15s;
}

.search-tag:hover {
  color: var(--text);
  border-color: var(--accent);
  background: var(--card-hover);
}

/* ── Card rating badge ── */

.card-rating {
  position: absolute;
  top: .4rem;
  right: .4rem;
  background: rgba(0,0,0,.7);
  color: var(--gold);
  font-size: .7rem;
  font-weight: 700;
  padding: .15rem .35rem;
  border-radius: 4px;
  backdrop-filter: blur(4px);
}

/* ── Sort bar desc/asc state ── */

.sort-btn .sort-arrow {
  display: inline-block;
  font-size: .7rem;
  margin-left: .2rem;
  transition: transform .15s;
}

.sort-btn.active .sort-arrow {
  color: var(--accent);
}

/* ================================================================
   Skip-to-content (Fix 2)
   ================================================================ */

.skip-link { position: absolute; left: -9999px; top: 0; z-index: 999; background: var(--accent); color: #fff; padding: .5rem 1rem; }
.skip-link:focus { left: 0; }

/* ================================================================
   Skeleton loading (Fix 4)
   ================================================================ */

@keyframes skeleton-pulse { 0% { opacity: .6; } 50% { opacity: .3; } 100% { opacity: .6; } }
.htmx-request .card { animation: skeleton-pulse 1.5s ease-in-out infinite; }
.htmx-request .card-img { background: var(--border); }
.htmx-request .card-title, .htmx-request .card-meta { color: transparent; background: var(--border); border-radius: 4px; }

/* ================================================================
   Breadcrumb (Fix 5)
   ================================================================ */

/*
 * Related grid — uses flexbox instead of CSS grid so items
 * distribute evenly regardless of viewport width.
 * Each card has a flex-basis that adapts, and the last row
 * naturally fills from left without awkward gaps.
 */
.related-grid {
  display: flex;
  flex-wrap: wrap;
  gap: .75rem;
}
.related-grid .card {
  flex: 0 0 calc((100% - 4 * .75rem) / 5);  /* 5 per row desktop */
  max-width: calc((100% - 4 * .75rem) / 5);
}
@media (max-width: 1024px) {
  .related-grid .card {
    flex: 0 0 calc((100% - 3 * .75rem) / 4);  /* 4 per row tablet */
    max-width: calc((100% - 3 * .75rem) / 4);
  }
}
@media (max-width: 768px) {
  .related-grid .card {
    flex: 0 0 calc((100% - 2 * .75rem) / 3);  /* 3 per row small tablet */
    max-width: calc((100% - 2 * .75rem) / 3);
  }
}
@media (max-width: 480px) {
  .related-grid .card {
    flex: 0 0 calc((100% - 1 * .75rem) / 2);  /* 2 per row phone */
    max-width: calc((100% - 1 * .75rem) / 2);
  }
}

.breadcrumb { font-size: .85rem; color: var(--text-secondary); margin-bottom: 1rem; }
.breadcrumb a { color: var(--text-secondary); }
.breadcrumb a:hover { color: var(--accent-bright); }
.breadcrumb span { color: var(--text); }

/* ================================================================
   Back-to-top (Fix 6)
   ================================================================ */

.back-to-top { position: fixed; bottom: 2rem; right: 2rem; width: 40px; height: 40px; border-radius: 50%; background: var(--accent); color: #fff; border: none; font-size: 1.2rem; cursor: pointer; opacity: 0; transition: opacity .2s; z-index: 100; }
.back-to-top.visible { opacity: 1; }
