:root {
  --blue-900: #053f8f;
  --blue-800: #0759bd;
  --blue-700: #086fd7;
  --blue-500: #19a7e8;
  --blue-200: #bfe7fb;
  --blue-100: #e9f7ff;
  --panel: rgba(255, 255, 255, 0.9);
  --panel-solid: #ffffff;
  --ink: #11223a;
  --muted: #61718a;
  --line: rgba(7, 89, 189, 0.16);
  --shadow: 0 18px 55px rgba(5, 63, 143, 0.18);
  --radius: 28px;
  --coord-size: clamp(1.15rem, 2.1vw, 1.65rem);
}

* {
  box-sizing: border-box;
}

body {
  height: 100vh;
  margin: 0;
  font-family: Arial, sans-serif;
  color: var(--ink);
  background:
    radial-gradient(circle at 82% 14%, rgba(255, 255, 255, 0.82), transparent 22rem),
    radial-gradient(circle at 22% 92%, rgba(5, 38, 174, 0.45), transparent 30rem),
    linear-gradient(140deg, rgba(8, 111, 215, 0.92), rgba(29, 175, 227, 0.48) 38%, rgba(247, 251, 255, 0.96) 58%),
    #d7f0ff;
  overflow: hidden;
}

body::before {
  position: fixed;
  inset: -20% -10% auto -12%;
  height: 58vh;
  pointer-events: none;
  content: "";
  background: linear-gradient(112deg, rgba(2, 111, 217, 0.96), rgba(36, 181, 225, 0.74), rgba(18, 58, 213, 0.9));
  border-radius: 0 0 52% 48%;
  filter: blur(7px);
  opacity: 0.9;
  transform: rotate(-5deg);
  z-index: -1;
}

.app-shell {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(270px, 330px);
  gap: clamp(1rem, 2vw, 1.5rem);
  height: 100vh;
  padding: clamp(0.75rem, 1.9vw, 1.55rem);
  overflow: hidden;
}

.board-panel,
.side-panel {
  background: var(--panel);
  border: 1px solid rgba(255, 255, 255, 0.7);
  box-shadow: var(--shadow);
  backdrop-filter: blur(20px);
}

.board-panel {
  display: grid;
  place-items: center;
  min-height: 0;
  border-radius: var(--radius);
  padding: clamp(0.75rem, 2.8vw, 2rem);
  overflow: hidden;
}

.board-wrap {
  display: grid;
  grid-template-columns: var(--coord-size) minmax(0, 1fr);
  grid-template-rows: minmax(0, 1fr) var(--coord-size);
  width: min(100%, calc(100vh - clamp(8rem, 15vh, 10rem)));
  max-height: 100%;
  aspect-ratio: 1;
  padding: clamp(0.35rem, 0.8vw, 0.55rem);
  background: rgba(255, 255, 255, 0.78);
  border-radius: 22px;
  box-shadow: 0 18px 45px rgba(5, 35, 80, 0.18);
}

.chessboard {
  position: relative;
  display: grid;
  grid-column: 2;
  grid-row: 1;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  width: 100%;
  height: 100%;
  border: 0;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 0 0 1px rgba(3, 30, 80, 0.22);
  user-select: none;
}

.rank-labels,
.file-labels {
  display: grid;
  color: var(--blue-900);
  font-size: clamp(0.75rem, 1.25vw, 1rem);
  font-weight: 900;
  line-height: 1;
}

.rank-labels {
  grid-column: 1;
  grid-row: 1;
  grid-template-rows: repeat(8, 1fr);
  padding-right: 0.35rem;
}

.file-labels {
  grid-column: 2;
  grid-row: 2;
  grid-template-columns: repeat(8, 1fr);
  padding-top: 0.35rem;
}

.rank-labels span,
.file-labels span {
  display: grid;
  place-items: center;
}

.square {
  position: relative;
  display: grid;
  place-items: center;
  min-width: 0;
  min-height: 0;
}

.square.light {
  background: #fdfdfd;
}

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

.square.selected {
  background: #9cc3ec;
}

.square.legal::after {
  width: 32%;
  height: 32%;
  content: "";
  background: rgba(8, 111, 215, 0.45);
  border-radius: 50%;
  box-shadow: 0 0 0 0.38rem rgba(255, 255, 255, 0.18);
}

.square.capture::after {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 74%;
  height: 74%;
  content: "";
  border: 0.35rem solid rgba(239, 68, 68, 0.62);
  border-radius: 50%;
  background: transparent;
  box-shadow: 0 0 0 0.38rem rgba(255, 255, 255, 0.18);
  transform: translate(-50%, -50%);
}

.square.reviewing {
  box-shadow: inset 0 0 0 999px rgba(255, 216, 77, 0.32);
}

.piece {
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  padding: 0;
  border: 0;
  background: transparent;
  cursor: grab;
  opacity: 1;
  transform: translateY(-0.01em);
  filter: drop-shadow(0 4px 4px rgba(0, 0, 0, 0.32));
}

.piece:active {
  cursor: grabbing;
}

.piece img {
  display: block;
  width: 90%;
  height: 90%;
  max-width: none;
  object-fit: contain;
  opacity: 1;
  pointer-events: none;
  user-select: none;
}

.piece.pawn img {
  width: 78%;
  height: 78%;
}

.piece.rook img,
.piece.knight img {
  width: 90%;
  height: 90%;
}

.piece.bishop img {
  width: 90%;
  height: 90%;
}

.piece.queen img {
  width: 90%;
  height: 90%;
}

.piece.king img {
  width: 90%;
  height: 90%;
}

.piece.black img {
  filter: drop-shadow(0 1px 0 rgba(255, 255, 255, 0.55));
}

.piece.white img {
  filter: drop-shadow(0 1px 0 rgba(255, 255, 255, 0.9));
}

.side-panel {
  display: flex;
  flex-direction: column;
  gap: clamp(0.65rem, 1.35vh, 1rem);
  min-height: 0;
  border-radius: var(--radius);
  padding: clamp(0.9rem, 1.7vw, 1.25rem);
  overflow: hidden;
}

.brand .eyebrow {
  margin: 0 0 0.45rem;
  color: var(--blue-800);
  font-size: clamp(0.68rem, 1vw, 0.76rem);
  font-weight: 800;
  letter-spacing: 0.06em;
  text-align: center;
  text-transform: uppercase;
}

.brand h1 {
  margin: 0;
  color: var(--blue-800);
  font-size: clamp(1.9rem, min(3.4vw, 7vh), 3rem);
  line-height: 0.88;
  letter-spacing: -0.08em;
  text-align: center;
  white-space: nowrap;
}

.turn-status {
  display: flex;
  align-items: center;
  justify-content: center;
  width: fit-content;
  min-height: 2.1rem;
  margin: 0.9rem auto 0;
  padding: 0.38rem 0.72rem;
  color: var(--blue-900);
  font-size: 0.9rem;
  font-weight: 800;
  background: var(--blue-100);
  border: 1px solid var(--line);
  border-radius: 999px;
}

.score-card {
  display: grid;
  grid-template-columns: 1fr 1fr;
  overflow: hidden;
  background: rgba(255, 255, 255, 0.82);
  border: 1px solid rgba(8, 111, 215, 0.28);
  border-radius: 14px;
}

.score-card div {
  padding: clamp(0.65rem, 1.4vh, 1rem) 0.7rem;
  text-align: center;
}

.score-card div + div {
  border-left: 1px solid var(--line);
}

.score-card span {
  display: block;
  color: var(--muted);
  font-size: 0.74rem;
  font-weight: 900;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.score-card strong {
  display: block;
  margin-top: 0.25rem;
  color: var(--blue-800);
  font-size: clamp(1.35rem, 3.1vh, 1.7rem);
}

.level-status {
  margin: -0.1rem 0 0.05rem;
  padding: 0.58rem 0.8rem;
  color: var(--blue-900);
  font-size: 0.84rem;
  font-weight: 900;
  text-align: center;
  background: linear-gradient(90deg, rgba(223, 243, 255, 0.92), rgba(204, 232, 251, 0.92));
  border: 1px solid rgba(8, 111, 215, 0.22);
  border-radius: 999px;
  box-shadow: 0 8px 20px rgba(5, 63, 143, 0.1);
}

.moves-card {
  flex: 1;
  min-height: 0;
  overflow: hidden;
  background: rgba(255, 255, 255, 0.82);
  border: 1px solid rgba(8, 111, 215, 0.28);
  border-radius: 14px;
}

.moves-card[hidden],
.piece-set-card[hidden] {
  display: none !important;
}

.moves-header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.72rem 0.9rem;
  color: var(--blue-900);
  background: linear-gradient(90deg, #dff3ff, #cce8fb);
  border-bottom: 1px solid var(--line);
}

.moves-header h2 {
  margin: 0;
  font-size: 0.95rem;
}

.moves-table-wrap {
  height: calc(100% - 2.7rem);
  overflow: auto;
}

.moves-table {
  width: 100%;
  border-collapse: collapse;
  font-family: "Courier New", monospace;
  font-size: 0.86rem;
}

.moves-table th,
.moves-table td {
  padding: 0.55rem 0.65rem;
  border-bottom: 1px solid rgba(9, 58, 120, 0.08);
  text-align: left;
}

.moves-table th {
  position: sticky;
  top: 0;
  z-index: 1;
  color: var(--muted);
  font-size: 0.72rem;
  background: #ffffff;
}

.moves-table tr.active {
  color: var(--blue-800);
  font-weight: 900;
  background: rgba(8, 111, 215, 0.08);
}

.controls {
  display: grid;
  gap: 0.7rem;
}

.piece-set-card {
  display: grid;
  justify-items: center;
  gap: 0.45rem;
  padding: 0.75rem;
  background: rgba(255, 255, 255, 0.82);
  border: 1px solid rgba(8, 111, 215, 0.28);
  border-radius: 14px;
}

.piece-set-card label {
  color: var(--blue-800);
  font-size: 0.74rem;
  font-weight: 900;
  letter-spacing: 0.05em;
  text-align: center;
  text-transform: uppercase;
}

.piece-set-card select {
  width: min(100%, 13.5rem);
  min-height: clamp(2.4rem, 5vh, 2.75rem);
  padding: 0 2.4rem 0 2.4rem;
  color: var(--blue-800);
  font: inherit;
  font-weight: 900;
  text-align: center;
  text-align-last: center;
  background:
    linear-gradient(45deg, transparent 50%, var(--blue-800) 50%) calc(100% - 1.15rem) 50% / 0.42rem 0.42rem no-repeat,
    linear-gradient(135deg, var(--blue-800) 50%, transparent 50%) calc(100% - 0.86rem) 50% / 0.42rem 0.42rem no-repeat,
    linear-gradient(180deg, #ffffff, var(--blue-100));
  border: 1px solid rgba(8, 111, 215, 0.18);
  border-radius: 999px;
  appearance: none;
  cursor: pointer;
}

.piece-set-card select:focus {
  outline: 3px solid rgba(25, 167, 232, 0.28);
  outline-offset: 2px;
}

.piece-set-card select:disabled {
  cursor: wait;
  opacity: 0.68;
}

.instructions-link {
  align-self: flex-end;
  min-height: auto;
  padding: 0.1rem 0.15rem;
  color: var(--blue-800);
  background: transparent;
  border: 0;
  border-radius: 0;
  box-shadow: none;
  font-size: 0.8rem;
  font-weight: 900;
  text-decoration: underline;
  text-underline-offset: 0.18em;
  cursor: pointer;
}

.instructions-link:hover:not(:disabled) {
  color: var(--blue-900);
  box-shadow: none;
}

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

.game-actions {
  display: grid;
  gap: 0.65rem;
}

button {
  font: inherit;
}

.review-controls button,
.restart-button,
.confetti-test-button,
.dialog-actions button {
  min-height: clamp(2.55rem, 5.6vh, 3rem);
  border: 0;
  border-radius: 14px;
  font-weight: 900;
  cursor: pointer;
  transition: transform 160ms ease, box-shadow 160ms ease, opacity 160ms ease;
}

.review-controls button {
  color: var(--blue-900);
  background: #e6f5ff;
  border: 1px solid rgba(8, 111, 215, 0.18);
  font-size: 1.25rem;
}

.restart-button,
.dialog-actions button[value="confirm"] {
  color: #ffffff;
  background: linear-gradient(180deg, var(--blue-700), var(--blue-800));
  box-shadow: 0 10px 20px rgba(7, 89, 189, 0.32);
}

.confetti-test-button {
  display: none;
  color: #ffffff;
  background: linear-gradient(180deg, #ff8a18, #ef476f);
  box-shadow: 0 10px 20px rgba(239, 71, 111, 0.28);
}

.dialog-actions button[value="cancel"],
#cancelRestart {
  color: var(--blue-900);
  background: #edf7ff;
}

button:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 11px 20px rgba(7, 89, 189, 0.22);
}

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

.game-dialog {
  width: min(92vw, 440px);
  padding: 0;
  color: var(--ink);
  background: var(--panel-solid);
  border: 1px solid rgba(8, 111, 215, 0.22);
  border-radius: 22px;
  box-shadow: var(--shadow);
}

.game-dialog::backdrop {
  background: rgba(4, 31, 70, 0.44);
  backdrop-filter: blur(4px);
}

.instructions-dialog {
  width: min(92vw, 560px);
  padding: 0;
  color: var(--ink);
  background: #ffffff;
  border: 1px solid rgba(8, 111, 215, 0.18);
  border-radius: 24px;
  box-shadow: var(--shadow);
}

.instructions-dialog::backdrop {
  background: rgba(4, 31, 70, 0.44);
  backdrop-filter: blur(4px);
}

.instructions-card {
  position: relative;
  padding: clamp(1.25rem, 4vw, 1.8rem);
  background: #ffffff;
}

.instructions-card h2 {
  margin: 0 3rem 1rem 0;
  color: var(--blue-800);
  font-size: clamp(1.55rem, 4vw, 2.05rem);
}

.instructions-card ul {
  display: grid;
  gap: 0.72rem;
  margin: 0;
  padding-left: 1.3rem;
  color: var(--ink);
  font-size: 0.98rem;
  line-height: 1.42;
}

.instructions-card li::marker {
  color: var(--blue-800);
}

.instructions-close {
  position: absolute;
  top: 0.75rem;
  right: 0.85rem;
  display: grid;
  place-items: center;
  width: 2.65rem;
  height: 2.65rem;
  min-height: 0;
  padding: 0;
  color: var(--blue-900);
  background: transparent;
  border: 0;
  border-radius: 50%;
  box-shadow: none;
  font-size: 2.25rem;
  font-weight: 900;
  line-height: 1;
  cursor: pointer;
}

.instructions-close:hover:not(:disabled) {
  background: var(--blue-100);
  box-shadow: none;
}

.game-dialog form {
  display: grid;
  gap: 1rem;
  padding: 1.35rem;
}

.game-dialog h2,
.game-dialog p {
  margin: 0;
}

.game-dialog h2 {
  color: var(--blue-800);
}

.game-dialog p {
  color: var(--muted);
}

fieldset {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.55rem;
  margin: 0;
  padding: 0.8rem;
  border: 1px solid var(--line);
  border-radius: 16px;
}

fieldset:last-of-type {
  grid-template-columns: repeat(2, 1fr);
}

legend {
  padding: 0 0.35rem;
  color: var(--blue-900);
  font-size: 0.82rem;
  font-weight: 900;
}

fieldset label {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.5rem;
  background: #f6fbff;
  border-radius: 10px;
  font-size: 0.9rem;
  font-weight: 700;
}

.dialog-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 1.1rem;
  z-index: 50;
  max-width: min(90vw, 520px);
  padding: 0.85rem 1rem;
  color: #ffffff;
  background: rgba(5, 63, 143, 0.92);
  border-radius: 999px;
  box-shadow: 0 15px 30px rgba(5, 35, 80, 0.28);
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 1rem);
  transition: opacity 180ms ease, transform 180ms ease;
}

.toast.center {
  top: 50%;
  bottom: auto;
  z-index: 70;
  width: max-content;
  padding: clamp(1rem, 2.5vw, 1.4rem) clamp(1.25rem, 4vw, 2.25rem);
  color: #073064;
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.96), rgba(233, 247, 255, 0.94)),
    #ffffff;
  border: 1px solid rgba(255, 255, 255, 0.88);
  border-radius: 24px;
  box-shadow: 0 24px 72px rgba(5, 35, 80, 0.32);
  font-size: clamp(1.05rem, 3vw, 1.65rem);
  font-weight: 950;
  text-align: center;
  transform: translate(-50%, calc(-50% + 1rem)) scale(0.92);
  backdrop-filter: blur(18px);
}

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

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

.site-credit {
  position: fixed;
  right: 0;
  bottom: 0.22rem;
  left: 0;
  z-index: 15;
  padding: 0 0.75rem;
  color: rgba(17, 34, 58, 0.68);
  font-size: clamp(0.68rem, 1vw, 0.78rem);
  font-weight: 800;
  line-height: 1.25;
  text-align: center;
  pointer-events: none;
}

.piece-loading {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: grid;
  place-items: center;
  background: rgba(233, 247, 255, 0.42);
  opacity: 0;
  pointer-events: none;
  transition: opacity 180ms ease;
}

.piece-loading.show {
  opacity: 1;
  pointer-events: auto;
}

.piece-loading-card {
  display: inline-flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.9rem 1.15rem;
  color: var(--blue-900);
  font-weight: 900;
  background: rgba(255, 255, 255, 0.94);
  border: 1px solid rgba(8, 111, 215, 0.22);
  border-radius: 999px;
  box-shadow: 0 18px 40px rgba(5, 63, 143, 0.22);
  backdrop-filter: blur(14px);
}

.piece-spinner {
  width: 1.45rem;
  height: 1.45rem;
  border: 0.22rem solid var(--blue-200);
  border-top-color: var(--blue-800);
  border-radius: 50%;
  animation: spin 760ms linear infinite;
}

.mobile-orientation-lock {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: none;
  place-items: center;
  padding: 1.25rem;
  background:
    radial-gradient(circle at 50% 15%, rgba(255, 255, 255, 0.42), transparent 18rem),
    linear-gradient(140deg, rgba(5, 63, 143, 0.96), rgba(25, 167, 232, 0.94));
}

.mobile-orientation-card {
  width: min(88vw, 380px);
  padding: 1.5rem;
  color: var(--blue-900);
  text-align: center;
  background: rgba(255, 255, 255, 0.94);
  border: 1px solid rgba(255, 255, 255, 0.78);
  border-radius: 24px;
  box-shadow: var(--shadow);
}

.mobile-orientation-card span {
  display: inline-grid;
  place-items: center;
  width: 4rem;
  height: 4rem;
  margin-bottom: 0.8rem;
  color: #ffffff;
  background: linear-gradient(180deg, var(--blue-700), var(--blue-900));
  border-radius: 50%;
  font-size: 2.3rem;
  font-weight: 900;
}

.mobile-orientation-card h2 {
  margin: 0 0 0.45rem;
  color: var(--blue-800);
  font-size: 1.7rem;
}

.mobile-orientation-card p {
  margin: 0;
  color: var(--muted);
  font-weight: 800;
  line-height: 1.35;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

body.is-mobile {
  height: 100svh;
}

body.is-mobile .moves-card,
body.is-mobile .piece-set-card {
  display: none;
}

body.is-mobile.is-landscape {
  overflow: hidden;
}

body.is-mobile.is-landscape .app-shell,
body.is-mobile.is-landscape .site-credit,
body.is-mobile.is-landscape .toast {
  display: none;
}

body.is-mobile.is-landscape .mobile-orientation-lock {
  display: grid;
}

@media (max-width: 980px) {
  .app-shell {
    grid-template-columns: minmax(0, 1fr) minmax(250px, 300px);
    gap: 0.85rem;
  }

  .board-wrap {
    width: min(100%, calc(100vh - 7.5rem));
  }
}

@media (max-width: 700px) and (orientation: portrait), (any-pointer: coarse) and (orientation: portrait) {
  body {
    padding-bottom: 0;
  }

  .app-shell {
    display: flex;
    flex-direction: column;
    gap: 0.55rem;
    height: 100svh;
    padding: 0.55rem 0.65rem 8rem;
  }

  .side-panel {
    display: contents;
  }

  .brand {
    order: -3;
    padding: 0.35rem 0.35rem 0;
    text-align: center;
  }

  .brand h1 {
    font-size: clamp(1.9rem, 9vw, 2.4rem);
  }

  .turn-status {
    min-height: 1.85rem;
    margin-top: 0.45rem;
    font-size: 0.82rem;
  }

  .board-panel {
    order: -2;
    padding: 0.4rem;
    border-radius: 22px;
  }

  .board-wrap {
    width: min(96vw, 43svh);
    padding: 0.3rem;
  }

  .rank-labels,
  .file-labels {
    font-size: 0.72rem;
  }

  .score-card {
    order: -1;
    margin: 0 0.35rem;
  }

  .score-card div {
    padding: 0.52rem 0.55rem;
  }

  .score-card strong {
    font-size: 1.3rem;
  }

  .controls {
    position: fixed;
    right: 0.75rem;
    bottom: calc(env(safe-area-inset-bottom, 0px) + 2rem);
    left: 0.75rem;
    z-index: 20;
    grid-template-columns: 0.85fr 1.15fr;
    align-items: center;
    padding: 0.55rem;
    background: rgba(255, 255, 255, 0.92);
    border: 1px solid rgba(8, 111, 215, 0.22);
    border-radius: 20px;
    box-shadow: var(--shadow);
    backdrop-filter: blur(16px);
  }

  .moves-card {
    flex: 1;
    min-height: 0;
    margin: 0 0.35rem;
  }

  .moves-header {
    padding: 0.58rem 0.75rem;
  }

  .moves-table {
    font-size: 0.78rem;
  }

  .moves-table th,
  .moves-table td {
    padding: 0.42rem 0.55rem;
  }

  .review-controls button,
  .restart-button,
  .confetti-test-button {
    min-height: 2.8rem;
  }

  .toast {
    bottom: calc(env(safe-area-inset-bottom, 0px) + 7.4rem);
  }

  .toast.center {
    top: 45%;
    max-width: min(88vw, 520px);
  }

  .site-credit {
    bottom: calc(env(safe-area-inset-bottom, 0px) + 0.4rem);
    font-size: 0.66rem;
  }
}
