/* ========== 基本 ========== */
:root {
  --bg: #eef2fa;
  --surface: #ffffff;
  --text: #1f2430;
  --muted: #6b7280;
  --border: #e2e6ef;
  --accent: #1e4fd6;
  --shadow: 0 6px 20px rgba(20, 30, 60, 0.12);
  --radius: 14px;

  /* 盤面 */
  --board-hi: #3a7bff;
  --board: #1f55dc;
  --board-lo: #143a9e;
  --board-edge: #0d2a78;
  --hole: #0a1848;
  --hole-lo: #050d2e;

  /* コイン */
  --red-hi: #ff9a9c;
  --red: #e6353b;
  --red-lo: #8f1418;
  --red-rim: #c9262b;
  --yellow-hi: #fff4b8;
  --yellow: #f7c531;
  --yellow-lo: #b7800c;
  --yellow-rim: #d9a41a;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  padding: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans JP", Meiryo, sans-serif;
  background:
    radial-gradient(900px 420px at 50% -80px, #dbe6ff 0%, rgba(219, 230, 255, 0) 70%),
    radial-gradient(600px 400px at 100% 100%, #e6e0ff 0%, rgba(230, 224, 255, 0) 70%),
    var(--bg);
  color: var(--text);
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  min-height: 100vh;
}

.app {
  max-width: 640px;
  margin: 0 auto;
  padding: 16px 16px calc(24px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* ========== ヘッダー ========== */
.header {
  text-align: center;
}

.title {
  margin: 0;
  font-size: 1.7rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  background: linear-gradient(180deg, #2a4fbf, #12307f);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.subtitle {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 0.85rem;
}

/* ========== パネル ========== */
.panel {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 18px;
  align-items: center;
}

.control-group {
  display: flex;
  align-items: center;
  gap: 8px;
}

.control-label {
  font-size: 0.8rem;
  color: var(--muted);
  font-weight: 600;
}

.segmented {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: 999px;
  overflow: hidden;
  background: #f7f8fb;
}

.seg-btn {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  padding: 6px 14px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
  min-width: 44px;
}

.seg-btn:hover {
  background: #eceff6;
}

.seg-btn.is-active {
  background: var(--accent);
  color: #fff;
}

.status-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  border-top: 1px solid var(--border);
  padding-top: 10px;
}

.turn {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  font-size: 1rem;
}

/* 小さいコイン（手番・スコア・結果） */
.turn-coin,
.score-coin,
.result-coin {
  display: inline-block;
  border-radius: 50%;
  flex-shrink: 0;
  box-shadow: inset 0 -2px 3px rgba(0, 0, 0, 0.25), inset 0 2px 2px rgba(255, 255, 255, 0.4), 0 1px 2px rgba(0, 0, 0, 0.25);
}

.turn-coin {
  width: 20px;
  height: 20px;
}

.score-coin {
  width: 12px;
  height: 12px;
}

.turn-coin.red,
.score-coin.red,
.result-coin.red {
  background: radial-gradient(circle at 35% 30%, var(--red-hi), var(--red) 50%, var(--red-lo));
}

.turn-coin.yellow,
.score-coin.yellow,
.result-coin.yellow {
  background: radial-gradient(circle at 35% 30%, var(--yellow-hi), var(--yellow) 50%, var(--yellow-lo));
}

.result-coin.draw {
  background: linear-gradient(90deg, var(--red) 50%, var(--yellow) 50%);
}

.spinner {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  border: 2px solid rgba(30, 79, 214, 0.25);
  border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.score {
  display: flex;
  gap: 12px;
  font-size: 0.85rem;
  color: var(--muted);
}

.score-item {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.score-value {
  font-weight: 800;
  color: var(--text);
  font-variant-numeric: tabular-nums;
  min-width: 1.2em;
  text-align: right;
}

/* ========== 盤面 ========== */
.board-wrap {
  display: flex;
  justify-content: center;
}

.board-frame {
  width: 100%;
  max-width: 560px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* 上部のレール（コインを入れる溝） */
.board-rail {
  width: 96%;
  height: 12px;
  border-radius: 6px 6px 0 0;
  background:
    repeating-linear-gradient(
      90deg,
      transparent 0 calc(100% / 7 - 14px),
      rgba(0, 0, 0, 0.35) calc(100% / 7 - 14px) calc(100% / 7 - 4px),
      transparent calc(100% / 7 - 4px) calc(100% / 7)
    ),
    linear-gradient(180deg, #2c62e8, #173f9e);
  background-position: 7px 0, 0 0;
  box-shadow: inset 0 2px 0 rgba(255, 255, 255, 0.25), inset 0 -2px 3px rgba(0, 0, 0, 0.35);
  margin-bottom: -2px;
}

.board {
  --gap: 6px;
  --pad: 12px;
  --cell: 60px;
  width: 100%;
  background:
    linear-gradient(115deg, rgba(255, 255, 255, 0.22) 0%, rgba(255, 255, 255, 0.06) 30%, rgba(255, 255, 255, 0) 55%),
    linear-gradient(165deg, var(--board-hi) 0%, var(--board) 45%, var(--board-lo) 100%);
  border: 3px solid var(--board-edge);
  border-radius: 18px;
  padding: var(--pad);
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: var(--gap);
  box-shadow:
    inset 0 2px 0 rgba(255, 255, 255, 0.35),
    inset 0 -3px 0 rgba(0, 0, 0, 0.35),
    inset 0 0 0 1px rgba(255, 255, 255, 0.12),
    0 18px 40px rgba(12, 35, 110, 0.45),
    0 4px 10px rgba(12, 35, 110, 0.3);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  cursor: pointer;
  position: relative;
  z-index: 1;
}

/* ネジ（四隅） */
.board::before,
.board::after {
  content: "";
  position: absolute;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #cfe0ff, #5d86e0 55%, #1c3f9c);
  box-shadow: inset 0 1px 1px rgba(255, 255, 255, 0.6), 0 1px 2px rgba(0, 0, 0, 0.5);
  pointer-events: none;
}

.board::before {
  top: 4px;
  left: 4px;
}

.board::after {
  top: 4px;
  right: 4px;
}

.board.is-busy,
.board.is-over {
  cursor: default;
}

.board.shake {
  animation: board-shake 0.26s ease-out;
}

@keyframes board-shake {
  0% {
    transform: translateY(0);
  }
  30% {
    transform: translateY(3px);
  }
  60% {
    transform: translateY(-1.5px);
  }
  100% {
    transform: translateY(0);
  }
}

/* 脚 */
.board-stand {
  width: 100%;
  height: 22px;
  display: flex;
  justify-content: space-between;
  padding: 0 6%;
  margin-top: -6px;
  position: relative;
}

.board-stand span {
  width: 18%;
  height: 100%;
  background: linear-gradient(180deg, var(--board-lo), var(--board-edge));
  clip-path: polygon(20% 0, 80% 0, 100% 100%, 0 100%);
  filter: drop-shadow(0 6px 6px rgba(12, 35, 110, 0.35));
}

.board-stand::after {
  content: "";
  position: absolute;
  left: 4%;
  right: 4%;
  bottom: -8px;
  height: 14px;
  border-radius: 50%;
  background: radial-gradient(ellipse at center, rgba(12, 35, 110, 0.35), rgba(12, 35, 110, 0) 70%);
  z-index: -1;
}

/* 穴 */
.cell {
  position: relative;
  aspect-ratio: 1 / 1;
  border-radius: 50%;
  background: radial-gradient(circle at 50% 40%, var(--hole) 0%, var(--hole-lo) 75%);
  box-shadow:
    inset 0 5px 9px rgba(0, 0, 0, 0.75),
    inset 0 -2px 3px rgba(120, 160, 255, 0.18),
    0 1px 0 rgba(255, 255, 255, 0.22),
    0 -1px 0 rgba(0, 0, 0, 0.35);
  transition: box-shadow 0.12s ease-out;
}

.cell.is-full-col {
  cursor: not-allowed;
}

/* ホバー中の列 */
.cell.col-hover {
  box-shadow:
    inset 0 5px 9px rgba(0, 0, 0, 0.75),
    inset 0 -2px 3px rgba(120, 160, 255, 0.18),
    0 0 0 2px rgba(255, 255, 255, 0.35),
    0 0 10px rgba(160, 200, 255, 0.5);
}

/* コインが通過中のマス */
.cell.passing {
  box-shadow:
    inset 0 5px 9px rgba(0, 0, 0, 0.75),
    inset 0 0 0 3px rgba(255, 255, 255, 0.55),
    0 0 0 2px rgba(255, 255, 255, 0.6),
    0 0 16px rgba(255, 255, 255, 0.6);
}

/* ========== コイン ========== */
.piece {
  position: absolute;
  inset: 5%;
  border-radius: 50%;
  background:
    radial-gradient(circle at 36% 28%, rgba(255, 255, 255, 0.7) 0%, rgba(255, 255, 255, 0.25) 18%, rgba(255, 255, 255, 0) 42%),
    radial-gradient(circle at 50% 60%, var(--c-hi) 0%, var(--c) 40%, var(--c-lo) 100%);
  box-shadow:
    inset 0 -6px 10px rgba(0, 0, 0, 0.3),
    inset 0 4px 6px rgba(255, 255, 255, 0.35),
    inset 0 0 0 2px rgba(0, 0, 0, 0.12),
    0 5px 8px rgba(0, 0, 0, 0.45),
    0 1px 2px rgba(0, 0, 0, 0.4);
  will-change: transform;
}

.piece.red {
  --c-hi: var(--red-hi);
  --c: var(--red);
  --c-lo: var(--red-lo);
  --c-rim: var(--red-rim);
}

.piece.yellow {
  --c-hi: var(--yellow-hi);
  --c: var(--yellow);
  --c-lo: var(--yellow-lo);
  --c-rim: var(--yellow-rim);
}

/* 縁のギザギザ（ミル） */
.piece::before {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 50%;
  background:
    repeating-conic-gradient(from 0deg, rgba(255, 255, 255, 0.35) 0deg 3deg, rgba(0, 0, 0, 0.22) 3deg 6deg),
    var(--c-rim);
  -webkit-mask: radial-gradient(circle, transparent 0 74%, #000 76% 100%);
  mask: radial-gradient(circle, transparent 0 74%, #000 76% 100%);
  opacity: 0.9;
}

/* 内側の浮き彫り */
.piece::after {
  content: "";
  position: absolute;
  inset: 22%;
  border-radius: 50%;
  background:
    radial-gradient(circle at 40% 32%, rgba(255, 255, 255, 0.28), rgba(255, 255, 255, 0) 55%),
    radial-gradient(circle, transparent 0 46%, rgba(0, 0, 0, 0.16) 48% 54%, transparent 56%);
  box-shadow:
    inset 0 2px 4px rgba(0, 0, 0, 0.3),
    inset 0 -2px 3px rgba(255, 255, 255, 0.35),
    0 1px 1px rgba(255, 255, 255, 0.25);
}

/* 落下予定位置のプレビュー（半透明＋白い破線リングで本物と区別） */
.piece.preview {
  opacity: 0.55;
  box-shadow: none;
  pointer-events: none;
  outline: 2px dashed rgba(255, 255, 255, 0.85);
  outline-offset: 2px;
  animation: preview-pulse 1.1s ease-in-out infinite;
}

.piece.preview::before,
.piece.preview::after {
  opacity: 0.5;
}

@keyframes preview-pulse {
  0%,
  100% {
    opacity: 0.4;
    transform: scale(0.92);
  }
  50% {
    opacity: 0.65;
    transform: scale(1);
  }
}

/* 落下中のコインを載せる箱（盤面座標で絶対配置） */
.mover {
  position: absolute;
  z-index: 5;
  pointer-events: none;
  will-change: transform;
}

.mover .piece {
  box-shadow:
    inset 0 -6px 10px rgba(0, 0, 0, 0.3),
    inset 0 4px 6px rgba(255, 255, 255, 0.35),
    0 10px 14px rgba(0, 0, 0, 0.45);
}

/* 着地の衝撃リング */
.impact {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  border: 3px solid rgba(255, 255, 255, 0.9);
  box-shadow: 0 0 12px rgba(255, 255, 255, 0.7), inset 0 0 8px rgba(255, 255, 255, 0.5);
  pointer-events: none;
  z-index: 3;
  animation: impact 0.5s ease-out forwards;
}

@keyframes impact {
  from {
    transform: scale(0.7);
    opacity: 0.95;
  }
  to {
    transform: scale(1.8);
    opacity: 0;
  }
}

/* 着地の火花 */
.burst {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 4;
}

.burst.red {
  --spark: #ffc4c4;
}

.burst.yellow {
  --spark: #fff3b0;
}

.burst i {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 13%;
  height: 13%;
  margin: -6.5% 0 0 -6.5%;
  border-radius: 50%;
  background: radial-gradient(circle, #fff 0%, var(--spark) 60%);
  box-shadow: 0 0 6px var(--spark);
  transform: rotate(var(--a)) translateY(0) scale(1);
  animation: spark 0.55s cubic-bezier(0.15, 0.8, 0.3, 1) forwards;
}

@keyframes spark {
  0% {
    transform: rotate(var(--a)) translateY(0) scale(1);
    opacity: 1;
  }
  100% {
    transform: rotate(var(--a)) translateY(calc(var(--cell) * -0.85 * var(--d, 1))) scale(0.15);
    opacity: 0;
  }
}

/* ========== 勝利演出 ========== */
.piece.winning {
  animation: pulse 0.8s ease-in-out infinite;
  box-shadow:
    inset 0 -6px 10px rgba(0, 0, 0, 0.3),
    inset 0 4px 6px rgba(255, 255, 255, 0.35),
    0 0 0 3px #fff,
    0 0 0 6px rgba(255, 200, 60, 0.95),
    0 0 26px 10px rgba(255, 225, 110, 0.95);
  z-index: 2;
}

@keyframes pulse {
  0%,
  100% {
    transform: scale(1);
    filter: brightness(1);
  }
  50% {
    transform: scale(0.9);
    filter: brightness(1.25);
  }
}

/* 4つを貫く光のライン */
.win-line {
  position: absolute;
  height: var(--thick, 14px);
  border-radius: 999px;
  background: linear-gradient(90deg, rgba(255, 255, 255, 0.6), #fff 20%, #fff 80%, rgba(255, 255, 255, 0.6));
  box-shadow: 0 0 10px 3px #fff, 0 0 24px 8px rgba(255, 225, 110, 0.95), 0 0 50px 16px rgba(255, 200, 60, 0.6);
  transform-origin: 0 50%;
  pointer-events: none;
  z-index: 6;
  mix-blend-mode: screen;
}

/* 盤面全体のフラッシュ */
.win-flash {
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: radial-gradient(circle at 50% 50%, #fff 0%, rgba(255, 240, 180, 0.9) 40%, rgba(255, 255, 255, 0) 80%);
  pointer-events: none;
  z-index: 7;
  animation: win-flash 0.7s ease-out forwards;
}

@keyframes win-flash {
  0% {
    opacity: 0;
  }
  15% {
    opacity: 0.95;
  }
  100% {
    opacity: 0;
  }
}

.board.win-shake {
  animation: board-win-shake 0.7s cubic-bezier(0.36, 0.07, 0.19, 0.97);
}

@keyframes board-win-shake {
  0%,
  100% {
    transform: translate(0, 0) rotate(0);
  }
  10% {
    transform: translate(-6px, 4px) rotate(-0.6deg);
  }
  20% {
    transform: translate(6px, -3px) rotate(0.6deg);
  }
  30% {
    transform: translate(-5px, 2px) rotate(-0.4deg);
  }
  40% {
    transform: translate(5px, -2px) rotate(0.4deg);
  }
  50% {
    transform: translate(-3px, 2px);
  }
  60% {
    transform: translate(3px, -1px);
  }
  70% {
    transform: translate(-2px, 1px);
  }
  80% {
    transform: translate(2px, 0);
  }
  90% {
    transform: translate(-1px, 0);
  }
}

/* 紙吹雪（画面全体） */
.confetti {
  position: fixed;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
  z-index: 11;
}

.confetti i {
  position: absolute;
  top: -6vh;
  left: var(--x);
  width: var(--w);
  height: var(--h);
  background: var(--c);
  border-radius: 2px;
  opacity: 0;
  transform: rotate(var(--r));
  animation:
    confetti-fall var(--t) cubic-bezier(0.25, 0.4, 0.6, 1) var(--delay) forwards,
    confetti-sway calc(var(--t) / 3) ease-in-out var(--delay) infinite alternate;
}

.confetti i.round {
  border-radius: 50%;
}

@keyframes confetti-fall {
  0% {
    transform: translateY(0) rotate(var(--r));
    opacity: 0;
  }
  8% {
    opacity: 1;
  }
  85% {
    opacity: 1;
  }
  100% {
    transform: translateY(118vh) rotate(calc(var(--r) + 720deg));
    opacity: 0;
  }
}

@keyframes confetti-sway {
  from {
    margin-left: -3vw;
  }
  to {
    margin-left: 3vw;
  }
}

/* 結果カードの見出しを弾ませる */
.result-title.is-win {
  animation: title-bounce 0.7s cubic-bezier(0.2, 1.4, 0.4, 1) both;
  background: linear-gradient(180deg, #ffd35c, #e08a00);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  text-shadow: none;
}

@keyframes title-bounce {
  0% {
    transform: scale(0.4);
    opacity: 0;
  }
  60% {
    transform: scale(1.15);
    opacity: 1;
  }
  100% {
    transform: scale(1);
  }
}

.result-coin.is-win {
  animation: coin-spin 1.2s ease-in-out infinite;
}

@keyframes coin-spin {
  0%,
  100% {
    transform: rotateY(0) scale(1);
  }
  50% {
    transform: rotateY(180deg) scale(1.08);
  }
}

/* 勝利時、勝利ライン以外を少し暗くして目立たせる */
.board.is-over .piece:not(.winning) {
  filter: brightness(0.7) saturate(0.85);
}

/* ========== ボタン ========== */
.actions {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 10px;
}

.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 700;
  font-size: 0.95rem;
  padding: 10px 18px;
  border-radius: 999px;
  cursor: pointer;
  box-shadow: 0 2px 6px rgba(20, 30, 60, 0.08);
  transition: transform 0.1s, background 0.15s, opacity 0.15s;
  min-width: 110px;
}

.btn:hover:not(:disabled) {
  background: #f2f4fa;
}

.btn:active:not(:disabled) {
  transform: translateY(1px);
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-primary {
  background: linear-gradient(180deg, #2f63e8, #1a45bd);
  border-color: #1a45bd;
  color: #fff;
}

.btn-primary:hover:not(:disabled) {
  background: linear-gradient(180deg, #2a5ad8, #163ba6);
}

.hint {
  margin: 0;
  text-align: center;
  color: var(--muted);
  font-size: 0.78rem;
}

/* ========== 結果オーバーレイ ========== */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(20, 30, 60, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 10;
  animation: fade-in 0.2s ease-out;
}

@keyframes fade-in {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

.overlay-card {
  background: var(--surface);
  border-radius: 20px;
  padding: 28px 24px 22px;
  width: min(100%, 340px);
  text-align: center;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.3);
  animation: pop-in 0.25s cubic-bezier(0.2, 0.9, 0.4, 1.2);
}

@keyframes pop-in {
  from {
    transform: scale(0.85);
    opacity: 0;
  }
  to {
    transform: scale(1);
    opacity: 1;
  }
}

.result-coin {
  width: 64px;
  height: 64px;
  margin: 0 auto 12px;
  box-shadow: inset 0 -5px 8px rgba(0, 0, 0, 0.25), inset 0 4px 5px rgba(255, 255, 255, 0.4), 0 6px 14px rgba(0, 0, 0, 0.25);
}

.result-title {
  margin: 0;
  font-size: 1.6rem;
  font-weight: 800;
}

.result-sub {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 0.9rem;
  min-height: 1.2em;
}

.overlay-actions {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin-top: 18px;
  flex-wrap: wrap;
}

/* ========== レスポンシブ ========== */
@media (max-width: 420px) {
  .app {
    padding: 12px 12px calc(20px + env(safe-area-inset-bottom));
    gap: 10px;
  }

  .title {
    font-size: 1.4rem;
  }

  .board {
    --gap: 4px;
    --pad: 8px;
    border-radius: 14px;
    border-width: 2px;
  }

  .board::before,
  .board::after {
    width: 7px;
    height: 7px;
    top: 3px;
  }

  .board::before {
    left: 3px;
  }

  .board::after {
    right: 3px;
  }

  .board-rail {
    height: 9px;
  }

  .board-stand {
    height: 16px;
  }

  .panel {
    padding: 10px 12px;
  }

  .seg-btn {
    padding: 6px 11px;
    font-size: 0.8rem;
  }

  .score {
    gap: 8px;
    font-size: 0.8rem;
  }

  .btn {
    min-width: 0;
    padding: 9px 14px;
    font-size: 0.9rem;
  }
}

/* 横長の低い画面（スマホ横向き）では盤面の高さを抑える */
@media (max-height: 560px) and (orientation: landscape) {
  .board-frame {
    max-width: min(560px, calc((100vh - 240px) * 7 / 6));
  }
}

@media (prefers-reduced-motion: reduce) {
  .piece.winning,
  .board.shake,
  .board.win-shake,
  .win-line,
  .win-flash,
  .confetti i,
  .result-title.is-win,
  .result-coin.is-win,
  .impact,
  .burst i,
  .overlay,
  .overlay-card,
  .spinner {
    animation: none;
  }
}
