:root {
  --bg: #312e2b;
  --panel: #262421;
  --panel-2: #21201d;
  --line: #3d3a37;
  --text: #e9e8e7;
  --muted: #a09e9b;
  --green: #81b64c;
  --green-dark: #5d8a35;
  --light: #ebecd0;
  --dark: #739552;
  --light-text: #6d8a4d;
  --dark-text: #ebecd0;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  overscroll-behavior: none;
}

body { min-height: 100dvh; }

.screen { display: none; }
.screen.active { display: block; animation: screen-in 0.24s ease-out; }

@keyframes screen-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: none; }
}

.app {
  max-width: 520px;
  margin: 0 auto;
  padding: 8px 10px calc(14px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 6px;
}

/* --- верхняя строка --- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 2px 2px 0;
}

.brand {
  font-weight: 800;
  font-size: 18px;
  letter-spacing: 0.2px;
  color: #f2f1ef;
  display: flex;
  align-items: center;
  gap: 8px;
}

.brand::before {
  content: "";
  display: block;
  width: 8px;
  height: 20px;
  border-radius: 3px;
  background: var(--green);
}

.top-actions { display: flex; align-items: center; gap: 8px; }

.brand-wrap { display: flex; align-items: center; gap: 6px; }
.icon-btn.back { background: transparent; color: var(--muted); width: 30px; height: 30px; }

/* --- главное меню --- */
.screen.home {
  min-height: 100dvh;
  background: radial-gradient(130% 55% at 50% -12%, #3b3834 0%, #312e2b 58%);
}

.home {
  max-width: 520px;
  margin: 0 auto;
  padding: calc(8px + env(safe-area-inset-top)) 16px calc(18px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.home .topbar { padding: 0 0 2px; }

.hero {
  position: relative;
  overflow: hidden;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 16px;
  border-radius: 16px;
  border: 1px solid rgba(255, 255, 255, 0.05);
  background: linear-gradient(150deg, #3a3733 0%, #292724 65%);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25);
}

.hero::after {
  content: "";
  position: absolute;
  right: -34px;
  top: -46px;
  width: 150px;
  height: 150px;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(129, 182, 76, 0.24), transparent 70%);
  pointer-events: none;
}

.hero-avatar { flex: 0 0 auto; width: 56px; height: 56px; position: relative; z-index: 1; }
.hero-avatar .avatar { width: 100%; height: 100%; font-size: 22px; }

.hero-body { position: relative; z-index: 1; min-width: 0; }
.hero-kicker {
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  font-weight: 700;
  color: var(--green);
}
.hero-title {
  margin-top: 2px;
  font-size: 21px;
  font-weight: 800;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.hero-sub { margin-top: 3px; font-size: 12.5px; color: var(--muted); line-height: 1.35; }

.stats-strip {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  background: var(--panel);
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.04);
  overflow: hidden;
}
.stat { position: relative; padding: 10px 2px 9px; text-align: center; }
.stat + .stat::before {
  content: "";
  position: absolute;
  left: 0;
  top: 22%;
  bottom: 22%;
  width: 1px;
  background: var(--line);
}
.stat b {
  display: block;
  font-size: 18px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  line-height: 1.15;
}
.stat span {
  font-size: 10px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.resume {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid rgba(129, 182, 76, 0.42);
  background: rgba(129, 182, 76, 0.12);
  color: var(--text);
  text-align: left;
  cursor: pointer;
  font-family: inherit;
  transition: background 0.15s, transform 0.08s;
}
.resume[hidden] { display: none; }
.resume:active { transform: scale(0.99); background: rgba(129, 182, 76, 0.18); }
.resume-mark {
  flex: 0 0 auto;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--green);
  box-shadow: 0 0 0 4px rgba(129, 182, 76, 0.16);
  animation: resume-pulse 1.9s ease-in-out infinite;
}
.resume.finished { border-color: var(--line); background: var(--panel); }
.resume.finished .resume-mark { background: #7d7a76; box-shadow: none; animation: none; }
.resume.finished .resume-title { color: var(--text); }
@keyframes resume-pulse {
  0%, 100% { box-shadow: 0 0 0 4px rgba(129, 182, 76, 0.14); }
  50% { box-shadow: 0 0 0 7px rgba(129, 182, 76, 0.03); }
}
.resume-body { display: flex; flex-direction: column; flex: 1 1 auto; min-width: 0; }
.resume-title { font-size: 14px; font-weight: 700; color: #cfe8ac; }
.resume-sub {
  margin-top: 1px;
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.resume-arrow { flex: 0 0 auto; color: var(--muted); }

.pick { display: flex; flex-direction: column; gap: 8px; }
.section-label {
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  font-weight: 700;
  color: var(--muted);
}

.levels { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.levels button {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  padding: 12px 6px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel);
  color: var(--text);
  font-family: inherit;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s, transform 0.08s;
}
.levels button:active { transform: scale(0.98); }
.lv-name { font-size: 13.5px; font-weight: 700; }
.lv-rating { font-size: 11px; color: var(--muted); font-variant-numeric: tabular-nums; }
.levels button.active {
  border-color: var(--green);
  background: rgba(129, 182, 76, 0.14);
  box-shadow: inset 0 0 0 1px rgba(129, 182, 76, 0.28);
}
.levels button.active .lv-name { color: #cfe8ac; }
.levels button.active .lv-rating { color: #9fc46f; }

.pick-note { min-height: 17px; font-size: 12px; color: var(--muted); line-height: 1.4; }

.colors { display: flex; gap: 8px; }
.colors button {
  flex: 1 1 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 11px 6px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel);
  color: var(--text);
  font-family: inherit;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}
.colors button.active {
  border-color: var(--green);
  background: rgba(129, 182, 76, 0.14);
  color: #cfe8ac;
}
.color-dot {
  flex: 0 0 auto;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.22);
}
.color-dot.white { background: #f2f1ef; }
.color-dot.black { background: #2c2a27; box-shadow: inset 0 0 0 1px #6a6663; }
.color-dot.random { background: linear-gradient(135deg, #f2f1ef 0 48%, #2c2a27 52% 100%); }

.btn.big {
  padding: 15px 18px;
  font-size: 17px;
  border-radius: 12px;
}
.home-note {
  font-size: 11.5px;
  color: #7d7a76;
  line-height: 1.45;
  text-align: center;
  padding: 0 8px;
}

.chip {
  border: 0;
  background: var(--panel);
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
  padding: 7px 12px;
  border-radius: 8px;
  cursor: pointer;
}

.icon-btn {
  border: 0;
  background: var(--panel);
  color: var(--text);
  width: 34px;
  height: 34px;
  border-radius: 8px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}

.icon-btn svg { display: block; }
.icon-btn .slash { display: none; }
.icon-btn.off { opacity: 0.55; }
.icon-btn.off .slash { display: inline; }
.icon-btn.off .wave { display: none; }

/* --- игроки --- */
.player {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--panel);
  border-radius: 10px;
  padding: 8px 10px;
  transition: box-shadow 0.24s ease, background 0.24s ease;
}

.player.active { box-shadow: inset 3px 0 0 var(--green); }

.avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  -webkit-clip-path: circle(50%);
  clip-path: circle(50%);
  background: var(--panel-2);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  flex: 0 0 auto;
  font-size: 15px;
  font-weight: 800;
  color: #d8d6d3;
}

.avatar img {
  width: 100%;
  height: 100%;
  min-width: 100%;
  min-height: 100%;
  object-fit: cover;
  border-radius: 50%;
  -webkit-clip-path: circle(50%);
  clip-path: circle(50%);
  display: block;
}
.avatar.bot { background: rgba(129, 182, 76, 0.2); }
.avatar.initial { background: #3b3835; text-transform: uppercase; }
.avatar .avatar-piece { position: static; width: 100%; height: 100%; }
.avatar .avatar-piece svg { width: 84%; height: 84%; margin: auto; display: block; }

.pbody { min-width: 0; flex: 1 1 auto; }

.pname {
  display: flex;
  align-items: baseline;
  gap: 6px;
  font-size: 14px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.material { color: var(--muted); font-weight: 600; font-size: 12px; }

.captured {
  display: flex;
  align-items: center;
  gap: 0;
  height: 18px;
  margin-top: 1px;
  overflow: hidden;
}

.captured .piece {
  position: static;
  width: 17px;
  height: 17px;
  margin-right: -5px;
  opacity: 0.95;
}

.clock {
  flex: 0 0 auto;
  background: #211f1c;
  color: #cfcdc9;
  border-radius: 6px;
  padding: 5px 10px;
  font-size: 17px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.3px;
  min-width: 72px;
  text-align: center;
  transition: background 0.15s, color 0.15s;
}

.clock.on { background: #f4f4f3; color: #2d2b28; }
.clock.low { background: #e0574a; color: #fff; animation: clock-low 1s ease-in-out infinite; }

@keyframes clock-low {
  0%, 100% { box-shadow: 0 0 0 0 rgba(224, 87, 74, 0.45); }
  50% { box-shadow: 0 0 0 5px rgba(224, 87, 74, 0); }
}

/* --- доска --- */
.board-area {
  position: relative;
  display: flex;
  justify-content: center;
  border-radius: 8px;
}

.board {
  width: clamp(240px, min(100%, calc(100dvh - 380px)), 440px);
  aspect-ratio: 1 / 1;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  border-radius: 6px;
  overflow: hidden;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.35);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.sq { position: relative; }
.sq.light { background: var(--light); }
.sq.dark { background: var(--dark); }

.sq .coord {
  position: absolute;
  font-size: 9px;
  font-weight: 700;
  line-height: 1;
  pointer-events: none;
  z-index: 4;
}
.sq .coord.rank { top: 3px; left: 3px; }
.sq .coord.file { bottom: 2px; right: 3px; }
.sq.light .coord { color: var(--light-text); }
.sq.dark .coord { color: var(--dark-text); }

.sq.last::before,
.sq.sel::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 1;
}
.sq.last::before { background: rgba(255, 255, 51, 0.42); }
.sq.sel::before { background: rgba(255, 255, 51, 0.58); }

.sq.check {
  background-image: radial-gradient(circle, rgba(255, 30, 30, 0.85) 0%, rgba(255, 20, 20, 0.55) 38%, rgba(255, 0, 0, 0) 72%);
  animation: check-pulse 1.4s ease-in-out infinite;
}

.sq.hint {
  box-shadow: inset 0 0 0 4px rgba(129, 182, 76, 0.92);
  animation: hint-pulse 1.1s ease-in-out infinite;
}

.sq.target::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 30%;
  height: 30%;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  background: rgba(20, 20, 20, 0.14);
  z-index: 3;
  pointer-events: none;
  animation: dot-in 0.15s ease-out;
}

.sq.capture::after {
  content: "";
  position: absolute;
  inset: 7%;
  border-radius: 50%;
  box-shadow: inset 0 0 0 5px rgba(20, 20, 20, 0.14);
  z-index: 3;
  pointer-events: none;
  animation: ring-in 0.16s ease-out;
}

.piece {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}

.piece svg { width: 96%; height: 96%; display: block; }

.piece .shapes {
  stroke-width: 1.8;
  stroke-linejoin: round;
  paint-order: stroke;
}

.piece.w .shapes { fill: #ffffff; stroke: #4b4a47; }
.piece.b .shapes { fill: #3b3936; stroke: #131211; }
.piece.w .eye { fill: #4b4a47; stroke: none; }
.piece.b .eye { fill: #dedcda; stroke: none; }
.piece .line { fill: none; stroke-linecap: round; }

.piece.ghost {
  position: fixed;
  z-index: 999;
  pointer-events: none;
  inset: auto;
  filter: drop-shadow(0 3px 5px rgba(0, 0, 0, 0.4));
}

/* появление фигуры (превращение, начало партии) и уход взятой */
.piece.appear { animation: piece-in 0.2s cubic-bezier(0.2, 0.8, 0.3, 1.25); }
.piece.vanish { z-index: 3; animation: piece-out 0.2s ease-out forwards; }

@keyframes piece-in {
  from { opacity: 0; transform: scale(0.55); }
  to { opacity: 1; transform: scale(1); }
}
@keyframes piece-out {
  from { opacity: 1; transform: scale(1); }
  to { opacity: 0; transform: scale(0.7) rotate(-8deg); }
}

.board.deal { animation: board-in 0.28s ease-out; }
@keyframes board-in {
  from { opacity: 0.3; transform: scale(0.96); }
  to { opacity: 1; transform: none; }
}

@keyframes dot-in {
  from { opacity: 0; transform: translate(-50%, -50%) scale(0.3); }
  to { opacity: 1; transform: translate(-50%, -50%) scale(1); }
}
@keyframes ring-in {
  from { opacity: 0; transform: scale(0.75); }
  to { opacity: 1; transform: scale(1); }
}
@keyframes check-pulse {
  0%, 100% { box-shadow: inset 0 0 0 0 rgba(255, 40, 40, 0); }
  50% { box-shadow: inset 0 0 26px 6px rgba(255, 25, 25, 0.5); }
}
@keyframes hint-pulse {
  0%, 100% { box-shadow: inset 0 0 0 3px rgba(129, 182, 76, 0.95); }
  50% { box-shadow: inset 0 0 0 5px rgba(129, 182, 76, 0.95); }
}

.board-overlay {
  position: absolute;
  inset: 0;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  background: rgba(38, 36, 33, 0.92);
  border-radius: 8px;
  color: var(--muted);
  font-size: 14px;
  z-index: 5;
}
.board-overlay.show { display: flex; }

.spinner {
  width: 26px;
  height: 26px;
  border: 3px solid rgba(255, 255, 255, 0.15);
  border-top-color: var(--green);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.statusline {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  min-height: 32px;
}

/* --- статус и ходы --- */
.status {
  font-size: 13px;
  color: var(--muted);
  text-align: center;
  min-height: 18px;
}

.status.flash { animation: status-in 0.22s ease-out; }

@keyframes status-in {
  from { opacity: 0; transform: translateY(3px); }
  to { opacity: 1; transform: none; }
}

.opening {
  font-size: 11px;
  color: #7d7a76;
  min-height: 13px;
  text-align: center;
}

.moves {
  background: var(--panel);
  border-radius: 8px;
  padding: 6px 8px;
  max-height: 76px;
  overflow-y: auto;
  font-variant-numeric: tabular-nums;
  font-size: 13px;
  scrollbar-width: thin;
}

.moves-inner { display: flex; flex-direction: column; gap: 1px; }

.mrow {
  display: grid;
  grid-template-columns: 30px 1fr 1fr;
  gap: 4px;
  padding: 2px 4px;
  border-radius: 5px;
}
.mrow:nth-child(odd) { background: rgba(255, 255, 255, 0.03); }
.mnum { color: #7d7a76; }
.mv { color: #ddd; font-weight: 600; }
.mv.last { color: #b9e07f; animation: mv-in 0.5s ease-out; border-radius: 4px; }

@keyframes mv-in {
  from { background: rgba(185, 224, 127, 0.28); }
  to { background: transparent; }
}
.moves .empty { color: #7d7a76; padding: 4px; }

.actions { display: flex; gap: 8px; }
.actions.tools { gap: 6px; }

.btn {
  border: 0;
  border-radius: 8px;
  padding: 12px 16px;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.05s, filter 0.15s;
  flex: 1 1 auto;
}
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--green); color: #fff; box-shadow: 0 3px 0 var(--green-dark); }
.btn.ghost { background: #3b3835; color: #cfcdc9; }
.btn.ghost.armed { background: #b2453a; color: #fff; }
.btn.small { padding: 9px 10px; font-size: 13px; font-weight: 600; }
.btn.wide { width: 100%; }
.btn:disabled { opacity: 0.5; }
.btn:disabled:active { transform: none; }

/* --- модальные окна --- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(15, 14, 13, 0.72);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 18px;
  z-index: 50;
}
.modal.open { display: flex; animation: fade-in 0.16s ease-out; }
.modal.open .card { animation: card-in 0.18s ease-out; }

@keyframes fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes card-in {
  from { opacity: 0; transform: translateY(10px) scale(0.97); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

.card {
  width: 100%;
  max-width: 360px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 18px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
}

.card h2 { margin: 0 0 14px; font-size: 19px; }

.label {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--muted);
  margin: 12px 0 6px;
}

.chips { display: flex; gap: 6px; }

.chips button {
  flex: 1 1 0;
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 8px;
  padding: 9px 4px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  line-height: 1.2;
}
.chips button small { color: var(--muted); font-size: 11px; font-weight: 500; }
.chips button.active { border-color: var(--green); background: rgba(129, 182, 76, 0.16); color: #b9e07f; }

.note { font-size: 12px; color: var(--muted); margin: 14px 0; line-height: 1.4; }

.result-card { text-align: center; }
.result-icon {
  font-size: 34px;
  font-weight: 800;
  letter-spacing: 1px;
  line-height: 1;
  margin-bottom: 8px;
  color: var(--muted);
  animation: result-pop 0.36s cubic-bezier(0.2, 0.9, 0.3, 1.35);
}

@keyframes result-pop {
  from { opacity: 0; transform: scale(0.6); }
  to { opacity: 1; transform: scale(1); }
}
.result-icon.win { color: #b9e07f; }
.result-icon.loss { color: #e0574a; }
.result-icon.draw { color: #cfcdc9; }
.result-title { font-size: 24px; font-weight: 800; }
.result-reason { color: var(--muted); font-size: 14px; margin-top: 4px; }
.result-stats { color: var(--muted); font-size: 13px; margin: 14px 0 16px; }
.result-card .btn + .btn { margin-top: 8px; }

/* --- превращение --- */
.promo {
  position: fixed;
  inset: 0;
  background: rgba(15, 14, 13, 0.7);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 60;
}
.promo.open { display: flex; animation: fade-in 0.16s ease-out; }
.promo-card { background: var(--panel); border-radius: 12px; padding: 12px; text-align: center; animation: card-in 0.18s ease-out; }
.promo-title { font-size: 12px; color: var(--muted); margin-bottom: 8px; text-transform: uppercase; letter-spacing: 0.6px; }
.promo-pieces { display: flex; gap: 6px; }
.promo-pieces button {
  width: 58px;
  height: 58px;
  border: 1px solid var(--line);
  background: var(--panel-2);
  border-radius: 8px;
  cursor: pointer;
  padding: 4px;
}
.promo-pieces .piece { position: static; width: 100%; height: 100%; }

/* --- всплывающее сообщение --- */
.toast {
  position: fixed;
  left: 50%;
  bottom: 22px;
  transform: translate(-50%, 20px);
  background: #1c1b19;
  color: #f0efed;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 80;
  max-width: 88%;
  text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-height: 700px) {
  .moves { max-height: 54px; }
  .player { padding: 6px 8px; }
}

/* кто попросил систему без анимации — видит спокойную доску */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
}
