* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;        /* фолбэк ПЕРЕД dvh */
  height: 100dvh;      /* динамическая высота вьюпорта — без скачков адресной строки iOS */
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
}

body {
  background: #ffffff;
  color: #1a1a1a;
  touch-action: none;
  overscroll-behavior: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  position: fixed;     /* запрет резинового скролла body на iOS */
  inset: 0;
}

#board {
  position: absolute;
  top: 0;
  left: 0;
  display: block;
  touch-action: none;
  cursor: crosshair;
}
#board.dragging { cursor: grabbing; }

.hidden { display: none !important; }
.muted { color: #6b7280; font-size: 12px; }

/* ===== Панели (стекло) ===== */
.panel {
  position: fixed;
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid rgba(0, 0, 0, 0.08);
  border-radius: 12px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06), 0 6px 20px rgba(0, 0, 0, 0.05);
  user-select: none;
  -webkit-user-select: none;
}

/* ===== Экран входа ===== */
.overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: calc(16px + env(safe-area-inset-top)) 16px calc(16px + env(safe-area-inset-bottom));
  background: rgba(255, 255, 255, 0.6);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  z-index: 50;
}
.card {
  width: 100%;
  max-width: 360px;
  background: #fff;
  border: 1px solid rgba(0, 0, 0, 0.1);
  border-radius: 16px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.12);
  padding: 28px 24px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  text-align: center;
}
.card h1 { font-size: 22px; font-weight: 600; }
.card .muted { font-size: 13px; line-height: 1.4; }

input[type="text"], #invite-url {
  width: 100%;
  font: inherit;
  font-size: 16px;            /* ≥16px — iOS не зумит при фокусе */
  padding: 11px 14px;
  border: 1px solid rgba(0, 0, 0, 0.15);
  border-radius: 10px;
  background: #fff;
  color: #1a1a1a;
  -webkit-user-select: text;
  user-select: text;
}
input[type="text"]:focus { outline: none; border-color: #3b82f6; }

button {
  font: inherit;
  font-size: 14px;
  cursor: pointer;
  background: rgba(0, 0, 0, 0.04);
  border: 1px solid rgba(0, 0, 0, 0.1);
  color: #1a1a1a;
  padding: 9px 14px;
  border-radius: 10px;
  transition: background .15s, border-color .15s, color .15s, transform .05s;
}
button:hover:not(:disabled) { background: rgba(0, 0, 0, 0.08); border-color: rgba(0, 0, 0, 0.2); }
button:active:not(:disabled) { transform: scale(0.97); }
button:disabled { opacity: 0.4; cursor: not-allowed; }
button.primary { background: #ef4444; border-color: #ef4444; color: #fff; font-weight: 600; padding: 11px 14px; }
button.primary:hover:not(:disabled) { background: #dc2626; border-color: #dc2626; }
button.armed { background: rgba(239, 68, 68, 0.1); border-color: rgba(239, 68, 68, 0.45); color: #b91c1c; }

.error-text { color: #dc2626; font-size: 13px; min-height: 16px; }

/* ===== HUD ===== */
#hud {
  top: calc(12px + env(safe-area-inset-top));
  left: calc(12px + env(safe-area-inset-left));
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-width: 220px;
  max-width: calc(100vw - 24px);
}
#turn { font-size: 14px; font-weight: 500; display: flex; align-items: center; gap: 10px; }
#status { font-size: 13px; color: #6b7280; min-height: 18px; }
#status.win { color: #059669; font-weight: 600; }
#status.warn { color: #b45309; }

.presence { display: flex; flex-direction: column; gap: 5px; font-size: 13px; padding: 6px 0; border-top: 1px solid rgba(0,0,0,0.06); border-bottom: 1px solid rgba(0,0,0,0.06); }
.pl { display: flex; align-items: center; gap: 8px; }
.pname { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pscore { font-variant-numeric: tabular-nums; font-weight: 600; font-size: 12px; color: #374151; min-width: 16px; text-align: right; }
.dot { width: 8px; height: 8px; border-radius: 50%; flex: none; }
.dot.on { background: #22c55e; }
.dot.off { background: #cbd5e1; }
.spectators { color: #6b7280; }

.mark { width: 14px; height: 14px; display: inline-block; flex: none; }
.mark.x { background: #ef4444; border-radius: 3px; }
.mark.o { background: transparent; border: 3px solid #3b82f6; border-radius: 50%; }

.row { display: flex; gap: 8px; }
.row button { flex: 1; }
#reset-confirm { display: flex; flex-direction: column; gap: 8px; }
#reset-confirm-text { font-size: 13px; color: #374151; }

/* ===== Счётчик ===== */
#counter {
  top: calc(12px + env(safe-area-inset-top));
  right: calc(12px + env(safe-area-inset-right));
  padding: 10px 14px;
  font-size: 13px;
  display: flex;
  gap: 14px;
  align-items: center;
}
#counter span { display: flex; align-items: center; gap: 6px; }
#counter b { font-variant-numeric: tabular-nums; }
.conn { font-size: 10px; }
.conn.online { color: #22c55e; }
.conn.offline { color: #f59e0b; }
.conn.dead { color: #dc2626; }

/* ===== Приглашение ===== */
#invite {
  top: calc(96px + env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  padding: 12px 14px;
  width: min(420px, calc(100vw - 24px));
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.invite-label { font-size: 12px; color: #374151; }
.invite-row { display: flex; gap: 8px; }
#invite-url { flex: 1; font-size: 13px; padding: 8px 10px; }
#copy-btn { flex: none; }

/* ===== Чат ===== */
#chat {
  bottom: calc(12px + env(safe-area-inset-bottom));
  right: calc(12px + env(safe-area-inset-right));
  width: min(320px, calc(100vw - 24px));
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.chat-toggle {
  border: none;
  border-radius: 12px 12px 0 0;
  background: transparent;
  font-weight: 600;
  text-align: left;
  display: flex;
  align-items: center;
  gap: 8px;
}
.badge { background: #ef4444; color: #fff; border-radius: 10px; font-size: 11px; padding: 1px 7px; font-weight: 600; }
#chat-body { display: flex; flex-direction: column; max-height: min(40vh, 320px); }
#chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 8px 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 13px;
  border-top: 1px solid rgba(0,0,0,0.06);
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  -webkit-user-select: text;
  user-select: text;
}
.msg { line-height: 1.35; word-break: break-word; }
.msg .who { font-weight: 600; }
.msg .who.x { color: #ef4444; }
.msg .who.o { color: #3b82f6; }
.msg .who.spectator { color: #6b7280; }
.msg.system { color: #6b7280; font-style: italic; }
.chat-input-row { display: flex; gap: 6px; padding: 8px; border-top: 1px solid rgba(0,0,0,0.06); }
#chat-input { flex: 1; font-size: 16px; padding: 8px 10px; }
#chat-send { flex: none; width: 40px; font-size: 16px; }

/* ===== Узкие экраны ===== */
@media (max-width: 600px) {
  #hud { min-width: 0; width: calc(100vw - 24px); padding: 10px 12px; }
  #counter { padding: 8px 10px; gap: 10px; }
  #chat { width: calc(100vw - 24px); }
}
