:root {
  --primary:       #1e3a5f;
  --primary-hover: #2d5a8c;
  --accent:        #c8a845;
  --bg:            #f0f2f7;
  --card:          #ffffff;
  --text:          #1a1f2e;
  --muted:         #6b7280;
  --border:        #e2e6ef;
  --success:       #16a34a;
  --error:         #dc2626;
  --shadow:        0 4px 20px rgba(30,58,95,0.12);
  --radius:        14px;
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  min-height: 100dvh;
  /* Предотвращаем случайный зум при двойном тапе на iOS */
  touch-action: manipulation;
}

/* ===== SCREENS ===== */
.screen { display: none; min-height: 100vh; min-height: 100dvh; }
.screen.active { display: flex; flex-direction: column; align-items: center; justify-content: center; }

/* ===== BUTTONS ===== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .5rem;
  padding: .75rem 1.5rem;
  border: none;
  border-radius: 10px;
  font-size: .95rem;
  font-weight: 600;
  cursor: pointer;
  transition: background .15s, transform .1s, box-shadow .15s;
  text-decoration: none;
}
.btn:active { transform: scale(.97); }

.btn-primary {
  background: var(--primary);
  color: #fff;
}
.btn-primary:hover { background: var(--primary-hover); box-shadow: 0 4px 12px rgba(30,58,95,.3); }

.btn-secondary {
  background: transparent;
  color: var(--primary);
  border: 2px solid var(--primary);
}
.btn-secondary:hover { background: rgba(30,58,95,.06); }

.btn-full { width: 100%; }
.btn-link { background: none; color: var(--muted); font-weight: 400; font-size: .875rem; }
.btn-link:hover { color: var(--text); }

.btn-back {
  background: none;
  color: var(--primary);
  font-size: .875rem;
  padding: .4rem .75rem;
}

/* ===== LOGIN ===== */
.login-card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 2.5rem 2rem;
  width: 100%;
  max-width: 380px;
  margin: 1rem;
}

.login-logo {
  text-align: center;
  margin-bottom: 2rem;
}
.login-icon { font-size: 3rem; margin-bottom: .5rem; }
.login-logo h1 { font-size: 1.6rem; color: var(--primary); font-weight: 800; letter-spacing: -.02em; }
.login-subtitle { color: var(--muted); font-size: .875rem; margin-top: .25rem; }

.field { margin-bottom: 1rem; }
.field input {
  width: 100%;
  padding: .75rem 1rem;
  border: 2px solid var(--border);
  border-radius: 10px;
  font-size: 16px; /* iOS не зумит на inputs >= 16px */
  color: var(--text);
  transition: border-color .15s;
  background: #fafbfd;
}
.field input:focus { outline: none; border-color: var(--primary); background: #fff; }

.error-msg { color: var(--error); font-size: .875rem; min-height: 1.25rem; margin-bottom: .75rem; text-align: center; }

/* ===== MENU ===== */
.menu-wrap {
  width: 100%;
  max-width: 420px;
  margin: 1rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
}

.menu-header { text-align: center; }
.menu-logo { font-size: 2.5rem; }
.menu-welcome { color: var(--muted); font-size: .875rem; margin-top: .5rem; }
.menu-header h2 { font-size: 1.4rem; color: var(--primary); font-weight: 700; }

.menu-progress-bar {
  width: 100%;
  height: 6px;
  background: var(--border);
  border-radius: 3px;
  overflow: hidden;
}
.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--primary), var(--accent));
  border-radius: 3px;
  transition: width .4s ease;
  width: 0%;
}
.menu-progress-text { color: var(--muted); font-size: .825rem; }

.menu-buttons { width: 100%; display: flex; flex-direction: column; gap: .75rem; }

.btn-menu {
  width: 100%;
  padding: 1.1rem 1.25rem;
  border-radius: var(--radius);
  justify-content: flex-start;
  gap: 1rem;
  color: #fff;
  text-align: left;
}
.btn-menu .btn-icon { font-size: 1.6rem; flex-shrink: 0; }
.btn-menu .btn-text { display: flex; flex-direction: column; gap: .1rem; }
.btn-menu .btn-text strong { font-size: 1rem; }
.btn-menu .btn-text small { font-weight: 400; font-size: .8rem; opacity: .85; }

.menu-footer { display: flex; gap: 1rem; justify-content: center; }

.btn-training   { background: linear-gradient(135deg, #1e3a5f, #2d6a9f); }
.btn-training:hover { background: linear-gradient(135deg, #2d5a8c, #3a7abf); box-shadow: 0 4px 16px rgba(30,58,95,.3); }
.btn-race       { background: linear-gradient(135deg, #7b3a10, #c0622a); }
.btn-race:hover { background: linear-gradient(135deg, #9b4a20, #d0723a); box-shadow: 0 4px 16px rgba(123,58,16,.3); }
.btn-leaderboard { background: linear-gradient(135deg, #1a5c3a, #2d8c5a); }
.btn-leaderboard:hover { background: linear-gradient(135deg, #2a6c4a, #3d9c6a); box-shadow: 0 4px 16px rgba(26,92,58,.3); }
.btn-directory  { background: linear-gradient(135deg, #5c3a1a, #8c6a2d); }
.btn-directory:hover { background: linear-gradient(135deg, #6c4a2a, #9c7a3d); box-shadow: 0 4px 16px rgba(92,58,26,.3); }

/* ===== СПРАВОЧНИК ===== */
#screen-directory {
  justify-content: flex-start;
  align-items: stretch;
  padding: 0;
}
.dir-wrap {
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 800px;
  margin: 0 auto;
  height: 100dvh;
  overflow-y: auto;
}
.dir-header {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  padding: .75rem 1rem;
  display: flex;
  flex-direction: column;
  gap: .6rem;
}
.dir-header h2 {
  font-size: 1.1rem;
  color: var(--primary);
  margin: 0;
}
.dir-header-top {
  display: flex;
  align-items: center;
  gap: .75rem;
}
#dir-search {
  width: 100%;
  padding: .55rem .9rem;
  border: 2px solid var(--border);
  border-radius: 10px;
  font-size: 16px;
  background: #fafbfd;
  color: var(--text);
}
#dir-search:focus { outline: none; border-color: var(--primary); }

.dir-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: .75rem;
  padding: 1rem;
}
@media (min-width: 480px) {
  .dir-grid { grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); }
}

.dir-card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: 0 2px 8px rgba(0,0,0,.08);
  overflow: hidden;
  text-align: center;
}
.dir-card img {
  width: 100%;
  aspect-ratio: 1;
  object-fit: cover;
  display: block;
}
.dir-card .dir-name {
  padding: .5rem .5rem .6rem;
  font-size: .8rem;
  line-height: 1.35;
  color: var(--text);
}
.dir-card .dir-name strong {
  display: block;
  color: var(--primary);
  font-size: .85rem;
}
.dir-card.hidden { display: none; }

/* ===== GAME ===== */
#screen-game {
  justify-content: flex-start;
  align-items: stretch;
  padding: 0;
}
.game-wrap {
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 720px;
  margin: 0 auto;
  min-height: 100vh;
  min-height: 100dvh;
}

/* Хедер: мобильный — 2 строки, десктоп — 1 строка */
.game-header {
  display: grid;
  grid-template-columns: auto 1fr auto;
  grid-template-rows: auto;
  align-items: center;
  gap: .5rem .75rem;
  padding: .6rem 1rem;
  background: var(--card);
  border-bottom: 1px solid var(--border);
}
.btn-back       { grid-column: 1; grid-row: 1; }
.mode-badge     { grid-column: 2; grid-row: 1; justify-self: start; }
.progress-text  { grid-column: 3; grid-row: 1; white-space: nowrap; }
.timer-box      { grid-column: 1 / -1; grid-row: 2; justify-self: stretch; }

@media (min-width: 520px) {
  .game-header { grid-template-columns: auto auto 1fr auto; }
  .mode-badge  { grid-column: 2; }
  .progress-text { grid-column: 4; }
  .timer-box   { grid-column: 3; grid-row: 1; justify-self: center; }
}

.mode-badge {
  font-size: .75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .08em;
  background: var(--primary);
  color: #fff;
  padding: .25rem .6rem;
  border-radius: 6px;
  white-space: nowrap;
}
.progress-text { color: var(--muted); font-size: .875rem; text-align: right; }

.timer-box {
  display: none;
  align-items: center;
  justify-content: center;
  gap: .4rem;
  background: #fff3cd;
  border: 1px solid #ffc107;
  border-radius: 8px;
  padding: .4rem .75rem;
  font-weight: 700;
  font-size: 1.05rem;
  color: #856404;
}
.penalty-display { font-size: .8rem; color: var(--error); font-weight: 700; }

.card-body {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
  padding: 1rem .875rem 2rem;
  flex: 1;
}

@media (min-width: 580px) {
  .card-body {
    flex-direction: row;
    align-items: flex-start;
    padding: 1.5rem;
    gap: 1.5rem;
  }
}

.photo-wrap { flex-shrink: 0; }

/* Мобильный: фото компактнее, чтобы влезли кнопки */
.card-photo {
  width: 140px;
  height: 140px;
  object-fit: cover;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  display: block;
}
@media (min-height: 700px) {
  .card-photo { width: 170px; height: 170px; }
}
@media (min-width: 580px) {
  .card-photo { width: 210px; height: 250px; }
}

.options-wrap {
  flex: 1;
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: .75rem;
}

.option-label {
  font-size: .7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--muted);
  margin-bottom: .4rem;
}
.option-buttons {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: .4rem;
}

.option-btn {
  padding: .55rem .5rem;
  min-height: 44px; /* минимальная тач-зона */
  border: 2px solid var(--border);
  border-radius: 10px;
  background: var(--card);
  color: var(--text);
  font-size: .85rem;
  font-weight: 500;
  cursor: pointer;
  transition: border-color .12s, background .12s, transform .1s;
  text-align: center;
  line-height: 1.25;
  word-break: break-word;
}
.option-btn:hover:not(:disabled) {
  border-color: var(--primary);
  background: #f0f4ff;
}
/* На тач-устройствах убираем hover-эффект (не залипает) */
@media (hover: none) {
  .option-btn:hover:not(:disabled) {
    border-color: var(--border);
    background: var(--card);
  }
}
.option-btn:active:not(:disabled) { transform: scale(.97); }

.option-btn.selected,
.option-btn.selected:hover:not(:disabled) {
  border-color: #e0b800;
  background: #ffe033;
  color: var(--text);
  font-weight: 700;
}
.option-btn.correct {
  border-color: var(--success);
  background: #dcfce7;
  color: var(--success);
  font-weight: 700;
}
.option-btn.wrong {
  border-color: var(--error);
  background: #fee2e2;
  color: var(--error);
  font-weight: 700;
}
.option-btn:disabled { cursor: default; }

/* ===== RESULT / LEADERBOARD ===== */
#screen-training-complete,
#screen-result,
#screen-leaderboard {
  padding: 1rem;
  align-items: stretch; /* на мобиле карточка на всю ширину */
}
@media (min-width: 520px) {
  #screen-training-complete,
  #screen-result,
  #screen-leaderboard { align-items: center; }
}
.result-wrap {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.5rem 1.25rem;
  width: 100%;
  max-width: 480px;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
}
.result-icon { font-size: 3rem; }
.result-wrap h2 { font-size: 1.5rem; color: var(--primary); font-weight: 800; }
.result-time-box {
  background: var(--bg);
  border-radius: 12px;
  padding: 1rem 2rem;
  width: 100%;
}
.result-time { font-size: 2.5rem; font-weight: 800; color: var(--primary); }
.result-penalties { color: var(--muted); font-size: .875rem; margin-top: .25rem; }

.result-actions { display: flex; gap: .75rem; flex-wrap: wrap; justify-content: center; }

.leaderboard-wrap { width: 100%; }
.leaderboard {
  width: 100%;
  border-collapse: collapse;
  font-size: .9rem;
  text-align: left;
}
.leaderboard th {
  padding: .6rem .75rem;
  background: var(--bg);
  color: var(--muted);
  font-weight: 700;
  font-size: .75rem;
  text-transform: uppercase;
  letter-spacing: .06em;
  border-bottom: 2px solid var(--border);
}
.leaderboard td {
  padding: .65rem .75rem;
  border-bottom: 1px solid var(--border);
}
.leaderboard tr.me td { background: #fffbea; font-weight: 700; color: var(--primary); }
.leaderboard tr:last-child td { border-bottom: none; }
.leaderboard td:first-child { color: var(--muted); font-size: .8rem; width: 2rem; }
.no-data { color: var(--muted); font-size: .9rem; padding: 1rem 0; }

/* ===== ПРАВИЛА (МОДАЛКА) ===== */
.rules-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.55);
  z-index: 200;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}
.rules-overlay.open { display: flex; }

.rules-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 1.75rem 1.5rem;
  width: 100%;
  max-width: 440px;
  max-height: 90vh;
  overflow-y: auto;
  position: relative;
  box-shadow: 0 8px 40px rgba(0,0,0,.25);
}
.rules-card h2 {
  color: var(--primary);
  font-size: 1.25rem;
  margin-bottom: 1.25rem;
}
.rules-close {
  position: absolute;
  top: 1rem; right: 1rem;
  background: var(--bg);
  border: none;
  border-radius: 50%;
  width: 32px; height: 32px;
  font-size: 1rem;
  cursor: pointer;
  color: var(--muted);
  display: flex; align-items: center; justify-content: center;
}
.rules-close:hover { background: var(--border); }

.rules-section {
  display: flex;
  gap: 1rem;
  align-items: flex-start;
  padding: .875rem 0;
  border-bottom: 1px solid var(--border);
}
.rules-section:last-of-type { border-bottom: none; }
.rules-icon { font-size: 1.5rem; flex-shrink: 0; margin-top: .1rem; }
.rules-section strong { display: block; color: var(--primary); margin-bottom: .3rem; }
.rules-section p { font-size: .9rem; color: var(--muted); line-height: 1.5; }
.rules-section em { font-style: normal; color: var(--text); font-weight: 600; }

/* ===== ANIMATIONS ===== */
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(12px); }
  to   { opacity: 1; transform: translateY(0); }
}
.screen.active > * { animation: fadeIn .25s ease; }
