:root {
  --bg: #111f19;
  --surface: #182b22;
  --raised: #20372b;
  --line: #395344;
  --ink: #fbf0d7;
  --muted: #becbb8;
  --accent: #e8c987;
  --accent-ink: #202418;
  --light-square: #e0d1a5;
  --dark-square: #50785f;
  --frame: #293e31;
  --bloom: #768a4f3d;
  --last: #e8ba5199;
  --selected: #f7dc84;
  --danger: #ffc2ba;
  --signal: #94d3a9;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  color-scheme: dark;
}

:root[data-theme="classic"] {
  --bg: #221813;
  --surface: #31231c;
  --raised: #403026;
  --line: #635042;
  --muted: #d7c3ad;
  --accent: #f2cf9a;
  --light-square: #ecd7ad;
  --dark-square: #935e42;
  --frame: #513427;
  --bloom: #9c603f55;
  --last: #e9af559e;
}

:root[data-theme="obsidian"] {
  --bg: #11151f;
  --surface: #1a202e;
  --raised: #242d40;
  --line: #3e4b65;
  --muted: #c2c9d8;
  --accent: #e9c98c;
  --light-square: #ccd0d1;
  --dark-square: #576782;
  --frame: #303e56;
  --bloom: #53689155;
  --last: #ddad5899;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body {
  margin: 0;
  min-width: 250px;
  min-height: 100dvh;
  background:
    radial-gradient(ellipse at 12% 0, var(--bloom), transparent 49%),
    radial-gradient(ellipse at 90% 82%, var(--bloom), transparent 58%),
    var(--bg);
  color: var(--ink);
  -webkit-tap-highlight-color: transparent;
}

button {
  font: inherit;
  color: inherit;
  touch-action: manipulation;
  cursor: pointer;
}

button:disabled { cursor: default; opacity: .53; }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; z-index: 3; }
button:active:not(:disabled) { filter: brightness(.9); }
h1, h2, p { margin: 0; }

.shell {
  max-width: 1040px;
  margin: 0 auto;
  padding: max(14px, env(safe-area-inset-top), var(--tg-content-safe-area-inset-top, 0px))
    max(12px, env(safe-area-inset-right))
    max(22px, env(safe-area-inset-bottom), var(--tg-content-safe-area-inset-bottom, 0px))
    max(12px, env(safe-area-inset-left));
}

.masthead {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 2px 3px 14px;
}

.brand { display: flex; align-items: center; gap: 9px; }
.seal { color: var(--accent); width: clamp(32px, 9.5vw, 46px); flex-shrink: 0; }
.brand-name { color: var(--accent); font-size: .62rem; letter-spacing: .18em; text-transform: uppercase; }
h1, h2 { font-family: Georgia, "Times New Roman", serif; font-weight: 400; }
h1 { font-size: clamp(1.7rem, 6vw, 2.45rem); line-height: 1.02; letter-spacing: -.025em; }

.connection, .turn-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  border: 1px solid var(--line);
  padding: 5px 8px;
  border-radius: 999px;
  font-size: .66rem;
  white-space: nowrap;
}

.connection i, .turn-pill::before {
  flex: none;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
  content: "";
}

.connection[data-mode="live"] { color: var(--signal); }
.connection[data-mode="polling"] { color: var(--accent); }

.layout { display: grid; gap: 14px; align-items: start; }
.table, .sidebar { min-width: 0; display: grid; gap: 8px; }
.sidebar { gap: 10px; }
.player, .panel, .notice {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 13px;
}

.player {
  min-height: 54px;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 9px;
  padding: 7px 10px;
}

.player.is-turn { border-color: color-mix(in srgb, var(--accent), var(--line) 48%); }
.avatar {
  width: 35px;
  aspect-ratio: 1;
  border: 1px solid var(--line);
  border-radius: 10px;
  display: grid;
  place-items: center;
  background: var(--raised);
  color: var(--muted);
  font-family: Georgia, serif;
  font-size: .9rem;
}

.avatar.white { background: #e4d7b4; color: #30291c; }
.avatar.black { background: #1c2226; color: #ecd9b1; }
.player-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: .84rem; font-weight: 650; }
.player-meta { min-height: 14px; display: flex; align-items: center; gap: 4px; color: var(--muted); font-size: .65rem; }
.taken { display: flex; flex-wrap: wrap; line-height: 0; }
.taken svg { width: 14px; height: 14px; }
.taken svg + svg { margin-inline-start: -3px; }
.advantage { color: var(--accent); font-size: .7rem; font-weight: 700; }
.turn-pill { color: var(--accent); border-color: transparent; background: var(--raised); }

.board-frame {
  background: linear-gradient(147deg, var(--line), var(--frame) 34%, var(--frame));
  padding: clamp(5px, 1.6vw, 10px);
  border-radius: 11px;
  box-shadow: 0 9px 24px #0005, inset 0 0 0 1px #ffffff18;
}

.board {
  display: grid;
  grid-template-columns: repeat(8, minmax(0, 1fr));
  grid-template-rows: repeat(8, minmax(0, 1fr));
  aspect-ratio: 1;
  overflow: hidden;
  border-radius: 4px;
  user-select: none;
}

.square {
  display: grid;
  place-items: center;
  position: relative;
  min-width: 0;
  padding: 0;
  border: 0;
  background: var(--light-square);
  color: var(--dark-square);
  isolation: isolate;
}

.square.dark { background: var(--dark-square); color: var(--light-square); }
.square.last::before, .square.selected::before, .square.in-check::before {
  position: absolute;
  inset: 0;
  content: "";
  z-index: -1;
}

.square.last::before { background: var(--last); }
.square.selected::before { background: var(--last); box-shadow: inset 0 0 0 2px var(--selected), inset 0 0 20px var(--selected); }
.square.in-check::before { background: #a71a32b8; animation: check-pulse 1.35s ease-in-out infinite alternate; }
.square:focus-visible { outline: 2px solid var(--selected); outline-offset: -3px; }
.square:hover { box-shadow: inset 0 0 0 2px #ffffff4f; }

.square.target::after {
  position: absolute;
  width: 26%;
  aspect-ratio: 1;
  border-radius: 50%;
  background: #11221e7d;
  content: "";
}

.square.target.capture::after {
  width: 83%;
  border: clamp(2px, .7vw, 4px) solid #553228b8;
  background: none;
}

.piece { display: block; overflow: visible; }
.square > .piece-slot { display: grid; place-items: center; width: 85%; height: 85%; }
.square .piece { width: 100%; height: 100%; filter: drop-shadow(0 2px 1px #20150f55); pointer-events: none; }
.coord { position: absolute; z-index: 2; font-size: clamp(.52rem, 2.3vw, .78rem); font-weight: 750; pointer-events: none; }
.coord.rank { top: 2px; left: 3px; }
.coord.file { bottom: 1px; right: 3px; }

.panel { padding: 12px; }
.game-panel {
  background: linear-gradient(122deg, var(--raised), var(--surface) 80%);
  border-color: color-mix(in srgb, var(--line), var(--accent) 18%);
}

.viewer { color: var(--accent); font-size: .69rem; letter-spacing: .045em; margin-bottom: 4px; }
.game-panel h2 { font-size: clamp(1.25rem, 5vw, 1.7rem); }
.result-detail { margin-top: 5px; font-size: .83rem; color: var(--muted); }
.primary, .danger-solid {
  padding: 10px 13px;
  border: 1px solid transparent;
  border-radius: 9px;
  font-size: .84rem;
  font-weight: 680;
}

.primary { margin-top: 10px; width: 100%; background: var(--accent); color: var(--accent-ink); }
.history-panel h2 { font-size: 1.12rem; margin-bottom: 7px; }
#history-empty { color: var(--muted); font-size: .78rem; }
.history {
  display: flex;
  gap: 5px;
  overflow-x: auto;
  padding: 0 0 3px;
  margin: 0;
  list-style: none;
  scrollbar-color: var(--line) transparent;
  scrollbar-width: thin;
}

.history li {
  flex: none;
  display: grid;
  grid-auto-flow: column;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  background: var(--raised);
  border-radius: 6px;
  font-size: .82rem;
  font-variant-numeric: tabular-nums;
}

.history .move-number { color: var(--muted); font-size: .72rem; }
.history .latest { color: var(--accent); }

.tools, .game-actions, .dialog-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.tools { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); }
.tool, .game-actions button, .dialog-actions button, .dialog-back {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--raised);
  padding: 8px;
  font-size: .74rem;
  min-height: 40px;
}

.tool { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px; }
.tool svg { width: 17px; height: 17px; color: var(--accent); fill: none; stroke: currentColor; stroke-width: 1.65; stroke-linecap: round; stroke-linejoin: round; }
.tool[aria-pressed="true"] { border-color: var(--accent); }
.game-actions { margin-top: 9px; }
.game-actions button { flex: 1 1 auto; }
.game-actions .danger { color: var(--danger); }
.notice { color: var(--danger); padding: 10px 12px; margin-bottom: 10px; font-size: .85rem; }

dialog {
  width: min(390px, calc(100vw - 24px));
  border: 1px solid var(--line);
  border-radius: 17px;
  background: var(--surface);
  color: var(--ink);
  padding: 18px;
  box-shadow: 0 18px 70px #000a;
}

dialog::backdrop { background: #0c1112bc; backdrop-filter: blur(3px); }
dialog h2 { font-size: 1.55rem; margin-bottom: 10px; }
dialog p { color: var(--muted); margin-bottom: 13px; font-size: .86rem; }
.promotion-options { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-bottom: 10px; }
.promotion-options button { border: 1px solid var(--line); background: var(--raised); border-radius: 9px; padding: 4px; }
.promotion-options svg { width: 100%; }
.dialog-actions .danger-solid { background: var(--danger); color: #3b1111; }

@keyframes check-pulse {
  from { box-shadow: inset 0 0 4px #7f0b22; }
  to { box-shadow: inset 0 0 17px #ff8090; }
}

@media (min-width: 720px) {
  .shell { padding: 24px; }
  .masthead { padding-bottom: 18px; }
  .layout { grid-template-columns: minmax(0, 1.8fr) minmax(230px, 1fr); gap: 16px; }
  .player { padding: 10px 12px; }
  .avatar { width: 39px; }
  .panel { padding: 15px; }
  .history { display: grid; max-height: 224px; overflow: auto; }
  .history li { grid-template-columns: 2.2em minmax(0, 1fr) minmax(0, 1fr); }
}

@media (prefers-reduced-motion: reduce) {
  .square.in-check::before { animation: none; }
}
