/* Tonttujahti styles */

.tonttujahti {
  --tj-bg: #99002c;
  --tj-text: #fff;
  --tj-button-bg: #fffadb;
  --tj-button-text: #111;

  /* Nollataan selaimen dialog-oletukset; tonttu saa mennä laatikon reunan yli */
  overflow: visible;
  width: min(38rem, calc(100% - 2rem));
  max-width: none;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--tj-text);
}

.tonttujahti::backdrop {
  background: rgb(0 0 0 / 0.4);
}

.tonttujahti__content {
  position: relative;
  /* Reilusti tilaa tekstin ympärille; oikealla lisätilaa tontulle */
  padding: 3.5rem 9rem 3.5rem 6rem;
  text-align: center;
  background: var(--tj-bg);
  border-radius: 3.5rem;
}

.tonttujahti__text {
  position: relative;
  z-index: 1;
}

.tonttujahti__title {
  margin: 0 0 1rem;
  font-family: "BwSeidoRaw-Bold", sans-serif;
  font-size: 1.75rem;
  line-height: 1.2;
  color: inherit;
}

.tonttujahti__description {
  margin: 0;
  font-family: "BwSeidoRaw-Regular", sans-serif;
  font-size: 1.0625rem;
  line-height: 1.45;
  color: inherit;
}

.tonttujahti__button {
  position: relative;
  z-index: 1;
  display: inline-flex;
  align-items: center;
  gap: 0.75rem;
  margin-top: 1.75rem;
  padding: 0.75rem 1.75rem;
  font-family: "BwSeidoRaw-Bold", sans-serif;
  font-size: 1.0625rem;
  color: var(--tj-button-text);
  text-decoration: none;
  background: var(--tj-button-bg);
  border-radius: 9999px;
  transition: transform 0.15s ease;
}

.tonttujahti__button:hover {
  color: var(--tj-button-text);
  transform: translateY(-1px);
}

.tonttujahti__button:focus-visible,
.tonttujahti__close:focus-visible {
  outline: 2px solid var(--tj-text);
  outline-offset: 3px;
}

.tonttujahti__close {
  position: absolute;
  top: 1.5rem;
  right: 2rem;
  z-index: 2;
  width: 2.5rem;
  height: 2.5rem;
  font-size: 1.5rem;
  color: var(--tj-text);
  background: transparent;
  border: 0;
  border-radius: 9999px;
  cursor: pointer;
}

.tonttujahti__close:hover {
  background: rgb(255 255 255 / 0.15);
}

/* Tonttu laatikon oikean reunan yli ja alareunan alle */
.tonttujahti__image {
  position: absolute;
  right: -7rem;
  bottom: -2.5rem;
  width: 15rem;
  pointer-events: none;
}

.tonttujahti__image img {
  display: block;
  width: 100%;
  height: auto;
}

/* Mobiili: ei tilaa reunan yli, tonttu laatikon yläpuolelle */
@media (max-width: 640px) {
  .tonttujahti {
    margin-top: 8rem;
  }

  .tonttujahti__content {
    padding: 4rem 2rem 2.5rem;
    border-radius: 2.75rem;
  }

  .tonttujahti__image {
    top: -7rem;
    right: 50%;
    bottom: auto;
    width: 10rem;
    transform: translateX(50%);
  }

  .tonttujahti__title {
    font-size: 1.375rem;
  }

  .tonttujahti__description,
  .tonttujahti__button {
    font-size: 1rem;
  }
}

.tonttujahti__runner {
  /* visibility eikä display: none, jotta selain lataa sprite-kuvan jo popupin auetessa.
     display: none -elementin taustakuvaa ei ladata, jolloin tonttu katoaisi hetkeksi ensimmäisellä sulkukerralla. */
  visibility: hidden;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  /* Juoksuvaihe on +50 yksikköä korkeampi kuin kokonainen tonttu (834 × 742), jotta jalat mahtuvat kuvaan.
     Leveys ja yläreuna ovat samat, joten vartalo osuu paikallaan olevan tontun kohdalle ja vain jalat jatkuvat alemmas. */
  aspect-ratio: 834 / 792;
  background: url('../images/tonttujahti/tonttu-juoksu.webp') 0 0 / 400% 100% no-repeat;
}

/* Sulkeutuessa: paikallaan oleva tonttu piiloon, juoksija näkyviin */
.tonttujahti.is-leaving .tonttujahti__image img { visibility: hidden; }
.tonttujahti.is-leaving .tonttujahti__runner {
  visibility: visible;
  animation: tj-legs 0.4s steps(4) infinite;   /* jalat vaihtuvat */
}
.tonttujahti.is-leaving .tonttujahti__content { animation: tj-bg-fade 0.3s forwards; }
.tonttujahti.is-leaving .tonttujahti__close   { visibility: hidden; }

@keyframes tj-bg-fade { to { background-color: transparent; } }

.tonttujahti.is-leaving .tonttujahti__image {
  animation: tj-run-away 1.2s ease-in forwards; /* tonttu juoksee vasemmalle pois */
}
.tonttujahti.is-leaving .tonttujahti__content > :not(.tonttujahti__image),
.tonttujahti.is-leaving::backdrop {
  animation: tj-fade 0.3s forwards;             /* laatikon sisältö ja tausta häivytetään */
}

@keyframes tj-legs     { to { background-position: 133.333% 0; } }
/* translate eikä transform: mobiilissa .tonttujahti__image keskitetään transformilla, jota ei saa korvata */
@keyframes tj-run-away { to { translate: -120vw 0; } }
@keyframes tj-fade     { to { opacity: 0; } }

@media (prefers-reduced-motion: reduce) {
  .tonttujahti.is-leaving * { animation: none !important; }
}
