:root {
  --bg: #f4f1ec;
  --card: #ffffff;
  --text: #22201c;
  --muted: #7a746b;
  --line: #e2ddd4;
  --accent: #2f7d5b;
  --accent-text: #ffffff;
  --danger: #b4412f;
  --sq-light: #ecdcbf;
  --sq-dark: #ae8460;
  --sq-last: rgba(240, 200, 40, .42);
  --sq-sel: rgba(60, 140, 230, .5);
  --dot: rgba(30, 30, 30, .28);
  --shadow: 0 1px 3px rgba(0, 0, 0, .08), 0 4px 16px rgba(0, 0, 0, .05);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1f1d1a;
    --card: #2a2824;
    --text: #ece8e1;
    --muted: #9c958a;
    --line: #3a3732;
    --accent: #4aa57c;
    --accent-text: #0f1a14;
    --danger: #e0715d;
    --sq-light: #d9c6a5;
    --sq-dark: #94704f;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
  touch-action: manipulation;
  overscroll-behavior: none;
}
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }

#app {
  max-width: 620px;
  margin: 0 auto;
  min-height: 100%;
  padding: env(safe-area-inset-top) max(12px, env(safe-area-inset-right)) env(safe-area-inset-bottom) max(12px, env(safe-area-inset-left));
}

.top {
  display: flex; align-items: center; gap: 8px;
  height: 52px;
}
.top h1 { font-size: 19px; margin: 0; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn {
  border: 0; background: transparent; border-radius: 10px;
  min-width: 40px; height: 40px; display: grid; place-items: center;
  font-size: 22px; line-height: 1;
}
.icon-btn:active { background: var(--line); }
.icon-btn:disabled { opacity: .3; }

.conn { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }
.conn i { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); display: inline-block; }
.conn[data-state="ok"] i { background: #3bb273; }
.conn[data-state="bad"] i { background: #e0a020; animation: blink 1s infinite; }
@keyframes blink { 50% { opacity: .25; } }

.card {
  background: var(--card); border-radius: 14px; padding: 14px;
  margin: 0 0 12px; box-shadow: var(--shadow);
}
.card h2 { font-size: 16px; margin: 0 0 10px; }
.field { display: grid; gap: 6px; }
.field span { font-size: 13px; color: var(--muted); }
input {
  width: 100%; padding: 11px 12px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--bg); outline: none;
}
input:focus { border-color: var(--accent); }
.muted { color: var(--muted); margin: 0; }

.btn {
  border: 1px solid var(--line); background: var(--card); border-radius: 10px;
  padding: 10px 14px; font-weight: 600; white-space: nowrap;
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.danger { color: var(--danger); }
.btn.wide { width: 100%; margin-top: 10px; padding: 13px; }
.btn:disabled { opacity: .45; }

.seg { display: grid; grid-template-columns: repeat(3, 1fr); background: var(--bg); border-radius: 10px; padding: 3px; gap: 3px; }
.seg button { border: 0; background: transparent; border-radius: 8px; padding: 8px 4px; }
.seg button.on { background: var(--card); box-shadow: var(--shadow); font-weight: 600; }

.code-row { display: flex; gap: 8px; margin-top: 12px; }
.code-row input { flex: 1; min-width: 0; }

.game-list { display: grid; gap: 8px; }
.game-item {
  display: flex; align-items: center; gap: 10px; width: 100%; text-align: left;
  border: 1px solid var(--line); background: transparent; border-radius: 10px; padding: 10px 12px;
}
.game-item .gi-main { flex: 1; min-width: 0; }
.game-item .gi-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.game-item .gi-sub { font-size: 13px; color: var(--muted); }
.game-item.turn { border-color: var(--accent); }
.game-item.turn .gi-sub { color: var(--accent); font-weight: 600; }
.game-item .gi-code { font: 600 12px/1 ui-monospace, Menlo, Consolas, monospace; color: var(--muted); text-transform: uppercase; }

.hint { font-size: 14px; color: var(--muted); }
.hint b { color: var(--text); }

/* ---------- партия ---------- */
#game { display: flex; flex-direction: column; align-items: stretch; }
.pbar { display: flex; align-items: center; gap: 8px; min-height: 38px; padding: 2px 2px; }
.pbar .pname { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 55%; }
.pbar .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--line); flex: none; }
.pbar .dot.on { background: #3bb273; }
.pbar .caps { display: flex; align-items: center; flex: 1; justify-content: flex-end; min-width: 0; overflow: hidden; }
.pbar .caps svg { width: 18px; height: 18px; margin-left: -5px; }
.pbar .adv { font-size: 13px; color: var(--muted); margin-left: 4px; }
.pbar.turn .pname::after { content: " · ходит"; color: var(--accent); font-weight: 500; font-size: 14px; }

.board-wrap { position: relative; width: min(100%, calc(100dvh - 290px), 600px); min-width: 260px; align-self: center; }
.board {
  display: grid; grid-template-columns: repeat(8, 1fr); grid-template-rows: repeat(8, 1fr);
  aspect-ratio: 1; width: 100%; border-radius: 6px; overflow: hidden;
  touch-action: none; user-select: none; -webkit-user-select: none;
  box-shadow: var(--shadow);
}
.sq { position: relative; background: var(--sq-light); }
.sq.dark { background: var(--sq-dark); }
.sq.last::before, .sq.sel::before, .sq.check::before { content: ""; position: absolute; inset: 0; }
.sq.last::before { background: var(--sq-last); }
.sq.sel::before { background: var(--sq-sel); }
.sq.check::before { background: radial-gradient(circle, rgba(255, 30, 0, .85) 0%, rgba(230, 0, 0, .45) 40%, transparent 72%); }
.sq svg { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.sq.dragging svg { opacity: .25; }
.sq.target::after {
  content: ""; position: absolute; left: 50%; top: 50%; width: 30%; height: 30%;
  transform: translate(-50%, -50%); border-radius: 50%; background: var(--dot);
}
.sq.target.occ::after { width: 100%; height: 100%; background: transparent; box-shadow: inset 0 0 0 5px var(--dot); border-radius: 50%; }
.sq .co { position: absolute; font-size: 10px; font-weight: 700; line-height: 1; opacity: .75; pointer-events: none; }
.sq .co.f { right: 3px; bottom: 2px; }
.sq .co.r { left: 3px; top: 3px; }
.sq.dark .co { color: var(--sq-light); }
.sq:not(.dark) .co { color: var(--sq-dark); }
.board.review { filter: saturate(.75); }
.ghost { position: fixed; pointer-events: none; z-index: 20; transform: translate(-50%, -50%) scale(1.15); }
.ghost svg { width: 100%; height: 100%; }

.status { text-align: center; font-weight: 600; min-height: 26px; margin: 6px 0 2px; }
.status.win { color: var(--accent); }
.status.lose { color: var(--danger); }

.offer {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap; justify-content: center;
  background: var(--card); border: 1px solid var(--accent); border-radius: 12px; padding: 10px; margin: 6px 0;
}
.offer span { flex: 1 1 100%; text-align: center; }

.controls { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; margin: 8px 0; }
.controls .btn { padding: 9px 12px; font-size: 15px; }

.nav { display: flex; align-items: center; gap: 2px; margin-bottom: 12px; }
.moves {
  flex: 1; display: flex; gap: 2px; overflow-x: auto; scrollbar-width: none; padding: 4px 0;
  font: 14px/1.2 ui-monospace, Menlo, Consolas, monospace; white-space: nowrap;
}
.moves::-webkit-scrollbar { display: none; }
.moves .no { color: var(--muted); padding: 4px 0 4px 6px; }
.moves button { border: 0; background: transparent; border-radius: 6px; padding: 4px 5px; font: inherit; }
.moves button.cur { background: var(--accent); color: var(--accent-text); }
.moves .empty { color: var(--muted); font-family: inherit; padding: 4px 6px; }

/* ---------- окна ---------- */
.modal {
  position: fixed; inset: 0; z-index: 30; background: rgba(0, 0, 0, .45);
  display: grid; place-items: center; padding: 16px;
}
.sheet { background: var(--card); border-radius: 16px; padding: 18px; width: min(100%, 380px); box-shadow: 0 10px 40px rgba(0, 0, 0, .3); }
.sheet h3 { margin: 0 0 12px; font-size: 18px; }
.sheet p { margin: 0 0 12px; color: var(--muted); }
.sheet .row { display: flex; gap: 8px; justify-content: flex-end; margin-top: 14px; flex-wrap: wrap; }
.sheet .stack { display: grid; gap: 8px; }
.promo { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.promo button { aspect-ratio: 1; border: 1px solid var(--line); border-radius: 12px; background: var(--sq-light); padding: 4px; }
.promo svg { width: 100%; height: 100%; }
.linkbox { font: 13px/1.3 ui-monospace, Menlo, Consolas, monospace; word-break: break-all; background: var(--bg); border-radius: 8px; padding: 8px; user-select: all; -webkit-user-select: all; }

.toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translate(-50%, 20px);
  background: #1d1b18; color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 15px;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 40; max-width: calc(100% - 32px); text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
