:root {
  --orange: #FF8A3D;
  --orange-hi: #FFB067;
  --orange-deep: #FF6B24;
  --cyan: #28E7FF;
  --magenta: #FF3FD8;
  --danger: #FF3D67;
  --ink: #06040F;
  --text: #F4F1FF;
  --muted: #8C86A6;
  --mono: "SFMono-Regular", "Roboto Mono", "JetBrains Mono", Menlo, Consolas, monospace;
  --ui: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: #05030c;
  color: var(--text);
  font-family: var(--ui);
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  -webkit-tap-highlight-color: transparent;
}

button {
  font-family: inherit;
  -webkit-appearance: none;
  appearance: none;
  cursor: pointer;
}

.app {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 520px;
  height: 100vh;
  height: 100dvh;
  margin: 0 auto;
  padding: max(8px, env(safe-area-inset-top)) clamp(12px, 4vw, 20px) max(10px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  isolation: isolate;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

/* ---------------- background ---------------- */

.bg {
  position: fixed;
  inset: 0;
  z-index: 0;
  overflow: hidden;
  pointer-events: none;
  background:
    radial-gradient(120% 65% at 50% -14%, rgba(255, 138, 61, 0.20), transparent 58%),
    radial-gradient(90% 60% at 0% 108%, rgba(255, 63, 216, 0.16), transparent 60%),
    radial-gradient(90% 58% at 100% 86%, rgba(40, 231, 255, 0.13), transparent 62%),
    linear-gradient(180deg, #080512 0%, #070510 46%, #04030A 100%);
}

.bg-glow {
  position: absolute;
  border-radius: 50%;
  filter: blur(52px);
  opacity: 0.55;
  will-change: transform;
}

.glow-a {
  width: 52vw;
  height: 52vw;
  max-width: 280px;
  max-height: 280px;
  top: -14%;
  left: -20%;
  background: rgba(255, 138, 61, 0.28);
  animation: driftA 16s ease-in-out infinite alternate;
}

.glow-b {
  width: 46vw;
  height: 46vw;
  max-width: 250px;
  max-height: 250px;
  bottom: 4%;
  right: -20%;
  background: rgba(40, 231, 255, 0.20);
  animation: driftB 19s ease-in-out infinite alternate;
}

.glow-c {
  width: 34vw;
  height: 34vw;
  max-width: 190px;
  max-height: 190px;
  top: 46%;
  left: 58%;
  background: rgba(255, 63, 216, 0.16);
  animation: driftA 22s ease-in-out infinite alternate-reverse;
}

@keyframes driftA {
  from { transform: translate3d(0, 0, 0) scale(1); }
  to { transform: translate3d(16px, 26px, 0) scale(1.12); }
}

@keyframes driftB {
  from { transform: translate3d(0, 0, 0) scale(1.05); }
  to { transform: translate3d(-22px, -18px, 0) scale(0.94); }
}

.bg-grid {
  position: absolute;
  left: -45%;
  right: -45%;
  bottom: -16%;
  height: 64%;
  background-image:
    linear-gradient(rgba(40, 231, 255, 0.20) 1px, transparent 1px),
    linear-gradient(90deg, rgba(40, 231, 255, 0.20) 1px, transparent 1px);
  background-size: 42px 42px;
  transform: perspective(300px) rotateX(63deg);
  transform-origin: 50% 100%;
  -webkit-mask-image: linear-gradient(to top, #000 0%, transparent 76%);
  mask-image: linear-gradient(to top, #000 0%, transparent 76%);
  opacity: 0.6;
}

.bg-horizon {
  position: absolute;
  left: -10%;
  right: -10%;
  bottom: 26%;
  height: 1px;
  background: linear-gradient(90deg, transparent, rgba(255, 138, 61, 0.45), rgba(40, 231, 255, 0.5), transparent);
  box-shadow: 0 0 16px rgba(255, 138, 61, 0.35);
  opacity: 0.7;
}

.bg-scan {
  position: absolute;
  inset: 0;
  background: repeating-linear-gradient(
    0deg,
    rgba(255, 255, 255, 0.035) 0px,
    rgba(255, 255, 255, 0.035) 1px,
    transparent 1px,
    transparent 3px
  );
  opacity: 0.30;
  mix-blend-mode: overlay;
}

/* ---------------- HUD ---------------- */

.hud {
  position: relative;
  z-index: 1;
  flex: 0 0 auto;
  display: flex;
  align-items: stretch;
  gap: 10px;
  padding: 9px 12px 10px;
  border-radius: 17px;
  border: 1px solid rgba(40, 231, 255, 0.18);
  background: linear-gradient(135deg, rgba(22, 17, 42, 0.92), rgba(8, 6, 20, 0.82));
  box-shadow:
    0 12px 28px rgba(0, 0, 0, 0.5),
    0 0 22px rgba(255, 138, 61, 0.06),
    inset 0 1px 0 rgba(255, 255, 255, 0.06);
  overflow: hidden;
}

.hud::before {
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 3px;
  background: linear-gradient(180deg, var(--orange), transparent 72%);
  box-shadow: 0 0 10px rgba(255, 138, 61, 0.6);
}

.hud::after {
  content: "";
  position: absolute;
  left: 12px;
  right: 12px;
  bottom: 0;
  height: 1px;
  background: linear-gradient(90deg, transparent, rgba(255, 138, 61, 0.75), rgba(40, 231, 255, 0.45), transparent);
}

.hud-score {
  flex: 1 1 auto;
  min-width: 0;
  padding-left: 4px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.eyebrow {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 9px;
  font-weight: 700;
  letter-spacing: 2.2px;
  color: rgba(40, 231, 255, 0.78);
  line-height: 1;
}

.live-dot {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--orange);
  box-shadow: 0 0 7px var(--orange);
  animation: livePulse 1.8s ease-in-out infinite;
}

@keyframes livePulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.35; transform: scale(0.7); }
}

.score {
  margin: 5px 0 4px;
  font-family: var(--mono);
  font-size: clamp(26px, 8.2vw, 35px);
  font-weight: 800;
  line-height: 0.94;
  letter-spacing: 0.5px;
  color: #FFF7EE;
  text-shadow:
    0 0 14px rgba(255, 138, 61, 0.55),
    0 0 30px rgba(255, 138, 61, 0.22);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.hud-meta {
  display: flex;
  overflow: hidden;
  align-items: center;
  gap: 6px;
  font-family: var(--mono);
  font-size: 9.5px;
  letter-spacing: 0.6px;
  color: rgba(180, 174, 204, 0.7);
  white-space: nowrap;
}

.hud-meta b {
  color: #E9E4FF;
  font-weight: 700;
}

.hud-meta .sep {
  width: 1px;
  height: 8px;
  background: rgba(140, 134, 166, 0.35);
}

.next {
  flex: 0 0 62px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 5px;
  padding-left: 9px;
  border-left: 1px solid rgba(40, 231, 255, 0.13);
}

.next-box {
  width: 52px;
  height: 46px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 9px;
  border: 1px solid rgba(40, 231, 255, 0.16);
  background:
    linear-gradient(rgba(40, 231, 255, 0.05), rgba(40, 231, 255, 0) 70%),
    rgba(4, 3, 12, 0.7);
  box-shadow: inset 0 0 14px rgba(40, 231, 255, 0.07);
}

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

/* ---------------- board ---------------- */

.stage {
  position: relative;
  z-index: 1;
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 9px 0;
}

.board-frame {
  position: relative;
  padding: 7px;
  border-radius: 18px;
  background: linear-gradient(
    155deg,
    rgba(255, 138, 61, 0.95) 0%,
    rgba(255, 138, 61, 0.28) 26%,
    rgba(40, 231, 255, 0.42) 58%,
    rgba(255, 63, 216, 0.72) 100%
  );
  box-shadow:
    0 0 34px rgba(255, 138, 61, 0.18),
    0 22px 46px rgba(0, 0, 0, 0.62);
  transition: box-shadow 0.35s ease;
}

.board-frame::before {
  content: "";
  position: absolute;
  inset: 1px;
  border-radius: 17px;
  background: #070611;
  box-shadow:
    inset 0 0 30px rgba(40, 231, 255, 0.10),
    inset 0 0 6px rgba(0, 0, 0, 0.9);
}

.board-frame::after {
  content: "";
  position: absolute;
  inset: 8px;
  border-radius: 11px;
  pointer-events: none;
  box-shadow: inset 0 0 36px rgba(0, 0, 0, 0.72);
  background: repeating-linear-gradient(
    0deg,
    rgba(255, 255, 255, 0.022) 0px,
    rgba(255, 255, 255, 0.022) 1px,
    transparent 1px,
    transparent 3px
  );
  z-index: 2;
}

#board {
  position: relative;
  z-index: 1;
  display: block;
  border-radius: 11px;
  background: #06050f;
}

.corner {
  position: absolute;
  width: 15px;
  height: 15px;
  z-index: 3;
  pointer-events: none;
  border-color: rgba(255, 168, 96, 0.95);
  filter: drop-shadow(0 0 5px rgba(255, 138, 61, 0.75));
}

.corner.tl { top: -2px; left: -2px; border-top: 2px solid; border-left: 2px solid; border-top-left-radius: 5px; }
.corner.tr { top: -2px; right: -2px; border-top: 2px solid; border-right: 2px solid; border-top-right-radius: 5px; }
.corner.bl { bottom: -2px; left: -2px; border-bottom: 2px solid; border-left: 2px solid; border-bottom-left-radius: 5px; }
.corner.br { bottom: -2px; right: -2px; border-bottom: 2px solid; border-right: 2px solid; border-bottom-right-radius: 5px; }

.board-frame.danger {
  animation: dangerPulse 1.25s ease-in-out infinite;
}

@keyframes dangerPulse {
  0%, 100% { box-shadow: 0 0 34px rgba(255, 138, 61, 0.16), 0 22px 46px rgba(0, 0, 0, 0.62); }
  50% { box-shadow: 0 0 40px rgba(255, 61, 103, 0.50), 0 0 76px rgba(255, 61, 103, 0.16), 0 22px 46px rgba(0, 0, 0, 0.62); }
}

.board-frame.shake {
  animation: boardShake 0.18s linear;
}

@keyframes boardShake {
  0% { transform: translate(0, 0); }
  25% { transform: translate(-2px, 1px); }
  50% { transform: translate(2px, -1px); }
  75% { transform: translate(-1px, 0); }
  100% { transform: translate(0, 0); }
}

/* ---------------- fx ---------------- */

.score-pop {
  position: absolute;
  left: 50%;
  top: 37%;
  z-index: 4;
  pointer-events: none;
  font-family: var(--mono);
  font-size: 21px;
  font-weight: 800;
  letter-spacing: 1px;
  color: #FFE2C8;
  text-shadow: 0 0 12px rgba(255, 138, 61, 0.95), 0 0 26px rgba(255, 138, 61, 0.45);
  white-space: nowrap;
  animation: floatUp 0.95s ease-out forwards;
}

@keyframes floatUp {
  0% { opacity: 0; transform: translate(-50%, 10px) scale(0.78); }
  9% { opacity: 1; transform: translate(-50%, 0) scale(1.08); }
  72% { opacity: 1; transform: translate(-50%, -44px) scale(1); }
  100% { opacity: 0; transform: translate(-50%, -78px) scale(0.95); }
}

.fx-toast {
  position: absolute;
  left: 50%;
  top: 41%;
  z-index: 4;
  pointer-events: none;
  transform: translate(-50%, -50%) scale(0.7);
  font-family: var(--mono);
  font-size: 26px;
  font-weight: 900;
  letter-spacing: 5px;
  color: #fff;
  opacity: 0;
  text-shadow:
    0 0 10px rgba(255, 63, 216, 0.95),
    0 0 26px rgba(255, 138, 61, 0.75),
    0 0 50px rgba(40, 231, 255, 0.5);
}

.fx-toast.show {
  animation: toastPop 0.92s cubic-bezier(0.22, 1, 0.36, 1) forwards;
}

@keyframes toastPop {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(0.55); letter-spacing: 2px; }
  22% { opacity: 1; transform: translate(-50%, -50%) scale(1.12); letter-spacing: 5px; }
  72% { opacity: 1; transform: translate(-50%, -50%) scale(1); }
  100% { opacity: 0; transform: translate(-50%, -62%) scale(0.98); }
}

/* ---------------- game over ---------------- */

.overlay {
  position: absolute;
  inset: 8px;
  z-index: 6;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  border-radius: 11px;
  background: rgba(5, 3, 13, 0.74);
  -webkit-backdrop-filter: blur(7px) saturate(0.8);
  backdrop-filter: blur(7px) saturate(0.8);
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.28s ease, visibility 0.28s ease;
}

.overlay.show {
  opacity: 1;
  visibility: visible;
}

.over-card {
  position: relative;
  width: 100%;
  max-width: 268px;
  padding: 20px 16px 16px;
  text-align: center;
  border-radius: 16px;
  border: 1px solid rgba(255, 138, 61, 0.5);
  background: linear-gradient(160deg, rgba(34, 18, 38, 0.97), rgba(9, 7, 20, 0.98));
  box-shadow:
    0 0 44px rgba(255, 138, 61, 0.20),
    0 18px 40px rgba(0, 0, 0, 0.55),
    inset 0 0 34px rgba(255, 63, 216, 0.07);
  overflow: hidden;
  transform: translateY(10px) scale(0.96);
  transition: transform 0.32s cubic-bezier(0.22, 1, 0.36, 1);
}

.overlay.show .over-card {
  transform: translateY(0) scale(1);
}

.over-card::before {
  content: "";
  position: absolute;
  top: 0;
  left: 15%;
  right: 15%;
  height: 1px;
  background: linear-gradient(90deg, transparent, var(--orange), var(--magenta), transparent);
  box-shadow: 0 0 12px rgba(255, 138, 61, 0.8);
}

.over-eyebrow {
  font-family: var(--mono);
  font-size: 9px;
  font-weight: 700;
  letter-spacing: 3.4px;
  color: rgba(255, 107, 157, 0.9);
}

.over-title {
  margin-top: 7px;
  font-size: 21px;
  font-weight: 800;
  letter-spacing: 4px;
  color: #FFF6EE;
  text-shadow: 0 0 18px rgba(255, 138, 61, 0.6);
}

.over-score {
  margin: 13px 0 3px;
  font-family: var(--mono);
  font-size: 38px;
  font-weight: 800;
  line-height: 1.05;
  color: var(--orange);
  text-shadow: 0 0 22px rgba(255, 138, 61, 0.55);
  font-variant-numeric: tabular-nums;
}

.over-sub {
  font-family: var(--mono);
  font-size: 10.5px;
  letter-spacing: 1px;
  color: rgba(185, 178, 208, 0.8);
}

.new-record {
  display: inline-block;
  margin-top: 10px;
  padding: 4px 10px;
  border-radius: 999px;
  border: 1px solid rgba(255, 210, 74, 0.6);
  background: rgba(255, 210, 74, 0.10);
  color: #FFD24A;
  font-family: var(--mono);
  font-size: 9px;
  font-weight: 700;
  letter-spacing: 2px;
  box-shadow: 0 0 14px rgba(255, 210, 74, 0.18);
}

.new-record[hidden] {
  display: none;
}

.restart {
  width: 100%;
  height: 48px;
  margin-top: 15px;
  border: 0;
  border-radius: 12px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  background: linear-gradient(150deg, #FFB067, #FF8A3D 45%, #FF6B24);
  color: #200D02;
  font-size: 15px;
  font-weight: 800;
  letter-spacing: 2px;
  box-shadow:
    0 9px 24px rgba(255, 138, 61, 0.36),
    inset 0 1px 0 rgba(255, 255, 255, 0.5);
  transition: transform 0.09s ease, filter 0.15s ease;
}

.restart .mp-icon {
  width: 20px;
  height: 20px;
  color: #200D02;
}

.restart:active {
  transform: scale(0.97);
  filter: brightness(1.08);
}

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

.deck {
  position: relative;
  z-index: 1;
  flex: 0 0 auto;
  padding-top: 2px;
}

.hint {
  margin: 0 0 8px;
  text-align: center;
  font-size: 10.5px;
  letter-spacing: 1.1px;
  color: rgba(178, 171, 202, 0.56);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.pad {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr 1.26fr;
  gap: 8px;
}

.key {
  position: relative;
  height: 62px;
  padding: 0 2px;
  border-radius: 15px;
  border: 1px solid rgba(40, 231, 255, 0.18);
  background: linear-gradient(180deg, rgba(25, 21, 46, 0.94), rgba(10, 8, 23, 0.94));
  color: #DCD8F2;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  box-shadow:
    0 7px 18px rgba(0, 0, 0, 0.38),
    inset 0 1px 0 rgba(255, 255, 255, 0.06);
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
  transition: transform 0.08s ease, box-shadow 0.16s ease, border-color 0.16s ease, background 0.16s ease;
  overflow: hidden;
}

.key::before {
  content: "";
  position: absolute;
  left: 18%;
  right: 18%;
  top: 0;
  height: 1px;
  background: linear-gradient(90deg, transparent, rgba(40, 231, 255, 0.45), transparent);
}

.key .mp-icon {
  width: 23px;
  height: 23px;
  color: #CFEBFF;
  filter: drop-shadow(0 0 5px rgba(40, 231, 255, 0.28));
}

.key span {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 1px;
  line-height: 1;
  color: rgba(208, 202, 232, 0.6);
}

.key:active,
.key.is-pressed {
  transform: translateY(2px) scale(0.965);
  border-color: rgba(255, 138, 61, 0.75);
  box-shadow:
    0 0 22px rgba(255, 138, 61, 0.28),
    inset 0 0 20px rgba(255, 138, 61, 0.12);
}

.key-accent {
  border-color: rgba(255, 138, 61, 0.42);
  background: linear-gradient(180deg, rgba(58, 32, 42, 0.96), rgba(20, 11, 24, 0.96));
}

.key-accent::before {
  background: linear-gradient(90deg, transparent, rgba(255, 138, 61, 0.7), transparent);
}

.key-accent .mp-icon {
  color: var(--orange);
  filter: drop-shadow(0 0 8px rgba(255, 138, 61, 0.6));
}

.key-accent span {
  color: rgba(255, 190, 145, 0.82);
}

.key-hard {
  border-color: rgba(255, 176, 103, 0.8);
  background: linear-gradient(150deg, #FFB067, #FF8A3D 48%, #FF6B24);
  box-shadow:
    0 9px 24px rgba(255, 138, 61, 0.34),
    inset 0 1px 0 rgba(255, 255, 255, 0.5);
}

.key-hard::before {
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.7), transparent);
}

.key-hard .mp-icon {
  color: #241005;
  filter: none;
}

.key-hard span {
  color: rgba(36, 16, 5, 0.78);
  font-weight: 800;
}

.app.is-over .pad {
  opacity: 0.34;
  filter: grayscale(0.35);
  pointer-events: none;
  transition: opacity 0.3s ease, filter 0.3s ease;
}

.key-hard:active,
.key-hard.is-pressed {
  border-color: #FFD9B8;
  box-shadow:
    0 0 26px rgba(255, 138, 61, 0.6),
    inset 0 0 16px rgba(255, 255, 255, 0.28);
}

/* ---------------- responsive ---------------- */

@media (max-height: 720px) {
  .hud { padding: 7px 10px 8px; border-radius: 15px; }
  .score { margin: 4px 0 3px; font-size: clamp(23px, 7.4vw, 30px); }
  .next-box { height: 40px; }
  .next-box canvas { height: 36px; }
  .stage { padding: 6px 0; }
  .hint { margin-bottom: 6px; }
  .key { height: 56px; border-radius: 13px; }
  .key .mp-icon { width: 21px; height: 21px; }
  .over-card { padding: 16px 14px 14px; }
  .over-score { font-size: 33px; margin-top: 10px; }
  .restart { height: 44px; margin-top: 12px; }
}

@media (max-height: 620px) {
  .hint { display: none; }
  .key { height: 52px; }
  .deck { padding-bottom: 0; }
  .hud-meta { font-size: 9px; }
}

@media (max-width: 350px) {
  .pad { gap: 6px; }
  .key { border-radius: 12px; }
  .key span { font-size: 9px; letter-spacing: 0; }
  .hud { gap: 7px; }
  .next { flex-basis: 56px; padding-left: 7px; }
  .hud-meta { font-size: 8.5px; gap: 4px; }
}

@media (prefers-reduced-motion: reduce) {
  .bg-glow,
  .live-dot,
  .board-frame.danger { animation: none !important; }
}
