:root {
  --bg: #162126;
  --panel: #23323a;
  --panel-2: #2b3c45;
  --grid: #314753;
  --grid-hot: #49616e;
  --text: #f4fbfb;
  --muted: #9fb0b6;
  --accent: #ffd166;
  --ok: #4ecdc4;
  --danger: #ff6b6b;
  --shadow: rgba(5, 12, 16, 0.36);
  --board-size: min(92vw, 484px);
  --cell-gap: 4px;
  --board-pad: 4px;
  --tray-height: 126px;
  --mini-cell: 22px;
  font-family: Inter, "Segoe UI", "Microsoft YaHei", system-ui, sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  width: 100%;
  min-height: 100vh;
  min-height: var(--app-height, 100dvh);
  margin: 0;
  color: var(--text);
  background:
    radial-gradient(circle at 16% 8%, rgba(78, 205, 196, 0.18), transparent 32%),
    radial-gradient(circle at 92% 86%, rgba(255, 107, 107, 0.15), transparent 34%),
    linear-gradient(135deg, #142026 0%, #20282b 52%, #18282d 100%);
  display: grid;
  place-items: center;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: manipulation;
}

button {
  font: inherit;
}

.app {
  width: min(100vw, 610px);
  height: 100vh;
  height: var(--app-height, 100dvh);
  min-height: 0;
  padding: 18px 14px 22px;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  gap: 14px;
  position: relative;
  overflow: hidden;
  min-width: 0;
}

.topbar,
.statusbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.mark {
  width: 34px;
  height: 34px;
  border-radius: 8px;
  background:
    linear-gradient(90deg, transparent 44%, rgba(255, 255, 255, 0.2) 44% 56%, transparent 56%),
    linear-gradient(0deg, transparent 44%, rgba(255, 255, 255, 0.2) 44% 56%, transparent 56%),
    conic-gradient(from 90deg, #ff6b6b, #ffd166, #4ecdc4, #62a7ff, #ff6b6b);
  box-shadow: 0 10px 20px var(--shadow);
  flex: 0 0 auto;
}

h1 {
  margin: 0;
  font-size: clamp(1.35rem, 4vw, 2rem);
  letter-spacing: 0;
  line-height: 1;
}

.scores {
  display: flex;
  align-items: center;
  gap: 8px;
}

.scorebox {
  min-width: 64px;
  padding: 7px 10px;
  border-radius: 8px;
  background: rgba(35, 50, 58, 0.88);
  border: 1px solid rgba(255, 255, 255, 0.08);
  box-shadow: 0 10px 18px var(--shadow);
  text-align: center;
}

.scorebox span {
  display: block;
  color: var(--muted);
  font-size: 0.72rem;
}

.scorebox strong {
  display: block;
  margin-top: 2px;
  font-size: 1.1rem;
  line-height: 1;
}

.icon-button {
  width: 42px;
  height: 42px;
  border: 0;
  border-radius: 8px;
  color: #17323a;
  background: var(--accent);
  box-shadow: 0 10px 18px rgba(255, 209, 102, 0.22);
  cursor: pointer;
  font-size: 1.35rem;
  line-height: 1;
}

.help-button {
  background: #4ecdc4;
  box-shadow: 0 10px 18px rgba(78, 205, 196, 0.22);
  font-weight: 900;
}

.audio-button {
  background: #ffd166;
  box-shadow: 0 10px 18px rgba(255, 209, 102, 0.22);
  font-size: 0.95rem;
  font-weight: 900;
}

.audio-button.muted {
  color: #9fb0b6;
  background: #2b3c45;
  box-shadow: none;
}

.icon-button:active,
.modal button:active {
  transform: translateY(1px);
}

.modebar {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 7px;
  padding: 6px;
  border-radius: 8px;
  background: rgba(35, 50, 58, 0.76);
  border: 1px solid rgba(255, 255, 255, 0.08);
  box-shadow: 0 10px 22px var(--shadow);
}

.mode-button {
  min-height: 34px;
  border: 0;
  border-radius: 7px;
  color: var(--muted);
  background: transparent;
  font-weight: 800;
  cursor: pointer;
}

.mode-button.active {
  color: #17323a;
  background: #ffd166;
  box-shadow: 0 8px 16px rgba(255, 209, 102, 0.2);
}

.stage {
  width: 100%;
  max-width: 100%;
  min-width: 0;
  align-self: stretch;
  display: grid;
  grid-template-rows: minmax(0, 1fr) var(--tray-height);
  justify-items: center;
  align-items: center;
  gap: 18px;
  min-height: 0;
  overflow: hidden;
}

.board-wrap {
  position: relative;
  max-width: 100%;
  min-width: 0;
  padding: 10px;
  border-radius: 8px;
  background: linear-gradient(180deg, var(--panel), #1d2b32);
  box-shadow: 0 18px 42px var(--shadow), inset 0 1px rgba(255, 255, 255, 0.08);
}

.board {
  width: var(--board-size);
  height: var(--board-size);
  display: grid;
  grid-template-columns: repeat(8, minmax(0, 1fr));
  grid-template-rows: repeat(8, minmax(0, 1fr));
  gap: var(--cell-gap);
  padding: var(--board-pad);
  background: #17262d;
  border-radius: 8px;
  touch-action: none;
}

.cell {
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  border-radius: 7px;
  background: var(--grid);
  box-shadow: inset 0 1px rgba(255, 255, 255, 0.06), inset 0 -3px rgba(0, 0, 0, 0.12);
  transition: transform 120ms ease, background 120ms ease, box-shadow 120ms ease;
}

.app[data-mode="classic"] .cell {
  background:
    linear-gradient(90deg, rgba(255, 255, 255, 0.05) 1px, transparent 1px),
    linear-gradient(0deg, rgba(0, 0, 0, 0.16) 1px, transparent 1px),
    #314753;
  background-size: 50% 50%, 100% 50%, auto;
}

.app[data-mode="sprint"] .cell {
  background:
    linear-gradient(135deg, transparent 0 35%, rgba(78, 205, 196, 0.16) 36% 45%, transparent 46%),
    #2d4651;
}

.app[data-mode="bomb"] .cell {
  background:
    repeating-linear-gradient(135deg, rgba(255, 209, 102, 0.09) 0 6px, transparent 6px 13px),
    #3b4144;
}

.app[data-mode="ice"] .cell {
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.1), transparent 34%),
    linear-gradient(45deg, transparent 58%, rgba(141, 223, 240, 0.13) 59% 63%, transparent 64%),
    #294955;
}

.cell.filled,
.mini-cell.filled {
  background: var(--block-color);
  box-shadow:
    inset 0 2px rgba(255, 255, 255, 0.34),
    inset 0 -5px rgba(0, 0, 0, 0.18),
    0 8px 16px rgba(0, 0, 0, 0.18);
}

.app[data-mode="classic"] .cell.filled,
.app[data-mode="classic"] .mini-cell.filled {
  background:
    linear-gradient(90deg, rgba(255, 255, 255, 0.2) 1px, transparent 1px),
    linear-gradient(0deg, rgba(0, 0, 0, 0.18) 2px, transparent 2px),
    linear-gradient(145deg, rgba(255, 255, 255, 0.28), transparent 44%),
    var(--block-color);
  background-size: 50% 50%, 100% 50%, auto, auto;
}

.app[data-mode="sprint"] .cell.filled,
.app[data-mode="sprint"] .mini-cell.filled {
  background:
    linear-gradient(115deg, transparent 0 20%, rgba(255, 255, 255, 0.42) 21% 28%, transparent 29% 48%, rgba(255, 255, 255, 0.24) 49% 53%, transparent 54%),
    linear-gradient(145deg, color-mix(in srgb, var(--block-color), white 18%), var(--block-color));
  box-shadow:
    inset 0 2px rgba(255, 255, 255, 0.42),
    inset 0 -5px rgba(0, 0, 0, 0.18),
    0 0 14px color-mix(in srgb, var(--block-color), white 20%);
}

.app[data-mode="bomb"] .cell.filled,
.app[data-mode="bomb"] .mini-cell.filled {
  background:
    repeating-linear-gradient(135deg, rgba(22, 33, 38, 0.28) 0 5px, transparent 5px 12px),
    linear-gradient(145deg, rgba(255, 255, 255, 0.24), transparent 42%),
    var(--block-color);
}

.app[data-mode="ice"] .cell.filled,
.app[data-mode="ice"] .mini-cell.filled {
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.46), transparent 28%),
    linear-gradient(45deg, transparent 52%, rgba(255, 255, 255, 0.38) 53% 57%, transparent 58%),
    linear-gradient(160deg, color-mix(in srgb, var(--block-color), white 30%), var(--block-color));
  box-shadow:
    inset 0 2px rgba(255, 255, 255, 0.5),
    inset 0 -5px rgba(0, 90, 120, 0.16),
    0 8px 18px rgba(141, 223, 240, 0.18);
}

.cell.ghost {
  background: color-mix(in srgb, var(--block-color), white 16%);
  opacity: 0.78;
  transform: scale(0.92);
}

.cell.bad {
  background: color-mix(in srgb, var(--danger), var(--grid) 42%);
}

.cell.bomb {
  position: relative;
  background:
    radial-gradient(circle at 50% 42%, rgba(255, 255, 255, 0.34) 0 12%, transparent 13%),
    repeating-linear-gradient(135deg, rgba(22, 33, 38, 0.36) 0 5px, transparent 5px 10px),
    #ff6b6b;
}

.cell.bomb::after {
  content: attr(data-count);
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: #fff;
  font-size: 0.9rem;
  font-weight: 900;
  text-shadow: 0 2px 5px rgba(0, 0, 0, 0.35);
}

.cell.ice {
  position: relative;
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.62), transparent 34%),
    linear-gradient(45deg, transparent 56%, rgba(255, 255, 255, 0.4) 57% 61%, transparent 62%),
    #8ddff0;
  box-shadow:
    inset 0 2px rgba(255, 255, 255, 0.48),
    inset 0 -5px rgba(0, 90, 120, 0.18);
}

.cell.ice::after {
  content: "";
  position: absolute;
  inset: 8px;
  border-radius: 5px;
  border: 2px solid rgba(255, 255, 255, 0.62);
}

.cell.clearing {
  z-index: 2;
  animation: pop 320ms ease forwards;
}

.tray {
  width: 100%;
  max-width: 100%;
  min-width: 0;
  height: var(--tray-height);
  min-height: var(--tray-height);
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  overflow: hidden;
}

.piece {
  min-width: 0;
  height: var(--tray-height);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 8px;
  background: rgba(35, 50, 58, 0.82);
  box-shadow: 0 10px 22px var(--shadow);
  display: grid;
  place-items: center;
  cursor: grab;
  touch-action: none;
  transition: transform 140ms ease, opacity 140ms ease, border-color 140ms ease;
}

.piece:active {
  cursor: grabbing;
}

.piece.selected {
  transform: translateY(-5px);
  border-color: rgba(255, 209, 102, 0.75);
}

.piece.used {
  opacity: 0.2;
  pointer-events: none;
}

.piece-grid {
  display: grid;
  gap: 4px;
}

.mini-cell {
  width: var(--mini-cell);
  height: var(--mini-cell);
  border-radius: 5px;
}

.statusbar {
  color: var(--muted);
  min-height: 28px;
  font-size: 0.92rem;
}

#combo {
  color: var(--accent);
  font-weight: 700;
}

#mode-info {
  color: #4ecdc4;
  font-weight: 800;
}

.pulse {
  position: absolute;
  inset: 10px;
  pointer-events: none;
  border-radius: 8px;
}

.pulse.flash {
  animation: boardFlash 360ms ease;
}

.fx-layer {
  position: absolute;
  inset: 10px;
  pointer-events: none;
  overflow: hidden;
  border-radius: 8px;
}

.spark {
  position: absolute;
  width: 8px;
  height: 8px;
  border-radius: 3px;
  background: var(--spark-color);
  box-shadow: 0 0 14px var(--spark-color);
  animation: sparkFly 520ms ease-out forwards;
}

.score-pop {
  position: absolute;
  left: 50%;
  top: 42%;
  transform: translate(-50%, -50%);
  color: #ffd166;
  font-size: 1.4rem;
  font-weight: 900;
  text-shadow: 0 3px 12px rgba(0, 0, 0, 0.4);
  animation: scorePop 680ms ease-out forwards;
}

.overlay {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  background: rgba(9, 17, 21, 0.7);
  backdrop-filter: blur(8px);
  overflow: auto;
}

.overlay.hidden {
  display: none;
}

.modal {
  width: min(92vw, 340px);
  padding: 24px;
  border-radius: 8px;
  background: #f7fbfb;
  color: #162126;
  text-align: center;
  box-shadow: 0 28px 70px rgba(0, 0, 0, 0.42);
}

.modal h2 {
  margin: 0 0 8px;
  font-size: 1.7rem;
  letter-spacing: 0;
}

.modal p {
  margin: 0 0 20px;
  color: #45616b;
  font-weight: 800;
  font-size: 2.3rem;
}

.final-reason {
  display: block;
  margin: -8px 0 18px;
  color: #607780;
  font-size: 0.95rem;
  font-weight: 700;
  line-height: 1.45;
}

.modal button {
  min-width: 132px;
  height: 44px;
  border: 0;
  border-radius: 8px;
  background: #4ecdc4;
  color: #102226;
  font-weight: 800;
  cursor: pointer;
}

.help-modal {
  position: relative;
  width: min(92vw, 430px);
  text-align: left;
}

.help-modal h2 {
  margin-right: 36px;
}

.help-modal .rules {
  display: grid;
  gap: 12px;
  margin: 18px 0 20px;
}

.help-modal section {
  padding: 12px;
  border-radius: 8px;
  background: #e9f2f2;
  border: 1px solid rgba(22, 33, 38, 0.08);
}

.help-modal h3 {
  margin: 0 0 5px;
  color: #17323a;
  font-size: 0.98rem;
  letter-spacing: 0;
}

.help-modal p {
  margin: 0;
  color: #45616b;
  font-weight: 500;
  font-size: 0.92rem;
  line-height: 1.55;
}

.help-modal > button:not(.close-button) {
  display: block;
  margin: 10px auto 0;
}

.help-modal #start-guide {
  background: #ffd166;
}

.close-button {
  position: absolute;
  top: 14px;
  right: 14px;
  width: 34px;
  min-width: 34px;
  height: 34px;
  border: 0;
  border-radius: 8px;
  background: rgba(22, 33, 38, 0.08);
  color: #162126;
  font-size: 1.3rem;
  line-height: 1;
  cursor: pointer;
}

.guide-overlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: max(18px, env(safe-area-inset-top)) max(18px, env(safe-area-inset-right)) max(18px, env(safe-area-inset-bottom)) max(18px, env(safe-area-inset-left));
  background:
    radial-gradient(circle at 50% 28%, rgba(255, 209, 102, 0.14), transparent 34%),
    rgba(9, 17, 21, 0.74);
  backdrop-filter: blur(8px);
  overflow: auto;
}

.guide-overlay.hidden {
  display: none;
}

.guide-card {
  position: relative;
  width: min(92vw, 410px);
  padding: 24px;
  border-radius: 8px;
  background: #f7fbfb;
  color: #162126;
  box-shadow: 0 28px 70px rgba(0, 0, 0, 0.42);
}

.guide-step {
  display: inline-flex;
  height: 28px;
  align-items: center;
  padding: 0 10px;
  border-radius: 8px;
  color: #17323a;
  background: #dff4f2;
  font-size: 0.82rem;
  font-weight: 800;
}

.guide-card h2 {
  margin: 14px 40px 8px 0;
  font-size: 1.55rem;
  letter-spacing: 0;
}

.guide-card p {
  min-height: 56px;
  margin: 0;
  color: #45616b;
  font-size: 0.98rem;
  line-height: 1.6;
}

.guide-visual {
  height: 126px;
  margin: 18px 0;
  border-radius: 8px;
  background:
    linear-gradient(90deg, rgba(23, 38, 45, 0.08) 1px, transparent 1px),
    linear-gradient(0deg, rgba(23, 38, 45, 0.08) 1px, transparent 1px),
    #e9f2f2;
  background-size: 22px 22px;
  display: grid;
  grid-template-columns: repeat(4, 32px);
  grid-template-rows: repeat(3, 32px);
  place-content: center;
  gap: 6px;
  overflow: hidden;
}

.guide-visual span {
  border-radius: 7px;
  background: var(--ok);
  box-shadow: inset 0 2px rgba(255, 255, 255, 0.34), inset 0 -5px rgba(0, 0, 0, 0.18);
}

.guide-visual.step-2 span {
  background: #ffd166;
}

.guide-visual.step-2 span:nth-child(1),
.guide-visual.step-2 span:nth-child(2),
.guide-visual.step-2 span:nth-child(3) {
  animation: guideFloat 1.4s ease-in-out infinite;
}

.guide-visual.step-3 span {
  background: #62a7ff;
}

.guide-visual.step-3 span:nth-child(4) {
  grid-column: 1 / 5;
  animation: guideClear 1.5s ease-in-out infinite;
}

.guide-visual.step-4 span {
  background: #ff6b6b;
}

.guide-dots {
  display: flex;
  justify-content: center;
  gap: 8px;
  margin-bottom: 16px;
}

.guide-dots button {
  width: 9px;
  min-width: 9px;
  height: 9px;
  padding: 0;
  border: 0;
  border-radius: 999px;
  background: #b7c8cd;
  cursor: pointer;
}

.guide-dots button.active {
  width: 24px;
  background: #4ecdc4;
}

.guide-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.guide-actions button {
  height: 44px;
  border: 0;
  border-radius: 8px;
  color: #102226;
  background: #4ecdc4;
  font-weight: 800;
  cursor: pointer;
}

.guide-actions .secondary-button {
  background: rgba(22, 33, 38, 0.09);
  color: #314753;
}

.drag-proxy {
  position: fixed;
  z-index: 10;
  pointer-events: none;
  transform: translate(-50%, -50%) scale(1.15);
  filter: drop-shadow(0 18px 24px rgba(0, 0, 0, 0.35));
}

@keyframes pop {
  0% {
    transform: scale(1);
    filter: brightness(1);
  }
  60% {
    transform: scale(1.14) rotate(4deg);
    background: #ffffff;
    filter: brightness(1.35);
  }
  100% {
    transform: scale(0);
    opacity: 0;
  }
}

@keyframes boardFlash {
  0% {
    box-shadow: inset 0 0 0 0 rgba(255, 209, 102, 0);
  }
  45% {
    box-shadow: inset 0 0 0 8px rgba(255, 209, 102, 0.35);
  }
  100% {
    box-shadow: inset 0 0 0 0 rgba(255, 209, 102, 0);
  }
}

@keyframes sparkFly {
  0% {
    opacity: 1;
    transform: translate(0, 0) scale(1);
  }
  100% {
    opacity: 0;
    transform: translate(var(--spark-x), var(--spark-y)) scale(0.35) rotate(180deg);
  }
}

@keyframes scorePop {
  0% {
    opacity: 0;
    transform: translate(-50%, -35%) scale(0.8);
  }
  22% {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1.08);
  }
  100% {
    opacity: 0;
    transform: translate(-50%, -110%) scale(1);
  }
}

@keyframes guideFloat {
  50% {
    transform: translateY(-10px);
  }
}

@keyframes guideClear {
  50% {
    transform: scaleX(1.06);
    background: #ffffff;
  }
}

@media (max-width: 700px), (max-height: 760px) {
  :root {
    --board-size: min(
      calc(100vw - 28px - env(safe-area-inset-left) - env(safe-area-inset-right)),
      calc(100dvh - 292px - env(safe-area-inset-top) - env(safe-area-inset-bottom))
    );
    --tray-height: clamp(92px, 17dvh, 116px);
    --mini-cell: clamp(15px, 4.3vw, 20px);
    --cell-gap: 3px;
    --board-pad: 4px;
  }

  .app {
    width: 100%;
    padding:
      max(10px, env(safe-area-inset-top))
      max(10px, env(safe-area-inset-right))
      max(12px, env(safe-area-inset-bottom))
      max(10px, env(safe-area-inset-left));
    gap: clamp(6px, 1.2dvh, 10px);
  }

  .topbar {
    align-items: center;
    gap: 8px;
  }

  .scores {
    gap: 6px;
    flex: 0 0 auto;
  }

  .scorebox {
    min-width: 50px;
    padding: 7px 8px;
  }

  .scorebox span {
    font-size: 0.66rem;
  }

  .scorebox strong {
    font-size: 1rem;
  }

  .brand {
    gap: 8px;
  }

  .mark {
    width: 30px;
    height: 30px;
  }

  h1 {
    font-size: clamp(1.1rem, 5.2vw, 1.45rem);
  }

  .icon-button {
    width: 36px;
    height: 36px;
    font-size: 1rem;
  }

  .modebar {
    gap: 5px;
    padding: 5px;
  }

  .mode-button {
    min-height: 31px;
    font-size: 0.78rem;
  }

  .stage {
    gap: clamp(7px, 1.3dvh, 12px);
  }

  .board-wrap {
    padding: 7px;
  }

  .tray {
    gap: 7px;
  }

  .statusbar {
    min-height: 24px;
    gap: 8px;
    font-size: 0.78rem;
    white-space: nowrap;
  }

  .guide-card,
  .help-modal {
    padding: 20px;
  }

  .guide-actions {
    grid-template-columns: 1fr;
  }
}

@media (max-height: 640px) {
  :root {
    --board-size: min(
      calc(100vw - 26px - env(safe-area-inset-left) - env(safe-area-inset-right)),
      calc(100dvh - 250px - env(safe-area-inset-top) - env(safe-area-inset-bottom))
    );
    --tray-height: clamp(78px, 15dvh, 96px);
    --mini-cell: clamp(13px, 3.8vw, 17px);
  }

  .app {
    gap: 5px;
  }

  .mode-button {
    min-height: 28px;
  }

  .scorebox {
    padding: 5px 7px;
  }

  .board-wrap {
    padding: 6px;
  }
}

@media (orientation: landscape) and (max-height: 520px) {
  :root {
    --board-size: min(
      calc(100dvh - 132px - env(safe-area-inset-top) - env(safe-area-inset-bottom)),
      calc(55vw - 26px)
    );
    --tray-height: var(--board-size);
    --mini-cell: clamp(12px, 2.6vw, 18px);
  }

  .app {
    grid-template-rows: auto auto minmax(0, 1fr) auto;
    gap: 5px;
  }

  .stage {
    width: 100%;
    grid-template-columns: auto minmax(156px, 1fr);
    grid-template-rows: minmax(0, 1fr);
    justify-content: center;
    align-items: center;
    gap: 10px;
  }

  .tray {
    width: min(40vw, 260px);
    height: var(--board-size);
    min-height: 0;
    grid-template-columns: 1fr;
    grid-template-rows: repeat(3, minmax(0, 1fr));
  }

  .piece {
    height: auto;
    min-height: 0;
  }
}
