:root {
  --bg: #0b1220;
  --bg-2: #152238;
  --ink: #eef3fb;
  --muted: #9aabc4;
  --line: rgba(255, 255, 255, 0.12);
  --gold: #f0c14b;
  --gold-2: #ffd978;
  --teal: #4fd1c5;
  --rose: #ff7b8a;
  --ok: #7ee0a3;
  --card: #243656;
  --card-2: #1b2c48;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.35);
  --radius: 18px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  background:
    radial-gradient(1200px 600px at 50% -10%, #1d3358 0%, transparent 55%),
    linear-gradient(180deg, var(--bg) 0%, #070b14 100%);
  color: var(--ink);
  font-family: "Yu Gothic UI", "Hiragino Sans", "Meiryo", system-ui, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

body {
  min-height: 100dvh;
  overflow: hidden;
}

body[data-screen="title"] {
  overflow: auto;
}

button {
  font: inherit;
  color: inherit;
  touch-action: manipulation;
}

.screen {
  min-height: 100vh;
  min-height: 100dvh;
}

.screen[hidden] {
  display: none !important;
}

.screen-title {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: calc(20px + var(--safe-top)) 16px calc(24px + var(--safe-bottom));
}

.title-card {
  width: min(520px, 100%);
  background: rgba(15, 24, 40, 0.82);
  border: 1px solid var(--line);
  border-radius: 28px;
  box-shadow: var(--shadow);
  padding: 28px 22px 24px;
  text-align: center;
  backdrop-filter: blur(10px);
}

.kicker {
  margin: 0 0 8px;
  color: var(--teal);
  letter-spacing: 0.12em;
  font-size: 0.78rem;
}

h1 {
  margin: 0;
  font-size: clamp(1.8rem, 6vw, 2.5rem);
  letter-spacing: 0.08em;
}

.sound-bars {
  display: flex;
  justify-content: center;
  align-items: flex-end;
  gap: 6px;
  height: 28px;
  margin: 16px 0 22px;
}

.sound-bars span {
  width: 5px;
  border-radius: 99px;
  background: linear-gradient(180deg, var(--gold-2), var(--teal));
  animation: bars 1.1s ease-in-out infinite;
}

.sound-bars span:nth-child(1) { height: 10px; animation-delay: 0s; }
.sound-bars span:nth-child(2) { height: 18px; animation-delay: 0.12s; }
.sound-bars span:nth-child(3) { height: 26px; animation-delay: 0.24s; }
.sound-bars span:nth-child(4) { height: 16px; animation-delay: 0.08s; }
.sound-bars span:nth-child(5) { height: 12px; animation-delay: 0.2s; }

@keyframes bars {
  50% { transform: scaleY(0.45); }
}

.setup {
  text-align: left;
  margin-bottom: 18px;
}

.setup h2 {
  margin: 0 0 8px;
  font-size: 0.92rem;
  color: var(--muted);
  font-weight: 600;
}

.choices {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.choices-size {
  grid-template-columns: 1fr 1fr 1fr;
}

.choice {
  appearance: none;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.04);
  border-radius: 14px;
  padding: 12px 8px;
  cursor: pointer;
  user-select: none;
}

.choice small {
  display: block;
  margin-top: 2px;
  color: var(--muted);
  font-size: 0.75rem;
}

.choice[data-selected="true"] {
  border-color: var(--gold);
  background: rgba(240, 193, 75, 0.14);
  box-shadow: inset 0 0 0 1px rgba(240, 193, 75, 0.35);
}

.btn {
  appearance: none;
  border: 0;
  border-radius: 999px;
  padding: 13px 18px;
  cursor: pointer;
  user-select: none;
}

.btn-primary {
  width: 100%;
  margin-top: 8px;
  background: linear-gradient(180deg, var(--gold-2), var(--gold));
  color: #2a1d04;
  font-weight: 700;
  letter-spacing: 0.08em;
}

.btn-ghost {
  width: 100%;
  background: transparent;
  border: 1px solid var(--line);
  color: var(--ink);
}

.screen-play {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  padding:
    calc(8px + var(--safe-top))
    8px
    calc(8px + var(--safe-bottom));
  user-select: none;
  -webkit-user-select: none;
}

.play-header {
  flex: 0 0 auto;
  padding: 2px 4px 8px;
}

.players {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: center;
}

.player-chip {
  display: flex;
  align-items: baseline;
  gap: 8px;
  padding: 6px 10px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--line);
  min-width: 0;
}

.player-chip[data-active="true"] {
  border-color: var(--gold);
  background: rgba(240, 193, 75, 0.16);
}

.player-name {
  font-size: 0.82rem;
  font-weight: 700;
}

.player-score {
  font-size: 1rem;
  color: var(--gold-2);
}

.player-score small {
  margin-left: 2px;
  font-size: 0.68rem;
  color: var(--muted);
}

.turn-banner {
  margin: 8px 0 0;
  text-align: center;
  font-size: clamp(1rem, 3.6vw, 1.25rem);
  font-weight: 800;
}

.status-hint {
  margin: 2px 0 0;
  text-align: center;
  color: var(--muted);
  font-size: 0.8rem;
  min-height: 1.2em;
}

.board-wrap {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
}

.board {
  --cols: 5;
  --rows: 4;
  display: grid;
  grid-template-columns: repeat(var(--cols), 1fr);
  grid-template-rows: repeat(var(--rows), 1fr);
  gap: 6px;
  padding: 4px;
  max-width: 100%;
  max-height: 100%;
}

.card {
  appearance: none;
  border: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  border-radius: 12px;
  background: transparent;
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}

.card-inner {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  border-radius: 12px;
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.08), transparent 40%),
    repeating-linear-gradient(
      135deg,
      rgba(255, 255, 255, 0.05) 0 6px,
      transparent 6px 12px
    ),
    linear-gradient(180deg, var(--card) 0%, var(--card-2) 100%);
  border: 1px solid rgba(255, 255, 255, 0.14);
  box-shadow: 0 6px 14px rgba(0, 0, 0, 0.28);
  color: #d5e4ff;
  transition: transform 0.12s ease, box-shadow 0.12s ease, filter 0.12s ease;
}

.card-icon {
  font-size: clamp(0.9rem, 3.4vw, 1.6rem);
  opacity: 0.9;
}

.card.is-selected .card-inner {
  transform: translateY(-2px) scale(1.03);
  border-color: var(--teal);
  box-shadow:
    0 0 0 2px rgba(79, 209, 197, 0.45),
    0 10px 18px rgba(0, 0, 0, 0.35);
  color: #fff;
}

.card.is-match .card-inner {
  border-color: var(--ok);
  box-shadow: 0 0 0 2px rgba(126, 224, 163, 0.5);
}

.card.is-mismatch .card-inner {
  border-color: var(--rose);
  box-shadow: 0 0 0 2px rgba(255, 123, 138, 0.5);
  animation: shake 0.28s ease;
}

.card.is-matched {
  cursor: default;
}

.card.is-matched .card-inner {
  filter: grayscale(0.35) brightness(0.62);
  box-shadow: none;
  opacity: 0.72;
}

.card.is-matched .card-icon {
  opacity: 0.35;
}

.card:disabled {
  cursor: default;
  opacity: 1;
}

@keyframes shake {
  25% { transform: translateX(-3px); }
  75% { transform: translateX(3px); }
}

.screen-result {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(5, 8, 14, 0.72);
  z-index: 5;
}

.screen-result[hidden] {
  display: none;
}

.result-card {
  width: min(440px, 100%);
  background: #152033;
  border: 1px solid var(--line);
  border-radius: 24px;
  padding: 24px 20px 18px;
  box-shadow: var(--shadow);
}

.result-card h2 {
  margin: 0 0 8px;
  text-align: center;
}

.winner {
  margin: 0 0 14px;
  text-align: center;
  color: var(--gold-2);
  font-weight: 800;
}

.ranking {
  list-style: none;
  margin: 0 0 18px;
  padding: 0;
  display: grid;
  gap: 8px;
}

.ranking li {
  display: grid;
  grid-template-columns: 52px 1fr auto;
  gap: 8px;
  align-items: center;
  padding: 10px 12px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.04);
}

.ranking li[data-top="true"] {
  background: rgba(240, 193, 75, 0.14);
}

.rank {
  color: var(--gold);
  font-weight: 800;
}

.result-actions {
  display: grid;
  gap: 8px;
}

@media (max-width: 420px) {
  .title-card {
    padding: 22px 14px 18px;
  }

  .board {
    gap: 4px;
    padding: 2px;
  }

  .card-inner {
    border-radius: 8px;
  }
}

@media (min-width: 900px) {
  .screen-play {
    padding-left: 24px;
    padding-right: 24px;
  }

  .board-wrap {
    max-width: 860px;
    margin: 0 auto;
  }
}
