:root,
[data-theme="mocha"] {
  --base: #1e1e2e;
  --crust: #11111b;
  --surface0: #313244;
  --surface1: #45475a;
  --surface2: #585b70;
  --overlay0: #6c7086;
  --overlay1: #7f849c;
  --subtext0: #a6adc8;
  --subtext1: #bac2de;
  --text: #cdd6f4;
  --mauve: #cba6f7;
  --lavender: #b4befe;
  --blue: #89b4fa;
  --green: #a6e3a1;
  --yellow: #f9e2af;
  --red: #f38ba8;
  --sq-light: #585b70;
  --sq-dark: #313244;
  --frame: #11111b;
  --piece-light: #cdd6f4;
  --piece-dark: #11111b;
  --piece-light-edge: #6c7086;
  --piece-dark-edge: #585b70;
  --hl-last: color-mix(in srgb, var(--yellow) 32%, transparent);
  --hl-selected: color-mix(in srgb, var(--blue) 42%, transparent);
  --hl-move: color-mix(in srgb, var(--green) 40%, transparent);
  --hl-check: color-mix(in srgb, var(--red) 55%, transparent);
  --hl-hint: color-mix(in srgb, var(--mauve) 55%, transparent);
}

[data-theme="latte"] {
  --base: #eff1f5;
  --crust: #dce0e8;
  --surface0: #ccd0da;
  --surface1: #bcc0cc;
  --surface2: #acb0be;
  --overlay0: #9ca0b0;
  --overlay1: #8c8fa1;
  --subtext0: #6c6f85;
  --subtext1: #5c5f77;
  --text: #4c4f69;
  --mauve: #8839ef;
  --lavender: #7287fd;
  --blue: #1e66f5;
  --green: #40a02b;
  --yellow: #df8e1d;
  --red: #d20f39;
  --sq-light: #d4d8e2;
  --sq-dark: #a9aec2;
  --frame: #b5b9c8;
  --piece-light: #fbfcff;
  --piece-dark: #2c2f42;
  --piece-light-edge: #9ba0b2;
  --piece-dark-edge: #15161f;
  --hl-last: color-mix(in srgb, var(--yellow) 38%, transparent);
  --hl-selected: color-mix(in srgb, var(--blue) 30%, transparent);
  --hl-move: color-mix(in srgb, var(--green) 32%, transparent);
  --hl-check: color-mix(in srgb, var(--red) 40%, transparent);
  --hl-hint: color-mix(in srgb, var(--mauve) 40%, transparent);
}

* {
  box-sizing: border-box;
}

body {
  --board: min(88vw, 560px);
  --sq: calc(var(--board) / 8);
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--base);
  color: var(--text);
  font-family: "Clear Sans", "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.2s, color 0.2s;
}

a {
  color: var(--blue);
  text-decoration-color: color-mix(in srgb, var(--blue) 40%, transparent);
}

a:hover {
  color: var(--mauve);
}

.app {
  width: min(94vw, 1000px);
  margin: 0 auto;
  padding: clamp(12px, 3vh, 28px) 0 28px;
}

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
}

.title {
  margin: 0;
  font-size: clamp(1.7rem, 5vw, 2.4rem);
  line-height: 1;
  font-weight: 800;
  letter-spacing: -0.02em;
  color: var(--mauve);
}

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

.chip {
  font: inherit;
  font-size: 0.78rem;
  font-weight: 700;
  padding: 7px 12px;
  color: var(--subtext0);
  background: var(--surface0);
  border: 1px solid transparent;
  border-radius: 999px;
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s;
}

.chip:hover {
  color: var(--text);
  border-color: var(--overlay0);
}

.chip:focus-visible,
.btn:focus-visible,
.link-btn:focus-visible,
.select:focus-visible,
.board:focus-visible,
input[type="checkbox"]:focus-visible {
  outline: 2px solid var(--lavender);
  outline-offset: 2px;
}

.layout {
  display: grid;
  grid-template-columns: auto minmax(260px, 300px);
  gap: clamp(14px, 2.5vw, 26px);
  align-items: start;
  justify-content: center;
}

.board-area {
  display: flex;
  gap: 10px;
  align-items: flex-start;
}

.board-wrap {
  position: relative;
  flex: 0 0 auto;
  width: var(--board);
  height: var(--board);
}

.board {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 0 0 6px var(--frame), 0 14px 34px color-mix(in srgb, var(--crust) 60%, transparent);
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

.sq {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: default;
}

.sq.movable,
.sq.target {
  cursor: pointer;
}

.sq.light {
  background: var(--sq-light);
}

.sq.dark {
  background: var(--sq-dark);
}

.sq::after {
  content: "";
  position: absolute;
  inset: 0;
  background: transparent;
  pointer-events: none;
}

.sq.last::after {
  background: var(--hl-last);
}

.sq.selected::after {
  background: var(--hl-selected);
}

.sq.hint::after {
  background: var(--hl-hint);
}

.sq.check::after {
  background: var(--hl-check);
}

.sq .dot {
  position: absolute;
  width: 26%;
  height: 26%;
  border-radius: 50%;
  background: var(--hl-move);
  pointer-events: none;
  z-index: 2;
}

.sq .ring {
  position: absolute;
  inset: 6%;
  border: calc(var(--sq) * 0.075) solid var(--hl-move);
  border-radius: 50%;
  pointer-events: none;
  z-index: 2;
}

.piece {
  position: relative;
  z-index: 3;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  font-size: calc(var(--sq) * 0.84);
  line-height: 1;
  font-family: "DejaVu Sans", "Segoe UI Symbol", "Apple Symbols", "Noto Sans Symbols 2", serif;
  color: var(--piece-light);
  -webkit-text-stroke: calc(var(--sq) * 0.016) var(--piece-light-edge);
  text-shadow: 0 1px 2px color-mix(in srgb, var(--crust) 40%, transparent);
  pointer-events: none;
  user-select: none;
}

.piece.black {
  color: var(--piece-dark);
  -webkit-text-stroke: calc(var(--sq) * 0.016) var(--piece-dark-edge);
}

.coord {
  position: absolute;
  font-size: calc(var(--sq) * 0.19);
  font-weight: 700;
  pointer-events: none;
  opacity: 0.6;
  z-index: 4;
}

.coord.file {
  right: 5%;
  bottom: 1%;
}

.coord.rank {
  left: 5%;
  top: 1%;
}

.sq.light .coord {
  color: var(--sq-dark);
}

.sq.dark .coord {
  color: var(--sq-light);
}

.modal {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 16px;
  text-align: center;
  background: color-mix(in srgb, var(--base) 82%, transparent);
  backdrop-filter: blur(3px);
  border-radius: 10px;
}

.modal.hidden {
  display: none;
}

.modal.promo-modal {
  cursor: pointer;
}

.modal-box {
  cursor: default;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 16px 18px;
  background: var(--surface0);
  border-radius: 12px;
}

.modal-title {
  margin: 0;
  font-size: 0.8rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--subtext0);
}

.promo-options {
  display: flex;
  gap: 6px;
}

.promo-btn {
  width: calc(var(--sq) * 0.8);
  height: calc(var(--sq) * 0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  background: var(--surface1);
  border: 0;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.15s;
}

.promo-btn:hover {
  background: var(--surface2);
}

.promo-btn .piece {
  font-size: calc(var(--sq) * 0.66);
}

.result-title {
  margin: 0;
  font-size: clamp(1.4rem, 5vw, 2rem);
  font-weight: 800;
}

.result-sub {
  margin: 0;
  font-size: 0.9rem;
  color: var(--subtext0);
}

.result-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
}

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

.status-card {
  padding: 12px 14px;
  background: var(--surface0);
  border-radius: 10px;
}

.status-line {
  margin: 0;
  font-size: 1rem;
  font-weight: 700;
}

.status-sub {
  margin: 4px 0 0;
  min-height: 1.1em;
  font-size: 0.8rem;
  line-height: 1.4;
  color: var(--subtext0);
}

.field {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.field-label {
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: var(--overlay1);
}

.select {
  font: inherit;
  font-size: 0.88rem;
  font-weight: 600;
  padding: 9px 10px;
  color: var(--text);
  background: var(--surface0);
  border: 1px solid var(--surface1);
  border-radius: 8px;
  cursor: pointer;
}

.checkbox-field {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.85rem;
  color: var(--subtext1);
  cursor: pointer;
}

.checkbox-field input {
  width: 16px;
  height: 16px;
  accent-color: var(--mauve);
  cursor: pointer;
}

.buttons {
  display: flex;
  gap: 8px;
}

.buttons .btn {
  flex: 1;
}

.btn {
  font: inherit;
  font-weight: 700;
  font-size: 0.85rem;
  padding: 10px 12px;
  color: var(--text);
  background: var(--surface1);
  border: 0;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.15s, opacity 0.15s;
}

.btn:hover {
  background: var(--surface2);
}

.btn:disabled {
  opacity: 0.4;
  cursor: default;
}

.btn-primary {
  background: var(--mauve);
  color: var(--crust);
}

.btn-primary:hover {
  background: var(--lavender);
}

.moves-card {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.moves-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: var(--overlay1);
}

.link-btn {
  font: inherit;
  font-size: 0.72rem;
  font-weight: 700;
  padding: 0;
  color: var(--blue);
  background: none;
  border: 0;
  cursor: pointer;
  text-transform: none;
}

.link-btn:hover {
  color: var(--mauve);
}

.moves-list {
  margin: 0;
  padding: 6px 8px;
  max-height: 190px;
  overflow-y: auto;
  list-style: none;
  background: var(--surface0);
  border-radius: 10px;
  font-size: 0.82rem;
  font-variant-numeric: tabular-nums;
  color: var(--subtext1);
}

.moves-list:empty::before {
  content: "—";
  color: var(--overlay0);
}

.moves-list li {
  display: grid;
  grid-template-columns: 2.2em 1fr 1fr;
  gap: 4px;
  padding: 2px;
  border-radius: 4px;
}

.moves-list li:nth-child(odd) {
  background: color-mix(in srgb, var(--surface1) 35%, transparent);
}

.moves-list .num {
  color: var(--overlay1);
}

.moves-list .san {
  font-weight: 600;
  color: var(--text);
}

.engine-card {
  margin: 0;
  font-size: 0.72rem;
  line-height: 1.4;
  color: var(--overlay1);
}

.privacy {
  margin: 18px 0 0;
  font-size: 0.78rem;
  line-height: 1.45;
  color: var(--overlay1);
}

.footer {
  margin-top: 16px;
  padding-top: 14px;
  border-top: 1px solid var(--surface0);
  font-size: 0.78rem;
  line-height: 1.5;
  color: var(--subtext0);
}

.footer p {
  margin: 0 0 4px;
}

.footer-small {
  font-size: 0.73rem;
  color: var(--overlay1);
}

.footer strong {
  color: var(--text);
}

.toast {
  position: fixed;
  left: 50%;
  bottom: max(20px, env(safe-area-inset-bottom));
  z-index: 50;
  max-width: min(90vw, 420px);
  padding: 10px 18px;
  font-size: 0.85rem;
  font-weight: 600;
  text-align: center;
  color: var(--text);
  background: var(--surface1);
  border-radius: 10px;
  box-shadow: 0 8px 24px var(--crust);
  opacity: 0;
  transform: translate(-50%, 12px);
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

@media (max-width: 860px) {
  .layout {
    grid-template-columns: 1fr;
    justify-items: center;
  }

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

  .panel {
    width: 100%;
    max-width: var(--board);
  }

  .moves-list {
    max-height: 130px;
  }
}

@media (max-width: 420px) {
  body {
    --board: min(calc(100vw - 24px), 560px);
  }

  .buttons {
    flex-wrap: wrap;
  }

  .buttons .btn {
    flex: 1 1 30%;
    padding: 9px 8px;
    font-size: 0.8rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  * {
    transition-duration: 1ms !important;
  }
}
