:root {
  color-scheme: dark;
  --ink: #143b31;
  --cream: #fff4da;
  --gold: #f6c879;
  --panel: #f9f2df;
  --muted: #c7d5bc;
  font-family: "Trebuchet MS", "Noto Sans TC", "Hiragino Sans", system-ui, sans-serif;
  background: #102e28;
  color: var(--cream);
}
* { box-sizing: border-box }
html, body { overscroll-behavior: none }
body {
  margin: 0;
  height: 100vh;
  height: 100dvh;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  background: radial-gradient(ellipse at 50% 34%, #365b45 0, #183e33 45%, #102d27 83%);
}
button, a, select { font: inherit; -webkit-tap-highlight-color: transparent }
button, select { cursor: pointer }
button:focus-visible, a:focus-visible, select:focus-visible { outline: 3px solid #e98d45; outline-offset: 3px }
[hidden] { display: none !important }
main {
  justify-self: center;
  width: min(100%, 560px);
  min-height: 0;
  padding: 0 clamp(8px, 3vw, 20px) max(6px, env(safe-area-inset-bottom));
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 5px;
}
.head { padding-block: clamp(4px, 1.3vh, 12px) 2px; text-align: center }
h1 { margin: 0; font-family: Georgia, "Noto Serif TC", serif; font-size: clamp(17px, 3.9vw, 25px); line-height: 1.12 }
.game-area { display: grid; grid-template-rows: minmax(0, 1fr) auto; min-height: 0; gap: 7px }
.mat-stage { min-height: 0; display: grid; place-items: center; container-type: size }
.game-mat {
  position: relative;
  width: min(100cqw, 69.55cqh);
  aspect-ratio: 1800 / 2588;
  container-type: inline-size;
  background: url('mat-portrait.webp') center / 100% 100% no-repeat;
  filter: drop-shadow(0 5px 8px #061e1d77);
}
/* Extend the painted interior seams to a full square, including the side margins. */
#board {
  position: absolute;
  top: 50%;
  left: 50%;
  height: 62%;
  aspect-ratio: 1;
  transform: translate(-50%, -50%);
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  grid-template-rows: repeat(4, minmax(0, 1fr));
}
.cell {
  appearance: none;
  margin: 0;
  border: 0;
  background: transparent;
  padding: 0;
  position: relative;
  display: grid;
  place-items: center;
  min-width: 0;
  min-height: 0;
  border-radius: 7%;
  cursor: default;
}
.cell.available, .cell.choice { cursor: pointer }
@media (hover: hover) { .cell.available:hover { background: #fff7d335 } }
.cell.available:focus-visible { background: #fff7d335 }
.cell.choice::before { content: ""; position: absolute; inset: 4%; border: 2px solid #fff2a0; border-radius: 14%; background: #fff2a03b }
.cell.selected-line::before { background: #fff2a095; border-color: #fff }
.cell .stone {
  width: 95%;
  display: block;
  aspect-ratio: 1;
  position: relative;
  transition: transform 150ms ease;
  filter: drop-shadow(0 3px 1px #422c2370);
}
.stone img { width: 100%; height: 100%; display: block; position: absolute; inset: 0; pointer-events: none }
.cell.flipping .stone { transform: scaleX(.02) }
.cell.placed .stone { animation: place-stone .25s ease-out }
.cell.marked::after { content: ""; position: absolute; top: 8%; right: 6%; width: 9%; aspect-ratio: 1; border-radius: 50%; background: #ffe2a6; border: 1px solid #6f3918 }
@keyframes place-stone { from { transform: translateY(-8%) scale(1.11) } }
.player { position: absolute; left: 0; width: 100%; height: 18.5%; pointer-events: none }
.opponent { top: 0 }
.human { bottom: 0 }
.player strong {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  border-radius: 12px;
  padding: 2px 9px;
  font-size: clamp(9px, 3.4cqw, 14px);
  font-weight: 700;
  color: #fff5d8;
  background: #201a12c9;
  line-height: 1.18;
}
.opponent strong { top: 2% }
.human strong { bottom: 2% }
.player.current strong { color: #ffe399; box-shadow: inset 0 0 0 1px #f4c56da8 }
.score-spots, .jars {
  position: absolute;
  left: 21%;
  width: 58%;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  justify-items: center;
  transform: translateY(-50%);
}
.opponent .score-spots, .opponent .jars { top: 63% }
.human .score-spots, .human .jars { top: 37% }
.score-spots i, .jars img { width: 73%; aspect-ratio: 1; border-radius: 50%; display: block }
.score-spots i { background: #ffffff1a; border: clamp(1px, .55cqw, 2px) dashed #fff9ed72 }
.jars img { visibility: hidden; object-fit: contain; width: 85% }
.score-spots i.filled { visibility: hidden }
.jars img.full { visibility: visible; filter: drop-shadow(0 2px 1px #25150588) }
.action-panel {
  background: var(--panel);
  color: var(--ink);
  border-radius: 12px;
  padding: 9px 11px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 74px;
  box-shadow: 0 3px 0 #d6c7a1;
}
#turn { font-weight: 700; font-size: clamp(.8rem, 3.2vw, 1rem); text-align: center; line-height: 1.32 }
#face { display: flex; justify-content: center; gap: 7px }
#face button, dialog button, .cta { font: inherit; font-weight: 720; color: var(--ink); border: 1px solid #abb5a2; border-radius: 8px; background: #fffbec; padding: 4px 13px; min-height: 36px }
#face button { display: flex; align-items: center; justify-content: center; gap: 5px; min-width: 75px }
#face img { width: 26px; height: 26px }
#face button[aria-pressed=true], .cta, dialog .cta { background: var(--ink); border-color: var(--ink); color: #fff3d4 }
#face button:disabled { opacity: .55; cursor: default }
.foot { min-height: 28px; display: flex; align-items: center; justify-content: space-between; gap: 8px; line-height: 1.25; color: var(--muted); font-size: .75rem }
#restart { flex: none; border: 0; background: transparent; color: var(--cream); text-decoration: underline; text-underline-offset: 3px; padding: 7px 0; font-size: .8rem }
dialog { max-width: 90vw; border: 1px solid #b4a16a; border-radius: 14px; background: var(--panel); color: var(--ink); padding: 20px; box-shadow: 0 12px 32px #00181255 }
dialog::backdrop { background: #07261ed9 }
dialog h2 { margin: 0 0 18px; font-size: 1.25rem }
@media (max-height: 620px) {
  main { gap: 3px; padding-bottom: max(4px, env(safe-area-inset-bottom)) }
  .head { padding-block: 3px 0 }
  .game-area { gap: 4px }
  .action-panel { padding: 5px 8px; gap: 4px; min-height: 59px }
  .foot { min-height: 23px; font-size: .68rem }
  #restart { padding: 4px 0; font-size: .72rem }
}
@media (orientation: landscape) and (max-height: 550px) {
  main { width: min(100%, 800px); padding-bottom: 7px }
  .head { grid-column: 2; grid-row: 1; align-self: end; padding-block: 2px }
  main { grid-template-columns: minmax(0, 1fr) minmax(175px, .85fr); grid-template-rows: minmax(0, 1fr) auto minmax(0, 1fr); gap: 6px 10px }
  .game-area { display: contents }
  .mat-stage { grid-column: 1; grid-row: 1/-1 }
  .action-panel { grid-column: 2; grid-row: 2 }
  .foot { grid-column: 2; grid-row: 3; align-self: start; flex-wrap: wrap }
}
@media (prefers-reduced-motion: reduce) {
  .cell .stone { transition: none }
  .cell.placed .stone { animation: none }
}
