:root {
  color-scheme: light;
  --paper: #ded5c3;
  --paper-light: #eee7d9;
  --ink: #24231f;
  --charcoal: #55534b;
  --fog: #bbbdb5;
  --earth: #77604b;
  --rust: #8a4b3f;
  --blue: #789cad;
  --focus: #6f332c;
}

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

html { min-height: 100%; background: #c9c0ae; }

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background:
    linear-gradient(112deg, rgba(255,255,255,.2), transparent 34%),
    var(--paper);
  font-family: Georgia, 'Times New Roman', serif;
  -webkit-font-smoothing: antialiased;
}

body::before {
  content: '';
  position: fixed;
  z-index: 4;
  inset: 0;
  pointer-events: none;
  opacity: .18;
  mix-blend-mode: multiply;
  background-image:
    repeating-linear-gradient(8deg, transparent 0 6px, rgba(63,54,42,.08) 7px, transparent 8px),
    repeating-linear-gradient(98deg, transparent 0 13px, rgba(255,255,255,.21) 14px, transparent 15px),
    radial-gradient(ellipse at 24% 35%, rgba(107,83,57,.1), transparent 20%);
}

button, a { font: inherit; }
button { color: inherit; }
[hidden] { display: none !important; }

.through-page {
  width: min(1180px, 100%);
  min-height: 100vh;
  margin: 0 auto;
  padding: clamp(.75rem, 2vw, 1.5rem);
  display: grid;
  align-content: center;
  gap: .65rem;
}

.through-header {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  min-height: 2rem;
  color: var(--charcoal);
  font-size: .72rem;
  letter-spacing: .13em;
  text-transform: uppercase;
}

.through-header > :last-child { justify-self: end; }
.movement-label { margin: 0; text-align: center; }

.header-controls {
  display: flex;
  justify-content: flex-end;
  gap: clamp(.35rem, 1.5vw, 1rem);
}

.text-control {
  width: max-content;
  padding: .35rem .15rem;
  border: 0;
  border-bottom: 1px solid transparent;
  color: var(--charcoal);
  background: transparent;
  text-decoration: none;
  cursor: pointer;
}

.text-control:hover { color: var(--ink); border-bottom-color: currentColor; }

:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 4px;
}

.paper-frame {
  position: relative;
  overflow: hidden;
  width: 100%;
  aspect-ratio: 16 / 9;
  min-height: 360px;
  max-height: min(77vh, 720px);
  background: var(--paper-light);
  clip-path: polygon(.3% .7%, 24% .15%, 57% .55%, 99.7% 0, 100% 99.1%, 69% 99.7%, 31% 99.25%, .15% 100%);
  box-shadow: 0 20px 58px rgba(45,39,29,.2);
}

.paper-frame::after {
  content: '';
  position: absolute;
  z-index: 3;
  inset: 0;
  pointer-events: none;
  box-shadow: inset 0 0 0 1px rgba(45,42,34,.23), inset 0 0 65px rgba(71,60,43,.11);
}

#through-canvas {
  display: block;
  /* Phaser's FIT scale manager preserves the paper's proportions. */
  touch-action: none;
  cursor: default;
}

#through-canvas:focus-visible { outline-offset: -7px; }

.edge-shadow {
  position: absolute;
  z-index: 2;
  inset: 0;
  pointer-events: none;
  background: linear-gradient(90deg, rgba(30,27,23,.13), transparent 8%, transparent 91%, rgba(30,27,23,.11));
  mix-blend-mode: multiply;
}

.canvas-description {
  max-width: 80ch;
  margin: .15rem auto 0;
  color: var(--charcoal);
  font-size: .79rem;
  line-height: 1.5;
  text-align: center;
}

.controls {
  color: var(--charcoal);
  font-size: clamp(.67rem, 1.4vw, .77rem);
  letter-spacing: .035em;
  line-height: 1.65;
  text-align: center;
}

.controls p { margin: 0; }
.controls summary { cursor: pointer; opacity: .7; }
.controls[open] summary { margin-bottom: .5rem; }
.opening-controls .touch-control-group { grid-template-columns: repeat(auto-fit, minmax(60px, 1fr)); }
kbd { color: var(--ink); font-family: inherit; }

.touch-controls { display: none; }

.touch-button {
  min-width: 0;
  min-height: 3.25rem;
  padding: .55rem .35rem;
  border: 1px solid rgba(62, 57, 48, .3);
  border-radius: 0;
  color: var(--ink);
  background:
    linear-gradient(118deg, rgba(242, 236, 221, .88), rgba(201, 201, 187, .72));
  box-shadow: 0 3px 10px rgba(49, 44, 35, .1);
  letter-spacing: .035em;
  touch-action: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.touch-button[aria-pressed="true"] {
  border-color: rgba(70, 55, 45, .62);
  background: linear-gradient(118deg, rgba(197, 210, 207, .94), rgba(180, 190, 183, .88));
  box-shadow: inset 0 2px 7px rgba(51, 47, 40, .16);
  transform: translateY(1px);
}

.touch-refuse[aria-pressed="true"] {
  background: linear-gradient(118deg, rgba(183, 139, 125, .78), rgba(197, 178, 158, .84));
}

.touch-arrow { font-size: 1.45rem; }
.touch-path { font-size: .76rem; }
.touch-command { color: var(--charcoal); font-size: .76rem; }
.touch-reach { color: #405965; }
.touch-responses .touch-reach { font-size: .75rem; line-height: 1.2; }

.touch-stillness {
  margin: 0;
  color: var(--charcoal);
  font-size: .72rem;
  letter-spacing: .035em;
  text-align: center;
}

.pause-dialog {
  width: min(39rem, calc(100% - 2rem));
  max-height: calc(100svh - 2rem);
  padding: 0;
  border: 0;
  color: var(--ink);
  background: transparent;
  overflow: visible;
}

.pause-dialog::backdrop {
  background: rgba(47, 45, 40, .58);
  backdrop-filter: blur(4px);
}

.pause-paper {
  position: relative;
  z-index: 2;
  max-height: calc(100svh - 2rem);
  overflow-y: auto;
  padding: clamp(1.5rem, 5vw, 3.2rem);
  background:
    repeating-linear-gradient(7deg, transparent 0 9px, rgba(76, 64, 48, .045) 10px, transparent 11px),
    linear-gradient(118deg, rgba(242, 236, 221, .98), rgba(215, 209, 194, .97));
  clip-path: polygon(.5% 1.2%, 32% .15%, 70% .8%, 99.5% 0, 100% 98.7%, 62% 99.6%, 1% 98.9%);
  box-shadow: 0 28px 80px rgba(35, 31, 25, .35);
}

.pause-cloth {
  position: absolute;
  z-index: 1;
  inset: -1.2rem 7% 18% -1.4rem;
  background: linear-gradient(135deg, rgba(91, 122, 134, .3), rgba(123, 146, 149, .08));
  clip-path: polygon(2% 8%, 93% 0, 100% 82%, 15% 100%);
  transform: rotate(-1.5deg);
}

.pause-dialog h2 {
  margin: 0 0 1.4rem;
  color: var(--charcoal);
  font-size: clamp(1rem, 3vw, 1.35rem);
  font-weight: normal;
  letter-spacing: .16em;
  text-transform: uppercase;
}

.pause-dialog p {
  margin: 0 0 1rem;
  line-height: 1.65;
}

.pause-controls {
  margin: 1.5rem 0 0;
  color: var(--charcoal);
  font-size: .88rem;
}

.pause-inventory {
  margin: 1.35rem 0 .5rem;
  padding: .8rem 1rem;
  border: 1px solid rgba(67, 61, 50, .16);
  background: rgba(238, 231, 216, .22);
}

.pause-inventory h3 {
  margin: 0 0 .5rem;
  color: var(--charcoal);
  font-size: .72rem;
  font-weight: normal;
  letter-spacing: .18em;
  text-transform: uppercase;
}

.pause-inventory ul {
  display: flex;
  flex-wrap: wrap;
  gap: .35rem 1rem;
  margin: 0;
  padding: 0;
  list-style: none;
}

.pause-inventory li::before { content: '· '; color: var(--blue); }
.pause-inventory #learned-movements { margin: .55rem 0 0; font-size: .82rem; }

.pause-controls div {
  display: grid;
  grid-template-columns: minmax(9rem, 1fr) 1.25fr;
  gap: 1rem;
  padding: .34rem 0;
  border-bottom: 1px solid rgba(67, 61, 50, .12);
}

.pause-controls dt { color: var(--ink); }
.pause-controls dd { margin: 0; }

.pause-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  margin-top: 1.8rem;
}

.pause-action {
  padding: .62rem 1rem;
  border: 1px solid rgba(47, 44, 38, .4);
  color: var(--ink);
  background: rgba(238, 231, 216, .5);
  text-decoration: none;
  cursor: pointer;
}

.pause-action:hover { background: var(--paper-light); }

.admin-menu {
  margin-top: 1.8rem;
  padding-top: 1.4rem;
  border-top: 1px solid rgba(67, 61, 50, .18);
}

.admin-menu-label {
  margin: 0 0 .7rem;
  color: rgba(67, 61, 50, .55);
  font-size: .72rem;
  letter-spacing: .22em;
  text-transform: uppercase;
}

.admin-menu-section + .admin-menu-section { margin-top: 1.1rem; }

.admin-menu-section h3 {
  margin: 0 0 .55rem;
  color: var(--charcoal);
  font-size: .72rem;
  font-weight: normal;
  letter-spacing: .12em;
  text-transform: uppercase;
}

.admin-menu-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: .55rem;
}

.admin-menu-grid button {
  padding: .5rem .7rem;
  border: 1px solid rgba(47, 44, 38, .3);
  color: var(--ink);
  background: rgba(238, 231, 216, .35);
  font-size: .82rem;
  text-align: left;
  cursor: pointer;
}

.admin-menu-grid button:hover,
.admin-menu-grid button:focus-visible {
  background: var(--paper-light);
}

@media (max-width: 30rem) {
  .admin-menu-grid { grid-template-columns: 1fr; }
}

.ending {
  position: absolute;
  z-index: 5;
  inset: 0;
  display: grid;
  place-content: center;
  justify-items: center;
  padding: 2rem;
  color: #ded8cc;
  text-align: center;
  background: rgba(35,33,29,.84);
  backdrop-filter: blur(3px);
  opacity: 0;
  transition: opacity 2.6s ease, backdrop-filter 2.6s ease;
}

.ending.visible { opacity: 1; }

.ending-passage {
  display: grid;
  gap: clamp(.8rem, 2.5vh, 1.7rem);
  font-size: clamp(1.15rem, 3vw, 2rem);
  line-height: 1.55;
  letter-spacing: .035em;
}

.ending-passage p {
  margin: 0;
  opacity: 0;
  transform: translateY(8px);
  transition: opacity 1.9s ease, transform 1.9s ease;
}

.ending-passage p.visible { opacity: 1; transform: none; }

.continue-link {
  margin-top: clamp(1.4rem, 4vh, 2.8rem);
  padding: .55rem 1.3rem;
  border: 1px solid rgba(222,216,204,.42);
  color: #ded8cc;
  text-decoration: none;
  letter-spacing: .09em;
  opacity: 0;
  transition: opacity 1.8s ease, color 300ms ease, background 300ms ease;
}

.continue-link.visible { opacity: 1; }

.continue-link:hover { color: var(--ink); background: #ded8cc; }

.replay-nudge {
  margin-top: clamp(1rem, 3vh, 2rem);
  font-size: .85rem;
  font-style: italic;
  color: rgba(222,216,204,.68);
  opacity: 0;
  transition: opacity 1.8s ease;
}

.replay-nudge.visible { opacity: 1; }

.status,
.visually-hidden {
  position: absolute !important;
  width: 1px !important;
  height: 1px !important;
  padding: 0 !important;
  margin: -1px !important;
  overflow: hidden !important;
  clip: rect(0, 0, 0, 0) !important;
  white-space: nowrap !important;
  border: 0 !important;
}

.noscript {
  position: fixed;
  z-index: 10;
  inset: 1rem;
  display: grid;
  place-content: center;
  padding: 2rem;
  color: var(--ink);
  background: var(--paper-light);
  text-align: center;
}

@media (max-width: 680px), (max-height: 650px) {
  .through-page { align-content: start; }
  .paper-frame { min-height: 320px; max-height: 70vh; aspect-ratio: auto; }
  .through-header { font-size: .63rem; }
  .header-controls { flex-direction: column; align-items: flex-end; gap: 0; }
  .movement-label { max-width: 14ch; }
  .canvas-description { font-size: .72rem; }
  .pause-controls div { grid-template-columns: 1fr; gap: .1rem; }
}

@media (max-width: 680px) and (orientation: portrait) {
  .paper-frame { aspect-ratio: 8 / 9; min-height: 0; max-height: 70svh; }
  .canvas-description { display: none; }
}

@media (hover: none), (pointer: coarse), (max-width: 820px) {
  .through-page {
    padding-bottom: max(.75rem, env(safe-area-inset-bottom));
  }

  .touch-controls {
    display: grid;
    gap: .42rem;
    width: 100%;
    padding: .35rem;
    background: rgba(214, 207, 191, .76);
    border: 1px solid rgba(63, 57, 48, .12);
    clip-path: polygon(.2% 2%, 32% .4%, 68% 1.5%, 99.8% 0, 100% 98%, 61% 99.5%, .3% 98.5%);
  }

  .touch-control-group {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: .38rem;
  }

  .touch-responses { grid-template-columns: repeat(5, minmax(0, 1fr)); }

  .controls { display: none; }
}

@media (max-height: 520px) and (orientation: landscape) {
  .paper-frame {
    min-height: 220px;
    height: 55svh;
  }

  .touch-controls {
    grid-template-columns: 1fr 1fr;
    align-items: center;
  }

  .touch-stillness { grid-column: 1 / -1; }
  .touch-button { min-height: 2.8rem; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    scroll-behavior: auto !important;
    transition-duration: .01ms !important;
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
  }
  body::before { opacity: .08; }
  .ending, .pause-dialog::backdrop { backdrop-filter: none; }
  .pause-cloth { transform: none; }
}

::selection { color: var(--paper-light); background: rgba(67,52,43,.55); }

.paper-intro {
  position: absolute; inset: 0; z-index: 6;
  color: var(--ink);
  background: repeating-linear-gradient(7deg, transparent 0 9px, rgba(76,64,48,.025) 10px, transparent 11px),
    radial-gradient(ellipse at 48% 42%, #e9dfca, #d5c8ae);
}
.paper-intro[hidden] { display: none; }
.intro-art { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.paper-intro h2 {
  position: absolute; top: 9%; width: 100%; margin: 0;
  text-align: center; font-weight: normal; font-size: clamp(1.2rem, 2.8vw, 2rem);
  letter-spacing: .15em;
}
.intro-skip { position: absolute; top: 1%; right: 2%; min-height: 44px; z-index: 1; }
.intro-caption {
  position: absolute; bottom: 17%; left: 8%; right: 8%; text-align: center;
  font-size: clamp(.95rem, 1.65vw, 1.15rem); line-height: 1.55;
}
.intro-caption p { max-width: 39rem; margin: .5rem auto 0; text-wrap: balance; }
.intro-caption .intro-controls { font-size: .8rem; line-height: 1.45; color: #665c4c; }
.intro-next { position: absolute; bottom: 5%; left: 50%; transform: translateX(-50%); white-space: nowrap; min-height: 44px; }
.intro-active .touch-controls,
.intro-active .canvas-description,
.intro-active .controls { visibility: hidden; }
@media (max-width: 680px) {
  .paper-intro h2 { top: 14%; }
  .intro-caption { bottom: 19%; left: 6%; right: 6%; }
  .paper-intro[data-beat="3"] .intro-art { opacity: .68; }
}
