:root {
  --bg: #f4f2ff;
  --bg-2: #ebe7ff;
  --card: #ffffff;
  --text: #1d1a33;
  --muted: #6b6788;
  --line: #e2ddf5;
  --primary: #6a4cf5;
  --primary-2: #8a72ff;
  --primary-ink: #ffffff;
  --accent: #ff8a1f;
  --ok: #16a05a;
  --ok-bg: #dcf6e8;
  --bad: #e0414a;
  --bad-bg: #fde3e4;
  --la: #2f6fed;
  --la-bg: #e3ecff;
  --de: #d9670b;
  --de-bg: #ffeedd;
  --gold: #f2b705;
  --shadow: 0 6px 18px rgba(45, 30, 120, .10);
  --radius: 16px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14122a;
    --bg-2: #1c1938;
    --card: #221f42;
    --text: #f1efff;
    --muted: #a9a4cc;
    --line: #34305c;
    --primary: #8a72ff;
    --primary-2: #a593ff;
    --ok: #3dd68c;
    --ok-bg: #173a2a;
    --bad: #ff6b72;
    --bad-bg: #44212a;
    --la: #79a6ff;
    --la-bg: #1f2d52;
    --de: #ffad5c;
    --de-bg: #4a2f17;
    --shadow: 0 6px 18px rgba(0, 0, 0, .35);
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 17px;
  line-height: 1.45;
  min-height: 100vh;
}
button, input, select { font: inherit; color: inherit; }
a { color: var(--primary); }
h1, h2, h3 { line-height: 1.2; margin: 0 0 .5em; }
h1 { font-size: 1.6rem; }
h2 { font-size: 1.25rem; }
.boot { padding: 40px 16px; text-align: center; color: var(--muted); }
.muted { color: var(--muted); }
.small { font-size: .88rem; }
.hidden { display: none !important; }

/* ---------- Layout ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.topbar-inner {
  max-width: 980px; margin: 0 auto; padding: 8px 16px;
  display: flex; align-items: center; gap: 12px;
}
.brand { font-weight: 800; font-size: 1.1rem; text-decoration: none; color: var(--text); white-space: nowrap; }
.brand span { color: var(--primary); }
.nav { display: flex; gap: 4px; margin-left: auto; overflow-x: auto; }
.nav a {
  text-decoration: none; color: var(--muted); font-weight: 600;
  padding: 8px 10px; border-radius: 10px; white-space: nowrap; font-size: .95rem;
}
.nav .ni { margin-right: 4px; }
.nav a.active { background: var(--card); color: var(--primary); box-shadow: var(--shadow); }
.icon-btn {
  border: 0; background: transparent; cursor: pointer; font-size: 1.2rem;
  width: 40px; height: 40px; border-radius: 10px; flex: none;
}
.icon-btn:hover { background: var(--bg-2); }
main { max-width: 980px; margin: 0 auto; padding: 20px 16px 48px; }

.card {
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 18px; margin-bottom: 16px; border: 1px solid var(--line);
}
.row { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
.grid { display: grid; gap: 14px; }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); }

/* ---------- Buttons & Formulare ---------- */
.btn {
  display: inline-block; text-align: center; text-decoration: none;
  border: 0; cursor: pointer; border-radius: 12px; padding: 11px 18px;
  font-weight: 700; background: var(--primary); color: var(--primary-ink);
  box-shadow: 0 3px 0 color-mix(in srgb, var(--primary) 60%, #000);
  transition: transform .06s, filter .15s;
}
.btn:hover { filter: brightness(1.07); }
.btn:active { transform: translateY(2px); box-shadow: 0 1px 0 color-mix(in srgb, var(--primary) 60%, #000); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.secondary { background: var(--bg-2); color: var(--text); box-shadow: 0 3px 0 var(--line); }
.btn.danger { background: var(--bad); box-shadow: 0 3px 0 color-mix(in srgb, var(--bad) 60%, #000); }
.btn.small { padding: 6px 12px; font-size: .9rem; border-radius: 10px; }
.btn.big { padding: 14px 24px; font-size: 1.1rem; }
label.field { display: block; margin-bottom: 12px; }
label.field span { display: block; font-weight: 600; font-size: .9rem; margin-bottom: 4px; color: var(--muted); }
input[type=text], input[type=password], input[type=number] {
  width: 100%; padding: 11px 12px; border-radius: 10px;
  border: 2px solid var(--line); background: var(--bg); outline: none;
}
input:focus { border-color: var(--primary); }
.check { display: flex; gap: 8px; align-items: center; margin-bottom: 12px; }
.error { color: var(--bad); font-weight: 600; min-height: 1.4em; margin: 6px 0; }
.ok-text { color: var(--ok); font-weight: 600; }

/* ---------- Login ---------- */
.auth { max-width: 400px; margin: 8vh auto 0; padding: 0 16px; }
.auth .logo { font-size: 3.2rem; text-align: center; }
.auth h1 { text-align: center; }

/* ---------- Start ---------- */
.hero { display: grid; grid-template-columns: auto 1fr; gap: 16px; align-items: center; }
.avatar {
  width: 64px; height: 64px; border-radius: 50%; display: grid; place-items: center;
  font-size: 1.8rem; font-weight: 800; color: #fff;
  background: linear-gradient(135deg, var(--primary), var(--accent));
}
.rank { font-weight: 800; font-size: 1.15rem; }
.rank small { color: var(--muted); font-weight: 600; }
.bar { height: 12px; background: var(--bg-2); border-radius: 99px; overflow: hidden; margin: 8px 0 4px; }
.bar > i { display: block; height: 100%; background: linear-gradient(90deg, var(--primary), var(--primary-2)); border-radius: 99px; transition: width .4s; }
.bar.ok > i { background: linear-gradient(90deg, var(--ok), #6fe0a6); }
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; margin-top: 14px; }
.stat { background: var(--bg); border-radius: 12px; padding: 10px; text-align: center; }
.stat b { display: block; font-size: 1.3rem; }
.stat span { font-size: .8rem; color: var(--muted); }
.games { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 14px; }
.game-card {
  text-align: left; border: 1px solid var(--line); cursor: pointer; padding: 18px;
  border-radius: var(--radius); background: var(--card); box-shadow: var(--shadow);
  transition: transform .12s; display: flex; flex-direction: column; gap: 6px;
}
.game-card:hover:not(:disabled) { transform: translateY(-3px); }
.game-card:disabled { opacity: .55; cursor: not-allowed; }
.game-card .gi { font-size: 2.2rem; }
.game-card b { font-size: 1.1rem; }
.game-card .best { margin-top: auto; font-size: .85rem; color: var(--muted); }

/* ---------- Lektionen ---------- */
.lessons { display: grid; grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); gap: 8px; }
.lesson {
  border: 2px solid var(--line); background: var(--card); border-radius: 12px; cursor: pointer;
  padding: 8px 6px 6px; text-align: center; position: relative;
}
.lesson b { display: block; font-size: 1.1rem; }
.lesson span { font-size: .75rem; color: var(--muted); }
.lesson .bar { height: 6px; margin: 4px 0 0; }
.lesson.on { border-color: var(--primary); background: color-mix(in srgb, var(--primary) 12%, var(--card)); }
.lesson.on::after { content: "✓"; position: absolute; top: 2px; right: 6px; color: var(--primary); font-weight: 800; font-size: .85rem; }
.range { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.range input { width: 80px; }
.wordlist { width: 100%; border-collapse: collapse; font-size: .95rem; }
.wordlist td { padding: 6px 4px; border-bottom: 1px solid var(--line); vertical-align: top; }
.wordlist td:first-child { color: var(--la); font-weight: 600; }
.dots { white-space: nowrap; letter-spacing: 1px; color: var(--line); }
.dots i { font-style: normal; }
.dots i.f { color: var(--ok); }

/* ---------- Rangliste ---------- */
.tabs { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 12px; }
.tabs button {
  border: 2px solid var(--line); background: var(--card); border-radius: 99px;
  padding: 6px 14px; cursor: pointer; font-weight: 600;
}
.tabs button.active { border-color: var(--primary); color: var(--primary); }
table.board { width: 100%; border-collapse: collapse; }
table.board th { text-align: left; font-size: .8rem; color: var(--muted); font-weight: 600; padding: 6px; }
table.board td { padding: 10px 6px; border-top: 1px solid var(--line); }
table.board td.num, table.board th.num { text-align: right; font-variant-numeric: tabular-nums; }
table.board tr.me td { background: color-mix(in srgb, var(--primary) 10%, transparent); font-weight: 700; }
.medal { font-size: 1.2rem; }

/* ---------- Admin ---------- */
.users { width: 100%; border-collapse: collapse; }
.users td { padding: 8px 4px; border-top: 1px solid var(--line); vertical-align: middle; }
.users + form { margin-top: 18px; }
.users .actions { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.badge { display: inline-block; font-size: .72rem; font-weight: 700; padding: 2px 8px; border-radius: 99px; background: var(--bg-2); color: var(--primary); }

/* ---------- Spiele allgemein ---------- */
.game { max-width: 720px; margin: 0 auto; }
.game-title { display: flex; align-items: center; gap: 12px; margin-bottom: 12px; }
.game-title h1 { margin: 0; flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hud {
  display: flex; gap: 10px; align-items: center; flex-wrap: wrap;
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 10px 14px; box-shadow: var(--shadow); margin-bottom: 14px; font-weight: 700;
}
.hud .score { font-size: 1.3rem; color: var(--primary); font-variant-numeric: tabular-nums; }
.hud .pill { background: var(--bg); padding: 4px 10px; border-radius: 99px; font-size: .95rem; font-variant-numeric: tabular-nums; }
.hud .pill.hot { background: color-mix(in srgb, var(--accent) 22%, var(--bg)); }
.timebar { height: 10px; background: var(--bg-2); border-radius: 99px; overflow: hidden; margin-bottom: 14px; }
.timebar > i { display: block; height: 100%; background: var(--ok); border-radius: 99px; }
.timebar > i.warn { background: var(--gold); }
.timebar > i.crit { background: var(--bad); }
.question {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow);
  text-align: center; padding: 22px 16px; margin-bottom: 14px;
}
.question .dir { font-size: .8rem; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }
.question .q { font-size: 1.6rem; font-weight: 800; margin-top: 6px; overflow-wrap: anywhere; }
.question .q.la { color: var(--la); }
.question .q.de { color: var(--de); }
.answers { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.answer {
  border: 2px solid var(--line); background: var(--card); border-radius: 14px; cursor: pointer;
  padding: 14px 12px; font-weight: 650; min-height: 64px; text-align: left;
  display: flex; gap: 10px; align-items: center; box-shadow: 0 3px 0 var(--line);
  overflow-wrap: anywhere; transition: transform .06s;
}
.answer:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--line); }
.answer kbd {
  flex: none; width: 26px; height: 26px; border-radius: 8px; display: grid; place-items: center;
  background: var(--bg-2); font-size: .8rem; color: var(--muted); font-family: var(--font);
}
.answer.right { border-color: var(--ok); background: var(--ok-bg); }
.answer.right kbd { background: var(--ok); color: #fff; }
.answer.wrong { border-color: var(--bad); background: var(--bad-bg); }
.answer.wrong kbd { background: var(--bad); color: #fff; }
.answer:disabled { cursor: default; }
.feedback { min-height: 1.6em; text-align: center; font-weight: 700; margin: 10px 0; }
.feedback .la { color: var(--la); }
.feedback .de { color: var(--de); }
.game-foot { display: flex; justify-content: center; margin-top: 18px; }

@keyframes pop { 0% { transform: scale(1); } 40% { transform: scale(1.12); } 100% { transform: scale(1); } }
@keyframes shake { 0%, 100% { transform: translateX(0); } 20%, 60% { transform: translateX(-8px); } 40%, 80% { transform: translateX(8px); } }
.pop { animation: pop .3s; }
.shake { animation: shake .35s; }
.float-pts {
  position: fixed; pointer-events: none; font-weight: 900; font-size: 1.4rem; color: var(--ok);
  animation: floatup .9s ease-out forwards; z-index: 50; text-shadow: 0 2px 0 var(--card);
}
@keyframes floatup { from { opacity: 1; transform: translate(-50%, 0); } to { opacity: 0; transform: translate(-50%, -60px); } }

/* Wort-Regen */
.sky {
  position: relative; height: min(46vh, 380px); border-radius: var(--radius); overflow: hidden; margin-bottom: 14px;
  background: linear-gradient(180deg, #2a2366 0%, #5b3fa8 60%, #b05ca8 100%);
  border: 1px solid var(--line);
}
.sky::after {
  content: ""; position: absolute; left: 0; right: 0; bottom: 0; height: 14px;
  background: repeating-linear-gradient(90deg, #ff8a1f 0 18px, #ffb35c 18px 36px);
}
.drop {
  position: absolute; left: 50%; top: 0; transform: translate(-50%, 0);
  background: #fff; color: #2140a8; font-weight: 800; font-size: 1.25rem;
  padding: 10px 16px; border-radius: 16px; box-shadow: 0 6px 20px rgba(0, 0, 0, .3);
  max-width: 90%; text-align: center; white-space: normal; overflow-wrap: anywhere;
}
.drop.burst { animation: burst .35s forwards; }
.drop.fail { background: var(--bad); color: #fff; }
@keyframes burst { to { transform: translate(-50%, var(--y)) scale(1.6); opacity: 0; } }
.lives { letter-spacing: 2px; }

/* Memory */
.memory { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
@media (max-width: 560px) { .memory { grid-template-columns: repeat(3, 1fr); } }
.mcard {
  aspect-ratio: 4 / 3.4; perspective: 800px; border: 0; padding: 0; background: transparent; cursor: pointer;
}
.mcard .in { position: relative; width: 100%; height: 100%; transition: transform .35s; transform-style: preserve-3d; }
.mcard.open .in, .mcard.done .in { transform: rotateY(180deg); }
.mcard .face {
  position: absolute; inset: 0; border-radius: 14px; display: grid; place-items: center; padding: 8px;
  backface-visibility: hidden; -webkit-backface-visibility: hidden; text-align: center; font-weight: 700;
  overflow-wrap: anywhere; overflow: hidden; font-size: .95rem; line-height: 1.25;
}
.mcard .back {
  background: linear-gradient(135deg, var(--primary), var(--primary-2)); color: #fff; font-size: 1.8rem;
  box-shadow: 0 3px 0 color-mix(in srgb, var(--primary) 60%, #000);
}
.mcard .front { transform: rotateY(180deg); border: 2px solid var(--line); }
.mcard .front.la { background: var(--la-bg); color: var(--la); }
.mcard .front.de { background: var(--de-bg); color: var(--de); }
.mcard.done .front { border-color: var(--ok); box-shadow: 0 0 0 3px var(--ok-bg); }
.mcard.miss .front { border-color: var(--bad); }

/* Buchstabensalat */
.slots, .tiles { --size: 44px; display: flex; flex-wrap: wrap; justify-content: center; gap: 6px; margin: 14px 0; }
/* Breite passt sich der Wortlänge an (--n), damit auch lange Wörter in eine Zeile passen. */
.slot, .tile {
  width: min(var(--size), calc((min(100vw - 32px, 720px) - (var(--n, 1) - 1) * 6px) / var(--n, 1)));
  height: 52px; border-radius: 10px; display: grid; place-items: center; padding: 0;
  font-weight: 800; font-size: 1.4rem; text-transform: uppercase;
}
.slot { border: 2px dashed var(--line); background: var(--card); cursor: pointer; }
.slot.filled { border-style: solid; border-color: var(--la); color: var(--la); }
.slot.hint { border-color: var(--gold); color: var(--gold); cursor: default; }
.slots.right .slot { border-color: var(--ok); color: var(--ok); background: var(--ok-bg); }
.tile {
  border: 0; cursor: pointer; background: var(--la); color: #fff;
  box-shadow: 0 3px 0 color-mix(in srgb, var(--la) 55%, #000);
}
.tile:disabled { visibility: hidden; }
.salat-actions { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }

/* Canvas-Spiele (Snake, Katapult) */
.question.compact { padding: 12px 14px; margin-bottom: 10px; }
.question.compact .q { font-size: 1.35rem; margin-top: 2px; }
.canvas-wrap { display: flex; justify-content: center; margin-bottom: 8px; }
.canvas-wrap canvas {
  display: block; border-radius: 14px; border: 1px solid var(--line); box-shadow: var(--shadow);
  touch-action: none; user-select: none; -webkit-user-select: none; cursor: crosshair;
}
.snake-canvas { cursor: default; }
.dpad {
  display: none; grid-template-columns: repeat(3, 64px); grid-template-rows: repeat(2, 54px);
  gap: 6px; justify-content: center; margin-top: 4px;
}
.dpad button {
  border: 0; border-radius: 14px; background: var(--card); color: var(--primary); font-size: 1.4rem;
  box-shadow: 0 3px 0 var(--line); border: 1px solid var(--line); cursor: pointer; touch-action: manipulation;
}
.dpad button:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--line); }
.dpad-up { grid-column: 2; grid-row: 1; }
.dpad-left { grid-column: 1; grid-row: 2; }
.dpad-down { grid-column: 2; grid-row: 2; }
.dpad-right { grid-column: 3; grid-row: 2; }
@media (pointer: coarse), (max-width: 640px) { .dpad { display: grid; } }

/* Tempelbau */
.tempel-form { display: flex; gap: 8px; margin-bottom: 4px; }
.tempel-form .tempel-input { flex: 1; font-size: 1.3rem; font-weight: 700; text-align: center; color: var(--la); }
.temple { display: flex; flex-direction: column; align-items: center; margin: 14px auto 6px; min-height: 40px; }
.temple .roof {
  width: 224px; height: 34px; background: linear-gradient(180deg, #f3e3c3, #d9bf8c);
  clip-path: polygon(50% 0, 100% 100%, 0 100%); animation: drop .35s ease-out;
}
.temple .floor {
  width: 200px; height: 30px; display: flex; justify-content: space-around; align-items: stretch;
  border-top: 5px solid #e2cfa6; padding: 0 6px;
}
.temple .floor i { display: block; width: 14px; background: linear-gradient(90deg, #efe2c4, #fffaf0 45%, #d8c49a); border-radius: 2px; }
.temple .floor.new { animation: drop .35s ease-out; }
.temple .steps { width: 236px; height: 14px; background: #cdb68a; border-radius: 3px; box-shadow: 0 6px 0 -2px #b89e70; }
@keyframes drop { from { transform: translateY(-30px); opacity: 0; } to { transform: none; opacity: 1; } }

/* Ergebnis */
.result { text-align: center; padding: 28px 18px; }
.result .points { font-size: 3rem; font-weight: 900; color: var(--primary); line-height: 1; margin: 8px 0; }
.result .record { color: var(--gold); font-weight: 800; font-size: 1.2rem; }
.result .levelup { color: var(--ok); font-weight: 800; }
.result .row { justify-content: center; margin-top: 18px; }
.result-words { text-align: left; margin-top: 16px; }

.confetti { position: fixed; inset: 0; pointer-events: none; overflow: hidden; z-index: 60; }
.confetti i {
  position: absolute; top: -12px; width: 10px; height: 14px; border-radius: 2px;
  animation: fall linear forwards;
}
@keyframes fall { to { transform: translateY(105vh) rotate(720deg); } }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 70;
  background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 12px; font-weight: 600;
  box-shadow: var(--shadow); max-width: calc(100% - 32px);
}

@media (max-width: 640px) {
  /* Navigation als Leiste am unteren Rand, damit alle vier Punkte sichtbar sind. */
  /* backdrop-filter würde die feste Leiste an die Kopfzeile binden statt an den Bildschirm. */
  .topbar { backdrop-filter: none; background: var(--bg); }
  .topbar-inner { justify-content: space-between; }
  .nav {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 30; margin: 0; gap: 0;
    background: var(--card); border-top: 1px solid var(--line);
    padding: 4px 4px calc(4px + env(safe-area-inset-bottom));
    box-shadow: 0 -4px 14px rgba(0, 0, 0, .08); overflow: visible;
  }
  .nav a { flex: 1; display: flex; flex-direction: column; align-items: center; padding: 6px 2px; font-size: .75rem; }
  .nav a.active { box-shadow: none; background: var(--bg-2); }
  .nav .ni { margin: 0; font-size: 1.3rem; line-height: 1.2; }
  main { padding-bottom: 96px; }
  .toast { bottom: 84px; }
  /* Im Spiel zählt jeder Pixel; zurück geht es über „Beenden“. */
  body.in-game .nav { display: none; }
  body.in-game main { padding-top: 12px; padding-bottom: 24px; }
  body.in-game .toast { bottom: 24px; }
}
@media (max-width: 560px) {
  body { font-size: 16px; }
  h1 { font-size: 1.35rem; }
  .stats { grid-template-columns: repeat(2, 1fr); }
  .answers { grid-template-columns: 1fr; }
  .answer { min-height: 54px; padding: 10px 12px; }
  .question .q { font-size: 1.35rem; }
  .slots, .tiles { --size: 38px; }
  .slot, .tile { height: 46px; font-size: 1.2rem; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
