:root {
  --bg: #0e1117;
  --bg-2: #161b25;
  --panel: #1c2230;
  --panel-2: #232a3a;
  --border: #2b3344;
  --text: #e7ecf3;
  --muted: #98a3b6;
  --primary: #6c8cff;
  --primary-2: #8aa2ff;
  --accent: #36d399;
  --warn: #fbbd23;
  --danger: #f87272;
  --good: #36d399;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
  --radius: 16px;
  --radius-sm: 10px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background:
    radial-gradient(1200px 600px at 85% -10%, rgba(108, 140, 255, 0.18), transparent 60%),
    radial-gradient(900px 500px at 0% 0%, rgba(54, 211, 153, 0.10), transparent 55%),
    var(--bg);
  color: var(--text);
  font-family: "Inter", system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, .brand-name { font-family: "Lexend", "Inter", sans-serif; }

a { color: var(--primary-2); }

.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
code {
  background: var(--bg-2);
  border: 1px solid var(--border);
  padding: 1px 6px;
  border-radius: 6px;
  font-size: 0.85em;
}

/* ============ TOP BAR ============ */
.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 22px;
  background: rgba(14, 17, 23, 0.7);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  user-select: none;
}
.brand-logo { font-size: 1.5rem; }
.brand-name { font-weight: 800; font-size: 1.25rem; letter-spacing: -0.02em; }
.brand-name span { color: var(--primary); }

.topbar-right { display: flex; align-items: center; gap: 12px; }
.streak {
  background: var(--panel);
  border: 1px solid var(--border);
  padding: 6px 12px;
  border-radius: 999px;
  font-weight: 600;
  font-size: 0.9rem;
}
.level-pill {
  background: linear-gradient(135deg, var(--primary), var(--accent));
  color: #0b0f17;
  font-weight: 800;
  padding: 6px 14px;
  border-radius: 999px;
  font-size: 0.9rem;
}
.icon-btn {
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--text);
  width: 38px;
  height: 38px;
  border-radius: 10px;
  font-size: 1.1rem;
  cursor: pointer;
  transition: 0.15s;
}
.icon-btn:hover { background: var(--panel-2); transform: translateY(-1px); }

/* ============ LAYOUT ============ */
#app { max-width: 1080px; margin: 0 auto; padding: 28px 22px 80px; }
.view { animation: fade 0.3s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

/* ============ HERO / HOME ============ */
.hero { text-align: center; padding: 18px 0 8px; }
.hero h1 { font-size: clamp(1.8rem, 4vw, 2.8rem); margin: 0 0 10px; letter-spacing: -0.02em; }
.hero p { color: var(--muted); max-width: 640px; margin: 0 auto 8px; font-size: 1.05rem; }

.level-row {
  display: flex;
  gap: 8px;
  justify-content: center;
  flex-wrap: wrap;
  margin: 22px 0 10px;
}
.level-chip {
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--muted);
  padding: 8px 16px;
  border-radius: 999px;
  cursor: pointer;
  font-weight: 600;
  font-size: 0.9rem;
  transition: 0.15s;
}
.level-chip small { display: block; font-weight: 400; font-size: 0.72rem; opacity: 0.8; }
.level-chip:hover { border-color: var(--primary); color: var(--text); }
.level-chip.active {
  background: linear-gradient(135deg, rgba(108,140,255,0.25), rgba(54,211,153,0.18));
  border-color: var(--primary);
  color: var(--text);
}

/* ============ MODE GRID ============ */
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 16px;
  margin-top: 26px;
}
.card {
  background: linear-gradient(180deg, var(--panel), var(--bg-2));
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 22px;
  cursor: pointer;
  transition: 0.18s;
  position: relative;
  overflow: hidden;
}
.card:hover { transform: translateY(-4px); border-color: var(--primary); box-shadow: var(--shadow); }
.card .emoji { font-size: 2rem; }
.card h3 { margin: 12px 0 6px; font-size: 1.15rem; }
.card p { margin: 0; color: var(--muted); font-size: 0.92rem; line-height: 1.5; }
.card .tag {
  position: absolute; top: 14px; right: 14px;
  font-size: 0.7rem; font-weight: 700; letter-spacing: 0.03em;
  background: var(--bg); border: 1px solid var(--border);
  padding: 3px 8px; border-radius: 999px; color: var(--muted);
}

/* ============ GENERIC PANEL ============ */
.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 22px;
  margin-bottom: 18px;
}
.panel-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; flex-wrap: wrap; gap: 10px; }
.panel-head h2 { margin: 0; font-size: 1.4rem; }

.back-btn {
  background: none; border: none; color: var(--muted);
  cursor: pointer; font-size: 0.95rem; margin-bottom: 14px; padding: 0;
}
.back-btn:hover { color: var(--text); }

/* ============ BUTTONS ============ */
.btn {
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  padding: 11px 18px;
  border-radius: var(--radius-sm);
  font-weight: 600;
  font-size: 0.95rem;
  cursor: pointer;
  transition: 0.15s;
  font-family: inherit;
}
.btn:hover { transform: translateY(-1px); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }
.btn-primary { background: linear-gradient(135deg, var(--primary), var(--primary-2)); border: none; color: #0b0f17; }
.btn-accent { background: linear-gradient(135deg, var(--accent), #2bb98a); border: none; color: #06231a; }
.btn-ghost { background: transparent; }
.btn-sm { padding: 7px 12px; font-size: 0.85rem; }
.btn-block { width: 100%; }

/* ============ FORM CONTROLS ============ */
.field-label { display: block; font-weight: 600; margin: 14px 0 6px; font-size: 0.9rem; }
.text-input, textarea, select {
  width: 100%;
  background: var(--bg-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 11px 13px;
  border-radius: var(--radius-sm);
  font-size: 0.98rem;
  font-family: inherit;
  outline: none;
  transition: 0.15s;
}
.text-input:focus, textarea:focus, select:focus { border-color: var(--primary); }
textarea { resize: vertical; min-height: 120px; line-height: 1.6; }
.field-row { display: flex; flex-direction: column; }

.chip-row { display: flex; gap: 8px; flex-wrap: wrap; margin: 8px 0; }
.chip {
  background: var(--bg-2); border: 1px solid var(--border); color: var(--text);
  padding: 8px 14px; border-radius: 999px; cursor: pointer; font-size: 0.9rem; transition: 0.15s;
}
.chip:hover { border-color: var(--primary); }
.chip.active { background: var(--primary); color: #0b0f17; border-color: var(--primary); font-weight: 600; }

/* ============ MODAL ============ */
.modal-overlay {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(4, 6, 12, 0.7);
  backdrop-filter: blur(6px);
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
}
/* Author `display:flex` above beats the UA `[hidden]` rule, so the modal
   couldn't hide. This restores hiding when the hidden attribute is set. */
.modal-overlay[hidden] { display: none !important; }
.modal {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px;
  max-width: 480px;
  width: 100%;
  box-shadow: var(--shadow);
  animation: pop 0.2s ease;
}
@keyframes pop { from { transform: scale(0.96); opacity: 0; } to { transform: none; opacity: 1; } }
.modal h2 { margin: 0 0 10px; }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 20px; }
.setup-status { margin-top: 14px; font-size: 0.9rem; min-height: 20px; }
.setup-status.ok { color: var(--good); }
.setup-status.err { color: var(--danger); }

/* ============ CHAT (conversation) ============ */
.chat-wrap { display: flex; flex-direction: column; height: min(64vh, 620px); }
.scenario-banner {
  background: var(--bg-2); border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 12px 16px; margin-bottom: 12px; font-size: 0.92rem; color: var(--muted);
}
.scenario-banner b { color: var(--text); }
.chat-log { flex: 1; overflow-y: auto; padding: 6px 4px; display: flex; flex-direction: column; gap: 12px; }
.bubble {
  max-width: 78%; padding: 12px 16px; border-radius: 16px; line-height: 1.55; font-size: 0.98rem;
  white-space: pre-wrap; word-wrap: break-word; animation: fade 0.25s ease;
}
.bubble.ai { background: var(--panel-2); border: 1px solid var(--border); align-self: flex-start; border-bottom-left-radius: 4px; }
.bubble.user { background: linear-gradient(135deg, var(--primary), var(--primary-2)); color: #0b0f17; align-self: flex-end; border-bottom-right-radius: 4px; font-weight: 500; }
.bubble.system { align-self: center; background: transparent; border: 1px dashed var(--border); color: var(--muted); font-size: 0.85rem; max-width: 90%; text-align: center; }
.bubble .speak-btn { background: none; border: none; cursor: pointer; opacity: 0.6; margin-left: 6px; font-size: 0.85rem; }
.bubble .speak-btn:hover { opacity: 1; }

.feedback-box {
  align-self: flex-start; max-width: 90%;
  background: rgba(251, 189, 35, 0.08); border: 1px solid rgba(251,189,35,0.4);
  border-radius: 12px; padding: 10px 14px; font-size: 0.9rem; line-height: 1.5;
}
.feedback-box b { color: var(--warn); }

.chat-input-row { display: flex; gap: 8px; margin-top: 12px; align-items: flex-end; }
.chat-input-row textarea { min-height: 48px; max-height: 140px; }
.mic-btn {
  width: 48px; height: 48px; flex: 0 0 auto; border-radius: 12px;
  background: var(--panel-2); border: 1px solid var(--border); color: var(--text);
  cursor: pointer; font-size: 1.2rem; transition: 0.15s;
}
.mic-btn.listening { background: var(--danger); border-color: var(--danger); animation: pulse 1.2s infinite; }
@keyframes pulse { 0%,100% { box-shadow: 0 0 0 0 rgba(248,114,114,0.5); } 50% { box-shadow: 0 0 0 10px rgba(248,114,114,0); } }

/* ============ EXERCISES ============ */
.ex-q { margin-bottom: 18px; }
.ex-q .q-text { font-size: 1.08rem; font-weight: 500; margin-bottom: 6px; line-height: 1.5; }
.ex-q .q-meta { font-size: 0.78rem; color: var(--muted); margin-bottom: 10px; text-transform: uppercase; letter-spacing: 0.04em; }
.opt-list { display: flex; flex-direction: column; gap: 8px; }
.opt {
  display: flex; align-items: center; gap: 10px;
  background: var(--bg-2); border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 12px 14px; cursor: pointer; transition: 0.12s; font-size: 0.98rem;
}
.opt:hover { border-color: var(--primary); }
.opt.selected { border-color: var(--primary); background: rgba(108,140,255,0.12); }
.opt.correct { border-color: var(--good); background: rgba(54,211,153,0.14); }
.opt.wrong { border-color: var(--danger); background: rgba(248,114,114,0.14); }
.opt .marker { width: 22px; height: 22px; flex: 0 0 auto; border-radius: 50%; border: 2px solid var(--border); display: grid; place-items: center; font-size: 0.8rem; }
.opt.correct .marker { border-color: var(--good); color: var(--good); }
.opt.wrong .marker { border-color: var(--danger); color: var(--danger); }

.blank-input { display: inline-block; width: 140px; margin: 0 4px; padding: 4px 8px; }

.explain {
  margin-top: 10px; padding: 10px 14px; border-radius: var(--radius-sm);
  background: rgba(108,140,255,0.08); border: 1px solid rgba(108,140,255,0.3);
  font-size: 0.92rem; line-height: 1.5;
}
.explain.correct { background: rgba(54,211,153,0.08); border-color: rgba(54,211,153,0.35); }
.explain.wrong { background: rgba(248,114,114,0.08); border-color: rgba(248,114,114,0.35); }

.progressbar { height: 8px; background: var(--bg-2); border-radius: 999px; overflow: hidden; margin-bottom: 16px; }
.progressbar > div { height: 100%; background: linear-gradient(90deg, var(--primary), var(--accent)); transition: width 0.3s; }

.score-card { text-align: center; padding: 30px 10px; }
.score-card .big { font-size: 3rem; font-weight: 800; font-family: "Lexend"; }

/* ============ WRITING FEEDBACK ============ */
.fb-grid { display: grid; gap: 14px; margin-top: 16px; }
.fb-block { background: var(--bg-2); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 14px 16px; }
.fb-block h4 { margin: 0 0 8px; font-size: 0.95rem; color: var(--primary-2); }
.fb-block ul { margin: 0; padding-left: 18px; }
.fb-block li { margin-bottom: 6px; line-height: 1.5; }
.score-pills { display: flex; gap: 10px; flex-wrap: wrap; }
.score-pill { background: var(--panel-2); border: 1px solid var(--border); border-radius: 999px; padding: 6px 14px; font-size: 0.85rem; }
.score-pill b { color: var(--accent); }
.diff-ins { background: rgba(54,211,153,0.22); border-radius: 3px; }
.diff-del { background: rgba(248,114,114,0.20); text-decoration: line-through; border-radius: 3px; }

/* ============ VOCAB / FLASHCARDS ============ */
.flashcard {
  perspective: 1200px; height: 280px; margin: 0 auto; max-width: 460px; cursor: pointer;
}
.flashcard-inner { position: relative; width: 100%; height: 100%; transition: transform 0.5s; transform-style: preserve-3d; }
.flashcard.flipped .flashcard-inner { transform: rotateY(180deg); }
.flash-face {
  position: absolute; inset: 0; backface-visibility: hidden;
  background: linear-gradient(180deg, var(--panel), var(--bg-2));
  border: 1px solid var(--border); border-radius: var(--radius);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 26px; text-align: center;
}
.flash-back { transform: rotateY(180deg); }
.flash-word { font-size: 2rem; font-weight: 800; font-family: "Lexend"; }
.flash-pos { color: var(--muted); font-style: italic; margin-top: 4px; }
.flash-def { font-size: 1.05rem; line-height: 1.5; }
.flash-ex { color: var(--muted); font-style: italic; margin-top: 12px; font-size: 0.95rem; }
.flash-nav { display: flex; align-items: center; justify-content: space-between; margin-top: 18px; }

/* ============ LOADING ============ */
.loader { display: flex; align-items: center; gap: 12px; color: var(--muted); padding: 20px 0; justify-content: center; }
.spinner { width: 22px; height: 22px; border: 3px solid var(--border); border-top-color: var(--primary); border-radius: 50%; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.typing-dots span { display: inline-block; width: 7px; height: 7px; margin: 0 1px; background: var(--muted); border-radius: 50%; animation: blink 1.2s infinite; }
.typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.typing-dots span:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%,80%,100% { opacity: 0.3; } 40% { opacity: 1; } }

/* ============ TOAST ============ */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--panel); border: 1px solid var(--border); color: var(--text);
  padding: 12px 20px; border-radius: 12px; box-shadow: var(--shadow); z-index: 60;
  font-size: 0.92rem; animation: fade 0.2s;
}
.toast.err { border-color: var(--danger); }
.toast.ok { border-color: var(--good); }

/* ============ MISC ============ */
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.spacer { flex: 1; }
.center { text-align: center; }
.mt { margin-top: 16px; }
.mt-sm { margin-top: 8px; }
.hidden { display: none !important; }
.pill-stat { display: inline-flex; gap: 6px; align-items: center; background: var(--bg-2); border: 1px solid var(--border); padding: 5px 12px; border-radius: 999px; font-size: 0.85rem; }

@media (max-width: 600px) {
  .topbar { padding: 12px 16px; }
  #app { padding: 20px 14px 70px; }
  .bubble { max-width: 88%; }
}
