:root {
  --bg: #0f1a17;
  --panel: #f3efe4;
  --panel-dark: #2a3b35;
  --light: #e7d9c3;
  --dark: #6b5b4b;
  --accent: #d85f2c;
  --accent-2: #2f7a62;
  --text: #101413;
  --muted: #4d5a56;
  --shadow: rgba(0, 0, 0, 0.25);
}

body.theme-ascii {
  --light: #e7d9c3;
  --dark: #6b5b4b;
  --ascii-white: #f7f1e6;
  --ascii-black: #0f1110;
}

body.theme-ascii .board {
  border: 2px solid var(--dark);
  gap: 1px;
  background: var(--dark);
}

body.theme-ascii .square.light,
body.theme-ascii .square.dark {
  background-color: var(--light);
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: "Alegreya", "Garamond", "Georgia", serif;
  background: radial-gradient(circle at top, #244138 0%, var(--bg) 60%);
  color: var(--panel);
  min-height: 100vh;
  overflow-x: hidden;
}

.app {
  width: 100%;
  margin: 0 auto;
  padding: 24px 24px 40px;
  display: grid;
  gap: 24px;
}

.header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
}

.header h1 {
  font-size: clamp(28px, 4vw, 42px);
  letter-spacing: 1px;
}

.header p {
  color: #c9d6d0;
  margin-top: 6px;
}

.top-actions {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
}

.text-button {
  background: transparent;
  color: #e4efe9;
  border: 1px solid rgba(228, 239, 233, 0.35);
  border-radius: 999px;
  padding: 8px 14px;
  font-size: 14px;
  cursor: pointer;
}

.text-button:hover {
  background: rgba(228, 239, 233, 0.15);
}

.text-button.active {
  background: rgba(228, 239, 233, 0.28);
  border-color: rgba(228, 239, 233, 0.7);
  color: #ffffff;
}

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: center;
}

.controls label {
  display: grid;
  gap: 6px;
  font-size: 14px;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: #c9d6d0;
}

select,
button {
  font: inherit;
  padding: 10px 14px;
  border-radius: 10px;
  border: none;
}

select {
  background: var(--panel);
  color: var(--text);
}

button {
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 10px 20px var(--shadow);
}

button:hover {
  background: #e6733f;
}

.modal .controls {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
}

.modal .controls label {
  width: 100%;
}

.modal .controls button {
  width: 100%;
}

.side-panel .controls {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
}

.side-panel .controls label {
  width: 100%;
}

@media (max-width: 600px) {
  .modal .controls {
    grid-template-columns: 1fr;
  }
}


.board-wrap {
  display: grid;
  grid-template-columns: 1fr;
  gap: 24px;
  align-items: center;
  justify-items: center;
  overflow-x: hidden;
}

.board-shell {
  position: relative;
  --label-gutter: 16px;
  --board-size: min(86vw, 82vh, 720px);
  display: grid;
  grid-template-columns: var(--label-gutter) var(--board-size) var(--label-gutter);
  grid-template-rows: var(--label-gutter) var(--board-size) var(--label-gutter);
  align-items: center;
  justify-items: center;
  margin: 0 auto;
}

.board {
  grid-column: 2;
  grid-row: 2;
  width: var(--board-size);
  height: var(--board-size);
  aspect-ratio: 1 / 1;
  border-radius: 16px;
  box-shadow: 0 25px 40px var(--shadow);
  overflow: hidden;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  border: 4px solid #1f2b26;
  position: relative;
  z-index: 1;
}

.file-labels {
  grid-column: 2;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  justify-items: center;
  font-size: 12px;
  color: #c9d6d0;
  letter-spacing: 1px;
  text-transform: lowercase;
  width: var(--board-size);
  padding: 0 4px;
}

.file-labels.bottom {
  grid-row: 3;
}

.file-labels.top {
  grid-row: 1;
}

.rank-labels {
  grid-row: 2;
  display: grid;
  grid-template-rows: repeat(8, 1fr);
  justify-items: center;
  font-size: 12px;
  color: #c9d6d0;
  letter-spacing: 1px;
  height: var(--board-size);
  padding: 4px 0;
}

.rank-labels.left {
  grid-column: 1;
}

.rank-labels.right {
  grid-column: 3;
}

.file-labels span,
.rank-labels span {
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0.9;
  pointer-events: none;
  text-align: center;
}

.board-shell.coords-off .file-labels,
.board-shell.coords-off .rank-labels {
  display: none;
}

.board-shell.coords-off {
  --label-gutter: 0px;
}

.board-shell.coords-one .file-labels.top,
.board-shell.coords-one .rank-labels.right {
  display: none;
}

.side-panel {
  display: none;
  width: 100%;
  max-width: 360px;
  color: var(--text);
  gap: 16px;
  height: fit-content;
}

.side-panel.hidden {
  display: none;
}

.side-panel-box {
  display: none;
  background: var(--panel);
  padding: 16px;
  border-radius: 16px;
  box-shadow: 0 20px 35px var(--shadow);
}

.side-panel-box.active {
  display: block;
}

.side-panel-box h3 {
  margin-bottom: 10px;
  color: var(--panel-dark);
  font-size: 16px;
}

.square {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(20px, 4vw, 34px);
  font-weight: 700;
  color: #1b1f1c;
  user-select: none;
}

.square.ascii-white {
  color: var(--ascii-white);
  text-shadow:
    -1px 0 var(--ascii-black),
    1px 0 var(--ascii-black),
    0 -1px var(--ascii-black),
    0 1px var(--ascii-black);
}

.square.ascii-black {
  color: var(--ascii-black);
  text-shadow:
    -1px 0 var(--ascii-white),
    1px 0 var(--ascii-white),
    0 -1px var(--ascii-white),
    0 1px var(--ascii-white);
}

body:not(.theme-ascii) .square.ascii-white {
  color: #f7f1e6;
  text-shadow:
    -1px 0 #0f1110,
    1px 0 #0f1110,
    0 -1px #0f1110,
    0 1px #0f1110;
}

body:not(.theme-ascii) .square.ascii-black {
  color: #0f1110;
  text-shadow:
    -1px 0 #f7f1e6,
    1px 0 #f7f1e6,
    0 -1px #f7f1e6,
    0 1px #f7f1e6;
}

body.theme-ascii .square.ascii-white {
  color: #f7f1e6;
  text-shadow:
    -1px 0 #0f1110,
    1px 0 #0f1110,
    0 -1px #0f1110,
    0 1px #0f1110;
}

body.theme-ascii .square.ascii-black {
  color: #0f1110;
  text-shadow:
    -1px 0 #f7f1e6,
    1px 0 #f7f1e6,
    0 -1px #f7f1e6,
    0 1px #f7f1e6;
}


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

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

.square.selected {
  outline: 3px solid var(--accent-2);
  outline-offset: -3px;
}

.square.legal::after {
  content: "";
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: rgba(47, 122, 98, 0.4);
  position: absolute;
}

.square.capture::after {
  width: 90%;
  height: 90%;
  border-radius: 12px;
  border: 3px solid rgba(216, 95, 44, 0.8);
  background: transparent;
}

.square {
  position: relative;
}

.square.last-move {
  box-shadow: inset 0 0 0 3px rgba(216, 95, 44, 0.6);
}

.square.bot-from {
  animation: botPulse 600ms ease-in-out 0s 2;
}

.square.bot-to {
  animation: botPulse 600ms ease-in-out 0s 2;
}

@keyframes botPulse {
  0% { box-shadow: inset 0 0 0 0 rgba(47, 122, 98, 0.0); }
  50% { box-shadow: inset 0 0 0 5px rgba(47, 122, 98, 0.6); }
  100% { box-shadow: inset 0 0 0 0 rgba(47, 122, 98, 0.0); }
}

.square[data-piece] {
  background-repeat: no-repeat;
  background-position: center;
  background-size: 80%;
}

.square[data-piece="wK"] { background-image: url("pieces/wK.png"); }
.square[data-piece="wQ"] { background-image: url("pieces/wQ.png"); }
.square[data-piece="wR"] { background-image: url("pieces/wR.png"); }
.square[data-piece="wB"] { background-image: url("pieces/wB.png"); }
.square[data-piece="wN"] { background-image: url("pieces/wN.png"); }
.square[data-piece="wP"] { background-image: url("pieces/wP.png"); }
.square[data-piece="bK"] { background-image: url("pieces/bK.png"); }
.square[data-piece="bQ"] { background-image: url("pieces/bQ.png"); }
.square[data-piece="bR"] { background-image: url("pieces/bR.png"); }
.square[data-piece="bB"] { background-image: url("pieces/bB.png"); }
.square[data-piece="bN"] { background-image: url("pieces/bN.png"); }
.square[data-piece="bP"] { background-image: url("pieces/bP.png"); }

.square:not([data-piece]) {
  background-image: none;
}

.icon-button {
  width: 30px;
  height: 30px;
  border-radius: 9px;
  border: none;
  background: rgba(255, 255, 255, 0.7);
  color: #1b1f1c;
  font-size: 14px;
  cursor: pointer;
  backdrop-filter: blur(6px);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

button.ghost {
  background: transparent;
  color: var(--panel-dark);
  border: 2px solid var(--panel-dark);
  box-shadow: none;
  border-radius: 12px;
  padding: 10px 14px;
}

button.ghost:hover {
  background: rgba(16, 20, 19, 0.08);
}

button.primary {
  background: var(--accent-2);
  color: #fff;
  border: 2px solid var(--accent-2);
  border-radius: 12px;
  padding: 10px 14px;
  box-shadow: 0 8px 16px var(--shadow);
}

button.primary:hover {
  background: #3a8a70;
}

button.danger {
  background: #b43a2b;
}

button.danger:hover {
  background: #c94a3a;
}

.status {
  font-weight: 700;
  color: var(--panel-dark);
  margin-bottom: 8px;
}

.moves-grid {
  display: grid;
  grid-template-columns: 40px 1fr 1fr;
  gap: 8px;
  font-weight: 700;
  color: var(--panel-dark);
  text-transform: uppercase;
  letter-spacing: 0.6px;
  font-size: 12px;
  padding-bottom: 6px;
  border-bottom: 1px solid rgba(16, 20, 19, 0.12);
  align-items: center;
}

.moves-rows {
  display: grid;
  gap: 8px;
  max-height: 320px;
  overflow: auto;
  padding-right: 6px;
  padding-top: 8px;
  margin-bottom: 16px;
}

.moves-row {
  display: grid;
  grid-template-columns: 40px 1fr 1fr;
  gap: 8px;
  align-items: center;
}

.moves-cell {
  background: #f7f1e6;
  border-radius: 8px;
  padding: 6px 8px;
  font-size: 14px;
  color: var(--panel-dark);
  box-shadow: inset 0 0 0 1px rgba(16, 20, 19, 0.06);
}

.moves-cell.number {
  text-align: center;
  background: transparent;
  color: var(--muted);
  font-weight: 700;
}

.moves-row:nth-child(odd) .moves-cell:not(.number) {
  background: #f1eadb;
}

@media (max-width: 900px) {
  .board-shell {
    --board-size: min(78vw, 70vh);
    --label-gutter: 12px;
  }

  .header {
    position: sticky;
    top: 0;
    background: rgba(15, 26, 23, 0.92);
    padding: 12px 8px;
    border-radius: 12px;
    z-index: 6;
  }
}

@media (min-width: 900px) and (max-width: 979px) {
  .board-wrap {
    justify-items: center;
    justify-content: center;
  }
}

@media (max-width: 600px) {
  .app {
    padding: 16px 12px 28px;
  }
}

@media (min-width: 980px) {
  .board-wrap {
    grid-template-columns: 1fr;
    justify-items: center;
    justify-content: center;
  }

  .board-wrap.with-side {
    grid-template-columns: minmax(320px, 600px) minmax(260px, 340px);
    align-items: start;
    justify-items: start;
    justify-content: center;
    column-gap: 56px;
  }

  .side-panel {
    display: grid;
  }

  .side-panel.hidden {
    display: none;
  }

  #closeLogs {
    display: none;
  }

  #saveSettings {
    display: none;
  }
}

@media (max-width: 1180px) and (min-width: 980px) {
  .board-wrap.with-side {
    grid-template-columns: 1fr;
    justify-items: center;
    justify-content: center;
  }

  .side-panel {
    max-width: 620px;
  }
}
@media (min-width: 980px) {
  .board-shell {
    --board-size: min(58vw, 68vh, 600px);
  }

  .board-wrap.with-side .board-shell {
    --board-size: min(52vw, 60vh, 560px);
  }
}

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 26, 23, 0.75);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 30;
}

.overlay.show {
  display: flex;
}

.overlay-card {
  background: var(--panel);
  color: var(--text);
  padding: 24px;
  border-radius: 18px;
  max-width: 420px;
  width: 100%;
  box-shadow: 0 20px 35px var(--shadow);
  display: grid;
  gap: 12px;
  position: relative;
}

.overlay-actions {
  display: flex;
  justify-content: flex-end;
  margin-top: 6px;
}

.overlay-close {
  position: absolute;
  top: 12px;
  right: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal {
  position: fixed;
  inset: 0;
  background: rgba(15, 26, 23, 0.75);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 20;
}

.modal.show {
  display: flex;
}

.modal-card {
  background: var(--panel);
  color: var(--text);
  padding: 22px;
  border-radius: 18px;
  max-width: 520px;
  width: 100%;
  box-shadow: 0 20px 35px var(--shadow);
  display: grid;
  gap: 16px;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  border-bottom: 1px solid rgba(16, 20, 19, 0.12);
  padding-bottom: 10px;
}

.modal-section {
  display: none;
}

.modal-section.active {
  display: block;
}

.modal-actions {
  display: flex;
  gap: 12px;
  justify-content: flex-end;
  margin-top: 12px;
}

.modal h3 {
  font-size: 16px;
  margin-bottom: 10px;
  color: var(--panel-dark);
}

.modal label {
  color: #2b3431;
  font-weight: 700;
  letter-spacing: 0.2px;
}

.modal select {
  border: 1px solid rgba(16, 20, 19, 0.15);
  background: #f6efe2;
  color: var(--panel-dark);
  border-radius: 12px;
  padding: 10px 12px;
}

.side-panel select {
  border: 1px solid rgba(16, 20, 19, 0.2);
  background: #efe6d4;
  color: var(--panel-dark);
  border-radius: 12px;
  padding: 10px 12px;
}

.modal p {
  color: var(--panel-dark);
  font-size: 15px;
  line-height: 1.4;
}

.side-panel label {
  color: #2b3431;
  font-weight: 700;
  letter-spacing: 0.2px;
}

.close-logs {
  align-self: flex-end;
  margin-top: 10px;
}

.copy-moves {
  align-self: flex-end;
  margin-top: 8px;
}

.copy-fen {
  align-self: flex-end;
  margin-top: 8px;
}
