:root {
  color-scheme: light;
  --ink: #3a3a3a;
  --muted: #8a9ab0;
  --muted-light: #dde4ee;   /* 薄いブルーグレー：背景アクセント用 */
  --muted-mid: #b4c0ce;     /* 中間ブルーグレー：セル・トラック用 */
  --surface: #f5f5f5;
  --panel-bg: #ffffff;
  --cell-bg: var(--muted-light);
  --shadow-sm: 0 2px 6px rgba(100, 130, 170, 0.18);
  --shadow-md: 0 6px 20px rgba(100, 130, 170, 0.20);
  --shadow-lg: 0 14px 36px rgba(100, 130, 170, 0.22);
  --spring: cubic-bezier(0.34, 1.56, 0.64, 1);
  --smooth: cubic-bezier(0.4, 0, 0.2, 1);
  --danger: #e05252;
}

* {
  box-sizing: border-box;
}

body {
  min-height: 100vh;
  margin: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  background: #eef1f6;
  color: var(--ink);
  font-family: "Manrope", ui-sans-serif, system-ui, -apple-system, sans-serif;
}

button {
  min-height: 42px;
  border: none;
  border-radius: 6px;
  background: var(--muted);
  color: #fff;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  transition:
    transform 160ms var(--spring),
    background 140ms var(--smooth),
    box-shadow 140ms var(--smooth);
  box-shadow: 0 3px 0 rgba(80, 110, 150, 0.20);
}

button:hover:not(:disabled):not(.cell) {
  background: color-mix(in srgb, var(--muted) 85%, #000);
  transform: translateY(2px);
  box-shadow: 0 1px 0 rgba(80, 110, 150, 0.12);
}

button:active:not(:disabled):not(.cell) {
  transform: translateY(3px);
  box-shadow: none;
}

button:disabled:not(.cell) {
  cursor: not-allowed;
  opacity: 0.4;
}

.game-shell {
  width: min(100%, 620px);
  display: grid;
  gap: 12px;
}

.top-bar,
.control-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 16px 20px;
  border-radius: 12px;
  background: var(--panel-bg);
  box-shadow: var(--shadow-md);
}

.eyebrow {
  margin: 0 0 2px;
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

h1 {
  margin: 0;
  font-size: clamp(1.5rem, 4vw, 2.4rem);
  line-height: 1;
  color: var(--ink);
}

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

.stat {
  min-width: 96px;
  padding: 8px 12px;
  border-radius: 8px;
  background: var(--muted);
  text-align: center;
  box-shadow: inset 0 2px 4px rgba(80, 110, 150, 0.14);
}

.panel-label {
  display: block;
  color: var(--muted);
}

.stat span {
  display: block;
  color: rgba(255, 255, 255, 0.75);
  font-size: 0.72rem;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.stat strong {
  display: block;
  margin-top: 2px;
  font-size: 1.3rem;
  font-weight: 800;
  color: #fff;
  font-variant-numeric: tabular-nums;
}

.goal-panel {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 20px;
  border-radius: 12px;
  background: var(--panel-bg);
  box-shadow: var(--shadow-md);
}

.goal-copy strong {
  display: flex;
  align-items: center;
  gap: 5px;
  margin-top: 0;
  font-size: 0.82rem;
  font-weight: 800;
  text-transform: uppercase;
}

.goal-prefix {
  color: var(--muted);
}

.goal-text-icon {
  display: flex;
  align-items: center;
  flex: 0 0 auto;
}

.goal-text-icon-chip {
  --goal-color: #d94a52;
  width: 15px;
  flex: 0 0 15px;
  aspect-ratio: 0.866 / 1;
  clip-path: url(#hex-r);
  background: var(--goal-color);
  box-shadow:
    0 2px 4px rgba(80, 110, 150, 0.22),
    inset 0 1px 0 rgba(255, 255, 255, 0.34);
}

.goal-text-icon-chip + .goal-text-icon-chip {
  margin-left: -5px;
}

.goal-meter {
  display: grid;
  grid-template-columns: max-content auto;
  align-items: center;
  justify-content: end;
  gap: 6px;
  min-width: min(100%, 320px);
  min-height: 32px;
}

.goal-tiles {
  --goal-color: #d94a52;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  min-height: 32px;
  overflow: visible;
  transform-origin: right center;
}

.goal-tiles.collecting {
  animation: goal-track-receive 980ms var(--spring) both;
}

.goal-tile-chip {
  width: 20px;
  flex: 0 0 20px;
  aspect-ratio: 0.866 / 1;
  clip-path: url(#hex-r);
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.35), transparent 38%),
    var(--goal-color);
  box-shadow:
    -4px 0 0 color-mix(in srgb, var(--goal-color) 72%, #000000),
    0 4px 6px rgba(80, 110, 150, 0.32),
    inset 0 1px 0 rgba(255, 255, 255, 0.34);
  position: relative;
  display: grid;
  place-items: center;
}

.goal-tile-chip.new {
  z-index: 12;
  animation: goal-chip-stack 1080ms var(--spring) both;
}

.goal-tile-chip + .goal-tile-chip {
  margin-left: -7px;
}

.goal-tile-chip.stack {
  width: 20px;
  flex-basis: 20px;
  margin-right: 0;
  box-shadow:
    -5px 0 0 color-mix(in srgb, var(--goal-color) 68%, #000000),
    -9px 0 0 color-mix(in srgb, var(--goal-color) 52%, #000000),
    0 4px 7px rgba(80, 110, 150, 0.34),
    inset 0 1px 0 rgba(255, 255, 255, 0.34);
}

.goal-tile-chip.stack + .goal-tile-chip,
.goal-tile-chip + .goal-tile-chip.stack {
  margin-left: -5px;
}

.goal-tile-stack-count {
  color: #fff;
  font-size: 0.62rem;
  font-weight: 900;
  line-height: 1;
  text-shadow: 0 1px 2px rgba(20, 35, 58, 0.35);
}

.goal-count {
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

/* ── Board ── */
.board-wrap {
  position: relative;
  padding: clamp(12px, 3vw, 20px);
  border-radius: 14px;
  background: var(--panel-bg);
  box-shadow: var(--shadow-lg);
}

.board {
  --hex-size: clamp(34px, 11vw, 62px);
  --hex-gap: clamp(3px, 0.7vw, 6px);
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  width: min(100%, 520px);
  margin: 0 auto;
  padding: calc(var(--hex-size) * 0.12) calc(var(--hex-size) * 0.34) calc(var(--hex-size) * 0.18);
}

.board-row {
  display: flex;
  justify-content: center;
  gap: var(--hex-gap);
  transform: translateX(calc((var(--hex-size) + var(--hex-gap)) / -4));
}

.board-row + .board-row {
  margin-top: calc(var(--hex-size) * -0.29 + var(--hex-gap));
}

.board-row.offset {
  transform: translateX(calc((var(--hex-size) + var(--hex-gap)) / 4));
}

.cell {
  position: relative;
  display: grid;
  place-items: center;
  width: var(--hex-size);
  min-height: auto;
  min-width: 0;
  aspect-ratio: 0.866 / 1;
  border: 0;
  clip-path: url(#hex-r);
  background: var(--cell-bg);
  cursor: pointer;
  transition:
    transform 200ms var(--spring),
    filter 140ms var(--smooth),
    background 140ms var(--smooth);
  box-shadow: none;
}

.cell:hover:not(.filled):not(.locked):not(.inactive) {
  background: var(--muted-mid);
}

.cell.inactive:hover {
  filter: brightness(0.88);
}

.cell.erase-target {
  filter: drop-shadow(0 0 8px rgba(224, 82, 82, 0.75));
  cursor: crosshair;
}

.cell.erase-target:hover {
  filter: drop-shadow(0 0 12px rgba(224, 82, 82, 0.9));
  transform: scale(1.05);
}

.cell.locked,
.cell.inactive {
  cursor: default;
}

.cell.inactive {
  background:
    repeating-linear-gradient(
      45deg,
      var(--muted) 0px,
      var(--muted) 4px,
      var(--muted-light) 4px,
      var(--muted-light) 10px
    );
  opacity: 0.6;
}

/* ── Tile ── */
.tile {
  --tile-color: #d94a52;
  position: relative;
  display: grid;
  place-items: center;
  width: 82%;
  aspect-ratio: 0.866 / 1;
  border: 0;
  clip-path: url(#hex-r);
  background: linear-gradient(
    150deg,
    color-mix(in srgb, var(--tile-color) 78%, #fff) 0%,
    var(--tile-color) 55%,
    color-mix(in srgb, var(--tile-color) 84%, #000) 100%
  );
  color: #ffffff;
  font-weight: 900;
  text-shadow: 0 1px 3px rgba(80, 110, 150, 0.26);
  filter: drop-shadow(0 3px 6px rgba(80, 110, 150, 0.22));
  transition: transform 180ms var(--spring), filter 140ms var(--smooth);
}

/* Multi-layer: show a subtle second-color band at the bottom */
.tile.multi-layer {
  --bottom-color: #555;
  background:
    linear-gradient(
      150deg,
      color-mix(in srgb, var(--tile-color) 78%, #fff) 0%,
      var(--tile-color) 55%,
      color-mix(in srgb, var(--tile-color) 84%, #000) 74%,
      var(--bottom-color) 74%
    );
}

/* Remove all pseudo-elements – no white lines */
.tile::before,
.tile::after {
  content: none;
}

.tile-layer {
  display: none;
}

.tile-top {
  display: none;
}

.tile-count {
  position: relative;
  z-index: 20;
  font-size: clamp(1rem, 4vw, 1.7rem);
  font-variant-numeric: tabular-nums;
  line-height: 1;
}

/* ── Number roll (slot machine) ── */
.cell.number-roll .tile-count {
  animation: slot-spin 55ms steps(1, end) infinite;
}

.cell.number-final .tile-count {
  animation: number-final-pop 200ms var(--spring) both;
}

.cell.number-final .tile {
  animation: tile-glow 200ms var(--smooth) both;
}

/* ── Next tiles ── */
.next-tiles {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 58px;
}

.preview-tile {
  width: 52px;
  cursor: pointer;
  transition:
    transform 180ms var(--spring),
    filter 140ms var(--smooth);
}

.preview-tile:hover {
  transform: translateY(-3px) scale(1.1);
  filter: drop-shadow(0 6px 10px rgba(80, 110, 150, 0.26));
}

.preview-tile.selected {
  transform: scale(1.12) translateY(-3px);
  filter:
    drop-shadow(0 0 0 3px rgba(255,255,255,0.9))
    drop-shadow(0 6px 12px rgba(80, 110, 150, 0.26));
}

.next-tile-panel {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 160px;
}

.actions {
  display: flex;
  gap: 8px;
}

.actions button {
  padding: 0 16px;
}

#erase-button.active {
  background: #c0514f;
  box-shadow: 0 3px 0 rgba(80, 110, 150, 0.22);
}

#reroll-button.ready {
  background: #4f8cc0;
  box-shadow: 0 3px 0 rgba(80, 110, 150, 0.22);
}

.color-total-bar {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 7px;
  padding: 10px 16px 12px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.76);
  box-shadow: var(--shadow-sm);
}

.color-total-bar .eyebrow {
  margin: 0;
  flex: 0 0 auto;
  font-size: 0.68rem;
}

.color-totals {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
  width: 100%;
}

.color-total-item {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  min-height: 24px;
  padding: 3px 7px;
  border-radius: 999px;
  background: #fff;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 800;
  box-shadow: inset 0 0 0 1px rgba(138, 154, 176, 0.18);
}

.color-total-icon {
  --color-total: #d94a52;
  width: 13px;
  flex: 0 0 13px;
  aspect-ratio: 0.866 / 1;
  clip-path: url(#hex-r);
  background: var(--color-total);
}

.color-total-name {
  color: var(--muted);
}

.color-total-count {
  min-width: 1.5em;
  color: var(--ink);
  font-size: 0.78rem;
  font-variant-numeric: tabular-nums;
  text-align: right;
}

/* ── Game over ── */
.game-over {
  position: absolute;
  inset: 12px;
  display: grid;
  place-content: center;
  gap: 10px;
  border-radius: 10px;
  background: rgba(10, 8, 6, 0.80);
  color: #ffffff;
  text-align: center;
}

.game-over[hidden] {
  display: none;
}

.game-over strong {
  font-size: clamp(2rem, 8vw, 4rem);
}

.game-over span {
  color: #d0d0d0;
  font-weight: 600;
}

.game-over-restart {
  margin-top: 8px;
  padding: 12px 28px;
  border: none;
  border-radius: 12px;
  background: #ffffff;
  color: #1e2330;
  font-family: var(--font);
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  transition: transform 120ms var(--spring), background 120ms var(--smooth), color 120ms var(--smooth);
}

.game-over-restart:hover {
  background: rgba(255, 255, 255, 0.15);
  color: #ffffff;
  transform: translateY(2px);
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.08);
}

.game-over-restart:active {
  transform: translateY(3px);
  box-shadow: none;
}

/* ── Flash animations ── */
.cell.merge-flash .tile {
  animation: merge-pop 420ms var(--spring) both;
}

.cell.clear-flash {
  animation: clear-burst 320ms var(--smooth) both;
}

.cell.chain-flash .tile {
  animation: chain-pop 150ms var(--spring) both;
}

/* Cleared tile: ふわーっと消える */
@keyframes tile-clear-out {
  0%   { transform: scale(1);    opacity: 1;   filter: brightness(1); }
  25%  { transform: scale(1.18); opacity: 0.9; filter: brightness(1.6); }
  100% { transform: scale(1.5);  opacity: 0;   filter: brightness(2); }
}

.cell.tile-clear-out .tile {
  animation: tile-clear-out 520ms var(--smooth) forwards;
}

/* Absorbed tiles shrink into the survivor */
@keyframes tile-absorb {
  0%   { transform: scale(1);   opacity: 1; }
  100% { transform: scale(0.2); opacity: 0; }
}

.cell.tile-absorb .tile {
  animation: tile-absorb 110ms var(--smooth) forwards;
}

/* Survivor: jelly scaleX/scaleY oscillation */
@keyframes merge-pop {
  0%   { transform: scale(0.82); }
  18%  { transform: scaleX(1.28) scaleY(0.78); filter: brightness(1.3); }
  34%  { transform: scaleX(0.86) scaleY(1.22); filter: brightness(1.1); }
  48%  { transform: scaleX(1.12) scaleY(0.93); }
  60%  { transform: scaleX(0.95) scaleY(1.07); }
  72%  { transform: scaleX(1.05) scaleY(0.97); }
  84%  { transform: scaleX(0.99) scaleY(1.02); }
  100% { transform: scale(1);   filter: brightness(1); }
}

@keyframes chain-pop {
  0%   { transform: scale(0.88); filter: brightness(1.3); }
  55%  { transform: scale(1.14); }
  100% { transform: scale(1); }
}

@keyframes clear-burst {
  0%   { filter: drop-shadow(0 0 0px rgba(255,220,50,0)) brightness(1); }
  32%  { filter: drop-shadow(0 0 18px rgba(255,220,50,1)) brightness(1.7); }
  65%  { filter: drop-shadow(0 0 10px rgba(255,190,50,0.5)) brightness(1.2); }
  100% { filter: drop-shadow(0 0 0px rgba(255,220,50,0)) brightness(1); }
}

@keyframes slot-spin {
  0%   { transform: translateY(-3px) scaleY(0.86); opacity: 0.65; }
  35%  { transform: translateY(0)    scaleY(1.05); opacity: 1;    }
  70%  { transform: translateY(3px)  scaleY(0.86); opacity: 0.65; }
  100% { transform: translateY(-3px) scaleY(0.86); opacity: 0.65; }
}

@keyframes number-final-pop {
  0%   { transform: scale(1.65) translateY(-1px); opacity: 0.75; }
  55%  { transform: scale(0.9); opacity: 1; }
  80%  { transform: scale(1.08); }
  100% { transform: scale(1); }
}

@keyframes tile-exit {
  0%   { transform: scale(1) translateY(0); opacity: 1; }
  100% { transform: scale(0.65) translateY(-8px); opacity: 0; }
}

.preview-tile.tile-exit {
  animation: tile-exit 130ms var(--smooth) forwards;
  pointer-events: none;
}

@keyframes tile-pop {
  0%   { transform: translateY(-3px) scale(1.12); }
  35%  { transform: scaleX(1.2) scaleY(0.82); }
  100% { transform: scale(1.12) translateY(-3px); }
}

.preview-tile.tile-pop {
  animation: tile-pop 160ms var(--spring) both;
}

@keyframes tile-place {
  0%   { transform: scale(0.4) translateY(6px); opacity: 0.6; }
  55%  { transform: scale(1.22) translateY(-3px); opacity: 1; }
  75%  { transform: scale(0.93) translateY(1px); }
  90%  { transform: scale(1.06); }
  100% { transform: scale(1) translateY(0); }
}

.tile.placing {
  animation: tile-place 260ms var(--spring) both;
}

@keyframes tile-glow {
  0%   { filter: drop-shadow(0 0 18px rgba(255,255,255,0.95)) brightness(1.5); }
  100% { filter: drop-shadow(0 3px 6px rgba(80, 110, 150, 0.22)) brightness(1); }
}

/* ── Level clear popup ── */
.level-clear-overlay {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(20, 30, 50, 0.45);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  z-index: 100;
  animation: overlay-in 160ms var(--smooth) both;
}

.level-clear-overlay[hidden] {
  display: none;
}

.level-clear-popup {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 40px 48px;
  border-radius: 20px;
  background: #fff;
  box-shadow: 0 24px 60px rgba(80, 110, 150, 0.28);
  text-align: center;
  animation: popup-in 260ms var(--spring) both;
}

.level-clear-eyebrow {
  margin: 0;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.level-clear-level {
  margin: 0;
  font-size: 3rem;
  font-weight: 800;
  color: var(--ink);
  line-height: 1;
}

.level-clear-message {
  margin: 0;
  color: var(--muted);
  font-size: 0.95rem;
  font-weight: 600;
}

.level-clear-btn {
  margin-top: 12px;
  padding: 0 28px;
  min-height: 44px;
  font-size: 1rem;
}

@keyframes overlay-in {
  from { opacity: 0; }
  to   { opacity: 1; }
}

@keyframes popup-in {
  from { transform: scale(0.8) translateY(12px); opacity: 0; }
  to   { transform: scale(1) translateY(0); opacity: 1; }
}

@keyframes goal-chip-stack {
  0% {
    opacity: 0;
    transform: translateX(128px) translateY(-18px) scale(0.34) rotate(28deg);
  }
  30% {
    opacity: 1;
    transform: translateX(52px) translateY(-6px) scale(0.84) rotate(12deg);
  }
  58% {
    opacity: 1;
    transform: translateX(-14px) translateY(2px) scale(1.18) rotate(-6deg);
  }
  76% {
    opacity: 1;
    transform: translateX(6px) translateY(0) scale(0.96) rotate(2deg);
  }
  100% {
    opacity: 1;
    transform: translateX(0) translateY(0) scale(1) rotate(0);
  }
}

@keyframes goal-track-receive {
  0% {
    transform: translateX(0) scale(1);
  }
  48% {
    transform: translateX(-5px) scale(1.04);
  }
  70% {
    transform: translateX(2px) scale(0.99);
  }
  100% {
    transform: translateX(0) scale(1);
  }
}

/* ── Responsive ── */
@media (max-width: 520px) {
  body {
    padding: 8px;
    align-items: start;
  }

  .game-shell {
    gap: 8px;
  }

  .top-bar,
  .goal-panel,
  .control-bar {
    align-items: stretch;
    flex-direction: column;
    padding: 12px 14px;
    border-radius: 10px;
  }

  .stats,
  .actions {
    width: 100%;
  }

  .color-total-bar {
    align-items: flex-start;
    flex-direction: column;
  }

  .stat {
    flex: 1;
    min-width: 0;
    padding: 6px 8px;
  }

  .stat strong {
    font-size: 1.1rem;
  }

  .actions button {
    flex: 1;
  }

  .next-tile-panel {
    justify-content: space-between;
    width: 100%;
  }

  .goal-meter {
    min-width: 0;
    width: 100%;
  }

  .board-wrap {
    padding: 8px 4px;
    border-radius: 10px;
  }

  .board {
    --hex-size: clamp(36px, 11.8vw, 48px);
    --hex-gap: 3px;
    width: 100%;
    padding: calc(var(--hex-size) * 0.1) calc(var(--hex-size) * 0.22) calc(var(--hex-size) * 0.12);
  }

  .level-clear-popup {
    width: calc(100% - 32px);
    max-width: 320px;
    padding: 30px 20px;
  }
}

/* ── Rock obstacle tile ─────────────────────────────────── */
.rock-cell {
  cursor: not-allowed !important;
}

.rock-cell:hover .rock-tile,
.rock-cell:focus .rock-tile {
  filter: brightness(0.85);
}

.rock-tile {
  width: 100%;
  height: 100%;
  clip-path: url(#hex-r);
  background: transparent;
  display: grid;
  place-items: center;
}

.rock-tile::after {
  content: "🪨";
  font-size: 1.8em;
  line-height: 1;
}

/* ── Chain bonus badge ──────────────────────────────────── */
.chain-badge {
  position: absolute;
  transform: translateX(-50%) translateY(calc(-100% - 8px));
  background: #1e2330;
  color: #fff;
  font-family: var(--font);
  font-size: 0.7rem;
  font-weight: 800;
  letter-spacing: 0.05em;
  padding: 4px 10px;
  border-radius: 8px;
  white-space: nowrap;
  pointer-events: none;
  z-index: 20;
  box-shadow: 0 4px 12px rgba(0,0,0,0.25);
  animation: chain-tooltip 1.0s ease forwards;
}

.chain-badge::after {
  content: "";
  position: absolute;
  top: 100%;
  left: 50%;
  transform: translateX(-50%);
  border: 5px solid transparent;
  border-top-color: #1e2330;
}

@keyframes chain-tooltip {
  0%   { opacity: 0; transform: translateX(-50%) translateY(calc(-100% - 8px + 12px)) scale(0.8); }
  18%  { opacity: 1; transform: translateX(-50%) translateY(calc(-100% - 8px - 4px)) scale(1.08); }
  32%  { transform: translateX(-50%) translateY(calc(-100% - 8px + 2px)) scale(0.97); }
  46%  { transform: translateX(-50%) translateY(calc(-100% - 8px - 2px)) scale(1.02); }
  60%  { transform: translateX(-50%) translateY(calc(-100% - 8px)) scale(1); }
  75%  { opacity: 1; }
  100% { opacity: 0; transform: translateX(-50%) translateY(calc(-100% - 8px - 8px)) scale(0.95); }
}
