:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --bg2: var(--tg-theme-secondary-bg-color, #f2f3f5);
  --text: var(--tg-theme-text-color, #15171a);
  --hint: var(--tg-theme-hint-color, #8a9099);
  --accent: var(--tg-theme-button-color, #2f80ed);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --link: var(--tg-theme-link-color, #2f80ed);
  --danger: #e5484d;
  --ok: #30a46c;
  --radius: 14px;
  --sq-light: #eedbb5;
  --sq-dark: #b6875c;
  --sea: #dbeefe;
  --sea-dark: #1f3a52;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text);
  font: 15px/1.35 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; }
body { overscroll-behavior: none; user-select: none; -webkit-user-select: none; }
#app { max-width: 560px; margin: 0 auto; min-height: 100%; }
.screen { display: none; padding: 12px 16px calc(24px + env(safe-area-inset-bottom)); animation: fade .22s ease; }
.screen.active { display: block; }
@keyframes fade { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

/* шапка */
.top { display: flex; align-items: center; justify-content: space-between; margin: 4px 0 8px; }
.me { display: flex; align-items: center; gap: 12px; }
.avatar { width: 46px; height: 46px; border-radius: 50%; background: var(--bg2); display: grid; place-items: center;
  font-size: 24px; overflow: hidden; }
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.me-name { font-weight: 700; font-size: 17px; }
.me-sub { color: var(--hint); font-size: 13px; }
.icon-btn { border: 0; background: var(--bg2); color: var(--text); width: 42px; height: 42px; border-radius: 12px; font-size: 20px; }
.icon-btn.small { width: 34px; height: 34px; font-size: 16px; font-weight: 700; }
.section-title { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--hint); margin: 18px 2px 8px; }
.grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.tile { background: var(--bg2); border-radius: var(--radius); padding: 14px 12px; border: 0; text-align: left; color: var(--text);
  display: flex; flex-direction: column; gap: 4px; min-height: 96px; transition: transform .12s ease; }
.tile:active { transform: scale(.97); }
.tile .em { font-size: 30px; line-height: 1; }
.tile .nm { font-weight: 700; font-size: 15px; margin-top: 6px; }
.tile .ds { color: var(--hint); font-size: 12px; }
.foot { color: var(--hint); font-size: 12px; text-align: center; margin-top: 22px; }

/* панели */
.bar { display: flex; align-items: center; gap: 8px; margin: 0 0 10px; min-height: 42px; }
.bar h1 { font-size: 18px; margin: 0; flex: 1; }
.back { border: 0; background: none; color: var(--link); font-size: 30px; line-height: 1; padding: 0 8px 4px 0; }
.card { background: var(--bg2); border-radius: var(--radius); padding: 14px; margin: 10px 0; display: flex; flex-direction: column; gap: 10px; }
.card-title { font-weight: 700; }
.btn { border: 0; border-radius: 12px; padding: 13px 14px; font-size: 15px; font-weight: 600; background: var(--bg); color: var(--text); }
.btn.primary { background: var(--accent); color: var(--accent-text); }
.btn:active { opacity: .85; }
.btn[disabled] { opacity: .45; }
.hint { color: var(--hint); font-size: 12px; }
.seg { display: flex; background: var(--bg); border-radius: 12px; padding: 3px; gap: 3px; }
.seg button { flex: 1; border: 0; background: none; padding: 9px 4px; border-radius: 10px; color: var(--text); font-weight: 600; font-size: 13px; }
.seg button.on { background: var(--accent); color: var(--accent-text); }
#top .seg { background: var(--bg2); margin-bottom: 10px; overflow-x: auto; }
.mode-hero { font-size: 56px; text-align: center; padding: 6px 0 2px; }

/* ожидание */
.wait-box { display: flex; flex-direction: column; align-items: center; gap: 18px; padding-top: 60px; text-align: center; }
.spinner { width: 46px; height: 46px; border: 4px solid var(--bg2); border-top-color: var(--accent); border-radius: 50%; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.wait-text { color: var(--hint); max-width: 280px; }

/* игра */
.players { display: flex; justify-content: space-between; gap: 8px; margin-bottom: 6px; }
.pl { flex: 1; background: var(--bg2); border-radius: 12px; padding: 8px 10px; display: flex; align-items: center; gap: 8px;
  border: 2px solid transparent; transition: border-color .2s ease; min-width: 0; }
.pl.turn { border-color: var(--accent); }
.pl .dot { width: 14px; height: 14px; border-radius: 50%; flex: none; border: 1px solid rgba(0,0,0,.25); }
.pl .nm { font-weight: 600; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.status { text-align: center; color: var(--hint); font-size: 14px; min-height: 22px; margin: 4px 0 8px; }
.status.win { color: var(--ok); font-weight: 700; }
.status.lose { color: var(--danger); font-weight: 700; }
.stage { display: flex; flex-direction: column; align-items: center; gap: 12px; }
.controls { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 14px; }
.controls .btn { flex: 1 1 40%; padding: 11px; font-size: 14px; background: var(--bg2); }
.controls .btn.primary { background: var(--accent); }

/* доски 8×8 */
.board { display: grid; width: min(100%, 440px); aspect-ratio: 1; border-radius: 10px; overflow: hidden;
  box-shadow: 0 2px 10px rgba(0,0,0,.12); touch-action: manipulation; }
.board.b8 { grid-template-columns: repeat(8, 1fr); }
.sq { position: relative; display: grid; place-items: center; }
.sq.l { background: var(--sq-light); }
.sq.d { background: var(--sq-dark); }
.sq.sel { box-shadow: inset 0 0 0 3px rgba(255, 225, 0, .95); }
.sq.last::before { content: ""; position: absolute; inset: 0; background: rgba(255, 230, 80, .38); }
.sq.check::before { content: ""; position: absolute; inset: 0; background: radial-gradient(circle, rgba(229,72,77,.85) 20%, transparent 70%); }
.sq .mv { position: absolute; width: 30%; height: 30%; border-radius: 50%; background: rgba(20, 20, 20, .28); z-index: 2; }
.sq .mv.cap { width: 86%; height: 86%; background: none; border: 4px solid rgba(20,20,20,.28); }
.coord { position: absolute; font-size: 9px; font-weight: 700; opacity: .65; color: #3a2a1a; }
.coord.f { right: 3px; bottom: 1px; } .coord.r { left: 3px; top: 1px; }
.pc { position: relative; z-index: 3; font-size: min(10vw, 46px); line-height: 1;
  font-family: "Segoe UI Symbol", "Noto Sans Symbols2", "DejaVu Sans", "Apple Symbols", serif; transition: transform .15s ease; }
.pc.w { color: #fff; text-shadow: 0 0 2px #000, 0 0 1px #000, 1px 1px 0 #222, -1px -1px 0 #222, 1px -1px 0 #222, -1px 1px 0 #222; }
.pc.b { color: #1d1d1d; text-shadow: 0 0 1px #fff8; }
.disc { position: relative; z-index: 3; width: 78%; height: 78%; border-radius: 50%; display: grid; place-items: center;
  box-shadow: inset 0 -4px 0 rgba(0,0,0,.25), 0 2px 4px rgba(0,0,0,.35); transition: transform .2s ease, background .25s ease; }
.disc.w { background: radial-gradient(circle at 35% 30%, #fff, #d9d4c7); }
.disc.b { background: radial-gradient(circle at 35% 30%, #5b5b5b, #151515); }
.disc .crown { font-size: min(5vw, 22px); filter: drop-shadow(0 1px 0 rgba(0,0,0,.5)); }
.board.reversi .sq { background: #2e8b57; box-shadow: inset 0 0 0 1px rgba(0,0,0,.25); }
.board.reversi .sq .mv { background: rgba(0,0,0,.25); width: 22%; height: 22%; }

/* крестики-нолики */
.ttt { display: grid; grid-template-columns: repeat(3, 1fr); width: min(100%, 330px); aspect-ratio: 1; gap: 8px; }
.ttt .cell { background: var(--bg2); border-radius: 14px; display: grid; place-items: center; font-size: min(18vw, 74px); font-weight: 800; }
.ttt .cell.x { color: var(--accent); } .ttt .cell.o { color: var(--danger); }
.ttt .cell.winc { background: color-mix(in srgb, var(--ok) 25%, var(--bg2)); }
.pop { animation: pop .22s ease; }
@keyframes pop { from { transform: scale(.4); opacity: 0; } to { transform: none; opacity: 1; } }

/* 4 в ряд */
.c4 { display: grid; grid-template-columns: repeat(7, 1fr); gap: 5px; width: min(100%, 420px); background: #2457c5; padding: 8px;
  border-radius: 14px; box-shadow: 0 3px 10px rgba(0,0,0,.2); }
.c4 .hole { aspect-ratio: 1; border-radius: 50%; background: var(--bg); box-shadow: inset 0 2px 4px rgba(0,0,0,.3); position: relative; }
.c4 .hole.w::after, .c4 .hole.b::after { content: ""; position: absolute; inset: 6%; border-radius: 50%; animation: drop .28s ease-in; }
.c4 .hole.w::after { background: radial-gradient(circle at 35% 30%, #ffe680, #f2b705); }
.c4 .hole.b::after { background: radial-gradient(circle at 35% 30%, #ff8a8a, #d62828); }
.c4 .hole.winc { box-shadow: 0 0 0 3px #fff, inset 0 2px 4px rgba(0,0,0,.3); }
.c4 .hole.static::after { animation: none; }
@keyframes drop { from { transform: translateY(-260%); } to { transform: none; } }

/* морской бой */
.sea-wrap { width: min(100%, 420px); }
.sea-label { font-size: 13px; font-weight: 700; color: var(--hint); margin: 2px 2px 4px; }
.sea { display: grid; grid-template-columns: repeat(10, 1fr); gap: 2px; background: var(--sea-dark); padding: 3px; border-radius: 10px; }
.sea.small { width: 72%; margin: 0 auto; }
.sea .c { aspect-ratio: 1; background: var(--sea); border-radius: 3px; position: relative; display: grid; place-items: center;
  font-size: min(3.6vw, 16px); transition: background .2s ease; }
.sea .c.ship { background: #6b7c8f; }
.sea .c.miss::after { content: ""; width: 26%; height: 26%; border-radius: 50%; background: #4f6d8a; }
.sea .c.hit { background: #f5a524; }
.sea .c.hit::after { content: "✕"; color: #7a1d00; font-weight: 800; }
.sea .c.sunk { background: var(--danger); }
.sea .c.sunk::after { content: "✕"; color: #fff; font-weight: 800; }
.sea.target .c:not(.miss):not(.hit):not(.sunk):active { background: #b6dcff; }

/* одиночные */
.g2048 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; background: #bbada0; padding: 8px; border-radius: 12px;
  width: min(100%, 380px); aspect-ratio: 1; grid-template-rows: repeat(4, 1fr); touch-action: none; }
.g2048 .t { border-radius: 8px; background: rgba(238,228,218,.35); display: grid; min-height: 0; aspect-ratio: 1; place-items: center; font-weight: 800;
  font-size: min(8vw, 34px); color: #776e65; transition: background .12s ease; }
.mines { display: grid; gap: 2px; background: #9aa5b1; padding: 3px; border-radius: 10px; width: min(100%, 420px); }
.mines .m { aspect-ratio: 1; background: #d8dee6; border-radius: 3px; display: grid; place-items: center; font-weight: 800; font-size: min(4.2vw, 18px); }
.mines .m.open { background: #f4f6f8; }
.mines .m.boom { background: var(--danger); }
.memory { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; width: min(100%, 380px); }
.memory .card2 { aspect-ratio: 1; border-radius: 12px; background: var(--accent); display: grid; place-items: center; font-size: min(10vw, 40px);
  transition: transform .3s ease, background .3s ease; }
.memory .card2.open, .memory .card2.done { background: var(--bg2); transform: rotateY(180deg); }
.memory .card2 span { transform: rotateY(180deg); }
.memory .card2.done { opacity: .55; }
.score-row { display: flex; gap: 10px; justify-content: center; width: 100%; }
.score { background: var(--bg2); border-radius: 10px; padding: 6px 14px; text-align: center; min-width: 90px; }
.score b { display: block; font-size: 18px; }
.score span { color: var(--hint); font-size: 11px; text-transform: uppercase; }

/* рейтинг */
.toplist { list-style: none; padding: 0; margin: 0; }
.toplist li { display: flex; align-items: center; gap: 10px; background: var(--bg2); border-radius: 12px; padding: 10px 12px; margin-bottom: 6px; }
.toplist .pos { width: 26px; font-weight: 800; color: var(--hint); }
.toplist .nm { flex: 1; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.toplist li:only-child .nm { white-space: normal; }
.toplist .pts { font-weight: 700; }
.toplist li.me { outline: 2px solid var(--accent); }

/* окно */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.45); display: grid; place-items: center; padding: 24px; z-index: 50; animation: fade .2s ease; }
.modal[hidden] { display: none; }
.modal-box { background: var(--bg); border-radius: 18px; padding: 20px; width: min(100%, 360px); text-align: center; }
.modal-title { font-size: 20px; font-weight: 800; margin-bottom: 6px; }
.modal-text { color: var(--hint); margin-bottom: 16px; white-space: pre-line; text-align: left; font-size: 14px; }
.modal-actions { display: flex; flex-direction: column; gap: 8px; }
.modal-actions .btn { background: var(--bg2); }
.modal-actions .btn.primary { background: var(--accent); }
.promo { display: flex; gap: 8px; justify-content: center; }
.promo button { font-size: 34px; width: 58px; height: 58px; border-radius: 12px; border: 0; background: var(--bg2); }
.toast { position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translate(-50%, 20px); opacity: 0;
  background: rgba(20,20,22,.88); color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 14px; max-width: 86%;
  text-align: center; transition: opacity .25s ease, transform .25s ease; z-index: 60; pointer-events: none; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---- Ровные клетки на iPhone: размеры задаём явно, а не через aspect-ratio контейнера ---- */
:root { --bw: min(calc(100vw - 32px), 440px); }
.board { width: var(--bw); height: var(--bw); aspect-ratio: auto; }
.board.b8 { grid-template-columns: repeat(8, minmax(0, 1fr)); grid-template-rows: repeat(8, minmax(0, 1fr)); }
.sq { min-width: 0; min-height: 0; overflow: hidden; }
.ttt { --tw: min(calc(100vw - 32px), 330px); width: var(--tw); height: var(--tw); aspect-ratio: auto;
  grid-template-columns: repeat(3, minmax(0, 1fr)); grid-template-rows: repeat(3, minmax(0, 1fr)); }
.ttt .cell { min-width: 0; min-height: 0; overflow: hidden; }
.g2048 { --gw: min(calc(100vw - 32px), 380px); width: var(--gw); height: var(--gw); aspect-ratio: auto;
  grid-template-columns: repeat(4, minmax(0, 1fr)); grid-template-rows: repeat(4, minmax(0, 1fr)); }
.g2048 .t { aspect-ratio: auto; min-width: 0; min-height: 0; overflow: hidden; }
.c4 { grid-template-columns: repeat(7, minmax(0, 1fr)); grid-auto-rows: 1fr; }
.sea { grid-template-columns: repeat(10, minmax(0, 1fr)); grid-auto-rows: 1fr; }
.mines, .memory { grid-auto-rows: 1fr; }
.memory { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.c4 .hole, .sea .c, .mines .m, .memory .card2 { min-width: 0; min-height: 0; }
