* {
  box-sizing: border-box;
}
:root {
  color-scheme: light;
  background: #f9edde;
  color: #483039;
  font-family: ui-monospace, "Courier New", monospace;
}
body {
  margin: 0;
  min-height: 100svh;
  background: radial-gradient(ellipse at 65% 5%, #fff8eb, #f7eada 74%);
  padding: 14px max(12px, env(safe-area-inset-right))
    max(12px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
}
main {
  max-width: 980px;
  margin: auto;
}
header {
  height: 42px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.brand {
  color: #705381;
  font-weight: 900;
  letter-spacing: 0.04em;
}
h1 {
  margin: 10px 0 16px;
  font-size: clamp(27px, 5vw, 48px);
  letter-spacing: -0.07em;
  line-height: 1.05;
}
button {
  font: inherit;
  font-weight: bold;
  color: #5a3740;
  background: #fff5e8;
  border: 1px solid #ac7971;
  border-radius: 8px;
  min-height: 44px;
  padding: 7px 12px;
  cursor: pointer;
  touch-action: manipulation;
}
button:focus-visible {
  outline: 3px solid #795295;
  outline-offset: 3px;
}
#map {
  font-size: 12px;
}
.stage {
  position: relative;
  isolation: isolate;
  border: 1px solid #c39b85;
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 8px 22px #8c554529;
}
canvas {
  display: block;
  width: 100%;
  height: clamp(290px, 60svh, 510px);
  image-rendering: pixelated;
  background: #f1dcc7;
}
#panel {
  position: absolute;
  left: 12px;
  bottom: 12px;
  width: min(calc(100% - 24px), 420px);
  border: 1px solid #ca9e82;
  border-radius: 8px;
  background: #fff7eaf7;
  box-shadow: 0 3px #bb80645c;
  padding: 14px 16px;
}
.stage.map-view canvas {
  height: 680px;
  min-height: 680px;
}
#calendar {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  z-index: 2;
}
.day {
  position: absolute;
  display: block;
  min-height: 0;
  max-width: none;
  padding: 0;
  border: 0;
  border-radius: 5px;
  background: transparent;
  box-shadow: none;
  color: transparent;
  touch-action: manipulation;
}
.day .day-number {
  opacity: 0;
}
.day:hover,
.day.is-complete {
  background: transparent;
  box-shadow: none;
  transform: none;
  color: transparent;
  border: 0;
}
.day:focus-visible {
  outline: 2px solid #f7d995;
  outline-offset: 1px;
}
#map-intro {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  border: 0;
}
.map-note {
  font:
    12px/1.4 system-ui,
    sans-serif;
  color: #8b7375;
  text-align: center;
  margin: 11px 0 0;
}
.label {
  margin: 0 0 7px;
  color: #82484e;
  font-size: 12px;
  letter-spacing: 0.1em;
  font-weight: 800;
}
h2 {
  font-size: clamp(19px, 4vw, 29px);
  line-height: 1.1;
  letter-spacing: -0.045em;
  margin: 0 0 9px;
}
#story p {
  font-size: 14px;
  line-height: 1.38;
  margin: 0 0 11px;
  color: #5e444c;
}
#story p strong {
  color: #783d47;
}
.primary {
  background: #934d56;
  color: #fff9ed;
  border-color: #783d47;
  box-shadow: 0 3px #6e3842;
}
.primary:hover {
  background: #824450;
}
#mission,
#hint {
  position: absolute;
  pointer-events: none;
  border: 2px solid #fff0d7;
  border-radius: 8px;
  background: #35233e;
  color: #fff8ed;
  box-shadow: 0 2px 0 #281c2f66;
  font:
    700 clamp(15px, 2vw, 17px)/1.2 system-ui,
    sans-serif;
  padding: 7px 12px;
}
#mission {
  top: 9px;
  left: 9px;
  max-width: calc(100% - 18px);
}
#hint {
  bottom: 8px;
  left: 50%;
  transform: translateX(-50%);
  width: max-content;
  max-width: calc(100% - 16px);
  text-align: center;
}
#failure-overlay {
  position: absolute;
  inset: 0;
  z-index: 3;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: #24182e;
  pointer-events: none;
  text-align: center;
  color: #fff7e7;
}
/* A short paper note, using the same nine-slice parchment as completion cards. */
#failure-overlay > div {
  width: min(100%, 430px);
  padding: 23px 18px 27px;
  border: 22px solid transparent;
  border-image: url("parchment-letter-v2.webp") 130 fill / 22px / 0 stretch;
  border-radius: 0;
  background: none;
  box-shadow: none;
  filter: drop-shadow(0 8px 14px #120e1852);
  color: #513b2e;
  font-family: Georgia, "Times New Roman", serif;
}
#failure-overlay strong {
  display: block;
  font:
    400 clamp(24px, 5vw, 30px)/1.2 Georgia,
    "Times New Roman",
    serif;
  letter-spacing: -0.025em;
  color: #513b2e;
}
#failure-overlay p {
  font:
    400 1rem/1.5 Georgia,
    "Times New Roman",
    serif;
  color: #70543d;
  margin: 15px 0 0;
  overflow-wrap: break-word;
}
@media (max-width: 640px) {
  #failure-overlay {
    padding: 12px;
  }
  #failure-overlay > div {
    border-width: 16px;
    border-image-width: 16px;
    padding: 22px 13px 25px;
  }
  #failure-overlay strong {
    font-size: 25px;
  }
}

#controls {
  margin-top: 13px;
  display: none;
  justify-content: space-between;
  gap: 5px;
  user-select: none;
  -webkit-user-select: none;
}
#controls div {
  display: flex;
  gap: 6px;
}
#controls button {
  min-width: 48px;
  min-height: 54px;
  padding: 5px 8px;
  touch-action: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  box-shadow: 0 2px #bb867769;
}
#controls button[data-key="left"],
#controls button[data-key="right"] {
  font-size: 23px;
}
#controls button.active {
  background: #e9d4f1;
  color: #502d6b;
  border-color: #89669a;
}
#keyboard {
  font-size: 13px;
  color: #654a52;
  text-align: center;
  margin-top: 14px;
}
kbd {
  padding: 1px 4px;
  border: 1px solid #ba9785;
  border-radius: 3px;
  color: #543742;
  background: #fff7ec;
}
[hidden] {
  display: none !important;
}
@media (any-pointer: coarse), (max-width: 640px) {
  #controls {
    display: flex;
  }
  #keyboard {
    display: none;
  }
  canvas {
    height: clamp(245px, calc(100svh - 184px), 520px);
  }
}
@media (max-height: 550px) {
  canvas {
    height: clamp(185px, calc(100svh - 153px), 330px);
  }
  #panel {
    padding: 8px 10px;
  }
  #story p {
    font-size: 12px;
    margin-bottom: 6px;
  }
  h1 {
    margin: 4px 0 8px;
  }
}
@media (max-height: 550px) {
  #hint {
    bottom: 4px;
    padding: 5px 8px;
  }
}

#controls button[data-key="jump"] {
  min-width: 89px;
}

#greeting {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

@media (max-width: 640px) {
  .stage.map-view canvas {
    height: 800px;
    min-height: 800px;
    aspect-ratio: auto;
  }
  .map-note {
    font-size: 11px;
  }
}

#scene-caption {
  width: fit-content;
  max-width: calc(100% - 24px);
  margin: 10px auto 0;
  padding: 8px 12px;
  border: 2px solid #fff0d7;
  border-radius: 8px;
  background: #35233ef5;
  color: #fff8ed;
  font:
    700 16px/1.3 system-ui,
    sans-serif;
  text-align: center;
}

/* Keep the railway visible below the introductory and completion panels. */
.stage.railway-tableau #panel {
  top: 12px;
  bottom: auto;
}

/* The canvas keeps its gameplay height; the completion panel sits below it. */
.stage.carousel-tableau #panel {
  position: relative;
  left: auto;
  bottom: auto;
  width: auto;
  margin: 12px;
}

/* Keep the full illuminated town hall visible above the completion panel. */
.stage.hall-tableau #panel {
  position: relative;
  left: auto;
  bottom: auto;
  width: auto;
  margin: 12px;
}

/* A real illustrated paper sheet behind each pre-level letter. */
.stage.brief-view {
  display: grid;
  place-items: center;
  min-height: 590px;
  padding: 24px 0;
}
.stage.brief-view canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}
.stage.brief-view::before {
  content: "";
  position: absolute;
  inset: 0;
  background: #241a2299;
  z-index: 1;
  pointer-events: none;
}
.stage.brief-view #panel {
  position: relative;
  inset: auto;
  isolation: isolate;
  z-index: 2;
  display: flex;
  flex-direction: column;
  width: min(calc(100% - 44px), 490px);
  min-height: 540px;
  margin: 0;
  padding: 49px 48px 43px;
  border: 0;
  border-radius: 0;
  background: none;
  box-shadow: none;
  color: #4f382d;
  font-family: Georgia, "Times New Roman", serif;
}
.stage.brief-view #panel::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  background: url("parchment-letter-v2.webp") center/100% 100% no-repeat;
  filter: drop-shadow(0 10px 14px #21141e45);
  pointer-events: none;
}
.stage.brief-view #panel::after {
  content: none;
}
.stage.brief-view #letter-date {
  margin: 0 0 23px;
  text-align: right;
  font:
    italic 16px/1.4 Georgia,
    serif;
  color: #8a6e50;
}
.stage.brief-view #label {
  font:
    500 10px/1.6 Georgia,
    serif;
  letter-spacing: 0.12em;
  color: #8b6b4d;
  margin: 0 0 8px;
}
.stage.brief-view #title {
  font:
    400 30px/1.18 Georgia,
    serif;
  letter-spacing: -0.025em;
  color: #51392e;
  margin: 0 0 25px;
}
.stage.brief-view #letter-salutation {
  font:
    19px/1.5 Georgia,
    serif;
  color: #5c4031;
  margin: 0 0 12px;
}
.stage.brief-view #story p {
  font:
    18px/1.65 Georgia,
    serif;
  color: #4f382d;
  margin: 0 0 16px;
}
.stage.brief-view #letter-signoff {
  font:
    italic 16px/1.5 Georgia,
    serif;
  text-align: right;
  color: #806147;
  margin: 18px 0 29px;
}
.stage.brief-view #continue {
  align-self: center;
  position: relative;
  min-width: 204px;
  min-height: 46px;
  width: auto;
  margin-top: auto;
  padding: 11px 21px;
  border: 1px solid #304c3d;
  border-radius: 4px;
  background: #496454;
  box-shadow: 0 2px 0 #304638;
  font:
    600 16px/1.3 Georgia,
    serif;
  letter-spacing: 0.015em;
  color: #fff3d8;
}
.stage.brief-view #continue:hover {
  background: #3c5748;
}
.stage.brief-view #continue:focus-visible {
  outline: 3px solid #9c7542;
  outline-offset: 4px;
}
@media (max-width: 640px) {
  .stage.brief-view {
    min-height: 560px;
    padding: 19px 0;
  }
  .stage.brief-view #panel {
    width: calc(100% - 22px);
    min-height: 520px;
    padding: 43px 33px 37px;
  }
  .stage.brief-view #letter-date {
    font-size: 15px;
    margin-bottom: 22px;
  }
  .stage.brief-view #title {
    font-size: 27px;
    margin-bottom: 23px;
  }
  .stage.brief-view #story p {
    font-size: 17px;
    line-height: 1.6;
  }
  .stage.brief-view #letter-signoff {
    font-size: 15px;
    margin: 19px 0 26px;
  }
}
@media (max-width: 360px) {
  .stage.brief-view #panel {
    padding-right: 27px;
    padding-left: 27px;
  }
  .stage.brief-view #title {
    font-size: 25px;
  }
  .stage.brief-view #continue {
    min-width: 185px;
    font-size: 15px;
  }
}

/* The repair scene plays first; only then does the completion card cover it. */
#completion-overlay {
  position: absolute;
  inset: 0;
  z-index: 4;
  display: flex;
  overflow: auto;
  padding: 18px;
  background: #241c295c;
  overscroll-behavior: contain;
}
#completion-overlay > section {
  flex-shrink: 0;
}
@keyframes completion-reveal {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}
#completion-overlay:not([hidden]) {
  animation: completion-reveal 0.22s ease-out;
}
#completion-note,
#final-message {
  position: relative;
  width: min(100%, 680px);
  margin: auto;
  padding: 14px 20px 18px;
  border: 22px solid transparent;
  border-image: url("parchment-letter-v2.webp") 130 fill / 22px / 0 stretch;
  color: #513b2e;
  font-family: Georgia, "Times New Roman", serif;
}
.completion-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  margin: 1px 0 20px;
}
.completion-stamp {
  flex: none;
  display: inline-block;
  margin: 0;
  padding: 7px 13px;
  border: 3px double #62765b;
  border-radius: 2px;
  color: #52694b;
  font:
    700 17px/1.1 Georgia,
    serif;
  letter-spacing: 0.055em;
  transform: rotate(-4deg);
  opacity: 0.88;
}
#completion-title {
  font:
    400 22px/1.45 Georgia,
    serif;
  letter-spacing: 0;
  color: #513b2e;
  margin: 0 0 22px;
  max-width: 47ch;
}
.paper-button {
  min-height: 44px;
  padding: 10px 18px;
  border: 1px solid #536a53;
  border-radius: 4px;
  background: #f7edcf;
  color: #3e5845;
  font:
    600 16px/1.3 Georgia,
    serif;
  box-shadow: 0 2px #bbab8659;
}
.paper-button:hover {
  background: #e9dfbf;
}
.paper-button:focus-visible {
  outline: 3px solid #8b6d42;
  outline-offset: 4px;
}
#final-message {
  max-width: 680px;
  text-align: center;
  padding: 20px 24px 24px;
}
#final-message .holiday-stars {
  font:
    20px/1.1 Georgia,
    serif;
  letter-spacing: 0.25em;
  color: #b18b44;
  margin: 0 0 15px;
}
#final-message h2 {
  font:
    italic 400 clamp(31px, 6vw, 42px)/1.15 Georgia,
    serif;
  letter-spacing: -0.025em;
  color: #47634d;
  margin: 0 0 21px;
}
#final-message .holiday-message {
  font:
    20px/1.5 Georgia,
    serif;
  color: #513b2e;
  margin: 0 0 18px;
}
#final-message .holiday-wishes {
  font:
    17px/1.6 Georgia,
    serif;
  color: #70543d;
  margin: 0 0 13px;
}
#final-message .holiday-signature {
  font:
    italic 17px/1.5 Georgia,
    serif;
  color: #6d4a3c;
  margin: 0 0 25px;
}
@keyframes repair-stamp {
  from {
    opacity: 0;
    transform: rotate(-8deg) scale(1.12);
  }
  to {
    opacity: 0.88;
    transform: rotate(-4deg) scale(1);
  }
}
#completion-note:not([hidden]) .completion-stamp {
  animation: repair-stamp 0.28s ease-out both;
}
@media (max-width: 640px) {
  #completion-note,
  #final-message {
    margin: auto;
    border-width: 16px;
    border-image-width: 16px;
    padding: 13px 13px 17px;
  }
  .completion-heading {
    gap: 13px;
    align-items: flex-start;
    flex-direction: column;
    margin-bottom: 19px;
  }
  #completion-title {
    font-size: 20px;
    margin-bottom: 20px;
  }
  #final-message {
    padding: 20px 15px 23px;
  }
  #final-message .holiday-message {
    font-size: 19px;
  }
  #final-message .holiday-wishes {
    font-size: 16px;
  }
}
@media (prefers-reduced-motion: reduce) {
  #completion-overlay:not([hidden]),
  #completion-note:not([hidden]) .completion-stamp {
    animation: none;
  }
}

@media (max-width: 640px) {
  #completion-overlay {
    padding: 12px;
  }
  #completion-title {
    font-size: 19px;
  }
  #final-message .holiday-stars {
    margin-bottom: 10px;
  }
  #final-message h2 {
    margin-bottom: 15px;
  }
  #final-message .holiday-message {
    font-size: 18px;
    margin-bottom: 13px;
  }
  #final-message .holiday-wishes {
    font-size: 15px;
    margin-bottom: 10px;
  }
  #final-message .holiday-signature {
    margin-bottom: 18px;
  }
}

/* Larger thumb targets on phones and touch screens. */
@media (any-pointer: coarse), (max-width: 640px) {
  #controls {
    gap: 12px;
  }
  #controls div {
    gap: 10px;
  }
  #controls button {
    min-width: 64px;
    min-height: 70px;
    padding: 10px 12px;
  }
  #controls button[data-key="left"],
  #controls button[data-key="right"] {
    font-size: 29px;
  }
  #controls button[data-key="jump"] {
    min-width: 112px;
    font-size: 18px;
  }
}

/* The report and the short play instructions are separate pages of the same letter. */
#level-instructions {
  margin: 0 0 28px;
}
#level-instructions p {
  font:
    400 18px/1.5 Georgia,
    "Times New Roman",
    serif;
  color: #51392e;
  margin: 0;
}
#level-instructions .instruction-touch {
  display: none;
}
#level-rule {
  border-top: 1px solid #b99c6b70;
  padding-top: 20px;
  margin-top: 22px !important;
}
.stage.brief-view #brief-back {
  align-self: center;
  min-height: 44px;
  margin: 10px 0 0;
  padding: 8px 10px;
  background: none;
  border: 0;
  border-radius: 0;
  box-shadow: none;
  color: #806147;
  font:
    400 16px/1.4 Georgia,
    serif;
  text-decoration: underline;
  text-underline-offset: 3px;
}
#brief-back:focus-visible {
  outline: 3px solid #9c7542;
  outline-offset: 2px;
}
#title:focus {
  outline: none;
}
@media (any-pointer: coarse), (max-width: 640px) {
  #level-instructions .instruction-keyboard {
    display: none;
  }
  #level-instructions .instruction-touch {
    display: block;
    font-size: 16px;
  }
}

/* Reserve the return-link row on both sheets so the primary action keeps its place. */
.stage.brief-view #brief-back[hidden] {
  display: block !important;
  visibility: hidden;
  pointer-events: none;
}

/* Touch targets stay controls, even during long presses on iOS. */
.stage, .stage *, #controls, #controls *, button, button * {
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}
@media (any-pointer: coarse), (max-width: 640px) {
  #controls button[data-key="left"], #controls button[data-key="right"] {
    min-width: 76px;
    min-height: 78px;
    font-size: 32px;
  }
}
@media (hover: none) {
  .paper-button:hover { background: #f7edcf; }
}

/* A held touch must never select the page title or surrounding page text. */
html, body, main, header, h1, .brand {
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}
