:root {
  --bg: #0b0a24;
  --ink: #3a2410;
  --paper: #f6e2bf;
  --cardboard: #d9a466;
  --cardboard-dark: #b57d42;
  --star: #ffd23f;
  --sky: #4fb3ff;
  --pink: #ff6fa8;
  --white: #fffaf0;
  --font: "M PLUS Rounded 1c", "Hiragino Maru Gothic ProN", "Hiragino Sans", "Meiryo", system-ui, sans-serif;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--white);
  font-family: var(--font);
  font-weight: 800;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  overscroll-behavior: none;
  touch-action: manipulation;
}

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

.hidden { display: none !important; }

button {
  font-family: inherit;
  font-weight: 900;
  border: none;
  cursor: pointer;
  touch-action: manipulation;
}

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

#hud { position: fixed; inset: 0; pointer-events: none; }

.topbar {
  position: absolute;
  top: calc(10px + var(--safe-top));
  left: calc(12px + var(--safe-left));
  right: calc(12px + var(--safe-right));
  display: flex;
  align-items: center;
  gap: 10px;
}

.stars {
  display: flex;
  align-items: center;
  gap: 4px;
  min-width: 92px;
  padding: 4px 14px 4px 8px;
  border-radius: 999px;
  background: rgba(20, 14, 50, 0.6);
  border: 3px solid rgba(255, 255, 255, 0.25);
  font-size: 26px;
  line-height: 1;
  font-variant-numeric: tabular-nums;
}
.stars.bump { animation: bump 0.25s ease-out; }
.icon-star { width: 30px; height: 30px; fill: var(--star); stroke: #a86b00; stroke-width: 1.4; stroke-linejoin: round; }

.progress { position: relative; flex: 1; height: 34px; display: flex; align-items: center; }
.progress .track {
  width: 100%;
  height: 10px;
  border-radius: 99px;
  background: rgba(255, 255, 255, 0.18);
  overflow: hidden;
}
#progressFill { height: 100%; width: 0; background: linear-gradient(90deg, #7fdcff, var(--pink)); border-radius: 99px; }
#progressRocket { position: absolute; left: 0; top: 3px; width: 40px; height: 26px; transform: translateX(-50%); }
#progressRocket svg { width: 100%; height: 100%; }
.progress .flag { position: absolute; right: -6px; top: 0; font-size: 22px; }

.round-btn {
  pointer-events: auto;
  width: 46px;
  height: 46px;
  flex: none;
  border-radius: 50%;
  background: rgba(20, 14, 50, 0.6);
  border: 3px solid rgba(255, 255, 255, 0.25);
  display: grid;
  place-items: center;
  padding: 0;
}
.round-btn svg { width: 26px; height: 26px; fill: var(--white); stroke: var(--white); }
.round-btn .mute { display: none; }
.round-btn.muted .wave { display: none; }
.round-btn.muted .mute { display: inline; }
.round-btn:active { transform: scale(0.92); }

#formBtn {
  pointer-events: auto;
  position: absolute;
  right: calc(18px + var(--safe-right));
  bottom: calc(22px + var(--safe-bottom));
  width: 112px;
  height: 112px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #fff6dc, var(--cardboard) 70%);
  border: 5px solid var(--ink);
  box-shadow: 0 6px 0 var(--ink), 0 0 24px rgba(127, 220, 255, 0.55);
  display: grid;
  place-items: center;
  padding: 10px 0 0;
  transition: transform 0.1s;
}
#formBtn:active { transform: translateY(4px) scale(0.96); box-shadow: 0 2px 0 var(--ink); }
#formBtn svg { width: 74px; height: 46px; margin-top: -6px; }
#formBtn .label {
  position: absolute;
  bottom: 12px;
  font-size: 15px;
  color: var(--ink);
  letter-spacing: 0.05em;
}
#formBtn .to-rocket { display: none; }
#formBtn.is-dolphin .to-rocket { display: block; }
#formBtn.is-dolphin .to-dolphin { display: none; }
#formBtn .ring {
  position: absolute;
  inset: -12px;
  border-radius: 50%;
  border: 5px solid #7fdcff;
  opacity: 0;
}
#formBtn.hint .ring { animation: ring 1s ease-out infinite; }
#formBtn.hint { animation: wiggle 1s ease-in-out infinite; }
#formBtn.pop { animation: popin 0.6s cubic-bezier(.2, 1.6, .4, 1); }
#formBtn.cool { filter: saturate(0.4) brightness(0.8); }

/* ---------------------------------------------------------------- メッセージ */

#message {
  position: fixed;
  left: 0;
  right: 0;
  top: 22%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  pointer-events: none;
  padding: 0 16px;
  text-align: center;
}
.msg {
  word-break: keep-all;
  overflow-wrap: anywhere;
  font-size: clamp(24px, 6.4vw, 40px);
  font-weight: 900;
  line-height: 1.25;
  color: var(--white);
  -webkit-text-stroke: 7px var(--ink);
  paint-order: stroke fill;
  text-shadow: 0 4px 0 var(--ink);
  animation: msgin 0.45s cubic-bezier(.2, 1.5, .4, 1);
}
.msg.big { font-size: clamp(34px, 10vw, 64px); color: var(--star); }
.msg.blue { color: #8fe3ff; }
.msg.pink { color: #ffb3d6; }
.msg.small { font-size: clamp(18px, 4.6vw, 28px); -webkit-text-stroke-width: 6px; }
.msg.out { animation: msgout 0.35s ease-in forwards; }

#countdown {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  pointer-events: none;
  font-size: clamp(90px, 30vw, 180px);
  font-weight: 900;
  color: var(--star);
  -webkit-text-stroke: 12px var(--ink);
  paint-order: stroke fill;
}
#countdown .num { animation: count 0.9s ease-out forwards; }

#flash {
  position: fixed;
  inset: 0;
  pointer-events: none;
  background: #fff;
  opacity: 0;
}

/* ---------------------------------------------------------------- 画面 */

.overlay {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: calc(16px + var(--safe-top)) 16px calc(16px + var(--safe-bottom));
  text-align: center;
  overflow-y: auto;
  touch-action: pan-y;
}
.overlay > .panel { margin: auto 0; }

#title { background: radial-gradient(ellipse at 50% 110%, rgba(11, 10, 36, 0) 30%, rgba(11, 10, 36, 0.55) 100%); justify-content: space-between; }

.title-top, .title-bottom { display: flex; flex-direction: column; align-items: center; gap: 14px; }
.logo { margin-top: 3vh; transform: rotate(-4deg); animation: float 3s ease-in-out infinite; }
.logo-small, .logo-big {
  font-weight: 900;
  -webkit-text-stroke: 9px var(--ink);
  paint-order: stroke fill;
  text-shadow: 0 6px 0 var(--ink);
  line-height: 1.05;
}
.logo-small {
  display: inline-block;
  font-size: clamp(26px, 8vw, 44px);
  color: var(--paper);
  background: var(--cardboard);
  padding: 2px 16px 6px;
  border: 4px solid var(--ink);
  border-radius: 6px;
  -webkit-text-stroke-width: 7px;
  transform: rotate(3deg);
  box-shadow: 0 5px 0 var(--ink);
}
.logo-big { font-size: clamp(50px, 16vw, 96px); color: var(--star); margin-top: 6px; }

.story {
  margin: 0;
  font-size: clamp(17px, 4.6vw, 24px);
  line-height: 1.5;
  -webkit-text-stroke: 5px var(--ink);
  paint-order: stroke fill;
}

.howto {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px 14px;
  font-size: clamp(14px, 3.8vw, 18px);
}
.howto > div { background: rgba(20, 14, 50, 0.6); padding: 6px 12px; border-radius: 999px; border: 2px solid rgba(255, 255, 255, 0.2); }
.pill { margin-right: 2px; }

.big-btn {
  font-size: clamp(26px, 7.5vw, 38px);
  color: var(--ink);
  background: linear-gradient(#ffe680, var(--star));
  padding: 14px 44px 16px;
  border-radius: 999px;
  border: 5px solid var(--ink);
  box-shadow: 0 7px 0 var(--ink);
  transition: transform 0.08s;
}
.big-btn:active { transform: translateY(5px); box-shadow: 0 2px 0 var(--ink); }
#startBtn { animation: pulse 1.4s ease-in-out infinite; }
.sub-btn {
  font-size: 20px;
  color: var(--white);
  background: transparent;
  padding: 10px 24px;
  border-radius: 999px;
  border: 3px solid rgba(255, 255, 255, 0.5);
}
.title-best {
  font-size: 18px;
  color: var(--star);
  -webkit-text-stroke: 5px var(--ink);
  paint-order: stroke fill;
}
.hint-small { margin: 0 0 2vh; font-size: 14px; opacity: 0.8; }

.panel {
  width: min(420px, 100%);
  background: var(--paper);
  color: var(--ink);
  border: 5px solid var(--ink);
  border-radius: 24px;
  box-shadow: 0 8px 0 var(--ink);
  padding: 20px 18px 24px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  background-image:
    repeating-linear-gradient(90deg, rgba(150, 100, 50, 0.06) 0 6px, rgba(255, 255, 255, 0.05) 6px 12px);
  animation: popin 0.5s cubic-bezier(.2, 1.4, .4, 1);
}
.panel h2 { margin: 0; font-size: 30px; }
#fail { background: rgba(11, 10, 36, 0.9); }
#fail p { margin: 0; font-size: 18px; line-height: 1.6; }
#pause, #result { background: rgba(11, 10, 36, 0.55); justify-content: flex-start; }

.goal-title {
  font-size: 46px !important;
  color: var(--star);
  -webkit-text-stroke: 8px var(--ink);
  paint-order: stroke fill;
  text-shadow: 0 5px 0 var(--ink);
}
.result-stars { display: flex; align-items: center; gap: 6px; font-size: 44px; }
.result-stars .icon-star { width: 46px; height: 46px; }
.result-stars .of { font-size: 22px; opacity: 0.7; }
.rank {
  font-size: 30px;
  color: #e53935;
  border: 4px solid #e53935;
  border-radius: 14px;
  padding: 0 14px 2px;
  transform: rotate(-6deg);
  animation: stamp 0.5s 0.6s cubic-bezier(.2, 1.6, .4, 1) both;
}
.best { font-size: 16px; min-height: 1em; }
.best.new { color: #e53935; animation: wiggle 0.8s ease-in-out infinite; }

.zukan {
  width: 100%;
  background: #1a1442;
  color: var(--white);
  border-radius: 16px;
  border: 4px solid var(--ink);
  padding: 10px 12px 12px;
}
.zukan-head {
  display: inline-block;
  font-size: 15px;
  color: var(--ink);
  background: var(--star);
  border-radius: 999px;
  padding: 2px 12px;
}
#zukanSvg { width: 100%; max-width: 280px; height: auto; display: block; margin: 4px auto 0; }
.zukan-name { font-size: 24px; color: #8fe3ff; }
.zukan-text { margin: 4px 0 0; font-size: 14px; line-height: 1.6; font-weight: 700; text-align: left; }

#debug {
  position: fixed;
  left: 6px;
  bottom: 6px;
  font: 12px/1.3 ui-monospace, monospace;
  background: rgba(0, 0, 0, 0.5);
  padding: 4px 6px;
  pointer-events: none;
  white-space: pre;
}

/* ---------------------------------------------------------------- 横長の画面 */

@media (max-height: 500px) {
  .logo { margin-top: 0; }
  .logo-big { font-size: 56px; }
  .logo-small { font-size: 26px; }
  .story { font-size: 16px; }
  .big-btn { font-size: 26px; padding: 8px 32px 10px; }
  #formBtn { width: 92px; height: 92px; padding: 0; }
  #formBtn svg { width: 62px; height: 40px; margin-top: 0; }
  #formBtn .label { display: none; }
  #message { top: 13%; }
  .msg { font-size: 26px; }
  .msg.big { font-size: 40px; }
  .zukan-text { display: none; }

  /* 横向き：タイトルは左に寄せて、右にメカを見せる */
  #title { align-items: flex-start; justify-content: center; gap: 4px; padding-left: calc(24px + var(--safe-left)); }
  #title .title-top, #title .title-bottom { align-items: flex-start; gap: 8px; }
  #title .logo { margin-left: 8px; }
  #title .story { text-align: left; }

  /* 横向き：けっかは 2 列 */
  .result-panel {
    width: min(720px, 100%);
    display: grid;
    grid-template-columns: 1fr 1.15fr;
    grid-template-areas: "title zukan" "stars zukan" "rank zukan" "best zukan" "again zukan";
    justify-items: center;
    align-items: center;
    gap: 4px 16px;
    padding: 12px 16px;
  }
  .goal-title { grid-area: title; font-size: 34px !important; }
  .result-stars { grid-area: stars; font-size: 34px; }
  .rank { grid-area: rank; font-size: 22px; }
  .best { grid-area: best; }
  .zukan { grid-area: zukan; align-self: stretch; display: flex; flex-direction: column; align-items: center; justify-content: center; }
  #zukanSvg { max-height: 170px; }
  #againBtn { grid-area: again; }
}

/* ---------------------------------------------------------------- 動き */

@keyframes bump { 50% { transform: scale(1.18); } }
@keyframes ring { 0% { opacity: 0.9; transform: scale(0.85); } 100% { opacity: 0; transform: scale(1.35); } }
@keyframes wiggle { 0%, 100% { rotate: 0deg; } 25% { rotate: -6deg; } 75% { rotate: 6deg; } }
@keyframes popin { 0% { transform: scale(0.3); opacity: 0; } 100% { transform: scale(1); opacity: 1; } }
@keyframes msgin { 0% { transform: scale(0.4) translateY(20px); opacity: 0; } 100% { transform: scale(1); opacity: 1; } }
@keyframes msgout { to { transform: translateY(-20px) scale(0.9); opacity: 0; } }
@keyframes count { 0% { transform: scale(2.2); opacity: 0; } 25% { transform: scale(1); opacity: 1; } 80% { opacity: 1; } 100% { transform: scale(0.8); opacity: 0; } }
@keyframes pulse { 50% { transform: scale(1.07); } }
@keyframes float { 50% { translate: 0 -8px; } }
@keyframes stamp { 0% { transform: rotate(-6deg) scale(2.5); opacity: 0; } 100% { transform: rotate(-6deg) scale(1); opacity: 1; } }

@media (prefers-reduced-motion: reduce) {
  .logo, #startBtn, #formBtn.hint { animation: none; }
}
