.mpj-app {
  --mpj-radius: 18px;
  --mpj-shadow: 0 14px 35px rgba(0,0,0,.10);
  --mpj-border: rgba(0,0,0,.10);
  --mpj-bg: #fff8fb;
  --mpj-card: #ffffff;
  --mpj-accent: #e85d9f;
  --mpj-accent-dark: #b93d79;
  --mpj-text: #2d2530;
  --mpj-muted: #746b75;
  --mpj-slot: rgba(232,93,159,.08);
}

.mpj-app, .mpj-app * { box-sizing: border-box; }
.mpj-app [hidden] { display: none !important; }
.mpj-app {
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--mpj-text);
  background: linear-gradient(180deg, #fff 0%, var(--mpj-bg) 100%);
}
.mpj-app button, .mpj-app select, .mpj-app input { font: inherit; }
.mpj-app button { cursor: pointer; }
.mpj-app button:disabled { cursor: not-allowed; opacity: .45; }

.mpj-app {
  width: calc(100vw - 24px);
  max-width: none;
  margin-left: calc(50% - 50vw + 12px);
  margin-right: calc(50% - 50vw + 12px);
  padding: 28px 0 56px;
}

.mpj-panel,
.mpj-preview-card,
.mpj-workspace,
.mpj-privacy {
  background: var(--mpj-card);
  border: 1px solid var(--mpj-border);
  border-radius: var(--mpj-radius);
  box-shadow: var(--mpj-shadow);
}

.mpj-setup {
  display: grid; grid-template-columns: minmax(330px, .9fr) minmax(0, 1.7fr);
  gap: 14px;
  align-items: end;
  padding: 18px;
}
.mpj-import-area { display: grid; gap: 10px; }
.mpj-settings-area { display: grid; grid-template-columns: minmax(150px,1fr) minmax(180px,1.15fr) minmax(220px,1.25fr); gap: 14px; align-items: end; }
.mpj-drop-zone { min-height: 116px; border: 2px dashed #379cec; border-radius: 14px; display: grid; place-content: center; justify-items: center; gap: 7px; color: #203044; background: #f9fcff; cursor: pointer; text-align: center; }
.mpj-drop-zone strong { font-size: 18px; }.mpj-drop-zone span { font-size: 14px; }
.mpj-field { display: grid; gap: 7px; font-weight: 700; min-width: 0; }
.mpj-piece-summary { align-self: end; min-height: 48px; display: grid; place-items: center start; color: var(--mpj-accent-dark); font-weight: 800; font-size: 14px; }
.mpj-field select { width: 100%; min-height: 48px; border: 1px solid var(--mpj-border); border-radius: 12px; padding: 0 12px; background: #fff; }
.mpj-file-button { min-height: 48px; display: inline-flex; align-items: center; justify-content: center; padding: 0 16px; border-radius: 12px; border: 2px dashed rgba(232,93,159,.35); background: rgba(232,93,159,.07); color: var(--mpj-accent-dark); font-weight: 800; }
.mpj-file-button input { position: absolute; opacity: 0; pointer-events: none; }
.mpj-file-button.mpj-drop-active { background: rgba(232,93,159,.18); border-color: var(--mpj-accent); transform: scale(1.01); }
.mpj-drop-zone.mpj-drop-active { background: #eaf6ff; border-color: #187bca; transform: scale(1.01); }
.mpj-primary { background: var(--mpj-accent) !important; color: white; border-color: transparent !important; font-weight: 800; }
.mpj-start-button { width: 100%; min-width: 230px; white-space: nowrap; }
.mpj-setup button, .mpj-actions button, .mpj-modal-actions button { min-height: 48px; border: 1px solid var(--mpj-border); border-radius: 12px; padding: 0 18px; background: #fff; }

.mpj-preview-wrap { margin-top: 16px; }
.mpj-preview-card { padding: 14px; display: grid; gap: 10px; justify-items: start; }
.mpj-preview-card span { font-size: 13px; font-weight: 800; color: var(--mpj-muted); }
.mpj-preview-card img { max-width: min(360px, 100%); max-height: 240px; border-radius: 12px; display: block; }

.mpj-stats { margin-top: 16px; display: grid; grid-template-columns: repeat(3, 1fr); gap: 1px; overflow: hidden; }
.mpj-stats > div { padding: 14px 18px; display: grid; gap: 4px; background: white; }
.mpj-stats span { color: var(--mpj-muted); font-size: 13px; font-weight: 700; }
.mpj-stats strong { font-size: 22px; }

.mpj-actions { display: flex; flex-wrap: wrap; gap: 10px; margin: 16px 0; }
.mpj-game { display: block; }
.mpj-workspace-wrap { min-width: 0; }
.mpj-section-row { display: flex; align-items: baseline; justify-content: space-between; gap: 16px; margin: 0 4px 8px; }
.mpj-section-title { font-weight: 900; }
.mpj-help { color: var(--mpj-muted); font-size: 13px; font-weight: 700; text-align: right; }

.mpj-workspace {
  position: relative;
  width: 100%;
  height: clamp(720px, 62vw, 960px);
  overflow: hidden;
  touch-action: pan-y;
  background:
    radial-gradient(circle at 15% 18%, rgba(255,255,255,.88) 0 2px, transparent 3px),
    radial-gradient(circle at 82% 76%, rgba(255,255,255,.7) 0 2px, transparent 3px),
    linear-gradient(135deg, rgba(232,93,159,.055), rgba(255,255,255,.8));
}
.mpj-workspace::before {
  content: "";
  position: absolute;
  inset: 12px;
  border: 1px dashed rgba(185,61,121,.18);
  border-radius: 14px;
  pointer-events: none;
}
.mpj-board-frame {
  position: absolute;
  z-index: 2;
  overflow: hidden;
  border-radius: 14px;
  border: 3px solid rgba(185,61,121,.30);
  background: rgba(255,255,255,.88);
  box-shadow: 0 12px 32px rgba(83,43,65,.13), 0 0 0 6px rgba(255,255,255,.72);
}
.mpj-board { width: 100%; height: 100%; display: grid; background: rgba(232,93,159,.10); border-radius: 10px; overflow: hidden; }
.mpj-board.mpj-triangle-board { display: block; position: relative; }
.mpj-slot { position: relative; background: var(--mpj-slot); overflow: hidden; outline: 1px dashed rgba(232,93,159,.28); outline-offset: -1px; }
.mpj-triangle-board .mpj-slot { position: absolute; }
.mpj-slot.mpj-correct { outline: none; }
.mpj-slot[data-shape="triangle"][data-dir="nw"], .mpj-slot[data-shape="triangle"][data-dir="nw"] img { clip-path: polygon(0 0, 100% 0, 0 100%); }
.mpj-slot[data-shape="triangle"][data-dir="se"], .mpj-slot[data-shape="triangle"][data-dir="se"] img { clip-path: polygon(100% 0, 100% 100%, 0 100%); }
.mpj-slot[data-shape="triangle"][data-dir="ne"], .mpj-slot[data-shape="triangle"][data-dir="ne"] img { clip-path: polygon(0 0, 100% 0, 100% 100%); }
.mpj-slot[data-shape="triangle"][data-dir="sw"], .mpj-slot[data-shape="triangle"][data-dir="sw"] img { clip-path: polygon(0 0, 0 100%, 100% 100%); }
.mpj-slot[data-shape="hexagon"], .mpj-slot[data-shape="hexagon"] img { clip-path: polygon(25% 0, 75% 0, 100% 50%, 75% 100%, 25% 100%, 0 50%); }
.mpj-slot img { width: 100%; height: 100%; object-fit: cover; display: block; pointer-events: none; user-select: none; }
.mpj-hint-image { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: fill; border-radius: 10px; z-index: 4; opacity: .88; pointer-events: none; }

.mpj-pieces-layer { position: absolute; inset: 0; z-index: 8; pointer-events: none; }
.mpj-piece {
  position: absolute;
  pointer-events: auto;
  border: 3px solid white;
  border-radius: 10px;
  padding: 0;
  overflow: hidden;
  background: #f4eef2;
  box-shadow: 0 8px 18px rgba(0,0,0,.20);
  touch-action: none;
  user-select: none;
  -webkit-user-drag: none;
  transition: box-shadow .12s ease, filter .12s ease;
}
.mpj-piece img { width: 100%; height: 100%; object-fit: cover; display: block; pointer-events: none; user-select: none; -webkit-user-drag: none; }
.mpj-piece:hover { box-shadow: 0 12px 26px rgba(0,0,0,.24); }
.mpj-piece:focus-visible { outline: 3px solid rgba(232,93,159,.45); outline-offset: 2px; }
.mpj-piece.mpj-dragging { box-shadow: 0 20px 46px rgba(0,0,0,.32); filter: saturate(1.05); }
.mpj-piece.mpj-wrong { animation: mpjWrong .28s linear; }
.mpj-piece[data-shape="triangle"] { border-radius: 0; }
.mpj-piece[data-shape="triangle"][data-dir="nw"] { clip-path: polygon(0 0, 100% 0, 0 100%); }
.mpj-piece[data-shape="triangle"][data-dir="se"] { clip-path: polygon(100% 0, 100% 100%, 0 100%); }
.mpj-piece[data-shape="triangle"][data-dir="ne"] { clip-path: polygon(0 0, 100% 0, 100% 100%); }
.mpj-piece[data-shape="triangle"][data-dir="sw"] { clip-path: polygon(0 0, 0 100%, 100% 100%); }
.mpj-piece[data-shape="hexagon"] { clip-path: polygon(25% 0, 75% 0, 100% 50%, 75% 100%, 25% 100%, 0 50%); border-radius: 0; }
@keyframes mpjWrong { 25% { translate: -6px 0; } 75% { translate: 6px 0; } }

.mpj-privacy { margin-top: 18px; padding: 14px 16px; color: #4f4751; font-size: 14px; }
.mpj-toast { position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 12px); background: #2f2730; color: #fff; padding: 10px 15px; border-radius: 999px; opacity: 0; pointer-events: none; transition: .18s ease; z-index: 10000; }
.mpj-toast.mpj-show { opacity: 1; transform: translate(-50%, 0); }
.mpj-sample-window { position: fixed; left: 24px; top: 110px; z-index: 9999; width: min(360px, calc(100vw - 32px)); background: #fff; border: 2px solid rgba(185,61,121,.35); border-radius: 14px; box-shadow: 0 18px 42px rgba(0,0,0,.25); overflow: hidden; }
.mpj-sample-handle { min-height: 42px; padding: 0 8px 0 14px; display: flex; align-items: center; justify-content: space-between; background: #fff0f7; cursor: grab; touch-action: none; }
.mpj-sample-handle button { width: 32px; height: 32px; border: 0; border-radius: 50%; background: transparent; font-size: 24px; line-height: 1; }
.mpj-sample-window img { display: block; width: 100%; max-height: 60vh; object-fit: contain; background: #fff; }

.mpj-modal { position: fixed; inset: 0; background: rgba(28,21,27,.58); display: grid; place-items: center; padding: 20px; z-index: 10001; }
.mpj-modal-card { width: min(560px, 100%); max-height: 92vh; overflow: auto; background: #fff; border-radius: 24px; padding: 22px; text-align: center; box-shadow: 0 30px 80px rgba(0,0,0,.30); }
.mpj-modal-card h2 { font-size: 36px; margin: 6px 0 14px; }
.mpj-modal-card img { max-width: 100%; max-height: 360px; border-radius: 16px; }
.mpj-confetti { font-size: 28px; letter-spacing: .12em; }
.mpj-result-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin: 16px 0; }
.mpj-result-grid > div { border: 1px solid var(--mpj-border); border-radius: 14px; padding: 12px; display: grid; gap: 4px; }
.mpj-result-grid span { color: var(--mpj-muted); font-size: 13px; font-weight: 700; }
.mpj-result-grid strong { font-size: 24px; }
.mpj-modal-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 10px; }

@media (max-width: 980px) {
  .mpj-setup { grid-template-columns: 1fr; }
  .mpj-settings-area { grid-template-columns: 1fr 1fr; }
  .mpj-start-button { grid-column: 1 / -1; min-width: 0; }
  .mpj-workspace { height: clamp(700px, 92vw, 900px); }
}

@media (max-width: 640px) {
  .mpj-app {
    width: calc(100vw - 16px);
    margin-left: calc(50% - 50vw + 8px);
    margin-right: calc(50% - 50vw + 8px);
    padding-top: 16px;
  }
  .mpj-setup { grid-template-columns: 1fr; }
  .mpj-settings-area { grid-template-columns: 1fr; }
  .mpj-start-button { grid-column: auto; }
  .mpj-stats { grid-template-columns: 1fr 1fr 1fr; }
  .mpj-stats > div { padding: 10px; }
  .mpj-stats strong { font-size: 18px; }
  .mpj-actions { display: grid; grid-template-columns: 1fr 1fr; }
  .mpj-actions button:first-child { grid-column: 1 / -1; }
  .mpj-section-row { display: grid; gap: 3px; }
  .mpj-help { text-align: left; }
  .mpj-workspace { height: 760px; }
  .mpj-piece { border-width: 2px; border-radius: 8px; }
}

/* v0.4.1 sample image picker */
.mpj-samples {
  margin-bottom: 16px;
  padding: 18px;
}
.mpj-samples-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 14px;
}
.mpj-samples-head > div { display: grid; gap: 3px; }
.mpj-samples-head strong { font-size: 18px; }
.mpj-samples-head span { color: var(--mpj-muted); font-size: 13px; }
.mpj-sample-count {
  flex: 0 0 auto;
  padding: 6px 10px;
  border-radius: 999px;
  background: rgba(232,93,159,.09);
  color: var(--mpj-accent-dark) !important;
  font-weight: 800;
}
.mpj-sample-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 280px));
  gap: 14px;
}
.mpj-sample-card {
  appearance: none;
  width: 100%;
  padding: 0;
  overflow: hidden;
  text-align: left;
  border: 2px solid transparent;
  border-radius: 15px;
  background: #fff;
  box-shadow: 0 8px 20px rgba(0,0,0,.08);
  transition: transform .14s ease, box-shadow .14s ease, border-color .14s ease;
}
.mpj-sample-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 12px 26px rgba(0,0,0,.12);
}
.mpj-sample-card.mpj-selected {
  border-color: var(--mpj-accent);
  box-shadow: 0 0 0 4px rgba(232,93,159,.12), 0 12px 26px rgba(0,0,0,.12);
}
.mpj-sample-card > img {
  display: block;
  width: 100%;
  aspect-ratio: 16 / 10;
  object-fit: cover;
  background: #f6f2f4;
}
.mpj-sample-card-body {
  display: grid;
  gap: 4px;
  padding: 12px 13px 13px;
}
.mpj-sample-card-body > strong { font-size: 16px; }
.mpj-sample-card-body > small { color: var(--mpj-muted); font-size: 12px; }
.mpj-sample-use {
  margin-top: 5px;
  color: var(--mpj-accent-dark);
  font-weight: 800;
  font-size: 13px;
}
.mpj-sample-card.mpj-selected .mpj-sample-use::before { content: "✓ "; }
.mpj-source-title {
  font-weight: 900;
  font-size: 16px;
}

@media (max-width: 640px) {
  .mpj-samples-head { align-items: flex-start; }
  .mpj-sample-grid { grid-template-columns: 1fr; }
}

/* v0.5.0 DB image library */
.mpj-library {
  margin-bottom: 16px;
  padding: 18px;
}
.mpj-library-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 14px;
}
.mpj-library-head > div { display: grid; gap: 3px; }
.mpj-library-head strong { font-size: 18px; }
.mpj-library-head span { color: var(--mpj-muted); font-size: 13px; }
.mpj-db-badge {
  flex: 0 0 auto;
  padding: 6px 10px;
  border-radius: 999px;
  background: rgba(232,93,159,.09);
  color: var(--mpj-accent-dark) !important;
  font-size: 11px !important;
  font-weight: 900;
  letter-spacing: .06em;
}
.mpj-category-strip {
  display: flex;
  flex-wrap: wrap;
  gap: 9px;
  min-height: 44px;
}
.mpj-category-chip {
  appearance: none;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 40px;
  padding: 8px 12px;
  border: 1px solid var(--mpj-border);
  border-radius: 999px;
  background: #fff;
  color: #3f3540;
  font-weight: 800;
  box-shadow: 0 3px 10px rgba(0,0,0,.04);
  transition: transform .12s ease, border-color .12s ease, background .12s ease, box-shadow .12s ease;
}
.mpj-category-chip:hover {
  transform: translateY(-1px);
  border-color: rgba(232,93,159,.45);
}
.mpj-category-chip.mpj-active {
  border-color: var(--mpj-accent);
  background: #fff0f7;
  box-shadow: 0 0 0 3px rgba(232,93,159,.10);
}
.mpj-category-chip small {
  padding: 2px 7px;
  border-radius: 999px;
  background: rgba(0,0,0,.045);
  color: var(--mpj-muted);
  font-size: 10px;
  font-weight: 800;
}
.mpj-library-loading {
  display: inline-flex;
  align-items: center;
  min-height: 40px;
  color: var(--mpj-muted);
  font-size: 13px;
  font-weight: 700;
}

.mpj-library-selection {
  display: grid;
  grid-template-columns: minmax(250px, 340px) minmax(0, 1fr);
  gap: 16px;
  align-items: stretch;
  margin-top: 16px;
}
.mpj-selected-image-card,
.mpj-sample-carousel {
  border: 1px solid var(--mpj-border);
  border-radius: 16px;
  background: #fff;
  box-shadow: 0 8px 22px rgba(0,0,0,.045);
}
.mpj-selected-image-card {
  padding: 14px;
  display: grid;
  gap: 10px;
  align-content: start;
}
.mpj-selected-image-card > span {
  color: var(--mpj-muted);
  font-size: 13px;
  font-weight: 800;
}
.mpj-selected-image-card img {
  width: 100%;
  max-width: none;
  max-height: 230px;
  aspect-ratio: 16 / 10;
  object-fit: cover;
  border-radius: 12px;
}
.mpj-preview-placeholder {
  min-height: 170px;
  display: grid;
  place-items: center;
  padding: 20px;
  border: 1px dashed rgba(232,93,159,.35);
  border-radius: 12px;
  background: linear-gradient(135deg, rgba(232,93,159,.04), rgba(255,255,255,.85));
  color: var(--mpj-muted);
  text-align: center;
  font-weight: 800;
}
.mpj-selected-title {
  min-height: 24px;
  font-size: 15px;
}
.mpj-sample-carousel {
  min-width: 0;
  padding: 14px;
}
.mpj-carousel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
}
.mpj-carousel-head strong { font-size: 16px; }
.mpj-carousel-head span {
  flex: 0 0 auto;
  padding: 4px 9px;
  border-radius: 999px;
  background: rgba(232,93,159,.09);
  color: var(--mpj-accent-dark);
  font-size: 11px;
  font-weight: 900;
}
.mpj-carousel-row {
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr) 44px;
  gap: 10px;
  align-items: center;
}
.mpj-carousel-arrow {
  width: 44px;
  height: 100%;
  min-height: 150px;
  padding: 0;
  border: 1px solid var(--mpj-border);
  border-radius: 12px;
  background: #fff7fb;
  color: var(--mpj-accent-dark);
  font-size: 28px;
  font-weight: 900;
}
.mpj-carousel-arrow:disabled {
  opacity: .28;
  cursor: default;
}
.mpj-sample-strip {
  min-width: 0;
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 10px;
  align-items: stretch;
}
.mpj-sample-strip .mpj-sample-card {
  min-width: 0;
  border-radius: 12px;
}
.mpj-sample-strip .mpj-sample-card > img {
  aspect-ratio: 16 / 10;
}
.mpj-sample-strip .mpj-sample-card-body {
  padding: 8px 9px 9px;
  gap: 2px;
}
.mpj-sample-strip .mpj-sample-card-body > strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 12px;
}
.mpj-sample-strip .mpj-sample-card-body > small { font-size: 10px; }
.mpj-sample-strip .mpj-sample-use {
  margin-top: 3px;
  font-size: 10px;
}

@media (max-width: 1180px) {
  .mpj-sample-strip { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}

@media (max-width: 980px) {
  .mpj-library-selection { grid-template-columns: 1fr; }
  .mpj-selected-image-card { grid-template-columns: minmax(180px, 280px) 1fr; align-items: start; }
  .mpj-selected-image-card > span,
  .mpj-selected-image-card > strong { grid-column: 1 / -1; }
  .mpj-preview-placeholder { min-height: 140px; }
}

@media (max-width: 640px) {
  .mpj-library-head { align-items: flex-start; }
  .mpj-db-badge { display: none; }
  .mpj-category-strip { flex-wrap: nowrap; overflow-x: auto; padding-bottom: 4px; scrollbar-width: thin; }
  .mpj-category-chip { flex: 0 0 auto; }
  .mpj-selected-image-card { display: grid; grid-template-columns: 1fr; }
  .mpj-carousel-row { grid-template-columns: 34px minmax(0, 1fr) 34px; gap: 6px; }
  .mpj-carousel-arrow { width: 34px; min-height: 132px; font-size: 22px; }
  .mpj-sample-strip { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 7px; }
}
