:root {
  --bg-1: #1b1d23;
  --bg-2: #14151a;
  --panel: #21242c;
  --panel-2: #2a2e38;
  --border: #373b46;
  --text: #e7e9ee;
  --muted: #8e94a2;
  --accent: #5b9df8;
  --accent-strong: #7db4ff;
  --green: #46c96c;
  --orange: #e8a33d;
  --red: #ec5b5f;
  --sq-light: #f0d9b5;
  --sq-dark: #b58863;
  --radius: 12px;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
  --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.25);
}

* {
  box-sizing: border-box;
}

html {
  color-scheme: dark;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: "Inter", "Segoe UI", system-ui, -apple-system, sans-serif;
  background:
    radial-gradient(1200px 600px at 50% -10%, rgba(91, 157, 248, 0.09), transparent 60%),
    linear-gradient(180deg, var(--bg-1), var(--bg-2));
  background-attachment: fixed;
  color: var(--text);
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 28px;
  background: rgba(26, 28, 35, 0.85);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand .logo {
  width: 34px;
  height: 34px;
  border-radius: 8px;
  box-shadow: var(--shadow-sm);
  flex: none;
}

.brand h1 {
  margin: 0;
  font-size: 20px;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.brand h1::after {
  content: "";
}

.status {
  font-size: 13px;
  color: var(--muted);
  text-align: right;
}

main {
  max-width: 1020px;
  margin: 28px auto;
  padding: 0 20px;
}

main > section {
  animation: riseIn 0.28s ease both;
}

@keyframes riseIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 22px 26px;
  box-shadow: var(--shadow);
}

h2 {
  margin: 0 0 12px;
  font-size: 19px;
  letter-spacing: -0.01em;
}

h3 {
  font-size: 16px;
}

.hint {
  color: var(--muted);
  font-size: 14px;
  margin-top: 0;
  line-height: 1.6;
}

textarea {
  width: 100%;
  background: var(--bg-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 13px;
  resize: vertical;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(91, 157, 248, 0.25);
}

.row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 14px;
  flex-wrap: wrap;
}

.spacer {
  flex: 1;
}

button {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 9px 16px;
  font-size: 14px;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.1s ease, box-shadow 0.15s ease, filter 0.15s ease;
}

button:hover:not(:disabled) {
  background: #343946;
  border-color: #454b59;
}

button:active:not(:disabled) {
  transform: translateY(1px);
}

button:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px rgba(91, 157, 248, 0.4);
}

button.primary {
  background: linear-gradient(180deg, var(--accent-strong), var(--accent));
  color: #0b0b0e;
  border-color: transparent;
  font-weight: 600;
  box-shadow: 0 4px 14px rgba(91, 157, 248, 0.35);
}

button.primary:hover:not(:disabled) {
  filter: brightness(1.08);
  background: linear-gradient(180deg, var(--accent-strong), var(--accent));
}

button.ghost {
  background: transparent;
}

button.ghost:hover:not(:disabled) {
  background: var(--panel-2);
}

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

input[type="file"] {
  color: var(--muted);
  font-size: 13px;
}

input[type="file"]::file-selector-button {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 12px;
  margin-right: 10px;
  font-family: inherit;
  cursor: pointer;
}

.error {
  color: var(--red);
  margin-top: 12px;
  font-size: 13px;
}

/* Game list */
.game-item {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 12px 14px;
  border-radius: 10px;
  cursor: pointer;
  border: 1px solid transparent;
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.1s ease;
}

.game-item:hover {
  background: var(--panel-2);
  border-color: var(--border);
}

.game-item:active {
  transform: scale(0.995);
}

.game-item .names {
  flex: 1;
  font-size: 15px;
  font-weight: 500;
}

.game-item .names .vs {
  color: var(--muted);
  font-size: 13px;
  font-weight: 400;
}

.game-item .meta {
  color: var(--muted);
  font-size: 12px;
  text-align: right;
  line-height: 1.5;
}

.game-item .result-badge {
  font-weight: 700;
}

.game-item .perf {
  color: var(--accent-strong);
  font-size: 11px;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
}

.game-item .perf.none {
  color: var(--muted);
  font-weight: 400;
}

.lib-stats {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.6;
}

.lib-stats b {
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

/* Train layout */
.train-layout {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 22px;
  align-items: start;
}

.board-col {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

#board {
  width: min(70vh, 480px);
  aspect-ratio: 1;
  user-select: none;
  border-radius: 8px;
  box-shadow: var(--shadow);
  line-height: 0;
}

#board svg {
  width: 100%;
  height: 100%;
  display: block;
  border-radius: 8px;
  overflow: hidden;
  background: var(--sq-dark);
}

#board svg text {
  font-family: "Segoe UI Symbol", "Noto Sans Symbols 2", "DejaVu Sans", sans-serif;
  font-weight: 600;
}

#board .piece {
  filter: url(#piece-shadow);
}

#board .piece use {
  display: block;
  width: 1;
  height: 1;
  transform-origin: 0.5px 0.5px;
  transition: transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

#board .piece.moving use {
  transform: scale(1.12);
}

#board svg.turn-white .piece-w,
#board svg.turn-black .piece-b {
  cursor: pointer;
}

#board svg.turn-white .piece-w:hover,
#board svg.turn-black .piece-b:hover {
  filter: url(#piece-shadow) drop-shadow(0 0 2px rgba(255, 255, 255, 0.6));
}

.board-controls {
  display: flex;
  gap: 10px;
  justify-content: center;
}

.side-col {
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-width: 0;
}

.meta {
  font-size: 14px;
  line-height: 1.5;
}

.meta .title {
  font-size: 17px;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.meta .sub {
  color: var(--muted);
}

/* Feedback */
.feedback {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
  box-shadow: var(--shadow-sm);
  animation: slideUp 0.3s cubic-bezier(0.4, 0, 0.2, 1) both;
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(12px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

.feedback .head {
  font-size: 19px;
  font-weight: 800;
  margin-bottom: 8px;
  animation: pop 0.35s cubic-bezier(0.34, 1.56, 0.64, 1) both;
}

@keyframes pop {
  from {
    opacity: 0;
    transform: scale(0.9);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

.feedback .badge {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 700;
  color: #0b0b0e;
}

.feedback .moves {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px 14px;
  margin: 12px 0;
  font-size: 14px;
  font-weight: 600;
}

.feedback .moves .label {
  color: var(--muted);
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.feedback .evals {
  border-top: 1px solid var(--border);
  padding-top: 10px;
  font-size: 13px;
  color: var(--muted);
  line-height: 1.7;
}

.feedback .evals b {
  color: var(--text);
}

/* Scoreboard */
.scoreboard {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.scoreboard .stat {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 8px;
  text-align: center;
  box-shadow: var(--shadow-sm);
}

.scoreboard .stat .val {
  font-size: 19px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.scoreboard .stat .lbl {
  font-size: 10px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-top: 2px;
}

/* Move list */
.move-list {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  max-height: 38vh;
  overflow-y: auto;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 13px;
  line-height: 2;
  box-shadow: var(--shadow-sm);
}

.move-list::-webkit-scrollbar {
  width: 8px;
}

.move-list::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 4px;
}

.move-list .move {
  display: inline-block;
  padding: 1px 7px;
  margin: 1px 2px;
  border-radius: 6px;
  cursor: default;
  transition: background 0.15s ease, color 0.15s ease, box-shadow 0.15s ease;
}

.move-list .move.correct {
  background: rgba(70, 201, 108, 0.16);
  color: var(--green);
}

.move-list .move.wrong {
  background: rgba(232, 163, 61, 0.16);
  color: var(--orange);
}

.move-list .move.revealed {
  background: rgba(142, 148, 162, 0.12);
  color: var(--muted);
}

.move-list .move.opponent {
  color: var(--muted);
  font-style: italic;
}

.move-list .move.current {
  outline: 1px solid var(--accent);
  background: rgba(91, 157, 248, 0.12);
  animation: moveHop 0.32s cubic-bezier(0.34, 1.56, 0.64, 1) both;
}

@keyframes moveHop {
  0% {
    transform: translateY(0) scale(1);
  }
  35% {
    transform: translateY(-3px) scale(1.12);
    background: rgba(91, 157, 248, 0.3);
  }
  100% {
    transform: translateY(0) scale(1);
  }
}

.move-list .move.scored {
  animation: scoreFlash 0.5s cubic-bezier(0.4, 0, 0.2, 1) both;
}

@keyframes scoreFlash {
  0% {
    transform: scale(1.25);
    box-shadow: 0 0 0 3px rgba(255, 255, 255, 0.35);
  }
  100% {
    transform: scale(1);
    box-shadow: none;
  }
}

/* Side picker */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(10, 11, 14, 0.7);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  animation: fadeIn 0.2s ease both;
}

.overlay[hidden] {
  display: none;
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

.modal {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 26px 30px;
  max-width: 400px;
  width: 90%;
  text-align: center;
  box-shadow: var(--shadow);
  animation: pop 0.28s cubic-bezier(0.34, 1.56, 0.64, 1) both;
}

.modal h3 {
  margin-top: 0;
  font-size: 19px;
}

.modal .hint {
  margin-bottom: 10px;
}

.modal .row {
  justify-content: center;
  margin-top: 18px;
}

.side-row button {
  min-width: 140px;
}

/* Done screen */
#done-summary {
  font-size: 15px;
  line-height: 1.8;
  margin: 14px 0;
}

#done-summary .big {
  font-size: 24px;
  font-weight: 800;
  margin-bottom: 8px;
  letter-spacing: -0.01em;
}

#done-summary .sub {
  color: var(--muted);
  font-size: 13px;
}

@media (max-width: 860px) {
  .train-layout {
    grid-template-columns: 1fr;
  }
  #board {
    width: 100%;
    max-width: 480px;
    margin: 0 auto;
  }
  .scoreboard {
    grid-template-columns: repeat(3, 1fr);
  }
  header {
    padding: 12px 16px;
  }
  main {
    padding: 0 14px;
    margin: 20px auto;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
