:root {
  color-scheme: light;
  --ink: #17211b;
  --muted: #637069;
  --paper: #f7f3ea;
  --paper-strong: #fffaf0;
  --felt: #254c3f;
  --felt-deep: #17352d;
  --teal: #2e9486;
  --coral: #e66d56;
  --gold: #d9a84f;
  --line: rgba(24, 35, 29, 0.18);
  --shadow: 0 18px 36px rgba(21, 30, 25, 0.22);
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  color: var(--ink);
  font-family:
    Inter,
    ui-sans-serif,
    system-ui,
    -apple-system,
    BlinkMacSystemFont,
    "Segoe UI",
    sans-serif;
  background:
    linear-gradient(115deg, rgba(255, 250, 240, 0.72), rgba(233, 244, 239, 0.66)),
    repeating-linear-gradient(0deg, rgba(255, 255, 255, 0.16) 0 1px, transparent 1px 6px),
    #d9e2dc;
}

button,
select,
input {
  font: inherit;
}

.app {
  width: min(1420px, 100%);
  min-height: 100vh;
  margin: 0 auto;
  padding: 22px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  margin-bottom: 14px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-mark {
  width: 38px;
  height: 38px;
  border: 2px solid rgba(23, 33, 27, 0.26);
  border-radius: 8px;
  background:
    radial-gradient(circle at 71% 28%, var(--coral) 0 10px, transparent 11px),
    radial-gradient(circle at 31% 70%, var(--teal) 0 10px, transparent 11px),
    linear-gradient(135deg, var(--paper-strong), #e7d39a);
  box-shadow: 0 8px 16px rgba(28, 42, 35, 0.14);
}

.brand h1 {
  margin: 0;
  font-size: clamp(1.35rem, 3vw, 2rem);
  line-height: 1.05;
  letter-spacing: 0;
}

.brand p {
  margin: 5px 0 0;
  color: var(--muted);
  font-size: 0.92rem;
}

.stats {
  display: flex;
  align-items: stretch;
  gap: 8px;
}

.stat {
  min-width: 92px;
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 250, 240, 0.72);
  box-shadow: 0 8px 20px rgba(24, 35, 29, 0.08);
}

.stat span {
  display: block;
  color: var(--muted);
  font-size: 0.76rem;
}

.stat strong {
  display: block;
  margin-top: 2px;
  font-size: 1.1rem;
  font-variant-numeric: tabular-nums;
}

.controls {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 14px;
}

.button,
.toggle,
.select-label {
  min-height: 40px;
  border: 1px solid rgba(23, 33, 27, 0.18);
  border-radius: 8px;
  color: var(--ink);
  background: rgba(255, 250, 240, 0.88);
  box-shadow: 0 8px 18px rgba(24, 35, 29, 0.08);
}

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 15px;
  cursor: pointer;
}

.button:hover,
.toggle:hover,
.select-label:hover {
  border-color: rgba(46, 148, 134, 0.48);
}

.button:active {
  transform: translateY(1px);
}

.button:disabled {
  color: rgba(23, 33, 27, 0.38);
  cursor: not-allowed;
  background: rgba(255, 250, 240, 0.48);
  box-shadow: none;
}

.button[aria-pressed="true"] {
  color: #fffaf0;
  background: var(--teal);
  border-color: rgba(23, 33, 27, 0.16);
}

.toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 0 12px;
  cursor: pointer;
  user-select: none;
}

.toggle input {
  position: absolute;
  inline-size: 1px;
  block-size: 1px;
  opacity: 0;
}

.toggle span {
  position: relative;
  width: 34px;
  height: 20px;
  border-radius: 999px;
  background: #9da8a1;
  transition:
    background 140ms ease,
    box-shadow 140ms ease;
}

.toggle span::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #fffaf0;
  box-shadow: 0 2px 6px rgba(24, 35, 29, 0.24);
  transition: transform 140ms ease;
}

.toggle input:checked + span {
  background: var(--teal);
  box-shadow: 0 0 0 3px rgba(46, 148, 134, 0.16);
}

.toggle input:checked + span::after {
  transform: translateX(14px);
}

.select-label {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 0 10px 0 12px;
  color: var(--muted);
}

.select-label select {
  height: 32px;
  border: 0;
  color: var(--ink);
  background: transparent;
  cursor: pointer;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 260px;
  align-items: start;
  gap: 16px;
}

.stage-viewport {
  position: relative;
  width: 100%;
  overflow: visible;
}

.stage {
  position: absolute;
  left: 0;
  top: 0;
  width: 1180px;
  height: 760px;
  overflow: hidden;
  border: 1px solid rgba(9, 21, 16, 0.38);
  border-radius: 8px;
  background:
    linear-gradient(90deg, rgba(255, 255, 255, 0.04) 1px, transparent 1px),
    linear-gradient(0deg, rgba(0, 0, 0, 0.05) 1px, transparent 1px),
    repeating-linear-gradient(27deg, rgba(255, 255, 255, 0.028) 0 2px, transparent 2px 9px),
    linear-gradient(145deg, var(--felt), var(--felt-deep));
  background-size:
    32px 32px,
    32px 32px,
    auto,
    auto;
  box-shadow: var(--shadow);
  transform-origin: top left;
  user-select: none;
  touch-action: none;
}

.board {
  position: absolute;
  overflow: visible;
  border: 1px solid rgba(255, 250, 240, 0.34);
  border-radius: 8px;
  background: rgba(255, 250, 240, 0.09);
  box-shadow:
    inset 0 0 0 1px rgba(13, 22, 18, 0.24),
    0 18px 38px rgba(10, 18, 14, 0.32);
}

.board::before {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background:
    linear-gradient(90deg, rgba(255, 250, 240, 0.11) 1px, transparent 1px),
    linear-gradient(0deg, rgba(255, 250, 240, 0.11) 1px, transparent 1px);
  background-size: 25% 25%;
  opacity: 0.52;
  pointer-events: none;
}

.board-image,
.preview img,
.thumb img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  user-select: none;
  -webkit-user-drag: none;
}

.board-image {
  position: absolute;
  inset: 0;
  border-radius: inherit;
  opacity: 0.2;
  filter: saturate(0.9) contrast(1.05);
}

.board-grain {
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background:
    repeating-linear-gradient(45deg, rgba(255, 255, 255, 0.045) 0 1px, transparent 1px 5px),
    linear-gradient(180deg, rgba(255, 250, 240, 0.08), rgba(0, 0, 0, 0.04));
  pointer-events: none;
}

.solution-layer {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  pointer-events: none;
}

.solution-layer path {
  fill: rgba(255, 250, 240, 0.035);
  stroke: rgba(255, 250, 240, 0.34);
  stroke-width: 1.35;
  vector-effect: non-scaling-stroke;
}

.snap-halo {
  position: absolute;
  border: 2px solid rgba(217, 168, 79, 0.85);
  border-radius: 6px;
  opacity: 0;
  pointer-events: none;
  transform: scale(0.98);
  box-shadow:
    0 0 0 5px rgba(217, 168, 79, 0.14),
    0 0 22px rgba(217, 168, 79, 0.45);
  transition:
    opacity 100ms ease,
    transform 100ms ease;
}

.snap-halo.is-visible {
  opacity: 1;
  transform: scale(1);
}

.preview {
  position: absolute;
  inset: 0;
  z-index: 18;
  border-radius: inherit;
  opacity: 0;
  pointer-events: none;
  transform: scale(0.985);
  box-shadow: inset 0 0 0 2px rgba(255, 250, 240, 0.58);
  transition:
    opacity 140ms ease,
    transform 140ms ease;
}

.preview.is-visible {
  opacity: 0.94;
  transform: scale(1);
}

.complete-panel {
  position: absolute;
  left: 50%;
  top: 50%;
  z-index: 40;
  min-width: 190px;
  padding: 18px 22px;
  border: 1px solid rgba(255, 250, 240, 0.42);
  border-radius: 8px;
  color: #fffaf0;
  text-align: center;
  background: rgba(23, 33, 27, 0.84);
  box-shadow: 0 22px 50px rgba(5, 10, 7, 0.38);
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, -46%) scale(0.96);
  transition:
    opacity 170ms ease,
    transform 170ms ease;
}

.complete-panel strong,
.complete-panel span {
  display: block;
}

.complete-panel strong {
  font-size: 1.7rem;
  letter-spacing: 0;
}

.complete-panel span {
  margin-top: 3px;
  color: rgba(255, 250, 240, 0.78);
  font-variant-numeric: tabular-nums;
}

.complete-panel.is-visible {
  opacity: 1;
  transform: translate(-50%, -50%) scale(1);
}

.tray-label {
  position: absolute;
  color: rgba(255, 250, 240, 0.58);
  font-size: 0.82rem;
  letter-spacing: 0;
  pointer-events: none;
}

.tray-label-right {
  right: 32px;
  top: 24px;
}

.tray-label-bottom {
  left: 48px;
  bottom: 22px;
}

.piece {
  position: absolute;
  left: 0;
  top: 0;
  z-index: 1;
  cursor: grab;
  touch-action: none;
  will-change: transform, filter;
  filter: drop-shadow(0 8px 8px rgba(5, 12, 8, 0.34)) drop-shadow(0 18px 24px rgba(5, 12, 8, 0.18));
}

.piece.is-rotating {
  transition: transform 170ms cubic-bezier(0.2, 1.25, 0.22, 1);
}

.piece svg {
  display: block;
  overflow: visible;
  pointer-events: none;
  transform-origin: center;
  transition: transform 120ms ease;
}

.piece.is-dragging {
  cursor: grabbing;
  filter: drop-shadow(0 14px 12px rgba(5, 12, 8, 0.38)) drop-shadow(0 30px 34px rgba(5, 12, 8, 0.24));
}

.piece.is-dragging svg {
  transform: scale(1.035);
}

.piece.is-near {
  filter: drop-shadow(0 0 10px rgba(217, 168, 79, 0.65)) drop-shadow(0 16px 20px rgba(5, 12, 8, 0.24));
}

.piece.is-selected:not(.is-locked) {
  filter: drop-shadow(0 0 9px rgba(46, 148, 134, 0.7)) drop-shadow(0 14px 18px rgba(5, 12, 8, 0.24));
}

.piece.is-snapped svg {
  animation: snap-settle 190ms cubic-bezier(0.2, 1.3, 0.3, 1);
}

.piece.is-misaligned svg {
  animation: twist-warning 260ms ease-out;
}

.piece.is-locked {
  cursor: default;
  filter: drop-shadow(0 4px 4px rgba(5, 12, 8, 0.22));
}

.piece-outline {
  fill: none;
  stroke: rgba(255, 250, 240, 0.84);
  stroke-width: 1.8;
  vector-effect: non-scaling-stroke;
}

.piece-rim {
  fill: none;
  stroke: rgba(20, 29, 24, 0.32);
  stroke-width: 3.2;
  vector-effect: non-scaling-stroke;
}

.toast {
  position: absolute;
  left: 50%;
  bottom: 28px;
  z-index: 80;
  min-width: 96px;
  padding: 9px 13px;
  border: 1px solid rgba(255, 250, 240, 0.38);
  border-radius: 8px;
  color: #fffaf0;
  text-align: center;
  background: rgba(20, 31, 25, 0.82);
  box-shadow: 0 12px 28px rgba(5, 10, 7, 0.26);
  opacity: 0;
  transform: translate(-50%, 8px);
  transition:
    opacity 130ms ease,
    transform 130ms ease;
  pointer-events: none;
}

.toast.is-visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

.spark {
  position: absolute;
  z-index: 75;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--gold);
  box-shadow: 0 0 12px rgba(217, 168, 79, 0.8);
  pointer-events: none;
  animation: spark-pop 520ms ease-out forwards;
}

.side-panel {
  display: grid;
  gap: 12px;
}

.thumb {
  overflow: hidden;
  aspect-ratio: 3 / 2;
  border: 1px solid rgba(23, 33, 27, 0.18);
  border-radius: 8px;
  background: var(--paper);
  box-shadow: 0 12px 26px rgba(24, 35, 29, 0.12);
}

.meter {
  display: grid;
  gap: 8px;
  padding: 12px;
  border: 1px solid rgba(23, 33, 27, 0.14);
  border-radius: 8px;
  background: rgba(255, 250, 240, 0.74);
}

.meter-track {
  overflow: hidden;
  height: 10px;
  border-radius: 999px;
  background: rgba(23, 33, 27, 0.12);
}

.meter-fill {
  width: 0%;
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--teal), var(--gold), var(--coral));
  transition: width 180ms ease;
}

.meter span {
  color: var(--muted);
  font-size: 0.92rem;
  font-variant-numeric: tabular-nums;
}

@keyframes snap-settle {
  0% {
    transform: scale(1.055);
  }

  58% {
    transform: scale(0.982);
  }

  100% {
    transform: scale(1);
  }
}

@keyframes twist-warning {
  0% {
    transform: rotate(0deg);
  }

  34% {
    transform: rotate(4deg);
  }

  68% {
    transform: rotate(-3deg);
  }

  100% {
    transform: rotate(0deg);
  }
}

@keyframes spark-pop {
  0% {
    opacity: 1;
    transform: translate(0, 0) scale(1);
  }

  100% {
    opacity: 0;
    transform: translate(var(--spark-x), var(--spark-y)) scale(0.2);
  }
}

@media (max-width: 1060px) {
  .workspace {
    grid-template-columns: 1fr;
  }

  .side-panel {
    grid-template-columns: minmax(170px, 260px) minmax(160px, 1fr);
    align-items: stretch;
  }
}

@media (max-width: 720px) {
  .app {
    padding: 14px;
  }

  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .stats {
    width: 100%;
    display: grid;
    grid-template-columns: repeat(3, 1fr);
  }

  .stat {
    min-width: 0;
    padding: 8px 9px;
  }

  .controls {
    gap: 8px;
  }

  .button,
  .toggle,
  .select-label {
    min-height: 38px;
  }

  .side-panel {
    grid-template-columns: 1fr;
  }
}
