* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

@font-face {
  font-family: "Peli Emoji";
  src: url("/fonts/NotoColorEmoji.woff2") format("woff2");
  font-display: swap;
}

html {
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  margin: 0;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  overscroll-behavior: none;
  font-family: "Comic Sans MS", "Trebuchet MS", Verdana, sans-serif;
  /* Rainforest light: green canopy above, warm shade below. */
  background: linear-gradient(180deg, #cfe9c4 0%, #a9d69c 55%, #e8dcc0 100%);
  color: #2f3b28;
  display: flex;
  flex-direction: column;
  align-items: center;
  user-select: none;
}

:root.no-emoji-font body {
  font-family: "Comic Sans MS", "Trebuchet MS", Verdana, "Peli Emoji", sans-serif;
}

header {
  width: 100%;
  padding: 0.5rem 1rem 0.8rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.8rem;
}

header h1 {
  margin: 0 auto;
  font-size: 2.1rem;
}

#active-player {
  flex-shrink: 0;
  font-weight: bold;
  background: #ffffff;
  border-radius: 14px;
  padding: 0.5rem 0.9rem;
  box-shadow: 0 4px 0 #6f9c5e;
}

main {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  width: 100%;
  max-width: 1100px;
  padding: 0 1rem 1.5rem;
  position: relative;
}

#hub-link {
  color: #2f3b28;
  font-weight: bold;
  text-decoration: none;
  background: #ffffff;
  border-radius: 14px;
  padding: 0.6rem 1.1rem;
  box-shadow: 0 4px 0 #6f9c5e;
}

.hidden {
  display: none !important;
}

#layout {
  display: grid;
  grid-template-columns: minmax(0, 3fr) minmax(0, 4fr);
  gap: 1.4rem;
  align-items: start;
  margin-top: 0.6rem;
}

/* ---------------- The sloth ---------------- */

#scene {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.6rem;
  position: sticky;
  top: 0;
}

#stage {
  position: relative;
  width: 100%;
  max-width: 340px;
  aspect-ratio: 300 / 340;
}

#sloth {
  width: 100%;
  height: 100%;
  display: block;
  overflow: visible;
}

/* The whole animal sways under the branch, slowly, as a sloth should. */
#hang {
  transform-box: fill-box;
  transform-origin: 50% 0%;
  animation: sway 7s ease-in-out infinite;
}

@keyframes sway {
  0%, 100% { transform: rotate(-1.6deg); }
  50% { transform: rotate(1.6deg); }
}

#head {
  transform-box: fill-box;
  transform-origin: 50% 50%;
  transition: transform 2.4s cubic-bezier(0.4, 0, 0.3, 1);
}

#lids {
  opacity: 0;
  transition: opacity 0.5s ease;
}

.hidden-part {
  opacity: 0;
  pointer-events: none;
}

/* --- eyes shut, for the sleeping card --- */
#sloth.nukkuu #lids { opacity: 1; }
#sloth.nukkuu #eyes { opacity: 0; }
#sloth.nukkuu #hang { animation-duration: 11s; }

/* --- head turned almost the whole way round --- */
#sloth.paa #head { transform: rotate(250deg); }

/* --- hanging upside down: the whole scene turns over, branch and all --- */
#sloth {
  transition: transform 2.2s ease-in-out;
  transform-origin: 50% 50%;
}

#sloth.ylosalaisin {
  transform: rotate(180deg);
}

/* --- a slow shuffle sideways --- */
#sloth.hidas #hang { animation: crawl 6s ease-in-out; }

@keyframes crawl {
  0% { transform: translateX(0) rotate(-1.6deg); }
  50% { transform: translateX(16px) rotate(1.6deg); }
  100% { transform: translateX(0) rotate(-1.6deg); }
}

/* --- algae greening the fur --- */
#sloth.leva #body,
#sloth.leva #arms,
#sloth.leva #legs,
#sloth.leva #head {
  filter: hue-rotate(48deg) saturate(1.5);
  transition: filter 1.6s ease;
}

/* --- claws catching the light --- */
#sloth.kynnet .claws path {
  stroke: #fff6d8;
  filter: drop-shadow(0 0 6px rgba(255, 226, 120, 0.95));
  transition: all 0.5s ease;
}

/* --- the baby clinging to her belly --- */
#sloth.vauva #baby {
  opacity: 1;
  transition: opacity 1s ease;
}

/* --- chewing --- */
#sloth.syo #mouth { animation: chew 0.7s ease-in-out 6; }

@keyframes chew {
  0%, 100% { d: path("M138 160 q12 10 24 0"); }
  50% { d: path("M138 158 q12 18 24 0"); }
}

/* Fallback for browsers that cannot animate `d`: nod the head instead. */
@supports not (d: path("M0 0")) {
  #sloth.syo #head { animation: nibble 0.7s ease-in-out 6; }
  @keyframes nibble {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(4px); }
  }
}

/* --- climbing down for the weekly trip --- */
#sloth.alas #hang { animation: descend 5s ease-in-out; }

@keyframes descend {
  0%, 100% { transform: translateY(0) rotate(-1.6deg); }
  50% { transform: translateY(38px) rotate(0deg); }
}

/* --- swimming --- */
#sloth.uinti #hang { animation: paddle 1.6s ease-in-out infinite; }

@keyframes paddle {
  0%, 100% { transform: rotate(-5deg); }
  50% { transform: rotate(5deg); }
}

/* --- two toes or three --- */
#sloth.lajit #claws-arms path:nth-child(3) { opacity: 0.15; }
#sloth.lajit #claws-arms path { transition: opacity 0.5s ease; }

/* ---------------- Effects layered over the sloth ---------------- */

#leaves,
#zzz,
#water {
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.6s ease;
}

#leaves.on,
#zzz.on,
#water.on {
  opacity: 1;
}

#leaves span {
  position: absolute;
  font-size: 1.7rem;
  animation: drift 4s ease-in-out infinite;
}

@keyframes drift {
  0% { transform: translate(0, 0) rotate(0deg); opacity: 0; }
  25% { opacity: 1; }
  100% { transform: translate(var(--dx), var(--dy)) rotate(var(--rot)); opacity: 0; }
}

#zzz {
  display: flex;
  justify-content: center;
  align-items: flex-start;
  gap: 0.35rem;
  padding-top: 12%;
}

#zzz span {
  font-size: 2rem;
  font-weight: bold;
  color: #4d6b8a;
  animation: rise 2.6s ease-in-out infinite;
}

#zzz span:nth-child(2) { animation-delay: 0.5s; font-size: 2.6rem; }
#zzz span:nth-child(3) { animation-delay: 1s; font-size: 3.2rem; }

@keyframes rise {
  0% { transform: translateY(10px) scale(0.7); opacity: 0; }
  40% { opacity: 1; }
  100% { transform: translateY(-40px) scale(1.1); opacity: 0; }
}

#water {
  top: auto;
  height: 26%;
  background: linear-gradient(180deg, rgba(93, 175, 214, 0.55) 0%, rgba(38, 122, 165, 0.85) 100%);
  border-radius: 40% 40% 0 0 / 14% 14% 0 0;
}

#water::after {
  content: "";
  position: absolute;
  inset: 0 0 auto 0;
  height: 8px;
  background: repeating-linear-gradient(90deg, rgba(255, 255, 255, 0.7) 0 12px, transparent 12px 26px);
  animation: ripple 2.2s linear infinite;
}

@keyframes ripple {
  from { background-position: 0 0; }
  to { background-position: 26px 0; }
}

#progress-badge {
  background: rgba(255, 255, 255, 0.85);
  border-radius: 14px;
  padding: 0.35rem 0.9rem;
  font-weight: bold;
  font-size: 0.95rem;
}

/* ---------------- Fact panel and cards ---------------- */

#panel {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  min-width: 0;
}

#fact-card {
  background: #ffffff;
  border-radius: 22px;
  padding: 1.2rem 1.3rem;
  box-shadow: 0 6px 0 #6f9c5e;
  min-height: 11rem;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

#fact-empty {
  text-align: center;
  color: #5a6b50;
}

#fact-hello {
  margin: 0 0 0.3rem;
  font-size: 1.3rem;
  font-weight: bold;
  color: #2f3b28;
}

#fact-prompt {
  margin: 0;
  font-size: 1rem;
}

#fact-body {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.5rem;
  animation: fact-in 0.45s ease;
}

@keyframes fact-in {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

#fact-icon {
  font-size: 2.6rem;
  line-height: 1;
}

#fact-title {
  margin: 0;
  font-size: 1.35rem;
}

#fact-text {
  margin: 0;
  font-size: 1.05rem;
  line-height: 1.45;
}

#replay-button {
  align-self: flex-start;
  margin-top: 0.3rem;
  border: none;
  border-radius: 14px;
  background: #fff3bf;
  font-family: inherit;
  font-weight: bold;
  font-size: 0.95rem;
  padding: 0.6rem 1rem;
  cursor: pointer;
  box-shadow: 0 4px 0 #d4a418;
  min-height: 48px;
}

#card-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.7rem;
}

.fact-card-button {
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
  aspect-ratio: 1;
  perspective: 700px;
  font-family: inherit;
}

.card-inner {
  position: relative;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  transition: transform 0.5s ease;
}

.fact-card-button.open .card-inner {
  transform: rotateY(180deg);
}

.card-back,
.card-front {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  border-radius: 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.15rem;
  padding: 0.2rem;
}

.card-back {
  background: linear-gradient(160deg, #6f9c5e 0%, #4f7c42 100%);
  color: #eaf6e4;
  font-size: 1.6rem;
  box-shadow: 0 5px 0 #3d6234;
}

.card-front {
  background: #ffffff;
  transform: rotateY(180deg);
  box-shadow: 0 5px 0 #6f9c5e;
}

.card-front .card-icon {
  font-size: 1.9rem;
  line-height: 1;
}

.card-front .card-name {
  font-size: 0.65rem;
  font-weight: bold;
  color: #4f6a44;
  text-align: center;
  line-height: 1.15;
  overflow-wrap: anywhere;
}

.fact-card-button.active .card-front {
  background: #fff8d6;
  box-shadow: 0 5px 0 #d4a418;
}

#restart-button {
  align-self: center;
  border: none;
  border-radius: 16px;
  background: #d7f7dc;
  color: #1b5e20;
  box-shadow: 0 4px 0 #2e7d32;
  font-family: inherit;
  font-weight: bold;
  font-size: 1.05rem;
  padding: 0.7rem 1.3rem;
  cursor: pointer;
  min-height: 48px;
}

button,
a {
  touch-action: manipulation;
}

@media (hover: none) {
  button:hover,
  a:hover {
    transform: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  #hang,
  #zzz span,
  #leaves span,
  #water::after {
    animation: none !important;
  }
}

/* One column on a narrow screen: the sloth on top, the cards under it. */
@media (max-width: 780px) {
  #layout {
    grid-template-columns: 1fr;
  }

  #scene {
    position: static;
  }

  #stage {
    max-width: 230px;
  }

  header h1 {
    font-size: 1.5rem;
  }

  #card-grid {
    grid-template-columns: repeat(4, 1fr);
    gap: 0.5rem;
  }

  .card-front .card-icon { font-size: 1.5rem; }
  .card-front .card-name { font-size: 0.55rem; }
  #fact-title { font-size: 1.15rem; }
  #fact-text { font-size: 0.95rem; }
}
