:root {
  color-scheme: light;
  --bg: #f4f1eb;
  --panel: #fffaf2;
  --ink: #252019;
  --muted: #756d62;
  --line: #d8cfc1;
  --red: #d84236;
  --green: #277348;
  --gold: #c9931d;
  --blue: #315d92;
  --shadow: 0 18px 50px rgba(36, 31, 24, 0.16);
  --preview-aspect: 550 / 370;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.52), rgba(255, 255, 255, 0)),
    var(--bg);
  color: var(--ink);
  font-family: "Segoe UI", "Malgun Gothic", system-ui, sans-serif;
}

button,
input {
  font: inherit;
}

.app {
  display: grid;
  grid-template-columns: minmax(120px, 150px) minmax(280px, 360px) minmax(0, 1fr);
  gap: 18px;
  width: min(1320px, calc(100% - 32px));
  min-height: 100vh;
  margin: 0 auto;
  padding: 28px 0;
  align-items: center;
}

.image-rail,
.control-panel,
.board-shell {
  border: 1px solid var(--line);
  background: rgba(255, 250, 242, 0.92);
  box-shadow: var(--shadow);
}

.image-rail {
  display: flex;
  flex-direction: column;
  gap: 18px;
  max-height: calc(100vh - 56px);
  overflow: auto;
  padding: 14px;
  border-radius: 8px;
}

.image-rail section {
  display: grid;
  gap: 10px;
}

.image-rail h2 {
  margin: 0;
  color: var(--green);
  font-size: 0.78rem;
  font-weight: 900;
}

.thumb-list {
  display: grid;
  gap: 8px;
}

.thumb-item {
  position: relative;
}

.thumb-button {
  display: block;
  width: 100%;
  padding: 0;
  overflow: hidden;
  border: 2px solid transparent;
  border-radius: 6px;
  background: #fff;
  cursor: pointer;
  box-shadow: 0 1px 0 rgba(37, 32, 25, 0.12);
}

.thumb-remove {
  position: absolute;
  top: 6px;
  right: 6px;
  display: grid;
  place-items: center;
  width: 24px;
  height: 24px;
  border: 1px solid rgba(37, 32, 25, 0.14);
  border-radius: 50%;
  background: rgba(255, 250, 242, 0.92);
  color: var(--ink);
  cursor: pointer;
  font-size: 0.8rem;
  font-weight: 900;
  line-height: 1;
  box-shadow: 0 2px 8px rgba(37, 32, 25, 0.16);
}

.thumb-remove:hover {
  background: var(--red);
  color: #fff;
}

.thumb-button:hover,
.thumb-button.is-active {
  border-color: var(--red);
}

.thumb-button img {
  display: block;
  width: 100%;
  aspect-ratio: var(--preview-aspect);
  object-fit: cover;
}

.thumb-label {
  display: block;
  overflow: hidden;
  padding: 5px 6px 6px;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 800;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.empty-history {
  margin: 0;
  color: var(--muted);
  font-size: 0.78rem;
  line-height: 1.35;
}

.control-panel {
  display: flex;
  flex-direction: column;
  gap: 20px;
  padding: 22px;
  border-radius: 8px;
}

.title-block {
  display: grid;
  gap: 4px;
}

.eyebrow {
  margin: 0;
  color: var(--green);
  font-size: 0.78rem;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1,
h2,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  max-width: 9.5em;
  font-size: clamp(1.45rem, 2.2vw, 1.9rem);
  line-height: 1.24;
  word-break: keep-all;
  overflow-wrap: normal;
}

.preview-wrap {
  overflow: hidden;
  aspect-ratio: var(--preview-aspect);
  border: 2px solid var(--ink);
  background: #ded7cc;
}

.preview-wrap img {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: cover;
}

.controls {
  display: grid;
  gap: 14px;
}

.file-control,
.grid-fields label,
.select-control,
.speed-control {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-size: 0.88rem;
  font-weight: 700;
}

.file-control input,
.grid-fields input,
.select-control select {
  width: 100%;
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
}

.file-control input {
  padding: 8px;
}

.grid-fields {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.grid-fields input,
.select-control select {
  padding: 8px 10px;
}

.speed-control span {
  display: flex;
  justify-content: space-between;
  gap: 10px;
}

.speed-control strong {
  color: var(--ink);
}

.speed-control input {
  width: 100%;
  accent-color: var(--red);
}

.primary-button,
.ghost-button {
  min-height: 44px;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 800;
  transition: transform 160ms ease, box-shadow 160ms ease, background 160ms ease;
}

.primary-button {
  border: 0;
  background: var(--red);
  color: white;
  box-shadow: 0 10px 20px rgba(216, 66, 54, 0.24);
}

.ghost-button {
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
  padding: 0 14px;
}

.primary-button:hover,
.ghost-button:hover {
  transform: translateY(-1px);
}

.primary-button:active,
.ghost-button:active {
  transform: translateY(0);
}

.primary-button:disabled,
.ghost-button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
  transform: none;
}

.score-board {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  margin: 0;
}

.score-board div {
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  padding: 12px;
}

.score-board dt {
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 800;
}

.score-board dd {
  margin: 4px 0 0;
  font-size: 1.35rem;
  font-weight: 900;
}

.score-board .wide-stat {
  grid-column: 1 / -1;
}

.play-area {
  min-width: 0;
}

.board-shell {
  padding: 18px;
  border-radius: 8px;
}

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

.board-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.status-label {
  margin-bottom: 3px;
  color: var(--green);
  font-weight: 900;
}

.status-text {
  margin-bottom: 0;
  color: var(--muted);
}

.puzzle-stage {
  position: relative;
}

.puzzle-board {
  position: relative;
  overflow: hidden;
  width: 100%;
  max-height: calc(100vh - 130px);
  aspect-ratio: var(--board-cols) / var(--rows);
  background: transparent;
  touch-action: none;
  user-select: none;
}

.puzzle-board::after {
  content: none;
}

.tile {
  position: absolute;
  left: calc(var(--x) * 100% / var(--board-cols));
  top: calc(var(--y) * 100% / var(--rows));
  width: calc(100% / var(--board-cols));
  height: calc(100% / var(--rows));
  overflow: hidden;
  border: 0;
  background: #fff;
  cursor: default;
  box-shadow:
    inset 0 0 0 1px rgba(216, 66, 54, 0.72),
    inset 0 0 0 2px rgba(255, 255, 255, 0.24);
  transition: left 180ms ease, top 180ms ease, transform 120ms ease, filter 120ms ease;
}

.tile.movable {
  cursor: grab;
  filter: saturate(1.08);
}

.tile.movable:hover {
  transform: scale(0.985);
}

.tile.is-pressed {
  cursor: grabbing;
  transform: scale(0.965);
}

.tile-image {
  position: absolute;
  left: calc(var(--source-x) * -100%);
  top: calc(var(--source-y) * -100%);
  width: calc(var(--cols) * 100%);
  height: calc(var(--rows) * 100%);
  background-position: center;
  background-repeat: no-repeat;
  background-size: 100% 100%;
}

.tile::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  box-shadow: inset 0 0 12px rgba(37, 32, 25, 0.08);
}

.empty-cell {
  position: absolute;
  left: calc(var(--x) * 100% / var(--board-cols));
  top: calc(var(--y) * 100% / var(--rows));
  width: calc(100% / var(--board-cols));
  height: calc(100% / var(--rows));
  border: 0;
  box-shadow: inset 0 0 0 1px rgba(216, 66, 54, 0.74);
  background:
    linear-gradient(45deg, transparent calc(50% - 0.5px), rgba(216, 66, 54, 0.78) calc(50% - 0.5px), rgba(216, 66, 54, 0.78) calc(50% + 0.5px), transparent calc(50% + 0.5px)),
    linear-gradient(135deg, rgba(255, 255, 255, 0.25), rgba(0, 0, 0, 0.06)),
    #c7bfb2;
}

.finish-overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  overflow: hidden;
  opacity: 0;
  pointer-events: none;
  transition: opacity 220ms ease;
}

.finish-overlay.is-visible {
  opacity: 1;
  pointer-events: auto;
}

.burst {
  position: absolute;
  width: 42%;
  aspect-ratio: 1;
  border-radius: 50%;
  background:
    radial-gradient(circle, rgba(255, 255, 255, 0.95) 0 18%, rgba(246, 196, 57, 0.85) 19% 34%, rgba(216, 66, 54, 0.78) 35% 48%, rgba(49, 93, 146, 0) 58%);
  transform: scale(0);
}

.finish-overlay.is-visible .burst {
  animation: burst-pop 800ms ease-out forwards;
}

.finish-card {
  position: relative;
  width: min(330px, calc(100% - 36px));
  border: 1px solid rgba(37, 32, 25, 0.16);
  border-radius: 8px;
  background: rgba(255, 250, 242, 0.94);
  padding: 24px;
  text-align: center;
  box-shadow: var(--shadow);
  transform: translateY(10px) scale(0.96);
}

.finish-overlay.is-visible .finish-card {
  animation: card-in 360ms ease-out 120ms forwards;
}

.finish-kicker {
  margin-bottom: 4px;
  color: var(--red);
  font-size: 2rem;
  font-weight: 1000;
}

.finish-card h2 {
  margin-bottom: 8px;
}

.finish-card .primary-button {
  width: 100%;
  margin-top: 10px;
}

.confetti-layer {
  position: fixed;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
  z-index: 30;
}

.confetti {
  position: absolute;
  top: -20px;
  width: 10px;
  height: 16px;
  background: var(--red);
  animation: confetti-fall 1500ms ease-in forwards;
}

@keyframes burst-pop {
  0% {
    opacity: 0.95;
    transform: scale(0);
  }
  55% {
    opacity: 0.9;
    transform: scale(1.65);
  }
  100% {
    opacity: 0;
    transform: scale(2.1);
  }
}

@keyframes card-in {
  to {
    transform: translateY(0) scale(1);
  }
}

@keyframes confetti-fall {
  to {
    top: 105vh;
    transform: translateX(var(--drift)) rotate(var(--spin));
    opacity: 0.2;
  }
}

@media (max-width: 1180px), (pointer: coarse) {
  .app {
    grid-template-columns: 1fr;
    align-items: start;
    width: min(100% - 20px, 960px);
    min-height: auto;
    padding: 12px 0 18px;
    gap: 14px;
  }

  .play-area {
    order: 1;
  }

  .board-shell {
    padding: 12px;
  }

  .board-topline {
    align-items: stretch;
    flex-direction: column;
    gap: 10px;
    margin-bottom: 10px;
  }

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

  .board-actions .ghost-button {
    width: 100%;
    min-height: 40px;
    padding: 0 8px;
    font-size: 0.92rem;
  }

  .status-label {
    font-size: 0.95rem;
  }

  .status-text {
    font-size: 0.88rem;
  }

  .puzzle-board {
    max-height: min(62vh, 680px);
  }

  .control-panel {
    order: 2;
    gap: 14px;
    padding: 16px;
  }

  .title-block {
    gap: 2px;
  }

  h1 {
    max-width: none;
    font-size: clamp(1.45rem, 5vw, 2rem);
  }

  .preview-wrap {
    display: none;
  }

  .controls {
    gap: 12px;
  }

  .score-board dd {
    font-size: 1.15rem;
  }

  .image-rail {
    order: 3;
    max-height: none;
    overflow: visible;
    padding: 12px;
  }

  .image-rail section {
    gap: 8px;
  }

  .thumb-list {
    grid-template-columns: repeat(auto-fill, minmax(118px, 1fr));
    gap: 8px;
  }

  .thumb-label {
    font-size: 0.7rem;
  }
}

@media (min-width: 721px) and (max-width: 1180px) {
  .thumb-list {
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  }

  .control-panel {
    display: grid;
    grid-template-columns: minmax(240px, 0.85fr) minmax(320px, 1.15fr);
    align-items: start;
  }

  .title-block,
  .preview-wrap {
    grid-column: 1;
  }

  .controls,
  .score-board {
    grid-column: 2;
  }

  .preview-wrap {
    display: block;
  }
}

@media (pointer: coarse) and (orientation: landscape) {
  .app {
    width: min(100% - 18px, 1180px);
  }

  .puzzle-board {
    max-height: calc(100vh - 190px);
  }
}

@media (max-width: 520px) {
  .app {
    width: min(100% - 12px, 1180px);
    padding-top: 8px;
  }

  .control-panel,
  .board-shell {
    padding: 10px;
  }

  .board-actions {
    grid-template-columns: 1fr;
  }

  .puzzle-board {
    max-height: min(58vh, 520px);
  }

  .tile {
    box-shadow: inset 0 0 0 1px rgba(216, 66, 54, 0.7);
  }
}
