  :root {
    --bg: #0f172a;
    --panel: #1e293b;
    --text: #f1f5f9;
    --muted: #94a3b8;
    --light-sq: #f0d9b5;
    --dark-sq: #b58863;
    --accent: #fbbf24;
    /* The vh term keeps the board (plus header, player rows and controls) inside
       short/wide windows; on phones the vmin term still wins. */
    --board: min(94vmin, 560px, calc(100vh - 236px));
  }
  * { margin: 0; padding: 0; box-sizing: border-box; }
  html, body {
    height: 100%;
    overflow: hidden;
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    touch-action: manipulation;
    -webkit-user-select: none;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
    overscroll-behavior: none;
  }
  body {
    position: fixed;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 10px;
    height: 100vh;
    height: 100dvh;
    padding: max(10px, env(safe-area-inset-top))
             max(10px, env(safe-area-inset-right))
             max(10px, env(safe-area-inset-bottom))
             max(10px, env(safe-area-inset-left));
  }
  header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: var(--board);
    gap: 10px;
  }
  header a { color: var(--muted); text-decoration: none; font-size: 14px; white-space: nowrap; }
  #status { font-size: 15px; font-weight: 600; text-align: right; min-height: 1.2em; }
  #wrap { position: relative; }
  #board {
    position: relative;
    width: var(--board);
    aspect-ratio: 1;
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    grid-template-rows: repeat(8, 1fr);
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.45);
    touch-action: none; /* board drags must reach pointermove, not the browser */
  }
  .drag-ghost {
    position: absolute;
    z-index: 4;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none;
    font-size: calc(var(--board) / 8 * 0.88);
    line-height: 1;
    transform: scale(1.15);
    filter: drop-shadow(0 6px 10px rgba(0, 0, 0, 0.5));
    will-change: left, top;
  }
  .fly {
    position: absolute;
    z-index: 4;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none;
    font-size: calc(var(--board) / 8 * 0.88);
    line-height: 1;
    transition: transform 0.18s ease;
    will-change: transform;
  }
  /* The computer's hand (js/shared/robot-hand.js): reaches down over the piece,
     closes around it, carries it, then opens again to set it down. It lives in
     #wrap rather than #board so reaching in from above isn't clipped. */
  .hand {
    position: absolute;
    z-index: 4;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.16s ease;
    will-change: transform, opacity;
  }
  .hand.in { opacity: 1; }
  .hand .held {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: calc(var(--board) / 8 * 0.88);
    line-height: 1;
    transform: scale(1.12); /* lifted off the board, like a dragged piece */
    filter: drop-shadow(0 6px 10px rgba(0, 0, 0, 0.5));
  }
  .hand .mitt {
    position: absolute;
    left: 50%;
    top: 50%;
    font-size: calc(var(--board) / 8 * 0.8);
    line-height: 1;
    transform: translate(-50%, -92%) rotate(180deg); /* reaching down from above */
    transition: transform 0.19s ease;
    filter: drop-shadow(0 3px 4px rgba(0, 0, 0, 0.5));
  }
  .hand.in .mitt { transform: translate(-50%, -62%) rotate(180deg); }
  .sq {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: calc(var(--board) / 8 * 0.88);
    line-height: 1;
    cursor: pointer;
  }
  .sq.light { background: var(--light-sq); }
  .sq.dark { background: var(--dark-sq); }
  .sq.last { box-shadow: inset 0 0 0 100vmax rgba(251, 191, 36, 0.28); }
  .sq.sel { box-shadow: inset 0 0 0 100vmax rgba(59, 130, 246, 0.4); }
  .sq.chk { box-shadow: inset 0 0 0 100vmax rgba(239, 68, 68, 0.45); }
  .sq.dot::after {
    content: "";
    position: absolute;
    width: 30%;
    height: 30%;
    border-radius: 50%;
    background: rgba(15, 23, 42, 0.35);
  }
  .sq.cap::after {
    content: "";
    position: absolute;
    inset: 6%;
    border-radius: 50%;
    border: calc(var(--board) / 8 * 0.09) solid rgba(15, 23, 42, 0.35);
  }
  .pc.w { color: #fafafa; text-shadow: 0 1px 2px rgba(0,0,0,0.9), 0 0 1px #000; }
  .pc.b { color: #1c1c1c; text-shadow: 0 1px 1px rgba(255,255,255,0.25); }
  /* Player rows: name with the running material count underneath, lichess-style */
  .player { display: flex; flex-direction: column; gap: 3px; width: var(--board); }
  .player .name { display: flex; align-items: center; gap: 5px; font-size: 14px; font-weight: 700; }
  .player .name .sub { color: var(--muted); font-weight: 600; font-size: 12px; }
  .player .material { display: flex; align-items: center; min-height: 17px; font-size: 15px; line-height: 1; }
  .player .material .tk + .tk { margin-left: -4px; } /* captures stack, so long lists still fit */
  .player .material .adv { margin-left: 6px; font-size: 12px; font-weight: 800; color: var(--accent); font-variant-numeric: tabular-nums; }
  .chip.w, .tk.w { color: #e8edf5; }
  .chip.b, .tk.b { color: #7b8798; } /* board black is near-black — too dark on the page bg */
  /* Engine evaluation bar: the filled width is the player's share of the position */
  #eval {
    width: var(--board);
    height: 16px;
    border-radius: 8px;
    background: #242a3a;
    position: relative;
    overflow: hidden;
    box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.08);
  }
  #eval-fill {
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 50%;
    transition: width 0.25s ease;
  }
  #eval-score {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 11px;
    font-weight: 800;
    color: #f1f5f9;
    text-shadow: 0 1px 3px rgba(0, 0, 0, 0.9);
    font-variant-numeric: tabular-nums;
    letter-spacing: 0.03em;
  }
  #controls { display: flex; gap: 10px; width: var(--board); }
  button {
    flex: 1;
    padding: 12px;
    border: 2px solid rgba(255, 255, 255, 0.14);
    border-radius: 12px;
    background: var(--panel);
    color: var(--text);
    font: 700 15px -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    cursor: pointer;
    touch-action: manipulation;
  }
  button:active { background: #34405f; }
  button:disabled { opacity: 0.4; }
  #overlay {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 16px;
    background: rgba(10, 12, 26, 0.88);
    -webkit-backdrop-filter: blur(5px);
    backdrop-filter: blur(5px);
    border-radius: 8px;
    padding: 20px;
    text-align: center;
    z-index: 5;
  }
  #overlay.hidden { display: none; }
  #overlay h1 { font-size: 30px; font-weight: 800; }
  #overlay .row { display: flex; gap: 10px; width: 100%; max-width: 320px; }
  #overlay .row button.on { border-color: var(--accent); background: #3a3320; }
  #overlay label { color: var(--muted); font-size: 14px; }
  #overlay label.opt { display: flex; align-items: center; gap: 8px; cursor: pointer; }
  #overlay label.opt input { width: 18px; height: 18px; accent-color: var(--accent); cursor: pointer; }
  #overlay input[type=range] { width: 100%; max-width: 320px; }
  #elo-label { font-size: 20px; font-weight: 800; color: var(--accent); }
  #ov-msg { color: var(--muted); font-size: 15px; line-height: 1.5; }
  .winbanner {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    font-size: clamp(36px, 11vmin, 72px);
    font-weight: 800;
    color: #fbbf24;
    text-shadow: 0 3px 12px rgba(0, 0, 0, 0.7);
    z-index: 6;
    pointer-events: none;
    animation: pop 0.5s cubic-bezier(0.2, 2.2, 0.4, 1);
  }
  @keyframes pop { from { transform: scale(0.2); opacity: 0; } to { transform: scale(1); opacity: 1; } }
  #promo { position: absolute; inset: 0; display: none; align-items: center; justify-content: center; gap: 10px; background: rgba(10,12,26,0.8); z-index: 6; border-radius: 8px; }
  #promo.show { display: flex; }
  #promo button { flex: 0 0 auto; width: 72px; height: 72px; font-size: 44px; line-height: 1; }
