:root {
  --bg: #f6f5ff;
  --surface: #ffffff;
  --surface-2: #efedff;
  --text: #1c1838;
  --muted: #6b678a;
  --primary: #5b4bff;
  --primary-ink: #ffffff;
  --accent: #ff7a59;
  --ok: #17a673;
  --warn-bg: #fff4e5;
  --warn-ink: #8a4b00;
  --danger: #d6334a;
  --border: #e3e0f5;
  --bubble-me: #5b4bff;
  --bubble-me-ink: #ffffff;
  --bubble-mia: #ffffff;
  --shadow: 0 2px 12px rgba(40, 30, 120, 0.08);
  --radius: 18px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12101f;
    --surface: #1d1a30;
    --surface-2: #26223f;
    --text: #f0eeff;
    --muted: #a19dc0;
    --primary: #8b7dff;
    --accent: #ff9478;
    --warn-bg: #3a2a12;
    --warn-ink: #ffcf8a;
    --border: #312c50;
    --bubble-me: #6c5cff;
    --bubble-mia: #26223f;
    --shadow: 0 2px 12px rgba(0, 0, 0, 0.35);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: 'Nunito', system-ui, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

#app {
  max-width: 560px;
  margin: 0 auto;
  min-height: 100dvh;
  position: relative;
}

.screen {
  padding: 16px 16px 96px;
  min-height: 100dvh;
}

[hidden] { display: none !important; }

h1 { margin: 0 0 8px; font-weight: 800; }
.title { font-size: 1.6rem; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }

.card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
}

/* Buttons */
.btn {
  border: 0;
  border-radius: 14px;
  padding: 12px 18px;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  background: var(--surface-2);
  color: var(--text);
}
.btn.primary { background: var(--primary); color: var(--primary-ink); }
.btn.big { width: 100%; padding: 16px; font-size: 1.1rem; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: 0.5; cursor: default; }

.icon-btn {
  border: 0;
  background: transparent;
  color: var(--text);
  font-size: 1.3rem;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); }

/* Onboarding */
.onboarding { text-align: center; padding-top: 24px; }
.hero-avatar, .avatar {
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--primary), var(--accent));
  color: #fff;
  font-weight: 800;
}
.hero-avatar { width: 96px; height: 96px; font-size: 2.6rem; margin: 0 auto 16px; }
.avatar.small { width: 36px; height: 36px; font-size: 1rem; flex: none; }

.form { display: grid; gap: 18px; text-align: left; margin-top: 20px; }
.form label { display: grid; gap: 6px; font-weight: 700; }
.form label.row { display: flex; align-items: center; gap: 10px; font-weight: 600; }
.form input:not([type=radio]):not([type=checkbox]):not([type=range]), .form select {
  font: inherit;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
}
fieldset { border: 0; padding: 0; margin: 0; }
legend { font-weight: 700; margin-bottom: 8px; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  display: inline-flex !important;
  align-items: center;
  padding: 10px 14px;
  border-radius: 999px;
  border: 2px solid var(--border);
  background: var(--surface);
  cursor: pointer;
  font-weight: 600 !important;
}
.chip input { position: absolute; opacity: 0; pointer-events: none; }
.chip:has(input:checked) { border-color: var(--primary); background: var(--surface-2); }
.chip:has(input:focus-visible) { outline: 2px solid var(--primary); outline-offset: 2px; }

/* Home */
.topbar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; }
.level-badge {
  background: var(--primary);
  color: var(--primary-ink);
  font-weight: 800;
  padding: 6px 12px;
  border-radius: 999px;
}

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 20px; }
.stat {
  background: var(--surface);
  border-radius: 16px;
  padding: 12px 8px;
  box-shadow: var(--shadow);
  display: grid;
  text-align: center;
}
.stat-value { font-size: 1.5rem; font-weight: 800; }
.stat-label { font-size: 0.75rem; color: var(--muted); }

.section-title { font-size: 1.05rem; margin: 20px 0 10px; }
.lesson-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.lesson-card {
  text-align: left;
  border: 0;
  font: inherit;
  color: inherit;
  cursor: pointer;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px;
  display: grid;
  gap: 4px;
  transition: transform 0.12s;
}
.lesson-card:hover { transform: translateY(-2px); }
.lesson-card.featured { grid-column: 1 / -1; background: linear-gradient(135deg, var(--primary), #8a5bff); color: #fff; }
.lesson-card.featured .muted { color: rgba(255, 255, 255, 0.85); }
.lesson-emoji { font-size: 1.8rem; }
.lesson-card strong { font-size: 1rem; }
.lesson-card .muted { font-size: 0.82rem; }

/* Chat */
.chat-screen { display: flex; flex-direction: column; padding: 0; height: 100dvh; }
.chat-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
  z-index: 1;
}
.chat-title { display: flex; align-items: center; gap: 10px; flex: 1; min-width: 0; }
.chat-title > div:last-child { display: grid; min-width: 0; }
.chat-title strong { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 10px; }

.msg { max-width: 85%; display: grid; gap: 6px; }
.msg.me { align-self: flex-end; }
.msg.mia { align-self: flex-start; }
.bubble { padding: 12px 14px; border-radius: 18px; line-height: 1.4; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.me .bubble { background: var(--bubble-me); color: var(--bubble-me-ink); border-bottom-right-radius: 6px; }
.msg.mia .bubble { background: var(--bubble-mia); border-bottom-left-radius: 6px; box-shadow: var(--shadow); }
.translation { font-size: 0.9rem; color: var(--muted); padding: 0 6px; }
.msg-actions { display: flex; gap: 4px; }
.msg-actions button {
  border: 0;
  background: var(--surface-2);
  color: var(--text);
  border-radius: 999px;
  padding: 4px 10px;
  font: inherit;
  font-size: 0.8rem;
  cursor: pointer;
}

.correction {
  align-self: flex-end;
  max-width: 85%;
  background: var(--warn-bg);
  color: var(--warn-ink);
  border-radius: 14px;
  padding: 10px 12px;
  font-size: 0.9rem;
  display: grid;
  gap: 4px;
}
.correction s { opacity: 0.8; }
.correction b { color: var(--ok); }

.words { display: flex; flex-wrap: wrap; gap: 6px; }
.word-chip {
  border: 1px dashed var(--primary);
  background: transparent;
  color: var(--text);
  border-radius: 12px;
  padding: 6px 10px;
  font: inherit;
  font-size: 0.85rem;
  cursor: pointer;
  text-align: left;
}
.word-chip.saved { border-style: solid; background: var(--surface-2); }

.typing { align-self: flex-start; color: var(--muted); font-style: italic; padding: 4px 8px; }

.suggestions { display: flex; gap: 8px; overflow-x: auto; padding: 0 16px 8px; scrollbar-width: none; }
.suggestions button {
  flex: none;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 999px;
  padding: 8px 12px;
  font: inherit;
  font-size: 0.85rem;
  cursor: pointer;
  max-width: 80%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.composer {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--border);
}
.composer input {
  flex: 1;
  min-width: 0;
  font: inherit;
  padding: 12px 14px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
}
.mic {
  flex: none;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: 0;
  font-size: 1.5rem;
  background: var(--primary);
  color: #fff;
  cursor: pointer;
  box-shadow: 0 4px 14px rgba(91, 75, 255, 0.4);
}
.mic.listening { background: var(--accent); animation: pulse 1.2s infinite; }
@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(255, 122, 89, 0.6); }
  100% { box-shadow: 0 0 0 18px rgba(255, 122, 89, 0); }
}

/* Vocab */
.vocab-list { list-style: none; padding: 0; margin: 0; display: grid; gap: 10px; }
.vocab-item { display: flex; gap: 10px; align-items: flex-start; }
.vocab-item > div { flex: 1; display: grid; gap: 2px; }
.vocab-item em { color: var(--muted); font-size: 0.9rem; }

/* Tab bar */
.tabbar {
  position: fixed;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 100%;
  max-width: 560px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar button {
  border: 0;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 0.75rem;
  font-weight: 700;
  padding: 8px 0 10px;
  display: grid;
  gap: 2px;
  cursor: pointer;
}
.tabbar button span { font-size: 1.3rem; }
.tabbar button.active { color: var(--primary); }

/* Dialog + toast */
dialog {
  border: 0;
  border-radius: 24px;
  padding: 0;
  width: min(420px, calc(100% - 32px));
  background: var(--surface);
  color: var(--text);
}
dialog::backdrop { background: rgba(10, 8, 30, 0.55); }
.dialog-body { padding: 24px; text-align: center; display: grid; gap: 8px; }
.level-big { font-size: 3.5rem; font-weight: 800; color: var(--primary); }

.toast {
  position: fixed;
  left: 50%;
  bottom: 90px;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 12px;
  font-size: 0.9rem;
  max-width: calc(100% - 32px);
  z-index: 10;
}
