:root {
  --bg: #16161f;
  --panel: #20202c;
  --panel-edge: #2c2c3c;
  --text: #e8e8f0;
  --muted: #9a9ab0;
  --yellow: #f7d02c;
  --yellow-dark: #c79f1f;
  --btn-face: #3a3a4e;
  --btn-face-active: #54546e;
  --red: #c8385a;
}

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

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
}

body {
  position: fixed;
  inset: 0;
  overflow: hidden;
  touch-action: manipulation;
}

#app {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* ---------- top bar ---------- */

#topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  flex: 0 0 auto;
}

#title {
  font-weight: 700;
  letter-spacing: 0.04em;
  color: var(--yellow);
}

#actions {
  display: flex;
  gap: 8px;
}

#actions button {
  background: var(--btn-face);
  color: var(--text);
  border: 1px solid var(--panel-edge);
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
  padding: 6px 10px;
  min-width: 40px;
}

#actions button:active {
  background: var(--btn-face-active);
}

#actions button:disabled {
  opacity: 0.4;
}

/* ---------- screen ---------- */

#screen-wrap {
  flex: 0 1 auto;
  display: flex;
  justify-content: center;
  padding: 4px 12px;
  min-height: 0;
}

#bezel {
  position: relative;
  background: var(--panel);
  border: 1px solid var(--panel-edge);
  border-radius: 12px;
  padding: 10px;
  width: 100%;
  max-width: min(100%, calc((100dvh - 360px) * 160 / 144));
}

#screen {
  display: block;
  width: 100%;
  aspect-ratio: 160 / 144;
  background: #0f380f;
  border-radius: 4px;
  image-rendering: pixelated;
}

/* ---------- start overlay ---------- */

#overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(16, 16, 24, 0.92);
  border-radius: 12px;
  padding: 16px;
}

#overlay[hidden] {
  display: none;
}

#overlay-card {
  text-align: center;
  max-width: 320px;
}

#overlay-icon {
  font-size: 40px;
}

#overlay-card h1 {
  margin: 4px 0 8px;
  font-size: 22px;
  color: var(--yellow);
}

#overlay-card p {
  font-size: 14px;
  color: var(--muted);
  line-height: 1.45;
}

#overlay-card .primary {
  display: block;
  width: 100%;
  margin: 10px 0;
  padding: 12px;
  font-size: 16px;
  font-weight: 700;
  color: #2b2b10;
  background: var(--yellow);
  border: none;
  border-radius: 10px;
}

#overlay-card .primary:active {
  background: var(--yellow-dark);
}

#install-hint {
  font-size: 12px !important;
}

/* ---------- controls ---------- */

#controls {
  flex: 1 0 auto;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  gap: 14px;
  padding: 8px 18px 18px;
  user-select: none;
  -webkit-user-select: none;
}

#pad-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

/* d-pad: one touch surface, arms are purely visual */
#dpad {
  position: relative;
  width: 148px;
  height: 148px;
  touch-action: none;
}

.dpad-arm {
  position: absolute;
  background: var(--btn-face);
  border: 1px solid var(--panel-edge);
  border-radius: 10px;
}

.dpad-v {
  left: 50%;
  top: 0;
  bottom: 0;
  width: 34%;
  transform: translateX(-50%);
}

.dpad-h {
  top: 50%;
  left: 0;
  right: 0;
  height: 34%;
  transform: translateY(-50%);
}

.dpad-hub {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 26%;
  height: 26%;
  transform: translate(-50%, -50%);
  background: var(--btn-face);
  border-radius: 50%;
  box-shadow: inset 0 0 0 3px rgba(0, 0, 0, 0.18);
}

#dpad.press-up .dpad-v,
#dpad.press-down .dpad-v,
#dpad.press-left .dpad-h,
#dpad.press-right .dpad-h {
  background: var(--btn-face-active);
}

#ab {
  display: flex;
  gap: 18px;
  align-items: flex-end;
  transform: rotate(-12deg);
}

.round-btn {
  width: 66px;
  height: 66px;
  border-radius: 50%;
  border: 1px solid #7e2440;
  background: var(--red);
  color: #ffe9ee;
  font-size: 20px;
  font-weight: 800;
  touch-action: none;
}

#btn-a {
  margin-bottom: 26px;
}

.round-btn.pressed {
  filter: brightness(1.35);
  transform: scale(0.94);
}

#meta-row {
  display: flex;
  justify-content: center;
  gap: 22px;
}

.pill-btn {
  min-width: 86px;
  padding: 9px 0;
  border-radius: 999px;
  border: 1px solid var(--panel-edge);
  background: var(--btn-face);
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.08em;
  touch-action: none;
}

.pill-btn.pressed {
  background: var(--btn-face-active);
  color: var(--text);
}

/* ---------- toast ---------- */

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom) + 24px);
  transform: translateX(-50%);
  background: rgba(0, 0, 0, 0.85);
  color: var(--text);
  font-size: 14px;
  padding: 10px 16px;
  border-radius: 10px;
  z-index: 50;
  max-width: 86vw;
  text-align: center;
}

/* landscape: D-pad left, A/B right, Select/Start under the D-pad */
@media (orientation: landscape) and (max-height: 500px) {
  #app {
    flex-direction: row;
    align-items: center;
    gap: 8px;
    padding-left: max(env(safe-area-inset-left), 12px);
    padding-right: max(env(safe-area-inset-right), 12px);
  }

  #topbar {
    display: none;
  }

  #screen-wrap {
    order: 2;
    flex: 1 1 auto;
    align-items: center;
    padding: 4px 0;
  }

  #bezel {
    max-width: min(100%, calc((100dvh - 28px) * 160 / 144));
  }

  #controls,
  #pad-row {
    display: contents;
  }

  #dpad {
    order: 1;
    flex: 0 0 auto;
  }

  #ab {
    order: 3;
    flex: 0 0 auto;
    margin-right: 6px;
  }

  #meta-row {
    position: absolute;
    left: max(env(safe-area-inset-left), 12px);
    bottom: max(env(safe-area-inset-bottom), 10px);
    width: 148px; /* matches the D-pad column */
    gap: 8px;
    z-index: 5;
  }

  .pill-btn {
    min-width: 64px;
    padding: 7px 0;
    font-size: 11px;
  }
}
