/* ==========================================================
   レールリレー - style.css
   ========================================================== */

:root {
  --color-bg-top: #1c2436;
  --color-bg-bottom: #0d1119;
  --color-panel: #232d42;
  --color-border: #3a4664;
  --color-text: #eef1fb;
  --color-text-muted: #9aa6c4;
  --color-danger: #ff5d6c;
  --color-gold: #ffd166;
  --accent: #7cb342;
  --accent-2: #5c9130;
  --header-h: 56px;
  --hud-h: 46px;
  --ad-h: 64px;
  --ad-gap-h: 16px;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overscroll-behavior: none;
}

body {
  font-family: "Hiragino Maru Gothic ProN", "Hiragino Kaku Gothic ProN", "Yu Gothic", "Segoe UI", Roboto, Arial, sans-serif;
  background: linear-gradient(180deg, var(--color-bg-top), var(--color-bg-bottom));
  color: var(--color-text);
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  user-select: none;
}

button {
  font-family: inherit;
}

/* ---------- ヘッダー ---------- */
.app-header {
  height: var(--header-h);
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 0.8rem;
  background: rgba(255, 255, 255, 0.05);
  border-bottom: 2px solid var(--color-border);
  z-index: 20;
}

.app-header__title {
  font-size: 1rem;
  font-weight: 900;
  display: flex;
  align-items: center;
  gap: 0.4rem;
  color: var(--color-gold);
  margin: 0;
}

.back-link {
  color: var(--color-text);
  text-decoration: none;
  font-size: 1.2rem;
  width: 38px;
  height: 38px;
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.08);
}

.back-link:active {
  background: rgba(255, 255, 255, 0.18);
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.icon-btn {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: none;
  background: rgba(255, 255, 255, 0.08);
  color: var(--color-text);
  font-size: 1.1rem;
  font-weight: bold;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.icon-btn:active {
  background: rgba(255, 255, 255, 0.2);
}

.icon-btn.is-muted {
  color: var(--color-text-muted);
}

/* ---------- HUD ---------- */
.game-main {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.hud-bar {
  height: var(--hud-h);
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 0.9rem;
  gap: 0.5rem;
  font-weight: 900;
  font-size: 0.85rem;
}

.hud-item {
  background: rgba(255, 255, 255, 0.06);
  border: 2px solid var(--color-border);
  border-radius: 999px;
  padding: 0.25rem 0.75rem;
}

.hud-stage span:last-child { color: var(--accent); }
.hud-score span:last-child { color: var(--color-gold); }

.hud-lives {
  letter-spacing: 0.15em;
  font-size: 0.95rem;
}

/* ---------- ゲームステージ ---------- */
.stage-area {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  margin: 0.4rem 0.7rem;
  border-radius: 18px;
  overflow: hidden;
  border: 2px solid var(--color-border);
  touch-action: manipulation;
}

.stage-bg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  filter: blur(2px) brightness(0.55) saturate(0.9);
  transform: scale(1.05);
  pointer-events: none;
}

.stage-flash {
  position: absolute;
  inset: 0;
  background: var(--color-danger);
  opacity: 0;
  pointer-events: none;
  z-index: 8;
}

.stage-flash.is-active {
  animation: stageFlash 0.4s ease;
}

@keyframes stageFlash {
  0% { opacity: 0.55; }
  100% { opacity: 0; }
}

.track-grid {
  position: absolute;
  left: 4%;
  right: 4%;
  top: 0;
  display: grid;
  grid-template-columns: repeat(var(--cols), 1fr);
  grid-auto-rows: var(--cell-size, 64px);
  will-change: transform;
}

.rail-tile {
  position: relative;
  border-radius: 10px;
}

.rail-tile.is-path {
  background: rgba(8, 12, 20, 0.62);
  border: 2px solid rgba(255, 255, 255, 0.22);
  cursor: pointer;
}

.rail-tile.is-path:active {
  background: rgba(8, 12, 20, 0.8);
}

.rail-tile.is-solved {
  border-color: var(--accent);
  box-shadow: inset 0 0 0 2px rgba(124, 179, 66, 0.35);
}

.rail-tile__svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: visible;
}

.rail-tile__svg path {
  fill: none;
  stroke-linecap: round;
}

.rail-tile__ghost {
  stroke: #ffffff;
  stroke-width: 7;
  stroke-dasharray: 3 10;
  opacity: 0.35;
}

.rail-tile__current {
  stroke: var(--accent);
  stroke-width: 10;
  opacity: 1;
  filter: drop-shadow(0 0 2px rgba(0,0,0,0.6));
  transition: d 0.05s;
}

.rail-tile.is-endpoint {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(1.1rem, 6vw, 1.6rem);
}

.rail-tile__coin {
  position: absolute;
  top: 4%;
  right: 8%;
  font-size: 0.75em;
  filter: drop-shadow(0 0 3px rgba(0,0,0,0.5));
  pointer-events: none;
}

.rail-token {
  position: absolute;
  width: 13%;
  aspect-ratio: 1;
  transform: translate(-50%, -50%);
  z-index: 6;
  pointer-events: none;
  filter: drop-shadow(0 3px 5px rgba(0,0,0,0.5));
}

.rail-token img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.rail-token__dir {
  position: absolute;
  top: 0;
  left: 50%;
  transform: translate(-50%, -100%);
  font-size: 0.9rem;
  color: var(--color-gold);
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.8);
  transition: transform 0.15s ease;
}

.rail-token.is-derailing {
  animation: tokenDerail 0.5s ease forwards;
}

@keyframes tokenDerail {
  0% { transform: translate(-50%, -50%) rotate(0deg); opacity: 1; }
  100% { transform: translate(-50%, 30px) rotate(220deg); opacity: 0; }
}

/* ---------- スライドパズル(ハムスター専用) ---------- */
.slide-grid {
  position: absolute;
  display: none;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: 3%;
}

.slide-cell {
  position: relative;
  border-radius: 10px;
  background: rgba(8, 12, 20, 0.62);
  border: 2px solid rgba(255, 255, 255, 0.22);
}

.slide-cell.is-tile {
  cursor: grab;
  touch-action: none;
}

.slide-cell.is-tile:active {
  cursor: grabbing;
}

.slide-cell.is-blank {
  background: radial-gradient(circle at 50% 38%, rgba(0, 0, 0, 0.55), rgba(0, 0, 0, 0.8) 72%);
  border: 2px dashed rgba(255, 255, 255, 0.16);
  box-shadow: inset 0 6px 16px rgba(0, 0, 0, 0.65);
}

.slide-cell.is-endpoint {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(1.1rem, 6vw, 1.6rem);
}

.slide-cell.is-solved {
  border-color: var(--accent);
  box-shadow: inset 0 0 0 2px rgba(124, 179, 66, 0.35);
}

.slide-cell.is-dragging {
  z-index: 5;
  transition: none;
}

.slide-cell.is-snapping {
  transition: transform 0.16s ease;
}

.slide-cell .rail-tile__coin {
  position: absolute;
  top: 4%;
  right: 8%;
  font-size: 0.75em;
  filter: drop-shadow(0 0 3px rgba(0, 0, 0, 0.5));
  pointer-events: none;
}

.floating-bonus {
  position: absolute;
  z-index: 9;
  font-weight: 900;
  font-size: 0.9rem;
  color: var(--color-gold);
  text-shadow: 0 1px 3px rgba(0,0,0,0.7);
  pointer-events: none;
  animation: floatUp 0.8s ease forwards;
}

@keyframes floatUp {
  0% { opacity: 1; transform: translate(-50%, 0); }
  100% { opacity: 0; transform: translate(-50%, -32px); }
}

/* ---------- ad slot ---------- */
.ad-gap {
  flex: 0 0 var(--ad-gap-h);
}

.ad-slot {
  flex: 0 0 var(--ad-h);
  display: flex;
  align-items: center;
  justify-content: center;
  border-top: 2px dashed var(--color-border);
  background: rgba(255, 255, 255, 0.04);
  color: var(--color-text-muted);
  font-size: 0.75rem;
  overflow: hidden;
}

/* ---------- オーバーレイ共通 ---------- */
.overlay {
  position: absolute;
  inset: 0;
  background: rgba(8, 10, 20, 0.78);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 40;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease;
}

.overlay.is-visible {
  opacity: 1;
  pointer-events: auto;
}

#guide-overlay {
  z-index: 50;
}

.overlay__box {
  text-align: center;
  padding: 1.6rem 1.6rem;
  background: var(--color-panel);
  border: 3px solid var(--color-border);
  border-radius: 22px;
  max-width: 88%;
  width: 380px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.5);
}

.overlay__box--wide {
  max-width: min(92%, 480px);
  max-height: 88%;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.overlay__box h2 {
  font-size: 1.3rem;
  margin: 0 0 0.8rem;
  color: var(--color-gold);
}

/* ---------- ガイド ---------- */
.guide-section {
  text-align: left;
  margin: 0.9rem 0;
}

.guide-section h3 {
  font-size: 0.9rem;
  color: var(--accent);
  margin: 0 0 0.35rem;
}

.guide-section p {
  font-size: 0.82rem;
  color: var(--color-text-muted);
  margin: 0;
  line-height: 1.65;
}

.guide-section ul {
  margin: 0;
  padding-left: 1.1rem;
  font-size: 0.8rem;
  color: var(--color-text-muted);
  line-height: 1.7;
}

.guide-section ul li {
  margin-bottom: 0.3rem;
}

/* ---------- ボタン ---------- */
.btn {
  border: none;
  border-radius: 999px;
  font-weight: 900;
  font-size: 0.95rem;
  padding: 0.8rem 1.6rem;
  cursor: pointer;
  width: 100%;
  margin-top: 0.6rem;
}

.btn--primary {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff;
}

.btn--primary:active {
  filter: brightness(0.92);
}

.btn--secondary {
  background: rgba(255, 255, 255, 0.08);
  color: var(--color-text);
  border: 2px solid var(--color-border);
}

.link-btn {
  background: none;
  border: none;
  color: var(--color-text-muted);
  font-size: 0.8rem;
  font-weight: bold;
  cursor: pointer;
  padding: 0;
  margin-bottom: 0.6rem;
  display: block;
}

/* ---------- テーマ選択 ---------- */
.theme-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.8rem;
}

.theme-card {
  display: flex;
  align-items: center;
  gap: 0.9rem;
  background: rgba(255, 255, 255, 0.06);
  border: 2px solid var(--color-border);
  border-radius: 16px;
  padding: 0.7rem 0.9rem;
  cursor: pointer;
  text-align: left;
}

.theme-card:active {
  background: rgba(255, 255, 255, 0.14);
}

.theme-card img {
  width: 58px;
  height: 58px;
  object-fit: contain;
  flex: 0 0 auto;
}

.theme-card__name {
  font-weight: 900;
  font-size: 0.95rem;
  margin: 0 0 0.2rem;
}

.theme-card__desc {
  font-size: 0.75rem;
  color: var(--color-text-muted);
  margin: 0;
  line-height: 1.4;
}

/* ---------- 難易度選択 ---------- */
.difficulty-list {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.difficulty-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: rgba(255, 255, 255, 0.06);
  border: 2px solid var(--color-border);
  border-radius: 14px;
  padding: 0.7rem 1rem;
  cursor: pointer;
  text-align: left;
}

.difficulty-item:active {
  background: rgba(255, 255, 255, 0.14);
}

.difficulty-item__name {
  font-weight: 900;
  font-size: 0.95rem;
}

.difficulty-item__best {
  font-size: 0.72rem;
  color: var(--color-text-muted);
}

/* ---------- ステージクリア ---------- */
.overlay--transparent {
  background: transparent;
}

.clear-banner {
  font-size: clamp(1.6rem, 8vw, 2.4rem);
  font-weight: 900;
  color: var(--color-gold);
  text-shadow: 0 3px 12px rgba(0,0,0,0.7);
  transform: scale(0.6);
  opacity: 0;
}

.overlay--transparent.is-visible .clear-banner {
  animation: clearPop 0.7s cubic-bezier(.2,1.4,.4,1) forwards;
}

@keyframes clearPop {
  0% { transform: scale(0.5); opacity: 0; }
  40% { transform: scale(1.15); opacity: 1; }
  70% { transform: scale(1); opacity: 1; }
  100% { transform: scale(1); opacity: 0; }
}

/* ---------- カウントダウン ---------- */
#countdown-overlay {
  background: transparent;
}

.countdown-number {
  font-size: clamp(3.5rem, 24vw, 6.5rem);
  font-weight: 900;
  color: #fff;
  text-shadow: 0 4px 18px rgba(0, 0, 0, 0.65);
  transform: scale(0.4);
  opacity: 0;
}

.countdown-number.is-go {
  color: var(--color-gold);
}

.countdown-number.is-popping {
  animation: countdownPop 0.8s cubic-bezier(0.2, 1.4, 0.4, 1) forwards;
}

@keyframes countdownPop {
  0% { transform: scale(0.4); opacity: 0; }
  35% { transform: scale(1.2); opacity: 1; }
  75% { transform: scale(1); opacity: 1; }
  100% { transform: scale(1); opacity: 0; }
}

/* ---------- ゲームオーバー ---------- */
.gameover-stat {
  font-size: 0.95rem;
  margin: 0.3rem 0;
}

.gameover-stat span:last-child {
  font-weight: 900;
  color: var(--color-gold);
}

.gameover-best {
  font-size: 0.8rem;
  color: var(--accent);
  font-weight: bold;
  min-height: 1.2em;
}

.gameover-actions {
  margin-top: 0.8rem;
}

@media (max-height: 640px) {
  .ad-slot { --ad-h: 44px; font-size: 0.65rem; }
  .ad-gap { --ad-gap-h: 8px; }
}

@media (min-width: 700px) {
  .stage-area { width: 100%; max-width: 480px; margin-left: auto; margin-right: auto; }
}
