:root {
  --bg-1: #e4f4ed;
  --bg-2: #fde9cf;
  --panel: rgba(255, 255, 255, 0.7);
  --panel-border: rgba(29, 49, 63, 0.16);
  --text: #162234;
  --text-soft: #35516b;
  --accent: #cb6d2e;
  --board-light: #f1eee4;
  --board-dark: #cf9a8e;
  --board-card: #f3f3f4;
  --board-edge: #d9d9dd;
  --select: rgba(255, 220, 94, 0.7);
  --last-move: rgba(111, 170, 249, 0.5);
  --target: rgba(36, 62, 75, 0.38);
  --capture: rgba(199, 66, 37, 0.62);
}

* {
  box-sizing: border-box;
}

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

body {
  font-family:
    "Avenir Next",
    "Baskerville",
    "Palatino Linotype",
    "Hiragino Sans GB",
    "STHeiti",
    serif;
  color: var(--text);
  background:
    radial-gradient(circle at 15% 15%, #fff5de 0%, transparent 40%),
    radial-gradient(circle at 90% 10%, #e3fff3 0%, transparent 35%),
    linear-gradient(150deg, var(--bg-1), var(--bg-2));
  padding:
    max(10px, env(safe-area-inset-top))
    max(12px, env(safe-area-inset-right))
    max(14px, env(safe-area-inset-bottom))
    max(12px, env(safe-area-inset-left));
  -webkit-tap-highlight-color: transparent;
}

.app {
  width: min(960px, 100%);
  margin: 0 auto;
  display: grid;
  gap: 12px;
  overflow-x: hidden;
}

.hero {
  text-align: center;
  padding: 6px 10px 0;
}

.hero h1 {
  margin: 0;
  font-size: clamp(1.7rem, 6vw, 2.4rem);
  letter-spacing: 0.06em;
  font-weight: 700;
}

.hero p {
  margin: 6px 0 0;
  color: var(--text-soft);
  font-size: 0.95rem;
}

.controls {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 14px;
  padding: 10px 12px;
  backdrop-filter: blur(8px);
}

.row {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.row + .row {
  margin-top: 10px;
}

.label {
  font-weight: 600;
  font-size: 0.95rem;
}

.side-group {
  display: flex;
  gap: 6px;
}

button,
select {
  border-radius: 10px;
  border: 1px solid rgba(25, 46, 60, 0.24);
  padding: 8px 12px;
  background: #ffffffcc;
  color: #122036;
  font: inherit;
  font-size: 0.95rem;
}

button {
  cursor: pointer;
}

button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

button:active {
  transform: translateY(1px);
}

.side-btn.active {
  background: linear-gradient(160deg, #ffd8b3, #ffeac8);
  border-color: rgba(150, 78, 25, 0.45);
}

#new-game {
  background: linear-gradient(160deg, #2f7164, #5f9a8f);
  color: #f3f8f7;
  border-color: rgba(21, 68, 60, 0.56);
}

#undo {
  background: linear-gradient(160deg, #e8f1ff, #d8e3f5);
}

.status {
  margin: 10px 0 0;
  min-height: 1.2em;
  font-size: 0.96rem;
  color: #18334a;
}

.search-info {
  margin: 8px 0 0;
  min-height: 1.1em;
  font-size: 0.86rem;
  color: #36506c;
}

.history {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 14px;
  padding: 8px 12px;
  backdrop-filter: blur(8px);
  width: min(96vw, 620px);
  margin: 0 auto;
  min-width: 0;
}

.history-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 6px;
}

.move-list {
  margin: 0;
  padding: 2px 2px 4px;
  height: 2.2em;
  width: 100%;
  max-width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  white-space: nowrap;
  font-family:
    "Menlo",
    "SF Mono",
    "Consolas",
    monospace;
  font-size: 0.85rem;
  color: #112136;
  line-height: 1.45;
}

.move-list::-webkit-scrollbar {
  height: 6px;
}

.move-list::-webkit-scrollbar-thumb {
  border-radius: 999px;
  background: rgba(18, 33, 54, 0.2);
}

.move-list::-webkit-scrollbar-track {
  background: rgba(18, 33, 54, 0.06);
}

.board-wrap {
  --theme-wrap-bg: #f3f3f4;
  --theme-wrap-border: #ececf0;
  --theme-wrap-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.65);
  --theme-board-border-width: 1px;
  --theme-board-border-color: var(--board-edge);
  --theme-board-shadow: 0 1px 1px rgba(15, 23, 42, 0.06);
  --theme-board-bg: #e8e8ea;
  --theme-dark-square-bg:
    linear-gradient(170deg, rgba(255, 255, 255, 0.14), rgba(0, 0, 0, 0.06)),
    #cf9a8e;
  --theme-light-square-bg:
    linear-gradient(170deg, rgba(255, 255, 255, 0.2), rgba(0, 0, 0, 0.04)),
    #f1eee4;
  display: flex;
  justify-content: center;
  width: min(96vw, 620px);
  margin: 0 auto;
  padding: 14px;
  border-radius: 22px;
  background: var(--theme-wrap-bg);
  border: 1px solid var(--theme-wrap-border);
  box-shadow: var(--theme-wrap-shadow);
}

.board-stack {
  position: relative;
  width: 100%;
}

.board-wrap.theme-maple1 {
  --theme-wrap-bg: #f7efe3;
  --theme-wrap-border: #e6d6bf;
  --theme-wrap-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.68);
  --theme-board-border-width: 8px;
  --theme-board-border-color: #b37d4a;
  --theme-board-shadow:
    0 12px 26px rgba(59, 39, 24, 0.18),
    inset 0 0 0 2px #d8ae7f,
    inset 0 1px 0 rgba(255, 255, 255, 0.22);
  --theme-board-bg: linear-gradient(140deg, #d7af80, #b98854);
  --theme-dark-square-bg:
    linear-gradient(140deg, rgba(255, 255, 255, 0.18), rgba(0, 0, 0, 0.12)),
    linear-gradient(150deg, #d2a06d 0%, #c6925f 58%, #b57e4e 100%);
  --theme-light-square-bg:
    linear-gradient(140deg, rgba(255, 255, 255, 0.28), rgba(0, 0, 0, 0.05)),
    linear-gradient(150deg, #f4e0c0 0%, #e9cfa6 60%, #ddbb8c 100%);
}

.board-wrap.theme-maple3 {
  --theme-wrap-bg: #f9f2e7;
  --theme-wrap-border: #e8d5bd;
  --theme-wrap-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.66);
  --theme-board-border-width: 8px;
  --theme-board-border-color: #b8763d;
  --theme-board-shadow:
    0 12px 26px rgba(72, 45, 23, 0.2),
    inset 0 0 0 2px #dea36b,
    inset 0 1px 0 rgba(255, 255, 255, 0.2);
  --theme-board-bg: linear-gradient(140deg, #d49d61, #b97840);
  --theme-dark-square-bg:
    linear-gradient(140deg, rgba(255, 255, 255, 0.18), rgba(0, 0, 0, 0.12)),
    linear-gradient(150deg, #dba86d 0%, #c9884f 58%, #b5733b 100%);
  --theme-light-square-bg:
    linear-gradient(140deg, rgba(255, 255, 255, 0.28), rgba(0, 0, 0, 0.05)),
    linear-gradient(150deg, #f8e7c8 0%, #f0d7a5 60%, #e6c68f 100%);
}

.board {
  width: 100%;
  aspect-ratio: 1;
  border-radius: 0;
  overflow: hidden;
  border: var(--theme-board-border-width) solid var(--theme-board-border-color);
  box-shadow: var(--theme-board-shadow);
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  background: var(--theme-board-bg);
}

.trail-layer {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 3;
}

.square {
  position: relative;
  -webkit-appearance: none;
  appearance: none;
  -webkit-border-radius: 0;
  border-radius: 0;
  border: 0;
  margin: 0;
  width: 100%;
  aspect-ratio: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  background: transparent;
  box-shadow: none;
  touch-action: manipulation;
}

.square:focus,
.square:focus-visible {
  outline: none;
}

.square:active {
  transform: none;
}

.square.dark {
  background: var(--theme-dark-square-bg);
}

.square.light {
  background: var(--theme-light-square-bg);
}

.square.selected {
  background-image: linear-gradient(0deg, var(--select), var(--select));
}

.square.last-move {
  background-image: linear-gradient(0deg, var(--last-move), var(--last-move));
}

.square.target::after {
  content: "";
  position: absolute;
  width: 24%;
  height: 24%;
  border-radius: 50%;
  background: var(--target);
}

.square.target.capture::after {
  width: 76%;
  height: 76%;
  background: transparent;
  border: 4px solid var(--capture);
}

.piece {
  display: block;
  width: 74%;
  height: 74%;
  object-fit: contain;
  background: transparent;
  user-select: none;
  pointer-events: none;
  transform: translateY(-1px);
  filter:
    drop-shadow(0 1px 1px rgba(0, 0, 0, 0.18))
    drop-shadow(0 3px 2px rgba(0, 0, 0, 0.12));
}

.piece[hidden] {
  display: none !important;
}

@media (max-width: 859px) {
  body {
    padding:
      max(6px, env(safe-area-inset-top))
      max(10px, env(safe-area-inset-right))
      max(10px, env(safe-area-inset-bottom))
      max(10px, env(safe-area-inset-left));
  }

  .app {
    gap: 8px;
  }

  .hero {
    padding: 2px 4px 0;
  }

  .hero h1 {
    font-size: clamp(1.25rem, 4.8vw, 1.6rem);
    letter-spacing: 0.03em;
  }

  .hero p {
    display: none;
  }

  .controls {
    padding: 7px 8px;
    border-radius: 12px;
  }

  .row {
    gap: 6px;
  }

  .row + .row {
    margin-top: 6px;
  }

  .label {
    font-size: 0.86rem;
  }

  button,
  select {
    padding: 6px 8px;
    font-size: 0.84rem;
    border-radius: 8px;
  }

  .status {
    margin-top: 6px;
    min-height: 1em;
    font-size: 0.86rem;
  }

  .search-info {
    margin-top: 4px;
    font-size: 0.76rem;
  }

  .board-wrap {
    padding: 10px;
    border-radius: 16px;
  }
}

@media (min-width: 860px) {
  .app {
    grid-template-columns: 1fr minmax(320px, 380px);
    grid-template-areas:
      "hero board"
      "controls board"
      "history board";
    align-items: start;
  }

  .hero {
    grid-area: hero;
    text-align: left;
    padding-left: 0;
  }

  .controls {
    grid-area: controls;
  }

  .board-wrap {
    grid-area: board;
    justify-content: flex-end;
  }

  .history {
    grid-area: history;
    width: 100%;
    margin: 0;
  }
}
