:root {
  --bg-deep: #0c1210;
  --bg: #121a17;
  --bg-elevated: #1a2420;
  --bg-soft: #24302b;
  --line: rgba(196, 214, 198, 0.12);
  --text: #e8f0ea;
  --text-muted: #8fa398;
  --accent: #3ecf8e;
  --accent-dim: #2a9a68;
  --danger: #e86a5c;
  --highlight: rgba(62, 207, 142, 0.38);
  --last-move: rgba(255, 214, 102, 0.42);
  --check: rgba(232, 106, 92, 0.55);
  --font: "Instrument Sans", system-ui, sans-serif;
  --mono: "JetBrains Mono", ui-monospace, monospace;
  --board-size: min(72vh, calc(100vw - 2rem), 640px);
  --radius: 14px;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.45);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
}

body {
  font-family: var(--font);
  color: var(--text);
  background:
    radial-gradient(ellipse 80% 50% at 10% -10%, rgba(62, 207, 142, 0.14), transparent 50%),
    radial-gradient(ellipse 60% 40% at 100% 0%, rgba(90, 140, 120, 0.12), transparent 45%),
    linear-gradient(165deg, var(--bg-deep) 0%, var(--bg) 45%, #0e1613 100%);
  background-attachment: fixed;
}

button,
select,
input {
  font: inherit;
  color: inherit;
}

.app {
  max-width: 1280px;
  margin: 0 auto;
  padding: 1.25rem 1.25rem 2.5rem;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1.5rem;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: baseline;
  gap: 0.65rem;
}

.brand-mark {
  width: 0.7rem;
  height: 0.7rem;
  border-radius: 2px;
  background: var(--accent);
  box-shadow: 0 0 0 4px rgba(62, 207, 142, 0.18);
  align-self: center;
}

.brand h1 {
  margin: 0;
  font-size: clamp(1.75rem, 4vw, 2.35rem);
  font-weight: 700;
  letter-spacing: -0.03em;
  line-height: 1;
}

.brand-sub {
  font-size: 0.85rem;
  color: var(--text-muted);
  letter-spacing: 0.04em;
  text-transform: uppercase;
  font-weight: 500;
}

.topbar-actions {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.btn {
  border: 1px solid var(--line);
  background: var(--bg-elevated);
  color: var(--text);
  padding: 0.55rem 1rem;
  border-radius: 999px;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.12s ease;
}

.btn:hover {
  background: var(--bg-soft);
  border-color: rgba(196, 214, 198, 0.28);
}

.btn:active {
  transform: scale(0.97);
}

.btn-primary {
  background: var(--accent);
  border-color: transparent;
  color: #062216;
  font-weight: 600;
}

.btn-primary:hover {
  background: #4fe0a0;
}

.btn-ghost {
  background: transparent;
}

.layout {
  display: grid;
  grid-template-columns: 240px minmax(0, 1fr) 220px;
  gap: 1.25rem;
  align-items: start;
}

.panel {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.card {
  background: rgba(26, 36, 32, 0.85);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 1rem 1.1rem;
  backdrop-filter: blur(10px);
  box-shadow: var(--shadow);
}

.card h2 {
  margin: 0 0 0.85rem;
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  margin-bottom: 0.85rem;
}

.field span,
.toggle span,
.status-label {
  font-size: 0.8rem;
  color: var(--text-muted);
}

.field select {
  appearance: none;
  background: var(--bg) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath fill='%8fa398' d='M1 1l5 5 5-5'/%3E%3C/svg%3E") no-repeat right 0.75rem center;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.55rem 2rem 0.55rem 0.75rem;
  outline: none;
}

.field select:focus {
  border-color: var(--accent-dim);
}

.toggle {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  cursor: pointer;
  user-select: none;
}

.toggle input {
  accent-color: var(--accent);
  width: 1rem;
  height: 1rem;
}

.status-block {
  margin-top: 0.35rem;
  padding-top: 0.85rem;
  border-top: 1px solid var(--line);
}

.status-text {
  margin: 0.25rem 0 0;
  font-size: 1.05rem;
  font-weight: 600;
}

.board-stage {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.65rem;
}

.board-row {
  display: flex;
  align-items: stretch;
  gap: 0.45rem;
}

.eval-rail {
  position: relative;
  width: 1.65rem;
  height: var(--board-size);
  flex-shrink: 0;
  border-radius: 5px;
  overflow: hidden;
  background: #403d39;
  box-shadow: var(--shadow), 0 0 0 1px rgba(255, 255, 255, 0.06);
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
}

.eval-rail.flipped {
  justify-content: flex-start;
}

.eval-white {
  width: 100%;
  height: 50%;
  background: #f0f0f0;
  transition: height 0.35s ease;
}

.eval-score {
  position: absolute;
  left: 0;
  right: 0;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0.15rem 0;
  font-family: var(--mono);
  font-size: 0.62rem;
  font-weight: 700;
  line-height: 1;
  letter-spacing: -0.02em;
  pointer-events: none;
  writing-mode: horizontal-tb;
  transform: rotate(0deg);
}

.eval-score.at-white {
  bottom: 0.2rem;
  top: auto;
  color: #1a1a1a;
}

.eval-score.at-black {
  top: 0.2rem;
  bottom: auto;
  color: #f2f2f2;
}

.eval-rail.flipped .eval-score.at-white {
  top: 0.2rem;
  bottom: auto;
}

.eval-rail.flipped .eval-score.at-black {
  bottom: 0.2rem;
  top: auto;
}

.board-wrap {
  position: relative;
  width: var(--board-size);
  height: var(--board-size);
  border-radius: 8px;
  overflow: hidden;
  box-shadow: var(--shadow), 0 0 0 1px rgba(255, 255, 255, 0.06);
  animation: board-in 0.55s ease both;
}

@keyframes board-in {
  from {
    opacity: 0;
    transform: translateY(12px) scale(0.985);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

.board {
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  background-size: cover;
  background-position: center;
  user-select: none;
  touch-action: none;
}

.square {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: default;
}

.square.selectable {
  cursor: pointer;
}

.square.highlight::after {
  content: "";
  position: absolute;
  inset: 0;
  background: var(--highlight);
  pointer-events: none;
}

.square.drag-over::after {
  content: "";
  position: absolute;
  inset: 0;
  background: rgba(62, 207, 142, 0.28);
  pointer-events: none;
  z-index: 1;
}

.square.last-move::before {
  content: "";
  position: absolute;
  inset: 0;
  background: var(--last-move);
  pointer-events: none;
}

.square.in-check::before {
  content: "";
  position: absolute;
  inset: 0;
  background: var(--check);
  pointer-events: none;
}

.square .move-dot,
.square .capture-ring {
  position: absolute;
  pointer-events: none;
  z-index: 1;
}

.square .move-dot {
  width: 28%;
  height: 28%;
  border-radius: 50%;
  background: rgba(20, 40, 30, 0.35);
}

.square .capture-ring {
  inset: 6%;
  border: 4px solid rgba(20, 40, 30, 0.35);
  border-radius: 50%;
}

.piece {
  width: 90%;
  height: 90%;
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
  z-index: 2;
  transition: transform 0.12s ease;
  pointer-events: none;
}

.square.selectable .piece {
  pointer-events: auto;
  cursor: grab;
  touch-action: none;
}

.square.selectable:hover .piece,
.square.selected .piece {
  transform: scale(1.06);
}

.square.dragging .piece {
  opacity: 0.25;
}

.piece.drag-ghost {
  position: fixed;
  width: calc(var(--board-size) / 8 * 0.9);
  height: calc(var(--board-size) / 8 * 0.9);
  pointer-events: none;
  z-index: 100;
  transform: translate(-50%, -50%);
  filter: drop-shadow(0 8px 16px rgba(0, 0, 0, 0.45));
  transition: none;
  cursor: grabbing;
}

.coord {
  position: absolute;
  font-size: clamp(0.55rem, 1.6vw, 0.72rem);
  font-weight: 600;
  color: rgba(255, 255, 255, 0.72);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.55);
  pointer-events: none;
  z-index: 3;
}

.coord.file {
  right: 4%;
  bottom: 2%;
}

.coord.rank {
  left: 4%;
  top: 3%;
}

.thinking {
  position: absolute;
  left: 50%;
  bottom: 1rem;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.55rem 0.9rem;
  border-radius: 999px;
  background: rgba(12, 18, 16, 0.88);
  border: 1px solid var(--line);
  font-size: 0.85rem;
  z-index: 5;
  backdrop-filter: blur(8px);
  pointer-events: none;
}

.thinking[hidden] {
  display: none !important;
}

.thinking-dot {
  width: 0.55rem;
  height: 0.55rem;
  border-radius: 50%;
  background: var(--accent);
  animation: pulse 1s ease-in-out infinite;
}

@keyframes pulse {
  0%,
  100% {
    opacity: 0.4;
    transform: scale(0.9);
  }
  50% {
    opacity: 1;
    transform: scale(1.15);
  }
}

.captured {
  display: flex;
  flex-wrap: wrap;
  gap: 0.15rem;
  min-height: 1.75rem;
  width: var(--board-size);
  margin-left: calc(1.65rem + 0.45rem);
  justify-content: flex-start;
}

.captured img {
  width: 1.55rem;
  height: 1.55rem;
  object-fit: contain;
  opacity: 0.9;
}

.moves-card {
  max-height: calc(var(--board-size) + 4rem);
  display: flex;
  flex-direction: column;
}

.move-list {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  font-family: var(--mono);
  font-size: 0.85rem;
  display: grid;
  gap: 0.25rem;
}

.move-list li {
  display: grid;
  grid-template-columns: 2rem 1fr 1fr;
  gap: 0.35rem;
  padding: 0.35rem 0.4rem;
  border-radius: 8px;
}

.move-list li:nth-child(odd) {
  background: rgba(255, 255, 255, 0.03);
}

.move-list .num {
  color: var(--text-muted);
}

.modal {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(6, 10, 8, 0.72);
  backdrop-filter: blur(6px);
  z-index: 50;
  padding: 1rem;
  animation: fade-in 0.2s ease;
}

.modal[hidden] {
  display: none;
}

@keyframes fade-in {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

.modal-panel {
  width: min(100%, 360px);
  background: var(--bg-elevated);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 1.4rem;
  box-shadow: var(--shadow);
  text-align: center;
  animation: board-in 0.3s ease;
}

.modal-panel h2 {
  margin: 0 0 0.5rem;
  font-size: 1.35rem;
}

.modal-panel p {
  margin: 0 0 1.1rem;
  color: var(--text-muted);
}

.promo-choices {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.5rem;
}

.promo-choices button {
  border: 1px solid var(--line);
  background: var(--bg);
  border-radius: 12px;
  padding: 0.5rem;
  cursor: pointer;
  aspect-ratio: 1;
  background-size: 80%;
  background-repeat: no-repeat;
  background-position: center;
  transition: border-color 0.15s ease, transform 0.12s ease;
}

.promo-choices button:hover {
  border-color: var(--accent);
  transform: translateY(-2px);
}

@media (max-width: 980px) {
  .layout {
    grid-template-columns: 1fr;
  }

  .panel-left,
  .panel-right {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 1rem;
  }

  .moves-card {
    max-height: 240px;
  }

  .board-stage {
    order: -1;
  }
}

@media (max-width: 520px) {
  .app {
    padding: 0.85rem 0.85rem 2rem;
  }

  .brand-sub {
    display: none;
  }
}
