/* Chess Mate */

* { box-sizing: border-box; }

:root {
  --bg: #f4f1ea;
  --card: #ffffff;
  --ink: #2b2723;
  --muted: #8a8177;
  --accent: #2f2a26;
  --sq-light: #f0d9b5;
  --sq-dark: #b58863;
  --radius: 10px;
}

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

#errbar {
  background: #b3261e;
  color: #fff;
  padding: 8px 14px;
  font-size: 14px;
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 14px;
  padding-top: max(10px, env(safe-area-inset-top));
  background: var(--accent);
  color: #f5efe6;
}

header h1 {
  font-size: 18px;
  margin: 0;
  font-weight: 650;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

header h1 #gameTitle {
  font-weight: 400;
  opacity: .65;
  font-size: 14px;
  margin-left: 6px;
}

button {
  font: inherit;
  color: var(--ink);
  background: #fff;
  border: 1px solid #d8d2c8;
  border-radius: 8px;
  padding: 6px 12px;
  cursor: pointer;
  touch-action: manipulation;
}

button:disabled { opacity: .4; cursor: default; }
button.primary { background: #2e7d32; border-color: #2e7d32; color: #fff; }

.hbtns { display: flex; gap: 8px; }
.hbtns button { background: rgba(255,255,255,.12); border-color: rgba(255,255,255,.25); color: #f5efe6; }

main {
  display: grid;
  gap: 16px;
  padding: 12px;
  padding-bottom: max(12px, env(safe-area-inset-bottom));
  max-width: 1600px;
  margin: 0 auto;
}

/* ---------------------------------------------------------------- board */

#boardWrap {
  position: relative;
  /* single column (portrait phone / narrow window): fill the width, but
     leave room below for status + notation */
  width: min(100%, calc(100dvh - 230px));
  aspect-ratio: 1;
  margin: 0 auto;
  border-radius: 6px;
  overflow: hidden;
  box-shadow: 0 2px 14px rgba(0,0,0,.18);
  user-select: none;
  -webkit-user-select: none;
}

/* wide screens: panel on the right, board scales to the full viewport height */
@media (min-width: 820px) {
  main { grid-template-columns: minmax(0, 1fr) 340px; align-items: start; }
  #boardWrap { width: min(100%, calc(100dvh - 90px)); }
}

/* landscape phones: same side-by-side layout, tighter, board sized by height */
@media (orientation: landscape) and (max-height: 520px) {
  main { grid-template-columns: auto minmax(170px, 1fr); gap: 10px; align-items: start; }
  #boardWrap { width: calc(100dvh - 78px); margin: 0; }
  #notation { max-height: calc(100dvh - 240px); }
}

.board {
  display: grid;
  grid-template: repeat(8, 1fr) / repeat(8, 1fr);
  width: 100%;
  height: 100%;
  touch-action: none; /* board handles its own gestures (drag & drop) */
  -webkit-touch-callout: none;
}

.sq { position: relative; }
.sq.light { background: var(--sq-light); }
.sq.dark { background: var(--sq-dark); }

.sq .piece {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 2;
  pointer-events: none;
}

.sq .coord {
  position: absolute;
  font-size: clamp(8px, 1.4vw, 11px);
  font-weight: 600;
  z-index: 1;
  pointer-events: none;
}
.sq .coord.rank { top: 2px; left: 3px; }
.sq .coord.file { bottom: 1px; right: 3px; }
.sq.light .coord { color: var(--sq-dark); }
.sq.dark .coord { color: var(--sq-light); }

/* square state overlays */
.sq.last::before,
.sq.selected::before,
.sq.check::before,
.sq.danger-hanging::before,
.sq.danger-exchange::before {
  content: '';
  position: absolute;
  inset: 0;
  z-index: 1;
}
.sq.last::before { background: rgba(205, 210, 106, .55); }
.sq.selected::before { background: rgba(20, 85, 30, .45); }
.sq.check::before { background: radial-gradient(circle, rgba(255,45,45,.75) 20%, rgba(231,0,0,.35) 55%, transparent 78%); }
.sq.danger-hanging::before { background: rgba(220, 38, 38, .5); }
.sq.danger-exchange::before { background: rgba(240, 140, 0, .45); }

/* legal-move markers */
.sq.target::after,
.sq.target-capture::after {
  content: '';
  position: absolute;
  inset: 0;
  z-index: 3;
  pointer-events: none;
}
.sq.target::after {
  background: radial-gradient(circle, rgba(20, 40, 20, .28) 0 26%, transparent 27%);
}
.sq.target-capture::after {
  border: 4px solid rgba(20, 40, 20, .38);
  border-radius: 50%;
  inset: 4%;
}

/* targets where the moved piece would lose material */
.sq.target.risky::after {
  background: radial-gradient(circle, rgba(200, 32, 28, .55) 0 26%, transparent 27%);
}
.sq.target-capture.risky::after {
  border-color: rgba(200, 32, 28, .65);
}

/* drag & drop */
.drag-ghost {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 6;
  pointer-events: none;
  filter: drop-shadow(0 4px 7px rgba(0, 0, 0, .45));
}
.sq.drag-over {
  outline: 3px solid rgba(245, 240, 230, .9);
  outline-offset: -3px;
  z-index: 5;
}

/* arrows */
.arrows {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 4;
  pointer-events: none;
}
.arrow line { stroke-linecap: round; }
.arrow-hint line { stroke: #2b6fd6; } .arrow-hint polygon { fill: #2b6fd6; }
.arrow-hint { opacity: .85; }
.arrow-top1 line { stroke: #15781b; } .arrow-top1 polygon { fill: #15781b; }
.arrow-top1 { opacity: .9; }
.arrow-top2 line { stroke: #15781b; } .arrow-top2 polygon { fill: #15781b; }
.arrow-top2 { opacity: .55; }
.arrow-top3 line { stroke: #15781b; } .arrow-top3 polygon { fill: #15781b; }
.arrow-top3 { opacity: .38; }
.arrow-worst line { stroke: #d0342c; } .arrow-worst polygon { fill: #d0342c; }
.arrow-worst { opacity: .8; }
.arrow-engine line { stroke: #8e44ad; } .arrow-engine polygon { fill: #8e44ad; }
.arrow-engine { opacity: .85; }

/* ---------------------------------------------------------------- panel */

#panel {
  background: var(--card);
  border-radius: var(--radius);
  padding: 12px;
  box-shadow: 0 1px 6px rgba(0,0,0,.08);
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-width: 0;
}

#status { min-height: 24px; font-size: 15px; }
#status .chk { color: #c62828; }
#status .eval {
  background: #eee7db;
  border-radius: 6px;
  padding: 1px 7px;
  font-variant-numeric: tabular-nums;
  font-weight: 600;
}
#status .eval small { font-weight: 400; color: var(--muted); }

.spinner {
  display: inline-block;
  width: 12px;
  height: 12px;
  border: 2px solid #c9c1b4;
  border-top-color: #5a5248;
  border-radius: 50%;
  vertical-align: -2px;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

#forkNote { font-size: 13px; color: var(--muted); }
#forkNote a { color: #2b6fd6; }

#notation {
  display: grid;
  grid-template-columns: 3ch 1fr 1fr;
  gap: 1px 6px;
  overflow-y: auto;
  max-height: 40dvh;
  min-height: 90px;
  font-size: 15px;
  font-variant-numeric: tabular-nums;
  align-content: start;
}
@media (min-width: 820px) { #notation { max-height: 52dvh; } }

#notation .mvnum { color: var(--muted); }
#notation .mv { padding: 1px 6px; border-radius: 5px; cursor: pointer; }
#notation .mv:hover { background: #efe9df; }
#notation .mv.cur { background: #2f2a26; color: #f5efe6; }
#notation .result { grid-column: 2 / 4; font-weight: 700; padding: 2px 6px; }

#nav { display: flex; gap: 8px; }
#nav button { flex: 1; font-size: 15px; padding: 8px 0; }

/* ---------------------------------------------------------------- dialogs */

dialog {
  border: none;
  border-radius: 12px;
  box-shadow: 0 8px 40px rgba(0,0,0,.35);
  padding: 18px;
  max-width: min(92vw, 560px);
  width: max-content;
}
dialog::backdrop { background: rgba(30, 25, 20, .5); }
dialog h2 { margin: 0 0 12px; font-size: 17px; }
dialog menu { display: flex; gap: 8px; justify-content: flex-end; margin: 14px 0 0; padding: 0; }
dialog .hint { color: var(--muted); font-size: 13px; margin: 10px 0 0; }

#dlgSettings table { border-collapse: collapse; }
#dlgSettings th { text-align: left; font-weight: 500; padding: 5px 14px 5px 0; font-size: 14px; }
#dlgSettings td { padding: 5px 8px; text-align: center; }
#dlgSettings td.dim { opacity: .35; }
#dlgSettings tr.sep th { padding-top: 14px; color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .06em; }
#dlgSettings input[type=range] { width: 90px; vertical-align: middle; }
#dlgSettings .skill-val { display: inline-block; width: 2ch; font-variant-numeric: tabular-nums; }

.dlg-actions { display: flex; gap: 8px; margin-bottom: 12px; flex-wrap: wrap; }

#gamesList {
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-height: 50dvh;
  overflow-y: auto;
  min-width: min(84vw, 460px);
}

.game-row {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-left: calc(var(--depth, 0) * 18px);
}
.game-row .open {
  flex: 1;
  text-align: left;
  display: flex;
  flex-direction: column;
  gap: 1px;
  padding: 7px 10px;
}
.game-row.current .open { border-color: #2e7d32; box-shadow: 0 0 0 1px #2e7d32; }
.game-row .meta { font-size: 12px; color: var(--muted); }
.game-row .row-btns { display: flex; gap: 4px; }
.game-row .row-btns button { padding: 5px 8px; font-size: 12px; }
.game-row .del { color: #b3261e; }

.promo { display: flex; gap: 10px; }
.promo button { width: 64px; height: 64px; padding: 6px; }
.promo img { width: 100%; height: 100%; }
