/* X vs O — оформление версии 4.
   Только текст: никаких изображений, SVG, canvas, иконок, эмодзи и внешних шрифтов.
   Главное требование вёрстки: игровое поле всегда целиком видно без прокрутки,
   поэтому корневой контейнер занимает ровно высоту экрана (100svh),
   а поле масштабируется под свободное место. */

:root {
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;

  --bg: #0e1116;
  --bg-soft: #161b22;
  --bg-cell: #1b222c;
  --bg-cell-hover: #253041;
  --line: #2b333f;
  --line-strong: #3d4855;
  --text: #e6edf3;
  --text-dim: #9aa7b4;
  --text-faint: #6e7d8c;
  --x: #6cb6ff;
  --o: #ff9b6c;
  --blocked: #55606d;
  --win-bg: #1f3a26;
  --win-line: #4ec26a;
  --danger: #ff7b72;
  --ok: #56d364;

  --radius: 8px;
  --gap: clamp(6px, 1.2vh, 14px);
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f5f7fa;
    --bg-soft: #ffffff;
    --bg-cell: #ffffff;
    --bg-cell-hover: #e8eef6;
    --line: #d3dae2;
    --line-strong: #b6c0cb;
    --text: #10161d;
    --text-dim: #4c5866;
    --text-faint: #76828f;
    --x: #0a5fbd;
    --o: #b8480f;
    --blocked: #98a3af;
    --win-bg: #dcf3e2;
    --win-line: #1f8f3d;
    --danger: #b42318;
    --ok: #17803d;
  }
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  padding: 0;
}

body {
  background: var(--bg);
  max-width: 100%;
  overflow-x: hidden;              /* никакого горизонтального сдвига на телефоне */
  color: var(--text);
  font-family: var(--font-mono);
  font-size: clamp(13px, 1.7vh, 16px);
  line-height: 1.4;
  overflow: hidden;              /* прокрутки страницы нет по условию */
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
}

.app {
  height: 100svh;
  max-width: 100%;
  display: flex;
  flex-direction: column;
  /* Заголовок и основной блок — одна группа, центрованная по экрану. */
  justify-content: center;
  gap: var(--gap);
  padding: max(var(--gap), env(safe-area-inset-top)) var(--gap) max(var(--gap), env(safe-area-inset-bottom));
}

/* --- Название игры ---------------------------------------------------------
   Требование: центр «vs» должен лежать на одной горизонтали с центрами букв
   X и O. Буквы X и O — прописные: их «чернильный» центр находится на высоте
   0.5cap над базовой линией. У строчного «vs» центр — на 0.5ex над базовой
   линией. Выравниваем по базовой линии и приподнимаем «vs» на разницу
   (0.5cap − 0.5ex). Единицы cap/ex берутся из метрик самого шрифта,
   поэтому центровка не зависит от гарнитуры и размера. */
.logo {
  margin: 0;
  flex: none;
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 0.16em;
  font-size: clamp(20px, 4.4vh, 40px);
  font-weight: 700;
  letter-spacing: 0.02em;
  user-select: none;
}

.logo-x { color: var(--x); }
.logo-o { color: var(--o); }

.logo-vs {
  font-size: 0.5em;
  font-weight: 500;
  color: var(--text-dim);
  /* Центр «vs» лежит на 0.5ex выше общей базовой линии, а центр X и O —
     на половине высоты их прописных букв. Так как font-size «vs» ровно
     вдвое меньше, половина cap букв X и O равна ровно 1cap в единицах
     самого «vs»: поднимаем «vs» на разницу этих величин. */
  transform: translateY(calc(0.5ex - 1cap));
}

/* Если браузер не знает единицу cap — приближение по em (cap ≈ 0.66em). */
@supports not (height: 1cap) {
  .logo-vs { transform: translateY(calc(0.5ex - 0.66em)); }
}

/* --- Экраны ---------------------------------------------------------------- */
.screen[hidden] { display: none; }

.screen {
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: var(--gap);
}

/* --- Лобби ----------------------------------------------------------------
   Лобби занимает ровно свою высоту: вместе с названием игры оно образует один
   центрованный блок, а не «оторванный» заголовок сверху. */
.screen-lobby {
  flex: 0 1 auto;
  overflow-y: auto;
  justify-content: center;
}

.screen-game {
  /* Игровой экран занимает ровно свою высоту: заголовок, поле и подписи
     образуют один плотный блок, который .app центрует по экрану. */
  flex: 0 1 auto;
  justify-content: center;
}

.lobby {
  width: 100%;
  max-width: 46rem;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: var(--gap);
}

.field, .panel {
  background: var(--bg-soft);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: clamp(10px, 1.8vh, 18px);
  display: flex;
  flex-direction: column;
  gap: 0.5em;
}

.panels {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(15rem, 1fr));
  gap: var(--gap);
}

.panel-title {
  margin: 0;
  font-size: 1em;
  font-weight: 700;
  color: var(--text);
}

.label {
  color: var(--text-dim);
  font-size: 0.9em;
}

.hint {
  margin: 0;
  color: var(--text-faint);
  font-size: 0.82em;
}

.input {
  font: inherit;
  color: var(--text);
  background: var(--bg-cell);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  padding: 0.6em 0.7em;
  width: 100%;
}

.input:focus-visible,
.btn:focus-visible,
.range:focus-visible,
.cell:focus-visible {
  outline: 2px solid var(--x);
  outline-offset: 2px;
}

.input-code {
  letter-spacing: 0.22em;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

.range {
  width: 100%;
  accent-color: var(--x);
  margin: 0;
}

output { font-weight: 700; color: var(--text); }

.btn {
  font: inherit;
  color: var(--text);
  background: var(--bg-cell);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  padding: 0.6em 1em;
  cursor: pointer;
  transition: background-color 0.15s ease, border-color 0.15s ease;
}

.btn:hover:not(:disabled) { background: var(--bg-cell-hover); }
.btn:disabled { opacity: 0.5; cursor: default; }

.btn-primary {
  border-color: var(--x);
  color: var(--x);
  font-weight: 700;
}

.btn-small { padding: 0.4em 0.8em; font-size: 0.9em; }

/* --- Игровое поле ---------------------------------------------------------
   Сторона поля считается на клиенте (--board-side): это минимум из ширины
   экрана и свободной высоты между названием игры и блоком описаний. Благодаря
   этому заголовок, поле и подписи образуют один плотный центрованный блок,
   без пустых провалов, и поле занимает всю ширину телефона. Если JS почему-то
   не успел посчитать размер, работает запасное значение min(96vw, 58vh). */
.board-area {
  flex: 0 1 auto;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.board {
  --cells: 15;
  width: var(--board-side, min(96vw, 58vh));
  height: var(--board-side, min(96vw, 58vh));
  max-width: 100%;
  aspect-ratio: 1 / 1;
  display: grid;
  grid-template-columns: repeat(var(--cells), 1fr);
  grid-template-rows: repeat(var(--cells), 1fr);
  gap: clamp(1px, 0.4vmin, 3px);
  font-size: calc(var(--board-side, min(96vw, 58vh)) / (var(--cells) * 1.42));
  touch-action: manipulation;
}

.cell {
  font: inherit;
  font-size: 0.95em;
  line-height: 1;
  color: var(--text);
  background: var(--bg-cell);
  border: 1px solid var(--line);
  border-radius: clamp(2px, 0.5vmin, 6px);
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: default;
  overflow: hidden;
  transition: background-color 0.12s ease, box-shadow 0.12s ease;
}

.cell.is-x { color: var(--x); }
.cell.is-o { color: var(--o); }
.cell.is-blocked { color: var(--blocked); background: transparent; border-style: dashed; }

/* Предыдущий ход соперника (и вообще последний ход в партии) — жирнее и в рамке. */
.cell.last-move {
  font-weight: 800;
  box-shadow: inset 0 0 0 2px currentColor;
}

.cell.win {
  background: var(--win-bg);
  border-color: var(--win-line);
  font-weight: 800;
}

/* Клетки становятся кликабельными только когда ход действительно наш. */
.board.my-turn .cell.free { cursor: pointer; }
.board.my-turn .cell.free:hover { background: var(--bg-cell-hover); }
.board.my-turn { box-shadow: 0 0 0 2px var(--line-strong); border-radius: var(--radius); }

/* --- Блок описаний под полем --------------------------------------------- */
.under {
  flex: none;
  display: flex;
  flex-direction: column;
  gap: 0.35em;
  align-items: center;
  text-align: center;
}

.status {
  margin: 0;
  font-weight: 700;
  font-size: clamp(13px, 1.9vh, 17px);
  min-height: 1.4em;
}

.status.win { color: var(--ok); }
.status.lose { color: var(--danger); }

.players {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.8em;
  flex-wrap: wrap;
  font-size: 0.9em;
}

.player { display: flex; align-items: center; gap: 0.35em; }
.player.active { text-shadow: 0 0 0.6em currentColor; }
.player.you .player-name { text-decoration: underline; text-underline-offset: 0.2em; }

.player-sym { font-weight: 800; }
.sym-x { color: var(--x); }
.sym-o { color: var(--o); }

.player-name {
  max-width: 11em;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.score {
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0.05em 0.5em;
}

.dot {
  width: 0.55em;
  height: 0.55em;
  border-radius: 50%;
  background: var(--text-faint);
  flex: none;
}

.dot.online { background: var(--ok); }
.dot.offline { background: var(--danger); }

.meta {
  margin: 0;
  display: flex;
  gap: 0.9em;
  flex-wrap: wrap;
  justify-content: center;
  color: var(--text-dim);
  font-size: 0.82em;
}

.code {
  color: var(--text);
  font-weight: 700;
  letter-spacing: 0.12em;
}

.actions {
  display: flex;
  gap: 0.5em;
  flex-wrap: wrap;
  justify-content: center;
}

.share-fallback {
  width: min(100%, 32rem);
  min-width: 0;
}

.share-fallback[hidden] { display: none; }

.error-line {
  margin: 0;
  flex: none;
  text-align: center;
  color: var(--danger);
  font-size: 0.88em;
  min-height: 1.2em;
}

.error-line[hidden] { display: none; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

/* Низкие экраны: прячем часть подписей, чтобы поле осталось крупным. */
@media (max-height: 520px) {
  .meta { font-size: 0.75em; gap: 0.6em; }
  .logo { font-size: clamp(18px, 3.6vh, 28px); }
}

/* --- Телефон в вертикальном положении -------------------------------------
   Поле должно занимать всю доступную ширину экрана, чтобы игра читалась без
   зума. Поэтому отступы и подписи по краям сжимаются, а поле — нет.
   На компьютере эти правила не действуют. */
@media (max-width: 560px) {
  :root { --gap: 5px; }

  body { font-size: clamp(12px, 3.4vw, 15px); }

  .app {
    padding: max(4px, env(safe-area-inset-top)) 3px max(4px, env(safe-area-inset-bottom));
  }

  .logo { font-size: clamp(19px, 6vw, 30px); }

  .board { gap: 1px; }
  .cell { border-radius: 2px; }

  .status { font-size: clamp(12px, 3.6vw, 16px); }
  .players { gap: 0.5em; }
  .player-name { max-width: 6.5em; }
  .meta { gap: 0.5em; font-size: 0.76em; }
  .under { gap: 0.25em; }
  .field, .panel { padding: 10px; }
  .panels { grid-template-columns: 1fr; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
