/* =========================================================
   PLAY — страница PvP
   ========================================================= */

html, body {
  overflow-x: hidden;
  height: auto;
  min-height: 100%;
}

.play {
  max-width: 1500px;
  margin: 0 auto;
  padding: 100px 24px 40px;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  gap: var(--gap-md);
}

/* =========================================================
   Layout: игроки | доска | управление
   ========================================================= */
.play-layout {
  display: grid;
  grid-template-columns: 200px minmax(0, 1fr) 320px;
  gap: var(--gap-lg);
  align-items: stretch;
  min-height: calc(100vh - 160px);
}

@supports (height: 100dvh) {
  .play-layout {
    min-height: calc(100dvh - 160px);
  }
}

/* =========================================================
   Левая колонка — игроки
   ========================================================= */
.play-players {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: var(--gap-md);
  min-width: 0;
}

.play-player {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 16px;
  background: var(--c-paper);
  border: 1px solid var(--c-line);
  border-radius: var(--r-md);
  aspect-ratio: 1 / 1;
  align-items: center;
  justify-content: center;
  text-align: center;
  transition: border-color var(--t-med), box-shadow var(--t-med), opacity var(--t-med);
  position: relative;
}
.play-player.active {
  border-color: var(--c-accent);
  box-shadow: 0 0 0 2px rgba(181, 114, 44, 0.15);
}
.play-player.disconnected {
  opacity: 0.6;
  border-color: var(--c-danger);
}

.play-player-avatar {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--c-accent), var(--c-accent-deep));
  color: var(--c-paper);
  font-family: var(--f-sans);
  font-size: 26px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  user-select: none;
  overflow: hidden;
}
.play-player-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.play-player-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
  width: 100%;
}
.play-player-name {
  font-size: 15px;
  font-weight: 600;
  color: var(--c-ink);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.play-player-status {
  font-size: 11px;
  color: var(--c-ink-muted);
  letter-spacing: 1px;
  text-transform: uppercase;
}

.play-player-clock {
  font-family: var(--f-mono);
  font-size: 28px;
  font-weight: 700;
  color: var(--c-ink);
  padding: 8px 14px;
  border-radius: var(--r-sm);
  background: rgba(44, 24, 16, 0.06);
  font-variant-numeric: tabular-nums;
  letter-spacing: 1px;
  transition: background var(--t-fast), color var(--t-fast);
  width: 100%;
  text-align: center;
}
.play-player.active .play-player-clock {
  background: rgba(181, 114, 44, 0.15);
  color: var(--c-accent-deep);
}
.play-player-clock.low {
  background: rgba(181, 61, 61, 0.12);
  color: var(--c-danger);
  animation: clockPulse 1s infinite;
}
@keyframes clockPulse {
  0%, 100% { opacity: 1; }
  50%      { opacity: 0.6; }
}

/* =========================================================
   Центр — доска
   ========================================================= */
.play-center {
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 0;
  min-height: 0;
}

.play .board-frame {
  height: 100%;
  aspect-ratio: 1 / 1;
  max-height: calc(100vh - 160px);
  width: auto;
  max-width: 100%;
  padding: 12px;
  position: relative;
}

/* =========================================================
   Вспышка над доской
   ========================================================= */
.play .board-flash {
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: 0;
  z-index: 100;
  border-radius: var(--r-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity 0.4s ease;
}
.play .board-flash.visible { opacity: 1; }

.play .board-flash-text {
  font-family: var(--f-sans);
  font-weight: 700;
  font-size: clamp(36px, 8vw, 96px);
  letter-spacing: 0.02em;
  text-transform: uppercase;
  color: #fff;
  text-shadow: 0 2px 12px rgba(0,0,0,0.6), 0 0 40px rgba(0,0,0,0.4);
  animation: flashPop 0.5s cubic-bezier(0.34, 1.56, 0.64, 1);
  opacity: 0;
  text-align: center;
  padding: 0 16px;
  line-height: 1.1;
}
.play .board-flash.visible .board-flash-text { opacity: 1; }

.play .board-flash.win {
  background: radial-gradient(circle at center,
    rgba(212, 165, 116, 0.85) 0%,
    rgba(181, 114, 44, 0.7) 50%,
    rgba(138, 82, 28, 0.5) 100%);
}
.play .board-flash.win .board-flash-text { color: #fff8e7; }

.play .board-flash.lose {
  background: radial-gradient(circle at center,
    rgba(181, 61, 61, 0.85) 0%,
    rgba(120, 30, 30, 0.7) 50%,
    rgba(80, 20, 20, 0.5) 100%);
}

.play .board-flash.draw {
  background: radial-gradient(circle at center,
    rgba(120, 110, 100, 0.8) 0%,
    rgba(90, 80, 70, 0.6) 50%,
    rgba(60, 50, 40, 0.4) 100%);
}
.play .board-flash.draw .board-flash-text { color: #f0e8d8; }

.play .board-flash.check {
  background: radial-gradient(circle at center,
    rgba(220, 130, 40, 0.75) 0%,
    rgba(180, 90, 20, 0.55) 50%,
    rgba(140, 60, 10, 0.35) 100%);
}

@keyframes flashPop {
  0%   { transform: scale(0.4); opacity: 0; }
  50%  { transform: scale(1.15); opacity: 1; }
  100% { transform: scale(1); opacity: 1; }
}

.play .board-frame.pulse {
  animation: playFramePulse 0.9s ease 3;
}
@keyframes playFramePulse {
  0%, 100% { box-shadow: var(--sh-board); }
  50% {
    box-shadow:
      0 0 0 4px rgba(212, 165, 116, 0.6),
      0 0 40px rgba(212, 165, 116, 0.4),
      var(--sh-board);
  }
}

.play .sq.in-check {
  background: rgba(200, 50, 50, 0.55) !important;
  box-shadow: inset 0 0 0 3px rgba(255, 80, 80, 0.8);
  animation: playCheckPulse 1s infinite;
}
@keyframes playCheckPulse {
  0%, 100% { background: rgba(200, 50, 50, 0.55); }
  50%      { background: rgba(220, 70, 70, 0.75); }
}

/* =========================================================
   Правая колонка: кнопки + история
   ========================================================= */
.play-side {
  display: flex;
  flex-direction: column;
  gap: var(--gap-md);
  position: sticky;
  top: 90px;
  max-height: calc(100vh - 120px);
}

.play-controls {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 16px;
  background: var(--c-paper);
  border: 1px solid var(--c-line);
  border-radius: var(--r-md);
  box-shadow: var(--sh-soft);
}

.play-controls .btn {
  width: 100%;
  justify-content: flex-start;
}

.time-control-picker {
  display: flex;
  border: 1px solid var(--c-line-strong);
  border-radius: var(--r-sm);
  overflow: hidden;
  background: rgba(44, 24, 16, 0.03);
  margin-bottom: 4px;
}
.time-control-picker[hidden] { display: none; }

.time-control-picker button {
  flex: 1;
  padding: 10px 8px;
  font-family: var(--f-mono);
  font-size: 13px;
  font-weight: 500;
  color: var(--c-ink-soft);
  border-right: 1px solid var(--c-line);
  transition: background var(--t-fast), color var(--t-fast);
  text-align: center;
}
.time-control-picker button:last-child { border-right: none; }
.time-control-picker button:hover {
  color: var(--c-ink);
  background: rgba(44, 24, 16, 0.05);
}
.time-control-picker button.active {
  background: var(--c-ink);
  color: var(--c-paper);
}

.play-status {
  text-align: center;
  font-family: var(--f-mono);
  font-size: 12px;
  color: var(--c-ink-muted);
  letter-spacing: 1px;
  min-height: 18px;
  padding: 0 4px;
}

.play-history {
  flex: 1;
  background: var(--c-paper);
  border: 1px solid var(--c-line);
  border-radius: var(--r-md);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 200px;
  overflow: hidden;
}
.play-side-head {
  font-size: 12px;
  letter-spacing: 2px;
  text-transform: uppercase;
  color: var(--c-ink-muted);
  padding-bottom: 12px;
  border-bottom: 1px solid var(--c-line);
}

.moves-list {
  flex: 1;
  overflow-y: auto;
  padding-right: 6px;
  font-family: var(--f-mono);
  font-size: 14px;
  line-height: 1.7;
  scroll-behavior: smooth;
}

.moves-empty {
  color: var(--c-ink-muted);
  font-size: 13px;
  text-align: center;
  margin-top: 20px;
  font-family: var(--f-sans);
}

.move-row {
  display: grid;
  grid-template-columns: 32px 1fr 1fr;
  gap: 4px;
  align-items: center;
  padding: 2px 6px;
  border-radius: var(--r-sm);
  transition: background var(--t-fast);
}
.move-row:hover { background: rgba(44, 24, 16, 0.04); }
.move-row.current {
  background: rgba(181, 114, 44, 0.15);
  color: var(--c-accent-deep);
}
.move-num {
  color: var(--c-ink-muted);
  font-size: 13px;
  text-align: right;
  padding-right: 6px;
  opacity: 0.7;
}
.move-white,
.move-black {
  font-weight: 500;
  color: var(--c-ink);
}

/* =========================================================
   Кнопки звонка в шапке
   ========================================================= */
.nav-call {
  display: inline-flex;
  gap: 6px;
  align-items: center;
  padding: 4px;
  background: rgba(44, 24, 16, 0.06);
  border: 1px solid var(--c-line);
  border-radius: var(--r-sm);
}
.nav-call[hidden] { display: none; }

.nav-call-btn {
  width: 36px;
  height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--r-sm);
  color: var(--c-ink);
  background: transparent;
  transition: background var(--t-fast), color var(--t-fast), transform var(--t-fast);
  font-size: 14px;
}
.nav-call-btn:hover { background: rgba(44, 24, 16, 0.1); }
.nav-call-btn:active { transform: scale(0.94); }
.nav-call-btn.active {
  background: var(--c-accent);
  color: var(--c-paper);
}
.nav-call-btn-end { color: var(--c-danger); }
.nav-call-btn-end:hover {
  background: var(--c-danger);
  color: var(--c-paper);
}
.nav-call-btn[hidden] { display: none; }

/* =========================================================
   Видео WebRTC
   ========================================================= */
.call-videos {
  position: fixed;
  right: 24px;
  bottom: 24px;
  z-index: 8000;
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
  width: min(360px, calc(100vw - 48px));
}
.call-videos[hidden] { display: none; }

.call-videos video {
  width: 100%;
  aspect-ratio: 4 / 3;
  background: #000;
  border-radius: var(--r-md);
  object-fit: cover;
  box-shadow: var(--sh-deep);
}

#localVideo {
  position: absolute;
  bottom: 0;
  right: 0;
  width: 100px;
  aspect-ratio: 4 / 3;
  border: 2px solid var(--c-paper);
  box-shadow: var(--sh-card);
}

/* =========================================================
   Оверлеи
   ========================================================= */
.play-overlay {
  position: fixed;
  inset: 0;
  z-index: 9000;
  background: rgba(26, 15, 10, 0.75);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  animation: fadeIn 0.25s ease;
}
.play-overlay[hidden] { display: none; }

.play-overlay-card {
  background: var(--c-paper);
  border: 1px solid var(--c-line);
  border-radius: var(--r-md);
  padding: 40px 32px;
  max-width: 420px;
  width: 100%;
  text-align: center;
  box-shadow: var(--sh-deep);
  animation: popIn 0.35s cubic-bezier(0.22, 1, 0.36, 1);
}

.play-overlay-title {
  font-family: var(--f-sans);
  font-size: 26px;
  font-weight: 700;
  color: var(--c-ink);
  margin-bottom: 8px;
  line-height: 1.2;
}
.play-overlay-sub {
  font-size: 15px;
  color: var(--c-ink-soft);
  line-height: 1.5;
  margin-bottom: 24px;
}

.play-overlay-actions {
  display: flex;
  gap: 12px;
  justify-content: center;
  margin-top: 8px;
  flex-wrap: wrap;
}

.play-end-icon {
  font-size: 64px;
  line-height: 1;
  margin-bottom: 16px;
  color: var(--c-accent);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.play-end-icon i {
  font-size: inherit;
  line-height: 1;
}
.play-end-icon.win  { color: var(--c-accent); }
.play-end-icon.lose { color: var(--c-danger); }
.play-end-icon.draw { color: var(--c-ink-muted); }

.play-spinner {
  width: 56px;
  height: 56px;
  margin: 0 auto 24px;
  border: 4px solid var(--c-line);
  border-top-color: var(--c-accent);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes popIn {
  from { transform: scale(0.85); opacity: 0; }
  to   { transform: scale(1); opacity: 1; }
}

/* =========================================================
   Toast
   ========================================================= */
.play-toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--c-ink);
  color: var(--c-paper);
  padding: 12px 24px;
  border-radius: var(--r-sm);
  font-size: 14px;
  z-index: 9500;
  box-shadow: var(--sh-card);
  animation: toastIn 0.3s ease;
}
.play-toast[hidden] { display: none; }
@keyframes toastIn {
  from { transform: translate(-50%, 20px); opacity: 0; }
  to   { transform: translate(-50%, 0); opacity: 1; }
}

/* =========================================================
   Адаптив
   ========================================================= */

/* Планшет и меньше */
@media (max-width: 1200px) {
  .play-layout {
    grid-template-columns: 180px minmax(0, 1fr) 280px;
    gap: var(--gap-md);
  }
  .play-player-avatar { width: 52px; height: 52px; font-size: 22px; }
  .play-player-clock { font-size: 24px; }
}

/* Мобиле — всё в одну колонку */
@media (max-width: 900px) {
  .play {
    min-height: auto;
    padding-top: calc(80px + env(safe-area-inset-top));
    padding-bottom: calc(24px + env(safe-area-inset-bottom));
    padding-left: calc(12px + env(safe-area-inset-left));
    padding-right: calc(12px + env(safe-area-inset-right));
  }

  .play-layout {
    grid-template-columns: 1fr;
    min-height: 0;
    gap: var(--gap-md);
  }

  /* Порядок: доска → игроки → управление */
  .play-center { order: 1; }
  .play-players { order: 2; }
  .play-side { order: 3; }

  /* Доска — не больше ширины контейнера и не больше доступной высоты */
  .play-center {
    width: 100%;
    max-width: 100%;
    min-width: 0;
    min-height: 0;
    overflow: hidden;
  }

  .play .board-frame {
    height: auto;
    width: 100%;
    max-width: min(100%, calc(100dvh - 320px), 560px);
    max-height: none;
    aspect-ratio: 1 / 1;
    margin: 0 auto;
    padding: 8px;
  }

  /* Игроки — в ряд */
  .play-players {
    flex-direction: row;
    gap: var(--gap-sm);
  }
  .play-player {
    aspect-ratio: auto;
    flex: 1;
    padding: 12px;
  }
  .play-player-avatar { width: 44px; height: 44px; font-size: 18px; }
  .play-player-clock { font-size: 20px; padding: 6px 10px; }

  /* Управление — в потоке, кнопки в строку */
  .play-side {
    position: static;
    max-height: none;
  }
  .play-controls {
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: center;
  }
  .play-controls .btn {
    width: auto;
    flex: 0 1 auto;
  }
  .play-history {
    max-height: 240px;
  }
}

@media (max-width: 600px) {
  .play {
    padding-top: calc(80px + env(safe-area-inset-top));
    padding-bottom: calc(24px + env(safe-area-inset-bottom));
    padding-left: calc(8px + env(safe-area-inset-left));
    padding-right: calc(8px + env(safe-area-inset-right));
  }

  .play .board-frame {
    max-width: min(100%, calc(100dvh - 340px), 520px);
  }

  .play-player { padding: 10px 8px; }
  .play-player-avatar { width: 36px; height: 36px; font-size: 15px; }
  .play-player-clock { font-size: 16px; padding: 4px 8px; min-width: 60px; }
  .play-player-name { font-size: 13px; }
  .play-player-status { font-size: 10px; letter-spacing: 0.5px; }

  .play-overlay-card { padding: 32px 24px; }
  .play-overlay-title { font-size: 22px; }
  .play-end-icon { font-size: 48px; }
  .time-control-picker button { padding: 8px 6px; font-size: 12px; }

  .call-videos {
    right: calc(12px + env(safe-area-inset-right));
    bottom: calc(12px + env(safe-area-inset-bottom));
    width: calc(100vw - 24px - env(safe-area-inset-left) - env(safe-area-inset-right));
  }
  #localVideo { width: 80px; }
}