/* =========================================================================
   chess-board — style.css
   Тёмная контрастная и светлая темы, сетка 1/2/3 колонки, модалка, анимации.
   Поддержка split-screen режима для двух турниров.
   ========================================================================= */

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

:root {
  --fs: 1rem;
  --radius: 14px;
  --gap: 1.1rem;
  --header-h: 92px;
  --split-header-h: 64px;
  --transition: 220ms cubic-bezier(.4,.0,.2,1);
}

/* ---------- ТЕМНАЯ ТЕМА (по умолчанию) ---------- */
body.theme-dark {
  --bg: #0b1020;
  --bg-grad: radial-gradient(ellipse at top, #141a30 0%, #0b1020 60%);
  --surface: #141a30;
  --surface-2: #1b2240;
  --surface-3: #232b4d;
  --border: rgba(255,255,255,.08);
  --text: #f5f7ff;
  --text-dim: #a7b0c9;
  --text-muted: #6b7390;
  --accent: #ffb400;
  --accent-2: #2dd4bf;
  --white-piece: #f8fafc;
  --black-piece: #0b0f1a;
  --white-piece-border: #cbd5e1;
  --black-piece-border: #4b5563;
  --status-ok: #34d399;
  --status-warn: #fbbf24;
  --status-err: #f87171;
  --result: #ffb400;
  --badge-bg: linear-gradient(135deg, #ffb400 0%, #ff7a00 100%);
  --badge-text: #1a0d00;
  --row-flash: rgba(255,180,0,.18);
  --divider: rgba(255,255,255,.12);
}

/* ---------- СВЕТЛАЯ ТЕМА ---------- */
body.theme-light {
  --bg: #f4f6fb;
  --bg-grad: radial-gradient(ellipse at top, #ffffff 0%, #e9eef6 60%);
  --surface: #ffffff;
  --surface-2: #f1f4fa;
  --surface-3: #e3e9f3;
  --border: rgba(15,23,42,.10);
  --text: #0f172a;
  --text-dim: #475569;
  --text-muted: #94a3b8;
  --accent: #ea580c;
  --accent-2: #0d9488;
  --white-piece: #ffffff;
  --black-piece: #1e293b;
  --white-piece-border: #cbd5e1;
  --black-piece-border: #0b1020;
  --status-ok: #059669;
  --status-warn: #d97706;
  --status-err: #dc2626;
  --result: #ea580c;
  --badge-bg: linear-gradient(135deg, #ea580c 0%, #c2410c 100%);
  --badge-text: #ffffff;
  --row-flash: rgba(234,88,12,.18);
  --divider: rgba(15,23,42,.15);
}

html, body { height: 100%; }

body {
  font-family: 'Segoe UI', 'Inter', 'Roboto', 'Helvetica Neue', Arial, 'Noto Sans', sans-serif;
  background: var(--bg-grad);
  background-attachment: fixed;
  color: var(--text);
  font-size: var(--fs);
  overflow: hidden;
  transition: background var(--transition), color var(--transition);
}

kbd {
  display: inline-block;
  background: var(--surface-3);
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 6px;
  padding: 2px 8px;
  font-family: inherit;
  font-size: .9em;
  font-weight: 600;
  color: var(--text);
}

code { font-family: 'JetBrains Mono', 'Consolas', monospace; color: var(--accent-2); }

/* ---------- ШАПКА (режим одного турнира) ---------- */
#topbar {
  position: fixed; top: 0; left: 0; right: 0;
  height: var(--header-h);
  display: grid;
  grid-template-columns: 1fr auto auto;
  align-items: center;
  gap: 1.5rem;
  padding: 0 1.8rem;
  background: linear-gradient(180deg, rgba(0,0,0,.25), rgba(0,0,0,0));
  z-index: 10;
  pointer-events: none;
}
.theme-light #topbar {
  background: linear-gradient(180deg, rgba(255,255,255,.55), rgba(255,255,255,0));
}

#topbar > * { pointer-events: auto; }

.title-wrap {
  display: flex;
  flex-direction: column;
  min-width: 0;
}
#tournamentTitle {
  font-size: 1.65em;
  font-weight: 700;
  line-height: 1.1;
  letter-spacing: -.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: pointer;
  user-select: none;
  max-width: 70vw;
}
#tournamentTitle.flash {
  animation: titleFlash 1.2s ease;
}
@keyframes titleFlash {
  0%   { color: var(--text); }
  30%  { color: var(--accent); text-shadow: 0 0 24px var(--accent); }
  100% { color: var(--text); text-shadow: none; }
}
.title-hint {
  font-size: .72em;
  color: var(--text-muted);
  margin-top: 2px;
  opacity: .6;
}

/* Бейдж тура */
.round-badge {
  background: var(--badge-bg);
  color: var(--badge-text);
  font-weight: 800;
  font-size: 1.45em;
  letter-spacing: .04em;
  padding: .35em 1.1em;
  border-radius: 999px;
  box-shadow: 0 6px 20px rgba(0,0,0,.35);
  white-space: nowrap;
  transition: transform var(--transition);
}
.round-badge.flash {
  animation: badgePulse 1.6s ease;
}
@keyframes badgePulse {
  0%   { transform: scale(1); box-shadow: 0 6px 20px rgba(0,0,0,.35); }
  20%  { transform: scale(1.18); box-shadow: 0 0 50px var(--accent); }
  100% { transform: scale(1); box-shadow: 0 6px 20px rgba(0,0,0,.35); }
}

/* Индикатор состояния */
.status {
  display: flex; align-items: center; gap: .6rem;
  font-size: .9em;
  color: var(--text-dim);
  white-space: nowrap;
}
.status-dot {
  width: 12px; height: 12px; border-radius: 50%;
  background: var(--status-warn);
  box-shadow: 0 0 0 0 currentColor;
  animation: dotPulse 2s infinite;
  flex: 0 0 auto;
}
.status-dot.ok   { background: var(--status-ok); animation: none; }
.status-dot.err  { background: var(--status-err); animation: none; }
.status-dot.warn { background: var(--status-warn); }
@keyframes dotPulse {
  0%   { box-shadow: 0 0 0 0 var(--status-warn); }
  70%  { box-shadow: 0 0 0 10px rgba(251,191,36,0); }
  100% { box-shadow: 0 0 0 0 rgba(251,191,36,0); }
}
.status-time { color: var(--text-muted); }

/* =========================================================================
   SPLIT-SCREEN РЕЖИМ (два турнира)
   ========================================================================= */

.split-container {
  position: fixed;
  inset: 0;
  display: flex;
  z-index: 5;
}

.split-half {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
}

.split-header {
  display: grid;
  grid-template-columns: 1fr auto auto;
  align-items: center;
  gap: 1rem;
  padding: 0 1.2rem;
  height: var(--split-header-h);
  background: linear-gradient(180deg, rgba(0,0,0,.2), rgba(0,0,0,0));
  border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
}
.theme-light .split-header {
  background: linear-gradient(180deg, rgba(255,255,255,.5), rgba(255,255,255,0));
}

.split-title-wrap {
  min-width: 0;
}
.split-title {
  font-size: 1.2em;
  font-weight: 700;
  line-height: 1.2;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.split-title.flash {
  animation: titleFlash 1.2s ease;
}

.split-round-badge {
  background: var(--badge-bg);
  color: var(--badge-text);
  font-weight: 800;
  font-size: 1.1em;
  letter-spacing: .04em;
  padding: .3em .9em;
  border-radius: 999px;
  box-shadow: 0 4px 14px rgba(0,0,0,.3);
  white-space: nowrap;
}
.split-round-badge.flash {
  animation: badgePulse 1.6s ease;
}

.split-status {
  display: flex;
  align-items: center;
  gap: .5rem;
  font-size: .85em;
  color: var(--text-dim);
}
.split-time {
  color: var(--text-muted);
}

/* Разделитель между половинами */
.split-divider {
  width: 2px;
  background: var(--divider);
  flex: 0 0 auto;
  position: relative;
}
.split-divider::before {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(180deg, transparent 0%, var(--accent) 50%, transparent 100%);
  opacity: .3;
}

/* Доска в split-режиме */
.split-board {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 1rem;
  scrollbar-width: none;
}
.split-board::-webkit-scrollbar { width: 0; }

/* В split-режиме сетка всегда 1 колонка на каждую половину (для читаемости) */
.split-board .pairings-grid {
  grid-template-columns: 1fr;
  gap: .8rem;
  max-width: none;
  padding: 0;
}

/* Карточки в split-режиме — компактнее */
.split-board .pairing-card {
  min-height: 56px;
  padding: .6rem .8rem;
  gap: .6rem;
}
.split-board .board-num {
  min-width: 48px;
  padding-right: .5rem;
}
.split-board .board-num .num {
  font-size: 1.2em;
}
.split-board .player-name {
  font-size: .95em;
}
.split-board .player-rating {
  font-size: .75em;
}
.split-board .piece {
  width: 26px;
  height: 26px;
  font-size: 1.1em;
}
.split-board .result .score,
.split-board .result .vs {
  font-size: .95em;
}

/* Скрываем основную шапку и доску в split-режиме */
body.split-mode #topbar,
body.split-mode #board,
body.split-mode #fadeBottom {
  display: none !important;
}

/* Показываем split-container в split-режиме */
body.split-mode #splitContainer {
  display: flex !important;
}

/* ---------- ОСНОВНАЯ ОБЛАСТЬ (режим одного турнира) ---------- */
#board {
  padding-top: calc(var(--header-h) + 1rem);
  padding-bottom: 80px;
  height: 100vh;
  overflow-y: auto;
  overflow-x: hidden;
  scroll-behavior: smooth;
}
#board::-webkit-scrollbar { width: 0; }
#board { scrollbar-width: none; }

/* Сетка карточек */
.pairings-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: var(--gap);
  padding: 0 1.8rem;
  max-width: 2560px;
  margin: 0 auto;
}
body.cols-1 .pairings-grid { grid-template-columns: 1fr; }
body.cols-2 .pairings-grid { grid-template-columns: repeat(2, 1fr); }
body.cols-3 .pairings-grid { grid-template-columns: repeat(3, 1fr); }

@media (max-width: 1100px) {
  body.cols-3 .pairings-grid { grid-template-columns: repeat(2, 1fr); }
}

/* ---------- КАРТОЧКА СТОЛА ---------- */
.pairing-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .85rem 1rem;
  display: grid;
  grid-template-columns: auto 1fr auto 1fr;
  grid-template-areas:
    "match match match match"
    "board white result black";
  align-items: center;
  gap: .8rem;
  min-height: 70px;
  animation: cardIn .35s ease both;
  transition: transform var(--transition), box-shadow var(--transition);
}
.pairing-card:hover { transform: translateY(-1px); box-shadow: 0 8px 22px rgba(0,0,0,.18); }

.pairing-card.flash {
  animation: cardFlash 2s ease;
}
@keyframes cardFlash {
  0%, 100% { background: var(--surface); }
  20%      { background: var(--row-flash); box-shadow: 0 0 30px var(--accent); }
}

@keyframes cardIn {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* Номер стола */
.board-num {
  grid-area: board;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  min-width: 60px;
  padding-right: .6rem;
  border-right: 1px solid var(--border);
}
.board-num .num {
  font-size: 1.5em;
  font-weight: 800;
  color: var(--accent);
  line-height: 1;
}
.board-num .label {
  font-size: .65em;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: .08em;
  margin-top: 2px;
}

/* Игрок */
.player {
  display: flex;
  align-items: center;
  gap: .65rem;
  min-width: 0;
}
.player.white { grid-area: white; justify-content: flex-start; }
.player.black { grid-area: black; justify-content: flex-end; text-align: right; }

.piece {
  flex: 0 0 auto;
  width: 30px; height: 30px;
  border-radius: 6px;
  border: 1px solid;
  display: flex; align-items: center; justify-content: center;
  font-size: 1.3em;
  line-height: 1;
}
.piece.white {
  background: var(--white-piece);
  border-color: var(--white-piece-border);
  color: #1e293b;
}
.piece.black {
  background: var(--black-piece);
  border-color: var(--black-piece-border);
  color: #f8fafc;
}

.player-info {
  display: flex; flex-direction: column;
  min-width: 0;
}
.player.white .player-info { align-items: flex-start; }
.player.black .player-info { align-items: flex-end; }

.player-name {
  font-size: 1.05em;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}
.player-rating {
  font-size: .8em;
  color: var(--text-dim);
  font-weight: 500;
}
.player-rating:empty { display: none; }

/* Звание игрока (для командных турниров) */
.player-title {
  font-size: .7em;
  font-weight: 700;
  color: var(--accent);
  margin-right: .4em;
  letter-spacing: .03em;
  text-transform: uppercase;
}

/* Название матча (для командных турниров) */
.match-info {
  grid-area: match;
  text-align: center;
  font-size: .75em;
  font-weight: 600;
  color: var(--text-dim);
  padding: .2rem 0;
  border-bottom: 1px solid var(--border);
  margin-bottom: .2rem;
  letter-spacing: .02em;
}

/* Результат */
.result {
  grid-area: result;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  padding: 0 .3rem;
  min-width: 70px;
}
.result .score {
  font-size: 1.05em;
  font-weight: 700;
  color: var(--result);
  letter-spacing: .03em;
  white-space: nowrap;
}
.result .vs {
  font-size: .85em;
  font-weight: 700;
  color: var(--text-muted);
  letter-spacing: .12em;
}

/* ---------- ПУСТОЕ СОСТОЯНИЕ ---------- */
.empty-state, .error-state {
  position: absolute;
  inset: 0;
  display: flex; align-items: center; justify-content: center;
  padding: 2rem;
  text-align: center;
  z-index: 5;
}
.empty-card, .error-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 3rem 4rem;
  max-width: 720px;
  box-shadow: 0 20px 60px rgba(0,0,0,.3);
}
.empty-icon {
  font-size: 4em;
  color: var(--accent);
  margin-bottom: .5rem;
  line-height: 1;
}
.empty-title {
  font-size: 1.5em;
  font-weight: 600;
  color: var(--text);
  margin-bottom: .8rem;
}
.empty-sub { color: var(--text-dim); margin-bottom: 1.5rem; }
.empty-example { color: var(--text-muted); font-size: .9em; }
.empty-example code { font-size: 1.05em; }

.error-icon { font-size: 3em; color: var(--status-err); margin-bottom: .5rem; }
.error-card p:nth-child(2) { font-size: 1.2em; font-weight: 600; margin-bottom: .5rem; }
.error-hint { color: var(--text-muted); font-size: .9em; }

.hidden { display: none !important; }

/* ---------- ПЛАВНЫЙ ЗАТЕХ ВНИЗУ ---------- */
.scroll-fade {
  position: fixed; left: 0; right: 0;
  height: 60px;
  pointer-events: none;
  z-index: 8;
}
.scroll-fade-bottom {
  bottom: 0;
  background: linear-gradient(180deg, rgba(11,16,32,0) 0%, var(--bg) 100%);
}
.theme-light .scroll-fade-bottom {
  background: linear-gradient(180deg, rgba(244,246,251,0) 0%, var(--bg) 100%);
}

/* ---------- МОДАЛКА ---------- */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.65);
  backdrop-filter: blur(4px);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
  animation: overlayIn .18s ease;
}
.theme-light .modal-overlay { background: rgba(15,23,42,.45); }
@keyframes overlayIn { from { opacity: 0; } to { opacity: 1; } }

.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  width: min(560px, 92vw);
  max-height: 92vh;
  overflow-y: auto;
  box-shadow: 0 30px 80px rgba(0,0,0,.45);
  animation: modalIn .25s cubic-bezier(.34,1.56,.64,1);
}
.modal-wide {
  width: min(720px, 92vw);
}
@keyframes modalIn {
  from { opacity: 0; transform: translateY(20px) scale(.96); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}

.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 1.2rem 1.4rem;
  border-bottom: 1px solid var(--border);
}
.modal-head h2 { font-size: 1.25em; font-weight: 700; }
.modal-close {
  background: transparent;
  border: none;
  color: var(--text-dim);
  font-size: 1.6em;
  line-height: 1;
  cursor: pointer;
  padding: 0 .3rem;
  border-radius: 6px;
  transition: var(--transition);
}
.modal-close:hover { color: var(--text); background: var(--surface-3); }

.modal-body { padding: 1.3rem 1.4rem; display: flex; flex-direction: column; gap: 1rem; }

/* Fieldset для группировки настроек турниров */
.tournament-fieldset {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1rem 1.2rem;
  display: flex;
  flex-direction: column;
  gap: .8rem;
}
.tournament-fieldset legend {
  font-size: .9em;
  font-weight: 700;
  color: var(--accent);
  padding: 0 .5rem;
  display: flex;
  align-items: center;
  gap: .5rem;
}

.optional {
  font-weight: 400;
  color: var(--text-muted);
  font-size: .85em;
}

/* Кнопка очистки второго турнира */
.clear-second-btn {
  background: var(--surface-3);
  border: 1px solid var(--border);
  color: var(--text-dim);
  width: 24px;
  height: 24px;
  border-radius: 50%;
  font-size: 1.1em;
  line-height: 1;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: var(--transition);
}
.clear-second-btn:hover {
  background: var(--status-err);
  color: #fff;
  border-color: var(--status-err);
}

.field { display: flex; flex-direction: column; gap: .35rem; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
.field-row.toggles { grid-template-columns: 1fr 1fr; gap: 1rem; align-items: center; }

.field label {
  font-size: .85em;
  font-weight: 600;
  color: var(--text-dim);
  letter-spacing: .02em;
}
.field input[type="text"],
.field input[type="number"],
.field select {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  padding: .6rem .8rem;
  font-size: 1em;
  font-family: inherit;
  transition: var(--transition);
}
.field input:focus, .field select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(255,180,0,.18);
}
.field input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}
.field-hint { color: var(--text-muted); font-size: .8em; line-height: 1.4; }
.field-hint code { font-size: .95em; word-break: break-all; }

/* Тумблер */
.toggle {
  display: flex; align-items: center; gap: .6rem;
  cursor: pointer;
  user-select: none;
}
.toggle input { position: absolute; opacity: 0; pointer-events: none; }
.toggle-track {
  position: relative;
  width: 44px; height: 24px;
  background: var(--surface-3);
  border: 1px solid var(--border);
  border-radius: 999px;
  transition: var(--transition);
  flex: 0 0 auto;
}
.toggle-track::after {
  content: '';
  position: absolute;
  top: 50%; left: 3px;
  transform: translateY(-50%);
  width: 18px; height: 18px;
  background: var(--text-dim);
  border-radius: 50%;
  transition: var(--transition);
}
.toggle input:checked + .toggle-track {
  background: var(--accent);
  border-color: var(--accent);
}
.toggle input:checked + .toggle-track::after {
  left: calc(100% - 21px);
  background: #fff;
}
.toggle-label { font-size: .9em; color: var(--text-dim); }

.modal-foot {
  display: flex; justify-content: flex-end; gap: .7rem;
  padding: 1rem 1.4rem;
  border-top: 1px solid var(--border);
}
.btn {
  border: none;
  border-radius: 8px;
  padding: .6rem 1.3rem;
  font-size: 1em;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: var(--transition);
}
.btn-primary {
  background: var(--accent);
  color: var(--badge-text);
}
.btn-primary:hover { filter: brightness(1.1); transform: translateY(-1px); }
.btn-secondary {
  background: var(--surface-3);
  color: var(--text);
  border: 1px solid var(--border);
}
.btn-secondary:hover { background: var(--surface-2); }

/* Масштаб шрифта через CSS-переменную */
body { font-size: var(--fs); }

/* Подсказка по горячим клавишам на старте */
.startup-hint {
  position: fixed;
  bottom: 1.5rem; left: 50%;
  transform: translateX(-50%);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: .6rem 1.4rem;
  font-size: .85em;
  color: var(--text-dim);
  z-index: 9;
  opacity: .85;
  transition: opacity .3s;
}
.startup-hint.fade { opacity: 0; }
