:root {
  --bg: #14171c;
  --panel-bg: #1c2028;
  --panel-bg-2: #232833;
  --border: #333a48;
  --text: #e8eaed;
  --text-dim: #9aa3b2;
  --accent: #4f9dff;
  --accent-2: #2ecc71;
  --danger: #e05c5c;
  --light-sq: #ebecd0;
  --dark-sq: #7d945d;
  --sel-sq: rgba(80, 160, 255, 0.55);
  --move-sq: rgba(60, 200, 120, 0.45);
  --lastmove-sq: rgba(255, 205, 60, 0.35);
  --check-sq: rgba(230, 60, 60, 0.55);
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, Arial, sans-serif;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

button {
  font-family: inherit;
  font-size: 0.85rem;
  background: var(--panel-bg-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 12px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
button:hover:not(:disabled) { background: #2c3140; border-color: var(--accent); }
button:disabled { opacity: 0.45; cursor: not-allowed; }
button.primary { background: var(--accent); border-color: var(--accent); color: #0b1220; font-weight: 600; }
button.primary:hover { background: #6bacff; }

input[type="text"] {
  font-family: "Cascadia Code", Consolas, monospace;
  background: var(--panel-bg-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 0.8rem;
}

.app { max-width: 1280px; margin: 0 auto; padding: 16px; }

.topbar {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: 16px;
  flex-wrap: wrap;
  gap: 8px;
}
.topbar h1 { font-size: 1.3rem; margin: 0; }
.topbar-status {
  font-size: 0.95rem;
  color: var(--text-dim);
  background: var(--panel-bg);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 6px 14px;
}

.layout {
  display: grid;
  grid-template-columns: minmax(320px, 640px) minmax(280px, 380px);
  gap: 20px;
  align-items: start;
}

@media (max-width: 880px) {
  .layout { grid-template-columns: 1fr; }
}

/* ---------- Board ---------- */
.board-column { display: flex; flex-direction: column; gap: 12px; }

.board-wrap { width: 100%; max-width: 640px; }

.board-row { display: flex; }

.coords.files {
  display: grid;
  grid-template-columns: 20px repeat(8, 1fr);
  font-size: 0.7rem;
  color: var(--text-dim);
  padding: 0 2px;
}
.coords.files span { text-align: center; }

.coords.ranks {
  display: grid;
  grid-template-rows: repeat(8, 1fr);
  width: 20px;
  font-size: 0.7rem;
  color: var(--text-dim);
}
.coords.ranks span { display: flex; align-items: center; justify-content: center; }

.board-square-container {
  position: relative;
  flex: 1;
  aspect-ratio: 1 / 1;
}

.board {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  width: 100%;
  height: 100%;
  border: 2px solid var(--border);
  border-radius: 4px;
  overflow: hidden;
  user-select: none;
}

.arrow-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  width: 100%;
  height: 100%;
}

.square {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  aspect-ratio: 1 / 1;
}
.square.light { background: var(--light-sq); }
.square.dark { background: var(--dark-sq); }

.square.selected::before,
.square.lastmove::before,
.square.check::before {
  content: "";
  position: absolute;
  inset: 0;
}
.square.selected::before { background: var(--sel-sq); }
.square.lastmove::before { background: var(--lastmove-sq); }
.square.check::before { background: var(--check-sq); }

.square .move-dot {
  position: absolute;
  width: 30%;
  height: 30%;
  border-radius: 50%;
  background: rgba(20, 20, 20, 0.35);
  pointer-events: none;
}
.square .move-dot.capture {
  width: 100%;
  height: 100%;
  border-radius: 0;
  background: transparent;
  box-shadow: inset 0 0 0 5px rgba(20, 20, 20, 0.35);
}

.square-label {
  position: absolute;
  bottom: 1px;
  right: 3px;
  font-size: 0.55rem;
  opacity: 0.55;
  pointer-events: none;
}

.piece {
  font-size: 6.2vw;
  line-height: 1;
  cursor: grab;
  position: relative;
  z-index: 2;
  filter: drop-shadow(0 1px 1px rgba(0,0,0,0.35));
}
@media (min-width: 640px) {
  .piece { font-size: 40px; }
}
.piece.white {
  color: #fbfbfb;
  -webkit-text-stroke: 1.4px #26282c;
  text-stroke: 1.4px #26282c;
  paint-order: stroke fill;
}
.piece.black {
  color: #23252a;
  -webkit-text-stroke: 1px #23252a;
}
.piece.dragging { opacity: 0.35; }

.piece-ghost {
  position: fixed;
  pointer-events: none;
  z-index: 999;
  font-size: 40px;
  transform: translate(-50%, -50%);
  filter: drop-shadow(0 2px 4px rgba(0,0,0,0.5));
}

.board-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

/* ---------- Setup panel ---------- */
.setup-panel {
  background: var(--panel-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.setup-title { font-size: 0.8rem; color: var(--text-dim); }

.piece-tray {
  display: flex;
  gap: 4px;
  background: var(--light-sq);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px;
  justify-content: space-around;
}
.piece-tray .piece { cursor: grab; font-size: 32px; }

.setup-row {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  font-size: 0.82rem;
}
.setup-row label { display: flex; align-items: center; gap: 4px; color: var(--text-dim); }

.tray-tool.active { border-color: var(--danger); color: var(--danger); }

.setup-error { color: var(--danger); font-size: 0.8rem; min-height: 1em; }

/* ---------- FEN panel ---------- */
.fen-panel {
  background: var(--panel-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.fen-panel label { font-size: 0.75rem; color: var(--text-dim); }
.fen-row { display: flex; gap: 6px; }
.fen-row input { flex: 1; min-width: 0; }

/* ---------- Side column / panels ---------- */
.side-column { display: flex; flex-direction: column; gap: 16px; }

.panel {
  background: var(--panel-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 14px;
}
.panel-title {
  font-size: 0.9rem;
  font-weight: 600;
  margin-bottom: 10px;
  color: var(--text);
}

.engine-toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
  flex-wrap: wrap;
  font-size: 0.82rem;
  color: var(--text-dim);
}
.switch { display: flex; align-items: center; gap: 6px; cursor: pointer; }
.switch input { accent-color: var(--accent); }

input[type="range"] { accent-color: var(--accent); flex: 1; }

.engine-status {
  font-size: 0.8rem;
  color: var(--text-dim);
  margin-bottom: 10px;
  min-height: 1.1em;
}

.best-move-box {
  background: var(--panel-bg-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px 12px;
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.best-move-label { font-size: 0.72rem; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.04em; }
.best-move-value {
  font-size: 1.4rem;
  font-weight: 700;
  color: var(--accent-2);
  font-family: "Cascadia Code", Consolas, monospace;
}
.eval-value {
  font-size: 1rem;
  font-weight: 600;
  color: var(--text);
  font-family: "Cascadia Code", Consolas, monospace;
}

.pv-box { font-size: 0.8rem; }
.pv-label { color: var(--text-dim); margin-bottom: 4px; font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.04em; }
.pv-line {
  font-family: "Cascadia Code", Consolas, monospace;
  line-height: 1.5;
  word-break: break-word;
  color: var(--text);
}

.move-list {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 320px;
  overflow-y: auto;
  font-family: "Cascadia Code", Consolas, monospace;
  font-size: 0.85rem;
}
.move-list li {
  display: flex;
  gap: 8px;
  padding: 3px 4px;
  border-radius: 4px;
}
.move-list li:hover { background: var(--panel-bg-2); }
.move-list .move-num { color: var(--text-dim); min-width: 26px; }
.move-list .san { padding: 1px 4px; border-radius: 3px; }
.move-list .san.current { background: var(--accent); color: #0b1220; font-weight: 700; }
