:root {
  color-scheme: light;
  --bg: #f6efe4;
  --panel: #fffaf2;
  --ink: #221b16;
  --muted: #74685e;
  --line: #a9784d;
  --line-soft: #dac4a5;
  --red: #b42322;
  --black: #24211e;
  --green: #2f6f5e;
  --gold: #c88724;
  --blue: #2d5f86;
  --shadow: 0 18px 42px rgba(54, 35, 20, 0.16);
}

* {
  box-sizing: border-box;
}

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

html.focus-board,
body.focus-board {
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;
}

body {
  margin: 0;
  color: var(--ink);
  background:
    linear-gradient(90deg, rgba(122, 83, 51, 0.08) 1px, transparent 1px),
    linear-gradient(rgba(122, 83, 51, 0.08) 1px, transparent 1px),
    var(--bg);
  background-size: 32px 32px;
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  -webkit-tap-highlight-color: transparent;
}

button {
  font: inherit;
}

.app {
  width: min(1180px, 100%);
  min-height: 100vh;
  margin: 0 auto;
  padding: max(14px, env(safe-area-inset-top)) max(14px, env(safe-area-inset-right)) max(14px, env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left));
  display: grid;
  grid-template-columns: minmax(0, 720px) minmax(280px, 360px);
  gap: 16px;
}

.topbar,
.panel {
  background: rgba(255, 250, 242, 0.96);
  border: 1px solid rgba(122, 83, 51, 0.18);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.topbar {
  min-height: 78px;
  padding: 14px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  display: none;
}

h2 {
  margin-bottom: 10px;
  font-size: 17px;
}

p,
span {
  line-height: 1.45;
}

#gameStatus {
  min-width: min(520px, 58vw);
  margin-top: 0;
  color: var(--muted);
  text-align: center;
  font-size: clamp(20px, 3vw, 30px);
  font-weight: 700;
}

#gameStatus.check-status {
  color: #9f4f16;
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.icon-btn {
  width: 46px;
  height: 46px;
  color: var(--blue);
  background: #f9ecd7;
  border: 1px solid var(--line-soft);
  border-radius: 50%;
  cursor: pointer;
  font-size: 26px;
  font-weight: 900;
  line-height: 1;
}

.pill {
  min-width: 76px;
  padding: 10px 12px;
  color: #fff;
  background: var(--red);
  border-radius: 999px;
  text-align: center;
  font-weight: 900;
}

.pill.black {
  background: var(--black);
}

.board-frame {
  position: relative;
  width: min(100%, 700px);
  aspect-ratio: 9 / 10;
  margin: 14px auto 0;
  padding: clamp(20px, 4vw, 34px);
  background: #e8bd78;
  border: 7px solid #7a5333;
  border-radius: 8px;
  box-shadow: var(--shadow), inset 0 0 0 2px rgba(255, 255, 255, 0.34);
  touch-action: none;
  user-select: none;
}

.board-frame.thinking::after {
  content: "";
  position: absolute;
  inset: 8px;
  border: 3px solid rgba(47, 111, 94, 0.28);
  border-radius: 8px;
  pointer-events: none;
  animation: thinkingPulse 1.4s ease-in-out infinite;
}

@keyframes thinkingPulse {
  0%,
  100% {
    opacity: 0.35;
  }

  50% {
    opacity: 0.95;
  }
}

.board-lines {
  display: none;
}

.board-grid {
  position: absolute;
  inset: 0;
  z-index: 1;
  pointer-events: none;
}

.grid-line {
  position: absolute;
  display: block;
  background: var(--line);
}

.grid-line.vertical {
  width: 2px;
  transform: translateX(-1px);
}

.grid-line.horizontal {
  height: 2px;
  transform: translateY(-1px);
}

.river {
  position: absolute;
  z-index: 1;
  left: 2px;
  right: 2px;
  top: calc(44.444% + 2px);
  height: calc(11.111% - 4px);
  display: flex;
  align-items: center;
  justify-content: space-around;
  color: rgba(91, 55, 29, 0.34);
  background: #e8bd78;
  font-size: clamp(26px, 5vw, 48px);
  font-weight: 900;
  pointer-events: none;
}

.palace {
  position: absolute;
  z-index: 2;
  left: 37.5%;
  width: 25%;
  height: 22.222%;
  background:
    linear-gradient(to bottom right, transparent calc(50% - 1px), var(--line) calc(50% - 1px) calc(50% + 1px), transparent calc(50% + 1px)),
    linear-gradient(to bottom left, transparent calc(50% - 1px), var(--line) calc(50% - 1px) calc(50% + 1px), transparent calc(50% + 1px));
  pointer-events: none;
}

.palace::before,
.palace::after {
  content: none;
}

.palace-top {
  top: 0;
}

.palace-bottom {
  bottom: 0;
}

.board {
  position: relative;
  width: 100%;
  height: 100%;
  touch-action: none;
}

.cell {
  position: absolute;
  z-index: 3;
  width: 12.5%;
  height: 11.111%;
  transform: translate(-50%, -50%);
  display: grid;
  place-items: center;
  background: transparent;
  border: 0;
  border-radius: 50%;
  padding: 0;
  cursor: pointer;
  touch-action: none;
  -webkit-appearance: none;
  appearance: none;
}

.cell.move::after,
.cell.check::before {
  content: "";
  position: absolute;
  border-radius: 50%;
}

.cell.move::after {
  width: 32%;
  aspect-ratio: 1;
  background: rgba(47, 111, 94, 0.7);
}

.cell.check::before {
  width: 92%;
  aspect-ratio: 1;
  border: 4px solid rgba(200, 135, 36, 0.88);
  background: rgba(255, 244, 208, 0.42);
}

.piece {
  position: relative;
  z-index: 2;
  width: clamp(40px, 8.9vw, 68px);
  height: clamp(40px, 8.9vw, 68px);
  display: grid;
  place-items: center;
  color: currentColor;
  background: #f1d39b;
  border: 3px solid currentColor;
  border-radius: 50%;
  box-shadow: 0 5px 0 rgba(94, 58, 31, 0.24), inset 0 0 0 2px rgba(130, 85, 42, 0.2);
  font-size: clamp(23px, 4.7vw, 40px);
  font-weight: 900;
  pointer-events: none;
}

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

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

.piece.last-piece::after {
  content: "";
  position: absolute;
  right: 6%;
  bottom: 6%;
  width: 22%;
  aspect-ratio: 1;
  background: var(--green);
  border: 2px solid #f1d39b;
  border-radius: 50%;
  box-shadow: 0 2px 4px rgba(34, 27, 22, 0.2);
}

.piece.human-last::after {
  background: var(--gold);
}

.board.locked {
  cursor: wait;
}

.side {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.panel {
  padding: 14px;
}

.segmented,
.mode-grid {
  display: grid;
  gap: 8px;
}

.segmented {
  grid-template-columns: 1fr 1fr;
  padding: 4px;
  background: #ecdec9;
  border: 1px solid var(--line-soft);
  border-radius: 8px;
}

.segment,
.mode,
.actions button {
  min-height: 44px;
  border: 0;
  border-radius: 7px;
  cursor: pointer;
  transition: transform 120ms ease, filter 120ms ease, box-shadow 120ms ease;
}

.segment:active,
.mode:active,
.actions button:active,
.button-feedback {
  transform: scale(0.97);
  filter: brightness(1.08);
  box-shadow: inset 0 0 0 3px rgba(255, 255, 255, 0.36), 0 3px 10px rgba(34, 27, 22, 0.14);
}

.segment {
  color: var(--muted);
  background: transparent;
  font-weight: 900;
}

.segment.active {
  color: var(--ink);
  background: var(--panel);
  box-shadow: 0 5px 16px rgba(54, 35, 20, 0.12);
}

.mode {
  padding: 12px;
  color: var(--ink);
  background: #f4e6d0;
  border: 2px solid transparent;
  text-align: left;
}

.mode.active {
  border-color: var(--green);
  background: #fffdf8;
}

.mode strong,
.mode span {
  display: block;
}

.mode span {
  color: var(--muted);
  font-size: 13px;
}

.color-choice {
  margin-top: 12px;
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 10px;
  align-items: center;
}

.actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.actions button {
  padding: 0 10px;
  color: #fff;
  background: var(--green);
  font-weight: 900;
}

.actions button:nth-child(3) {
  background: #8c3b32;
}

.actions button:nth-child(4) {
  grid-column: span 2;
  background: var(--blue);
}

.actions button:disabled {
  color: #80766b;
  background: #ded0bb;
}

.records {
  max-height: 260px;
  margin: 0;
  padding-left: 22px;
  overflow: auto;
  color: var(--muted);
}

.records li {
  margin: 8px 0;
}

.win-modal {
  position: fixed;
  z-index: 20;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(34, 27, 22, 0.24);
  opacity: 0;
  pointer-events: none;
  transition: opacity 180ms ease;
}

.win-modal.show {
  opacity: 1;
  pointer-events: auto;
}

.win-modal[hidden] {
  display: none;
}

.win-card {
  width: min(390px, 92vw);
  padding: 24px;
  color: var(--ink);
  background: var(--panel);
  border: 1px solid rgba(122, 83, 51, 0.18);
  border-radius: 8px;
  box-shadow: 0 24px 70px rgba(34, 27, 22, 0.24);
  text-align: center;
}

.win-card h2 {
  margin: 12px 0 6px;
  font-size: 30px;
}

.win-card p {
  color: var(--muted);
  font-weight: 800;
}

.dino {
  position: relative;
  width: 150px;
  height: 112px;
  margin: 0 auto;
}

.dino-body {
  position: absolute;
  left: 30px;
  top: 20px;
  width: 92px;
  height: 70px;
  background: #69a986;
  border: 4px solid #2f6f5e;
  border-radius: 48px 48px 34px 34px;
}

.dino-tail {
  position: absolute;
  left: 10px;
  top: 58px;
  width: 44px;
  height: 24px;
  background: #69a986;
  border: 4px solid #2f6f5e;
  border-right: 0;
  border-radius: 28px 0 0 28px;
  transform: rotate(-18deg);
}

.dino-eye {
  position: absolute;
  right: 24px;
  top: 18px;
  width: 8px;
  height: 8px;
  background: var(--ink);
  border-radius: 50%;
}

.dino-smile {
  position: absolute;
  right: 16px;
  top: 38px;
  width: 20px;
  height: 10px;
  border-bottom: 3px solid var(--ink);
  border-radius: 0 0 20px 20px;
}

.dino-tear {
  display: none;
  position: absolute;
  right: 18px;
  top: 28px;
  width: 8px;
  height: 14px;
  background: #5aa7d8;
  border-radius: 8px 8px 10px 10px;
}

.win-card.lose .dino-body,
.win-card.lose .dino-tail,
.win-card.lose .dino-leg {
  background: #9bb8a9;
  border-color: #547668;
}

.win-card.lose .dino-smile {
  top: 46px;
  border-bottom: 0;
  border-top: 3px solid var(--ink);
  border-radius: 20px 20px 0 0;
}

.win-card.lose .dino-tear {
  display: block;
}

.dino-leg {
  position: absolute;
  bottom: 12px;
  width: 18px;
  height: 24px;
  background: #69a986;
  border: 4px solid #2f6f5e;
  border-top: 0;
  border-radius: 0 0 12px 12px;
}

.leg-one {
  left: 54px;
}

.leg-two {
  left: 92px;
}

.win-actions {
  margin-top: 18px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.win-actions button {
  min-height: 46px;
  color: #fff;
  background: var(--green);
  border: 0;
  border-radius: 7px;
  font-weight: 900;
}

.win-actions button:last-child {
  color: var(--ink);
  background: #f4e6d0;
}

@media (max-width: 920px) {
  .app {
    grid-template-columns: 1fr;
  }

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

@media (max-width: 620px) {
  .app {
    padding: 10px;
  }

  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .pill {
    flex: 1;
  }

  .top-actions {
    width: 100%;
  }

  .board-frame {
    border-width: 5px;
    padding: 16px;
  }

  .side {
    grid-template-columns: 1fr;
  }
}

body.focus-board {
  overflow: hidden;
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100dvh;
}

body.focus-board .app {
  width: 100vw;
  max-width: 100vw;
  height: 100dvh;
  min-height: 100dvh;
  padding: max(8px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: auto minmax(0, 1fr);
}

html.force-landscape,
body.force-landscape {
  width: 100vw;
  height: 100dvh;
}

body.force-landscape .app {
  position: fixed;
  left: 0;
  top: 0;
  width: 100dvh;
  max-width: none;
  height: 100dvw;
  min-height: 100dvw;
  transform: rotate(90deg) translateY(-100%);
  transform-origin: top left;
}

body.focus-board .play-area {
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
}

body.focus-board .side {
  display: none;
}

body.focus-board .topbar {
  min-height: 64px;
  padding: 10px 12px;
}

body.focus-board h1 {
  display: none;
}

body.focus-board .board-frame {
  width: min(calc((100dvh - 118px) * 0.9), calc(100dvw - 28px));
  max-height: calc(100dvh - 118px);
  margin-top: 8px;
  padding: clamp(14px, 2.6vmin, 24px);
}

@media (orientation: landscape) {
  body.focus-board .board-frame {
    width: min(calc((100dvh - 118px) * 0.86), calc(100dvw - 28px));
    max-height: calc(100dvh - 118px);
  }
}

body.force-landscape .topbar {
  min-height: 58px;
  padding: 8px 12px;
}

body.force-landscape #gameStatus {
  font-size: clamp(18px, 3.4dvh, 26px);
}

body.force-landscape .board-frame {
  width: min(calc((100dvw - 84px) * 0.9), calc(100dvh - 24px));
  max-height: calc(100dvw - 84px);
  margin-top: 6px;
  padding: clamp(12px, 2.2dvh, 20px);
}
