:root {
  --bg-deep: #0a0620;
  --bg-main: #1a0a3e;
  --bg-panel: rgba(20, 10, 60, 0.85);
  --blue-light: #60a5fa;
  --blue-mid: #3b82f6;
  --purple-light: #a78bfa;
  --purple-mid: #7c3aed;
  --gold: #fbbf24;
  --gold-light: #fde68a;
  --white: #f0f0ff;
  --text-dim: #9ca3d0;
  --danger: #f87171;
  --cell-size: 28px;
}

html {
  font-size: 16px;
}

body {
  font-family: 'M PLUS Rounded 1c', 'Zen Maru Gothic', sans-serif;
  background: linear-gradient(135deg, var(--bg-deep) 0%, var(--bg-main) 40%, #1e1060 100%);
  color: var(--white);
  min-height: 100vh;
  min-height: 100dvh;
  overflow-x: hidden;
  position: relative;
}

/* ===== Back link ===== */
.back-to-top-link {
  position: fixed;
  top: 10px;
  left: 10px;
  color: var(--purple-light);
  text-decoration: none;
  font-size: 0.85rem;
  font-weight: 700;
  z-index: 1000;
  background: var(--bg-panel);
  padding: 6px 12px;
  border-radius: 20px;
  border: 1px solid rgba(167, 139, 250, 0.3);
  transition: all 0.3s;
}

.back-to-top-link:hover {
  background: rgba(167, 139, 250, 0.2);
  color: var(--gold);
}

/* ===== Background stars ===== */
.bg-stars {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 0;
}

.star {
  position: absolute;
  left: var(--x);
  top: var(--y);
  font-size: 1rem;
  animation: twinkle var(--dur) var(--delay) infinite ease-in-out;
  opacity: 0.3;
}

@keyframes twinkle {
  0%, 100% { opacity: 0.2; transform: scale(0.8); }
  50% { opacity: 0.8; transform: scale(1.2); }
}

/* ===== Comet streaks ===== */
.comet-streak {
  position: fixed;
  width: 200px;
  height: 2px;
  background: linear-gradient(90deg, transparent, var(--blue-light), var(--purple-light), transparent);
  top: 20%;
  left: -200px;
  transform: rotate(-15deg);
  animation: cometFly 8s 2s infinite linear;
  opacity: 0;
  z-index: 0;
  pointer-events: none;
}

.comet-streak.c2 {
  width: 150px;
  top: 60%;
  animation: cometFly 10s 5s infinite linear;
  transform: rotate(-20deg);
}

@keyframes cometFly {
  0% { left: -200px; opacity: 0; }
  5% { opacity: 0.7; }
  95% { opacity: 0.7; }
  100% { left: 110vw; opacity: 0; }
}

/* ===== Screens ===== */
.screen {
  display: none;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  min-height: 100dvh;
  position: relative;
  z-index: 1;
}

.screen.active {
  display: flex;
}

/* ===== Title Screen ===== */
.title-container {
  text-align: center;
  padding: 40px 20px;
  max-width: 500px;
}

.comet-icon {
  font-size: 4rem;
  margin-bottom: 10px;
  animation: cometBounce 2s infinite ease-in-out;
}

@keyframes cometBounce {
  0%, 100% { transform: translateY(0) rotate(0deg); }
  50% { transform: translateY(-10px) rotate(5deg); }
}

.game-title {
  font-family: 'Dela Gothic One', sans-serif;
  font-size: 3rem;
  background: linear-gradient(135deg, var(--blue-light), var(--purple-light), var(--gold));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  filter: drop-shadow(0 0 20px rgba(96, 165, 250, 0.5));
  margin-bottom: 8px;
}

.game-subtitle {
  font-size: 1.2rem;
  color: var(--gold);
  font-weight: 700;
  margin-bottom: 24px;
  text-shadow: 0 0 10px rgba(251, 191, 36, 0.5);
}

.title-piece-deco {
  display: flex;
  justify-content: center;
  gap: 4px;
  margin-bottom: 30px;
}

.deco-block {
  width: 20px;
  height: 20px;
  border-radius: 3px;
  animation: decoFloat 3s infinite ease-in-out;
}

.deco-block.b1 { background: #00f0f0; animation-delay: 0s; }
.deco-block.b2 { background: #f0a000; animation-delay: 0.3s; }
.deco-block.b3 { background: #a000f0; animation-delay: 0.6s; }
.deco-block.b4 { background: #f00000; animation-delay: 0.9s; }

@keyframes decoFloat {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-8px); }
}

.btn-primary {
  display: inline-block;
  background: linear-gradient(135deg, var(--blue-mid), var(--purple-mid));
  color: white;
  border: none;
  padding: 16px 48px;
  font-size: 1.3rem;
  font-family: 'Zen Maru Gothic', sans-serif;
  font-weight: 700;
  border-radius: 50px;
  cursor: pointer;
  transition: all 0.3s;
  box-shadow: 0 0 20px rgba(96, 165, 250, 0.3), 0 4px 15px rgba(0,0,0,0.3);
  margin-bottom: 24px;
}

.btn-primary:hover {
  transform: translateY(-2px);
  box-shadow: 0 0 30px rgba(96, 165, 250, 0.5), 0 6px 20px rgba(0,0,0,0.4);
}

.btn-primary:active {
  transform: translateY(1px);
}

.btn-secondary {
  display: inline-block;
  background: transparent;
  color: var(--purple-light);
  border: 2px solid var(--purple-light);
  padding: 12px 36px;
  font-size: 1rem;
  font-family: 'Zen Maru Gothic', sans-serif;
  font-weight: 700;
  border-radius: 50px;
  cursor: pointer;
  transition: all 0.3s;
  margin-bottom: 20px;
}

.btn-secondary:hover {
  background: rgba(167, 139, 250, 0.15);
}

.btn-small {
  position: fixed;
  top: 10px;
  right: 10px;
  background: var(--bg-panel);
  color: var(--purple-light);
  border: 1px solid rgba(167, 139, 250, 0.3);
  padding: 6px 14px;
  font-size: 0.8rem;
  font-family: 'Zen Maru Gothic', sans-serif;
  font-weight: 700;
  border-radius: 20px;
  cursor: pointer;
  z-index: 100;
  transition: all 0.3s;
}

.btn-small:hover {
  background: rgba(167, 139, 250, 0.2);
}

.controls-help {
  background: var(--bg-panel);
  border: 1px solid rgba(96, 165, 250, 0.2);
  border-radius: 12px;
  padding: 16px;
  margin-bottom: 20px;
  font-size: 0.85rem;
  color: var(--text-dim);
  line-height: 1.8;
}

.help-title {
  color: var(--blue-light);
  font-weight: 700;
  margin-bottom: 4px;
}

.disclaimer {
  font-size: 0.75rem;
  color: var(--text-dim);
  line-height: 1.6;
  opacity: 0.7;
}

/* ===== Game Screen ===== */
#game-screen {
  flex-direction: column;
  padding-top: 50px;
}

.game-layout {
  display: flex;
  justify-content: center;
  align-items: flex-start;
  gap: 16px;
}

.side-panel {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-width: 100px;
}

.info-box {
  background: var(--bg-panel);
  border: 1px solid rgba(96, 165, 250, 0.2);
  border-radius: 10px;
  padding: 12px 16px;
  text-align: center;
}

.info-label {
  font-size: 0.7rem;
  color: var(--text-dim);
  letter-spacing: 2px;
  margin-bottom: 4px;
}

.info-value {
  font-family: 'Dela Gothic One', sans-serif;
  font-size: 1.4rem;
  color: var(--gold);
  text-shadow: 0 0 8px rgba(251, 191, 36, 0.4);
}

.board-wrapper {
  position: relative;
  background: rgba(10, 5, 30, 0.9);
  border: 2px solid rgba(96, 165, 250, 0.3);
  border-radius: 4px;
  box-shadow: 0 0 30px rgba(96, 165, 250, 0.1), inset 0 0 30px rgba(0, 0, 0, 0.5);
  line-height: 0;
}

#game-canvas {
  display: block;
}

#next-canvas {
  display: block;
  margin: 8px auto 0;
}

/* Line clear flash */
.line-flash {
  position: absolute;
  inset: 0;
  background: radial-gradient(ellipse, rgba(96, 165, 250, 0.4), transparent);
  opacity: 0;
  pointer-events: none;
  border-radius: 4px;
  transition: opacity 0.1s;
}

.line-flash.active {
  opacity: 1;
  animation: flashPulse 0.4s ease-out;
}

@keyframes flashPulse {
  0% { opacity: 1; }
  100% { opacity: 0; }
}

/* Comet mode text */
.comet-mode-text {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0);
  font-family: 'Dela Gothic One', sans-serif;
  font-size: 1.6rem;
  color: var(--gold);
  text-shadow: 0 0 20px rgba(251, 191, 36, 0.8), 0 0 40px rgba(251, 191, 36, 0.4);
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  z-index: 10;
}

.comet-mode-text.active {
  animation: cometModeFlash 1.5s ease-out forwards;
}

@keyframes cometModeFlash {
  0% { opacity: 1; transform: translate(-50%, -50%) scale(0.5); }
  20% { opacity: 1; transform: translate(-50%, -50%) scale(1.3); }
  40% { transform: translate(-50%, -50%) scale(1); }
  100% { opacity: 0; transform: translate(-50%, -50%) scale(1.1) translateY(-30px); }
}

/* Mobile stats bar */
.mobile-stats {
  display: none;
  justify-content: space-around;
  background: var(--bg-panel);
  border: 1px solid rgba(96, 165, 250, 0.2);
  border-radius: 8px;
  padding: 8px 12px;
  margin: 8px 10px;
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--gold);
}

/* Mobile controls */
.mobile-controls {
  display: none;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 10px;
}

.ctrl-row {
  display: flex;
  gap: 10px;
}

.ctrl-btn {
  width: 64px;
  height: 54px;
  border: 2px solid rgba(96, 165, 250, 0.4);
  background: rgba(20, 10, 60, 0.8);
  color: var(--blue-light);
  font-size: 1.4rem;
  font-family: 'Zen Maru Gothic', sans-serif;
  font-weight: 700;
  border-radius: 12px;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
  transition: all 0.1s;
}

.ctrl-btn.wide {
  width: 100px;
  font-size: 1rem;
}

.ctrl-btn:active {
  background: rgba(96, 165, 250, 0.3);
  transform: scale(0.95);
}

/* ===== Result Screen ===== */
.result-container {
  text-align: center;
  padding: 40px 20px;
  max-width: 420px;
}

.result-comet {
  font-size: 3rem;
  margin-bottom: 8px;
}

.result-title {
  font-family: 'Dela Gothic One', sans-serif;
  font-size: 2rem;
  color: var(--purple-light);
  margin-bottom: 24px;
}

.result-stats {
  background: var(--bg-panel);
  border: 1px solid rgba(96, 165, 250, 0.2);
  border-radius: 16px;
  padding: 20px;
  margin-bottom: 20px;
}

.result-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid rgba(96, 165, 250, 0.1);
}

.result-row:last-child {
  border-bottom: none;
}

.result-label {
  color: var(--text-dim);
  font-size: 0.95rem;
}

.result-value {
  font-family: 'Dela Gothic One', sans-serif;
  font-size: 1.5rem;
  color: var(--gold);
  text-shadow: 0 0 8px rgba(251, 191, 36, 0.4);
}

.result-rank {
  font-family: 'Dela Gothic One', sans-serif;
  font-size: 1.8rem;
  margin-bottom: 24px;
  min-height: 2.2rem;
}

/* ===== Responsive ===== */
@media (max-width: 700px) {
  :root {
    --cell-size: 22px;
  }

  .back-to-top-link {
    font-size: 0.75rem;
    padding: 4px 10px;
  }

  .game-title {
    font-size: 2.2rem;
  }

  .side-panel {
    display: none;
  }

  .mobile-stats {
    display: flex;
  }

  .mobile-controls {
    display: flex;
  }

  #game-screen {
    padding-top: 40px;
    min-height: auto;
    justify-content: flex-start;
  }

  .game-layout {
    margin-top: 8px;
  }

  .comet-mode-text {
    font-size: 1.2rem;
  }

  .btn-small {
    top: 8px;
    right: 8px;
    font-size: 0.7rem;
    padding: 4px 10px;
  }
}

@media (max-width: 400px) {
  :root {
    --cell-size: 18px;
  }

  .game-title {
    font-size: 1.8rem;
  }

  .ctrl-btn {
    width: 56px;
    height: 48px;
    font-size: 1.2rem;
  }

  .ctrl-btn.wide {
    width: 86px;
    font-size: 0.9rem;
  }
}

@media (min-width: 701px) {
  :root {
    --cell-size: 30px;
  }

  .screen {
    padding-inline: 20px;
  }

  #title-screen.active,
  #result-screen.active {
    justify-content: flex-start;
    padding-top: clamp(84px, 11vh, 128px);
  }

  #game-screen {
    justify-content: flex-start;
    padding-top: clamp(64px, 10vh, 108px);
  }

  .title-container {
    max-width: 560px;
  }

  .game-layout {
    gap: 24px;
    margin-top: 18px;
  }

  .side-panel {
    min-width: 112px;
  }

  .info-box {
    padding: 14px 18px;
  }

  .result-container {
    max-width: 480px;
  }

  .right-panel {
    display: flex;
  }
}
