:root {
  color-scheme: light;
  font-family: ui-rounded, "Hiragino Sans GB", "Microsoft YaHei", system-ui, sans-serif;
  background: #e7f6fb;
  color: #283f48;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  overflow: hidden;
}

button {
  min-width: 56px;
  min-height: 52px;
  border: 0;
  border-radius: 8px;
  padding: 0 14px;
  font: inherit;
  font-weight: 800;
  color: #283f48;
  background: #fff8e7;
  box-shadow: 0 6px 0 rgba(40, 63, 72, 0.16);
  cursor: pointer;
}

button:active {
  transform: translateY(2px);
  box-shadow: 0 3px 0 rgba(40, 63, 72, 0.16);
}

.game-shell {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  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));
  background:
    linear-gradient(145deg, #dff6fb 0%, #fff3cf 52%, #e5f7df 100%);
}

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

.stage {
  container-type: size;
  position: relative;
  --stage-inset: clamp(8px, 2cqh, 14px);
  --control-bottom: clamp(8px, 2cqh, 14px);
  --control-height: clamp(44px, 7cqh, 54px);
  --control-gap: clamp(6px, 1.3cqh, 10px);
  --caption-gap: clamp(8px, 2cqh, 16px);
  width: min(100%, calc((100svh - 24px) * 16 / 9));
  aspect-ratio: 16 / 9;
  max-width: 1280px;
  max-height: calc(100svh - 24px);
  overflow: hidden;
  border-radius: 8px;
  background: #cfeef5;
  box-shadow: 0 18px 45px rgba(48, 68, 82, 0.2);
  touch-action: none;
}

.stage[data-rotated="true"] {
  width: min(100svw - 24px, 100svh - 24px);
  max-height: none;
  transform: rotate(90deg);
  transform-origin: center;
}

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

.topbar {
  position: absolute;
  top: var(--stage-inset);
  left: var(--stage-inset);
  right: var(--stage-inset);
  z-index: 3;
  display: flex;
  align-items: center;
  gap: 10px;
  pointer-events: none;
}

.progress,
.scene-title {
  min-height: 40px;
  border-radius: 8px;
  padding: 8px 13px;
  background: rgba(255, 255, 255, 0.84);
  box-shadow: 0 6px 18px rgba(60, 72, 86, 0.12);
  font-weight: 900;
}

.scene-title {
  max-width: min(62%, 520px);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.caption {
  position: absolute;
  z-index: 4;
  left: 50%;
  bottom: calc(var(--control-bottom) + var(--control-height) + var(--caption-gap));
  transform: translateX(-50%);
  width: min(84%, 820px);
  min-height: 46px;
  border-radius: 8px;
  padding: 10px 18px;
  text-align: center;
  font-size: clamp(16px, 3.2cqh, 21px);
  font-weight: 900;
  color: #283f48;
  background: rgba(255, 255, 255, 0.9);
  box-shadow: 0 8px 18px rgba(60, 72, 86, 0.14);
}

.panel {
  position: absolute;
  z-index: 2;
  inset: 0;
  display: grid;
  place-content: center;
  gap: 16px;
  padding: 28px;
  text-align: center;
  background: rgba(232, 250, 252, 0.76);
  backdrop-filter: blur(3px);
}

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

.panel h1,
.panel h2 {
  margin: 0;
  font-size: clamp(32px, 8cqh, 58px);
  letter-spacing: 0;
}

.panel p {
  max-width: 590px;
  margin: 0 auto;
  font-size: clamp(17px, 3.6cqh, 22px);
  font-weight: 800;
}

.primary-button {
  min-width: 170px;
  justify-self: center;
  background: #ffe17a;
}

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

.controls button {
  min-height: var(--control-height);
}

.debug-panel {
  position: fixed;
  right: max(12px, env(safe-area-inset-right));
  bottom: max(86px, env(safe-area-inset-bottom));
  z-index: 6;
  width: min(520px, calc(100svw - 24px));
  max-height: 42svh;
  padding: 12px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.94);
  box-shadow: 0 12px 30px rgba(40, 63, 72, 0.22);
}

.debug-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.debug-actions button {
  min-height: 38px;
  min-width: 52px;
  padding: 0 10px;
  font-size: 14px;
}

#debugLog {
  overflow: auto;
  max-height: calc(42svh - 62px);
  margin: 0;
  font-size: 12px;
  white-space: pre-wrap;
}

@media (orientation: portrait) {
  .game-shell {
    padding-bottom: max(12px, env(safe-area-inset-bottom));
  }

  .stage {
    max-height: none;
  }

  button {
    min-width: 54px;
    min-height: 50px;
    padding: 0 12px;
  }
}

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

  .stage {
    --stage-inset: 8px;
    --control-bottom: 8px;
    --control-height: 44px;
    --control-gap: 7px;
    width: min(100%, calc((100svh - 16px) * 16 / 9));
    max-height: calc(100svh - 16px);
  }

  .panel {
    gap: 8px;
    padding: 14px;
  }

  .panel h1,
  .panel h2 {
    font-size: clamp(26px, 8cqh, 40px);
  }

  .panel p {
    max-width: 480px;
    font-size: clamp(14px, 3.4cqh, 18px);
  }

  .caption {
    bottom: calc(var(--control-bottom) + var(--control-height) + 8px);
    width: min(76%, 720px);
    min-height: 38px;
    padding: 7px 12px;
  }

  button {
    min-width: 50px;
    min-height: 48px;
    padding: 0 10px;
  }
}
