/* ===== Reset & Base ===== */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

:root {
  --p1: #ff6b9d;
  --p2: #4ecdc4;
  --p3: #ffa726;
  --p4: #ab47bc;
  --radius-card: 14px;
  --font: 'Comic Sans MS', 'Hiragino Maru Gothic ProN', 'Meiryo', 'BIZ UDRound Gothic', sans-serif;
}

html, body {
  height: 100%;
  overflow-x: hidden;
}

body {
  font-family: var(--font);
  background: linear-gradient(160deg, #e8f4ff 0%, #fff0f8 45%, #e8fff3 100%);
  background-attachment: fixed;
  min-height: 100vh;
  color: #333;
}

/* ===== Screen System ===== */
.screen {
  display: none;
  min-height: 100vh;
  padding: 16px;
  justify-content: center;
  align-items: flex-start;
  padding-top: 24px;
}
.screen.active { display: flex; flex-direction: column; align-items: center; }

/* ===== Setup Card ===== */
.screen-card {
  background: rgba(255,255,255,0.94);
  border-radius: 28px;
  padding: 28px 24px;
  width: 100%;
  max-width: 520px;
  box-shadow: 0 12px 48px rgba(0,0,0,0.12), 0 2px 8px rgba(0,0,0,0.06);
  text-align: center;
  position: relative;
}

/* もどるボタンと文字のかぶり解消 */
.screen-card.has-back {
  padding-top: 62px;
}

/* ===== Typography ===== */
h1 {
  font-size: clamp(1.7rem, 6vw, 2.6rem);
  color: var(--p1);
  text-shadow: 2px 3px 0 #ffc8dd;
  margin-bottom: 10px;
}
h2 {
  font-size: clamp(1.2rem, 4.5vw, 1.8rem);
  color: #5b7fe8;
  margin-bottom: 18px;
}
.sub-hint  { font-size: 0.95rem; color: #999; margin-bottom: 12px; }
.cards-hint { margin-top: 16px; font-size: 1rem; color: #888; }

/* ===== Back Button ===== */
.back-btn {
  position: absolute;
  top: 16px;
  left: 16px;
  background: none;
  border: 2px solid #ddd;
  border-radius: 20px;
  padding: 8px 16px;
  font-size: 0.9rem;
  font-family: var(--font);
  cursor: pointer;
  color: #888;
  transition: all 0.2s;
}
.back-btn:hover { border-color: var(--p1); color: var(--p1); }

/* ===== SCREEN 1: Count Buttons ===== */
.count-buttons {
  display: flex;
  gap: 16px;
  justify-content: center;
  margin: 24px 0 8px;
  flex-wrap: wrap;
}
.count-btn {
  width: 110px; height: 110px;
  border-radius: 50%;
  border: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: transform 0.25s cubic-bezier(0.34,1.56,0.64,1), box-shadow 0.2s;
  font-family: var(--font);
  user-select: none;
}
.count-btn:hover, .count-btn:active { transform: scale(1.12) translateY(-4px); }
.btn-pink  { background: linear-gradient(135deg,#ff9a9e,#fecfef); box-shadow: 0 8px 24px rgba(255,107,157,.4); }
.btn-blue  { background: linear-gradient(135deg,#89c4f9,#c2e9fb); box-shadow: 0 8px 24px rgba(78,205,196,.4); }
.btn-green { background: linear-gradient(135deg,#96fbc4,#b8f7c8); box-shadow: 0 8px 24px rgba(150,251,196,.4); }
.count-num   { font-size: 2.4rem; font-weight: bold; color: white; line-height: 1; text-shadow: 0 2px 4px rgba(0,0,0,.2); }
.count-label { font-size: 1rem; color: rgba(255,255,255,.85); font-weight: bold; }

/* ===== SCREEN 2: Name Inputs ===== */
#name-inputs { margin: 4px 0 20px; }
.name-row { display: flex; align-items: center; gap: 12px; margin: 10px 0; }
.player-badge {
  width: 48px; height: 48px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 1.5rem; flex-shrink: 0;
  box-shadow: 0 4px 12px rgba(0,0,0,.15);
}
.name-input {
  flex: 1;
  padding: 12px 18px;
  border: 3px solid #e0e0e0;
  border-radius: 50px;
  font-size: 1.05rem;
  font-family: var(--font);
  outline: none;
  transition: border-color 0.25s, box-shadow 0.25s;
  background: #fafafa;
}
.name-input:focus {
  border-color: var(--p2);
  box-shadow: 0 0 0 4px rgba(78,205,196,.18);
  background: white;
}

/* ===== SCREEN 3: Theme Buttons ===== */
.theme-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  margin: 8px 0 20px;
}
.theme-btn {
  padding: 14px 8px;
  border: 3px solid #e8e8e8;
  border-radius: 18px;
  background: white;
  cursor: pointer;
  font-family: var(--font);
  transition: all 0.25s cubic-bezier(0.34,1.56,0.64,1);
  display: flex; flex-direction: column; align-items: center; gap: 5px;
}
.theme-btn:hover { transform: scale(1.04); border-color: #ccc; }
.theme-btn.selected {
  border-color: var(--p1);
  background: linear-gradient(135deg,#fff0f5,#ffe4ef);
  box-shadow: 0 4px 18px rgba(255,107,157,.28);
  transform: scale(1.05);
}
.theme-emojis { font-size: 1.8rem; line-height: 1; }
.theme-label  { font-size: 0.82rem; font-weight: bold; color: #555; line-height: 1.2; }

/* ===== Buttons ===== */
.primary-btn {
  background: linear-gradient(135deg,#ff6b9d,#ff9a9e);
  color: white; border: none;
  padding: 15px 36px; border-radius: 50px;
  font-size: 1.2rem; font-family: var(--font); font-weight: bold;
  cursor: pointer;
  box-shadow: 0 6px 22px rgba(255,107,157,.45);
  transition: transform 0.25s cubic-bezier(0.34,1.56,0.64,1), box-shadow 0.2s;
  display: inline-block; user-select: none;
}
.primary-btn:hover, .primary-btn:active {
  transform: translateY(-3px) scale(1.04);
  box-shadow: 0 10px 28px rgba(255,107,157,.55);
}
.start-btn { margin-top: 4px; font-size: 1.3rem; width: 100%; }

.secondary-btn {
  background: linear-gradient(135deg,#a1c4fd,#c2e9fb);
  color: white; border: none;
  padding: 13px 28px; border-radius: 50px;
  font-size: 1.05rem; font-family: var(--font);
  cursor: pointer;
  box-shadow: 0 5px 16px rgba(100,160,250,.35);
  transition: transform 0.2s; user-select: none;
}
.secondary-btn:hover { transform: translateY(-2px); }

/* ===== SCREEN 4: Game ===== */
#screen-game {
  padding: 8px;
  align-items: stretch;
  min-height: 100vh;
  overflow: hidden;
  height: 100svh;
}

#game-header {
  background: rgba(255,255,255,.96);
  border-radius: 20px;
  padding: 8px 12px;
  margin-bottom: 8px;
  box-shadow: 0 4px 18px rgba(0,0,0,.1);
  display: flex;
  gap: 7px;
  align-items: center;
  flex-wrap: wrap;
  flex-shrink: 0;
}

/* ===== Home Button ===== */
#home-btn {
  flex-shrink: 0;
  width: 36px; height: 36px;
  border-radius: 50%;
  border: 2px solid #e8e8e8;
  background: white;
  font-size: 1rem;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: all 0.2s;
  box-shadow: 0 2px 8px rgba(0,0,0,.08);
}
#home-btn:hover, #home-btn:active {
  border-color: var(--p1);
  background: #fff0f5;
  transform: scale(1.12);
}

/* ===== Turn Box ===== */
#turn-box {
  display: flex; align-items: center; gap: 5px;
  padding: 7px 13px; border-radius: 50px;
  font-size: 0.9rem; font-weight: bold; color: white;
  flex-shrink: 0; white-space: nowrap;
  transition: background 0.3s;
  box-shadow: 0 4px 12px rgba(0,0,0,.15);
}
#turn-icon   { font-size: 1.1rem; }
#turn-name   { font-size: 0.9rem; }
#turn-suffix { font-size: 0.8rem; opacity: 0.9; }

#scoreboard {
  display: flex; gap: 5px; flex-wrap: wrap;
  align-items: center; flex: 1; justify-content: flex-end;
}
.score-chip {
  padding: 4px 10px; border-radius: 20px;
  font-size: 0.75rem; font-weight: bold; color: white;
  white-space: nowrap;
  box-shadow: 0 2px 8px rgba(0,0,0,.12);
  transition: transform 0.2s;
}
.score-chip.current { transform: scale(1.1); box-shadow: 0 4px 14px rgba(0,0,0,.22); }

/* ===== Card Grid ===== */
#card-grid {
  display: grid;
  gap: 6px;
  width: 100%;
  flex: 1;
  min-height: 0;
  align-content: stretch;
  padding-bottom: 4px;
}

/* 2〜3人は画面内に収める */
#card-grid.fit-screen {
  overflow: hidden;
}
#card-grid.fit-screen .card-wrap {
  height: 100%;
}
#card-grid.fit-screen .card {
  aspect-ratio: unset;
  height: 100%;
}

/* ===== Card ===== */
.card-wrap {
  perspective: 700px;
  cursor: pointer;
  user-select: none;
}
.card-wrap:active .card:not(.flipped):not(.matched) {
  transform: scale(0.93);
}

.card {
  width: 100%;
  aspect-ratio: 3 / 4;
  position: relative;
  transform-style: preserve-3d;
  transition: transform 0.42s cubic-bezier(0.175,0.885,0.32,1.275);
  border-radius: var(--radius-card);
}
.card.flipped { transform: rotateY(180deg); }
.card.matched { animation: matchPop 0.4s ease forwards; }

@keyframes matchPop {
  0%   { transform: rotateY(180deg) scale(1); }
  40%  { transform: rotateY(180deg) scale(1.12); }
  100% { transform: rotateY(180deg) scale(1); }
}

.card-back, .card-front {
  position: absolute; inset: 0;
  border-radius: var(--radius-card);
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  display: flex; align-items: center; justify-content: center;
  overflow: hidden;
}

/* Back face */
.card-back {
  background: linear-gradient(145deg,#6c63ff 0%,#a855f7 50%,#ec4899 100%);
  border: 3px solid rgba(255,255,255,.35);
  box-shadow: 0 4px 14px rgba(108,99,255,.4);
  flex-direction: column; gap: 2px;
}
.card-back-star {
  font-size: clamp(1.4rem, 5vw, 2.4rem);
  filter: drop-shadow(0 0 8px rgba(255,255,255,.9));
  animation: starPulse 2.2s ease-in-out infinite;
}
@keyframes starPulse {
  0%,100% { opacity:.7; transform:scale(1); }
  50%      { opacity:1;  transform:scale(1.18); }
}
.card-back-dots { display:flex; gap:3px; opacity:.3; font-size:.5rem; color:white; }

/* Front face */
.card-front {
  transform: rotateY(180deg);
  flex-direction: column; gap: 3px; padding: 4px;
  border: 3px solid rgba(255,255,255,.8);
  box-shadow: 0 4px 14px rgba(0,0,0,.12);
}

/* 絵柄（絵文字） */
.card-emoji {
  font-size: clamp(2rem, 7vw, 4rem);
  line-height: 1;
  display: block;
  filter: drop-shadow(0 2px 4px rgba(0,0,0,.1));
}

/* 実写写真カード */
.card-img {
  flex: 1;
  width: 100%;
  min-height: 0;
  object-fit: cover;
  border-radius: 8px;
  display: block;
}

.card-label {
  font-size: clamp(0.6rem, 1.6vw, 0.85rem);
  text-align: center;
  font-weight: bold; line-height: 1.2;
  padding: 0 2px; color: rgba(0,0,0,.6);
  flex-shrink: 0;
}

/* Theme colours */
.card-front.trump-red   { background: linear-gradient(145deg,#fff5f5,#ffe0e0); }
.card-front.trump-black { background: linear-gradient(145deg,#f5f5f5,#e8e8e8); }
.card-front.bugs   { background: linear-gradient(145deg,#f0fff4,#d4efdf); }
.card-front.fish   { background: linear-gradient(145deg,#e3f8ff,#c9eaf7); }
.card-front.trains { background: linear-gradient(145deg,#fffde7,#fff3c4); }

/* トランプは数字＋マーク表示なので個別サイズ */
.card-front.trump-red .card-emoji {
  color: #d32f2f;
  font-size: clamp(0.9rem, 3.5vw, 1.6rem);
  font-weight: 900;
}
.card-front.trump-black .card-emoji {
  color: #212121;
  font-size: clamp(0.9rem, 3.5vw, 1.6rem);
  font-weight: 900;
}

/* fit-screen 時は絵柄をさらに大きく */
#card-grid.fit-screen .card-emoji {
  font-size: clamp(1.6rem, 6vh, 3.5rem);
}
#card-grid.fit-screen .card-front.trump-red .card-emoji,
#card-grid.fit-screen .card-front.trump-black .card-emoji {
  font-size: clamp(0.9rem, 3.5vh, 1.8rem);
}

/* Matched glow */
.card.matched .card-front {
  box-shadow: 0 0 0 3px gold, 0 0 24px rgba(255,215,0,.7);
  border-color: gold;
}

/* ===== Message Overlay ===== */
#msg-overlay {
  position: fixed;
  top: 50%; left: 50%;
  transform: translate(-50%,-50%);
  background: rgba(255,255,255,.97);
  border-radius: 24px;
  padding: 18px 32px;
  font-size: clamp(1rem, 3.5vw, 1.4rem);
  font-weight: bold; text-align: center;
  z-index: 200;
  box-shadow: 0 20px 60px rgba(0,0,0,.28);
  min-width: min(280px, 85vw); max-width: 90vw;
  word-break: break-word;
  pointer-events: none;
  animation: popIn 0.28s cubic-bezier(0.34,1.56,0.64,1);
  line-height: 1.5;
}
#msg-overlay.hidden { display: none; }
@keyframes popIn {
  from { transform: translate(-50%,-50%) scale(0.4); opacity:0; }
  to   { transform: translate(-50%,-50%) scale(1);   opacity:1; }
}

/* ===== Confirm Dialog ===== */
#confirm-dialog {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.45);
  z-index: 500;
  display: flex; align-items: center; justify-content: center;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  animation: fadeIn 0.2s ease;
}
#confirm-dialog.hidden { display: none; }
@keyframes fadeIn { from{opacity:0} to{opacity:1} }
#confirm-box {
  background: white; border-radius: 28px;
  padding: 32px 28px 24px; text-align: center;
  box-shadow: 0 24px 64px rgba(0,0,0,.28);
  max-width: 300px; width: 88vw;
  animation: popIn 0.3s cubic-bezier(0.34,1.56,0.64,1);
}
#confirm-emoji { font-size: 3rem; margin-bottom: 12px; }
#confirm-msg   { font-size: 1.2rem; font-weight: bold; color: #444; line-height: 1.5; margin-bottom: 22px; }
#confirm-btns  { display: flex; gap: 12px; justify-content: center; }
.confirm-btn {
  flex: 1; padding: 13px 10px; border: none; border-radius: 50px;
  font-size: 1rem; font-family: var(--font); font-weight: bold;
  cursor: pointer; transition: transform 0.2s;
}
.yes-btn { background: linear-gradient(135deg,#ff6b9d,#ff9a9e); color:white; box-shadow: 0 4px 14px rgba(255,107,157,.4); }
.no-btn  { background: linear-gradient(135deg,#a1c4fd,#c2e9fb); color:white; box-shadow: 0 4px 14px rgba(100,160,250,.35); }
.confirm-btn:hover { transform: scale(1.05); }

/* ===== Sparkles ===== */
.sparkle {
  position: fixed; pointer-events: none; z-index: 300;
  font-size: clamp(1rem,3vw,1.6rem);
  animation: sparkleUp 0.9s ease-out forwards;
}
@keyframes sparkleUp {
  0%   { opacity:1; transform:translateY(0) scale(1) rotate(0deg); }
  100% { opacity:0; transform:translateY(-90px) scale(2.2) rotate(200deg); }
}

/* ===== Confetti ===== */
#confetti-canvas {
  position: fixed; inset: 0;
  width: 100%; height: 100%;
  pointer-events: none; z-index: 400;
}

/* ===== SCREEN 5: End ===== */
.end-card { padding-top: 28px; }
#end-title {
  font-size: clamp(1.3rem,5vw,2rem); font-weight: bold;
  color: var(--p1); text-shadow: 2px 2px 0 #ffc8dd;
  margin-bottom: 6px; line-height: 1.3;
}
#end-subtitle { font-size: clamp(1rem,3.5vw,1.4rem); color: #5b7fe8; margin-bottom: 20px; font-weight: bold; }
#final-rankings { margin: 0 0 20px; }
.rank-row {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 16px; border-radius: 16px; margin: 7px 0;
  font-size: 1rem;
  animation: rankSlide 0.4s ease both;
}
@keyframes rankSlide {
  from { opacity:0; transform:translateX(-20px); }
  to   { opacity:1; transform:translateX(0); }
}
.rank-medal { font-size: 1.8rem; width: 40px; text-align:center; flex-shrink:0; }
.rank-icon  { font-size: 1.3rem; }
.rank-name  { flex:1; text-align:left; font-weight:bold; font-size:.95rem; }
.rank-score { font-weight:bold; white-space:nowrap; opacity:.85; font-size:.9rem; }
.end-btns   { display: flex; flex-direction: column; gap: 10px; align-items: center; }

/* ===== Responsive ===== */
@media (max-width: 420px) {
  .screen-card         { padding: 20px 12px; border-radius: 20px; }
  .screen-card.has-back{ padding-top: 58px; }
  .count-btn           { width: 88px; height: 88px; }
  .count-num           { font-size: 1.9rem; }
  #game-header         { padding: 6px 8px; gap: 5px; }
  #home-btn            { width: 32px; height: 32px; font-size: 0.95rem; }
  #turn-box            { font-size: 0.82rem; padding: 6px 10px; }
  .score-chip          { font-size: 0.68rem; padding: 4px 8px; }
  .primary-btn         { padding: 13px 24px; font-size: 1.05rem; }
  #card-grid           { gap: 5px; }
}
@media (min-width: 700px) {
  #screen-game { align-items: center; }
  #card-grid   { max-width: 960px; margin: 0 auto; }
  #game-header { max-width: 960px; margin-left: auto; margin-right: auto; width: 100%; }
}
