/* ==========================================================================
   Bloxxy — cozy wooden block puzzle
   All texture is CSS-only (gradients): no image assets, no layout shift.
   ========================================================================== */

/* ===== Theme variables — edit these to re-skin the whole game ===== */
:root {
  /* Layout */
  --board-size: min(92vw, 56vh, 440px); /* board stays square & on-screen */
  --grid-n: 10;                          /* visual only; logic lives in game.js CONFIG */
  --cell-gap: 4px;
  --cell-radius: 4px;
  --tray-cell: calc(var(--board-size) / 18);

  /* Palette: deep workshop + sanded maple */
  --c-bg: #241507;
  --c-bg-glow: #3d2410;
  --c-frame: #4a2e17;
  --c-frame-edge: #2c1a0b;
  --c-board: #e7d3a8;
  --c-cell-empty: #d9bf8c;
  --c-cell-line: #c4a873;
  --c-ink: #3a2513;
  --c-cream: #f4e7c8;
  --c-cream-dim: #c9b08a;
  --c-accent: #e0993f;
  --c-danger: #b8442c;

  /* Wood tones for pieces (1–5). Add more in game.js CONFIG.woodTones */
  --wood-1: #dd9c43;  --wood-1-hi: #f0bd6e;  /* honey oak  */
  --wood-2: #bc5f37;  --wood-2-hi: #d68457;  /* cherry     */
  --wood-3: #8a5a2c;  --wood-3-hi: #a97a45;  /* walnut     */
  --wood-4: #a8814e;  --wood-4-hi: #c5a06b;  /* chestnut   */
  --wood-5: #756337;  --wood-5-hi: #948253;  /* smoked ash */

  --font-display: "Fraunces", Georgia, "Times New Roman", serif;
  --shadow-soft: 0 10px 30px rgba(0, 0, 0, 0.45);
}

/* ===== Base ===== */
* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  font-family: var(--font-display);
  color: var(--c-cream);
  background-color: var(--c-bg);
  /* Layered CSS "wood grain": vignette + warm glow + fine diagonal grain */
  background-image:
    radial-gradient(120% 90% at 50% 0%, var(--c-bg-glow) 0%, transparent 60%),
    repeating-linear-gradient(94deg,
      rgba(255, 220, 170, 0.028) 0px, rgba(255, 220, 170, 0.028) 2px,
      transparent 2px, transparent 7px),
    repeating-linear-gradient(88deg,
      rgba(0, 0, 0, 0.10) 0px, rgba(0, 0, 0, 0.10) 1px,
      transparent 1px, transparent 11px);
  overscroll-behavior: none;       /* stop pull-to-refresh while dragging */
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.visually-hidden {
  position: absolute;
  width: 1px; height: 1px;
  margin: -1px; padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

/* ===== App shell ===== */
.app {
  min-height: 100dvh;
  max-width: 560px;
  margin: 0 auto;
  padding: clamp(8px, 2vh, 20px) 12px calc(16px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(8px, 1.6vh, 18px);
}

/* ===== Header ===== */
.topbar {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.title {
  margin: 0;
  font-size: clamp(1.7rem, 6vw, 2.4rem);
  font-weight: 700;
  letter-spacing: 0.02em;
  color: var(--c-cream);
  /* carved-into-wood look: dark cut below, warm light above */
  text-shadow:
    0 2px 0 rgba(0, 0, 0, 0.55),
    0 -1px 0 rgba(255, 226, 170, 0.25);
}

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

.score-chip {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 64px;
  padding: 4px 12px 6px;
  border-radius: 10px;
  background: linear-gradient(180deg, #57371c, #41280f);
  box-shadow:
    inset 0 1px 0 rgba(255, 220, 170, 0.18),
    inset 0 -2px 4px rgba(0, 0, 0, 0.35),
    0 2px 6px rgba(0, 0, 0, 0.4);
}

.score-label {
  font-size: 0.62rem;
  text-transform: uppercase;
  letter-spacing: 0.14em;
  color: var(--c-cream-dim);
}

.score-value {
  font-size: 1.25rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  line-height: 1.15;
}

.score-value.bump { animation: score-bump 0.35s ease; }
@keyframes score-bump {
  40% { transform: scale(1.3); color: var(--c-accent); }
}

/* ===== Buttons ===== */
.icon-btn {
  width: 42px;
  height: 42px;
  padding: 8px;
  border: 0;
  border-radius: 10px;
  cursor: pointer;
  color: var(--c-cream);
  background: linear-gradient(180deg, #57371c, #41280f);
  box-shadow:
    inset 0 1px 0 rgba(255, 220, 170, 0.18),
    0 2px 6px rgba(0, 0, 0, 0.4);
}
.icon-btn:active { transform: translateY(1px); }
.icon-btn svg { width: 100%; height: 100%; fill: currentColor; stroke: currentColor; }
.icon-btn .icon-sound-off { display: none; }
.icon-btn[aria-pressed="true"] .icon-sound-on { display: none; }
.icon-btn[aria-pressed="true"] .icon-sound-off { display: block; }

.wood-btn {
  font-family: inherit;
  font-size: 1rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  color: #2f1c0a;
  padding: 10px 30px;
  border: 0;
  border-radius: 12px;
  cursor: pointer;
  background:
    repeating-linear-gradient(92deg,
      rgba(120, 70, 20, 0.10) 0 3px, transparent 3px 8px),
    linear-gradient(180deg, var(--wood-1-hi), var(--wood-1));
  box-shadow:
    inset 0 1px 0 rgba(255, 240, 200, 0.6),
    inset 0 -3px 0 rgba(90, 50, 10, 0.45),
    0 3px 8px rgba(0, 0, 0, 0.45);
  transition: transform 0.08s ease, box-shadow 0.08s ease;
}
.wood-btn:hover { filter: brightness(1.05); }
.wood-btn:active {
  transform: translateY(2px);
  box-shadow:
    inset 0 1px 0 rgba(255, 240, 200, 0.6),
    inset 0 -1px 0 rgba(90, 50, 10, 0.45),
    0 1px 3px rgba(0, 0, 0, 0.45);
}

.icon-btn:focus-visible,
.wood-btn:focus-visible,
.piece:focus-visible {
  outline: 3px solid var(--c-accent);
  outline-offset: 2px;
}

/* ===== Game area ===== */
.game-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(8px, 1.6vh, 16px);
  width: 100%;
}

/* Dark walnut frame around the board */
.board-frame {
  padding: clamp(8px, 1.8vw, 14px);
  border-radius: 18px;
  background:
    repeating-linear-gradient(178deg,
      rgba(0, 0, 0, 0.12) 0 2px, transparent 2px 9px),
    linear-gradient(180deg, var(--c-frame), var(--c-frame-edge));
  box-shadow:
    inset 0 1px 0 rgba(255, 220, 170, 0.15),
    var(--shadow-soft);
}

.board-wrap { position: relative; }

.board {
  display: grid;
  grid-template-columns: repeat(var(--grid-n), 1fr);
  gap: var(--cell-gap);
  width: var(--board-size);
  height: var(--board-size);
  padding: var(--cell-gap);
  border-radius: 10px;
  background:
    repeating-linear-gradient(91deg,
      rgba(140, 100, 50, 0.08) 0 2px, transparent 2px 10px),
    linear-gradient(180deg, var(--c-board), #ddc489);
  box-shadow: inset 0 2px 10px rgba(70, 40, 10, 0.45);
  touch-action: none; /* the board is a drag target — never scroll from it */
}

/* ===== Cells ===== */
.cell {
  border-radius: var(--cell-radius);
  background: var(--c-cell-empty);
  box-shadow:
    inset 0 1px 3px rgba(90, 55, 15, 0.4),
    inset 0 -1px 0 rgba(255, 240, 200, 0.35);
}

/* Filled cells: wood tone + sheen + fine grain stripes */
.cell.filled,
.mini-cell,
.cell.clearing {
  background-image:
    linear-gradient(180deg, rgba(255, 245, 215, 0.35) 0%, transparent 45%),
    repeating-linear-gradient(96deg,
      rgba(60, 30, 5, 0.12) 0 2px, transparent 2px 7px),
    linear-gradient(180deg, var(--tone-hi), var(--tone));
  box-shadow:
    inset 0 1px 0 rgba(255, 245, 215, 0.5),
    inset 0 -2px 0 rgba(40, 20, 0, 0.3),
    0 1px 2px rgba(50, 25, 0, 0.35);
}

.wood-1 { --tone: var(--wood-1); --tone-hi: var(--wood-1-hi); }
.wood-2 { --tone: var(--wood-2); --tone-hi: var(--wood-2-hi); }
.wood-3 { --tone: var(--wood-3); --tone-hi: var(--wood-3-hi); }
.wood-4 { --tone: var(--wood-4); --tone-hi: var(--wood-4-hi); }
.wood-5 { --tone: var(--wood-5); --tone-hi: var(--wood-5-hi); }

/* Placement pop */
.cell.pop { animation: cell-pop 0.22s ease-out; }
@keyframes cell-pop {
  0% { transform: scale(0.55); }
  70% { transform: scale(1.08); }
  100% { transform: scale(1); }
}

/* Line-clear: brighten then shrink away */
.cell.clearing { animation: cell-clear 0.32s ease-in forwards; }
@keyframes cell-clear {
  0% { transform: scale(1); filter: brightness(1.6); }
  100% { transform: scale(0.1) rotate(8deg); opacity: 0; filter: brightness(2); }
}

/* Ghost previews while dragging / placing */
.cell.ghost-valid {
  background-image:
    linear-gradient(180deg, rgba(255, 245, 215, 0.35) 0%, transparent 45%),
    linear-gradient(180deg, var(--tone-hi), var(--tone));
  opacity: 0.55;
  box-shadow: inset 0 0 0 2px rgba(255, 250, 230, 0.7);
}
.cell.ghost-invalid {
  background: color-mix(in srgb, var(--c-danger) 45%, var(--c-cell-empty));
  box-shadow: inset 0 0 0 2px var(--c-danger);
}
/* Fallback for browsers without color-mix */
@supports not (background: color-mix(in srgb, red 50%, blue)) {
  .cell.ghost-invalid { background: #c97f63; }
}

/* Rows/columns that would complete glow softly during preview */
.cell.would-clear { box-shadow: inset 0 0 0 2px var(--c-accent), inset 0 1px 3px rgba(90, 55, 15, 0.4); }

/* ===== Tray ===== */
.tray {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  width: var(--board-size);
  min-height: calc(var(--tray-cell) * 5 + 24px); /* reserve space: no layout shift */
}

.piece {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 8px;
  border-radius: 12px;
  cursor: grab;
  background: rgba(255, 220, 170, 0.05);
  box-shadow: inset 0 0 0 1px rgba(255, 220, 170, 0.12);
  touch-action: none; /* pieces are drag handles */
  transition: transform 0.12s ease, box-shadow 0.12s ease, opacity 0.2s ease;
}
.piece:hover { transform: translateY(-2px); }
.piece.selected {
  box-shadow: inset 0 0 0 2px var(--c-accent), 0 0 14px rgba(224, 153, 63, 0.35);
  transform: translateY(-3px);
}
.piece.drag-source { opacity: 0.25; }
.piece.unplaceable { opacity: 0.35; filter: grayscale(0.6); }
.piece-slot-empty { border-radius: 12px; } /* keeps the 3-column rhythm */

.piece-grid {
  display: grid;
  gap: 2px;
  pointer-events: none;
}
.piece-grid .mini-cell {
  width: var(--tray-cell);
  height: var(--tray-cell);
  border-radius: 3px;
}
.piece-grid .mini-gap { width: var(--tray-cell); height: var(--tray-cell); }

/* New tray set slides in */
.piece.deal-in { animation: deal-in 0.3s ease backwards; }
@keyframes deal-in {
  from { transform: translateY(18px); opacity: 0; }
}

/* ===== Drag clone (follows the pointer) ===== */
#dragLayer {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 50;
}
.drag-clone {
  position: absolute;
  filter: drop-shadow(0 12px 14px rgba(0, 0, 0, 0.45));
}
.drag-clone .mini-cell {
  position: absolute;
  border-radius: var(--cell-radius);
}

/* ===== Floating score text & combo banner ===== */
.float-text {
  position: absolute;
  z-index: 10;
  transform: translate(-50%, -50%);
  font-weight: 700;
  font-size: 1.15rem;
  color: var(--c-ink);
  text-shadow: 0 1px 0 rgba(255, 245, 215, 0.8);
  pointer-events: none;
  animation: float-up 0.9s ease-out forwards;
}
.float-text.big { font-size: 1.5rem; color: #8a3d12; }
@keyframes float-up {
  0% { opacity: 0; translate: 0 6px; }
  15% { opacity: 1; }
  100% { opacity: 0; translate: 0 -44px; }
}

.combo-msg {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(1.4rem, 7vw, 2.2rem);
  font-weight: 700;
  color: var(--c-cream);
  text-shadow: 0 2px 0 rgba(0, 0, 0, 0.6), 0 0 24px rgba(224, 153, 63, 0.8);
  opacity: 0;
  pointer-events: none;
  z-index: 11;
}
.combo-msg.show { animation: combo-pop 1.1s ease forwards; }
@keyframes combo-pop {
  0% { opacity: 0; transform: scale(0.6); }
  20% { opacity: 1; transform: scale(1.1); }
  35% { transform: scale(1); }
  80% { opacity: 1; }
  100% { opacity: 0; transform: scale(1.05); }
}

.hint {
  margin: 0;
  font-size: 0.78rem;
  color: var(--c-cream-dim);
  text-align: center;
}

/* ===== Game-over overlay ===== */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(18, 9, 2, 0.78);
  backdrop-filter: blur(3px);
  animation: overlay-in 0.35s ease;
}
.overlay.hidden { display: none; }
@keyframes overlay-in { from { opacity: 0; } }

.overlay-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  width: min(92vw, 340px);
  padding: 28px 24px 26px;
  border-radius: 20px;
  text-align: center;
  background:
    repeating-linear-gradient(93deg,
      rgba(0, 0, 0, 0.08) 0 2px, transparent 2px 9px),
    linear-gradient(180deg, #5b3a1d, #3c240e);
  box-shadow:
    inset 0 1px 0 rgba(255, 220, 170, 0.2),
    0 20px 60px rgba(0, 0, 0, 0.6);
  animation: card-in 0.4s cubic-bezier(0.2, 1.4, 0.4, 1);
}
@keyframes card-in {
  from { transform: translateY(40px) scale(0.92); opacity: 0; }
}

.overlay-card h2 {
  margin: 0;
  font-size: 1.7rem;
  text-shadow: 0 2px 0 rgba(0, 0, 0, 0.5);
}

.new-best-tag {
  margin: -6px 0 0;
  font-weight: 700;
  color: var(--c-accent);
  animation: score-bump 0.6s ease 0.3s backwards;
}
.new-best-tag.hidden { display: none; }

.overlay-scores {
  display: flex;
  gap: 28px;
}
.overlay-scores > div { display: flex; flex-direction: column; gap: 2px; }
.overlay-score {
  font-size: 2rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

/* Board shake on game over */
.board-frame.shake { animation: shake 0.5s ease; }
@keyframes shake {
  20% { transform: translateX(-5px) rotate(-0.5deg); }
  40% { transform: translateX(5px) rotate(0.5deg); }
  60% { transform: translateX(-3px); }
  80% { transform: translateX(3px); }
}

/* ===== Responsive tweaks ===== */
@media (min-width: 480px) {
  :root { --tray-cell: calc(var(--board-size) / 20); }
}

/* ===== Reduced motion ===== */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
