:root {
  --bg:        #15131f;
  --panel:     #1f1c2e;
  --accent:    #6b5bd2;
  --accent-d:  #4c3fa3;
  --screen-bg: #0c0b12;
  --btn:       #2a2740;
  --btn-press: #6b5bd2;
  --red:       #d63b6a;
  --red-press: #f06090;
  --text:      #e8e6f2;
  --muted:     #9a96b5;
  --safe-top:    env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left:   env(safe-area-inset-left, 0px);
  --safe-right:  env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Kaku Gothic ProN",
               "Segoe UI", Roboto, sans-serif;
  /* Lock the page: no scrolling / rubber-banding / double-tap zoom */
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100%;
  padding: var(--safe-top) var(--safe-right) var(--safe-bottom) var(--safe-left);
}

/* ---------- Toolbar ---------- */
#toolbar {
  flex: 0 0 auto;
  display: flex;
  gap: 6px;
  padding: 8px 10px;
  background: var(--panel);
  align-items: center;
}
.tool {
  appearance: none;
  border: none;
  background: var(--btn);
  color: var(--text);
  font-size: 15px;
  font-weight: 600;
  padding: 8px 11px;
  border-radius: 10px;
  min-height: 38px;
  cursor: pointer;
}
.tool:active { background: var(--accent); }
#btn-speed { min-width: 44px; }

/* ---------- Screen ---------- */
#screen-wrap {
  flex: 1 1 auto;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--screen-bg);
  overflow: hidden;
  min-height: 0;
}
#screen {
  /* Keep the native 160:144 aspect ratio, pixel-perfect upscaling */
  max-width: 100%;
  max-height: 100%;
  aspect-ratio: 160 / 144;
  width: auto;
  height: 100%;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  background: #000;
}

/* Start overlay */
#overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(12, 11, 18, 0.92);
  padding: 24px;
  text-align: center;
}
#overlay.hidden { display: none; }
#overlay-card { max-width: 320px; }
#overlay-card h1 {
  margin: 0 0 10px;
  font-size: 30px;
  letter-spacing: 1px;
  color: var(--accent);
}
#overlay-card p { color: var(--muted); line-height: 1.6; margin: 0 0 18px; }
#overlay-card .hint { font-size: 13px; }
.primary {
  appearance: none;
  border: none;
  background: var(--accent);
  color: #fff;
  font-size: 17px;
  font-weight: 700;
  padding: 14px 26px;
  border-radius: 14px;
  cursor: pointer;
}
.primary:active { background: var(--accent-d); }

/* ---------- Controls ---------- */
#controls {
  flex: 0 0 auto;
  position: relative;
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-areas:
    "dpad ab"
    "startsel startsel";
  align-items: center;
  gap: 10px;
  padding: 18px 22px calc(18px + var(--safe-bottom));
  background: var(--panel);
}

/* D-pad */
#dpad {
  grid-area: dpad;
  position: relative;
  width: 150px;
  height: 150px;
  justify-self: center;
}
.dpad-center {
  position: absolute;
  left: 50px; top: 50px;
  width: 50px; height: 50px;
  background: var(--btn);
}
.dbtn {
  position: absolute;
  width: 50px; height: 50px;
  border: none;
  background: var(--btn);
  cursor: pointer;
}
.dbtn.up    { left: 50px; top: 0;    border-radius: 10px 10px 0 0; }
.dbtn.down  { left: 50px; top: 100px;border-radius: 0 0 10px 10px; }
.dbtn.left  { left: 0;    top: 50px; border-radius: 10px 0 0 10px; }
.dbtn.right { left: 100px;top: 50px; border-radius: 0 10px 10px 0; }
.dbtn.pressed { background: var(--btn-press); }

/* A / B */
#ab {
  grid-area: ab;
  justify-self: center;
  display: flex;
  align-items: center;
  gap: 22px;
  transform: rotate(-18deg);
}
.rbtn {
  width: 72px; height: 72px;
  border-radius: 50%;
  border: none;
  background: var(--red);
  color: #fff;
  font-weight: 800;
  font-size: 20px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
.rbtn span { transform: rotate(18deg); }
.rbtn.a { transform: translateY(-14px); }
.rbtn.pressed { background: var(--red-press); }

/* Start / Select */
#startsel {
  grid-area: startsel;
  display: flex;
  justify-content: center;
  gap: 26px;
  margin-top: 4px;
}
.pill {
  border: none;
  background: var(--btn);
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 1px;
  padding: 9px 20px;
  border-radius: 20px;
  cursor: pointer;
}
.pill.pressed { background: var(--accent); color: #fff; }

/* Landscape: controls beside the screen */
@media (orientation: landscape) {
  #controls {
    grid-template-columns: 1fr 2fr 1fr;
    grid-template-areas: "dpad startsel ab";
  }
  #dpad     { transform: scale(0.92); }
}

@media (min-width: 820px) {
  #screen { image-rendering: pixelated; }
}

/* ---------- Diagnostics log ---------- */
#log {
  position: fixed;
  left: 0; right: 0;
  bottom: 0;
  max-height: 45vh;
  margin: 0;
  padding: 10px 12px calc(10px + var(--safe-bottom));
  background: rgba(0, 0, 0, 0.9);
  color: #b6f09a;
  font: 11px/1.45 ui-monospace, SFMono-Regular, Menlo, monospace;
  white-space: pre-wrap;
  word-break: break-word;
  overflow-y: auto;
  z-index: 1000;
  border-top: 1px solid #333;
}
#log[hidden] { display: none; }
#log .err { color: #ff8a8a; }
