/* Морской бой — тетрадный лист в клетку, шариковая ручка и красная учительская паста. */

/* Шрифты лежат рядом (лицензия OFL, см. fonts/), чтобы игра работала и без интернета. */
@font-face {
  font-family: 'Caveat';
  font-style: normal;
  font-weight: 400 700;
  font-display: swap;
  src: url(fonts/caveat-cyrillic.woff2) format('woff2');
  unicode-range: U+0301, U+0400-045F, U+0490-0491, U+04B0-04B1, U+2116;
}
@font-face {
  font-family: 'Caveat';
  font-style: normal;
  font-weight: 400 700;
  font-display: swap;
  src: url(fonts/caveat-latin.woff2) format('woff2');
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
@font-face {
  font-family: 'Neucha';
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: url(fonts/neucha-cyrillic.woff2) format('woff2');
  unicode-range: U+0301, U+0400-045F, U+0490-0491, U+04B0-04B1, U+2116;
}
@font-face {
  font-family: 'Neucha';
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: url(fonts/neucha-latin.woff2) format('woff2');
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}

:root {
  --paper: #fafaf6;
  --grid: #c6daee;        /* клетка тетради */
  --grid-field: #a9c3e3;  /* клетка игрового поля — чуть темнее */
  --ink: #2a3a9c;         /* синяя шариковая ручка */
  --ink-soft: rgba(42, 58, 156, 0.5);
  --red: #d2372b;         /* красная паста */
  --margin: #e9a3a3;      /* поля тетради */
  --pencil: #6f7179;      /* карандаш */
  --marker: #fff07a;      /* жёлтый маркер */

  --pitch: 24px;          /* шаг клетки тетради */
  --cell-base: clamp(26px, calc((100vw - 56px) / 11), 34px);
  --margin-x: clamp(12px, 4vw, 60px);

  --hand: 'Caveat', 'Segoe Print', 'Bradley Hand', cursive;
  --text: 'Neucha', 'Caveat', 'Segoe Print', 'Comic Sans MS', cursive;

  color-scheme: light;
}

* { box-sizing: border-box; }

html { background: var(--paper); }

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  font-family: var(--text);
  font-size: 19px;
  line-height: 1.35;
  background-color: var(--paper);
  background-image:
    linear-gradient(var(--grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size: var(--pitch) var(--pitch);
  -webkit-tap-highlight-color: transparent;
}

/* красная линия полей */
body::before {
  content: '';
  position: fixed;
  top: 0;
  bottom: 0;
  left: calc(var(--margin-x) + env(safe-area-inset-left));
  width: 2px;
  background: var(--margin);
  pointer-events: none;
}

[hidden] { display: none !important; }

.sheet {
  max-width: 1120px;
  margin: 0 auto;
  padding:
    calc(12px + env(safe-area-inset-top)) calc(16px + env(safe-area-inset-right))
    calc(64px + env(safe-area-inset-bottom)) calc(var(--margin-x) + 20px + env(safe-area-inset-left));
}

/* ---------- Заголовок ---------- */

.masthead { padding: 8px 0 4px; }

.title {
  display: inline-block;
  margin: 0;
  font-family: var(--hand);
  font-weight: 700;
  font-size: clamp(48px, 9vw, 76px);
  line-height: 1;
  letter-spacing: 0.5px;
  transform: rotate(-1.5deg);
  transform-origin: left center;
  padding-bottom: 10px;
  background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 300 12' preserveAspectRatio='none'%3E%3Cpath d='M2 7c25-5 45 4 70-1s45-4 72 0 48-5 74-1 50 4 80-2' fill='none' stroke='%23d2372b' stroke-width='2.4' stroke-linecap='round'/%3E%3C/svg%3E") bottom left / 100% 12px no-repeat;
}

/* ---------- Кнопки и поля ввода ---------- */

.btn {
  font-family: var(--hand);
  font-weight: 700;
  font-size: 26px;
  line-height: 1.1;
  color: var(--ink);
  background: transparent;
  border: 2.5px solid var(--ink);
  border-radius: 255px 15px 225px 15px / 15px 225px 15px 255px;
  padding: 6px 20px 8px;
  cursor: pointer;
  transition: transform 0.12s ease, background-color 0.12s ease;
}
.btn:hover:not(:disabled) { background-color: var(--marker); transform: rotate(-1deg); }
.btn:active:not(:disabled) { transform: scale(0.97); }
.btn:disabled { opacity: 0.35; cursor: not-allowed; }
.btn.primary { background-color: var(--marker); border-color: var(--ink); }
.btn.primary:hover:not(:disabled) { background-color: #ffe94a; }
.btn.small { font-size: 21px; padding: 3px 14px 5px; border-width: 2px; }

.btn:focus-visible, .link:focus-visible, input:focus-visible {
  outline: 2px dashed var(--red);
  outline-offset: 4px;
}

.link {
  font: inherit;
  font-size: 18px;
  color: var(--pencil);
  background: none;
  border: none;
  padding: 4px 0;
  cursor: pointer;
}
.link:hover { color: var(--ink); text-decoration: underline wavy var(--red) 1px; }

input[type='text'] {
  font-family: var(--hand);
  font-size: 30px;
  font-weight: 700;
  color: var(--ink);
  background: transparent;
  border: none;
  border-bottom: 2px solid var(--ink);
  border-radius: 0;
  padding: 0 4px;
  width: 100%;
  outline: none;
}
input[type='text']::placeholder { color: var(--ink-soft); opacity: 0.6; }

/* ---------- Меню ---------- */

.menu { max-width: 560px; padding-top: 28px; }

.invite-note {
  margin: 0 0 20px;
  font-size: 21px;
  padding: 8px 12px;
  border-left: 4px solid var(--red);
  background: rgba(255, 240, 122, 0.55);
}

.field-line { display: block; margin-bottom: 28px; }
.field-label { display: block; color: var(--pencil); font-size: 18px; margin-bottom: 2px; }
.field-line input { max-width: 320px; }

.check {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  margin: -6px 0 28px;
  max-width: 540px;
  font-size: 19px;
  cursor: pointer;
}
.check input {
  appearance: none;
  -webkit-appearance: none;
  flex: none;
  width: 24px;
  height: 24px;
  margin: 0;
  border: 2.5px solid var(--ink);
  border-radius: 3px 6px 3px 5px / 5px 3px 6px 3px;
  background: var(--paper) center / 20px no-repeat;
  cursor: pointer;
}
.check input:checked {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20'%3E%3Cpath d='M3 10.5l4.5 5L17 3.5' fill='none' stroke='%232a3a9c' stroke-width='3' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
}

.menu-actions { display: flex; flex-wrap: wrap; gap: 14px 18px; margin-bottom: 22px; }

.resume { display: flex; flex-direction: column; align-items: flex-start; gap: 12px; margin: 0 0 30px; }

.menu-block { margin: 0 0 34px; }
.menu-h {
  margin: 0 0 12px;
  font-family: var(--hand);
  font-weight: 700;
  font-size: 30px;
  line-height: 1.1;
}

/* уровень компьютера — три «галочки» от руки */
.levels { display: flex; flex-wrap: wrap; gap: 8px 10px; margin: 0 0 16px; border: 0; padding: 0; }
.level { position: relative; cursor: pointer; }
.level input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.level span {
  display: inline-block;
  padding: 2px 14px 4px;
  font-family: var(--hand);
  font-weight: 700;
  font-size: 23px;
  border: 2px dashed transparent;
  border-radius: 255px 15px 225px 15px / 15px 225px 15px 255px;
  color: var(--pencil);
}
.level:hover span { color: var(--ink); }
.level input:checked + span { color: var(--ink); border: 2px solid var(--ink); background: var(--marker); }
.level input:focus-visible + span { outline: 2px dashed var(--red); outline-offset: 3px; }

.score { margin: -8px 0 0; color: var(--pencil); font-size: 18px; }
.install-hint { margin: 18px 0 0; font-size: 17px; }

.join-form { display: flex; align-items: flex-end; gap: 16px; margin-top: 4px; }
.join-form .field-line { margin: 0; flex: 0 1 220px; }
.join-form input { text-transform: uppercase; letter-spacing: 3px; }

.rules {
  margin-top: 40px;
  color: var(--pencil);
  font-size: 18px;
}
.rules h2 { font-family: var(--hand); font-size: 30px; margin: 0 0 4px; color: var(--ink); }
.rules ul { margin: 0; padding-left: 20px; }
.rules li { margin: 2px 0; }

/* ---------- Экран игры ---------- */

.topbar { display: flex; align-items: center; flex-wrap: wrap; gap: 4px 18px; min-height: 32px; }
.topbar .resign { margin-left: auto; color: var(--red); }
.topbar .resign:hover { color: var(--red); }
.link-badge { animation: blink 1.6s ease-in-out infinite; }
@keyframes blink { 50% { opacity: 0.35; } }
.room-tag { color: var(--pencil); font-size: 18px; }
.room-tag b { color: var(--ink); letter-spacing: 2px; }

.status {
  margin: 10px 0 0;
  font-family: var(--hand);
  font-weight: 700;
  font-size: clamp(36px, 6vw, 50px);
  line-height: 1.1;
}
.status span {
  background-image: linear-gradient(100deg, transparent 0.2em, var(--marker) 0.25em, var(--marker) calc(100% - 0.1em), transparent);
  background-repeat: no-repeat;
  background-position: 0 72%;
  background-size: 0% 52%;
  padding: 0 0.12em;
}
.status.go span { background-size: 100% 52%; transition: background-size 0.45s cubic-bezier(0.3, 0.7, 0.2, 1); }
.status.win { color: var(--red); transform: rotate(-2deg); transform-origin: left; }
.status.win span { text-decoration: underline double var(--red) 2px; text-underline-offset: 8px; }
.status.lose { color: var(--pencil); }

.substatus { margin: 4px 0 0; color: var(--pencil); font-size: 19px; min-height: 1.35em; max-width: 720px; display: grid; }
/* видимая подсказка и невидимые варианты лежат друг на друге: высота — по самому длинному */
.substatus > span { grid-area: 1 / 1; }
.substatus .sub-reserve { visibility: hidden; }

.invite {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 8px 14px;
  margin-top: 18px;
  max-width: 720px;
}
.invite-label { font-size: 19px; padding-bottom: 6px; }
.invite input { flex: 1 1 260px; font-size: 24px; font-weight: 500; letter-spacing: 0; }
.invite-hint { flex-basis: 100%; margin: 0; color: var(--pencil); font-size: 17px; }

.over-actions { display: flex; gap: 16px; margin-top: 16px; flex-wrap: wrap; }

.arena {
  display: flex;
  flex-wrap: wrap;
  gap: 32px 64px;
  margin-top: 28px;
  align-items: flex-start;
}

.side { --cell: var(--cell-base); }
.side-title {
  margin: 0 0 6px;
  font-family: var(--hand);
  font-weight: 700;
  font-size: 30px;
  line-height: 1.1;
  min-height: 1.1em;
}
.side-title span:first-child { color: var(--ink); }

.game.my-turn .side-op .side-title {
  background: linear-gradient(transparent 50%, var(--marker) 50%, var(--marker) 90%, transparent 90%) no-repeat 0 0 / 100% 100%;
  display: inline-block;
}

.badge { font-family: var(--text); font-weight: 400; font-size: 16px; color: var(--red); }

/* ---------- Поле ---------- */

.board {
  display: grid;
  grid-template-columns: calc(var(--cell) * 0.95) repeat(10, var(--cell));
  grid-template-rows: calc(var(--cell) * 0.95) repeat(10, var(--cell));
  position: relative;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

.lbl {
  display: grid;
  place-items: center;
  font-family: var(--hand);
  font-weight: 700;
  font-size: calc(var(--cell) * 0.66);
  line-height: 1;
  color: var(--ink);
}

.field {
  grid-row: 2 / span 10;
  grid-column: 2 / span 10;
  margin: -2px;
  border: 2px solid var(--ink);
  border-radius: 3px 6px 2px 5px / 5px 2px 6px 3px;
  background-color: var(--paper);
  background-image:
    linear-gradient(var(--grid-field) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid-field) 1px, transparent 1px);
  background-size: var(--cell) var(--cell);
  z-index: 0;
}

.cell {
  all: unset;
  display: block;
  position: relative;
  z-index: 2;
  cursor: default;
}
.cell:focus-visible { outline: 2px dashed var(--red); outline-offset: -3px; }

.cell svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: visible;
  pointer-events: none;
}
.cell path { fill: none; stroke: var(--ink); stroke-width: 3.2; stroke-linecap: round; }
.cell[data-mark='#'] path { stroke: var(--red); }
.cell .dot { fill: var(--ink); }
.cell[data-mark='-'] .dot { fill: var(--ink-soft); }

/* выстрел рисуется ручкой: сначала одна черта, потом вторая */
.cell.fresh path { stroke-dasharray: 1; stroke-dashoffset: 1; animation: draw 0.15s ease-out forwards; }
.cell.fresh path + path { animation-delay: 0.13s; }
.cell.fresh .dot { transform-box: fill-box; transform-origin: center; animation: dot 0.22s ease-out both; }
.cell.fresh[data-mark='-'] .dot { animation-delay: 0.2s; }
@keyframes draw { to { stroke-dashoffset: 0; } }
@keyframes dot { from { transform: scale(0); } }

/* последний выстрел обведён карандашом */
.cell.last::after {
  content: '';
  position: absolute;
  inset: 0;
  border: 1.8px solid var(--red);
  border-radius: 48% 52% 45% 55% / 52% 45% 55% 48%;
  opacity: 0.6;
}

.board.target .cell[data-mark='.'] { cursor: crosshair; }
@media (hover: hover) {
  .board.target .cell[data-mark='.']:hover::before {
    content: '';
    position: absolute;
    inset: 28%;
    border-radius: 50%;
    background: var(--marker);
    box-shadow: 0 0 0 2px rgba(210, 55, 43, 0.35);
  }
}

.board.placing .cell { cursor: pointer; }
.board.waiting { opacity: 0.45; }

.ship {
  z-index: 1;
  margin: 3px;
  pointer-events: none;
  border: 2.5px solid var(--ink);
  border-radius: 3px 6px 3px 5px / 5px 3px 6px 3px;
  background: repeating-linear-gradient(-45deg, rgba(42, 58, 156, 0.2) 0 2px, transparent 2px 6px);
}
.ship.sunk {
  border-color: var(--red);
  background: repeating-linear-gradient(-45deg, rgba(210, 55, 43, 0.16) 0 2px, transparent 2px 6px);
}
.ship.ghost { border: 2px dashed var(--pencil); background: none; }
.ship.preview { border-style: dashed; background: rgba(42, 58, 156, 0.08); }
.ship.preview.bad { border-color: var(--red); background: rgba(210, 55, 43, 0.12); }

.board.nope { animation: nope 0.3s ease; }
@keyframes nope {
  20% { transform: translateX(-4px); }
  50% { transform: translateX(4px); }
  80% { transform: translateX(-2px); }
}

/* ---------- Состав флота под полем ---------- */

.fleet {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 10px;
  margin: 12px 0 0 calc(var(--cell) * 0.95);
  max-width: calc(var(--cell) * 10);
}
.mini {
  --m: var(--mini, 11px);
  position: relative;
  width: calc(var(--len) * var(--m) + 2px);
  height: calc(var(--m) + 2px);
  border: 1.8px solid var(--ink);
  border-radius: 2px;
  background: repeating-linear-gradient(90deg, transparent 0 calc(var(--m) - 1px), var(--ink-soft) calc(var(--m) - 1px) var(--m));
}
.mini.sunk { border-color: var(--red); opacity: 0.55; background: none; }
.mini.sunk::after {
  content: '';
  position: absolute;
  left: -4px;
  right: -4px;
  top: 50%;
  height: 2px;
  background: var(--red);
  transform: rotate(-8deg);
}

/* ---------- Расстановка ---------- */

.dock { max-width: 340px; }
.dock-ships { display: grid; gap: 10px; margin: 6px 0 18px; }
.dock-row {
  all: unset;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 4px 8px;
  cursor: pointer;
  border-radius: 4px;
  font-family: var(--hand);
  font-weight: 700;
  font-size: 24px;
}
.dock-row:hover { background: rgba(255, 240, 122, 0.5); }
.dock-row.selected { background: var(--marker); }
.dock-row.done { opacity: 0.35; cursor: default; background: none; }
.dock-row:focus-visible { outline: 2px dashed var(--red); }
.dock-glyph {
  --g: 24px;
  flex: none;
  height: var(--g);
  width: calc(var(--len) * var(--g));
  border: 2.5px solid var(--ink);
  border-radius: 3px 6px 3px 5px / 5px 3px 6px 3px;
  background:
    repeating-linear-gradient(90deg, transparent 0 calc(var(--g) - 2.5px), var(--ink-soft) calc(var(--g) - 2.5px) calc(var(--g) - 1px)),
    repeating-linear-gradient(-45deg, rgba(42, 58, 156, 0.2) 0 2px, transparent 2px 6px);
}
.dock-count { min-width: 2.2em; }
.dock-tools { display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 18px; }

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

.log-wrap { flex: 1 1 100%; max-width: 720px; }
.log-wrap h3 { font-family: var(--hand); font-weight: 700; font-size: 30px; line-height: 1.1; margin: 0 0 6px; }
.log {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: calc(var(--pitch) * 9);
  overflow-y: auto;
  columns: 2 220px;
  column-gap: 32px;
  font-size: 19px;
  line-height: var(--pitch);
}
.log li { break-inside: avoid; }
.log .theirs { color: var(--pencil); }
.log .res-sunk { color: var(--red); }
.log .log-start, .log .log-win { font-style: italic; }

/* ---------- Подсказка внизу ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom));
  max-width: min(440px, calc(100vw - 32px));
  padding: 10px 16px;
  font-size: 19px;
  color: var(--ink);
  background: #fff7b0;
  box-shadow: 0 6px 14px rgba(30, 30, 60, 0.18);
  transform: translate(-50%, 20px) rotate(-1deg);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 10;
}
.toast.show { opacity: 1; transform: translate(-50%, 0) rotate(-1deg); }

/* ---------- Узкие экраны ---------- */

@media (max-width: 820px) {
  /* на телефоне в игре заголовок поменьше — место нужнее полю */
  body[data-screen='game'] .masthead { padding: 0; }
  body[data-screen='game'] .title { font-size: 34px; padding-bottom: 6px; background-size: 100% 8px; }

  .arena { flex-direction: column; align-items: flex-start; gap: 14px; margin-top: 18px; }

  /* расстановка: кнопки над полем, выбор корабля и «Готово!» — под ним */
  .dock { display: contents; }
  .dock > .side-title { display: none; }
  .dock-tools { order: -1; margin: 0; gap: 8px; }
  .dock-ships { order: 1; display: flex; flex-wrap: wrap; gap: 4px 6px; margin: 0; }
  .dock #btn-ready { order: 2; }
  .dock-row { font-size: 20px; gap: 6px; padding: 2px 6px; }
  .dock-glyph { --g: 15px; border-width: 2px; }

  /* В бою всё на одном экране: сверху поле соперника, по нему стреляем;
     снизу слева своё поле поменьше, справа — журнал. */
  body[data-screen='game'] .masthead { display: none; }
  .game[data-phase='battle'] .status,
  .game[data-phase='over'] .status { font-size: 34px; margin-top: 4px; }
  .game[data-phase='battle'] .substatus { font-size: 17px; min-height: 0; }
  .game[data-phase='battle'] .arena,
  .game[data-phase='over'] .arena {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr);
    grid-template-areas: 'op op' 'me log';
    gap: 12px 14px;
    margin-top: 10px;
  }
  .game .side-op { grid-area: op; }
  .game .side-me { grid-area: me; }
  .game .log-wrap { grid-area: log; min-width: 0; }
  .game[data-phase='battle'] .side-op .side-title,
  .game[data-phase='over'] .side-op .side-title { font-size: 24px; }
  .game[data-phase='battle'] .side-me,
  .game[data-phase='over'] .side-me { --cell: calc(var(--cell-base) * 0.56); }
  .game[data-phase='battle'] .side-me .side-title,
  .game[data-phase='over'] .side-me .side-title,
  .game .log-wrap h3 { font-size: 21px; margin-bottom: 2px; }
  .game[data-phase='battle'] .side-me .fleet,
  .game[data-phase='over'] .side-me .fleet { --mini: 6px; gap: 3px 5px; }
  .game .log { columns: 1; font-size: 15px; line-height: 20px; max-height: calc(var(--cell-base) * 0.56 * 10.95 + 30px); }
  .dock { max-width: none; }
}

@media (max-width: 480px) {
  body { font-size: 18px; }
  .sheet { padding-left: calc(var(--margin-x) + 10px); padding-right: 10px; }
  .btn { font-size: 23px; }
  .btn.small { font-size: 19px; padding: 3px 10px 4px; }
  .menu-actions .btn { width: 100%; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-delay: 0ms !important;
    transition-duration: 0.01ms !important;
  }
}
