/* === Базовые переменные === */
:root {
  --bg: #f5f5f5;
  --bg-card: #fff;
  --key-top: #e8e8e8;
  --key-bottom: #c0c0c0;
  --text: #1a1a1a;
  --text-muted: #666;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --success: #16a34a;
  --error: #dc2626;
  --border: #e5e7eb;
  --star: #f59e0b;
  --hand-fill: #fcd5b0;
  --hand-stroke: #d4a574;
  --finger-active: #2563eb;
  --finger-active-glow: rgba(37, 99, 235, 0.5);
}

.theme-dark {
  --bg: #1a1a2e;
  --bg-card: #16213e;
  --key-top: #3a3a4a;
  --key-bottom: #2d2d3d;
  --text: #e8e8e8;
  --text-muted: #a0a0a0;
  --accent: #3b82f6;
  --accent-hover: #60a5fa;
  --success: #22c55e;
  --error: #ef4444;
  --border: #2d3748;
  --star: #fbbf24;
  --hand-fill: #c4a77d;
  --hand-stroke: #8b7355;
  --finger-active: #60a5fa;
  --finger-active-glow: rgba(96, 165, 250, 0.5);
}

/* === Reset & Base === */
*, *::before, *::after { box-sizing: border-box; }
html { font-size: 16px; }
body {
  margin: 0;
  font-family: 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

#app {
  max-width: 900px;
  margin: 0 auto;
  padding: 1rem;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* === Screens === */
.screen {
  display: none;
  flex-direction: column;
  flex: 1;
  animation: screenIn 0.2s ease;
}
.screen.active {
  display: flex;
}
@keyframes screenIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

/* === Header === */
.header {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-bottom: 1.5rem;
}
.header h1, .header h2 {
  margin: 0;
  font-size: 1.5rem;
}
.theme-toggle {
  margin-left: auto;
  width: 44px;
  height: 44px;
  border-radius: 12px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.25rem;
  transition: background 0.2s;
}
.theme-toggle:hover { background: var(--border); }
.theme-dark .icon-sun { display: none; }
.theme-light .icon-moon { display: none; }

.btn-back {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  cursor: pointer;
  font-size: 1.25rem;
  color: var(--text);
  transition: background 0.2s;
}
.btn-back:hover { background: var(--border); }

/* === Menu === */
.menu-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2rem;
}
.menu-desc { color: var(--text-muted); }
.lang-buttons {
  display: flex;
  gap: 1rem;
}
.settings-row { margin-top: 1rem; }

/* === Buttons === */
.btn {
  padding: 0.75rem 1.5rem;
  border-radius: 12px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  border: none;
  transition: background 0.2s, transform 0.1s;
}
.btn:active { transform: scale(0.98); }
.btn-primary {
  background: var(--accent);
  color: white;
}
.btn-primary:hover { background: var(--accent-hover); }
.btn-secondary {
  background: var(--bg-card);
  color: var(--text);
  border: 1px solid var(--border);
}
.btn-secondary:hover { background: var(--border); }
.btn-outline {
  background: transparent;
  border: 1px dashed var(--border);
  color: var(--text-muted);
}
.btn-outline:hover {
  background: color-mix(in srgb, var(--error) 10%, transparent);
  border-color: var(--error);
  color: var(--error);
}
.btn-lang {
  min-width: 140px;
}

.sound-toggle {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  cursor: pointer;
}

/* === Levels List === */
.levels-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  flex: 1;
  margin-bottom: 1rem;
}
.level-item {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 1rem 1.25rem;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
}
.level-item.unlocked { cursor: pointer; }
.level-item.unlocked:hover { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 10%, transparent); }
.level-item.locked {
  opacity: 0.6;
  cursor: not-allowed;
}
.level-stars { color: var(--star); font-size: 1.25rem; }
.level-name { flex: 1; font-weight: 600; }
.level-desc { font-size: 0.875rem; color: var(--text-muted); }
.levels-actions { margin-top: auto; }
.levels-actions .btn { width: 100%; }

/* === Lesson === */
.lesson-header { flex-shrink: 0; }
.lesson-stats {
  display: flex;
  gap: 2rem;
  margin-bottom: 0.5rem;
  font-weight: 600;
}
.layout-hint {
  font-size: 0.875rem;
  color: var(--text-muted);
  margin: 0 0 1rem 0;
}
.lesson-text-area {
  flex: 1;
  min-width: 0;
  padding: 1.5rem;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow-wrap: break-word;
  overflow-x: hidden;
}
.lesson-text {
  font-size: var(--lesson-font-size, 24px);
  line-height: 1.8;
  min-height: 6em;
  letter-spacing: 0.06em;
  word-wrap: break-word;
}
.lesson-text .char { display: inline; }
.lesson-text .char.current { background: var(--accent); color: white; padding: 0 2px; border-radius: 2px; }
.lesson-text .char.correct { color: var(--success); }
.lesson-text .char.wrong { color: var(--error); background: color-mix(in srgb, var(--error) 20%, transparent); }
.lesson-text .char.pending { color: var(--text-muted); }

.free-text-area {
  flex: 1;
  min-width: 0;
  padding: 1.5rem;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
  max-height: 50vh;
  overflow-y: auto;
  overflow-wrap: break-word;
  overflow-x: hidden;
}
.free-text {
  font-size: var(--lesson-font-size, 20px);
  line-height: 1.6;
  letter-spacing: 0.06em;
  word-wrap: break-word;
}

/* === Виртуальная клавиатура + руки === */
.keyboard-hands-container {
  flex-shrink: 0;
  padding: 1rem 0;
  display: flex;
  justify-content: center;
}
.keyboard-zone {
  width: 100%;
  max-width: 680px;
}
.keyboard-hands-svg {
  width: 100%;
  height: auto;
}
.keyboard-hands-svg .key {
  transition: fill 0.2s, filter 0.2s;
}
.keyboard-hands-svg .key.active {
  fill: var(--finger-active);
  filter: drop-shadow(0 0 4px var(--finger-active-glow));
}
.keyboard-hands-svg .key.active + text { fill: white; }
.keyboard-hands-svg .hands-layer {
  pointer-events: none;
}
.keyboard-hands-svg .finger-wrap .finger {
  transition: fill 0.2s ease, opacity 0.2s ease;
}
.keyboard-hands-svg .finger-wrap .finger-label {
  pointer-events: none;
  user-select: none;
}
.keyboard-hands-svg .finger-wrap.active .finger {
  filter: drop-shadow(0 0 6px var(--finger-active-glow));
}
.finger-legend {
  font-size: 0.8rem;
  color: var(--text-muted);
  text-align: center;
  margin: 0.5rem 0 0 0;
}

/* === Result overlay === */
.lesson-result {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.lesson-result.visible {
  display: flex;
}
.result-content {
  background: var(--bg-card);
  padding: 2rem;
  border-radius: 16px;
  max-width: 400px;
  text-align: center;
}
.result-stars {
  font-size: 2rem;
  color: var(--star);
  margin: 1rem 0;
}
.result-buttons { display: flex; gap: 0.75rem; justify-content: center; margin-top: 1.5rem; flex-wrap: wrap; }
.result-buttons .btn { flex: 1; min-width: 120px; }

/* === Таблица умножения === */
.times-table-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.5rem;
}
.times-table-desc {
  color: var(--text-muted);
  margin: 0;
}
.times-table-grid {
  overflow-x: auto;
  width: 100%;
  display: flex;
  justify-content: center;
}
.times-table {
  border-collapse: collapse;
  font-size: 1rem;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
}
.times-table th,
.times-table td {
  padding: 0.5rem 0.75rem;
  text-align: center;
  border: 1px solid var(--border);
  min-width: 2.5rem;
}
.times-table thead th {
  background: var(--accent);
  color: white;
  font-weight: 600;
}
.times-table tbody th {
  background: color-mix(in srgb, var(--accent) 15%, transparent);
  font-weight: 600;
}
.times-table tbody tr:nth-child(even) td {
  background: color-mix(in srgb, var(--bg) 50%, transparent);
}

.times-table-range {
  width: 100%;
  max-width: 400px;
  padding: 1.5rem;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}
.range-row {
  display: flex;
  align-items: center;
  gap: 1rem;
}
.range-label {
  flex-shrink: 0;
  min-width: 80px;
  font-weight: 500;
}
.range-row input[type="range"] {
  flex: 1;
  min-width: 0;
  height: 8px;
  -webkit-appearance: none;
  appearance: none;
  background: var(--border);
  border-radius: 4px;
}
.range-row input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
  transition: transform 0.15s;
}
.range-row input[type="range"]::-webkit-slider-thumb:hover {
  transform: scale(1.1);
}
.range-row input[type="range"]::-moz-range-thumb {
  width: 20px;
  height: 20px;
  border: none;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
}
.range-value {
  min-width: 1.5rem;
  font-weight: 600;
  text-align: right;
}
.range-hint {
  font-size: 0.875rem;
  color: var(--text-muted);
  margin: 0;
  line-height: 1.4;
}

/* === Статистика ошибок таблицы умножения === */
.times-errors-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}
.times-errors-desc {
  color: var(--text-muted);
  margin: 0;
}
.times-errors-list {
  flex: 1;
  overflow: auto;
  min-height: 0;
}
.times-errors-empty {
  color: var(--text-muted);
  font-style: italic;
  margin: 0;
}
.times-errors-table {
  width: 100%;
  border-collapse: collapse;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
}
.times-errors-table th,
.times-errors-table td {
  padding: 0.75rem 1rem;
  text-align: left;
  border-bottom: 1px solid var(--border);
}
.times-errors-table th {
  background: color-mix(in srgb, var(--accent) 15%, transparent);
  font-weight: 600;
}
.times-errors-table td:last-child {
  text-align: right;
  font-weight: 600;
  color: var(--error);
}
.times-errors-actions {
  display: flex;
  gap: 0.75rem;
  flex-wrap: wrap;
}

/* === Тест таблицы умножения === */
.times-test-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.5rem;
  padding: 1rem 0;
}
.times-test-progress {
  font-size: 0.875rem;
  color: var(--text-muted);
}
.times-test-question {
  font-size: 2rem;
  font-weight: 600;
  margin: 0;
}
.times-test-input {
  font-size: 1.5rem;
  padding: 0.75rem 1rem;
  border: 2px solid var(--border);
  border-radius: 12px;
  width: 120px;
  text-align: center;
  background: var(--bg-card);
  color: var(--text);
}
.times-test-input:focus {
  outline: none;
  border-color: var(--accent);
}
.times-test-feedback {
  min-height: 1.5rem;
  margin: 0;
  font-weight: 600;
}
.times-test-feedback.correct { color: var(--success); }
.times-test-feedback.wrong { color: var(--error); }
.times-test-result {
  display: none;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
  text-align: center;
}
.times-test-result.visible {
  display: flex;
}
.times-test-result h3 { margin: 0; }
.times-test-result p { margin: 0; color: var(--text-muted); }
