:root {
  --bg: #f4f4f1;
  --surface: #ffffff;
  --surface-2: #f7f7f5;
  --line: #e7e5e0;
  --ink: #1c1d21;
  --ink-2: #55585f;
  --ink-3: #8b8e95;
  --up: #ea580c;
  --down: #2563eb;
  --warn: #9a5b00;
  --warn-soft: #fff6e0;
  --radius: 16px;
  --shadow: 0 1px 2px rgba(20, 20, 25, .06), 0 8px 28px rgba(20, 20, 25, .10);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--bg); color: var(--ink); font: 15px/1.45 var(--font); }
button { font: inherit; color: inherit; }
[hidden] { display: none !important; }
.i { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }

#map { position: fixed; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; cursor: grab; }
#map.dragging { cursor: grabbing; }

/* --- Kopfleiste ------------------------------------------------------------ */
.top { position: fixed; top: calc(10px + var(--safe-t)); left: 10px; right: 10px; z-index: 10; max-width: 460px; pointer-events: none; }
.top > * { pointer-events: auto; }
.searchbar {
  display: flex; align-items: center; gap: 8px; height: 48px; padding: 0 6px 0 14px;
  background: var(--surface); border-radius: 999px; box-shadow: var(--shadow); color: var(--ink-3);
}
.searchbar:focus-within { outline: 2px solid var(--ink); }
#search { flex: 1; min-width: 0; border: 0; outline: 0; background: transparent; font: 16px var(--font); color: var(--ink); height: 100%; }
#search::-webkit-search-cancel-button { display: none; }
.icon-btn { width: 36px; height: 36px; border-radius: 50%; border: 0; background: var(--surface-2); font-weight: 700; cursor: pointer; color: var(--ink-2); }
.icon-btn:active { transform: scale(.94); }
.results {
  list-style: none; margin: 8px 0 0; padding: 6px; background: var(--surface); border-radius: var(--radius);
  box-shadow: var(--shadow); max-height: min(60vh, 440px); overflow-y: auto; overscroll-behavior: contain;
}
.results li { display: flex; align-items: center; gap: 10px; padding: 10px; border-radius: 10px; cursor: pointer; }
.results li.active, .results li:hover { background: var(--surface-2); }
.results .r-title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.results .r-id { color: var(--ink-3); font-size: 13px; font-variant-numeric: tabular-nums; }
.results .empty { color: var(--ink-3); cursor: default; }

.chips { display: flex; gap: 6px; overflow-x: auto; padding: 10px 2px 4px; scrollbar-width: none; -webkit-overflow-scrolling: touch; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; display: inline-flex; align-items: center; gap: 7px; height: 34px; padding: 0 12px 0 10px;
  border-radius: 999px; border: 0; background: var(--surface); box-shadow: 0 1px 2px rgba(20,20,25,.08), 0 2px 8px rgba(20,20,25,.06);
  font-size: 13.5px; font-weight: 500; cursor: pointer; white-space: nowrap;
}
.chip .dot { width: 10px; height: 10px; border-radius: 50%; }
.chip b { font-weight: 700; color: var(--ink-3); font-size: 12px; }
.chip:active { transform: scale(.96); }
body.list-open .chips { display: none; }

/* --- Schwebende Knöpfe ------------------------------------------------------- */
.fabs { position: fixed; right: 12px; bottom: calc(14px + var(--safe-b)); z-index: 9; display: flex; flex-direction: column; gap: 10px; transition: transform .25s ease, opacity .2s; }
.fab {
  display: inline-flex; align-items: center; gap: 8px; height: 46px; padding: 0 16px 0 14px; border: 0; border-radius: 999px;
  background: var(--surface); box-shadow: var(--shadow); font-weight: 600; font-size: 14px; cursor: pointer;
}
.fab:active { transform: scale(.96); }
.fab.on { background: var(--ink); color: #fff; }
body.sheet-open .fabs { opacity: 0; pointer-events: none; }
body.list-open #fitBtn { display: none; }

/* --- Liste -------------------------------------------------------------------- */
.list { position: fixed; inset: 0; z-index: 8; background: var(--bg); overflow-y: auto; overscroll-behavior: contain; padding: calc(72px + var(--safe-t)) 12px calc(90px + var(--safe-b)); }
.list-inner { max-width: 760px; margin: 0 auto; }
.l-chapter { margin-bottom: 14px; background: var(--surface); border-radius: var(--radius); box-shadow: 0 1px 2px rgba(20,20,25,.05); }
.l-head { display: flex; align-items: center; gap: 10px; padding: 14px 16px; border-bottom: 1px solid var(--line); }
.l-head .num { width: 34px; height: 34px; border-radius: 10px; display: grid; place-items: center; font-weight: 700; font-size: 13px; flex: none; }
.l-head h3 { margin: 0; font-size: 16px; line-height: 1.2; }
.l-head small { color: var(--ink-3); margin-left: auto; font-size: 12.5px; flex: none; }
.l-item { all: unset; box-sizing: border-box; display: flex; align-items: center; gap: 10px; width: 100%; padding: 11px 16px; cursor: pointer; border-top: 1px solid var(--surface-2); }
.l-item:hover { background: var(--surface-2); }
.l-item .t { flex: 1; min-width: 0; }
.l-item .t span { display: block; }
.l-item .t small { color: var(--ink-3); font-size: 12.5px; }
.l-item.main .t span { font-weight: 650; }

/* --- Art-Abzeichen -------------------------------------------------------------- */
.kind { flex: none; display: inline-grid; place-items: center; min-width: 26px; height: 22px; padding: 0 6px; border-radius: 6px; font-size: 11.5px; font-weight: 700; letter-spacing: .02em; }
.kind-Axiom, .kind-Basis { background: #292524; color: #fde68a; }
.kind-Definition { background: #eef2f6; color: #475569; }
.kind-Satz { background: #1c1d21; color: #fff; }
.kind-Lemma { background: #f1f0ec; color: #57534e; }

/* --- Detail-Blatt ------------------------------------------------------------------ */
.sheet {
  position: fixed; z-index: 20; background: var(--surface); box-shadow: 0 -2px 30px rgba(20,20,25,.18);
  display: flex; flex-direction: column;
  left: 0; right: 0; bottom: 0; height: 92vh; height: 92dvh; border-radius: 22px 22px 0 0;
  transform: translateY(100%); transition: transform .32s cubic-bezier(.2,.8,.2,1); will-change: transform;
}
.sheet.dragging { transition: none; }
.sheet-head { flex: none; position: relative; height: 40px; }
.sheet-handle { position: absolute; inset: 0; display: grid; place-items: center; cursor: grab; touch-action: none; }
.sheet-handle span { width: 40px; height: 5px; border-radius: 3px; background: #d6d3cd; }
.sheet-close { position: absolute; top: 6px; right: 10px; width: 36px; height: 36px; border-radius: 50%; border: 0; background: var(--surface-2); display: grid; place-items: center; cursor: pointer; color: var(--ink-2); z-index: 2; }
.sheet-body { flex: 1; overflow-y: auto; overscroll-behavior: contain; padding: 0 18px calc(28px + var(--safe-b)); }
.sheet.peek .sheet-body { overflow-y: hidden; }

@media (min-width: 761px) {
  .sheet { left: auto; top: 10px; right: 10px; bottom: 10px; height: auto; width: 420px; border-radius: 20px; transform: translateX(calc(100% + 20px)); }
  .sheet.peek, .sheet.full { transform: none !important; }
  .sheet.peek .sheet-body { overflow-y: auto; }
  .sheet-handle { display: none; }
  .sheet-head { height: 48px; }
  .sheet-close { top: 10px; }
  .fabs { flex-direction: row; }
  body.sheet-open .fabs { opacity: 1; pointer-events: auto; transform: translateX(-430px); }
}

.d-meta { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.pill { display: inline-flex; align-items: center; gap: 6px; height: 24px; padding: 0 9px; border-radius: 999px; font-size: 12.5px; font-weight: 600; background: var(--surface-2); color: var(--ink-2); }
.pill .dot { width: 8px; height: 8px; border-radius: 50%; }
.d-title { font-size: 24px; line-height: 1.15; font-weight: 750; letter-spacing: -.015em; margin: 10px 0 2px; }
.d-sub { color: var(--ink-3); font-size: 14px; }
.callout { margin-top: 12px; padding: 10px 12px; border-radius: 12px; background: var(--warn-soft); color: var(--warn); font-size: 14px; }

.seg { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; margin-top: 16px; padding: 4px; background: var(--surface-2); border-radius: 14px; }
.seg button { border: 0; background: transparent; border-radius: 10px; padding: 8px 4px; cursor: pointer; line-height: 1.15; }
.seg button b { display: block; font-size: 19px; font-weight: 750; font-variant-numeric: tabular-nums; }
.seg button span { font-size: 11.5px; color: var(--ink-3); font-weight: 500; }
.seg button.on { background: var(--surface); box-shadow: 0 1px 3px rgba(20,20,25,.12); }
.seg button.on[data-mode="up"] b { color: var(--up); }
.seg button.on[data-mode="down"] b { color: var(--down); }
.seg-explain { font-size: 13.5px; color: var(--ink-2); margin: 8px 2px 0; }
.seg-explain .sw { display: inline-block; width: 18px; height: 4px; border-radius: 2px; vertical-align: middle; margin-right: 6px; }

.sec { margin-top: 22px; }
.sec-h { font-size: 12px; font-weight: 700; color: var(--ink-3); text-transform: uppercase; letter-spacing: .08em; margin: 0 0 8px; display: flex; align-items: center; gap: 8px; }
.sec-h .count { background: var(--surface-2); border-radius: 999px; padding: 1px 8px; letter-spacing: 0; }
.card { border: 1px solid var(--line); border-radius: 14px; padding: 10px; background: #fff; }
.card img { display: block; width: 100%; height: auto; }
.more { all: unset; cursor: pointer; display: inline-flex; align-items: center; gap: 6px; margin-top: 10px; font-weight: 600; font-size: 14px; color: var(--down); }
.rows { display: flex; flex-direction: column; border: 1px solid var(--line); border-radius: 14px; overflow: hidden; }
.row { all: unset; box-sizing: border-box; display: flex; align-items: center; gap: 10px; padding: 11px 12px; cursor: pointer; border-top: 1px solid var(--surface-2); }
.row:first-child { border-top: 0; }
.row:hover { background: var(--surface-2); }
.row .t { flex: 1; min-width: 0; }
.row .t small { display: block; color: var(--ink-3); font-size: 12.5px; }
.row .chev { color: var(--ink-3); width: 16px; height: 16px; }
.none { color: var(--ink-3); font-size: 14px; padding: 4px 2px; }
.axioms { display: flex; flex-wrap: wrap; gap: 6px; }
.axioms span { font-size: 12.5px; font-weight: 600; padding: 4px 9px; border-radius: 8px; background: #292524; color: #fde68a; }
.axioms .missing { background: var(--surface-2); color: var(--ink-3); text-decoration: line-through; font-weight: 500; }
.ax-note { font-size: 13px; color: var(--ink-3); margin-top: 8px; }

/* --- Erklärung ------------------------------------------------------------------ */
.help { position: fixed; inset: 0; z-index: 30; background: rgba(20, 20, 25, .38); display: grid; place-items: end center; padding: 12px; padding-bottom: calc(12px + var(--safe-b)); }
@media (min-width: 761px) { .help { place-items: center; } }
.help-card { width: min(460px, 100%); max-height: 100%; overflow-y: auto; background: var(--surface); border-radius: 22px; padding: 22px; box-shadow: var(--shadow); }
.eyebrow { margin: 0; font-size: 12px; font-weight: 600; color: var(--ink-3); }
.help-card h2 { margin: 4px 0 14px; font-size: 22px; letter-spacing: -.01em; }
.steps { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 12px; }
.steps li { display: flex; gap: 12px; font-size: 14.5px; }
.step-ic { flex: none; width: 38px; height: 38px; border-radius: 12px; background: var(--surface-2); display: grid; place-items: center; }
.step-ic svg { width: 22px; height: 22px; }
.ic-axiom { fill: #292524; } .ic-land { fill: #dbeafe; stroke: #7fb0f5; stroke-width: 1.5; }
.ic-line { fill: none; stroke: #94a3b8; stroke-width: 2; } .ic-dot { fill: #fff; stroke: #1c1d21; stroke-width: 2; }
.ic-sel { fill: #fff; stroke: var(--up); stroke-width: 3; }
.t-up { color: var(--up); font-weight: 700; } .t-down { color: var(--down); font-weight: 700; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 14px; margin: 16px 0 6px; padding: 12px; background: var(--surface-2); border-radius: 14px; font-size: 13px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend svg { width: 18px; height: 18px; }
.lg-axiom { fill: #292524; } .lg-def { fill: #fff; stroke: #1c1d21; stroke-width: 1.6; }
.lg-satz { fill: #fff; stroke: #1c1d21; stroke-width: 2; } .lg-lemma { fill: #f1f0ec; stroke: #57534e; stroke-width: 1.4; }
.lg-main { fill: #1c1d21; stroke: #fff; stroke-width: 1.5; }
.hint { font-size: 13px; color: var(--ink-3); margin: 6px 0 16px; }
.primary { width: 100%; height: 48px; border: 0; border-radius: 14px; background: var(--ink); color: #fff; font-weight: 650; font-size: 16px; cursor: pointer; }
.primary:active { transform: scale(.98); }

@media (prefers-reduced-motion: reduce) { .sheet, .fabs { transition: none; } }
.results:not([hidden]) ~ .chips { display: none; }

/* --- Fassung: einheitlich (LaTeX) / Skript --------------------------------------- */
.sec-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 8px; }
.sec-row .sec-h { margin: 0; }
.src-switch { display: inline-flex; padding: 3px; background: var(--surface-2); border-radius: 10px; }
.src-switch button { border: 0; background: transparent; padding: 5px 11px; border-radius: 8px; font-size: 13px; font-weight: 600; color: var(--ink-3); cursor: pointer; }
.src-switch button.on { background: var(--surface); color: var(--ink); box-shadow: 0 1px 3px rgba(20,20,25,.12); }
.src-switch button:disabled { opacity: .4; cursor: default; }
.src-note { font-size: 12.5px; color: var(--ink-3); margin: 6px 2px 0; }
.card.tex { padding: 14px 16px; border-left: 4px solid var(--accent, var(--line)); font-size: 15.5px; line-height: 1.6; overflow-x: auto; }
.tex p { margin: 0 0 8px; }
.tex p:last-child { margin-bottom: 0; }
.tex strong { font-weight: 700; }
.tex .katex { font-size: 1.08em; }
.tex .katex-display { margin: 10px 0; overflow-x: auto; overflow-y: hidden; padding: 2px 0; }
.tx-items { list-style: none; margin: 4px 0 8px; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.tx-items li { padding-left: 2.2em; text-indent: -2.2em; }
.tx-items li .katex-display { text-indent: 0; }
.tx-lbl { display: inline-block; min-width: 2em; text-indent: 0; font-weight: 600; color: var(--ink-3); }

.sec-first { margin-top: 16px; }
.seg-explain { margin-bottom: 4px; }
.implied { margin-top: 10px; }
.implied summary { cursor: pointer; font-size: 13.5px; color: var(--ink-3); padding: 4px 2px; list-style: none; }
.implied summary::before { content: "▸ "; }
.implied[open] summary::before { content: "▾ "; }
.implied .rows { margin-top: 6px; opacity: .85; }

/* Vorlesungs-Chips */
.chip.course { font-weight: 700; padding: 0 14px; }
.chip.course.on { background: var(--ink); color: #fff; }
.chip-sep { flex: none; width: 1px; margin: 6px 2px; background: rgba(20,20,25,.15); }
.l-course { font-size: 22px; letter-spacing: -.01em; margin: 26px 4px 12px; }
.l-course:first-child { margin-top: 4px; }
.l-course small { font-size: 14px; color: var(--ink-3); font-weight: 500; }
