:root {
  color-scheme: dark;
  --ink: #071018;
  --ink-soft: #0c1821;
  --panel: rgba(10, 18, 24, 0.94);
  --panel-raised: rgba(18, 30, 38, 0.96);
  --stone: #747c78;
  --stone-dark: #202b30;
  --stone-edge: #47545a;
  --text: #e8e1d3;
  --muted: #8e9da2;
  --cyan: #55e6d3;
  --cyan-soft: #9df7e8;
  --brass: #d6a85f;
  --danger: #d76d61;
  --shadow: rgba(0, 0, 0, 0.58);
  --display-font: "Palatino Linotype", "Book Antiqua", Palatino, Georgia, serif;
  --ui-font: "Avenir Next", Avenir, Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

html {
  min-width: 320px;
  min-height: 100%;
  background: var(--ink);
}

body {
  min-height: 100vh;
  margin: 0;
  overflow-x: hidden;
  background-color: var(--ink);
  background-image: url("./assets/moonlit-vault-background.webp");
  background-position: center;
  background-size: cover;
  background-attachment: fixed;
  color: var(--text);
  font-family: var(--ui-font);
}

button {
  font: inherit;
}

button:focus-visible,
[tabindex="0"]:focus-visible {
  outline: 2px solid var(--cyan-soft);
  outline-offset: 4px;
}

.game-shell {
  position: relative;
  z-index: 1;
  display: grid;
  grid-template-columns: minmax(170px, 0.72fr) minmax(510px, 2.65fr) minmax(230px, 0.94fr);
  align-items: center;
  gap: clamp(16px, 2.2vw, 34px);
  width: min(1520px, 100%);
  min-height: 100vh;
  margin: 0 auto;
  padding: clamp(18px, 2vw, 34px);
}

.left-rail {
  align-self: stretch;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  min-width: 0;
  padding-block: 4vh 5vh;
}

.brand-plaque {
  position: relative;
  display: grid;
  justify-items: center;
  width: 100%;
  padding: 48px 12px 15px;
  border: 2px solid #344149;
  background: linear-gradient(180deg, rgba(17, 26, 33, 0.98), rgba(8, 14, 19, 0.98));
  box-shadow:
    inset 0 0 0 4px #0a1116,
    inset 0 0 0 5px #4e5a5e,
    0 16px 42px var(--shadow);
  clip-path: polygon(10% 8%, 36% 8%, 42% 0, 58% 0, 64% 8%, 90% 8%, 100% 24%, 95% 88%, 50% 100%, 5% 88%, 0 24%);
}

.brand-plaque svg {
  position: absolute;
  top: 8px;
  width: 36px;
  fill: var(--brass);
  filter: drop-shadow(0 1px 0 #3a2a16);
}

.brand-plaque h1 {
  margin: 0;
  color: #e6d7b9;
  font-family: var(--display-font);
  font-size: clamp(1.18rem, 1.9vw, 1.8rem);
  font-weight: 600;
  letter-spacing: 0.09em;
  line-height: 1;
  text-align: center;
  text-shadow: 0 2px 0 #11181d;
  white-space: nowrap;
}

.keyboard-hint {
  display: flex;
  align-items: center;
  gap: 8px;
  color: rgba(213, 224, 225, 0.52);
  font-size: 0.62rem;
  font-weight: 700;
  letter-spacing: 0.16em;
}

.keyboard-hint i {
  width: 3px;
  height: 3px;
  border-radius: 50%;
  background: var(--brass);
}

.d-pad {
  display: grid;
  grid-template-columns: repeat(3, clamp(48px, 4.2vw, 62px));
  grid-template-rows: repeat(3, clamp(48px, 4.2vw, 62px));
  gap: 6px;
}

.d-pad-button {
  display: grid;
  place-items: center;
  border: 1px solid #647179;
  border-radius: 9px;
  background: linear-gradient(160deg, #3d4a50, #18242c 62%);
  color: #e5c68e;
  box-shadow:
    inset 0 0 0 3px #111a20,
    inset 0 0 0 4px rgba(151, 164, 166, 0.35),
    0 8px 14px rgba(0, 0, 0, 0.34);
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 120ms ease, border-color 120ms ease, color 120ms ease;
}

.d-pad-button:hover {
  border-color: var(--brass);
  color: #f2d59e;
  transform: translateY(-1px);
}

.d-pad-button:active {
  transform: translateY(1px) scale(0.97);
}

.d-pad-button svg {
  width: 42%;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2.6;
}

.d-pad-button.up {
  grid-column: 2;
  grid-row: 1;
}

.d-pad-button.left {
  grid-column: 1;
  grid-row: 2;
}

.d-pad-button.right {
  grid-column: 3;
  grid-row: 2;
}

.d-pad-button.down {
  grid-column: 2;
  grid-row: 3;
}

.board-column {
  display: grid;
  min-width: 0;
  gap: 12px;
}

.board-frame {
  position: relative;
  width: 100%;
  padding: clamp(15px, 1.7vw, 24px);
  border: 2px solid #647078;
  background: #182229;
  box-shadow:
    inset 0 0 0 7px #091117,
    inset 0 0 0 9px #3d494e,
    0 32px 80px rgba(0, 0, 0, 0.64);
}

.board-frame::before,
.board-frame::after {
  position: absolute;
  z-index: -1;
  content: "";
  background: #2d393e;
  box-shadow: inset 0 0 0 4px #0b1217, inset 0 0 0 5px #5b6668;
}

.board-frame::before {
  inset: 22% -9px;
}

.board-frame::after {
  inset: -9px 18%;
}

.corner-rune {
  position: absolute;
  width: 23px;
  aspect-ratio: 1;
  border: 2px solid #8b9698;
  background: #263238;
  box-shadow: inset 0 0 0 4px #111a1f;
  transform: rotate(45deg);
}

.corner-rune.top-left { top: 4px; left: 4px; }
.corner-rune.top-right { top: 4px; right: 4px; }
.corner-rune.bottom-left { bottom: 4px; left: 4px; }
.corner-rune.bottom-right { right: 4px; bottom: 4px; }

.board {
  --columns: 10;
  --rows: 8;
  position: relative;
  display: grid;
  grid-template-columns: repeat(var(--columns), minmax(0, 1fr));
  grid-template-rows: repeat(var(--rows), minmax(0, 1fr));
  width: 100%;
  aspect-ratio: calc(var(--columns) / var(--rows));
  overflow: hidden;
  border: 1px solid #0c151a;
  background: #0a1217;
  box-shadow: inset 0 0 26px rgba(0, 0, 0, 0.7);
  touch-action: none;
}

.tile {
  position: relative;
  min-width: 0;
  min-height: 0;
}

.tile.floor {
  background-image: url("./assets/stone-floor.webp");
  background-position: calc(var(--tile-x) * -41px) calc(var(--tile-y) * -37px);
  background-size: 280%;
  box-shadow: inset 0 0 0 1px rgba(12, 23, 27, 0.42);
}

.tile.floor::after {
  position: absolute;
  inset: 0;
  z-index: 0;
  content: "";
  box-shadow: inset 0 0 13px rgba(1, 8, 12, 0.2);
  pointer-events: none;
}

.tile.wall {
  z-index: 4;
  background-color: #202b30;
  background-image: url("./assets/stone-floor.webp");
  background-position: calc(var(--tile-x) * -59px) calc(var(--tile-y) * -47px);
  background-size: 190%;
  box-shadow:
    inset 0 0 0 2px rgba(12, 17, 19, 0.72),
    inset 0 -8px 12px rgba(3, 7, 9, 0.48),
    0 5px 10px rgba(0, 0, 0, 0.45);
  filter: brightness(0.58) saturate(0.75);
}

.game-sprite {
  position: absolute;
  inset: 50% auto auto 50%;
  z-index: 2;
  width: 112%;
  height: 112%;
  object-fit: contain;
  pointer-events: none;
  user-select: none;
  transform: translate(-50%, -50%);
  filter: drop-shadow(0 4px 4px rgba(0, 0, 0, 0.45));
}

.game-sprite.sigil {
  z-index: 1;
  width: 88%;
  height: 88%;
  filter: drop-shadow(0 0 8px rgba(85, 230, 211, 0.7));
  animation: sigil-breathe 2.2s ease-in-out infinite;
}

.game-sprite.trap {
  z-index: 1;
  width: 94%;
  height: 94%;
  filter: drop-shadow(0 2px 2px rgba(0, 0, 0, 0.55));
}

.game-sprite.key {
  width: 74%;
  height: 74%;
  filter: drop-shadow(0 0 7px rgba(214, 168, 95, 0.48));
  animation: key-hover 1.8s ease-in-out infinite;
}

.game-sprite.block {
  z-index: 3;
  width: 104%;
  height: 104%;
  transition: filter 160ms ease;
}

.game-sprite.block-on-sigil {
  filter:
    drop-shadow(0 0 10px rgba(85, 230, 211, 0.8))
    drop-shadow(0 5px 3px rgba(0, 0, 0, 0.5));
}

.game-sprite.player {
  z-index: 5;
  width: 94%;
  height: 94%;
  transition: transform 100ms ease;
}

.game-sprite.player.facing-up { transform: translate(-50%, -50%) rotate(180deg); }
.game-sprite.player.facing-left { transform: translate(-50%, -50%) rotate(90deg); }
.game-sprite.player.facing-right { transform: translate(-50%, -50%) rotate(-90deg); }

.door-tile {
  overflow: visible;
}

.game-sprite.door-sprite,
.game-sprite.openDoor,
.game-sprite.closedDoor {
  z-index: 4;
  width: 132%;
  height: 132%;
}

.door-open .game-sprite {
  filter: drop-shadow(0 0 12px rgba(85, 230, 211, 0.62));
}

.recall-button {
  position: absolute;
  inset: 0;
  z-index: 2;
  width: 100%;
  padding: 0;
  border: 0;
  background: transparent;
  cursor: pointer;
}

.recall-button .game-sprite {
  width: 90%;
  height: 90%;
}

.board.bump {
  animation: board-bump 260ms ease;
}

.board.trap-triggered {
  animation: trap-flash 430ms ease;
}

.board.room-complete {
  animation: room-awake 650ms ease;
}

.board-status {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 14px;
  min-height: 24px;
  padding-inline: 4px;
  color: var(--muted);
  font-size: clamp(0.62rem, 0.78vw, 0.74rem);
  font-weight: 700;
  letter-spacing: 0.13em;
}

.board-status span:nth-child(2) {
  color: #c9c2b2;
  font-family: var(--display-font);
  letter-spacing: 0.16em;
  text-align: center;
}

.board-status span:last-child {
  text-align: right;
}

.door-status.is-open {
  color: var(--cyan-soft);
  text-shadow: 0 0 10px rgba(85, 230, 211, 0.55);
}

.status-panel {
  align-self: center;
  overflow: hidden;
  border: 2px solid #5b686d;
  background: var(--panel);
  box-shadow:
    inset 0 0 0 5px #080e12,
    inset 0 0 0 6px #334148,
    0 24px 54px var(--shadow);
}

.status-panel > section,
.action-buttons {
  margin: 7px;
  border: 1px solid #344148;
  background: rgba(17, 27, 34, 0.78);
  box-shadow: inset 0 0 0 3px rgba(4, 8, 11, 0.55);
}

.room-progress {
  padding: clamp(18px, 2.1vw, 28px) 14px 20px;
}

.room-label {
  margin: 0 0 18px;
  font-family: var(--display-font);
  font-size: clamp(1.25rem, 1.9vw, 1.75rem);
  letter-spacing: 0.04em;
  text-align: center;
}

.room-progress ol {
  position: relative;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  align-items: center;
  margin: 0;
  padding: 0;
  list-style: none;
}

.room-progress ol::before {
  position: absolute;
  right: 8%;
  left: 8%;
  height: 2px;
  background: #705c3b;
  content: "";
}

.room-progress li {
  position: relative;
  z-index: 1;
  display: grid;
  place-items: center;
  width: 30px;
  aspect-ratio: 1;
  margin: auto;
  border: 1px solid #756a58;
  border-radius: 50%;
  background: #0e171d;
  color: #a49c8e;
  font-family: var(--display-font);
  font-size: 0.82rem;
  font-weight: 700;
  box-shadow: 0 0 0 3px #091015;
}

.room-progress li.current,
.room-progress li.complete {
  border-color: var(--brass);
  color: #efe2c5;
}

.room-progress li.current {
  background: #123d4a;
  box-shadow: 0 0 0 3px #091015, 0 0 14px rgba(85, 230, 211, 0.35);
}

.room-progress li.complete {
  background: #2b3b36;
}

.objective-panel {
  display: grid;
  grid-template-columns: 66px 1fr;
  align-items: center;
  min-height: 108px;
  padding: 14px 14px 14px 8px;
}

.objective-panel img {
  width: 72px;
  height: 72px;
  object-fit: contain;
  filter: drop-shadow(0 0 8px rgba(85, 230, 211, 0.5));
}

.objective-panel p {
  margin: 0;
  font-family: var(--display-font);
  font-size: clamp(1.04rem, 1.55vw, 1.4rem);
  line-height: 1.22;
  text-align: center;
}

.moves-panel {
  display: grid;
  justify-items: center;
  padding: 18px 10px 14px;
}

.moves-panel span {
  color: #c9bfac;
  font-family: var(--display-font);
  font-size: 0.95rem;
  letter-spacing: 0.12em;
}

.moves-panel strong {
  color: #95dfdd;
  font-family: var(--display-font);
  font-size: clamp(2.75rem, 5.2vw, 4.4rem);
  font-weight: 400;
  letter-spacing: 0.05em;
  line-height: 1;
  text-shadow: 0 2px 0 #071014, 0 0 16px rgba(85, 230, 211, 0.2);
}

.inventory {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 52px;
  padding: 6px 10px;
  color: #d7c9ac;
  font-size: 0.68rem;
  font-weight: 800;
  letter-spacing: 0.11em;
}

.inventory img {
  width: 40px;
  height: 40px;
  object-fit: contain;
  filter: drop-shadow(0 0 5px rgba(214, 168, 95, 0.4));
}

.inventory img.not-required {
  filter: grayscale(1);
  opacity: 0.38;
}

.action-buttons {
  display: grid;
  gap: 9px;
  padding: 10px;
  background: rgba(6, 12, 16, 0.82);
}

.action-button {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  min-height: 54px;
  padding: 10px 16px;
  border: 1px solid #74848a;
  border-radius: 2px;
  background: linear-gradient(160deg, #294656, #152a36 70%);
  color: #efe9dc;
  font-family: var(--display-font);
  font-size: 1.04rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  box-shadow:
    inset 0 0 0 3px #0c151b,
    inset 0 0 0 4px #806e4d,
    0 6px 13px rgba(0, 0, 0, 0.36);
  cursor: pointer;
  transition: filter 150ms ease, transform 150ms ease;
}

.action-button:hover:not(:disabled) {
  filter: brightness(1.16);
  transform: translateY(-1px);
}

.action-button:active:not(:disabled) {
  transform: translateY(1px);
}

.action-button:disabled {
  cursor: not-allowed;
  filter: grayscale(1);
  opacity: 0.42;
}

.action-button svg {
  width: 25px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2;
}

.completion-modal[hidden] {
  display: none;
}

.completion-modal {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(2, 8, 12, 0.82);
  backdrop-filter: blur(12px);
}

.completion-card {
  display: grid;
  justify-items: center;
  width: min(460px, 100%);
  padding: 38px;
  border: 2px solid #6b787c;
  background: #101d25;
  box-shadow: inset 0 0 0 6px #071015, inset 0 0 0 7px #3f4d51, 0 34px 90px #000;
  text-align: center;
}

.completion-sigil img {
  width: 124px;
  height: 124px;
  object-fit: contain;
  filter: drop-shadow(0 0 18px rgba(85, 230, 211, 0.68));
  animation: sigil-breathe 2s ease-in-out infinite;
}

.completion-card h2 {
  margin: 10px 0 8px;
  color: #eadbbd;
  font-family: var(--display-font);
  font-size: clamp(1.6rem, 5vw, 2.4rem);
  letter-spacing: 0.08em;
}

.completion-card p {
  margin: 0 0 26px;
  color: var(--muted);
}

.completion-card .action-button {
  min-width: 210px;
}

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

@keyframes sigil-breathe {
  0%, 100% { opacity: 0.82; transform: translate(-50%, -50%) scale(0.95); }
  50% { opacity: 1; transform: translate(-50%, -50%) scale(1.03); }
}

@keyframes key-hover {
  0%, 100% { transform: translate(-50%, -52%); }
  50% { transform: translate(-50%, -60%); }
}

@keyframes board-bump {
  0%, 100% { transform: translateX(0); }
  35% { transform: translateX(-4px); }
  70% { transform: translateX(3px); }
}

@keyframes trap-flash {
  0%, 100% { filter: none; }
  45% { filter: sepia(0.7) saturate(2.2) hue-rotate(318deg) brightness(1.2); }
}

@keyframes room-awake {
  0%, 100% { filter: none; }
  45% { filter: brightness(1.24) drop-shadow(0 0 18px rgba(85, 230, 211, 0.55)); }
}

@media (max-width: 1100px) {
  .game-shell {
    grid-template-columns: minmax(0, 1fr) 250px;
    align-content: start;
    min-height: 100vh;
    padding-top: 20px;
  }

  .left-rail {
    grid-column: 1 / -1;
    flex-direction: row;
    min-height: 94px;
    padding: 0;
  }

  .brand-plaque {
    width: min(260px, 32vw);
    padding-top: 42px;
  }

  .keyboard-hint {
    display: none;
  }

  .d-pad {
    grid-template-columns: repeat(3, 44px);
    grid-template-rows: repeat(3, 32px);
    gap: 4px;
  }

  .d-pad-button {
    border-radius: 6px;
  }

  .status-panel {
    align-self: stretch;
  }
}

@media (max-width: 720px) {
  body {
    background-position: 34% center;
  }

  .game-shell {
    grid-template-columns: 1fr;
    gap: 14px;
    padding: 14px;
  }

  .left-rail,
  .board-column,
  .status-panel {
    grid-column: 1;
  }

  .left-rail {
    align-items: center;
  }

  .brand-plaque {
    width: min(230px, 56vw);
    padding: 39px 10px 12px;
  }

  .brand-plaque h1 {
    font-size: 1.15rem;
  }

  .d-pad {
    grid-template-columns: repeat(3, 36px);
    grid-template-rows: repeat(3, 27px);
  }

  .board-frame {
    padding: 13px;
  }

  .corner-rune {
    width: 18px;
  }

  .board-status {
    grid-template-columns: 1fr 1fr;
  }

  .board-status span:nth-child(2) {
    display: none;
  }

  .status-panel {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }

  .room-progress,
  .action-buttons {
    grid-column: 1 / -1;
  }

  .room-progress {
    padding: 14px 12px;
  }

  .room-label {
    margin-bottom: 12px;
    font-size: 1.2rem;
  }

  .objective-panel {
    grid-template-columns: 48px 1fr;
    min-height: 80px;
  }

  .objective-panel img {
    width: 52px;
    height: 52px;
  }

  .objective-panel p {
    font-size: 0.94rem;
  }

  .moves-panel {
    padding: 12px 8px;
  }

  .moves-panel strong {
    font-size: 2.6rem;
  }

  .inventory {
    grid-column: 1 / -1;
  }

  .action-buttons {
    grid-template-columns: 1fr 1fr;
  }
}

@media (max-width: 430px) {
  .left-rail {
    justify-content: space-between;
    gap: 8px;
  }

  .brand-plaque {
    width: min(205px, 53vw);
  }

  .d-pad {
    position: static;
    flex: 0 0 auto;
    grid-template-columns: repeat(3, 42px);
    grid-template-rows: repeat(3, 42px);
    padding: 0;
    border: 0;
    background: transparent;
    box-shadow: none;
    backdrop-filter: none;
  }

  .board-status {
    font-size: 0.56rem;
  }

  .room-progress li {
    width: 27px;
  }

  .action-button {
    min-height: 50px;
    padding-inline: 8px;
    font-size: 0.88rem;
  }
}

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