:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --surface-2: #f0f2f8;
  --text: #1a1d29;
  --muted: #5f6577;
  --border: #dfe3ee;
  --accent: #4f46e5;
  --accent-soft: #e8e7fd;
  --again: #d6334a;
  --hard: #c97a06;
  --good: #1f8a4c;
  --easy: #2563eb;
  --checker: #7c3aed;
  --transfer: #0e7490;
  --aufgabe: #b45309;
  --basic: #4b5563;
  --shadow: 0 1px 2px rgba(20, 24, 40, .06), 0 4px 16px rgba(20, 24, 40, .06);
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11131a;
    --surface: #1a1d27;
    --surface-2: #232734;
    --text: #e8eaf2;
    --muted: #9aa1b5;
    --border: #2e3343;
    --accent: #8b85ff;
    --accent-soft: #2a2950;
    --again: #f0607a;
    --hard: #f0a43a;
    --good: #46c27c;
    --easy: #6aa0ff;
    --checker: #b392ff;
    --transfer: #38c3dc;
    --aufgabe: #f0a43a;
    --basic: #a3aab9;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px rgba(0, 0, 0, .25);
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font: 16px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  padding-bottom: env(safe-area-inset-bottom);
}
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }

.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 12px;
  padding: calc(8px + env(safe-area-inset-top)) 16px 8px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; background: none; border: 0; font-weight: 700; padding: 0; }
.topbar nav { display: flex; gap: 4px; margin-left: auto; }
.topbar nav button { background: none; border: 0; padding: 6px 10px; border-radius: 8px; color: var(--muted); }
.topbar nav button.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.sync { font-size: 12px; color: var(--hard); }
@media (max-width: 560px) { .brand span { display: none; } .topbar nav button { padding: 6px 8px; font-size: 15px; } }

main { max-width: 860px; margin: 0 auto; padding: 16px; }
h1 { font-size: 24px; margin: 8px 0 16px; }
h2 { font-size: 18px; margin: 24px 0 10px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

.panel {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px;
}
.grid { display: grid; gap: 12px; }
.stats { grid-template-columns: repeat(4, minmax(0, 1fr)); }
@media (max-width: 640px) { .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
.stat .value { font-size: 26px; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat .label { font-size: 13px; color: var(--muted); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  border: 1px solid var(--border); background: var(--surface); padding: 10px 16px;
  border-radius: 10px; font-weight: 600; min-height: 44px;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.ghost { background: transparent; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; cursor: default; }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.spacer { flex: 1; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  border: 1px solid var(--border); background: var(--surface); border-radius: 999px;
  padding: 6px 12px; font-size: 14px; min-height: 36px;
}
.chip[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); font-weight: 600; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.seg button { border: 0; background: var(--surface); padding: 8px 12px; font-size: 14px; }
.seg button[aria-pressed="true"] { background: var(--accent); color: #fff; }

table.decks { width: 100%; border-collapse: collapse; font-size: 14px; }
table.decks th, table.decks td { padding: 8px 6px; text-align: right; border-bottom: 1px solid var(--border); font-variant-numeric: tabular-nums; }
table.decks th:first-child, table.decks td:first-child { text-align: left; }
table.decks .due { color: var(--accent); font-weight: 700; }
.bar { height: 8px; border-radius: 4px; background: var(--surface-2); overflow: hidden; display: flex; }
.bar span { display: block; height: 100%; }

.forecast { display: flex; align-items: flex-end; gap: 4px; height: 90px; }
.forecast div { flex: 1; background: var(--accent); border-radius: 3px 3px 0 0; min-height: 2px; position: relative; }
.forecast-labels { display: flex; gap: 4px; font-size: 11px; color: var(--muted); }
.forecast-labels span { flex: 1; text-align: center; }

/* Lernen */
.study-head { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; font-size: 14px; color: var(--muted); }
.progress { flex: 1; height: 6px; background: var(--surface-2); border-radius: 3px; overflow: hidden; }
.progress span { display: block; height: 100%; background: var(--accent); }
.card { padding: 22px; min-height: 220px; }
.card .meta { display: flex; gap: 8px; align-items: center; margin-bottom: 12px; font-size: 12px; color: var(--muted); flex-wrap: wrap; }
.tag { border-radius: 6px; padding: 2px 8px; font-weight: 700; font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: #fff; }
.tag.basic { background: var(--basic); }
.tag.checker { background: var(--checker); }
.tag.transfer { background: var(--transfer); }
.tag.aufgabe { background: var(--aufgabe); }
.divider { border: 0; border-top: 1px dashed var(--border); margin: 18px 0; }
.md { overflow-wrap: anywhere; }
.md p { margin: .5em 0; }
.md img { max-width: 100%; height: auto; background: #fff; border-radius: 8px; padding: 6px; }
.md table { border-collapse: collapse; margin: .6em 0; font-size: 14px; display: block; overflow-x: auto; max-width: 100%; }
.md th, .md td { border: 1px solid var(--border); padding: 4px 8px; }
.md blockquote { margin: .6em 0; padding: 8px 12px; border-left: 4px solid var(--accent); background: var(--surface-2); border-radius: 0 8px 8px 0; }
.md code { background: var(--surface-2); padding: 1px 5px; border-radius: 4px; font-size: .92em; }
.md pre { background: var(--surface-2); padding: 10px; border-radius: 8px; overflow-x: auto; }
.md .katex-display { overflow-x: auto; overflow-y: hidden; padding: 4px 0; }
.md mark { background: #fde68a; color: #1a1d29; border-radius: 3px; }
.front-q { font-size: 18px; }
.answer-box { width: 100%; min-height: 110px; border: 1px solid var(--border); border-radius: 10px; padding: 10px; background: var(--surface-2); resize: vertical; }
.my-answer { white-space: pre-wrap; background: var(--surface-2); border-radius: 10px; padding: 10px; font-size: 14px; }

.ratings { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-top: 16px; }
.ratings button { border: 0; border-radius: 12px; padding: 10px 4px; color: #fff; font-weight: 700; min-height: 56px; }
.ratings small { display: block; font-weight: 500; opacity: .9; font-size: 12px; }
.r1 { background: var(--again); } .r2 { background: var(--hard); } .r3 { background: var(--good); } .r4 { background: var(--easy); }
.toolbar { display: flex; gap: 6px; margin-top: 12px; flex-wrap: wrap; }
.toolbar button { background: none; border: 1px solid var(--border); border-radius: 8px; padding: 6px 10px; font-size: 13px; color: var(--muted); min-height: 36px; }
.toolbar button.on { color: var(--hard); border-color: var(--hard); }
.kbd { font-size: 11px; color: var(--muted); }
@media (hover: none) { .kbd { display: none; } }
.sticky-actions { position: sticky; bottom: 0; padding: 12px 0 calc(12px + env(safe-area-inset-bottom)); background: linear-gradient(transparent, var(--bg) 30%); }

/* Browse */
.search { width: 100%; padding: 10px 12px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface); }
.list { display: grid; gap: 8px; margin-top: 12px; }
.item { text-align: left; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; }
.item .q { font-weight: 600; }
.item .a { color: var(--muted); font-size: 14px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }

/* Modal */
.modal { position: fixed; inset: 0; background: rgba(10, 12, 20, .55); display: flex; align-items: flex-start; justify-content: center; z-index: 50; padding: 16px; overflow-y: auto; }
.modal .panel { width: 100%; max-width: 860px; margin-top: env(safe-area-inset-top); }
.modal textarea { width: 100%; min-height: 110px; border: 1px solid var(--border); border-radius: 10px; padding: 10px; background: var(--surface-2); font-family: ui-monospace, Menlo, monospace; font-size: 14px; }
.edit-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
@media (max-width: 700px) { .edit-grid { grid-template-columns: 1fr; } }
label { font-size: 13px; color: var(--muted); font-weight: 600; display: block; margin: 8px 0 4px; }

.toast { position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom)); transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; z-index: 60; font-size: 14px; max-width: 90vw; }

/* Login */
.login { max-width: 380px; margin: 12vh auto; text-align: center; }
.login input { width: 100%; padding: 12px; border: 1px solid var(--border); border-radius: 10px; margin: 12px 0; background: var(--surface); }

/* Exam */
.timer { font-variant-numeric: tabular-nums; font-weight: 700; font-size: 20px; }
.timer.low { color: var(--again); }
.exam-item { margin-bottom: 14px; }
.score-btns { display: flex; gap: 8px; margin-top: 10px; }
.score-btns button { flex: 1; border: 1px solid var(--border); background: var(--surface); border-radius: 10px; padding: 8px; min-height: 44px; font-weight: 600; }
.score-btns button[aria-pressed="true"] { background: var(--accent); color: #fff; border-color: var(--accent); }
.grade { font-size: 48px; font-weight: 800; }
