*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #f0f4f2;
  --surface: #ffffff;
  --primary: #1a5f4a;
  --primary-light: #2d8a6e;
  --primary-dark: #134a3a;
  --accent: #e8a838;
  --text: #1a2e28;
  --text-muted: #5a7268;
  --border: #d4e4dc;
  --correct: #22a06b;
  --correct-bg: #e3f5ec;
  --wrong: #de350b;
  --wrong-bg: #ffebe6;
  --shadow: 0 2px 12px rgba(26, 95, 74, 0.08);
  --radius: 16px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  line-height: 1.5;
}

#app {
  max-width: 520px;
  margin: 0 auto;
  min-height: 100dvh;
}

.screen {
  display: none;
  flex-direction: column;
  min-height: 100dvh;
  padding-bottom: var(--safe-bottom);
}

.screen.active {
  display: flex;
}

/* Header */
.app-header {
  background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
  color: #fff;
  padding: 2rem 1.25rem 1.75rem;
  text-align: center;
}

.app-header h1 {
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.subtitle {
  margin-top: 0.35rem;
  font-size: 0.875rem;
  opacity: 0.85;
}

.home-body {
  flex: 1;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.section-title {
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-top: 0.25rem;
}

.btn-all-random {
  width: 100%;
  background: linear-gradient(135deg, var(--accent) 0%, #d4922a 100%);
  color: #1a2e28;
  border: none;
  border-radius: var(--radius);
  padding: 1rem 1.15rem;
  display: flex;
  align-items: center;
  gap: 0.85rem;
  cursor: pointer;
  text-align: left;
  box-shadow: var(--shadow);
  -webkit-tap-highlight-color: transparent;
}

.btn-all-random:active {
  transform: scale(0.98);
}

.btn-all-random-icon {
  font-size: 1.5rem;
}

.btn-all-random strong {
  display: block;
  font-size: 0.95rem;
}

.btn-all-random small {
  display: block;
  font-size: 0.78rem;
  opacity: 0.8;
  margin-top: 0.1rem;
}

.btn-all-wrongs-random {
  width: 100%;
  background: var(--surface);
  color: var(--wrong);
  border: 2px dashed var(--wrong);
  border-radius: var(--radius);
  padding: 0.85rem 1.15rem;
  display: flex;
  align-items: center;
  gap: 0.85rem;
  cursor: pointer;
  text-align: left;
  box-shadow: var(--shadow);
  -webkit-tap-highlight-color: transparent;
}

.btn-all-wrongs-random:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  border-color: var(--border);
  color: var(--text-muted);
}

.btn-all-wrongs-random:not(:disabled):active {
  transform: scale(0.98);
  background: var(--wrong-bg);
}

.btn-all-wrongs-random-icon {
  font-size: 1.25rem;
}

.btn-all-wrongs-random strong {
  display: block;
  font-size: 0.9rem;
}

.btn-all-wrongs-random small {
  display: block;
  font-size: 0.75rem;
  opacity: 0.85;
  margin-top: 0.1rem;
}

.wrongs-section {
  background: var(--surface);
  border: 1px solid var(--wrong-bg);
  border-left: 4px solid var(--wrong);
  border-radius: var(--radius);
  padding: 1rem 1.15rem;
  box-shadow: var(--shadow);
}

.wrongs-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.wrongs-header h2 {
  font-size: 1rem;
  color: var(--wrong);
}

.wrongs-badge {
  font-size: 0.75rem;
  font-weight: 700;
  background: var(--wrong-bg);
  color: var(--wrong);
  padding: 0.2rem 0.55rem;
  border-radius: 20px;
}

.wrongs-hint {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin: 0.5rem 0 0.85rem;
  line-height: 1.45;
}

.btn-wrongs {
  width: 100%;
  background: var(--wrong);
  color: #fff;
  border: none;
  border-radius: 12px;
  padding: 0.85rem;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.btn-wrongs:disabled {
  background: var(--border);
  color: var(--text-muted);
  cursor: not-allowed;
}

.btn-wrongs:not(:disabled):active {
  opacity: 0.9;
}

/* Unit list */
.unit-list {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.unit-group {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.btn-random-unit {
  width: 100%;
  background: #e8f5f0;
  color: var(--primary);
  border: 1px dashed var(--primary-light);
  border-radius: 12px;
  padding: 0.65rem 1rem;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  text-align: center;
  -webkit-tap-highlight-color: transparent;
}

.btn-random-unit span {
  font-weight: 400;
  opacity: 0.75;
}

.btn-random-unit:active {
  background: #d4ebe3;
}

.btn-random-wrongs-unit {
  width: 100%;
  background: var(--wrong-bg);
  color: var(--wrong);
  border: 1px dashed var(--wrong);
  border-radius: 12px;
  padding: 0.6rem 1rem;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  text-align: center;
  -webkit-tap-highlight-color: transparent;
}

.btn-random-wrongs-unit:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  border-color: var(--border);
  color: var(--text-muted);
  background: var(--bg);
}

.btn-random-wrongs-unit:not(:disabled):active {
  background: #ffd6cc;
}

.unit-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.15rem;
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s;
  box-shadow: var(--shadow);
  display: flex;
  align-items: center;
  gap: 0.85rem;
  -webkit-tap-highlight-color: transparent;
}

.unit-card:active {
  transform: scale(0.98);
}

.unit-card:hover {
  box-shadow: 0 4px 20px rgba(26, 95, 74, 0.12);
}

.unit-number {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  background: var(--primary);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 1rem;
  flex-shrink: 0;
}

.unit-info {
  flex: 1;
  min-width: 0;
}

.unit-info h3 {
  font-size: 0.95rem;
  font-weight: 600;
  line-height: 1.3;
}

.unit-info p {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-top: 0.15rem;
}

.unit-progress {
  font-size: 0.75rem;
  color: var(--primary);
  font-weight: 600;
  white-space: nowrap;
}

.unit-card.done .unit-number {
  background: var(--correct);
}

.app-footer {
  padding: 1rem;
  text-align: center;
  font-size: 0.75rem;
  color: var(--text-muted);
}

/* Quiz header */
.quiz-header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0.75rem 1rem 0;
  position: sticky;
  top: 0;
  z-index: 10;
}

.quiz-top-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.5rem;
}

.btn-back-text {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--primary);
  cursor: pointer;
  padding: 0.5rem 0.85rem;
  border-radius: 10px;
  font-size: 0.9rem;
  font-weight: 600;
  -webkit-tap-highlight-color: transparent;
}

.btn-back-text:active {
  background: var(--border);
}

.quiz-meta {
  display: flex;
  align-items: center;
  margin-bottom: 0.75rem;
}

.unit-badge {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--primary);
  background: #e8f5f0;
  padding: 0.2rem 0.6rem;
  border-radius: 20px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 70%;
}

.counter {
  font-size: 0.8rem;
  color: var(--text-muted);
  font-weight: 500;
  flex-shrink: 0;
}

.progress-wrap {
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  overflow: hidden;
  margin-bottom: 0;
}

.progress-bar {
  height: 100%;
  background: var(--primary);
  border-radius: 2px;
  transition: width 0.3s ease;
  width: 0%;
}

/* Quiz body */
.quiz-body {
  flex: 1;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.question-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 1.25rem;
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
}

.question-text {
  font-size: 1rem;
  font-weight: 500;
  line-height: 1.6;
  white-space: pre-line;
}

.streak-info {
  margin-top: 0.75rem;
  font-size: 0.8rem;
  color: var(--accent);
  background: #fef6e8;
  padding: 0.5rem 0.65rem;
  border-radius: 8px;
  font-weight: 500;
}

.options-list {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.option-btn {
  width: 100%;
  text-align: left;
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: 12px;
  padding: 0.9rem 1rem;
  font-size: 0.95rem;
  color: var(--text);
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
  display: flex;
  align-items: flex-start;
  gap: 0.65rem;
  line-height: 1.4;
  -webkit-tap-highlight-color: transparent;
}

.option-btn:active:not(:disabled) {
  transform: scale(0.99);
}

.option-btn:disabled {
  cursor: default;
}

.option-letter {
  width: 28px;
  height: 28px;
  border-radius: 8px;
  background: var(--bg);
  color: var(--primary);
  font-weight: 700;
  font-size: 0.85rem;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.option-btn.correct {
  border-color: var(--correct);
  background: var(--correct-bg);
}

.option-btn.correct .option-letter {
  background: var(--correct);
  color: #fff;
}

.option-btn.wrong {
  border-color: var(--wrong);
  background: var(--wrong-bg);
}

.option-btn.wrong .option-letter {
  background: var(--wrong);
  color: #fff;
}

.option-btn.dimmed {
  opacity: 0.45;
}

/* Feedback */
.feedback {
  border-radius: 12px;
  padding: 0.85rem 1rem;
  font-size: 0.9rem;
  font-weight: 500;
  text-align: center;
}

.feedback.correct-fb {
  background: var(--correct-bg);
  color: var(--correct);
}

.feedback.wrong-fb {
  background: var(--wrong-bg);
  color: var(--wrong);
}

.feedback.hidden {
  display: none;
}

/* Footer buttons */
.quiz-footer {
  padding: 0.75rem 1rem calc(0.75rem + var(--safe-bottom));
}

.quiz-footer-actions {
  display: flex;
  gap: 0.6rem;
}

.quiz-footer-actions .btn-primary {
  flex: 1;
}

.btn-outline {
  flex: 0 0 auto;
  background: var(--surface);
  color: var(--text-muted);
  border: 2px solid var(--border);
  border-radius: 12px;
  padding: 0.95rem 1.1rem;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.btn-outline:active {
  background: var(--bg);
}

.btn-outline.hidden {
  display: none;
}

.btn-primary {
  width: 100%;
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: 12px;
  padding: 0.95rem;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
  -webkit-tap-highlight-color: transparent;
}

.btn-primary:active {
  background: var(--primary-dark);
}

.btn-primary.hidden {
  display: none;
}

.btn-secondary {
  width: 100%;
  background: var(--surface);
  color: var(--primary);
  border: 2px solid var(--primary);
  border-radius: 12px;
  padding: 0.9rem;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  margin-top: 0.6rem;
}

/* Result screen */
#result-screen {
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
}

.result-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 2rem 1.5rem;
  text-align: center;
  box-shadow: var(--shadow);
  width: 100%;
  border: 1px solid var(--border);
}

.result-icon {
  font-size: 3rem;
  margin-bottom: 0.5rem;
}

.result-card h2 {
  font-size: 1.35rem;
  color: var(--primary);
}

.result-unit {
  color: var(--text-muted);
  font-size: 0.9rem;
  margin: 0.35rem 0 1.5rem;
}

.result-stats {
  display: flex;
  justify-content: center;
  gap: 2rem;
  margin-bottom: 1.75rem;
}

.stat {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.stat-value {
  font-size: 1.75rem;
  font-weight: 700;
  color: var(--primary);
}

.stat-label {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 0.15rem;
}

@media (min-width: 400px) {
  .question-text {
    font-size: 1.05rem;
  }
}
