/* Chapter 4: Dalia's Memory Machine */
.mm-layout {
  display: grid;
  grid-template-areas: "head" "machine" "keys" "shelf" "cont";
  justify-items: center;
  align-content: center;
  row-gap: clamp(6px, 1.4dvh, 12px);
  width: 100%;
}
.mm-head { grid-area: head; text-align: center; }
.mm-head .label { margin: 0 0 2px; }
.mm-keys {
  grid-area: keys;
  margin: -2px 0 0;
  font: 700 12px/1 var(--font-sans);
  color: var(--cocoa-soft);
  opacity: .85;
}
@media (hover: none), (pointer: coarse) { .mm-keys { display: none; } }
.chapter-memories .continue-wrap { grid-area: cont; }

/* the intro page */
.mm-start {
  position: absolute;
  inset: 0;
  z-index: 3;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: clamp(8px, 1.6dvh, 14px);
  padding: 24px 20px;
  text-align: center;
}
.mm-start .label { margin: 0; }
.mm-start-title { margin: 0; font: italic 600 clamp(34px, 3vw + 24px, 50px)/1.05 var(--font-serif); color: var(--cocoa); }
.mm-start-message { margin: 0; max-width: 26ch; font: italic 500 clamp(21px, 1.2vw + 16px, 26px)/1.3 var(--font-serif); color: var(--cocoa); text-wrap: balance; }
.mm-start-row { display: flex; justify-content: center; gap: clamp(2px, 1vw, 8px); margin: clamp(10px, 3dvh, 26px) 0 clamp(4px, 1dvh, 10px); }
.mm-start-plush { width: clamp(36px, 10vw, 58px); animation: mm-hop 2.6s var(--d, 0s) ease-in-out infinite; }
.mm-start-plush svg { display: block; width: 100%; height: auto; }
@keyframes mm-hop { 0%, 70%, 100% { transform: translateY(0); } 80% { transform: translateY(-16%) rotate(-5deg); } 90% { transform: translateY(0) scale(1.04, .96); } }
.mm-coin {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  padding: 6px;
  border: 0;
  background: none;
  cursor: pointer;
  color: var(--cocoa);
  font: 800 16px/1 var(--font-sans);
  perspective: 400px;
}
.mm-coin svg { width: clamp(78px, 20vw, 100px); height: auto; animation: mm-coin 3s ease-in-out infinite; }
.mm-coin:hover svg { animation-duration: 1.2s; }
@keyframes mm-coin { 0%, 100% { transform: rotateY(0deg) translateY(0); } 50% { transform: rotateY(28deg) translateY(-5px); } }
.mm-coin-label { padding: 8px 16px 9px; border-radius: 999px; background: var(--paper); box-shadow: 0 6px 14px -8px rgba(122, 75, 71, .45); }
.mm-coin:focus-visible { outline: 3px solid var(--candy); outline-offset: 6px; border-radius: 20px; }
.mm-start-how { margin: 2px 0 0; }

/* the cabinet */
.machine {
  grid-area: machine;
  position: relative;
  width: min(380px, 84vw, calc((100dvh - 260px) * 300 / 440));
  aspect-ratio: 300 / 440;
  container-type: inline-size;
  filter: drop-shadow(0 18px 22px rgba(122, 75, 71, .22));
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}
.machine-art, .machine-art svg { position: absolute; inset: 0; width: 100%; height: 100%; }
.machine-front { pointer-events: none; z-index: 3; }
.bulb { fill: #FFF6F8; stroke: #7A4B47; stroke-width: 1.6; animation: bulb 1.1s steps(1) infinite; }
.bulb.b2 { animation-delay: .55s; }
@keyframes bulb { 0% { fill: #FFF6F8; } 50% { fill: #FB6F92; } }

.machine-marquee {
  position: absolute;
  left: 11.3%;
  top: 4.1%;
  width: 77.4%;
  height: 10%;
  margin: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font: italic 600 6.5cqw/1 var(--font-serif);
  color: var(--cocoa);
  white-space: nowrap;
  z-index: 1;
}

/* the glass: where the claw and the plushies live */
.machine-glass {
  position: absolute;
  left: 10%;
  top: 20%;
  width: 80%;
  height: 53.18%;
  border-radius: 6cqw;
  overflow: hidden;
  background:
    radial-gradient(circle at 20% 30%, rgba(255, 255, 255, .9) 0 1.2cqw, transparent 1.3cqw),
    radial-gradient(circle at 70% 18%, rgba(255, 194, 209, .6) 0 .9cqw, transparent 1cqw),
    radial-gradient(circle at 86% 46%, rgba(255, 255, 255, .9) 0 .8cqw, transparent .9cqw),
    radial-gradient(circle at 40% 52%, rgba(255, 194, 209, .45) 0 .7cqw, transparent .8cqw),
    linear-gradient(#FFF9FB, #FFE3EB);
  z-index: 2;
}
.machine-rail {
  position: absolute;
  left: 3%;
  right: 3%;
  top: 2.4%;
  height: 3.4%;
  border-radius: 99px;
  background: #FF8FAB;
  border: .7cqw solid #7A4B47;
}
.claw { position: absolute; left: 0; top: 0; z-index: 5; will-change: transform; }
.claw-cable {
  position: absolute;
  left: 50%;
  top: 0;
  width: .9cqw;
  margin-left: -.45cqw;
  background: #7A4B47;
  border-radius: 1cqw;
}
.claw-cable::before {   /* the carriage on the rail */
  content: "";
  position: absolute;
  left: 50%;
  top: -1.6cqw;
  width: 7cqw;
  height: 3.6cqw;
  translate: -50% 0;
  border-radius: 1.4cqw;
  background: #FFB3C6;
  border: .7cqw solid #7A4B47;
}
.claw-art { position: absolute; left: 0; top: 0; width: 100%; transform-origin: 50% 0; }
.claw-art svg { display: block; width: 100%; height: auto; }

.ball-pit { position: absolute; inset: 0; width: 100%; height: 100%; z-index: 3; pointer-events: none; }
.prize { position: absolute; left: 0; top: 0; z-index: 2; transform-origin: 50% 60%; will-change: transform; }
.prize.is-up { z-index: 1; }      /* higher in the pile = further back */
.prize.is-held { z-index: 4; }
.prize svg { display: block; width: 100%; height: auto; }

.machine-bubble {
  position: absolute;
  left: 50%;
  top: 17%;
  margin: 0;
  padding: 1.6cqw 3.4cqw;
  translate: -50% 0;
  border-radius: 99px;
  background: #FFF6F8;
  border: .7cqw solid #7A4B47;
  font: 800 3.9cqw/1.1 var(--font-sans);
  color: var(--cocoa);
  white-space: nowrap;
  opacity: 0;
  scale: .8;
  transition: opacity .25s ease, scale .35s var(--ease-bounce);
  z-index: 5;
  pointer-events: none;
}
.machine-bubble.is-on { opacity: 1; scale: 1; }

/* the control panel */
.machine-panel {
  position: absolute;
  left: 34.7%;
  top: 77.3%;
  width: 57.3%;
  height: 16.4%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 2.6cqw;
  z-index: 4;
}
.mm-arrow, .mm-drop {
  flex: none;
  padding: 0;
  border: .9cqw solid #7A4B47;
  cursor: pointer;
  touch-action: none;
  transition: transform .12s ease, box-shadow .12s ease;
}
.mm-arrow {
  width: 14.5cqw;
  height: 14.5cqw;
  min-width: 40px;
  min-height: 40px;
  border-radius: 4.2cqw;
  background: #FFB3C6;
  color: #7A4B47;
  box-shadow: 0 1.1cqw 0 #FF8FAB;
}
.mm-arrow svg { width: 62%; height: auto; display: block; margin: auto; }
.mm-drop {
  width: 18cqw;
  height: 18cqw;
  min-width: 48px;
  min-height: 48px;
  margin-left: 1.4cqw;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #FF8FAB, #FB6F92 55%, #E63950);
  box-shadow: 0 1.3cqw 0 #C92A42;
  font: 800 max(13px, 4.6cqw)/1 var(--font-sans);
  color: #fff;
  letter-spacing: .02em;
}
.mm-arrow:active, .mm-arrow.is-held { transform: translateY(1cqw); box-shadow: 0 .2cqw 0 #FF8FAB; }
.mm-drop:active { transform: translateY(1.1cqw); box-shadow: 0 .2cqw 0 #C92A42; }
.mm-arrow:focus-visible, .mm-drop:focus-visible { outline: 3px solid var(--candy); outline-offset: 3px; }
.is-off { opacity: .55; cursor: default; }

/* winning */
.prize-pop {
  position: absolute;
  left: 20.3%;
  top: 86%;
  width: 18cqw;
  opacity: 0;
  transform: translate(-50%, -50%);
  z-index: 5;
  pointer-events: none;
}
.prize-pop svg { display: block; width: 100%; height: auto; filter: drop-shadow(0 1cqw 1cqw rgba(122, 75, 71, .3)); }
.machine-cheer {
  position: absolute;
  left: 50%;
  top: 44%;
  margin: 0;
  padding: 2.6cqw 6cqw;
  border-radius: 99px;
  background: #FB6F92;
  border: 1cqw solid #7A4B47;
  font: 800 7cqw/1 var(--font-sans);
  color: #fff;
  white-space: nowrap;
  opacity: 0;
  transform: translate(-50%, -50%);
  box-shadow: 0 1.4cqw 0 #C92A42;
  z-index: 6;
  pointer-events: none;
}
.mm-burst {
  position: absolute;
  left: 50%;
  top: 44%;
  width: 6cqw;
  z-index: 6;
  pointer-events: none;
}
.mm-burst svg { display: block; width: 100%; height: auto; }

/* the collection row */
.mm-shelf { grid-area: shelf; display: flex; flex-direction: column; align-items: center; gap: 6px; }
.mm-slots { display: flex; flex-wrap: wrap; justify-content: center; gap: 5px; max-width: 92vw; }
.mm-slot {
  width: clamp(32px, 9vw, 42px);
  height: clamp(32px, 9vw, 42px);
  padding: 3px;
  border-radius: 12px;
  border: 2px dashed rgba(251, 111, 146, .55);
  background: rgba(255, 246, 248, .7);
  cursor: default;
}
.mm-slot svg { display: block; width: 100%; height: auto; filter: grayscale(1) brightness(1.1); opacity: .4; }
.mm-slot.is-won {
  border: 2.5px solid #7A4B47;
  background: #FFF6F8;
  cursor: pointer;
  transition: transform .2s var(--ease-bounce);
}
.mm-slot.is-won svg { filter: none; opacity: 1; }
.mm-slot.is-won:hover { transform: translateY(-3px) rotate(-4deg); }
.mm-slot:focus-visible { outline: 3px solid var(--candy); outline-offset: 2px; }
.mm-counter-row { display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 4px 10px; }
.mm-counter { margin: 0; font: 700 14px/1.2 var(--font-sans); color: var(--cocoa-soft); }
.mm-reset {
  padding: 5px 11px 6px;
  border-radius: 999px;
  border: 2px solid rgba(122, 75, 71, .32);
  background: rgba(255, 246, 248, .85);
  color: var(--cocoa-soft);
  font: 800 12px/1 var(--font-sans);
  white-space: nowrap;
  cursor: pointer;
  transition: background .2s, border-color .2s, color .2s;
}
.mm-reset:hover { border-color: var(--cocoa); color: var(--cocoa); }
.mm-reset.is-armed { background: var(--rouge); border-color: var(--rouge); color: #fff; }
.mm-reset:focus-visible { outline: 3px solid var(--candy); outline-offset: 2px; }
.mm-reset[hidden] { display: none; }
.mm-counter.is-complete { font: italic 600 20px/1.2 var(--font-serif); color: var(--rouge); }

/* the memory card: a scrapbook keepsake */
.memory-overlay {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 16px;
  background: rgba(255, 229, 236, .88);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
}
.memory-card {
  position: relative;
  width: min(440px, calc(100vw - 32px));
  max-height: calc(100dvh - 120px);
  margin-top: 30px;
  display: flex;
  flex-direction: column;
  border-radius: 24px;
  background:
    radial-gradient(circle, rgba(255, 179, 198, .35) 1px, transparent 1.5px) 0 0 / 14px 14px,
    linear-gradient(#FFFDFB, #FFF6F5);
  box-shadow: var(--shadow-soft), inset 0 0 0 1px rgba(255, 179, 198, .5);
}
.memory-badge {
  position: absolute;
  top: -31px;
  left: 50%;
  width: 62px;
  height: 62px;
  translate: -50% 0;
  padding: 5px;
  border-radius: 50%;
  background: #FFE5EC;
  border: 3px solid #7A4B47;
  box-shadow: 0 6px 12px -6px rgba(122, 75, 71, .5);
  z-index: 2;
}
.memory-badge svg { display: block; width: 100%; height: auto; }
.memory-scroll {
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 38px clamp(18px, 5vw, 30px) 22px;
  border-radius: 24px;
  text-align: center;
  color: var(--cocoa);
  scrollbar-width: thin;
  scrollbar-color: var(--pastel) transparent;
}
.memory-eyebrow { margin: 0 0 6px; font: 800 12px/1 var(--font-sans); letter-spacing: .14em; text-transform: uppercase; color: var(--rouge); }
.memory-title { margin: 0; font: italic 600 clamp(25px, 1.6vw + 18px, 30px)/1.15 var(--font-serif); text-wrap: balance; }
.memory-photo {
  position: relative;
  margin: 18px 4px 0;
  padding: 10px 10px 14px;
  background: #fff;
  border-radius: 6px;
  box-shadow: 0 10px 22px -12px rgba(122, 75, 71, .45), 0 0 0 1px rgba(122, 75, 71, .08);
  rotate: -1.2deg;
}
.memory-photo::before, .memory-photo::after {   /* washi tape */
  content: "";
  position: absolute;
  top: -10px;
  width: 64px;
  height: 20px;
  background: repeating-linear-gradient(45deg, rgba(255, 143, 171, .6) 0 6px, rgba(255, 194, 209, .6) 6px 12px);
  z-index: 1;
}
.memory-photo::before { left: 10px; rotate: -8deg; }
.memory-photo::after { right: 10px; rotate: 7deg; }
.photo-view { position: relative; height: clamp(150px, 46dvh, 400px); overflow: hidden; border-radius: 3px; background: #FFF6F8; touch-action: pan-y; }
.photo-track { display: flex; height: 100%; transition: transform .45s var(--ease-soft); }
.photo-slide { flex: 0 0 100%; height: 100%; }
.photo-slide img { display: block; width: 100%; height: 100%; object-fit: contain; cursor: zoom-in; user-select: none; -webkit-user-drag: none; }
.photo-slide.is-missing {
  display: grid;
  place-items: center;
  box-sizing: border-box;
  border: 2px dashed rgba(251, 111, 146, .5);
  border-radius: 3px;
  align-content: center;
  gap: 4px;
  font: 700 14px/1.2 var(--font-sans);
  color: var(--cocoa-soft);
}
.photo-slide.is-missing small { font-weight: 600; font-size: 12px; opacity: .8; }
.photo-btn {
  position: absolute;
  top: calc(50% - 4px);
  width: 36px;
  height: 36px;
  translate: 0 -50%;
  padding: 0 0 3px;
  border-radius: 50%;
  border: 2.5px solid #7A4B47;
  background: rgba(255, 255, 255, .92);
  color: #7A4B47;
  font: 800 22px/1 var(--font-sans);
  cursor: pointer;
  z-index: 2;
}
.photo-btn.prev { left: 2px; }
.photo-btn.next { right: 2px; }
.photo-btn:disabled { opacity: .3; cursor: default; }
.photo-btn:focus-visible { outline: 3px solid var(--candy); outline-offset: 2px; }
.photo-zoom-btn {
  position: absolute;
  right: 16px;
  bottom: 22px;
  width: 32px;
  height: 32px;
  padding: 5px;
  border-radius: 50%;
  border: 2px solid #7A4B47;
  background: rgba(255, 255, 255, .92);
  color: #7A4B47;
  cursor: pointer;
  z-index: 2;
}
.photo-zoom-btn svg { display: block; width: 100%; height: 100%; }
.photo-zoom-btn:focus-visible { outline: 3px solid var(--candy); outline-offset: 2px; }
.photo-zoom {
  position: absolute;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  padding: max(14px, env(safe-area-inset-top)) 12px max(14px, env(safe-area-inset-bottom));
  background: rgba(58, 34, 32, .86);
  cursor: zoom-out;
}
.photo-zoom img { max-width: calc(100vw - 24px); max-height: calc(100dvh - 40px); width: auto; height: auto; min-width: 0; min-height: 0; border-radius: 6px; box-shadow: 0 20px 40px -16px rgba(0, 0, 0, .6); }
.photo-zoom-close {
  position: absolute;
  top: max(10px, env(safe-area-inset-top));
  right: 12px;
  width: 44px;
  height: 44px;
  padding: 0 0 4px;
  border-radius: 50%;
  border: 2.5px solid #FFE5EC;
  background: rgba(122, 75, 71, .6);
  color: #FFE5EC;
  font: 700 28px/1 var(--font-sans);
  cursor: pointer;
}
.photo-dots { display: flex; justify-content: center; gap: 6px; margin-top: 9px; }
.photo-dots span { width: 7px; height: 7px; border-radius: 50%; background: var(--pastel); transition: background .2s; }
.photo-dots span.is-on { background: var(--rouge); }
.memory-caption {
  margin: 16px 0 0;
  font: 500 clamp(19px, .5vw + 16px, 20.5px)/1.5 var(--font-serif);
  white-space: pre-line;
  text-wrap: pretty;
}
.memory-close {
  min-height: 44px;
  padding: 10px 22px;
  border: 3px solid var(--cocoa);
  border-radius: 999px;
  background: var(--paper);
  color: var(--cocoa);
  font: 800 16px/1 var(--font-sans);
  cursor: pointer;
}
.memory-close:focus-visible { outline: 3px solid var(--candy); outline-offset: 3px; }

/* short screens */
@media (max-height: 700px) {
  .mm-head .label { display: none; }
  .machine { width: min(380px, 84vw, calc((100dvh - 230px) * 300 / 440)); }
}
/* phones held sideways: machine on the left, everything else on the right */
@media (orientation: landscape) and (max-height: 600px) {
  .mm-start { gap: 6px; padding: 12px 20px; }
  .mm-start-row { margin: 4px 0; }
  .mm-start-plush { width: 38px; }
  .mm-coin svg { width: 64px; }
  .mm-layout {
    grid-template-areas: "machine head" "machine keys" "machine shelf" "machine cont";
    grid-template-columns: auto minmax(0, 390px);
    grid-template-rows: auto auto auto auto;
    column-gap: clamp(20px, 4vw, 44px);
    align-items: center;
    align-content: center;
    justify-content: center;
  }
  .machine { width: calc((100dvh - 28px) * 300 / 440); }
  .mm-head .label { display: block; }
  .memory-card { max-height: calc(100dvh - 96px); margin-top: 26px; }
  .memory-overlay { gap: 8px; padding: 8px 16px; }
  .photo-view { height: clamp(120px, 44dvh, 240px); }
  .memory-scroll { padding-top: 32px; }
}

@media (prefers-reduced-motion: reduce) {
  .bulb, .mm-start-plush, .mm-coin svg { animation: none; }
  .photo-track, .machine-bubble, .mm-slot.is-won, .mm-arrow, .mm-drop { transition: none; }
}
