:root {
  --bg: #2b1d14;
  --bg-2: #3b2819;
  --text: #f3e6cf;
  --muted: #c9b08a;
  --gold: #d4a64a;
  --gold-soft: #e8c77a;
  --light: #f0d9b5;
  --dark: #b58863;
  --frame: #5a3a22;
  --frame-edge: #3a2414;
  --paper: #f6ecd8;
  --ink: #3a2616;
  --sel: rgba(255, 214, 90, 0.75);
  --last: rgba(205, 210, 106, 0.55);
  --move-dot: rgba(40, 25, 10, 0.3);
  --check: #d64533;
  --shadow: 0 14px 40px rgba(0, 0, 0, 0.55);
  --serif: "Cormorant Garamond", "Georgia", "Times New Roman", serif;
  --ui: "Inter", "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  --ink-strong: #2b1707;
  --ink-mid: #4a2f18;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  height: 100vh;
  overflow: hidden;
  color: var(--text);
  font-family: var(--serif);
  background:
    radial-gradient(900px 600px at 50% 0%, rgba(212, 166, 74, 0.12), transparent 70%),
    repeating-linear-gradient(90deg, rgba(0, 0, 0, 0.05) 0 2px, transparent 2px 7px),
    linear-gradient(180deg, var(--bg-2), var(--bg));
  display: flex;
  justify-content: center;
  padding: 16px;
}

.app {
  width: 100%;
  max-width: 1000px;
  height: 100%;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

/* ---------- Header ---------- */
.topbar {
  text-align: center;
  margin-bottom: 12px;
  flex: 0 0 auto;
}
.topbar h1 {
  margin: 0;
  font-size: clamp(28px, 5vh, 42px);
  font-weight: 700;
  letter-spacing: 8px;
  text-transform: uppercase;
  color: var(--gold-soft);
  text-shadow: 0 2px 0 #1a0f08, 0 0 24px rgba(212, 166, 74, 0.25);
}
.tagline {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 15px;
  font-style: italic;
  letter-spacing: 1px;
}
.tagline::before,
.tagline::after {
  content: "\2766";
  color: var(--gold);
  margin: 0 10px;
  font-style: normal;
}

.layout {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 290px;
  gap: 24px;
  align-items: center;
}

/* ---------- Board ---------- */
.board-area {
  display: flex;
  flex-direction: column;
  gap: 8px;
  height: 100%;
  min-height: 0;
  justify-content: center;
  align-items: center;
  width: 100%;
  justify-self: center;
}

.board-wrap {
  position: relative;
  aspect-ratio: 1 / 1;
  /* fit the square to whichever is smaller: the column width or the available height.
     The subtracted amount reserves room for the header + padding + captured-piece trays. */
  width: min(100%, calc(100vh - 178px));
  flex: 0 0 auto;
  margin: auto;
  padding: 14px;
  border-radius: 6px;
  background:
    repeating-linear-gradient(100deg, rgba(255, 255, 255, 0.04) 0 3px, transparent 3px 11px),
    linear-gradient(145deg, #74492a, var(--frame) 45%, #452b18);
  box-shadow:
    var(--shadow),
    inset 0 0 0 2px var(--frame-edge),
    inset 0 0 0 5px rgba(212, 166, 74, 0.35);
}

.board {
  position: absolute;
  inset: 14px;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  border: 2px solid var(--frame-edge);
  box-shadow: 0 0 0 1px rgba(212, 166, 74, 0.5);
}

.square {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  user-select: none;
}
.square.light {
  background: var(--light);
}
.square.dark {
  background: var(--dark);
}
/* subtle wood grain */
.square::before {
  content: "";
  position: absolute;
  inset: 0;
  background: repeating-linear-gradient(80deg, rgba(0, 0, 0, 0.025) 0 2px, transparent 2px 6px);
  pointer-events: none;
}

.square .piece {
  line-height: 1;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(30px, 6.4vw, 60px);
  font-family: "Segoe UI Symbol", "DejaVu Sans", "Noto Sans Symbols 2", serif;
  font-variant-emoji: text;
  z-index: 2;
  pointer-events: none;
  transition: transform 0.12s ease;
}
/* paint-order draws the outline behind the fill, so fine details stay visible */
.piece.white {
  color: #fffaf0;
  -webkit-text-stroke: 0.07em #2a1a0e;
  paint-order: stroke fill;
  text-shadow: 0 0.05em 0.06em rgba(0, 0, 0, 0.35);
}
.piece.black {
  color: #1e140c;
  -webkit-text-stroke: 0.04em #f3e2c0;
  paint-order: stroke fill;
  text-shadow: 0 0.05em 0.06em rgba(0, 0, 0, 0.4);
}
.square:hover .piece {
  transform: translateY(-2px) scale(1.04);
}

.square.selected {
  box-shadow: inset 0 0 0 4px var(--sel);
}
.square.selected .piece {
  transform: translateY(-3px) scale(1.07);
}
.square.last-move::after {
  content: "";
  position: absolute;
  inset: 0;
  background: var(--last);
  z-index: 1;
  pointer-events: none;
}
.square.in-check {
  background: radial-gradient(circle, #ff6a55 0%, var(--check) 45%, rgba(214, 69, 51, 0.25) 80%) !important;
}

.square .hint {
  position: absolute;
  width: 28%;
  height: 28%;
  border-radius: 50%;
  background: var(--move-dot);
  z-index: 1;
  pointer-events: none;
}
.square .hint.capture {
  width: 94%;
  height: 94%;
  background: transparent;
  border: 5px solid var(--move-dot);
}

.coord {
  position: absolute;
  font-family: var(--serif);
  font-size: 13px;
  font-weight: 700;
  z-index: 3;
  pointer-events: none;
}
.square.light .coord {
  color: var(--dark);
}
.square.dark .coord {
  color: var(--light);
}
.coord.file {
  right: 4px;
  bottom: 1px;
}
.coord.rank {
  left: 4px;
  top: 2px;
}

/* ---------- Captured pieces ---------- */
.captured {
  height: 30px;
  flex: 0 0 auto;
  width: 100%;
  max-width: min(100%, calc(100vh - 178px));
  display: flex;
  align-items: center;
  gap: 1px;
  font-size: 22px;
  padding: 0 10px;
  color: var(--paper);
  font-family: "Segoe UI Symbol", "DejaVu Sans", serif;
  font-variant-emoji: text;
  flex-wrap: nowrap;
  overflow: hidden;
  background: rgba(0, 0, 0, 0.18);
  border: 1px solid rgba(212, 166, 74, 0.2);
  border-radius: 4px;
}
.captured:empty {
  background: none;
  border-color: transparent;
}
.captured .cap.white {
  color: #fffaf0;
  -webkit-text-stroke: 0.07em #2a1a0e;
  paint-order: stroke fill;
}
.captured .cap.black {
  color: #1e140c;
  -webkit-text-stroke: 0.05em #d9c29a;
  paint-order: stroke fill;
}
.captured .adv {
  margin-left: 8px;
  font-family: var(--serif);
  font-size: 16px;
  font-weight: 700;
  color: var(--gold-soft);
}

/* ---------- Promotion overlay ---------- */
.promotion {
  position: absolute;
  inset: 0;
  background: rgba(20, 12, 6, 0.65);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}
.promotion[hidden] {
  display: none;
}
.promotion-box {
  display: flex;
  gap: 10px;
  background: var(--paper);
  padding: 16px;
  border-radius: 6px;
  border: 3px double var(--frame);
  box-shadow: var(--shadow);
}
.promotion-box button {
  width: 70px;
  height: 70px;
  font-size: 48px;
  font-family: "Segoe UI Symbol", "DejaVu Sans", serif;
  font-variant-emoji: text;
  border: 1px solid var(--dark);
  background: var(--light);
  border-radius: 4px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.1s ease, background 0.1s ease;
}
.promotion-box button.white {
  color: #fffaf0;
  -webkit-text-stroke: 0.07em #2a1a0e;
  paint-order: stroke fill;
}
.promotion-box button.black {
  color: #1e140c;
}
.promotion-box button:hover {
  transform: translateY(-3px);
  background: var(--gold-soft);
}

/* ---------- Side panel (parchment) ---------- */
.panel {
  background:
    radial-gradient(circle at 20% 10%, rgba(255, 255, 255, 0.5), transparent 60%),
    linear-gradient(180deg, #f8efdc, #eadbbd);
  color: var(--ink);
  border-radius: 6px;
  padding: 18px;
  border: 1px solid #c9ad7f;
  box-shadow: var(--shadow), inset 0 0 0 6px rgba(255, 255, 255, 0.25), inset 0 0 0 7px rgba(90, 58, 34, 0.25);
  max-height: 100%;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.status {
  font-family: var(--serif);
  font-size: 23px;
  font-weight: 700;
  padding: 10px 12px;
  text-align: center;
  margin-bottom: 18px;
  color: var(--ink-strong);
  border-top: 2px solid var(--frame);
  border-bottom: 2px solid var(--frame);
  letter-spacing: 0.5px;
}
.status.check {
  color: #a8321f;
}
.status.over {
  color: #8f1f12;
}
.status.thinking {
  color: #6a4820;
  font-style: italic;
}

.controls {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 18px;
}
.field {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-family: var(--ui);
  font-size: 14px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--ink-mid);
}
.field select {
  font-family: var(--ui);
  font-size: 14px;
  font-weight: 700;
  background: #fffaf0;
  color: var(--ink-strong);
  border: 1.5px solid #8a6440;
  border-radius: 4px;
  padding: 6px 10px;
  cursor: pointer;
  text-transform: none;
  letter-spacing: 0;
}
.field select:hover {
  border-color: #5a3a22;
}

.engine-card {
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 10px 13px;
  margin: 0 0 16px;
  border-radius: 5px;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.45), rgba(90, 58, 34, 0.06));
  border: 1px solid #c9ad7f;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.5);
}
.engine-dot {
  flex: 0 0 auto;
  width: 11px;
  height: 11px;
  border-radius: 50%;
  background: #b0a58c;
  box-shadow: 0 0 0 3px rgba(176, 165, 140, 0.25);
}
.engine-card.ok .engine-dot {
  background: #4a8f3c;
  box-shadow: 0 0 0 3px rgba(74, 143, 60, 0.25);
}
.engine-card.warn .engine-dot {
  background: #c47a1f;
  box-shadow: 0 0 0 3px rgba(196, 122, 31, 0.25);
}
.engine-info {
  min-width: 0;
  line-height: 1.2;
}
.engine-name {
  font-family: var(--ui);
  font-size: 16px;
  font-weight: 800;
  letter-spacing: 0.2px;
  color: var(--ink-strong);
}
.engine-sub {
  font-family: var(--ui);
  font-size: 12px;
  font-weight: 600;
  color: var(--ink-mid);
  text-transform: uppercase;
  letter-spacing: 0.6px;
}

/* ---------- Online panel ---------- */
.online-panel {
  margin: 0 0 16px;
  padding: 14px;
  border-radius: 5px;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.4), rgba(90, 58, 34, 0.06));
  border: 1px solid #c9ad7f;
}
.online-panel[hidden] {
  display: none;
}
.online-lobby,
.online-live {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.online-lobby[hidden],
.online-live[hidden] {
  display: none;
}
.online-name,
.online-code {
  font-family: var(--ui);
  font-size: 15px;
  font-weight: 600;
  padding: 10px 12px;
  border: 1.5px solid #8a6440;
  border-radius: 4px;
  background: #fffaf0;
  color: var(--ink-strong);
  width: 100%;
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.online-name::placeholder,
.online-code::placeholder {
  color: #9a7a52;
  font-weight: 500;
}
.online-name:focus,
.online-code:focus {
  border-color: #3a2414;
  box-shadow: 0 0 0 3px rgba(212, 166, 74, 0.35);
}
.online-code {
  text-transform: uppercase;
  letter-spacing: 6px;
  font-weight: 800;
  font-size: 18px;
  text-align: center;
}
.online-or {
  font-family: var(--ui);
  text-align: center;
  color: var(--ink-mid);
  font-size: 11px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 2px;
  position: relative;
  margin: 4px 0;
}
.online-or::before,
.online-or::after {
  content: "";
  position: absolute;
  top: 50%;
  width: 28%;
  height: 1.5px;
  background: rgba(90, 58, 34, 0.3);
}
.online-or::before { left: 0; }
.online-or::after { right: 0; }
.online-join {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  align-items: center;
}
.online-room {
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: center;
  flex-wrap: wrap;
}
.online-label {
  font-family: var(--ui);
  font-size: 11px;
  font-weight: 800;
  color: var(--ink-mid);
  text-transform: uppercase;
  letter-spacing: 1.5px;
}
.online-roomcode {
  font-family: var(--ui);
  font-size: 22px;
  font-weight: 800;
  letter-spacing: 5px;
  color: var(--ink-strong);
  background: #fffaf0;
  border: 2px dashed #8a6440;
  border-radius: 5px;
  padding: 4px 14px;
}
.online-copy {
  font-size: 12px;
  padding: 6px 12px;
}
.online-players {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 4px 0;
}
.online-players .pl {
  display: flex;
  align-items: center;
  gap: 10px;
  font-family: var(--ui);
  font-size: 15px;
  font-weight: 700;
  color: var(--ink-strong);
}
.online-players .pl em {
  color: var(--ink-mid);
  font-size: 12px;
  font-weight: 600;
  font-style: normal;
  text-transform: uppercase;
  letter-spacing: 1px;
}
.online-players .dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 1.5px solid var(--ink-strong);
  flex: 0 0 auto;
}
.online-players .dot.w { background: #fffaf0; }
.online-players .dot.b { background: #1e140c; }
.online-msg {
  font-family: var(--ui);
  font-size: 13px;
  font-weight: 600;
  color: var(--ink-mid);
  text-align: center;
  min-height: 1.2em;
}
.online-error {
  font-family: var(--ui);
  color: #a8321f;
  font-weight: 700;
  font-size: 13px;
}
.online-error:empty {
  display: none;
}
.online-resign {
  color: #8f1f12;
  border-color: #a8321f;
}
.online-resign:hover {
  border-color: #6a1409;
  background: linear-gradient(180deg, #fff0ec, #f0d5cd);
}

.buttons {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 16px;
  flex: 0 0 auto;
}
.btn {
  font-family: var(--ui);
  font-size: 14px;
  font-weight: 700;
  padding: 10px 12px;
  border-radius: 4px;
  border: 1.5px solid #8a6440;
  background: linear-gradient(180deg, #fffaf0, #eadcc0);
  color: var(--ink-strong);
  cursor: pointer;
  letter-spacing: 0.3px;
  transition: filter 0.15s ease, transform 0.05s ease, border-color 0.15s ease;
}
.btn:hover {
  filter: brightness(1.05);
  border-color: #5a3a22;
}
.btn:active {
  transform: translateY(1px);
}
.btn-primary {
  grid-column: 1 / -1;
  font-size: 15px;
  letter-spacing: 2px;
  text-transform: uppercase;
  color: #fff3d6;
  border-color: #3a2414;
  background: linear-gradient(180deg, #7a4d2b, #4f311b);
  text-shadow: 0 1px 0 rgba(0,0,0,0.4);
}
.btn-primary:hover {
  border-color: #1a0f08;
  background: linear-gradient(180deg, #8a5732, #5a3820);
}
#sound {
  grid-column: 1 / -1;
}
#sound[aria-pressed="false"] {
  color: #9a5a1f;
  font-style: italic;
}

.history-wrap {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
}
.history-wrap h2 {
  font-family: var(--ui);
  font-size: 12px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 3px;
  color: var(--ink-mid);
  margin: 0 0 8px;
  text-align: center;
  flex: 0 0 auto;
}
.history-wrap h2::before,
.history-wrap h2::after {
  content: "\2014";
  margin: 0 8px;
  color: #b08a5a;
}
.history {
  list-style: none;
  margin: 0;
  padding: 0;
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  font-family: var(--ui);
  font-size: 15px;
  font-weight: 700;
  color: var(--ink-strong);
  font-variant-numeric: tabular-nums;
}
.history li {
  display: grid;
  grid-template-columns: 36px 1fr 1fr;
  gap: 6px;
  padding: 4px 8px;
  border-bottom: 1px dotted rgba(90, 58, 34, 0.25);
}
.history .num {
  color: var(--ink-mid);
  font-weight: 600;
}
.history .ply {
  font-weight: 700;
}
.history:empty::after {
  content: "No moves yet";
  display: block;
  text-align: center;
  font-family: var(--ui);
  font-weight: 600;
  font-style: normal;
  color: var(--ink-mid);
  padding: 10px 0;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 1px;
}

@media (max-width: 800px) {
  body {
    height: auto;
    min-height: 100vh;
    overflow: auto;
  }
  .app {
    height: auto;
  }
  .layout {
    grid-template-columns: 1fr;
    align-items: start;
  }
  .board-area {
    height: auto;
  }
  .board-wrap {
    /* on stacked/mobile layout, size by width only */
    width: min(100%, calc(100vh - 140px));
  }
  .panel {
    max-height: none;
  }
  .history {
    max-height: 240px;
    flex: 0 0 auto;
  }
  .topbar h1 {
    font-size: 34px;
    letter-spacing: 6px;
  }
}
