:root {
  --ink: #111111;
  --paper: #ffffff;
  --gold: #b81432;
  --castle-dark: #111111;
  --castle-mid: #343434;
  --castle-light: #6a6a6a;
}

* { box-sizing: border-box; }

html, body {
  width: 100%;
  height: auto;
  min-height: 0;
  margin: 0;
  overflow: hidden;
  background: transparent;
  color: var(--ink);
  border: 0 !important;
  outline: 0 !important;
  box-shadow: none !important;
}

body {
  font-family: "Pixelify Sans", "Courier New", monospace;
  transition: opacity .18s ease;
}

.world-shell,
.world-shell * {
  -webkit-user-select: none !important;
  user-select: none !important;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

body.leaving { opacity: .15; }
button { color: inherit; font: inherit; }

main,
.world-shell {
  width: 100%;
  height: auto;
  min-height: 0;
  border: 0 !important;
  outline: 0 !important;
  box-shadow: none !important;
}

.world-shell {
  --controls-height: 104px;
  position: relative;
  width: min(100%, calc((100dvh - var(--controls-height)) * 1.6));
  margin-inline: auto;
  padding: 0;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  justify-content: flex-start;
}

#stage-wrap {
  width: 100%;
  height: auto;
  aspect-ratio: 8 / 5;
  position: relative;
  overflow: hidden;
  background: #fff;
  border: 0;
  outline: 0;
  box-shadow: none;
}

canvas {
  position: absolute;
  top: 50%;
  left: 50%;
  display: block;
  width: 100%;
  height: auto;
  transform: translate(-50%, -50%);
  border: 0;
  outline: none;
  box-shadow: none;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

canvas:focus,
canvas:focus-visible,
canvas:-moz-focusring {
  border: 0 !important;
  outline: 0 !important;
  box-shadow: none !important;
}

#prompt,
#toast { display: none !important; }

.touch-controls {
  position: relative;
  width: 100%;
  height: 104px;
  display: grid;
  grid-template-columns: 90px minmax(0, 1fr) 96px;
  gap: clamp(12px, 3vw, 34px);
  align-items: center;
  padding: 6px clamp(18px, 5vw, 54px);
  border: 0;
  background-color: var(--castle-mid);
  background-image:
    linear-gradient(90deg, rgba(255,255,255,.09) 25%, transparent 25% 50%, rgba(0,0,0,.10) 50% 75%, transparent 75%),
    linear-gradient(rgba(0,0,0,.11) 3px, transparent 3px);
  background-size: 28px 24px, 100% 12px;
  box-shadow: inset 0 4px 0 var(--castle-light);
  pointer-events: none;
}

.d-pad { width: 90px; height: 90px; position: relative; }
.d-pad::after {
  content: "";
  position: absolute;
  left: 30px;
  top: 30px;
  width: 30px;
  height: 30px;
  border: 3px solid var(--castle-dark);
  background: #3b3b3b;
}

.touch-controls button {
  width: 30px;
  height: 30px;
  padding: 0;
  border: 3px solid var(--castle-dark);
  border-radius: 0;
  color: #fff;
  background: #696969;
  box-shadow: 3px 3px 0 var(--castle-dark), inset 2px 2px 0 var(--castle-light);
  font: 700 14px/1 "Pixelify Sans", "Courier New", monospace;
  text-shadow: 1px 1px var(--castle-dark);
  touch-action: none;
  -webkit-appearance: none;
  appearance: none;
  pointer-events: auto;
}

.touch-controls button:active,
.touch-controls button.is-pressed {
  color: #fff;
  background: var(--castle-dark);
  box-shadow: 1px 1px 0 var(--castle-dark);
  transform: translate(2px, 2px);
}

.d-pad button { position: absolute; }
.d-pad button::before {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 0;
  height: 0;
  transform: translate(-50%, -50%);
  border-style: solid;
}
.d-pad .up::before { border-width: 0 5px 7px; border-color: transparent transparent #fff; }
.d-pad .down::before { border-width: 7px 5px 0; border-color: #fff transparent transparent; }
.d-pad .left::before { border-width: 5px 7px 5px 0; border-color: transparent #fff transparent transparent; }
.d-pad .right::before { border-width: 5px 0 5px 7px; border-color: transparent transparent transparent #fff; }
.d-pad .up { left: 30px; top: 0; }
.d-pad .left { left: 0; top: 30px; }
.d-pad .down { left: 30px; top: 60px; }
.d-pad .right { left: 60px; top: 30px; }

.control-copy {
  margin: 0;
  color: #c7c7c7;
  font: 500 clamp(17px, 1.7vw, 23px)/1.08 "Pixelify Sans", "Courier New", monospace;
  letter-spacing: .025em;
  text-align: center;
  text-shadow: 1px 1px 0 var(--castle-dark);
}

.touch-copy { display: none; }

.touch-controls .interact {
  width: 96px;
  height: 54px;
  border: 4px solid var(--castle-dark);
  background: #696969;
  box-shadow: 5px 5px 0 var(--castle-dark), inset 3px 3px 0 var(--castle-light);
  color: #fff;
  opacity: 1;
  font-size: 16px;
  letter-spacing: .045em;
}
.touch-controls .interact:disabled { opacity: .28; pointer-events: none; }

.touch-controls button:focus { outline: 0; }
.touch-controls button:focus-visible {
  outline: 2px solid #d6d6d6;
  outline-offset: 3px;
}

@media (pointer: coarse), (max-width: 760px) {
  .world-shell { --controls-height: 118px; }

  #stage-wrap {
    width: 100%;
    height: auto;
  }

  .touch-controls {
    height: 118px;
    grid-template-columns: 100px minmax(0, 1fr) 72px;
    gap: clamp(8px, 4vw, 30px);
    padding: 9px clamp(14px, 6vw, 46px);
  }

  .d-pad { width: 100px; height: 100px; }
  .d-pad::after {
    left: 34px;
    top: 34px;
    width: 32px;
    height: 32px;
  }
  .touch-controls button { width: 32px; height: 32px; }
  .d-pad .up { left: 34px; top: 0; }
  .d-pad .left { left: 0; top: 34px; }
  .d-pad .down { left: 34px; top: 68px; }
  .d-pad .right { left: 68px; top: 34px; }
  .desktop-copy { display: none; }
  .touch-copy { display: inline; }
  .control-copy { font-size: clamp(14px, 3.5vw, 18px); line-height: 1.08; }
  .touch-controls .interact {
    width: 72px;
    height: 54px;
    font-size: 14px;
  }
}

@media (max-width: 760px) {
  .world-shell {
    width: min(100%, calc(100dvh - var(--controls-height)));
  }

  #stage-wrap {
    width: 100%;
    height: auto;
    aspect-ratio: 1;
  }

  .touch-controls { width: 100%; }
  canvas { width: 160%; }
}

@media (max-width: 420px) {
  .touch-controls {
    grid-template-columns: 96px minmax(0, 1fr) 64px;
    gap: 7px;
    padding-inline: 9px;
  }
  .control-copy { font-size: clamp(12px, 3.7vw, 15px); letter-spacing: 0; }
  .touch-controls .interact { width: 64px; font-size: 12px; }
}
