/* ───────────────── Blockbloom — candy claymorphism ─────────────────
   Calm sugar chrome, loud candy blocks, bouncy spring motion.
   Build matches design.md. */

:root {
  --sugar: #FBEEE3;
  --sugar-deep: #F3E1D2;
  --tray-well: #EAD7C6;
  --cell-empty: #F6E7DA;
  --cocoa: #4A3527;
  --cocoa-muted: #9A8473;
  --grape: #7C5CFC;
  --grape-deep: #5B3FD6;
  --gold: #FFB200;

  --r-cell: 24%;
  --r-panel: 24px;
  --ease-spring: cubic-bezier(.34, 1.56, .64, 1);
  --ease-out: cubic-bezier(.16, 1, .3, 1);

  --shadow-raise: 0 4px 0 rgba(120, 84, 54, .18), 0 7px 14px -4px rgba(120, 84, 54, .35);
  --shadow-inset: inset 0 4px 9px rgba(120, 84, 54, .28), inset 0 -2px 4px rgba(255, 255, 255, .5);
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; }
body {
  margin: 0;
  min-height: 100dvh;
  background: radial-gradient(120% 90% at 50% 0%, #FFF6EE 0%, var(--sugar) 55%, var(--sugar-deep) 100%);
  color: var(--cocoa);
  font-family: "DM Sans", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
  touch-action: manipulation;
  user-select: none; -webkit-user-select: none;
}
button { font: inherit; color: inherit; cursor: pointer; border: none; background: none; }

#fx { position: fixed; inset: 0; z-index: 60; pointer-events: none; }
.popup-layer { position: fixed; inset: 0; z-index: 70; pointer-events: none; display: grid; place-items: center; }

/* ───────── shell ───────── */
.app {
  max-width: 520px;
  margin: 0 auto;
  min-height: 100dvh;
  padding: max(16px, env(safe-area-inset-top)) 16px max(20px, env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 18px;
  justify-content: space-between;
}
.app.is-shaking { animation: shake .36s var(--ease-out); }
@keyframes shake {
  0%,100% { transform: translate(0,0); }
  15% { transform: translate(calc(var(--shake,1)*-3px), calc(var(--shake,1)*2px)); }
  35% { transform: translate(calc(var(--shake,1)*4px), calc(var(--shake,1)*-2px)); }
  55% { transform: translate(calc(var(--shake,1)*-3px), calc(var(--shake,1)*3px)); }
  75% { transform: translate(calc(var(--shake,1)*2px), calc(var(--shake,1)*-1px)); }
}

/* ───────── topbar ───────── */
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.icon-btn {
  width: 48px; height: 48px; border-radius: 50%;
  background: var(--sugar);
  box-shadow: var(--shadow-raise);
  display: grid; place-items: center; flex: none;
  transition: transform .15s var(--ease-out), box-shadow .15s var(--ease-out);
}
.icon-btn:active { transform: translateY(2px) scale(.96); box-shadow: 0 2px 0 rgba(120,84,54,.18); }
.icon-btn svg { width: 24px; height: 24px; fill: none; stroke: var(--cocoa); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.score-wrap { text-align: center; flex: 1; }
.score {
  margin: 0; font-family: "Nunito", sans-serif; font-weight: 900;
  font-size: clamp(2.6rem, 12vw, 4rem); line-height: .9;
  font-variant-numeric: tabular-nums; letter-spacing: -.02em;
  color: var(--cocoa);
}
.score.pop { animation: scorePop .42s var(--ease-spring); }
@keyframes scorePop { 0% { transform: scale(1); } 35% { transform: scale(1.14); color: var(--gold); } 100% { transform: scale(1); } }
.score-cap { margin: 2px 0 0; font-size: .74rem; font-weight: 700; letter-spacing: .12em; color: var(--cocoa-muted); }
.score-cap #best { color: var(--cocoa); }
.combo { color: var(--gold); font-family: "Nunito"; font-weight: 900; margin-right: 8px; }

/* ───────── board ───────── */
.board-wrap { display: grid; place-items: center; }
.board {
  --n: 8;
  width: min(92vw, 460px); aspect-ratio: 1;
  display: grid;
  grid-template-columns: repeat(var(--n), 1fr);
  grid-template-rows: repeat(var(--n), 1fr);
  gap: 5px; padding: 10px;
  background: var(--tray-well);
  border-radius: var(--r-panel);
  box-shadow: var(--shadow-inset), 0 1px 0 rgba(255,255,255,.6);
}
.board.hint-flash { animation: hintFlash .6s var(--ease-out); }
@keyframes hintFlash { 0%,100% { box-shadow: var(--shadow-inset); } 50% { box-shadow: var(--shadow-inset), 0 0 0 3px var(--gold); } }

.cell {
  border-radius: var(--r-cell);
  background: var(--cell-empty);
  box-shadow: inset 0 2px 3px rgba(120,84,54,.14);
  position: relative;
  transition: transform .12s var(--ease-out);
}
.cell.filled, .cell.ghost {
  background: linear-gradient(180deg, var(--top), var(--base));
  box-shadow:
    inset 0 3px 0 rgba(255,255,255,.5),
    inset 0 -5px 6px rgba(0,0,0,.16),
    0 3px 5px rgba(0,0,0,.18);
}
.cell.placed { animation: squash .34s var(--ease-spring); }
@keyframes squash { 0% { transform: scale(.55); } 55% { transform: scale(1.12); } 100% { transform: scale(1); } }
.cell.ghost { opacity: .5; }
.cell.will-clear { animation: willClear .7s ease-in-out infinite; }
@keyframes willClear { 0%,100% { box-shadow: inset 0 0 0 2px var(--gold), 0 0 8px rgba(255,178,0,.6); } 50% { box-shadow: inset 0 0 0 3px var(--gold), 0 0 16px rgba(255,178,0,.9); } }
.cell.bad { background: #F4C7C2; box-shadow: inset 0 0 0 2px #E0584A; }
.cell.clearing { animation: clearFlash .3s ease-out forwards; z-index: 2; }
@keyframes clearFlash { 0% { filter: brightness(1); } 40% { filter: brightness(1.9) saturate(1.3); transform: scale(1.08); } 100% { filter: brightness(2.4); transform: scale(.2); opacity: 0; } }

/* ───────── tray + hold ───────── */
.tray-rail { display: flex; align-items: stretch; gap: 12px; }
.hold {
  flex: none; width: 88px; min-height: 96px;
  border-radius: 20px; background: var(--sugar-deep);
  box-shadow: var(--shadow-inset);
  display: grid; place-items: center; padding: 8px; position: relative;
}
.hold::after {
  content: "HOLD"; position: absolute; top: 6px; left: 0; right: 0;
  text-align: center; font-size: .58rem; font-weight: 700; letter-spacing: .14em; color: var(--cocoa-muted);
}
.hold.empty::before {
  content: ""; width: 30px; height: 30px; border-radius: 9px;
  border: 2px dashed var(--cocoa-muted); opacity: .4;
}
.tray {
  flex: 1; display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px;
  background: var(--sugar-deep); border-radius: 20px; padding: 12px 10px;
  box-shadow: var(--shadow-inset);
}
.tray-slot, .hold { transition: transform .15s var(--ease-out), background .15s var(--ease-out); }
.tray-slot { display: grid; place-items: center; min-height: 76px; border-radius: 14px; }
/* the whole slot (not just the gummy) is the grab/tap target + hover highlight */
.tray-slot:not(.empty), .hold:not(.empty) { cursor: grab; }
.tray-slot:not(.empty):hover, .hold:not(.empty):hover { background: rgba(124, 92, 252, 0.10); }
.tray-slot:not(.empty):hover .piece, .hold:not(.empty):hover .piece { transform: scale(1.12); }

.piece {
  --cell: clamp(12px, 4.2vw, 20px);
  display: grid;
  grid-template-columns: repeat(var(--pw), var(--cell));
  grid-template-rows: repeat(var(--ph), var(--cell));
  gap: 3px; cursor: grab;
  transition: transform .16s var(--ease-spring);
  touch-action: none;
}
.piece:hover { transform: scale(1.06); }
.piece.selected { transform: scale(1.08); filter: drop-shadow(0 0 0 var(--grape)); }
.piece.selected .gummy { outline: 2px solid var(--grape); outline-offset: 1px; }
.piece.dragging-src { opacity: .25; }
.hold .piece { --cell: clamp(11px, 3.6vw, 17px); }

.gummy {
  border-radius: 28%;
  background: linear-gradient(180deg, var(--top), var(--base));
  box-shadow:
    inset 0 2px 0 rgba(255,255,255,.55),
    inset 0 -3px 4px rgba(0,0,0,.18),
    0 2px 3px rgba(0,0,0,.2);
}

.drag-clone {
  position: fixed; z-index: 65; pointer-events: none;
  display: grid;
  grid-template-columns: repeat(var(--pw), var(--cell));
  grid-template-rows: repeat(var(--ph), var(--cell));
  gap: 5px; transform: scale(1.06);
  filter: drop-shadow(0 10px 14px rgba(0,0,0,.3));
}
.drag-clone .gummy { border-radius: var(--r-cell); }

/* ───────── action rail ───────── */
.action-rail { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.act {
  position: relative;
  background: var(--sugar);
  border-radius: 18px; padding: 10px 6px 9px;
  box-shadow: var(--shadow-raise);
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  font-size: .72rem; font-weight: 700; color: var(--cocoa);
  min-height: 60px;
  transition: transform .14s var(--ease-out), box-shadow .14s var(--ease-out), opacity .2s;
}
.act svg { width: 22px; height: 22px; fill: none; stroke: var(--cocoa); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.act i { font-style: normal; color: var(--grape); }
.act:not(:disabled):active { transform: translateY(3px); box-shadow: 0 1px 0 rgba(120,84,54,.18); }
.act:disabled { opacity: .42; cursor: default; }
.act.reroll { overflow: hidden; }
.act.reroll.ready { background: linear-gradient(180deg, #FFD36B, var(--gold)); box-shadow: var(--shadow-raise), 0 0 16px rgba(255,178,0,.6); animation: readyPulse 1.4s ease-in-out infinite; }
.act.reroll.ready svg, .act.reroll.ready { stroke: #5A3B00; color: #5A3B00; }
@keyframes readyPulse { 0%,100% { transform: scale(1); } 50% { transform: scale(1.04); } }
.charge { position: absolute; left: 6px; right: 6px; bottom: 5px; height: 4px; border-radius: 2px; background: rgba(120,84,54,.18); overflow: hidden; }
.charge-fill { display: block; height: 100%; width: 0%; background: var(--gold); border-radius: 2px; transition: width .35s var(--ease-out); }
.act.reroll.ready .charge { display: none; }

.hint-line { margin: 0; text-align: center; font-size: .78rem; color: var(--cocoa-muted); }
.hint-line b { color: var(--grape); }

/* ───────── combo popup ───────── */
.combo-popup {
  font-family: "Nunito"; font-weight: 900;
  font-size: calc(1.7rem + var(--heat,1) * .25rem);
  color: var(--gold);
  text-shadow: 0 3px 0 rgba(255,178,0,.35), 0 6px 16px rgba(255,178,0,.5);
  -webkit-text-stroke: 2px #fff;
  animation: comboPop 1.1s var(--ease-spring) forwards;
}
@keyframes comboPop {
  0% { transform: scale(.3) translateY(20px); opacity: 0; }
  25% { transform: scale(1.15) translateY(-6px); opacity: 1; }
  70% { transform: scale(1) translateY(-10px); opacity: 1; }
  100% { transform: scale(.9) translateY(-46px); opacity: 0; }
}

/* ───────── modals ───────── */
.modal {
  position: fixed; inset: 0; z-index: 90;
  background: rgba(74,53,39,.45);
  display: grid; place-items: center; padding: 20px;
  animation: fadeIn .2s var(--ease-out);
}
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
.sheet {
  width: min(420px, 100%);
  background: var(--sugar); border-radius: 28px; padding: 26px 22px 18px;
  box-shadow: 0 20px 50px -12px rgba(74,53,39,.5);
  animation: sheetIn .32s var(--ease-spring);
}
@keyframes sheetIn { from { transform: scale(.85) translateY(20px); opacity: 0; } to { transform: none; opacity: 1; } }
.sheet-title { font-family: "Nunito"; font-weight: 900; font-size: 1.7rem; margin: 0 0 18px; text-align: center; }
.field { margin-bottom: 16px; }
.field-label { display: block; font-size: .74rem; font-weight: 700; letter-spacing: .1em; color: var(--cocoa-muted); margin-bottom: 8px; }
.seg { display: flex; gap: 6px; background: var(--sugar-deep); padding: 5px; border-radius: 14px; box-shadow: var(--shadow-inset); }
.seg-btn {
  flex: 1; padding: 10px; border-radius: 10px; font-weight: 700; font-size: .9rem;
  color: var(--cocoa-muted); transition: all .18s var(--ease-out); min-height: 44px;
}
.seg-btn.on { background: var(--grape); color: #fff; box-shadow: 0 3px 0 var(--grape-deep); }
.vol-row { display: flex; align-items: center; gap: 12px; background: var(--sugar-deep); padding: 11px 14px; border-radius: 14px; box-shadow: var(--shadow-inset); }
.vol-ic { width: 22px; height: 22px; flex: none; fill: none; stroke: var(--cocoa-muted); stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.vol-pct { font-size: .8rem; font-weight: 700; color: var(--cocoa-muted); min-width: 40px; text-align: right; font-variant-numeric: tabular-nums; }
#volSlider { -webkit-appearance: none; appearance: none; flex: 1; height: 6px; border-radius: 3px; background: rgba(120,84,54,.2); outline: none; }
#volSlider::-webkit-slider-thumb { -webkit-appearance: none; width: 22px; height: 22px; border-radius: 50%; background: var(--grape); box-shadow: 0 2px 0 var(--grape-deep); cursor: pointer; }
#volSlider::-moz-range-thumb { width: 22px; height: 22px; border: none; border-radius: 50%; background: var(--grape); box-shadow: 0 2px 0 var(--grape-deep); cursor: pointer; }

.stat-row, .over-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin: 18px 0; }
.over-stats { grid-template-columns: repeat(2, 1fr); }
.stat { background: var(--sugar-deep); border-radius: 14px; padding: 14px 8px; text-align: center; box-shadow: var(--shadow-inset); }
.stat b { display: block; font-family: "Nunito"; font-weight: 800; font-size: 1.5rem; }
.stat span { font-size: .68rem; color: var(--cocoa-muted); letter-spacing: .05em; }
.howto { margin: 10px 0 6px; font-size: .86rem; }
.howto summary { font-weight: 700; cursor: pointer; padding: 8px 0; color: var(--grape); }
.howto ul { margin: 6px 0 0; padding-left: 18px; color: var(--cocoa-muted); line-height: 1.6; }
.howto b { color: var(--cocoa); }
.sheet-actions { display: flex; gap: 10px; margin-top: 16px; }
.btn-primary, .btn-ghost {
  flex: 1; padding: 14px; border-radius: 14px; font-weight: 800; font-family: "Nunito"; font-size: 1rem; min-height: 48px;
  transition: transform .14s var(--ease-out), box-shadow .14s var(--ease-out);
}
.btn-primary { background: var(--grape); color: #fff; box-shadow: 0 4px 0 var(--grape-deep); }
.btn-primary:active { transform: translateY(4px); box-shadow: none; }
.btn-primary.big { width: 100%; padding: 16px; font-size: 1.15rem; margin-top: 8px; }
.btn-ghost { background: var(--sugar-deep); color: var(--cocoa); box-shadow: var(--shadow-inset); }
.sheet-close { display: block; width: 100%; margin-top: 12px; padding: 10px; color: var(--cocoa-muted); font-weight: 700; font-size: .88rem; }

.sheet--over { text-align: center; }
.over-score { font-family: "Nunito"; font-weight: 900; font-size: 4rem; line-height: 1; margin: 6px 0; color: var(--grape); }
.over-cap { font-size: .8rem; font-weight: 700; letter-spacing: .1em; color: var(--cocoa-muted); margin: 0 0 4px; }
.new-best { color: var(--gold); margin-left: 8px; }

/* ───────── focus + reduced motion + responsive ───────── */
:focus-visible { outline: 3px solid var(--grape); outline-offset: 2px; border-radius: 8px; }

@media (prefers-reduced-motion: reduce) {
  .app.is-shaking { animation: none; }
  .cell.will-clear { animation: none; box-shadow: inset 0 0 0 2px var(--gold); }
  .combo-popup { animation: comboStatic 0.7s linear forwards; }
  @keyframes comboStatic { 0% { opacity: 1; } 100% { opacity: 0; } }
  .cell.placed { animation: squashShort .2s var(--ease-out); }
  @keyframes squashShort { 0% { transform: scale(.8); } 100% { transform: scale(1); } }
  .score.pop, .act.reroll.ready, .board.hint-flash, .sheet { animation: none; }
}

@media (min-width: 768px) {
  .app { gap: 22px; padding-top: 32px; }
  .hold { width: 100px; }
}
