/* ============================================================
   Level 5 — Шкатулка с пуговицами
   A linen work surface, a wooden box with four felt wells,
   twelve buttons. Sorting by colour, by size or by holes is
   equally correct.
   ============================================================ */

/* --- the surface: woven linen -------------------------------
   Crossed repeating gradients give a warp and weft that reads
   as cloth without needing a bitmap.
   ----------------------------------------------------------- */
.scene {
  position: fixed;
  inset: 0;
  background-color: #cec4b5;
  background-image: repeating-linear-gradient(
      90deg,
      rgba(255, 252, 245, 0.09) 0 1px,
      rgba(58, 52, 46, 0.05) 1px 2px,
      transparent 2px 4px
    ),
    repeating-linear-gradient(
      0deg,
      rgba(255, 252, 245, 0.08) 0 1px,
      rgba(58, 52, 46, 0.045) 1px 2px,
      transparent 2px 4px
    ),
    radial-gradient(ellipse 86% 68% at 42% 30%, rgba(255, 250, 240, 0.55), transparent 72%);
}

.stage-wrap {
  position: fixed;
  inset: 0;
  z-index: 5;
}

.stage {
  position: absolute;
  top: 50%;
  left: 50%;
  transform-origin: center center;
}

/* ============================================================
   Props — desaturated so they never compete with the buttons
   ============================================================ */

.props {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.prop {
  position: absolute;
  filter: drop-shadow(0 4px 7px rgba(58, 52, 46, 0.16));
  opacity: 0.9;
}

.prop--spool {
  top: 22px;
  left: -46px;
  width: 150px;
  transform: rotate(-13deg);
}

.prop--scissors {
  right: -58px;
  bottom: 96px;
  width: 210px;
  transform: rotate(-18deg);
}

.prop--ribbon {
  bottom: -22px;
  left: -70px;
  width: 300px;
}

/* Portrait puts the loose buttons above and below the box, so the
   props move to the side margins the box leaves free. */
.stage.is-portrait .prop--spool {
  top: 560px;
  left: -62px;
}

.stage.is-portrait .prop--scissors {
  right: -78px;
  bottom: 620px;
}

.stage.is-portrait .prop--ribbon {
  bottom: 34px;
  left: -96px;
}

/* ============================================================
   The box
   ============================================================ */

.box {
  position: absolute;
  /* Set by the level from the current orientation's layout. */
  top: var(--box-y, 190px);
  left: var(--box-x, 310px);
  width: 580px;
  height: 490px;
  background: linear-gradient(166deg, #d3b483 0%, #c09f6c 44%, #a5854f 100%);
  border-radius: 10px;
  box-shadow: 0 2px 2px rgba(58, 52, 46, 0.12), 0 22px 40px -14px rgba(58, 52, 46, 0.34),
    inset 0 1px 0 rgba(255, 248, 232, 0.62);
}

/* A chamfer, so the box has thickness rather than being a slab. */
.box__lip {
  position: absolute;
  inset: 11px;
  border-radius: 7px;
  box-shadow: inset 0 3px 7px rgba(58, 52, 46, 0.3), inset 0 -1px 0 rgba(255, 248, 232, 0.3);
}

/* Wood grain: fine irregular striping plus paper tooth. */
.box::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background-image: repeating-linear-gradient(
      92deg,
      rgba(112, 78, 38, 0.13) 0 2px,
      transparent 2px 7px,
      rgba(112, 78, 38, 0.08) 7px 9px,
      transparent 9px 18px,
      rgba(112, 78, 38, 0.05) 18px 19px,
      transparent 19px 31px
    ),
    var(--noise-coarse);
  background-size: auto, 180px 180px;
  border-radius: 10px;
  opacity: 0.6;
  mix-blend-mode: multiply;
}

.box__wells {
  position: absolute;
  top: 28px;
  left: 28px;
  display: grid;
  grid-template-columns: 250px 250px;
  grid-template-rows: 205px 205px;
  gap: 24px;
}

/* --- a felt-lined well ------------------------------------- */
.well {
  position: relative;
  background: radial-gradient(ellipse 130% 120% at 48% 18%, #74814e 0%, #616e42 58%, #505c36 100%);
  border-radius: var(--r-tray);
  box-shadow: inset 0 4px 10px rgba(28, 32, 16, 0.52), inset 0 -1px 0 rgba(255, 248, 232, 0.1),
    0 1px 0 rgba(255, 248, 232, 0.36);
}

/* Felt nap: dense fine noise, only visible up close. */
.well::before {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background-image: var(--noise-fine);
  background-size: 84px 84px;
  border-radius: var(--r-tray);
  opacity: 0.26;
  mix-blend-mode: overlay;
}

/* A well holding its full three buttons gets a warm rim — the
   only progress feedback the level needs. */
.well::after {
  content: "";
  position: absolute;
  inset: -3px;
  pointer-events: none;
  border: 2px solid rgba(212, 165, 60, 0);
  border-radius: 9px;
  transition: border-color var(--dur-snap) var(--ease-soft);
}

.well.is-full::after {
  border-color: rgba(212, 165, 60, 0.6);
}

/* ============================================================
   Buttons
   ============================================================ */

.pieces {
  position: absolute;
  inset: 0;
}

.btn-piece {
  position: absolute;
  /* Stacking is done with z-index rather than DOM order: reordering
     the children to raise the held button would re-parent it, and
     re-parenting a focused element blurs it, which silently breaks
     keyboard play. .is-held in ui.css supplies the raised value. */
  z-index: 2;
  top: 0;
  left: 0;
  display: grid;
  place-items: center;
  /* --bx/--by are the virtual-stage position of the centre.
     Position is expressed only through transform, never through
     left/top, so dragging never triggers layout. */
  transform: translate(calc(var(--bx) * 1px - 50%), calc(var(--by) * 1px - 50%))
    rotate(var(--tilt, 0deg));
}

.btn-piece:focus-visible {
  outline: none;
}

.btn-piece:focus-visible .btn-piece__disc {
  box-shadow: var(--shadow-snap);
}

.btn-piece.is-animated {
  transition: transform var(--dur-drop) var(--ease-snap);
}

/* Compound grab: scale AND lift AND tilt. Scale alone feels like
   a button press; the tilt is what sells "held in a hand". */
.btn-piece.is-held {
  transform: translate(calc(var(--bx) * 1px - 50%), calc(var(--by) * 1px - 50%))
    rotate(calc(var(--tilt, 0deg) - 1.6deg)) scale(1.055);
  transition: none;
}

.btn-piece.is-locked {
  pointer-events: none;
}

.btn-piece__disc {
  position: relative;
  display: grid;
  place-items: center;
  width: calc(var(--d) * 1px);
  height: calc(var(--d) * 1px);
  border-radius: 50%;
  box-shadow: var(--shadow-rest);
  transition: box-shadow var(--dur-lift) var(--ease-soft);
}

.btn-piece:hover .btn-piece__disc {
  box-shadow: var(--shadow-hover);
}

.btn-piece.is-held .btn-piece__disc {
  box-shadow: var(--shadow-lift);
}

.btn-piece svg {
  display: block;
  width: 100%;
  height: 100%;
  overflow: visible;
}

/* Per-object grain, clipped to the disc, so the texture travels
   with the button instead of sitting over the frame. */
.btn-piece__disc::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background-image: var(--noise-coarse);
  background-size: 128px 128px;
  border-radius: 50%;
  opacity: 0.14;
  mix-blend-mode: multiply;
}

@keyframes piece-settle {
  0% {
    transform: scale(1.05);
  }
  55% {
    transform: scale(0.985);
  }
  100% {
    transform: scale(1);
  }
}

.btn-piece.is-settling .btn-piece__disc {
  animation: piece-settle var(--dur-snap) var(--ease-snap);
}

/* Solved: every button snaps true and lifts in a stagger. The
   contrast between the scattered tilt and the aligned finish is
   where the satisfaction lives. */
@keyframes piece-solved {
  0% {
    transform: translateY(0);
  }
  40% {
    transform: translateY(-8px);
  }
  100% {
    transform: translateY(0);
  }
}

.is-solved .btn-piece {
  transform: translate(calc(var(--bx) * 1px - 50%), calc(var(--by) * 1px - 50%)) rotate(0deg);
}

.is-solved .btn-piece__disc {
  animation: piece-solved var(--dur-reward) var(--ease-snap) both;
  animation-delay: calc(var(--i, 0) * 42ms);
}
