* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

body {
  font-family: "DM Sans", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: #f5f7fa;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 24px;
}

@media (max-width: 640px) {
  body {
    padding: 12px 8px;
    align-items: flex-start;
    padding-top: max(12px, env(safe-area-inset-top));
    padding-bottom: max(12px, env(safe-area-inset-bottom));
    padding-left: max(8px, env(safe-area-inset-left));
    padding-right: max(8px, env(safe-area-inset-right));
  }
}

.screen {
  width: 100%;
  max-width: 720px;
  min-width: 300px;
  flex-shrink: 0;
}

.hidden {
  display: none !important;
}

.container {
  background: #fff;
  border-radius: 16px;
  padding: 32px 16px;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.06);
}

h1 {
  text-align: center;
  color: #1a1a2e;
  margin-bottom: 24px;
  font-size: 1.75rem;
  font-weight: 700;
}

.screen-title {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
}

.screen-title-icon {
  font-size: 1.5em;
}

/* Game settings (start) screen */
.game-settings {
  margin: 0 20px;
}

.setting-section {
  margin-bottom: 28px;
}

.setting-label {
  color: #374151;
  margin-bottom: 12px;
  font-size: 0.9375rem;
  font-weight: 600;
}

.size-buttons {
  display: flex;
  gap: 12px;
}

.size-btn {
  flex: 1;
  padding: 14px 20px;
  background: #EDECE8;
  color: #5B5B6D;
  border: none;
  border-radius: 12px;
  font-family: inherit;
  font-size: 1rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.2s, color 0.2s;
}

.size-btn:hover {
  background: #e2e0db;
  color: #4a4a5a;
}

.size-btn.selected {
  background: #3BB49A;
  color: #fff;
  font-weight: 700;
}

.size-btn.selected:hover {
  background: #35a08a;
}

h2 {
  color: #374151;
  margin-bottom: 16px;
  font-size: 1.125rem;
  font-weight: 600;
}

h3 {
  color: #6b7280;
  margin: 24px 0 12px 0;
  font-size: 0.9375rem;
  font-weight: 600;
}

/* Image section */
.image-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
  margin-bottom: 16px;
}

.image-option {
  position: relative;
  aspect-ratio: 1;
  overflow: hidden;
  cursor: pointer;
  border: 2px solid #e5e7eb;
  border-radius: 12px;
  padding: 0;
  transition: border-color 0.2s, box-shadow 0.2s, transform 0.2s;
  background: #fff;
}

.image-option::after {
  content: "";
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 40%;
  background: linear-gradient(to top, rgba(0, 0, 0, 0.65), transparent);
  pointer-events: none;
}

.image-option:hover {
  border-color: #5eead4;
  box-shadow: 0 4px 12px rgba(13, 148, 136, 0.15);
  transform: translateY(-2px);
}

.image-option.selected {
  border-color: #0d9488;
  box-shadow: 0 0 0 2px rgba(13, 148, 136, 0.2);
}

.image-option img {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.image-option span {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 12px 8px 10px;
  color: #fff;
  font-size: 0.9375rem;
  font-weight: 600;
  text-align: center;
  z-index: 1;
}

.upload-section {
  margin: 24px 0;
  text-align: center;
}

.upload-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  padding: 14px 20px;
  background: #fff;
  color: #5B5B6D;
  border: 2px dashed #d1d5db;
  border-radius: 10px;
  cursor: pointer;
  transition: background 0.2s, border-color 0.2s, color 0.2s;
  font-family: inherit;
  font-size: 0.9375rem;
  font-weight: 500;
}

.upload-btn:hover {
  background: #3BB49A;
  border-color: #3BB49A;
  color: #fff;
}

.upload-btn-icon {
  width: 20px;
  height: 20px;
  flex-shrink: 0;
}

.preview-container {
  margin-top: 16px;
}

.preview-container img {
  max-width: 280px;
  max-height: 280px;
  border-radius: 12px;
  border: 2px solid #e5e7eb;
  margin: 0 auto;
  display: block;
}

.preview-tile {
  position: relative;
  overflow: hidden;
  max-width: 280px;
  max-height: 280px;
  margin: 0 auto;
  border: 2px solid #e5e7eb;
  border-radius: 12px;
  aspect-ratio: 1;
}

.preview-tile::after {
  content: "";
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 40%;
  background: linear-gradient(to top, rgba(0, 0, 0, 0.65), transparent);
  pointer-events: none;
}

.preview-tile img {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  max-width: none;
  max-height: none;
  border: none;
  border-radius: 0;
}

.preview-tile span {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 12px 8px 10px;
  color: #fff;
  font-size: 0.9375rem;
  font-weight: 600;
  text-align: center;
  z-index: 1;
}

.play-btn {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 16px 24px;
  background: #0d9488;
  color: #fff;
  border: none;
  border-radius: 12px;
  font-size: 1.25rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
  margin-top: 28px;
}

.play-btn:hover:not(:disabled) {
  background: #0f766e;
}

.play-btn:disabled {
  background: #d1d5db;
  color: #9ca3af;
  cursor: not-allowed;
}

.play-btn-icon {
  font-size: 0.9em;
}

/* Game screen */
#gameScreen .container {
  max-width: 552px;
  margin-left: auto;
  margin-right: auto;
}

@media (max-width: 640px) {
  #gameScreen .container {
    max-width: 100%;
  }
}

.game-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 24px;
  margin-left: 20px;
  margin-right: 20px;
  flex-wrap: wrap;
  gap: 12px;
}

.game-header-left {
  flex: 1;
  min-width: 0;
}

.game-title {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 6px;
  font-size: 1.5rem;
  font-weight: 700;
  color: #1a1a2e;
}

.game-title-icon {
  font-size: 1.25em;
}

.game-stats {
  font-size: 0.875rem;
  color: #6b7280;
  font-weight: 500;
  margin: 0;
}

.game-header-actions {
  display: flex;
  gap: 10px;
}

.game-action-btn {
  width: 44px;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  background: #f3f4f6;
  color: #374151;
  border: 1px solid #e5e7eb;
  border-radius: 50%;
  cursor: pointer;
  font-size: 1.25rem;
  transition: background 0.2s, box-shadow 0.2s;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
}

.game-action-btn:hover {
  background: #e5e7eb;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.08);
}

.game-action-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.game-action-icon {
  width: 22px;
  height: 22px;
  flex-shrink: 0;
}

.game-preview-card {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 18px 20px;
  margin-bottom: 24px;
  margin-left: 20px;
  margin-right: 20px;
  background: #fff;
  border: 1px solid #e5e7eb;
  border-radius: 14px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
}

.game-preview-img {
  max-width: 200px;
  max-height: 120px;
  width: auto;
  height: auto;
  object-fit: contain;
  border-radius: 8px;
  flex-shrink: 0;
  display: block;
}

.game-preview-text {
  margin: 0;
  font-size: 1rem;
  font-weight: 500;
  color: #374151;
}

.game-controls {
  display: flex;
  gap: 8px;
}

.btn {
  padding: 10px 18px;
  background: #0d9488;
  color: #fff;
  border: none;
  border-radius: 10px;
  cursor: pointer;
  font-size: 0.9375rem;
  font-weight: 500;
  transition: background 0.2s;
}

.btn:hover {
  background: #0f766e;
}

.btn:disabled {
  background: #d1d5db;
  color: #9ca3af;
  cursor: not-allowed;
}

.puzzle-container {
  width: 100%;
  max-width: 480px;
  margin-left: auto;
  margin-right: auto;
  position: relative;
  border-radius: 14px;
  overflow: visible;
  background: transparent;
  display: grid;
  gap: 6px;
}

.puzzle-piece {
  position: relative;
  border: none;
  cursor: pointer;
  transition:
    transform 0.2s ease,
    grid-row 0.3s ease,
    grid-column 0.3s ease;
  overflow: hidden;
  background: #f3f4f6;
  border-radius: 8px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.08);
}

.puzzle-piece img {
  width: 100%;
  height: 100%;
  display: block;
  border-radius: 6px;
}

.puzzle-piece:hover {
  transform: scale(1.02);
  z-index: 10;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.12);
}

.puzzle-piece.empty {
  background: #e5e7eb;
  cursor: default;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
}

.puzzle-piece.empty:hover {
  transform: none;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
}

.win-message {
  text-align: center;
  margin: 28px 20px 0;
  padding: 28px 24px;
  background: #ecfdf5;
  border-radius: 12px;
  border: 1px solid #a7f3d0;
}

.win-message h2 {
  color: #059669;
  margin-bottom: 16px;
  font-size: 1.25rem;
}

@media (max-width: 768px) {
  .container {
    padding: 20px 16px;
  }

  h1 {
    font-size: 1.5rem;
  }

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

  .game-header {
    flex-direction: column;
    text-align: center;
    margin-left: 12px;
    margin-right: 12px;
  }

  .game-header-left {
    text-align: center;
  }

  .game-header-actions {
    justify-content: center;
  }

  .game-preview-card {
    flex-direction: column;
    margin-left: 12px;
    margin-right: 12px;
    padding: 14px 12px;
  }

  .game-preview-img {
    max-width: 100%;
    max-height: 140px;
  }

  .win-message {
    margin-left: 12px;
    margin-right: 12px;
  }
}

@media (max-width: 640px) {
  .container {
    padding: 16px 12px;
  }

  .game-header {
    margin-left: 0;
    margin-right: 0;
  }

  .game-preview-card {
    margin-left: 0;
    margin-right: 0;
  }

  .win-message {
    margin-left: 0;
    margin-right: 0;
  }

  .puzzle-container {
    max-width: 100%;
  }

  .game-settings {
    margin: 0;
  }
}
