:root {
  --bg: #f3f4fa;
  --card: #ffffff;
  --text: #1b1d2a;
  --muted: #6b7084;
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --primary-text: #ffffff;
  --ok: #15803d;
  --ok-bg: #dcfce7;
  --ok-border: #86efac;
  --bad: #b91c1c;
  --bad-bg: #fee2e2;
  --bad-border: #fca5a5;
  --warn: #b45309;
  --warn-bg: #fef3c7;
  --border: #e2e4ee;
  --sel: #e0e7ff;
  --radius: 16px;
  --shadow: 0 2px 12px rgba(20, 20, 60, 0.08);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #11131f; --card: #1b1e30; --text: #eef0fa; --muted: #9ba1bb;
    --border: #2c3048; --sel: #2e2f6b; --ok-bg: #14532d; --ok-border: #166534; --ok: #86efac;
    --bad-bg: #7f1d1d; --bad-border: #991b1b; --bad: #fca5a5; --warn-bg: #78350f; --warn: #fcd34d;
    --shadow: 0 2px 14px rgba(0, 0, 0, 0.45);
  }
}
:root[data-theme="dark"] {
  --bg: #11131f; --card: #1b1e30; --text: #eef0fa; --muted: #9ba1bb;
  --border: #2c3048; --sel: #2e2f6b; --ok-bg: #14532d; --ok-border: #166534; --ok: #86efac;
  --bad-bg: #7f1d1d; --bad-border: #991b1b; --bad: #fca5a5; --warn-bg: #78350f; --warn: #fcd34d;
  --shadow: 0 2px 14px rgba(0, 0, 0, 0.45);
}
:root[data-font="large"] { font-size: 115%; }

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { height: 100%; }
body {
  margin: 0; min-height: 100%; background: var(--bg); color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 16px; line-height: 1.45; overscroll-behavior-y: contain;
  -webkit-font-smoothing: antialiased; touch-action: manipulation;
}
a { color: var(--primary); }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; user-select: none; -webkit-user-select: none; }
img { max-width: 100%; display: block; }

/* Kopfzeile */
.topbar {
  position: sticky; top: 0; z-index: 10; display: flex; align-items: center; gap: 6px;
  padding: calc(8px + var(--safe-top)) 10px 8px; background: var(--card);
  border-bottom: 1px solid var(--border); min-height: 56px;
}
.topbar h1 { flex: 1; margin: 0; font-size: 1.1rem; font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; text-align: center; }
.topbar .actions { display: flex; gap: 4px; min-width: 44px; justify-content: flex-end; }
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center; width: 44px; height: 44px;
  border: none; background: transparent; border-radius: 12px; font-size: 1.4rem; text-decoration: none; color: var(--text);
}
.icon-btn:active { background: var(--sel); }
.icon-btn.placeholder { visibility: hidden; }

/* Inhalt */
.view { padding: 16px 16px calc(24px + var(--safe-bottom)); max-width: 720px; margin: 0 auto; }
.section-title { margin: 22px 0 8px; font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.muted { color: var(--muted); }
.small { font-size: 0.9rem; }
.center { text-align: center; }
.row { display: flex; gap: 10px; flex-wrap: wrap; }
.row > * { flex: 1 1 auto; }
.stack { display: flex; flex-direction: column; gap: 10px; }

/* Listen */
.list { display: flex; flex-direction: column; gap: 10px; }
.item {
  display: flex; align-items: center; gap: 12px; padding: 14px 16px; background: var(--card);
  border-radius: var(--radius); box-shadow: var(--shadow); text-decoration: none; color: inherit; border: 1px solid transparent;
  text-align: left; width: 100%;
}
.item:active { border-color: var(--primary); }
.item .body { flex: 1; min-width: 0; }
.item .title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.item .sub { color: var(--muted); font-size: 0.85rem; margin-top: 2px; }
.item .chev { color: var(--muted); font-size: 1.3rem; }
.item .thumb { width: 44px; height: 44px; border-radius: 8px; object-fit: cover; flex: none; }

.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 0.75rem; font-weight: 600; background: var(--sel); color: var(--primary); }
.badge.ok { background: var(--ok-bg); color: var(--ok); }
.badge.bad { background: var(--bad-bg); color: var(--bad); }
.badge.warn { background: var(--warn-bg); color: var(--warn); }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; padding: 12px 18px; border-radius: 14px;
  border: 1px solid var(--border); background: var(--card); color: var(--text); font-weight: 600; min-height: 48px; text-decoration: none;
}
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn.primary:disabled { opacity: 0.45; }
.btn.danger { color: var(--bad); border-color: var(--bad-border); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--primary); }
.btn.big { width: 100%; font-size: 1.05rem; min-height: 54px; }
.btn.block { width: 100%; }
.btn.sm { min-height: 38px; padding: 6px 12px; font-size: 0.9rem; border-radius: 10px; }
.btn:disabled { cursor: default; opacity: 0.6; }

/* Formulare */
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field label { font-size: 0.85rem; font-weight: 600; color: var(--muted); }
input[type="text"], textarea, select {
  width: 100%; padding: 12px; border-radius: 12px; border: 1px solid var(--border); background: var(--card); min-height: 46px;
}
textarea { resize: vertical; min-height: 90px; }
textarea.code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 0.9rem; min-height: 260px; white-space: pre; }
.opt-row { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.opt-row input[type="text"] { flex: 1; }
.opt-row .check {
  width: 46px; height: 46px; border-radius: 12px; border: 1px solid var(--border); background: var(--card); font-size: 1.2rem; flex: none;
}
.opt-row .check.on { background: var(--ok-bg); border-color: var(--ok-border); color: var(--ok); }
.opt-row .del { width: 40px; height: 46px; border: none; background: transparent; color: var(--muted); font-size: 1.2rem; flex: none; }
.img-field { display: flex; align-items: center; gap: 10px; }
.img-field img { width: 80px; height: 80px; object-fit: cover; border-radius: 10px; border: 1px solid var(--border); }
.setting { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 0; border-bottom: 1px solid var(--border); }
.setting .lbl { flex: 1; }
.setting .lbl small { display: block; color: var(--muted); font-size: 0.8rem; }
.setting select, .setting input[type="number"] { width: auto; min-width: 90px; padding: 8px 10px; min-height: 40px; }
.switch { position: relative; width: 50px; height: 30px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch span { position: absolute; inset: 0; background: var(--border); border-radius: 30px; transition: 0.2s; }
.switch span::before { content: ""; position: absolute; width: 24px; height: 24px; left: 3px; top: 3px; background: #fff; border-radius: 50%; transition: 0.2s; box-shadow: 0 1px 3px rgba(0,0,0,.3); }
.switch input:checked + span { background: var(--primary); }
.switch input:checked + span::before { transform: translateX(20px); }

.errors { background: var(--bad-bg); border: 1px solid var(--bad-border); color: var(--bad); border-radius: 12px; padding: 10px 14px; margin: 10px 0; }
.errors ul { margin: 4px 0 0; padding-left: 18px; }
.note { background: var(--warn-bg); color: var(--warn); border-radius: 12px; padding: 10px 14px; margin: 10px 0; }
.panel { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; }
pre.fmt { background: var(--bg); border: 1px solid var(--border); border-radius: 12px; padding: 12px; overflow: auto; font-size: 0.82rem; line-height: 1.4; }

.empty { text-align: center; padding: 50px 16px; color: var(--muted); }
.empty .big-icon { font-size: 3rem; margin-bottom: 8px; }

/* Fortschritt */
.progress { height: 8px; background: var(--border); border-radius: 8px; overflow: hidden; margin: 4px 0 6px; }
.progress .bar { height: 100%; background: var(--primary); transition: width 0.3s; }
.progress-text { font-size: 0.85rem; color: var(--muted); text-align: center; margin-bottom: 12px; }

/* Lernkarte (eine Seite; nach „Prüfen" werden die Antworten eingefärbt) */
.face {
  background: var(--card); border-radius: 20px; box-shadow: var(--shadow); padding: 18px 16px 16px;
  display: flex; flex-direction: column; gap: 12px; min-height: 320px; border: 2px solid transparent;
}
.face.ok { border-color: var(--ok-border); }
.face.bad { border-color: var(--bad-border); }
.face-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.pill { font-size: 0.75rem; font-weight: 600; padding: 3px 10px; border-radius: 999px; background: var(--sel); color: var(--primary); }
.pill.muted { background: var(--bg); color: var(--muted); }
.q-text { font-size: 1.15rem; font-weight: 600; white-space: pre-wrap; }
.q-text.small { font-size: 0.95rem; font-weight: 500; color: var(--muted); }
.card-img { max-height: 42vh; width: auto; max-width: 100%; margin: 0 auto; border-radius: 12px; cursor: zoom-in; object-fit: contain; }
.verdict { font-size: 1.3rem; font-weight: 700; text-align: center; padding: 6px 0; border-radius: 12px; }
.face.ok .verdict { color: var(--ok); background: var(--ok-bg); }
.face.bad .verdict { color: var(--bad); background: var(--bad-bg); }
.answer-text { background: var(--bg); border-radius: 12px; padding: 12px; white-space: pre-wrap; }
.options { display: flex; flex-direction: column; gap: 8px; }
.opt {
  display: flex; align-items: flex-start; gap: 10px; text-align: left; width: 100%; padding: 12px 14px;
  border: 1.5px solid var(--border); border-radius: 14px; background: var(--card); min-height: 50px;
}
.opt .mark { flex: none; width: 24px; font-size: 1.15rem; line-height: 1.3; }
.opt .txt { flex: 1; white-space: pre-wrap; }
.opt.sel { background: var(--sel); border-color: var(--primary); }
.opt.is-correct { background: var(--ok-bg); border-color: var(--ok-border); }
.opt.is-correct .mark { color: var(--ok); }
.opt.is-wrong { background: var(--bad-bg); border-color: var(--bad-border); }
.opt.is-wrong .mark { color: var(--bad); }
.opt.is-neutral { opacity: 0.55; }
.opt.is-correct .you { color: var(--ok); font-weight: 600; }
.opt.is-wrong .you { color: var(--bad); font-weight: 600; }
.opt .you { font-size: 0.72rem; color: var(--muted); align-self: center; white-space: nowrap; }
.spacer { flex: 1; }

/* Ergebnis */
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin: 12px 0 18px; }
.stat { background: var(--card); border-radius: 14px; padding: 12px 8px; text-align: center; box-shadow: var(--shadow); }
.stat .num { font-size: 1.5rem; font-weight: 700; }
.stat .lbl { font-size: 0.75rem; color: var(--muted); }
.stat.ok .num { color: var(--ok); }
.stat.bad .num { color: var(--bad); }

/* Menü-Blatt, Toast, Bildansicht */
.sheet-bg { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 50; display: flex; align-items: flex-end; justify-content: center; }
.sheet { background: var(--card); width: 100%; max-width: 720px; border-radius: 20px 20px 0 0; padding: 14px 16px calc(16px + var(--safe-bottom)); display: flex; flex-direction: column; gap: 8px; }
.sheet h3 { margin: 0 0 6px; font-size: 1rem; }
.sheet .btn { justify-content: flex-start; }
.toast {
  position: fixed; left: 50%; bottom: calc(24px + var(--safe-bottom)); transform: translateX(-50%); background: #222; color: #fff;
  padding: 10px 16px; border-radius: 12px; font-size: 0.9rem; z-index: 60; display: flex; gap: 12px; align-items: center; max-width: 92vw; box-shadow: 0 4px 16px rgba(0,0,0,.3);
}
.toast button { background: transparent; border: none; color: #a5b4fc; font-weight: 700; }
.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.92); z-index: 70; display: flex; align-items: center; justify-content: center; padding: 16px; }
.lightbox img { max-width: 100%; max-height: 100%; object-fit: contain; border-radius: 8px; }
.fab-bar { position: sticky; bottom: calc(10px + var(--safe-bottom)); display: flex; gap: 10px; margin-top: 20px; }
.fab-bar .btn { flex: 1; box-shadow: var(--shadow); }
.error { color: var(--bad); }
kbd { background: var(--bg); border: 1px solid var(--border); border-radius: 6px; padding: 0 5px; font-family: ui-monospace, monospace; font-size: 0.85em; }

/* Drag & Drop in der Import-Ansicht */
.dropzone { position: relative; }
.dropzone .drop-hint { display: none; }
.dropzone.drag::after {
  content: ""; position: fixed; inset: 0; z-index: 40; pointer-events: none;
  border: 3px dashed var(--primary); background: rgba(79, 70, 229, 0.12);
}
.dropzone.drag .drop-hint {
  display: block; position: fixed; left: 50%; top: 50%; transform: translate(-50%, -50%); z-index: 41; pointer-events: none;
  background: var(--primary); color: #fff; padding: 12px 20px; border-radius: 14px; font-weight: 700;
}

/* Zuordnung */
.hint { font-size: 0.85rem; color: var(--muted); margin: 0; }
.match { display: flex; flex-direction: column; gap: 8px; }
.match-row { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.4fr); gap: 8px; align-items: stretch; }
.match-left { padding: 10px 12px; border-radius: 12px; background: var(--bg); font-weight: 600; display: flex; align-items: center; white-space: pre-wrap; }
.match-right { display: flex; align-items: center; gap: 6px; padding: 8px 8px 8px 12px; border: 1.5px solid var(--border); border-radius: 12px; background: var(--card); cursor: pointer; min-height: 50px; }
.match-right .txt { flex: 1; white-space: pre-wrap; }
.match-right .mark { flex: none; width: 20px; font-size: 1.1rem; }
.match-ctl { display: flex; flex-direction: column; gap: 2px; flex: none; }
.match-ctl button { width: 34px; height: 24px; border: 1px solid var(--border); background: var(--bg); border-radius: 7px; font-size: 0.7rem; color: var(--text); padding: 0; }
.match-ctl button:disabled { opacity: 0.3; }
.match-row.sel .match-right { background: var(--sel); border-color: var(--primary); }
.match-row.is-correct .match-right { background: var(--ok-bg); border-color: var(--ok-border); }
.match-row.is-correct .mark { color: var(--ok); }
.match-row.is-wrong .match-right { background: var(--bad-bg); border-color: var(--bad-border); }
.match-row.is-wrong .mark { color: var(--bad); }
.match-row .fix { grid-column: 1 / -1; }
.fix { font-size: 0.85rem; color: var(--ok); font-weight: 600; padding: 2px 4px 0; }
.pair-row { display: grid; grid-template-columns: 1fr auto 1.3fr auto; gap: 6px; align-items: center; margin-bottom: 8px; }
.pair-row .eq { color: var(--muted); font-weight: 700; }
.pair-row .del { width: 36px; height: 46px; border: none; background: transparent; color: var(--muted); font-size: 1.2rem; }

/* Lückentext */
.gap { display: inline-block; min-width: 4.5em; border-bottom: 2px solid var(--primary); padding: 0 6px; text-align: center; color: var(--primary); font-size: 0.85em; vertical-align: baseline; line-height: 1.3; }
.gap.ok { color: var(--ok); border-color: var(--ok); font-weight: 700; }
.gap.bad { color: var(--ok); border-color: var(--bad); font-weight: 700; }
.gap.bad s { color: var(--bad); font-weight: 500; margin-right: 4px; }
.gap-inputs { display: flex; flex-direction: column; gap: 10px; }
.gap-row label { display: block; font-size: 0.8rem; font-weight: 600; color: var(--muted); margin-bottom: 4px; }
.gap-row input { font-size: 1.05rem; }
.gap-row input.is-ok { background: var(--ok-bg); border-color: var(--ok-border); color: var(--ok); font-weight: 600; }
.gap-row input.is-bad { background: var(--bad-bg); border-color: var(--bad-border); color: var(--bad); text-decoration: line-through; }
.gap-no { width: 28px; text-align: center; font-weight: 700; color: var(--muted); flex: none; }

/* Reihenfolge */
.order-row { grid-template-columns: 1fr; }
.order-row .match-right { padding-left: 8px; }
.order-row .pos { flex: none; width: 26px; height: 26px; border-radius: 50%; background: var(--bg); color: var(--muted); font-size: 0.8rem; font-weight: 700; display: inline-flex; align-items: center; justify-content: center; }
.order-row.is-correct .pos { background: var(--ok); color: #fff; }
.order-row.is-wrong .pos { background: var(--bad); color: #fff; }

/* Animationen bei richtig / falsch */
canvas.fx { position: fixed; inset: 0; width: 100vw; height: 100vh; pointer-events: none; z-index: 80; }
@keyframes verdict-pop { 0% { transform: scale(0.5); opacity: 0; } 60% { transform: scale(1.12); opacity: 1; } 100% { transform: scale(1); } }
@keyframes card-ok { 0% { transform: scale(1); box-shadow: 0 0 0 0 rgba(34, 197, 94, 0.55); } 40% { transform: scale(1.025); box-shadow: 0 0 0 14px rgba(34, 197, 94, 0); } 100% { transform: scale(1); box-shadow: var(--shadow); } }
@keyframes card-bad { 0%, 100% { transform: translateX(0); } 12% { transform: translateX(-10px); } 30% { transform: translateX(9px); } 48% { transform: translateX(-7px); } 66% { transform: translateX(5px); } 84% { transform: translateX(-2px); } }
@keyframes flash-bad { 0% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.6); } 100% { box-shadow: 0 0 0 16px rgba(239, 68, 68, 0); } }
@keyframes row-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
.face.anim-ok { animation: card-ok 0.7s cubic-bezier(.3,1.4,.5,1); }
.face.anim-bad { animation: card-bad 0.55s ease, flash-bad 0.8s ease-out; }
.face.anim-ok .verdict, .face.anim-bad .verdict { animation: verdict-pop 0.45s cubic-bezier(.3,1.5,.5,1) both; }
.face.anim-ok .opt.is-correct, .face.anim-bad .opt.is-correct, .face.anim-bad .opt.is-wrong,
.face.anim-ok .match-row, .face.anim-bad .match-row, .face.anim-ok .answer-text, .face.anim-bad .answer-text { animation: row-in 0.35s ease both; }
.face.anim-ok .answer-text, .face.anim-bad .answer-text { animation-delay: 0.25s; }
@media (prefers-reduced-motion: reduce) { .face.anim-ok, .face.anim-bad, .face .verdict, .face .opt, .face .match-row, .face .answer-text { animation: none !important; } }

/* Karte gemeistert */
@keyframes card-master {
  0% { transform: scale(1); box-shadow: 0 0 0 0 rgba(251, 191, 36, 0.7); }
  35% { transform: scale(1.04); box-shadow: 0 0 0 18px rgba(251, 191, 36, 0), 0 0 40px 6px rgba(251, 191, 36, 0.55); }
  70% { transform: scale(0.995); box-shadow: 0 0 24px 2px rgba(251, 191, 36, 0.35); }
  100% { transform: scale(1); box-shadow: var(--shadow); }
}
@keyframes badge-in { 0% { transform: scale(0.3) rotate(-12deg); opacity: 0; } 55% { transform: scale(1.15) rotate(3deg); opacity: 1; } 75% { transform: scale(0.96) rotate(-1deg); } 100% { transform: scale(1) rotate(0); } }
@keyframes shimmer { 0% { background-position: -200% 0; } 100% { background-position: 200% 0; } }
@keyframes star-spin { 0% { transform: rotate(0) scale(1); } 50% { transform: rotate(180deg) scale(1.4); } 100% { transform: rotate(360deg) scale(1); } }
@keyframes bar-bump { 0% { filter: brightness(1); } 40% { filter: brightness(1.6); transform: scaleY(1.6); } 100% { filter: brightness(1); } }
.face.anim-master { animation: card-master 1.1s cubic-bezier(.3,1.3,.5,1); border-color: #fbbf24; }
.face.anim-master .verdict { animation: verdict-pop 0.45s cubic-bezier(.3,1.5,.5,1) both; }
.mastered-badge {
  align-self: center; display: inline-flex; align-items: center; gap: 8px; padding: 8px 18px; border-radius: 999px;
  font-weight: 800; font-size: 1.05rem; color: #78350f;
  background: linear-gradient(110deg, #fde68a 0%, #fbbf24 35%, #fff7cc 50%, #fbbf24 65%, #fde68a 100%);
  background-size: 200% 100%; box-shadow: 0 4px 14px rgba(251, 191, 36, 0.45);
}
.face.anim-master .mastered-badge { animation: badge-in 0.7s cubic-bezier(.3,1.5,.5,1) 0.15s both, shimmer 1.6s linear 0.8s 2; }
.face.anim-master .mastered-badge .star { display: inline-block; animation: star-spin 0.9s ease-in-out 0.4s 1; }
.face.anim-master .opt.is-correct, .face.anim-master .match-row, .face.anim-master .answer-text, .face.anim-master input.is-ok { animation: row-in 0.35s ease both; animation-delay: 0.3s; }
.progress .bar.bump { animation: bar-bump 0.8s ease; transform-origin: center; }
@media (prefers-reduced-motion: reduce) { .face.anim-master, .mastered-badge, .mastered-badge .star, .progress .bar.bump { animation: none !important; } }
