:root {
  --bg: #12121a;
  --surface: #12121a;
  --accent: #f0c040;
  --accent-dim: #c9a035;
  --text: #e8e8f0;
  --text-dim: #8888a0;
  --key-bg: #2a2a35;
  --key-border: #3a3a48;
  --key-active: #f0c040;
  --correct: #4caf50;
  --error: #e53935;
  /* размер клавиши — общий для полей и клавиатуры */
  --key-size: clamp(22px, min(3.4vw, 4.8vh), 54px);
  --kb-gap: clamp(3px, 0.4vw, 6px);
}

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

html, body {
  height: 100%;
  overflow: hidden;
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: #12121a;
  color: var(--text);
  user-select: none;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  width: 100vw;
}

.module {
  position: relative;
  overflow: hidden;
  background: #12121a;
}

/* ========== TOP MODULE: 5 панелей ========== */
.top-module {
  --cell-size: clamp(12px, calc((40vh - 2 * var(--key-size) - 2.2rem) / 11), 28px);
  flex: 4 0 0; /* 40% */
  min-height: 0;
  background: #12121a;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.9rem;
  padding: var(--key-size) 1rem; /* поля = одна клавиша */
  position: relative;
  border: none;
}

.panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.3rem;
}

.panel-label {
  font-size: 0.72rem;
  color: var(--text-dim);
  letter-spacing: 0.03em;
  text-transform: uppercase;
  white-space: nowrap;
}

.panel-label span {
  color: var(--accent);
  font-weight: 600;
}

/* Мега-блоки скрыты по умолчанию (включая заголовок) */
.mega-storage-panel,
.mega-achievements-panel {
  display: none;
}

.mega-storage-panel.visible,
.mega-achievements-panel.visible {
  display: flex;
}


.letter-grid {
  display: grid;
  gap: 2px;
  background: #0c0c12;
  border: 1px solid #2a2a38;
  border-radius: 6px;
  padding: 3px;
  box-shadow: inset 0 0 16px rgba(0,0,0,0.4);
  /* columns/rows задаются из JS через --cell-size */
}

.grid-cell {
  width: var(--cell-size);
  height: var(--cell-size);
  background: #1a1a24;
  border-radius: 2px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--cell-size) * 0.55);
  font-weight: 700;
  color: transparent;
  overflow: hidden;
  line-height: 1;
  transition: background 0.12s, color 0.12s;
}

.grid-cell.filled {
  background: linear-gradient(145deg, #3a5a3a, #2d4a2d);
  color: #b8e0b8;
  text-transform: uppercase;
}

.grid-cell.filled-error {
  background: linear-gradient(145deg, #e53935, #b71c1c);
  color: #ffe0e0;
  text-transform: uppercase;
}

.falling-letter-error {
  background: linear-gradient(145deg, #e53935, #b71c1c) !important;
  color: #ffe0e0 !important;
}

.grid-shrink-clone .grid-cell.filled-error {
  background: linear-gradient(145deg, #e53935, #b71c1c);
  color: #ffe0e0;
}


.grid-cell.stored {
  background: linear-gradient(145deg, #c9a035, #8a7020);
  color: #1a1a10;
  font-size: calc(var(--cell-size) * 0.45);
}

.grid-cell.mega-stored {
  background: linear-gradient(145deg, #e8c050, #b89020);
  color: #1a1a10;
  font-size: calc(var(--cell-size) * 0.45);
  box-shadow: 0 0 4px rgba(240, 192, 64, 0.4);
}

/* Падающая буква */
.fly-layer {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 40;
  overflow: hidden;
}

.falling-letter {
  position: fixed;
  width: var(--cell-size);
  height: var(--cell-size);
  margin-left: calc(var(--cell-size) / -2);
  margin-top: calc(var(--cell-size) / -2);
  background: linear-gradient(145deg, #5a9a5a, #3a7a3a);
  border-radius: 2px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--cell-size) * 0.55);
  font-weight: 700;
  color: #e0ffe0;
  text-transform: uppercase;
  box-shadow: 0 2px 6px rgba(0,0,0,0.4);
  z-index: 41;
  will-change: transform;
}

/* Клон сетки при отправке на склад */
.grid-shrink-clone {
  display: grid !important;
  gap: 2px;
  background: #0c0c12;
  border: 1px solid #2a2a38;
  border-radius: 6px;
  padding: 3px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.5);
  transform-origin: center center;
}

.grid-shrink-clone .grid-cell {
  width: var(--cell-size);
  height: var(--cell-size);
  background: #1a1a24;
  border-radius: 2px;
}

.grid-shrink-clone .grid-cell.filled {
  background: linear-gradient(145deg, #3a5a3a, #2d4a2d);
  color: #b8e0b8;
}

.grid-shrink-clone .grid-cell.stored {
  background: linear-gradient(145deg, #c9a035, #8a7020);
  color: #1a1a10;
}

/* ========== WORDS MODULE (middle ~25%) ========== */
.words-module {
  flex: 2 0 0; /* 20% */
  min-height: 0;
  background: #12121a;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  border: none;
  padding: 0;
}

/* --- Уровни --- */
.levels-row {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.25rem;
  width: 100%;
}

.levels-bar {
  display: flex;
  gap: 6px;
  justify-content: center;
  flex-wrap: wrap;
}

.level-btn {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  border: 1px solid var(--key-border);
  background: var(--key-bg);
  color: var(--text-dim);
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, color 0.15s, border-color 0.15s, transform 0.1s;
  font-family: inherit;
}

.level-btn:hover {
  background: #3a3a48;
  color: var(--text);
  border-color: #5a5a6a;
}

.level-btn.active {
  background: var(--accent);
  color: #1a1a10;
  border-color: var(--accent-dim);
  box-shadow: 0 0 10px rgba(240, 192, 64, 0.35);
  transform: scale(1.08);
}

.level-hint {
  font-size: 0.8rem;
  color: var(--text-dim);
  opacity: 0.85;
}

.word-display {
  text-align: center;
}

.current-word {
  font-size: 3.2rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  font-family: 'Consolas', 'Courier New', monospace;
  min-height: 1.2em;
}

.current-word .typed {
  color: var(--correct);
}

.current-word .remaining {
  color: var(--text);
  opacity: 0.85;
}

.current-word .error {
  color: var(--error);
  background: rgba(229, 57, 53, 0.2);
  border-radius: 3px;
}

.word-hint {
  margin-top: 0.4rem;
  font-size: 0.9rem;
  color: var(--text-dim);
}

.stats-bar {
  display: flex;
  gap: 2.5rem;
  font-size: 1rem;
  color: var(--text-dim);
}

.stats-bar span {
  min-width: 80px;
  text-align: center;
}

/* ========== KEYBOARD MODULE (bottom ~45%) ========== */
.keyboard-module {
  flex: 3 0 0; /* 30% */
  min-height: 0;
  background: #12121a;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: var(--key-size) 0.5rem; /* поля = одна клавиша */
  gap: 0;
  overflow: hidden;
  border: none;
}

.layout-indicator {
  font-size: 0.95rem;
  color: var(--text-dim);
  margin-bottom: 0.3rem;
}

.layout-indicator strong {
  color: var(--accent);
}

.layout-indicator .hint {
  margin-left: 0.8rem;
  font-size: 0.8rem;
  opacity: 0.7;
}

.keyboard {
  /* --key-size и --kb-gap из :root */

  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: var(--kb-gap);
  max-width: min(980px, calc(100vw - 1.2rem));
  width: fit-content;
  margin: 0 auto;
}

.keyboard-row {
  display: flex;
  justify-content: flex-start;
  gap: var(--kb-gap);
  flex-wrap: nowrap;
}

.key {
  background: var(--key-bg);
  border: 1px solid var(--key-border);
  border-radius: max(4px, calc(var(--key-size) * 0.12));
  height: var(--key-size);
  width: var(--key-size);
  min-width: var(--key-size);
  max-width: var(--key-size);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--key-size) * 0.42);
  font-weight: 600;
  color: var(--text);
  box-shadow: 0 3px 0 #1a1a22, 0 4px 6px rgba(0,0,0,0.3);
  transition: background 0.08s, color 0.08s, transform 0.05s, box-shadow 0.08s;
  position: relative;
  flex-shrink: 0;
  box-sizing: border-box;
}

/* Ширины модификаторов (от стандартной клавиши) */
.key.w15 {
  width: calc(var(--key-size) * 1.5);
  min-width: calc(var(--key-size) * 1.5);
  max-width: calc(var(--key-size) * 1.5);
  font-size: calc(var(--key-size) * 0.22);
  font-weight: 500;
  color: var(--text-dim);
}

.key.w18 {
  width: calc(var(--key-size) * 1.8);
  min-width: calc(var(--key-size) * 1.8);
  max-width: calc(var(--key-size) * 1.8);
  font-size: calc(var(--key-size) * 0.22);
  font-weight: 500;
  color: var(--text-dim);
}

.key.w21 {
  width: calc(var(--key-size) * 2.1);
  min-width: calc(var(--key-size) * 2.1);
  max-width: calc(var(--key-size) * 2.1);
  font-size: calc(var(--key-size) * 0.22);
  font-weight: 500;
  color: var(--text-dim);
}

.key.w22 {
  width: calc(var(--key-size) * 2.2);
  min-width: calc(var(--key-size) * 2.2);
  max-width: calc(var(--key-size) * 2.2);
  font-size: calc(var(--key-size) * 0.22);
  font-weight: 500;
  color: var(--text-dim);
}

.key.backspace {
  width: calc(var(--key-size) * 2);
  min-width: calc(var(--key-size) * 2);
  max-width: calc(var(--key-size) * 2);
  font-size: calc(var(--key-size) * 0.32);
  font-weight: 500;
  color: var(--text-dim);
}

.key.fill {
  /* точная ширина выставляется в JS по первому ряду */
  min-width: 48px;
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--text-dim);
}

.key.space {
  /* ширина задаётся из JS = ширина 1-го ряда */
  min-width: 0;
  max-width: none;
  width: auto;
}

/* Неактивные клавиши (Tab, Caps, Shift, Backspace) */
.key.inactive {
  background: #1e1e28;
  border-color: #2a2a35;
  box-shadow: 0 2px 0 #15151c;
  cursor: default;
  opacity: 0.7;
}

/* Домашние позиции указательных пальцев (А/О рус, F/J англ) */
.key.home-key {
  text-decoration: underline;
  text-underline-offset: 5px;
  text-decoration-thickness: 2.5px;
  text-decoration-color: var(--accent);
}

.key.active {
  background: var(--key-active);
  color: #1a1a10;
  transform: translateY(2px);
  box-shadow: 0 1px 0 #1a1a22, 0 2px 4px rgba(0,0,0,0.3);
}

/* Правильное нажатие — зелёный (дольше) */
.key.correct-flash,
.key.correct-flash.active {
  background: var(--correct) !important;
  color: white !important;
  box-shadow: 0 0 0 2px #81c784, 0 3px 0 #1a1a22 !important;
  transition: background 0.1s, color 0.1s, box-shadow 0.1s !important;
}

/* Ошибка — яркий красный сразу */
.key.error-red,
.key.error-red.active {
  background: #e53935 !important;
  color: #ffffff !important;
  box-shadow: 0 0 0 3px #ff6b6b, 0 3px 0 #1a1a22 !important;
  transform: none !important;
  transition: background 0.12s, color 0.12s, box-shadow 0.12s !important;
}

/* Ошибка через 1с — бардовый (остаётся) */
.key.error-burgundy,
.key.error-burgundy.active {
  background: #6b1515 !important;
  color: #ffb3b3 !important;
  box-shadow: 0 0 0 2px #a03030, 0 3px 0 #1a1a22 !important;
  transform: none !important;
  transition: background 0.35s, color 0.35s, box-shadow 0.35s !important;
}

/* Подсказка правильной клавиши — белый */
.key.hint-correct,
.key.hint-correct.active {
  background: #ffffff !important;
  color: #1a1a10 !important;
  box-shadow: 0 0 14px 5px rgba(255, 255, 255, 0.75), 0 3px 0 #1a1a22 !important;
  transition: background 0.1s, color 0.1s, box-shadow 0.1s !important;
  z-index: 5;
}

/* Special keys smaller text */
.key.mod {
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--text-dim);
}

/* Responsive for smaller screens (still desktop) */
@media (max-width: 1100px) {
  .current-word {
    font-size: 2.4rem;
  }
}
}

@media (max-height: 800px) {
  .top-module {
    flex: 0 0 26%;
  }
  .words-module {
    flex: 0 0 18%;
  }
  .key {
    --key-size: 46px;
    height: var(--key-size);
  }
}

/* ========== Кнопка звука (снизу справа) ========== */
.sound-toggle {
  position: fixed;
  right: 18px;
  bottom: 18px;
  z-index: 100;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  border: 1px solid var(--key-border);
  background: var(--key-bg);
  color: var(--text-dim);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.45);
  transition: background 0.15s, transform 0.1s, border-color 0.15s, color 0.15s;
  font-family: inherit;
  padding: 0;
}

.sound-toggle svg {
  width: 24px;
  height: 24px;
  fill: currentColor;
  display: block;
}

.sound-toggle:hover {
  background: #3a3a48;
  border-color: #5a5a6a;
  color: var(--text);
  transform: scale(1.07);
}

.sound-toggle .icon-off {
  display: none;
}

.sound-toggle.muted .icon-on {
  display: none;
}

.sound-toggle.muted .icon-off {
  display: block;
}

.sound-toggle.muted {
  opacity: 0.55;
  color: var(--text-dim);
}


/* Золотой кубик: Прогресс → Мега-Склад */
.golden-cube {
  position: fixed;
  z-index: 60;
  margin-left: 0;
  margin-top: 0;
  transform: translate(-50%, -50%);
  border-radius: 6px;
  background: linear-gradient(145deg, #ffe566, #f0c040 40%, #c9a035 70%, #8a7020);
  box-shadow:
    0 0 20px rgba(240, 192, 64, 0.65),
    0 8px 24px rgba(0, 0, 0, 0.45),
    inset 0 1px 0 rgba(255, 255, 255, 0.35);
  pointer-events: none;
  will-change: transform, width, height;
}

/* Фактура: тонкие внутренние блики, оттенок задаётся inline */
.grid-cell.mega-stored {
  color: #1a1a10;
  font-size: calc(var(--cell-size) * 0.45);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.22),
    inset 0 -1px 0 rgba(0, 0, 0, 0.18),
    0 0 4px rgba(240, 192, 64, 0.3);
}

.grid-cell.filled {
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.1), inset 0 -1px 0 rgba(0, 0, 0, 0.12);
}
