:root {
  --navy: #263352;
  --navy-deep: #10192e;
  --teal: #1d9e75;
  --teal-dark: #146f52;
  --orange: #d85a30;
  --orange-dark: #a8431f;
  --amber: #ef9f27;
  --paper: #fffdf8;
  --cream: #fff7d6;
  --ink: #231a18;
  --muted: #6b6460;
  --red: #d8382f;
  --mint: #5dcaa5;
  --radius: 12px;
  --font: 'Baloo 2', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --safe-top: max(12px, env(safe-area-inset-top));
  --safe-right: max(12px, env(safe-area-inset-right));
  --safe-bottom: max(12px, env(safe-area-inset-bottom));
  --safe-left: max(12px, env(safe-area-inset-left));
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  height: 100dvh;
  margin: 0;
  overflow: hidden;
  background: var(--navy);
  touch-action: none;
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

#game {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

#ui {
  position: absolute;
  inset: 0;
  pointer-events: none;
  font-family: var(--font);
  color: var(--ink);
}

button {
  font-family: var(--font);
}

/* ---------- Screens ---------- */

.screen {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--safe-top) var(--safe-right) var(--safe-bottom) var(--safe-left);
  background: rgba(16, 25, 46, 0.55);
  opacity: 0;
  visibility: hidden;
  transform: translateY(14px);
  transition: opacity 200ms ease, transform 200ms ease, visibility 0s linear 200ms;
  pointer-events: none;
  overflow-y: auto;
  touch-action: pan-y;
}

/* Centred when it fits. When it is taller than the screen (a phone held sideways), it starts
   at the top and scrolls; plain centring would push its top out of reach. */
.screen > * {
  margin-block: auto;
}

.screen.is-visible {
  opacity: 1;
  visibility: visible;
  transform: none;
  transition: opacity 200ms ease, transform 200ms ease;
  pointer-events: auto;
}

.screen-title {
  background: linear-gradient(180deg, rgba(16, 25, 46, 0.65) 0%, rgba(16, 25, 46, 0.25) 55%, rgba(16, 25, 46, 0.6) 100%);
}

.screen-intro {
  background: rgba(16, 25, 46, 0.35);
  pointer-events: none;
}

.screen-results {
  align-items: flex-start;
  background: rgba(16, 25, 46, 0.6);
  /* Clear the mute button in the top-right corner. */
  padding-top: calc(var(--safe-top) + 56px);
}

.card {
  background: var(--paper);
  border-radius: var(--radius);
  box-shadow: 0 8px 0 rgba(0, 0, 0, 0.16), 0 24px 60px rgba(0, 0, 0, 0.35);
  padding: 22px 22px 24px;
  width: min(440px, 100%);
}

h2 {
  margin: 0 0 12px;
  font-size: 28px;
  line-height: 1.1;
  font-weight: 800;
  color: var(--navy);
}

h3 {
  margin: 16px 0 6px;
  font-size: 15px;
  font-weight: 800;
  color: var(--muted);
  text-transform: none;
}

.button-row {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  justify-content: center;
  margin-top: 18px;
}

.button-col {
  display: grid;
  gap: 12px;
  margin-top: 8px;
}

/* ---------- Buttons ---------- */

.btn {
  font: 800 20px/1 var(--font);
  min-height: 52px;
  min-width: 120px;
  padding: 14px 22px 12px;
  border: 0;
  border-radius: var(--radius);
  cursor: pointer;
  color: #fff;
  background: var(--teal);
  box-shadow: 0 5px 0 var(--teal-dark);
  transition: transform 80ms ease, box-shadow 80ms ease, filter 120ms ease;
  touch-action: manipulation;
}

.btn:hover {
  filter: brightness(1.06);
}

.btn:active {
  transform: translateY(4px);
  box-shadow: 0 1px 0 var(--teal-dark);
}

.btn:focus-visible,
.icon-btn:focus-visible,
.stage-card:focus-visible {
  outline: 3px solid var(--amber);
  outline-offset: 3px;
}

.btn-primary {
  background: var(--orange);
  box-shadow: 0 5px 0 var(--orange-dark);
}

.btn-primary:active {
  box-shadow: 0 1px 0 var(--orange-dark);
}

.btn-light {
  background: #ffffff;
  color: var(--navy);
  box-shadow: 0 5px 0 #c9c2b8;
}

.btn-light:active {
  box-shadow: 0 1px 0 #c9c2b8;
}

.btn-teal {
  background: var(--teal);
}

.btn-big {
  font-size: 26px;
  min-height: 64px;
  min-width: 180px;
}

.btn-small {
  min-height: 44px;
  min-width: 0;
  font-size: 16px;
  padding: 10px 16px 8px;
}

.btn[hidden] {
  display: none;
}

.icon-btn {
  pointer-events: auto;
  position: absolute;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  border: 0;
  background: rgba(16, 25, 46, 0.8);
  color: #fff;
  display: grid;
  place-items: center;
  cursor: pointer;
  box-shadow: 0 3px 0 rgba(0, 0, 0, 0.3);
  touch-action: manipulation;
}

.icon-btn:active {
  transform: translateY(2px);
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.3);
}

.mute-btn {
  top: var(--safe-top);
  right: var(--safe-right);
  z-index: 5;
}

.pause-btn {
  top: var(--safe-top);
  left: var(--safe-left);
}

/* ---------- Title ---------- */

.title-block {
  text-align: center;
  max-width: 560px;
}

.title-icon {
  width: 72px;
  height: 72px;
  filter: drop-shadow(0 4px 0 rgba(0, 0, 0, 0.3));
}

.logo {
  margin: 6px 0 4px;
  line-height: 0.92;
  font-weight: 800;
}

.logo span {
  display: block;
}

.logo-top {
  font-size: clamp(34px, 10vw, 58px);
  color: #ffffff;
  text-shadow: 0 3px 0 var(--navy-deep), 3px 0 0 var(--navy-deep), -3px 0 0 var(--navy-deep), 0 -3px 0 var(--navy-deep), 0 8px 0 rgba(0, 0, 0, 0.25);
}

.logo-bottom {
  font-size: clamp(46px, 14vw, 84px);
  color: var(--amber);
  text-shadow: 0 4px 0 var(--orange-dark), 4px 0 0 var(--navy-deep), -4px 0 0 var(--navy-deep), 0 -4px 0 var(--navy-deep), 0 8px 0 var(--navy-deep), 0 12px 0 rgba(0, 0, 0, 0.25);
}

.subtitle {
  margin: 10px auto 0;
  max-width: 460px;
  font-size: 20px;
  font-weight: 700;
  color: #ffffff;
  text-shadow: 0 2px 0 var(--navy-deep);
}

/* ---------- How to play ---------- */

.howto-list {
  margin: 0;
  padding-left: 1.4em;
  font-size: 19px;
  line-height: 1.45;
  font-weight: 600;
}

.howto-list li + li {
  margin-top: 8px;
}

/* ---------- Stage select ---------- */

.stages-block {
  width: min(760px, 100%);
  padding: 8px 0 16px;
}

.screen-heading {
  color: #ffffff;
  text-align: center;
  text-shadow: 0 3px 0 var(--navy-deep);
}

.stage-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 12px;
}

.stage-card {
  display: grid;
  justify-items: center;
  gap: 2px;
  padding: 12px 12px 14px;
  border: 0;
  border-radius: var(--radius);
  background: var(--paper);
  color: var(--ink);
  text-align: center;
  cursor: pointer;
  box-shadow: 0 6px 0 #c9c2b8;
  transition: transform 80ms ease, box-shadow 80ms ease;
  touch-action: manipulation;
}

.stage-card:active {
  transform: translateY(4px);
  box-shadow: 0 2px 0 #c9c2b8;
}

.stage-portrait {
  width: 72px;
  height: 72px;
  border-radius: 50%;
  background: var(--navy);
  border: 3px solid var(--amber);
}

.stage-number {
  font-size: 13px;
  font-weight: 800;
  color: var(--orange);
}

.stage-name {
  font-size: 18px;
  font-weight: 800;
  line-height: 1.1;
  color: var(--navy);
}

.stage-intro {
  font-size: 13px;
  line-height: 1.25;
  color: var(--muted);
}

.stage-status {
  margin-top: 6px;
  font-size: 13px;
  font-weight: 800;
  color: var(--teal-dark);
}

.stage-card.is-locked {
  filter: grayscale(0.85);
  opacity: 0.72;
  cursor: not-allowed;
}

.stage-card.is-locked .stage-status {
  color: var(--muted);
}

.stage-card.nope {
  animation: nope 320ms ease;
}

@keyframes nope {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-6px); }
  75% { transform: translateX(6px); }
}

/* ---------- Stage intro ---------- */

.intro-card {
  text-align: center;
  width: min(380px, 100%);
}

.intro-number {
  margin: 0;
  font-size: 16px;
  font-weight: 800;
  color: var(--orange);
}

.intro-name {
  margin: 2px 0 6px;
  font-size: 30px;
}

.intro-line {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  color: var(--muted);
}

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

.hud {
  position: absolute;
  inset: 0;
  pointer-events: none;
  display: none;
}

.hud.is-visible {
  display: block;
}

.hud-bar {
  position: absolute;
  top: var(--safe-top);
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 16px 4px;
  border-radius: 999px;
  background: rgba(16, 25, 46, 0.86);
  color: #ffffff;
  font-size: 17px;
  font-weight: 800;
  white-space: nowrap;
  box-shadow: 0 3px 0 rgba(0, 0, 0, 0.3);
}

.score-num {
  min-width: 1.2em;
  text-align: center;
  font-size: 24px;
  line-height: 1;
  color: var(--amber);
}

.score-sep {
  opacity: 0.7;
}

.serve-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--mint);
  opacity: 0;
  transition: opacity 200ms ease;
}

.serve-dot.is-on {
  opacity: 1;
}

.hud-stage {
  position: absolute;
  top: calc(var(--safe-top) + 46px);
  left: 50%;
  transform: translateX(-50%);
  margin: 0;
  font-size: 13px;
  font-weight: 700;
  color: rgba(255, 255, 255, 0.9);
  text-shadow: 0 1px 0 var(--navy-deep);
  white-space: nowrap;
}

/* Short landscape screens: keep the middle clear for Jianyun. */
@media (orientation: landscape) and (max-height: 520px) {
  .hud-bar {
    left: calc(var(--safe-left) + 60px);
    transform: none;
  }
  .hud-stage {
    left: calc(var(--safe-left) + 66px);
    transform: none;
  }
}

.serve-hint {
  position: absolute;
  bottom: calc(var(--safe-bottom) + 10px);
  left: 50%;
  margin: 0;
  padding: 8px 18px 6px;
  border-radius: 999px;
  background: rgba(16, 25, 46, 0.8);
  color: #ffffff;
  font-size: 18px;
  font-weight: 800;
  opacity: 0;
  transform: translate(-50%, 8px);
  transition: opacity 200ms ease, transform 200ms ease;
}

.serve-hint.is-visible {
  opacity: 1;
  transform: translate(-50%, 0);
  animation: pulse 1.4s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(239, 159, 39, 0.5); }
  50% { box-shadow: 0 0 0 8px rgba(239, 159, 39, 0); }
}

/* ---------- Pause ---------- */

.pause-card {
  width: min(320px, 100%);
  text-align: center;
}

/* ---------- Results (incident report) ---------- */

.results-block {
  width: min(500px, 100%);
  margin: auto 0;
  padding: 8px 0 16px;
}

.final-banner {
  display: none;
  margin: 0 0 12px;
  padding: 10px 14px 8px;
  border-radius: var(--radius);
  background: var(--amber);
  color: var(--navy-deep);
  font-size: 22px;
  font-weight: 800;
  text-align: center;
  box-shadow: 0 5px 0 var(--orange-dark);
}

.final-banner.is-visible {
  display: block;
}

.report {
  padding: 0 0 18px;
  overflow: hidden;
  width: 100%;
}

.report-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
  padding: 12px 18px 10px;
  color: #ffffff;
  font-weight: 800;
}

.report-head.is-lost {
  background: var(--red);
}

.report-head.is-won {
  background: var(--teal);
}

.report-id {
  font-size: 14px;
  opacity: 0.9;
}

.report-title {
  font-size: 18px;
}

.report > h3,
.report-fields,
.report-timeline,
.report-metrics,
.report-actions {
  margin-left: 18px;
  margin-right: 18px;
}

.report-fields {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 4px 12px;
  margin-top: 14px;
  margin-bottom: 0;
  font-size: 16px;
}

.report-fields dt {
  font-weight: 800;
  color: var(--muted);
}

.report-fields dd {
  margin: 0;
  font-weight: 700;
}

.report-timeline {
  margin-top: 0;
  margin-bottom: 0;
  padding-left: 0;
  list-style: none;
  font-size: 15px;
  font-weight: 600;
}

.report-timeline li {
  position: relative;
  padding-left: 18px;
}

.report-timeline li::before {
  content: '';
  position: absolute;
  left: 2px;
  top: 0.55em;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--orange);
}

.report-metrics {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(84px, 1fr));
  gap: 8px;
}

.metric {
  display: grid;
  padding: 8px 6px 6px;
  border-radius: 10px;
  background: #f3ede2;
  text-align: center;
}

.metric-value {
  font-size: 22px;
  font-weight: 800;
  line-height: 1;
  color: var(--navy);
}

.metric-label {
  font-size: 12px;
  font-weight: 700;
  color: var(--muted);
}

.report-actions {
  margin-top: 0;
  margin-bottom: 0;
  padding-left: 0;
  list-style: none;
  font-size: 15px;
  font-weight: 600;
}

.report-actions li {
  position: relative;
  padding-left: 26px;
}

.report-actions li + li {
  margin-top: 4px;
}

.report-actions li::before {
  content: '';
  position: absolute;
  left: 0;
  top: 0.2em;
  width: 16px;
  height: 16px;
  border: 2px solid var(--teal);
  border-radius: 4px;
}

.results-buttons .btn {
  min-width: 0;
  flex: 1 1 140px;
}

.manual-share {
  display: none;
  margin-top: 12px;
  padding: 12px;
  border-radius: var(--radius);
  background: var(--paper);
}

.manual-share.is-visible {
  display: block;
}

.manual-share p {
  margin: 0 0 6px;
  font-weight: 700;
}

.manual-text {
  width: 100%;
  font: 600 15px/1.35 var(--font);
  padding: 8px;
  border: 2px solid var(--amber);
  border-radius: 8px;
  resize: none;
  user-select: text;
  -webkit-user-select: text;
  margin-bottom: 8px;
}

/* ---------- Toast ---------- */

.toast {
  position: absolute;
  left: 50%;
  bottom: calc(var(--safe-bottom) + 80px);
  padding: 10px 20px 8px;
  border-radius: 999px;
  background: var(--navy-deep);
  color: #ffffff;
  font-weight: 800;
  font-size: 18px;
  opacity: 0;
  transform: translate(-50%, 12px);
  transition: opacity 200ms ease, transform 200ms ease;
  pointer-events: none;
}

.toast.is-visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

@media (prefers-reduced-motion: reduce) {
  .screen,
  .screen.is-visible,
  .toast,
  .serve-hint {
    transition: none;
    transform: none;
  }
  .toast {
    transform: translate(-50%, 0);
  }
  .serve-hint,
  .serve-hint.is-visible {
    transform: translate(-50%, 0);
    animation: none;
  }
  .stage-card.nope {
    animation: none;
  }
}
