:root {
  --bg: #f6f4ef;
  --card: #ffffff;
  --text: #1f2a2e;
  --muted: #6b7478;
  --line: #e4e0d8;
  --primary: #2f8f83;
  --primary-dark: #24736a;
  --accent: #f4a340;
  --tutor: #ffffff;
  --user: #2f8f83;
  --user-text: #ffffff;
  --error: #c8443a;
  --ok: #2f8a4c;
  --corr-bg: #fff8ec;
  --radius: 16px;
  --shadow: 0 2px 10px rgba(30, 40, 40, 0.08);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #151b1d;
    --card: #1f272a;
    --text: #e8eceb;
    --muted: #9aa4a6;
    --line: #2e3a3d;
    --primary: #3aa597;
    --primary-dark: #2f8f83;
    --tutor: #253033;
    --user: #2f8f83;
    --corr-bg: #2c2a22;
    --shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans Thai", "Leelawadee UI", sans-serif;
  -webkit-tap-highlight-color: transparent;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.error { color: var(--error); }
.ok { color: var(--ok); }
.label { display: block; font-size: 0.85rem; color: var(--muted); margin-bottom: 6px; }
h1, h2, h3 { line-height: 1.25; }
h2 { margin: 0 0 12px; }
h3 { margin: 24px 0 12px; }

button { font: inherit; color: inherit; cursor: pointer; }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--card);
  border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: -1px; }

.btn {
  border: 1px solid var(--line); background: var(--card); border-radius: 12px;
  padding: 11px 18px; font-weight: 600;
}
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-dark); }
.btn.primary:disabled { opacity: 0.5; cursor: default; }
.btn.ghost { background: transparent; }
.btn.small { padding: 7px 12px; font-size: 0.9rem; }
.btn.big { padding: 14px 24px; font-size: 1.1rem; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.check { display: flex; align-items: center; gap: 8px; }
.check input { width: auto; }
.row-between { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; }

/* ---------------- Anmeldung */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.auth-card {
  background: var(--card); border-radius: 24px; box-shadow: var(--shadow);
  padding: 28px 24px; width: 100%; max-width: 400px; text-align: center;
}
.auth-card form { text-align: left; margin-top: 16px; }
.auth-card h1 { margin: 8px 0 4px; }
.auth-avatar { width: 140px; margin: 0 auto; }
.note { background: var(--corr-bg); padding: 10px 12px; border-radius: 10px; margin: 0; }
.lang-switch { display: inline-flex; gap: 4px; background: var(--bg); padding: 4px; border-radius: 999px; margin-top: 8px; }
.lang-switch button { border: 0; background: transparent; padding: 5px 14px; border-radius: 999px; font-size: 0.9rem; }
.lang-switch button.active { background: var(--primary); color: #fff; }

/* ---------------- Kopfzeile */
.topbar {
  position: sticky; top: 0; z-index: 10; display: flex; align-items: center; justify-content: space-between;
  gap: 8px; padding: 10px 16px; background: var(--card); border-bottom: 1px solid var(--line);
}
.brand { border: 0; background: none; display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 1.05rem; padding: 0; }
.logo {
  display: inline-grid; place-items: center; width: 32px; height: 32px; border-radius: 10px;
  background: var(--primary); color: #fff; font-size: 0.8rem; font-weight: 800;
}
.nav { display: flex; gap: 2px; overflow-x: auto; }
.nav button { border: 0; background: none; padding: 8px 12px; border-radius: 10px; white-space: nowrap; color: var(--muted); }
.nav button.active { background: var(--bg); color: var(--text); font-weight: 600; }
.nav button[data-nav="home"] { display: none; }

main { max-width: 980px; margin: 0 auto; padding: 20px 16px 40px; }

/* ---------------- Start */
.home-hero { display: flex; align-items: center; gap: 20px; margin-bottom: 8px; }
.home-avatar { width: 120px; flex-shrink: 0; }
.home-hero h2 { margin: 0 0 4px; }
.modes { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 12px; }
.mode-card {
  display: flex; gap: 14px; align-items: center; text-align: left; padding: 16px;
  background: var(--card); border: 2px solid transparent; border-radius: var(--radius); box-shadow: var(--shadow);
}
.mode-card.active { border-color: var(--primary); }
.mode-icon { font-size: 1.8rem; }
.mode-text { display: flex; flex-direction: column; }
.mode-text small { color: var(--muted); }
.mode-options {
  margin-top: 16px; padding: 16px; background: var(--card); border-radius: var(--radius);
  box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 14px;
}
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  border: 1px solid var(--line); background: var(--card); border-radius: 999px; padding: 7px 14px;
  font-size: 0.95rem; text-align: left;
}
.chip.active { background: var(--primary); border-color: var(--primary); color: #fff; }
.chip.small { padding: 3px 10px; font-size: 0.8rem; }
.chip:disabled { opacity: 0.7; cursor: default; }

/* ---------------- Gespräch */
body.in-chat main { max-width: 1100px; padding-bottom: 0; }
.chat-layout { display: grid; grid-template-columns: 300px 1fr; gap: 20px; height: calc(100vh - 100px); height: calc(100dvh - 100px); }
.stage {
  background: var(--card); border-radius: 24px; box-shadow: var(--shadow); padding: 20px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px;
}
.stage-avatar { width: 100%; max-width: 260px; }
.status { min-height: 1.5em; color: var(--muted); margin: 0; text-align: center; }
.chat-main { display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.log { flex: 1; overflow-y: auto; overflow-x: hidden; overflow-wrap: anywhere; padding: 4px 4px 12px; display: flex; flex-direction: column; gap: 10px; }

.bubble { max-width: 85%; padding: 12px 16px; border-radius: 18px; box-shadow: var(--shadow); position: relative; }
.bubble p { margin: 0; }
.bubble.tutor { background: var(--tutor); align-self: flex-start; border-bottom-left-radius: 6px; }
.bubble.user { background: var(--user); color: var(--user-text); align-self: flex-end; border-bottom-right-radius: 6px; }
.bubble.user.failed { opacity: 0.5; }
.bubble.fresh { animation: pop 0.25s ease-out; }
@keyframes pop { from { transform: translateY(6px); opacity: 0; } to { transform: none; opacity: 1; } }
.spoken-tag { position: absolute; left: -24px; bottom: 6px; font-size: 0.85rem; opacity: 0.7; }
.translation { color: var(--muted); font-style: italic; margin-top: 6px !important; padding-top: 6px; border-top: 1px dashed var(--line); }
.praise { font-size: 0.85rem; color: #b7791f; font-weight: 600; margin-bottom: 4px; }
.bubble-tools { display: flex; gap: 2px; margin: 6px -6px -6px; }
.icon-btn { border: 0; background: none; padding: 4px 6px; border-radius: 8px; font-size: 1rem; line-height: 1; }
.icon-btn:hover { background: var(--bg); }
.vocab-chips { margin-top: 10px; padding-top: 8px; border-top: 1px solid var(--line); display: flex; flex-direction: column; gap: 6px; }
.vocab-chips .label { margin: 0; }
.vocab-item { display: flex; align-items: center; flex-wrap: wrap; gap: 4px; font-size: 0.92rem; }

.typing { display: flex; gap: 5px; padding: 16px 18px; }
.typing span { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite; }
.typing span:nth-child(2) { animation-delay: 0.2s; }
.typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%, 80%, 100% { opacity: 0.25; } 40% { opacity: 1; } }

.corrections { align-self: flex-end; max-width: 85%; display: flex; flex-direction: column; gap: 6px; margin-top: -4px; }
.corr { background: var(--corr-bg); border-left: 4px solid var(--accent); border-radius: 10px; padding: 8px 12px; font-size: 0.92rem; }
.corr.ok { border-left-color: var(--ok); color: var(--ok); }
.corr.translation { border-left-color: #6a8dd6; }
.corr.style { border-left-color: #9b7bd1; }
.corr-kind { font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); font-weight: 700; }
.corr-orig { text-decoration: line-through; text-decoration-color: var(--error); color: var(--muted); }
.corr-orig.plain { text-decoration: none; }
.corr-fixed { display: flex; align-items: center; flex-wrap: wrap; gap: 4px; }
.corr-fixed strong { color: var(--ok); }
.corr-expl { color: var(--text); opacity: 0.85; }

.suggestions { display: flex; flex-wrap: wrap; gap: 8px; padding: 8px 0; }
.composer {
  display: flex; align-items: flex-end; gap: 8px; padding: 10px 0 16px;
  padding-bottom: max(16px, env(safe-area-inset-bottom));
}
.composer textarea { resize: none; scrollbar-width: none; min-height: 48px; max-height: 140px; border-radius: 24px; padding: 12px 18px; }
.mic-btn, .send-btn, .hint-btn {
  flex-shrink: 0; width: 48px; height: 48px; border-radius: 50%; border: 0; display: grid; place-items: center;
}
.hint-btn { background: var(--card); box-shadow: var(--shadow); font-size: 1.2rem; }
.mic-btn { background: var(--accent); color: #fff; width: 56px; height: 56px; box-shadow: var(--shadow); }
.mic-btn.on { background: #e2574c; animation: pulse 1s infinite; }
.send-btn { background: var(--primary); color: #fff; }
.mic-btn:disabled, .send-btn:disabled { opacity: 0.5; cursor: default; }
@keyframes pulse { 0% { box-shadow: 0 0 0 0 rgba(226, 87, 76, 0.5); } 100% { box-shadow: 0 0 0 16px rgba(226, 87, 76, 0); } }

/* ---------------- Listen */
.list { display: flex; flex-direction: column; gap: 10px; }
.list-item {
  background: var(--card); border-radius: 14px; box-shadow: var(--shadow); padding: 12px 14px;
  display: flex; justify-content: space-between; align-items: flex-start; gap: 10px;
}
.list-item.corr { background: var(--card); }
.list-item.clickable { cursor: pointer; }
.list-item.clickable:hover { outline: 2px solid var(--line); }

/* ---------------- Einstellungen */
.settings { max-width: 480px; }
.settings label > span { display: block; font-size: 0.85rem; color: var(--muted); margin-bottom: 4px; }
.settings label.check > span { display: inline; font-size: 1rem; color: var(--text); margin: 0; }
.settings input[type="range"] { padding: 0; border: 0; background: none; }
.avatar-pick { display: flex; gap: 12px; }
.avatar-choice {
  border: 2px solid var(--line); background: var(--card); border-radius: 16px; padding: 8px 12px 6px;
  display: flex; flex-direction: column; align-items: center; gap: 2px; width: 120px;
}
.avatar-choice.active { border-color: var(--primary); }
.avatar-mini { width: 90px; }
#logout-btn { margin-top: 28px; }

.avatar-svg { width: 100%; height: auto; display: block; }

.toast {
  position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%); z-index: 50;
  background: #1f2a2e; color: #fff; padding: 10px 18px; border-radius: 12px; max-width: calc(100% - 32px);
  box-shadow: var(--shadow); text-align: center;
}

/* ---------------- Handy */
@media (max-width: 760px) {
  .nav button { padding: 8px 8px; font-size: 0.9rem; }
  .brand span:not(.logo) { display: none; }
  body.in-chat .topbar { display: none; }
  body.in-chat main { padding: 0 12px; }
  .chat-layout { grid-template-columns: 1fr; grid-template-rows: auto 1fr; gap: 6px; height: 100vh; height: 100dvh; }
  .stage {
    flex-direction: row; padding: 8px 12px; border-radius: 0 0 20px 20px; margin: 0 -12px;
    justify-content: flex-start; gap: 12px;
  }
  .stage-avatar { width: 110px; flex-shrink: 0; }
  .status { flex: 1; text-align: left; font-size: 0.9rem; }
  .bubble, .corrections { max-width: 92%; }
  .spoken-tag { display: none; }
  .home-avatar { width: 90px; }
  .modes { grid-template-columns: 1fr; }
}
code { word-break: break-all; font-size: 0.85rem; }
