:root {
  color-scheme: light;
  --bg: #f6f4ed; --paper: #fffef9; --ink: #222d27; --muted: #58645c;
  --line: #d5d8cd; --green: #214a38; --green-hover: #163b2b; --wash: #e5ebe0;
  --gold: #dfb46a; --bad: #a23831; --serif: "Iowan Old Style", Georgia, serif;
  --mono: ui-monospace, "SF Mono", Menlo, monospace;
  --glyph: "Apple Symbols", "Segoe UI Symbol", "DejaVu Sans", sans-serif;
  --sq-light: #eee3cb; --sq-dark: #b18c65;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--ink); font: 15px/1.5 system-ui, -apple-system, sans-serif; }
.hidden, [hidden] { display: none !important; }
button, input, select { font: inherit; }
button, summary, select { cursor: pointer; }
button { color: inherit; }
button:disabled { opacity: .55; cursor: default; }
:where(button, summary, input, select):focus-visible { outline: 3px solid #9a6a23; outline-offset: 3px; }
h1, h2, p { margin-top: 0; }
h1, h2 { font-family: var(--serif); font-weight: 500; line-height: 1.05; letter-spacing: -.045em; }
.panel-title, .auth-title { font-size: clamp(40px, 4.5vw, 68px); margin: 0 0 30px; }
.hint, .empty, .sub, .why { color: var(--muted); }
.hint { font-size: 13px; margin: 9px 0 0; }
.row, .controls, .replay-controls { display: flex; gap: 8px; align-items: center; }
.grow { flex: 1; min-width: 0; }

/* One header for the lobby, board and account screens. */
.sidebar, .masthead { display: flex; gap: 8px; align-items: center; padding: 0 max(5vw, calc((100vw - 1280px)/2)); height: 76px; border-bottom: 1px solid var(--line); }
.brand { display: flex; align-items: center; gap: 9px; color: var(--green); font: 600 28px/1 var(--serif); letter-spacing: -.05em; }
.brand-mark { color: var(--gold); background: var(--green); display: grid; place-items: center; width: 34px; height: 36px; border-radius: 6px; font: 23px/1 var(--glyph); }
.sidebar .brand { margin-right: clamp(12px, 3vw, 44px); }
.nav-item { background: none; border: 0; border-radius: 6px; padding: 9px 14px; color: var(--muted); font-weight: 600; }
.nav-item .ico { display: none; }
.nav-item:hover, .nav-item.active { background: var(--wash); color: var(--green); }
.spacer { flex: 1; }
.me { display: flex; align-items: center; gap: 8px; min-width: 0; }
.me-text { line-height: 1.1; min-width: 0; }
.me-text b, .me-text span { display: block; }
.me-text b { max-width: 16ch; overflow: hidden; text-overflow: ellipsis; }
.me-text span { margin-top: 4px; font-size: 11px; color: var(--muted); }
.pill { color: var(--green); background: var(--wash); padding: 2px 8px; font: 600 12px/1.6 var(--mono); border-radius: 4px; }
.badge { display: inline-block; margin-left: 5px; padding: 2px 5px; background: var(--green); color: white; border-radius: 5px; font: 600 11px/1.25 system-ui; }
.avatar { flex: none; display: inline-grid; place-items: center; background: #dce5d8; color: var(--green); border-radius: 50%; width: 34px; height: 34px; font-weight: 700; text-transform: uppercase; }
.avatar.sm { width: 29px; height: 29px; font-size: 13px; }
.avatar.bot { font: 23px/1 var(--glyph); }
.content { max-width: 1344px; margin: auto; padding: 36px clamp(16px, 4vw, 48px); }

/* The board and the play form share one spread on wide screens. */
.home, .auth-wrap { display: grid; grid-template-columns: minmax(290px, 390px) minmax(0, 1fr); gap: clamp(52px, 8vw, 128px); align-items: center; max-width: 1120px; margin: 24px auto; }
.auth-wrap { margin: clamp(32px, 8vh, 92px) auto; width: 88%; }
.home .panel { grid-column: 1; grid-row: 1; }
.home .preview { grid-column: 2; grid-row: 1; }
.preview { justify-self: end; width: min(100%, 555px); padding: clamp(16px, 2.5vw, 38px); background: #e4e7dc; border-radius: 3px; }
.preview .board-wrap { width: 100%; height: auto; aspect-ratio: 1; }
.preview .board { position: absolute; inset: 0; touch-action: auto; }
.preview .coord { display: none; }
.preview .board-frame { border: 1px solid #a79d84; box-shadow: 0 5px 12px #20291918; }
.auth-title { font-size: clamp(54px, 5.2vw, 76px); text-wrap: balance; }
#account-options > summary { display: none; }
.has-invite #account-options > summary { display: list-item; padding: 8px 0; color: var(--green); font-weight: 600; }
.has-invite .auth-title { display: none; }
#invite-title { font-size: clamp(32px, 3.5vw, 48px); overflow-wrap: anywhere; margin-bottom: 14px; }
#invite-card { border-bottom: 1px solid var(--line); padding-bottom: 22px; margin-bottom: 22px; }
#invite-details { color: var(--muted); }
.narrow-card, .claim-card { max-width: 620px; margin: 24px auto; }
.claim-card { background: var(--wash); border-radius: 8px; padding: 12px 18px; }
.claim-card form { margin-top: 12px; }
.claim-card summary { font-weight: 600; }
.form-error, #claim-error { color: var(--bad); font-size: 13px; }
.form-error:not(:empty) { margin-bottom: 10px; }
#claim-error:empty, .hint:empty { display: none; }

.btn, .chip { background: transparent; border: 1px solid #bdc4b9; border-radius: 7px; padding: 10px 14px; font-weight: 600; line-height: 1.4; }
.btn:hover, .chip:hover { background: var(--wash); }
.btn.primary { background: var(--green); color: #fffef9; border-color: var(--green); }
.btn.primary:hover { background: var(--green-hover); }
.btn.big { width: 100%; min-height: 49px; }
.btn.small { padding: 8px 10px; font-size: 13px; }
.btn.ghost { border-color: transparent; }
.btn.danger { color: var(--bad); }
.btn.danger.armed { background: var(--bad); color: white; }
.link { border: 0; background: none; color: var(--green); font: inherit; text-decoration: underline; text-underline-offset: 3px; }
input, select { display: block; color: var(--ink); background: var(--paper); width: 100%; min-width: 0; padding: 11px 12px; border: 1px solid #bdc4b9; border-radius: 6px; font-size: 16px; }
label, .field-label { color: var(--muted); font-size: 13px; font-weight: 600; }
label { display: block; margin-bottom: 15px; }
label input { margin-top: 5px; }
#mode-bot label { margin: 0 0 5px; }
#bot-description { margin: 7px 0 17px; }
.seg { display: flex; gap: 5px; border-bottom: 1px solid var(--line); margin-bottom: 18px; }
.seg button { flex: 1; border: 0; border-bottom: 2px solid transparent; background: transparent; color: var(--muted); padding: 10px 6px; font-weight: 600; }
.seg button:hover { color: var(--green); }
.seg button.active { border-bottom-color: var(--green); color: var(--green); }
#settings { margin-bottom: 16px; }
.disclosure { border-bottom: 1px solid var(--line); }
.disclosure summary { display: flex; align-items: center; justify-content: space-between; gap: 8px; min-height: 45px; padding: 8px 0; font-size: 14px; font-weight: 550; }
.disclosure summary::-webkit-details-marker { display: none; }
.disclosure summary::after { content: ''; margin-right: 4px; width: 6px; height: 6px; border: solid var(--muted); border-width: 0 1.5px 1.5px 0; transform: rotate(45deg); }
.disclosure[open] summary::after { transform: rotate(225deg); }
.disclosure form, .field { margin: 4px 0 14px; }
.field-label { margin-bottom: 5px; }
.chips { display: flex; flex-wrap: wrap; gap: 5px; }
.chip { min-width: 58px; padding: 6px 9px; font-size: 13px; }
.chip.active { color: var(--green); background: var(--wash); border-color: var(--green); }
#create-box, #waiting-box { margin-bottom: 18px; }
.code-display { font: 600 36px/1.3 var(--mono); letter-spacing: .13em; }
#waiting-box .row { flex-wrap: wrap; margin-top: 12px; }
.code-input { text-transform: uppercase; font-family: var(--mono); letter-spacing: .1em; }
.guest-session .account-only, .guest-session [data-page="friends"] { display: none; }
.list, .moves { list-style: none; padding: 0; margin: 0; }
.list:not(:empty) { margin: 14px 0; }
.list li, .request { display: flex; align-items: center; gap: 10px; padding: 12px 0; border-bottom: 1px solid var(--line); }
.who { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.who b { display: block; }
.list .sub { font-size: 13px; display: flex; align-items: center; gap: 6px; }
.elo { color: var(--muted); font-size: 12px; }
.empty { font-size: 14px; padding: 12px 0; }
.dot { width: 7px; height: 7px; border-radius: 50%; background: #9b9d98; }
.dot.on { background: #317547; }
.banner, .offer { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; padding: 12px 16px; background: var(--wash); border-radius: 7px; margin-bottom: 12px; }

.board-frame { position: relative; overflow: hidden; border-radius: 3px; background: var(--sq-light); }
.board-wrap { --sq: calc(var(--board) / 8); position: relative; width: var(--board); height: var(--board); }
.board {
  display: grid; grid-template-columns: repeat(8, 1fr); grid-template-rows: repeat(8, 1fr);
  width: 100%; height: 100%; border-radius: 5px; overflow: hidden; touch-action: none; user-select: none; -webkit-user-select: none;
}
.sq { position: relative; display: flex; align-items: center; justify-content: center; }
.sq.light { background: var(--sq-light); }
.sq.dark { background: var(--sq-dark); }
.sq.last::before { content: ""; position: absolute; inset: 0; background: rgba(245, 213, 71, .5); }
.sq.selected::before { content: ""; position: absolute; inset: 0; background: rgba(245, 213, 71, .85); }
.sq.check::before { content: ""; position: absolute; inset: 0; background: radial-gradient(circle, #ff3b30 0%, rgba(255, 59, 48, .7) 45%, transparent 76%); }
.sq.mine { cursor: grab; }
.sq.target { cursor: pointer; }
.sq.target::after { content: ""; position: absolute; width: 30%; height: 30%; border-radius: 50%; background: rgba(20, 16, 12, .32); pointer-events: none; }
.sq.target.castle::after, .sq.target.capture::after { width: 88%; height: 88%; background: transparent; border: calc(var(--sq) * .085) solid rgba(20, 16, 12, .3); }
.sq.target:hover::after { background: rgba(20, 16, 12, .5); }
.sq.target.capture:hover::after { background: rgba(20, 16, 12, .18); }
.coord { position: absolute; font-size: calc(var(--sq) * .17); font-weight: 700; pointer-events: none; }
.coord.rank { top: 3%; left: 5%; }
.coord.file { bottom: 2%; right: 6%; }
.sq.light .coord { color: var(--sq-dark); }
.sq.dark .coord { color: var(--sq-light); }
.piece { position: relative; z-index: 1; font: calc(var(--sq) * .82)/1 var(--glyph); pointer-events: none; }
.piece.w { color: #fdfcf8; text-shadow: 0 0 2px #000, 1px 0 0 #1a1a1a, -1px 0 0 #1a1a1a, 0 1px 0 #1a1a1a, 0 -1px 0 #1a1a1a, 0 3px 4px rgba(0, 0, 0, .45); }
.piece.b { color: #1a1b1f; text-shadow: 0 0 1px rgba(255, 255, 255, .55), 0 3px 4px rgba(0, 0, 0, .3); }
.piece.dragging { opacity: .25; }
.piece.moved { animation: land .16s ease-out; }
@keyframes land { from { transform: scale(1.18); } to { transform: none; } }
.drag-ghost { position: fixed; z-index: 50; transform: translate(-50%, -50%); }

/* Vector pieces use the same silhouettes on every screen. */
svg.piece { width: 93%; height: 93%; stroke-width: 2.2; stroke-linejoin: round; stroke-linecap: round; }
svg.piece.w { fill: #fff8e6; stroke: #443426; --piece-detail: #443426; }
body[data-player-color="w"] svg.piece.w[data-piece-type="p"] { fill: #e5aa53; stroke: #50351c; }
svg.piece.b { fill: #323435; stroke: #16191b; --piece-detail: #f4ddaa; filter: drop-shadow(0 1px 0 #dbbc93); }
.sq.premove::before { content: ''; position: absolute; inset: 0; background: rgba(92, 143, 224, .62); }

/* The live board gets the available height. Controls occupy one adjacent column. */
#view-game, #view-replay { --board: min(calc(100vw - 450px), max(320px, calc(100dvh - 244px)), 728px); }
.game-layout { display: grid; grid-template-columns: var(--board) minmax(260px, 330px); gap: clamp(24px, 3.5vw, 54px); justify-content: center; align-items: start; }
.board-col, .side, .replay-side { min-width: 0; }
.player-bar { height: 50px; display: flex; align-items: center; gap: 9px; }
.pwho { min-width: 0; }
.pname { font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pname .elo { margin-left: 4px; }
.psub { font-size: 11px; color: var(--muted); }
.player-bar .captured { flex: 1; display: flex; align-items: center; overflow: hidden; padding-right: 4px; }
.captured svg.piece { flex: 0 0 16px; width: 16px; height: 20px; margin-right: -5px; }
.captured b { margin-left: 8px; font-size: 12px; }
.clock { flex: none; min-width: 78px; border-radius: 5px; text-align: center; font: 600 22px/1.45 var(--mono); color: var(--muted); background: #e4e4da; padding: 1px 8px; }
.clock.running { color: white; background: var(--green); }
.clock.low { color: white; background: var(--bad); }
.side, .replay-side { padding-top: 46px; }
.status { display: flex; align-items: center; gap: 8px; font: 500 25px/1.25 var(--serif); overflow-wrap: anywhere; margin-bottom: 14px; }
.status::before { content: ''; border-radius: 50%; width: 7px; height: 7px; flex: none; background: #949b91; }
.status.mine::before { background: #358450; }
.game-tabs { margin-bottom: 8px; }
.side:not(.chat-open) .chat, .side.chat-open #moves { display: none; }
.moves, .chat { height: clamp(130px, 27vh, 300px); margin: 0 0 12px; }
.moves { overflow: auto; font: 14px/1.5 var(--mono); }
.moves li { display: grid; grid-template-columns: 3ch 1fr 1fr; gap: 6px; padding: 5px 8px; }
.moves li:nth-child(even) { background: #ebece3; }
.moves li.empty { display: block; font: 14px/1.5 system-ui; }
.moves .num { color: var(--muted); }
.moves .latest { font-weight: 700; color: var(--green); }
.chat { display: flex; flex-direction: column; gap: 8px; }
.chat-log { overflow: auto; flex: 1; min-height: 0; }
.chat-log .msg { margin: 4px 0; overflow-wrap: anywhere; }
.chat-log b { margin-right: 6px; }
.controls { flex-wrap: wrap; border-top: 1px solid var(--line); padding-top: 12px; }
.controls .btn { flex: 1; padding: 9px 8px; font-size: 13px; }
.premove-settings { margin-top: 12px; display: flex; align-items: center; gap: 8px; }
.premove-settings label { margin: 0; display: flex; align-items: center; gap: 6px; }
.premove-settings input { width: 16px; height: 16px; margin: 0; accent-color: var(--green); }
.replay-side .panel-title { font-size: 36px; margin-bottom: 12px; }
.replay-controls { justify-content: space-between; margin-bottom: 12px; }
.replay-controls output { font: 13px var(--mono); }
.notation { border: 0; background: transparent; color: inherit; font: inherit; text-align: left; padding: 2px 5px; }
.notation:hover, .notation.latest { background: var(--wash); }
#analysis:empty { display: none; }
.promo, .result { position: absolute; inset: 0; z-index: 5; display: flex; align-items: center; justify-content: center; gap: 6px; background: #1f29236e; }
.promo button { width: 18%; aspect-ratio: 1; background: var(--paper); border: 1px solid var(--green); border-radius: 6px; }
.result-card { width: min(90%, 345px); text-align: center; padding: 20px; background: var(--paper); border-radius: 12px; box-shadow: 0 5px 30px #14281a26; }
.emblem { font: 32px var(--glyph); color: var(--green); }
.result-card h2 { margin: 4px 0 8px; font-size: 34px; }
.rating-line { margin: 14px 0; padding: 9px; background: var(--wash); border-radius: 5px; font-size: 14px; }
.rating-line b { font: 600 18px var(--mono); margin: 0 4px; }
.result-card .link { margin-top: 10px; }
.delta.up { color: var(--green); }
.delta.down { color: var(--bad); }
#toasts { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); display: grid; gap: 5px; z-index: 100; width: max-content; max-width: 92vw; }
.toast { background: var(--ink); color: white; padding: 10px 16px; border-radius: 6px; }
.toast.error { background: var(--bad); }

@media (max-width: 800px) {
  .content { padding: 18px 14px 32px; }
  .sidebar, .masthead { padding: 0 16px; height: 60px; }
  .sidebar .brand { margin-right: 4px; }
  .me-text { display: none; }
  .home, .auth-wrap { display: block; width: min(100%, 430px); margin: 32px auto; }
  .auth-wrap { width: min(calc(100% - 36px), 430px); }
  .preview { display: none; }
  .auth-title { max-width: none; font-size: clamp(43px, 9vw, 62px); }
  .panel-title { margin-bottom: 24px; }
  #view-game, #view-replay { --board: min(calc(100vw - 28px), max(254px, calc(100dvh - 218px)), 630px); }
  .game-layout { grid-template-columns: var(--board); gap: 15px; }
  .side, .replay-side { padding-top: 0; }
  .moves, .chat { height: 140px; }
  .narrow-card { margin-top: 26px; }
}
@media (max-width: 600px) {
  .sidebar { padding: 0 8px; gap: 1px; }
  .sidebar .brand-text, .sidebar .me .avatar, .sidebar .quiet .lbl { display: none; }
  .sidebar .quiet .ico { display: inline; }
  .sidebar .brand-mark { width: 29px; height: 32px; }
  .nav-item { padding: 11px 8px; font-size: 13px; }
  .sidebar .me { gap: 0; }
  .pill { padding: 3px 5px; }
  .status { font-size: 21px; margin-bottom: 6px; }
  .result-card { padding: 15px; }
  .player-bar { height: 43px; }
}
@media (prefers-reduced-motion: reduce) {
  .piece.moved { animation: none; }
}

#admin-link { text-decoration: none; }
