:root {
  --bg0: #050d1a;
  --text: #f4f7ff;
  --muted: #a8bad8;
  --gold: #ffd56a;
  --panel-border: rgba(140, 190, 255, 0.32);
  --cell-border: rgba(90, 140, 210, 0.22);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --pad-x: 3vw;
  --board-size: min(94vw, 62vh);
  --board-pad: clamp(8px, 1.6vw, 14px);
  --board-gap: clamp(5px, 1.05vw, 9px);
  --cell-r: clamp(9px, 1.8vw, 14px);
  --cell-size: calc((var(--board-size) - 2 * var(--board-pad) - 7 * var(--board-gap)) / 8);
  --tray-cell: calc(var(--cell-size) * 0.75);
  --header-h: clamp(72px, 9.5vw, 110px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; padding: 0; height: 100%;
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
  background:
    radial-gradient(ellipse 95% 70% at 50% 42%, rgba(70, 130, 230, 0.34) 0%, rgba(30, 70, 140, 0.14) 42%, transparent 68%),
    radial-gradient(ellipse 70% 40% at 50% 100%, rgba(40, 80, 160, 0.22) 0%, transparent 55%),
    linear-gradient(180deg, #153056 0%, #0b1a30 45%, #050d1a 100%);
}

body::before {
  content: "";
  position: fixed; inset: 0;
  pointer-events: none;
  background: radial-gradient(circle at 50% 48%, rgba(120, 180, 255, 0.12), transparent 55%);
  z-index: 0;
}

#app {
  position: relative;
  z-index: 1;
  height: 100%;
  width: 100%;
  max-width: none;
  margin: 0 auto;
  padding: calc(1.4vh + var(--safe-top)) var(--pad-x) calc(1vh + var(--safe-bottom));
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  gap: 1.2vh;
}

.topbar {
  width: min(90vw, var(--board-size));
  display: grid;
  grid-template-columns: 1.2fr 1fr auto;
  gap: clamp(10px, 2vw, 16px);
  align-items: stretch;
  min-height: var(--header-h);
  flex: 0 0 auto;
}

.score-box, .best-box {
  background: linear-gradient(180deg, rgba(42, 78, 132, 0.98), rgba(14, 32, 60, 0.98));
  border: 1px solid var(--panel-border);
  border-radius: clamp(16px, 3.2vw, 22px);
  padding: clamp(10px, 1.8vw, 16px) clamp(12px, 2.4vw, 20px);
  text-align: center;
  box-shadow:
    0 12px 28px rgba(0,0,0,0.42),
    inset 0 1px 0 rgba(255,255,255,0.14),
    inset 0 -1px 0 rgba(0,0,0,0.25);
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: center;
}
.best-box { padding-top: clamp(16px, 2.8vw, 24px); }
.box-label {
  font-size: clamp(0.78rem, 2.6vw, 1.05rem);
  letter-spacing: 0.18em;
  color: var(--muted);
  font-weight: 750;
}
.box-value {
  font-size: clamp(1.85rem, 7vw, 2.6rem);
  font-weight: 850;
  margin-top: 2px;
  font-variant-numeric: tabular-nums;
  line-height: 1.05;
  text-shadow: 0 2px 10px rgba(0,0,0,0.4);
}
.crown {
  position: absolute;
  top: -14px; left: 50%; transform: translateX(-50%);
  font-size: clamp(1.3rem, 4.5vw, 1.7rem);
  filter: drop-shadow(0 3px 6px rgba(0,0,0,0.55));
  line-height: 1;
}

.btn-gear {
  width: clamp(58px, 12vw, 78px);
  height: clamp(58px, 12vw, 78px);
  border-radius: 50%;
  border: 1px solid var(--panel-border);
  background: linear-gradient(180deg, #2a5080, #142848);
  color: #fff;
  font-size: clamp(1.5rem, 5vw, 2rem);
  box-shadow: 0 10px 22px rgba(0,0,0,0.42), inset 0 1px 0 rgba(255,255,255,0.12);
  cursor: pointer;
  align-self: center;
  display: grid; place-items: center;
  padding: 0;
}
.btn-gear:active { transform: scale(0.94); }

.mode-badge {
  flex: 0 0 auto;
  background: linear-gradient(180deg, #ffd56a, #e0a810);
  color: #1a1200;
  font-weight: 850;
  font-size: clamp(0.72rem, 2.3vw, 0.9rem);
  letter-spacing: 0.14em;
  padding: 6px 16px;
  border-radius: 999px;
  z-index: 4;
  box-shadow: 0 4px 14px rgba(0,0,0,0.35);
  width: max-content;
}
.mode-badge.hidden { display: none; }

.board-area {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  flex: 0 0 auto;
}
.board-frame {
  position: relative;
  width: var(--board-size);
  max-width: 100%;
  aspect-ratio: 1;
  background:
    radial-gradient(120% 90% at 50% 0%, rgba(55, 100, 170, 0.4), transparent 55%),
    linear-gradient(165deg, rgba(26, 50, 88, 0.98), rgba(8, 18, 36, 0.99));
  border-radius: clamp(18px, 3.5vw, 26px);
  padding: var(--board-pad);
  border: 1px solid rgba(130, 180, 250, 0.36);
  box-shadow:
    0 22px 52px rgba(0,0,0,0.52),
    0 0 48px rgba(70, 140, 240, 0.16),
    inset 0 1px 0 rgba(255,255,255,0.12);
}

.board {
  width: 100%; height: 100%;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  gap: var(--board-gap);
}

.cell {
  background: linear-gradient(180deg, #15263f 0%, #0e1a2e 100%);
  border-radius: var(--cell-r);
  border: 1px solid var(--cell-border);
  position: relative;
  box-shadow: inset 0 3px 6px rgba(0,0,0,0.45), inset 0 -1px 0 rgba(255,255,255,0.04);
}
.cell.ghost {
  outline: 2px solid rgba(94, 200, 255, 0.6);
  outline-offset: -2px;
  background: rgba(94, 200, 255, 0.2);
}
.cell.ghost.invalid {
  outline-color: rgba(255, 90, 90, 0.55);
  background: rgba(255, 80, 80, 0.14);
}
.cell.clearing {
  animation: clearPulse 0.55s ease-out forwards;
  z-index: 2;
}
.cell.clearing.freeze {
  animation: none;
  filter: brightness(2.2);
  box-shadow: 0 0 18px rgba(255, 235, 160, 0.95), inset 0 0 12px rgba(255,255,255,0.55);
}
.cell.clearing.freeze .block {
  filter: brightness(1.8) saturate(0.4);
  opacity: 0.35;
}

.block {
  position: absolute; inset: 0;
  border-radius: calc(var(--cell-r) - 1px);
  box-shadow:
    inset 0 3px 4px rgba(255,255,255,0.52),
    inset 0 -4px 6px rgba(0,0,0,0.32),
    0 3px 6px rgba(0,0,0,0.3);
}
.block::before {
  content: "";
  position: absolute;
  left: 8%; right: 8%; top: 7%;
  height: 38%;
  border-radius: 40% / 50%;
  background: linear-gradient(180deg, rgba(255,255,255,0.45), rgba(255,255,255,0));
  pointer-events: none;
}
.block::after {
  content: "";
  position: absolute;
  left: 14%; top: 12%;
  width: 34%; height: 22%;
  border-radius: 50%;
  background: rgba(255,255,255,0.55);
  filter: blur(0.5px);
  pointer-events: none;
}

.block.c0 { background: linear-gradient(155deg, #ffb06a 0%, #ff8a3a 40%, #d94a08 100%); }
.block.c1 { background: linear-gradient(155deg, #7af6ff 0%, #2ed4f0 40%, #0a9bc4 100%); }
.block.c2 { background: linear-gradient(155deg, #d4a4ff 0%, #a14cf0 40%, #6a20c8 100%); }
.block.c3 { background: linear-gradient(155deg, #ff9ae4 0%, #f04cb8 40%, #c01880 100%); }
.block.c4 { background: linear-gradient(155deg, #9dffaa 0%, #3fd864 40%, #18a840 100%); }
.block.c5 { background: linear-gradient(155deg, #ffe98a 0%, #f0c028 40%, #d09808 100%); }
.block.c6 { background: linear-gradient(155deg, #ff8a8a 0%, #f04050 40%, #c01828 100%); }
.block.c7 { background: linear-gradient(155deg, #8abcff 0%, #3a78f0 40%, #1848c0 100%); }

/* Line-clear beam */
.clear-beam {
  position: absolute;
  left: var(--board-pad);
  right: var(--board-pad);
  height: calc(var(--cell-size) * 0.72);
  border-radius: 999px;
  pointer-events: none;
  z-index: 7;
  background:
    linear-gradient(90deg,
      transparent 0%,
      rgba(255, 250, 220, 0.55) 6%,
      #fffef6 22%,
      #ffe9a0 50%,
      #fffef6 78%,
      rgba(255, 250, 220, 0.55) 94%,
      transparent 100%);
  box-shadow:
    0 0 10px 2px rgba(255, 245, 200, 0.95),
    0 0 28px 8px rgba(255, 220, 120, 0.85),
    0 0 56px 18px rgba(255, 200, 80, 0.55),
    0 0 90px 28px rgba(255, 180, 60, 0.28);
  animation: beamPulse 0.9s ease-in-out infinite alternate;
}
.clear-beam.freeze { animation: none; opacity: 1; }
.clear-beam::before {
  content: "";
  position: absolute;
  inset: -40% -2%;
  border-radius: 999px;
  background: radial-gradient(ellipse at center, rgba(255,255,255,0.55), transparent 70%);
  filter: blur(6px);
  opacity: 0.85;
}
@keyframes beamPulse {
  from { filter: brightness(1); transform: scaleY(1); }
  to { filter: brightness(1.25); transform: scaleY(1.08); }
}

.fx-canvas {
  position: absolute;
  left: var(--board-pad);
  top: var(--board-pad);
  width: calc(100% - 2 * var(--board-pad));
  height: calc(100% - 2 * var(--board-pad));
  pointer-events: none;
  z-index: 8;
}
.float-scores {
  position: absolute; inset: 0;
  pointer-events: none;
  z-index: 9;
  overflow: visible;
}
.float-score {
  position: absolute;
  left: 50%;
  transform: translate(-50%, 0);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  white-space: nowrap;
  animation: floatUp 1.1s ease-out forwards;
  pointer-events: none;
}
.float-score.freeze {
  animation: none;
  opacity: 1;
  transform: translate(-50%, -8px);
}
.float-score .pts {
  font-weight: 900;
  font-size: clamp(2rem, 8.5vw, 3.2rem);
  line-height: 1;
  color: #fffef8;
  letter-spacing: 0.02em;
  text-shadow:
    0 0 8px rgba(255,255,255,0.95),
    0 0 22px rgba(255, 230, 140, 1),
    0 0 42px rgba(255, 200, 80, 0.9),
    0 3px 10px rgba(0,0,0,0.65);
}
.float-score .combo-lbl {
  font-weight: 850;
  font-size: clamp(1.05rem, 4.2vw, 1.55rem);
  letter-spacing: 0.14em;
  color: #ffd56a;
  text-shadow:
    0 0 10px rgba(255, 210, 90, 0.95),
    0 0 24px rgba(255, 180, 40, 0.75),
    0 2px 6px rgba(0,0,0,0.6);
}
.float-score.combo .pts { color: #fffef8; }

@keyframes floatUp {
  0% { opacity: 0; transform: translate(-50%, 10px) scale(0.85); }
  18% { opacity: 1; transform: translate(-50%, -4px) scale(1.06); }
  100% { opacity: 0; transform: translate(-50%, -56px) scale(1); }
}
@keyframes clearPulse {
  0% { filter: brightness(1); box-shadow: 0 0 0 rgba(255,220,120,0); }
  40% { filter: brightness(2.5); box-shadow: 0 0 24px rgba(255,230,140,1); }
  100% { filter: brightness(3); opacity: 0; transform: scale(0.55); }
}

.tray-wrap {
  width: var(--board-size);
  max-width: 100%;
  flex: 0 0 auto;
  background: linear-gradient(180deg, rgba(30, 56, 98, 0.96), rgba(12, 26, 48, 0.98));
  border: 1px solid var(--panel-border);
  border-radius: clamp(18px, 3.5vw, 24px);
  padding: clamp(12px, 2vw, 18px) clamp(10px, 1.8vw, 14px);
  box-shadow:
    0 14px 32px rgba(0,0,0,0.42),
    inset 0 1px 0 rgba(255,255,255,0.1);
}
.tray {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: clamp(8px, 1.5vw, 12px);
  min-height: calc(var(--tray-cell) * 3.4 + 16px);
  align-items: center;
}
.tray-slot {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: calc(var(--tray-cell) * 3.2);
  border-radius: clamp(12px, 2.5vw, 16px);
  background: rgba(6, 14, 28, 0.45);
  position: relative;
  box-shadow: inset 0 2px 6px rgba(0,0,0,0.35);
}
.tray-piece {
  display: grid;
  gap: calc(var(--tray-cell) * 0.12);
  cursor: grab;
  touch-action: none;
  transition: transform 0.12s ease, opacity 0.12s ease;
  transform-origin: center center;
}
.tray-piece:active, .tray-piece.dragging { cursor: grabbing; }
.tray-piece.used { opacity: 0; pointer-events: none; }
.tray-cell {
  width: var(--tray-cell);
  height: var(--tray-cell);
  border-radius: calc(var(--tray-cell) * 0.28);
  position: relative;
  box-shadow:
    inset 0 2px 3px rgba(255,255,255,0.5),
    inset 0 -3px 4px rgba(0,0,0,0.28),
    0 2px 4px rgba(0,0,0,0.25);
}
.tray-cell::before {
  content: "";
  position: absolute;
  left: 10%; right: 10%; top: 8%;
  height: 36%;
  border-radius: 40%;
  background: linear-gradient(180deg, rgba(255,255,255,0.4), transparent);
}
.tray-cell::after {
  content: "";
  position: absolute;
  left: 16%; top: 14%;
  width: 32%; height: 22%;
  border-radius: 50%;
  background: rgba(255,255,255,0.55);
}
.tray-cell.c0 { background: linear-gradient(155deg, #ffb06a, #d94a08); }
.tray-cell.c1 { background: linear-gradient(155deg, #7af6ff, #0a9bc4); }
.tray-cell.c2 { background: linear-gradient(155deg, #d4a4ff, #6a20c8); }
.tray-cell.c3 { background: linear-gradient(155deg, #ff9ae4, #c01880); }
.tray-cell.c4 { background: linear-gradient(155deg, #9dffaa, #18a840); }
.tray-cell.c5 { background: linear-gradient(155deg, #ffe98a, #d09808); }
.tray-cell.c6 { background: linear-gradient(155deg, #ff8a8a, #c01828); }
.tray-cell.c7 { background: linear-gradient(155deg, #8abcff, #1848c0); }

.credit {
  text-align: center;
  margin: 0;
  font-size: clamp(0.7rem, 2vw, 0.82rem);
  color: rgba(158, 176, 208, 0.55);
  letter-spacing: 0.12em;
  flex: 0 0 auto;
  padding-bottom: 2px;
}

.drag-layer {
  position: fixed; inset: 0;
  pointer-events: none;
  z-index: 50;
}
.drag-piece {
  position: absolute;
  display: grid;
  gap: 4px;
  filter: drop-shadow(0 14px 22px rgba(0,0,0,0.5));
  will-change: transform;
}
.drag-cell {
  width: var(--drag-size, 40px);
  height: var(--drag-size, 40px);
  border-radius: 9px;
  position: relative;
  box-shadow:
    inset 0 2px 3px rgba(255,255,255,0.55),
    inset 0 -3px 5px rgba(0,0,0,0.3),
    0 2px 5px rgba(0,0,0,0.28);
}
.drag-cell::before {
  content: "";
  position: absolute;
  left: 10%; right: 10%; top: 8%;
  height: 36%;
  border-radius: 40%;
  background: linear-gradient(180deg, rgba(255,255,255,0.42), transparent);
}
.drag-cell::after {
  content: "";
  position: absolute;
  left: 16%; top: 14%;
  width: 32%; height: 22%;
  border-radius: 50%;
  background: rgba(255,255,255,0.55);
}
.drag-cell.c0 { background: linear-gradient(155deg, #ffb06a, #d94a08); }
.drag-cell.c1 { background: linear-gradient(155deg, #7af6ff, #0a9bc4); }
.drag-cell.c2 { background: linear-gradient(155deg, #d4a4ff, #6a20c8); }
.drag-cell.c3 { background: linear-gradient(155deg, #ff9ae4, #c01880); }
.drag-cell.c4 { background: linear-gradient(155deg, #9dffaa, #18a840); }
.drag-cell.c5 { background: linear-gradient(155deg, #ffe98a, #d09808); }
.drag-cell.c6 { background: linear-gradient(155deg, #ff8a8a, #c01828); }
.drag-cell.c7 { background: linear-gradient(155deg, #8abcff, #1848c0); }

.overlay {
  position: fixed; inset: 0;
  background: rgba(4, 10, 22, 0.74);
  backdrop-filter: blur(8px);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
  padding: 20px;
}
.overlay.hidden { display: none; }
.overlay-card {
  width: min(420px, 92%);
  background: linear-gradient(180deg, #1c3862, #0e1e38);
  border: 1px solid var(--panel-border);
  border-radius: 22px;
  padding: 26px 20px;
  box-shadow: 0 24px 56px rgba(0,0,0,0.55);
  display: flex; flex-direction: column; gap: 10px;
}
.overlay-card h2 {
  margin: 0 0 6px;
  text-align: center;
  font-size: clamp(1.5rem, 5vw, 1.85rem);
}
.overlay-card h2.new-best {
  background: linear-gradient(90deg, #ffd56a, #ffe9a8, #ffd56a);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.overlay-score, .overlay-mode, .about-text {
  text-align: center;
  color: var(--muted);
  margin: 0 0 6px;
  line-height: 1.45;
}
.overlay-btn {
  border: 1px solid var(--panel-border);
  background: rgba(255,255,255,0.06);
  color: var(--text);
  border-radius: 12px;
  padding: 14px 14px;
  font-size: 1.05rem;
  font-weight: 650;
  cursor: pointer;
}
.overlay-btn.primary {
  background: linear-gradient(180deg, #3aa0ff, #1a68d4);
  border-color: transparent;
}
.overlay-btn.share {
  background: linear-gradient(180deg, #34c759, #1f9a42);
  border-color: transparent;
}
.overlay-btn.ghost { opacity: 0.85; }
.overlay-btn:active { transform: scale(0.98); }

/* Landscape / very wide: keep phone column centered, never shrink board below 94vw of a phone column */
@media (orientation: landscape) and (min-width: 900px) {
  #app {
    max-width: min(560px, 62vh);
  }
  :root {
    --board-size: min(94vw, 62vh, 520px);
  }
}

@media (max-height: 680px) {
  :root { --header-h: 58px; }
  .credit { display: none; }
  #app { gap: 0.6vh; }
}
