/* Kinoportal redesign — Stage 8: final performance, motion and responsive polish */

:root {
  --kp-ad-radius: clamp(16px, 2vw, 24px);
  --kp-motion-fast: 160ms;
  --kp-motion-base: 280ms;
  --kp-motion-slow: 560ms;
  --kp-ease-out: cubic-bezier(.2, .8, .2, 1);
}

html {
  scroll-behavior: smooth;
  scrollbar-gutter: stable;
}

body:not(.admin-body) {
  min-width: 320px;
  overflow-x: clip;
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
}

body:not(.admin-body),
body:not(.admin-body) *,
body:not(.admin-body) *::before,
body:not(.admin-body) *::after {
  box-sizing: border-box;
}

body:not(.admin-body) :where(img, video, iframe, svg, canvas) {
  max-inline-size: 100%;
}

body:not(.admin-body) img {
  height: auto;
}

body:not(.admin-body) :where(button, a, input, select, textarea, summary) {
  -webkit-tap-highlight-color: transparent;
}

body:not(.admin-body) :where(button, [role="button"], input, select, textarea, a) {
  outline-offset: 3px;
}

body:not(.admin-body) :where(button, [role="button"], input, select, textarea, a):focus-visible {
  outline: 3px solid color-mix(in srgb, var(--kp-cyan, #22d3ee) 80%, #fff 20%);
  outline-offset: 3px;
}

body:not(.admin-body) :where(button, input, select, textarea) {
  font: inherit;
}

body:not(.admin-body) :where(.home-button, .kp-button, .btn, button, [role="button"]) {
  touch-action: manipulation;
}

body:not(.admin-body) table {
  max-width: 100%;
}

body:not(.admin-body) :where(.table-wrap, .responsive-table, .forum-table-wrap) {
  overflow-x: auto;
  overscroll-behavior-inline: contain;
  scrollbar-width: thin;
}

/* Below-the-fold rendering: improves long catalog/editorial pages without changing layout. */
@supports (content-visibility: auto) {
  body:not(.admin-body) :where(
    .home-section:nth-of-type(n+3),
    .catalog-grid,
    .editorial-grid,
    .articles-grid,
    .reviews-grid,
    .quiz-list,
    .forum-list,
    .profile-section,
    .title-detail__section
  ) {
    content-visibility: auto;
    contain-intrinsic-size: auto 760px;
  }
}

/* Final reveal animation is added only by JS, so content remains visible without JS. */
html.kp-final-ui:not(.kp-reduced-motion) [data-kp-final-reveal] {
  opacity: 0;
  transform: translateY(18px);
  transition:
    opacity var(--kp-motion-slow) ease,
    transform var(--kp-motion-slow) var(--kp-ease-out);
}

html.kp-final-ui:not(.kp-reduced-motion) [data-kp-final-reveal].is-kp-visible {
  opacity: 1;
  transform: none;
}

/* Image loading feedback and graceful failures. */
body:not(.admin-body) .kp-media-loading {
  position: relative;
  overflow: hidden;
  background: color-mix(in srgb, var(--kp-surface-2, #172033) 92%, #fff 8%);
}

body:not(.admin-body) .kp-media-loading::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  background: linear-gradient(
    105deg,
    transparent 20%,
    rgba(255, 255, 255, .08) 42%,
    rgba(255, 255, 255, .16) 50%,
    rgba(255, 255, 255, .08) 58%,
    transparent 80%
  );
  transform: translateX(-120%);
  animation: kp-final-shimmer 1.35s linear infinite;
}

body:not(.admin-body) .kp-media-loading > img {
  z-index: 1;
  opacity: 1;
  transition: opacity var(--kp-motion-base) ease;
}

body:not(.admin-body) .kp-media-ready::before {
  display: none;
}

body:not(.admin-body) .kp-media-ready > img {
  opacity: 1;
}

body:not(.admin-body) .kp-media-error {
  min-height: 120px;
  background-color: color-mix(in srgb, var(--kp-surface-2, #172033) 88%, #000 12%);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 160 120'%3E%3Cg fill='none' stroke='%2394a3b8' stroke-width='5' stroke-linecap='round' stroke-linejoin='round' opacity='.75'%3E%3Crect x='24' y='18' width='112' height='84' rx='12'/%3E%3Ccircle cx='58' cy='49' r='9'/%3E%3Cpath d='m37 88 27-27 19 18 14-13 26 22'/%3E%3C/g%3E%3C/svg%3E");
  background-position: center;
  background-repeat: no-repeat;
  background-size: min(46%, 160px) auto;
}

body:not(.admin-body) .kp-media-error > img {
  opacity: 0 !important;
}

@keyframes kp-final-shimmer {
  to { transform: translateX(120%); }
}

/* Pause non-essential movement while tab is hidden. */
html.kp-page-hidden *,
html.kp-page-hidden *::before,
html.kp-page-hidden *::after {
  animation-play-state: paused !important;
}

/* Cards: one consistent depth/interaction language. */
@media (hover: hover) and (pointer: fine) {
  body:not(.admin-body) :where(
    .catalog-card,
    .editorial-card,
    .home-title-card,
    .home-reading-card,
    .quiz-card,
    .forum-card,
    .profile-card,
    .collection-card
  ) {
    transition:
      transform var(--kp-motion-base) var(--kp-ease-out),
      box-shadow var(--kp-motion-base) ease,
      border-color var(--kp-motion-base) ease;
  }

  body:not(.admin-body) :where(
    .catalog-card,
    .editorial-card,
    .home-title-card,
    .home-reading-card,
    .quiz-card,
    .forum-card,
    .profile-card,
    .collection-card
  ):hover {
    transform: translateY(-5px);
  }
}

/* Back-to-top appears only after useful scrolling. */
body:not(.admin-body) .kp-back-top {
  opacity: .45;
  transform: translateY(4px);
  transition: opacity var(--kp-motion-fast) ease, transform var(--kp-motion-fast) ease;
}

body:not(.admin-body).kp-has-scrolled .kp-back-top {
  opacity: 1;
  transform: none;
}

/* Advertising: compact, non-destructive and dismissible for the current tab. */
.kp-inline-banner-wrap {
  position: relative;
  width: min(calc(100% - clamp(20px, 5vw, 64px)), 1180px);
  margin: clamp(12px, 1.8vw, 22px) auto;
  isolation: isolate;
}

.kp-inline-banner {
  position: relative;
  width: 100%;
  min-height: 74px;
  max-height: 124px;
  padding: 8px;
  display: grid;
  place-items: center;
  overflow: hidden;
  border: 1px solid var(--kp-border, rgba(148, 163, 184, .22));
  border-radius: var(--kp-ad-radius);
  background:
    radial-gradient(circle at 20% 0%, rgba(124, 58, 237, .14), transparent 34%),
    linear-gradient(135deg, color-mix(in srgb, var(--kp-surface-solid, #111827) 94%, #fff 6%), var(--kp-surface-solid, #111827));
  box-shadow: 0 16px 42px rgba(4, 7, 14, .17);
  line-height: 0;
  text-decoration: none;
  transition: transform var(--kp-motion-base) var(--kp-ease-out), border-color var(--kp-motion-base) ease, box-shadow var(--kp-motion-base) ease;
}

.kp-inline-banner img {
  width: auto;
  max-width: 100%;
  height: auto;
  max-height: 108px;
  display: block;
  object-fit: contain;
  object-position: center;
  border-radius: calc(var(--kp-ad-radius) - 8px);
}

.kp-inline-banner-label,
.kp-side-banner__badge {
  position: absolute;
  z-index: 4;
  top: 10px;
  left: 10px;
  min-height: 23px;
  padding: 0 8px;
  display: inline-flex;
  align-items: center;
  border: 1px solid rgba(255, 255, 255, .22);
  border-radius: 999px;
  background: rgba(4, 7, 14, .72);
  color: rgba(255, 255, 255, .9);
  font: 800 9px/1 system-ui, -apple-system, "Segoe UI", sans-serif;
  letter-spacing: .09em;
  text-transform: uppercase;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

.kp-ad-dismiss {
  position: absolute;
  z-index: 8;
  top: 8px;
  right: 8px;
  width: 30px;
  height: 30px;
  padding: 0;
  display: grid;
  place-items: center;
  border: 1px solid rgba(255, 255, 255, .22);
  border-radius: 50%;
  background: rgba(4, 7, 14, .76);
  color: #fff;
  box-shadow: 0 7px 22px rgba(0, 0, 0, .2);
  font-size: 19px;
  line-height: 1;
  cursor: pointer;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

.kp-ad-dismiss:hover {
  background: rgba(244, 63, 94, .9);
}

.kp-inline-banner-wrap.is-kp-ad-hidden,
.kp-side-banner.is-kp-ad-hidden {
  display: none !important;
}

@media (hover: hover) and (pointer: fine) {
  a.kp-inline-banner:hover {
    transform: translateY(-2px);
    border-color: color-mix(in srgb, var(--kp-primary, #7c3aed) 46%, transparent);
    box-shadow: 0 22px 52px rgba(4, 7, 14, .23);
  }
}

/* Side banners are now contained instead of stretched/cropped. */
.kp-page-frame {
  width: min(100% - 28px, 1920px);
  margin: 0 auto;
  padding: 20px 0;
  display: grid;
  grid-template-columns: minmax(160px, 250px) minmax(0, 1fr) minmax(160px, 250px);
  gap: clamp(16px, 2vw, 26px);
  align-items: start;
}

.kp-page-content {
  min-width: 0;
}

.kp-side-banner {
  position: sticky;
  top: 92px;
  min-width: 0;
}

.kp-side-banner.is-empty {
  display: none !important;
}

.kp-side-banner__link,
.kp-side-banner__box {
  position: relative;
  min-height: 160px;
  padding: 8px;
  display: grid;
  place-items: center;
  overflow: hidden;
  border: 1px solid var(--kp-border, rgba(148, 163, 184, .22));
  border-radius: 20px;
  background: var(--kp-surface-solid, #111827);
  box-shadow: 0 16px 40px rgba(4, 7, 14, .16);
}

.kp-side-banner__image {
  width: auto;
  max-width: 100%;
  height: auto;
  max-height: calc(100vh - 210px);
  display: block;
  object-fit: contain;
  border-radius: 13px;
}

.kp-ad-dismiss--side {
  top: 8px;
  right: 8px;
}

/* Mobile/tablet final pass. */
@media (max-width: 1199px) {
  .kp-page-frame {
    width: 100%;
    padding: 0;
    display: block;
  }

  .kp-side-banner {
    display: none !important;
  }
}

@media (max-width: 900px) {
  html {
    scroll-padding-top: 76px;
  }

  body:not(.admin-body) :where(button, [role="button"], .home-button, .kp-button, .btn) {
    min-height: 42px;
  }

  .kp-inline-banner-wrap {
    width: calc(100% - 24px);
    margin: 12px auto;
  }

  .kp-inline-banner {
    min-height: 68px;
    max-height: 100px;
    padding: 6px;
    border-radius: 18px;
  }

  .kp-inline-banner img {
    max-height: 88px;
    border-radius: 13px;
  }
}

@media (max-width: 640px) {
  body:not(.admin-body) {
    padding-bottom: env(safe-area-inset-bottom, 0px);
  }

  body:not(.admin-body) :where(input:not([type="checkbox"]):not([type="radio"]), select, textarea) {
    font-size: 16px; /* prevents iOS zoom */
  }

  body:not(.admin-body) :where(.modal, [role="dialog"]) {
    max-width: calc(100vw - 16px);
  }

  .kp-inline-banner-wrap {
    width: calc(100% - 16px);
    margin: 9px auto;
  }

  .kp-inline-banner {
    min-height: 58px;
    max-height: 84px;
    padding: 5px;
    border-radius: 15px;
  }

  .kp-inline-banner img {
    max-height: 72px;
    border-radius: 11px;
  }

  .kp-inline-banner-label {
    top: 7px;
    left: 7px;
    min-height: 19px;
    padding-inline: 6px;
    font-size: 8px;
  }

  .kp-ad-dismiss {
    top: 6px;
    right: 6px;
    width: 27px;
    height: 27px;
    font-size: 17px;
  }

  body:not(.admin-body) table {
    font-size: .88rem;
  }
}

/* Users who request less motion get a genuinely calm interface. */
@media (prefers-reduced-motion: reduce) {
  html {
    scroll-behavior: auto !important;
  }

  *,
  *::before,
  *::after {
    animation-duration: .001ms !important;
    animation-iteration-count: 1 !important;
    scroll-behavior: auto !important;
    transition-duration: .001ms !important;
  }

  body:not(.admin-body) .kp-media-loading::before {
    animation: none;
  }
}

/* Printing: content first, navigation/ads never. */
@media print {
  .kp-site-header,
  .kp-mobile-nav,
  .kp-site-footer,
  .kp-inline-banner-wrap,
  .kp-side-banner,
  .kp-back-top,
  .kp-ad-dismiss {
    display: none !important;
  }

  body {
    background: #fff !important;
    color: #000 !important;
  }

  a {
    color: inherit !important;
    text-decoration: none !important;
  }
}
