:root {
  --bg: #0f1117;
  --surface: #181b24;
  --surface-2: #1f2330;
  --text: #e8eaf0;
  --muted: #8b90a0;
  --accent: #ff6b35;
  --primary: #4f7cff;
  --ok: #3ecf8e;
  --radius: 12px;
  --focus: #ffd166;
  --page-gutter: clamp(14px, 3vw, 48px);
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; scroll-behavior: smooth; }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  line-height: 1.5;
}

a { color: inherit; text-decoration: none; }
img { max-width: 100%; }
button, input, select { font: inherit; }

:where(a, button, input, select, [tabindex]):focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 3px;
  box-shadow: 0 0 0 2px var(--bg);
}

.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0, 0, 0, 0);
  white-space: nowrap; border: 0;
}
.skip-link {
  position: fixed;
  z-index: 100;
  top: 8px; left: 8px;
  padding: 10px 16px;
  border-radius: var(--radius);
  background: var(--primary);
  transform: translateY(-150%);
}
.skip-link:focus { transform: translateY(0); }

/* ---------- Шапка ---------- */

.topbar {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: max(12px, env(safe-area-inset-top)) max(var(--page-gutter), env(safe-area-inset-right))
    12px max(var(--page-gutter), env(safe-area-inset-left));
  background: rgba(24, 27, 36, .92);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid #262a38;
  position: sticky;
  top: 0;
  z-index: 10;
}

.topbar-row { display: flex; align-items: center; gap: 20px; flex: 1; }

.logo { font-size: 22px; font-weight: 800; letter-spacing: -0.5px; }
.logo span { color: var(--accent); }

.searchbar { display: flex; flex: 2; max-width: 480px; gap: 8px; }
.searchbar input {
  flex: 1;
  min-width: 0;
  padding: 10px 14px;
  border-radius: var(--radius);
  border: 1px solid #2c3040;
  background: var(--surface-2);
  color: var(--text);
  font-size: 16px; /* не даёт iOS зумить при фокусе */
}
.searchbar input:focus { border-color: var(--primary); }

.searchbar button,
.btn {
  padding: 10px 18px;
  border-radius: var(--radius);
  border: 1px solid #2c3040;
  background: var(--surface-2);
  color: var(--text);
  font-size: 15px;
  cursor: pointer;
  display: inline-block;
  min-height: 46px;
}
.btn:hover, .searchbar button:hover,
.btn:focus-visible, .searchbar button:focus-visible { border-color: var(--primary); }
.btn-primary { background: var(--primary); border-color: var(--primary); font-weight: 600; }
.btn-accent { background: var(--accent); border-color: var(--accent); font-weight: 600; }
.btn-lg { padding: 14px 26px; font-size: 17px; }

.topbar nav { display: flex; gap: 16px; margin-left: auto; align-items: center; }
.topbar nav a { color: var(--muted); padding: 10px 4px; min-height: 44px; display: inline-flex; align-items: center; }
.topbar nav a:hover { color: var(--text); }
.tg-ok { color: var(--ok) !important; }

main { max-width: 1440px; margin: 0 auto; padding: 32px var(--page-gutter) 72px; }
footer {
  text-align: center;
  color: var(--muted);
  font-size: 13px;
  padding: 24px max(var(--page-gutter), env(safe-area-inset-right))
    max(24px, env(safe-area-inset-bottom)) max(var(--page-gutter), env(safe-area-inset-left));
}

/* ---------- Общие ---------- */

.muted { color: var(--muted); }
.small { font-size: 13px; }
.alert {
  background: #3a2230;
  border: 1px solid #6b3048;
  color: #ffb3c0;
  padding: 12px 16px;
  border-radius: var(--radius);
  margin: 16px 0;
}

.badge {
  display: inline-block;
  font-size: 12px;
  padding: 3px 10px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid #2c3040;
  margin: 4px 4px 0 0;
}
.badge-new { background: #33230f; border-color: var(--accent); color: var(--accent); }
.badge-ok { background: #12291f; border-color: var(--ok); color: var(--ok); }

/* ---------- Герой на главной ---------- */

.hero { text-align: center; padding: 56px 16px; }
.hero-gradient {
  background:
    radial-gradient(ellipse 70% 90% at 50% -10%, rgba(79, 124, 255, .25), transparent),
    radial-gradient(ellipse 40% 60% at 85% 10%, rgba(255, 107, 53, .12), transparent);
  border-radius: 20px;
}
.hero h1 { font-size: clamp(30px, 4vw, 52px); line-height: 1.15; margin-top: 0; }
.hero p { color: var(--muted); max-width: 560px; margin: 16px auto 28px; }
.hero-actions { display: flex; gap: 14px; justify-content: center; flex-wrap: wrap; }

/* ---------- Карточки «мои сериалы» / поиск ---------- */

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(220px, 100%), 1fr));
  gap: 20px;
  margin-top: 20px;
}
.card {
  background: var(--surface);
  border: 1px solid #262a38;
  border-radius: var(--radius);
  overflow: hidden;
  transition: transform .15s, border-color .15s;
}
.card:hover, .card:focus-visible { transform: translateY(-3px); border-color: var(--primary); }
.card img { width: 100%; aspect-ratio: 2/3; object-fit: cover; display: block; }
.card-body { padding: 14px; }
.card-body h3 { margin: 0 0 4px; font-size: 16px; }
.card-body p { margin: 4px 0; }

.progress {
  height: 6px;
  border-radius: 999px;
  background: var(--surface-2);
  overflow: hidden;
  margin: 8px 0 4px;
}
.progress-fill { height: 100%; background: linear-gradient(90deg, var(--primary), var(--ok)); }

/* ---------- Популярное (ряды постеров) ---------- */

.popular { margin-top: 40px; }
.popular h2 { margin-bottom: 14px; }
.poster-row {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 16px;
}
.poster-card { display: flex; flex-direction: column; gap: 2px; }
.poster-wrap {
  position: relative;
  border-radius: var(--radius);
  overflow: hidden;
  border: 1px solid #262a38;
  transition: transform .15s, border-color .15s;
}
.poster-card:hover .poster-wrap,
.poster-card:focus-visible .poster-wrap { transform: translateY(-3px); border-color: var(--primary); }
.poster-wrap img { width: 100%; aspect-ratio: 2/3; object-fit: cover; display: block; }
.rating {
  position: absolute;
  top: 8px; left: 8px;
  background: rgba(15, 17, 23, .85);
  color: var(--ok);
  font-size: 12px;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 999px;
}
.poster-title {
  font-size: 14px;
  font-weight: 600;
  margin-top: 8px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* ---------- Страница сериала ---------- */

.show-header { display: flex; gap: 28px; align-items: flex-start; }
.show-header .poster { width: 220px; border-radius: var(--radius); flex-shrink: 0; }
.show-header h1 { margin-top: 0; }
.actions { display: flex; gap: 12px; align-items: center; margin: 18px 0; flex-wrap: wrap; }
.actions form { margin: 0; }

.stats { display: flex; gap: 12px; flex-wrap: wrap; margin-top: 8px; }
.stat {
  background: var(--surface);
  border: 1px solid #262a38;
  border-radius: var(--radius);
  padding: 10px 18px;
  text-align: center;
}
.stat b { display: block; font-size: 18px; }
.stat span { font-size: 12px; color: var(--muted); }
.stat-new { border-color: var(--accent); }
.stat-new b { color: var(--accent); }

/* ---------- Плеер ---------- */

.player-section { margin-top: 32px; scroll-margin-top: 120px; }
.player-toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}
.player-toolbar select {
  padding: 10px 12px;
  border-radius: var(--radius);
  border: 1px solid #2c3040;
  background: var(--surface-2);
  color: var(--text);
  font-size: 16px;
  max-width: 100%;
  min-height: 46px;
}
.player-close { margin-left: auto; }
.player-frame {
  position: relative;
  width: 100%;
  aspect-ratio: 16/9;
  background: #000;
  border-radius: var(--radius);
  overflow: hidden;
  border: 1px solid #262a38;
}
.player-frame iframe {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  border: 0;
}

/* ---------- Список серий ---------- */

.season { margin-top: 36px; }
.season h2 { border-bottom: 1px solid #262a38; padding-bottom: 8px; }
.episodes { list-style: none; margin: 0; padding: 0; }
.episode {
  display: flex;
  align-items: center;
  gap: 14px;
  min-height: 64px;
  padding: 10px 12px;
  border-radius: 8px;
}
.episode:hover, .episode:focus-within { background: var(--surface); }
.episode.future { opacity: .55; }
.episode.watched .ep-name, .episode.watched .ep-num { color: var(--muted); text-decoration: line-through; }
.episode form { margin: 0; }
.check {
  width: 46px; height: 46px;
  border-radius: 8px;
  border: 1.5px solid #3a3f52;
  background: var(--surface-2);
  color: var(--ok);
  font-size: 16px;
  cursor: pointer;
  line-height: 1;
  flex-shrink: 0;
}
.check:disabled { opacity: .4; cursor: default; }
.episode.watched .check { border-color: var(--ok); }
.ep-play {
  min-width: 118px;
  min-height: 46px;
  padding: 10px 14px;
  border-radius: 8px;
  border: 1.5px solid #3a3f52;
  background: var(--surface-2);
  color: var(--primary);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  line-height: 1.2;
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.ep-play:hover, .ep-play:focus-visible { border-color: var(--primary); }
.ep-info { display: grid; grid-template-columns: 52px minmax(0, 1fr) auto; gap: 10px; align-items: center; flex: 1; min-width: 0; }
.ep-num { font-family: ui-monospace, monospace; color: var(--muted); }
.ep-name { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ep-date { font-size: 13px; white-space: nowrap; }
.upto button {
  min-height: 44px;
  background: none; border: 1px solid transparent; border-radius: 8px; color: var(--muted);
  font-size: 13px; cursor: pointer; padding: 8px 10px;
}
.upto button:hover, .upto button:focus-visible { color: var(--text); border-color: #3a3f52; }

/* «до этой» показываем по наведению только там, где есть мышь */
@media (hover: hover) {
  .episode:not(:hover):not(:focus-within) .upto { opacity: .45; }
}

/* ---------- Адаптивная вёрстка ---------- */

@media (min-width: 1280px) {
  .topbar { min-height: 72px; }
  .logo { font-size: 26px; }
  .card-body h3 { font-size: 18px; }
  .poster-title { font-size: 16px; }
  .show-header .poster { width: 260px; }
}

@media (min-width: 721px) and (max-width: 1024px) {
  .cards { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .show-header .poster { width: 180px; }
  .episode { align-items: flex-start; flex-wrap: wrap; }
  .ep-info { flex-basis: calc(100% - 146px); }
}

@media (max-width: 720px) {
  .topbar {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
    padding: max(8px, env(safe-area-inset-top)) max(14px, env(safe-area-inset-right))
      8px max(14px, env(safe-area-inset-left));
  }
  .topbar-row { justify-content: space-between; }
  .topbar nav { margin-left: 0; gap: 14px; }
  .searchbar { max-width: none; }

  main {
    padding: 18px max(14px, env(safe-area-inset-right))
      max(56px, env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left));
  }

  .hero { padding: 36px 12px; }
  .hero h1 { font-size: clamp(25px, 8vw, 34px); }
  .hero-actions .btn { width: 100%; }

  /* мои сериалы — компактные карточки в 2 колонки */
  .cards { grid-template-columns: repeat(2, 1fr); gap: 12px; }
  .card-body { padding: 10px; }
  .card-body h3 { font-size: 14px; }

  /* популярное — горизонтальная лента со свайпом */
  .poster-row {
    display: flex;
    overflow-x: auto;
    gap: 12px;
    padding-bottom: 8px;
    scroll-snap-type: x mandatory;
    -webkit-overflow-scrolling: touch;
  }
  .poster-row::-webkit-scrollbar { display: none; }
  .poster-card { flex: 0 0 130px; scroll-snap-align: start; }

  /* страница сериала */
  .show-header { flex-direction: column; gap: 14px; }
  .show-header .poster { width: min(42vw, 180px); align-self: center; }
  .show-header h1 { font-size: 21px; }
  .actions .btn { flex: 1; text-align: center; }
  .actions form { flex: 1; display: flex; }
  .actions form .btn { width: 100%; }

  .stats { gap: 8px; }
  .stat { padding: 8px 12px; flex: 1; min-width: 90px; }
  .stat b { font-size: 15px; }

  .player-toolbar { align-items: stretch; flex-direction: column; gap: 8px; }
  .player-toolbar select { width: 100%; }
  .player-close { margin-left: 0; }
  .player-section { scroll-margin-top: 150px; }

  .episode { align-items: stretch; flex-wrap: wrap; gap: 8px; padding: 12px 6px; }
  .ep-play { order: 2; flex: 1; min-width: 0; }
  .ep-info { order: 1; flex-basis: 100%; grid-template-columns: 44px minmax(0, 1fr); }
  .ep-date { grid-column: 2; white-space: normal; }
  .episode form { order: 3; }
  .episode .upto { order: 4; margin-left: auto; opacity: 1; }
  .check { width: 46px; }
}

@media (max-width: 380px) {
  .poster-card { flex-basis: 112px; }
  .cards { grid-template-columns: repeat(2, 1fr); }
}

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after {
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
  }
}

/* ---------- Вход с ПК ---------- */

.login-panel {
  margin: 24px 0;
  padding: 20px;
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid #262a38;
}

.login-panel-alt {
  background: var(--surface-2);
}

.login-panel-title {
  margin: 0 0 12px;
  font-size: 18px;
}

.login-code-box {
  display: inline-block;
  margin: 8px 0 12px;
  padding: 14px 28px;
  border-radius: var(--radius);
  background: #0b0d12;
  border: 2px dashed var(--primary);
  font-size: 32px;
  font-weight: 800;
  letter-spacing: 0.28em;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

.login-command code {
  display: inline-block;
  padding: 8px 12px;
  border-radius: 8px;
  background: #0b0d12;
  font-size: 16px;
}

/* ---------- FM Радио ---------- */

.radio-page {
  padding: 24px var(--page-gutter) 120px;
  max-width: 1100px;
  margin: 0 auto;
}

.radio-hero h1 {
  margin: 0 0 6px;
  font-size: clamp(28px, 4vw, 40px);
  font-weight: 800;
  letter-spacing: -0.03em;
}

.radio-search {
  display: flex;
  gap: 8px;
  margin-top: 16px;
  max-width: 420px;
}

.radio-search input {
  flex: 1;
  min-width: 0;
  padding: 10px 14px;
  border-radius: var(--radius);
  border: 1px solid #2c3142;
  background: var(--surface);
  color: var(--text);
}

.radio-countries,
.radio-genres {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 14px;
}

.genre-pill {
  display: inline-block;
  padding: 6px 12px;
  border-radius: 999px;
  border: 1px solid #2c3142;
  background: var(--surface);
  font-size: 13px;
  color: var(--text);
}

.genre-pill.is-active,
.genre-pill:hover {
  border-color: var(--accent);
  color: #fff;
}

.radio-section {
  margin-top: 36px;
}

.radio-section h2 {
  margin: 0 0 14px;
  font-size: 20px;
}

.station-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 14px;
}

.station-card {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 0;
  border: 0;
  background: transparent;
  color: inherit;
  text-align: left;
  cursor: pointer;
}

.station-card img {
  width: 100%;
  aspect-ratio: 1;
  object-fit: cover;
  border-radius: 10px;
  background: var(--surface-2);
}

.station-card-name {
  font-size: 14px;
  font-weight: 600;
  line-height: 1.25;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

body.has-radio-player {
  padding-bottom: 88px;
}

.radio-player {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 40;
  display: grid;
  grid-template-columns: 56px minmax(0, 1fr) auto;
  gap: 12px 16px;
  align-items: center;
  padding: 10px max(var(--page-gutter), env(safe-area-inset-right))
    max(10px, env(safe-area-inset-bottom)) max(var(--page-gutter), env(safe-area-inset-left));
  background: rgba(15, 17, 23, 0.94);
  backdrop-filter: blur(12px);
  border-top: 1px solid #262a38;
}

.radio-player-cover {
  width: 56px;
  height: 56px;
  border-radius: 8px;
  object-fit: cover;
  background: var(--surface-2);
}

.radio-player-title {
  font-weight: 700;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.radio-player-controls {
  display: flex;
  align-items: center;
  gap: 4px;
}

.btn-icon {
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  font-size: 16px;
}

.btn-icon-main {
  background: var(--text);
  color: #0f1117;
  font-size: 14px;
}

.btn-icon.is-liked { color: var(--accent); }

@media (max-width: 720px) {
  .radio-player-cover { width: 48px; height: 48px; }
}
