/* Палитра берётся из темы Telegram, значения по умолчанию — на случай
   открытия в обычном браузере. */
:root {
  --bg: var(--tg-theme-bg-color, #17212b);
  --secondary-bg: var(--tg-theme-secondary-bg-color, #232e3c);
  --text: var(--tg-theme-text-color, #ffffff);
  --hint: var(--tg-theme-hint-color, #8a9aa9);
  --accent: var(--tg-theme-button-color, #3390ec);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --danger: #e5484d;
  --water: rgba(255, 255, 255, 0.06);
  --water-line: rgba(255, 255, 255, 0.12);
  --ship: #6d7d8c;
  --hit: #e5484d;
  --sunk: #8b1f22;
  --miss: rgba(255, 255, 255, 0.28);
  --radius: 12px;
}

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

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

body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
  padding: env(safe-area-inset-top) env(safe-area-inset-right)
           env(safe-area-inset-bottom) env(safe-area-inset-left);
}

h1 { font-size: 26px; margin: 8px 0 4px; }
h2 { font-size: 20px; margin: 8px 0 4px; }
.muted { color: var(--hint); margin: 4px 0; }

/* ------------------------------------------------------------- экраны */

.screen { display: none; padding: 16px; min-height: 100vh; }
.screen.active { display: block; }

.center {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  min-height: 82vh;
  text-align: center;
}

.center .btn { width: 100%; max-width: 320px; }

.logo { font-size: 64px; line-height: 1; }

/* ------------------------------------------------------------- контролы */

.btn {
  appearance: none;
  border: 0;
  border-radius: var(--radius);
  padding: 14px 18px;
  font-size: 16px;
  font-weight: 600;
  color: var(--text);
  background: var(--secondary-bg);
  cursor: pointer;
  transition: opacity .15s, transform .05s;
}
.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .45; cursor: default; }
.btn.primary { background: var(--accent); color: var(--accent-text); }
.btn.ghost { background: transparent; border: 1px solid var(--water-line); }
.btn.danger { color: var(--danger); border-color: rgba(229, 72, 77, .4); }

/* Полоса с главной кнопкой прилипает к низу экрана. Фон держит обёртка,
   а не сама кнопка: у выключенной кнопки opacity < 1, и подложка на ней
   просвечивала бы. */
.sticky-bar {
  position: sticky;
  bottom: 0;
  margin-top: 14px;
  padding: 12px 0 max(12px, env(safe-area-inset-bottom));
  background: var(--bg);
}
.sticky-bar .btn {
  width: 100%;
  box-shadow: 0 6px 20px rgba(0, 0, 0, .35);
}

.input {
  flex: 1;
  min-width: 0;
  background: var(--secondary-bg);
  border: 1px solid var(--water-line);
  border-radius: var(--radius);
  color: var(--text);
  padding: 14px 16px;
  font-size: 18px;
  font-weight: 700;
  letter-spacing: 3px;
  text-align: center;
  text-transform: uppercase;
}
.input:focus { outline: none; border-color: var(--accent); }

.join-row {
  display: flex;
  gap: 8px;
  width: 100%;
  max-width: 320px;
}
.join-row .btn { width: auto; flex: 0 0 auto; }

.divider {
  position: relative;
  width: 100%;
  max-width: 320px;
  text-align: center;
  margin: 6px 0;
  color: var(--hint);
  font-size: 13px;
}
.divider::before {
  content: '';
  position: absolute;
  left: 0; right: 0; top: 50%;
  border-top: 1px solid var(--water-line);
}
.divider span { position: relative; background: var(--bg); padding: 0 10px; }

.code-box {
  font-size: 34px;
  font-weight: 800;
  letter-spacing: 8px;
  padding: 14px 20px 14px 28px;
  background: var(--secondary-bg);
  border-radius: var(--radius);
  border: 1px dashed var(--water-line);
  margin: 6px 0 10px;
}

/* ------------------------------------------------------------- шапка */

.bar { margin-bottom: 12px; }
.bar-title { font-size: 19px; font-weight: 700; }
.bar-sub { font-size: 13px; color: var(--hint); margin-top: 4px; min-height: 18px; }
.bar.your-turn .bar-title { color: #4bb34b; }

.bar-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
}

.timer {
  font-size: 20px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: var(--hint);
}
.timer.urgent { color: var(--danger); }

.timer-track {
  height: 3px;
  border-radius: 2px;
  background: var(--water-line);
  overflow: hidden;
  margin-top: 6px;
}
.timer-fill {
  height: 100%;
  width: 100%;
  background: var(--accent);
  border-radius: 2px;
  transition: width .2s linear, background-color .3s;
}
.timer-fill.urgent { background: var(--danger); }

.skips {
  font-size: 12.5px;
  color: var(--danger);
  margin-top: 4px;
  min-height: 16px;
}

.hint {
  font-size: 12.5px;
  line-height: 1.5;
  color: var(--hint);
  text-align: center;
  margin: 10px auto 12px;
  max-width: 420px;
}
.hint b { color: var(--text); font-weight: 600; }

/* ------------------------------------------------------------- поле */

.board-section { margin-bottom: 14px; }

.board-label {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  font-size: 13px;
  color: var(--hint);
  margin-bottom: 6px;
}
.counter { font-variant-numeric: tabular-nums; }

.board-wrap { max-width: 420px; margin: 0 auto; }
.board-wrap.small { max-width: 280px; }

/* 11×11: первая строка/колонка — подписи координат */
.board {
  display: grid;
  grid-template-columns: 1.2em repeat(10, 1fr);
  grid-template-rows: 1.2em repeat(10, 1fr);
  gap: 2px;
}

.cell {
  aspect-ratio: 1;
  border-radius: 3px;
  background: var(--water);
  box-shadow: inset 0 0 0 1px var(--water-line);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  position: relative;
}

.cell.label {
  background: none;
  box-shadow: none;
  color: var(--hint);
  font-size: 10px;
}

.cell.ship { background: var(--ship); box-shadow: inset 0 0 0 1px rgba(255,255,255,.18); }

/* Поле расстановки рисуется пальцем — браузер не должен скроллить и зумить. */
#board-place { touch-action: none; user-select: none; -webkit-user-select: none; }

/* Корабль, который сейчас чертят или тащат */
.cell.preview {
  background: var(--accent);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .45);
}
.cell.invalid {
  background: rgba(229, 72, 77, .55);
  box-shadow: inset 0 0 0 1px rgba(229, 72, 77, .9);
}

/* Палец вынесен за доску — на отпускании корабль будет убран.
   Рамка вокруг поля, а не прозрачность: содержимое не перерисовывается. */
.board.removing {
  outline: 2px dashed rgba(229, 72, 77, .8);
  outline-offset: 7px;
}

.cell.miss::after {
  content: '';
  width: 22%;
  aspect-ratio: 1;
  border-radius: 50%;
  background: var(--miss);
}

.cell.hit { background: var(--hit); }
.cell.sunk { background: var(--sunk); }
.cell.hit::after,
.cell.sunk::after {
  content: '✕';
  font-size: 13px;
  font-weight: 700;
  color: #fff;
  opacity: .9;
}

/* Раскрытые в конце партии корабли противника */
.cell.revealed { background: rgba(109, 125, 140, .45); }

.board.enemy .cell:not(.label) { cursor: default; }
.board.enemy.active .cell:not(.label):not(.miss):not(.hit):not(.sunk) { cursor: pointer; }
.board.enemy.active .cell:not(.label):not(.miss):not(.hit):not(.sunk):active {
  background: rgba(51, 144, 236, .5);
}

.board.own .cell { font-size: 9px; }

/* ------------------------------------------------------------- док кораблей */

.dock {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  margin: 14px 0 10px;
}

/* Док — индикатор оставшихся кораблей, а не селектор: длину задаёт свайп. */
.dock-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-radius: var(--radius);
  background: var(--secondary-bg);
  border: 1px solid transparent;
}
.dock-item.next { border-color: var(--accent); background: rgba(51, 144, 236, .15); }
.dock-item.done { opacity: .3; }

.dock-ship { display: flex; gap: 2px; }
.dock-ship i {
  width: 10px;
  height: 10px;
  border-radius: 2px;
  background: var(--ship);
  display: block;
}
.dock-count { font-size: 13px; font-weight: 700; color: var(--hint); }

.actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: center;
}
.actions .btn { flex: 1 1 auto; padding: 11px 12px; font-size: 14px; }

/* ------------------------------------------------------------- журнал */

.log {
  margin-top: 12px;
  max-height: 130px;
  overflow-y: auto;
  font-size: 13px;
  color: var(--hint);
  background: var(--secondary-bg);
  border-radius: var(--radius);
  padding: 10px 12px;
}
.log div { padding: 2px 0; }
.log div:last-child { color: var(--text); }

/* ------------------------------------------------------------- прочее */

.spinner {
  width: 28px;
  height: 28px;
  border: 3px solid var(--water-line);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.toast {
  position: fixed;
  left: 50%;
  top: 16px;
  transform: translate(-50%, -150%);
  background: rgba(0, 0, 0, .85);
  color: #fff;
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 14px;
  max-width: 88vw;
  text-align: center;
  z-index: 100;
  transition: transform .25s ease;
  pointer-events: none;
}
.toast.show { transform: translate(-50%, 0); }

.offline { color: var(--danger); }
