:root {
  --line: #5b3f35;
  --cream: #fff4d6;
  --bg1: #fff8ea;
  --bg2: #ffe9c7;
  --jp: "M PLUS Rounded 1c", "Hiragino Maru Gothic ProN", "Hiragino Maru Gothic Pro", "BIZ UDPGothic", sans-serif;
  --en: "Fredoka", "Arial Rounded MT Bold", var(--jp);
  --bar-h: clamp(64px, 10vh, 92px);
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
}

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

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  background: radial-gradient(circle at 20% 10%, #fffdf6 0, var(--bg1) 40%, var(--bg2) 100%);
  color: var(--line);
  font-family: var(--jp);
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  touch-action: manipulation;
}

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

svg { display: block; }

/* 縁取り文字: 重なった画の内側に線が出ないよう、縁取りを ::before で背面に描く */
.ol { position: relative; isolation: isolate; }
.ol::before {
  content: attr(data-t);
  position: absolute;
  left: 0;
  top: 0;
  z-index: -1;
  color: var(--line);
  -webkit-text-stroke: var(--sw, 0.1em) var(--line);
  white-space: nowrap;
}

.latin { font-family: var(--en); letter-spacing: 0.04em; }

/* ---------- 背景のふわふわ ---------- */
#sky { position: fixed; inset: 0; pointer-events: none; overflow: hidden; z-index: 0; }
.cloud {
  position: absolute;
  border-radius: 50%;
  background: #fff;
  opacity: 0.7;
  animation: drift linear infinite;
}
@keyframes drift {
  from { transform: translateX(-30vw); }
  to { transform: translateX(130vw); }
}

/* ---------- 画面切り替え ---------- */
.screen {
  position: fixed;
  inset: 0;
  z-index: 1;
  display: none;
  flex-direction: column;
  padding: calc(var(--safe-t) + 10px) 12px calc(var(--safe-b) + 10px);
}
.screen.active { display: flex; animation: screen-in 0.45s cubic-bezier(.2, 1.4, .4, 1); }
@keyframes screen-in {
  from { opacity: 0; transform: scale(0.92); }
  to { opacity: 1; transform: none; }
}

/* ---------- ホーム ---------- */
#home { align-items: center; justify-content: center; gap: clamp(20px, 5vh, 56px); }

.logo {
  margin: 0;
  display: flex;
  font-size: clamp(48px, 10vw, 110px);
  line-height: 1;
}
.logo span {
  color: var(--c);
  --sw: 0.09em;
  
  display: inline-block;
  animation: hop 2.4s ease-in-out infinite;
}
.logo span:nth-child(2) { animation-delay: 0.15s; }
.logo span:nth-child(4) { animation-delay: 0.3s; }
.logo span:nth-child(5) { animation-delay: 0.45s; }
.logo span:nth-child(6) { animation-delay: 0.6s; }
.logo .gap { width: 0.35em; animation: none; }
@keyframes hop {
  0%, 60%, 100% { transform: translateY(0); }
  30% { transform: translateY(-0.18em) rotate(-4deg); }
}

.menu {
  display: flex;
  gap: clamp(14px, 3vw, 36px);
  flex-wrap: wrap;
  justify-content: center;
}
.menu-card {
  width: clamp(200px, 28vw, 300px);
  aspect-ratio: 0.85;
  background: var(--c);
  border: 7px solid var(--line);
  border-radius: 44px;
  box-shadow: 0 10px 0 var(--line);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: transform 0.12s, box-shadow 0.12s;
}
.menu-card:active { transform: translateY(8px); box-shadow: 0 2px 0 var(--line); }
.menu-mascot { width: 72%; aspect-ratio: 1; animation: bob 2s ease-in-out infinite; }
.menu-card:nth-child(2) .menu-mascot { animation-delay: -0.6s; }
.menu-card:nth-child(3) .menu-mascot { animation-delay: -1.2s; }
.menu-mascot svg { width: 100%; height: 100%; }
.menu-label {
  font-size: clamp(22px, 3.1vw, 38px);
  white-space: nowrap;
  background: #fff;
  border: 5px solid var(--line);
  border-radius: 999px;
  padding: 0.05em 0.6em 0.1em;
}
@keyframes bob {
  0%, 100% { transform: translateY(0) rotate(-3deg); }
  50% { transform: translateY(-6%) rotate(3deg); }
}

/* ---------- ヘッダー ---------- */
.bar {
  height: var(--bar-h);
  flex: none;
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 10px;
}
.round-btn {
  width: var(--bar-h);
  height: var(--bar-h);
  flex: none;
  background: #fff;
  border: 6px solid var(--line);
  border-radius: 50%;
  box-shadow: 0 6px 0 var(--line);
  padding: calc(var(--bar-h) * 0.12);
  transition: transform 0.1s, box-shadow 0.1s;
}
.round-btn:active { transform: translateY(5px); box-shadow: 0 1px 0 var(--line); }
.round-btn svg { width: 100%; height: 100%; }
.bar-title {
  flex: 1;
  text-align: center;
  font-size: clamp(30px, 5vh, 48px);
}
.bar-title span {
  display: inline-block;
  color: #fff;
  --sw: 0.14em;
  
}
.pill-btn {
  height: var(--bar-h);
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 0 22px 0 14px;
  background: #ff8fab;
  border: 6px solid var(--line);
  border-radius: 999px;
  box-shadow: 0 6px 0 var(--line);
  font-size: clamp(22px, 3.4vh, 32px);
  color: #fff;
  transition: transform 0.1s, box-shadow 0.1s;
}
.pill-btn span { --sw: 0.14em;
   white-space: nowrap; }
.pill-btn:active { transform: translateY(5px); box-shadow: 0 1px 0 var(--line); }
.pill-btn svg { height: 62%; width: auto; aspect-ratio: 1; }
.pill-btn .ico-stop { display: none; }
.playing .pill-btn { background: #5b9bf0; animation: pulse 0.8s ease-in-out infinite; }
.playing .pill-btn .ico-play { display: none; }
.playing .pill-btn .ico-stop { display: block; }
@keyframes pulse { 50% { transform: scale(1.06); } }

/* ---------- 文字表 ---------- */
#grid {
  flex: 1;
  min-height: 0;
  display: grid;
  gap: clamp(5px, 1vmin, 12px);
}
.cell {
  position: relative;
  min-width: 0;
  min-height: 0;
  background: var(--c);
  border: 5px solid var(--line);
  border-radius: 22%/26%;
  box-shadow: 0 5px 0 var(--line);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: visible;
  container-type: size;
  transition: transform 0.1s, box-shadow 0.1s;
}
.cell:active { transform: translateY(4px); box-shadow: 0 1px 0 var(--line); }
.cell.empty { visibility: hidden; }
.cell .ch {
  font-size: min(58cqh, 66cqw);
  line-height: 1;
  color: #fff;
  --sw: 0.1em;
  
  position: relative;
  z-index: 1;
  transform: translateY(-3%);
}
.cell .mini {
  position: absolute;
  width: min(64cqh, 64cqw);
  aspect-ratio: 1;
  right: -14%;
  bottom: -10%;
  z-index: 2;
  animation: peek 3.2s ease-in-out infinite;
  animation-delay: var(--d, 0s);
  pointer-events: none;
}
.cell .mini svg { width: 100%; height: 100%; }
@keyframes peek {
  0%, 100% { transform: rotate(-6deg); }
  50% { transform: rotate(6deg) translateY(-6%); }
}

/* 横長: 伝統的な縦書き五十音表（右から あ・か・さ…）、縦長: 横書き */
.kana #grid { grid-template-columns: repeat(11, 1fr); grid-template-rows: repeat(5, 1fr); }
@media (orientation: portrait) {
  .kana #grid { grid-template-columns: repeat(5, 1fr); grid-template-rows: repeat(11, 1fr); }
  .kana .cell .ch { font-size: min(64cqh, 50cqw); }
  .kana .cell .mini { width: min(60cqh, 36cqw); right: 2%; bottom: -8%; }
  .kana .cell .ch { transform: translate(-8%, -3%); }
}
.abc #grid { grid-template-columns: repeat(7, 1fr); grid-template-rows: repeat(4, 1fr); }
.abc .cell .ch { font-family: var(--en); }
@media (orientation: portrait) {
  .abc #grid { grid-template-columns: repeat(5, 1fr); grid-template-rows: repeat(6, 1fr); }
}

.cell.lit {
  z-index: 3;
  animation: lit 0.45s cubic-bezier(.2, 1.6, .4, 1);
  transform: scale(1.12);
  box-shadow: 0 0 0 6px #fff, 0 0 26px 10px #ffe066;
}
@keyframes lit {
  0% { transform: scale(1); }
  50% { transform: scale(1.28) rotate(-4deg); }
  100% { transform: scale(1.12); }
}
.cell.lit .mini { animation: mini-jump 0.45s ease-out; }
@keyframes mini-jump {
  40% { transform: translateY(-40%) rotate(-10deg) scale(1.2); }
}
.cell.done { filter: saturate(1.25); }
.cell.wave { animation: wave 0.7s ease-in-out; }
@keyframes wave {
  40% { transform: translateY(-18%) scale(1.1); }
}

/* うた・じゅんばん中に真ん中に出る大きなキャラ */
#spot {
  position: absolute;
  left: 50%;
  top: 55%;
  width: min(46vh, 46vw);
  aspect-ratio: 0.8;
  transform: translate(-50%, -50%);
  pointer-events: none;
  z-index: 10;
  display: none;
  flex-direction: column;
  align-items: center;
}
.playing #spot { display: flex; }
#spot::before {
  content: "";
  position: absolute;
  inset: -6%;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(255, 255, 255, 0.95) 45%, rgba(255, 255, 255, 0) 70%);
  z-index: -1;
}
.spot-mascot { width: 100%; aspect-ratio: 1; }
.spot-mascot svg { width: 100%; height: 100%; }
.spot-char {
  font-size: min(20vh, 20vw);
  line-height: 1;
  margin-top: -10%;
  color: var(--c, #ffd84d);
  --sw: 0.09em;
  
}
.abc .spot-char { font-family: var(--en); }
.pop-in { animation: pop-in 0.42s cubic-bezier(.2, 1.7, .4, 1); }
@keyframes pop-in {
  from { transform: scale(0.2) rotate(-20deg); opacity: 0; }
  to { transform: none; opacity: 1; }
}
.playing #grid .cell:not(.lit) { opacity: 0.55; }

/* ---------- 1文字の画面 ---------- */
#stage {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: none;
  align-items: center;
  justify-content: center;
  background:
    radial-gradient(circle at 50% 45%, rgba(255, 255, 255, 0.85) 0, rgba(255, 255, 255, 0) 55%),
    var(--c, #ffd84d);
  touch-action: none;
}
#stage.open { display: flex; animation: stage-in 0.4s cubic-bezier(.2, 1.3, .4, 1); }
@keyframes stage-in {
  from { clip-path: circle(0 at var(--ox, 50%) var(--oy, 50%)); }
  to { clip-path: circle(150% at var(--ox, 50%) var(--oy, 50%)); }
}
#stage::before {
  content: "";
  position: absolute;
  inset: 0;
  background-image: radial-gradient(rgba(255, 255, 255, 0.35) 12%, transparent 13%);
  background-size: 64px 64px;
  animation: dots 12s linear infinite;
  pointer-events: none;
}
@keyframes dots { to { background-position: 64px 64px; } }

.stage-close { position: absolute; top: calc(var(--safe-t) + 14px); left: 14px; z-index: 3; }
.arrow {
  position: absolute;
  top: 50%;
  width: clamp(64px, 9vmin, 96px);
  height: clamp(64px, 9vmin, 96px);
  margin-top: calc(clamp(64px, 9vmin, 96px) / -2);
  background: #fff;
  border: 6px solid var(--line);
  border-radius: 50%;
  box-shadow: 0 6px 0 var(--line);
  padding: clamp(8px, 1.3vmin, 14px);
  z-index: 3;
}
.arrow:active { transform: translateY(5px); box-shadow: 0 1px 0 var(--line); }
.arrow svg { width: 100%; height: 100%; }
.arrow.prev { left: 14px; }
.arrow.next { right: 14px; }

.stage-inner {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: clamp(10px, 4vw, 60px);
  width: 100%;
  padding: 0 clamp(80px, 12vmin, 130px);
}
@media (orientation: portrait) {
  .stage-inner { flex-direction: column; }
}
.stage-mascot-wrap {
  position: relative;
  width: min(62vh, 50vw);
  aspect-ratio: 1;
  flex: none;
}
@media (orientation: portrait) {
  .stage-mascot-wrap { width: min(70vw, 44vh); }
}
.stage-shadow {
  position: absolute;
  left: 22%;
  right: 22%;
  bottom: 2%;
  height: 7%;
  border-radius: 50%;
  background: rgba(91, 63, 53, 0.18);
  animation: shadow 1.6s ease-in-out infinite;
}
@keyframes shadow { 50% { transform: scaleX(0.85); } }
.stage-mascot {
  position: absolute;
  inset: 0;
  cursor: pointer;
  transform-origin: 50% 92%;
  animation: idle 1.6s ease-in-out infinite;
}
@keyframes idle {
  0%, 100% { transform: translateY(0) scale(1, 1); }
  50% { transform: translateY(-2.5%) scale(0.98, 1.02); }
}
.stage-mascot svg { width: 100%; height: 100%; overflow: visible; }
.stage-mascot.enter { animation: enter 0.7s cubic-bezier(.2, 1.5, .4, 1); }
@keyframes enter {
  0% { transform: translateY(-60%) scale(0.3); opacity: 0; }
  60% { transform: translateY(4%) scale(1.1, 0.88); opacity: 1; }
  100% { transform: none; }
}

.stage-text { display: flex; flex-direction: column; align-items: center; gap: 8px; }
.stage-char {
  font-size: min(40vh, 30vw);
  line-height: 1;
  color: #fff;
  --sw: 0.07em;
  
  filter: drop-shadow(0 0.04em 0 var(--line));
}
.abc-stage .stage-char, .abc-stage .stage-word { font-family: var(--en); }
@media (orientation: portrait) {
  .stage-char { font-size: min(26vh, 50vw); }
}
.stage-word {
  font-size: clamp(30px, 6vmin, 64px);
  background: #fff;
  border: 6px solid var(--line);
  border-radius: 999px;
  padding: 0.05em 0.7em 0.12em;
  white-space: nowrap;
}
.stage-word b { color: var(--hl, #ff6b6b); }
.bounce { animation: bounce 0.5s cubic-bezier(.2, 1.6, .4, 1); }
@keyframes bounce {
  30% { transform: scale(1.25, 0.8); }
  60% { transform: scale(0.9, 1.15) translateY(-8%); }
}

/* ---------- マスコット共通のうごき ---------- */
.m-eyes {
  transform-box: fill-box;
  transform-origin: center;
  animation: blink 4.2s infinite;
  animation-delay: var(--blink, 0s);
}
@keyframes blink {
  0%, 94%, 100% { transform: scaleY(1); }
  97% { transform: scaleY(0.1); }
}
.m-arm-l, .m-arm-r { transform-box: fill-box; }
.m-arm-l { transform-origin: 85% 30%; }
.m-arm-r { transform-origin: 15% 30%; }
.stage-mascot .m-arm-l, .spot-mascot .m-arm-l { animation: arm-l 1.6s ease-in-out infinite; }
.stage-mascot .m-arm-r, .spot-mascot .m-arm-r { animation: arm-r 1.6s ease-in-out infinite; }
@keyframes arm-l { 50% { transform: rotate(10deg); } }
@keyframes arm-r { 50% { transform: rotate(-10deg); } }
.waving .m-arm-l { animation: wave-l 0.35s ease-in-out infinite alternate !important; }
.waving .m-arm-r { animation: wave-r 0.35s ease-in-out infinite alternate !important; }
@keyframes wave-l { from { transform: rotate(0); } to { transform: rotate(30deg); } }
@keyframes wave-r { from { transform: rotate(0); } to { transform: rotate(-30deg); } }

.m-happy { display: none; }
.happy .m-eyes { opacity: 0; }
.happy .m-happy { display: inline; }

/* ---------- エフェクト ---------- */
#fx { position: fixed; inset: 0; pointer-events: none; z-index: 50; overflow: hidden; }
.particle { position: absolute; left: 0; top: 0; will-change: transform, opacity; }
.particle svg { width: 100%; height: 100%; }
.bubble {
  position: absolute;
  border-radius: 50%;
  border: 4px solid rgba(91, 63, 53, 0.55);
  background: radial-gradient(circle at 30% 30%, #fff 0 18%, rgba(255, 255, 255, 0.35) 20%);
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; }
}

/* ---------- 小道具（キャラ別エフェクト） ---------- */
.stage-mascot { z-index: 1; }
.stage-prop {
  position: absolute;
  width: calc(var(--s, 0.6) * 100%);
  aspect-ratio: 1;
  bottom: 0;
  pointer-events: none;
}
.stage-prop.behind { left: 50%; transform: translateX(-50%); bottom: -4%; z-index: 0; }
.stage-prop.left { right: 72%; z-index: 0; max-width: 60%; }
.stage-prop.front { left: 50%; transform: translateX(-50%); bottom: -20%; z-index: 2; max-width: 62%; }
.prop-inner { width: 100%; height: 100%; transform-origin: 50% 100%; }
.prop-inner svg { width: 100%; height: 100%; overflow: visible; animation: prop-bob 1.2s ease-in-out infinite; transform-origin: 50% 100%; }
@keyframes prop-bob { 50% { transform: scale(1.03, 0.97); } }

/* ---------- 背景の景色 ---------- */
.scene svg { position: absolute; inset: 0; width: 100%; height: 100%; }
#sky.scene { opacity: 0.9; }
.stage-bg { position: absolute; inset: 0; z-index: 0; overflow: hidden; pointer-events: none; }
.scene-layer { position: absolute; inset: 0; }
/* 真ん中を少し明るくして、キャラと文字を見やすく */
.stage-bg::after {
  content: "";
  position: absolute;
  inset: 0;
  background: radial-gradient(ellipse 55% 50% at 50% 50%, rgba(255, 255, 255, 0.35), rgba(255, 255, 255, 0) 100%);
}
#stage.has-scene::before { display: none; }
/* 画面いっぱいの .stage-inner がボタンを覆わないよう、ボタンを上にする */
.stage-inner { z-index: 2; }
.stage-close, .arrow, .peek { z-index: 4; }

.s-drift { animation: s-drift 16s ease-in-out infinite alternate; }
.s-drift:nth-of-type(2n) { animation-duration: 21s; animation-direction: alternate-reverse; }
@keyframes s-drift { from { transform: translateX(-50px); } to { transform: translateX(50px); } }
.s-sway { transform-box: fill-box; transform-origin: 50% 100%; animation: s-sway 4.5s ease-in-out infinite; }
.s-sway:nth-of-type(2n) { animation-delay: -1.7s; }
@keyframes s-sway { 0%, 100% { transform: rotate(-2.5deg); } 50% { transform: rotate(2.5deg); } }
.s-bob { animation: s-bob 3.2s ease-in-out infinite; }
.s-bob:nth-of-type(2n) { animation-delay: -1.3s; }
@keyframes s-bob { 50% { transform: translateY(-12px); } }
.s-twinkle { animation: s-twinkle 2.6s ease-in-out infinite; }
.s-twinkle:nth-of-type(2n) { animation-delay: -0.9s; }
.s-twinkle:nth-of-type(3n) { animation-delay: -1.7s; }
@keyframes s-twinkle { 50% { opacity: 0.25; } }
.s-spin { transform-box: fill-box; transform-origin: center; animation: s-spin 40s linear infinite; }
@keyframes s-spin { to { transform: rotate(360deg); } }

/* ---------- 前後の文字（左右ボタンの下） ---------- */
.peek {
  position: absolute;
  top: calc(50% + clamp(64px, 9vmin, 96px) / 2 + 16px);
  width: clamp(76px, 11vmin, 116px);
  aspect-ratio: 0.82;
  background: var(--c, #fff);
  border: 5px solid var(--line);
  border-radius: 24px;
  box-shadow: 0 5px 0 var(--line);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding-top: 4px;
  transition: transform 0.1s, box-shadow 0.1s;
}
.peek:active { transform: translateY(4px); box-shadow: 0 1px 0 var(--line); }
.peek.prev { left: max(8px, calc(14px + clamp(64px, 9vmin, 96px) / 2 - clamp(76px, 11vmin, 116px) / 2)); }
.peek.next { right: max(8px, calc(14px + clamp(64px, 9vmin, 96px) / 2 - clamp(76px, 11vmin, 116px) / 2)); }
.peek-m { width: 62%; aspect-ratio: 1; }
.peek-m svg { width: 100%; height: 100%; }
.peek-ch {
  font-size: clamp(24px, 3.8vmin, 40px);
  line-height: 1;
  margin-top: -4px;
  color: #fff;
  --sw: 0.16em;
}
.peek.latin .peek-ch { font-family: var(--en); }

/* ---------- BGM ボタン ---------- */
.bgm-btn { position: relative; }
.bgm-btn .slash { display: none; }
.bgm-off .bgm-btn { background: #e6e0da; }
.bgm-off .bgm-btn .slash { display: inline; }
#home .bgm-btn { position: absolute; top: calc(var(--safe-t) + 14px); right: 14px; }

/* ---------- 生成画像のマスコット ---------- */
.m-img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;
  -webkit-user-drag: none;
  user-select: none;
}
