:root {
  --bg: #f6f5fb;
  --surface: #ffffff;
  --surface-2: #efedf8;
  --text: #1d1b2b;
  --muted: #6b6880;
  --primary: #5b4cdb;
  --primary-soft: #e6e2ff;
  --good: #1f9d63;
  --good-soft: #dcf5e8;
  --bad: #d64545;
  --bad-soft: #fde4e4;
  --warn: #e8890c;
  --fire: #ff7a1a;
  --border: #e1deee;
  --shadow: 0 2px 10px rgba(40, 30, 90, .07);
  --radius: 16px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14131c;
    --surface: #1e1c2a;
    --surface-2: #29263a;
    --text: #f0eef8;
    --muted: #a19db8;
    --primary: #8f83ff;
    --primary-soft: #2f2a55;
    --good: #3ccf8a;
    --good-soft: #173a2a;
    --bad: #ff6b6b;
    --bad-soft: #3d1d22;
    --border: #322f45;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  line-height: 1.45;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; cursor: pointer; }
input, textarea, select { font: inherit; color: var(--text); }

#app {
  max-width: 560px;
  margin: 0 auto;
  min-height: 100%;
  display: flex;
  flex-direction: column;
}

.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 8px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: var(--bg);
}
.topbar h1 { flex: 1; font-size: 1.25rem; margin: 0; }
.icon-btn {
  border: 0; background: var(--surface-2); border-radius: 10px;
  width: 36px; height: 36px; font-size: 1.1rem;
}
.lang-switch { display: flex; background: var(--surface-2); border-radius: 999px; padding: 3px; }
.lang-btn {
  border: 0; background: transparent; border-radius: 999px;
  padding: 5px 10px; font-size: .85rem; font-weight: 600; color: var(--muted);
}
.lang-btn.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

main {
  flex: 1;
  padding: 4px 16px calc(90px + env(safe-area-inset-bottom));
}

.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 5;
  display: flex; justify-content: center;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
.tab {
  flex: 1; max-width: 112px;
  border: 0; background: none;
  padding: 8px 0 10px;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  font-size: .7rem; color: var(--muted);
}
.tab span { font-size: 1.3rem; filter: grayscale(1); opacity: .7; }
.tab.active { color: var(--primary); font-weight: 600; }
.tab.active span { filter: none; opacity: 1; }

/* Generic building blocks */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  margin-bottom: 12px;
}
.card h2, .card h3 { margin: 0 0 6px; font-size: 1.05rem; }
.muted { color: var(--muted); font-size: .9rem; }
.row { display: flex; align-items: center; gap: 12px; }
.grow { flex: 1; min-width: 0; }
.stack > * + * { margin-top: 10px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 0; border-radius: 12px;
  padding: 12px 16px; font-weight: 600;
  background: var(--primary); color: #fff;
}
.btn.block { width: 100%; }
.btn.secondary { background: var(--surface-2); color: var(--text); }
.btn.ghost { background: transparent; color: var(--primary); padding: 8px; }
.btn.danger { background: var(--bad-soft); color: var(--bad); }
.btn:disabled { opacity: .5; cursor: default; }

.tile {
  text-align: left; width: 100%;
  display: flex; align-items: center; gap: 14px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow);
  margin-bottom: 10px;
}
.tile .emoji {
  width: 46px; height: 46px; flex: none; border-radius: 12px;
  display: grid; place-items: center; font-size: 1.5rem;
  background: var(--primary-soft);
}
.tile strong { display: block; }
.badge {
  background: var(--primary); color: #fff; border-radius: 999px;
  padding: 2px 9px; font-size: .8rem; font-weight: 700;
}

.field { display: block; margin-bottom: 14px; }
.field > span { display: block; font-size: .85rem; font-weight: 600; margin-bottom: 4px; }
.input {
  width: 100%; padding: 11px 12px;
  border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface);
}
textarea.input { resize: vertical; min-height: 44px; }

/* Home */
.hero {
  display: flex; align-items: center; gap: 16px;
}
.streak-num { font-size: 2.2rem; font-weight: 800; line-height: 1; }
.flame { font-size: 2.4rem; }
.flame.off { filter: grayscale(1); opacity: .45; }
.bar { height: 10px; background: var(--surface-2); border-radius: 999px; overflow: hidden; }
.bar > div { height: 100%; background: var(--primary); border-radius: 999px; transition: width .4s; }
.bar.good > div { background: var(--good); }

/* Flashcards */
.flash {
  min-height: 230px;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-align: center; gap: 8px;
}
.flash .front { font-size: 1.7rem; font-weight: 700; }
.flash .back { font-size: 1.2rem; }
.flash .example { font-style: italic; color: var(--muted); }
.rate { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.rate .btn { padding: 10px 4px; flex-direction: column; font-size: .85rem; gap: 0; }
.rate .btn small { font-weight: 400; opacity: .8; font-size: .7rem; }
.rate .again { background: var(--bad-soft); color: var(--bad); }
.rate .hard { background: #fff1dc; color: #9a5a00; }
.rate .goodr { background: var(--good-soft); color: var(--good); }
.rate .easy { background: var(--primary-soft); color: var(--primary); }
@media (prefers-color-scheme: dark) { .rate .hard { background: #3a2b14; color: #ffb64d; } }

.speak {
  border: 0; background: var(--primary-soft); color: var(--primary);
  border-radius: 999px; width: 40px; height: 40px; font-size: 1.1rem; flex: none;
}

/* Exercises */
.options { display: grid; gap: 8px; }
.option {
  text-align: left; padding: 13px 14px; border-radius: 12px;
  border: 2px solid var(--border); background: var(--surface); font-weight: 500;
}
.option.correct { border-color: var(--good); background: var(--good-soft); }
.option.wrong { border-color: var(--bad); background: var(--bad-soft); }
.chips { display: flex; flex-wrap: wrap; gap: 8px; min-height: 48px; }
.chip {
  border: 1px solid var(--border); background: var(--surface);
  border-radius: 10px; padding: 8px 12px; font-weight: 500;
}
.chip.used { visibility: hidden; }
.answer-zone {
  border-bottom: 2px dashed var(--border); padding-bottom: 8px; margin-bottom: 14px;
}
.feedback { border-radius: 12px; padding: 12px 14px; margin-top: 12px; }
.feedback.ok { background: var(--good-soft); color: var(--good); }
.feedback.ko { background: var(--bad-soft); color: var(--bad); }
.feedback p { margin: 4px 0 0; color: var(--text); }
.prompt-text { font-size: 1.2rem; font-weight: 600; }

/* Tutor chat */
.chat { display: flex; flex-direction: column; gap: 10px; padding-bottom: 130px; }
.msg { max-width: 86%; padding: 10px 13px; border-radius: 16px; white-space: pre-wrap; word-wrap: break-word; }
.msg.user { align-self: flex-end; background: var(--primary); color: #fff; border-bottom-right-radius: 4px; }
.msg.ai { align-self: flex-start; background: var(--surface); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.msg.err { align-self: center; background: var(--bad-soft); color: var(--bad); font-size: .9rem; }
.msg .tools { display: flex; gap: 6px; margin-top: 6px; }
.msg .tools button { border: 0; background: var(--surface-2); border-radius: 8px; padding: 3px 8px; font-size: .8rem; }
.fix {
  align-self: flex-start; max-width: 86%;
  background: #fff7e0; color: #5c4300;
  border-radius: 12px; padding: 9px 12px; font-size: .9rem; white-space: pre-wrap;
}
@media (prefers-color-scheme: dark) { .fix { background: #3a3014; color: #ffe08a; } }
.composer {
  position: fixed; left: 0; right: 0;
  bottom: calc(58px + env(safe-area-inset-bottom));
  background: var(--bg);
  padding: 8px 16px;
}
.composer-inner { max-width: 528px; margin: 0 auto; display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { flex: 1; max-height: 120px; }
.mic {
  border: 0; width: 46px; height: 46px; border-radius: 50%; flex: none;
  background: var(--surface-2); font-size: 1.2rem;
}
.mic.on { background: var(--bad); color: #fff; animation: pulse 1.2s infinite; }
@keyframes pulse { 50% { transform: scale(1.08); } }
.send { width: 46px; height: 46px; padding: 0; border-radius: 50%; flex: none; }
.scenarios { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.scenario {
  text-align: left; background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; padding: 12px; box-shadow: var(--shadow);
}
.scenario b { display: block; margin-top: 4px; }
.scenario small { color: var(--muted); }

/* Modo llamada */
.call-start { margin-bottom: 12px; background: var(--good); }
.call {
  position: fixed; inset: 0; z-index: 30;
  display: flex; flex-direction: column; align-items: center;
  padding: calc(16px + env(safe-area-inset-top)) 16px calc(20px + env(safe-area-inset-bottom));
  background: linear-gradient(180deg, #2a2160 0%, #14131c 100%);
  color: #fff;
}
.call-top { width: 100%; max-width: 528px; display: flex; justify-content: space-between; opacity: .9; }
.call-top span { font-variant-numeric: tabular-nums; opacity: .7; }
.orb {
  margin: 7vh 0 22px; width: 190px; height: 190px; flex: none;
  border: 0; border-radius: 50%; padding: 0; position: relative;
  background: radial-gradient(circle at 35% 30%, #b3aaff, #5b4cdb 60%, #3a2fa0);
  box-shadow: 0 0 60px rgba(143, 131, 255, .35);
  transition: transform .3s, filter .3s;
}
.orb span {
  position: absolute; inset: -14px; border-radius: 50%;
  border: 3px solid rgba(179, 170, 255, .5); opacity: 0;
}
.call[data-phase="listening"] .orb { background: radial-gradient(circle at 35% 30%, #9ff0c6, #1f9d63 60%, #146b43); box-shadow: 0 0 60px rgba(60, 207, 138, .4); }
.call[data-phase="listening"] .orb span { border-color: rgba(60, 207, 138, .6); animation: ring 1.6s ease-out infinite; }
.call[data-phase="speaking"] .orb { animation: talk .9s ease-in-out infinite; }
.call[data-phase="thinking"] .orb { animation: think 1.4s ease-in-out infinite; }
.call[data-phase="idle"] .orb { filter: grayscale(.7) brightness(.8); }
@keyframes ring { from { transform: scale(.9); opacity: 1; } to { transform: scale(1.25); opacity: 0; } }
@keyframes talk { 0%, 100% { transform: scale(1); } 30% { transform: scale(1.07); } 60% { transform: scale(.97); } }
@keyframes think { 50% { filter: brightness(1.25); transform: scale(.96); } }
.call-status { font-size: 1.05rem; font-weight: 600; text-align: center; min-height: 1.5em; max-width: 528px; }
.call-subs { flex: 1; width: 100%; max-width: 528px; overflow-y: auto; margin: 16px 0; text-align: center; }
.call-subs.off { visibility: hidden; }
.call-you { color: #b7f5d4; font-style: italic; margin-bottom: 12px; }
.call-caption { font-size: 1.2rem; line-height: 1.5; }
.call-fix {
  margin-top: 16px; text-align: left; white-space: pre-wrap; font-size: .9rem;
  background: rgba(255, 224, 138, .12); color: #ffe08a; border-radius: 12px; padding: 10px 12px;
}
.call-actions { display: flex; gap: 28px; align-items: center; }
.call-btn {
  width: 64px; height: 64px; border-radius: 50%; border: 0;
  background: rgba(255, 255, 255, .12); color: #fff; font-size: 1.5rem;
  display: flex; flex-direction: column; align-items: center; justify-content: center; position: relative;
}
.call-btn small { position: absolute; bottom: -22px; font-size: .7rem; opacity: .75; white-space: nowrap; }
.call-btn.active { background: rgba(255, 255, 255, .3); }
.call-btn.hang { width: 76px; height: 76px; background: #e5484d; font-size: 1.8rem; }
.call-btn.hang .ico { display: inline-block; transform: rotate(135deg); }
.call-btn.hang small { bottom: -24px; }

/* Progress */
.stat { text-align: center; }
.stat .num { font-size: 1.8rem; font-weight: 800; }
.week { display: flex; align-items: flex-end; gap: 8px; height: 120px; padding-top: 10px; }
.week .col { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 4px; height: 100%; justify-content: flex-end; }
.week .colbar { width: 100%; background: var(--primary); border-radius: 6px 6px 0 0; min-height: 3px; }
.week .colbar.met { background: var(--good); }
.week small { font-size: .7rem; color: var(--muted); }

.empty { text-align: center; padding: 30px 10px; }
.empty .big { font-size: 3rem; }

#toast {
  position: fixed; left: 50%; bottom: calc(80px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(20px);
  background: var(--text); color: var(--bg);
  padding: 10px 16px; border-radius: 12px; font-weight: 600; font-size: .9rem;
  opacity: 0; pointer-events: none; transition: .25s; z-index: 40;
  max-width: calc(100% - 32px); text-align: center;
}
#toast.show { opacity: 1; transform: translateX(-50%); }

.spinner {
  width: 18px; height: 18px; border-radius: 50%;
  border: 3px solid var(--primary-soft); border-top-color: var(--primary);
  animation: spin .8s linear infinite; display: inline-block;
}
@keyframes spin { to { transform: rotate(360deg); } }
