/* =========================================================
   1. 全局变量与基础样式
   ========================================================= */
:root {
  color-scheme: dark;
  --bg: #071015;
  --panel: #0d1a22;
  --panel-light: #152833;
  --board: #0f1c25;
  --line: rgba(126, 154, 168, 0.2);
  --text: #ecf6f2;
  --muted: #8da8b4;
  --orange: #ff9f1c;
  --orange-bright: #ffbd57;
  --green: #78e39a;
  --danger: #ff8c7c;
  --radius: 24px;
  --shadow: 0 30px 80px rgba(0, 0, 0, 0.42);
}

* {
  box-sizing: border-box;
}

html {
  min-width: 280px;
}

body {
  min-height: 100svh;
  margin: 0;
  color: var(--text);
  font-family: "Segoe UI", "Microsoft YaHei UI", "Microsoft YaHei",
    system-ui, sans-serif;
  background:
    radial-gradient(circle at 12% -8%, rgba(255, 159, 28, 0.16), transparent 30rem),
    radial-gradient(circle at 90% 5%, rgba(120, 227, 154, 0.09), transparent 26rem),
    linear-gradient(180deg, #071015 0%, #0a151c 68%, #081116 100%);
  -webkit-font-smoothing: antialiased;
}

button {
  font: inherit;
}

button:focus-visible {
  outline: 2px solid var(--orange);
  outline-offset: 3px;
}

.app {
  width: min(100% - 32px, 700px);
  margin: 0 auto;
  padding: clamp(20px, 4vh, 42px) 0 28px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* =========================================================
   2. 顶部标题与计分板
   ========================================================= */
.topbar {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 22px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 14px;
  min-width: 0;
}

.brand-ball {
  font-size: 42px;
  line-height: 1;
  filter: drop-shadow(0 10px 22px rgba(255, 159, 28, 0.24));
}

.brand-kicker {
  margin: 0 0 2px;
  color: var(--orange);
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.18em;
}

h1 {
  margin: 0;
  font-size: clamp(27px, 4vw, 34px);
  line-height: 1.1;
  letter-spacing: -0.035em;
}

.brand-subtitle {
  margin: 5px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.scoreboard {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 10px 18px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: rgba(13, 26, 34, 0.78);
  box-shadow: inset 0 1px rgba(255, 255, 255, 0.035);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}

.stat {
  min-width: 56px;
  text-align: center;
}

.stat-label {
  display: block;
  margin-bottom: 1px;
  color: var(--muted);
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.08em;
}

.stat-value {
  display: block;
  font-size: 26px;
  line-height: 1.15;
  font-variant-numeric: tabular-nums;
}

.stat-value.accent {
  color: var(--orange);
}

.stat-value.is-bumping {
  animation: score-bump 0.28s ease;
}

.stat-divider {
  width: 1px;
  height: 34px;
  background: var(--line);
}

/* =========================================================
   3. 游戏卡片与画布
   ========================================================= */
.game-card {
  width: 100%;
  max-width: min(644px, max(340px, calc((100svh - 245px) * 1.2 + 20px)));
  margin-inline: auto;
  padding: 10px;
  border: 1px solid rgba(126, 154, 168, 0.2);
  border-radius: 26px;
  background: linear-gradient(180deg, rgba(18, 34, 44, 0.94), rgba(10, 20, 27, 0.96));
  box-shadow: var(--shadow), inset 0 1px rgba(255, 255, 255, 0.04);
}

.board-wrap {
  position: relative;
  overflow: hidden;
  aspect-ratio: 624 / 520;
  border: 1px solid rgba(126, 154, 168, 0.13);
  border-radius: 18px;
  background: var(--board);
}

canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
}

.game-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 11px 8px 2px;
}

.game-status {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0;
  color: var(--muted);
  font-size: 12px;
}

.status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #5c7380;
  transition: background-color 0.2s, box-shadow 0.2s;
}

.game-card[data-state="running"] .status-dot {
  background: var(--green);
  box-shadow: 0 0 0 4px rgba(120, 227, 154, 0.1);
  animation: status-pulse 1.8s ease-in-out infinite;
}

.game-card[data-state="paused"] .status-dot {
  background: var(--orange);
  box-shadow: 0 0 0 4px rgba(255, 159, 28, 0.1);
}

.game-card[data-state="over"] .status-dot {
  background: var(--danger);
}

.game-card[data-state="won"] .status-dot {
  background: var(--green);
}

.game-actions {
  display: flex;
  gap: 8px;
}

.ghost-button {
  padding: 7px 13px;
  border: 1px solid var(--line);
  border-radius: 9px;
  color: #d5e3e9;
  background: rgba(255, 255, 255, 0.025);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.15s, border-color 0.15s, opacity 0.15s;
}

.ghost-button:hover:not(:disabled) {
  border-color: rgba(255, 159, 28, 0.38);
  background: rgba(255, 159, 28, 0.08);
}

.ghost-button:disabled {
  opacity: 0.35;
  cursor: default;
}

/* =========================================================
   4. 开始 / 暂停 / 结束遮罩
   ========================================================= */
.overlay {
  position: absolute;
  z-index: 2;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(3, 9, 13, 0.58);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  opacity: 1;
  visibility: visible;
  transition: opacity 0.22s ease, visibility 0.22s ease;
}

.overlay.is-hidden {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

.overlay-card {
  position: relative;
  width: min(390px, 100%);
  max-height: 100%;
  overflow-y: auto;
  padding: 28px 30px 25px;
  text-align: center;
  border: 1px solid rgba(126, 154, 168, 0.2);
  border-radius: 22px;
  background: linear-gradient(180deg, rgba(25, 45, 57, 0.97), rgba(13, 25, 33, 0.97));
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.46), inset 0 1px rgba(255, 255, 255, 0.055);
}

.overlay-card::before {
  content: "";
  position: absolute;
  top: 0;
  left: 50%;
  width: 56px;
  height: 3px;
  border-radius: 0 0 3px 3px;
  background: var(--orange);
  transform: translateX(-50%);
}

.overlay-card[data-tone="over"]::before {
  background: var(--danger);
}

.overlay-card[data-tone="won"]::before {
  background: var(--green);
}

.overlay-ball {
  margin-bottom: 7px;
  font-size: 38px;
  line-height: 1;
  filter: drop-shadow(0 8px 18px rgba(255, 159, 28, 0.24));
}

.overlay-kicker {
  margin: 0 0 6px;
  color: var(--orange);
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.16em;
  text-transform: uppercase;
}

.overlay-card[data-tone="over"] .overlay-kicker {
  color: var(--danger);
}

.overlay-card[data-tone="won"] .overlay-kicker {
  color: var(--green);
}

.overlay-card h2 {
  margin: 0;
  font-size: clamp(25px, 4vw, 32px);
  line-height: 1.2;
  letter-spacing: -0.025em;
}

.overlay-text {
  max-width: 30em;
  margin: 8px auto 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.7;
}

.primary-button {
  width: 100%;
  margin-top: 18px;
  padding: 12px 18px;
  border: 0;
  border-radius: 12px;
  color: #201305;
  background: linear-gradient(180deg, #ffb445, #ff9410);
  box-shadow: 0 10px 24px rgba(255, 159, 28, 0.22), inset 0 1px rgba(255, 255, 255, 0.35);
  font-size: 14px;
  font-weight: 800;
  cursor: pointer;
  transition: transform 0.16s, filter 0.16s, box-shadow 0.16s;
}

.primary-button:hover {
  filter: brightness(1.05);
  transform: translateY(-1px);
}

.primary-button:active {
  transform: translateY(0) scale(0.99);
}

.overlay-hint {
  margin: 11px 0 0;
  color: var(--muted);
  font-size: 11px;
}

/* =========================================================
   5. 手机方向键 & 键盘提示
   ========================================================= */
.touch-controls {
  display: none;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  padding-top: 2px;
}

.touch-tip {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
}

.dpad {
  display: grid;
  grid-template-columns: repeat(3, 52px);
  grid-template-rows: repeat(3, 52px);
  gap: 6px;
}

.pad-button {
  display: grid;
  place-items: center;
  padding: 0;
  border: 1px solid rgba(126, 154, 168, 0.22);
  border-radius: 15px;
  color: #dfeef3;
  background: rgba(17, 34, 44, 0.92);
  box-shadow: inset 0 1px rgba(255, 255, 255, 0.035);
  font-size: 20px;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  transition: background-color 0.12s, color 0.12s, transform 0.12s;
}

.pad-button:active {
  color: #1d1204;
  background: var(--orange);
  transform: scale(0.94);
}

.pad-up { grid-area: 1 / 2; }
.pad-left { grid-area: 2 / 1; }
.pad-right { grid-area: 2 / 3; }
.pad-down { grid-area: 3 / 2; }

.pad-center {
  display: grid;
  place-items: center;
  grid-area: 2 / 2;
  font-size: 24px;
  opacity: 0.7;
}

.keyboard-hints {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 9px 22px;
  padding-top: 2px;
  color: var(--muted);
  font-size: 12px;
}

.keyboard-hints span {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

kbd {
  display: inline-grid;
  place-items: center;
  min-width: 22px;
  height: 22px;
  padding: 0 5px;
  border: 1px solid #2a4552;
  border-bottom-color: #162932;
  border-radius: 6px;
  color: #cbdfe6;
  background: var(--panel);
  box-shadow: 0 2px 0 #040a0d;
  font-family: inherit;
  font-size: 10px;
  font-weight: 700;
}

/* =========================================================
   6. 响应式布局
   ========================================================= */
@media (max-width: 700px), (hover: none) and (pointer: coarse) {
  .touch-controls {
    display: flex;
  }

  .keyboard-hints {
    display: none;
  }
}

@media (max-width: 650px) {
  .app {
    width: calc(100% - 20px);
    padding: 18px 0 24px;
    gap: 12px;
  }

  .topbar {
    align-items: flex-start;
    gap: 12px;
  }

  .brand-ball {
    font-size: 36px;
  }

  .brand-kicker {
    font-size: 9px;
  }

  h1 {
    font-size: 27px;
  }

  .brand-subtitle {
    display: none;
  }

  .scoreboard {
    width: 100%;
    justify-content: space-around;
    padding: 9px 14px;
    border-radius: 14px;
  }

  .stat {
    display: flex;
    align-items: baseline;
    gap: 8px;
    min-width: 0;
  }

  .stat-label {
    margin: 0;
    font-size: 10px;
  }

  .stat-value {
    font-size: 22px;
  }

  .stat-divider {
    height: 22px;
  }

  .game-card {
    padding: 6px;
    border-radius: 20px;
  }

  .board-wrap {
    border-radius: 15px;
  }

  .game-bar {
    padding: 9px 5px 2px;
  }

  .ghost-button {
    min-height: 40px;
    padding: 8px 16px;
  }

  .overlay {
    padding: 12px;
  }

  .overlay-card {
    padding: 20px 18px;
    border-radius: 18px;
  }

  .overlay-ball {
    margin-bottom: 4px;
    font-size: 29px;
  }

  .overlay-kicker {
    margin-bottom: 4px;
    font-size: 9px;
  }

  .overlay-card h2 {
    font-size: 25px;
  }

  .overlay-text {
    margin-top: 5px;
    font-size: 12px;
    line-height: 1.55;
  }

  .primary-button {
    margin-top: 13px;
    padding: 10px 16px;
    font-size: 13px;
  }

  .overlay-hint {
    margin-top: 7px;
    font-size: 10px;
  }

  .dpad {
    grid-template-columns: repeat(3, 48px);
    grid-template-rows: repeat(3, 48px);
    gap: 5px;
  }

  .pad-button {
    border-radius: 13px;
    font-size: 18px;
  }

  .pad-center {
    font-size: 22px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* =========================================================
   7. 动画
   ========================================================= */
@keyframes status-pulse {
  0%,
  100% {
    box-shadow: 0 0 0 4px rgba(120, 227, 154, 0.08);
  }
  50% {
    box-shadow: 0 0 0 7px rgba(120, 227, 154, 0.02);
  }
}

@keyframes score-bump {
  0% {
    transform: translateY(0) scale(1);
  }
  45% {
    color: var(--orange-bright);
    transform: translateY(-4px) scale(1.14);
  }
  100% {
    transform: translateY(0) scale(1);
  }
}
