:root {
  --ink: #10161b;
  --muted: #6f756f;
  --paper: #f4f1e8;
  --paper-deep: #e9e2d2;
  --line: #d6cbb5;
  --gold: #bc852d;
  --gold-light: #e6c573;
  --navy: #182630;
  --navy-soft: #27414f;
  --square-light: #f2dfb6;
  --square-dark: #a56d42;
  --shadow: 0 20px 48px rgb(21 30 36 / 16%);
}

* { box-sizing: border-box; }
html { min-width: 320px; background: var(--paper); }
body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background: radial-gradient(circle at 10% 0%, #fffdf7 0 12%, transparent 40%), var(--paper);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button, select { font: inherit; }
button { cursor: pointer; }
button:focus-visible, select:focus-visible { outline: 3px solid var(--gold); outline-offset: 3px; }

.app-shell { width: min(1180px, calc(100% - 40px)); margin: 0 auto; padding: 26px 0 38px; }
.masthead { display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 28px; }
.brand { display: inline-flex; align-items: center; gap: 11px; color: var(--navy); text-decoration: none; font-family: Georgia, "Times New Roman", serif; font-size: 1.54rem; letter-spacing: -.04em; }
.brand strong { font-weight: 700; }
.brand em { color: var(--gold); font-style: normal; font-weight: 700; }
.brand-mark { display: grid; place-items: center; width: 42px; height: 42px; border-radius: 50%; background: var(--navy); color: var(--gold-light); font-size: 1.7rem; box-shadow: 0 7px 16px rgb(20 37 47 / 24%); }
.engine-badge { display: flex; align-items: center; gap: 8px; margin: 0; color: var(--muted); font-size: .84rem; font-weight: 650; }
.status-dot { width: 9px; height: 9px; border-radius: 50%; background: #b2aaa0; }
.status-dot.is-ready { background: #4c936c; box-shadow: 0 0 0 4px #4c936c22; }
.status-dot.is-thinking { background: var(--gold); animation: pulse 1.1s infinite; }

.game-layout { display: grid; grid-template-columns: minmax(330px, 670px) minmax(260px, 360px); align-items: start; justify-content: center; gap: clamp(24px, 4vw, 54px); }
.board-frame { position: relative; width: min(100%, 670px); padding: 0 0 21px 21px; filter: drop-shadow(0 18px 28px rgb(39 32 21 / 15%)); }
.board { display: grid; grid-template-columns: repeat(8, minmax(0, 1fr)); grid-template-rows: repeat(8, minmax(0, 1fr)); aspect-ratio: 1; border: 7px solid var(--navy); border-radius: 4px; overflow: hidden; background: var(--navy); }
.square { position: relative; display: grid; place-items: center; min-width: 0; min-height: 0; border: 0; padding: 0; transition: filter .16s ease, transform .16s ease; }
.square:disabled { cursor: default; }
.square.light { background: var(--square-light); }
.square.dark { background: var(--square-dark); }
.square.is-selected { box-shadow: inset 0 0 0 4px #fff6bd; z-index: 1; }
.square.is-last-move::after { content: ""; position: absolute; inset: 0; background: #f8d4474d; pointer-events: none; }
.square.is-target::before { content: ""; position: absolute; width: 25%; aspect-ratio: 1; border-radius: 50%; background: rgb(27 39 36 / 45%); z-index: 2; }
.square.is-target:has(.piece)::before { width: calc(100% - 10px); height: calc(100% - 10px); border: 4px solid rgb(50 43 32 / 48%); background: transparent; }
.square:not(:disabled):hover { filter: brightness(1.08); }
.piece { position: relative; z-index: 3; display: block; width: 92%; height: 92%; object-fit: contain; transform: translateY(-1.5%); user-select: none; pointer-events: none; }
.coordinates { position: absolute; color: var(--muted); font: 700 .72rem/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.coordinates.ranks { left: 3px; top: 9px; height: calc(100% - 30px); display: grid; grid-template-rows: repeat(8, 1fr); align-items: center; }
.coordinates.files { left: 28px; bottom: 3px; width: calc(100% - 28px); display: grid; grid-template-columns: repeat(8, 1fr); text-align: center; }
.board-overlay { position: absolute; inset: 0 0 21px 21px; display: grid; place-items: center; background: rgb(12 23 30 / 66%); color: #fffdf5; font-weight: 700; letter-spacing: .01em; opacity: 0; pointer-events: none; transition: opacity .2s ease; }
.board-overlay.is-visible { opacity: 1; pointer-events: auto; }

.side-panel { display: grid; gap: 13px; }
.turn-card, .control-card, .insight-card, .history-card { border: 1px solid var(--line); background: #fffdf8b8; box-shadow: 0 6px 18px rgb(37 32 22 / 5%); }
.turn-card { padding: 24px; border-top: 4px solid var(--gold); }
.eyebrow, .label { margin: 0; color: var(--muted); font-size: .71rem; font-weight: 800; letter-spacing: .11em; text-transform: uppercase; }
.turn-card h1 { margin: 7px 0 4px; color: var(--navy); font-family: Georgia, "Times New Roman", serif; font-size: 2.1rem; letter-spacing: -.045em; }
.turn-card p:last-child { margin: 0; color: #4c5759; line-height: 1.45; font-size: .94rem; }
.control-card, .insight-card, .history-card { padding: 18px; }
.segmented { display: grid; grid-template-columns: 1fr 1fr; margin: 9px 0 18px; border: 1px solid var(--line); border-radius: 3px; overflow: hidden; }
.color-choice { border: 0; padding: 9px 7px; color: #596061; background: transparent; font-size: .86rem; font-weight: 700; }
.color-choice + .color-choice { border-left: 1px solid var(--line); }
.color-choice.is-selected { color: #fff; background: var(--navy); }
select { width: 100%; margin: 9px 0 14px; padding: 10px 11px; border: 1px solid var(--line); border-radius: 3px; color: var(--ink); background: #fffdf8; font-size: .9rem; }
.primary-action { width: 100%; border: 0; padding: 12px 15px; color: #fffdf8; background: var(--navy); font-weight: 800; transition: background .16s ease, transform .16s ease; }
.primary-action:not(:disabled):hover { background: var(--navy-soft); transform: translateY(-1px); }
.primary-action:disabled { opacity: .5; cursor: default; }
.card-heading { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
#book-badge { border-radius: 20px; padding: 3px 7px; color: #64430c; background: #f2d98e; font-size: .68rem; font-weight: 800; text-transform: uppercase; }
.metrics { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; margin: 14px 0; }
.metrics div { padding-right: 6px; border-right: 1px solid var(--line); }
.metrics div:last-child { border-right: 0; }
.metrics span { display: block; margin-bottom: 3px; color: var(--muted); font-size: .67rem; }
.metrics strong { color: var(--navy); font-size: .88rem; }
.pv { min-height: 2.6em; margin: 0; overflow-wrap: anywhere; color: #536164; font: .75rem/1.45 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.text-button { border: 0; padding: 0; color: #87611e; background: none; font-size: .74rem; font-weight: 750; }
.history-card ol { display: grid; grid-template-columns: repeat(2, 1fr); max-height: 160px; margin: 12px 0 0; padding: 0; overflow: auto; list-style: none; }
.history-card li { display: flex; gap: 7px; padding: 5px 4px; border-bottom: 1px solid #e8e1d3; color: #4d5757; font-size: .81rem; }
.history-card li span { color: var(--muted); }
.history-card code { color: var(--navy); font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.muted { color: var(--muted) !important; }
footer { margin-top: 32px; color: var(--muted); font-size: .78rem; text-align: center; }

dialog { width: min(380px, calc(100% - 36px)); border: 1px solid var(--line); box-shadow: var(--shadow); background: #fffdf8; }
dialog::backdrop { background: rgb(9 20 27 / 55%); }
dialog h2 { margin: 7px 0 20px; color: var(--navy); font-family: Georgia, "Times New Roman", serif; font-size: 1.8rem; }
.promotion-options { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.promotion-options button { display: grid; place-items: center; border: 1px solid var(--line); padding: 6px; color: var(--navy); background: var(--paper); }
.promotion-piece { display: block; width: 100%; max-width: 54px; height: 54px; object-fit: contain; }
.promotion-options button:hover { border-color: var(--gold); background: #f7e8bc; }

@keyframes pulse { 50% { box-shadow: 0 0 0 5px #bc852d22; } }
@media (max-width: 820px) {
  .game-layout { grid-template-columns: minmax(0, 620px); }
  .board-frame { justify-self: center; }
  .side-panel { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .turn-card { grid-column: 1 / -1; }
}
@media (max-width: 570px) {
  .app-shell { width: min(100% - 24px, 520px); padding-top: 18px; }
  .masthead { margin-bottom: 18px; }
  .engine-badge { font-size: .72rem; }
  .board-frame { padding-left: 15px; padding-bottom: 17px; }
  .board { border-width: 5px; }
  .coordinates.ranks { left: 2px; top: 7px; height: calc(100% - 24px); font-size: .61rem; }
  .coordinates.files { left: 20px; bottom: 2px; width: calc(100% - 20px); font-size: .61rem; }
  .board-overlay { inset: 0 0 17px 15px; font-size: .86rem; }
  .side-panel { grid-template-columns: 1fr; }
  .turn-card { grid-column: auto; }
  .piece { font-size: clamp(1.85rem, 12vw, 3.75rem); }
  .history-card ol { max-height: 130px; }
}
