:root {
  color-scheme: light;
  --page: #f6f6f6;
  --paper: #fff;
  --floor: #fdfdfd;
  --ink: #181818;
  --soft-ink: #666;
  --rule: #d0d0d0;
  --tile-rule: #dedede;
  --stage-heading: 3.1rem;
  --move-size: 2.65rem;
  --mono: ui-monospace, "SFMono-Regular", Consolas, monospace;
  font-family: system-ui, "PingFang SC", "Microsoft YaHei", sans-serif;
  color: var(--ink);
  background: var(--page);
}

*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; }
button { font: inherit; color: inherit; }
button:enabled { cursor: pointer; }
button:disabled { cursor: default; }
button, .board { touch-action: manipulation; }
button:focus-visible, .board:focus-visible { outline: 2px solid var(--ink); outline-offset: 3px; }
svg { display: block; width: 100%; height: 100%; }
[hidden] { display: none !important; }

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

.game-shell {
  width: min(calc(100% - clamp(2rem, 9vw, 8rem)), 68.5rem);
  margin-inline: auto;
  padding: clamp(1.5rem, 5vh, 3.1rem) 0 clamp(1rem, 2.5vh, 2rem);
}

.masthead {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1.5rem;
  padding-bottom: clamp(1.1rem, 3vh, 1.6rem);
  border-bottom: 1px solid var(--ink);
}

.wordmark, .masthead-tools, .level-nav { display: flex; align-items: center; }
.wordmark { gap: clamp(.55rem, 1.5vw, 1rem); }
.brand-mark { display: block; width: clamp(1.75rem, 3.15vw, 2.6rem); aspect-ratio: 1; }
.wordmark h1 {
  margin: 0;
  font: italic 400 clamp(2.65rem, 5.6vw, 4rem)/.94 Baskerville, "Iowan Old Style", Georgia, serif;
  letter-spacing: -.064em;
}

.masthead-tools { gap: 1.05rem; }
.level-nav { gap: .28rem; }
.level-tab, .icon-button {
  position: relative;
  display: grid;
  place-items: center;
  padding: 0;
  border: 1px solid var(--rule);
  border-radius: 0;
  background: var(--paper);
  transition: color 140ms, background-color 140ms, border-color 140ms;
}

.level-tab {
  width: 2.75rem;
  min-height: 2.75rem;
  font: 1.04rem/1 Baskerville, Georgia, serif;
}

.level-tab:nth-child(3) { margin-inline-start: .83rem; }
.level-tab:nth-child(3)::before {
  content: "";
  position: absolute;
  left: -.62rem;
  height: .95rem;
  width: 1px;
  background: var(--rule);
  pointer-events: none;
}

.level-tab[data-finished]::after {
  content: "";
  position: absolute;
  right: .3rem;
  top: .3rem;
  width: .26rem;
  height: .26rem;
  background: currentColor;
}

.level-tab:hover, .icon-button:enabled:hover { background-color: #eaeaea; border-color: #939393; }
.level-tab[aria-current="step"], .level-tab[aria-current="step"]:hover {
  color: var(--paper);
  background: var(--ink);
  border-color: var(--ink);
}

.help-toggle { width: 2.75rem; min-height: 2.75rem; }
.help-toggle span, .utility span, .close-help span { display: block; width: 1.24rem; height: 1.24rem; }

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(12.75rem, 24%);
  gap: clamp(1.8rem, 6vw, 4.65rem);
  max-width: calc((100svh - 14rem) * 9 / 7 + 17.5rem);
  margin-inline: auto;
  padding-top: clamp(1.1rem, 4.9vh, 3rem);
}

.stage-heading {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 1rem;
  min-height: var(--stage-heading);
}

.stage-heading h2 {
  margin: 0;
  font: 650 clamp(1.4rem, 2.55vw, 1.9rem)/1.15 "Songti SC", "Noto Serif CJK SC", Georgia, serif;
  letter-spacing: .025em;
}

.level-kind { font-size: .875rem; color: var(--soft-ink); letter-spacing: .12em; }

.sheet {
  position: relative;
  padding: clamp(.55rem, 1.47vw, 1.2rem);
  background: var(--paper);
  border: 1px solid var(--ink);
}

.board {
  position: relative;
  isolation: isolate;
  overflow: hidden;
  aspect-ratio: var(--columns, 9) / var(--rows, 7);
  border: 1px solid #a0a0a0;
  background: #e9e9e9;
}

.tiles, .scope-layer, .gate-layer, .board-effect { position: absolute; inset: 0; }
.tiles {
  display: grid;
  grid-template-columns: repeat(var(--columns), minmax(0, 1fr));
  grid-template-rows: repeat(var(--rows), minmax(0, 1fr));
}

.tile {
  position: relative;
  display: grid;
  place-items: center;
  min-width: 0;
  min-height: 0;
  padding: 0;
  border: 0;
  border-right: 1px solid var(--tile-rule);
  border-bottom: 1px solid var(--tile-rule);
  border-radius: 0;
}

.wall {
  background-color: #ececec;
  background-image: repeating-linear-gradient(135deg, transparent 0 5px, #e0e0e0 5px 6px);
  background-attachment: fixed;
}

.floor {
  background: var(--floor);
  transition: background-color 160ms;
}

.floor:enabled { cursor: default; }
.floor[data-near]:enabled, .floor[data-interactive]:enabled { cursor: pointer; }
.floor[data-near]:hover { background-color: #f0f0f0; }
.floor[data-tile="."]::before {
  content: "";
  width: 2px;
  height: 2px;
  background: #d3d3d3;
}

.floor[data-interactive]::after {
  content: "";
  position: absolute;
  inset: 11%;
  border: 1px dashed #a1a1a1;
  pointer-events: none;
}

.prop {
  display: block;
  width: 54%;
  height: 54%;
  max-width: 2.6rem;
  pointer-events: none;
  transition: transform 125ms;
}

.floor[data-tile="S"] .prop { color: #878787; }
.floor[data-occupied]:not([data-tile="S"]) .prop { transform: translate(42%, -40%) scale(.72); }
.floor[data-tile="K"][data-occupied] .prop { transition: none; }
.lever-arm { transform: rotate(-28deg); transform-origin: 12px 16px; transition: transform 220ms; }
.board[data-bank="B"] .lever-arm { transform: rotate(28deg); }
.cabinet-key { opacity: 0; }
.floor[data-keys="1"] .cabinet-key.first, .floor[data-keys="2"] .cabinet-key { opacity: 1; }
.floor[data-tile="D"] .prop { position: relative; width: 63%; height: 63%; }
.opening-letter {
  position: absolute;
  top: 57%;
  left: 50%;
  transform: translate(-50%, -50%);
  font: 700 clamp(.71rem, 1.2vw, .96rem)/1 var(--mono);
}

.scope-layer { z-index: 1; pointer-events: none; }
.scope-frame {
  position: absolute;
  border: 1px dashed var(--soft-ink);
  background: rgb(0 0 0 / .024);
}
.scope-mark {
  position: absolute;
  inset-block-start: .16rem;
  inset-inline-end: .16rem;
  padding-inline: .09rem;
  background: var(--floor);
  font: 650 .88rem/1 var(--mono);
}

.gate-layer { z-index: 2; pointer-events: none; }
.gate {
  position: absolute;
  display: grid;
  place-items: center;
  width: calc(100% / var(--columns));
  height: calc(100% / var(--rows));
  transform: translate(-50%, -50%);
}

.gate-shape { width: 35%; height: 85%; overflow: visible; }
.gate[data-orientation="horizontal"] .gate-shape { transform: rotate(90deg); }
.gate-barrier { transform-origin: 50% 50%; transition: opacity 175ms, scale 175ms; }
.gate[data-open] .gate-barrier { opacity: 0; scale: 1 .4; }
.keyhole { fill: var(--floor); transition: fill 120ms; }
.gate[data-ready] .keyhole { fill: #b9b9b9; }
.gate-letter {
  position: absolute;
  left: 62%;
  top: 14%;
  color: var(--ink);
  font: 650 clamp(.75rem, 1vw, .875rem)/1 var(--mono);
  white-space: nowrap;
}

.gate[data-orientation="horizontal"] .gate-letter { left: 70%; top: 63%; }

.actor {
  position: absolute;
  display: grid;
  place-items: center;
  width: calc(100% / var(--columns));
  height: calc(100% / var(--rows));
  transform: translate(-50%, -50%);
  pointer-events: none;
  transition: left 125ms ease-out, top 125ms ease-out;
}

.actor span { width: 64%; height: 64%; max-width: 3.2rem; }
.player { z-index: 5; }
.remnant { z-index: 4; display: none; transition: none; }

.controls {
  align-self: start;
  margin-top: var(--stage-heading);
  border-top: 1px solid var(--ink);
}

.control-section { padding: 1rem 0; border-bottom: 1px solid var(--rule); }
.control-section h3 {
  margin: 0 0 .7rem;
  font-size: .875rem;
  font-weight: 600;
  letter-spacing: .1em;
}

.inventory { display: flex; align-items: center; justify-content: space-between; gap: .5rem; }
.inventory h3 { margin: 0; }
.slots { display: flex; gap: .35rem; }
.slot {
  display: grid;
  place-items: center;
  width: 2.65rem;
  aspect-ratio: 1;
  border: 1px dashed #b7b7b7;
  background: var(--page);
  transition: background-color 160ms, border-color 160ms;
}

.slot svg { width: 58%; height: 58%; opacity: 0; }
.slot[data-filled] { background: var(--paper); border: 1px solid var(--ink); }
.slot[data-filled] svg { opacity: 1; animation: ink-in 200ms ease-out both; }
.reset-keys { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: .35rem; }
.reset-key {
  align-content: center;
  gap: .23rem;
  min-width: 0;
  min-height: 4.55rem;
}

.reset-key span { display: block; width: 1.9rem; height: 1.9rem; }
.reset-key kbd { color: var(--soft-ink); font: .79rem/1.1 var(--mono); }
.icon-button:disabled { background: transparent; color: #959595; border-color: #dedede; }
.reset-key:disabled kbd { color: #9b9b9b; }
.reset-key[data-cue]:enabled { border-color: var(--ink); }
.reset-key[data-cue]:enabled::after {
  content: "";
  position: absolute;
  inset: 3px;
  border: 1px dotted var(--ink);
  pointer-events: none;
  animation: cue 1.8s ease-in-out infinite;
}

.scope-status {
  display: flex;
  align-items: center;
  gap: .38rem;
  margin: .58rem 0 0;
  color: var(--soft-ink);
  font-size: .875rem;
}
.scope-status kbd { padding: .08rem .27rem; border: 1px solid var(--rule); font: .8rem/1.1 var(--mono); }

.opening-settings { margin-top: .75rem; }
.opening-preview { display: grid; grid-template-columns: 1fr 1fr; gap: .35rem; margin: 0; }
.opening-preview div { padding: .38rem .52rem; border: 1px solid var(--rule); }
.opening-preview dt { color: var(--soft-ink); font-size: .79rem; }
.opening-preview dd { margin: .2rem 0 0; font-size: .875rem; font-weight: 600; }
.opening-settings[data-phase="load"] .opening-preview div:last-child { color: var(--paper); background: var(--ink); border-color: var(--ink); }
.opening-settings[data-phase="load"] .opening-preview div:last-child dt { color: #ddd; }
.opening-cue { margin: .5rem 0 0; color: var(--soft-ink); font-size: .82rem; line-height: 1.55; }
.opening-cue > span { display: none; }
.opening-settings[data-phase="edit"] [data-step="edit"],
.opening-settings[data-phase="load"] [data-step="load"],
.opening-settings[data-phase="applied"] [data-step="applied"] { display: inline; }
.setup-icon { display: inline-block; width: 1.2em; height: 1.2em; margin-right: .32rem; vertical-align: -.22em; }
.opening-cue kbd { display: inline-block; padding: 0 .24rem; color: var(--ink); border: 1px solid var(--rule); font: .81rem/1.2 var(--mono); }

.movement-tools { display: flex; justify-content: space-between; align-items: end; gap: .4rem; }
.dpad { display: grid; grid-template-columns: repeat(3, var(--move-size)); grid-template-rows: repeat(3, var(--move-size)); gap: .26rem; }
.dpad .up { grid-area: 1 / 2; }
.dpad .left { grid-area: 2 / 1; }
.dpad .action { grid-area: 2 / 2; }
.dpad .right { grid-area: 2 / 3; }
.dpad .down { grid-area: 3 / 2; }
.direction span { display: block; width: 1.23rem; height: 1.23rem; }
.action kbd { font: .9rem/1 var(--mono); }
.action:enabled, .action:enabled:hover { color: var(--paper); border-color: var(--ink); background: var(--ink); }
.utilities { display: flex; flex-direction: column; gap: .26rem; }
.utility { width: var(--move-size); height: var(--move-size); }

.result {
  position: absolute;
  z-index: 6;
  left: 50%;
  bottom: clamp(1rem, 2.6vw, 2rem);
  display: flex;
  align-items: center;
  gap: .6rem;
  width: max-content;
  max-width: calc(100% - 2rem);
  padding: .46rem .58rem .46rem .85rem;
  color: var(--paper);
  background: var(--ink);
  transform: translateX(-50%);
  animation: result-in 250ms ease-out both;
}

.result span { display: block; width: 1.2rem; height: 1.2rem; }
.result strong { font-size: .91rem; padding-right: .2rem; }
.result button {
  min-height: 2.25rem;
  padding: 0 .68rem;
  border: 1px solid var(--paper);
  border-radius: 0;
  background: var(--paper);
  color: var(--ink);
  font-size: .86rem;
}
.result button:focus-visible { outline-color: var(--paper); }
.result button:hover { background: #e6e6e6; }

dialog {
  width: min(23rem, calc(100vw - 2rem));
  padding: 1.25rem;
  border: 1px solid var(--ink);
  border-radius: 0;
  background: var(--paper);
  color: var(--ink);
}
dialog::backdrop { background: rgb(0 0 0 / .24); }
.help-heading { display: flex; align-items: center; justify-content: space-between; gap: 1rem; border-bottom: 1px solid var(--ink); padding-bottom: .8rem; }
.help-heading h2 { margin: 0; font: 650 1.3rem/1.2 "Songti SC", Georgia, serif; }
.close-help { width: 2.5rem; height: 2.5rem; }
.help-controls { margin: .6rem 0 0; }
.help-controls div { display: flex; align-items: center; justify-content: space-between; gap: .8rem; padding: .38rem 0; }
.help-controls dt { display: flex; align-items: center; gap: .35rem; font-size: .92rem; }
.help-controls dt span { display: block; width: 1.05rem; height: 1.05rem; }
.help-controls dd { margin: 0; }
.help-controls kbd { display: inline-block; padding: .16rem .34rem; border: 1px solid var(--rule); font: .82rem/1.3 var(--mono); }
.level-hint { margin: .65rem 0 0; padding: .7rem 0 0; border-top: 1px solid var(--rule); font-size: .95rem; line-height: 1.5; }
.level-hint summary { cursor: pointer; }
.level-hint p { margin: .45rem 0 0; }

.board-effect { z-index: 4; overflow: hidden; pointer-events: none; }
.board-effect::before, .board-effect::after { content: ""; display: none; position: absolute; }
.board[data-effect="world"] .board-effect::before, .board[data-effect="lever"] .board-effect::before {
  display: block;
  inset: -18% 0 auto;
  height: 22%;
  border-bottom: 1px solid #878787;
  background: linear-gradient(transparent, rgb(255 255 255 / .68));
  animation: scan 430ms cubic-bezier(.33, .15, .37, 1) both;
}
.board[data-effect="lever"] .board-effect::before { opacity: .5; animation-duration: 277ms; }
.board[data-scoped][data-effect="world"] .board-effect::before { display: none; }
.board[data-scoped][data-effect="world"] .floor[data-reset-target] { animation: scope-reprint 400ms ease-out both; }
.board[data-effect="player"] .remnant { display: grid; animation: left-behind 390ms ease-out both; }
.board[data-effect="player"] .player span { animation: arrive 300ms ease-out both; }
.board[data-effect="game"] .board-effect::before, .board[data-effect="enter"] .board-effect::before {
  display: block;
  inset: 0;
  transform-origin: left;
  border-right: 2px solid var(--ink);
  background: var(--paper);
  animation: turn-page 480ms cubic-bezier(.35, .08, .25, 1) both;
}
.board[data-effect="enter"] .board-effect::before { animation-duration: 320ms; }
.board:is([data-effect="player"], [data-effect="game"], [data-effect="undo"], [data-effect="enter"]) .player { transition: none; }
.board[data-effect="blocked"] .player span { animation: nudge 170ms ease-out both; }
.board[data-effect="opening"] .floor[data-tile="D"] .prop { animation: stamp 250ms ease-out both; }

@keyframes scan { from { transform: translateY(0); } to { transform: translateY(545%); } }
@keyframes scope-reprint { from { background-color: #dcdcdc; } to { background-color: var(--floor); } }
@keyframes turn-page { from { transform: translateX(0); } to { transform: translateX(-105%); } }
@keyframes left-behind { from { opacity: .42; } to { opacity: 0; scale: .8; } }
@keyframes arrive { 0% { scale: .58; opacity: .45; } 72% { scale: 1.12; } 100% { scale: 1; opacity: 1; } }
@keyframes nudge { 0%, 100% { translate: 0; } 36% { translate: 2px; } 68% { translate: -2px; } }
@keyframes stamp { 0% { opacity: .35; scale: 1.16; } 100% { opacity: 1; scale: 1; } }
@keyframes ink-in { from { opacity: 0; translate: 0 .2rem; } to { opacity: 1; translate: 0; } }
@keyframes result-in { from { opacity: 0; translate: 0 .4rem; } to { opacity: 1; translate: 0; } }
@keyframes cue { 0%, 100% { opacity: .2; } 50% { opacity: .85; } }

@media (max-width: 53rem) {
  .masthead { position: relative; flex-wrap: wrap; gap: .9rem; }
  .masthead-tools { width: 100%; min-width: 0; }
  .level-nav { width: 100%; min-width: 0; }
  .level-tab { flex: 0 1 2.75rem; min-width: 0; }
  .help-toggle { position: absolute; top: 0; right: 0; }
}

@media (max-width: 46rem) {
  .layout { grid-template-columns: minmax(0, 1fr); max-width: none; gap: 1rem; }
  .controls { display: grid; grid-template-columns: minmax(0, 1fr) auto; column-gap: clamp(.55rem, 3.5vw, 1.3rem); margin-top: 0; }
  .inventory { grid-area: 1 / 1; }
  .resets { grid-area: 2 / 1; }
  .moves { grid-area: 1 / 2 / span 2; }
  .control-section { padding: .7rem 0; }
  .reset-key { min-height: 3.8rem; }
  .board { min-width: 0; }
  .stage-heading { min-height: 2.55rem; }
}

@media (max-width: 34rem) {
  :root { --move-size: clamp(2rem, 9.4vw, 2.6rem); }
  .level-tab, .help-toggle { width: 2.5rem; min-height: 2.5rem; }
  .level-tab { flex-basis: 2.5rem; }
  .reset-key span { width: 1.55rem; height: 1.55rem; }
  .slot { width: 2.2rem; }
  .opening-preview div { padding-inline: .25rem; }
  .gate-letter { left: 65%; }
}

@media (max-width: 22rem) {
  .level-nav { gap: .2rem; }
  .level-tab { font-size: .95rem; }
  .level-tab:nth-child(3) { margin-inline-start: .62rem; }
  .level-tab:nth-child(3)::before { left: -.48rem; }
  .movement-tools { gap: .3rem; }
  .dpad, .utilities { gap: .18rem; }
  .reset-keys { gap: .19rem; }
  .opening-preview { grid-template-columns: 1fr; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}
