/* =========================================================
   砂浜の三目並べ  スタイル
   色・太さの微調整は :root の変数で行えます
   ========================================================= */
:root {
  /* 砂浜と海 */
  --sand: #f8e3b8;
  --sand-deep: #efcf94;
  --wet-sand: #e6c48a;
  --sea-top: #8fe6fb;
  --sea-bottom: #4fcbef;

  /* マス目の溝（明るい縁 / 影 / 溝の底） */
  --groove-hi: #fff6e0;
  --groove-lo: #b98a4e;
  --groove-mid: #d6ab6c;

  /* ○×の溝（マス目より深く濃い） */
  --mark-hi: #fff3d6;
  --mark-lo: #855a2c;
  --mark-mid: #b5803f;

  /* 勝敗表示線 */
  --win-o: #ff4f9a;
  --win-o-lo: #b8125e;
  --win-x: #5b3a1e;
  --win-x-lo: #2b170a;

  /* 文字・ボタン */
  --ink: #6b3f2a;
  --pink: #ff5fa2;
  --pink-deep: #f0388a;
  --orange: #ff9f43;

  /* 画面の実寸。main.js が実際に見えている幅・高さ（px）を --app-w / --app-h に入れる。
     iPad の Safari は横向き・回転直後に vh/svh を実際より大きく返すことがあるため、実測値を優先する */
  --app-w: 100vw;
  --app-h: 100vh;
  --board: min(calc(var(--app-w) * .9), calc(var(--app-h) - 200px), 470px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
}

body {
  background: var(--sand);
  color: var(--ink);
  font-family: "Hiragino Maru Gothic ProN", "Hiragino Sans", "BIZ UDPGothic", "Yu Gothic UI", "Meiryo", system-ui, sans-serif;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
}

.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

/* ---------- 背景 ---------- */
.beach {
  position: fixed; inset: 0; z-index: 0;
  background:
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='180' height='180'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.85' numOctaves='2' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 .55 0 0 0 0 .38 0 0 0 0 .18 .9 0 0 0 -.38'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E"),
    radial-gradient(120% 70% at 50% 60%, #fbe9c4 0%, var(--sand) 55%, var(--sand-deep) 100%);
}
.sea {
  position: absolute; left: 0; right: 0; top: 0;
  height: 11vh; min-height: 56px;
  background: linear-gradient(var(--sea-top), var(--sea-bottom));
}
.wet {
  position: absolute; left: 0; right: 0; top: 11vh; height: 7vh;
  background: linear-gradient(rgba(214, 170, 110, .45), rgba(214, 170, 110, 0));
}
/* 波打ち際（js/waves.js が海と泡を描く）。.sea は位置の基準としてだけ残す */
.waves-on .sea { background: none; }
.shore {
  position: absolute; left: 0; top: 0; width: 100%;
  pointer-events: none;
}
.shore .water { fill: url(#seaGrad); }
.shore .foam { fill: #fff; fill-opacity: .92; }
.shore .ripple { fill: none; stroke: #fff; stroke-opacity: .45; stroke-width: 2; stroke-linecap: round; }

/* ---------- 花 ---------- */
.flowers { position: fixed; inset: 0; z-index: 1; pointer-events: none; }
.flower {
  position: absolute;
  width: var(--size); height: var(--size);
  transform: translate(-50%, -50%) rotate(var(--r)) scale(0);
  opacity: 0;
  transition: transform .7s cubic-bezier(.34, 1.56, .64, 1), opacity .35s ease-out;
}
.flower.on { transform: translate(-50%, -50%) rotate(var(--r)) scale(1); opacity: 1; }
.flower.off {
  transform: translate(-50%, -50%) rotate(var(--r)) scale(.2);
  opacity: 0;
  transition: transform .6s ease-in, opacity .6s ease-in;
}
.flower svg {
  display: block; width: 100%; height: 100%; overflow: visible;
  animation: sway var(--sway, 5s) ease-in-out infinite alternate;
}
@keyframes sway {
  from { transform: rotate(-5deg); }
  to   { transform: rotate(5deg); }
}

/* ---------- 画面構成 ---------- */
/* .stage には z-index を付けない（中の勝敗メッセージだけを演出より手前に出すため）。
   重なり順：花(1) < 盤面・ボタン(2) < 演出(5) < メッセージ(7) < 音ボタン(10) */
.stage {
  position: relative;
  height: var(--app-h);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: clamp(10px, 2.5vh, 22px);
  padding: 16px;
}

.message {
  margin: 0;
  min-height: 2.6em;
  display: flex; align-items: center; justify-content: center; text-align: center;
  padding: .45em 1.2em;
  max-width: 100%;
  border-radius: 999px;
  background: rgba(255, 255, 255, .78);
  box-shadow: 0 4px 14px rgba(170, 110, 50, .15);
  font-weight: 700;
  font-size: clamp(15px, 4.2vw, 20px);
  letter-spacing: .02em;
  position: relative; z-index: 7;
  transition: opacity .3s ease-out, visibility 0s;
}
/* 勝敗メッセージ：演出の上でも読めるよう、背景を濃く・影を強く */
.message.win, .message.lose, .message.draw {
  background: rgba(255, 255, 255, .96);
  box-shadow: 0 6px 22px rgba(60, 30, 40, .28), 0 0 0 3px rgba(255, 255, 255, .6);
  font-size: clamp(17px, 4.8vw, 23px);
}
.message.win { color: var(--pink-deep); animation: pop .5s cubic-bezier(.34, 1.56, .64, 1); }
.message.lose { color: #5d4a7a; }
.message.draw { color: #c4691a; }
/* 演出後は消す（高さは残してレイアウトを動かさない） */
.message.hidden { opacity: 0; visibility: hidden; transition: opacity .3s ease-out, visibility 0s .3s; }

/* ---------- 盤面 ---------- */
.board {
  position: relative; z-index: 2;
  width: var(--board); height: var(--board);
  /* 高さが足りなくても盤面だけ縦に縮まないようにする（縮むとマス目の絵と押せる範囲がずれる） */
  flex: none;
  aspect-ratio: 1 / 1;
  min-width: 240px; min-height: 240px;
  border-radius: 50%;
  /* 盤面周りだけ少しならした砂 */
  background: radial-gradient(closest-side, rgba(255, 246, 222, .85), rgba(255, 246, 222, .5) 72%, rgba(255, 246, 222, 0));
}
.board::before {
  content: ""; position: absolute; inset: -10%; border-radius: 50%;
  background: radial-gradient(closest-side, rgba(255, 140, 200, .5), rgba(255, 210, 120, .25) 60%, transparent);
  opacity: 0; pointer-events: none;
}
.board.glow::before { animation: glow 2.6s ease-out; }
@keyframes glow {
  0% { opacity: 0; transform: scale(.8); }
  20% { opacity: 1; transform: scale(1.05); }
  100% { opacity: 0; transform: scale(1.15); }
}

.board-svg { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; }

.cells {
  position: absolute; inset: 0;
  display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr);
}
.cell {
  appearance: none; -webkit-appearance: none;
  margin: 0; padding: 0; border: 0;
  background: transparent;
  border-radius: 18px;
  cursor: pointer;
  touch-action: manipulation;
  transition: background-color .15s;
}
.cell[aria-disabled="true"] { cursor: default; }
@media (hover: hover) {
  .cell:not([aria-disabled="true"]):hover { background: rgba(255, 255, 255, .32); }
}
.cell:focus-visible { outline: 3px dashed var(--pink); outline-offset: -8px; }

/* 砂の溝 */
.groove path {
  fill: none;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.groove .g-hi  { stroke: var(--groove-hi); }
.groove .g-lo  { stroke: var(--groove-lo); opacity: .75; }
.groove .g-mid { stroke: var(--groove-mid); }
.groove.o .g-hi, .groove.x .g-hi { stroke: var(--mark-hi); }
.groove.o .g-lo, .groove.x .g-lo { stroke: var(--mark-lo); opacity: .9; }
.groove.o .g-mid, .groove.x .g-mid { stroke: var(--mark-mid); }
.groove.win-o .g-lo  { stroke: var(--win-o-lo); }
.groove.win-o .g-mid { stroke: var(--win-o); }
.groove.win-x .g-lo  { stroke: var(--win-x-lo); }
.groove.win-x .g-mid { stroke: var(--win-x); }

.stick-body { stroke: #9a6a3a; stroke-width: 5; stroke-linecap: round; }

/* 引き分け：○×がぱっと消える */
#marks .mark { transform-box: fill-box; transform-origin: center; }
#marks.vanish .mark { animation: vanish .4s ease-in var(--delay, 0ms) forwards; }
@keyframes vanish {
  0%   { transform: scale(1); opacity: 1; }
  35%  { transform: scale(1.14); opacity: 1; }
  100% { transform: scale(.1); opacity: 0; }
}
#marks.fade, #winline.fade { opacity: 0; transition: opacity .25s ease-out; }

/* ---------- もう一回ボタン ---------- */
.actions { position: relative; z-index: 2; width: var(--board); max-width: 100%; min-height: 60px; display: flex; align-items: center; justify-content: center; }
.again {
  appearance: none; border: 0; cursor: pointer;
  min-height: 54px; padding: 0 2.2em;
  border-radius: 999px;
  font: inherit; font-weight: 800; font-size: clamp(17px, 4.6vw, 21px); letter-spacing: .08em;
  color: #fff;
  background: linear-gradient(135deg, #ff7ab8, var(--pink-deep) 60%, var(--orange));
  box-shadow: 0 6px 0 #c7286f, 0 10px 20px rgba(240, 56, 138, .3);
  touch-action: manipulation;
  visibility: hidden; opacity: 0;
  transform: translateY(8px) scale(.9);
  transition: opacity .25s, transform .3s cubic-bezier(.34, 1.56, .64, 1), visibility 0s .3s;
}
.again.show {
  visibility: visible; opacity: 1; transform: none;
  transition: opacity .25s, transform .35s cubic-bezier(.34, 1.56, .64, 1), visibility 0s;
}
.again:active { transform: translateY(3px); box-shadow: 0 3px 0 #c7286f, 0 6px 12px rgba(240, 56, 138, .3); }
.again:focus-visible { outline: 3px solid #fff; outline-offset: 3px; }

/* ---------- 音ボタン ---------- */
.mute {
  position: fixed; z-index: 10;
  top: max(10px, env(safe-area-inset-top)); right: max(10px, env(safe-area-inset-right));
  width: 44px; height: 44px; border-radius: 50%;
  border: 0; padding: 10px; cursor: pointer;
  background: rgba(255, 255, 255, .8);
  box-shadow: 0 3px 10px rgba(0, 80, 120, .15);
  touch-action: manipulation;
}
.mute svg { width: 100%; height: 100%; }
.mute path { fill: none; stroke: var(--pink-deep); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.mute .spk { fill: var(--pink-deep); }
.mute .cross { display: none; }
.mute.off .wave { display: none; }
.mute.off .cross { display: inline; }
.mute.off path { stroke: #a08a7a; }
.mute.off .spk { fill: #a08a7a; }
.mute:focus-visible { outline: 3px dashed var(--pink); outline-offset: 2px; }

/* ---------- 演出 ---------- */
.fx { position: fixed; inset: 0; z-index: 5; pointer-events: none; overflow: hidden; }
.dim {
  position: absolute; inset: 0;
  background: radial-gradient(circle at 50% 40%, rgba(40, 28, 70, .72), rgba(12, 8, 26, .86));
  opacity: 0; transition: opacity .22s ease-out;
}
body.storm .dim { opacity: 1; transition: opacity .1s ease-in; }
body.storm .board { animation: shake .45s .3s; }
@keyframes shake {
  0%, 100% { transform: translate(0, 0); }
  20% { transform: translate(-6px, 3px); }
  40% { transform: translate(5px, -4px); }
  60% { transform: translate(-4px, 2px); }
  80% { transform: translate(3px, -1px); }
}

.flashlayer { position: absolute; inset: 0; background: #fffbe6; opacity: 0; }
.fx.flash .flashlayer { animation: flash .45s linear; }
@keyframes flash {
  0% { opacity: 0; } 8% { opacity: .9; } 20% { opacity: .1; } 32% { opacity: .7; } 100% { opacity: 0; }
}

.bolt {
  position: absolute; top: 0;
  width: min(24vw, 150px); height: 64vh;
  animation: bolt .5s linear forwards;
  filter: drop-shadow(0 0 10px #fff6a0) drop-shadow(0 0 24px #ffe14d);
}
.bolt svg { width: 100%; height: 100%; }
.bolt path { fill: #fffbd1; stroke: #ffe14d; stroke-width: 2; }
@keyframes bolt {
  0% { opacity: 0; } 10% { opacity: 1; } 30% { opacity: .3; } 45% { opacity: 1; } 100% { opacity: 0; }
}

.bigx {
  position: absolute;
  width: var(--sz); height: var(--sz);
  animation: xdrop .42s cubic-bezier(.55, 0, 1, .45) both;
}
.bigx svg { width: 100%; height: 100%; overflow: visible; }
.bigx path { fill: none; stroke: #141018; stroke-width: 17; stroke-linecap: round; }
.bigx.gone { opacity: 0; transition: opacity .2s; }
@keyframes xdrop {
  0%   { transform: translateY(-130vh) rotate(calc(var(--r) - 40deg)); }
  80%  { transform: translateY(0) rotate(var(--r)) scale(1.06, .9); }
  100% { transform: translateY(0) rotate(var(--r)) scale(1); }
}

.sparkle {
  position: absolute;
  width: var(--sz); height: var(--sz);
  margin: calc(var(--sz) / -2) 0 0 calc(var(--sz) / -2);
  background: var(--c);
  clip-path: polygon(50% 0, 61% 39%, 100% 50%, 61% 61%, 50% 100%, 39% 61%, 0 50%, 39% 39%);
  opacity: 0;
  animation: sparkle 1.5s ease-out both;
}
@keyframes sparkle {
  0%   { transform: translate(0, 0) scale(.2) rotate(0); opacity: 0; }
  15%  { opacity: 1; }
  70%  { opacity: 1; }
  100% { transform: translate(var(--dx), var(--dy)) scale(1) rotate(180deg); opacity: 0; }
}

/* 勝ち演出：画面いっぱいに咲く大輪 */
.bloom {
  position: absolute;
  width: var(--size); height: var(--size);
  transform: translate(-50%, -50%) rotate(calc(var(--r) + var(--spin))) scale(0);
  animation: bloomIn .4s cubic-bezier(.3, 1.7, .55, 1) both;
}
.bloom svg { display: block; width: 100%; height: 100%; overflow: visible; }
.bloom.out { animation: bloomOut var(--out, .28s) cubic-bezier(.4, 0, 1, 1) both; }
@keyframes bloomIn {
  0%   { transform: translate(-50%, -50%) rotate(calc(var(--r) + var(--spin))) scale(0); opacity: 0; }
  40%  { opacity: 1; }
  100% { transform: translate(-50%, -50%) rotate(var(--r)) scale(1); opacity: 1; }
}
@keyframes bloomOut {
  from { transform: translate(-50%, -50%) rotate(var(--r)) scale(1); opacity: 1; }
  to   { transform: translate(-50%, -50%) rotate(calc(var(--r) + 8deg)) scale(.85); opacity: 0; }
}

.sparkle.twinkle { animation-name: twinkle; }
@keyframes twinkle {
  0%   { transform: scale(0) rotate(0); opacity: 0; }
  45%  { transform: scale(1.2) rotate(45deg); opacity: 1; }
  100% { transform: scale(0) rotate(90deg); opacity: 0; }
}

@keyframes pop {
  0% { transform: scale(.85); } 100% { transform: scale(1); }
}

.defs { position: absolute; width: 0; height: 0; overflow: hidden; }

/* ---------- 動きを減らす設定 ---------- */
@media (prefers-reduced-motion: reduce) {
  .flower svg { animation: none; }
  body.storm .board { animation: none; }
  .board.glow::before { animation-duration: 1s; }
}

/* 横向きの小さい画面 */
@media (max-height: 480px) and (orientation: landscape) {
  :root { --board: min(calc(var(--app-h) * .7), calc(var(--app-w) * .9)); }
  .stage { gap: 6px; padding: 8px; }
  .message { min-height: 2em; padding: .3em 1em; }
  .actions { min-height: 48px; }
}
