.ad-popup {
  position: fixed;
  inset: 0;
  inset: 0;
  z-index: 99999;

  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;

  width: 100%;
  height: 100%;
  min-width: 0;
  margin: 0;
  padding:
    16px 16px max(16px, env(safe-area-inset-bottom, 0px));

  box-sizing: border-box;

  opacity: 0;
  visibility: hidden;
  pointer-events: none;

  isolation: isolate;
  overscroll-behavior: contain;
  -webkit-tap-highlight-color: transparent;

  transition:
    opacity 0.28s ease,
    visibility 0.28s ease;
}

/* Prevent sizing inconsistencies */

.ad-popup,
.ad-popup *,
.ad-popup *::before,
.ad-popup *::after {
  box-sizing: border-box;
}

/* Visible state */

.ad-popup.is-visible {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
}

/* Fixed background overlay */

.ad-popup__backdrop {
  position: absolute;
  inset: 0;
  z-index: 0;

  width: 100%;
  height: 100%;

  background: rgba(4, 7, 12, 0.38);

  opacity: 0;
  transition: opacity 0.28s ease;

  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
}

.ad-popup.is-visible .ad-popup__backdrop {
  opacity: 1;
}

/* Popup container */

.ad-popup__container {
  position: relative;
  z-index: 1;

  display: block;

  width: 100%;
  max-width: 676px;
  min-width: 0;
  max-height: 100%;

  flex: 0 1 auto;

  margin: 0 auto;

  overflow: visible;

  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 12px;

  background: #11141d;

  box-shadow:
    0 12px 38px rgba(0, 0, 0, 0.35),
    0 0 0 1px rgba(14, 220, 58, 0.035);

  transform: translate3d(0, 22px, 0);

  transition: transform 0.28s ease;

  will-change: transform;
}

.ad-popup.is-visible .ad-popup__container {
  transform: translate3d(0, 0, 0);
}

/* Clickable advertisement */

.ad-popup__link {
  position: relative;

  display: block;

  width: 100%;
  min-width: 0;

  overflow: hidden;

  border-radius: 11px;

  line-height: 0;
  text-decoration: none;

  -webkit-tap-highlight-color: transparent;
}

/* Advertisement image */

.ad-popup__image {
  display: block;

  width: 100%;
  height: auto;

  max-width: 100%;
  max-height: 35vh;
  max-height: 35dvh;

  margin: 0 auto;

  object-fit: contain;
  object-position: center;

  border: 0;
  border-radius: 11px;

  user-select: none;
  -webkit-user-drag: none;
}

/* Close button */

.ad-popup__close {
  position: absolute;
  top: 7px;
  right: 7px;
  z-index: 5;

  display: flex;
  align-items: center;
  justify-content: center;

  width: 36px;
  height: 36px;
  min-width: 36px;
  min-height: 36px;

  margin: 0;
  padding: 0;

  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 50%;

  background: rgba(12, 15, 20, 0.94);
  color: #fff;

  font-family: Arial, sans-serif;
  font-size: 27px;
  font-weight: 400;
  line-height: 1;

  cursor: pointer;
  touch-action: manipulation;

  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);

  transition:
    background-color 0.2s ease,
    border-color 0.2s ease,
    color 0.2s ease,
    transform 0.2s ease;

  -webkit-tap-highlight-color: transparent;
  appearance: none;
  -webkit-appearance: none;
}

.ad-popup__close:hover {
  background: #0edc3a;
  border-color: #0edc3a;
  color: #07100a;
}

.ad-popup__close:active {
  transform: scale(0.94);
}

.ad-popup__close:focus-visible,
.ad-popup__link:focus-visible {
  outline: 2px solid #0edc3a;
  outline-offset: 3px;
}

/* =========================================
   TABLET — max 1400px
========================================= */

@media (max-width: 1400px) {
  .ad-popup {
    padding:
      16px 16px max(16px, env(safe-area-inset-bottom, 0px));
  }

  .ad-popup__container {
    width: min(100%, 676px);
  }

  .ad-popup__image {
    max-height: 35vh;
    max-height: 35dvh;
  }
}

/* =========================================
   MOBILE + TABLET PORTRAIT — max 600px
========================================= */

@media (max-width: 600px) {
  .ad-popup {
    /*
      Fixed overlay remains attached to the viewport.
      Safe-area padding prevents overlap with phone edges.
    */
    position: fixed;
    inset: 0;

    width: 100%;
    height: 100%;

    padding:
      12px max(12px, env(safe-area-inset-right, 0px)) max(12px, env(safe-area-inset-bottom, 0px)) max(12px, env(safe-area-inset-left, 0px));

    justify-content: flex-end;
  }

  .ad-popup__container {
    width: 100%;
    max-width: 100%;
    max-height: 100%;

    border-radius: 10px;
  }

  .ad-popup__link,
  .ad-popup__image {
    border-radius: 9px;
  }

  .ad-popup__image {
    width: 100%;
    height: auto;

    max-width: 100%;
    max-height: 30vh;
    max-height: 30dvh;

    object-fit: contain;
    object-position: center;
  }

  .ad-popup__close {
    top: 5px;
    right: 5px;

    width: 32px;
    height: 32px;
    min-width: 32px;
    min-height: 32px;

    font-size: 24px;
  }
}

/* =========================================
   SMALL PHONES — max 420px
========================================= */

@media (max-width: 420px) {
  .ad-popup {
    padding:
      10px max(10px, env(safe-area-inset-right, 0px)) max(10px, env(safe-area-inset-bottom, 0px)) max(10px, env(safe-area-inset-left, 0px));
  }

  .ad-popup__container {
    border-radius: 9px;
  }

  .ad-popup__link,
  .ad-popup__image {
    border-radius: 8px;
  }

  .ad-popup__image {
    max-height: 28vh;
    max-height: 28dvh;
  }

  .ad-popup__close {
    top: 5px;
    right: 5px;

    width: 30px;
    height: 30px;
    min-width: 30px;
    min-height: 30px;

    font-size: 22px;
  }
}

/* =========================================
   SHORT SCREENS — LANDSCAPE / SMALL HEIGHT
========================================= */

@media (max-height: 500px) {
  .ad-popup {
    justify-content: center;

    padding-top: max(8px, env(safe-area-inset-top, 0px));
    padding-bottom: max(8px, env(safe-area-inset-bottom, 0px));
  }

  .ad-popup__container {
    max-height: 100%;
  }

  .ad-popup__image {
    max-height: 55vh;
    max-height: 55dvh;
  }
}

/* =========================================
   REDUCED MOTION
========================================= */

@media (prefers-reduced-motion: reduce) {

  .ad-popup,
  .ad-popup__backdrop,
  .ad-popup__container,
  .ad-popup__close {
    transition: none !important;
    animation: none !important;
  }

  .ad-popup__container {
    transform: none;
    will-change: auto;
  }

  .ad-popup__close:active {
    transform: none;
  }
}