:root {
  color-scheme: light;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: linear-gradient(180deg, #edf4ff 0%, #f8fafc 100%);
  color: #1f2937;
  display: flex;
  justify-content: center;
  padding:
    calc(16px + env(safe-area-inset-top))
    max(12px, env(safe-area-inset-right))
    calc(18px + env(safe-area-inset-bottom))
    max(12px, env(safe-area-inset-left));
}

.page {
  width: min(100%, 540px);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
}

h1 {
  margin: 0;
  font-size: 1.35rem;
}

.panel {
  width: 100%;
  background: #ffffff;
  border: 1px solid #dbe4f0;
  border-radius: 12px;
  box-shadow: 0 10px 28px rgba(15, 23, 42, 0.05);
  padding: 12px;
  display: grid;
  gap: 10px;
}

.stats {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.metric {
  border-radius: 10px;
  border: 1px solid #dbe4f0;
  background: #f8fafc;
  padding: 8px 6px;
  text-align: center;
}

.metric span {
  display: block;
  margin-bottom: 4px;
  font-size: 0.74rem;
  color: #64748b;
}

.metric strong {
  font-size: 1.1rem;
}

#score {
  color: #0f766e;
}

#bestScore {
  color: #4338ca;
}

#speedLabel {
  color: #b45309;
}

#startButton {
  width: 100%;
  border: none;
  border-radius: 9px;
  padding: 10px 12px;
  min-height: 44px;
  font-size: 0.98rem;
  font-weight: 600;
  letter-spacing: 0.01em;
  cursor: pointer;
  color: #ffffff;
  background: #2563eb;
  transition: transform 0.12s ease, background-color 0.12s ease;
}

#startButton:active {
  transform: translateY(1px);
}

#startButton:hover {
  background: #1d4ed8;
}

.page[data-phase="running"] #startButton {
  background: #0f766e;
}

.page[data-phase="running"] #startButton:hover {
  background: #0d675f;
}

.page[data-phase="paused"] #startButton {
  background: #d97706;
}

.page[data-phase="paused"] #startButton:hover {
  background: #b45309;
}

.page[data-phase="game-over"] #startButton {
  background: #dc2626;
}

.page[data-phase="game-over"] #startButton:hover {
  background: #b91c1c;
}

.status {
  margin: 0;
  min-height: 1.5em;
  font-size: 0.9rem;
  border-radius: 8px;
  padding: 8px 10px;
  border: 1px solid #e2e8f0;
  border-left: 4px solid #94a3b8;
  background: #f8fafc;
}

.status[data-state="idle"] {
  border-left-color: #64748b;
  background: #f1f5f9;
}

.status[data-state="running"] {
  border-left-color: #0f766e;
  background: #ecfdf5;
}

.status[data-state="over"] {
  border-left-color: #dc2626;
  background: #fef2f2;
}

.status[data-state="paused"] {
  border-left-color: #d97706;
  background: #fffbeb;
}

.status-detail {
  margin: 0;
  min-height: 1.3em;
  font-size: 0.84rem;
  color: #475569;
}

.progress-anchor {
  margin: 0;
  border-radius: 8px;
  border: 1px solid #fde68a;
  background: #fffbeb;
  padding: 8px 10px;
}

.anchor-title,
.anchor-detail {
  margin: 0;
}

.anchor-title {
  font-size: 0.84rem;
  font-weight: 600;
  color: #92400e;
}

.anchor-detail {
  margin-top: 3px;
  font-size: 0.8rem;
  color: #78350f;
}

.anchor-bar {
  margin-top: 6px;
  width: 100%;
  height: 6px;
  border-radius: 999px;
  background: #fde68a;
  overflow: hidden;
}

.anchor-bar-fill {
  display: block;
  width: 0;
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, #f97316 0%, #f59e0b 100%);
  transition: width 0.18s ease;
}

.input-hint {
  margin: 0;
  min-height: 1.3em;
  font-size: 0.82rem;
  color: #334155;
}

.round-summary {
  margin: 0;
  border-radius: 8px;
  border: 1px dashed #bfdbfe;
  background: #eff6ff;
  padding: 8px 10px;
}

.summary-title,
.summary-body {
  margin: 0;
}

.summary-title {
  font-size: 0.88rem;
  font-weight: 600;
  color: #1e40af;
}

.summary-body {
  margin-top: 3px;
  font-size: 0.82rem;
  color: #1e3a8a;
}

.canvas-shell {
  position: relative;
  width: min(100%, 420px);
}

#gameCanvas {
  width: 100%;
  aspect-ratio: 1 / 1;
  border: 2px solid #334155;
  border-radius: 10px;
  background: #0b1220;
  display: block;
  box-shadow: 0 12px 24px rgba(15, 23, 42, 0.2);
}

.canvas-overlay {
  position: absolute;
  left: 50%;
  bottom: 10px;
  transform: translateX(-50%);
  margin: 0;
  width: calc(100% - 20px);
  padding: 7px 8px;
  border-radius: 999px;
  text-align: center;
  font-size: 0.8rem;
  color: #e2e8f0;
  background: rgba(15, 23, 42, 0.68);
  transition: opacity 0.18s ease, transform 0.18s ease;
}

.canvas-overlay[data-visible="false"] {
  opacity: 0;
  transform: translate(-50%, 8px);
}

.controls {
  width: min(100%, 320px);
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-areas:
    ". up ."
    "left down right"
    "hint hint hint";
  gap: 10px;
}

.ctrl {
  border: 1px solid #cbd5e1;
  border-radius: 10px;
  background: #ffffff;
  min-height: 52px;
  padding: 10px 0;
  font-size: 1.15rem;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.1s ease;
  touch-action: manipulation;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.ctrl:active {
  background: #e2e8f0;
}

.ctrl:focus-visible {
  outline: 2px solid #2563eb;
  outline-offset: 1px;
}

.up {
  grid-area: up;
}

.left {
  grid-area: left;
}

.down {
  grid-area: down;
}

.right {
  grid-area: right;
}

.hint {
  grid-area: hint;
  margin: 0;
  text-align: center;
  font-size: 0.8rem;
  color: #475569;
}

@media (max-width: 560px) {
  h1 {
    font-size: 1.26rem;
  }

  .panel {
    padding: 11px;
  }

  .metric strong {
    font-size: 1.02rem;
  }

  .canvas-overlay {
    width: calc(100% - 16px);
    font-size: 0.76rem;
  }
}

@media (max-width: 460px) {
  body {
    padding:
      calc(12px + env(safe-area-inset-top))
      max(10px, env(safe-area-inset-right))
      calc(14px + env(safe-area-inset-bottom))
      max(10px, env(safe-area-inset-left));
  }

  .page {
    gap: 10px;
  }

  .controls {
    width: min(100%, 360px);
    gap: 8px;
  }

  .ctrl {
    min-height: 56px;
    font-size: 1.2rem;
  }

  .hint {
    font-size: 0.78rem;
  }
}

@media (max-width: 360px) {
  .stats {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .metric:last-child {
    grid-column: 1 / -1;
  }

  .status,
  .status-detail,
  .anchor-title,
  .anchor-detail,
  .input-hint {
    font-size: 0.78rem;
  }
}
