:root {
  color-scheme: dark;
  --gold: #f0c94b;
  --ink: #1a120c;
  --wood: #2a2118;
  --wood-hi: #4a3b28;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
}

body {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  padding: 24px 12px 32px;
  background:
    radial-gradient(ellipse at 50% 0%, #2a3148 0%, transparent 55%),
    #12141c;
  color: var(--gold);
  font-family: "Courier New", Courier, monospace;
}

#home {
  position: fixed;
  z-index: 8;
  top: max(10px, env(safe-area-inset-top));
  left: max(10px, env(safe-area-inset-left));
  display: flex;
  align-items: center;
  gap: 6px;
  min-height: 40px;
  padding: 10px;
  text-decoration: none;
  letter-spacing: 0.08em;
  font-size: 13px;
  color: var(--gold);
  background: #241c14;
  border: 2px solid #8a7040;
  border-radius: 999px;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.4);
}

#home svg {
  width: 18px;
  height: 18px;
  fill: currentColor;
  display: block;
}

#home:active { background: #3a2e1c; }

#bezel {
  width: min(96vw, 820px);
  background: linear-gradient(#3a3126, var(--wood) 30%, #16110d);
  border-radius: 22px;
  padding: 18px 18px 14px;
  box-shadow:
    0 24px 50px rgba(0, 0, 0, 0.45),
    inset 0 0 0 3px #6d5a3c,
    inset 0 0 0 6px #241c14;
}

.brand {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 8px 10px;
  letter-spacing: 0.18em;
  font-size: 11px;
  color: #d7c08a;
}

.brand span:last-child {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #d24b4b;
  box-shadow: 0 0 8px #d24b4b;
}

#screen {
  position: relative;
  background: #000;
  border-radius: 6px;
  overflow: hidden;
  box-shadow: inset 0 0 0 2px #0a0a0a, inset 0 0 24px rgba(0, 0, 0, 0.45);
}

#screen::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: repeating-linear-gradient(
    to bottom,
    rgba(0, 0, 0, 0.09) 0 1px,
    transparent 1px 3px
  );
  mix-blend-mode: multiply;
}

canvas {
  display: block;
  width: 100%;
  height: auto;
  background: #000;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  outline: none;
}

.motto {
  margin: 10px 0 0;
  text-align: center;
  letter-spacing: 0.22em;
  font-size: 11px;
  color: #e7d7a4;
}

.site-head {
  width: min(96vw, 820px);
  text-align: center;
}

.site-head h1 {
  margin: 0;
  font-size: 18px;
  font-weight: normal;
  letter-spacing: 0.18em;
  color: var(--gold);
}

.site-head p {
  margin: 6px 0 0;
  font-size: 13px;
  line-height: 1.45;
  color: #d7c08a;
}

#lost {
  position: fixed;
  z-index: 6;
  left: 50%;
  bottom: max(10px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  width: min(94vw, 440px);
  padding: 12px;
  background: #241c14;
  color: var(--gold);
  border: 2px solid #8a7040;
  border-radius: 14px;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.45);
}

#namebox {
  position: absolute;
  left: 22%;
  top: 16%;
  width: 56%;
  height: 12%;
  z-index: 2;
  margin: 0;
  padding: 0;
  border: 0;
  background: transparent;
  color: transparent;
  caret-color: transparent;
  font-size: 16px;
  text-align: center;
}

#namebox[hidden] { display: none !important; }

#lost p {
  margin: 0 0 8px;
  text-align: center;
  letter-spacing: 0.04em;
  font-size: 14px;
}

#lost button {
  min-height: 44px;
  width: 100%;
}

#lost-share { margin-bottom: 8px; }

#tip {
  width: min(96vw, 420px);
  padding: 14px 14px 12px;
  background: #241c14;
  color: var(--gold);
  border: 2px solid #8a7040;
  border-radius: 14px;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.45);
  text-align: center;
}

#tip[hidden] { display: none !important; }

#tip p {
  margin: 0 0 10px;
  letter-spacing: 0.03em;
  font-size: 15px;
  line-height: 1.35;
}

#tip-qr {
  width: 148px;
  height: 148px;
  margin: 0 auto 8px;
  background: #fff;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

#tip-addr {
  font-size: 12px;
  line-height: 1.4;
  word-break: break-all;
  user-select: text;
  -webkit-user-select: text;
}

#tip a {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 56px;
  margin-top: 4px;
  padding: 8px;
  text-decoration: none;
  letter-spacing: 0.06em;
  font-size: 14px;
  color: var(--gold);
  background: #241c14;
  border: 2px solid #8a7040;
  border-radius: 10px;
  touch-action: manipulation;
}

#tip a:active { background: #3a2e1c; }

#touch {
  display: none;
  width: min(96vw, 820px);
  box-sizing: border-box;
  gap: 8px;
  justify-content: space-between;
  align-items: center;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
  background: linear-gradient(#3a3228, #241c14 40%, #16110d);
  border: 3px solid #6d5a3c;
  border-radius: 28px;
  padding: 10px 8px 12px;
  box-shadow: inset 0 0 0 3px #1a140e;
}

.pad, .nes-mid, .face {
  display: grid;
}

.pad {
  gap: 2px;
  grid-template-columns: repeat(3, 42px);
  grid-template-areas:
    ". up ."
    "left down right";
}

.pad [data-key="ArrowUp"] { grid-area: up; }
.pad [data-key="ArrowLeft"] { grid-area: left; }
.pad [data-key="ArrowDown"] { grid-area: down; }
.pad [data-key="ArrowRight"] { grid-area: right; }

.nes-mid {
  gap: 8px;
  align-content: center;
  justify-items: center;
}

.face {
  grid-template-columns: 56px 56px;
  grid-template-rows: 16px 54px;
  align-items: end;
}

.face .btn-b { grid-column: 1; grid-row: 2; }
.face .btn-a { grid-column: 2; grid-row: 1 / span 2; align-self: start; }

#touch button {
  font-family: inherit;
  color: #d7c7a2;
  background: #1a1612;
  border: 2px solid #3a3228;
  border-radius: 6px;
  min-height: 42px;
  min-width: 42px;
  padding: 0;
  letter-spacing: 0.06em;
  font-size: 10px;
  box-shadow: inset 0 -3px 0 #0c0a08;
}

#touch .pad button { position: relative; }

#touch .pad button::after {
  content: '';
  width: 0;
  height: 0;
  border-style: solid;
}

#touch .pad [data-key="ArrowUp"]::after {
  border-width: 0 8px 12px 8px;
  border-color: transparent transparent #d7c7a2 transparent;
}

#touch .pad [data-key="ArrowDown"]::after {
  border-width: 12px 8px 0 8px;
  border-color: #d7c7a2 transparent transparent transparent;
}

#touch .pad [data-key="ArrowLeft"]::after {
  border-width: 8px 12px 8px 0;
  border-color: transparent #d7c7a2 transparent transparent;
}

#touch .pad [data-key="ArrowRight"]::after {
  border-width: 8px 0 8px 12px;
  border-color: transparent transparent transparent #d7c7a2;
}

#touch .pad button:active,
#touch .nes-mid button:active { background: #2a241c; }

#touch .nes-mid button {
  min-height: 26px;
  min-width: 58px;
  height: 26px;
  border-radius: 13px;
  background: #2a241c;
  color: #8a7040;
  box-shadow: inset 0 -2px 0 #0c0a08;
  font-size: 8px;
}

#touch .face button {
  width: 54px;
  height: 54px;
  min-width: 54px;
  min-height: 54px;
  border-radius: 50%;
  background: radial-gradient(circle at 40% 35%, #c45a52, #7a2824 70%);
  border-color: #e7b2a4;
  color: #fff6e8;
  font-size: 18px;
  letter-spacing: 0;
  box-shadow: inset 0 -4px 0 #4a1614, 0 3px 0 #1a100c;
}

#touch .face button:active { background: #6a2420; }

button {
  font-family: inherit;
  font-size: 14px;
  letter-spacing: 0.06em;
  color: var(--gold);
  background: #241c14;
  border: 2px solid #8a7040;
  border-radius: 10px;
  min-height: 56px;
  padding: 8px;
  touch-action: none;
  display: flex;
  align-items: center;
  justify-content: center;
}

button svg {
  width: 28px;
  height: 28px;
  fill: currentColor;
  display: block;
}

button:active { background: #3a2e1c; }

@media (pointer: coarse), (max-width: 820px) {
  html, body {
    height: 100%;
    overflow: hidden;
  }

  body {
    min-height: 100dvh;
    height: 100dvh;
    padding:
      max(8px, env(safe-area-inset-top))
      max(8px, env(safe-area-inset-right))
      max(8px, env(safe-area-inset-bottom))
      max(8px, env(safe-area-inset-left));
    gap: 8px;
    justify-content: flex-start;
    overscroll-behavior: none;
    touch-action: manipulation;
  }

  #bezel {
    width: 100%;
    flex: 0 1 auto;
    min-height: 0;
    padding: 8px 8px 6px;
    border-radius: 14px;
  }

  .brand {
    padding-bottom: 4px;
    font-size: 10px;
  }

  #screen {
    display: flex;
    justify-content: center;
  }

  canvas {
    width: min(100%, calc((100dvh - 250px) * 256 / 224));
    height: auto;
    aspect-ratio: 256 / 224;
  }

  .motto {
    margin-top: 6px;
    font-size: 10px;
    letter-spacing: 0.14em;
  }

  #touch {
    display: flex;
    width: 100%;
    flex: 0 0 auto;
    margin-top: auto;
  }

  body.ending {
    overflow: auto;
    height: auto;
  }

  body.ending #touch { display: none; }
}

@media (max-height: 740px) and (pointer: coarse), (max-height: 740px) and (max-width: 820px) {
  .brand { display: none; }
  .site-head p { display: none; }
  .site-head h1 { font-size: 13px; letter-spacing: 0.12em; }
  button { min-height: 48px; font-size: 13px; }
  #touch .pad { grid-template-columns: repeat(3, 36px); }
  #touch .pad button { min-height: 36px; min-width: 36px; }
  #touch .face button { width: 48px; height: 48px; min-width: 48px; min-height: 48px; font-size: 16px; }
  #touch .nes-mid button { min-width: 52px; min-height: 24px; height: 24px; }
  #lost button { min-height: 40px; }
}

@media (prefers-reduced-motion: reduce) {
  #screen::after { display: none; }
}
