:root {
  /* —— Color roles (Material-inspired, kid-friendly) —— */
  --color-primary: #3b82f6;
  --color-on-primary: #ffffff;
  --color-primary-deep: #1d4ed8;
  --color-primary-container: #dbeafe;
  --color-primary-soft: #eff6ff;
  --color-on-primary-container: #1d4ed8;

  --color-success: #10b981;
  --color-success-container: #d1fae5;
  --color-on-success-container: #065f46;
  --color-success-deep: #047857;

  --color-error: #f43f5e;
  --color-error-container: #ffe4e6;
  --color-on-error-container: #9f1239;
  --color-error-soft: #fecdd3;

  --color-surface: #f8fafc;
  --color-surface-dim: #f1f5f9;
  --color-surface-container: #ffffff;
  --color-surface-container-high: #f8fafc;
  --color-on-surface: #334155;
  --color-on-surface-variant: #64748b;
  --color-outline: #e2e8f0;
  --color-outline-strong: #475569;
  --color-scrim: rgba(241, 245, 249, 0.9);
  --color-focus: #93c5fd;
  --color-fixation: #cbd5e1;

  /* —— Spacing (Material 8dp grid) —— */
  --space-0-5: 4px;
  --space-1: 8px;
  --space-2: 16px;
  --space-3: 24px;
  --space-4: 32px;

  /* —— Shape scale —— */
  --radius-sm: 12px;
  --radius-md: 16px;
  --radius-lg: 24px;
  --radius-xl: 28px;
  --radius-full: 999px;

  /* —— Elevation —— */
  --elev-1: 0 4px 12px rgba(15, 23, 42, 0.06);
  --elev-2: 0 12px 32px rgba(15, 23, 42, 0.06);
  --elev-3: 0 18px 48px rgba(15, 23, 42, 0.08);
  --elev-primary: 0 14px 24px rgba(37, 99, 235, 0.16);

  /* —— Shared interactive button language —— */
  --btn-lip: 6px;
  --btn-press-y: 5px;
  --touch-min: 48px;

  /* Legacy aliases used during the transition */
  --bg: var(--color-surface);
  --ink: var(--color-on-surface);
  --muted: var(--color-on-surface-variant);
  --go: var(--color-primary);
  --go-deep: var(--color-primary-deep);
  --ok: var(--color-success);
  --ok-deep: var(--color-success-deep);
  --nogo: var(--color-error);
  --nogo-ink: var(--color-on-error-container);
  --card: var(--color-surface-container);
  --radius: var(--radius-lg);
  --shadow: var(--elev-2);

  color-scheme: light;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  background: var(--color-surface);
  color: var(--color-on-surface);
  font-family: "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", "Segoe UI", sans-serif;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
  overflow: hidden;
}

body {
  position: fixed;
  inset: 0;
  width: 100%;
  min-height: 100dvh;
  padding:
    env(safe-area-inset-top)
    env(safe-area-inset-right)
    env(safe-area-inset-bottom)
    env(safe-area-inset-left);
}

button {
  font: inherit;
  color: inherit;
  border: 0;
  background: none;
  padding: 0;
  cursor: pointer;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

button:focus {
  outline: none;
}

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

#app {
  height: 100%;
  max-width: 520px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.header {
  position: relative;
  z-index: 30;
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-1) var(--space-2) var(--space-0-5);
  background: var(--color-surface);
}

.header h1 {
  flex: 1;
  margin: 0;
  font-size: clamp(1rem, 4.2vw, 1.2rem);
  line-height: 1.35;
  font-weight: 800;
  letter-spacing: 0.01em;
}

.mute-btn {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  gap: var(--space-0-5);
  min-height: var(--touch-min);
  padding: var(--space-0-5) var(--space-1) var(--space-0-5) var(--space-1);
  border-radius: var(--radius-full);
  background: var(--color-primary-container);
  color: var(--color-on-primary-container);
  font-size: 0.95rem;
  font-weight: 800;
  line-height: 1;
  box-shadow: var(--elev-1);
}

.mute-btn svg {
  width: 28px;
  height: 28px;
  display: block;
  flex: 0 0 auto;
}

.mute-btn .speaker-slash {
  display: none;
}

.mute-btn.is-muted {
  background: var(--color-outline);
  color: var(--color-outline-strong);
}

.mute-btn.is-muted .speaker-waves {
  display: none;
}

.mute-btn.is-muted .speaker-slash {
  display: block;
}

.mute-btn.is-pressed {
  transform: translateY(1px);
  box-shadow: var(--elev-1);
}

main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.screen {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding: var(--space-1) var(--space-2) var(--space-2);
}

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

#welcome {
  justify-content: space-evenly;
  gap: var(--space-2);
}

.robot-wrap {
  display: grid;
  place-items: center;
}

.robot {
  width: min(42vw, 168px);
  height: auto;
  filter: drop-shadow(0 12px 18px rgba(59, 130, 246, 0.12));
  animation: float 3s ease-in-out infinite;
}

.rules {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.rule-card {
  margin: 0;
  padding: var(--space-2);
  border-radius: var(--radius-lg);
  font-size: clamp(1.02rem, 4.4vw, 1.18rem);
  font-weight: 700;
  line-height: 1.55;
  box-shadow: var(--elev-2);
}

.rule-go {
  background: var(--color-success-container);
  color: var(--color-on-success-container);
}

.rule-nogo {
  background: var(--color-error-container);
  color: var(--color-on-error-container);
}

.app-version {
  margin: 0;
  text-align: center;
  color: var(--color-on-surface-variant);
  font-size: 0.85rem;
  font-weight: 600;
  letter-spacing: 0.02em;
}

.app-version-results {
  margin-top: var(--space-2);
}

/* Shared full-width action buttons */
.start-btn,
.ghost-btn,
.cushion {
  width: 100%;
  border-radius: var(--radius-lg);
  font-weight: 800;
  letter-spacing: 0.02em;
}

.start-btn,
.cushion {
  background: var(--color-primary);
  color: var(--color-on-primary);
  box-shadow: 0 var(--btn-lip) 0 var(--color-primary-deep), var(--elev-primary);
}

.start-btn {
  min-height: 64px;
  font-size: 1.3rem;
}

.ghost-btn {
  min-height: 56px;
  background: var(--color-surface-dim);
  color: var(--color-outline-strong);
  font-size: 1.15rem;
  box-shadow: 0 var(--btn-lip) 0 var(--color-outline);
}

.start-btn.is-pressed,
.ghost-btn.is-pressed {
  transform: translateY(var(--btn-press-y));
  box-shadow: 0 1px 0 rgba(15, 23, 42, 0.08);
}

#game {
  gap: var(--space-1);
  padding-bottom: var(--space-1);
}

.status {
  display: flex;
  justify-content: space-between;
  gap: var(--space-2);
  background: var(--color-surface-container);
  border-radius: var(--radius-md);
  padding: var(--space-1) var(--space-2);
  box-shadow: var(--elev-2);
}

.stat {
  margin: 0;
  font-weight: 700;
  font-size: 1.05rem;
}

.stat-k {
  color: var(--color-on-surface-variant);
  font-weight: 700;
  margin-right: var(--space-0-5);
}

.stat strong {
  color: var(--color-primary);
  font-size: 1.35rem;
}

.stage {
  position: relative;
  flex: 1;
  display: grid;
  place-items: center;
  min-height: 0;
}

.fixation {
  font-size: clamp(96px, 28vw, 148px);
  line-height: 1;
  font-weight: 500;
  color: var(--color-fixation);
}

.shape-btn {
  width: min(52vw, 210px);
  height: min(52vw, 210px);
  display: grid;
  place-items: center;
  background: transparent;
}

.shape-btn[hidden] {
  display: none !important;
}

.shape-btn svg {
  width: 100%;
  height: 100%;
  filter: drop-shadow(0 10px 16px rgba(15, 23, 42, 0.08));
}

.shape-btn.pop {
  animation: pop 180ms ease-out;
}

.badge {
  position: absolute;
  top: var(--space-0-5);
  left: 0;
  right: 0;
  z-index: 2;
  width: fit-content;
  max-width: min(88vw, 340px);
  margin: 0 auto;
  padding: var(--space-1) var(--space-2);
  border-radius: var(--radius-full);
  font-weight: 800;
  font-size: 1.05rem;
  line-height: 1.4;
  text-align: center;
  pointer-events: none;
  opacity: 0;
  visibility: hidden;
  box-shadow: var(--elev-2);
  transition: opacity 180ms ease, visibility 180ms ease;
}

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

.badge.is-good {
  background: #ecfdf5;
  color: var(--color-success-deep);
}

.badge.is-oops {
  background: #fff1f2;
  color: var(--color-on-error-container);
}

.cushion {
  flex: 0 0 auto;
  min-height: 76px;
  padding: var(--space-2);
  font-size: clamp(1.15rem, 4.6vw, 1.4rem);
}

.cushion.is-pressed {
  transform: translateY(var(--btn-press-y));
  box-shadow: 0 1px 0 var(--color-primary-deep), 0 6px 12px rgba(37, 99, 235, 0.12);
}

.results {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  padding:
    calc(76px + env(safe-area-inset-top))
    calc(var(--space-2) + env(safe-area-inset-right))
    calc(var(--space-2) + env(safe-area-inset-bottom))
    calc(var(--space-2) + env(safe-area-inset-left));
  background: var(--color-scrim);
  touch-action: pan-y;
  overflow: auto;
}

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

.results-card {
  width: min(100%, 420px);
  background: rgba(255, 255, 255, 0.94);
  border-radius: var(--radius-xl);
  box-shadow: var(--elev-3);
  padding: var(--space-3) var(--space-2) var(--space-2);
  text-align: center;
}

.results-robot svg {
  width: 84px;
  height: 84px;
  filter: drop-shadow(0 12px 18px rgba(59, 130, 246, 0.12));
}

.results-card h2 {
  margin: var(--space-0-5) 0 var(--space-1);
  font-size: 1.35rem;
}

.score-line {
  margin: 0;
  color: var(--color-primary);
  font-weight: 800;
  font-size: 3rem;
  line-height: 1;
}

.score-unit {
  margin-left: var(--space-0-5);
  font-size: 1.3rem;
}

.score-max {
  margin: var(--space-0-5) 0 var(--space-1);
  color: var(--color-on-surface-variant);
  font-weight: 700;
}

.tier-text {
  margin: 0 0 var(--space-2);
  text-align: left;
  font-size: 1.02rem;
  line-height: 1.6;
  font-weight: 700;
  background: var(--color-surface-container-high);
  border-radius: var(--radius-md);
  padding: var(--space-1) var(--space-2);
}

.results-actions {
  display: flex;
  flex-direction: column;
  /* Visible gap ≈ space-3; lip is reserved so shadows don't collapse it */
  gap: calc(var(--space-3) + var(--btn-lip));
}

.results-actions .start-btn,
.results-actions .ghost-btn {
  min-height: 56px;
  font-size: 1.2rem;
}

.rotate-hint {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 50;
  background: var(--color-surface);
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: var(--space-3);
  font-size: 1.35rem;
  font-weight: 800;
  line-height: 1.5;
}

@keyframes float {
  0%,
  100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-12px);
  }
}

@keyframes pop {
  from {
    transform: scale(0.9);
  }
  to {
    transform: scale(1);
  }
}

@media (max-height: 700px) {
  .robot {
    width: min(32vw, 120px);
  }

  .rule-card {
    padding: var(--space-1) var(--space-2);
  }

  .cushion {
    min-height: 64px;
  }

  .start-btn {
    min-height: 56px;
  }

  .score-line {
    font-size: 2.4rem;
  }

  .results {
    padding-top: calc(64px + env(safe-area-inset-top));
  }

  .results-card {
    padding: var(--space-1) var(--space-2);
  }

  .results-robot svg {
    width: 64px;
    height: 64px;
  }

  .tier-text {
    margin-bottom: var(--space-1);
    padding: var(--space-1) var(--space-2);
  }
}

@media (orientation: landscape) and (max-height: 500px) {
  .rotate-hint {
    display: flex;
  }
}

@media (prefers-reduced-motion: reduce) {
  .robot {
    animation: none;
  }

  .shape-btn.pop {
    animation: none;
  }

  .badge,
  .start-btn,
  .ghost-btn,
  .cushion {
    transition: none;
  }
}
