* {
  box-sizing: border-box;
}

:root {
  --bg0: #07121f;
  --bg1: #0e1c30;
  --panel: #122338;
  --ink: #eaf2ff;
  --muted: #7f93ae;
  --accent: #5eead4;
  --accent2: #fbbf24;
  --danger: #fb7185;
  --line: #243750;
  --pad-h: 0px;
}

html {
  margin: 0;
  height: 100%;
  /* 允许整页上下滑动 */
  touch-action: pan-y;
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100%;
  background:
    radial-gradient(ellipse 65% 40% at 20% -5%, #16324f 0%, transparent 55%),
    radial-gradient(ellipse 50% 35% at 90% 100%, #1a2a22 0%, transparent 45%),
    linear-gradient(180deg, var(--bg0), var(--bg1));
  color: var(--ink);
  font-family: "Avenir Next", "PingFang SC", "Hiragino Sans GB", sans-serif;
  touch-action: pan-y;
  overscroll-behavior-y: contain;
  user-select: none;
  -webkit-user-select: none;
}

#wrap {
  max-width: 640px;
  margin: 0 auto;
  padding: 1.1rem 0.85rem calc(1.75rem + var(--pad-h));
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.65rem;
}

.top {
  text-align: center;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 0.75rem;
  text-align: left;
}

.brand-logo {
  width: 48px;
  height: 48px;
  border-radius: 8px;
  flex-shrink: 0;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.35);
}

.top h1 {
  margin: 0;
  font-size: clamp(1.45rem, 4vw, 1.9rem);
  letter-spacing: 0.06em;
  font-weight: 800;
}

.subtitle {
  margin: 0.3rem 0 0;
  color: var(--muted);
  font-size: 0.88rem;
}

#stage {
  display: flex;
  gap: 0.75rem;
  align-items: stretch;
  width: 100%;
  justify-content: center;
}

.hud {
  width: 110px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  padding: 0.7rem 0.5rem;
  background: rgba(0, 0, 0, 0.35);
  border: 2px solid var(--line);
  border-radius: 6px;
}

.hud-block {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
}

.hud-label {
  font-size: 0.65rem;
  letter-spacing: 0.14em;
  color: var(--muted);
  text-transform: uppercase;
}

.level-num,
.score,
.lines,
.next-goal {
  font-size: 1.25rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: var(--accent);
}

.level-name {
  font-size: 0.72rem;
  color: var(--accent2);
}

.next-block canvas {
  display: block;
  width: 96px;
  height: 96px;
  margin-top: 0.25rem;
  background: rgba(0, 0, 0, 0.35);
  border: 1px solid var(--line);
  border-radius: 4px;
}

.canvas-wrap {
  position: relative;
  flex-shrink: 0;
  border: 3px solid var(--line);
  border-radius: 6px;
  overflow: hidden;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
  background: #050b14;
  touch-action: none;
}

#game {
  display: block;
  width: 300px;
  height: 600px;
  max-width: min(300px, 72vw);
  max-height: min(600px, 72vh);
  image-rendering: pixelated;
  touch-action: none;
}

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(4, 10, 18, 0.82);
  backdrop-filter: blur(2px);
}

.overlay[hidden],
.panel[hidden] {
  display: none !important;
}

.panel {
  width: min(92%, 260px);
  padding: 1.25rem 1.1rem;
  text-align: center;
  background: var(--panel);
  border: 2px solid var(--line);
  border-radius: 10px;
  max-height: 90%;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
}

.panel h2 {
  margin: 0 0 0.5rem;
  font-size: 1.25rem;
}

.panel-desc {
  margin: 0 0 1rem;
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.45;
}

.controls-help {
  margin-top: 0.9rem;
  text-align: left;
  font-size: 0.75rem;
  color: var(--muted);
  line-height: 1.55;
}

.controls-help p {
  margin: 0.15rem 0;
}

.controls-help strong {
  color: var(--accent);
}

.end-score {
  margin: 0.35rem 0 1rem;
  color: var(--accent2);
  font-weight: 700;
}

.btn {
  display: block;
  width: 100%;
  margin: 0.4rem 0;
  padding: 0.65rem 0.8rem;
  border: 2px solid transparent;
  border-radius: 6px;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}

.btn-primary {
  background: var(--accent);
  color: #04201a;
}

.btn-secondary {
  background: transparent;
  border-color: var(--accent2);
  color: var(--accent2);
}

.btn-ghost {
  background: transparent;
  border-color: var(--line);
  color: var(--muted);
}

.toast {
  position: absolute;
  left: 50%;
  top: 18%;
  transform: translateX(-50%);
  padding: 0.4rem 0.85rem;
  background: rgba(94, 234, 212, 0.92);
  color: #04201a;
  font-weight: 800;
  font-size: 0.95rem;
  border-radius: 999px;
  pointer-events: none;
  animation: toast-pop 0.7s ease-out forwards;
}

.toast[hidden] {
  display: none !important;
}

@keyframes toast-pop {
  0% { opacity: 0; transform: translate(-50%, 8px) scale(0.9); }
  20% { opacity: 1; transform: translate(-50%, 0) scale(1); }
  100% { opacity: 0; transform: translate(-50%, -12px) scale(1); }
}

.hint {
  margin: 0.4rem 0 0;
  font-size: 0.78rem;
  color: var(--muted);
  text-align: center;
}

.hint a {
  color: var(--accent);
}

.hint-touch {
  display: none;
}

.help-touch {
  display: none;
}

.touch-pad {
  display: none;
  width: min(100%, 360px);
  margin-top: 0.35rem;
  justify-content: space-between;
  align-items: flex-end;
  gap: 1rem;
  padding: 0.25rem 0.15rem;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}

.pad-dirs {
  display: grid;
  grid-template-columns: repeat(3, 56px);
  gap: 0.4rem;
}

.pad-ab {
  display: flex;
  gap: 0.55rem;
  align-items: center;
}

.pad-btn {
  width: 56px;
  height: 56px;
  border: 2px solid var(--line);
  border-radius: 14px;
  background: rgba(18, 35, 56, 0.92);
  color: var(--ink);
  font-size: 1.05rem;
  font-weight: 800;
  font-family: inherit;
  box-shadow: 0 4px 0 #0a1420;
  -webkit-tap-highlight-color: transparent;
  touch-action: none;
}

.pad-btn:active,
.pad-btn.is-down {
  transform: translateY(2px);
  box-shadow: 0 2px 0 #0a1420;
  background: rgba(36, 58, 86, 0.98);
}

.pad-a {
  background: linear-gradient(160deg, #5eead4, #2dd4bf);
  color: #04201a;
  border-color: #99f6e4;
}

.pad-b {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  background: linear-gradient(160deg, #fbbf24, #f59e0b);
  color: #3b2205;
  border-color: #fde68a;
  font-size: 1.25rem;
}

@media (max-width: 520px) {
  #stage {
    flex-direction: column;
    align-items: center;
  }

  .hud {
    width: 100%;
    max-width: 300px;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: space-between;
    gap: 0.45rem;
    padding: 0.5rem 0.55rem;
  }

  .hud-block {
    min-width: 56px;
  }

  .next-block {
    width: auto;
    align-items: flex-start;
  }

  .next-block canvas {
    width: 48px;
    height: 48px;
  }
}

/* 手机 / 触控：底部固定虚拟键，棋盘缩小保证同屏可见 */
@media (hover: none), (max-width: 820px) {
  :root {
    --pad-h: 96px;
  }

  body.is-playing {
    --pad-h: 108px;
  }

  .touch-pad:not([hidden]) {
    display: flex;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 40;
    width: 100%;
    max-width: none;
    margin: 0;
    padding: 0.55rem 1rem calc(0.55rem + env(safe-area-inset-bottom, 0));
    justify-content: space-between;
    align-items: center;
    background: linear-gradient(180deg, transparent, rgba(5, 12, 22, 0.92) 28%, rgba(5, 12, 22, 0.98));
    box-shadow: 0 -8px 24px rgba(0, 0, 0, 0.35);
  }

  .hint-desk,
  .help-desk {
    display: none;
  }

  .hint-touch,
  .help-touch {
    display: inline;
  }

  .help-touch {
    display: block;
  }

  .top {
    padding-top: 0.15rem;
  }

  .brand-logo {
    width: 36px;
    height: 36px;
  }

  .top h1 {
    font-size: 1.15rem;
  }

  .subtitle {
    font-size: 0.72rem;
    margin-top: 0.1rem;
  }

  #wrap {
    padding: 0.55rem 0.65rem calc(0.75rem + var(--pad-h));
    gap: 0.4rem;
    min-height: 100%;
  }

  #game {
    width: min(280px, 78vw);
    height: auto;
    max-width: min(280px, 78vw);
    max-height: calc(100dvh - 220px - var(--pad-h));
    aspect-ratio: 1 / 2;
  }

  .hint {
    font-size: 0.7rem;
    padding-bottom: 0.25rem;
  }
}
