:root {
  --paper: #efe3c5;
  --paper-deep: #d2ba84;
  --ink: #231815;
  --red: #9f1f1f;
  --black: #23272f;
  --accent: #4c6b58;
  --panel: rgba(255, 250, 236, 0.92);
  --panel-strong: rgba(255, 252, 244, 0.96);
  --shadow: 0 22px 60px rgba(45, 30, 15, 0.18);
  --ui-font-family: "Bitter", "Georgia", serif;
  --chinese-font-family: "Songti SC", "STSong", "SimSun", "Noto Serif CJK SC", "Source Han Serif SC", serif;
  --xiangqi-piece-font-family: "KaiTi", "Kaiti SC", "STKaiti", "BiauKai", "DFKai-SB", "Kai", "Noto Serif SC", "Source Han Serif SC", "Songti SC", "STSong", "SimSun", serif;
  --xiangqi-piece-font-family-active: var(--xiangqi-piece-font-family);
  --piece-font-family: var(--chinese-font-family);
  --river-font-family: var(--chinese-font-family);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: var(--ui-font-family);
  color: var(--ink);
  min-height: 100vh;
  background:
    radial-gradient(circle at top left, rgba(255, 241, 203, 0.82), transparent 30%),
    radial-gradient(circle at bottom right, rgba(126, 156, 135, 0.3), transparent 35%),
    linear-gradient(140deg, #f4ead0, #d8c59a 55%, #c2b082);
}

.page-shell {
  width: min(1400px, calc(100vw - 32px));
  margin: 0 auto;
  padding: 32px 0 40px;
}

.hero {
  margin-bottom: 24px;
}

.eyebrow {
  margin: 0 0 8px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--accent);
  font-size: 0.8rem;
}

.hero h1,
.mode-selector-card h2 {
  margin: 0;
  font-size: clamp(2rem, 4vw, 3.4rem);
}

.hero-copy,
.helper-copy {
  line-height: 1.55;
}

.hero-copy {
  width: min(760px, 100%);
  margin: 12px 0 0;
}

.card,
.board-panel {
  background: var(--panel);
  border: 1px solid rgba(60, 40, 20, 0.12);
  border-radius: 24px;
  box-shadow: var(--shadow);
  backdrop-filter: blur(10px);
}

.card {
  padding: 18px;
}

.board-panel {
  padding: 20px;
}

.mode-selector-card {
  margin-bottom: 24px;
}

.mode-selector-header {
  display: flex;
  justify-content: space-between;
  gap: 24px;
  align-items: start;
}

.mode-nav {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
  margin-top: 16px;
}

.mode-tab {
  display: grid;
  gap: 6px;
  text-align: left;
  min-height: 88px;
}

.mode-tab span {
  font-size: 0.9rem;
  line-height: 1.35;
  color: rgba(35, 24, 21, 0.75);
}

.mode-tab.active {
  background: linear-gradient(135deg, rgba(76, 107, 88, 0.18), rgba(76, 107, 88, 0.06));
  border-color: rgba(76, 107, 88, 0.35);
}

.mode-panel[hidden] {
  display: none;
}

.mode-shell,
.workspace {
  display: grid;
  grid-template-columns: minmax(540px, 1.15fr) minmax(320px, 0.85fr);
  gap: 24px;
}

.control-panel {
  display: grid;
  gap: 16px;
}

.board-toolbar,
.toolbar-group,
.inline-fields {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
}

.board-toolbar {
  justify-content: space-between;
  margin-bottom: 18px;
}

.wrap {
  flex-wrap: wrap;
}

button,
select,
input,
textarea {
  font: inherit;
}

button,
select,
input,
textarea {
  border-radius: 10px;
  border: 1px solid rgba(50, 34, 18, 0.2);
  background: rgba(255, 252, 244, 0.95);
}

button {
  min-width: 96px;
  min-height: 48px;
  padding: 10px 12px;
  cursor: pointer;
  transition: transform 160ms ease, background 160ms ease, box-shadow 160ms ease;
}

button:hover:enabled {
  transform: translateY(-1px);
  box-shadow: 0 10px 24px rgba(67, 51, 20, 0.12);
}

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

.primary-button {
  background: linear-gradient(135deg, #5d7d69, #395241);
  color: #fff9ee;
}

.file-dropzone {
  display: grid;
  gap: 4px;
  padding: 14px 16px;
  border: 1px dashed rgba(76, 107, 88, 0.42);
  border-radius: 14px;
  background: rgba(255, 252, 244, 0.72);
  color: rgba(35, 24, 21, 0.8);
  cursor: pointer;
  transition: border-color 160ms ease, background 160ms ease, transform 160ms ease, box-shadow 160ms ease;
}

.file-dropzone strong {
  font-size: 0.98rem;
  color: var(--accent);
}

.file-dropzone span {
  font-size: 0.92rem;
}

.file-dropzone:hover,
.file-dropzone.active,
.file-dropzone:focus-visible {
  border-color: rgba(76, 107, 88, 0.72);
  background: rgba(240, 247, 239, 0.95);
  box-shadow: 0 12px 26px rgba(67, 51, 20, 0.08);
  transform: translateY(-1px);
  outline: none;
}

.status-pill {
  display: inline-flex;
  align-items: center;
  min-height: 38px;
  padding: 0 12px;
  border-radius: 999px;
  background: rgba(72, 100, 83, 0.12);
  color: var(--accent);
  font-size: 0.92rem;
}

.status-pill.turn-red {
  background: rgba(159, 31, 31, 0.14);
  color: var(--red);
  border-color: rgba(159, 31, 31, 0.22);
}

.status-pill.turn-black {
  background: rgba(35, 39, 47, 0.12);
  color: var(--black);
  border-color: rgba(35, 39, 47, 0.18);
}

.board-wrap {
  width: min(100%, 720px);
  margin: 0 auto;
  container-type: inline-size;
  --coord-font-size: clamp(0.9rem, 2.8cqw, 1.12rem);
}

.canvas-board-wrap {
  width: min(100%, 760px);
  margin: 0 auto;
}

.board-canvas {
  width: 100%;
  height: auto;
  display: block;
  border-radius: 18px;
  background: rgba(255, 248, 235, 0.5);
  border: 1px solid rgba(79, 53, 24, 0.12);
}

.board-row {
  display: grid;
  grid-template-columns: 28px minmax(0, 1fr);
  gap: 8px;
  align-items: stretch;
}

.board {
  position: relative;
  container-type: inline-size;
  display: grid;
  grid-template-columns: repeat(9, minmax(0, 1fr));
  grid-template-rows: repeat(10, minmax(0, 1fr));
  width: 100%;
  aspect-ratio: 9 / 10;
  --cell-size: 11.111cqw;
  --river-font-size: calc(var(--cell-size) * 0.86);
  --river-letter-spacing: calc(var(--cell-size) * 0.035);
  --marker-size: calc(var(--cell-size) * 0.17);
  --piece-disc-size: calc(var(--cell-size) * 0.82);
  --piece-font-size: calc(var(--piece-disc-size) * 0.49);
  border-radius: 24px;
  overflow: hidden;
}

.board-xiangqi {
  background: linear-gradient(180deg, #f4dba7, #dbb573);
  --piece-disc-size: calc(var(--cell-size) * 0.94);
  --piece-font-size: calc(var(--piece-disc-size) * 0.51);
}

.mode-shell-xiangqi {
  --piece-font-family: var(--xiangqi-piece-font-family-active);
  --river-font-family: var(--xiangqi-piece-font-family-active);
}

.mode-shell-xiangqi .board-toolbar label {
  display: flex;
  flex-direction: column;
  gap: 3px;
  width: auto;
  min-width: 0;
  font-size: 0.85rem;
}

.mode-shell-xiangqi .board-toolbar select {
  width: auto;
  min-width: 118px;
  padding: 6px 10px;
}

.mode-shell-go .board-panel {
  display: grid;
  gap: 18px;
}

.mode-shell-go .board-toolbar label,
.mode-shell-shogi .board-toolbar label {
  display: flex;
  flex-direction: column;
  gap: 3px;
  width: auto;
  min-width: 0;
  font-size: 0.85rem;
}

.mode-shell-go .board-toolbar select,
.mode-shell-shogi .board-toolbar select {
  width: auto;
  min-width: 108px;
  padding: 6px 10px;
}

.mode-shell-shogi .board-panel,
.mode-shell-shogi .card {
  position: relative;
  overflow: hidden;
  background:
    linear-gradient(180deg, rgba(255, 252, 244, 0.97), rgba(244, 232, 205, 0.92)),
    var(--panel);
}

.mode-shell-shogi .board-panel::before,
.mode-shell-shogi .card::before {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background:
    linear-gradient(135deg, rgba(124, 92, 42, 0.08), transparent 28%),
    radial-gradient(circle at top right, rgba(76, 107, 88, 0.08), transparent 32%);
}

.mode-shell-shogi .board-panel::after,
.mode-shell-shogi .card::after {
  content: "";
  position: absolute;
  inset: 10px;
  border: 1px solid rgba(102, 73, 33, 0.08);
  border-radius: 18px;
  pointer-events: none;
}

.mode-shell-shogi .card > *,
.mode-shell-shogi .board-panel > * {
  position: relative;
  z-index: 1;
}

.mode-shell-shogi .card h2 {
  display: flex;
  align-items: center;
  gap: 10px;
}

.mode-shell-shogi .card h2::before {
  content: "";
  width: 18px;
  height: 18px;
  border-radius: 4px;
  background:
    linear-gradient(135deg, rgba(122, 90, 40, 0.22), rgba(76, 107, 88, 0.12)),
    rgba(255, 250, 240, 0.9);
  border: 1px solid rgba(96, 69, 30, 0.18);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.35);
}

.mode-shell-shogi .canvas-board-wrap {
  padding: 8px 10px;
  border-radius: 26px;
  background:
    linear-gradient(180deg, rgba(255, 249, 238, 0.8), rgba(228, 207, 165, 0.4)),
    rgba(255, 251, 241, 0.88);
  border: 1px solid rgba(92, 66, 28, 0.12);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.55);
}

.mode-shell-shogi .board-toolbar {
  padding-bottom: 8px;
  border-bottom: 1px solid rgba(89, 64, 28, 0.1);
  align-items: end;
}

.mode-shell-shogi .board-footer {
  margin-top: 12px;
  padding-top: 10px;
  border-top: 1px dashed rgba(89, 64, 28, 0.18);
}

.mode-shell-shogi .helper-copy {
  color: rgba(35, 24, 21, 0.74);
}

.mode-shell-shogi {
  gap: 16px;
}

.mode-shell-shogi .control-panel {
  gap: 12px;
}

.mode-shell-shogi .board-panel {
  gap: 12px;
  padding: 16px;
}

.mode-shell-shogi .card {
  padding: 14px;
}

.mode-shell-shogi .board-toolbar {
  margin-bottom: 10px;
}

.mode-shell-shogi .toolbar-group,
.mode-shell-shogi .inline-fields {
  gap: 8px;
}

.mode-shell-shogi .card h2,
.mode-shell-shogi .board-panel h2 {
  margin: 0 0 8px;
}

.mode-shell-shogi .shogi-board-subpanel {
  margin-top: 2px;
  padding: 14px 16px;
}

.mode-shell-shogi .shogi-board-subpanel .field-label {
  margin-bottom: 8px;
}

.mode-shell-shogi .field-label,
.mode-shell-shogi label {
  gap: 4px;
}

.mode-shell-shogi textarea,
.mode-shell-shogi input,
.mode-shell-shogi select {
  padding: 8px 10px;
}

.mode-shell-shogi .toggle-list {
  gap: 8px;
  margin-top: 8px;
}

.mode-shell-shogi .gif-preview {
  margin-top: 6px;
}

.mode-shell-shogi .palette-option {
  min-height: 68px;
  padding: 6px;
  background: rgba(255, 252, 244, 0.38);
  border-color: rgba(92, 66, 28, 0.1);
  box-shadow: none;
}

.mode-shell-shogi .palette-option.active {
  background: rgba(76, 107, 88, 0.1);
  border-color: rgba(76, 107, 88, 0.26);
  box-shadow: inset 0 0 0 1px rgba(76, 107, 88, 0.08);
}

.mode-shell-shogi .palette-piece {
  width: clamp(62px, 4.8vw, 82px);
  height: clamp(62px, 4.8vw, 82px);
  border: 0;
  border-radius: 0;
  background: transparent;
  box-shadow: none;
}

.mode-shell-shogi .erase-piece {
  border: 2px solid rgba(159, 31, 31, 0.35);
  border-radius: 14px;
  background: rgba(255, 252, 244, 0.7);
}

.go-canvas-board-wrap {
  width: min(100%, 860px);
}

.mode-shell-go .palette-piece {
  width: 34px;
  height: 34px;
  background: transparent;
  border: 0;
  box-shadow: none;
  font-size: 2rem;
}

.mode-shell-go .erase-piece {
  width: 34px;
  height: 34px;
  border: 2px solid rgba(159, 31, 31, 0.45);
  background: rgba(255, 252, 244, 0.9);
}

.board-xiangqi::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 0;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 9 10' preserveAspectRatio='none'%3E%3Cg fill='none' stroke='%23502e12' stroke-width='0.04' stroke-linecap='square'%3E%3Crect x='0.2' y='0.2' width='8.6' height='9.6' rx='0.22'/%3E%3Cpath d='M0.5 0.5H8.5M0.5 1.5H8.5M0.5 2.5H8.5M0.5 3.5H8.5M0.5 4.5H8.5M0.5 5.5H8.5M0.5 6.5H8.5M0.5 7.5H8.5M0.5 8.5H8.5M0.5 9.5H8.5'/%3E%3Cpath d='M0.5 0.5V9.5M1.5 0.5V4.5M1.5 5.5V9.5M2.5 0.5V4.5M2.5 5.5V9.5M3.5 0.5V4.5M3.5 5.5V9.5M4.5 0.5V4.5M4.5 5.5V9.5M5.5 0.5V4.5M5.5 5.5V9.5M6.5 0.5V4.5M6.5 5.5V9.5M7.5 0.5V4.5M7.5 5.5V9.5M8.5 0.5V9.5'/%3E%3Cpath d='M3.5 0.5L5.5 2.5M5.5 0.5L3.5 2.5M3.5 7.5L5.5 9.5M5.5 7.5L3.5 9.5'/%3E%3C/g%3E%3C/svg%3E");
  background-size: 100% 100%;
  background-repeat: no-repeat;
  pointer-events: none;
}

.board-xiangqi::after {
  content: attr(data-river-text);
  position: absolute;
  z-index: 0;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  font-family: var(--river-font-family);
  font-size: var(--river-font-size);
  letter-spacing: var(--river-letter-spacing);
  color: rgba(35, 24, 21, 0.55);
  pointer-events: none;
  white-space: pre;
}

.square {
  position: relative;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  background: transparent;
  border: 0;
  border-radius: 0;
  padding: 0;
}

.square::before {
  content: "";
  position: relative;
  z-index: 1;
  width: var(--marker-size);
  height: var(--marker-size);
  border-radius: 999px;
  background: rgba(66, 39, 16, 0.5);
  opacity: 0;
  transition: opacity 140ms ease;
}

.square.legal::before,
.square.selected::before {
  opacity: 1;
}

.square.legal::before {
  background: rgba(77, 106, 89, 0.75);
}

.square.selected::before {
  background: rgba(169, 44, 36, 0.75);
}

.piece {
  position: absolute;
  z-index: 2;
  width: var(--piece-disc-size);
  height: var(--piece-disc-size);
  display: flex;
  align-items: center;
  justify-content: center;
  left: 49.2%;
  top: 50%;
  transform: translate(-50%, -50%);
  border-radius: 999px;
  border: 2px solid rgba(80, 46, 18, 0.9);
  background: radial-gradient(circle at 30% 25%, #fff9ec, #ead39d 65%, #d6b171);
  box-shadow: 0 10px 18px rgba(60, 36, 14, 0.18);
  font-family: var(--piece-font-family);
  font-size: var(--piece-font-size);
  font-weight: 700;
}

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

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

.board-coords {
  display: grid;
  font-size: var(--coord-font-size);
  color: rgba(35, 24, 21, 0.7);
}

.board-coords.files {
  grid-template-columns: repeat(9, 1fr);
  margin: 0 0 0 36px;
}

.board-coords.ranks {
  grid-template-rows: repeat(10, 1fr);
}

.board-coords span {
  display: flex;
  align-items: center;
  justify-content: center;
}

.board-footer {
  margin-top: 16px;
  display: grid;
  gap: 8px;
}

.board-subpanel {
  margin-top: 18px;
  background: rgba(255, 251, 241, 0.86);
  box-shadow: none;
}

.go-board-subpanel {
  margin-top: 0;
}

.go-board-subpanel-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 18px;
}

.go-board-subsection {
  display: grid;
  gap: 12px;
  align-content: start;
}

.go-subsection-header {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
  flex-wrap: wrap;
}

.go-subsection-header h2 {
  margin: 0;
  font-size: 1.2rem;
}

.go-branch-tree {
  display: grid;
  gap: 8px;
  max-height: 320px;
  overflow: auto;
  padding: 6px 4px 2px;
}

.go-branch-line {
  display: flex;
  align-items: center;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 2px;
}

.go-tree-move {
  min-width: 0;
  min-height: 0;
  width: fit-content;
  display: inline-flex;
  align-items: center;
  gap: 0;
  padding: 4px;
  border-radius: 999px;
  background: transparent;
  border-color: transparent;
  box-shadow: none;
  position: relative;
}

.go-tree-move.active {
  background: rgba(76, 107, 88, 0.08);
  border-color: rgba(76, 107, 88, 0.24);
}

.go-tree-move.active-preview {
  box-shadow: 0 0 0 2px rgba(159, 31, 31, 0.15);
}

.go-tree-move::before {
  content: "";
  position: absolute;
  right: calc(100% + 4px);
  top: 50%;
  width: 18px;
  border-top: 1px solid rgba(79, 53, 24, 0.3);
  transform: translateY(-50%);
}

.go-tree-move.first::before {
  display: none;
}

.go-tree-stone {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border-radius: 999px;
  font-size: 0.8rem;
  font-weight: 700;
  border: 1px solid rgba(70, 48, 24, 0.2);
}

.go-tree-stone.black {
  background: linear-gradient(180deg, #4e535b, #111214);
  color: #f7efe1;
  border-color: rgba(17, 18, 20, 0.5);
}

.go-tree-stone.white {
  background: linear-gradient(180deg, #ffffff, #dde1e8);
  color: #7d2417;
}

.go-tree-empty {
  color: rgba(35, 24, 21, 0.7);
}

.go-board-toggles {
  margin-top: 0;
}

.go-crop-debug {
  margin: 0;
  padding: 10px 12px;
  border-radius: 12px;
  background: rgba(76, 107, 88, 0.08);
  border: 1px solid rgba(76, 107, 88, 0.16);
  font: 0.9rem/1.45 ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  white-space: pre-wrap;
}

.palette {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
}

.palette-option {
  min-height: 76px;
  display: grid;
  place-items: center;
  padding: 8px;
  text-align: center;
  aspect-ratio: 1 / 1;
}

.palette-option.active {
  background: rgba(76, 107, 88, 0.18);
  border-color: rgba(76, 107, 88, 0.35);
}

.palette-piece {
  width: clamp(58px, 4.4vw, 72px);
  height: clamp(58px, 4.4vw, 72px);
  display: grid;
  place-items: center;
  border-radius: 999px;
  border: 2px solid rgba(80, 46, 18, 0.9);
  background: radial-gradient(circle at 30% 25%, #fff9ec, #ead39d 65%, #d6b171);
  font-family: var(--piece-font-family);
  font-size: clamp(1.55rem, 2.6vw, 1.9rem);
  font-weight: 700;
}

.palette-piece.sprite {
  background-repeat: no-repeat;
  image-rendering: pixelated;
}

.palette-piece.lishogi-preview {
  border: 0;
  border-radius: 0;
  background-color: transparent;
  background-repeat: no-repeat;
  background-position: center;
}

.palette-piece.chess-palette-piece {
  border: 0;
  border-radius: 0;
  background: transparent;
  box-shadow: none;
}

.chess-piece-asset {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
  user-select: none;
  -webkit-user-drag: none;
}

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

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

.erase-piece {
  position: relative;
  border-color: rgba(159, 31, 31, 0.7);
}

.erase-piece::before,
.erase-piece::after {
  content: "";
  position: absolute;
  width: 64%;
  height: 4px;
  border-radius: 999px;
  background: var(--red);
}

.erase-piece::before {
  transform: rotate(45deg);
}

.erase-piece::after {
  transform: rotate(-45deg);
}

.field-label,
label {
  display: grid;
  gap: 6px;
  font-size: 0.95rem;
}

textarea,
input,
select {
  width: 100%;
  padding: 10px 12px;
}

.toggle-list {
  display: grid;
  gap: 10px;
  margin-top: 12px;
}

.toggle-option {
  display: flex;
  align-items: center;
  gap: 10px;
}

.toggle-option input {
  width: auto;
  margin: 0;
}

.gif-preview {
  width: 100%;
  display: none;
  margin-top: 12px;
  border-radius: 18px;
  border: 1px solid rgba(70, 48, 24, 0.12);
}

code {
  padding: 0.15rem 0.35rem;
  border-radius: 6px;
  background: rgba(76, 107, 88, 0.12);
}

@media (max-width: 1180px) {
  .mode-shell,
  .workspace {
    grid-template-columns: 1fr;
  }

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

@media (max-width: 720px) {
  .page-shell {
    width: min(100vw - 20px, 100%);
    padding-top: 20px;
  }

  .board-panel,
  .card {
    border-radius: 18px;
  }

  .palette {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .mode-nav {
    grid-template-columns: 1fr;
  }
}
