/* ═══════════════════════════════════════════════════════════════════════════
   tile.css — Tile appearance, 3D effect, states, animations, tile sets
   ═══════════════════════════════════════════════════════════════════════════ */

/* ── Tile sizing variables (overridden by media query for mobile) ── */
:root {
  --tile-w: 56px;
  --tile-h: 68px;
  --tile-radius: 6px;
  --tile-depth: 5px;       /* how many px of staircase shadow = 3D depth */
  --layer-offset-x: 5px;   /* px shift right per layer */
  --layer-offset-y: -5px;  /* px shift up per layer */
}

/* ── Base tile ── */
.tile {
  position: absolute;
  width: var(--tile-w);
  height: var(--tile-h);
  border-radius: var(--tile-radius);
  background: var(--tile-bg);
  --tile-frame-current: var(--tile-border);
  --tile-frame-inner-current: var(--tile-face-border-inner);
  --tile-frame-highlight-current: var(--tile-face-highlight);
  --tile-frame-shadow-current: var(--tile-face-shadow);
  --tile-side-border-current: var(--tile-side-border);
  --tile-side-highlight-current: var(--tile-side-highlight);
  border: 1px solid transparent;
  box-shadow:
    8px 12px 20px rgba(0, 0, 0, 0.26),
    3px 4px 9px rgba(0, 0, 0, 0.16);
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
  display: flex;
  align-items: center;
  justify-content: center;
  transition:
    transform 0.12s cubic-bezier(0.34, 1.56, 0.64, 1),
    box-shadow 0.12s ease,
    opacity 0.28s ease,
    filter 0.15s ease;
  will-change: transform, opacity;
  overflow: visible;
  isolation: isolate;
}

.tile::before {
  content: '';
  position: absolute;
  top: 4px;
  left: 4px;
  right: calc(-1 * var(--tile-depth));
  bottom: calc(-1 * var(--tile-depth));
  border-radius: calc(var(--tile-radius) + 1px);
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.14) 0 22%, transparent 22% 100%),
    linear-gradient(145deg, var(--tile-shadow-1) 0%, var(--tile-shadow-2) 52%, var(--tile-shadow-3) 100%);
  border: 1px solid var(--tile-side-border-current);
  box-shadow:
    inset 0 1px 0 var(--tile-side-highlight-current),
    inset -1px -2px 0 rgba(0, 0, 0, 0.14);
  pointer-events: none;
  z-index: -2;
}

.tile::after {
  content: '';
  position: absolute;
  inset: 1px;
  border-radius: calc(var(--tile-radius) - 1px);
  background:
    linear-gradient(180deg,
      var(--tile-face-sheen) 0%,
      rgba(255, 255, 255, 0.08) 24%,
      rgba(255, 255, 255, 0) 42%,
      rgba(120, 92, 52, 0.08) 100%);
  border: 1px solid var(--tile-frame-current);
  box-shadow:
    inset 0 0 0 1px var(--tile-frame-inner-current),
    inset 0 1px 0 var(--tile-frame-highlight-current),
    inset 0 -2px 3px var(--tile-frame-shadow-current);
  pointer-events: none;
  z-index: 1;
}

/* ── Free tile — slightly brighter, full cursor ── */
body.show-free .tile.free {
  background: var(--tile-bg-free);
  --tile-frame-current: var(--tile-border-free);
}

/* ── Blocked (not free) — dimmer, no-pointer cursor ── */
body.show-free .tile.blocked {
  filter: brightness(0.78) saturate(0.7);
  cursor: not-allowed;
}

/* ── Hover on free tiles ── */
.tile.free:hover {
  transform: translateY(-2px) scale(1.02);
  box-shadow:
    10px 16px 26px rgba(0, 0, 0, 0.3),
    4px 6px 12px rgba(0, 0, 0, 0.18);
}

/* ── Selected tile ── */
.tile.selected {
  background: var(--tile-bg-selected);
  --tile-frame-current: rgba(255, 210, 0, 0.95);
  --tile-frame-inner-current: rgba(255, 248, 160, 0.9);
  --tile-frame-highlight-current: rgba(255, 255, 200, 0.95);
  --tile-frame-shadow-current: rgba(140, 100, 0, 0.6);
  transform: translateY(-6px) scale(1.1);
  outline: 4px solid #ffd700;
  outline-offset: 2px;
  box-shadow:
    0 0 0 4px rgba(255, 215, 0, 1),
    0 0 14px 4px rgba(255, 215, 0, 0.75),
    0 0 32px 8px rgba(255, 200, 0, 0.45),
    14px 20px 30px rgba(0, 0, 0, 0.4);
  filter: brightness(1.12);
  z-index: 9999 !important;
}

/* ── Metal tile set: steel-blue selection ── */
.tiles-metal .tile.selected {
  --tile-frame-current: rgba(160, 200, 230, 0.78);
  --tile-frame-inner-current: rgba(226, 242, 252, 0.52);
  --tile-frame-highlight-current: rgba(245, 250, 255, 0.62);
  --tile-frame-shadow-current: rgba(74, 104, 126, 0.44);
  box-shadow:
    0 0 0 2px rgba(140, 190, 220, 0.55),
    0 0 16px rgba(140, 190, 220, 0.4),
    12px 18px 28px rgba(0, 0, 0, 0.38);
}

/* ── Wood tile set: warm-brown selection ── */
.tiles-wood .tile.selected {
  --tile-frame-current: rgba(190, 120, 50, 0.82);
  --tile-frame-inner-current: rgba(255, 220, 180, 0.34);
  --tile-frame-highlight-current: rgba(255, 232, 206, 0.44);
  --tile-frame-shadow-current: rgba(112, 64, 18, 0.46);
  box-shadow:
    0 0 0 2px rgba(190, 120, 50, 0.5),
    0 0 16px rgba(190, 120, 50, 0.38),
    12px 18px 28px rgba(0, 0, 0, 0.38);
}

/* ── Fruits tile set: fresh green selection ── */
.tiles-fruits .tile.selected {
  --tile-frame-current: rgba(80, 200, 80, 0.84);
  --tile-frame-inner-current: rgba(220, 255, 220, 0.52);
  --tile-frame-highlight-current: rgba(240, 255, 240, 0.72);
  --tile-frame-shadow-current: rgba(30, 110, 30, 0.44);
  box-shadow:
    0 0 0 2px rgba(80, 200, 80, 0.5),
    0 0 16px rgba(80, 200, 80, 0.4),
    12px 18px 28px rgba(0, 0, 0, 0.34);
}

/* ── Animals tile set: warm amber selection ── */
.tiles-animals .tile.selected {
  --tile-frame-current: rgba(255, 160, 40, 0.86);
  --tile-frame-inner-current: rgba(255, 240, 200, 0.55);
  --tile-frame-highlight-current: rgba(255, 252, 235, 0.80);
  --tile-frame-shadow-current: rgba(160, 80, 10, 0.44);
  box-shadow:
    0 0 0 2px rgba(255, 160, 40, 0.55),
    0 0 16px rgba(255, 160, 40, 0.42),
    12px 18px 28px rgba(0, 0, 0, 0.34);
}

/* ── Cartoonish tile set: bright selection ── */
.tiles-cartoonish .tile.selected {
  --tile-frame-current: rgba(255, 180, 0, 0.86);
  --tile-frame-inner-current: rgba(255, 248, 205, 0.58);
  --tile-frame-highlight-current: rgba(255, 253, 230, 0.82);
  --tile-frame-shadow-current: rgba(180, 104, 12, 0.42);
  box-shadow:
    0 0 0 2px rgba(255, 180, 0, 0.55),
    0 0 16px rgba(255, 180, 0, 0.45),
    12px 18px 28px rgba(0, 0, 0, 0.34);
}

/* ── Hint tiles ── */
.tile.hint {
  background: var(--tile-bg-hint);
  --tile-frame-current: rgba(188, 255, 88, 0.96);
  --tile-frame-inner-current: rgba(253, 255, 232, 0.9);
  --tile-frame-highlight-current: rgba(255, 255, 255, 0.95);
  --tile-frame-shadow-current: rgba(96, 140, 26, 0.5);
  filter: brightness(1.08) saturate(1.22);
  outline: 3px solid rgba(205, 255, 120, 0.95);
  outline-offset: 2px;
  animation: hint-pulse 0.9s cubic-bezier(0.34, 1.56, 0.64, 1) infinite alternate;
  transform-origin: center;
  z-index: 9998 !important;
}

.tile.hint::before {
  border-color: rgba(126, 186, 42, 0.95);
  box-shadow:
    inset 0 1px 0 rgba(246, 255, 214, 0.9),
    inset -1px -2px 0 rgba(78, 110, 20, 0.26);
}

.tile.hint::after {
  box-shadow:
    inset 0 0 0 2px rgba(226, 255, 166, 0.96),
    inset 0 0 18px rgba(186, 255, 72, 0.34),
    inset 0 1px 0 var(--tile-frame-highlight-current),
    inset 0 -2px 3px var(--tile-frame-shadow-current);
}

.tile.hint .tile-face {
  box-shadow:
    inset 0 0 0 2px rgba(233, 255, 181, 0.92),
    inset 0 0 24px rgba(153, 255, 72, 0.22);
  filter: brightness(1.16) saturate(1.28) contrast(1.06);
}

@keyframes hint-pulse {
  from {
    transform: translateY(-2px) scale(1.04);
    box-shadow:
      0 0 0 3px rgba(188, 255, 88, 0.45),
      0 0 18px rgba(188, 255, 88, 0.42),
      8px 12px 20px rgba(0, 0, 0, 0.26);
  }
  to {
    transform: translateY(-6px) scale(1.09);
    box-shadow:
      0 0 0 4px rgba(214, 255, 126, 0.95),
      0 0 30px rgba(188, 255, 88, 0.82),
      0 0 54px rgba(134, 255, 72, 0.46),
      14px 20px 30px rgba(0, 0, 0, 0.32);
  }
}

/* ── Removing (match animation) ── */
.tile.removing {
  transform: scale(0.7) translateY(-6px) !important;
  opacity: 0 !important;
  pointer-events: none;
}

/* ── Shake on invalid select ── */
.tile.shake {
  animation: tile-shake 0.35s ease;
}
@keyframes tile-shake {
  0%,100% { transform: translateX(0); }
  20%      { transform: translateX(-5px) rotate(-2deg); }
  40%      { transform: translateX(5px) rotate(2deg); }
  60%      { transform: translateX(-4px); }
  80%      { transform: translateX(4px); }
}

/* ── Shuffle animation ── */
.tile.shuffle-out {
  transform: scale(0.85);
  opacity: 0;
  transition: transform 0.18s ease, opacity 0.18s ease !important;
}
.tile.shuffle-in {
  animation: shuffle-pop 0.3s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}
@keyframes shuffle-pop {
  from { transform: scale(0.85); opacity: 0; }
  to   { transform: scale(1); opacity: 1; }
}

/* ── Tile face (the symbol/number OR sprite image) ── */
.tile-face {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  padding: 5px;
  font-weight: 800;
  font-size: 1.1rem;
  line-height: 1;
  color: var(--tile-text);
  pointer-events: none;
  user-select: none;
  border-radius: calc(var(--tile-radius) - 1px);
  position: relative;
  overflow: hidden;
  z-index: 0;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.24);
}

/* When using image sprite, fill the face div with no padding */
.tile-face[style*="background-image"] {
  padding: 0;
}
/* Hide the color dot when using image sprite */
.tile-face[style*="background-image"]::before {
  display: none;
}

/* ── Tile Set: Classic Numbers ── */
.tiles-numbers .tile-face {
  font-size: 1rem;
  flex-direction: column;
  gap: 1px;
}
.tiles-numbers .tile-face .face-top {
  font-size: 0.65rem;
  opacity: 0.5;
  line-height: 1;
  align-self: flex-start;
  padding-left: 3px;
}
.tiles-numbers .tile-face .face-main {
  font-size: 1.15rem;
  font-weight: 900;
}

/* ── Tile Set: Symbols ── */
.tiles-symbols .tile-face {
  font-size: 1.4rem;
}

/* ── Tile Set: Minimal ── */
.tiles-minimal .tile-face {
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  flex-direction: column;
  gap: 0;
}
.tiles-minimal .tile-face .face-top {
  font-size: 0.6rem;
  opacity: 0.4;
  line-height: 1;
  align-self: flex-start;
  padding-left: 3px;
}

/* ── Face colors (applied to .tile-face via JS data-face attribute) ── */
/* These are applied inline via style tag injection from themes.css face vars */

/* ── Tile face color dot (small colored indicator on tile) ── */
.tile-face::before {
  content: '';
  position: absolute;
  top: 4px;
  right: 4px;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--face-color, #666);
  opacity: 0.7;
}

/* ── Responsive tile sizes ── */
@media (max-width: 600px) {
  :root {
    --tile-w: 42px;
    --tile-h: 52px;
    --tile-radius: 5px;
    --tile-depth: 4px;
    --layer-offset-x: 4px;
    --layer-offset-y: -4px;
  }
  .tile {
    box-shadow:
      6px 10px 16px rgba(0, 0, 0, 0.24),
      2px 3px 7px rgba(0, 0, 0, 0.16);
  }
  .tile::before {
    top: 3px;
    left: 3px;
  }
  .tile-face { font-size: 0.8rem; }
  .tiles-symbols .tile-face { font-size: 1rem; }
}

@media (max-width: 380px) {
  :root {
    --tile-w: 36px;
    --tile-h: 44px;
    --tile-radius: 4px;
    --layer-offset-x: 3px;
    --layer-offset-y: -3px;
  }
  .tile-face { font-size: 0.7rem; }
  .tile-face::before { width: 5px; height: 5px; top: 3px; right: 3px; }
}

/* ── Board enter animation ── */
.tile.board-enter {
  animation: tile-enter 0.3s ease forwards;
}
@keyframes tile-enter {
  from { transform: scale(0.5) translateY(-10px); opacity: 0; }
  to   { transform: scale(1) translateY(0); opacity: 1; }
}
