:root {
  color-scheme: light;
  --bg: #eef0e7;
  --ink: #18211d;
  --muted: #667066;
  --panel: #f8f4e9;
  --line: #c7b48a;
  --board: #d9aa6b;
  --board-light: #efcc8d;
  --board-dark: #7e4d2c;
  --grid: #7a2f31;
  --grid-soft: rgba(122, 47, 49, 0.42);
  --red: #ba2f2a;
  --black: #1f2c2a;
  --green: #22312b;
  --gold: #d4a93c;
  --move: #2f8d68;
  --capture: #d45a34;
  --shadow: 0 18px 40px rgba(42, 30, 15, 0.18);
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  min-height: 100%;
}

body {
  margin: 0;
  min-height: 100dvh;
  background:
    radial-gradient(circle at top, rgba(255, 255, 255, 0.58), transparent 38rem),
    linear-gradient(145deg, #eef0e7 0%, #ddd8c7 100%);
  color: var(--ink);
  font-family: ui-rounded, "SF Pro Rounded", "PingFang TC", "Noto Sans TC", system-ui, -apple-system, sans-serif;
  letter-spacing: 0;
  overflow-x: hidden;
  overflow-y: hidden;
  -webkit-tap-highlight-color: transparent;
}

button,
select {
  font: inherit;
}

button {
  border: 0;
  cursor: pointer;
}

.app-shell {
  --shell-gap: 10px;
  height: 100vh;
  height: var(--viewport-height, 100dvh);
  min-height: 0;
  width: min(100%, 520px);
  margin: 0 auto;
  padding: max(12px, env(safe-area-inset-top)) 16px max(14px, env(safe-area-inset-bottom));
  display: grid;
  grid-template-rows: auto auto auto minmax(0, 1fr) auto;
  gap: var(--shell-gap);
  overflow: hidden;
}

.top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.brand-lockup {
  display: flex;
  align-items: center;
  min-width: 0;
  gap: 10px;
}

.app-mark {
  width: 44px;
  height: 44px;
  flex: 0 0 44px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  background: var(--green);
  color: #f6edd9;
  font-weight: 900;
  font-size: 24px;
  box-shadow: 0 8px 18px rgba(34, 49, 43, 0.25);
}

h1 {
  margin: 0;
  font-size: 20px;
  line-height: 1.05;
  font-weight: 900;
}

#status-main {
  margin: 5px 0 0;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.2;
  white-space: nowrap;
}

.counts {
  display: flex;
  align-items: center;
  gap: 6px;
}

.count-pill {
  min-width: 43px;
  height: 40px;
  display: grid;
  place-items: center;
  gap: 0;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.58);
  border: 1px solid rgba(90, 70, 42, 0.14);
}

.count-pill span {
  font-size: 11px;
  line-height: 1;
  color: var(--muted);
}

.count-pill strong {
  font-size: 16px;
  line-height: 1;
}

.red-side strong {
  color: var(--red);
}

.black-side strong {
  color: var(--black);
}

.taken strong {
  color: #7e5a22;
}

.sub-status {
  margin: 0;
  min-height: 20px;
  color: #4d5b52;
  font-size: 14px;
  line-height: 1.35;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.record-strip {
  display: grid;
  grid-template-columns: auto 1fr 1fr 1fr auto;
  align-items: center;
  gap: 5px;
  min-height: 32px;
  padding: 4px;
  border-radius: 8px;
  background: rgba(255, 250, 239, 0.68);
  border: 1px solid rgba(89, 63, 31, 0.12);
  box-shadow: 0 6px 14px rgba(54, 36, 14, 0.06);
}

.record-title,
.record-rate,
.record-strip strong {
  min-width: 0;
  height: 24px;
  display: grid;
  place-items: center;
  border-radius: 6px;
  line-height: 1;
  white-space: nowrap;
}

.record-title {
  padding: 0 8px;
  background: var(--green);
  color: #fff8e8;
  font-size: 12px;
  font-weight: 900;
}

.record-strip strong {
  background: rgba(123, 82, 39, 0.08);
  color: #503823;
  font-size: 13px;
  font-weight: 900;
}

.record-rate {
  padding: 0 7px;
  color: #5f675f;
  font-size: 12px;
  font-weight: 800;
}

.play-area {
  min-height: 0;
  display: grid;
  place-items: center;
  width: 100%;
  overflow: hidden;
}

.board-stage {
  --rail-width: var(--fit-rail-width, clamp(22px, 6vw, 30px));
  --stage-gap: var(--fit-stage-gap, clamp(3px, 1.2vw, 6px));
  --fallback-board-width: max(112px, min(calc(100vw - 96px), calc((100dvh - 250px) / 2), 356px));
  --board-width: var(--fit-board-width, var(--fallback-board-width));
  width: min(100%, calc(var(--board-width) + var(--rail-width) * 2 + var(--stage-gap) * 2));
  max-height: 100%;
  display: grid;
  grid-template-columns: var(--rail-width) minmax(0, var(--board-width)) var(--rail-width);
  align-items: stretch;
  justify-content: center;
  gap: var(--stage-gap);
}

.capture-rail {
  min-width: 0;
  height: 100%;
  max-height: calc(var(--board-width) * 2);
  align-self: center;
  display: grid;
  grid-template-rows: auto 1fr;
  gap: 6px;
  padding: 5px 3px;
  border-radius: 8px;
  background:
    linear-gradient(180deg, rgba(255, 250, 235, 0.72), rgba(218, 178, 112, 0.42)),
    rgba(90, 54, 26, 0.08);
  border: 1px solid rgba(87, 52, 24, 0.2);
  box-shadow:
    inset 0 0 0 1px rgba(255, 255, 255, 0.26),
    0 8px 18px rgba(53, 34, 18, 0.1);
}

.rail-label {
  min-height: 34px;
  display: grid;
  place-items: center;
  border-radius: 6px;
  background: rgba(48, 35, 25, 0.82);
  color: #fff3d1;
  font-size: 11px;
  line-height: 1;
  font-weight: 900;
  writing-mode: vertical-rl;
  letter-spacing: 0;
}

.capture-stack {
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  overflow: hidden;
}

.capture-token {
  width: min(100%, 30px, calc((var(--board-width) * 2 - 82px) / 16));
  aspect-ratio: 1;
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background:
    radial-gradient(circle at 32% 24%, rgba(255, 255, 255, 0.92), transparent 24%),
    radial-gradient(circle at center, #fff9e9 0 48%, #ead7a7 49% 67%, #7a5a39 68% 73%, transparent 74%),
    linear-gradient(145deg, #fff8e7, #d3b275);
  border: 1px solid #2f251f;
  box-shadow:
    inset 0 0 0 2px rgba(255, 248, 226, 0.74),
    0 2px 0 #2c241f,
    0 4px 8px rgba(47, 28, 13, 0.2);
  overflow: hidden;
  font-size: clamp(11px, 3.6vw, 16px);
  line-height: 1;
  font-weight: 950;
  transform: translateZ(0);
}

.capture-token.red {
  color: var(--red);
}

.capture-token.black {
  color: var(--black);
}

.capture-token.recent {
  box-shadow:
    0 0 0 2px rgba(245, 204, 85, 0.75),
    0 2px 0 #2c241f,
    0 5px 10px rgba(47, 28, 13, 0.24);
}

.capture-empty {
  background:
    linear-gradient(135deg, rgba(93, 54, 24, 0.09), rgba(255, 247, 222, 0.08));
  border: 1px dashed rgba(95, 58, 28, 0.22);
  box-shadow: none;
}

.board {
  --board-pad: clamp(12px, 3.2vw, 18px);
  width: 100%;
  max-height: 100%;
  aspect-ratio: 4 / 8;
  position: relative;
  padding: var(--board-pad);
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(8, 1fr);
  gap: 0;
  border-radius: 9px;
  background:
    linear-gradient(90deg, rgba(255, 255, 255, 0.2), transparent 31%, rgba(61, 29, 10, 0.15)),
    repeating-linear-gradient(6deg, rgba(95, 50, 20, 0.16) 0 2px, transparent 2px 12px),
    repeating-linear-gradient(92deg, rgba(255, 255, 255, 0.11) 0 1px, transparent 1px 18px),
    var(--board);
  border: 4px solid #382414;
  box-shadow:
    inset 0 0 0 3px rgba(245, 213, 145, 0.7),
    inset 0 0 0 7px rgba(117, 72, 36, 0.72),
    0 18px 40px rgba(42, 30, 15, 0.22);
  overflow: hidden;
}

.board::before,
.board::after {
  content: "";
  position: absolute;
  inset: var(--board-pad);
  pointer-events: none;
}

.board::before {
  z-index: 0;
  background:
    repeating-linear-gradient(
      to right,
      transparent 0,
      transparent calc(25% - 1px),
      rgba(93, 35, 34, 0.88) calc(25% - 1px),
      rgba(93, 35, 34, 0.88) calc(25% + 1px),
      transparent calc(25% + 1px),
      transparent 25%
    ),
    repeating-linear-gradient(
      to bottom,
      transparent 0,
      transparent calc(12.5% - 1px),
      rgba(93, 35, 34, 0.88) calc(12.5% - 1px),
      rgba(93, 35, 34, 0.88) calc(12.5% + 1px),
      transparent calc(12.5% + 1px),
      transparent 12.5%
    );
  border: 2px solid rgba(82, 30, 29, 0.95);
  box-shadow:
    inset 0 0 0 2px rgba(245, 215, 155, 0.38),
    0 0 0 1px rgba(58, 29, 19, 0.4);
}

.board::after {
  z-index: 1;
  opacity: 0.62;
  background:
    linear-gradient(135deg, transparent calc(50% - 1px), rgba(96, 36, 34, 0.62) calc(50% - 1px), rgba(96, 36, 34, 0.62) calc(50% + 1px), transparent calc(50% + 1px)) 25% 12.5% / 25% 12.5% no-repeat,
    linear-gradient(45deg, transparent calc(50% - 1px), rgba(96, 36, 34, 0.62) calc(50% - 1px), rgba(96, 36, 34, 0.62) calc(50% + 1px), transparent calc(50% + 1px)) 50% 12.5% / 25% 12.5% no-repeat,
    linear-gradient(135deg, transparent calc(50% - 1px), rgba(96, 36, 34, 0.52) calc(50% - 1px), rgba(96, 36, 34, 0.52) calc(50% + 1px), transparent calc(50% + 1px)) 25% 75% / 25% 12.5% no-repeat,
    linear-gradient(45deg, transparent calc(50% - 1px), rgba(96, 36, 34, 0.52) calc(50% - 1px), rgba(96, 36, 34, 0.52) calc(50% + 1px), transparent calc(50% + 1px)) 50% 75% / 25% 12.5% no-repeat;
}

.cell {
  position: relative;
  z-index: 2;
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  display: grid;
  place-items: center;
  border-radius: 0;
  background: transparent;
  color: var(--ink);
  transition: background 140ms ease;
  touch-action: manipulation;
}

.cell::before {
  content: "";
  position: absolute;
  inset: 12%;
  z-index: 1;
  opacity: 0.24;
  background:
    linear-gradient(var(--grid), var(--grid)) left top / 16px 2px no-repeat,
    linear-gradient(var(--grid), var(--grid)) left top / 2px 16px no-repeat,
    linear-gradient(var(--grid), var(--grid)) right bottom / 16px 2px no-repeat,
    linear-gradient(var(--grid), var(--grid)) right bottom / 2px 16px no-repeat;
  pointer-events: none;
}

.cell:disabled {
  cursor: default;
}

.cell.empty {
  background: rgba(255, 246, 215, 0.03);
}

.token {
  position: relative;
  z-index: 2;
  width: min(calc(var(--board-width) / 5.55), 66px);
  aspect-ratio: 1;
  display: grid;
  place-items: center;
  border-radius: 50%;
  overflow: hidden;
  transition: transform 140ms ease, box-shadow 140ms ease, filter 140ms ease;
}

.cell.hidden .token {
  background:
    radial-gradient(circle at 32% 26%, rgba(255, 255, 255, 0.35), transparent 24%),
    radial-gradient(circle at 50% 64%, rgba(54, 96, 72, 0.95), transparent 58%),
    linear-gradient(145deg, #3f8a4c, #16442b 68%, #0f2d21);
  color: #f3e0b8;
  border: 2px solid rgba(241, 216, 144, 0.68);
  box-shadow:
    inset 0 3px 6px rgba(255, 255, 255, 0.18),
    inset 0 -8px 13px rgba(0, 0, 0, 0.34),
    0 3px 0 rgba(28, 42, 33, 0.9),
    0 6px 12px rgba(40, 25, 13, 0.28);
}

.cell.piece .token {
  background:
    radial-gradient(circle at 31% 24%, rgba(255, 255, 255, 0.95), transparent 23%),
    radial-gradient(circle at center, #fff9e9 0 50%, #ead7a7 51% 67%, #ab7745 68% 72%, #4c3526 73% 78%, transparent 79%),
    linear-gradient(145deg, #fff8e8, #cfaf72);
  border: 2px solid #34261f;
  box-shadow:
    inset 0 0 0 3px rgba(255, 248, 226, 0.78),
    inset 0 -7px 10px rgba(105, 66, 30, 0.24),
    0 3px 0 #2c241f,
    0 7px 13px rgba(50, 31, 17, 0.3);
}

.cell.red .piece-symbol {
  color: var(--red);
}

.cell.black .piece-symbol {
  color: var(--black);
}

.piece-symbol,
.back-mark {
  display: grid;
  place-items: center;
  position: relative;
  z-index: 2;
  width: 82%;
  height: 82%;
  font-size: clamp(24px, calc(var(--board-width) / 10.2), 34px);
  line-height: 0.88;
  font-weight: 950;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.45);
  overflow: hidden;
}

.back-mark {
  font-size: clamp(18px, calc(var(--board-width) / 12), 29px);
  letter-spacing: 0;
  color: #f6dfad;
  text-shadow: 0 2px 2px rgba(0, 0, 0, 0.3);
}

.cell.selected {
  background: rgba(255, 232, 145, 0.12);
}

.cell.selected .token {
  transform: translateY(-2px);
  box-shadow:
    0 0 0 4px rgba(244, 201, 75, 0.72),
    0 3px 0 #2c241f,
    0 9px 16px rgba(50, 31, 17, 0.34);
}

.cell.legal-move::after,
.cell.legal-capture::after {
  content: "";
  position: absolute;
  z-index: 3;
  inset: 23%;
  border-radius: 50%;
  border: 3px solid rgba(47, 141, 104, 0.82);
  background: rgba(47, 141, 104, 0.12);
  pointer-events: none;
}

.cell.legal-capture::after {
  border-color: rgba(212, 90, 52, 0.9);
  background: rgba(212, 90, 52, 0.12);
  inset: 12%;
}

.cell.last-action .token {
  filter: brightness(1.04);
  box-shadow:
    0 0 0 3px rgba(255, 236, 164, 0.95),
    0 3px 0 #2c241f,
    0 8px 15px rgba(50, 31, 17, 0.32);
}

.controls {
  display: grid;
  gap: 9px;
}

.control-row {
  display: grid;
  grid-template-columns: 1fr 1fr 1.25fr 1fr;
  gap: 8px;
}

.control-row button,
.select-wrap select,
.segmented button {
  min-height: 42px;
  border-radius: 8px;
  background: #fffaf0;
  color: var(--ink);
  border: 1px solid rgba(87, 66, 35, 0.18);
  box-shadow: 0 6px 14px rgba(54, 36, 14, 0.08);
  font-weight: 800;
}

.control-row button:disabled {
  opacity: 0.45;
}

.select-wrap {
  display: block;
}

.select-wrap select {
  width: 100%;
  height: 100%;
  padding: 0 9px;
  appearance: none;
  text-align: center;
  text-align-last: center;
}

.segmented {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0;
  padding: 3px;
  border-radius: 10px;
  background: rgba(32, 47, 41, 0.14);
}

.segmented button {
  min-height: 36px;
  border-radius: 7px;
  background: transparent;
  box-shadow: none;
  border: 0;
  color: #425048;
}

.segmented button.active {
  background: var(--green);
  color: #fff8e8;
}

.rules-dialog {
  width: min(92vw, 420px);
  border: 0;
  border-radius: 10px;
  padding: 20px;
  background: #fffaf0;
  color: var(--ink);
  box-shadow: 0 22px 70px rgba(0, 0, 0, 0.28);
}

.rules-dialog::backdrop {
  background: rgba(20, 25, 22, 0.5);
}

.rules-dialog h2 {
  margin: 0 0 12px;
  font-size: 21px;
}

.rules-dialog p {
  margin: 0 0 11px;
  color: #4a554d;
  line-height: 1.55;
}

.rules-dialog button {
  width: 100%;
  margin-top: 6px;
  min-height: 42px;
  border-radius: 8px;
  background: var(--green);
  color: #fff8e8;
  font-weight: 900;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: max(18px, env(safe-area-inset-bottom));
  transform: translate(-50%, 16px);
  max-width: min(92vw, 420px);
  padding: 10px 14px;
  border-radius: 8px;
  background: rgba(25, 33, 29, 0.92);
  color: #fff9e8;
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 160ms ease, transform 160ms ease;
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

.game-over {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 20px;
  background:
    radial-gradient(circle at center, rgba(255, 246, 218, 0.26), transparent 18rem),
    rgba(22, 24, 22, 0.58);
  backdrop-filter: blur(5px);
}

.game-over[hidden] {
  display: none;
}

.game-over-card {
  width: min(88vw, 350px);
  padding: 22px 18px 18px;
  border-radius: 10px;
  text-align: center;
  background:
    linear-gradient(145deg, rgba(255, 250, 235, 0.98), rgba(231, 205, 150, 0.98)),
    #fff8e7;
  border: 3px solid #3c2718;
  box-shadow:
    inset 0 0 0 3px rgba(255, 255, 255, 0.42),
    0 24px 70px rgba(0, 0, 0, 0.32);
}

.game-over-kicker {
  display: inline-grid;
  place-items: center;
  min-height: 24px;
  padding: 0 10px;
  border-radius: 999px;
  background: rgba(51, 36, 24, 0.88);
  color: #fff4d6;
  font-size: 12px;
  font-weight: 900;
}

.game-over-card h2 {
  margin: 12px 0 8px;
  color: var(--green);
  font-size: 36px;
  line-height: 1;
  font-weight: 950;
}

.game-over[data-result="human"] h2 {
  color: var(--red);
}

.game-over[data-result="ai"] h2 {
  color: #1f2c2a;
}

.game-over-card p {
  margin: 0;
  color: #5d4932;
  font-size: 15px;
  line-height: 1.45;
}

.game-over-record {
  margin: 16px 0 14px;
  padding: 10px;
  border-radius: 8px;
  background: rgba(88, 58, 25, 0.1);
  color: #503823;
}

.game-over-record span,
.game-over-record strong {
  display: block;
}

.game-over-record span {
  margin-bottom: 5px;
  color: #75624b;
  font-size: 12px;
  font-weight: 800;
}

.game-over-record strong {
  font-size: 16px;
  line-height: 1.2;
}

.game-over-card button {
  width: 100%;
  min-height: 44px;
  border-radius: 8px;
  background: var(--green);
  color: #fff8e8;
  font-weight: 950;
  box-shadow: 0 8px 16px rgba(34, 49, 43, 0.2);
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

@media (max-width: 370px) {
  .app-shell {
    padding-left: 12px;
    padding-right: 12px;
    --shell-gap: 8px;
  }

  .board {
    --board-pad: 10px;
  }

  .app-mark {
    width: 40px;
    height: 40px;
    flex-basis: 40px;
  }

  h1 {
    font-size: 18px;
  }

  .count-pill {
    min-width: 38px;
    height: 36px;
  }

  .control-row {
    gap: 6px;
  }

  .control-row button,
  .select-wrap select {
    min-height: 40px;
    font-size: 14px;
  }
}

@media (max-height: 740px) {
  .app-shell {
    --shell-gap: 7px;
    padding-top: max(8px, env(safe-area-inset-top));
    padding-bottom: max(8px, env(safe-area-inset-bottom));
  }

  .app-mark {
    width: 38px;
    height: 38px;
    flex-basis: 38px;
    font-size: 21px;
  }

  h1 {
    font-size: 18px;
  }

  #status-main,
  .sub-status {
    font-size: 13px;
  }

  #status-main {
    margin-top: 3px;
  }

  .sub-status {
    min-height: 18px;
  }

  .count-pill {
    min-width: 39px;
    height: 36px;
  }

  .record-strip {
    min-height: 29px;
    padding: 3px;
  }

  .record-title,
  .record-rate,
  .record-strip strong {
    height: 22px;
  }

  .controls {
    gap: 6px;
  }

  .control-row {
    gap: 6px;
  }

  .control-row button,
  .select-wrap select,
  .segmented button {
    min-height: 36px;
    font-size: 14px;
  }

  .segmented {
    padding: 2px;
  }

  .segmented button {
    min-height: 31px;
  }
}

@media (max-height: 620px) {
  .app-shell {
    --shell-gap: 5px;
    padding-left: 12px;
    padding-right: 12px;
  }

  .brand-lockup {
    gap: 8px;
  }

  .app-mark {
    width: 34px;
    height: 34px;
    flex-basis: 34px;
    font-size: 19px;
  }

  h1 {
    font-size: 17px;
  }

  #status-main,
  .sub-status {
    font-size: 12px;
  }

  .count-pill {
    min-width: 36px;
    height: 34px;
  }

  .count-pill strong {
    font-size: 15px;
  }

  .record-strip strong {
    font-size: 12px;
  }

  .record-title,
  .record-rate {
    font-size: 11px;
  }

  .board {
    --board-pad: clamp(8px, 2.4vw, 12px);
    border-width: 3px;
  }

  .control-row button,
  .select-wrap select,
  .segmented button {
    min-height: 33px;
    font-size: 13px;
  }

  .segmented button {
    min-height: 28px;
  }
}

@media (display-mode: standalone) {
  body {
    background: linear-gradient(145deg, #eef0e7 0%, #ddd8c7 100%);
  }
}
