:root {
  color-scheme: dark;
  --ink: #f7f2dc;
  --muted: #b5bfd8;
  --panel: #151923;
  --edge: #414a5f;
  --red: #f05a42;
  --gold: #f2c14e;
  --cyan: #43c7d8;
  --green: #79d279;
  --cream: #f7f2dc;
  --bg: #151923;
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  overflow: hidden;
  position: fixed;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

body {
  display: grid;
  place-items: center;
  background:
    radial-gradient(circle at 15% 10%, rgba(67, 199, 216, 0.18), transparent 28%),
    linear-gradient(135deg, #0d1218 0%, #1b2230 48%, #11141b 100%);
  color: var(--ink);
  font-family: Impact, Haettenschweiler, "Arial Narrow Bold", sans-serif;
}

.shell {
  width: 100%;
  height: 100%;
  display: grid;
  place-items: center;
}

.game-wrap {
  position: relative;
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
}

canvas {
  display: block;
  max-width: 100vw;
  max-height: 100vh;
  width: auto;
  height: auto;
  object-fit: contain;
  background: #101620;
  touch-action: none;
}

/* ── Touch Controls ── */
.touch-controls {
  position: absolute;
  inset: 0;
  display: none;
  pointer-events: none;
  /* Reduced padding to let controls sit closer to screen edges */
  padding: 1% 1.5%;
}

.mode-start {
  position: absolute;
  left: 50%;
  bottom: clamp(18px, 4vh, 36px);
  z-index: 5;
  width: auto;
  height: auto;
  min-width: clamp(180px, 30vw, 300px);
  min-height: clamp(56px, 8vh, 72px);
  padding: 0 32px;
  transform: translateX(-50%);
  border: 3px solid var(--gold);
  border-radius: 12px;
  background: rgba(12, 17, 25, 0.9);
  color: var(--cream);
  box-shadow: 0 0 0 2px rgba(0, 0, 0, 0.5), 0 10px 24px rgba(0, 0, 0, 0.45);
  font: 800 clamp(1.1rem, 3vw, 1.6rem) "Courier New", monospace;
  letter-spacing: 0;
  text-transform: uppercase;
  touch-action: manipulation;
  cursor: pointer;
}

.mode-start:hover,
.mode-start:focus-visible {
  background: rgba(67, 199, 216, 0.3);
  border-color: var(--cyan);
  outline: none;
}

.mode-start:active {
  transform: translateX(-50%) scale(0.94);
}

.mode-start[hidden] {
  display: none;
}

/* On touch devices, show controls and fill the viewport */
@media (hover: none) and (pointer: coarse) {
  .touch-controls {
    display: block;
  }

  canvas {
    border-radius: 0;
    border: none;
    box-shadow: none;
    width: 100vw;
    height: 100vh;
    max-width: 100vw;
    max-height: 100vh;
    object-fit: fill;
  }

  .shell {
    padding: 0;
  }
}

.pad,
.actions {
  position: absolute;
  bottom: 3%;
  display: grid;
  gap: 8px;
  pointer-events: auto;
  z-index: 10;
}

/* D-pad: diamond layout on the left — bigger for fat thumbs */
.pad {
  left: 1.5%;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  width: clamp(140px, 30vmin, 210px);
  height: clamp(140px, 30vmin, 210px);
}

/* Diamond positions: jump(▲) top-centre, left(◀) middle-left, right(▶) middle-right, block(◆) bottom-centre */
.pad button:nth-child(1) { grid-column: 1; grid-row: 2; }  /* ◀ left */
.pad button:nth-child(2) { grid-column: 3; grid-row: 2; }  /* ▶ right */
.pad button:nth-child(3) { grid-column: 2; grid-row: 1; }  /* ▲ jump */
.pad button:nth-child(4) { grid-column: 2; grid-row: 3; }  /* ◆ block */

/* Action buttons: right side — switched to a taller 3-column grid so each button is a proper thumb target */
.actions {
  right: 1.5%;
  grid-template-columns: repeat(3, 1fr);
  width: clamp(160px, 35vmin, 240px);
  height: clamp(100px, 18vmin, 140px);
}

button {
  position: relative;
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  border: 2px solid rgba(255, 255, 255, 0.45);
  border-radius: 12px;
  background: rgba(12, 17, 25, 0.75);
  color: var(--ink);
  font: 700 clamp(1.1rem, 4vmin, 1.6rem)/1 Arial, sans-serif;
  touch-action: none;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  backdrop-filter: blur(2px);
}

button:active,
button.is-down {
  transform: scale(0.88);
  background: rgba(67, 199, 216, 0.5);
  border-color: var(--cyan);
  box-shadow: 0 0 16px rgba(67, 199, 216, 0.35);
}

/* ── Landscape phone (small screens) — KEEP BUTTONS BIG, only tighten spacing ── */
@media (orientation: landscape) and (max-height: 500px) {
  .game-wrap {
    width: 100%;
    height: 100%;
  }

  .pad {
    width: clamp(110px, 26vmin, 170px);
    height: clamp(110px, 26vmin, 170px);
    gap: 6px;
    bottom: 2%;
    left: 1%;
  }

  .actions {
    width: clamp(130px, 30vmin, 200px);
    height: clamp(80px, 16vmin, 120px);
    gap: 6px;
    bottom: 2%;
    right: 1%;
  }

  button {
    border-radius: 10px;
    font-size: clamp(1rem, 3.5vmin, 1.4rem);
    border-width: 2px;
  }

}

/* ── Larger touch screens (tablets) ── */
@media (min-width: 1024px) and (pointer: coarse) {
  .pad {
    width: 220px;
    height: 220px;
    gap: 10px;
  }

  .actions {
    width: 260px;
    height: 160px;
    gap: 10px;
  }

  button {
    font-size: 1.8em;
    border-radius: 14px;
  }
}

/* Desktop: never show touch controls */
@media (hover: hover) and (pointer: fine) {
  .touch-controls {
    display: none !important;
  }
}