:root {
  color-scheme: dark;
  --bg: #100d17;
  --surface: #191522;
  --surface-2: #211b2d;
  --border: rgba(255, 255, 255, 0.09);
  --text: #f7f3ff;
  --muted: #aaa1b8;
  --violet: #a784ff;
  --violet-strong: #8b62f2;
  --gold: #e6bd6b;
  --light-square: #cfc1dc;
  --dark-square: #655376;
  --danger: #ff8a93;
  --board-size: min(70vh, 660px, calc(100vw - 2rem));
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  background: var(--bg);
}

body {
  min-height: 100vh;
  min-height: 100svh;
  margin: 0;
  color: var(--text);
  background:
    radial-gradient(circle at 12% 0%, rgba(139, 98, 242, 0.18), transparent 34rem),
    radial-gradient(circle at 96% 88%, rgba(230, 189, 107, 0.08), transparent 28rem),
    var(--bg);
}

button {
  font: inherit;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}

.app-shell {
  width: min(1180px, calc(100% - 2rem));
  margin: 0 auto;
  padding: clamp(1.25rem, 3vw, 2.5rem) 0 calc(2rem + env(safe-area-inset-bottom));
}

.masthead {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: clamp(1.25rem, 3vw, 2.25rem);
}

.eyebrow {
  margin: 0 0 0.35rem;
  color: var(--violet);
  font-size: 0.75rem;
  font-weight: 800;
  letter-spacing: 0.18em;
}

h1,
h2,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  font-family: Georgia, "Times New Roman", serif;
  font-size: clamp(2rem, 5vw, 3.75rem);
  font-weight: 500;
  letter-spacing: -0.035em;
  line-height: 0.95;
}

.turn-pill {
  display: inline-flex;
  align-items: center;
  gap: 0.6rem;
  min-height: 2.7rem;
  padding: 0.7rem 1rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: rgba(25, 21, 34, 0.82);
  color: var(--text);
  font-size: 0.9rem;
  font-weight: 700;
  backdrop-filter: blur(12px);
}

.turn-dot {
  width: 0.65rem;
  height: 0.65rem;
  border-radius: 50%;
  background: #f4edf8;
  box-shadow: 0 0 0 3px rgba(244, 237, 248, 0.14);
}

.turn-dot.black {
  background: #6e6078;
  box-shadow: 0 0 0 3px rgba(110, 96, 120, 0.18);
}

.turn-dot.finished {
  background: var(--gold);
  box-shadow: 0 0 0 3px rgba(230, 189, 107, 0.16);
}

.game-layout {
  display: grid;
  grid-template-columns: minmax(0, var(--board-size)) minmax(260px, 1fr);
  align-items: stretch;
  gap: clamp(1.25rem, 3vw, 2rem);
}

.board-column {
  min-width: 0;
}

.player-strip {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  min-height: 3.2rem;
  padding: 0.35rem 0.15rem;
}

.player-strip-top {
  margin-bottom: 0.35rem;
}

.player-strip-bottom {
  margin-top: 0.35rem;
}

.player-strip strong {
  display: block;
  font-size: 0.95rem;
}

.player-mark {
  display: grid;
  width: 2.5rem;
  height: 2.5rem;
  place-items: center;
  border: 1px solid var(--border);
  border-radius: 0.75rem;
  background: var(--surface-2);
}

.player-mark img {
  width: 88%;
  height: 88%;
  object-fit: contain;
}

.captured {
  display: flex;
  align-items: center;
  min-height: 1rem;
  padding-top: 0.05rem;
}

.captured img {
  width: 1.15rem;
  height: 1.15rem;
  margin-right: -0.22rem;
  object-fit: contain;
  opacity: 0.72;
}

.board-frame {
  width: 100%;
  aspect-ratio: 1;
  padding: clamp(0.35rem, 1.1vw, 0.6rem);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: clamp(0.75rem, 2vw, 1.1rem);
  background: linear-gradient(145deg, #30263c, #19141f);
  box-shadow:
    0 30px 80px rgba(0, 0, 0, 0.35),
    0 3px 12px rgba(0, 0, 0, 0.28),
    inset 0 1px rgba(255, 255, 255, 0.08);
}

.board {
  display: grid;
  width: 100%;
  height: 100%;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  overflow: hidden;
  border-radius: clamp(0.45rem, 1.5vw, 0.7rem);
  touch-action: manipulation;
  user-select: none;
}

.board.drag-mode {
  touch-action: none;
  -webkit-touch-callout: none;
}

.square {
  position: relative;
  display: grid;
  min-width: 0;
  min-height: 0;
  padding: 0;
  place-items: center;
  overflow: hidden;
  border: 0;
  border-radius: 0;
  color: inherit;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

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

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

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

.square.last-move::before,
.square.selected::before {
  position: absolute;
  inset: 0;
  content: "";
  pointer-events: none;
}

.square.last-move::before {
  background: rgba(230, 189, 107, 0.35);
}

.square.selected::before {
  background: rgba(167, 132, 255, 0.56);
  box-shadow: inset 0 0 0 4px rgba(255, 255, 255, 0.34);
}

.square.knight-boost::before {
  position: absolute;
  z-index: 1;
  inset: 0;
  background: rgba(167, 132, 255, 0.62);
  box-shadow:
    inset 0 0 0 clamp(3px, 0.55vw, 6px) rgba(255, 230, 150, 0.92),
    inset 0 0 24px rgba(167, 132, 255, 0.72);
  content: "";
  pointer-events: none;
}

.square.knight-boost .piece {
  filter:
    drop-shadow(0 2px 1px rgba(20, 14, 25, 0.18))
    drop-shadow(0 0 9px rgba(255, 230, 150, 0.92));
}

.square.legal::after {
  position: absolute;
  width: 23%;
  aspect-ratio: 1;
  border-radius: 50%;
  background: rgba(24, 17, 30, 0.45);
  content: "";
  pointer-events: none;
}

.square.legal.capture::after {
  width: 76%;
  background: transparent;
  border: clamp(3px, 0.5vw, 6px) solid rgba(35, 24, 44, 0.43);
}

.square.legal.vaticano::after {
  width: 76%;
  background: transparent;
  border: clamp(3px, 0.5vw, 6px) solid rgba(230, 189, 107, 0.92);
  box-shadow:
    0 0 0 3px rgba(61, 43, 75, 0.3),
    0 0 18px rgba(230, 189, 107, 0.5);
}

.square.vaticano-victim::before {
  position: absolute;
  z-index: 1;
  inset: 0;
  background: rgba(230, 189, 107, 0.34);
  box-shadow: inset 0 0 0 3px rgba(255, 231, 168, 0.45);
  content: "";
  pointer-events: none;
}

.square.forced-en-passant {
  box-shadow:
    inset 0 0 0 clamp(3px, 0.55vw, 6px) rgba(230, 189, 107, 0.95),
    inset 0 0 18px rgba(230, 189, 107, 0.42);
}

.square.forced-en-passant .piece {
  filter:
    drop-shadow(0 2px 1px rgba(20, 14, 25, 0.18))
    drop-shadow(0 0 7px rgba(255, 222, 143, 0.7));
}

.square.in-check {
  background: #b44758;
}

.square.drag-source {
  cursor: grab;
}

.square.drag-source:active {
  cursor: grabbing;
}

.square.drag-origin .piece {
  opacity: 0.18;
}

.square.drag-hover-valid {
  box-shadow:
    inset 0 0 0 clamp(4px, 0.65vw, 7px) rgba(226, 255, 185, 0.96),
    inset 0 0 26px rgba(160, 221, 104, 0.56);
}

.square.drag-hover-invalid {
  box-shadow:
    inset 0 0 0 clamp(3px, 0.55vw, 6px) rgba(255, 126, 139, 0.75),
    inset 0 0 20px rgba(180, 50, 70, 0.28);
}

.drag-piece {
  position: fixed;
  z-index: 100;
  display: block;
  object-fit: contain;
  transform: translate(-50%, -57%) scale(1.08);
  filter:
    drop-shadow(0 10px 8px rgba(8, 5, 12, 0.38))
    drop-shadow(0 2px 2px rgba(8, 5, 12, 0.46));
  pointer-events: none;
  will-change: left, top;
}

.piece {
  position: relative;
  z-index: 2;
  display: block;
  width: 94%;
  height: 94%;
  object-fit: contain;
  transform: translateY(-1%);
  filter: drop-shadow(0 2px 1px rgba(20, 14, 25, 0.18));
  transition: transform 110ms ease, filter 110ms ease;
  pointer-events: none;
}

.square:hover .piece {
  transform: translateY(-4%) scale(1.035);
}

.coord {
  position: absolute;
  z-index: 3;
  font-size: clamp(0.5rem, 1.2vw, 0.72rem);
  font-weight: 900;
  line-height: 1;
  pointer-events: none;
}

.coord.rank {
  top: 0.27rem;
  left: 0.3rem;
}

.coord.file {
  right: 0.3rem;
  bottom: 0.25rem;
}

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

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

.side-panel {
  display: flex;
  min-height: 0;
  flex-direction: column;
  padding: clamp(1.1rem, 2.5vw, 1.5rem);
  border: 1px solid var(--border);
  border-radius: 1.25rem;
  background: rgba(25, 21, 34, 0.82);
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.22);
  backdrop-filter: blur(16px);
}

.panel-heading {
  display: flex;
  align-items: start;
  justify-content: space-between;
  gap: 0.75rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid var(--border);
}

.panel-heading h2 {
  margin: 0;
  font-family: Georgia, "Times New Roman", serif;
  font-size: clamp(1.4rem, 3vw, 2rem);
  font-weight: 500;
  line-height: 1.1;
}

.move-count {
  flex: 0 0 auto;
  padding: 0.42rem 0.65rem;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 800;
}

.online-card {
  margin-top: 1rem;
  padding: 0.9rem;
  border: 1px solid rgba(167, 132, 255, 0.2);
  border-radius: 0.9rem;
  background: linear-gradient(135deg, rgba(139, 98, 242, 0.13), rgba(255, 255, 255, 0.025));
}

.online-card-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}

.online-card-heading .eyebrow {
  margin-bottom: 0.15rem;
  font-size: 0.68rem;
}

.online-card-heading strong {
  font-family: Georgia, "Times New Roman", serif;
  font-size: 1.08rem;
  font-weight: 500;
}

.connection-badge {
  display: inline-flex;
  align-items: center;
  min-height: 1.7rem;
  padding: 0.3rem 0.55rem;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.07);
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 800;
}

.connection-badge.live {
  background: rgba(106, 211, 154, 0.13);
  color: #9ce0bb;
}

.connection-badge.waiting {
  background: rgba(230, 189, 107, 0.13);
  color: #f0cf8d;
}

.online-idle,
.online-room {
  margin-top: 0.75rem;
}

.online-idle[hidden],
.online-room[hidden] {
  display: none;
}

.online-idle p,
.online-room p {
  margin: 0.55rem 0 0;
  color: var(--muted);
  font-size: 0.78rem;
  line-height: 1.4;
}

.online-main-button,
.online-actions button {
  min-height: 2.45rem;
  border: 1px solid rgba(167, 132, 255, 0.28);
  border-radius: 0.65rem;
  background: rgba(139, 98, 242, 0.2);
  color: var(--text);
  font-size: 0.82rem;
  font-weight: 800;
  cursor: pointer;
}

.online-main-button {
  width: 100%;
  padding: 0.6rem 0.75rem;
}

.online-main-button:disabled,
.online-actions button:disabled {
  opacity: 0.45;
  cursor: wait;
}

.online-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
  margin-top: 0.7rem;
}

.online-actions button {
  padding: 0.55rem 0.65rem;
  border-color: var(--border);
  background: rgba(255, 255, 255, 0.055);
}

.online-main-button:hover:not(:disabled),
.online-actions button:hover:not(:disabled) {
  border-color: rgba(167, 132, 255, 0.55);
  background: rgba(139, 98, 242, 0.28);
}

.online-main-button:focus-visible,
.online-actions button:focus-visible {
  outline: 3px solid rgba(167, 132, 255, 0.75);
  outline-offset: 2px;
}

.history-wrap {
  position: relative;
  min-height: 11rem;
  flex: 1;
  margin: 0.9rem 0 1rem;
  overflow: auto;
}

.empty-history {
  position: absolute;
  inset: 0;
  display: grid;
  align-content: center;
  justify-items: center;
  color: var(--muted);
  text-align: center;
}

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

.empty-history span {
  margin-bottom: 0.25rem;
  color: rgba(255, 255, 255, 0.22);
  font-family: Georgia, "Times New Roman", serif;
  font-size: 2.5rem;
}

.empty-history p {
  margin: 0;
  font-size: 0.9rem;
}

.move-history {
  display: grid;
  grid-template-columns: 2.4rem 1fr 1fr;
  gap: 0.25rem 0.4rem;
  align-items: center;
}

.move-number,
.move-entry {
  min-height: 2rem;
  padding: 0.4rem 0.5rem;
  border-radius: 0.45rem;
  font-size: 0.9rem;
}

.move-number {
  color: #756d80;
  font-variant-numeric: tabular-nums;
  text-align: right;
}

.move-entry {
  background: rgba(255, 255, 255, 0.035);
  font-weight: 700;
}

.move-entry.latest {
  background: rgba(167, 132, 255, 0.16);
  color: #d8c9ff;
}

.controls {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.65rem;
}

.control-button {
  min-height: 2.9rem;
  padding: 0.75rem 0.9rem;
  border: 1px solid var(--border);
  border-radius: 0.75rem;
  background: var(--surface-2);
  color: var(--text);
  font-size: 0.9rem;
  font-weight: 800;
  cursor: pointer;
  transition: transform 120ms ease, border-color 120ms ease, background 120ms ease;
}

.control-button:hover:not(:disabled) {
  transform: translateY(-1px);
  border-color: rgba(255, 255, 255, 0.18);
  background: #2a2237;
}

.control-button:focus-visible,
.promotion-button:focus-visible {
  outline: 3px solid rgba(167, 132, 255, 0.75);
  outline-offset: 2px;
}

.control-button:disabled {
  opacity: 0.38;
  cursor: not-allowed;
}

.control-button.primary {
  border-color: rgba(167, 132, 255, 0.25);
  background: rgba(139, 98, 242, 0.2);
}

.control-button.mode-toggle {
  grid-column: 1 / -1;
}

.control-button.mode-toggle.active {
  border-color: rgba(167, 132, 255, 0.48);
  background: rgba(139, 98, 242, 0.25);
  color: #e0d5ff;
}

.control-button.danger {
  grid-column: 1 / -1;
  color: #ffc1c6;
}

.modal-backdrop {
  position: fixed;
  z-index: 50;
  inset: 0;
  display: grid;
  padding: 1rem;
  place-items: center;
  background: rgba(8, 6, 12, 0.74);
  backdrop-filter: blur(10px);
}

.modal-backdrop[hidden] {
  display: none;
}

.promotion-card {
  width: min(30rem, 100%);
  padding: 1.5rem;
  border: 1px solid rgba(255, 255, 255, 0.13);
  border-radius: 1.25rem;
  background: #1c1725;
  box-shadow: 0 30px 100px rgba(0, 0, 0, 0.55);
  text-align: center;
}

.promotion-card h2 {
  margin-bottom: 1.25rem;
  font-family: Georgia, "Times New Roman", serif;
  font-size: 1.8rem;
  font-weight: 500;
}

.promotion-options {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.65rem;
}

.promotion-button {
  display: grid;
  aspect-ratio: 1;
  place-items: center;
  border: 1px solid var(--border);
  border-radius: 0.85rem;
  background: var(--surface-2);
  cursor: pointer;
}

.promotion-button img {
  width: 88%;
  height: 88%;
  object-fit: contain;
  pointer-events: none;
}

.promotion-button:hover:not(:disabled) {
  border-color: rgba(167, 132, 255, 0.6);
  background: rgba(139, 98, 242, 0.22);
}

.promotion-button:disabled {
  opacity: 0.32;
  filter: grayscale(0.8);
  cursor: not-allowed;
}

.promotion-hint {
  margin: 0.9rem 0 0;
  color: #d8c9ff;
  font-size: 0.82rem;
  line-height: 1.4;
}

.toast {
  position: fixed;
  z-index: 60;
  left: 50%;
  bottom: calc(1.2rem + env(safe-area-inset-bottom));
  max-width: calc(100vw - 2rem);
  padding: 0.75rem 1rem;
  transform: translate(-50%, 1rem);
  border: 1px solid var(--border);
  border-radius: 999px;
  background: #251e30;
  box-shadow: 0 12px 35px rgba(0, 0, 0, 0.38);
  color: var(--text);
  font-size: 0.88rem;
  font-weight: 700;
  opacity: 0;
  pointer-events: none;
  transition: opacity 160ms ease, transform 160ms ease;
}

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

@media (max-width: 850px) {
  :root {
    --board-size: min(100%, calc(100vw - 2rem));
  }

  .game-layout {
    grid-template-columns: 1fr;
  }

  .side-panel {
    min-height: 24rem;
  }

  .history-wrap {
    max-height: 15rem;
  }
}

@media (max-width: 540px) {
  .app-shell {
    width: min(100% - 1rem, 1180px);
    padding-top: 0.9rem;
  }

  .masthead {
    align-items: center;
    margin-bottom: 0.85rem;
  }

  .masthead .eyebrow {
    display: none;
  }

  h1 {
    font-size: 1.75rem;
  }

  .turn-pill {
    min-height: 2.25rem;
    padding: 0.5rem 0.75rem;
    font-size: 0.78rem;
  }

  .player-strip {
    min-height: 2.65rem;
  }

  .player-mark {
    width: 2.1rem;
    height: 2.1rem;
    border-radius: 0.6rem;
    font-size: 1.25rem;
  }

  .player-strip strong {
    font-size: 0.84rem;
  }

  .captured {
    font-size: 0.85rem;
  }

  .board-frame {
    padding: 0.3rem;
    border-radius: 0.75rem;
  }

  .side-panel {
    min-height: 22rem;
    border-radius: 1rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    transition-duration: 0.01ms !important;
  }
}
