:root {
  color-scheme: light;
  --bg: #f6f1e8;
  --ink: #1f2328;
  --muted: #626a73;
  --line: rgba(31, 35, 40, 0.14);
  --panel: rgba(255, 255, 255, 0.78);
  --accent: #2f7c72;
  --accent-strong: #1f5f57;
  --warn: #d58a2c;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
}

* {
  box-sizing: border-box;
}

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

body {
  margin: 0;
  background:
    radial-gradient(circle at 20% 10%, rgba(255, 210, 128, 0.34), transparent 32rem),
    linear-gradient(135deg, #f6f1e8 0%, #e9f2ef 52%, #f8e7d3 100%);
  color: var(--ink);
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

button,
label {
  font: inherit;
}

.app {
  width: min(1120px, 100%);
  min-height: 100svh;
  margin: 0 auto;
  padding: max(18px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right))
    max(18px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: end;
  gap: 16px;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--accent-strong);
  font-size: 0.82rem;
  font-weight: 800;
}

h1 {
  margin: 0;
  font-size: clamp(1.7rem, 4vw, 3.3rem);
  line-height: 1.02;
  letter-spacing: 0;
}

.stats {
  display: flex;
  gap: 8px;
}

.stat {
  min-width: 84px;
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  backdrop-filter: blur(12px);
}

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

.stat strong {
  display: block;
  margin-top: 2px;
  font-size: 1.18rem;
  line-height: 1;
}

.game-shell {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 286px;
  align-items: start;
  gap: 18px;
}

.board-wrap {
  position: relative;
  width: min(100%, calc(100svh - 150px));
  max-width: 720px;
  margin: 0 auto;
  aspect-ratio: 1;
}

.board {
  position: relative;
  width: 100%;
  height: 100%;
  border: 1px solid rgba(31, 35, 40, 0.18);
  border-radius: 8px;
  background: rgba(31, 35, 40, 0.13);
  overflow: hidden;
  box-shadow: 0 24px 70px rgba(31, 35, 40, 0.18);
  touch-action: none;
}

.tile {
  position: absolute;
  width: calc(100% / var(--size));
  height: calc(100% / var(--size));
  border: 1px solid rgba(255, 255, 255, 0.84);
  border-radius: 6px;
  background-image: var(--image);
  background-size: calc(var(--size) * 100%) calc(var(--size) * 100%);
  cursor: pointer;
  transition:
    transform 150ms ease,
    filter 150ms ease,
    box-shadow 150ms ease;
  will-change: transform;
}

.tile::after {
  content: attr(data-label);
  position: absolute;
  right: 8px;
  bottom: 7px;
  min-width: 24px;
  height: 24px;
  padding: 0 7px;
  border-radius: 999px;
  display: grid;
  place-items: center;
  background: rgba(255, 255, 255, 0.82);
  color: rgba(31, 35, 40, 0.72);
  font-size: clamp(0.7rem, 2.3vw, 0.88rem);
  font-weight: 800;
}

.tile:active {
  filter: brightness(1.05);
  box-shadow: inset 0 0 0 999px rgba(255, 255, 255, 0.12);
}

.tile.selected {
  z-index: 2;
  box-shadow:
    inset 0 0 0 4px var(--warn),
    0 10px 24px rgba(31, 35, 40, 0.22);
  filter: saturate(1.08) brightness(1.04);
}

.tile.dragging {
  z-index: 3;
  box-shadow: 0 18px 36px rgba(31, 35, 40, 0.3);
  filter: saturate(1.1) brightness(1.06);
  transition:
    filter 120ms ease,
    box-shadow 120ms ease;
}

.board.swap-mode {
  background: rgba(255, 255, 255, 0.38);
}

.complete {
  position: absolute;
  inset: 0;
  border-radius: 8px;
  display: grid;
  place-items: center;
  background: rgba(246, 241, 232, 0.78);
  backdrop-filter: blur(8px);
  text-align: center;
}

.complete[hidden] {
  display: none;
}

.complete div {
  width: min(320px, 82%);
  padding: 22px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.88);
}

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

.complete strong {
  font-size: 2rem;
}

.complete span {
  margin-top: 6px;
  color: var(--muted);
}

.panel {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.preview {
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  background: var(--panel);
}

.preview canvas {
  display: block;
  width: 100%;
  aspect-ratio: 1;
}

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

.control-row {
  display: grid;
  gap: 8px;
}

.control-row > span {
  color: var(--muted);
  font-size: 0.84rem;
  font-weight: 800;
}

.segments {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  background: rgba(255, 255, 255, 0.55);
}

.mode-segments {
  grid-template-columns: repeat(2, 1fr);
}

.segment {
  min-height: 44px;
  border: 0;
  border-right: 1px solid var(--line);
  background: transparent;
  color: var(--ink);
  font-weight: 800;
}

.segment:last-child {
  border-right: 0;
}

.segment.active {
  background: var(--accent);
  color: #fff;
}

.actions {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

.primary,
.secondary {
  min-height: 48px;
  border-radius: 8px;
  display: grid;
  place-items: center;
  border: 1px solid transparent;
  font-weight: 900;
}

.primary {
  background: var(--ink);
  color: #fff;
}

.secondary {
  border-color: var(--line);
  background: rgba(255, 255, 255, 0.72);
  color: var(--ink);
  cursor: pointer;
}

.secondary:disabled {
  cursor: progress;
  opacity: 0.68;
}

.image-status {
  min-height: 18px;
  margin: -4px 0 0;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
}

#imageInput {
  position: absolute;
  inline-size: 1px;
  block-size: 1px;
  opacity: 0;
  pointer-events: none;
}

@media (max-width: 820px) {
  .app {
    gap: 14px;
  }

  .topbar {
    align-items: start;
  }

  .game-shell {
    grid-template-columns: 1fr;
  }

  .board-wrap {
    width: min(100%, calc(100svh - 230px));
    max-width: 680px;
  }

  .panel {
    display: grid;
    grid-template-columns: 116px 1fr;
    align-items: start;
  }
}

@media (max-width: 560px) {
  .topbar {
    display: grid;
  }

  .stats {
    width: 100%;
  }

  .stat {
    flex: 1;
  }

  .board-wrap {
    width: min(100%, calc(100svh - 292px));
  }

  .panel {
    grid-template-columns: 92px 1fr;
    gap: 10px;
  }

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

  .primary,
  .secondary {
    min-height: 44px;
  }

  .tile::after {
    right: 5px;
    bottom: 5px;
    min-width: 20px;
    height: 20px;
    padding: 0 5px;
  }
}
