@import url('https://fonts.googleapis.com/css2?family=DM+Sans:wght@400;500;600;700&family=Fraunces:opsz,wght@9..144,650;9..144,800&display=swap');

:root {
  --ink: #322822;
  --muted: #76685f;
  --cream: #fff8ef;
  --paper: #fffdf8;
  --pink: #ef6f8d;
  --pink-dark: #d94f72;
  --brown: #633d2d;
  --line: #5d4c43;
  --shadow: 0 22px 60px rgba(99, 61, 45, .16);
}

* { box-sizing: border-box; }

html { min-height: 100%; background: var(--cream); }

body {
  min-height: 100vh;
  margin: 0;
  color: var(--ink);
  font-family: "DM Sans", "PingFang HK", "Noto Sans TC", sans-serif;
  background:
    radial-gradient(circle at 12% 18%, rgba(239, 111, 141, .13), transparent 23rem),
    radial-gradient(circle at 92% 82%, rgba(240, 190, 75, .13), transparent 25rem),
    var(--cream);
}

button, input { font: inherit; }
button { color: inherit; }

.topbar {
  width: min(1180px, calc(100% - 40px));
  height: 82px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid rgba(93, 76, 67, .18);
}

.wordmark {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: var(--brown);
  text-decoration: none;
  font-family: "Fraunces", Georgia, serif;
  font-size: 25px;
  font-weight: 800;
  letter-spacing: -.03em;
}

.brand-pig {
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  border: 2px solid var(--brown);
  border-radius: 50%;
  background: #ffd5df;
  font-size: 24px;
  box-shadow: 3px 3px 0 var(--brown);
}

.text-button {
  min-height: 42px;
  padding: 8px 16px;
  border: 1px solid rgba(93, 76, 67, .34);
  border-radius: 999px;
  background: transparent;
  cursor: pointer;
  font-weight: 700;
}

.text-button:hover { background: rgba(255, 255, 255, .6); }

.game-shell {
  width: min(1180px, calc(100% - 40px));
  min-height: calc(100vh - 82px);
  margin: 0 auto;
  padding: 54px 0 70px;
  display: grid;
  grid-template-columns: minmax(300px, .8fr) minmax(440px, 1fr);
  gap: clamp(48px, 8vw, 112px);
  align-items: center;
}

.game-copy { align-self: center; }

.eyebrow {
  margin: 0 0 18px;
  color: var(--pink-dark);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: .16em;
}

h1, h2 { font-family: "Fraunces", Georgia, serif; }

h1 {
  margin: 0;
  color: var(--brown);
  font-size: clamp(48px, 6.2vw, 82px);
  line-height: .98;
  letter-spacing: -.055em;
}

h1 em {
  color: var(--pink-dark);
  font-style: normal;
}

.lede {
  max-width: 500px;
  margin: 28px 0 32px;
  color: var(--muted);
  font-size: 17px;
  line-height: 1.75;
}

.level-tabs {
  display: flex;
  gap: 8px;
  margin-bottom: 28px;
}

.level-tabs button {
  min-width: 72px;
  min-height: 42px;
  padding: 8px 14px;
  border: 1px solid rgba(93, 76, 67, .28);
  border-radius: 12px;
  background: rgba(255, 255, 255, .5);
  cursor: pointer;
  font-weight: 700;
}

.level-tabs button.active {
  border-color: var(--brown);
  background: var(--brown);
  color: #fff;
}

.rules-mini {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 150px));
  gap: 12px 20px;
  color: var(--muted);
  font-size: 13px;
}

.rules-mini span { display: flex; align-items: center; gap: 9px; }
.rules-mini b {
  display: grid;
  place-items: center;
  width: 25px;
  height: 25px;
  border-radius: 50%;
  background: #ffd7e0;
  color: var(--brown);
}

.play-panel {
  width: min(100%, 620px);
  justify-self: end;
  padding: clamp(20px, 4vw, 34px);
  border: 1px solid rgba(93, 76, 67, .2);
  border-radius: 26px;
  background: rgba(255, 253, 248, .9);
  box-shadow: var(--shadow);
}

.game-status {
  display: flex;
  justify-content: space-between;
  gap: 24px;
  align-items: flex-end;
  margin-bottom: 22px;
}

.game-status > div:first-child { display: grid; gap: 4px; }
.status-label {
  color: var(--pink-dark);
  font-size: 10px;
  font-weight: 800;
  letter-spacing: .17em;
}

.game-status strong {
  color: var(--brown);
  font-family: "Fraunces", Georgia, serif;
  font-size: 24px;
}

.status-right {
  display: flex;
  align-items: center;
  gap: 16px;
  white-space: nowrap;
}

#hearts {
  color: var(--pink-dark);
  font-size: 17px;
  letter-spacing: 3px;
}

#hearts.danger { animation: heart-shake .35s ease; }

#timer {
  min-width: 52px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  font-weight: 700;
}

.board {
  --size: 5;
  display: grid;
  grid-template-columns: repeat(var(--size), 1fr);
  width: 100%;
  aspect-ratio: 1;
  border: 3px solid var(--line);
  border-radius: 12px;
  background: var(--line);
  overflow: hidden;
  user-select: none;
  touch-action: manipulation;
}

.cell {
  position: relative;
  display: grid;
  place-items: center;
  min-width: 0;
  padding: 0;
  border: 0;
  border-right: 1px solid rgba(93, 76, 67, .34);
  border-bottom: 1px solid rgba(93, 76, 67, .34);
  background: var(--region-color);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.cell::before {
  content: "";
  position: absolute;
  inset: 5px;
  border-radius: 10px;
  background: transparent;
  transition: background .15s, transform .15s;
}

.cell:hover::before { background: rgba(255, 255, 255, .34); }
.cell:active::before { transform: scale(.94); }
.cell:focus-visible { z-index: 2; outline: 4px solid #20809d; outline-offset: -4px; }
.cell.edge-right { border-right: 3px solid var(--line); }
.cell.edge-bottom { border-bottom: 3px solid var(--line); }

.mark {
  position: relative;
  z-index: 1;
  display: grid;
  place-items: center;
  line-height: 1;
  pointer-events: none;
}

.mark.cross {
  color: rgba(80, 65, 58, .52);
  font-size: clamp(17px, 4.4vw, 30px);
  font-weight: 500;
}

.mark.pig {
  font-size: clamp(25px, 5.2vw, 42px);
  filter: drop-shadow(0 2px 0 rgba(99, 61, 45, .14));
  animation: pig-pop .18s ease-out;
}

.cell.conflict {
  animation: cell-shake .28s ease;
  box-shadow: inset 0 0 0 4px #d33131;
}

.message {
  min-height: 24px;
  margin: 18px 0 14px;
  color: var(--muted);
  text-align: center;
  font-size: 13px;
}

.message.error { color: #b62828; font-weight: 700; }
.message.success { color: #287560; font-weight: 700; }

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

.toolbar button {
  min-height: 48px;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  gap: 7px;
  border: 1px solid rgba(93, 76, 67, .24);
  border-radius: 12px;
  background: #fff;
  cursor: pointer;
  font-size: 13px;
  font-weight: 700;
}

.toolbar button:hover { border-color: var(--pink-dark); color: var(--pink-dark); }
.toolbar button:disabled { opacity: .38; cursor: not-allowed; }
.toolbar span { font-size: 18px; }

.primary-button {
  width: 100%;
  min-height: 48px;
  border: 0;
  border-radius: 12px;
  background: var(--pink-dark);
  color: #fff;
  cursor: pointer;
  font-weight: 800;
}

.primary-button:hover { background: #bf3e60; }

dialog {
  width: min(500px, calc(100% - 30px));
  padding: 28px;
  border: 1px solid rgba(93, 76, 67, .24);
  border-radius: 22px;
  background: var(--paper);
  color: var(--ink);
  box-shadow: 0 28px 90px rgba(55, 32, 22, .3);
}

dialog::backdrop { background: rgba(50, 40, 34, .56); backdrop-filter: blur(3px); }
.dialog-head { display: flex; justify-content: space-between; align-items: center; }
.dialog-pig { font-size: 38px; }
.dialog-head button {
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 50%;
  background: #f1e9df;
  cursor: pointer;
  font-size: 24px;
}

dialog h2 { margin: 20px 0 14px; color: var(--brown); font-size: 34px; }
dialog ol { padding-left: 23px; color: var(--muted); line-height: 1.75; }
dialog p { color: var(--muted); line-height: 1.65; }
dialog .primary-button { margin-top: 8px; }

.win-card {
  position: fixed;
  z-index: 20;
  left: 50%;
  top: 50%;
  width: min(390px, calc(100% - 30px));
  padding: 32px;
  border: 2px solid var(--brown);
  border-radius: 24px;
  background: var(--paper);
  box-shadow: 8px 8px 0 var(--brown), 0 30px 100px rgba(50, 40, 34, .35);
  text-align: center;
  transform: translate(-50%, -50%);
}

.win-card[hidden] { display: none; }
.win-card .celebration { display: block; font-size: 64px; animation: pig-pop .35s ease-out; }
.win-card p { margin: 12px 0 6px; color: var(--pink-dark); font-size: 10px; font-weight: 800; letter-spacing: .14em; }
.win-card h2 { margin: 0 0 7px; color: var(--brown); font-size: 40px; }
.win-card > span:not(.celebration) { display: block; margin-bottom: 22px; color: var(--muted); }

@keyframes pig-pop {
  from { opacity: 0; transform: scale(.55) rotate(-8deg); }
  to { opacity: 1; transform: scale(1) rotate(0); }
}

@keyframes cell-shake {
  0%, 100% { transform: translateX(0); }
  33% { transform: translateX(-4px); }
  66% { transform: translateX(4px); }
}

@keyframes heart-shake {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.25); }
}

@media (max-width: 900px) {
  .game-shell {
    grid-template-columns: 1fr;
    gap: 38px;
    padding-top: 38px;
  }
  .game-copy { text-align: center; }
  .lede { margin-left: auto; margin-right: auto; }
  .level-tabs { justify-content: center; }
  .rules-mini { justify-content: center; grid-template-columns: repeat(4, auto); }
  .play-panel { justify-self: center; }
}

@media (max-width: 560px) {
  .topbar, .game-shell { width: min(100% - 24px, 500px); }
  .topbar { height: 70px; }
  .game-shell { min-height: calc(100vh - 70px); padding: 30px 0 44px; }
  h1 { font-size: clamp(43px, 15vw, 64px); }
  .lede { font-size: 15px; margin-top: 22px; }
  .rules-mini { grid-template-columns: repeat(2, auto); text-align: left; }
  .play-panel { padding: 16px; border-radius: 19px; }
  .game-status strong { font-size: 20px; }
  .status-right { gap: 10px; }
  #hearts { font-size: 14px; }
  .board { border-radius: 8px; }
  .cell::before { inset: 3px; border-radius: 6px; }
  .toolbar button { min-height: 46px; padding: 5px; font-size: 11px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
