/* Telegram injects --tg-theme-* on <html>; the --fb-* values only show in a plain browser. */
:root {
  --fb-bg: #ffffff;
  --fb-text: #141a22;
  --fb-hint: #8a929c;
  --fb-button: #2f7fd0;
  --fb-button-text: #ffffff;
  --fb-surface: #f0f2f5;

  --bg: var(--tg-theme-bg-color, var(--fb-bg));
  --text: var(--tg-theme-text-color, var(--fb-text));
  --hint: var(--tg-theme-hint-color, var(--fb-hint));
  --accent: var(--tg-theme-button-color, var(--fb-button));
  --accent-text: var(--tg-theme-button-text-color, var(--fb-button-text));
  --surface: var(--tg-theme-secondary-bg-color, var(--fb-surface));

  --water: #d9e8f4;
  --water-hi: #c7dcee;
  --grid: #bcd3e7;
  --ship: #3a4c60;
  --miss: #6d8cab;
  --hit: #f0612a;
  --hit-core: #ffd27c;
  --sunk: #5a3a34;
  --sunk-cross: #ffb59b;
  --ok: #2c9b63;
  --bad: #e0464c;
  --line: rgba(0, 0, 0, 0.08);
  --shadow: 0 6px 20px rgba(20, 30, 45, 0.18);

  --gutter: 16px;
  --radius: 12px;
  --label: 14px;

  color-scheme: light;
}

:root[data-scheme="dark"] {
  --fb-bg: #17212b;
  --fb-text: #f2f5f8;
  --fb-hint: #7d8b99;
  --fb-button: #5288c1;
  --fb-button-text: #ffffff;
  --fb-surface: #232e3c;

  --water: #1d3a51;
  --water-hi: #284a66;
  --grid: #0f2131;
  --ship: #93a9bf;
  --miss: #6a8db0;
  --hit: #ff6a33;
  --hit-core: #ffd27c;
  --sunk: #3d2724;
  --sunk-cross: #f08c6b;
  --ok: #3cb878;
  --bad: #ff5a5f;
  --line: rgba(255, 255, 255, 0.08);
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.45);

  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
}

body { min-height: 100vh; min-height: var(--tg-viewport-stable-height, 100vh); }

button { font: inherit; color: inherit; }

.app {
  max-width: 480px;
  margin: 0 auto;
  padding:
    max(env(safe-area-inset-top), calc(var(--tg-safe-area-inset-top, 0px) + var(--tg-content-safe-area-inset-top, 0px))) calc(var(--gutter) + env(safe-area-inset-right))
    calc(20px + max(env(safe-area-inset-bottom), var(--tg-safe-area-inset-bottom, 0px))) calc(var(--gutter) + env(safe-area-inset-left));
}

.screen { display: flex; flex-direction: column; gap: 16px; padding-top: 14px; }
.screen--center { min-height: 70vh; align-items: center; justify-content: center; text-align: center; }

h1, h2, p { margin: 0; }
.title { font-size: 22px; font-weight: 700; letter-spacing: -0.01em; }
.subtitle { color: var(--hint); }
.hint { color: var(--hint); font-size: 13px; }
.center { text-align: center; }

.head { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 32px; }
.head__main { display: flex; flex-direction: column; min-width: 0; }
.head__title { font-size: 19px; font-weight: 700; }
.head__sub { color: var(--hint); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 46px; padding: 0 18px;
  border: 0; border-radius: var(--radius);
  background: var(--accent); color: var(--accent-text);
  font-weight: 600; font-size: 15px;
  cursor: pointer; user-select: none;
  transition: opacity 0.15s, filter 0.15s;
}
.btn:active { opacity: 0.75; }
.btn:disabled { opacity: 0.45; cursor: default; }
.btn--ghost { background: var(--surface); color: var(--text); }
.btn--text { background: transparent; color: var(--hint); min-height: 40px; }
.btn--danger { background: var(--bad); color: #fff; }
.btn--small { min-height: 36px; padding: 0 14px; font-size: 14px; border-radius: 10px; }
.btn--block { width: 100%; }
@media (hover: hover) { .btn:not(:disabled):hover { filter: brightness(1.06); } }

.row { display: flex; gap: 10px; flex-wrap: wrap; }
.row > .btn { flex: 1 1 0; }
.stack { display: flex; flex-direction: column; gap: 10px; }

.chip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 4px 10px; border-radius: 999px;
  background: var(--surface); color: var(--hint);
  font-size: 13px; font-weight: 500; white-space: nowrap;
}
.chip--ok { color: var(--ok); }
.chip--warn { color: var(--bad); }
.chip-btn { border: 0; cursor: pointer; color: var(--text); min-height: 32px; padding: 0 11px; }
.chip-btn:active { opacity: 0.7; }

.card { background: var(--surface); border-radius: var(--radius); padding: 14px 16px; }

/* Timer */
.timer {
  font-variant-numeric: tabular-nums; font-weight: 600; font-size: 14px;
  padding: 4px 10px; border-radius: 999px; background: var(--surface); color: var(--text);
  white-space: nowrap;
}
.timer--low { color: var(--bad); animation: pulse 1s ease-in-out infinite; }
.timer:empty { display: none; }

/* Boards */
.field {
  --cell: min(38px, calc((100vw - 2 * var(--gutter) - var(--label) - 4px - 11 * var(--gap)) / 10));
  --gap: 2px;
  display: grid;
  grid-template-columns: var(--label) auto;
  grid-template-rows: var(--label) auto;
  column-gap: 4px; row-gap: 3px;
  justify-content: center;
  user-select: none; -webkit-user-select: none;
}
.field--small { --cell: min(22px, calc((100vw - 2 * var(--gutter) - var(--label) - 4px - 11 * var(--gap)) / 10)); --gap: 1px; }
.field--mid { --cell: min(28px, calc((100vw - 2 * var(--gutter) - var(--label) - 4px - 11 * var(--gap)) / 10)); }

.labels {
  display: grid; gap: var(--gap);
  color: var(--hint); font-size: clamp(9px, calc(var(--cell) * 0.38), 12px); font-weight: 600;
}
.labels--cols { grid-column: 2; grid-template-columns: repeat(10, var(--cell)); padding: 0 var(--gap); }
.labels--rows { grid-row: 2; grid-template-rows: repeat(10, var(--cell)); padding: var(--gap) 0; }
.labels span { display: flex; align-items: center; justify-content: center; transition: color 0.15s; }
.labels span.on { color: var(--accent); }

.board {
  grid-column: 2; grid-row: 2;
  position: relative;
  display: grid;
  grid-template-columns: repeat(10, var(--cell));
  grid-template-rows: repeat(10, var(--cell));
  gap: var(--gap); padding: var(--gap);
  background: var(--grid);
  border-radius: 8px;
}
.cell { background: var(--water); border-radius: 2px; position: relative; }
.board--aimable .cell--free { cursor: pointer; }
.board--aimable .cell--free:active { background: var(--water-hi); }
.cell--line { background: var(--water-hi); }
.cell--aim { background: var(--water-hi); box-shadow: inset 0 0 0 2px var(--accent); z-index: 3; }
.cell--aim::after {
  content: ""; position: absolute; inset: 32%; border-radius: 50%;
  border: 2px solid var(--accent);
}
.board--idle { opacity: 0.82; transition: opacity 0.2s; }

.ship {
  z-index: 1;
  background: var(--ship);
  border-radius: calc(var(--cell) * 0.22);
  margin: 1px;
}
.field--small .ship { margin: 0; border-radius: 4px; }
.ship--sunk { background: var(--sunk); }

.mark { z-index: 2; position: relative; border-radius: 2px; pointer-events: none; }
.mark--miss { background: radial-gradient(circle, var(--miss) 0 15%, transparent 18%); }
.mark--hit { background: radial-gradient(circle, var(--hit-core) 0 18%, var(--hit) 46%); }
.mark--sunk {
  background:
    linear-gradient(45deg, transparent calc(50% - 1.5px), var(--sunk-cross) calc(50% - 1.5px) calc(50% + 1.5px), transparent calc(50% + 1.5px)),
    linear-gradient(-45deg, transparent calc(50% - 1.5px), var(--sunk-cross) calc(50% - 1.5px) calc(50% + 1.5px), transparent calc(50% + 1.5px));
  background-size: 64% 64%; background-position: center; background-repeat: no-repeat;
}
.field--small .mark--sunk { background-size: 80% 80%; }

.mark--fresh.mark--miss::after {
  content: ""; position: absolute; inset: 18%; border-radius: 50%;
  border: 2px solid var(--miss);
  animation: ring 0.7s ease-out forwards;
}
.mark--fresh.mark--hit, .mark--fresh.mark--sunk { animation: flash 0.55s ease-out; }

@keyframes ring { from { transform: scale(0.3); opacity: 1; } to { transform: scale(1.5); opacity: 0; } }
@keyframes flash { 0% { transform: scale(1.25); filter: brightness(1.7); } 100% { transform: scale(1); filter: none; } }
@keyframes pulse { 50% { opacity: 0.55; } }
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes sheet-in { from { transform: translateY(24px); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes spin { to { transform: rotate(360deg); } }

/* Editor */
.board--edit, .dock { touch-action: none; }
.board--edit .ship { cursor: grab; touch-action: none; transition: background 0.15s; }
.board--edit .ship--bad { background: var(--bad); }
.board--edit .ship--lifted { opacity: 0.22; }
.preview {
  z-index: 4; pointer-events: none; margin: 1px;
  border-radius: calc(var(--cell) * 0.22);
  background: rgba(44, 155, 99, 0.3);
  background: color-mix(in srgb, var(--ok) 35%, transparent);
  box-shadow: inset 0 0 0 2px var(--ok);
}
.preview--bad {
  background: rgba(224, 70, 76, 0.3);
  background: color-mix(in srgb, var(--bad) 35%, transparent);
  box-shadow: inset 0 0 0 2px var(--bad);
}

.dock {
  --dcell: min(24px, calc((100vw - 2 * var(--gutter)) / 14));
  display: grid; grid-template-columns: 1fr 1fr; gap: 12px 16px;
  padding: 14px 16px; border-radius: var(--radius);
  background: var(--surface);
  outline: 2px dashed transparent; outline-offset: -2px;
  transition: outline-color 0.15s;
}
.dock--target { outline-color: var(--hint); }
.dock__item { display: flex; align-items: center; gap: 10px; min-height: var(--dcell); }
.dock__ship {
  width: calc(var(--len) * var(--dcell) + (var(--len) - 1) * 2px); height: var(--dcell);
  border-radius: calc(var(--dcell) * 0.25);
  background: var(--ship);
  cursor: grab; touch-action: none;
}
.dock__ship--empty { background: transparent; box-shadow: inset 0 0 0 1.5px var(--line); cursor: default; }
.dock__count { color: var(--hint); font-size: 13px; font-weight: 600; font-variant-numeric: tabular-nums; }
.dock__done { grid-column: 1 / -1; color: var(--hint); font-size: 13px; text-align: center; margin: 0; }
.dock--empty { padding: 12px 16px; }

.ghost {
  position: fixed; left: 0; top: 0; z-index: 50; pointer-events: none;
  background: var(--ship); opacity: 0.88;
  border-radius: 6px; box-shadow: var(--shadow);
}

.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.toolbar .btn { flex: 1 1 auto; }
.presets { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; justify-content: center; }

/* Game */
.status {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 14px; border-radius: var(--radius); background: var(--surface);
  box-shadow: inset 3px 0 0 transparent; transition: box-shadow 0.2s;
}
.status--mine { box-shadow: inset 3px 0 0 var(--accent); }
.status__text { display: flex; flex-direction: column; min-width: 0; }
.status__turn { font-weight: 700; font-size: 17px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.status__last { color: var(--hint); font-size: 13px; }
.status .timer { background: var(--bg); }

.board-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; font-size: 13px; color: var(--hint); }
.board-head b { color: var(--text); font-weight: 600; }
.section { display: flex; flex-direction: column; gap: 8px; }

.tally { display: flex; gap: 3px; align-items: center; flex-shrink: 0; }
.tally i { display: block; height: 6px; width: calc(var(--len) * 6px); border-radius: 3px; background: var(--ship); }
.tally span { margin-left: 5px; font-variant-numeric: tabular-nums; min-width: 3.2em; text-align: right; }
.tally i.gone { background: var(--hint); opacity: 0.3; }

/* Result & lobby */
.hero { display: flex; flex-direction: column; align-items: center; gap: 6px; text-align: center; padding: 8px 0; }
.hero__title { font-size: 30px; font-weight: 800; letter-spacing: -0.02em; }
.hero__title--win { color: var(--ok); }
.hero__title--loss { color: var(--bad); }
.code {
  font-size: 44px; font-weight: 800; letter-spacing: 0.18em; font-variant-numeric: tabular-nums;
  padding-left: 0.18em;
}
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; width: 100%; }
.stat { background: var(--surface); border-radius: var(--radius); padding: 12px 8px; text-align: center; }
.stat b { display: block; font-size: 22px; font-variant-numeric: tabular-nums; }
.stat span { color: var(--hint); font-size: 12px; }
.waves { width: 72px; height: 72px; color: var(--accent); }
.dots::after { content: ""; animation: dots 1.4s steps(4) infinite; }
@keyframes dots { 0% { content: ""; } 25% { content: "."; } 50% { content: ".."; } 75% { content: "..."; } }

.saved { display: flex; flex-direction: column; align-items: center; gap: 8px; text-align: center; padding: 20px 16px 4px; animation: fade-in 0.25s ease-out; }

/* Overlays */
.toast {
  position: fixed; left: 50%; z-index: 60;
  bottom: calc(24px + max(env(safe-area-inset-bottom), var(--tg-safe-area-inset-bottom, 0px)));
  transform: translateX(-50%);
  max-width: calc(100vw - 32px);
  padding: 10px 16px; border-radius: 12px;
  background: var(--text); color: var(--bg);
  font-size: 14px; box-shadow: var(--shadow);
  animation: fade-in 0.2s ease-out;
}
.modal {
  position: fixed; inset: 0; z-index: 70;
  display: flex; align-items: flex-end; justify-content: center;
  background: rgba(0, 0, 0, 0.45);
  padding: 16px 16px calc(16px + max(env(safe-area-inset-bottom), var(--tg-safe-area-inset-bottom, 0px)));
  animation: fade-in 0.15s ease-out;
}
.modal__sheet {
  width: 100%; max-width: 420px;
  background: var(--bg); border-radius: 16px; padding: 20px;
  display: flex; flex-direction: column; gap: 14px;
  box-shadow: var(--shadow);
  animation: sheet-in 0.2s ease-out;
}
.modal__title { font-size: 18px; font-weight: 700; }

.spinner {
  width: 28px; height: 28px; border-radius: 50%;
  border: 3px solid var(--surface); border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}

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