:root {
  /* Цвета берутся из темы Telegram, в браузере — запасные значения */
  --bg: var(--tg-theme-bg-color, #ffffff);
  --bg2: var(--tg-theme-secondary-bg-color, #f1f1f4);
  --text: var(--tg-theme-text-color, #16161a);
  --hint: var(--tg-theme-hint-color, #8b8b93);
  --accent: var(--tg-theme-button-color, #2f7bf5);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --link: var(--tg-theme-link-color, #2f7bf5);
  --danger: var(--tg-theme-destructive-text-color, #e5484d);
  --good: #2fa84f;
  --wood-1: #ecc67f;
  --wood-2: #d9a657;
  --line: #5c3d12;
  --radius: 14px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: var(--tg-theme-bg-color, #17181c);
    --bg2: var(--tg-theme-secondary-bg-color, #24252c);
    --text: var(--tg-theme-text-color, #f2f2f5);
    --hint: var(--tg-theme-hint-color, #8e8e98);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
}
body { min-height: 100vh; overscroll-behavior: none; -webkit-user-select: none; user-select: none; }
#app {
  max-width: 560px;
  margin: 0 auto;
  padding: 12px 16px calc(16px + env(safe-area-inset-bottom));
}
.hidden { display: none !important; }
.hint { color: var(--hint); margin: 0; }
.center { text-align: center; }

/* ── Кнопки ── */
.btn {
  appearance: none;
  border: 0;
  border-radius: 12px;
  padding: 11px 14px;
  font: inherit;
  font-weight: 600;
  background: var(--bg2);
  color: var(--text);
  cursor: pointer;
  transition: transform .08s, opacity .15s;
}
.btn:active:not(:disabled) { transform: scale(.97); }
.btn:disabled { opacity: .45; cursor: default; }
.btn.primary { background: var(--accent); color: var(--accent-text); }
.btn.ghost { background: transparent; box-shadow: inset 0 0 0 1.5px var(--bg2); }
.btn.danger { color: var(--danger); }
.btn.big { width: 100%; padding: 14px; font-size: 16px; }
.link-btn {
  border: 0; background: none; padding: 6px 0;
  color: var(--link); font: inherit; font-size: 16px; cursor: pointer;
}

/* ── Главный экран ── */
.hero { display: flex; align-items: center; gap: 14px; margin: 8px 0 20px; }
.logo { width: 56px; height: 56px; border-radius: 14px; box-shadow: 0 4px 14px rgba(0,0,0,.15); }
h1 { margin: 0; font-size: 26px; letter-spacing: -.3px; }
.lang-row {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  margin-top: 12px; padding: 10px 14px;
  border-radius: var(--radius); background: var(--bg2);
  font-weight: 600; cursor: pointer;
}
.lang-row select {
  max-width: 60%;
  border: 0; background: transparent; color: var(--link);
  font: inherit; font-weight: 600; text-align: right; cursor: pointer; outline: none;
}
.lang-row option { color: #000; }
.join { display: flex; gap: 8px; margin-top: 10px; }
.join input {
  flex: 1; min-width: 0;
  padding: 12px 14px; border: 0; border-radius: 12px;
  background: var(--bg2); color: var(--text);
  font: inherit; font-size: 16px; outline: none;
  -webkit-user-select: text; user-select: text;
}
.join input::placeholder { color: var(--hint); }
.join input:focus { box-shadow: inset 0 0 0 2px var(--accent); }
.code-chip {
  align-self: center;
  border: 0; border-radius: 99px; padding: 6px 14px;
  background: var(--bg2); color: var(--text); font: inherit; cursor: pointer;
}
.code-chip b { letter-spacing: 1px; font-family: ui-monospace, Menlo, Consolas, monospace; }
.section-title { font-size: 13px; text-transform: uppercase; letter-spacing: .5px; color: var(--hint); margin: 24px 4px 8px; font-weight: 600; }

.list { display: flex; flex-direction: column; gap: 8px; }
.item {
  display: flex; align-items: center; gap: 12px;
  width: 100%; padding: 12px 14px;
  border: 0; border-radius: var(--radius);
  background: var(--bg2); color: var(--text);
  font: inherit; text-align: left; cursor: pointer;
}
.item:active { opacity: .7; }
.item-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.item-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item-sub { color: var(--hint); font-size: 13px; }
.badge { font-size: 12px; font-weight: 700; padding: 4px 9px; border-radius: 99px; white-space: nowrap; background: var(--bg); color: var(--hint); }
.badge.accent { background: var(--accent); color: var(--accent-text); }
.badge.good { background: rgba(47,168,79,.15); color: var(--good); }
.badge.bad { background: rgba(229,72,77,.12); color: var(--danger); }
.empty { color: var(--hint); text-align: center; padding: 24px 12px; }
.big-empty { padding-top: 30vh; display: flex; flex-direction: column; align-items: center; gap: 14px; font-size: 16px; }

.rules { margin-top: 24px; background: var(--bg2); border-radius: var(--radius); padding: 12px 14px; }
.rules summary { font-weight: 600; cursor: pointer; }
.rules ul { margin: 10px 0 0; padding-left: 20px; }
.rules li { margin: 4px 0; }

/* ── Камни (иконки) ── */
.stone { flex: none; width: 22px; height: 22px; border-radius: 50%; display: inline-block; }
.stone.black { background: radial-gradient(circle at 35% 30%, #6a6a6a, #0b0b0b 70%); }
.stone.white { background: radial-gradient(circle at 35% 30%, #fff, #cdcdcd 75%); box-shadow: inset 0 0 0 1px rgba(0,0,0,.18); }

/* ── Экран партии ── */
.topbar { display: flex; align-items: center; justify-content: space-between; min-height: 32px; }
.conn { font-size: 13px; color: var(--danger); }

.players { display: flex; align-items: center; gap: 8px; margin: 6px 0 4px; }
.player {
  flex: 1; min-width: 0;
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px;
  border-radius: var(--radius);
  background: var(--bg2);
  box-shadow: inset 0 0 0 2px transparent;
  transition: box-shadow .2s;
}
.player.right { justify-content: flex-end; text-align: right; }
.player.active { box-shadow: inset 0 0 0 2px var(--accent); }
.pinfo { min-width: 0; }
.pname { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pmeta { font-size: 12px; color: var(--hint); }
.player.online .pmeta { color: var(--good); }
.score { font-weight: 800; font-size: 18px; white-space: nowrap; padding: 0 2px; }

.status { text-align: center; font-weight: 700; font-size: 17px; margin: 10px 0 8px; min-height: 24px; }
.status.mine { color: var(--accent); }
.status.win { color: var(--good); }
.status.loss { color: var(--danger); }
.timer {
  display: inline-block; margin-left: 8px; padding: 1px 9px; border-radius: 99px;
  background: var(--bg2); color: var(--text);
  font-size: 15px; font-variant-numeric: tabular-nums; vertical-align: 1px;
}
.timer.low { background: var(--danger); color: #fff; }

.banner {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  padding: 10px 12px; margin-bottom: 8px;
  border-radius: var(--radius); background: var(--bg2);
}
.banner .text { flex: 1 1 160px; font-weight: 600; }
.banner .btn { padding: 8px 12px; }

.board-wrap {
  position: relative;
  width: 100%;
  max-width: max(280px, calc(var(--tg-viewport-stable-height, 100vh) - 250px));
  margin: 0 auto;
  aspect-ratio: 1;
}
#board {
  display: block; width: 100%; height: 100%;
  border-radius: 10px;
  box-shadow: 0 6px 20px rgba(0,0,0,.18);
  touch-action: manipulation;
  cursor: pointer;
}
#board .wood { fill: url(#wood); }
#board .grid line { stroke: var(--line); stroke-width: 1px; vector-effect: non-scaling-stroke; opacity: .85; }
#board .stars circle { fill: var(--line); }
#board .shadow { fill: rgba(0,0,0,.28); }
#board .st.b { fill: url(#gb); }
#board .st.w { fill: url(#gw); stroke: rgba(0,0,0,.25); stroke-width: .03; }
#board .st.ghost { opacity: .55; }
#board .st.new { animation: drop .18s ease-out; transform-box: fill-box; transform-origin: center; }
#board .last { fill: #e5484d; }
#board .winline { stroke: #e5484d; stroke-width: .14; stroke-linecap: round; opacity: .85; }
#board .target { fill: none; stroke: var(--accent); stroke-width: .06; }
@keyframes drop { from { transform: scale(1.25); opacity: .4; } to { transform: scale(1); opacity: 1; } }

.overlay {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  padding: 16px;
  background: rgba(0,0,0,.25);
  border-radius: 10px;
}
.overlay-card {
  width: 100%; max-width: 320px;
  display: flex; flex-direction: column; gap: 10px;
  padding: 18px; border-radius: 18px;
  background: var(--bg); text-align: center;
  box-shadow: 0 10px 30px rgba(0,0,0,.25);
}
.overlay-title { font-size: 18px; font-weight: 700; }

.actions { display: flex; gap: 8px; margin-top: 12px; }
.actions .btn { flex: 1; }
.actions .btn.primary { flex: 1.4; }

/* ── Лист создания партии ── */
.sheet {
  position: fixed; inset: 0; z-index: 10;
  display: flex; align-items: flex-end; justify-content: center;
  background: rgba(0,0,0,.4);
  animation: fade .15s;
}
.sheet-card {
  width: 100%; max-width: 560px;
  display: flex; flex-direction: column; gap: 10px;
  padding: 20px 16px calc(16px + env(safe-area-inset-bottom));
  border-radius: 20px 20px 0 0;
  background: var(--bg);
  animation: up .2s ease-out;
}
.sheet-card h3 { margin: 0 0 6px; font-size: 19px; }
.field { margin-bottom: 8px; }
.sheet-note { font-size: 13px; margin: -4px 2px 6px; }
.label { font-size: 13px; color: var(--hint); margin: 0 0 6px 2px; }
.seg { display: flex; gap: 4px; padding: 4px; border-radius: 12px; background: var(--bg2); }
.seg button {
  flex: 1; border: 0; border-radius: 9px; padding: 9px 4px;
  background: transparent; color: var(--text); font: inherit; font-weight: 600; cursor: pointer;
}
.seg button.on { background: var(--bg); box-shadow: 0 1px 4px rgba(0,0,0,.15); }
@keyframes fade { from { opacity: 0; } }
@keyframes up { from { transform: translateY(40px); opacity: 0; } }

.toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); z-index: 20;
  max-width: calc(100% - 32px);
  padding: 10px 16px; border-radius: 12px;
  background: rgba(20,20,24,.92); color: #fff; font-size: 14px; text-align: center;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
