:root {
  --bg: #faf7f5;
  --surface: #ffffff;
  --text: #222222;
  --muted: #6b6b6b;
  --line: #e4ded9;
  --accent: #e8457a;
  --accent-text: #ffffff;
  --ok: #1f9d55;
  --warn: #d9480f;
  --noimg: #d9d4cf;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #17161a;
    --surface: #222127;
    --text: #f1eff2;
    --muted: #a6a3ab;
    --line: #38363d;
    --accent: #ff6b9a;
    --accent-text: #1a0b10;
    --ok: #4ade80;
    --warn: #fb923c;
    --noimg: #3a383f;
  }
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Noto Sans JP", sans-serif;
  line-height: 1.6;
}
.top {
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}
.top a { color: var(--text); text-decoration: none; font-weight: 700; }
main { max-width: 600px; margin: 0 auto; padding: 16px 16px 120px; }
h1, h2 { line-height: 1.3; }
h2 { font-size: 1.3rem; margin: 8px 0; }
.muted { color: var(--muted); font-size: .9rem; }

.btn {
  display: inline-block;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  text-align: center;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn:disabled { opacity: .4; cursor: default; }
.btn.small { padding: 6px 12px; font-size: .85rem; }
.stack { display: flex; flex-direction: column; gap: 12px; max-width: 360px; }
.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 20px;
  margin: 16px 0;
}
.steps { padding-left: 1.2em; }
.steps li { margin: 6px 0; }

/* 進捗バー */
.progress { height: 6px; background: var(--line); border-radius: 3px; overflow: hidden; margin: 8px 0 12px; }
.progress > div { height: 100%; background: var(--accent); }

/* 予選・本選のカード */
.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}
.card {
  position: relative;
  padding: 0;
  border: 2px solid transparent;
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  overflow: hidden;
  cursor: pointer;
  text-align: left;
  font: inherit;
}
.card .thumb, .noimg { width: 100%; aspect-ratio: 1 / 1; object-fit: cover; display: block; background: var(--noimg); }
.card .meta { padding: 4px 6px 6px; }
.card .name { font-size: .85rem; font-weight: 600; line-height: 1.3; }
.card .grp { font-size: .72rem; color: var(--muted); line-height: 1.3; }
.card[aria-pressed="true"] { border-color: var(--accent); }
.card[aria-pressed="true"]::after {
  content: "✓";
  position: absolute; top: 6px; right: 6px;
  width: 26px; height: 26px; border-radius: 50%;
  background: var(--accent); color: var(--accent-text);
  display: grid; place-items: center; font-weight: 700;
}

/* 下に固定するバー */
.bottombar {
  position: fixed; left: 0; right: 0; bottom: 0;
  background: var(--surface);
  border-top: 1px solid var(--line);
  padding: 10px 16px calc(10px + env(safe-area-inset-bottom));
}
.bottombar .inner { max-width: 600px; margin: 0 auto; display: flex; align-items: center; gap: 8px; }
.bottombar .count { flex: 1; font-size: .9rem; }
.count .ok { color: var(--ok); font-weight: 700; }
.count .over { color: var(--warn); font-weight: 700; }

/* 決勝の2択 */
.vs { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; max-width: 640px; margin: 16px auto; }
.vs .card { border-color: var(--line); }
.vs .card:hover { border-color: var(--accent); }
.vs .name { font-size: 1rem; }
.center { text-align: center; }

/* 順位の微調整 */
.adjust { list-style: none; padding: 0; margin: 0; max-width: 520px; }
.adjust li {
  display: flex; align-items: center; gap: 10px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px;
  padding: 6px 8px; margin-bottom: 6px;
}
.adjust .rank { width: 2.2em; text-align: center; font-weight: 800; font-size: 1.1rem; }
.adjust img, .adjust .noimg { width: 48px; height: 48px; border-radius: 8px; object-fit: cover; }
.adjust .who { flex: 1; min-width: 0; }
.adjust .who div { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* 結果 3×3 */
.result {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px;
  max-width: 600px;
  margin: 16px auto;
}
.cell { position: relative; aspect-ratio: 1 / 1; overflow: hidden; background: var(--noimg); border-radius: 4px; }
.cell.empty { background: var(--line); }
.cell img { width: 100%; height: 100%; object-fit: cover; display: block; }
.cell .rk {
  position: absolute; top: 4px; left: 6px;
  color: #fff; font-weight: 900; font-size: clamp(1.4rem, 7vw, 2.6rem); line-height: 1;
  text-shadow: 0 0 6px rgba(0,0,0,.7), 0 2px 2px rgba(0,0,0,.6);
}
.cell .gp {
  position: absolute; left: 0; right: 0; bottom: 0;
  padding: 14px 6px 4px;
  background: linear-gradient(transparent, rgba(0,0,0,.75));
  color: #fff; font-size: clamp(.65rem, 2.6vw, .9rem); font-weight: 700;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.namelist { max-width: 600px; margin: 0 auto; padding-left: 0; list-style: none; }
.namelist li { padding: 4px 0; border-bottom: 1px solid var(--line); }
.actions { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; margin: 20px 0; }

/* コラージュページ */
.collage-img { display: block; width: 100%; max-width: 600px; margin: 16px auto; border-radius: 4px; }

/* 予選・本選：上の進み具合ゲージ（スクロールしても上に残る） */
.gauge {
  position: sticky; top: 0; z-index: 2;
  margin: -16px -16px 8px; padding: 10px 16px 2px;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
}
.gauge-label { display: flex; justify-content: space-between; font-size: .95rem; }
.gauge .progress { margin: 6px 0 8px; height: 8px; }

/* 確認の小窓 */
.dialog-backdrop {
  position: fixed; inset: 0; z-index: 10;
  background: rgba(0, 0, 0, .45);
  display: grid; place-items: center;
  padding: 16px;
}
.dialog {
  width: 100%; max-width: 360px;
  background: var(--surface); color: var(--text);
  border-radius: 16px; padding: 20px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .25);
}
.dialog-msg { margin: 0 0 16px; white-space: pre-line; }
.dialog-btns { display: flex; gap: 10px; justify-content: flex-end; }

/* 結果のマス：グループ名と名前を2行で（どちらも小さめ） */
.cell .gp small {
  display: block;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  font-size: clamp(.6rem, 2.4vw, .82rem);
}

/* 予選・本選：選んだ人数はゲージの下に、「前へ／次へ」はグループの一番下に */
.gauge .count { font-size: .85rem; padding-bottom: 6px; }
.pager { display: flex; justify-content: space-between; gap: 12px; margin: 20px 0 8px; }
.pager .btn { flex: 1; }
/* スマホでの素早い2回タップで画面が拡大されたり、選択が2回切り替わったりしないように */
.card, .btn { touch-action: manipulation; }

/* トップ画面：遊び方を選ぶ */
.top-lead { font-weight: 700; font-size: 1.05rem; margin: 8px 0 4px; }
.mode-choice h2 { margin-top: 0; }
.mode-choice .btn { width: 100%; }
.resume-note { color: var(--accent); font-size: .85rem; font-weight: 700; margin: 0 0 8px; }