:root {
  --wood-light: #f5e6c8;
  --wood-dark: #d4b483;
  --wood-border: #8b6f47;
  --ui-bg: rgba(40, 25, 10, 0.88);
  --text: #e8d9b5;
  --accent: #d4a35a;
  --ink: #1a1a1a;
  --jade: #2d6a4f;
}

/* 🔒 Блокировка прокрутки (требование Яндекс Игр п. 1.10.2) */
html, body {
  overflow: hidden;
  position: fixed;
  width: 100%;
  height: 100%;
  overscroll-behavior: none;
}

/* 🔧 ИСПРАВЛЕНО: Добавлена звёздочка * для глобального сброса */
*, *::before, *::after { 
  box-sizing: border-box; 
  margin: 0; 
  padding: 0; 
}

body {
  width: 100%;
  height: 100%;
  background: url('../assets/bg.png') no-repeat center center fixed;
  background-size: cover;
  background-color: #1a1008; /* Запасной цвет, если картинка не загрузится */
  font-family: "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  display: flex; 
  justify-content: center; 
  align-items: center;
  color: var(--text);
  overflow: hidden;
  touch-action: manipulation;
  position: relative;
  font-size: 16px;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  padding: env(safe-area-inset-top) 0 env(safe-area-inset-bottom) 0;
}

/* ================= ЗАСТАВКА ================= */
#splash-screen {
  position: fixed; 
  inset: 0;
  min-height: 100dvh;
  background-color: #fdfbf7;
  background-image: url('../assets/splash.png');
  background-repeat: no-repeat;
  background-position: center center;
  background-size: 30%;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  align-items: center;
  padding-bottom: 100px;
  animation: fadeIn 0.5s ease-out;
}

#splash-screen.hidden {
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.8s ease;
}

.splash-content {
  text-align: center;
  width: 100%;
  color: #2c1a0b;
  text-shadow: 0 2px 6px rgba(253, 251, 247, 0.9);
  padding: 0 10px;
}

.loader-bar {
  width: 200px; 
  height: 6px;
  background: rgba(0,0,0,0.1);
  border-radius: 3px; 
  margin: 15px auto 0; 
  overflow: hidden;
}

.loader-fill {
  height: 100%; 
  width: 0%;
  background: linear-gradient(90deg, #8b6f47, #d4a35a);
  border-radius: 3px;
  animation: loading 3s ease-in-out forwards;
}

@keyframes loading {
  0% { width: 0%; }
  100% { width: 100%; }
}

@keyframes fadeIn { 
  from { opacity: 0; } 
  to { opacity: 1; } 
}

/* ================= ГЛАВНОЕ МЕНЮ ================= */
#menu-overlay {
  position: fixed; 
  inset: 0;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  display: flex; 
  justify-content: center; 
  align-items: center;
  z-index: 100;
  animation: fadeIn 0.4s ease;
}

.menu-card {
  background: linear-gradient(145deg, #3a2818 0%, #24170d 100%);
  border: 2px solid #5c4022;
  border-radius: 20px;
  padding: 32px 28px;
  width: min(90vw, 420px);
  text-align: center;
  box-shadow: 0 20px 50px rgba(0,0,0,0.7), inset 0 0 0 1px rgba(255,255,255,0.1);
  color: #e8d9b5;
  transform: translateY(20px) scale(0.96);
  animation: slideUp 0.5s cubic-bezier(0.16, 1, 0.3, 1) forwards;
  position: relative;
  overflow: hidden;
}

@keyframes slideUp {
  to { transform: translateY(0) scale(1); opacity: 1; }
}

.menu-card::before {
  content: '';
  position: absolute; 
  top: 0; 
  left: 0; 
  right: 0; 
  height: 4px;
  background: linear-gradient(90deg, var(--ink), var(--jade), var(--ink));
  opacity: 0.8;
}

.menu-header h1 {
  font-size: 2.8rem;
  margin: 0 0 8px;
  font-family: "PingFang SC", serif;
  color: #d4a35a;
  letter-spacing: 2px;
  text-shadow: 0 2px 4px rgba(0,0,0,0.5);
}

.subtitle {
  font-size: 1rem;
  color: #c4a574;
  margin-bottom: 24px;
  font-style: italic;
}

.menu-group {
  margin-bottom: 16px;
  text-align: left;
  position: relative;
}

.menu-group label {
  display: block;
  font-weight: 600;
  margin-bottom: 6px;
  font-size: 1rem;
  color: #c4a574;
}

select {
  appearance: none;
  width: 100%;
  padding: 12px 16px;
  border-radius: 10px;
  border: 1px solid #5c4022;
  background-color: #1a1008;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%23d4a35a' stroke-width='2'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 14px center;
  color: #e8d9b5;
  font-size: 1rem;
  cursor: pointer;
  transition: 0.2s;
}

select:hover {
  border-color: #d4a35a;
  background-color: #24170d;
}

select:focus {
  outline: none;
  border-color: #d4a35a;
  box-shadow: 0 0 0 3px rgba(212, 163, 90, 0.2);
}

.primary-btn {
  width: 100%;
  padding: 14px;
  background: linear-gradient(135deg, #d4a35a 0%, #b8862e 100%);
  color: #1a1008;
  border: none;
  border-radius: 12px;
  font-size: 1.2rem;
  font-weight: bold;
  cursor: pointer;
  margin-top: 12px;
  text-shadow: 0 1px 0 rgba(255,255,255,0.2);
  box-shadow: 0 4px 15px rgba(212, 163, 90, 0.3);
  transition: all 0.25s cubic-bezier(0.16, 1, 0.3, 1);
  position: relative;
  overflow: hidden;
}

.primary-btn::after {
  content: ''; 
  position: absolute; 
  inset: 0;
  background: linear-gradient(135deg, transparent, rgba(255,255,255,0.3), transparent);
  transform: translateX(-100%);
  transition: 0.4s;
}

.primary-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(212, 163, 90, 0.5);
}

.primary-btn:hover::after {
  transform: translateX(100%);
}

.primary-btn:active {
  transform: translateY(0) scale(0.98);
}

.rules-btn {
  background: transparent !important;
  border: 1px solid #5c4022 !important;
  color: #c4a574 !important;
  padding: 10px 20px !important;
  border-radius: 8px;
  cursor: pointer;
  margin: 12px 0;
  font-size: 0.95rem;
  transition: 0.2s;
}

.rules-btn:hover {
  border-color: #d4a35a !important;
  color: #d4a35a !important;
  background: rgba(212, 163, 90, 0.1) !important;
}

/* ================= ИГРОВОЙ КОНТЕЙНЕР ================= */
#game-container {
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: auto;              /* 🔑 Подстраивается под содержимое */
  max-height: 100dvh;        /* Не больше экрана при zoom in */
  width: min(95vw, 640px);
  background: linear-gradient(145deg, rgba(245, 230, 200, 0.92), rgba(212, 180, 131, 0.92));
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border: 3px solid rgba(139, 111, 71, 0.8);
  border-radius: 16px;
  box-shadow: 0 10px 40px rgba(0,0,0,0.4), inset 0 0 20px rgba(255,255,255,0.1);
  padding: 12px 10px;
  position: relative;
  animation: fadeIn 0.3s ease;
  box-sizing: border-box;
  overflow: hidden;
}

canvas {
  display: block;
  /* 🔑 КЛЮЧЕВАЯ СТРОКА: доска будет квадратом, но не больше оставшегося места */
  width: min(100%, calc(100dvh - 180px));
  aspect-ratio: 1 / 1;
  max-width: 100%;
  max-height: calc(100dvh - 180px);
  background: radial-gradient(ellipse at 30% 20%, #e8d4b8 0%, #c4a574 100%);
  border-radius: 12px;
  box-shadow: 0 6px 20px rgba(0,0,0,0.3), inset 0 0 0 2px rgba(139, 90, 43, 0.2);
  cursor: crosshair;
  touch-action: none;
}

/* ================= СЧЁТ И КНОПКИ ================= */
.scores {
  display: flex; 
  gap: 14px; 
  width: 100%; 
  justify-content: space-between;
}

.score-box {
  background: var(--ui-bg);
  padding: 10px 18px;
  border-radius: 10px;
  text-align: center;
  flex: 1;
  border: 1px solid rgba(255,255,255,0.15);
  transition: 0.2s;
}

.score-box span {
  display: block;
  font-size: 1rem;
  color: #d4c4a0;
  margin-bottom: 4px;
}

.score-box b {
  font-size: 1.5rem;
  color: #ffd700;
  display: block;
  font-weight: bold;
  text-shadow: 0 1px 3px rgba(0,0,0,0.4);
}

.controls {
  display: flex;
  gap: 8px;
  width: 100%;
  justify-content: center;
  flex-wrap: wrap;
}

.controls button {
  background: var(--ui-bg);
  border: 1px solid var(--wood-border);
  color: var(--text);
  padding: 12px 16px;
  border-radius: 10px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: 0.2s;
  flex: 1;
  min-width: 80px;
  max-width: 140px;
}

.controls button:hover {
  background: #4a3218;
  transform: translateY(-1px);
}

.controls button:active {
  transform: scale(0.96);
}

.scores, .status, .controls {
  flex-shrink: 0;
  width: 100%;
}

#btn-menu {
  background: #5a3a20;
}

.status {
  font-weight: 600;
  padding: 6px 10px; 
  text-align: center;
  min-height: 28px;
  color: var(--accent);
  width: 100%;
  font-size: 1rem;
  background: rgba(40, 25, 10, 0.75);
  border-radius: 8px;
  margin: 0; 
  line-height: 1.3;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ================= КНОПКИ UNDO/HINT ================= */
#btn-undo, #btn-hint {
  background: linear-gradient(145deg, #c9a97c, #b8956a) !important;
  border: 2px solid #8b6f47 !important;
  color: #2c1a0b !important;
  width: 44px; 
  height: 44px; 
  min-width: 44px !important;
  padding: 0 !important; 
  border-radius: 10px !important;
  font-size: 1.4rem; 
  display: flex; 
  align-items: center; 
  justify-content: center;
  box-shadow: 0 3px 8px rgba(0,0,0,0.25), inset 0 1px 0 rgba(255,255,255,0.5), inset 0 -2px 4px rgba(0,0,0,0.15);
  cursor: pointer; 
  transition: all 0.2s ease;
  position: relative; 
  overflow: hidden;
  flex: none !important;
}

#btn-undo:hover, #btn-hint:hover {
  transform: translateY(-2px);
  background: linear-gradient(145deg, #d4b483, #c4a57b) !important;
  box-shadow: 0 5px 12px rgba(0,0,0,0.3), inset 0 1px 0 rgba(255,255,255,0.6);
}

#btn-undo:active, #btn-hint:active {
  transform: translateY(0) scale(0.96);
  box-shadow: 0 2px 5px rgba(0,0,0,0.2), inset 0 2px 4px rgba(0,0,0,0.1);
}

#btn-undo:disabled, #btn-hint:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none !important;
  filter: grayscale(0.6);
}

#btn-undo::before, #btn-hint::before {
  content: '';
  position: absolute;
  top: 3px; 
  left: 6px; 
  right: 6px; 
  height: 35%;
  background: linear-gradient(to bottom, rgba(255,255,255,0.4), transparent);
  border-radius: 8px 8px 0 0;
  pointer-events: none;
}

/* ================= КНОПКА ЗВУКА ================= */
#btn-sound {
  background: linear-gradient(145deg, #e0c495, #c9a97c) !important;
  border: 2px solid #6d5636 !important;
  color: #2c1a0b !important;
  font-weight: bold;
  width: 44px; 
  height: 44px; 
  min-width: 44px !important;
  padding: 0 !important; 
  border-radius: 50% !important;
  font-size: 1.3rem;
  display: flex; 
  align-items: center; 
  justify-content: center;
  box-shadow: 0 3px 8px rgba(0,0,0,0.3), inset 0 1px 0 rgba(255,255,255,0.6), inset 0 -2px 4px rgba(0,0,0,0.2);
  cursor: pointer; 
  transition: all 0.2s ease;
  position: relative; 
  overflow: hidden;
  flex: none !important;
}

#btn-sound::before {
  content: '';
  position: absolute;
  top: 4px; 
  left: 8px; 
  right: 8px; 
  height: 40%;
  background: linear-gradient(to bottom, rgba(255,255,255,0.35), transparent);
  border-radius: 50% 50% 0 0;
  pointer-events: none;
}

#btn-sound:hover {
  background: linear-gradient(145deg, #ebd4a5, #d4b483) !important;
  border-color: #5a4630 !important;
  transform: translateY(-2px);
  box-shadow: 0 5px 14px rgba(0,0,0,0.3), inset 0 1px 0 rgba(255,255,255,0.7);
}

#btn-sound:active {
  transform: translateY(0) scale(0.96);
}

#btn-sound.muted {
  opacity: 0.8;
  background: linear-gradient(145deg, #a89075, #8b7355) !important;
  box-shadow: 0 2px 6px rgba(0,0,0,0.15), inset 0 1px 0 rgba(255,255,255,0.2);
}

/* ================= МОДАЛЬНЫЕ ОКНА ================= */
.modal-overlay {
  position: fixed; 
  inset: 0;
  background: rgba(10, 8, 5, 0.75);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  display: flex; 
  justify-content: center; 
  align-items: center;
  z-index: 200;
  animation: fadeIn 0.3s ease;
  padding: 20px;
}

.modal-card {
  background: linear-gradient(145deg, #f9f3e3, #e8d5b0);
  border: 3px solid var(--wood-border);
  border-radius: 16px;
  padding: 28px 24px;
  width: min(90vw, 400px);
  text-align: center;
  box-shadow: 0 15px 40px rgba(0,0,0,0.5), inset 0 0 20px rgba(255,255,255,0.3);
  color: #3a2810;
  transform: translateY(20px) scale(0.96);
  animation: slideUp 0.4s cubic-bezier(0.16, 1, 0.3, 1) forwards;
  position: relative;
  overflow: hidden;
}

.modal-card::before {
  content: '';
  position: absolute; 
  top: 0; 
  left: 0; 
  right: 0; 
  height: 4px;
  background: linear-gradient(90deg, var(--ink), var(--jade), var(--ink));
  opacity: 0.8;
}

.modal-card h3 {
  font-size: 1.5rem;
  margin: 0 0 14px;
  color: #2c1a0b;
  text-align: center;
}

.modal-card p {
  margin: 0 0 18px;
  font-size: 1.05rem;
  color: #4a3218;
  line-height: 1.5;
}

.modal-actions {
  display: flex; 
  gap: 12px; 
  justify-content: center; 
  margin-top: 10px;
}

.btn-primary, .btn-secondary {
  flex: 1;
  padding: 8px 20px;
  border-radius: 10px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  border: none;
  transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
}

.btn-primary {
  background: linear-gradient(135deg, #8b6f47, #6d5636);
  color: #fff;
  box-shadow: 0 4px 12px rgba(109, 86, 54, 0.3);
}

.btn-primary:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(109, 86, 54, 0.4);
}

.btn-primary:active {
  transform: translateY(0) scale(0.98);
}

.btn-secondary {
  background: rgba(40, 25, 10, 0.85);
  color: #e8d9b5;
  border: 1px solid rgba(255,255,255,0.15);
}

.btn-secondary:hover {
  background: rgba(60, 40, 20, 0.95);
}

/* ================= ПРАВИЛА ИГРЫ ================= */
.rules-modal .modal-overlay {
  padding: 20px;
}

.rules-modal .modal-card {
  background: linear-gradient(145deg, #f9f3e3, #e8d5b0);
  width: min(95vw, 1000px);
  max-width: 95vw;
  max-height: 90vh;
  overflow: hidden;
  padding: 20px 28px;
  color: #3a2810;
}

.rules-scroll {
  max-height: 70vh;
  overflow-y: auto;
  padding-right: 15px;
  text-align: left;
  margin-top: 10px;
}

.rules-scroll::-webkit-scrollbar { width: 8px; }
.rules-scroll::-webkit-scrollbar-track { background: rgba(0,0,0,0.05); border-radius: 4px; }
.rules-scroll::-webkit-scrollbar-thumb { background: #c9a97c; border-radius: 4px; }
.rules-scroll::-webkit-scrollbar-thumb:hover { background: #b8956a; }

.rules-scroll h4 {
  color: #8b6f47;
  margin-top: 24px;
  margin-bottom: 10px;
  border-bottom: 2px solid rgba(139, 111, 71, 0.3);
  padding-bottom: 6px;
  font-size: 1.2rem;
}

.rules-scroll h4:first-child {
  margin-top: 0;
}

.rules-scroll p, .rules-scroll li {
  font-size: 1rem;
  line-height: 1.6;
  color: #4a3218;
  margin-bottom: 8px;
}

.rules-scroll ul {
  padding-left: 25px;
  margin: 10px 0;
}

.rules-scroll li {
  margin: 6px 0;
}

.rules-scroll .highlight {
  background: rgba(212, 163, 90, 0.25);
  padding: 2px 6px;
  border-radius: 4px;
  font-weight: 600;
}

/* Экран победы */
.victory-card {
  padding: 32px 28px;
}

.victory-icon {
  font-size: 4rem;
  margin-bottom: 12px;
  filter: drop-shadow(0 2px 4px rgba(0,0,0,0.2));
}

#victory-title {
  font-size: 2rem;
  margin: 0 0 12px;
  color: #2c1a0b;
}

#victory-details {
  font-size: 1.3rem;
  margin: 0 0 8px;
  font-weight: 600;
  color: #4a3218;
}

.diff-text {
  font-size: 1.05rem;
  color: #8b6f47;
  margin: 0 0 20px;
  font-style: italic;
}

/* ================= АДАПТИВНОСТЬ ================= */
@media (max-width: 768px) {
  .menu-card {
    padding: 26px 22px;
  }
  .menu-header h1 {
    font-size: 2.2rem;
  }
  .rules-modal .modal-card {
    width: min(95vw, 100%);
    padding: 24px 20px;
  }
  .rules-scroll h4 {
    font-size: 1.1rem;
  }
  .rules-scroll p, .rules-scroll li {
    font-size: 0.95rem;
  }
  #game-container {
    padding: 12px;
    border-width: 3px;
  }
  .score-box {
    padding: 8px 12px;
  }
  .score-box span {
    font-size: 0.9rem;
  }
  .score-box b {
    font-size: 1.3rem;
  }
  .controls button {
    padding: 10px 12px;
    font-size: 0.95rem;
  }
  #btn-undo, #btn-hint, #btn-sound {
    width: 40px; 
    height: 40px; 
    min-width: 40px !important;
    font-size: 1.2rem;
  }
}

@media (max-width: 480px) {
  body {
    font-size: 15px;
  }
  #splash-screen {
    padding-bottom: 60px;
  }
  .menu-card {
    padding: 22px 18px;
  }
  .menu-header h1 {
    font-size: 2rem;
  }
  .subtitle {
    font-size: 0.9rem;
  }
  select {
    padding: 10px 14px;
    font-size: 0.95rem;
  }
  .primary-btn {
    padding: 12px;
    font-size: 1.1rem;
  }
  .modal-card {
    padding: 22px 18px;
  }
  .modal-card h3 {
    font-size: 1.3rem;
  }
  .modal-card p {
    font-size: 0.95rem;
  }
}

/* Кнопка фоновой музыки */
#btn-bgm {
  background: linear-gradient(145deg, #a89075, #8b7355) !important;
  border: 2px solid #6d5636 !important;
  color: #2c1a0b !important;
}

#btn-bgm:hover {
  background: linear-gradient(145deg, #b8956a, #9c8060) !important;
}

#btn-bgm.muted {
  opacity: 0.5;
  filter: grayscale(0.8);
}