/* Adaptive layout: phone / tablet / desktop / Smart TV */

:root {
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --touch-min: 44px;
  --container-pad: 1rem;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  scroll-behavior: smooth;
  width: 100%;
  max-width: 100%;
  overflow-x: hidden;
  overscroll-behavior-x: none;
}

body {
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
  padding-bottom: var(--safe-bottom);
  width: 100%;
  max-width: 100%;
  overflow-x: hidden;
  overscroll-behavior-x: none;
  touch-action: pan-y pinch-zoom;
}

.container {
  width: min(var(--container-max, 1200px), calc(100% - 2 * var(--container-pad)));
  padding-left: 0;
  padding-right: 0;
}

.header {
  padding-top: var(--safe-top);
}

.header__inner {
  gap: 0.65rem;
}

.nav {
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}

.nav::-webkit-scrollbar {
  display: none;
}

.nav__btn,
.header-bell,
.header__logout,
.filters-toggle,
.pagination button,
.user-list-bar button,
.lists-tabs button,
.score-row button,
.sources button,
.episodes button,
.nav-drop__menu button {
  min-height: var(--touch-min);
}

.header__logout,
.header-bell {
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

/* Focus for keyboard / Smart TV remote */
.js-focus :focus-visible,
.nav__btn:focus-visible,
.card:focus-visible,
.reco-card:focus-visible,
button:focus-visible,
a:focus-visible,
select:focus-visible,
input:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

.card:focus-visible,
.reco-card:focus-visible {
  border-color: var(--accent);
  transform: translateY(-4px);
}

/* PWA install banner */
.pwa-install {
  position: fixed;
  z-index: 80;
  left: max(0.75rem, var(--safe-left));
  right: max(0.75rem, var(--safe-right));
  bottom: max(0.75rem, var(--safe-bottom));
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  align-items: center;
  justify-content: space-between;
  padding: 0.85rem 1rem;
  background: rgba(22, 22, 26, 0.96);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
  backdrop-filter: blur(10px);
}

.pwa-install[hidden] {
  display: none !important;
}

.pwa-install__text {
  display: grid;
  gap: 0.15rem;
  min-width: 0;
}

.pwa-install__text strong {
  font-size: 0.95rem;
}

.pwa-install__text span {
  color: var(--muted);
  font-size: 0.82rem;
}

.pwa-install__actions {
  display: flex;
  gap: 0.45rem;
  flex-wrap: wrap;
}

.pwa-install__actions .auth-submit {
  margin: 0;
  padding: 0.55rem 0.9rem;
}

/* Standalone (installed app) */
@media (display-mode: standalone) {
  .pwa-install {
    display: none !important;
  }

  body {
    overscroll-behavior-y: contain;
  }
}

/* ——— Phones ——— */
@media (max-width: 720px) {
  :root {
    --container-pad: 0.75rem;
    --touch-min: 42px;
  }

  .container {
    width: min(100%, calc(100% - 2 * var(--container-pad)));
  }

  .logo {
    font-size: 1.2rem;
  }

  .header__inner {
    flex-wrap: wrap;
    min-height: 56px;
    padding: 0.45rem 0;
  }

  .nav {
    order: 0;
    flex: 0 0 auto;
    width: auto;
  }

  .header__user {
    order: 2;
    margin-left: auto;
    max-width: calc(100% - 5rem);
    gap: 0.35rem;
    flex-wrap: wrap;
    justify-content: flex-end;
  }

  .header__email {
    max-width: 7.5rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .header,
  .header__inner,
  .header__user,
  .toolbar,
  .filters-panel,
  .auth-card,
  .modal__panel {
    max-width: 100%;
  }

  .search,
  .select-wrap,
  .select-wrap select {
    min-width: 0;
    width: 100%;
  }

  .nav-drop__menu {
    max-width: calc(100vw - 1.5rem);
  }

  .toolbar {
    margin: 1rem 0;
    gap: 0.65rem;
  }

  .toolbar__right {
    width: 100%;
  }

  .toolbar__right .select-wrap {
    flex: 1;
  }

  .toolbar__right .select-wrap select {
    width: 100%;
    min-width: 0;
  }

  .catalog {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.7rem;
  }

  .card__body {
    padding: 0.55rem 0.6rem 0.7rem;
  }

  .card__title {
    font-size: 0.85rem;
  }

  .card:hover {
    transform: none;
  }

  .modal {
    align-items: stretch;
  }

  .modal__panel {
    width: 100%;
    max-width: 100%;
    max-height: none;
    min-height: 100%;
    border-radius: 0;
    margin: 0;
  }

  .modal__close {
    top: max(0.5rem, var(--safe-top));
    right: max(0.5rem, var(--safe-right));
    width: 44px;
    height: 44px;
  }

  .detail {
    grid-template-columns: 1fr;
    gap: 1rem;
  }

  .detail__poster {
    max-width: 180px;
    margin: 0 auto;
  }

  .player-wrap {
    aspect-ratio: 16 / 9;
  }

  .auth-gate {
    padding: 1rem var(--container-pad);
    align-items: flex-start;
  }

  .auth-card {
    width: 100%;
    max-width: 100%;
    margin: 1rem 0;
  }

  .reco-rail {
    grid-auto-columns: minmax(120px, 42vw);
  }

  .pwa-install {
    flex-direction: column;
    align-items: stretch;
  }

  .pwa-install__actions {
    width: 100%;
  }

  .pwa-install__actions .auth-submit,
  .pwa-install__actions .header__logout {
    flex: 1;
  }
}

/* Very small phones */
@media (max-width: 380px) {
  .catalog {
    gap: 0.5rem;
  }

  .header__user .nav__btn {
    padding: 0.35rem 0.55rem;
    font-size: 0.85rem;
  }
}

/* ——— Tablets ——— */
@media (min-width: 721px) and (max-width: 1100px) {
  :root {
    --container-max: 1000px;
  }

  .catalog {
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  }

  .header__inner {
    flex-wrap: wrap;
  }

  .nav {
    flex: 1 1 auto;
    min-width: 0;
  }

  .modal__panel {
    width: min(920px, calc(100% - 2rem));
  }
}

/* Landscape phones / small tablets */
@media (max-height: 480px) and (orientation: landscape) {
  .auth-gate {
    align-items: flex-start;
    overflow-y: auto;
  }

  .modal__panel {
    max-height: 100%;
  }

  .player-wrap {
    max-height: 70vh;
  }
}

/* ——— Large desktop ——— */
@media (min-width: 1400px) {
  :root {
    --container-max: 1360px;
  }

  .catalog {
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
    gap: 1.25rem;
  }
}

/* ——— Smart TV / 1080p+ living room ——— */
@media (min-width: 1600px) {
  :root {
    --container-max: 1600px;
    --touch-min: 52px;
  }

  html {
    font-size: 18px;
  }

  .header__inner {
    min-height: 76px;
  }

  .logo {
    font-size: 1.6rem;
  }

  .nav__btn {
    padding: 0.65rem 1.1rem;
    font-size: 1.05rem;
  }

  .catalog {
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 1.4rem;
  }

  .card__title {
    font-size: 1.05rem;
  }

  .search input,
  .select-wrap select {
    padding: 1rem 1.1rem;
    font-size: 1.05rem;
  }

  .modal__panel {
    width: min(1280px, calc(100% - 4rem));
  }

  .player-wrap {
    min-height: 420px;
  }
}

/* 4K TV */
@media (min-width: 2560px) {
  :root {
    --container-max: 2200px;
  }

  html {
    font-size: 22px;
  }

  .catalog {
    grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  }
}

/* Hover-capable only (skip sticky hover on touch) */
@media (hover: none) {
  .card:hover,
  .reco-card:hover {
    transform: 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;
    scroll-behavior: auto !important;
  }
}

/* Print / rare */
@media print {
  .header,
  .pwa-install,
  .auth-gate,
  .pagination {
    display: none !important;
  }
}
