:root {
  --bg: #f2f2f7;
  --card: #ffffff;
  --card-2: #f7f7fa;
  --text: #1c1c1e;
  --text-2: #6c6c70;
  --text-3: #aeaeb2;
  --line: rgba(60, 60, 67, .14);
  --accent: #0a7cff;
  --accent-text: #ffffff;
  --danger: #e5484d;
  --ok: #30a46c;
  --warn: #d97706;
  --bar: rgba(242, 242, 247, .82);
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.06);
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --tabbar-h: 64px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --card: #1c1c1e;
    --card-2: #242426;
    --text: #f2f2f7;
    --text-2: #9a9aa0;
    --text-3: #5a5a60;
    --line: rgba(84, 84, 88, .5);
    --accent: #3b95ff;
    --bar: rgba(18, 18, 20, .82);
    --shadow: none;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  min-height: 100dvh;
  overscroll-behavior-y: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent); text-decoration: none; }
[hidden] { display: none !important; }
svg { width: 1em; height: 1em; fill: currentColor; flex: none; }

/* ---------- Layout ---------- */
#view {
  max-width: 820px;
  margin: 0 auto;
  padding: calc(var(--safe-top) + 12px) 16px calc(var(--tabbar-h) + var(--safe-bottom) + 28px);
}
.page-head { display: flex; align-items: center; gap: 10px; margin: 6px 0 14px; min-height: 44px; }
.page-head h1 { font-size: 30px; line-height: 1.1; margin: 0; flex: 1; letter-spacing: -.02em; overflow-wrap: anywhere; }
.page-head.small h1 { font-size: 22px; }
.back {
  display: inline-flex; align-items: center; gap: 2px; border: 0; background: none; color: var(--accent);
  font-size: 17px; padding: 8px 8px 8px 0; margin-left: -4px;
}
.back svg { font-size: 22px; }

.section-title {
  font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em;
  color: var(--text-2); margin: 26px 4px 8px;
  display: flex; justify-content: space-between; align-items: baseline;
}
.section-title a, .section-title button { text-transform: none; letter-spacing: 0; font-weight: 500; font-size: 15px; background: none; border: 0; color: var(--accent); padding: 0; }

.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); }
.muted { color: var(--text-2); }
.small { font-size: 13px; }
.empty { text-align: center; color: var(--text-2); padding: 36px 16px; }
.empty svg { font-size: 44px; color: var(--text-3); display: block; margin: 0 auto 10px; }

/* ---------- Tabbar ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: grid; grid-template-columns: 1fr 1fr auto 1fr 1fr; align-items: center;
  height: calc(var(--tabbar-h) + var(--safe-bottom));
  padding: 0 6px var(--safe-bottom);
  background: var(--bar); backdrop-filter: saturate(180%) blur(20px); -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-top: .5px solid var(--line);
}
.tab { display: flex; flex-direction: column; align-items: center; gap: 2px; color: var(--text-3); font-size: 10.5px; font-weight: 500; padding: 6px 2px; min-width: 0; }
.tab span { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tab svg { font-size: 26px; }
.tab.active { color: var(--accent); }
.scan-fab {
  width: 60px; height: 60px; border-radius: 50%; border: 0; margin: -26px 6px 0;
  background: var(--accent); color: var(--accent-text);
  display: grid; place-items: center; box-shadow: 0 6px 20px rgba(10,124,255,.35);
  transition: transform .15s;
}
.scan-fab:active { transform: scale(.93); }
.scan-fab svg { font-size: 30px; }

/* ---------- Buttons & Inputs ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  border: 0; border-radius: 12px; padding: 12px 18px; font-weight: 600; font-size: 16px;
  background: var(--card-2); color: var(--text); min-height: 46px;
}
.btn.primary { background: var(--accent); color: var(--accent-text); }
.btn.danger { color: var(--danger); }
.btn.block { width: 100%; }
.btn.small { min-height: 34px; padding: 6px 12px; font-size: 14px; border-radius: 10px; }
.btn:disabled { opacity: .45; }
.btn svg { font-size: 20px; }
.btn-row { display: flex; gap: 10px; flex-wrap: wrap; }
.btn-row > .btn { flex: 1; }

.search {
  display: flex; align-items: center; gap: 8px; background: var(--card); border-radius: 12px;
  padding: 0 12px; height: 42px; box-shadow: var(--shadow);
}
.search svg { color: var(--text-3); font-size: 18px; }
.search input { border: 0; outline: 0; background: none; flex: 1; font-size: 17px; min-width: 0; }

.field { display: block; margin-bottom: 14px; }
.field > span { display: block; font-size: 13px; color: var(--text-2); margin: 0 4px 6px; }
.input, textarea.input, select.input {
  width: 100%; border: 1px solid var(--line); background: var(--card); border-radius: 12px;
  padding: 11px 12px; font-size: 16px; outline: none;
}
.input:focus { border-color: var(--accent); }
textarea.input { resize: vertical; min-height: 80px; }

/* ---------- Listen ---------- */
.list { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.row {
  display: flex; align-items: center; gap: 12px; padding: 11px 14px; min-height: 52px;
  color: var(--text); width: 100%; background: none; border: 0; text-align: left;
}
.row + .row { border-top: .5px solid var(--line); }
.row .grow { flex: 1; min-width: 0; }
.row .title { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .sub { font-size: 13px; color: var(--text-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .chev { color: var(--text-3); font-size: 18px; }
.row .val { color: var(--text-2); font-size: 15px; }
.thumb {
  width: 44px; height: 58px; border-radius: 6px; background: var(--card-2) center/cover no-repeat;
  border: .5px solid var(--line); flex: none; position: relative;
}
.thumb .pending {
  position: absolute; right: -4px; bottom: -4px; width: 16px; height: 16px; border-radius: 50%;
  background: var(--warn); border: 2px solid var(--card);
}
.dot { width: 12px; height: 12px; border-radius: 50%; flex: none; }
.badge {
  width: 34px; height: 34px; border-radius: 9px; display: grid; place-items: center;
  font-weight: 700; font-size: 13px; color: #fff; flex: none; letter-spacing: -.02em;
}
.snippet { font-size: 13px; color: var(--text-2); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; white-space: normal; }
mark { background: rgba(255, 200, 0, .45); color: inherit; border-radius: 3px; padding: 0 1px; }

.course-select {
  max-width: 44%; flex: none; border: 1px solid var(--line); background: var(--card-2); color: var(--text);
  border-radius: 10px; padding: 7px 8px; font-size: 14px;
}

/* ---------- Fächer-Raster ---------- */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.tile {
  display: flex; flex-direction: column; gap: 10px; padding: 12px; border-radius: var(--radius);
  background: var(--card); box-shadow: var(--shadow); color: var(--text); min-height: 96px;
  position: relative; overflow: hidden;
}
.tile::after { content: ""; position: absolute; inset: auto 0 0 0; height: 3px; background: var(--c); opacity: .9; }
.tile .name { font-weight: 600; font-size: 16px; line-height: 1.2; overflow-wrap: anywhere; }
.tile .count { color: var(--text-2); font-size: 13px; margin-top: auto; }

/* ---------- Heute-Karte ---------- */
.today { padding: 10px 14px 12px; display: grid; gap: 4px; }
.lessons { display: grid; }
.lesson {
  display: grid; grid-template-columns: 42px 10px minmax(0, 1fr) auto auto; column-gap: 10px; align-items: center;
  padding: 8px 0; border-top: .5px solid var(--line); font-size: 15px;
}
.lesson:first-child { border-top: 0; }
.lesson .time { color: var(--text-2); font-size: 14px; font-variant-numeric: tabular-nums; }
.lesson .dot { width: 10px; height: 10px; }
.lesson .name { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lesson .meta { color: var(--text-2); font-size: 13px; white-space: nowrap; }
.lesson .info { grid-column: 3 / -1; font-size: 12px; color: var(--text-2); margin-top: 2px; }
.lesson.done { opacity: .45; }
.lesson.cancelled .name, .lesson.cancelled .meta { text-decoration: line-through; opacity: .6; }
.lesson.is-now { background: color-mix(in srgb, var(--accent) 10%, transparent); border-radius: 10px; margin: 0 -8px; padding: 8px; border-top-color: transparent; }
.lesson.is-now + .lesson { border-top-color: transparent; }
.tag { font-size: 11px; font-weight: 600; padding: 2px 7px; border-radius: 999px; white-space: nowrap; }
.tag.cancel { background: color-mix(in srgb, var(--danger) 16%, transparent); color: var(--danger); }
.tag.change { background: color-mix(in srgb, var(--warn) 18%, transparent); color: var(--warn); }
.tag.now { background: var(--accent); color: var(--accent-text); }
.subhead {
  font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; color: var(--text-2);
  margin-top: 8px; padding-top: 10px; border-top: .5px solid var(--line);
}
.todo { display: grid; grid-template-columns: minmax(0, 1fr) auto; column-gap: 8px; padding: 4px 0; font-size: 15px; }
.todo b { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.todo .when { color: var(--text-2); font-size: 13px; white-space: nowrap; }
.todo .what { grid-column: 1 / -1; color: var(--text-2); font-size: 13px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; padding: 7px 12px; border-radius: 999px;
  border: 1.5px solid var(--line); background: var(--card); font-size: 15px; font-weight: 500;
}
.chip .dot { width: 9px; height: 9px; }
.chip.selected { border-color: var(--c, var(--accent)); background: color-mix(in srgb, var(--c, var(--accent)) 16%, var(--card)); }

.status-pill {
  display: inline-flex; align-items: center; gap: 6px; font-size: 13px; font-weight: 500;
  padding: 5px 10px; border-radius: 999px; background: var(--card); color: var(--text-2); box-shadow: var(--shadow);
  border: 0;
}
.status-pill .dot { width: 8px; height: 8px; }

/* ---------- Dokument-Ansicht ---------- */
.pages { display: grid; gap: 12px; }
.pages canvas, .pages img { width: 100%; height: auto; border-radius: 8px; box-shadow: var(--shadow); background: #fff; display: block; }
.meta-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
details.ocr { margin-top: 16px; }
details.ocr summary { cursor: pointer; color: var(--accent); font-weight: 500; padding: 8px 4px; }
details.ocr pre { white-space: pre-wrap; font: 14px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; background: var(--card); padding: 14px; border-radius: 12px; margin: 0; overflow-wrap: anywhere; }

/* ---------- Scan-Sheet ---------- */
.sheet {
  position: fixed; inset: 0; z-index: 50; background: var(--bg);
  display: flex; flex-direction: column;
  padding-top: var(--safe-top);
  animation: slide-up .22s ease-out;
}
@keyframes slide-up { from { transform: translateY(24px); opacity: 0; } to { transform: none; opacity: 1; } }
.sheet-head {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 8px 12px; min-height: 52px;
}
.sheet-head h2 { font-size: 17px; margin: 0; font-weight: 600; text-align: center; flex: 1; }
.sheet-head .link { border: 0; background: none; color: var(--accent); font-size: 17px; padding: 8px; min-width: 80px; text-align: left; }
.sheet-head .link.right { text-align: right; font-weight: 600; }
.sheet-body { flex: 1; overflow-y: auto; padding: 8px 16px 24px; -webkit-overflow-scrolling: touch; }
.sheet-foot { padding: 12px 16px calc(12px + var(--safe-bottom)); border-top: .5px solid var(--line); background: var(--bar); backdrop-filter: blur(20px); -webkit-backdrop-filter: blur(20px); }

.capture-start { display: grid; gap: 12px; margin-top: 12px; }
.big-option {
  display: flex; align-items: center; gap: 14px; padding: 18px; border-radius: var(--radius);
  background: var(--card); border: 0; box-shadow: var(--shadow); text-align: left; width: 100%;
}
.big-option svg { font-size: 30px; color: var(--accent); }
.big-option b { display: block; font-size: 17px; }
.big-option span { color: var(--text-2); font-size: 14px; }

.page-strip { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 12px; }
.page-card { position: relative; }
.page-card img { width: 100%; aspect-ratio: 1 / 1.414; object-fit: contain; border-radius: 8px; box-shadow: var(--shadow); background: var(--card-2); display: block; }
.page-card .num { position: absolute; left: 6px; bottom: 6px; background: rgba(0,0,0,.6); color: #fff; font-size: 12px; border-radius: 6px; padding: 1px 6px; }
.page-card .rm {
  position: absolute; right: -6px; top: -6px; width: 26px; height: 26px; border-radius: 50%; border: 0;
  background: var(--danger); color: #fff; display: grid; place-items: center; font-size: 16px; line-height: 1;
}
.page-card .edit { position: absolute; inset: 0; border: 0; background: none; }
.add-page {
  aspect-ratio: 1 / 1.414; border-radius: 8px; border: 2px dashed var(--line); background: none;
  display: grid; place-items: center; color: var(--accent); font-size: 14px; font-weight: 500; text-align: center; padding: 6px;
}
.add-page svg { font-size: 26px; display: block; margin: 0 auto 4px; }

/* Zuschneiden */
.crop-wrap { position: relative; margin: 0 auto; touch-action: none; user-select: none; -webkit-user-select: none; }
.crop-wrap canvas { display: block; width: 100%; height: 100%; border-radius: 6px; }
.crop-wrap svg.quad { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; pointer-events: none; }
.crop-wrap svg.quad polygon { fill: rgba(10,124,255,.16); stroke: var(--accent); stroke-width: 2; vector-effect: non-scaling-stroke; }
.handle {
  position: absolute; width: 44px; height: 44px; margin: -22px 0 0 -22px; border-radius: 50%;
  display: grid; place-items: center; touch-action: none; cursor: grab;
}
.handle::before { content: ""; width: 22px; height: 22px; border-radius: 50%; background: #fff; border: 3px solid var(--accent); box-shadow: 0 1px 6px rgba(0,0,0,.35); }
.loupe {
  position: absolute; width: 110px; height: 110px; border-radius: 50%; border: 3px solid #fff;
  box-shadow: 0 4px 16px rgba(0,0,0,.4); pointer-events: none; overflow: hidden; background: #000;
}
.loupe canvas { width: 100%; height: 100%; }
.crop-tools { display: flex; justify-content: center; gap: 10px; margin-top: 14px; flex-wrap: wrap; }

/* Prüfen */
.review-pages { display: flex; gap: 10px; overflow-x: auto; padding: 4px 2px 10px; scroll-snap-type: x mandatory; }
.review-pages img { height: 260px; width: auto; border-radius: 8px; box-shadow: var(--shadow); scroll-snap-align: start; background: #fff; }
.segmented { display: flex; background: var(--card-2); border-radius: 10px; padding: 3px; gap: 2px; }
.segmented button { flex: 1; border: 0; background: none; border-radius: 8px; padding: 7px 8px; font-size: 14px; font-weight: 500; }
.segmented button.on { background: var(--card); box-shadow: 0 1px 3px rgba(0,0,0,.12); }
.reason { font-size: 13px; color: var(--text-2); margin: 8px 4px 0; display: flex; gap: 6px; align-items: flex-start; }
.reason svg { font-size: 15px; margin-top: 1px; color: var(--accent); }

.progress { display: grid; place-items: center; gap: 16px; padding: 60px 20px; text-align: center; }
.spinner { width: 42px; height: 42px; border-radius: 50%; border: 4px solid var(--line); border-top-color: var(--accent); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.bar { width: min(280px, 80%); height: 6px; border-radius: 3px; background: var(--line); overflow: hidden; }
.bar > div { height: 100%; background: var(--accent); width: 0; transition: width .2s; }

/* Toast */
#toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 18px); transform: translateX(-50%);
  z-index: 100; background: rgba(28,28,30,.94); color: #fff; padding: 11px 16px; border-radius: 12px;
  font-size: 15px; max-width: min(92vw, 460px); box-shadow: 0 8px 24px rgba(0,0,0,.25);
  animation: slide-up .2s ease-out;
}

/* Hinweis-Kasten */
.note { padding: 14px; border-radius: var(--radius); background: color-mix(in srgb, var(--accent) 10%, var(--card)); font-size: 15px; }
.note.warn { background: color-mix(in srgb, var(--warn) 14%, var(--card)); }
.note b { display: block; margin-bottom: 4px; }
.note ol { margin: 6px 0 0; padding-left: 20px; }
.note code { font-size: 13px; background: var(--card-2); padding: 1px 5px; border-radius: 5px; overflow-wrap: anywhere; }

@media (min-width: 700px) {
  .review-pages img { height: 340px; }
}

/* ---------- KI: Auswertung, Themen, Lernen ---------- */
a.todo { color: inherit; }
.ki-card { padding: 14px 16px; margin-top: 12px; }
.ki-head { display: flex; align-items: center; gap: 6px; font-weight: 600; font-size: 15px; }
.ki-head svg { color: var(--accent); font-size: 18px; }
.ki-head .muted { margin-left: auto; font-weight: 400; }
.ki-topic { display: inline-flex; align-items: center; gap: 2px; font-size: 14px; font-weight: 500; margin-top: 8px; }
.ki-topic svg { font-size: 15px; }
.ki-summary { margin: 8px 0 10px; line-height: 1.45; }
.kw { display: flex; flex-wrap: wrap; gap: 6px; }
.kw span { font-size: 12.5px; padding: 3px 9px; border-radius: 999px; background: var(--card-2); color: var(--text-2); }
.ki-sug { display: flex; align-items: center; gap: 10px; margin-top: 12px; padding-top: 12px; border-top: .5px solid var(--line); }
.ki-sug .grow { min-width: 0; }
.ki-sug .grow span, .ki-sug .grow b { display: block; }
.ki-q { margin-top: 12px; border-top: .5px solid var(--line); }
.ki-q > summary { cursor: pointer; color: var(--accent); font-weight: 500; padding: 10px 0 4px; }
details.qa { padding: 8px 0; border-bottom: .5px solid var(--line); }
details.qa summary { cursor: pointer; font-weight: 500; list-style: none; }
details.qa summary::-webkit-details-marker { display: none; }
details.qa summary::after { content: ' ›'; color: var(--text-3); }
details.qa[open] summary::after { content: ''; }
details.qa p { margin: 6px 0 0; color: var(--text-2); }
.ki-foot { margin-top: 10px; }
.ki-hint { display: flex; align-items: center; gap: 12px; padding: 14px 16px; margin-top: 14px; color: var(--text); }
.ki-hint > svg { color: var(--accent); font-size: 22px; }
.ki-hint .grow { flex: 1; min-width: 0; }
.ki-hint b, .ki-hint span { display: block; }
.ki-hint .chev { color: var(--text-3); font-size: 18px; }
.sug-row .old { font-size: 13px; color: var(--text-2); text-decoration: line-through; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.section-subject { display: flex; align-items: center; gap: 8px; }
.section-subject .dot { width: 10px; height: 10px; }
.topic-row .snippet { margin-top: 2px; }
.quiz-progress { height: 4px; border-radius: 2px; background: var(--card-2); overflow: hidden; margin: 0 4px 14px; }
.quiz-progress span { display: block; height: 100%; background: var(--accent); transition: width .2s; }
.quiz-card { padding: 22px 18px; min-height: 190px; display: flex; flex-direction: column; gap: 12px; }
.quiz-q { font-size: 20px; font-weight: 600; line-height: 1.35; }
.quiz-a { font-size: 17px; line-height: 1.45; padding-top: 12px; border-top: .5px solid var(--line); }
