:root {
  --bg: #0a0c10;
  --panel: #12161e;
  --border: #2a3544;
  --text: #e8eef6;
  --dim: #7a889c;
  --accent: #5ec8ff;
  --danger: #ff5a6a;
  --cell: 30px;
  --font: "Press Start 2P", ui-monospace, monospace;
}

*, *::before, *::after { box-sizing: border-box; }
html, body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-font-smoothing: none;
}
body {
  min-height: 100vh;
  display: grid;
  place-items: center;
  background:
    radial-gradient(ellipse 70% 50% at 50% -10%, rgba(94, 200, 255, 0.12), transparent),
    var(--bg);
}

.crt {
  width: 100%;
  min-height: 100vh;
  display: grid;
  place-items: center;
  position: relative;
}
.crt::after {
  content: "";
  pointer-events: none;
  position: fixed;
  inset: 0;
  background: repeating-linear-gradient(
    0deg,
    transparent,
    transparent 2px,
    rgba(0, 0, 0, 0.06) 2px,
    rgba(0, 0, 0, 0.06) 4px
  );
  z-index: 50;
}

.wrap {
  width: min(920px, 100%);
  padding: 1.25rem 1rem 1.5rem;
}

.top {
  text-align: center;
  margin-bottom: 1rem;
}
.top h1 {
  margin: 0;
  font-size: clamp(1.1rem, 3vw, 1.6rem);
  letter-spacing: 0.2em;
  color: var(--accent);
  text-shadow: 0 0 18px rgba(94, 200, 255, 0.35);
}
.sub {
  margin: 0.55rem 0 0;
  font-size: 0.45rem;
  color: var(--dim);
  letter-spacing: 0.08em;
  line-height: 1.6;
}

.stage {
  display: grid;
  grid-template-columns: 140px minmax(0, 300px) 140px;
  gap: 0.85rem;
  justify-content: center;
  align-items: start;
}

.panel {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.box {
  background: var(--panel);
  border: 3px solid var(--border);
  border-radius: 4px;
  padding: 0.65rem 0.55rem;
  box-shadow: inset 0 0 0 1px rgba(255,255,255,0.03);
}
.box h2 {
  margin: 0 0 0.5rem;
  font-size: 0.5rem;
  color: var(--dim);
  letter-spacing: 0.12em;
  text-align: center;
}
.box canvas {
  display: block;
  margin: 0 auto;
  image-rendering: pixelated;
  background: #0a0c10;
  border: 2px solid #1a222d;
}
.hint {
  margin: 0.4rem 0 0;
  font-size: 0.4rem;
  color: var(--dim);
  text-align: center;
  line-height: 1.5;
}

.stats {
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
}
.stats > div {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}
.stats .label {
  font-size: 0.4rem;
  color: var(--dim);
  letter-spacing: 0.1em;
}
.stats span:last-child {
  font-size: 0.65rem;
  color: var(--text);
  word-break: break-all;
}

.board-wrap {
  position: relative;
  width: 300px;
  justify-self: center;
  border: 4px solid #3a4a5c;
  border-radius: 4px;
  box-shadow:
    0 0 0 2px #0a0c10,
    0 12px 40px rgba(0,0,0,0.45),
    inset 0 0 30px rgba(0,0,0,0.35);
  background: #05070a;
}
#board {
  display: block;
  width: 100%;
  height: auto;
  image-rendering: pixelated;
  background: #05070a;
}

.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(5, 7, 10, 0.82);
  padding: 0.75rem;
  z-index: 5;
}
.overlay.hidden { display: none; }
.overlay-card {
  text-align: center;
  max-width: 100%;
}
.overlay-card h2 {
  margin: 0 0 0.75rem;
  font-size: 0.85rem;
  color: var(--accent);
  letter-spacing: 0.12em;
  line-height: 1.4;
}
.overlay-card p {
  margin: 0 0 1rem;
  font-size: 0.48rem;
  color: var(--text);
  line-height: 1.7;
}
.controls {
  list-style: none;
  margin: 0;
  padding: 0;
  text-align: left;
  font-size: 0.4rem;
  color: var(--dim);
  line-height: 1.9;
}
.controls kbd {
  display: inline-block;
  min-width: 1.1rem;
  padding: 0.12rem 0.28rem;
  margin-right: 0.15rem;
  border: 1px solid #3a4a5c;
  border-radius: 2px;
  background: #151b24;
  color: var(--accent);
  font: inherit;
  font-size: 0.38rem;
  text-align: center;
}

.btn {
  width: 100%;
  font: inherit;
  font-size: 0.42rem;
  padding: 0.55rem 0.4rem;
  border: 2px solid #3a4a5c;
  border-radius: 3px;
  background: #151b24;
  color: var(--text);
  cursor: pointer;
  letter-spacing: 0.04em;
}
.btn:hover { border-color: var(--accent); color: var(--accent); }
.btn.muted {
  color: var(--danger);
  border-color: rgba(255, 90, 106, 0.45);
}
.btn-icon {
  width: 2.2rem;
  min-width: 2.2rem;
  padding: 0.4rem 0;
  font-size: 0.75rem;
}
.btn-ghost-lite {
  background: transparent;
  color: var(--dim);
}

/* Hall of fame */
.hall-list {
  list-style: none;
  margin: 0;
  padding: 0;
  font-size: 0.42rem;
  line-height: 1.75;
}
.hall-list li {
  display: grid;
  grid-template-columns: 1.1rem 2.2rem 1fr;
  gap: 0.25rem;
  color: var(--text);
  border-bottom: 1px solid rgba(255,255,255,0.04);
  padding: 0.12rem 0;
}
.hall-list li:last-child { border-bottom: none; }
.hall-list .rank { color: var(--dim); }
.hall-list .name { color: var(--accent); letter-spacing: 0.08em; }
.hall-list .pts { text-align: right; color: var(--text); }
.hall-list .empty,
.hall-list .muted { display: block; color: var(--dim); font-size: 0.38rem; }

/* Start menu */
.menu-card .controls { margin-top: 0.85rem; }
.menu-panel {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  align-items: stretch;
  margin: 0.5rem 0 0.25rem;
  width: min(240px, 100%);
  margin-left: auto;
  margin-right: auto;
}
.menu-panel.hidden { display: none !important; }
.menu-btn {
  width: 100%;
  font-size: 0.45rem;
  padding: 0.65rem 0.5rem;
  letter-spacing: 0.06em;
}
.menu-btn-cheat {
  border-color: rgba(255, 183, 77, 0.45);
  color: #ffb74d;
}
.menu-note { margin: 0.15rem 0 0; text-align: center; }
.menu-row {
  display: flex;
  gap: 0.4rem;
}
.menu-row .menu-btn { flex: 1; }
.cheat-warn {
  margin: 0 0 0.25rem;
  color: #ffb74d !important;
  font-size: 0.38rem !important;
  line-height: 1.6 !important;
}
.menu-level {
  margin: 0.25rem 0;
}

/* Level cheat (menu only) */
.level-cheat {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  position: relative;
  z-index: 2;
}
.level-cheat .btn-icon {
  width: 2.4rem;
  min-width: 2.4rem;
  height: 2.4rem;
  padding: 0;
  font-size: 1rem;
  line-height: 1;
  flex-shrink: 0;
  touch-action: manipulation;
  width: 2.4rem;
}
.start-level-val {
  min-width: 2.2rem;
  text-align: center;
  font-size: 0.85rem;
  color: var(--accent);
  user-select: none;
  touch-action: manipulation;
}

/* Initials entry — fixed fullscreen above gamepad (mobile + desktop) */
.initials-overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: grid;
  place-items: center;
  background: rgba(5, 7, 10, 0.94);
  padding:
    max(1rem, env(safe-area-inset-top, 0px))
    1rem
    max(1rem, env(safe-area-inset-bottom, 0px));
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  touch-action: manipulation;
}
.initials-overlay.hidden,
.initials-overlay[hidden] {
  display: none !important;
}
.initials-card {
  width: min(340px, 100%);
  text-align: center;
  background: var(--panel);
  border: 3px solid var(--border);
  border-radius: 8px;
  padding: 1.1rem 0.9rem 1.2rem;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.55);
}
.initials-card h2 {
  margin: 0 0 0.5rem;
  color: #ffd54a;
  font-size: 0.9rem;
  letter-spacing: 0.1em;
}
.initials-rank {
  margin: 0 0 0.35rem;
  font-size: 0.55rem;
  color: var(--accent);
}
.initials-score {
  margin: 0 0 0.75rem;
  font-size: 0.55rem;
  color: var(--text);
}
.initials-slots {
  display: flex;
  justify-content: center;
  gap: 0.65rem;
  margin: 0.85rem 0 0.5rem;
}
.ini-col {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.3rem;
}
.ini-spin {
  appearance: none;
  width: 3.2rem;
  height: 2.2rem;
  border: 2px solid #3a4a5c;
  border-radius: 8px;
  background: #151b24;
  color: var(--accent);
  font: inherit;
  font-size: 0.75rem;
  cursor: pointer;
  padding: 0;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
.ini-spin:active {
  border-color: var(--accent);
  background: #1a2430;
}
.ini-slot {
  width: 3.2rem;
  height: 3.4rem;
  border: 2px solid #3a4a5c;
  border-radius: 8px;
  background: #0a0c10;
  color: var(--accent);
  font: inherit;
  font-size: 1.25rem;
  cursor: pointer;
  padding: 0;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
.ini-slot.active {
  border-color: #ffd54a;
  color: #ffd54a;
  box-shadow: 0 0 12px rgba(255, 213, 74, 0.25);
}
.ini-help { margin-top: 0.25rem; }
.ini-actions {
  display: flex;
  gap: 0.45rem;
  justify-content: center;
  margin-top: 0.85rem;
}
.ini-actions .btn {
  width: 100%;
  max-width: 18rem;
  min-height: 3rem;
  font-size: 0.48rem;
  touch-action: manipulation;
}
.ini-required {
  margin: 0.55rem 0 0 !important;
  color: #ffb74d !important;
  font-size: 0.36rem !important;
}
.ini-err {
  margin: 0.55rem 0 0;
  font-size: 0.4rem;
  color: var(--danger);
  line-height: 1.5;
}
/* Hide on-screen gamepad while claiming a high score */
body.hof-entry .gamepad {
  display: none !important;
  visibility: hidden !important;
  pointer-events: none !important;
}
.overlay-extra {
  margin: 0 0 0.75rem;
  font-size: 0.42rem;
  color: var(--dim);
  line-height: 1.6;
}
.overlay-extra .start-lv {
  color: #ffb74d;
}

.foot {
  margin-top: 1rem;
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  font-size: 0.4rem;
  color: var(--dim);
  letter-spacing: 0.04em;
}
#status { color: var(--accent); }

.mobile-only { display: none !important; }
.mobile-hud { display: none; }
.gamepad { display: none; }

/* —— Mobile game shell (no scroll, fixed gamepad) —— */
.gamepad {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 60;
  display: none;
  grid-template-columns: 1.15fr 0.7fr 1.15fr;
  gap: 0.4rem;
  align-items: stretch;
  /* Height is content-driven; JS measures it into --gp-h for board layout */
  min-height: 132px;
  height: auto;
  padding:
    0.45rem 0.55rem
    calc(0.55rem + env(safe-area-inset-bottom, 0px))
    0.55rem;
  background: #0a0c10;
  border-top: 1px solid #1e2836;
  box-shadow: 0 -8px 24px rgba(0, 0, 0, 0.45);
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
  -webkit-touch-callout: none;
}
.gamepad[hidden] { display: none !important; }
body.is-mobile .gamepad:not([hidden]) {
  display: grid;
}

.gp-zone {
  display: grid;
  gap: 0.35rem;
  min-height: 0;
}
.gp-move {
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
}
.gp-move .gp-left { grid-column: 1; grid-row: 1; }
.gp-move .gp-right { grid-column: 2; grid-row: 1; }
.gp-move .gp-soft { grid-column: 1 / -1; grid-row: 2; }

.gp-util {
  grid-template-rows: 1fr 1fr 1fr;
}
.gp-action {
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1.35fr 1fr;
}
.gp-action .gp-rot-main {
  grid-column: 1 / -1;
  grid-row: 1;
}
.gp-action .gp-rot-sec { grid-column: 1; grid-row: 2; }
.gp-action .gp-hard { grid-column: 2; grid-row: 2; }

.gp-btn {
  appearance: none;
  -webkit-appearance: none;
  margin: 0;
  border: 2px solid #2e3c4f;
  border-radius: 14px;
  background: linear-gradient(180deg, #1c2533, #121820);
  color: var(--text);
  font: inherit;
  font-size: 1.05rem;
  font-weight: 600;
  min-height: 0;
  min-width: 0;
  padding: 0;
  cursor: pointer;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
  box-shadow: 0 3px 0 #070a0e, inset 0 1px 0 rgba(255,255,255,0.06);
  display: grid;
  place-items: center;
  user-select: none;
}
.gp-btn:active,
.gp-btn.is-down {
  transform: translateY(2px);
  box-shadow: 0 1px 0 #070a0e;
  border-color: var(--accent);
  color: var(--accent);
  background: #182230;
}
.gp-sm { font-size: 0.55rem; letter-spacing: 0.04em; }
.gp-rot-main {
  font-size: 1.65rem;
  border-color: rgba(94, 200, 255, 0.5);
  color: var(--accent);
}
.gp-hard {
  font-size: 0.48rem;
  letter-spacing: 0.1em;
  color: #ffb74d;
  border-color: rgba(255, 183, 77, 0.45);
}
.gp-soft {
  font-size: 1.1rem;
}

.mobile-hud {
  display: none;
  width: min(var(--board-w, 300px), 100%);
  margin: 0 auto 0.35rem;
  grid-template-columns: repeat(3, 1fr) auto auto;
  gap: 0.25rem 0.35rem;
  align-items: center;
  padding: 0.3rem 0.35rem;
  background: var(--panel);
  border: 2px solid var(--border);
  border-radius: 6px;
}
.mh-stat {
  display: flex;
  flex-direction: column;
  gap: 0.12rem;
  min-width: 0;
}
.mh-stat .label,
.mh-mini .label {
  font-size: 0.32rem;
  color: var(--dim);
  letter-spacing: 0.08em;
}
.mh-stat span:last-child {
  font-size: 0.52rem;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
}
.mh-mini {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.1rem;
}
.mh-mini canvas {
  display: block;
  width: 40px;
  height: 40px;
  image-rendering: pixelated;
  background: #05070a;
  border: 1px solid #1a222d;
}

/*
 * Mobile shell ONLY when body.is-mobile is set by JS (touch-primary devices).
 * Desktop mouse/trackpad never gets the gamepad or mobile chrome.
 */
body.is-mobile .mobile-only { display: block !important; }
body.is-mobile .desktop-only { display: none !important; }

body.is-mobile {
  height: 100%;
  height: 100dvh;
  max-height: 100dvh;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;
  -webkit-text-size-adjust: 100%;
  position: fixed;
  inset: 0;
  width: 100%;
  display: block;
  background: var(--bg);
}
body.is-mobile .crt {
  min-height: 0;
  height: 100%;
  place-items: stretch;
}
body.is-mobile .crt::after { z-index: 30; }
body.is-mobile .wrap {
  width: 100%;
  height: 100%;
  max-width: none;
  /* Reserve exact measured gamepad height so the board never sits under it */
  padding:
    calc(0.35rem + env(safe-area-inset-top, 0px))
    0.5rem
    calc(var(--gp-h, 160px) + 0.5rem)
    0.5rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  overflow: hidden;
  box-sizing: border-box;
}
body.is-mobile .top {
  margin: 0 0 0.25rem;
  flex-shrink: 0;
}
body.is-mobile .top h1 {
  font-size: 0.85rem;
  letter-spacing: 0.16em;
}
body.is-mobile .mobile-hud {
  display: grid;
  flex-shrink: 0;
}
body.is-mobile .stage {
  flex: 1 1 auto;
  min-height: 0;
  max-height: var(--play-h, 50dvh);
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  gap: 0;
  overflow: hidden;
}
body.is-mobile .board-wrap {
  width: var(--board-w, min(92vw, 280px));
  height: var(--board-h, auto);
  max-width: 100%;
  max-height: 100%;
  border-width: 3px;
  box-shadow: 0 0 0 1px #0a0c10, 0 8px 24px rgba(0,0,0,0.4);
  flex-shrink: 0;
}
body.is-mobile #board {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: contain;
  touch-action: none;
}
body.is-mobile .overlay {
  padding: 0.5rem;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
}
body.is-mobile .overlay-card p { font-size: 0.48rem; }
body.is-mobile .menu-panel { width: min(260px, 100%); }
body.is-mobile .menu-btn {
  min-height: 2.75rem;
  font-size: 0.5rem;
}
body.is-mobile .ini-slot {
  width: 3rem;
  height: 3.2rem;
  font-size: 1.15rem;
}
body.is-mobile .ini-actions .btn {
  min-height: 2.6rem;
  min-width: 6rem;
}
body.is-mobile .menu-gestures {
  display: block !important;
  margin-top: 0.65rem;
  font-size: 0.36rem;
  line-height: 1.55;
  color: var(--dim);
}
body.is-mobile .mobile-hall {
  display: block !important;
  margin-top: 0.55rem;
  text-align: left;
  max-height: 7.5rem;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
  border-top: 1px solid var(--border);
  padding-top: 0.45rem;
}
body.is-mobile .mh-title {
  margin: 0 0 0.3rem;
  font-size: 0.38rem;
  color: var(--dim);
  letter-spacing: 0.1em;
  text-align: center;
}
body.is-mobile .mobile-hall .hall-list {
  font-size: 0.38rem;
}

/* Landscape phones only (still requires body.is-mobile) */
@media (orientation: landscape) {
  body.is-mobile .wrap {
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: center;
    align-content: center;
    gap: 0.35rem;
  }
  body.is-mobile .top { width: 100%; }
  body.is-mobile .mobile-hud { width: auto; }
}
