:root {
  --bg: #181411;
  --panel: #f7f3ea;
  --paper: #fffaf0;
  --ink: #231b16;
  --muted: #7c6d62;
  --line: rgba(35, 27, 22, 0.16);
  --orange: #d66b28;
  --green: #4b8063;
  --blue: #285a70;
  --shadow: 0 24px 70px rgba(0, 0, 0, 0.32);
  --cols: 5;
  --rows: 5;
  --bg-size-x: 500%;
  --bg-size-y: 500%;
  --image-ratio: 835 / 980;
  --piece-ratio: 1.7041;
  --tab-outset: 18%;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  font-family: Georgia, "Times New Roman", serif;
  background:
    linear-gradient(115deg, rgba(214, 107, 40, 0.2), transparent 32%),
    linear-gradient(245deg, rgba(75, 128, 99, 0.28), transparent 35%),
    radial-gradient(circle at top right, rgba(255, 250, 240, 0.22), transparent 30%),
    var(--bg);
}

button,
input,
select {
  font: inherit;
}

button,
select {
  border: 1px solid rgba(255, 250, 240, 0.2);
  border-radius: 7px;
  padding: 0.78rem 0.82rem;
  color: #fffaf0;
  background: #2a211b;
  cursor: pointer;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.04);
  transition: transform 160ms ease, background 160ms ease, border-color 160ms ease;
}

button:hover,
button:focus-visible,
select:hover,
select:focus-visible {
  background: #3a2b22;
  border-color: rgba(214, 107, 40, 0.72);
  transform: translateY(-1px);
}

button:active {
  transform: translateY(0);
}

button.active {
  background: var(--green);
  border-color: rgba(255, 250, 240, 0.4);
}

.shell {
  width: min(1220px, calc(100vw - 32px));
  margin: 0 auto;
  padding: 24px 0 30px;
}

.hero {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 18px;
  margin-bottom: 18px;
  color: #fffaf0;
}

.eyebrow {
  margin: 0 0 5px;
  color: #f1bc86;
  font-size: 0.78rem;
  letter-spacing: 0.16em;
  text-transform: uppercase;
}

h1 {
  margin: 0 0 8px;
  font-size: clamp(2.25rem, 5vw, 5rem);
  font-weight: 700;
  line-height: 0.94;
  letter-spacing: 0;
}

.difficulty-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 6px;
  font-size: 0.92rem;
  color: #f1bc86;
}

.difficulty-row label {
  white-space: nowrap;
}

#difficultySelect {
  padding: 0.45rem 0.7rem;
  font-size: 0.92rem;
  min-width: 190px;
}

.meter {
  display: grid;
  gap: 8px;
  min-width: min(270px, 44vw);
  font-size: 0.95rem;
  text-align: right;
}

.meter-track {
  display: block;
  height: 8px;
  overflow: hidden;
  border: 1px solid rgba(255, 250, 240, 0.22);
  border-radius: 999px;
  background: rgba(255, 250, 240, 0.12);
}

#progressBar {
  display: block;
  width: 0%;
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--orange), var(--green));
  transition: width 220ms ease;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(300px, 0.95fr) minmax(330px, 0.75fr);
  gap: 18px;
  align-items: start;
}

.board-wrap,
.side-panel {
  border: 1px solid rgba(255, 250, 240, 0.17);
  border-radius: 8px;
  background: rgba(247, 243, 234, 0.95);
  box-shadow: var(--shadow);
}

.board-wrap {
  padding: clamp(10px, 1.8vw, 18px);
}

.board {
  position: relative;
  display: grid;
  grid-template-columns: repeat(var(--cols), minmax(0, 1fr));
  grid-template-rows: repeat(var(--rows), minmax(0, 1fr));
  width: min(100%, 620px);
  margin: 0 auto;
  aspect-ratio: var(--image-ratio);
  border: 3px solid #241b15;
  border-radius: 8px;
  background: #4a4038;
  overflow: visible;
}

.board::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 0;
  background-image: var(--puzzle-image);
  background-size: 100% 100%;
  background-position: center;
  opacity: 0;
  transition: opacity 180ms ease;
  pointer-events: none;
  border-radius: 5px;
}

.board.show-hint::before,
.board.show-preview::before {
  opacity: 0.18;
}

.board.is-complete::before {
  opacity: 1;
  z-index: 2;
  background-size: 100% 100%;
}

.board.is-complete .piece {
  opacity: 0;
  pointer-events: none;
}

.board.is-complete .slot {
  z-index: 1;
}

.slot {
  position: relative;
  z-index: 1;
  min-width: 0;
  min-height: 0;
  border: 1px solid rgba(255, 250, 240, 0.14);
  background: rgba(24, 20, 17, 0.22);
}

.slot.drop-target {
  outline: 3px solid rgba(75, 128, 99, 0.85);
  outline-offset: -4px;
  z-index: 5;
}

.slot.filled {
  border-color: transparent;
  background: transparent;
}

/* slot ghost - jigsaw outline in empty cells */
.slot-ghost {
  position: absolute;
  inset: calc(var(--tab-outset) * -1);
  width: calc(100% + (var(--tab-outset) * 2));
  height: calc(100% + (var(--tab-outset) * 2));
  pointer-events: none;
  z-index: 0;
}

.slot-ghost path {
  fill: rgba(255, 240, 220, 0.04);
  stroke: rgba(255, 210, 150, 0.32);
  stroke-width: 1.8;
  stroke-linejoin: round;
  stroke-dasharray: 5 3;
}

.slot.filled .slot-ghost {
  opacity: 0;
  transition: opacity 150ms ease;
}

.board.is-complete .slot-ghost {
  opacity: 0;
}

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

.portrait {
  overflow: hidden;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: #201914;
}

.portrait img {
  display: block;
  width: 100%;
  max-height: 220px;
  object-fit: contain;
  object-position: center;
}

.controls {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 7px;
}

#photoInput {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  opacity: 0;
  pointer-events: none;
}

.tray {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(62px, 1fr));
  align-content: start;
  gap: 18px 12px;
  max-height: min(58vh, 680px);
  min-height: 240px;
  overflow-x: hidden;
  overflow-y: auto;
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background:
    linear-gradient(180deg, rgba(40, 90, 112, 0.12), transparent 130px),
    rgba(255, 250, 240, 0.72);
}

.tray.is-empty {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 72px;
  max-height: 72px;
  padding: 12px;
}

.tray.is-empty::after {
  content: "All pieces placed.";
  color: var(--muted);
  font-size: 0.95rem;
}

.piece {
  position: relative;
  display: block;
  width: 100%;
  min-width: 0;
  overflow: visible;
  border: 0;
  border-radius: 0;
  padding: 0;
  aspect-ratio: var(--piece-ratio);
  background: transparent;
  box-shadow: none;
  touch-action: none;
  user-select: none;
}

.piece svg {
  position: absolute;
  inset: calc(var(--tab-outset) * -1);
  width: calc(100% + (var(--tab-outset) * 2));
  height: calc(100% + (var(--tab-outset) * 2));
  overflow: visible;
  pointer-events: none;
  filter: drop-shadow(0 3px 6px rgba(35, 27, 22, 0.18));
  shape-rendering: geometricPrecision;
}

.piece svg image {
  image-rendering: -webkit-optimize-contrast;
  image-rendering: crisp-edges;
}

.piece:hover,
.piece:focus-visible,
.piece.selected {
  outline: 3px solid rgba(214, 107, 40, 0.5);
  outline-offset: -3px;
  z-index: 10;
}

.slot .piece {
  width: 100%;
  height: 100%;
  box-shadow: none;
}

.slot .piece svg {
  filter: none;
}

.slot .piece.correct svg path {
  stroke: none;
}

/* snap glow when piece locks into correct slot */
@keyframes snapGlow {
  0%   { filter: drop-shadow(0 0 0 rgba(75, 200, 120, 0)) brightness(1); }
  30%  { filter: drop-shadow(0 0 14px rgba(75, 200, 120, 0.9)) brightness(1.15); }
  100% { filter: drop-shadow(0 0 0 rgba(75, 200, 120, 0)) brightness(1); }
}

.piece.snap svg {
  animation: snapGlow 550ms ease-out;
}

/* wrong placement shake */
@keyframes wrongShake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-5px); }
  40% { transform: translateX(5px); }
  60% { transform: translateX(-3px); }
  80% { transform: translateX(3px); }
}

.piece.wrong {
  outline: 3px solid rgba(210, 50, 50, 0.7);
  outline-offset: -3px;
  animation: wrongShake 350ms ease;
}

.piece.dragging {
  cursor: grabbing;
  opacity: 0.94;
  transform: scale(1.03);
}

.piece-placeholder {
  width: 100%;
  aspect-ratio: var(--piece-ratio);
  border: 1px dashed rgba(35, 27, 22, 0.35);
  border-radius: 4px;
  background: rgba(35, 27, 22, 0.08);
}

.slot .piece-placeholder {
  height: 100%;
}

.status {
  min-height: 34px;
  margin-top: 14px;
  color: #fffaf0;
  font-size: 1rem;
}

.status.win {
  color: #f1bc86;
}

@media (max-width: 900px) {
  .hero {
    display: grid;
  }

  .meter {
    min-width: 0;
    text-align: left;
  }

  .workspace {
    grid-template-columns: 1fr;
  }

  .board {
    width: min(100%, 520px);
  }

  .controls {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .tray {
    max-height: none;
    grid-template-columns: repeat(auto-fill, minmax(55px, 1fr));
  }
}

@media (max-width: 520px) {
  .shell {
    width: min(100vw - 18px, 1220px);
    padding-top: 12px;
  }

  h1 {
    font-size: 2.2rem;
  }

  .board-wrap,
  .side-panel {
    padding: 9px;
  }

  .controls {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  #difficultySelect {
    min-width: unset;
    width: 100%;
  }

  .difficulty-row {
    flex-wrap: wrap;
  }
}
