:root {
  /* Durchgehend dunkles Design (kein Hell-Umschalter). */
  color-scheme: dark;
  --bg: #0E1014;
  --surface: #171A20;
  --surface-2: #1F232B;
  --text: #E8EAEF;
  --text-2: #C4C8D1;
  --muted: #959BA8;
  --line: #262A32;
  --line-strong: #363B45;
  --accent: #8C9EFF;
  --accent-dark: #B3C0FF;
  --accent-soft: #1C2238;
  --on-accent: #0E1014;
  --short-bg: #14161B;
  --short-hl: #9DB0FF;
  --ok-bg: #10301C; --ok: #74D68F;
  --warn-bg: #3A2A0E; --warn: #F2B65C;
  --err-bg: #3B1613; --err: #FF8E84;
  --radius: 14px;
  --font: Inter, system-ui, sans-serif;
  --font-head: "Space Grotesk", Inter, system-ui, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { font-family: var(--font); color: var(--text); background: var(--bg); font-size: 15px; line-height: 1.5; }
h1, h2, h3 { font-family: var(--font-head); line-height: 1.2; margin: 0; }
h1 { font-size: 28px; }
h2 { font-size: 18px; }
a { color: var(--accent); }
a:hover { color: var(--accent-dark); }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
[hidden] { display: none !important; }

/* ---------- Kopfzeile ---------- */
.topbar { display: flex; flex-wrap: wrap; align-items: center; gap: 12px 24px; padding: 12px 24px;
  background: var(--surface); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 20; }
.brand { display: flex; align-items: center; gap: 10px; font-family: var(--font-head); font-weight: 700; font-size: 19px; text-decoration: none; color: var(--text); }
.brand-mark { width: 32px; height: 32px; border-radius: 9px; background: var(--accent); display: grid; place-items: center; color: var(--on-accent); }
.nav { display: flex; gap: 4px; flex: 1 1 auto; }
.nav a { padding: 9px 14px; border-radius: 8px; color: var(--text-2); font-weight: 500; font-size: 14px; text-decoration: none; }
.nav a[aria-current="page"] { background: var(--accent-soft); color: var(--accent-dark); font-weight: 600; }

/* ---------- Bausteine ---------- */
.page { max-width: 1400px; margin: 0 auto; padding: 24px; display: flex; flex-direction: column; gap: 24px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 20px; display: flex; flex-direction: column; gap: 14px; min-width: 0; }
.row { display: flex; flex-wrap: wrap; gap: 12px; align-items: center; }
.spread { justify-content: space-between; }
.grid-form { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 14px; }
label.field { display: flex; flex-direction: column; gap: 6px; font-size: 14px; font-weight: 500; }
input[type=text], input[type=number], select, textarea {
  padding: 10px 12px; border: 1px solid var(--line-strong); border-radius: 8px; background: var(--surface); min-width: 0; }
textarea { resize: vertical; line-height: 1.45; }
input:focus-visible, select:focus-visible, textarea:focus-visible, button:focus-visible, a:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px; }
.check { display: flex; gap: 10px; align-items: center; font-size: 14px; }
.check input { width: 18px; height: 18px; accent-color: var(--accent); }

.btn { padding: 10px 16px; min-height: 40px; border-radius: 9px; border: 1px solid var(--line-strong); background: var(--surface); font-weight: 600; font-size: 14px; }
.btn:hover { border-color: var(--muted); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn-primary:hover { background: var(--accent-dark); border-color: var(--accent-dark); }
.btn-dark { background: var(--text); border-color: var(--text); color: var(--bg); }
.btn-danger { color: var(--err); border-color: #E8B4AF; }
.btn-danger:hover { background: var(--err-bg); border-color: var(--err); }
.btn-sm { padding: 6px 11px; min-height: 32px; font-size: 13px; font-weight: 500; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.seg-btns { display: flex; gap: 6px; flex-wrap: wrap; }
.seg-btns button[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }

.badge { display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; }
.badge-ok { background: var(--ok-bg); color: var(--ok); }
.badge-warn { background: var(--warn-bg); color: var(--warn); }
.badge-err { background: var(--err-bg); color: var(--err); }
.badge-info { background: var(--accent-soft); color: var(--accent-dark); }
.badge-mute { background: var(--surface-2); color: var(--muted); }
.alert { padding: 12px 14px; border-radius: 10px; background: var(--err-bg); color: var(--err); font-size: 14px; white-space: pre-wrap; word-break: break-word; }
.empty { padding: 28px; text-align: center; color: var(--muted); }

table.tbl { width: 100%; border-collapse: collapse; font-size: 14px; }
.tbl th { text-align: left; color: var(--muted); font-weight: 500; font-size: 13px; padding: 10px 8px; border-bottom: 1px solid var(--line); }
.tbl td { padding: 11px 8px; border-bottom: 1px solid var(--surface-2); vertical-align: middle; }
.tbl .num { font-variant-numeric: tabular-nums; }
.tbl-wrap { overflow-x: auto; }

/* ---------- Short (Hochformat 9:16) ---------- */
.short { position: relative; aspect-ratio: 9 / 16; background: var(--short-bg); box-shadow: 0 0 0 1px var(--line); color: #F2F4F8; border-radius: 18px; overflow: hidden;
  container-type: inline-size; user-select: none; }
/* Unten ist Platz für zwei Untertitelzeilen reserviert, damit das Bild nie verschoben wird. */
.short-stage { position: absolute; inset: 0; display: flex; flex-direction: column; padding: 14cqw 6cqw 27cqw; }
.short.no-subs .short-stage { padding-bottom: 6cqw; }
.short-bars { position: absolute; top: 4cqw; left: 4cqw; right: 4cqw; display: flex; gap: 1.2cqw; z-index: 3; }
.short-bars span { flex: 1; height: 0.9cqw; min-height: 3px; border-radius: 2px; background: rgba(255,255,255,.25); overflow: hidden; }
.short-bars i { display: block; height: 100%; width: 0; background: #fff; }
.short-head { position: absolute; top: 7.5cqw; left: 4.5cqw; right: 4.5cqw; font-size: 3.4cqw; color: rgba(242,244,248,.7); z-index: 3;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.short-visual { flex: 1; display: flex; flex-direction: column; justify-content: center; align-items: stretch; min-height: 0; gap: 4cqw; }
.short-sub { position: absolute; left: 5cqw; right: 5cqw; bottom: 6cqw; z-index: 1; margin: 0 auto; width: fit-content; max-width: calc(100% - 10cqw);
  font-size: 4.6cqw; line-height: 1.4; font-weight: 500; text-align: center; background: rgba(0,0,0,.55);
  border-radius: 3cqw; padding: 2.2cqw 3.4cqw; }
.short-sub:empty { display: none; }
.short-sub[hidden] { display: none; }
/* Kurzer Pfeil beim Tippen links/rechts */
.short-flash { position: absolute; top: 50%; z-index: 2; width: 16cqw; height: 16cqw; margin-top: -8cqw; border-radius: 50%;
  background: rgba(0,0,0,.45); display: grid; place-items: center; opacity: 0; pointer-events: none; }
.short-flash.prev { left: 6cqw; }
.short-flash.next { right: 6cqw; }
.short-flash svg { width: 9cqw; height: 9cqw; fill: none; stroke: #fff; stroke-width: 2.6; stroke-linecap: round; stroke-linejoin: round; }
.short-flash.show { animation: short-flash .45s ease-out; }
@keyframes short-flash { 0% { opacity: .95; transform: scale(.85); } 100% { opacity: 0; transform: scale(1.1); } }
.short-sub .w { transition: color .12s; }
.short-sub .w.on { color: var(--short-hl); }
.short-play { position: absolute; inset: 0; display: grid; place-items: center; background: none; border: 0; z-index: 2; color: #fff; }
.short-play svg { width: 18cqw; height: 18cqw; opacity: .9; filter: drop-shadow(0 2px 8px rgba(0,0,0,.4)); }
.short.playing .short-play svg { display: none; }

.v-title { font-family: var(--font-head); font-size: 9cqw; font-weight: 700; line-height: 1.12; }
/* Lange deutsche Komposita („Spezifikationsphase“) silbengetrennt umbrechen statt abschneiden. */
.v-title, .v-subtitle, .v-bullets li, .short-sub { hyphens: auto; -webkit-hyphens: auto; overflow-wrap: break-word; }
.v-subtitle { font-size: 4.6cqw; color: var(--short-hl); margin-top: 3cqw; }
.v-formula { font-size: 6.2cqw; text-align: center; overflow: hidden; }
.v-formula .katex-display { margin: 0; }
.v-bullets { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 3.4cqw; }
.v-bullets li { font-size: 5.6cqw; font-weight: 500; padding-left: 6cqw; position: relative; line-height: 1.3; }
.v-bullets li::before { content: ""; position: absolute; left: 0; top: .5em; width: 2.4cqw; height: 2.4cqw; border-radius: 50%; background: var(--short-hl); }
.v-crop { position: relative; overflow: hidden; background: #fff; border-radius: 2.4cqw; width: 100%; max-height: 100%; margin: 0 auto; }
.v-crop img { position: absolute; display: block; max-width: none; }
.v-diagram { background: #fff; border-radius: 2.4cqw; padding: 3cqw; display: grid; place-items: center; max-height: 100%; overflow: hidden; }
.v-diagram svg { max-width: 100%; height: auto; }
.v-error { font-size: 4cqw; color: #FFB4AB; }

/* ---------- Icon-Leiste unten (Handy) ---------- */
:root { --tabbar-h: 58px; }
.tabbar { display: none; }
.mobile-only { display: none !important; }
@media (max-width: 1000px) { .mobile-only { display: flex !important; } }
@media (max-width: 1000px) {
  .topbar { display: none; }
  body { padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom)); }
  .tabbar {
    display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 60;
    height: calc(var(--tabbar-h) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
    background: var(--surface); border-top: 1px solid var(--line);
  }
  .tabbar a { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
    font-size: 11px; font-weight: 500; color: var(--muted); text-decoration: none; min-height: 44px; }
  .tabbar a[aria-current="page"] { color: var(--accent); font-weight: 600; }
  .tabbar svg { width: 24px; height: 24px; }
  .page { padding: 16px; }
}

/* ---------- Fenster von unten (Handy) ---------- */
.sheet { position: fixed; inset: 0; z-index: 80; display: flex; flex-direction: column; justify-content: flex-end; }
.sheet[hidden] { display: none; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,.45); border: 0; padding: 0; cursor: default; }
.sheet-panel { position: relative; background: var(--surface); color: var(--text); border-radius: 18px 18px 0 0;
  max-height: 78dvh; display: flex; flex-direction: column; padding-bottom: env(safe-area-inset-bottom);
  box-shadow: 0 -10px 40px rgba(0,0,0,.25); animation: sheet-in .18s ease-out; }
@keyframes sheet-in { from { transform: translateY(24px); opacity: .6; } }
.sheet-grip { width: 40px; height: 4px; border-radius: 2px; background: var(--line-strong); margin: 8px auto 0; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 16px 12px; border-bottom: 1px solid var(--line); }
.sheet-head h2 { font-size: 17px; }
.sheet-body { overflow-y: auto; padding: 12px 16px 20px; display: flex; flex-direction: column; gap: 14px; overscroll-behavior: contain; }
.sheet-body > * { flex-shrink: 0; }  /* sonst quetscht eine lange Liste andere Bereiche (z. B. Module) auf 0 px */
.icon-btn { width: 40px; height: 40px; border-radius: 50%; border: 0; background: var(--surface-2); display: grid; place-items: center; font-size: 18px; flex: none; }

/* ---------- Dialog ---------- */
dialog { background: var(--surface); color: var(--text); border: 0; border-radius: 16px; padding: 0; width: min(1100px, 96vw); max-height: 92vh; box-shadow: 0 20px 60px rgba(0,0,0,.25); }
dialog::backdrop { background: rgba(0,0,0,.6); }
.dlg { display: flex; flex-direction: column; max-height: 92vh; }
.dlg-head, .dlg-foot { padding: 16px 20px; display: flex; gap: 12px; align-items: center; justify-content: space-between; border-bottom: 1px solid var(--line); }
.dlg-foot { border-top: 1px solid var(--line); border-bottom: 0; }
.dlg-body { padding: 20px; overflow: auto; display: grid; grid-template-columns: minmax(0, 1fr) 300px; gap: 20px; }
@media (max-width: 800px) { .dlg-body { grid-template-columns: 1fr; } }
.result-tex { overflow-x: auto; overflow-y: hidden; padding: 4px 0; position: relative; }

/* ---------- Taschenrechner (rechner.js) ---------- */
.calc { position: fixed; z-index: 50; top: 76px; right: 16px; bottom: 16px; width: 380px;
  background: var(--surface); border: 1px solid var(--line-strong); border-radius: 16px; box-shadow: 0 16px 48px rgba(0,0,0,.35);
  display: flex; flex-direction: column; gap: 10px; padding: 12px; }
.calc[hidden] { display: none; }
.calc-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.calc-head b { font-family: var(--font-head); font-size: 16px; }
.calc-head-r { display: flex; align-items: center; gap: 6px; }
.calc-chip { min-height: 30px; padding: 0 10px; border-radius: 8px; border: 1px solid var(--line-strong); background: var(--bg);
  font-size: 12px; font-weight: 700; letter-spacing: .05em; color: var(--accent-dark); }
.calc-x { width: 32px; height: 32px; border-radius: 50%; border: 0; background: var(--surface-2); font-size: 18px; line-height: 1; }
.calc-hist-wrap { flex: 1 1 auto; min-height: 48px; display: flex; flex-direction: column; overflow: hidden; }
.calc-hist { list-style: none; margin: 0; padding: 0 2px; overflow-y: auto; flex: 1 1 auto; display: flex; flex-direction: column; gap: 2px; }
.calc-hist li { display: flex; flex-direction: column; align-items: stretch; border-bottom: 1px solid var(--line); padding: 4px 0; }
.calc-hist button { border: 0; background: none; padding: 2px 4px; border-radius: 6px; overflow-x: auto; overflow-y: hidden; white-space: nowrap; }
.calc-hist button:hover { background: var(--surface-2); }
.calc-h-expr { text-align: left; color: var(--text-2); font-size: 14px; }
.calc-h-val { text-align: right; font-size: 16px; color: var(--text); }
.calc-link { align-self: flex-end; border: 0; background: none; color: var(--muted); font-size: 12px; padding: 4px; }
.calc-disp { background: var(--bg); border: 1px solid var(--line); border-radius: 12px; padding: 8px 10px; display: flex; flex-direction: column; gap: 4px; }
.calc-expr { min-height: 26px; font-size: 17px; overflow-x: auto; overflow-y: hidden; white-space: nowrap; }
.calc-res { min-height: 24px; text-align: right; font-size: 20px; font-weight: 600; overflow-x: auto; overflow-y: hidden; white-space: nowrap; }
.calc-res.pending { font-size: 13px; font-weight: 400; color: var(--muted); }
.calc-res.err { font-size: 13px; font-weight: 600; color: var(--err); }
.calc-check { font-size: 12px; text-align: right; }
.calc-check.ok { color: var(--ok); }
.calc-check.bad { color: var(--warn); }
.calc-in { width: 100%; border: 0; border-top: 1px solid var(--line); background: transparent; padding: 8px 2px 2px; font-size: 16px;
  font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; outline: none; color: var(--text); }
.calc-vars { display: flex; flex-direction: column; gap: 6px; max-height: 120px; overflow-y: auto; }
.calc-vgroup { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.calc-vtitle { font-size: 11px; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); width: 100%; }
.calc-var { border: 1px solid var(--line-strong); background: var(--surface-2); border-radius: 8px; padding: 3px 8px; font-size: 13px; min-height: 30px; }
.calc-var:hover { border-color: var(--accent); }
.calc-own { display: inline-flex; align-items: center; }
.calc-own .calc-var { border-radius: 8px 0 0 8px; }
.calc-del { border: 1px solid var(--line-strong); border-left: 0; background: var(--surface-2); border-radius: 0 8px 8px 0; min-height: 30px; padding: 0 7px; color: var(--muted); }
.calc-keys { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 5px; flex: none; }
.calc-key { min-height: 42px; border-radius: 9px; border: 1px solid var(--line); background: var(--surface-2); font-size: 16px; font-weight: 500;
  touch-action: manipulation; user-select: none; -webkit-user-select: none; padding: 0; }
.calc-key:hover { border-color: var(--line-strong); filter: brightness(1.12); }
.calc-key:active { transform: translateY(1px); }
.calc-key.num { background: #262B35; font-size: 18px; }
.calc-key.fn { font-size: 14px; color: var(--text-2); }
.calc-key.op { color: var(--accent-dark); font-size: 18px; }
.calc-key.del { color: var(--warn); }
.calc-key.eq { grid-column: span 4; background: var(--accent); border-color: var(--accent); color: var(--on-accent); font-weight: 700; font-size: 20px; }
.calc-key[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); color: var(--accent-dark); }
@media (min-width: 1001px) { body.calc-open .page { padding-right: 420px; } }
@media (max-width: 1000px) {
  .calc { top: auto; left: 0; right: 0; width: auto; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
    max-height: 66dvh; border-radius: 16px 16px 0 0; border-bottom: 0; padding: 10px 10px 8px; gap: 8px; }
  .calc-hist-wrap { min-height: 0; max-height: 14dvh; }
  .calc-vars { max-height: 72px; }
  .calc-key { min-height: 40px; }
  body.calc-open { padding-bottom: calc(66dvh + var(--tabbar-h)); }
}
.calc-toggle { display: inline-flex; align-items: center; gap: 6px; }
.calc-toggle[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); color: var(--accent-dark); }
@media (min-width: 1001px) and (max-height: 860px) { .calc-key { min-height: 34px; font-size: 15px; } .calc-vars { max-height: 84px; } }
