/*
 * 本体ページの見た目。配色は /shared/themes.css の変数のみを使い、
 * テーマごとの演出(デザイン)は [data-theme="..."] で切り替える。
 *   hotaru: 漂う光の粒 + タップした所が一瞬キラッと光る
 *   iki   : 呼吸する光 + タップした所から波紋
 *   tegami: 便箋にセリフが書き綴られる
 */

/* ---------- 共通 ---------- */

body {
  background: linear-gradient(180deg, var(--bg-1) 0%, var(--bg-2) 100%);
  background-attachment: fixed;
  color: var(--ink);
  overflow: hidden;
  transition: color 0.5s ease;
}

.bg {
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  overflow: hidden;
}

.app {
  position: relative;
  z-index: 1;
  padding-top: calc(36px + env(safe-area-inset-top, 0px));
  gap: 18px;
}

.genre-switcher {
  gap: 4px 18px;
  margin: 0 24px;
  padding: 0 8px 12px;
  border-bottom: 1px solid var(--line);
}

.genre-btn {
  position: relative;
  padding: 6px 2px;
  background: transparent;
  color: var(--sub);
  font-size: 13px;
  font-weight: 500;
  letter-spacing: 0.04em;
  transition: color 0.3s ease;
}

.genre-btn::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  bottom: -13px;
  height: 2px;
  border-radius: 2px;
  background: var(--accent);
  transform: scaleX(0);
  transition: transform 0.3s ease;
}

.genre-btn.is-active {
  color: var(--accent);
}

.genre-btn.is-active::after {
  transform: scaleX(1);
}

.tap-area {
  position: relative;
  overflow: hidden;
  min-height: 0; /* 画面が低いときはここが縮み、下部のテーマ切り替えを押し出さない */
  padding: 24px 36px 64px;
}

.letter {
  position: relative;
  width: 100%;
  max-width: 88vw;
}

.letter__to,
.letter__from {
  display: none;
}

.phrase {
  max-width: none;
  font-family: "Hiragino Mincho ProN", "Yu Mincho", "Noto Serif JP", serif;
  font-weight: 500;
  color: var(--ink);
  font-size: clamp(19px, 5.4vw, 26px);
  line-height: 2;
  /*
   * 改行位置はセリフ本文の \n で指定する(base.css の white-space: pre-line)。
   * 1行が画面幅を超えた場合だけ自動で折り返す。その際、対応ブラウザでは文節単位にする。
   */
  word-break: auto-phrase;
  /* アニメーション中と固定表示で描画レイヤーを揃え、終了時に文字の描画が切り替わって跳ねないようにする */
  will-change: transform;
}

.hint {
  position: absolute;
  left: 0;
  right: 0;
  top: 50%;
  transform: translateY(-50%);
  font-size: 13px;
  letter-spacing: 0.1em;
  color: var(--sub);
  animation: hint-breathe 3.2s ease-in-out infinite;
}

.hint [data-for] {
  display: none;
}

[data-theme="hotaru"] .hint [data-for="hotaru"],
[data-theme="iki"] .hint [data-for="iki"],
[data-theme="tegami"] .hint [data-for="tegami"] {
  display: inline;
}

@keyframes hint-breathe {
  0%,
  100% {
    opacity: 0.45;
  }
  50% {
    opacity: 1;
  }
}

body.has-played .hint {
  animation: none;
  opacity: 0;
}

.footer {
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding-bottom: 20px;
}

.status {
  color: var(--error);
}

/* テーマ専用要素は、該当テーマ以外では出さない */
.orb,
.motes,
.ripple,
.glint {
  display: none;
}

.orb {
  position: absolute;
  border-radius: 50%;
  filter: blur(56px);
  opacity: 0.6;
}

/* ---------- ほたる ---------- */

[data-theme="hotaru"] .motes {
  display: block;
  position: absolute;
  inset: 0;
}

.mote {
  position: absolute;
  width: var(--size, 5px);
  height: var(--size, 5px);
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 10px 3px var(--glow);
  opacity: 0;
  animation-name: mote-rise, mote-twinkle;
  animation-timing-function: linear, ease-in-out;
  animation-iteration-count: infinite, infinite;
}

@keyframes mote-rise {
  from {
    transform: translate(0, 0);
  }
  to {
    transform: translate(20px, -40vh);
  }
}

@keyframes mote-twinkle {
  0%,
  100% {
    opacity: 0.1;
  }
  50% {
    opacity: 0.75;
  }
}

[data-theme="hotaru"] .genre-switcher {
  border-bottom: none;
  gap: 6px;
  margin: 0 16px;
  padding: 0;
}

[data-theme="hotaru"] .genre-btn {
  padding: 7px 14px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--line);
}

[data-theme="hotaru"] .genre-btn::after {
  display: none;
}

[data-theme="hotaru"] .genre-btn.is-active {
  border-color: var(--accent);
  box-shadow: 0 0 12px var(--glow);
}

[data-theme="hotaru"] .phrase {
  font-size: clamp(20px, 5.6vw, 27px);
  text-shadow: 0 0 24px var(--glow);
}

/*
 * 大きくぼんやり現れて、徐々に小さく・くっきりなる演出。
 * 以前は字間(letter-spacing)で大きさを出していたが、字間の変化は折り返し位置の組み替えを起こし、
 * 最終値も固定表示とずれていたため終了時にカクついた。
 * 今は transform: scale で大きさを変える(レイアウトに影響しない)。ぼかしは filter ではなく
 * 「文字色を透明 + text-shadow」で表現し、最終フレームを固定表示と完全に同じ値にしている。
 */
[data-theme="hotaru"] .phrase.is-entering {
  animation: kindle 0.9s ease-out;
}

@keyframes kindle {
  0% {
    opacity: 0;
    transform: scale(1.35);
    color: transparent;
    text-shadow: 0 0 12px var(--ink);
  }
  100% {
    opacity: 1;
    transform: none;
    color: var(--ink);
    text-shadow: 0 0 24px var(--glow);
  }
}

/* タップした所が一瞬キラッと光る: 丸い光のにじみ + 十字の輝き */
[data-theme="hotaru"] .glint {
  display: block;
  position: absolute;
  left: var(--tap-x, 50%);
  top: var(--tap-y, 50%);
  width: 0;
  height: 0;
  pointer-events: none;
}

.glint::before,
.glint::after {
  content: "";
  position: absolute;
  opacity: 0;
}

.glint::before {
  left: -48px;
  top: -48px;
  width: 96px;
  height: 96px;
  border-radius: 50%;
  background: radial-gradient(circle, var(--sparkle) 0%, var(--glow) 30%, transparent 68%);
}

.glint::after {
  left: -38px;
  top: -38px;
  width: 76px;
  height: 76px;
  background: linear-gradient(90deg, transparent, var(--sparkle), transparent) center / 100% 2px
      no-repeat,
    linear-gradient(180deg, transparent, var(--sparkle), transparent) center / 2px 100% no-repeat;
  filter: drop-shadow(0 0 3px var(--sparkle));
}

.tap-area.is-triggered .glint::before {
  animation: glint-glow 0.5s ease-out;
}

.tap-area.is-triggered .glint::after {
  animation: glint-star 0.45s cubic-bezier(0.2, 0.8, 0.3, 1);
}

@keyframes glint-glow {
  0% {
    opacity: 0;
    transform: scale(0.3);
  }
  18% {
    opacity: 1;
    transform: scale(1);
  }
  100% {
    opacity: 0;
    transform: scale(1.35);
  }
}

@keyframes glint-star {
  0% {
    opacity: 0;
    transform: scale(0.2) rotate(0deg);
  }
  22% {
    opacity: 1;
    transform: scale(1) rotate(15deg);
  }
  100% {
    opacity: 0;
    transform: scale(0.5) rotate(45deg);
  }
}

/* ---------- いき ---------- */

[data-theme="iki"] .orb {
  display: block;
  animation: breathe 8s ease-in-out infinite;
}

[data-theme="iki"] .orb--1 {
  width: 300px;
  height: 300px;
  top: -80px;
  left: -80px;
  background: var(--orb-1);
}

[data-theme="iki"] .orb--2 {
  width: 260px;
  height: 260px;
  bottom: -70px;
  right: -70px;
  background: var(--orb-2);
  animation-delay: -4s;
}

[data-theme="iki"] .orb--3 {
  width: 180px;
  height: 180px;
  top: 45%;
  right: -60px;
  background: var(--orb-3);
  animation-delay: -2s;
}

@keyframes breathe {
  0%,
  100% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.14);
  }
}

[data-theme="iki"] .phrase.is-entering {
  animation: fade-drift 0.55s ease-out;
}

@keyframes fade-drift {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

[data-theme="iki"] .ripple {
  display: block;
  position: absolute;
  left: var(--tap-x, 50%);
  top: var(--tap-y, 50%);
  width: 0;
  height: 0;
  pointer-events: none;
}

.ripple::before,
.ripple::after {
  content: "";
  position: absolute;
  left: -110px;
  top: -110px;
  width: 220px;
  height: 220px;
  border-radius: 50%;
  border: 1px solid var(--accent);
  opacity: 0;
}

.tap-area.is-triggered .ripple::before {
  animation: ripple 1.1s ease-out;
}

.tap-area.is-triggered .ripple::after {
  animation: ripple 1.1s ease-out 0.18s;
}

@keyframes ripple {
  0% {
    opacity: 0.55;
    transform: scale(0.1);
  }
  100% {
    opacity: 0;
    transform: scale(1.3);
  }
}

/* ---------- てがみ ---------- */

[data-theme="tegami"] .orb--1,
[data-theme="tegami"] .orb--2 {
  display: block;
  filter: blur(60px);
  opacity: 0.55;
}

[data-theme="tegami"] .orb--1 {
  width: 280px;
  height: 280px;
  top: -60px;
  right: -80px;
  background: var(--orb-1);
}

[data-theme="tegami"] .orb--2 {
  width: 240px;
  height: 240px;
  bottom: -40px;
  left: -80px;
  background: var(--orb-2);
}

[data-theme="tegami"] .tap-area {
  padding: 28px 20px 72px;
}

[data-theme="tegami"] .letter {
  width: min(86vw, 400px);
  padding: 34px 28px 26px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 4px;
  box-shadow: 0 24px 48px -24px var(--glow), 0 2px 6px rgba(0, 0, 0, 0.04);
  text-align: left;
}

/* マスキングテープ(便箋本体は傾けず、文字がにじまないようにする) */
[data-theme="tegami"] .letter::before {
  content: "";
  position: absolute;
  top: -10px;
  left: 50%;
  width: 84px;
  height: 22px;
  background: var(--accent);
  opacity: 0.3;
  transform: translateX(-50%) rotate(-3deg);
}

[data-theme="tegami"] .tap-area.is-triggered .letter {
  animation: lift 0.5s ease;
}

@keyframes lift {
  40% {
    transform: translateY(-4px);
  }
}

[data-theme="tegami"] .letter__to,
[data-theme="tegami"] .letter__from {
  display: block;
  margin: 0;
  font-family: Georgia, "Times New Roman", serif;
  font-style: italic;
  font-size: 14px;
  color: var(--sub);
}

[data-theme="tegami"] .letter__from {
  text-align: right;
  color: var(--accent);
  opacity: 0;
  transition: opacity 0.5s ease 0.5s;
}

body.has-played .letter__from {
  opacity: 1;
}

/* 罫線: 1行の高さ(2.2em)ごとに下線を引く */
[data-theme="tegami"] .letter__body {
  margin: 10px 0 14px;
  font-size: clamp(17px, 4.6vw, 20px);
  min-height: 6.6em;
  background-image: linear-gradient(180deg, transparent calc(100% - 1px), var(--line) calc(100% - 1px));
  background-size: 100% 2.2em;
}

[data-theme="tegami"] .phrase {
  font-size: 1em;
  line-height: 2.2em;
  letter-spacing: 0.04em;
}

[data-theme="tegami"] .phrase.is-entering {
  animation: write 0.9s cubic-bezier(0.3, 0.7, 0.3, 1);
}

@keyframes write {
  from {
    clip-path: inset(0 100% 0 0);
  }
  to {
    clip-path: inset(0 0 0 0);
  }
}

[data-theme="tegami"] .hint {
  top: auto;
  bottom: 24px;
  transform: none;
}

/* ---------- 画面の低い端末(iPhone SE など、ツールバー表示時) ---------- */

@media (max-height: 620px) {
  .app {
    padding-top: calc(20px + env(safe-area-inset-top, 0px));
    gap: 10px;
  }

  .tap-area {
    padding-top: 12px;
    padding-bottom: 32px;
  }

  [data-theme="tegami"] .tap-area {
    padding: 16px 20px 36px;
  }

  [data-theme="tegami"] .letter {
    padding: 26px 24px 18px;
  }

  [data-theme="tegami"] .hint {
    bottom: 10px;
  }

  .footer {
    padding-bottom: 12px;
  }
}
