:root {
  --bg-top: #ffd9ec;
  --bg-bottom: #ffb6d9;
  --card-bg: #fff5fa;
  --accent: #ff5fa2;
  --accent-dark: #d63b81;
  --tile-radius: 14px;
  --board-gap: 6px;
  --text-dark: #3a2233;
}

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

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: linear-gradient(160deg, var(--bg-top), var(--bg-bottom));
  color: var(--text-dark);
  display: flex;
  justify-content: center;
  min-height: 100vh;
}

.app {
  width: 100%;
  max-width: 480px;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top, 12px) 14px calc(env(safe-area-inset-bottom, 12px) + 12px);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 4px 10px;
}

.brand {
  font-size: 1.3rem;
  font-weight: 800;
  color: var(--accent-dark);
}

.stats {
  display: flex;
  gap: 10px;
}

.stat {
  background: rgba(255,255,255,0.65);
  border-radius: 12px;
  padding: 6px 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 58px;
}

.stat-label {
  font-size: 0.62rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--accent-dark);
  opacity: 0.8;
}

.stat-value {
  font-size: 1.1rem;
  font-weight: 800;
  line-height: 1.2;
}

.timer-bar-track {
  width: 100%;
  height: 8px;
  background: rgba(255,255,255,0.5);
  border-radius: 999px;
  overflow: hidden;
  margin-bottom: 12px;
}

.timer-bar {
  height: 100%;
  width: 100%;
  background: linear-gradient(90deg, #ff8fc0, var(--accent));
  border-radius: 999px;
  transform-origin: left;
  transition: width 0.25s linear, background-color 0.3s;
}

.timer-bar.low {
  background: linear-gradient(90deg, #ff6b6b, #e03131);
}

.board-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

.board {
  --cols: 6;
  --rows: 8;
  display: grid;
  grid-template-columns: repeat(var(--cols), 1fr);
  grid-template-rows: repeat(var(--rows), 1fr);
  gap: var(--board-gap);
  width: 100%;
  aspect-ratio: var(--cols) / var(--rows);
  background: rgba(255,255,255,0.35);
  border-radius: 18px;
  padding: var(--board-gap);
  position: relative;
}

.tile {
  border-radius: var(--tile-radius);
  background: hsl(var(--hue, 320) var(--tile-sat, 72%) var(--tile-light, 86%));
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 0 rgba(0,0,0,0.08), inset 0 0 0 1px rgba(255,255,255,0.6);
  cursor: pointer;
  transition: transform 0.12s ease, box-shadow 0.12s ease, opacity 0.18s ease;
  position: relative;
}

.tile svg {
  width: 82%;
  height: 82%;
}

.tile.selected {
  transform: scale(0.9);
  box-shadow: 0 0 0 3px var(--accent), 0 2px 0 rgba(0,0,0,0.08);
}

.tile.matched {
  animation: pop 0.28s ease forwards;
}

.tile.swap-invalid {
  animation: shake 0.28s ease;
}

.tile.falling {
  animation: drop 0.22s ease;
}

@keyframes pop {
  0% { transform: scale(1); opacity: 1; }
  60% { transform: scale(1.25); opacity: 0.7; }
  100% { transform: scale(0.2); opacity: 0; }
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-6px); }
  75% { transform: translateX(6px); }
}

@keyframes drop {
  0% { transform: translateY(-14px); opacity: 0.4; }
  100% { transform: translateY(0); opacity: 1; }
}

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(60, 20, 45, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 10;
  backdrop-filter: blur(2px);
}

.overlay.hidden {
  display: none;
}

.overlay-card {
  background: var(--card-bg);
  border-radius: 20px;
  padding: 28px 24px;
  text-align: center;
  max-width: 340px;
  width: 100%;
  box-shadow: 0 10px 30px rgba(0,0,0,0.25);
}

.overlay-card h1 {
  margin: 0 0 8px;
  font-size: 1.6rem;
  color: var(--accent-dark);
}

.overlay-card p {
  margin: 0 0 18px;
  font-size: 0.92rem;
  line-height: 1.4;
  color: var(--text-dark);
  opacity: 0.85;
}

.badge-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  margin: 2px 0 16px;
}

.badge-visual {
  width: 104px;
  height: 104px;
  filter: drop-shadow(0 6px 10px rgba(0,0,0,0.25));
}

.badge-visual svg {
  width: 100%;
  height: 100%;
  overflow: visible;
}

.sparkle {
  transform-box: fill-box;
  transform-origin: center;
  animation: twinkle 1.4s ease-in-out infinite;
}

@keyframes twinkle {
  0%, 100% { opacity: 0.25; transform: scale(0.75); }
  50% { opacity: 1; transform: scale(1.15); }
}

@media (prefers-reduced-motion: reduce) {
  .sparkle { animation: none; opacity: 0.9; }
}

.badge-label {
  font-weight: 800;
  font-size: 1.05rem;
  color: var(--accent-dark);
  text-align: center;
}

.badge-sub {
  font-size: 0.78rem;
  color: var(--text-dark);
  opacity: 0.75;
  text-align: center;
  font-weight: 600;
}

.final-score-label {
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--accent-dark);
  opacity: 0.8;
}

.final-score {
  font-size: 2.6rem;
  font-weight: 900;
  color: var(--accent-dark);
  margin: 2px 0 4px;
}

.best-score-line {
  font-size: 0.85rem;
  margin-bottom: 16px;
  opacity: 0.8;
}

.hidden {
  display: none !important;
}

.save-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  margin: 0 0 18px;
}

.pseudo-field {
  font-family: inherit;
  font-weight: 700;
  text-align: center;
  padding: 9px 12px;
  border-radius: 10px;
  border: 2px solid rgba(255,255,255,0.6);
  background: rgba(255,255,255,0.55);
  color: var(--text-dark);
  font-size: 0.9rem;
  width: 180px;
  max-width: 65vw;
}

.pseudo-field::placeholder {
  color: var(--text-dark);
  opacity: 0.6;
  font-weight: 600;
}

.save-status {
  font-size: 0.75rem;
  color: var(--accent-dark);
  font-weight: 700;
  min-height: 14px;
}

.actions-row {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
}

.start-btn, .ghost-btn {
  border: none;
  font-size: 1.05rem;
  font-weight: 800;
  padding: 12px 30px;
  border-radius: 999px;
  cursor: pointer;
}

.start-btn {
  background: linear-gradient(135deg, #ff8fc0, var(--accent));
  color: white;
  box-shadow: 0 6px 16px rgba(255, 95, 162, 0.45);
}

.ghost-btn {
  background: transparent;
  color: var(--accent-dark);
  border: 2px solid var(--accent-dark);
  font-size: 0.95rem;
  padding: 10px 22px;
}

.start-btn:active, .ghost-btn:active {
  transform: scale(0.96);
}

.start-btn:disabled, .ghost-btn:disabled {
  opacity: 0.6;
  cursor: default;
}

a.race-link {
  display: inline-block;
  text-decoration: none;
  box-sizing: border-box;
}

.lb-modal {
  position: fixed;
  inset: 0;
  background: rgba(60, 20, 45, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 20;
  backdrop-filter: blur(2px);
}

.lb-box {
  background: var(--card-bg);
  border-radius: 20px;
  padding: 24px 20px;
  max-width: 320px;
  width: 100%;
  max-height: 78vh;
  overflow-y: auto;
  box-shadow: 0 10px 30px rgba(0,0,0,0.25);
  text-align: center;
}

.lb-box h2 {
  margin: 0 0 14px;
  font-size: 1.4rem;
  color: var(--accent-dark);
}

.lb-msg {
  font-size: 0.85rem;
  color: var(--text-dark);
  opacity: 0.75;
  line-height: 1.5;
}

.lb-list {
  list-style: none;
  margin: 0 0 4px;
  padding: 0;
  text-align: left;
}

.lb-list li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 4px;
  font-size: 0.9rem;
  border-bottom: 1px solid rgba(0,0,0,0.08);
}

.lb-list li:last-child {
  border-bottom: none;
}

.lb-rank {
  font-weight: 800;
  color: var(--accent-dark);
  width: 26px;
  flex-shrink: 0;
}

.lb-name {
  flex: 1;
  font-weight: 700;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.lb-score {
  font-weight: 800;
  color: var(--accent-dark);
}

.score-float {
  position: absolute;
  font-weight: 800;
  color: var(--accent-dark);
  pointer-events: none;
  font-size: 0.95rem;
  animation: floatUp 0.7s ease forwards;
  z-index: 5;
}

@keyframes floatUp {
  0% { transform: translateY(0); opacity: 1; }
  100% { transform: translateY(-26px); opacity: 0; }
}
