/* ============================================================
   Шахматы — строгая тёмная тема
   Палитра по ТЗ: фон #14151A, поверхности #1E2027, рамки #2E313B,
   клетки #EEEED2/#769656, акцент #E8C465, опасность #E0564F.
   ============================================================ */

:root {
  --bg: #14151A;
  --surface: #1E2027;
  --border: #2E313B;
  --cell-light: #EEEED2;
  --cell-dark: #769656;
  --accent: #E8C465;
  --accent-soft: rgba(232, 196, 101, 0.28);
  --danger: #E0564F;
  --hint: #79A7E0;
  --text: #E8EAED;
  --text-dim: #9AA0AB;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }

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

.screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

#screen-game.screen {
  align-items: flex-start;
  padding-top: 22px;
}

/* ---------- Кнопки ---------- */

.btn {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 9px 14px;
  font: inherit;
  font-size: 14px;
  cursor: pointer;
  transition: border-color .15s ease, background .15s ease, color .15s ease, transform .05s ease;
}

.btn:hover:not(:disabled) { border-color: var(--accent); }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: .45; cursor: default; }

.btn-primary {
  background: var(--accent);
  color: #14151A;
  border-color: var(--accent);
  font-weight: 600;
}
.btn-primary:hover:not(:disabled) { background: #F0D48A; border-color: #F0D48A; }

.btn-danger { color: #F2A9A5; border-color: #6A2E2C; }
.btn-danger:hover:not(:disabled) { border-color: var(--danger); }

.btn-wide { width: 100%; }

.link-btn {
  background: none;
  border: none;
  color: var(--accent);
  cursor: pointer;
  text-decoration: underline;
  font: inherit;
  font-size: 13px;
  padding: 0;
}

/* ---------- Стартовый экран ---------- */

.start-card {
  text-align: center;
  max-width: 660px;
  width: 100%;
  padding: 24px 16px;
}

.logo {
  font-size: 44px;
  letter-spacing: .32em;
  margin: 0 0 8px;
  font-weight: 700;
}

.tagline {
  color: var(--text-dim);
  letter-spacing: .12em;
  text-transform: uppercase;
  font-size: 12px;
  margin: 0 0 36px;
}

.resume-banner {
  background: var(--surface);
  border: 1px solid var(--accent);
  border-radius: 10px;
  padding: 14px 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 24px;
  font-size: 14px;
}

.mode-cards {
  display: flex;
  gap: 14px;
  justify-content: center;
  flex-wrap: wrap;
}

.mode-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 22px 26px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-width: 230px;
  text-align: left;
  color: var(--text);
  font: inherit;
  transition: border-color .15s ease, transform .15s ease, box-shadow .15s ease;
}

.mode-card:hover {
  border-color: var(--accent);
  transform: translateY(-2px);
  box-shadow: 0 10px 30px rgba(0, 0, 0, .35);
}

.mode-title { font-size: 17px; font-weight: 600; }
.mode-desc { color: var(--text-dim); font-size: 13px; }

.ai-settings {
  margin-top: 24px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.setting-group {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  flex-wrap: wrap;
}

.setting-label { color: var(--text-dim); font-size: 14px; }

.segmented {
  display: flex;
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow: hidden;
}

.seg {
  background: transparent;
  border: none;
  border-right: 1px solid var(--border);
  color: var(--text-dim);
  padding: 8px 16px;
  cursor: pointer;
  font: inherit;
  font-size: 14px;
  transition: color .15s ease, background .15s ease;
}

.seg:last-child { border-right: none; }
.seg:hover { color: var(--text); }
.seg.active { background: rgba(232, 196, 101, .16); color: var(--accent); }

/* ---------- Игровой экран ---------- */

.game-layout {
  display: flex;
  gap: 22px;
  align-items: flex-start;
  justify-content: center;
  padding: 0 18px 24px;
  width: 100%;
  max-width: 1100px;
  margin: 0 auto;
}

.board-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  flex-shrink: 0;
}

.board-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: min(76vh, 660px, 94vw);
  margin-bottom: 10px;
  min-height: 22px;
  gap: 12px;
}

#turn-indicator { font-weight: 600; }

.thinking { color: var(--accent); font-size: 14px; }

.dots span { animation: blink 1.2s infinite; }
.dots span:nth-child(2) { animation-delay: .2s; }
.dots span:nth-child(3) { animation-delay: .4s; }

@keyframes blink {
  0%, 100% { opacity: .2; }
  50% { opacity: 1; }
}

#board-wrap {
  position: relative;
  width: min(76vh, 660px, 94vw);
  padding: 5px;
  border-radius: 14px;
  background: linear-gradient(135deg, #8A6A1C 0%, #E9CE6F 22%, #A87F1E 42%, #F2DD8B 62%, #9C761B 82%, #C9A227 100%);
  box-shadow: 0 14px 44px rgba(0, 0, 0, .5), 0 0 22px rgba(233, 206, 111, .16);
}

#board {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  aspect-ratio: 1;
  width: 100%;
  border-radius: 9px;
  overflow: hidden;
}

/* Лаковый блик на всей доске */
#board::after {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(115deg, rgba(255, 255, 255, .13) 0%, rgba(255, 255, 255, .05) 20%, rgba(255, 255, 255, 0) 38%, rgba(0, 0, 0, .07) 100%);
  pointer-events: none;
  z-index: 6;
  border-radius: 9px;
}

/* Деревянная мозаика клеток — оттенки из chessboard.jpeg:
   тёмные как на фото, светлые — охлаждённый вариант для контраста */
.cell {
  position: relative;
  background-image:
    repeating-linear-gradient(90deg, rgba(74, 60, 50, .05) 0 1.5px, rgba(255, 255, 255, 0) 1.5px 7px),
    linear-gradient(180deg, rgba(255, 255, 255, .10), rgba(255, 255, 255, 0) 45%, rgba(0, 0, 0, .06));
}

.cell.light { background-color: #C3B1A9; }
.cell.light:nth-child(even) { background-color: #CAB9B1; }

.cell.dark {
  background-color: #4A2A28;
  background-image:
    repeating-linear-gradient(90deg, rgba(0, 0, 0, .22) 0 1.5px, rgba(255, 255, 255, 0) 1.5px 7px),
    linear-gradient(180deg, rgba(255, 255, 255, .07), rgba(255, 255, 255, 0) 45%, rgba(0, 0, 0, .14));
}

.cell.dark:nth-child(even) { background-color: #4F2D2A; }

.cell img.piece {
  position: absolute;
  max-height: 92%;
  max-width: 92%;
  height: auto;
  width: auto;
  bottom: 4%;
  left: 50%;
  transform: translateX(-50%);
  pointer-events: none;
  user-select: none;
  -webkit-user-drag: none;
  filter: drop-shadow(0 2px 2px rgba(0, 0, 0, .4));
  z-index: 2;
}

/* Координаты */
.coord {
  position: absolute;
  font-size: 11px;
  font-weight: 600;
  line-height: 1;
  z-index: 3;
  pointer-events: none;
}

.coord.cf { right: 4px; bottom: 2px; }
.coord.cr { left: 4px; top: 2px; }
.cell.light .coord { color: #4A3B30; }
.cell.dark .coord { color: #E8DCC8; }

/* Подсветки-оверлеи */
.ov {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 1;
}

.ov-last { background: rgba(232, 196, 101, .30); }
.cell.light .ov-last { background: rgba(214, 168, 64, .4); }
.ov-sel { background: rgba(232, 196, 101, .45); }

.ov-check {
  background: rgba(224, 86, 79, .50);
  animation: checkPulse 1.2s ease-in-out infinite;
}

@keyframes checkPulse {
  0%, 100% { opacity: .9; }
  50% { opacity: .4; }
}

/* Индикаторы легальных ходов — адаптивные цвета:
   тёмные точки на светлых клетках, светлые на тёмных */
.cell.legal::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 28%;
  height: 28%;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  z-index: 3;
  pointer-events: none;
}

.cell.light.legal::after { background: rgba(70, 90, 70, .8); }
.cell.dark.legal::after { background: rgba(240, 233, 214, .85); }

.cell.legal-capture::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 86%;
  height: 86%;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  border-width: 3px;
  border-style: solid;
  z-index: 3;
  pointer-events: none;
}

.cell.light.legal-capture::after { border-color: rgba(70, 90, 70, .85); }
.cell.dark.legal-capture::after { border-color: rgba(240, 233, 214, .9); }

.cell.hint-cell { box-shadow: inset 0 0 0 3px var(--hint); }

/* Анимация перемещения фигуры */
.piece-anim {
  position: absolute;
  width: auto;
  transform: translate(-50%, -50%);
  pointer-events: none;
  user-select: none;
  z-index: 8;
  filter: drop-shadow(0 3px 3px rgba(0, 0, 0, .45));
}

.cell img.piece.piece-hidden { opacity: 0; }

.hint-arrow {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 7;
}

.preview-banner {
  margin-top: 10px;
  color: var(--text-dim);
  font-size: 13px;
  text-align: center;
}

/* ---------- Боковая панель ---------- */

.side-panel {
  width: 340px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  flex-shrink: 0;
}

.panel-block { display: flex; flex-direction: column; gap: 8px; }

.panel-label {
  margin: 0;
  color: var(--text-dim);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .08em;
}

.move-history {
  max-height: 44vh;
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px;
  font-family: ui-monospace, "JetBrains Mono", "Cascadia Mono", Consolas, monospace;
  font-size: 14px;
}

.mv-empty { color: var(--text-dim); padding: 6px; }

.mv-row {
  display: flex;
  gap: 8px;
  padding: 3px 6px;
  border-radius: 4px;
  align-items: baseline;
}

.mv-num {
  color: var(--text-dim);
  min-width: 34px;
  text-align: right;
  user-select: none;
  font-size: 13px;
}

.mv-ply {
  cursor: pointer;
  padding: 0 5px;
  border-radius: 3px;
  color: var(--text);
  flex: 1;
}

.mv-ply:hover { background: rgba(232, 196, 101, .15); }
.mv-ply.active { background: var(--accent-soft); color: #fff; }

.buttons-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

/* Переключатель подсветки ходов */
.switch-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  cursor: pointer;
  user-select: none;
  padding: 4px 0;
}

.switch-label { font-size: 14px; }

.switch {
  position: relative;
  width: 44px;
  height: 24px;
  flex-shrink: 0;
}

.switch input {
  opacity: 0;
  width: 100%;
  height: 100%;
  position: absolute;
  margin: 0;
  cursor: pointer;
  z-index: 2;
}

.switch .slider {
  position: absolute;
  inset: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  transition: background .15s ease, border-color .15s ease;
}

.switch .slider::after {
  content: '';
  position: absolute;
  top: 2px;
  left: 2px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--text-dim);
  transition: transform .15s ease, background .15s ease;
}

.switch input:checked + .slider {
  background: rgba(232, 196, 101, .2);
  border-color: var(--accent);
}

.switch input:checked + .slider::after {
  transform: translateX(20px);
  background: var(--accent);
}

/* ---------- Модальные окна ---------- */

.modal {
  position: fixed;
  inset: 0;
  background: rgba(10, 11, 14, .62);
  backdrop-filter: blur(3px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 16px;
}

.modal-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 26px 30px;
  min-width: 300px;
  max-width: 92vw;
  text-align: center;
  box-shadow: 0 24px 70px rgba(0, 0, 0, .5);
}

.modal-wide { width: 560px; max-width: 92vw; }

.modal-title { margin: 0 0 8px; font-size: 18px; }
.modal-sub { margin: 0 0 18px; color: var(--text-dim); font-size: 14px; }

.result-title { margin: 0 0 8px; font-size: 28px; }
.result-text { margin: 0 0 22px; color: var(--text-dim); }

.modal-actions {
  display: flex;
  gap: 10px;
  justify-content: center;
  flex-wrap: wrap;
}

.promo-row {
  display: flex;
  gap: 10px;
  justify-content: center;
  margin: 14px 0 18px;
}

.promo-btn {
  width: 76px;
  height: 76px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  padding: 6px;
  transition: border-color .15s ease, transform .1s ease;
}

.promo-btn:hover { border-color: var(--accent); transform: translateY(-2px); }
.promo-btn img { width: 100%; height: 100%; object-fit: contain; }

#pgn-import-text {
  width: 100%;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px;
  font-family: ui-monospace, Consolas, monospace;
  font-size: 13px;
  resize: vertical;
}

.error { color: #F2A9A5; font-size: 13px; margin: 10px 0 0; }

/* ---------- Адаптивность ---------- */

@media (max-width: 1023px) {
  .game-layout {
    flex-direction: column;
    align-items: center;
  }

  .board-header { width: min(640px, 92vw); }

  .side-panel {
    width: min(640px, 92vw);
    flex-direction: column;
  }

  .move-history { max-height: 32vh; }
}

@media (max-width: 639px) {
  .logo { font-size: 34px; letter-spacing: .24em; }
  .mode-cards { flex-direction: column; align-items: stretch; }
  .mode-card { min-width: 0; }
  .seg { padding: 8px 12px; }
}
