/* ===== シュールソート ===== */
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  font-family: 'Zen Maru Gothic', 'Hachi Maru Pop', system-ui, sans-serif;
  background: linear-gradient(135deg, #2a1b56 0%, #5a2c8c 50%, #d56fcf 100%);
  color: #fff;
  min-height: 100vh;
  min-height: 100dvh;
  overflow-x: hidden;
  user-select: none;
  -webkit-user-select: none;
}

#game-wrapper {
  width: 100%;
  max-width: 520px;
  margin: 0 auto;
  min-height: 100vh;
  min-height: 100dvh;
  position: relative;
  padding: 12px;
  display: flex;
  flex-direction: column;
}

/* ===== 画面切替 ===== */
.screen { display: none; flex: 1; flex-direction: column; }
.screen.active { display: flex; }

/* ===== タイトル ===== */
.title-inner {
  margin: auto;
  text-align: center;
  padding: 24px 16px;
  max-width: 420px;
  width: 100%;
}
.title-emoji {
  font-size: 80px;
  margin-bottom: 8px;
  filter: drop-shadow(0 4px 8px rgba(0,0,0,0.3));
  animation: spin 3s linear infinite;
}
@keyframes spin {
  0% { transform: rotateY(0deg); }
  100% { transform: rotateY(360deg); }
}
.game-title {
  font-family: 'Hachi Maru Pop', sans-serif;
  font-size: 38px;
  margin: 4px 0;
  text-shadow: 0 2px 8px rgba(0,0,0,0.4);
  letter-spacing: 0.04em;
}
.game-subtitle {
  font-size: 14px;
  margin: 0 0 18px;
  opacity: 0.85;
}
.progress-display {
  font-size: 14px;
  background: rgba(0,0,0,0.25);
  padding: 8px 14px;
  border-radius: 999px;
  display: inline-block;
  margin-bottom: 18px;
  border: 1px solid rgba(255,255,255,0.18);
}

/* ===== ボタン ===== */
.btn-primary, .btn-secondary {
  display: block;
  width: 100%;
  max-width: 280px;
  margin: 8px auto;
  padding: 14px 22px;
  font-size: 17px;
  font-weight: bold;
  border: none;
  border-radius: 999px;
  cursor: pointer;
  font-family: inherit;
  transition: transform .08s ease, box-shadow .12s ease, filter .12s ease;
  box-shadow: 0 4px 14px rgba(0,0,0,0.25);
}
.btn-primary {
  background: linear-gradient(135deg, #ffd166, #ff7e6c);
  color: #422;
}
.btn-secondary {
  background: rgba(255,255,255,0.12);
  color: #fff;
  border: 1px solid rgba(255,255,255,0.3);
}
.btn-primary:hover, .btn-secondary:hover { filter: brightness(1.06); }
.btn-primary:active, .btn-secondary:active { transform: translateY(2px); box-shadow: 0 2px 6px rgba(0,0,0,0.25); }
.btn-icon {
  background: rgba(0,0,0,0.25);
  border: 1px solid rgba(255,255,255,0.2);
  color: #fff;
  border-radius: 10px;
  width: 36px;
  height: 36px;
  font-size: 18px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-family: inherit;
  transition: background .12s, transform .08s;
}
.btn-icon:hover { background: rgba(0,0,0,0.4); }
.btn-icon:active { transform: scale(0.92); }
.btn-icon:disabled { opacity: 0.35; cursor: not-allowed; }

.controls-help {
  background: rgba(0,0,0,0.25);
  border-radius: 14px;
  padding: 14px 16px;
  margin-top: 16px;
  font-size: 13px;
  line-height: 1.7;
  text-align: left;
  border: 1px solid rgba(255,255,255,0.1);
}
.help-title {
  margin: 0 0 6px;
  font-weight: bold;
  font-size: 14px;
  text-align: center;
  color: #ffd166;
}
.controls-help p { margin: 4px 0; }

/* ===== ステージ選択 ===== */
.select-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 4px 0 10px;
}
.select-title {
  margin: 0;
  font-size: 22px;
  font-family: 'Hachi Maru Pop', sans-serif;
}
.stage-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 10px;
  padding: 8px 4px 24px;
  overflow-y: auto;
  max-height: calc(100vh - 90px);
  max-height: calc(100dvh - 90px);
}
.stage-cell {
  aspect-ratio: 1 / 1;
  background: rgba(255,255,255,0.1);
  border: 2px solid rgba(255,255,255,0.18);
  border-radius: 12px;
  color: #fff;
  font-size: 16px;
  font-weight: bold;
  font-family: inherit;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  position: relative;
  transition: transform .08s, background .12s, border-color .12s;
}
.stage-cell:hover { background: rgba(255,255,255,0.18); }
.stage-cell:active { transform: scale(0.94); }
.stage-cell--cleared {
  background: linear-gradient(135deg, rgba(255,209,102,0.5), rgba(255,126,108,0.5));
  border-color: #ffd166;
}
.stage-cell--cleared::after {
  content: '⭐';
  position: absolute;
  top: 2px;
  right: 4px;
  font-size: 12px;
}
.stage-cell--locked {
  opacity: 0.35;
  cursor: not-allowed;
}
.stage-cell--current {
  outline: 3px solid #ffd166;
  outline-offset: -3px;
  animation: pulse 1.4s ease-in-out infinite;
}
@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(255,209,102,0.5); }
  50% { box-shadow: 0 0 0 6px rgba(255,209,102,0); }
}

/* ===== ゲームHUD ===== */
.game-hud {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 2px 10px;
  gap: 6px;
}
.hud-center {
  display: flex;
  gap: 12px;
  align-items: center;
}
.hud-stage, .hud-moves {
  display: flex;
  align-items: baseline;
  gap: 4px;
  background: rgba(0,0,0,0.25);
  padding: 6px 10px;
  border-radius: 10px;
  border: 1px solid rgba(255,255,255,0.15);
}
.hud-label { font-size: 11px; opacity: 0.75; }
.hud-value { font-size: 18px; font-weight: bold; color: #ffd166; }
.hud-actions { display: flex; gap: 6px; }

/* ===== ボード ===== */
.board {
  flex: 1;
  display: flex;
  flex-wrap: wrap;
  align-content: center;
  justify-content: center;
  gap: 10px;
  padding: 10px 4px 20px;
  position: relative;
}

.tube {
  --tube-w: 56px;
  --slot-h: 48px;
  --capacity: 4;
  width: var(--tube-w);
  height: calc(var(--slot-h) * var(--capacity) + 16px);
  background: rgba(255,255,255,0.08);
  border: 3px solid rgba(255,255,255,0.35);
  border-top: 2px dashed rgba(255,255,255,0.4);
  border-radius: 0 0 28px 28px;
  display: flex;
  flex-direction: column-reverse;
  align-items: center;
  padding: 6px 4px 8px;
  cursor: pointer;
  position: relative;
  transition: transform .15s ease, border-color .15s, background .15s;
}
.tube:hover { background: rgba(255,255,255,0.13); }
.tube--selected {
  border-color: #ffd166;
  background: rgba(255,209,102,0.18);
  transform: translateY(-6px);
  box-shadow: 0 8px 18px rgba(255,209,102,0.25);
}
.tube--complete {
  border-color: #7af1a8;
  background: rgba(122,241,168,0.18);
}
.tube--hint-from {
  animation: hintGlow 0.8s ease-in-out 3;
  border-color: #ffe066;
}
.tube--hint-to {
  animation: hintGlow 0.8s ease-in-out 3;
  border-color: #66ddff;
}
@keyframes hintGlow {
  0%, 100% { box-shadow: 0 0 0 rgba(255,255,255,0); }
  50% { box-shadow: 0 0 18px 4px rgba(255,255,255,0.5); }
}

.coin {
  width: calc(var(--tube-w) - 14px);
  height: calc(var(--slot-h) - 4px);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 26px;
  margin-top: 2px;
  position: relative;
  box-shadow: 0 2px 4px rgba(0,0,0,0.3), inset 0 -3px 6px rgba(0,0,0,0.18), inset 0 2px 4px rgba(255,255,255,0.4);
  transition: transform .25s cubic-bezier(.34,1.56,.64,1);
}
.coin--floating {
  transform: translateY(calc(-1 * var(--float-distance, 90px)));
  z-index: 5;
}

/* レイアウト調整：4列以下にするためのフレックス制御 */
.board[data-tube-count="4"] .tube,
.board[data-tube-count="5"] .tube,
.board[data-tube-count="6"] .tube { --tube-w: 60px; --slot-h: 50px; }
.board[data-tube-count="7"] .tube,
.board[data-tube-count="8"] .tube { --tube-w: 52px; --slot-h: 44px; }
.board[data-tube-count="9"] .tube,
.board[data-tube-count="10"] .tube,
.board[data-tube-count="11"] .tube,
.board[data-tube-count="12"] .tube { --tube-w: 44px; --slot-h: 38px; }
.board[data-tube-count="9"] .coin,
.board[data-tube-count="10"] .coin,
.board[data-tube-count="11"] .coin,
.board[data-tube-count="12"] .coin { font-size: 20px; }

/* エフェクトテキスト */
.effect-text {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  background: rgba(0,0,0,0.7);
  padding: 8px 18px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: bold;
  pointer-events: none;
  opacity: 0;
  transition: opacity .25s ease, transform .25s ease;
  white-space: nowrap;
}
.effect-text.show {
  opacity: 1;
  transform: translate(-50%, -120%);
}

/* ===== クリア画面 ===== */
.clear-inner {
  margin: auto;
  text-align: center;
  padding: 24px 16px;
  max-width: 420px;
}
.clear-burst {
  font-size: 80px;
  animation: burst 1s ease-out;
}
@keyframes burst {
  0% { transform: scale(0) rotate(-180deg); opacity: 0; }
  60% { transform: scale(1.3) rotate(20deg); opacity: 1; }
  100% { transform: scale(1) rotate(0deg); opacity: 1; }
}
.clear-title {
  font-family: 'Hachi Maru Pop', sans-serif;
  font-size: 30px;
  margin: 8px 0 18px;
  color: #ffd166;
  text-shadow: 0 2px 8px rgba(0,0,0,0.4);
}
.clear-stats {
  background: rgba(0,0,0,0.3);
  border-radius: 14px;
  padding: 12px 16px;
  margin: 0 auto 18px;
  max-width: 280px;
  border: 1px solid rgba(255,255,255,0.15);
}
.clear-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 0;
  border-bottom: 1px dashed rgba(255,255,255,0.15);
}
.clear-row:last-child { border-bottom: none; }
.clear-label { font-size: 14px; opacity: 0.85; }
.clear-value { font-size: 18px; font-weight: bold; color: #ffd166; }
.clear-rank { font-size: 22px; letter-spacing: 2px; }
.all-clear-msg {
  font-size: 14px;
  line-height: 1.7;
  background: rgba(0,0,0,0.25);
  padding: 14px 18px;
  border-radius: 14px;
  margin-bottom: 20px;
  border: 1px solid rgba(255,255,255,0.15);
}

/* ===== 戻るリンク（タイトル画面でのみ表示） ===== */
.back-to-top-link {
  position: fixed;
  bottom: 10px;
  left: 10px;
  background: rgba(0,0,0,0.4);
  color: #fff;
  padding: 6px 12px;
  border-radius: 6px;
  text-decoration: none;
  font-size: 12px;
  z-index: 100;
  border: 1px solid rgba(255,255,255,0.2);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  display: none;
}
.back-to-top-link:hover { background: rgba(0,0,0,0.6); }
/* タイトル画面でのみ表示（ゲーム/選択中はHUDの戻るボタンを使う） */
body:has(#title-screen.active) .back-to-top-link { display: inline-block; }

/* 言語トグルもタイトル画面でのみ表示（HUDボタンと干渉するため） */
.sg-lang-toggle { display: none !important; }
body:has(#title-screen.active) .sg-lang-toggle { display: inline-flex !important; }

/* ===== レスポンシブ ===== */
@media (min-width: 600px) {
  .game-title { font-size: 44px; }
  .stage-grid { grid-template-columns: repeat(8, 1fr); }
}

@media (max-height: 640px) {
  .title-emoji { font-size: 60px; }
  .game-title { font-size: 32px; }
  .controls-help { padding: 10px 12px; font-size: 12px; }
  .btn-primary, .btn-secondary { padding: 10px 18px; font-size: 15px; }
}

/* ===== タッチターゲットの安全領域 ===== */
@media (max-width: 380px) {
  .board { gap: 6px; }
  .board[data-tube-count="9"] .tube,
  .board[data-tube-count="10"] .tube,
  .board[data-tube-count="11"] .tube,
  .board[data-tube-count="12"] .tube { --tube-w: 38px; --slot-h: 34px; }
  .board[data-tube-count="9"] .coin,
  .board[data-tube-count="10"] .coin,
  .board[data-tube-count="11"] .coin,
  .board[data-tube-count="12"] .coin { font-size: 18px; }
}
