:root {
  --bg: #f5f6fa;
  --card: #ffffff;
  --text: #2d3142;
  --muted: #8b8fa3;
  --primary: #4f6ef7;
  --primary-dark: #3b53c4;
  --green: #27ae60;
  --red: #e74c3c;
  --radius: 16px;
  --shadow: 0 8px 30px rgba(45, 49, 66, 0.12);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.app {
  max-width: 960px;
  margin: 0 auto;
  padding: 48px 16px;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
}

.screen {
  text-align: center;
  animation: fade 0.25s ease;
}

#quiz-screen,
#result-screen {
  max-width: 480px;
  margin: auto;
  width: 100%;
}

@keyframes fade {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

.hidden { display: none !important; }

h1 { margin: 0 0 8px; font-size: 32px; }

.subtitle { color: var(--muted); margin: 0 0 28px; }

.last-score {
  background: var(--card);
  border-radius: var(--radius);
  padding: 12px 16px;
  margin-bottom: 20px;
  box-shadow: var(--shadow);
  color: var(--muted);
}

.btn {
  border: none;
  border-radius: var(--radius);
  padding: 14px 24px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  color: #fff;
  transition: transform 0.08s ease, box-shadow 0.2s ease, opacity 0.2s ease;
}

.btn:active { transform: scale(0.97); }

.btn-primary { background: var(--primary); box-shadow: 0 6px 18px rgba(79, 110, 247, 0.4); }
.btn-primary:hover { background: var(--primary-dark); }

.btn-ghost {
  background: transparent;
  color: var(--primary);
  border: 2px solid var(--primary);
  margin-top: 12px;
  display: block;
  width: 100%;
}

.deck-list {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-auto-rows: 1fr;
  gap: 12px;
}

.deck {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  position: relative;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.deck-header {
  padding: 14px 16px 10px;
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  text-align: left;
  width: 100%;
}

.deck-badge {
  position: absolute;
  top: -10px;
  right: -10px;
  z-index: 2;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 14px;
  color: #fff;
  background: hsl(var(--hue, 120), 75%, 45%);
  border: 3px solid var(--bg);
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.18);
}

.deck-header > span:first-child {
  flex: 1;
  min-width: 0;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  overflow: hidden;
}

.deck-actions {
  margin: auto 16px 12px;
  display: flex;
  gap: 10px;
}

.deck-actions .btn {
  flex: 1;
  margin: 0;
  padding: 10px 12px;
  font-size: 14px;
}

.btn-start {
  background: var(--green);
  box-shadow: 0 6px 18px rgba(39, 174, 96, 0.4);
}

.btn-start:hover { background: #1e8f50; }

.deck-questions {
  border-top: 1px solid #eceef6;
  padding: 4px 20px 16px;
  text-align: left;
}

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(20, 24, 40, 0.55);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.modal {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.35);
  width: 100%;
  max-width: 640px;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.modal-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 20px;
  border-bottom: 1px solid #eceef6;
}

.modal-header h2 {
  margin: 0;
  font-size: 17px;
  text-align: left;
  line-height: 1.35;
}

.modal-close {
  background: transparent;
  border: none;
  font-size: 28px;
  line-height: 1;
  color: var(--muted);
  cursor: pointer;
  flex-shrink: 0;
  padding: 0;
}

.modal-close:hover { color: var(--text); }

.modal-body {
  overflow-y: auto;
  padding: 4px 20px 16px;
  text-align: left;
}

.q-item {
  padding: 10px 0;
  border-bottom: 1px solid #f0f2fa;
}

.q-item:last-child { border-bottom: none; }

.q-item .q { display: block; font-weight: 600; font-size: 14px; margin-bottom: 2px; }

.q-item .a { display: block; color: var(--muted); font-size: 14px; }

.progress { margin-bottom: 24px; }

.quiz-top { display: flex; justify-content: flex-start; margin-bottom: 16px; }

.btn-exit {
  background: transparent;
  color: var(--muted);
  border: none;
  padding: 8px 4px;
  font-size: 14px;
  font-weight: 600;
}

.btn-exit:hover { color: var(--text); }
.progress span { color: var(--muted); font-size: 14px; }
.progress-bar {
  height: 8px;
  background: #e2e4ee;
  border-radius: 999px;
  overflow: hidden;
  margin-top: 8px;
}
.progress-fill {
  height: 100%;
  width: 0;
  background: var(--primary);
  border-radius: 999px;
  transition: width 0.3s ease;
}

.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 32px 20px;
  margin-bottom: 24px;
  min-height: 220px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.card h2 { margin: 0 0 20px; font-size: 20px; line-height: 1.4; }

.btn-flip {
  background: transparent;
  color: var(--primary);
  border: 2px solid var(--primary);
}

.answer {
  margin: 20px 0 0;
  font-size: 18px;
  font-weight: 600;
  color: var(--green);
}

.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

.btn-know { background: var(--green); box-shadow: 0 6px 18px rgba(39, 174, 96, 0.4); }
.btn-dont { background: var(--red); box-shadow: 0 6px 18px rgba(231, 76, 60, 0.4); }

#next-btn { grid-column: 1 / -1; }

.result-text { color: var(--muted); margin: 0 0 12px; font-size: 18px; }

.result-score {
  font-size: 56px;
  font-weight: 700;
  margin: 0 0 32px;
}

.result-score.record { color: var(--green); }

#again-btn { width: 100%; }

/* ===== Tabs ===== */
.tabs {
  display: flex;
  gap: 6px;
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 6px;
  margin-bottom: 24px;
}

.tab {
  flex: 1;
  border: none;
  background: transparent;
  padding: 10px 12px;
  font-size: 15px;
  font-weight: 600;
  color: var(--muted);
  border-radius: 12px;
  cursor: pointer;
}

.tab.active {
  background: var(--primary);
  color: #fff;
}

.tab-logout {
  flex: 0 0 auto;
  font-size: 14px;
  color: var(--muted);
}

.tab-logout:hover { color: var(--red); }

.auth-form {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  max-width: 360px;
  margin: 0 auto;
  text-align: left;
}

.auth-input {
  border: 2px solid #e2e4ee;
  border-radius: 12px;
  padding: 12px 14px;
  font-size: 15px;
  width: 100%;
  background: #fff;
  color: var(--text);
}

.auth-input:focus { outline: none; border-color: var(--primary); }

.auth-error { color: var(--red); font-size: 14px; margin: 0; }

.auth-actions { display: flex; flex-direction: column; gap: 10px; }

.auth-actions .btn-ghost { margin-top: 0; }

/* ===== Статистика ===== */
.stats-summary {
  display: flex;
  gap: 12px;
  margin-bottom: 8px;
}

.stat-card {
  flex: 1;
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.stat-num { font-size: 30px; font-weight: 700; }

.stat-lbl { color: var(--muted); font-size: 13px; }

.stats-title {
  text-align: left;
  font-size: 16px;
  margin: 24px 0 10px;
}

.chart-scroll { overflow-x: auto; }

.chart {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  height: 200px;
  min-width: 100%;
}

.bar-wrap {
  flex: 1;
  min-width: 22px;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.bar-area {
  flex: 1;
  width: 100%;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  position: relative;
}

.bar {
  width: 100%;
  max-width: 44px;
  border-radius: 4px 4px 0 0;
  min-height: 2px;
  background: var(--primary);
}

.bar-value {
  position: absolute;
  transform: translateY(-100%);
  font-size: 11px;
  font-weight: 600;
  color: var(--muted);
}

.bar-label {
  margin-top: 4px;
  font-size: 10px;
  color: var(--muted);
  white-space: nowrap;
}

/* ===== Mobile ===== */
html { -webkit-text-size-adjust: 100%; }

button { -webkit-tap-highlight-color: transparent; }

@media (max-width: 600px) {
  .app {
    padding: 20px 12px 28px;
  }

  h1 { font-size: 26px; }

  .subtitle { margin-bottom: 20px; font-size: 15px; }

  .deck-header {
    padding: 12px 12px 8px;
    font-size: 14px;
    gap: 8px;
  }

  .deck-badge { width: 40px; height: 40px; font-size: 12px; top: -8px; right: -8px; }

  .deck-actions {
    margin: auto 12px 12px;
    flex-direction: column;
    gap: 8px;
  }

  .deck-actions .btn {
    width: 100%;
    padding: 12px 12px;
    font-size: 14px;
  }

  .modal { max-height: 90vh; }

  .modal-header { padding: 14px 16px; }

  .modal-header h2 { font-size: 15px; }

  .modal-body { padding: 4px 16px 14px; }

  .card {
    padding: 24px 16px;
    min-height: 200px;
  }

  .card h2 { font-size: 18px; }

  .answer { font-size: 16px; }

  .actions { gap: 10px; }

  .btn { font-size: 15px; }

  .result-score { font-size: 48px; }
}

/* Переходы колонок сетки: 3 → 2 → 1 */
@media (max-width: 900px) {
  .deck-list { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 520px) {
  .deck-list { grid-template-columns: 1fr; }
}