:root {
  --green: #7dc500;
  --green-hot: #b6ff2a;
  --bg: #070807;
  --panel: #111111;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  background: var(--bg);
  color: #fff;
  font-family: "Segoe UI", system-ui, sans-serif;
  overflow: hidden;
  touch-action: manipulation;
  user-select: none;
}

#layout {
  height: 100%;
  display: flex;
  align-items: stretch;
  justify-content: center;
  gap: 18px;
  padding: 12px 16px;
}

#wrap {
  position: relative;
  width: min(100%, calc((100vh - 24px) * 480 / 720));
  height: min(100%, calc(100vw * 720 / 480));
  max-height: calc(100vh - 24px);
  flex: 0 0 auto;
}

#game {
  display: block;
  width: 100%;
  height: 100%;
  background: #070807;
}

#hud {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.hud-top {
  display: flex;
  justify-content: space-between;
  padding: 14px 18px 0;
  font-variant-numeric: tabular-nums;
}

.hud-top .label {
  display: block;
  font-size: 11px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: #8a8a8a;
}

.hud-top strong {
  font-size: 28px;
  line-height: 1.1;
  color: var(--green);
}

.mute-btn {
  pointer-events: auto;
  align-self: flex-start;
  margin-top: 4px;
  border: 1px solid #2a2a2a;
  background: rgba(17, 17, 17, 0.85);
  color: var(--green);
  font: 600 12px "Segoe UI", system-ui, sans-serif;
  letter-spacing: 0.04em;
  padding: 6px 10px;
  border-radius: 8px;
  cursor: pointer;
}
.mute-btn:hover {
  border-color: var(--green);
  color: var(--green-hot);
}
.mute-btn[aria-pressed="true"] {
  color: #8a8a8a;
}

#board {
  width: 240px;
  flex: 0 0 240px;
  background: var(--panel);
  border: 1px solid #2a2a2a;
  border-radius: 12px;
  padding: 16px 14px;
  align-self: center;
  max-height: calc(100vh - 24px);
  overflow: auto;
}

#board h2 {
  color: var(--green);
  font-size: 14px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  margin-bottom: 14px;
}

#board-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

#board-list li {
  display: grid;
  grid-template-columns: 28px 1fr auto;
  gap: 8px;
  align-items: center;
  padding: 8px 10px;
  background: #0c0c0c;
  border: 1px solid #222;
  border-radius: 8px;
  font-variant-numeric: tabular-nums;
}

#board-list .rank {
  color: #8a8a8a;
  font-weight: 700;
  font-size: 13px;
}

#board-list .name {
  color: #fff;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#board-list .pts {
  color: var(--green);
  font-weight: 800;
}

#board-list li.top1 {
  border-color: var(--green);
}

.board-empty {
  color: #8a8a8a;
  font-size: 13px;
  line-height: 1.4;
}

.board-empty[hidden],
#board-list:not(:empty) + .board-empty {
  display: none;
}

.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(7, 8, 7, 0.72);
  backdrop-filter: blur(4px);
}

.overlay.hidden { display: none; }

.panel {
  text-align: center;
  padding: 28px 24px 24px;
  background: var(--panel);
  border: 1px solid var(--green);
  border-radius: 12px;
  width: min(90%, 360px);
  box-shadow: 0 0 40px rgba(125, 197, 0, 0.18);
  overflow: hidden;
}

.brand {
  color: var(--green);
  letter-spacing: 0.22em;
  font-size: 12px;
  text-transform: uppercase;
  margin-bottom: 6px;
}

.panel h1 {
  font-size: clamp(36px, 11vw, 48px);
  letter-spacing: 0.02em;
  line-height: 1;
  color: #fff;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

.tag {
  margin-top: 10px;
  color: var(--green-hot);
  font-weight: 600;
}

.hint {
  margin: 16px 0 20px;
  color: #cfcfcf;
  font-size: 14px;
  line-height: 1.45;
}

kbd {
  display: inline-block;
  padding: 1px 6px;
  border: 1px solid #444;
  border-radius: 4px;
  background: #1a1a1a;
  font-size: 12px;
}

.overlay.review {
  align-items: start;
  padding-top: 48px;
  background: linear-gradient(
    to bottom,
    rgba(7, 8, 7, 0.9) 0%,
    rgba(7, 8, 7, 0.45) 32%,
    rgba(7, 8, 7, 0.08) 58%,
    transparent 100%
  );
  backdrop-filter: none;
}

.overlay.review .panel {
  padding: 16px 20px 14px;
}

.overlay.review .panel h1 {
  font-size: 42px;
}

.save-box {
  margin: 0 0 14px;
  text-align: left;
}

.save-box label {
  display: block;
  font-size: 12px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: #8a8a8a;
  margin-bottom: 6px;
}

.save-box input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid #333;
  border-radius: 8px;
  background: #0c0c0c;
  color: #fff;
  font-size: 16px;
  outline: none;
}

.save-box input:focus {
  border-color: var(--green);
}

#save {
  margin-top: 8px;
  width: 100%;
  cursor: pointer;
  border: none;
  background: #1f1f1f;
  color: var(--green);
  border: 1px solid var(--green);
  font-weight: 800;
  font-size: 14px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  padding: 10px 14px;
  border-radius: 8px;
}

#save:hover { background: rgba(125, 197, 0, 0.16); }
#save:disabled {
  opacity: 0.45;
  cursor: default;
}

.save-msg {
  margin-top: 8px;
  color: var(--green-hot);
  font-size: 13px;
  text-align: center;
}

.actions {
  display: flex;
  gap: 10px;
  justify-content: center;
  flex-wrap: wrap;
}

#play {
  cursor: pointer;
  border: none;
  background: var(--green);
  color: #0a0a0a;
  font-weight: 800;
  font-size: 18px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  padding: 12px 28px;
  border-radius: 8px;
}

#play:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  background: #444;
  color: #aaa;
}

#play:disabled:hover {
  background: #444;
}

.secondary {
  cursor: pointer;
  border: 1px solid var(--green);
  background: transparent;
  color: var(--green);
  font-weight: 800;
  font-size: 16px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  padding: 11px 20px;
  border-radius: 8px;
}

.secondary:hover {
  background: rgba(125, 197, 0, 0.16);
}

.best-line {
  margin-top: 14px;
  color: #8a8a8a;
  font-size: 13px;
}

.touch-controls {
  display: none;
  position: relative;
  width: min(100%, 420px);
  height: 118px;
  flex: 0 0 auto;
  z-index: 5;
  pointer-events: none;
  background: rgba(17, 17, 17, 0.55);
  border: 1px solid #2a2a2a;
  border-radius: 12px;
  margin-top: 2px;
}

.touch-action {
  pointer-events: auto;
  position: absolute;
  left: max(14px, env(safe-area-inset-left));
  top: 50%;
  transform: translateY(-50%);
  width: 72px;
  height: 72px;
  border-radius: 50%;
  border: 2px solid rgba(125, 197, 0, 0.65);
  background: rgba(17, 17, 17, 0.75);
  color: rgba(182, 255, 42, 0.95);
  font: 800 22px "Segoe UI", system-ui, sans-serif;
  letter-spacing: 0.04em;
  opacity: 0.9;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.25);
}

.touch-action:active {
  opacity: 1;
  background: rgba(125, 197, 0, 0.35);
}

.touch-dpad {
  pointer-events: auto;
  position: absolute;
  right: max(10px, env(safe-area-inset-right));
  top: 50%;
  transform: translateY(-50%);
  width: 126px;
  height: 126px;
  display: grid;
  grid-template-columns: 40px 40px 40px;
  grid-template-rows: 40px 40px 40px;
  gap: 3px;
  place-content: center;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
}

.touch-dir {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  border: 2px solid rgba(125, 197, 0, 0.6);
  background: rgba(17, 17, 17, 0.8);
  color: rgba(182, 255, 42, 0.95);
  font: 700 14px "Segoe UI", system-ui, sans-serif;
  padding: 0;
  line-height: 1;
  opacity: 0.9;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

.touch-dir:active,
.touch-dir.is-held {
  opacity: 1;
  background: rgba(125, 197, 0, 0.4);
}

.touch-dir[data-key="ArrowUp"] { grid-column: 2; grid-row: 1; }
.touch-dir[data-key="ArrowLeft"] { grid-column: 1; grid-row: 2; }
.touch-dir[data-key="ArrowRight"] { grid-column: 3; grid-row: 2; }
.touch-dir[data-key="ArrowDown"] { grid-column: 2; grid-row: 3; }

@media (pointer: coarse), (hover: none) and (max-width: 900px) {
  .touch-controls.is-on {
    display: block;
  }
}

@media (max-width: 780px) {
  #layout {
    flex-direction: column;
    align-items: center;
    overflow: auto;
    gap: 10px;
  }
  #board {
    display: none;
    width: min(480px, 100%);
    flex: 0 0 auto;
    align-self: stretch;
    max-height: min(42vh, 320px);
  }
  #board.is-on {
    display: block;
  }
  #wrap {
    width: min(100%, 420px);
    height: auto;
    aspect-ratio: 480 / 720;
  }

  .touch-controls {
    width: min(100%, 420px);
    height: 124px;
  }

  .touch-action {
    width: 68px;
    height: 68px;
    font-size: 20px;
  }

  .touch-dpad {
    width: 120px;
    height: 120px;
  }
}
