/* ================================================================
   Ковёр для Анютки
   A wall carpet on an acid-mint wall. Everything is made of knots.
   ================================================================ */

:root {
  --blood: #b0132e;
  --wine: #4a0717;
  --mustard: #f5b82e;
  --ink: #1b1f6b;
  --milk: #f8ebd0;
  --mint: #47f0b8;
  --mint-deep: #33d9a1;
  --rose: #ff4f93;

  --display: 'Unbounded', 'Arial Black', system-ui, sans-serif;
  --hand: 'Neucha', 'Comic Sans MS', cursive;

  --knot: 6px; /* one carpet knot on screen */
}

*, *::before, *::after { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
  background: var(--mint);
}

body {
  margin: 0;
  min-height: 100svh;
  color: var(--ink);
  font-family: var(--hand);
  font-size: 1.3rem;
  line-height: 1.35;
  overflow-x: hidden;
  /* the wall: mint wallpaper with stitched stripes */
  background:
    radial-gradient(circle at 50% 50%, var(--mint-deep) 0 1.5px, transparent 2px) 0 0 / 28px 28px,
    repeating-linear-gradient(90deg, transparent 0 26px, rgba(27, 31, 107, .06) 26px 28px),
    var(--mint);
}

main { overflow-x: clip; }

button { font: inherit; color: inherit; }

:focus-visible {
  outline: 4px dashed var(--ink);
  outline-offset: 4px;
}

/* ---------------------------------------------------------------
   fringe: the threads at the ends of every carpet
   --------------------------------------------------------------- */
.fringe {
  height: 22px;
  background: repeating-linear-gradient(90deg, var(--milk) 0 3px, transparent 3px 7px);
  -webkit-mask: linear-gradient(var(--dir, 180deg), #000 55%, rgba(0, 0, 0, .55));
          mask: linear-gradient(var(--dir, 180deg), #000 55%, rgba(0, 0, 0, .55));
}
.fringe--top { --dir: 0deg; }
.fringe--bottom { --dir: 180deg; }

/* ===============================================================
   HERO: the carpet with the giant title spilling off its edges
   =============================================================== */
.hero {
  position: relative;
  padding: 34px 0 8px;
}
.hero__carpet {
  position: relative;
  isolation: isolate;
  padding-bottom: 34px;
}

.rug--hero {
  position: absolute;
  z-index: -1;
  inset: -16px 22px 0;
  display: flex;
  flex-direction: column;
  filter: drop-shadow(10px 14px 0 rgba(27, 31, 107, .35));
}
.rug__field {
  flex: 1;
  background: var(--carpet-hero) 0 calc(16 * var(--knot)) / calc(32 * var(--knot)) calc(32 * var(--knot));
  image-rendering: pixelated;
  /* woven border bands */
  box-shadow:
    inset 0 0 0 var(--knot) var(--wine),
    inset 0 0 0 calc(var(--knot) * 2) var(--mustard),
    inset 0 0 0 calc(var(--knot) * 3) var(--wine),
    inset 0 0 0 calc(var(--knot) * 5) var(--ink),
    inset 0 0 0 calc(var(--knot) * 6) var(--wine);
}

/* nails driven through the top band of the carpet */
.nail {
  position: absolute;
  top: 6px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 35%, #fff 0 2px, #9aa0b8 3px, #4b4f6a 8px);
  box-shadow: 2px 3px 0 rgba(27, 31, 107, .45);
  z-index: 2;
}
.nail--l { left: 18%; }
.nail--r { right: 18%; }

.title {
  margin: 0;
  padding-top: calc(6vw + 18px);
  font-family: var(--display);
  font-weight: 900;
  line-height: .88;
  letter-spacing: -.02em;
  text-align: center;
  cursor: default;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}

/* JS sets the font-size so each line is exactly as wide as the screen,
   then --sy stretches the letters until the greeting fills the whole first screen */
.title__line {
  display: block;
  width: max-content;
  margin: 0 auto;
  white-space: nowrap;
  font-size: 17vw;
  color: var(--milk);
  -webkit-text-stroke: .09em var(--wine);
  paint-order: stroke fill;
  text-shadow:
    .045em .05em 0 var(--wine),
    .07em .09em 0 var(--ink);
  transform: scaleY(var(--sy, 1));
  transform-origin: 50% 0;
  margin-bottom: calc((var(--sy, 1) - 1) * .88em);
}
.title__line + .title__line {
  color: var(--mustard);
  margin-top: .04em;
}

.ch {
  display: inline-block;
  transform-origin: 50% 90%;
  transform: rotate(var(--tilt, 0deg));
  opacity: 0;
}
.ch:nth-child(3n + 1) { --tilt: -4deg; }
.ch:nth-child(3n + 2) { --tilt: 3deg; }
.ch:nth-child(4n) { --tilt: -1deg; translate: 0 .04em; }

/* the smiley: two eyes and a grin cut through the carpet, the wall shows through */
.title__face {
  display: block;
  width: min(84vw, 60vh, 560px);
  width: min(84vw, 60svh, 560px);
  margin: 4vw auto 0;
  opacity: 0;
  cursor: pointer;
  transform-origin: 50% 40%;
}
.title__face svg { display: block; width: 100%; height: auto; }
.face__wink { display: none; }
.is-winking .face__wink { display: inline; }
.is-winking .face__open { display: none; }

.is-ready .ch {
  opacity: 1;
  animation: drop .9s cubic-bezier(.2, 1.6, .4, 1) both;
  animation-delay: calc(var(--n) * 55ms);
}
.is-ready .title__face {
  opacity: 1;
  animation: face-in 1.1s cubic-bezier(.2, 1.4, .4, 1) both;
  animation-delay: calc(var(--n) * 55ms + 150ms);
}
.is-ready .ch.is-poked {
  animation: poke .55s cubic-bezier(.3, 1.8, .5, 1) both;
}
.is-ready .title__face.is-poked {
  animation: face-poke .5s cubic-bezier(.3, 1.8, .5, 1) both;
}

@keyframes drop {
  0% { transform: translateY(-110vh) rotate(-35deg); }
  100% { transform: translateY(0) rotate(var(--tilt, 0deg)); }
}
@keyframes face-in {
  0% { transform: scale(0) rotate(-200deg); }
  100% { transform: scale(1) rotate(0); }
}
@keyframes poke {
  0% { transform: rotate(var(--tilt, 0deg)); }
  30% { transform: translateY(-.25em) scale(.9, 1.15) rotate(calc(var(--tilt, 0deg) - 14deg)); }
  60% { transform: translateY(0) scale(1.12, .88) rotate(calc(var(--tilt, 0deg) + 8deg)); }
  100% { transform: rotate(var(--tilt, 0deg)); }
}
@keyframes face-poke {
  0% { transform: none; }
  35% { transform: scale(1.06, .92) rotate(-3deg); }
  70% { transform: scale(.97, 1.04) rotate(2deg); }
  100% { transform: none; }
}

.hero__aside {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  margin: 26px 22px 0;
}
.hero__note {
  margin: 0;
  max-width: 22ch;
  font-size: 1.45rem;
  line-height: 1.2;
  rotate: -3deg;
}
.hero__arrow {
  flex: none;
  width: 40px;
  margin-bottom: -26px;
  fill: none;
  stroke: var(--ink);
  stroke-width: 3.5;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* ===============================================================
   section titles
   =============================================================== */
.section-title {
  margin: 0;
  font-family: var(--display);
  font-weight: 900;
  line-height: .85;
  letter-spacing: -.03em;
  hyphens: manual;
  -webkit-text-stroke: .06em var(--ink);
  paint-order: stroke fill;
  text-shadow: .05em .05em 0 var(--ink);
  font-size: clamp(3.6rem, 21.5vw, 7.4rem);
}
.section-lead {
  margin: 0;
  max-width: 34ch;
}

/* ===============================================================
   КОТОБАШНЯ
   =============================================================== */
.tower-section {
  padding: 56px 14px 32px;
  max-width: 900px;
  margin: 0 auto;
}
.section-title--tower {
  font-size: clamp(3.4rem, 19vw, 7.4rem);
  color: var(--blood);
  rotate: 2deg;
  margin-bottom: 18px;
}
.tower-section .section-lead { rotate: -1deg; }

.tower {
  --cat-scale: .9;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 72px 0 0;
  transform-origin: 50% 100%;
}
.tower.is-wobbling { animation: wobble .9s cubic-bezier(.3, 1.6, .5, 1); }
@keyframes wobble {
  0% { rotate: 0deg; }
  25% { rotate: -2.4deg; }
  55% { rotate: 1.7deg; }
  80% { rotate: -.6deg; }
  100% { rotate: 0deg; }
}

/* each cat stands with its paws on the ears of the cat below */
.cat {
  --u: calc(var(--px) * var(--cat-scale, 1) * 1px);
  position: relative;
  z-index: calc(10 - var(--i));
  width: calc(var(--u) * 19);
  margin-top: calc(var(--u) * -3);
  translate: var(--shift, 0) 0;
  rotate: var(--lean, 0deg);
  transform-origin: 50% 100%;
}
.cat:first-child { margin-top: 0; }
.cat:nth-child(odd) { --shift: -8px; --lean: -2deg; }
.cat:nth-child(even) { --shift: 10px; --lean: 2.5deg; }
.cat:last-child { --shift: -2px; --lean: 0deg; }
.cat.is-talking { z-index: 30; }

.cat__btn {
  display: block;
  width: 100%;
  padding: 0;
  border: 0;
  background: none;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  /* only the stitched pixels are tappable, not the transparent corners */
  pointer-events: none;
}
.cat__svg rect { pointer-events: auto; }
.cat__svg {
  display: block;
  width: 100%;
  height: auto;
  overflow: visible;
}
.cat--flip .cat__svg { transform: rotate(180deg); }

.cat__lids { display: none; }
.is-blinking .cat__lids { display: inline; }
.is-blinking .cat__eyes { display: none; }

.is-talking .cat__btn { animation: squash .45s cubic-bezier(.3, 1.8, .5, 1); transform-origin: 50% 100%; }
@keyframes squash {
  0% { scale: 1 1; }
  35% { scale: 1.1 .86; }
  70% { scale: .95 1.06; }
  100% { scale: 1 1; }
}

.cat__label {
  position: absolute;
  top: 40%;
  margin: 0;
  width: 5.8em;
  font-size: 1rem;
  line-height: 1.05;
  overflow-wrap: break-word;
  hyphens: auto;
  pointer-events: none;
}
.cat__label b { display: block; font-weight: 400; font-size: 1.3em; }
.cat__label span { opacity: .75; }
.cat:nth-child(odd) .cat__label { left: calc(100% + 4px); rotate: 4deg; }
.cat:nth-child(even) .cat__label { right: calc(100% + 4px); text-align: right; rotate: -4deg; }
.cat:last-child .cat__label { top: calc(100% + 6px); left: 50%; right: auto; width: max-content; max-width: 90vw; translate: -50% 0; text-align: center; rotate: -2deg; }
.cat:last-child .cat__label b { display: inline; }
.cat:last-child .cat__label span::before { content: ' '; }

.cat__bubble {
  position: absolute;
  z-index: 5;
  /* anchored at the tail side, above the eyes: it may cover the paws of the cat above, never a face */
  bottom: 75%;
  left: calc(100% - 3 * var(--u));
  width: max-content;
  max-width: min(190px, 46vw);
  margin: 0;
  padding: 7px 10px 9px;
  font-size: 1.1rem;
  line-height: 1.1;
  color: var(--ink);
  background: var(--milk);
  border: 3px solid var(--ink);
  box-shadow: 5px 5px 0 var(--ink);
  translate: var(--bx, 0px) 0;
  scale: 0;
  transform-origin: 0 100%;
  pointer-events: none;
  transition: scale .18s ease-in;
  text-wrap: balance;
}
/* bubbles pop out on the label side, so they never sit on the cat above */
.cat:nth-child(even):not(:last-child) .cat__bubble { left: auto; right: calc(100% - 3 * var(--u)); transform-origin: 100% 100%; }
.cat:last-child .cat__bubble { bottom: auto; top: 48%; transform-origin: 0 0; }
.cat--flip .cat__bubble { bottom: auto; top: 80%; transform-origin: 0 0; }
.cat__bubble::after {
  content: '';
  position: absolute;
  top: 100%;
  left: 12px;
  width: 14px;
  height: 14px;
  margin: -6px 0 0 0;
  background: var(--milk);
  border-right: 3px solid var(--ink);
  border-bottom: 3px solid var(--ink);
  rotate: 45deg;
}
.cat:nth-child(even):not(:last-child) .cat__bubble::after { left: auto; right: 12px; }
.cat--flip .cat__bubble::after,
.cat:last-child .cat__bubble::after { top: auto; bottom: 100%; margin: 0 0 -6px; rotate: 225deg; }
.is-talking .cat__bubble {
  scale: 1;
  transition: scale .35s cubic-bezier(.3, 1.8, .5, 1);
}
.cat__label { transition: opacity .15s; }
.cat.is-talking .cat__label { opacity: 0; }

.meow-count {
  margin: 56px 0 0;
  text-align: center;
  font-size: 1.4rem;
}
.meow-count b {
  font-family: var(--display);
  font-weight: 900;
  color: var(--blood);
}

/* ===============================================================
   ДЛИННОКОТ
   =============================================================== */
.game-section {
  padding: 40px 14px 24px;
  max-width: 560px;
  margin: 0 auto;
}
.section-title--game {
  font-size: clamp(3rem, 16.5vw, 5.6rem);
  color: var(--mustard);
  rotate: -2deg;
  margin-bottom: 18px;
}
.game-section .section-lead { margin-bottom: 18px; }

.play { padding-top: 4px; }

.scoreboard {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 8px;
  margin: 0 4px 2px;
  font-size: 1.3rem;
}
.scoreboard > span:last-child { text-align: right; }
.scoreboard b {
  font-family: var(--display);
  font-weight: 900;
  font-variant-numeric: tabular-nums;
  color: var(--blood);
}

.pause {
  display: grid;
  place-items: center;
  width: 52px;
  height: 44px;
  padding: 0;
  border: 3px solid var(--ink);
  background: var(--ink);
  box-shadow: 3px 3px 0 var(--wine);
  cursor: pointer;
  rotate: -3deg;
  -webkit-tap-highlight-color: transparent;
}
.pause:active { translate: 2px 2px; box-shadow: 1px 1px 0 var(--wine); }
.pause__icon { width: 18px; height: 18px; fill: var(--mustard); }
.pause__icon--pause { display: none; }
.pause[data-state="running"] .pause__icon--pause { display: block; }
.pause[data-state="running"] .pause__icon--play { display: none; }

.board {
  width: min(100% - 2 * var(--knot), 500px, 100vh - 300px);
  width: max(240px, min(100% - 2 * var(--knot), 500px, 100svh - 300px));
  margin: 0 auto;
  filter: drop-shadow(8px 10px 0 rgba(27, 31, 107, .35));
}
.fringe--ink { height: 14px; margin: 0 10px; }

.board__frame {
  position: relative;
  border: var(--knot) solid var(--wine);
  box-shadow:
    inset 0 0 0 var(--knot) var(--mustard),
    0 0 0 var(--knot) var(--mustard),
    0 0 0 calc(var(--knot) * 2) var(--wine);
  padding: calc(var(--knot) * 2);
  margin: calc(var(--knot) * 2) 0;
  background: var(--ink);
  container-type: inline-size;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}
.board__frame:focus { outline: none; }
/* the page only stops scrolling under your thumb while the cat is actually running */
.board__frame.is-playing { touch-action: none; }
.board__frame.is-dead { animation: shake .4s; }
@keyframes shake {
  0%, 100% { translate: 0 0; }
  20% { translate: -8px 2px; }
  40% { translate: 7px -3px; }
  60% { translate: -5px 1px; }
  80% { translate: 3px 0; }
}

#game {
  display: block;
  width: 100%;
  aspect-ratio: 1;
}

.board__overlay {
  position: absolute;
  inset: calc(var(--knot) * 2);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 16px;
  text-align: center;
  color: var(--milk);
  background: rgba(27, 31, 107, .8);
}
/* asleep: the message sits low so the napping cat stays visible at the top */
.board__overlay[data-mode="idle"] {
  justify-content: flex-end;
  padding-bottom: 20px;
  background: linear-gradient(rgba(27, 31, 107, 0) 20%, rgba(27, 31, 107, .88) 40%);
}
.board__overlay[hidden] { display: none; }
.overlay__msg { display: flex; flex-direction: column; align-items: center; gap: 8px; }
/* a win: let the cat that fills the whole carpet show, keep the words on a patch */
.board__overlay[data-mode="won"] { background: rgba(27, 31, 107, .2); }
.board__overlay[data-mode="won"] .overlay__title { font-size: clamp(1rem, min(4.5vw, 5vh), 1.4rem); }
.board__overlay[data-mode="won"] .overlay__text { margin: 0; font-size: 1.05rem; }
.board__overlay[data-mode="won"] .overlay__msg {
  gap: 4px;
  padding: 10px 14px 12px;
  background: rgba(27, 31, 107, .9);
  box-shadow: 0 0 0 3px var(--mustard);
}
@media (pointer: coarse) { .only-kbd { display: none; } }
.overlay__title {
  margin: 0;
  font-family: var(--display);
  font-weight: 700;
  font-size: clamp(1.1rem, min(6vw, 7vh), 1.9rem);
  line-height: 1.05;
  color: var(--mustard);
  max-width: 14ch;
  text-wrap: balance;
}
.overlay__text { margin: 0 0 8px; max-width: 26ch; font-size: 1.2rem; }

.btn {
  padding: 14px 22px 15px;
  font-family: var(--display);
  font-weight: 700;
  font-size: 1.05rem;
  color: var(--wine);
  background: var(--mint);
  border: 3px solid var(--wine);
  box-shadow: 5px 5px 0 var(--wine);
  cursor: pointer;
  rotate: -2deg;
  white-space: nowrap;
  transition: translate .08s, box-shadow .08s;
}
.btn:active { translate: 4px 4px; box-shadow: 1px 1px 0 var(--wine); }
.btn:focus-visible { outline-color: var(--milk); }

/* d-pad: four fat knots around a knot */
.dpad {
  display: grid;
  grid-template-columns: repeat(3, 72px);
  grid-template-rows: repeat(3, 58px);
  grid-template-areas:
    ".    up    ."
    "left knot  right"
    ".    down  .";
  gap: 6px;
  justify-content: center;
  margin: 10px auto 0;
  touch-action: manipulation;
}
.play.is-playing .dpad { touch-action: none; }
.dpad__btn {
  display: grid;
  place-items: center;
  padding: 0;
  border: 3px solid var(--wine);
  background: var(--milk);
  box-shadow: 4px 4px 0 var(--wine);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
  transition: translate .06s, box-shadow .06s, background-color .06s;
}
.dpad__btn svg { width: 24px; height: 24px; fill: var(--blood); }
.dpad__btn:active { translate: 3px 3px; box-shadow: 1px 1px 0 var(--wine); background: var(--mustard); }
.dpad__up { grid-area: up; rotate: -3deg; }
.dpad__down { grid-area: down; rotate: 2deg; }
.dpad__left { grid-area: left; rotate: 2deg; }
.dpad__right { grid-area: right; rotate: -2deg; }
.dpad__knot {
  grid-area: knot;
  place-self: center;
  width: 26px;
  height: 26px;
  rotate: 45deg;
  background:
    linear-gradient(var(--mustard), var(--mustard)) center / 8px 8px no-repeat,
    var(--blood);
  box-shadow: 0 0 0 4px var(--wine);
}

/* a sewn-on clothing label */
.footer {
  padding: 48px 24px calc(56px + env(safe-area-inset-bottom));
  text-align: center;
}
.tag {
  display: inline-block;
  max-width: 22ch;
  margin: 0;
  padding: 16px 22px 18px;
  font-size: 1.35rem;
  line-height: 1.2;
  color: var(--milk);
  background: var(--blood);
  outline: 2px dashed var(--milk);
  outline-offset: -8px;
  box-shadow: 6px 6px 0 var(--ink);
  rotate: -2deg;
}

/* ===============================================================
   bigger screens: the wall gets wider, the carpet stays a carpet
   =============================================================== */
/* left-side cats talk from where their label was, beside their own face */
.cat:nth-child(even):not(:last-child) .cat__bubble {
  top: 28%;
  bottom: auto;
  right: calc(100% - .5 * var(--u));
  transform-origin: 100% 30%;
}
.cat:nth-child(even):not(:last-child) .cat__bubble::after {
  top: 12px;
  left: 100%;
  right: auto;
  margin: 0 0 0 -8px;
  rotate: -45deg;
}

/* very narrow screens: labels get a little smaller so they stay on screen */
@media (max-width: 340px) {
  .cat__label { width: 5.2em; font-size: .9rem; overflow-wrap: normal; }
  .cat__label b { font-size: 1.15em; }
}

/* a phone held sideways: board on the left, arrows on the right, all on one screen */
@media (orientation: landscape) and (max-height: 500px) {
  .game-section { max-width: 760px; }
  /* the score sits above the arrows, so the board can use almost the full height */
  .play {
    padding-top: 0;
    display: grid;
    grid-template-columns: auto 280px;
    grid-template-areas:
      "board score"
      "board dpad";
    grid-template-rows: auto auto;
    justify-content: center;
    align-content: center;
    column-gap: 32px;
    row-gap: 14px;
  }
  .play .scoreboard { grid-area: score; align-self: end; font-size: 1.1rem; margin: 0; }
  .scoreboard > span { white-space: nowrap; }
  .board {
    grid-area: board;
    /* fringes, frame margins and shadow all scale with the knot size */
    width: max(200px, calc(100vh - 60px - 4 * var(--knot)));
    width: max(200px, calc(100svh - 60px - 4 * var(--knot)));
  }
  .dpad { grid-area: dpad; align-self: start; margin: 0; }
}

/* a small board (narrow phones, phones held sideways): compact overlays that fit inside it */
@container (max-width: 340px) {
  .only-kbd { display: none; }
  .board__overlay { padding: 10px; gap: 6px; }
  .board__overlay[data-mode="idle"] { padding-bottom: 12px; }
  .overlay__msg { gap: 4px; }
  .overlay__title { font-size: 1.15rem; max-width: none; }
  .overlay__text { font-size: 1rem; margin-bottom: 2px; }
  .btn { padding: 9px 14px 10px; font-size: .9rem; }
}

@media (min-width: 430px) {
  .tower { --cat-scale: 1; }
  .cat__bubble { font-size: 1.25rem; padding: 8px 12px 10px; }
}

@media (min-width: 760px) {
  :root { --knot: 8px; }
  body { font-size: 1.55rem; }
  .hero { padding-top: 48px; }
  .rug--hero { inset: -22px max(60px, calc(50% - 520px)) 0; }
  .hero__aside { max-width: 1040px; margin: 34px auto 0; padding: 0 60px; }
  .hero__note { font-size: 1.9rem; }
  .tower-section { padding-top: 80px; }
  .tower { --cat-scale: 1.6; padding-top: 110px; }
  .cat__label { font-size: 1.35rem; width: 7.5em; }
  .cat__bubble { font-size: 1.5rem; max-width: 260px; }
  .game-section { padding-top: 80px; }
  .scoreboard { font-size: 1.5rem; }
  .tag { font-size: 1.7rem; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 1ms !important;
    animation-delay: 0ms !important;
    transition: none !important;
    scroll-behavior: auto !important;
  }
}
