:root {
  --paper: #f6edd6;
  --paper-deep: #edd9ac;
  --wood: #d7b06d;
  --wood-dark: #af7f36;
  --ink: #28180a;
  --ink-soft: #5d4527;
  --black-piece: #1d1108;
  --white-piece: #f7f2e8;
  --accent: #c15f2d;
  --ok: #2f6b3e;
  --danger: #b64033;
  --panel: rgba(255, 250, 238, 0.82);
  --line: rgba(61, 35, 14, 0.38);
  --page-padding: clamp(12px, 2vw, 24px);
  --frame-padding: clamp(8px, 1.2vw, 12px);
  --panel-width: 320px;
  --stage-gap: clamp(4px, 0.8vw, 10px);
  --tap-size: 44px;
  --cell-size: clamp(44px, min(calc((100dvh - 180px) / 9), calc((100vw - 420px) / 11.4)), 78px);
  --board-size: calc(var(--cell-size) * 9);
  --hand-col-width: clamp(68px, calc(var(--cell-size) * 1.16), 92px);
  --hand-row-width: clamp(56px, calc(var(--cell-size) * 1.2), 78px);
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
}

body {
  margin: 0;
  font-family: 'Avenir Next', 'Hiragino Kaku Gothic ProN', 'Yu Gothic', sans-serif;
  color: var(--ink);
  background:
    radial-gradient(circle at 14% 16%, rgba(250, 243, 222, 0.8), transparent 44%),
    radial-gradient(circle at 88% 12%, rgba(242, 211, 154, 0.58), transparent 42%),
    linear-gradient(150deg, #f8f2df, #f1dfb8 48%, #e6c583 100%);
  min-height: 100vh;
  overflow-x: hidden;
}

.app-shell {
  max-width: 1600px;
  margin: 0 auto;
  padding: var(--page-padding);
  animation: reveal 420ms ease-out both;
}

@keyframes reveal {
  from {
    opacity: 0;
    transform: translateY(14px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.layout-shell {
  display: grid;
  grid-template-columns: max-content minmax(280px, var(--panel-width));
  gap: clamp(12px, 2vw, 24px);
  align-items: start;
  justify-content: center;
}

.game-layout {
  width: min(100%, calc(var(--board-size) + (var(--hand-col-width) * 2) + (var(--frame-padding) * 2) + 28px));
  overflow-x: auto;
  overflow-y: visible;
  padding-bottom: 4px;
  overscroll-behavior-x: contain;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-x pinch-zoom;
  scrollbar-gutter: stable both-edges;
}

.board-column {
  border: 1px solid var(--line);
  border-radius: 18px;
  background: rgba(255, 253, 247, 0.8);
  padding: var(--frame-padding);
  box-shadow: 0 14px 28px rgba(96, 51, 14, 0.1);
  width: max-content;
  min-width: 0;
  margin: 0 auto;
}

.board-stage {
  display: grid;
  grid-template-columns: var(--hand-col-width) var(--board-size) var(--hand-col-width);
  gap: var(--stage-gap);
  align-items: start;
}

.hand-side {
  width: var(--hand-col-width);
  min-width: 0;
  display: grid;
  gap: 6px;
  align-content: start;
}

.hand-side h2 {
  margin: 0;
  font-size: 0.78rem;
  color: var(--ink-soft);
  text-align: center;
}

.side-panel {
  display: grid;
  gap: 10px;
  align-content: start;
  width: min(100%, var(--panel-width));
  position: sticky;
  top: var(--page-padding);
}

.help-links {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.help-links a {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: var(--tap-size);
  text-decoration: none;
  color: var(--ink);
  border: 1px solid rgba(73, 42, 17, 0.34);
  background: rgba(255, 255, 255, 0.7);
  padding: 8px 12px;
  border-radius: 999px;
  font-size: 0.92rem;
  text-align: center;
}

.controls {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: end;
  gap: 12px;
  padding: 14px;
  border-radius: 16px;
  border: 1px solid var(--line);
  background: var(--panel);
  backdrop-filter: blur(4px);
}

.control-item {
  display: grid;
  gap: 6px;
  min-width: 0;
}

label {
  font-size: 0.9rem;
  color: var(--ink-soft);
}

select,
button {
  font: inherit;
}

select {
  width: 100%;
  min-width: 0;
  min-height: var(--tap-size);
  padding: 8px 10px;
  border-radius: 10px;
  border: 1px solid rgba(62, 34, 11, 0.34);
  background: #fffdf8;
  color: var(--ink);
}

#newGameBtn {
  min-height: var(--tap-size);
  border-radius: 10px;
  cursor: pointer;
  touch-action: manipulation;
}

#newGameBtn {
  border: none;
  padding: 10px 15px;
  background: linear-gradient(135deg, #be5f27, #9d4820);
  color: #fff6eb;
  font-weight: 600;
  white-space: nowrap;
}

#newGameBtn:hover {
  filter: brightness(1.08);
}

.info-panel {
  display: grid;
  gap: 10px;
  align-content: start;
}

.stat-card {
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px;
  background: var(--panel);
  display: grid;
  gap: 4px;
}

.stat-card span {
  color: var(--ink-soft);
  font-size: 0.85rem;
}

.stat-card strong {
  font-size: 1.04rem;
}

.history-panel {
  display: grid;
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 12px 14px;
  background: var(--panel);
  backdrop-filter: blur(4px);
}

.history-copy {
  display: grid;
  gap: 4px;
  min-width: 0;
}

.history-copy span {
  color: var(--ink-soft);
  font-size: 0.85rem;
}

.history-copy strong {
  font-size: 1rem;
  overflow-wrap: anywhere;
}

.status-line {
  margin: 0;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: rgba(255, 253, 245, 0.9);
  padding: 12px 14px;
  color: #3a2715;
  min-height: 56px;
  display: flex;
  align-items: center;
  line-height: 1.45;
}

.hand {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 0;
  min-height: 36px;
  align-content: flex-start;
}

.hand-piece {
  border: 1px solid rgba(72, 43, 18, 0.4);
  border-radius: 10px;
  background: #fff9ea;
  color: var(--ink);
  padding: 6px 8px;
  min-width: 0;
  min-height: var(--tap-size);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font-size: 0.9rem;
}

.hand-piece.button {
  cursor: pointer;
  touch-action: manipulation;
}

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

.hand-piece.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(193, 95, 45, 0.25);
}

.hand-piece-icon {
  width: clamp(22px, calc(var(--cell-size) * 0.36), 30px);
  height: clamp(22px, calc(var(--cell-size) * 0.36), 30px);
  object-fit: contain;
  display: block;
}

.hand-cpu .hand-piece-icon {
  transform: rotate(180deg);
}

.hand-piece-count {
  font-weight: 600;
  font-size: 0.76rem;
  line-height: 1;
}

.hand-vertical {
  flex-direction: column;
  flex-wrap: nowrap;
}

.hand-vertical .hand-piece {
  width: var(--hand-col-width);
  justify-content: space-between;
}

.board {
  margin: 0;
  width: var(--board-size);
  height: var(--board-size);
  display: grid;
  grid-template-columns: repeat(9, var(--cell-size));
  grid-template-rows: repeat(9, var(--cell-size));
  border: 2px solid rgba(89, 54, 20, 0.75);
  background:
    linear-gradient(140deg, rgba(255, 241, 207, 0.92), rgba(222, 179, 100, 0.95));
}

.cell {
  border: 1px solid rgba(79, 47, 17, 0.55);
  background: transparent;
  color: var(--ink);
  padding: 0;
  margin: 0;
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: clamp(0.72rem, 1.4vw, 1.05rem);
  position: relative;
  cursor: pointer;
  width: var(--cell-size);
  height: var(--cell-size);
  min-width: var(--cell-size);
  min-height: var(--cell-size);
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.cell:disabled {
  cursor: default;
}

.cell .piece {
  width: 90%;
  height: 90%;
  display: grid;
  place-items: center;
  pointer-events: none;
}

.cell .piece.owner-w {
  transform: rotate(180deg);
}

.cell .piece-image {
  width: 100%;
  height: 100%;
  object-fit: contain;
  user-select: none;
  -webkit-user-drag: none;
}

.cell.target::after {
  content: '';
  position: absolute;
  inset: 5px;
  border: 2px solid rgba(45, 107, 62, 0.55);
  border-radius: 5px;
  pointer-events: none;
}

.cell.selected {
  box-shadow: inset 0 0 0 3px rgba(193, 95, 45, 0.52);
}

.cell.last-move-from {
  background: rgba(193, 95, 45, 0.14);
}

.cell.last-move-to {
  background: rgba(47, 107, 62, 0.18);
}

.cell.king-check {
  box-shadow: inset 0 0 0 3px rgba(182, 64, 51, 0.63);
}

@media (max-width: 1180px) {
  :root {
    --cell-size: clamp(40px, min(calc((100dvh - 190px) / 9), calc((100vw - 360px) / 9.9)), 70px);
  }

  .layout-shell {
    grid-template-columns: max-content 280px;
  }
}

@media (max-width: 1140px) {
  :root {
    --cell-size: clamp(34px, min(calc((100vw - (var(--page-padding) * 2) - (var(--frame-padding) * 2) - 18px) / 9), calc((100dvh - 132px) / 11.4)), 60px);
  }

  .layout-shell {
    grid-template-columns: minmax(0, 1fr);
    gap: 8px;
    justify-items: stretch;
  }

  .side-panel {
    position: static;
    width: min(100%, 760px);
    margin: 0 auto;
  }

  .game-layout {
    width: 100%;
    max-width: 760px;
    margin: 0 auto;
  }

  .board-column {
    width: 100%;
    max-width: 100%;
  }

  .board-stage {
    grid-template-columns: minmax(0, 1fr);
    grid-template-areas:
      'cpu'
      'board'
      'player';
    justify-items: center;
  }

  .cpu-side {
    grid-area: cpu;
  }

  .player-side {
    grid-area: player;
  }

  .board {
    grid-area: board;
  }

  .hand-side {
    width: 100%;
    max-width: var(--board-size);
  }

  .hand-side h2 {
    text-align: left;
    padding-inline: 4px;
  }

  .hand-vertical {
    flex-direction: row;
    flex-wrap: wrap;
  }

  .hand-vertical .hand-piece {
    width: auto;
    min-width: var(--hand-row-width);
    flex: 1 1 var(--hand-row-width);
    justify-content: center;
  }
}

@media (max-width: 640px) {
  .app-shell {
    padding:
      max(8px, env(safe-area-inset-top))
      max(8px, env(safe-area-inset-right))
      max(12px, env(safe-area-inset-bottom))
      max(8px, env(safe-area-inset-left));
  }

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

  .controls,
  .history-panel {
    grid-template-columns: minmax(0, 1fr);
  }

  #newGameBtn {
    width: 100%;
  }

  .status-line {
    min-height: 64px;
    padding: 12px;
  }
}

@media (max-width: 480px) {
  :root {
    --cell-size: clamp(32px, min(calc((100vw - 28px) / 9), calc((100dvh - 120px) / 11.2)), 42px);
    --hand-row-width: clamp(52px, calc(var(--cell-size) * 1.22), 68px);
  }

  .board-column {
    padding: 6px;
    border-radius: 16px;
  }

  .cell.target::after {
    inset: 3px;
  }

  .hand-piece {
    padding: 6px;
  }

  .hand-piece-count {
    font-size: 0.72rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  .app-shell {
    animation: none;
  }
}
