:root {
  --bg: var(--tg-theme-bg-color, #16171b);
  --bg2: var(--tg-theme-secondary-bg-color, #0e0f12);
  --card: var(--tg-theme-section-bg-color, #1f2126);
  --text: var(--tg-theme-text-color, #f1f1f1);
  --hint: var(--tg-theme-hint-color, #8d919b);
  --accent: var(--tg-theme-button-color, #3d8bfd);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --link: var(--tg-theme-link-color, #5aa2ff);
  --danger: var(--tg-theme-destructive-text-color, #e5484d);
  --sep: color-mix(in srgb, var(--hint) 22%, transparent);
  --soft: color-mix(in srgb, var(--hint) 12%, transparent);
  --win: #2fb36b;
  --gold: #e8b23a;
  --light-sq: #f0d9b5;
  --dark-sq: #b58863;
  --radius: 14px;
  --safe-bottom: max(env(safe-area-inset-bottom), var(--tg-safe-area-inset-bottom, 0px));
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg2);
  color: var(--text);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", Roboto, "Segoe UI", system-ui, sans-serif;
  overscroll-behavior: none;
  -webkit-user-select: none; user-select: none;
}
button { font: inherit; color: inherit; border: 0; background: none; cursor: pointer; padding: 0; }
input { font: inherit; color: inherit; }
[hidden] { display: none !important; }

#app { max-width: 560px; margin: 0 auto; min-height: 100vh; min-height: var(--tg-viewport-stable-height, 100vh); }
.screen { padding: 12px 12px calc(20px + var(--safe-bottom)); animation: fade .18s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.splash { display: grid; place-items: center; align-content: center; gap: 8px; min-height: 80vh; }
.splash-logo { font-size: 64px; }
.hint { color: var(--hint); }
.small { font-size: 13px; }
.center { text-align: center; }
.row { display: flex; align-items: center; gap: 10px; }
.grow { flex: 1; min-width: 0; }
.ellipsis { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------- карточки и кнопки ---------- */
.card { background: var(--card); border-radius: var(--radius); padding: 14px; margin-bottom: 10px; }
.card h3 { margin: 0 0 10px; font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; color: var(--hint); }
.btn {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  width: 100%; min-height: 48px; padding: 0 16px; border-radius: 12px;
  background: var(--accent); color: var(--accent-text); font-weight: 600;
  transition: transform .08s ease, opacity .15s;
}
.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .45; }
.btn.secondary { background: var(--soft); color: var(--text); }
.btn.danger { background: color-mix(in srgb, var(--danger) 16%, transparent); color: var(--danger); }
.btn.small { min-height: 36px; width: auto; padding: 0 14px; font-size: 14px; border-radius: 10px; }
.btns { display: grid; gap: 8px; }
.btns.two { grid-template-columns: 1fr 1fr; }

.menu-item {
  display: flex; align-items: center; gap: 12px; width: 100%; padding: 12px 4px; text-align: left;
  border-bottom: 1px solid var(--sep);
}
.menu-item:last-child { border-bottom: 0; }
.menu-item .ico {
  width: 38px; height: 38px; border-radius: 10px; display: grid; place-items: center; font-size: 20px;
  background: var(--soft); flex: none;
}
.menu-item .chev { color: var(--hint); font-size: 20px; }
.menu-item b { display: block; font-weight: 600; }

/* ---------- шапка профиля ---------- */
.me-head { display: flex; align-items: center; gap: 12px; padding: 4px 2px 12px; }
.me-head .name { font-weight: 700; font-size: 18px; }
.rating-pill {
  display: inline-flex; align-items: center; gap: 4px; padding: 2px 10px; border-radius: 99px;
  background: var(--soft); font-weight: 700; font-variant-numeric: tabular-nums;
}
.online-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--win); }

.avatar {
  width: 40px; height: 40px; border-radius: 50%; flex: none; display: grid; place-items: center;
  font-weight: 700; color: #fff; overflow: hidden; position: relative; font-size: 16px;
}
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.avatar.lg { width: 72px; height: 72px; font-size: 28px; }
.avatar.sm { width: 32px; height: 32px; font-size: 13px; }
.avatar .on { position: absolute; right: 0; bottom: 0; width: 11px; height: 11px; border-radius: 50%; background: var(--win); border: 2px solid var(--card); }

/* ---------- контроль времени ---------- */
.tc-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.tc {
  padding: 12px 4px; border-radius: 12px; background: var(--soft); text-align: center;
  transition: transform .08s, background .15s;
}
.tc:active { transform: scale(.97); }
.tc b { display: block; font-size: 19px; font-variant-numeric: tabular-nums; }
.tc span { font-size: 12px; color: var(--hint); }
.tc.on { background: var(--accent); color: var(--accent-text); }
.tc.on span { color: inherit; opacity: .8; }

.seg { display: flex; background: var(--soft); border-radius: 10px; padding: 3px; gap: 3px; }
.seg button { flex: 1; padding: 8px 6px; border-radius: 8px; font-weight: 600; font-size: 14px; color: var(--hint); }
.seg button.on { background: var(--card); color: var(--text); box-shadow: 0 1px 3px rgba(0, 0, 0, .15); }
.field { margin-bottom: 14px; }
.field > label { display: block; margin-bottom: 6px; font-size: 13px; color: var(--hint); }

.search {
  width: 100%; padding: 11px 14px; border-radius: 12px; border: 0; outline: 0;
  background: var(--soft); -webkit-user-select: text; user-select: text;
}

/* ---------- списки игроков ---------- */
.plist { display: flex; flex-direction: column; }
.prow { display: flex; align-items: center; gap: 10px; padding: 9px 2px; border-bottom: 1px solid var(--sep); width: 100%; text-align: left; }
.prow:last-child { border-bottom: 0; }
.prow .place { width: 28px; text-align: center; font-weight: 700; color: var(--hint); font-variant-numeric: tabular-nums; }
.prow .place.medal { font-size: 20px; }
.prow .val { font-weight: 700; font-variant-numeric: tabular-nums; }
.prow.me { background: color-mix(in srgb, var(--accent) 10%, transparent); border-radius: 10px; }
.plus { color: var(--win); }
.minus { color: var(--danger); }

.sticky-me { position: sticky; bottom: calc(8px + var(--safe-bottom)); background: var(--card); border-radius: 12px; padding: 4px 8px; box-shadow: 0 4px 18px rgba(0, 0, 0, .25); }

/* ---------- вызовы ---------- */
.challenge {
  display: flex; align-items: center; gap: 10px; padding: 10px; border-radius: 12px;
  background: color-mix(in srgb, var(--accent) 12%, var(--card)); margin-bottom: 8px;
}
.challenge.in { animation: pulse 1.6s ease-in-out 2; }
@keyframes pulse { 50% { box-shadow: 0 0 0 4px color-mix(in srgb, var(--accent) 35%, transparent); } }
.banner {
  display: flex; align-items: center; gap: 10px; padding: 12px 14px; border-radius: var(--radius);
  background: var(--accent); color: var(--accent-text); margin-bottom: 10px; font-weight: 600; width: 100%;
}

/* ---------- доска ---------- */
.board {
  position: relative; width: 100%; aspect-ratio: 1; border-radius: 6px; overflow: hidden;
  touch-action: none; box-shadow: 0 6px 24px rgba(0, 0, 0, .28);
}
.squares { position: absolute; inset: 0; display: grid; grid-template-columns: repeat(8, 1fr); grid-template-rows: repeat(8, 1fr); }
.sq { position: relative; }
.sq.light { background: var(--light-sq); }
.sq.dark { background: var(--dark-sq); }
.sq.last::before { content: ""; position: absolute; inset: 0; background: rgba(255, 214, 10, .42); }
.sq.selected::before { content: ""; position: absolute; inset: 0; background: rgba(20, 110, 60, .5); }
.sq.hover::before { content: ""; position: absolute; inset: 0; background: rgba(255, 255, 255, .28); }
.sq.check::after {
  content: ""; position: absolute; inset: 0;
  background: radial-gradient(circle, rgba(255, 30, 30, .95) 0%, rgba(230, 20, 20, .55) 38%, rgba(200, 0, 0, 0) 72%);
}
.sq.dot::after {
  content: ""; position: absolute; left: 50%; top: 50%; width: 30%; height: 30%; transform: translate(-50%, -50%);
  border-radius: 50%; background: rgba(20, 70, 40, .38);
}
.sq.ring::after {
  content: ""; position: absolute; inset: 0; border-radius: 50%;
  background: radial-gradient(circle, transparent 0 62%, rgba(20, 70, 40, .42) 63%);
}
.coord { position: absolute; font-size: 10px; font-weight: 700; line-height: 1; pointer-events: none; z-index: 1; }
.coord.rank { left: 3px; top: 3px; }
.coord.file { right: 3px; bottom: 2px; }
.sq.light .coord { color: var(--dark-sq); }
.sq.dark .coord { color: var(--light-sq); }

.pieces { position: absolute; inset: 0; pointer-events: none; }
.piece {
  position: absolute; left: 0; top: 0; width: 12.5%; height: 12.5%;
  transition: transform .2s cubic-bezier(.3, .7, .3, 1), opacity .2s; will-change: transform; z-index: 2;
}
.piece.no-anim { transition: none; }
.piece.dragging { transition: none; z-index: 10; filter: drop-shadow(0 6px 6px rgba(0, 0, 0, .35)); }
.piece.captured { opacity: 0; z-index: 1; }
.piece-svg { width: 100%; height: 100%; display: block; filter: drop-shadow(0 1px 1px rgba(0, 0, 0, .25)); }

.promo-shade { position: absolute; inset: 0; background: rgba(0, 0, 0, .35); z-index: 20; }
.promo { position: absolute; width: 12.5%; display: flex; flex-direction: column; z-index: 21; background: #fff; border-radius: 6px; box-shadow: 0 6px 20px rgba(0, 0, 0, .4); }
.promo button { aspect-ratio: 1; width: 100%; padding: 6%; }
.promo button:active { background: #e8e8e8; }

/* ---------- экран партии ---------- */
.game { display: flex; flex-direction: column; gap: 8px; padding-top: 8px; }
.plate { display: flex; align-items: center; gap: 10px; padding: 6px 4px; min-height: 48px; }
.plate .who { flex: 1; min-width: 0; }
.plate .who b { display: block; font-weight: 600; }
.plate .who .hint { font-size: 13px; }
.captures { font-size: 12px; color: var(--hint); letter-spacing: -1px; min-height: 16px; }
.clock {
  min-width: 86px; padding: 6px 12px; border-radius: 10px; text-align: center;
  font: 700 22px/1.2 ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-variant-numeric: tabular-nums;
  background: var(--soft); color: var(--hint); transition: background .2s, color .2s;
}
.clock.active { background: var(--card); color: var(--text); box-shadow: inset 0 0 0 2px var(--accent); }
.clock.low.active { background: var(--danger); color: #fff; box-shadow: none; }
.status-line { min-height: 22px; text-align: center; font-size: 14px; color: var(--hint); }
.status-line.warn { color: var(--danger); font-weight: 600; }

.moves {
  display: flex; gap: 2px; overflow-x: auto; scrollbar-width: none; padding: 4px 2px; align-items: center;
  font-variant-numeric: tabular-nums; min-height: 36px;
}
.moves::-webkit-scrollbar { display: none; }
.moves .n { color: var(--hint); padding: 0 2px 0 6px; font-size: 13px; }
.moves button { padding: 4px 6px; border-radius: 6px; white-space: nowrap; font-weight: 500; }
.moves button.cur { background: var(--accent); color: var(--accent-text); }
.toolbar { display: flex; gap: 6px; }
.toolbar button {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 8px 2px;
  border-radius: 12px; background: var(--card); font-size: 12px; color: var(--hint); position: relative;
}
.toolbar button .i { font-size: 20px; line-height: 1; color: var(--text); }
.toolbar button:disabled { opacity: .4; }
.toolbar .badge { position: absolute; top: 4px; right: 18%; min-width: 8px; height: 8px; border-radius: 4px; background: var(--danger); }
.offer {
  display: flex; align-items: center; gap: 8px; padding: 10px 12px; border-radius: 12px;
  background: color-mix(in srgb, var(--gold) 22%, var(--card));
}

/* ---------- всплывающие окна ---------- */
.sheet-shade { position: fixed; inset: 0; background: rgba(0, 0, 0, .45); z-index: 100; animation: fadeIn .15s; display: flex; align-items: flex-end; justify-content: center; }
@keyframes fadeIn { from { opacity: 0; } }
.sheet {
  width: 100%; max-width: 560px; background: var(--bg); border-radius: 18px 18px 0 0;
  padding: 10px 16px calc(16px + var(--safe-bottom)); max-height: 85vh; overflow-y: auto;
  animation: up .22s cubic-bezier(.3, .7, .3, 1);
}
@keyframes up { from { transform: translateY(100%); } }
.sheet .grip { width: 36px; height: 5px; border-radius: 3px; background: var(--sep); margin: 0 auto 12px; }
.sheet h2 { margin: 4px 0 12px; font-size: 20px; text-align: center; }
.result-title { font-size: 28px; font-weight: 800; text-align: center; margin: 6px 0 2px; }
.result-delta { font-size: 22px; font-weight: 800; text-align: center; margin: 8px 0 14px; font-variant-numeric: tabular-nums; }

.chat-log { display: flex; flex-direction: column; gap: 6px; max-height: 40vh; overflow-y: auto; margin-bottom: 10px; padding: 2px; }
.bubble { max-width: 80%; padding: 7px 11px; border-radius: 14px; background: var(--card); align-self: flex-start; -webkit-user-select: text; user-select: text; word-wrap: break-word; }
.bubble.mine { align-self: flex-end; background: var(--accent); color: var(--accent-text); }
.quick { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; }
.quick button { padding: 6px 10px; border-radius: 99px; background: var(--soft); font-size: 14px; }

.toast {
  position: fixed; left: 50%; bottom: calc(24px + var(--safe-bottom)); transform: translateX(-50%); z-index: 200;
  background: rgba(20, 20, 24, .92); color: #fff; padding: 10px 16px; border-radius: 12px; max-width: 90vw;
  font-size: 14px; box-shadow: 0 6px 20px rgba(0, 0, 0, .3); animation: fadeIn .15s;
}

.searching { display: grid; place-items: center; gap: 6px; padding: 20px 0 6px; }
.spinner { width: 54px; height: 54px; border-radius: 50%; border: 4px solid var(--soft); border-top-color: var(--accent); animation: spin .9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; text-align: center; }
.stats > div { background: var(--soft); border-radius: 12px; padding: 10px 4px; }
.stats b { display: block; font-size: 20px; font-variant-numeric: tabular-nums; }
.stats span { font-size: 12px; color: var(--hint); }
.wdl { display: flex; height: 8px; border-radius: 4px; overflow: hidden; background: var(--soft); margin: 10px 0 4px; }
.wdl i { display: block; }
.spark { width: 100%; height: 70px; display: block; }
.game-row { display: flex; align-items: center; gap: 10px; padding: 10px 2px; border-bottom: 1px solid var(--sep); width: 100%; text-align: left; }
.game-row:last-child { border-bottom: 0; }
.res { width: 30px; height: 30px; border-radius: 8px; display: grid; place-items: center; font-weight: 800; font-size: 14px; flex: none; }
.res.w { background: color-mix(in srgb, var(--win) 20%, transparent); color: var(--win); }
.res.l { background: color-mix(in srgb, var(--danger) 18%, transparent); color: var(--danger); }
.res.d { background: var(--soft); color: var(--hint); }
.empty { text-align: center; color: var(--hint); padding: 24px 8px; }
.tabs { margin-bottom: 10px; }
.level { display: flex; align-items: center; gap: 12px; padding: 12px; border-radius: 12px; background: var(--soft); margin-bottom: 8px; width: 100%; text-align: left; }
.level.on { background: var(--accent); color: var(--accent-text); }
.level .stars { margin-left: auto; letter-spacing: 1px; }
