* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: #f4f1ea;
  color: #1f2933;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input {
  font: inherit;
}

button:disabled,
input:disabled {
  cursor: not-allowed;
  opacity: 0.58;
}

.app-shell {
  width: min(1180px, 100%);
  margin: 0 auto;
  padding: 14px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 54px;
}

.topbar h1 {
  margin: 0;
  font-size: 1.35rem;
  letter-spacing: 0;
}

.back-link {
  color: #315f72;
  font-weight: 700;
  text-decoration: none;
}

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

.character-select {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
  margin: 8px 0 14px;
}

.character-card {
  display: grid;
  grid-template-columns: 42px 1fr;
  grid-template-rows: auto auto;
  gap: 2px 8px;
  align-items: center;
  min-width: 0;
  padding: 7px;
  border: 1px solid #d2c5ab;
  border-radius: 8px;
  background: #fffdfa;
  color: #1f2933;
  cursor: pointer;
  text-align: left;
}

.character-card.selected {
  border-color: #315f72;
  background: #eaf3f0;
  box-shadow: inset 0 0 0 2px rgba(49, 95, 114, 0.18);
}

.character-card img {
  grid-row: 1 / 3;
  width: 42px;
  height: 42px;
  border-radius: 9px;
  object-fit: cover;
}

.character-name {
  overflow: hidden;
  font-weight: 800;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.character-level {
  color: #667085;
  font-size: 0.78rem;
}

.command-button,
.icon-button,
.level-control select,
.question-form button,
.prompt-buttons button {
  border: 1px solid #8d7a56;
  border-radius: 7px;
  background: #fffaf0;
  color: #1f2933;
  cursor: pointer;
}

.level-control {
  display: flex;
  align-items: center;
  gap: 6px;
  color: #52616f;
  font-size: 0.85rem;
  font-weight: 700;
}

.level-control select {
  min-height: 38px;
  padding: 0 8px;
}

.command-button {
  min-height: 38px;
  padding: 0 14px;
  font-weight: 700;
}

.icon-button {
  min-width: 44px;
  min-height: 38px;
}

.layout {
  display: grid;
  grid-template-columns: minmax(320px, 680px) minmax(280px, 1fr);
  gap: 16px;
  align-items: start;
}

.board-area {
  display: grid;
  gap: 10px;
}

.event-banner {
  min-height: 38px;
  padding: 8px 12px;
  border: 1px solid #d2c5ab;
  border-radius: 8px;
  background: #fffdfa;
  color: #315f72;
  font-weight: 800;
  text-align: center;
}

.board-wrap {
  display: grid;
  gap: 4px;
}

.board-frame {
  position: relative;
}

.move-arrow {
  position: absolute;
  z-index: 3;
  height: 4px;
  border-radius: 999px;
  background: #9b1c1c;
  pointer-events: none;
  transform-origin: left center;
  opacity: 0.82;
}

.move-arrow::after {
  position: absolute;
  right: -2px;
  top: 50%;
  width: 0;
  height: 0;
  border-top: 7px solid transparent;
  border-bottom: 7px solid transparent;
  border-left: 12px solid #9b1c1c;
  content: "";
  transform: translateY(-50%);
}

.files {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  padding-left: 0;
  color: #6b5d43;
  font-size: 0.8rem;
  text-align: center;
}

.board {
  display: grid;
  grid-template-columns: repeat(9, minmax(32px, 1fr));
  aspect-ratio: 1;
  border: 3px solid #6f5630;
  background: #d9a85d;
  user-select: none;
}

.board.is-busy {
  opacity: 0.72;
}

.cell {
  position: relative;
  display: grid;
  place-items: center;
  border-right: 1px solid rgba(75, 51, 22, 0.7);
  border-bottom: 1px solid rgba(75, 51, 22, 0.7);
  background: #e4b86f;
}

.cell:nth-child(9n) {
  border-right: 0;
}

.cell:nth-last-child(-n + 9) {
  border-bottom: 0;
}

.cell.selected,
.cell.legal {
  background: #f3d58d;
}

.cell.last-from {
  background: #f6d6a6;
}

.cell.last-to {
  background: #8fc5b3;
  box-shadow: inset 0 0 0 4px #315f72;
}

.cell.last-drop {
  animation: dropPulse 1.2s ease-out 2;
}

.cell.king-check {
  background: #f7b7a3;
  animation: checkPulse 0.85s ease-in-out infinite alternate;
}

@keyframes dropPulse {
  0% {
    box-shadow: inset 0 0 0 5px #315f72, 0 0 0 0 rgba(49, 95, 114, 0.55);
  }
  100% {
    box-shadow: inset 0 0 0 4px #315f72, 0 0 0 14px rgba(49, 95, 114, 0);
  }
}

@keyframes checkPulse {
  from {
    box-shadow: inset 0 0 0 4px #9b1c1c;
  }
  to {
    box-shadow: inset 0 0 0 7px #f97316;
  }
}

.coord {
  position: absolute;
  right: 3px;
  bottom: 2px;
  color: rgba(70, 45, 20, 0.45);
  font-size: 0.62rem;
}

.piece {
  display: grid;
  place-items: center;
  width: 76%;
  height: 84%;
  clip-path: polygon(50% 0, 88% 18%, 80% 100%, 20% 100%, 12% 18%);
  background: #f4d28a;
  border: 1px solid #7a5a29;
  color: #171717;
  font-weight: 800;
  font-size: clamp(0.9rem, 3.3vw, 1.45rem);
  line-height: 1;
}

.piece.white {
  transform: rotate(180deg);
  background: #eec47b;
}

.piece.promoted {
  color: #9b1c1c;
}

.hand {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  min-height: 48px;
  padding: 8px;
  border: 1px solid #d2c5ab;
  border-radius: 7px;
  background: #fffaf0;
}

.hand-title {
  color: #6b5d43;
  font-size: 0.85rem;
  font-weight: 700;
}

.hand-piece {
  min-width: 44px;
  min-height: 38px;
  border: 1px solid #8d7a56;
  border-radius: 7px;
  background: #f4d28a;
  cursor: pointer;
  font-weight: 800;
}

.hand-piece.selected {
  outline: 3px solid #315f72;
  outline-offset: 2px;
}

.hand-piece.droppable {
  box-shadow: 0 0 0 3px rgba(49, 95, 114, 0.16);
}

.side-panel {
  display: grid;
  gap: 12px;
}

.opponent-panel,
.ai-panel,
.moves-panel {
  border: 1px solid #d2c5ab;
  border-radius: 8px;
  background: #fffdfa;
  padding: 14px;
}

.profile,
.opponent-profile {
  display: grid;
  grid-template-columns: 72px 1fr;
  gap: 12px;
  align-items: center;
}

.profile img,
.opponent-profile img {
  width: 72px;
  height: 72px;
  border-radius: 14px;
}

.label {
  margin: 0;
  color: #6b7280;
  font-size: 0.78rem;
  font-weight: 700;
}

.profile h2,
.opponent-profile strong,
.moves-panel h2 {
  margin: 0;
  font-size: 1.05rem;
}

.profile p:last-child,
.opponent-profile span {
  margin: 4px 0 0;
  color: #52616f;
  font-size: 0.9rem;
  line-height: 1.5;
}

.opponent-profile strong,
.opponent-profile span {
  display: block;
}

.thinking {
  margin-top: 12px;
  padding: 8px 10px;
  border-radius: 7px;
  background: #eaf3f0;
  color: #315f72;
  font-weight: 700;
}

.coach-thinking {
  background: #f3eefb;
  color: #5d4b8c;
}

.status-line {
  margin-top: 8px;
  color: #667085;
  font-size: 0.78rem;
  line-height: 1.4;
}

.speech {
  margin-top: 12px;
  min-height: 84px;
  line-height: 1.7;
}

.speech p {
  margin: 0 0 8px;
}

.result-panel {
  margin: 10px 0;
  padding: 10px;
  border-radius: 8px;
  font-weight: 800;
}

.result-panel.win {
  background: #eef8e8;
  color: #326322;
}

.result-panel.lose {
  background: #fff4e5;
  color: #8a4b10;
}

.prompt-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.prompt-buttons button {
  min-height: 34px;
  padding: 0 10px;
}

.question-form {
  display: grid;
  grid-template-columns: 1fr 58px;
  gap: 8px;
  margin-top: 10px;
}

.question-form input {
  min-width: 0;
  min-height: 38px;
  border: 1px solid #d2c5ab;
  border-radius: 7px;
  padding: 0 10px;
}

.panel-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.panel-heading button {
  min-height: 30px;
  border: 1px solid #d2c5ab;
  border-radius: 7px;
  background: #fffaf0;
  color: #315f72;
  cursor: pointer;
  font-size: 0.8rem;
  font-weight: 700;
}

.moves-panel ol {
  max-height: 240px;
  overflow: auto;
  margin: 10px 0 0;
  padding-left: 0;
  line-height: 1.45;
  list-style: none;
}

.moves-panel li {
  margin: 0 0 8px;
  padding: 8px 10px;
  border-left: 4px solid #d2c5ab;
  background: #fffaf0;
}

.moves-panel li.move-ai {
  border-left-color: #315f72;
  background: #eef7f4;
}

.move-main {
  font-weight: 800;
}

.move-natural {
  margin-top: 3px;
  color: #344054;
  font-size: 0.86rem;
}

.move-meta,
.move-reason {
  margin-top: 3px;
  color: #667085;
  font-size: 0.78rem;
}

.move-reason {
  color: #52616f;
}

dialog {
  border: 0;
  border-radius: 8px;
  padding: 18px;
}

.dialog-actions {
  display: flex;
  gap: 8px;
}

@media (max-width: 860px) {
  .character-select {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

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

  .topbar {
    flex-wrap: wrap;
  }
}

@media (max-width: 460px) {
  .top-actions {
    width: 100%;
    flex-wrap: wrap;
  }

  .command-button {
    flex: 1 1 auto;
    padding: 0 10px;
  }

  .character-card {
    grid-template-columns: 36px 1fr;
  }

  .character-card img {
    width: 36px;
    height: 36px;
  }
}
