/* ==========================================================================
   DuneForge — "The Map of Dunes", the playable mini comic (#demo-root)
   Owner: demo agent. Classes are prefixed demo-; the comic grammar comes from
   base.css (.panel .caption .balloon .sfx .btn). The panels sit straight on the
   golden-hour section: the gutters between them are open sky.

   Layout
     phone   <640   header / panels stacked, each with its own shape / choices
     tablet  640+   header / art tier (one or two panels) / choices in a row
     desktop 1024+  header / art tier (2fr) beside the choice panel (1fr)
     The choice panel is a size container: under 340px wide its choices stack
     (desktop up to ~1350px), under 276px the ending slots go two by two.
     Phones on their side (max-height 520px) put the choices beside the art.
   ========================================================================== */

.demo-root {
  container-type: inline-size;
  width: min(100% - 2 * var(--page-pad), var(--container));
  margin-inline: auto;
}
/* If the root is placed inside a .wrap it simply fills it */
.wrap .demo-root { width: 100%; }

.demo {
  display: grid;
  gap: var(--gutter);
}

/* ---- Header: story title + progress (footprints) ------------------------- */
.demo-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--s-2) var(--s-5);
}
.demo-title {
  font-size: var(--fs-h3);
  line-height: var(--lh-snug);
  color: var(--ink);
}
.demo-progress {
  display: flex;
  flex-wrap: wrap;   /* with very large text the page number drops under the footprints instead of widening the demo */
  align-items: center;
  gap: var(--s-3);
}
.demo-pagenum {
  font-family: var(--font-letter);
  font-size: 1rem;
  font-weight: 700;
  line-height: 1.2;
  white-space: nowrap;
  border-radius: 4px;
}
.demo-pagenum:focus { outline: none; }
.demo-pagenum:focus-visible { outline: 3px solid var(--ink); outline-offset: 4px; }

.demo-steps {
  display: flex;
  gap: 2px;
  margin: 0;
  padding: 0;
  list-style: none;
}
.demo-step { width: 32px; height: 32px; }
.demo-step svg { width: 100%; height: 100%; overflow: visible; }
.demo-step path {
  fill: transparent;
  stroke: var(--ink-soft);
  stroke-width: 1.4;
  transition: fill var(--dur-3) var(--ease-out);
}
.demo-step.is-done path { fill: var(--ink); stroke: var(--ink); }
.demo-step.is-current path { fill: var(--ember); stroke: var(--ink); stroke-width: 1.8; }
[dir="rtl"] .demo-step svg { transform: scaleX(-1); }

/* ---- Page: art tier + action panel --------------------------------------- */
.demo-page {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: var(--gutter);
}
.demo-scenes {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: var(--gutter);
}

/* An art panel: the scene fills it; lettering sits on top in reading order */
.demo-panel {
  --panel-bg: var(--night);
  container-type: inline-size;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  overflow: clip; /* clip, unlike hidden, lets the panel grow with long translations */
}
.demo-art {
  position: absolute;
  inset: 0;
  z-index: 0;
  width: 100%;
  height: 100%;
  max-width: none;
  object-fit: cover;
  object-position: var(--focus, 50% 50%);
}
/* Arabic pages read right to left: the art is mirrored so speakers stay beside their balloons */
[dir="rtl"] .demo-art { transform: scaleX(-1); }

.demo-layer {
  position: relative;
  z-index: 1;
  flex: 1 0 auto;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: var(--s-3);
  min-height: calc(var(--phone, 0.8) * 100cqi);
  padding-block-end: var(--s-4);
}

/* Captions (narration) */
.demo-cap {
  max-width: min(40ch, 88%);
  text-wrap: pretty;
}
.demo-cap--corner {
  margin-block-start: calc(var(--bw) * -1);
  margin-inline-start: calc(var(--bw) * -1);
}
.demo-cap--chain {
  margin-block-start: calc(var(--s-1) * -1);
  margin-inline-start: clamp(20px, 7cqi, 56px);
}
.demo-cap--end {
  align-self: flex-end;
  margin-block-start: auto;
  margin-block-end: calc((var(--s-4) + var(--bw)) * -1);
  margin-inline-end: calc(var(--bw) * -1);
}

/* Balloons (the stranger) */
.demo-balloon {
  --demo-inset: clamp(14px, 6cqi, 48px);
  margin-block: var(--s-1) 18px;
  margin-inline: var(--demo-inset);
  max-width: min(32ch, 100% - 2 * var(--demo-inset));
  text-wrap: pretty;
}
.demo-balloon--center { align-self: center; }
.demo-balloon--end { align-self: flex-end; }
.demo-balloon--start { align-self: flex-start; }
.demo-type__off { visibility: hidden; }

.demo-balloon.is-waiting,
.demo-sfx.is-waiting { opacity: 0; }
.demo-balloon.is-in {
  transform-origin: var(--tail-pos) 100%;
  animation: demo-pop 320ms var(--ease-pop) both;
}
[dir="rtl"] .demo-balloon.is-in { transform-origin: calc(100% - var(--tail-pos)) 100%; }
@keyframes demo-pop {
  from { opacity: 0; transform: scale(0.6); }
  to   { opacity: 1; transform: none; }
}

/* Sound effect lettering: "Next time" drawn into the dunes */
.demo-sfx {
  align-self: center;
  margin-block: auto;
  padding-inline: 4cqi;
  max-width: 100%;
  font-size: clamp(2.5rem, 12.5cqi, 8rem);
  line-height: 0.88;
  text-align: center;
  text-transform: uppercase;
  text-wrap: balance;
  -webkit-text-stroke-width: clamp(3px, 0.7cqi, 7px);
}
.demo-sfx.is-in { animation: demo-slam 520ms var(--ease-pop) both; }
[dir="rtl"] .demo-sfx { transform: rotate(6deg); }
@keyframes demo-slam {
  0%   { opacity: 0; transform: rotate(-6deg) scale(1.6); }
  55%  { opacity: 1; }
  100% { opacity: 1; transform: rotate(-6deg) scale(1); }
}
[dir="rtl"] .demo-sfx.is-in { animation-name: demo-slam-rtl; }
@keyframes demo-slam-rtl {
  0%   { opacity: 0; transform: rotate(6deg) scale(1.6); }
  55%  { opacity: 1; }
  100% { opacity: 1; transform: rotate(6deg) scale(1); }
}

/* Narrow panels letter a touch smaller */
@container (max-width: 400px) {
  .demo-cap, .demo-balloon { font-size: 0.9375rem; }
}

/* ---- Action panel: your move ---------------------------------------------- */
.demo-action {
  container: demo-action / inline-size; /* its width, not the window's, decides how choices and slots fit */
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: clamp(18px, 2.4vw, 28px);
  padding: clamp(22px, 3vw, 36px);
  overflow: hidden;
  overflow: clip;
}
/* Ember rings are ~2.4:1 on paper; ink keeps the focus ring above 3:1 inside these panels */
.demo-action :focus-visible { outline-color: var(--ink); }

.demo-prompt {
  font-family: var(--font-display);
  font-size: clamp(1.625rem, 1.2rem + 1.5vw, 2.5rem);
  line-height: var(--lh-snug);
  text-wrap: balance;
}
.demo-choices {
  display: grid;
  gap: var(--s-4);
}
.demo-choice {
  width: 100%;
  min-height: 64px;
  justify-content: flex-start;
  gap: 0.6em;
  padding-inline: 0.9em 1.1em;
  font-size: 1.1875rem;
  text-align: start;
  white-space: normal;
}
.demo-choice svg {
  width: 1.5em;
  height: 1.5em;
  fill: var(--ink);
  transition: transform var(--dur-2) var(--ease-pop);
}
.demo-choice:hover svg,
.demo-choice:focus-visible svg { transform: translateX(3px) rotate(8deg); }
[dir="rtl"] .demo-choice svg { transform: scaleX(-1); }
[dir="rtl"] .demo-choice:hover svg,
[dir="rtl"] .demo-choice:focus-visible svg { transform: scaleX(-1) translateX(3px) rotate(8deg); }

/* The crossroads drawing between question and choices (desktop only) */
.demo-fork { display: none; }
.demo-fork__path {
  fill: none;
  stroke: var(--ink-soft);
  stroke-width: 3;
  stroke-linecap: round;
  stroke-dasharray: 0.1 11;
}
.demo-fork__steps { fill: var(--ink); }

/* Start page for a returning reader: the endings found so far, in miniature */
.demo-tally {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--s-2) var(--s-3);
  padding-block-start: var(--s-4);
  border-block-start: var(--bw-thin) dashed var(--ink-soft);
  font-family: var(--font-letter);
  font-size: 0.9375rem;
  font-weight: 600;
  line-height: 1.3;
  color: var(--ink-soft);
}
.demo-tally__tiles { display: flex; flex: none; gap: 5px; }
.demo-tally__tile {
  display: grid;
  place-items: center;
  width: 24px;
  height: 24px;
  border: var(--bw-thin) dashed var(--ink-soft);
  border-radius: var(--r-panel);
}
.demo-tally__tile.is-found {
  border-style: solid;
  border-color: var(--ink);
  background: var(--night-2);
  color: var(--molten);
}
.demo-tally__tile svg { width: 72%; height: 72%; }
.demo-tally__text { flex: 1 1 12em; min-width: 0; text-wrap: pretty; }

/* ---- End card: the ending, the tracker, what it means ------------------- */
.demo-end {
  justify-content: flex-start;
  gap: var(--s-4);
  padding-block-start: clamp(56px, 6vw, 72px);
}
.demo-end > .demo-cap--corner {
  position: absolute;
  inset-block-start: calc(var(--bw) * -1);
  inset-inline-start: calc(var(--bw) * -1);
  margin: 0;
}
.demo-end__title {
  font-family: var(--font-display);
  font-size: clamp(1.75rem, 1.3rem + 1.4vw, 2.5rem);
  line-height: var(--lh-snug);
  text-wrap: balance;
}
.demo-end__status {
  margin-block-start: calc(var(--s-2) * -1);
  font-weight: 600;
  color: var(--ink-soft);
}
.demo-end__status + .demo-slots { margin-block-start: var(--s-1); }

.demo-slots {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: var(--s-3) var(--s-2);
  margin: 0;
  padding: 0;
  list-style: none;
}
.demo-slot {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--s-2);
  min-width: 0;
  font-family: var(--font-letter);
  font-size: 0.8125rem;
  font-weight: 600;
  line-height: 1.2;
  text-align: center;
  overflow-wrap: anywhere;
}
.demo-slot__tile {
  display: grid;
  place-items: center;
  width: 100%;
  max-width: 80px;
  aspect-ratio: 1;
  border: var(--bw-thin) dashed var(--ink-soft);
  border-radius: var(--r-panel);
  background: transparent;
  color: #C2B6B5; /* ink 28% on paper, for browsers without color-mix() */
  color: color-mix(in srgb, var(--ink) 28%, var(--paper));
}
.demo-slot__tile svg { width: 54%; height: 54%; }
.demo-slot__q { font-family: var(--font-display); font-weight: 400; color: var(--ink-soft); }
.demo-slot.is-found .demo-slot__tile {
  border-style: solid;
  border-color: var(--ink);
  background: var(--night-2);
  color: var(--molten);
}
.demo-slot.is-current .demo-slot__tile {
  border-width: var(--bw);
  background: var(--ink);
  box-shadow: 0 0 0 3px var(--paper), 0 0 0 5px var(--ink);
}
.demo-slot.is-fresh .demo-slot__tile { animation: demo-stamp 620ms var(--ease-pop) both; }
@keyframes demo-stamp {
  0%   { opacity: 0; transform: scale(1.7) rotate(-10deg); }
  60%  { opacity: 1; }
  100% { opacity: 1; transform: none; }
}

.demo-end__point {
  padding-block-start: var(--s-4);
  border-block-start: var(--bw-thin) dashed var(--ink-soft);
  font-family: var(--font-letter);
  font-weight: 600;
  line-height: 1.4;
  text-wrap: pretty;
}
.demo-end__actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--s-3);
  margin-block-start: auto;
}
.demo-end__actions .btn { flex: 1 1 auto; }

/* ---- The ink wipe that turns the page -------------------------------------- */
.demo-wipe {
  position: absolute;
  z-index: 5;
  inset-block: 0;
  inset-inline: -40%;
  visibility: hidden; /* parked off-panel; shown only while it sweeps or covers */
  background: var(--ink);
  transform: translateX(-130%) skewX(-12deg);
  pointer-events: none;
}
.demo-wipe[data-phase],
.demo-wipe.is-covering { visibility: visible; }
.demo-wipe::after {
  content: "";
  position: absolute;
  inset-block: 0;
  inset-inline-end: 0;
  width: 6px;
  background: var(--molten);
}
.demo-wipe[data-phase="out"]::after { inset-inline-end: auto; inset-inline-start: 0; }
.demo-wipe.is-covering { transform: translateX(0) skewX(-12deg); }
[dir="rtl"] .demo-wipe { transform: translateX(130%) skewX(12deg); }
[dir="rtl"] .demo-wipe.is-covering { transform: translateX(0) skewX(12deg); }

/* ---- Tablet ------------------------------------------------------------------ */
@media (min-width: 640px) {
  .demo-layer { min-height: 0; }
  .demo-scenes { min-height: calc(100cqi * 0.56); }
  .demo-scenes--2 { grid-template-columns: minmax(0, 3fr) minmax(0, 2fr); }
  /* Choices side by side. A label too long for half the row takes the room it
     needs (RU «воспоминание»); a pair that cannot share a row wraps. */
  .demo-choose .demo-choices { display: flex; flex-wrap: wrap; }
  .demo-choose .demo-choice { flex: 1 1 0; width: auto; }
}

/* ---- Desktop ----------------------------------------------------------------- */
@media (min-width: 1024px) {
  .demo-page { grid-template-columns: minmax(0, 2fr) minmax(320px, 1fr); }
  .demo-scenes { min-height: calc((100cqi - var(--gutter)) * 0.4); }
  .demo-choose { gap: var(--s-4); text-align: center; }
  .demo-choose .demo-prompt { margin-block-end: var(--s-2); }
  .demo-fork { display: block; width: 100%; height: auto; margin-block: calc(var(--s-2) * -1); }
  .demo-choose .demo-choice {
    flex-direction: column;
    justify-content: center;
    gap: 0.35em;
    min-height: 104px;
    padding: 0.8em 0.7em 0.85em;
    text-align: center;
    text-wrap: balance;
  }
  .demo-choose .demo-tally { justify-content: center; }
  .demo-choose .demo-tally__text { flex: 0 1 auto; }
}

/* ---- Narrow action column (desktop up to ~1350px, phones on their side) ---- */
/* Two choices side by side would squeeze words like RU «воспоминание» past the
   button edges: stack them as on phones and drop the crossroads drawing. */
@container demo-action (width < 340px) {
  .demo-choose .demo-prompt,
  .demo-choose .demo-tally { text-align: start; }
  .demo-choose .demo-tally { justify-content: flex-start; }
  .demo-choose .demo-tally__text { flex: 1 1 12em; }
  .demo-choose .demo-choices { display: grid; }
  .demo-choose .demo-choice {
    flex-direction: row;
    justify-content: flex-start;
    gap: 0.6em;
    min-height: 64px;
    padding-block: 0.62em 0.68em;
    padding-inline: 0.9em 1.1em;
    text-align: start;
  }
  .demo-fork { display: none; }
}
/* Four ending slots need ~60px each for the longest title word (RU «В другой»):
   below that they go two by two instead of breaking words. */
@container demo-action (width < 276px) {
  .demo-slots { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

/* ---- Browsers without container query units (iOS 15, older Firefox) ------ */
@supports not (width: 1cqi) {
  .demo-layer { min-height: calc(var(--phone, 0.8) * (100vw - 2 * var(--page-pad))); }
  .demo-balloon { --demo-inset: 24px; }
  .demo-cap--chain { margin-inline-start: 32px; }
  .demo-sfx { font-size: clamp(2.5rem, 9vw, 6.5rem); -webkit-text-stroke-width: 4px; padding-inline: 16px; }
  @media (min-width: 640px) {
    .demo-layer { min-height: 0; }
    .demo-scenes { min-height: calc(min(100vw - 2 * var(--page-pad), var(--container)) * 0.56); }
  }
  @media (min-width: 1024px) {
    .demo-scenes { min-height: calc(min(100vw - 2 * var(--page-pad), var(--container)) * 0.4); }
  }
}

/* ---- Phones on their side (and short windows) ------------------------------ */
/* The art is cut to the screen's height (minus ~150px: site header, demo header,
   a little air) and the choices sit beside it, so a page turn shows the question
   without a scroll past the picture. The action column keeps >= 276px so stacked
   RU choices still fit; the start page gives its wider half to the stranger, who
   has the most to say, and the camp keeps 150px for its caption's longest word. */
@media (min-width: 640px) and (max-height: 520px) and (orientation: landscape) {
  .demo-page { grid-template-columns: minmax(0, 3fr) minmax(276px, 2fr); }
  .demo-scenes { min-height: calc(100vh - 150px); min-height: calc(100svh - 150px); }
  .demo-scenes--2 { grid-template-columns: minmax(150px, 2fr) minmax(0, 3fr); }
  .demo-action { gap: 14px; padding: 18px; justify-content: flex-start; }
  .demo-prompt { font-size: 1.5rem; }
  .demo-choices { gap: var(--s-3); }
  .demo-end { padding-block-start: 48px; }
}

/* ---- Reduced motion: no wipe, no pops, text appears whole ---------------- */
@media (prefers-reduced-motion: reduce) {
  .demo-wipe { display: none; }
  .demo-balloon.is-in,
  .demo-sfx.is-in,
  .demo-slot.is-fresh .demo-slot__tile { animation: none; }
  .demo-balloon.is-waiting,
  .demo-sfx.is-waiting { opacity: 1; }
}
