/* Mobile-first: базовые стили — телефон в портретной ориентации. */

:root {
  --bg: #f2f2f7;
  --card: #ffffff;
  --text: #000000;
  --text-2: #6c6c70;
  --separator: rgba(60, 60, 67, 0.18);
  --accent: #0a84ff;
  --danger: #ff3b30;
  --control: #e9e9ee;
  --radius: 18px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-rounded: ui-rounded, "SF Pro Rounded", "Nunito", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --card: #1c1c1e;
    --text: #ffffff;
    --text-2: #98989f;
    --separator: rgba(84, 84, 88, 0.6);
    --control: #2c2c2e;
  }
}

*,
*::before,
*::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font: 17px/1.35 var(--font);
  -webkit-tap-highlight-color: transparent;
}

button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }

body.sheet-open { overflow: hidden; }

/* ---------- Экран настроек ---------- */

.setup {
  max-width: 640px;
  margin: 0 auto;
  padding: calc(var(--safe-t) + 12px) calc(var(--safe-r) + 16px) calc(var(--safe-b) + 112px) calc(var(--safe-l) + 16px);
}

.setup-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 8px 0 4px;
}

.setup-header h1,
.rules-header h1 {
  margin: 0;
  font-size: 34px;
  font-weight: 800;
  letter-spacing: -0.02em;
}

.icon-btn {
  flex: none;
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 50%;
  background: var(--card);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.12);
}

.group-title {
  margin: 24px 16px 8px;
  font-size: 15px;
  font-weight: 600;
  color: var(--text-2);
}

.group {
  overflow: hidden;
  margin-top: 8px;
  background: var(--card);
  border-radius: var(--radius);
}

.row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 52px;
  margin: 0;
  padding: 10px 16px;
  border: 0;
  width: 100%;
  background: none;
  text-align: left;
}

.row + .row { border-top: 0.5px solid var(--separator); }

.row small {
  display: block;
  font-size: 13px;
  color: var(--text-2);
}

.row-btn { color: var(--accent); }
.row-btn.danger { color: var(--danger); }
.row-btn:active { background: var(--control); }

.player-row {
  flex-direction: column;
  align-items: stretch;
  gap: 10px;
  padding: 12px 16px 14px;
}

.name-input {
  width: 100%;
  padding: 4px 0;
  border: 0;
  outline: none;
  background: none;
  font-size: 19px;
  font-weight: 600;
}
.name-input::placeholder { color: var(--text-2); opacity: 0.6; }

.swatches {
  display: flex;
  gap: 10px;
  overflow-x: auto;
  margin: 0 -16px;
  padding: 2px 16px;
  scrollbar-width: none;
}
.swatches::-webkit-scrollbar { display: none; }

.swatch {
  position: relative;
  flex: none;
  width: 36px;
  height: 36px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: var(--c);
}
/* Цвет соперника: приглушён и перечёркнут, выбрать нельзя. */
.swatch.taken { opacity: 0.3; cursor: not-allowed; }
.swatch.taken::before {
  content: "";
  position: absolute;
  inset: 0;
  margin: auto;
  width: 2.5px;
  height: 30px;
  border-radius: 2px;
  background: var(--text);
  transform: rotate(45deg);
}

.swatch.selected::after {
  content: "";
  position: absolute;
  inset: 0;
  margin: auto;
  width: 10px;
  height: 17px;
  border: solid #fff;
  border-width: 0 3px 3px 0;
  transform: translateY(-2px) rotate(45deg);
}

.segmented {
  display: flex;
  width: 100%;
  padding: 2px;
  border-radius: 10px;
  background: var(--control);
}
.segmented button {
  flex: 1;
  min-width: 0;
  min-height: 36px;
  padding: 4px 8px;
  border: 0;
  border-radius: 8px;
  background: none;
  font-size: 15px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: background 0.2s;
}
.segmented button.on {
  background: var(--card);
  font-weight: 600;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.12);
}

select {
  flex: none;
  max-width: 55%;
  padding: 6px 0;
  border: 0;
  background: none;
  color: var(--text-2);
  text-align: right;
  text-align-last: right;
  direction: ltr;
}

.switch {
  appearance: none;
  -webkit-appearance: none;
  flex: none;
  position: relative;
  width: 51px;
  height: 31px;
  margin: 0;
  border-radius: 31px;
  background: var(--control);
  transition: background 0.2s;
  cursor: pointer;
}
.switch::before {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 27px;
  height: 27px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.25);
  transition: transform 0.2s;
}
.switch:checked { background: #34c759; }
.switch:checked::before { transform: translateX(20px); }
.switch:focus-visible, button:focus-visible, select:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.start-bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 16px calc(var(--safe-r) + 16px) calc(var(--safe-b) + 16px) calc(var(--safe-l) + 16px);
  background: linear-gradient(to top, var(--bg) 60%, transparent);
  pointer-events: none;
}

.primary-btn {
  display: block;
  width: 100%;
  max-width: 608px;
  margin: 0 auto;
  min-height: 56px;
  border: 0;
  border-radius: 28px;
  background: var(--accent);
  color: #fff;
  font-size: 20px;
  font-weight: 700;
  pointer-events: auto;
  box-shadow: 0 6px 20px rgba(10, 132, 255, 0.35);
}
.primary-btn:active { transform: scale(0.98); }

/* ---------- Табло ---------- */

body.board-mode {
  overflow: hidden;
  overscroll-behavior: none;
  background: #000;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.board {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  touch-action: manipulation;
}

.panel {
  flex: 1 1 0;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 16px;
  background: linear-gradient(to bottom, color-mix(in srgb, var(--c), #fff 14%), var(--c));
  color: #fff;
  cursor: pointer;
  text-align: center;
}
.panel:active { filter: brightness(1.08); }

/* Внешние края учитывают вырезы экрана, внутренние оставляют место под кнопки по центру. */
.panel:first-child { padding-top: calc(var(--safe-t) + 12px); padding-bottom: 44px; }
.panel:nth-child(2) { padding-top: 44px; padding-bottom: calc(var(--safe-b) + 12px); }

.panel-name {
  max-width: 100%;
  font: 700 clamp(24px, 7vw, 36px)/1.15 var(--font-rounded);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.serve-badge {
  padding: 6px 16px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.25);
  font-size: clamp(16px, 4.6vw, 20px);
  font-weight: 700;
  white-space: nowrap;
}
.invisible { visibility: hidden; }

.points-box {
  flex: 1 1 0;
  align-self: stretch;
  min-height: 0;
  display: grid;
  place-items: center;
  container-type: size;
}

.points {
  font: 900 min(92cqh, 62cqw)/1 var(--font-rounded);
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.03em;
}
.points.bump { animation: bump 0.28s ease-out; }
@keyframes bump {
  0% { transform: scale(0.82); opacity: 0.4; }
  100% { transform: scale(1); opacity: 1; }
}

.status {
  min-height: 1.3em;
  font-size: 18px;
  font-weight: 600;
  opacity: 0.92;
}

.counters {
  display: flex;
  gap: 28px;
}
.counters div { display: flex; flex-direction: column; align-items: center; }
.counters b {
  font: 700 clamp(28px, 9vw, 40px)/1 var(--font-rounded);
  font-variant-numeric: tabular-nums;
}
.counters span {
  margin-top: 2px;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  opacity: 0.8;
}

.center-controls {
  position: absolute;
  top: 50%;
  left: 50%;
  display: flex;
  align-items: center;
  gap: 14px;
  transform: translate(-50%, -50%);
  pointer-events: none;
}
.center-controls > * { pointer-events: auto; }

.glass-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 64px;
  height: 64px;
  padding: 0;
  border: 1px solid rgba(255, 255, 255, 0.45);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.28);
  -webkit-backdrop-filter: blur(16px) saturate(1.6);
  backdrop-filter: blur(16px) saturate(1.6);
  color: #fff;
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.2);
  font-weight: 700;
}
.glass-btn:active { transform: scale(0.94); }
.glass-btn:disabled { opacity: 0.4; }
.glass-btn.wide { width: auto; height: 50px; padding: 0 22px; font-size: 17px; }
.glass-btn.prominent { background: #fff; color: #111; }

.sets-summary {
  padding: 8px 12px;
  border-radius: 14px;
  background: rgba(0, 0, 0, 0.35);
  -webkit-backdrop-filter: blur(16px);
  backdrop-filter: blur(16px);
  color: #fff;
  font-size: 15px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  line-height: 1.3;
  text-align: center;
  white-space: nowrap;
}

/* Альбомная ориентация: игроки слева и справа. */
@media (orientation: landscape) {
  .board { flex-direction: row; }
  .panel { padding: calc(var(--safe-t) + 12px) 16px calc(var(--safe-b) + 12px); }
  .panel:first-child { padding-left: calc(var(--safe-l) + 16px); padding-right: 52px; padding-top: calc(var(--safe-t) + 12px); padding-bottom: calc(var(--safe-b) + 12px); }
  .panel:nth-child(2) { padding-left: 52px; padding-right: calc(var(--safe-r) + 16px); padding-top: calc(var(--safe-t) + 12px); padding-bottom: calc(var(--safe-b) + 12px); }
  .center-controls { flex-direction: column; }
  .panel-name { font-size: clamp(22px, 4vh + 8px, 36px); }
  .status { font-size: 16px; }
}

/* Низкий альбомный экран телефона: компактнее подписи. */
@media (orientation: landscape) and (max-height: 430px) {
  .panel { gap: 2px; }
  .serve-badge { padding: 4px 12px; font-size: 15px; }
  .counters b { font-size: 26px; }
  .glass-btn { width: 54px; height: 54px; }
}

/* Уведомление о смене сторон */
.ends-banner {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(0, 0, 0, 0.55);
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.25s, visibility 0.25s;
}
.ends-banner.visible { opacity: 1; visibility: visible; }

.ends-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  max-width: 360px;
  width: 100%;
  padding: 32px 24px;
  border: 1px solid rgba(255, 255, 255, 0.35);
  border-radius: 32px;
  background: rgba(255, 255, 255, 0.2);
  -webkit-backdrop-filter: blur(24px) saturate(1.6);
  backdrop-filter: blur(24px) saturate(1.6);
  color: #fff;
  text-align: center;
  transform: scale(0.92);
  transition: transform 0.25s;
}
.ends-banner.visible .ends-card { transform: scale(1); }
.ends-icon { font-size: 64px; line-height: 1; font-weight: 700; animation: wiggle 1.2s ease-in-out infinite; }
.ends-title { font: 800 clamp(30px, 9vw, 40px)/1.1 var(--font-rounded); }
.ends-hint { font-size: 15px; opacity: 0.75; }
@keyframes wiggle {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-8px); }
  75% { transform: translateX(8px); }
}

/* Экран победителя */
.winner {
  position: absolute;
  inset: 0;
  z-index: 6;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: calc(var(--safe-t) + 24px) calc(var(--safe-r) + 24px) calc(var(--safe-b) + 24px) calc(var(--safe-l) + 24px);
  background: linear-gradient(to bottom, color-mix(in srgb, var(--c), #fff 14%), var(--c));
  color: #fff;
  text-align: center;
  animation: fade-in 0.3s ease-out;
}
.trophy { font-size: 64px; line-height: 1; }
.winner-title { font: 600 28px/1.2 var(--font-rounded); }
.winner-name {
  max-width: 100%;
  font: 800 clamp(34px, 11vw, 52px)/1.1 var(--font-rounded);
  overflow-wrap: anywhere;
}
.winner-score { font-size: 24px; font-weight: 700; font-variant-numeric: tabular-nums; }
.winner-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 12px; margin-top: 12px; }

@media (orientation: landscape) and (max-height: 430px) {
  .winner { gap: 6px; }
  .trophy { font-size: 40px; }
}

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

.sheet {
  position: fixed;
  inset: 0;
  z-index: 20;
  animation: fade-in 0.2s ease-out;
}
.sheet.closing { animation: fade-out 0.2s ease-in forwards; }

@keyframes fade-in { from { opacity: 0; } }
@keyframes fade-out { to { opacity: 0; } }

.dialog-backdrop {
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding: 16px calc(var(--safe-r) + 12px) calc(var(--safe-b) + 12px) calc(var(--safe-l) + 12px);
  background: rgba(0, 0, 0, 0.4);
}

.dialog {
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: 100%;
  max-width: 420px;
  animation: slide-up 0.25s ease-out;
}
.dialog-title {
  padding: 16px;
  border-radius: 14px 14px 4px 4px;
  background: var(--card);
  color: var(--text-2);
  font-size: 14px;
  text-align: center;
}
.dialog-btn {
  min-height: 56px;
  border: 0;
  border-radius: 14px;
  background: var(--card);
  color: var(--accent);
  font-size: 19px;
  font-weight: 600;
}
.dialog-title + .dialog-btn { margin-top: -6px; border-radius: 4px 4px 14px 14px; }
.dialog-btn.danger { color: var(--danger); font-weight: 500; }

@keyframes slide-up { from { transform: translateY(40px); opacity: 0; } }

.rules-sheet {
  overflow-y: auto;
  overscroll-behavior: contain;
  background: var(--bg);
  -webkit-overflow-scrolling: touch;
}

.rules {
  max-width: 640px;
  margin: 0 auto;
  padding: 0 calc(var(--safe-r) + 16px) calc(var(--safe-b) + 32px) calc(var(--safe-l) + 16px);
  animation: slide-up 0.25s ease-out;
}

.rules-header {
  position: sticky;
  top: 0;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 0 -16px;
  padding: calc(var(--safe-t) + 12px) 16px 10px;
  background: color-mix(in srgb, var(--bg), transparent 15%);
  -webkit-backdrop-filter: blur(20px);
  backdrop-filter: blur(20px);
}
.rules-header h1 { font-size: 28px; }

.done-btn {
  flex: none;
  min-height: 40px;
  padding: 0 18px;
  border: 0;
  border-radius: 20px;
  background: var(--card);
  font-weight: 600;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.12);
}

.rules-title {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--text);
  font-size: 17px;
  font-weight: 700;
}
.rules-icon {
  display: inline-grid;
  place-items: center;
  width: 24px;
  font-size: 18px;
}

.rule { display: block; line-height: 1.45; padding-top: 12px; padding-bottom: 12px; }

.court-row { flex-direction: column; align-items: stretch; padding: 16px; }
.court { display: block; width: min(100%, 200px); height: auto; margin: 0 auto 6px; }
.legend { display: flex; align-items: center; gap: 10px; font-size: 14px; }
.legend i { flex: none; width: 12px; height: 12px; border-radius: 50%; background: var(--c); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
