:root {
  color-scheme: light;
  font-family:
    "Nunito", "PingFang SC", "Microsoft YaHei", system-ui, -apple-system,
    sans-serif;
  background: #fff8ee;
  color: #49352a;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  overflow: hidden;
  background:
    radial-gradient(circle at 15% 15%, rgba(143, 202, 241, 0.2), transparent 28%),
    linear-gradient(135deg, #fff3de, #edf8f4 48%, #f6edff);
}

button {
  min-width: 56px;
  min-height: 48px;
  border: 0;
  border-radius: 999px;
  padding: 0 16px;
  font: inherit;
  font-weight: 800;
  color: #513a30;
  background: rgba(255, 255, 255, 0.9);
  box-shadow: 0 6px 16px rgba(92, 68, 50, 0.16);
  cursor: pointer;
}

button:focus-visible {
  outline: 4px solid rgba(143, 202, 241, 0.75);
  outline-offset: 3px;
}

.game-shell {
  position: fixed;
  inset: 0;
  min-height: 100svh;
  padding:
    max(12px, env(safe-area-inset-top))
    max(12px, env(safe-area-inset-right))
    max(12px, env(safe-area-inset-bottom))
    max(12px, env(safe-area-inset-left));
  display: grid;
  place-items: center;
}

.stage-wrap {
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
}

.stage {
  position: relative;
  container-type: size;
  --stage-ui-scale: 1;
  --stage-inset: clamp(8px, 2cqh, 14px);
  --control-bottom: clamp(8px, 2cqh, 14px);
  --control-height: clamp(44px, 7cqh, 48px);
  --control-gap: clamp(6px, 1.4cqh, 10px);
  --caption-gap: clamp(8px, 2cqh, 16px);
  width: min(100%, 1280px);
  aspect-ratio: 16 / 9;
  max-height: calc(100svh - 24px);
  overflow: hidden;
  border-radius: 8px;
  background: #fff7e9;
  box-shadow: 0 18px 50px rgba(91, 70, 53, 0.2);
  touch-action: none;
}

.stage[data-rotated="true"] {
  transform: rotate(90deg);
  transform-origin: center;
}

canvas {
  display: block;
  width: 100%;
  height: 100%;
}

.top-bar {
  position: absolute;
  top: var(--stage-inset);
  left: var(--stage-inset);
  right: var(--stage-inset);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: clamp(8px, 1.6cqh, 12px);
  pointer-events: none;
}

.progress,
.scene-title {
  min-height: clamp(34px, 6.2cqh, 42px);
  border-radius: 999px;
  padding: clamp(6px, 1.5cqh, 9px) clamp(10px, 2.4cqh, 16px);
  display: grid;
  place-items: center;
  color: #513a30;
  background: rgba(255, 255, 255, 0.84);
  box-shadow: 0 6px 16px rgba(92, 68, 50, 0.12);
  font-size: clamp(14px, 3cqh, 24px);
  font-weight: 900;
  line-height: 1.1;
}

.scene-title {
  max-width: min(60%, 420px);
}

.caption {
  position: absolute;
  left: 50%;
  bottom: calc(var(--control-bottom) + var(--control-height) + var(--caption-gap));
  transform: translateX(-50%);
  width: min(86%, 760px);
  min-height: clamp(40px, 7cqh, 48px);
  border-radius: 8px;
  padding: clamp(8px, 1.8cqh, 12px) clamp(12px, 2.6cqh, 18px);
  display: grid;
  place-items: center;
  text-align: center;
  color: #463327;
  background: rgba(255, 255, 255, 0.88);
  box-shadow: 0 8px 20px rgba(92, 68, 50, 0.14);
  font-size: clamp(14px, 3cqh, 22px);
  font-weight: 800;
  line-height: 1.24;
}

.controls {
  position: absolute;
  left: var(--stage-inset);
  bottom: var(--control-bottom);
  right: auto;
  max-width: min(420px, calc(100% - 28px));
  display: flex;
  justify-content: flex-start;
  gap: var(--control-gap);
  flex-wrap: wrap;
  z-index: 3;
}

.controls button {
  min-width: clamp(50px, 10cqh, 56px);
  min-height: clamp(44px, 7cqh, 48px);
  padding-inline: clamp(10px, 2.2cqh, 16px);
  background: rgba(255, 255, 255, 0.9);
  font-size: clamp(15px, 3.1cqh, 18px);
}

.panel {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  gap: clamp(10px, 2.3cqh, 16px);
  padding: clamp(14px, 4cqh, 24px);
  text-align: center;
  background: rgba(255, 248, 238, 0.74);
  backdrop-filter: blur(2px);
}

.panel[hidden],
.caption[hidden],
.debug-panel[hidden] {
  display: none;
}

.panel h1,
.panel h2 {
  margin: 0;
  color: #4c352b;
  font-size: clamp(30px, 12cqh, 76px);
  letter-spacing: 0;
  line-height: 1.05;
}

.panel p {
  max-width: 620px;
  margin: 0 auto;
  color: #61483a;
  font-size: clamp(16px, 4.4cqh, 28px);
  font-weight: 800;
  line-height: 1.35;
}

.panel button {
  justify-self: center;
  min-width: clamp(128px, 26cqh, 156px);
  min-height: clamp(46px, 8cqh, 58px);
  padding-inline: clamp(18px, 4cqh, 26px);
  font-size: clamp(17px, 3.7cqh, 22px);
  background: #ffd55f;
}

.debug-panel {
  position: fixed;
  right: max(12px, env(safe-area-inset-right));
  bottom: max(12px, env(safe-area-inset-bottom));
  width: min(460px, calc(100vw - 24px));
  max-height: min(520px, calc(100svh - 24px));
  border-radius: 8px;
  padding: 14px;
  background: rgba(48, 39, 35, 0.94);
  color: #fffaf2;
  box-shadow: 0 14px 40px rgba(20, 16, 14, 0.35);
  z-index: 10;
}

.debug-panel__header,
.debug-panel__actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.debug-panel pre {
  max-height: 330px;
  overflow: auto;
  padding: 10px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.08);
  font-size: 12px;
  white-space: pre-wrap;
}

.debug-panel button {
  min-width: 58px;
  min-height: 40px;
  padding: 0 12px;
  box-shadow: none;
}

@media (max-height: 560px) {
  .caption {
    bottom: calc(var(--control-bottom) + var(--control-height) + var(--caption-gap));
    min-height: 42px;
    padding: 9px 14px;
  }

  .controls {
    gap: 6px;
  }

  button {
    min-height: 44px;
    padding-inline: 12px;
  }
}

@container (max-height: 360px) {
  .panel {
    gap: 8px;
    padding: 12px;
    padding-bottom: calc(var(--control-bottom) + var(--control-height) + 10px);
  }

  .panel h1,
  .panel h2 {
    font-size: clamp(28px, 10.5cqh, 44px);
  }

  .panel p {
    max-width: min(620px, 76cqw);
    font-size: clamp(15px, 4cqh, 20px);
    line-height: 1.26;
  }

  .panel button {
    min-height: 46px;
    min-width: 136px;
  }

  .caption {
    width: min(76%, 660px);
    min-height: 40px;
    font-size: clamp(13px, 3.4cqh, 17px);
  }
}

@media (max-height: 430px) and (orientation: landscape) {
  .game-shell {
    padding:
      max(4px, env(safe-area-inset-top))
      max(8px, env(safe-area-inset-right))
      max(4px, env(safe-area-inset-bottom))
      max(8px, env(safe-area-inset-left));
  }

  .stage {
    --stage-ui-scale: 0.86;
    --stage-inset: 8px;
    --control-bottom: 8px;
    --control-height: 44px;
    --control-gap: 6px;
    --caption-gap: 7px;
    max-height: calc(100svh - 8px);
  }
}
