:root {
  --board-bg: #2b2118;
  --board-border: #1a130d;
  --gap: 8px;
  --radius: 10px;
  --cell: 92px; /* JS keeps this in sync with actual size */
  --accent: #d64541;
  --accent-dark: #a8302c;
  --wood: #c69c6d;
  --wood-dark: #9c7748;
  --soldier: #6c8ebf;
  --soldier-dark: #4a6a96;
  --general: #82a36b;
  --general-dark: #5f7e49;
  --text: #f4ece2;
  --muted: #b6a994;
  --bg: #f4f0e8;
  --panel: #ffffff;
  --shadow: 0 10px 30px rgba(40, 28, 16, 0.18);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, Helvetica, Arial, sans-serif;
  background: radial-gradient(circle at 50% -10%, #fffdf8, var(--bg) 60%);
  color: #2b2118;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding: 32px 16px 64px;
}

.app {
  width: 100%;
  max-width: 460px;
}

.app__header { text-align: center; margin-bottom: 20px; }

.app__title {
  margin: 0;
  font-size: 2.4rem;
  letter-spacing: 0.06em;
  font-weight: 800;
  color: var(--accent-dark);
}

.app__subtitle { margin: 6px 0 0; color: #6b5d4a; font-size: 0.95rem; }

.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  background: var(--panel);
  border-radius: 14px;
  padding: 12px 14px;
  box-shadow: var(--shadow);
  margin-bottom: 18px;
}

.stat {
  display: flex;
  flex-direction: column;
  line-height: 1.1;
  min-width: 52px;
}

.stat__label {
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}

.stat__value {
  font-size: 1.3rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.select { display: flex; flex-direction: column; gap: 2px; margin-left: auto; }

.select__label {
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}

#layoutSelect {
  border: 1px solid #e2d9c8;
  border-radius: 8px;
  padding: 6px 8px;
  font-size: 0.85rem;
  background: #fffdf8;
  cursor: pointer;
}

.btn {
  border: none;
  border-radius: 9px;
  padding: 9px 14px;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  background: #efe7d8;
  color: #5a4b38;
  transition: transform 0.08s ease, background 0.15s ease, box-shadow 0.15s ease;
}

.btn:hover { background: #e6dcc8; }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.45; cursor: default; }

.btn--primary {
  background: var(--accent);
  color: #fff;
  box-shadow: 0 4px 12px rgba(214, 69, 65, 0.35);
}
.btn--primary:hover { background: var(--accent-dark); }

.level-banner {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  margin: 0 0 14px;
  text-align: center;
}

.level-banner__main {
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--accent-dark);
  letter-spacing: 0.02em;
}

.level-banner__sub {
  font-size: 0.78rem;
  color: #8a7c66;
  min-height: 1em;
}

.board-wrap {
  position: relative;
  display: flex;
  justify-content: center;
}

.board {
  position: relative;
  /* content-box so the JS-set width/height describe the play area itself,
     with the frame border drawn outside it. Otherwise the global
     border-box rule would shrink the play area by the border width and
     push the pieces off-center. */
  box-sizing: content-box;
  background: var(--board-bg);
  border: var(--gap) solid var(--board-border);
  border-radius: var(--radius);
  box-shadow: var(--shadow), inset 0 0 30px rgba(0, 0, 0, 0.45);
  touch-action: none;
}

/* exit gate at the bottom middle */
.board__exit {
  position: absolute;
  bottom: calc(var(--gap) * -1 - 6px);
  left: 50%;
  transform: translateX(-50%);
  width: calc(var(--cell) * 2 + var(--gap));
  height: 6px;
  background: linear-gradient(90deg, transparent, var(--accent) 20%, var(--accent) 80%, transparent);
  border-radius: 4px;
}

.piece {
  position: absolute;
  border-radius: 8px;
  cursor: grab;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  color: var(--text);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.35);
  user-select: none;
  transition: left 0.16s cubic-bezier(0.22, 0.61, 0.36, 1),
              top 0.16s cubic-bezier(0.22, 0.61, 0.36, 1);
  box-shadow: inset 0 2px 0 rgba(255, 255, 255, 0.18),
              inset 0 -4px 8px rgba(0, 0, 0, 0.28),
              0 4px 10px rgba(0, 0, 0, 0.3);
}

.piece:active { cursor: grabbing; }
.piece.dragging { transition: none; z-index: 5; }

.piece--general {
  background: linear-gradient(160deg, var(--accent), var(--accent-dark));
  font-size: 1rem;
}
.piece--v {
  background: linear-gradient(160deg, var(--soldier), var(--soldier-dark));
}
.piece--h {
  background: linear-gradient(160deg, var(--general), var(--general-dark));
}
.piece--s {
  background: linear-gradient(160deg, var(--wood), var(--wood-dark));
  color: #4a3a22;
  text-shadow: none;
}

.piece__label {
  font-size: 0.8rem;
  opacity: 0.92;
  text-align: center;
  padding: 0 4px;
  pointer-events: none;
}

.win-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(33, 24, 14, 0.55);
  backdrop-filter: blur(3px);
  border-radius: var(--radius);
  z-index: 10;
  animation: fade 0.25s ease;
}

/* When hidden, the overlay must not show despite the display:flex above. */
.win-overlay[hidden] { display: none; }

@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

.win-card {
  background: var(--panel);
  border-radius: 16px;
  padding: 28px 32px;
  text-align: center;
  box-shadow: var(--shadow);
  animation: pop 0.3s cubic-bezier(0.18, 0.89, 0.32, 1.28);
}

@keyframes pop {
  from { transform: scale(0.8); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

.win-card__title { margin: 0 0 6px; color: var(--accent-dark); font-size: 1.25rem; }

.win-card__stars {
  font-size: 2rem;
  letter-spacing: 0.1em;
  color: #e8c34a;
  margin: 2px 0 6px;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.15);
  animation: starpop 0.4s cubic-bezier(0.18, 0.89, 0.32, 1.28);
}

@keyframes starpop {
  from { transform: scale(0.4); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

.win-card__stats {
  margin: 0 0 12px;
  color: #6b5d4a;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.win-card__badges {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: center;
  margin: 0 0 12px;
}

.badge {
  font-size: 0.78rem;
  font-weight: 600;
  padding: 4px 9px;
  border-radius: 999px;
  background: #efe7d8;
  color: #5a4b38;
  white-space: nowrap;
}

.badge--new {
  background: var(--accent);
  color: #fff;
  animation: starpop 0.4s cubic-bezier(0.18, 0.89, 0.32, 1.28);
}

.win-card__best {
  margin: 0 0 16px;
  font-size: 0.85rem;
  color: #8a7c66;
  min-height: 1em;
}

.confetti-canvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 50;
}

.app__footer {
  margin-top: 22px;
  text-align: center;
  font-size: 0.82rem;
  color: #8a7c66;
  line-height: 1.5;
}

@media (max-width: 480px) {
  .app__title { font-size: 2rem; }
  .select { margin-left: 0; }
}
