:root {
  --bg: #0f1020;
  --panel: #1a1b28;
  --panel-2: #212233;
  --accent: #6842ff;
  --accent-hi: #8a6bff;
  --text: #fff;
  --muted: #aaadbe;
  --secondary: #2a2b3d;
  --light: #efe9ff;
  --dark: #8b76e8;
  --gold: #ffd23f;
  --bs: min(100%, 520px);
  color-scheme: dark;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family:
    "Nunito",
    ui-rounded,
    "SF Pro Rounded",
    system-ui,
    -apple-system,
    "Segoe UI",
    sans-serif;
  font-weight: 700;
  -webkit-tap-highlight-color: transparent;
}

body {
  overflow-x: hidden;
  background:
    radial-gradient(
      ellipse 70% 55% at 40% 45%,
      rgba(104, 66, 255, 0.2),
      transparent 70%
    ),
    var(--bg);
}

[hidden] {
  display: none !important;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

button {
  font: inherit;
  color: inherit;
}

:focus-visible {
  outline: 3px solid #b9a6ff;
  outline-offset: 3px;
}

kbd {
  display: inline-block;
  min-width: 20px;
  padding: 0 5px;
  border-radius: 6px;
  background: var(--secondary);
  box-shadow: inset 0 -2px 0 rgba(0, 0, 0, 0.35);
  color: #fff;
  font: inherit;
  font-size: 11px;
  text-align: center;
}

/* ---------- Layout ---------- */
.cs {
  max-width: 560px;
  margin: 0 auto;
  padding: 10px 12px 14px;
}

.hud {
  display: flex;
  gap: 8px;
  justify-content: center;
  margin-bottom: 10px;
}

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-areas:
    "top"
    "board"
    "bottom"
    "status"
    "actions"
    "moves"
    "help";
  gap: 8px;
  justify-items: center;
}

.layout > * {
  width: var(--bs);
}

.player-top {
  grid-area: top;
}
.player-bottom {
  grid-area: bottom;
}
.board-wrap {
  grid-area: board;
}
.status {
  grid-area: status;
}
.moves-panel {
  grid-area: moves;
}
.actions {
  grid-area: actions;
}
.help {
  grid-area: help;
}

@media (min-width: 820px) {
  .cs {
    max-width: 880px;
    padding: 10px 20px 12px;
  }
  .layout {
    --bs: 520px;
    grid-template-columns: 520px 300px;
    grid-template-rows: auto auto minmax(0, 1fr) auto auto auto;
    grid-template-areas:
      "board top"
      "board status"
      "board moves"
      "board actions"
      "board help"
      "board bottom";
    column-gap: 20px;
    row-gap: 8px;
    justify-content: center;
    align-items: stretch;
  }
  .layout > * {
    width: 100%;
  }
  .board-wrap {
    width: 520px;
  }
  .moves-panel {
    min-height: 0;
  }
  .moves {
    max-height: none !important;
    height: 100%;
  }
}

/* ---------- HUD chips ---------- */
.chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 38px;
  padding: 4px 14px;
  border-radius: 999px;
  background: var(--panel-2);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.07),
    0 4px 12px rgba(0, 0, 0, 0.25);
  font-size: 14px;
  font-weight: 900;
  white-space: nowrap;
  min-width: 0;
}

.chip-mode {
  padding-left: 5px;
  overflow: hidden;
}

.chip-mode > span:last-child {
  overflow: hidden;
  text-overflow: ellipsis;
}

.chip-icon {
  flex: none;
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: linear-gradient(160deg, #9d84ff, var(--accent));
  box-shadow: inset 0 2px 0 rgba(255, 255, 255, 0.3);
}

.chip-icon svg {
  width: 16px;
  height: 16px;
  fill: #fff;
  stroke: #fff;
  stroke-width: 1.2;
  stroke-linejoin: round;
}

.chip-label {
  color: var(--muted);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.chip strong {
  font-size: 17px;
  font-variant-numeric: tabular-nums;
}

.chip.bump strong {
  animation: bump 0.45s ease;
}

/* ---------- Players ---------- */
.player {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 48px;
  padding: 5px 6px 5px 5px;
  border-radius: 16px;
  background: var(--panel);
  border: 2px solid transparent;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.05);
  transition:
    border-color 0.2s,
    box-shadow 0.2s;
}

.player.is-turn {
  border-color: rgba(255, 210, 63, 0.85);
  box-shadow:
    0 0 0 3px rgba(255, 210, 63, 0.12),
    0 6px 18px rgba(255, 210, 63, 0.14);
}

.avatar {
  flex: none;
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border-radius: 12px;
  background: linear-gradient(160deg, #3a3560, #23213a);
  box-shadow: inset 0 2px 0 rgba(255, 255, 255, 0.12);
}

.avatar.w {
  background: linear-gradient(160deg, #ffffff, #d9d0ff);
}

.avatar .pv {
  width: 32px;
  height: 32px;
}

.pinfo {
  display: flex;
  flex-direction: column;
  min-width: 0;
  flex: 1;
  gap: 1px;
}

.pname {
  font-size: 14px;
  font-weight: 900;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.pname small {
  color: var(--muted);
  font-weight: 800;
  font-size: 12px;
}

.caps {
  display: flex;
  align-items: center;
  min-height: 18px;
  flex-wrap: nowrap;
  overflow: hidden;
}

.caps .pv {
  width: 18px;
  height: 18px;
  margin-right: -6px;
  flex: none;
}

.caps .pv.gap {
  margin-left: 6px;
}

.caps .diff {
  margin-left: 10px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 900;
}

.clock {
  flex: none;
  min-width: 72px;
  padding: 6px 10px;
  border-radius: 10px;
  background: #11121c;
  color: var(--muted);
  font-size: 18px;
  font-weight: 900;
  font-variant-numeric: tabular-nums;
  text-align: right;
}

.clock:empty {
  display: none;
}

.player.is-turn .clock {
  background: #fff;
  color: #17162a;
}

.clock.low {
  color: #ff5b6e !important;
}

.player.is-turn .clock.low {
  background: #ffe1e5;
}

/* ---------- Board ---------- */
.board-wrap {
  position: relative;
}

.board {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  border-radius: 14px;
  overflow: hidden;
  box-shadow:
    0 0 0 5px #2b2748,
    0 0 0 7px rgba(157, 132, 255, 0.35),
    0 18px 40px rgba(0, 0, 0, 0.5);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.squares {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
}

.sq {
  position: relative;
  border: 0;
  margin: 0;
  padding: 0;
  cursor: pointer;
  background: var(--light);
  font-size: clamp(8px, 2.2vw, 12px);
  font-weight: 900;
}

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

.sq:focus-visible {
  outline: 3px solid #fff;
  outline-offset: -3px;
  z-index: 2;
}

.sq.last::before,
.sq.sel::before {
  content: "";
  position: absolute;
  inset: 0;
  background: rgba(255, 208, 64, 0.5);
}

.sq.sel::before {
  background: rgba(255, 196, 30, 0.72);
  box-shadow: inset 0 0 0 3px rgba(255, 255, 255, 0.55);
}

.sq.check {
  background-image: radial-gradient(
    circle,
    #ff3b52 0%,
    rgba(255, 59, 82, 0.8) 30%,
    rgba(255, 59, 82, 0) 72%
  );
  animation: check-pulse 1.1s ease-in-out infinite;
}

.sq.tgt::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 30%;
  height: 30%;
  border-radius: 50%;
  transform: translate(-50%, -50%);
  background: rgba(38, 20, 90, 0.32);
  z-index: 1;
}

.sq.tgt.cap::after {
  width: 86%;
  height: 86%;
  background: transparent;
  border: 5px solid rgba(38, 20, 90, 0.34);
}

.sq.drop {
  box-shadow: inset 0 0 0 4px rgba(255, 255, 255, 0.85);
}

.coord {
  position: absolute;
  line-height: 1;
  pointer-events: none;
  color: var(--dark);
}

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

.coord-r {
  top: 3px;
  left: 4px;
}

.coord-f {
  right: 4px;
  bottom: 3px;
}

.pieces,
.fx {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.pc {
  position: absolute;
  left: 0;
  top: 0;
  width: 12.5%;
  height: 12.5%;
  display: grid;
  place-items: center;
  transition: transform 0.2s cubic-bezier(0.25, 0.8, 0.3, 1);
  will-change: transform;
  z-index: 2;
}

.pc.moving {
  z-index: 4;
}

.pc.dragging {
  transition: none;
  z-index: 6;
}

.pc.dragging .pv {
  transform: scale(1.14);
  filter: drop-shadow(0 10px 10px rgba(0, 0, 0, 0.45));
}

.pc.faded {
  opacity: 0;
  transition: opacity 0.18s;
}

.pc .pv {
  width: 88%;
  height: 88%;
  transition: transform 0.12s;
}

.pv {
  display: block;
  background-repeat: no-repeat;
  background-position: center;
  background-size: contain;
  filter: drop-shadow(0 3px 2px rgba(20, 8, 60, 0.35));
}

.pv svg {
  display: block;
  width: 100%;
  height: 100%;
  overflow: visible;
}

.pc.pop .pv {
  animation: pop 0.28s ease-out;
}

.pc.shake .pv {
  animation: shake 0.36s ease;
}

.board.shake {
  animation: shake 0.36s ease;
}

/* Promotion picker */
.promo {
  position: absolute;
  z-index: 10;
  display: flex;
  flex-direction: column;
  width: 12.5%;
  min-width: 46px;
  padding: 4px;
  gap: 4px;
  border-radius: 14px;
  background: #fff;
  box-shadow:
    0 10px 30px rgba(0, 0, 0, 0.5),
    0 0 0 3px var(--accent);
  animation: pop-in 0.18s ease-out;
}

.promo[hidden] {
  display: none;
}

.promo button {
  display: grid;
  place-items: center;
  aspect-ratio: 1;
  min-height: 40px;
  border: 0;
  border-radius: 10px;
  background: #f0ecff;
  cursor: pointer;
  padding: 3px;
}

.promo button:hover,
.promo button:focus-visible {
  background: #ddd3ff;
}

.promo button .pv {
  width: 100%;
  height: 100%;
}

.promo .promo-x {
  aspect-ratio: auto;
  min-height: 30px;
  background: transparent;
  color: #5c577a;
  font-size: 18px;
  font-weight: 900;
}

/* Thinking badge */
.think-badge {
  position: absolute;
  z-index: 8;
  right: 8px;
  top: 8px;
  display: flex;
  gap: 4px;
  padding: 7px 10px;
  border-radius: 999px;
  background: rgba(22, 18, 48, 0.82);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.35);
  opacity: 0;
  transform: scale(0.7);
  transition:
    opacity 0.2s,
    transform 0.2s;
  pointer-events: none;
}

.board.thinking .think-badge {
  opacity: 1;
  transform: scale(1);
}

.think-badge span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #c9b9ff;
  animation: think 0.9s ease-in-out infinite;
}

.think-badge span:nth-child(2) {
  animation-delay: 0.15s;
}

.think-badge span:nth-child(3) {
  animation-delay: 0.3s;
}

/* Particles */
.spark {
  position: absolute;
  width: 9px;
  height: 9px;
  margin: -4.5px 0 0 -4.5px;
  border-radius: 50%;
  background: var(--c, #ffd23f);
  box-shadow: 0 0 8px var(--c, #ffd23f);
  animation: spark 0.6s ease-out forwards;
}

.ring {
  position: absolute;
  width: 12.5%;
  height: 12.5%;
  border-radius: 50%;
  border: 4px solid rgba(255, 230, 120, 0.9);
  animation: ring 0.5s ease-out forwards;
}

/* ---------- Status, moves, actions ---------- */
.status {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin: 0;
  min-height: 36px;
  padding: 4px 14px;
  border-radius: 999px;
  background: rgba(104, 66, 255, 0.16);
  color: #ddd5ff;
  font-size: 14px;
  font-weight: 800;
  text-align: center;
}

.status-dot {
  flex: none;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.25);
}

.status.black-turn .status-dot {
  background: #2d2946;
  box-shadow: 0 0 0 2px #8b83b8;
}

.status.is-alert {
  background: rgba(255, 70, 95, 0.18);
  color: #ffc2cb;
}

.status.is-over {
  background: rgba(255, 210, 63, 0.16);
  color: #ffe58c;
}

.status.is-thinking .status-dot {
  animation: think 0.9s ease-in-out infinite;
}

.moves-panel {
  position: relative;
  border-radius: 16px;
  background: var(--panel);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.05);
  overflow: hidden;
}

.moves {
  list-style: none;
  margin: 0;
  padding: 6px;
  max-height: 104px;
  overflow-y: auto;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(128px, 1fr));
  align-content: start;
  gap: 2px;
  font-size: 14px;
  scrollbar-width: thin;
  scrollbar-color: #3d3b5c transparent;
}

@media (min-width: 820px) {
  .moves {
    grid-template-columns: 1fr;
  }
}

.moves li {
  display: grid;
  grid-template-columns: 30px 1fr 1fr;
  align-items: center;
  padding: 2px 4px;
  border-radius: 8px;
}

.moves li:nth-child(odd) {
  background: rgba(255, 255, 255, 0.03);
}

.moves .n {
  color: var(--muted);
  font-size: 12px;
}

.moves .mv {
  padding: 2px 6px;
  border-radius: 6px;
  font-weight: 900;
}

.moves .mv.cur {
  background: var(--accent);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.25);
}

.moves-empty {
  margin: 0;
  padding: 14px;
  color: var(--muted);
  font-size: 13px;
  text-align: center;
}

.moves-empty[hidden] {
  display: none;
}

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

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  padding: 8px 14px;
  border: 0;
  border-radius: 999px;
  cursor: pointer;
  font-size: 15px;
  font-weight: 900;
  transition:
    transform 0.12s,
    filter 0.12s,
    background 0.12s;
}

.btn svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.4;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.btn:active:not(:disabled) {
  transform: scale(0.95);
}

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

.btn-primary {
  background: linear-gradient(180deg, #8563ff, var(--accent));
  box-shadow:
    inset 0 2px 0 rgba(255, 255, 255, 0.28),
    0 5px 0 #4424c4,
    0 10px 20px rgba(104, 66, 255, 0.4);
  color: #fff;
}

.btn-primary:hover:not(:disabled) {
  filter: brightness(1.08);
}

.btn-secondary {
  background: var(--secondary);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.08),
    0 3px 0 #1a1a28;
  color: #fff;
}

.btn-secondary:hover:not(:disabled) {
  background: #34354b;
}

.btn-big {
  min-height: 54px;
  padding: 12px 34px;
  font-size: 20px;
}

.help {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.7;
  text-align: center;
}

@media (hover: none) and (pointer: coarse) {
  .help {
    display: none;
  }
}

/* ---------- Overlays ---------- */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 16px;
  overflow-y: auto;
  background: rgba(10, 10, 24, 0.72);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}

.overlay[hidden] {
  display: none;
}

.card {
  position: relative;
  width: min(100%, 420px);
  padding: 14px 18px 18px;
  border-radius: 26px;
  background: linear-gradient(180deg, #25264a, #1a1b28);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.1),
    0 0 0 2px rgba(135, 110, 255, 0.35),
    0 30px 60px rgba(0, 0, 0, 0.55);
  text-align: center;
  animation: pop-in 0.3s cubic-bezier(0.2, 1.4, 0.4, 1);
}

.card h2 {
  margin: 2px 0 0;
  font-size: 32px;
  font-weight: 900;
  letter-spacing: -0.01em;
  background: linear-gradient(180deg, #fff 30%, #cbbcff);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  filter: drop-shadow(0 3px 0 #4424c4);
}

.lead {
  margin: 0 0 12px;
  color: var(--muted);
  font-size: 15px;
}

.logo {
  display: flex;
  justify-content: center;
  align-items: flex-end;
  gap: 2px;
  height: 64px;
}

.logo .pv {
  width: 50px;
  height: 50px;
  animation: bob 2.4s ease-in-out infinite;
}

.logo .pv:nth-child(2) {
  width: 64px;
  height: 64px;
  animation-delay: 0.3s;
}

.logo .pv:nth-child(3) {
  animation-delay: 0.6s;
}

.opt {
  display: grid;
  gap: 4px;
  margin-bottom: 8px;
  text-align: left;
}

.opt[hidden] {
  display: none;
}

.opt-label {
  color: var(--muted);
  font-size: 12px;
  font-weight: 900;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding-left: 6px;
}

.opt-label em {
  font-style: normal;
  color: #cbbcff;
  text-transform: none;
  letter-spacing: 0;
  margin-left: 4px;
}

.seg-row {
  display: flex;
  gap: 4px;
  padding: 4px;
  border-radius: 999px;
  background: #13141f;
}

.seg {
  flex: 1;
  min-height: 40px;
  padding: 6px 8px;
  border: 0;
  border-radius: 999px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 14px;
  font-weight: 900;
  white-space: nowrap;
  transition:
    background 0.15s,
    color 0.15s;
}

.seg[aria-checked="true"] {
  background: linear-gradient(180deg, #8563ff, var(--accent));
  color: #fff;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.25);
}

.start-actions {
  display: grid;
  gap: 10px;
  margin-top: 12px;
}

.card-end {
  overflow: hidden;
}

.end-badge {
  display: grid;
  place-items: center;
  width: 96px;
  height: 96px;
  margin: 0 auto;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #fff5c2, #ffd23f 45%, #f09a00);
  box-shadow:
    inset 0 -6px 0 rgba(0, 0, 0, 0.15),
    0 10px 24px rgba(255, 180, 0, 0.35);
  animation: pop-in 0.45s cubic-bezier(0.2, 1.6, 0.4, 1);
}

.end-badge.draw {
  background: radial-gradient(circle at 35% 30%, #e6e0ff, #a996ff 45%, #6842ff);
  box-shadow:
    inset 0 -6px 0 rgba(0, 0, 0, 0.15),
    0 10px 24px rgba(104, 66, 255, 0.4);
}

.end-badge.loss {
  background: radial-gradient(circle at 35% 30%, #d8d6e8, #7d7a99 45%, #3b3957);
  box-shadow: inset 0 -6px 0 rgba(0, 0, 0, 0.2);
}

.end-badge .pv {
  width: 64px;
  height: 64px;
}

.end-score {
  margin: 0 0 14px;
  color: #fff;
  font-size: 15px;
}

.end-score:empty {
  display: none;
}

.end-actions {
  display: grid;
  gap: 10px;
}

.end-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.burst {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.confetti {
  position: absolute;
  top: -12px;
  width: 9px;
  height: 14px;
  border-radius: 3px;
  animation: confetti 1.6s ease-in forwards;
}

/* ---------- Animations ---------- */
@keyframes pop {
  0% {
    transform: scale(1.18);
  }
  60% {
    transform: scale(0.95);
  }
  100% {
    transform: scale(1);
  }
}

@keyframes pop-in {
  from {
    transform: scale(0.85);
    opacity: 0;
  }
  to {
    transform: scale(1);
    opacity: 1;
  }
}

@keyframes shake {
  0%,
  100% {
    transform: translateX(0);
  }
  20% {
    transform: translateX(-6px);
  }
  40% {
    transform: translateX(6px);
  }
  60% {
    transform: translateX(-4px);
  }
  80% {
    transform: translateX(3px);
  }
}

@keyframes bump {
  40% {
    transform: scale(1.3);
  }
}

@keyframes think {
  0%,
  100% {
    transform: translateY(0);
    opacity: 0.55;
  }
  50% {
    transform: translateY(-4px);
    opacity: 1;
  }
}

@keyframes check-pulse {
  50% {
    filter: brightness(1.15) saturate(1.2);
  }
}

@keyframes spark {
  to {
    transform: translate(var(--dx), var(--dy)) scale(0.2);
    opacity: 0;
  }
}

@keyframes ring {
  from {
    transform: scale(0.4);
    opacity: 1;
  }
  to {
    transform: scale(1.4);
    opacity: 0;
  }
}

@keyframes bob {
  50% {
    transform: translateY(-6px);
  }
}

@keyframes confetti {
  to {
    transform: translate(var(--dx), 440px) rotate(var(--rot));
    opacity: 0.2;
  }
}

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

@media (max-width: 420px) {
  .wide,
  .lvname {
    display: none;
  }
  .chip {
    padding: 4px 10px;
    font-size: 13px;
  }
  .chip-mode {
    padding-left: 4px;
  }
}

@media (max-width: 350px) {
  #chip-record .chip-label {
    display: none;
  }
}
