:root {
  --ink: #172033;
  --muted: #596579;
  --paper: #fff7dc;
  --white: #ffffff;
  --sky: #5bb7dc;
  --deep-sky: #2477a5;
  --leaf: #3f9a61;
  --sun: #f3c34d;
  --coral: #ef8068;
  --grape: #8265d9;
  --mint: #9be6b4;
  --line: rgba(23, 32, 51, 0.14);
  --panel: rgba(255, 255, 255, 0.88);
  --board: #bfefff;
  --grid: #5bb7dc;
  --field: #9dd50c;
  --beam-field: #64c9a3;
  --corner-field: #bfdc33;
  --porch-field: #3e9f3f;
  --brown: #8b5a43;
  --red: #ce4c33;
  --yellow: #f0c22e;
  --shadow: 0 18px 45px rgba(23, 32, 51, 0.12);
  --display-font: "Chalkboard SE", "Marker Felt", "Trebuchet MS", Arial, sans-serif;
  --body-font: "Trebuchet MS", Arial, Helvetica, sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  min-height: 100vh;
  margin: 0;
  color: var(--ink);
  font-family: var(--body-font);
  background:
    linear-gradient(160deg, rgba(91, 183, 220, 0.2), rgba(243, 195, 77, 0.18) 46%, rgba(155, 230, 180, 0.2)),
    var(--paper);
}

button,
select {
  font: inherit;
  touch-action: manipulation;
  -webkit-tap-highlight-color: rgba(91, 183, 220, 0.2);
}

button {
  -webkit-user-select: none;
  user-select: none;
}

a {
  color: inherit;
}

.site-header {
  position: sticky;
  top: 0;
  z-index: 10;
  background: rgba(255, 250, 240, 0.92);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(14px);
}

.nav {
  width: min(1120px, calc(100% - 28px));
  min-height: 64px;
  margin: 0 auto;
  display: flex;
  gap: 16px;
  align-items: center;
  justify-content: space-between;
}

.brand {
  display: inline-flex;
  gap: 9px;
  align-items: center;
  font-family: var(--display-font);
  font-size: 20px;
  font-weight: 800;
  text-decoration: none;
  white-space: nowrap;
}

.brand::before {
  content: "";
  width: 14px;
  height: 14px;
  border: 3px solid var(--deep-sky);
  border-radius: 50%;
  background: var(--sun);
  box-shadow: 7px 7px 0 var(--coral);
  transform: rotate(-8deg);
}

.nav-links {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  justify-content: flex-end;
}

.nav-links a {
  min-height: 40px;
  display: inline-flex;
  align-items: center;
  padding: 8px 13px;
  border: 1px solid rgba(23, 32, 51, 0.12);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.58);
  box-shadow: 0 4px 0 rgba(23, 32, 51, 0.08);
  font-size: 14px;
  font-weight: 800;
  text-decoration: none;
}

.nav-links a:nth-child(1) {
  background: #fff0b8;
}

.nav-links a:nth-child(2) {
  background: #dff5ff;
}

.nav-links a:nth-child(3) {
  background: #ffe1d8;
}

.nav-links a:nth-child(4) {
  background: #e3f8cf;
}

.nav-links a:hover,
.nav-links a.active {
  background: var(--white);
  border-color: var(--line);
  box-shadow: 0 2px 0 rgba(23, 32, 51, 0.14);
  transform: translateY(2px);
}

.page-shell {
  width: min(1120px, calc(100% - 32px));
  margin: 0 auto;
  padding: 44px 0 72px;
}

.page-hero {
  max-width: 820px;
  padding: 28px 0 24px;
}

.page-hero h1 {
  position: relative;
  max-width: 760px;
  margin: 0 0 18px;
  color: var(--ink);
  font-family: var(--display-font);
  font-size: 58px;
  line-height: 1.02;
  letter-spacing: 0;
  text-shadow:
    3px 3px 0 rgba(243, 195, 77, 0.48),
    6px 6px 0 rgba(91, 183, 220, 0.2);
}

.page-hero h1::after {
  content: "";
  display: block;
  width: min(180px, 54%);
  height: 10px;
  margin-top: 10px;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--coral), var(--sun), var(--sky));
}

.page-hero p {
  max-width: 760px;
  margin: 0;
  color: #3d4a5d;
  font-size: 20px;
  line-height: 1.64;
}

.inspiration-note {
  margin: 4px 0 28px;
  padding: 14px 16px;
  border-left: 7px solid var(--coral);
  background: rgba(255, 255, 255, 0.68);
  color: var(--muted);
  font-size: 15px;
  line-height: 1.55;
}

.inspiration-note a {
  color: var(--deep-sky);
  font-weight: 800;
}

.rules-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 14px;
  margin: 4px 0 24px;
}

.rule-card {
  min-height: 150px;
  padding: 16px;
  border: 2px solid rgba(23, 32, 51, 0.1);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.88);
  box-shadow: 0 10px 0 rgba(23, 32, 51, 0.06);
}

.rule-card:nth-child(1) {
  border-top-color: var(--sun);
}

.rule-card:nth-child(2) {
  border-top-color: var(--sky);
}

.rule-card:nth-child(3) {
  border-top-color: var(--leaf);
}

.rule-card:nth-child(4) {
  border-top-color: var(--coral);
}

.rule-card strong {
  display: block;
  margin-bottom: 8px;
  font-family: var(--display-font);
  font-size: 21px;
}

.rule-card p {
  margin: 0;
  color: var(--muted);
  font-size: 15px;
  line-height: 1.48;
}

.tap-demo {
  display: flex;
  flex-wrap: wrap;
  gap: 7px;
  margin-top: 12px;
}

.tap-demo span {
  display: inline-flex;
  min-height: 28px;
  align-items: center;
  padding: 5px 9px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #ffffff;
  box-shadow: 0 3px 0 rgba(23, 32, 51, 0.08);
  color: var(--deep-sky);
  font-size: 13px;
  font-weight: 800;
}

.tap-demo span:nth-child(2) {
  color: var(--leaf);
}

.tap-demo span:nth-child(3) {
  color: var(--coral);
}

.shell {
  display: grid;
  grid-template-columns: minmax(300px, 680px) minmax(320px, 360px);
  gap: 20px;
  align-items: start;
  margin: 0 auto;
}

.game-stage,
.side-panel {
  background: var(--panel);
  border: 2px solid rgba(23, 32, 51, 0.1);
  border-radius: 8px;
  box-shadow: 0 10px 0 rgba(23, 32, 51, 0.06);
}

.game-stage {
  position: relative;
  padding: clamp(16px, 2.4vw, 28px);
}

.side-panel {
  display: grid;
  gap: 18px;
  padding: 18px;
}

.play-controls {
  display: grid;
  gap: 18px;
}

.topbar,
.status-row,
.level-controls {
  display: flex;
  gap: 12px;
  align-items: center;
  justify-content: space-between;
}

.eyebrow,
#status,
#solutionCount {
  margin: 0;
  color: var(--muted);
  font-size: 0.88rem;
}

.eyebrow {
  display: inline-flex;
  gap: 8px;
  align-items: center;
  margin: 0 0 18px;
  padding: 8px 12px;
  border: 1px solid rgba(23, 32, 51, 0.16);
  border-radius: 999px;
  background: #ffffff;
  box-shadow: 0 4px 0 rgba(91, 183, 220, 0.18);
  color: var(--deep-sky);
  font-size: 14px;
  font-weight: 800;
}

#levelTitle {
  margin: 2px 0 0;
  color: var(--ink);
  font-family: var(--display-font);
  font-size: 40px;
  line-height: 1;
  letter-spacing: 0;
  text-shadow: 2px 2px 0 rgba(243, 195, 77, 0.32);
}

.scorebox {
  display: grid;
  gap: 5px;
  justify-items: end;
  min-width: 92px;
}

#modeBadge {
  display: inline-flex;
  align-items: center;
  min-height: 30px;
  padding: 0 12px;
  color: #295133;
  background: #e3f8cf;
  border: 1px solid rgba(67, 125, 50, 0.18);
  border-radius: 999px;
  font-weight: 800;
  box-shadow: 0 3px 0 rgba(23, 32, 51, 0.08);
}

#modeBadge.night {
  color: #fff;
  background: #2f4158;
}

.board-wrap {
  display: grid;
  place-items: center;
  padding: 20px 0;
}

.board {
  display: grid;
  grid-template-columns: repeat(4, minmax(58px, 116px));
  grid-template-rows: repeat(4, minmax(58px, 116px));
  gap: 7px;
  width: min(100%, 520px);
  aspect-ratio: 1;
  padding: 18px;
  background: var(--board);
  border: 8px solid rgba(91, 183, 220, 0.45);
  border-radius: 8px;
  box-shadow:
    0 18px 38px rgba(36, 119, 165, 0.16),
    10px 10px 0 rgba(239, 128, 104, 0.16);
}

.cell {
  position: relative;
  display: grid;
  place-items: center;
  min-width: 0;
  padding: 0;
  overflow: hidden;
  background: rgba(222, 246, 250, 0.92);
  border: 3px solid var(--grid);
  border-radius: 8px;
  cursor: pointer;
}

.cell:hover {
  outline: 3px solid rgba(36, 49, 61, 0.18);
}

.cell-missing {
  visibility: hidden;
  pointer-events: none;
}

.occupied {
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.18), rgba(33, 82, 34, 0.1)),
    var(--piece-field, var(--field));
  border-color: rgba(255, 255, 255, 0.7);
}

.piece-brown.house-cell::before,
.piece-red.house-cell::before,
.piece-yellow.house-cell::before {
  content: "";
  position: absolute;
  inset: 16% 13% 13%;
  z-index: 1;
  background-color: transparent;
  background-image: var(--house-asset);
  background-position: center;
  background-repeat: no-repeat;
  background-size: contain;
  filter: drop-shadow(0 8px 12px rgba(44, 38, 29, 0.18));
}

.piece-brown {
  --house-color: var(--brown);
  --house-asset: url("assets/board-house-brown.png");
  --piece-field: var(--beam-field);
}

.piece-red {
  --house-color: var(--red);
  --house-asset: url("assets/board-house-red.png");
  --piece-field: var(--porch-field);
}

.piece-yellow {
  --house-color: var(--yellow);
  --house-asset: url("assets/board-house-yellow.png");
  --piece-field: var(--corner-field);
}

.pig-token,
.wolf-token {
  position: relative;
  z-index: 4;
  display: block;
  width: 78%;
  aspect-ratio: 292 / 326;
  border-radius: 8px;
  background-position: center;
  background-repeat: no-repeat;
  background-size: contain;
}

.pig-token {
  background-image: url("assets/board-pig.png");
  filter: drop-shadow(0 7px 9px rgba(123, 66, 88, 0.18));
}

.wolf-token {
  background-image: url("assets/board-wolf.png");
  filter: drop-shadow(0 7px 9px rgba(54, 62, 70, 0.18));
}

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

.piece-card {
  display: grid;
  gap: 9px;
  justify-items: center;
  min-width: 0;
  min-height: 136px;
  padding: 12px 4px;
  color: var(--ink);
  background: #ffffff;
  border: 2px solid rgba(23, 32, 51, 0.1);
  border-radius: 8px;
  box-shadow: 0 5px 0 rgba(23, 32, 51, 0.08);
  cursor: pointer;
}

.piece-card.selected {
  background: #dff5ff;
  border-color: rgba(36, 119, 165, 0.24);
  box-shadow:
    inset 0 0 0 2px rgba(91, 183, 220, 0.22),
    0 3px 0 rgba(23, 32, 51, 0.12);
}

.piece-name {
  font-weight: 800;
  font-size: 0.9rem;
}

.piece-preview {
  position: relative;
  display: block;
  width: 72px;
  height: 72px;
}

.mini-cell {
  position: absolute;
  width: 22px;
  height: 22px;
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.22), rgba(33, 82, 34, 0.08)),
    var(--piece-field, var(--field));
  border: 2px solid rgba(255, 255, 255, 0.68);
  border-radius: 5px;
}

.mini-house {
  background: var(--house-color);
  background-image: var(--house-asset);
  background-position: center;
  background-repeat: no-repeat;
  background-size: 160%;
}

.asset-house-brown {
  --house-asset: url("assets/house-brown.png");
}

.asset-house-red {
  --house-asset: url("assets/house-red.png");
}

.asset-house-yellow {
  --house-asset: url("assets/house-yellow.png");
}

.controls,
.level-controls {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.level-controls {
  grid-template-columns: minmax(0, 0.8fr) minmax(0, 1.5fr) minmax(0, 0.8fr);
}

button,
select {
  min-width: 0;
  min-height: 44px;
  padding: 0 12px;
  color: var(--ink);
  background: #ffffff;
  border: 1px solid var(--line);
  border-radius: 999px;
  box-shadow: 0 4px 0 rgba(23, 32, 51, 0.08);
  font-weight: 800;
}

button:hover {
  background: #dff5ff;
  transform: translateY(1px);
  box-shadow: 0 3px 0 rgba(23, 32, 51, 0.1);
}

select {
  width: 100%;
  min-width: 0;
  padding: 0 8px;
  border-radius: 8px;
  font-weight: 700;
}

button:disabled {
  color: #9aa5ad;
  background: #eef1f2;
  box-shadow: none;
  cursor: not-allowed;
  transform: none;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  color: var(--muted);
  font-size: 0.88rem;
}

.legend span {
  display: inline-flex;
  gap: 7px;
  align-items: center;
}

.legend i {
  display: inline-block;
  width: 24px;
  height: 24px;
  background-position: center;
  background-repeat: no-repeat;
  background-size: contain;
}

.legend-pig {
  background-image: url("assets/pig.png");
}

.legend-wolf {
  background-image: url("assets/wolf.png");
}

.legend-house {
  background-image: url("assets/house-red.png");
}

.success-burst {
  position: absolute;
  inset: 50% auto auto 50%;
  z-index: 20;
  display: grid;
  gap: 4px;
  min-width: min(280px, calc(100% - 36px));
  padding: 18px 22px;
  text-align: center;
  color: #163447;
  pointer-events: none;
  opacity: 0;
  background: rgba(255, 255, 255, 0.92);
  border: 2px solid rgba(68, 151, 87, 0.36);
  border-radius: 8px;
  box-shadow: 0 22px 50px rgba(27, 54, 73, 0.22);
  transform: translate(-50%, -50%) scale(0.92);
}

.success-burst strong {
  font-size: 48px;
  line-height: 1;
}

.success-burst span {
  color: var(--muted);
  font-weight: 700;
}

.success-burst.show {
  animation: success-pop 1.6s ease both;
}

.success-burst.show::before,
.success-burst.show::after {
  content: "";
  position: absolute;
  inset: -16px;
  z-index: -1;
  border: 3px solid rgba(157, 213, 12, 0.55);
  border-radius: 12px;
  animation: success-ring 1.4s ease-out both;
}

.site-footer {
  width: min(1120px, calc(100% - 32px));
  margin: 0 auto;
  padding: 28px 0 48px;
  color: var(--ink);
  font-size: 15px;
}

.footer-badge {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  border: 1px solid rgba(23, 32, 51, 0.14);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.74);
  box-shadow: 0 5px 0 rgba(23, 32, 51, 0.08);
  font-family: var(--display-font);
  font-weight: 800;
}

.footer-badge::before,
.footer-badge::after {
  content: "";
  width: 11px;
  height: 11px;
  border-radius: 50%;
}

.footer-badge::before {
  background: var(--coral);
  box-shadow: 7px 0 0 var(--sun);
}

.footer-badge::after {
  background: var(--sky);
  box-shadow: 7px 0 0 var(--leaf);
}

.success-burst.show::after {
  border-color: rgba(238, 120, 169, 0.45);
  animation-delay: 0.12s;
}

@keyframes success-pop {
  0% {
    opacity: 0;
    transform: translate(-50%, -50%) scale(0.86);
  }
  12%, 78% {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1);
  }
  100% {
    opacity: 0;
    transform: translate(-50%, -50%) scale(1.04);
  }
}

@keyframes success-ring {
  0% {
    opacity: 0.85;
    transform: scale(0.84);
  }
  100% {
    opacity: 0;
    transform: scale(1.36);
  }
}

@media (max-width: 820px) {
  .site-header {
    position: static;
  }

  .nav {
    align-items: flex-start;
    padding: 10px 0;
    flex-direction: column;
  }

  .nav-links {
    justify-content: flex-start;
  }

  .page-shell {
    width: min(100vw - 20px, 620px);
    padding: 28px 0 48px;
  }

  .page-hero h1 {
    font-size: 40px;
  }

  #levelTitle {
    font-size: 34px;
  }

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

  .shell {
    grid-template-columns: 1fr;
    width: 100%;
    margin: 10px auto;
  }

  .side-panel {
    display: contents;
  }

  .play-controls {
    position: sticky;
    top: max(8px, env(safe-area-inset-top));
    z-index: 9;
    order: 2;
    align-self: start;
    gap: 8px;
    padding: 10px;
    border: 2px solid rgba(23, 32, 51, 0.12);
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.97);
    box-shadow: 0 8px 0 rgba(23, 32, 51, 0.08);
    backdrop-filter: blur(12px);
  }

  .play-controls .controls {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 8px;
  }

  .play-controls .level-controls {
    gap: 8px;
    padding-top: 2px;
  }

  .play-controls .piece-card {
    min-height: 105px;
    gap: 3px;
    padding: 6px 2px;
  }

  .game-stage {
    order: 3;
    padding: 14px;
  }

  .legend {
    order: 4;
    padding: 4px 10px;
  }

  .board {
    gap: 5px;
    padding: 12px;
    border-width: 6px;
  }

  .topbar,
  .status-row {
    align-items: flex-start;
  }
}

@media (max-width: 520px) {
  .page-hero h1 {
    font-size: 34px;
  }

  .page-hero p {
    font-size: 17px;
  }

  .success-burst strong {
    font-size: 36px;
  }

  .piece-tray {
    gap: 8px;
  }

  .piece-card {
    min-height: 105px;
  }

  .controls {
    gap: 8px;
  }
}
