/**
 * Copyright since 2002 Creabilis
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License 3.0 (AFL-3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * https://opensource.org/licenses/AFL-3.0
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to contact@creabilis.com so we can send you a copy immediately.
 *
 * @author    Creabilis <contact@creabilis.com>
 * @copyright Since 2002 Creabilis
 * @license   https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
 * International Registered Trademark & Property of Creabilis
 */

/*
 * Every visual setting is a custom property: override them from the theme
 * (e.g. `.crea-popup { --crea-popup-accent: ...; }`) rather than the rules.
 *
 * Modifiers (popup "CSS class" field):
 *   crea-popup--text-dark   dark text on a light background image
 *   crea-popup--no-overlay  no darkening layer over the background image
 *   crea-popup--align-left  content aligned to the left
 *   crea-popup--fullscreen  full screen on desktop too
 *   crea-popup--compact     smaller popup (desktop)
 *   crea-popup--scratch-label-dark  dark scratch instruction, for a light texture
 *   crea-popup--scratch-shape       scratch surface shaped like the cover image (transparent PNG)
 */
.crea-popup {
  --crea-popup-z-index: 1070;
  --crea-popup-width: 560px;
  --crea-popup-min-height: 600px;
  --crea-popup-radius: 0;
  --crea-popup-bg: #fef9f5;
  --crea-popup-color: #2e2e2e;
  --crea-popup-color-on-image: #fff;
  --crea-popup-overlay: linear-gradient(180deg, rgba(46, 46, 46, 0.15) 0%, rgba(46, 46, 46, 0.45) 100%);
  --crea-popup-backdrop: rgba(46, 46, 46, 0.6);
  --crea-popup-accent: #edb09e;
  --crea-popup-accent-light: #ffece1;
  --crea-popup-code-bg: #fff;
  --crea-popup-code-color: #2e2e2e;
  --crea-popup-scratch-width: 340px;
  --crea-popup-scratch-ratio: 2 / 1;
  --crea-popup-scratch-radius: 999px;
  --crea-popup-scratch-label-color: #fff;
  --crea-popup-scratch-label-shadow: 0 1px 2px rgba(0, 0, 0, 0.15);
  --crea-popup-duration: 0.6s;
  --crea-popup-easing: cubic-bezier(0.22, 1, 0.36, 1);
  --crea-popup-cta-duration: 0.3s;

  position: fixed;
  inset: 0;
  z-index: var(--crea-popup-z-index);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.crea-popup[hidden] {
  display: none;
}

/* Backdrop ---------------------------------------------------------------- */

.crea-popup__backdrop {
  position: absolute;
  inset: 0;
  background: var(--crea-popup-backdrop);
  opacity: 0;
  /* No backdrop-filter blur on purpose: on a full-screen layer it makes every
     repaint of the dialog (CTA hover...) costly and drops frames, especially
     on Safari / low-end devices. */
  transition: opacity var(--crea-popup-duration) ease;
}

.crea-popup.is-open .crea-popup__backdrop {
  opacity: 1;
}

/* Dialog ------------------------------------------------------------------ */

.crea-popup__dialog {
  position: relative;
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: var(--crea-popup-width);
  min-height: min(var(--crea-popup-min-height), calc(100dvh - 2rem));
  max-height: calc(100dvh - 2rem);
  overflow: hidden auto;
  overscroll-behavior: contain;
  color: var(--crea-popup-color);
  background-color: var(--crea-popup-bg);
  border-radius: var(--crea-popup-radius);
  box-shadow: 0 1.5rem 4rem rgba(0, 0, 0, 0.25);
  opacity: 0;
  transform: translateY(2.5rem) scale(0.96);
  transition:
    opacity calc(var(--crea-popup-duration) * 0.6) ease,
    transform var(--crea-popup-duration) var(--crea-popup-easing);
}

.crea-popup__dialog:focus {
  outline: none;
}

.crea-popup.is-open .crea-popup__dialog {
  opacity: 1;
  transform: none;
}

/* Background image + legibility overlay */

.crea-popup__background {
  position: absolute;
  inset: 0;
  z-index: 0;
}

.crea-popup__background img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transform: scale(1.08);
  transition: transform calc(var(--crea-popup-duration) * 2.5) var(--crea-popup-easing);
}

/* Slow zoom-out of the background while the popup opens */
.crea-popup.is-open .crea-popup__background img {
  transform: none;
}

/* Opening: the content appears element by element (title, text, scratch area...) */
.crea-popup.is-open .crea-popup__content > * {
  animation: crea-popup-rise var(--crea-popup-duration) var(--crea-popup-easing) both;
}

.crea-popup.is-open .crea-popup__content > :nth-child(1) {
  animation-delay: 0.15s;
}

.crea-popup.is-open .crea-popup__content > :nth-child(2) {
  animation-delay: 0.25s;
}

.crea-popup.is-open .crea-popup__content > :nth-child(3) {
  animation-delay: 0.35s;
}

.crea-popup.is-open .crea-popup__content > :nth-child(n + 4) {
  animation-delay: 0.45s;
}

@keyframes crea-popup-rise {
  from {
    opacity: 0;
    transform: translateY(1rem);
  }

  to {
    opacity: 1;
    transform: none;
  }
}

.crea-popup--has-image .crea-popup__dialog {
  color: var(--crea-popup-color-on-image);
}

.crea-popup--has-image .crea-popup__dialog::before {
  position: absolute;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  content: "";
  background: var(--crea-popup-overlay);
}

/* Close button */

.crea-popup__close {
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
  z-index: 3;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 2.75rem;
  height: 2.75rem;
  padding: 0;
  color: inherit;
  cursor: pointer;
  background: transparent;
  border: 0;
  border-radius: 50%;
  transition: background-color 0.15s ease;
}

.crea-popup__close:hover,
.crea-popup__close:focus-visible {
  background-color: rgba(255, 255, 255, 0.2);
}

.crea-popup__close:focus-visible {
  outline: 2px solid currentColor;
  outline-offset: 2px;
}

/* Content ----------------------------------------------------------------- */

.crea-popup__content {
  position: relative;
  z-index: 1;
  display: flex;
  flex: 1 0 auto;
  flex-direction: column;
  gap: 1.25rem;
  align-items: center;
  justify-content: center;
  padding: 4rem 2rem 2.5rem;
  text-align: center;
}

.crea-popup__title {
  margin: 0;
  font-size: clamp(2rem, 7vw, 2.75rem);
  font-weight: 600;
  line-height: 1.1;
  color: inherit;
  text-wrap: balance;
}

.crea-popup__description,
.crea-popup__reveal-text {
  max-width: 28rem;
  font-size: 1.0625rem;
  line-height: 1.45;
  text-wrap: pretty;
}

.crea-popup__description > :last-child,
.crea-popup__reveal-text > :last-child {
  margin-bottom: 0;
}

.crea-popup__description a,
.crea-popup__reveal-text a {
  color: inherit;
  text-decoration: underline;
}

.crea-popup__cta {
  min-width: 12rem;
  margin-top: 0.25rem;
  /* The theme transition (0.15s) is too short for the full color inversion of
     the button on hover: it reads as a flash rather than a transition. */
  transition-duration: var(--crea-popup-cta-duration);
  transition-timing-function: ease;
}

/* Scratch and Reveal ------------------------------------------------------ */

.crea-popup__scratch {
  position: relative;
  width: 100%;
  max-width: var(--crea-popup-scratch-width);
  aspect-ratio: var(--crea-popup-scratch-ratio);
  margin: 0.5rem 0;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

/* Revealed code, under the canvas */
.crea-popup__code {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  gap: 0.375rem;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  color: var(--crea-popup-code-color);
  background-color: var(--crea-popup-code-bg);
  border: 2px dashed var(--crea-popup-accent);
  border-radius: var(--crea-popup-scratch-radius);
}

.crea-popup__code-label {
  font-size: 0.75rem;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  opacity: 0.7;
}

.crea-popup__code-value {
  font-size: clamp(1.5rem, 6vw, 2rem);
  font-weight: 700;
  line-height: 1;
  letter-spacing: 0.08em;
  word-break: break-all;
  user-select: all;
  -webkit-user-select: all;
}

.crea-popup__copy {
  display: inline-flex;
  gap: 0.375rem;
  align-items: center;
  padding: 0.375rem 0.875rem;
  font-family: inherit;
  font-size: 0.8125rem;
  font-weight: 600;
  color: var(--crea-popup-code-color);
  cursor: pointer;
  background-color: var(--crea-popup-accent-light);
  border: 0;
  border-radius: 999px;
  transition: background-color 0.15s ease;
}

.crea-popup__copy:hover,
.crea-popup__copy:focus-visible {
  background-color: var(--crea-popup-accent);
}

.crea-popup__copy.is-copied {
  background-color: var(--crea-popup-accent);
}

.crea-popup__copy:focus {
  outline: none;
}

.crea-popup__copy:focus-visible {
  outline: 2px solid var(--crea-popup-code-color);
  outline-offset: 2px;
}

/* Scratch surface */
.crea-popup__canvas {
  position: absolute;
  inset: -2px; /* also covers the anti-aliased edge of the dashed border */
  z-index: 1;
  width: calc(100% + 4px);
  height: calc(100% + 4px);
  cursor: grab;
  touch-action: none;
  border-radius: var(--crea-popup-scratch-radius);
  transition: opacity 0.5s ease;
}

.crea-popup__canvas:active {
  cursor: grabbing;
}

.crea-popup__scratch-label {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  font-size: 1.125rem;
  font-weight: 500;
  color: var(--crea-popup-scratch-label-color);
  text-align: center;
  text-transform: uppercase;
  letter-spacing: 0.14em;
  pointer-events: none;
  transition: opacity 0.3s ease;
  text-shadow: var(--crea-popup-scratch-label-shadow);
}

.crea-popup__reveal-button {
  position: absolute;
  bottom: -2.5rem;
  left: 50%;
  z-index: 3;
  transform: translateX(-50%);
}

.crea-popup__scratch.is-scratching .crea-popup__scratch-label {
  opacity: 0;
}

.crea-popup__scratch.is-revealed .crea-popup__canvas {
  pointer-events: none;
  opacity: 0;
}

.crea-popup__scratch.is-revealed .crea-popup__scratch-label,
.crea-popup__scratch.is-revealed .crea-popup__reveal-button {
  display: none;
}

/* Hint animation while nothing has been scratched */
@media (prefers-reduced-motion: no-preference) {
  /* Keeps the entrance animation (same selector specificity issue): rise, then wiggle */
  .crea-popup.is-open .crea-popup__content > .crea-popup__scratch:not(.is-scratching) {
    animation:
      crea-popup-rise var(--crea-popup-duration) var(--crea-popup-easing) 0.35s both,
      crea-popup-wiggle 2.4s ease-in-out 1.6s infinite;
  }
}

@keyframes crea-popup-wiggle {
  0%,
  80%,
  100% {
    transform: rotate(0);
  }

  85% {
    transform: rotate(-2deg);
  }

  90% {
    transform: rotate(2deg);
  }

  95% {
    transform: rotate(-1deg);
  }
}

/* Content displayed once revealed (text contrast: see the modifiers, e.g. crea-popup--text-dark) */
.crea-popup.is-open .crea-popup__content > .crea-popup__revealed,
.crea-popup__revealed {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  align-items: center;
  animation: crea-popup-fade-in 0.5s var(--crea-popup-easing) both;
}

/* Same specificity as the rule above: keeps the block hidden until revealed */
.crea-popup.is-open .crea-popup__content > .crea-popup__revealed[hidden],
.crea-popup__revealed[hidden] {
  display: none;
}

.crea-popup__reveal-image {
  max-width: min(100%, 22rem);
  height: auto;
}

@keyframes crea-popup-fade-in {
  from {
    opacity: 0;
    transform: translateY(0.75rem);
  }

  to {
    opacity: 1;
    transform: none;
  }
}

/* Mobile: full screen, as on the reference ------------------------------- */

@media (max-width: 767.98px) {
  .crea-popup {
    padding: 0;
  }

  .crea-popup__dialog {
    max-width: none;
    height: 100dvh;
    min-height: 0;
    max-height: none;
    border-radius: 0;
    box-shadow: none;
  }

  .crea-popup__content {
    padding: 4.5rem 1.25rem 2rem;
  }

  .crea-popup__scratch {
    max-width: min(var(--crea-popup-scratch-width), 85vw);
  }
}

/* Modifiers --------------------------------------------------------------- */

.crea-popup--text-dark {
  --crea-popup-color-on-image: #2e2e2e;
  --crea-popup-overlay: linear-gradient(180deg, rgba(254, 249, 245, 0.2) 0%, rgba(254, 249, 245, 0.55) 100%);
}

.crea-popup--text-dark .crea-popup__close:hover,
.crea-popup--text-dark .crea-popup__close:focus-visible {
  background-color: rgba(46, 46, 46, 0.08);
}

.crea-popup--scratch-label-dark {
  --crea-popup-scratch-label-color: #2e2e2e;
  --crea-popup-scratch-label-shadow: 0 1px 2px rgba(255, 255, 255, 0.6);
}

/* Scratch surface shaped like the cover image (transparent PNG, e.g. a cream
   swatch): no pill, the image is fitted whole ("contain"); once scratched,
   nothing is left behind and the code sits directly on the popup background. */
.crea-popup--scratch-shape {
  --crea-popup-scratch-width: 400px;
  --crea-popup-scratch-radius: 0;
}

.crea-popup--scratch-shape .crea-popup__code {
  color: var(--crea-popup-color-on-image);
  background: none;
  border: 0;
}

.crea-popup--scratch-shape .crea-popup__canvas {
  inset: 0;
  width: 100%;
  height: 100%;
}

/* The code must not show through before the shape is drawn */
.crea-popup--scratch-shape .crea-popup__scratch:not(.is-ready) .crea-popup__code {
  visibility: hidden;
}

.crea-popup--no-overlay {
  --crea-popup-overlay: none;
}

.crea-popup--align-left .crea-popup__content {
  align-items: flex-start;
  text-align: left;
}

@media (min-width: 768px) {
  .crea-popup--fullscreen {
    padding: 0;
  }

  .crea-popup--fullscreen .crea-popup__dialog {
    max-width: none;
    height: 100dvh;
    max-height: none;
    border-radius: 0;
  }

  .crea-popup--compact {
    --crea-popup-width: 440px;
    --crea-popup-min-height: 460px;
    --crea-popup-scratch-width: 280px;
  }
}

/* Reduced motion ---------------------------------------------------------- */

@media (prefers-reduced-motion: reduce) {
  .crea-popup {
    --crea-popup-duration: 0.01s;
  }

  .crea-popup__dialog,
  .crea-popup__background img {
    transform: none;
  }

  .crea-popup.is-open .crea-popup__content > *,
  .crea-popup.is-open .crea-popup__content > .crea-popup__revealed,
  .crea-popup__revealed {
    animation: none;
  }
}

/* Page scroll lock while open */
.crea-popup-open {
  overflow: hidden;
}
