:root {
  --bg: #0a0810;
  --frame: #2a2218;
  --gold: #d4a84b;
  --text: #f0e8c8;
  --topbar-h: 48px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  height: 100%;
  height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font-family: "Outfit", system-ui, sans-serif;
  overflow: hidden;
}

body {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  min-height: 100dvh;
  padding:
    calc(var(--topbar-h) + 4px)
    8px
    calc(8px + var(--safe-bottom))
    8px;
  background:
    radial-gradient(ellipse 60% 50% at 50% 0%, rgba(80, 60, 40, 0.35), transparent 70%),
    var(--bg);
}

.topbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.65rem 1.25rem;
  padding-top: max(0.65rem, env(safe-area-inset-top, 0px));
  height: var(--topbar-h);
  z-index: 5;
  font-size: 0.8rem;
  letter-spacing: 0.06em;
  color: rgba(240, 232, 200, 0.7);
}

.topbar a {
  color: var(--gold);
  text-decoration: none;
}

.topbar a:hover {
  text-decoration: underline;
}

.frame {
  border: 4px solid #5c4510;
  box-shadow:
    0 0 0 2px #d4a84b,
    0 0 40px rgba(212, 168, 75, 0.15),
    inset 0 0 0 2px #1a1420;
  background: #000;
  line-height: 0;
  max-width: 100%;
  touch-action: none;
}

#game {
  display: block;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  background: #000;
  max-width: 100%;
  height: auto;
  touch-action: none;
}

.hint {
  margin-top: 0.85rem;
  font-size: 0.75rem;
  color: rgba(240, 232, 200, 0.45);
  letter-spacing: 0.04em;
  text-align: center;
  max-width: 36rem;
  padding: 0 1rem;
  flex-shrink: 0;
}

@media (max-width: 900px) {
  :root {
    --topbar-h: 40px;
  }

  .topbar {
    padding: 0.4rem 0.75rem;
    padding-top: max(0.4rem, env(safe-area-inset-top, 0px));
    font-size: 0.7rem;
  }

  .topbar span {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 55vw;
  }

  .hint {
    margin-top: 0.5rem;
    font-size: 0.65rem;
  }

  .frame {
    border-width: 3px;
  }
}

@media (max-width: 520px) {
  :root {
    --topbar-h: 36px;
  }

  .hint {
    display: none;
  }

  body {
    padding:
      calc(var(--topbar-h) + 2px)
      4px
      calc(4px + var(--safe-bottom))
      4px;
  }
}

/* —— Virtual pad (smartphone) —— */
.hint-mobile {
  display: none;
}

body.has-virtual-pad .hint-desktop {
  display: none;
}

body.has-virtual-pad .hint-mobile {
  display: inline;
}

body.has-virtual-pad .hint {
  display: block;
}

@media (max-width: 520px) {
  body.has-virtual-pad .hint {
    display: block;
    font-size: 0.6rem;
    margin-top: 0.35rem;
  }
}

.virtual-pad {
  position: fixed;
  inset: 0;
  z-index: 20;
  pointer-events: none;
  touch-action: none;
  /* leave top clear for topbar / canvas */
}

.virtual-pad .vp-dpad,
.virtual-pad .vp-actions {
  pointer-events: auto;
  touch-action: none;
  position: absolute;
  bottom: calc(12px + env(safe-area-inset-bottom, 0px));
  user-select: none;
  -webkit-user-select: none;
}

.virtual-pad .vp-dpad {
  left: calc(10px + env(safe-area-inset-left, 0px));
  display: grid;
  grid-template-columns: repeat(3, 56px);
  grid-template-rows: repeat(3, 56px);
  gap: 4px;
  grid-template-areas:
    ". up ."
    "left center right"
    ". down .";
}

.virtual-pad .vp-dir[data-dir="up"] { grid-area: up; }
.virtual-pad .vp-dir[data-dir="left"] { grid-area: left; }
.virtual-pad .vp-dir[data-dir="right"] { grid-area: right; }
.virtual-pad .vp-dir[data-dir="down"] { grid-area: down; }
.virtual-pad .vp-center { grid-area: center; opacity: 0; pointer-events: none; }

.virtual-pad .vp-actions {
  right: calc(10px + env(safe-area-inset-right, 0px));
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 10px;
}

.virtual-pad .vp-ab {
  display: flex;
  gap: 12px;
  align-items: flex-end;
}

.virtual-pad .vp-btn {
  appearance: none;
  border: 2px solid rgba(212, 168, 75, 0.55);
  background: rgba(20, 14, 28, 0.72);
  color: rgba(240, 232, 200, 0.9);
  font-family: "Press Start 2P", monospace;
  font-size: 10px;
  border-radius: 12px;
  width: 56px;
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.45);
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
  cursor: pointer;
}

.virtual-pad .vp-btn.is-down,
.virtual-pad .vp-btn:active {
  transform: translateY(2px);
  box-shadow: 0 2px 0 rgba(0, 0, 0, 0.45);
  background: rgba(212, 168, 75, 0.35);
  color: #fff8e0;
}

.virtual-pad .vp-a {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  font-size: 14px;
  background: rgba(56, 104, 160, 0.75);
  border-color: rgba(120, 180, 255, 0.6);
}

.virtual-pad .vp-b {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  font-size: 12px;
  background: rgba(160, 56, 40, 0.7);
  border-color: rgba(255, 140, 120, 0.55);
  margin-bottom: 8px;
}

.virtual-pad .vp-menu {
  width: auto;
  min-width: 72px;
  height: 36px;
  padding: 0 10px;
  font-size: 8px;
  letter-spacing: 0.04em;
  border-radius: 8px;
  opacity: 0.9;
}

@media (max-width: 420px) {
  .virtual-pad .vp-dpad {
    grid-template-columns: repeat(3, 48px);
    grid-template-rows: repeat(3, 48px);
    gap: 3px;
  }
  .virtual-pad .vp-btn {
    width: 48px;
    height: 48px;
    font-size: 9px;
  }
  .virtual-pad .vp-a {
    width: 56px;
    height: 56px;
  }
  .virtual-pad .vp-b {
    width: 46px;
    height: 46px;
  }
}

/* Dim pad slightly when not needed visual clutter on large coarse tablets */
@media (min-width: 901px) and (pointer: fine) {
  .virtual-pad:not([hidden]) {
    opacity: 0.55;
  }
}

/* —— Mobile OS keyboard overlay for Dialog.prompt fields —— */
.mobile-text-input {
  position: fixed;
  z-index: 15;
  margin: 0;
  padding: 0 4px;
  border: none;
  outline: none;
  background: transparent;
  color: transparent;
  caret-color: transparent;
  font-family: "Press Start 2P", monospace;
  line-height: 1.2;
  -webkit-appearance: none;
  appearance: none;
  border-radius: 0;
  box-shadow: none;
  /* Nearly invisible but still a real tap/focus target for the OS keyboard */
  opacity: 0.02;
  pointer-events: none;
  touch-action: manipulation;
  -webkit-user-select: text;
  user-select: text;
  -webkit-tap-highlight-color: transparent;
}

.mobile-text-input.is-active {
  pointer-events: auto;
  opacity: 0.02;
}

.mobile-text-input:focus {
  opacity: 0.02;
}
