:root {
  color-scheme: light;
  --bg: #eef2f5;
  --surface: rgba(255, 255, 255, 0.78);
  --ink: #17202a;
  --muted: #667380;
  --light: #e7eef2;
  --dark: #78909c;
  --selected: #f7d866;
  --hint: #1f8f73;
  --danger: #e04f42;
  --panel: rgba(255, 255, 255, 0.84);
  --line: rgba(23, 32, 42, 0.12);
  --shadow: 0 22px 70px rgba(23, 32, 42, 0.16);
  --board-size: min(620px, calc(100vh - 190px), calc(100vw - 440px));
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  overflow-x: hidden;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background:
    radial-gradient(circle at top left, rgba(120, 144, 156, 0.28), transparent 34rem),
    linear-gradient(135deg, #f8fafb 0%, var(--bg) 48%, #e4ebef 100%);
  color: var(--ink);
}

button {
  font: inherit;
}

.app {
  width: min(1120px, calc(100vw - 24px));
  min-height: 100vh;
  margin: 0 auto;
  padding: 18px 0;
  display: grid;
  grid-template-columns: minmax(320px, calc(var(--board-size) + 54px)) minmax(250px, 320px);
  gap: 18px;
  align-items: start;
  max-width: 100%;
}

.game {
  min-width: 0;
  padding: 14px;
  border: 1px solid rgba(255, 255, 255, 0.7);
  border-radius: 18px;
  background: var(--surface);
  box-shadow: var(--shadow);
  backdrop-filter: blur(18px);
}

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

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: clamp(1.65rem, 4vw, 2.45rem);
  line-height: 1;
  letter-spacing: 0;
}

h2 {
  font-size: 1rem;
  margin-bottom: 12px;
}

#status {
  display: inline-flex;
  margin-top: 7px;
  color: var(--ink);
  background: rgba(120, 144, 156, 0.16);
  border: 1px solid rgba(120, 144, 156, 0.22);
  border-radius: 999px;
  padding: 6px 10px;
  font-size: 0.9rem;
  font-weight: 650;
}

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

#new-game,
#undo-move,
#plunder-help {
  border: 0;
  background: #17202a;
  color: white;
  border-radius: 999px;
  padding: 9px 13px;
  cursor: pointer;
  box-shadow: 0 10px 22px rgba(23, 32, 42, 0.18);
  transition: transform 150ms ease, box-shadow 150ms ease, background 150ms ease;
}

#undo-move.secondary,
#plunder-help.secondary {
  background: rgba(255, 255, 255, 0.7);
  color: var(--ink);
  border: 1px solid var(--line);
  box-shadow: none;
}

#new-game:hover,
#undo-move:hover:not(:disabled),
#plunder-help:hover:not(:disabled) {
  background: #24313d;
  box-shadow: 0 12px 28px rgba(23, 32, 42, 0.22);
  transform: translateY(-1px);
}

#undo-move.secondary:hover:not(:disabled),
#plunder-help.secondary:hover:not(:disabled) {
  color: white;
}

#undo-move:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

.board-wrap {
  width: min(100%, calc(var(--board-size) + 30px));
  margin: 0 auto;
  max-width: 100%;
}

.rank-file-layout {
  display: grid;
  grid-template-columns: 18px minmax(0, var(--board-size));
  gap: 8px;
  max-width: 100%;
}

.files {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  width: var(--board-size);
  max-width: calc(100% - 26px);
  margin-left: 26px;
  padding-top: 5px;
  color: var(--muted);
  font-weight: 700;
  font-size: 0.78rem;
  text-align: center;
}

.files-top {
  padding: 0 0 5px;
}

.ranks {
  display: grid;
  grid-template-rows: repeat(8, 1fr);
  color: var(--muted);
  font-weight: 700;
  font-size: 0.78rem;
  align-items: center;
  text-align: right;
}

.board {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  aspect-ratio: 1;
  width: 100%;
  border: 1px solid rgba(23, 32, 42, 0.16);
  border-radius: 14px;
  overflow: hidden;
  box-shadow: 0 18px 45px rgba(23, 32, 42, 0.2);
}

.square {
  position: relative;
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  padding: 0;
  border: 0;
  cursor: pointer;
  line-height: 1;
  background: var(--light);
  color: #17120f;
  transition: background 120ms ease, box-shadow 120ms ease;
}

.square:disabled {
  cursor: default;
  opacity: 1;
}

.piece {
  width: 86%;
  height: 86%;
  object-fit: contain;
  pointer-events: none;
  user-select: none;
  filter: drop-shadow(0 3px 2px rgba(23, 32, 42, 0.22));
  will-change: transform, filter;
}

.ring-badge {
  position: absolute;
  right: 8%;
  bottom: 8%;
  display: grid;
  place-items: center;
  width: 26%;
  aspect-ratio: 1;
  border: 2px solid rgba(255, 255, 255, 0.9);
  border-radius: 999px;
  background: rgba(247, 216, 102, 0.94);
  color: #17202a;
  font-size: clamp(0.55rem, 1.5vw, 0.86rem);
  font-weight: 900;
  box-shadow: 0 4px 10px rgba(23, 32, 42, 0.22);
  pointer-events: none;
}

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

.square.selected {
  background: var(--selected);
  box-shadow: inset 0 0 0 4px rgba(255, 255, 255, 0.35);
}

.square.legal::after {
  content: "";
  width: 24%;
  aspect-ratio: 1;
  border-radius: 999px;
  background: rgba(31, 143, 115, 0.72);
}

.square.capture::after {
  content: "";
  position: absolute;
  inset: 10%;
  border: 5px solid rgba(31, 143, 115, 0.72);
  border-radius: 999px;
}

.square.in-check {
  box-shadow: inset 0 0 0 6px rgba(224, 79, 66, 0.86);
}

.side {
  display: grid;
  gap: 12px;
}

.settings {
  display: grid;
  gap: 9px;
}

.settings label {
  display: grid;
  gap: 6px;
}

.settings span {
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 700;
}

.settings select,
.settings input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.76);
  color: var(--ink);
  font: inherit;
  padding: 8px 10px;
}

.settings select:disabled {
  opacity: 0.48;
  cursor: not-allowed;
}

.online-controls {
  display: grid;
  gap: 9px;
  border-top: 1px solid var(--line);
  margin-top: 2px;
  padding-top: 10px;
}

.online-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.online-actions button {
  border: 0;
  border-radius: 10px;
  background: #17202a;
  color: white;
  cursor: pointer;
  font: inherit;
  padding: 9px 10px;
}

.online-status {
  color: var(--muted);
  font-size: 0.84rem;
  line-height: 1.35;
}

.panel {
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--panel);
  padding: 12px;
  box-shadow: 0 14px 34px rgba(23, 32, 42, 0.09);
  backdrop-filter: blur(14px);
}

.captures {
  display: grid;
  gap: 8px;
}

.captures strong {
  display: block;
  margin-bottom: 6px;
  color: var(--muted);
  font-size: 0.85rem;
}

.pieces {
  min-height: 28px;
  display: flex;
  flex-wrap: wrap;
  gap: 2px;
  align-items: center;
}

.captured-piece {
  width: 24px;
  height: 24px;
  object-fit: contain;
}

.notation-table {
  display: grid;
  grid-template-columns: 38px 1fr 1fr;
  border: 1px solid var(--line);
  border-radius: 12px;
  overflow: hidden;
}

.notation-head {
  background: rgba(120, 144, 156, 0.16);
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 800;
  padding: 8px 10px;
  border-bottom: 1px solid var(--line);
}

.notation-body {
  grid-column: 1 / -1;
  max-height: min(260px, calc(100vh - 470px));
  overflow: auto;
}

.notation-row {
  display: grid;
  grid-template-columns: 38px 1fr 1fr;
  min-height: 28px;
  border-bottom: 1px solid rgba(214, 198, 173, 0.62);
}

.notation-row:last-child {
  border-bottom: 0;
}

.notation-number,
.notation-move {
  padding: 5px 9px;
  font-variant-numeric: tabular-nums;
}

.notation-number {
  color: var(--muted);
  font-weight: 700;
  background: rgba(120, 144, 156, 0.1);
}

dialog {
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--panel);
  color: var(--ink);
  padding: 18px;
  box-shadow: 0 20px 60px rgba(33, 28, 23, 0.24);
}

.guide-dialog {
  width: min(520px, calc(100vw - 32px));
  padding: 24px;
}

.guide-dialog form {
  position: relative;
}

.guide-dialog h2 {
  padding-right: 40px;
}

.close-dialog {
  position: absolute;
  top: -8px;
  right: -8px;
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: white;
  color: var(--ink);
  cursor: pointer;
  font-size: 1.35rem;
  line-height: 1;
}

.guide-copy {
  display: grid;
  gap: 12px;
  color: var(--muted);
  line-height: 1.5;
}

.guide-copy p {
  margin: 0;
}

dialog::backdrop {
  background: rgba(33, 28, 23, 0.4);
}

.promotion-options {
  display: grid;
  grid-template-columns: repeat(4, 64px);
  gap: 8px;
}

.dialog-copy {
  max-width: 280px;
  margin-bottom: 14px;
  color: var(--muted);
  line-height: 1.4;
}

.promotion-options button,
.ring-options button {
  border: 1px solid var(--line);
  border-radius: 12px;
  background: white;
  min-height: 64px;
  cursor: pointer;
}

.promotion-options img {
  width: 54px;
  height: 54px;
  pointer-events: none;
}

.ring-options {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
}

.ring-options button {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 48px;
  padding: 9px 12px;
  color: var(--ink);
  text-transform: capitalize;
}

.ring-choice {
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border-radius: 999px;
  background: var(--selected);
  font-weight: 900;
}

@media (max-width: 860px) {
  :root {
    --board-size: min(calc(100vw - 58px), 620px);
  }

  .app {
    grid-template-columns: 1fr;
    width: 100%;
    padding: 8px;
    gap: 10px;
    overflow-x: hidden;
  }

  .game,
  .panel {
    border-radius: 12px;
    padding: 10px;
  }

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

  h1 {
    font-size: clamp(1.55rem, 8vw, 2.1rem);
  }

  #status {
    max-width: 100%;
    white-space: normal;
  }

  .actions {
    justify-content: stretch;
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 6px;
  }

  #new-game,
  #undo-move,
  #plunder-help {
    width: 100%;
    padding: 8px 6px;
    font-size: 0.86rem;
    white-space: nowrap;
  }

  .board-wrap {
    width: calc(var(--board-size) + 26px);
  }

  .rank-file-layout {
    grid-template-columns: 16px minmax(0, var(--board-size));
    gap: 6px;
  }

  .files {
    width: var(--board-size);
    max-width: var(--board-size);
    margin-left: 22px;
    font-size: 0.68rem;
  }

  .ranks {
    font-size: 0.68rem;
  }

  .side {
    grid-template-columns: 1fr;
    min-width: 0;
  }

  .settings {
    gap: 8px;
  }

  .notation-table,
  .notation-row {
    grid-template-columns: 32px minmax(0, 1fr) minmax(0, 1fr);
  }

  .notation-head,
  .notation-number,
  .notation-move {
    padding-left: 7px;
    padding-right: 7px;
    min-width: 0;
    overflow-wrap: anywhere;
  }

  dialog {
    width: min(420px, calc(100vw - 24px));
    max-width: calc(100vw - 24px);
  }

  .promotion-options {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }

  .promotion-options button {
    min-width: 0;
  }

  .promotion-options img {
    width: min(54px, 18vw);
    height: min(54px, 18vw);
  }
}

@media (max-height: 760px) and (min-width: 861px) {
  :root {
    --board-size: min(560px, calc(100vh - 170px), calc(100vw - 410px));
  }

  h2 {
    margin-bottom: 8px;
  }

  .panel {
    padding: 10px;
  }

  .notation-head {
    padding: 6px 9px;
  }

  .notation-body {
    max-height: min(210px, calc(100vh - 430px));
  }
}

@media (max-width: 390px) {
  :root {
    --board-size: calc(100vw - 52px);
  }

  .actions {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  #new-game {
    grid-column: 1 / -1;
  }
}
