:root {
  --purple: #6C5CE7;
  --purple-dark: #5849c2;
  --purple-light: #EDE9FE;
  --orange: #FF9F43;
  --green: #2ECC71;
  --red: #FF6B6B;
  --bg: #F6F3FF;
  --card: #FFFFFF;
  --text: #2D2A3E;
  --text-soft: #6E6A85;
  --border: #E7E2FB;
  --radius: 20px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", "Nunito", Verdana, system-ui, -apple-system, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

#app {
  height: 100dvh;
  width: 100%;
  position: relative;
  overflow: hidden;
}

.screen {
  display: none;
  height: 100%;
  width: 100%;
}
.screen.active { display: block; }

/* ===== Onboarding shared card ===== */
.onboarding-card {
  max-width: 480px;
  margin: 0 auto;
  padding: calc(28px + var(--safe-top)) 24px 28px;
  height: 100%;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}
.mascot { font-size: 64px; text-align: center; margin-bottom: 8px; }
.mascot-small { font-size: 36px; text-align: center; margin-bottom: 6px; }
.onboarding-card h1 {
  font-size: 24px;
  text-align: center;
  margin: 0 0 6px;
  color: var(--purple-dark);
}
.onboarding-card .sub {
  text-align: center;
  color: var(--text-soft);
  margin: 0 0 24px;
  font-size: 15px;
  line-height: 1.4;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 18px;
}
.field span {
  font-weight: 600;
  font-size: 14px;
  color: var(--text-soft);
}
.field input, .field select, .field textarea {
  border: 2px solid var(--border);
  border-radius: 14px;
  padding: 14px 16px;
  font-size: 16px;
  background: var(--card);
  color: var(--text);
  font-family: inherit;
  outline: none;
  transition: border-color .15s;
}
.field input:focus, .field select:focus, .field textarea:focus {
  border-color: var(--purple);
}

.avatar-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 8px;
}
.avatar-opt {
  font-size: 26px;
  border: 2px solid var(--border);
  border-radius: 14px;
  background: var(--card);
  aspect-ratio: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
.avatar-opt.selected {
  border-color: var(--purple);
  background: var(--purple-light);
}

.btn-primary {
  background: var(--purple);
  color: white;
  border: none;
  border-radius: 16px;
  padding: 16px;
  font-size: 17px;
  font-weight: 700;
  cursor: pointer;
  margin-top: 8px;
}
.btn-primary:active { background: var(--purple-dark); }
.btn-primary:disabled { opacity: .5; }

.btn-link {
  background: none;
  border: none;
  color: var(--purple);
  font-size: 15px;
  font-weight: 600;
  padding: 14px;
  cursor: pointer;
}

.btn-danger {
  background: #FFF0F0;
  color: var(--red);
  border: 2px solid #FFD6D6;
  border-radius: 14px;
  padding: 12px 16px;
  font-weight: 700;
  cursor: pointer;
}

.back-link {
  background: none;
  border: none;
  color: var(--purple);
  font-size: 15px;
  font-weight: 600;
  padding: 0;
  margin-bottom: 12px;
  cursor: pointer;
  text-align: left;
}

.howto-list { padding-left: 20px; line-height: 1.6; color: var(--text); }
.howto-list li { margin-bottom: 10px; }

.hint { font-size: 13px; color: var(--text-soft); line-height: 1.4; }
code {
  background: var(--purple-light);
  padding: 2px 6px;
  border-radius: 6px;
  font-size: 13px;
}

/* ===== Main shell ===== */
.main-shell {
  display: none;
  flex-direction: column;
  height: 100%;
}
.main-shell.active { display: flex; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(14px + var(--safe-top)) 18px 14px;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.topbar-left { display: flex; align-items: center; gap: 12px; }
.topbar-avatar {
  font-size: 28px;
  width: 44px; height: 44px;
  background: var(--purple-light);
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
}
.topbar-text { display: flex; flex-direction: column; line-height: 1.25; }
.topbar-text strong { font-size: 16px; }
.topbar-text span { font-size: 13px; color: var(--text-soft); }
.topbar-right { display: flex; gap: 6px; }
.icon-btn {
  background: var(--purple-light);
  border: none;
  border-radius: 12px;
  width: 40px; height: 40px;
  font-size: 18px;
  cursor: pointer;
}

.tab-content {
  flex: 1;
  overflow: hidden;
  display: none;
  flex-direction: column;
  min-height: 0;
}
.tab-content.active { display: flex; }

.chips-row {
  display: flex;
  gap: 8px;
  padding: 12px 16px 4px;
  overflow-x: auto;
  flex-shrink: 0;
}
.chip {
  flex-shrink: 0;
  background: var(--card);
  border: 2px solid var(--border);
  border-radius: 100px;
  padding: 8px 16px;
  font-size: 14px;
  font-weight: 600;
  color: var(--text-soft);
  cursor: pointer;
  white-space: nowrap;
}
.chip.active {
  border-color: var(--purple);
  color: var(--purple-dark);
  background: var(--purple-light);
}

/* ===== Chat ===== */
.chat-log {
  flex: 1;
  overflow-y: auto;
  padding: 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.chat-empty {
  margin: auto;
  text-align: center;
  color: var(--text-soft);
  max-width: 320px;
  font-size: 15px;
  line-height: 1.5;
}
.msg {
  max-width: 82%;
  padding: 12px 16px;
  border-radius: 18px;
  font-size: 15.5px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-wrap: break-word;
}
.msg.user {
  align-self: flex-end;
  background: var(--purple);
  color: white;
  border-bottom-right-radius: 4px;
}
.msg.assistant {
  align-self: flex-start;
  background: var(--card);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}
.msg.assistant b, .msg.assistant strong { color: var(--purple-dark); }
.msg.error {
  align-self: center;
  background: #FFF0F0;
  color: #C0392B;
  border: 1px solid #FFD6D6;
  font-size: 14px;
}
.msg.typing {
  align-self: flex-start;
  background: var(--card);
  border: 1px solid var(--border);
  display: flex;
  gap: 4px;
  padding: 16px 18px;
}
.dot {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--purple);
  animation: bounce 1.2s infinite;
}
.dot:nth-child(2) { animation-delay: .15s; }
.dot:nth-child(3) { animation-delay: .3s; }
@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: .5; }
  30% { transform: translateY(-5px); opacity: 1; }
}

.chat-input-bar {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 14px calc(12px + var(--safe-bottom));
  background: var(--card);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}
.chat-input-bar textarea {
  flex: 1;
  resize: none;
  border: 2px solid var(--border);
  border-radius: 20px;
  padding: 12px 16px;
  font-size: 16px;
  font-family: inherit;
  max-height: 120px;
  outline: none;
}
.chat-input-bar textarea:focus { border-color: var(--purple); }
.mic-btn, .send-btn {
  flex-shrink: 0;
  width: 46px; height: 46px;
  border-radius: 50%;
  border: none;
  font-size: 19px;
  cursor: pointer;
}
.mic-btn {
  background: var(--purple-light);
  color: var(--purple-dark);
}
.mic-btn.recording {
  background: var(--red);
  color: white;
  animation: pulse 1s infinite;
}
@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(255,107,107,.5); }
  50% { box-shadow: 0 0 0 8px rgba(255,107,107,0); }
}
.send-btn {
  background: var(--purple);
  color: white;
}
.send-btn:disabled { opacity: .4; }

/* ===== Tests tab ===== */
#tab-tests { overflow-y: auto; padding: 4px 16px 24px; }
.tests-setup h2, #tab-profile h2 {
  font-size: 19px;
  margin: 14px 0 4px;
  color: var(--purple-dark);
}
.tests-setup .sub { color: var(--text-soft); font-size: 14px; margin: 0 0 14px; }

.segmented {
  display: flex;
  background: var(--purple-light);
  border-radius: 14px;
  padding: 4px;
  gap: 4px;
}
.seg-btn {
  flex: 1;
  border: none;
  background: transparent;
  padding: 10px;
  border-radius: 10px;
  font-weight: 700;
  font-size: 14px;
  color: var(--text-soft);
  cursor: pointer;
}
.seg-btn.active {
  background: var(--card);
  color: var(--purple-dark);
}

.tests-quiz { padding-top: 8px; }
.quiz-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 16px;
  margin-bottom: 14px;
}
.quiz-q {
  font-weight: 700;
  margin-bottom: 10px;
  font-size: 15.5px;
}
.quiz-opt {
  display: block;
  width: 100%;
  text-align: left;
  border: 2px solid var(--border);
  background: var(--card);
  border-radius: 12px;
  padding: 11px 14px;
  margin-bottom: 8px;
  font-size: 15px;
  cursor: pointer;
  font-family: inherit;
  color: var(--text);
}
.quiz-opt.selected { border-color: var(--purple); background: var(--purple-light); }
.quiz-opt.correct { border-color: var(--green); background: #E9FBF0; }
.quiz-opt.incorrect { border-color: var(--red); background: #FFF0F0; }
.quiz-explain {
  margin-top: 10px;
  font-size: 14px;
  color: var(--text-soft);
  background: var(--bg);
  border-radius: 10px;
  padding: 10px 12px;
  display: none;
}
.quiz-explain.shown { display: block; }
.quiz-short-input {
  width: 100%;
  border: 2px solid var(--border);
  border-radius: 12px;
  padding: 11px 14px;
  font-size: 15px;
  font-family: inherit;
}

.card-flip {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 20px;
  margin-bottom: 14px;
  text-align: center;
  cursor: pointer;
}
.card-flip .card-label {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--text-soft);
  margin-bottom: 8px;
  font-weight: 700;
}
.card-flip .card-text { font-size: 16px; font-weight: 600; }
.card-flip .card-back { display: none; color: var(--purple-dark); }
.card-flip.flipped .card-front { display: none; }
.card-flip.flipped .card-back { display: block; }

.quiz-actions {
  display: flex;
  gap: 10px;
  margin-top: 4px;
}
.quiz-actions .btn-primary { flex: 1; margin-top: 0; }
.btn-secondary {
  flex: 1;
  background: var(--purple-light);
  color: var(--purple-dark);
  border: none;
  border-radius: 16px;
  padding: 16px;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
}
.score-banner {
  text-align: center;
  font-weight: 700;
  font-size: 17px;
  color: var(--purple-dark);
  background: var(--purple-light);
  border-radius: 14px;
  padding: 14px;
  margin-bottom: 14px;
}

/* ===== Profile tab ===== */
#tab-profile { overflow-y: auto; padding: 4px 16px 24px; }
.profile-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 16px;
  margin-bottom: 16px;
}
.danger-zone h3 { margin: 0 0 6px; font-size: 15px; color: var(--red); }
.danger-zone .hint { margin-bottom: 12px; }

/* ===== Bottom nav ===== */
.bottom-nav {
  display: flex;
  background: var(--card);
  border-top: 1px solid var(--border);
  padding: 6px 8px calc(6px + var(--safe-bottom));
  flex-shrink: 0;
}
.nav-btn {
  flex: 1;
  background: none;
  border: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 6px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-soft);
  cursor: pointer;
  border-radius: 12px;
}
.nav-btn .nav-icon { font-size: 21px; }
.nav-btn.active { color: var(--purple-dark); background: var(--purple-light); }

@media (min-width: 700px) {
  .chat-log, .chat-input-bar { padding-left: 8%; padding-right: 8%; }
}
