
/* ---------- Tokens ---------- */
:root {
  --bg: #141218;
  --card: #211f26;
  --field: #2b2930;
  --text: #e6e0e9;
  --muted: #cac4d0;
  --accent: #d0bcff;
  --on-accent: #381e72;
  --chip: #4a4458;
  --outline: #938f99;
  --watched-bg: #2d4a35;
  --watched-text: #a5d6a7;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
}

/* ---------- Base ---------- */
body {
  background: var(--bg);
  color: var(--text);
  padding: 20px;
  margin: 0;
  -webkit-tap-highlight-color: transparent;
  font-family: Roboto, system-ui, -apple-system, "Segoe UI", sans-serif;
}

/* ---------- Buttons ---------- */
#search-button,
#watch-button,
#save-button,
#episodes button,
#lang-toggle button,
.nav-btn,
#library-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  height: 40px;
  padding: 0 20px;
  border: 0;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
  cursor: pointer;
  text-decoration: none;
  transition: filter 0.15s, background 0.15s;
}

/* Primary — accent background */
#search-button,
#watch-button,
.nav-btn,
#library-link {
  background: var(--accent);
  color: var(--on-accent);
}

.nav-btn,
.nav-btn:visited,
.nav-btn:hover,
.nav-btn:active,
#library-link,
#library-link:visited,
#library-link:hover,
#library-link:active {
  color: var(--on-accent);
  text-decoration: none;
}

#search-button:hover,
#search-button:active,
#watch-button:hover,
#watch-button:active,
.nav-btn:hover,
.nav-btn:active,
#library-link:hover,
#library-link:active {
  filter: brightness(1.1);
}

/* Secondary — chip background */
#save-button,
#episodes button,
#lang-toggle button {
  background: var(--chip);
  color: var(--text);
}

#save-button:hover,
#save-button:active,
#episodes button:hover:not(.active):not(:disabled),
#episodes button:active:not(.active):not(:disabled),
#lang-toggle button:hover:not(.active):not(:disabled),
#lang-toggle button:active:not(.active):not(:disabled) {
  background: var(--field);
}

/* Active state — accent */
#episodes button.active,
#lang-toggle button.active {
  background: var(--accent);
  color: var(--on-accent);
}

/* Disabled */
#episodes button:disabled,
#lang-toggle button:disabled {
  opacity: 0.35;
  cursor: default;
  background: var(--chip);
}

/* Per-button tweaks */
#episodes button { padding: 0 12px; }
#save-button { margin-top: 12px; }
.nav-btn { margin-bottom: 16px; }
.nav-btn svg,
#library-link svg { flex: 0 0 auto; }

/* Lang toggle container */
#lang-toggle {
  display: flex;
  gap: 8px;
  margin-top: 12px;
}

#lang-toggle[hidden] { display: none; }

/* ---------- Search ---------- */
#search-bar {
  display: flex;
  gap: 8px;
  align-items: center;
}

#search-box {
  flex: 1;
  min-width: 0;
  height: 56px;
  box-sizing: border-box;
  padding: 0 20px;
  font-size: 16px;
  background: var(--field);
  color: var(--text);
  border: 0;
  border-radius: 28px;
}

#search-box:focus { outline: 2px solid var(--accent); }

/* ---------- Cards grid (search results + library) ---------- */
#results {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 12px;
  margin-top: 16px;
}

#results a {
  display: block;
  padding: 8px;
  background: var(--card);
  border-radius: var(--radius-lg);
  color: inherit;
  text-decoration: none;
}

#results a:hover,
#results a:active { background: var(--field); }

#results p {
  margin: 8px 4px 4px;
  font-size: 15px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

#results .card-sub {
  margin: 2px 4px 4px;
  font-size: 12px;
  color: var(--muted);
  -webkit-line-clamp: 1;
  line-clamp: 1;
}

/* ---------- Home shelves ---------- */
#home[hidden] { display: none; }

.shelf { margin-top: 24px; }

.shelf h2 {
  margin: 0 0 12px;
  font-size: 18px;
  font-weight: 500;
}

.shelf-track {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 140px;
  gap: 12px;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
  padding-bottom: 8px;
  scrollbar-width: none;
}

.shelf-track::-webkit-scrollbar { display: none; }

.shelf-card {
  display: block;
  position: relative;
  color: inherit;
  text-decoration: none;
  scroll-snap-align: start;
}

.shelf-card:hover,
.shelf-card:active {
  background: var(--field);
  border-radius: var(--radius-lg);
}

.shelf-title {
  margin: 8px 4px 0;
  font-size: 13px;
  line-height: 1.3;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.shelf-sub {
  margin: 2px 4px 0;
  font-size: 12px;
  color: var(--muted);
}

/* ---------- Watch page ---------- */
#player {
  width: 100%;
  aspect-ratio: 16 / 9;
  margin-top: 12px;
  border: 0;
  border-radius: var(--radius-lg);
}

#episodes {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(72px, 1fr));
  gap: 8px;
  margin-top: 16px;
}

/* Episode range chips */
#ep-ranges {
  display: flex;
  gap: 8px;
  margin-top: 16px;
  overflow-x: auto;
  padding-bottom: 8px;
  scrollbar-width: none;
}

#ep-ranges[hidden] { display: none; }

#ep-ranges::-webkit-scrollbar { display: none; }

.range-chip {
  flex: 0 0 auto;
  height: 36px;
  padding: 0 16px;
  font-size: 13px;
  font-weight: 500;
  white-space: nowrap;
  background: var(--chip);
  color: var(--text);
  border: 0;
  border-radius: 999px;
  cursor: pointer;
}

.range-chip:hover:not(.active) { background: var(--field); }

.range-chip.active {
  background: var(--accent);
  color: var(--on-accent);
}

/* Watched episodes */
#episodes button.watched {
  background: var(--watched-bg);
  color: var(--watched-text);
}

#episodes button.watched:not(.active):hover,
#episodes button.watched:not(.active):active {
  filter: brightness(1.15);
}

/* ---------- Details page ---------- */
.detail-hero {
  display: flex;
  gap: 16px;
  margin-top: 16px;
}

.detail-hero .img-wrap {
  flex: 0 0 auto;
  width: 140px;
}

.detail-info {
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}

.detail-info h1 {
  margin: 0;
  font-size: 22px;
  line-height: 1.2;
}

.detail-info p { margin: 6px 0 0; }

.detail-genres {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 16px;
}

.chip {
  padding: 4px 10px;
  font-size: 12px;
  background: var(--chip);
  color: var(--text);
  border-radius: 999px;
}

#detail-synopsis {
  margin-top: 16px;
  line-height: 1.5;
}

@media (min-width: 640px) {
  .detail-hero .img-wrap { width: 180px; }
}

/* Related section */
.shelf-card.unavailable {
  opacity: 0.45;
  cursor: default;
  pointer-events: none;
}

.relation-badge {
  position: absolute;
  top: 6px;
  left: 6px;
  padding: 3px 8px;
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  background: var(--accent);
  color: var(--on-accent);
  border-radius: 999px;
  z-index: 2;
}

/* ---------- Loader ---------- */
.loader {
  width: 36px;
  height: 36px;
  margin: 32px auto;
  background: var(--accent);
  border-radius: 50%;
  animation: morph 1.2s ease-in-out infinite;
}

@keyframes morph {
  0%   { border-radius: 50%; transform: rotate(0deg) scale(1); }
  25%  { border-radius: 30% 70% 70% 30% / 30% 30% 70% 70%; transform: rotate(90deg) scale(0.9); }
  50%  { border-radius: 20%; transform: rotate(180deg) scale(1); }
  75%  { border-radius: 70% 30% 30% 70% / 70% 70% 30% 30%; transform: rotate(270deg) scale(0.9); }
  100% { border-radius: 50%; transform: rotate(360deg) scale(1); }
}

@media (prefers-reduced-motion: reduce) {
  .loader { animation-duration: 6s; }
}

/* ---------- Image with loading spinner ---------- */
.img-wrap {
  position: relative;
  aspect-ratio: 2 / 3;
  background: var(--field);
  border-radius: var(--radius-md);
  overflow: hidden;
}

.img-wrap img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  opacity: 0;
  transition: opacity 0.3s;
}

.img-wrap img.loaded { opacity: 1; }

.spinner {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 28px;
  height: 28px;
  margin: -14px 0 0 -14px;
  border: 3px solid var(--chip);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

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