:root {
  --bg: #0a0810;
  --frame: #2a2218;
  --gold: #d4a84b;
  --text: #f0e8c8;
  --topbar-h: 48px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  /* Set by fitCanvas when the touch pad can sit below the playfield. */
  --pad-reserve: 0px;
  --pad-lift: 28px;
}

* {
  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) + var(--pad-reserve))
    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;
}

.topbar-tools {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 0.65rem;
  flex-shrink: 0;
}

.fs-btn {
  appearance: none;
  border: 2px solid rgba(212, 168, 75, 0.75);
  background: rgba(20, 14, 28, 0.88);
  color: var(--text);
  font-family: "Outfit", system-ui, sans-serif;
  font-size: 0.72rem;
  letter-spacing: 0.03em;
  padding: 0.28rem 0.55rem;
  border-radius: 6px;
  cursor: pointer;
  line-height: 1.2;
}

.fs-btn:hover,
.fs-btn:focus-visible {
  background: rgba(212, 168, 75, 0.28);
  outline: none;
}

/* Wide window: less chrome so the 16:9 frame uses more of the viewport. */
body.is-landscape {
  --topbar-h: 32px;
  padding:
    calc(var(--topbar-h) + 2px)
    4px
    calc(2px + var(--safe-bottom) + var(--pad-reserve))
    4px;
}

body.is-landscape .hint {
  display: none;
}

body.is-landscape .frame {
  border-width: 2px;
}

body.is-landscape .topbar {
  padding: 0.2rem 0.7rem;
  padding-top: max(0.2rem, env(safe-area-inset-top, 0px));
  font-size: 0.72rem;
}

/* OS fullscreen, or the iOS-style viewport fill when that API is missing. */
body.is-max-fill {
  --topbar-h: 0px;
  padding: 0 !important;
  width: 100vw;
  height: 100dvh;
  min-height: 100dvh;
}

body.is-max-fill .hint {
  display: none !important;
}

body.is-max-fill .frame {
  border-width: 0;
  box-shadow: none;
}

body.is-max-fill .topbar {
  height: auto;
  padding: 0;
  background: transparent;
  pointer-events: none;
}

body.is-max-fill .topbar .back {
  display: none;
}

body.is-max-fill .topbar-tools {
  pointer-events: auto;
  position: fixed;
  top: max(6px, env(safe-area-inset-top, 0px));
  right: max(6px, env(safe-area-inset-right, 0px));
  z-index: 40;
}

.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;
  flex: 0 0 auto;
  touch-action: none;
}

#game {
  display: block;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  background: #000;
  /* Width/height come from fitCanvas so the CSS box tracks the window. */
  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) + var(--pad-reserve))
      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-stick,
.virtual-pad .vp-dpad,
.virtual-pad .vp-actions {
  pointer-events: auto;
  touch-action: none;
  position: absolute;
  /* Extra lift so the controls are not jammed against the bottom edge (esp. fullscreen). */
  bottom: calc(12px + var(--pad-lift, 28px) + env(safe-area-inset-bottom, 0px));
  user-select: none;
  -webkit-user-select: none;
}

/* Same corner as the old d-pad. ~168px so it does not cover A/B/Run. */
.virtual-pad .vp-stick {
  left: calc(10px + env(safe-area-inset-left, 0px));
  width: 168px;
  height: 168px;
}

.virtual-pad .vp-stick-base {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  border: 2px solid rgba(212, 168, 75, 0.46);
  background: rgba(20, 14, 28, 0.58);
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.35);
  position: relative;
  touch-action: none;
}

.virtual-pad .vp-stick-base.is-down {
  background: rgba(20, 14, 28, 0.66);
}

.virtual-pad .vp-stick-knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 64px;
  height: 64px;
  margin: -32px 0 0 -32px;
  border-radius: 50%;
  border: 2px solid rgba(240, 232, 200, 0.55);
  background: rgba(212, 168, 75, 0.7);
  box-shadow: 0 3px 0 rgba(0, 0, 0, 0.35);
  pointer-events: none;
}

/* Same corner and footprint as the joystick. Shown only for discrete menus. */
.virtual-pad .vp-dpad {
  display: none;
  left: calc(10px + env(safe-area-inset-left, 0px));
  width: 168px;
  height: 168px;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  place-items: center;
  pointer-events: none;
}

.virtual-pad.is-discrete .vp-stick {
  display: none;
}

.virtual-pad.is-discrete .vp-dpad {
  display: grid;
}

.virtual-pad .vp-dpad-up { grid-column: 2; grid-row: 1; }
.virtual-pad .vp-dpad-left { grid-column: 1; grid-row: 2; }
.virtual-pad .vp-dpad-right { grid-column: 3; grid-row: 2; }
.virtual-pad .vp-dpad-down { grid-column: 2; grid-row: 3; }

.virtual-pad .vp-dpad-btn {
  pointer-events: auto;
  width: 54px;
  height: 54px;
  padding: 0;
}

.virtual-pad .vp-arrow {
  width: 0;
  height: 0;
  border-style: solid;
  display: block;
}

.virtual-pad .vp-arrow-up {
  border-width: 0 8px 13px 8px;
  border-color: transparent transparent currentColor transparent;
}

.virtual-pad .vp-arrow-down {
  border-width: 13px 8px 0 8px;
  border-color: currentColor transparent transparent transparent;
}

.virtual-pad .vp-arrow-left {
  border-width: 8px 13px 8px 0;
  border-color: transparent currentColor transparent transparent;
}

.virtual-pad .vp-arrow-right {
  border-width: 8px 0 8px 13px;
  border-color: transparent transparent transparent currentColor;
}

.virtual-pad:not(.is-walking) .vp-run {
  display: 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.46);
  background: rgba(20, 14, 28, 0.58);
  color: rgba(240, 232, 200, 0.82);
  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.28);
  color: #fff8e0;
}

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

.virtual-pad .vp-b {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  font-size: 12px;
  background: rgba(160, 56, 40, 0.58);
  border-color: rgba(255, 140, 120, 0.46);
  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.78;
}

.virtual-pad .vp-run {
  width: auto;
  min-width: 72px;
  height: 44px;
  padding: 0 12px;
  font-size: 8px;
  letter-spacing: 0.04em;
  border-radius: 10px;
  background: rgba(36, 96, 52, 0.66);
  border-color: rgba(150, 220, 160, 0.5);
}

@media (max-width: 420px) {
  .virtual-pad .vp-stick,
  .virtual-pad .vp-dpad {
    width: 148px;
    height: 148px;
  }
  .virtual-pad .vp-dpad-btn {
    width: 46px;
    height: 46px;
  }
  .virtual-pad .vp-stick-knob {
    width: 56px;
    height: 56px;
    margin: -28px 0 0 -28px;
  }
  .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;
}

/* —— Mobile entry panel (touch): pinned above the OS keyboard —— */
.mobile-entry {
  position: fixed;
  z-index: 60;
  box-sizing: border-box;
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-areas: "text text" "slot slot" "btns btns";
  gap: 12px;
  padding: 14px;
  overflow: hidden;
  background: #181818;
  border: 3px solid #d4a84b;
  box-shadow: 0 0 0 3px #8a6830, 0 8px 24px #000, 0 0 0 100vmax rgba(0, 0, 0, 0.5);
  color: #f0e8c8;
  font-family: "Press Start 2P", monospace;
  -webkit-text-size-adjust: 100%;
}
.mobile-entry[hidden] { display: none; }
.mobile-entry .me-text { grid-area: text; min-width: 0; }
.mobile-entry .me-title { color: #d4a84b; font-size: 11px; line-height: 1.4; margin-bottom: 8px; }
.mobile-entry .me-q { font-size: 12px; line-height: 1.6; overflow-wrap: anywhere; }
.mobile-entry .me-hint { font-size: 10px; line-height: 1.5; color: #a89878; margin-top: 6px; }
.mobile-entry .me-hint[hidden] { display: none; }
.mobile-entry .me-slot {
  grid-area: slot;
  position: relative;
  height: 50px;
  box-sizing: border-box;
  background: #1e1628;
  border: 2px solid #d4a84b;
}
.mobile-entry .me-slot[data-prefix]:not([data-prefix=""])::before {
  content: attr(data-prefix);
  position: absolute;
  left: 8px;
  top: 50%;
  transform: translateY(-50%);
  color: #fff4c8;
  font-size: 16px;
}
.mobile-entry .me-btns { grid-area: btns; display: flex; gap: 10px; }
.mobile-entry button {
  flex: 1;
  min-height: 44px;
  font: 11px "Press Start 2P", monospace;
  border: 2px solid #8a6830;
  background: #2a2018;
  color: #c8b878;
  border-radius: 0;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
.mobile-entry button.me-done { background: #d4a84b; color: #181818; border-color: #fff4c8; }
/* Landscape: one compact row (question | input | buttons). */
.mobile-entry.is-compact {
  grid-template-columns: minmax(0, 1.3fr) minmax(0, 1fr) auto;
  grid-template-areas: "text slot btns";
  align-items: center;
  padding: 10px 12px;
}
.mobile-entry.is-compact .me-title { margin-bottom: 6px; font-size: 10px; }
.mobile-entry.is-compact .me-q { font-size: 11px; }
.mobile-entry.is-compact .me-btns { flex-direction: column; gap: 6px; width: 112px; }
.mobile-entry.is-compact button { min-height: 36px; font-size: 10px; }
.mobile-entry.no-hint .me-hint { display: none; }

/* The real input, shown over the panel's slot while the panel is open. */
.mobile-text-input.in-panel,
.mobile-text-input.in-panel:focus {
  z-index: 61;
  opacity: 1;
  color: #fff4c8;
  caret-color: #fff4c8;
  background: transparent;
  padding: 0 8px;
  pointer-events: auto;
}
body.mobile-entry-open .virtual-pad { visibility: hidden; }
.mobile-entry .me-error { font-size: 10px; line-height: 1.5; color: #ff8a6a; margin-top: 6px; }
.mobile-entry .me-error[hidden] { display: none; }

/* —— Title menu: Create User Profile with AI —— */
.ai-profile {
  position: fixed;
  inset: 0;
  z-index: 80;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
  box-sizing: border-box;
  background: rgba(0, 0, 0, 0.72);
}
.ai-profile-panel {
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: min(820px, 100%);
  max-height: 100%;
  height: min(720px, 100%);
  box-sizing: border-box;
  padding: 16px;
  background: #181818;
  border: 3px solid #d4a84b;
  box-shadow: 0 0 0 3px #8a6830, 0 8px 24px #000;
  color: #f0e8c8;
}
.ai-profile-title {
  flex: none;
  margin: 0;
  font: 14px/1.5 "Press Start 2P", monospace;
  color: #d4a84b;
}
.ai-profile-intro {
  flex: none;
  margin: 0;
  max-height: 28%;
  overflow-y: auto;
  white-space: pre-line;
  font: 15px/1.45 "Outfit", system-ui, sans-serif;
}
.ai-profile-text {
  flex: 1 1 auto;
  min-height: 90px;
  width: 100%;
  box-sizing: border-box;
  resize: none;
  overflow: auto;
  padding: 10px;
  background: #0f0b14;
  color: #f0e8c8;
  border: 2px solid #d4a84b;
  border-radius: 0;
  font: 13px/1.45 ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace;
  -webkit-user-select: text;
  user-select: text;
}
.ai-profile-btns { flex: none; display: flex; gap: 10px; }
.ai-profile-btns button {
  flex: 1;
  min-height: 44px;
  font: 12px "Press Start 2P", monospace;
  border: 2px solid #8a6830;
  background: #2a2018;
  color: #c8b878;
  border-radius: 0;
  touch-action: manipulation;
  cursor: pointer;
}
.ai-profile-btns .ai-profile-copy { background: #d4a84b; color: #181818; border-color: #fff4c8; }
.ai-profile-btns button:focus-visible { outline: 3px solid #fff4c8; outline-offset: 2px; }
@media (max-width: 600px) {
  .ai-profile { padding: 6px; }
  .ai-profile-panel { padding: 12px; gap: 10px; }
  .ai-profile-title { font-size: 11px; }
  .ai-profile-intro { font-size: 14px; }
  .ai-profile-text { font-size: 12px; }
}

/* Touch devices: the pad has its own labels, so drop the hint line and give the game the room. */
body.has-virtual-pad .hint {
  display: none;
}

/* Landscape: the pad overlays the game, so lift it less. */
body.is-landscape .virtual-pad { --pad-lift: 16px; }
