:root {
  --bg: #121214;
  --surface: #1c1c20;
  --surface-2: #26262b;
  --text: #ece9e2;
  --muted: #9a978f;
  --accent: #e8a34a;
  --accent-ink: #1a1206;
  --line: #2e2e34;
  --radius: 12px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: dark;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body { font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; min-height: 100dvh; }
body.reading { overflow: hidden; overscroll-behavior: none; }
a { color: inherit; text-decoration: none; }
button, input, select { font: inherit; color: inherit; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

/* ---------- Grundlayout ---------- */
.topbar {
  position: sticky; top: 0; z-index: 5; display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: calc(10px + var(--safe-top)) 16px 10px; background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px); border-bottom: 1px solid var(--line);
}
.topbar h1 { margin: 0; font-size: 20px; letter-spacing: .2px; }
.title-ellipsis { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 17px !important; }
.topbar-actions { display: flex; gap: 4px; }
.icon-btn { display: grid; place-items: center; width: 40px; height: 40px; border: 0; border-radius: 50%; background: transparent; cursor: pointer; }
.icon-btn:active { background: var(--surface-2); }
.page { padding: 12px 16px calc(32px + var(--safe-bottom)); max-width: 1400px; margin: 0 auto; }
h2 { font-size: 17px; margin: 18px 0 10px; }
.section-head { display: flex; align-items: center; justify-content: space-between; }
select, .search {
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px;
}
.search { width: 100%; margin-bottom: 14px; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 11px 16px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--surface-2); cursor: pointer; font-weight: 600;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn.ghost { background: transparent; border-color: transparent; color: var(--muted); }
.mini { border: 0; background: none; color: var(--accent); padding: 0 0 0 6px; font-size: 12px; cursor: pointer; }
.empty { color: var(--muted); padding: 40px 0; text-align: center; grid-column: 1 / -1; }
.toast {
  position: fixed; left: 50%; bottom: calc(24px + var(--safe-bottom)); transform: translateX(-50%); z-index: 50;
  background: var(--surface-2); padding: 10px 16px; border-radius: 999px; box-shadow: 0 6px 24px #0008; max-width: calc(100% - 32px);
}

/* ---------- Kacheln ---------- */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(108px, 1fr)); gap: 16px 12px; }
@media (min-width: 560px) { .grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); } }
.tile { position: relative; min-width: 0; }
.tile > a { display: block; position: relative; }
.cover {
  aspect-ratio: 2 / 3; border-radius: 8px; overflow: hidden; background: var(--surface);
  display: grid; place-items: center; box-shadow: 0 2px 10px #0006;
}
.cover img { width: 100%; height: 100%; object-fit: cover; display: block; }
.cover-fallback { font-size: 28px; font-weight: 700; color: var(--muted); text-transform: uppercase; }
.tile-title { margin-top: 6px; font-size: 13.5px; font-weight: 600; line-height: 1.25; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.tile-sub { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar { height: 3px; background: #ffffff22; border-radius: 2px; margin-top: 6px; overflow: hidden; }
.tile > a .bar { position: absolute; left: 6px; right: 6px; bottom: 6px; margin: 0; }
.bar span { display: block; height: 100%; background: var(--accent); }
.badge { position: absolute; top: 6px; right: 6px; width: 26px; height: 26px; border-radius: 50%; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; }
.badge svg { width: 16px; height: 16px; stroke-width: 3; }
.row-scroll { display: grid; grid-auto-flow: column; grid-auto-columns: 104px; gap: 12px; overflow-x: auto; padding-bottom: 6px; scrollbar-width: none; }
.row-scroll::-webkit-scrollbar { display: none; }
.series-hero { display: grid; grid-template-columns: 120px 1fr; gap: 16px; align-items: end; margin: 8px 0 22px; }
.series-hero h2 { margin: 0 0 4px; font-size: 20px; }
.series-hero .btn { margin-top: 10px; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; display: block; }

/* ---------- Anmeldung ---------- */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 360px; text-align: center; }
.login-logo { width: 72px; height: 72px; border-radius: 18px; }
.login h1 { margin: 10px 0 2px; }
.login form { display: grid; gap: 12px; margin-top: 18px; text-align: left; }
.login label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.login input { padding: 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); color: var(--text); font-size: 16px; }
.error { color: #ff8a7a; margin: 0; font-size: 14px; }

/* ---------- Reader ---------- */
.reader { --rbg: #000; position: fixed; inset: 0; background: var(--rbg); user-select: none; -webkit-user-select: none; }
.reader[data-bg="paper"] { --rbg: #f4f2ed; }
.reader[data-bg="gray"] { --rbg: #2a2a2a; }
.reader[data-bg="white"] { --rbg: #fff; }
.stage { position: absolute; inset: 0; overflow: hidden; touch-action: none; }
.zoom { width: 100%; height: 100%; transform-origin: 0 0; will-change: transform; }
.spread { position: relative; width: 100%; height: 100%; display: flex; align-items: center; justify-content: center; }
/* Buchfalz wie bei Perfect Viewer: helles Papier, das zur Mitte weich grau abfällt.
   Liegt als eigene Ebene über Seiten und Umblätter-Animation und bleibt dadurch ruhig stehen. */
.book-shade { position: absolute; inset: 0; pointer-events: none; z-index: 3; display: none; }
.book-shade.on { display: block; }
.book-shade::after {
  content: ""; position: absolute; top: 0; bottom: 0; left: 50%; width: 36%; transform: translateX(-50%);
  mix-blend-mode: multiply;
  background:
    linear-gradient(90deg, transparent 38%, rgba(0,0,0,.02) 42%, rgba(0,0,0,.07) 44.5%, rgba(0,0,0,.13) 46%, rgba(0,0,0,.21) 47.3%,
      rgba(0,0,0,.32) 48.4%, rgba(0,0,0,.45) 49.3%, rgba(0,0,0,.55) 50%, rgba(0,0,0,.45) 50.7%, rgba(0,0,0,.32) 51.6%,
      rgba(0,0,0,.21) 52.7%, rgba(0,0,0,.13) 54%, rgba(0,0,0,.07) 55.5%, rgba(0,0,0,.02) 58%, transparent 62%),
    linear-gradient(90deg, transparent, rgba(0,0,0,.05) 30%, rgba(0,0,0,.10) 45%, rgba(0,0,0,.12) 50%, rgba(0,0,0,.10) 55%, rgba(0,0,0,.05) 70%, transparent);
}
/* Seitenränder: leichte graue Kante außen, wie der Papierschnitt */
.book-shade::before {
  content: ""; position: absolute; inset: 0; mix-blend-mode: multiply;
  background: linear-gradient(90deg, rgba(0,0,0,.12), rgba(0,0,0,.03) .4%, transparent 1%, transparent 99%, rgba(0,0,0,.03) 99.6%, rgba(0,0,0,.12));
}
/* Leichte Wölbung: das Papier fängt neben dem Falz etwas Licht */
.book-shade > i {
  position: absolute; inset: 0; mix-blend-mode: screen;
  background: linear-gradient(90deg, transparent 35%, rgba(255,255,255,.10) 44%, transparent 48%, transparent 52%, rgba(255,255,255,.10) 56%, transparent 65%);
}
.turn { z-index: 2; }

.spread.pair.rtl { flex-direction: row-reverse; }
.pg { height: 100%; display: flex; align-items: center; justify-content: center; min-width: 0; }
.pg img, .pg canvas { max-width: 100%; max-height: 100%; width: auto; height: auto; display: block; object-fit: contain; }
.spread.pair .pg { width: 50%; }
.spread.pair .pg:first-child { justify-content: flex-end; }
.spread.pair .pg:last-child { justify-content: flex-start; }
.spread.pair.rtl .pg:first-child { justify-content: flex-start; }
.spread.pair.rtl .pg:last-child { justify-content: flex-end; }
.pg.full { width: 100%; }
.pg img.fill, .pg canvas.fill, .tp img.fill, .tp canvas.fill { width: 100%; height: 100%; max-width: none; max-height: none; object-fit: fill; }

/* Umblätter-Animation */
.turn { position: absolute; inset: 0; overflow: hidden; background: var(--rbg); }
.turn.mir { transform: scaleX(-1); }
.turn.mir img, .turn.mir canvas { transform: scaleX(-1); }
.tp { height: 100%; display: flex; align-items: center; overflow: hidden; background: var(--rbg); }
.tp.l { justify-content: flex-start; } .tp.r { justify-content: flex-end; } .tp.c { justify-content: center; }
.tp img, .tp canvas { max-width: 100%; max-height: 100%; display: block; }
.t-under { position: absolute; inset: 0; display: flex; }
.t-under > .tp { width: 100%; }
.turn.dbl .t-under > .tp { width: 50%; }
.t-static { position: absolute; left: 0; top: 0; width: 50%; height: 100%; }
.t-static > .tp, .t-page > .tp, .t-back > .tp { width: 100%; }
.t-page, .t-flap, .t-cast { position: absolute; top: 0; }
.t-page { overflow: hidden; }
.t-flap { transform-origin: 0 0; }
.t-back { position: absolute; inset: 0; transform: scaleX(-1); }
/* Nur das Bild selbst wird umgeschlagen, nicht die schwarzen Ränder */
.t-page > .tp, .t-back > .tp { background: transparent; }
.t-back.paper { background: #f6f4ef; }
.t-shade, .t-cast { pointer-events: none; }
.t-shade { position: absolute; inset: 0; }

.scroller { position: absolute; inset: 0; overflow: auto; touch-action: pan-x pan-y; --sz: 1; }
.spg { width: calc(100% * var(--sz)); margin: 0 auto; aspect-ratio: 0.7; }
.spg.loaded { aspect-ratio: auto; }
.spg img, .spg canvas { width: 100%; height: auto; display: block; }
@media (min-aspect-ratio: 1/1) { .spg { width: calc(60% * var(--sz)); } }

.epub-stage { position: absolute; inset: calc(28px + var(--safe-top)) 12px calc(28px + var(--safe-bottom)); }
.epub-stage[data-theme="sepia"] { background: #f4ecd8; }
.epub-stage[data-theme="light"] { background: #fff; }
.reader:has(.epub-stage[data-theme="sepia"]:not([hidden])) { background: #f4ecd8; }
.reader:has(.epub-stage[data-theme="light"]:not([hidden])) { background: #fff; }
.reader:has(.epub-stage[data-theme="dark"]:not([hidden])) { background: #121214; }

.r-top, .r-bottom {
  position: absolute; left: 0; right: 0; z-index: 10; display: flex; align-items: center; gap: 8px;
  background: #121214e8; backdrop-filter: blur(10px); color: var(--text);
  transition: transform .22s ease, opacity .22s ease;
}
.r-top { top: 0; padding: calc(6px + var(--safe-top)) 8px 6px; }
.r-bottom { bottom: 0; padding: 10px 16px calc(12px + var(--safe-bottom)); flex-direction: column; align-items: stretch; }
.reader:not(.ui) .r-top { transform: translateY(-100%); opacity: 0; pointer-events: none; }
.reader:not(.ui) .r-bottom { transform: translateY(100%); opacity: 0; pointer-events: none; }
.r-title { flex: 1; min-width: 0; line-height: 1.2; }
.r-title div, .r-title small { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.r-title small { color: var(--muted); }
.r-label { text-align: center; font-size: 13px; color: var(--muted); margin-top: 4px; font-variant-numeric: tabular-nums; }
input[type=range] { width: 100%; accent-color: var(--accent); }

.sheet { position: absolute; inset: 0; z-index: 20; background: #0008; display: flex; align-items: flex-end; justify-content: center; }
.sheet[hidden], .endcard[hidden] { display: none; }
.sheet-inner {
  width: 100%; max-width: 560px; max-height: 85dvh; overflow: auto; background: var(--surface); border-radius: 18px 18px 0 0;
  padding: 18px 16px calc(18px + var(--safe-bottom)); display: grid; gap: 6px;
}
.sheet h3 { margin: 10px 0 2px; font-size: 13px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .5px; }
.seg { display: flex; background: var(--surface-2); border-radius: 10px; padding: 3px; gap: 3px; }
.seg button { flex: 1; border: 0; background: transparent; padding: 9px 6px; border-radius: 8px; font-size: 14px; cursor: pointer; }
.seg button.on { background: var(--accent); color: var(--accent-ink); font-weight: 600; }
.sheet .btn { margin-top: 12px; }

.endcard { position: absolute; inset: 0; z-index: 15; background: #000c; display: grid; place-items: center; padding: 16px; }
.endcard-inner { background: var(--surface); padding: 22px; border-radius: 16px; width: 100%; max-width: 360px; display: grid; gap: 10px; text-align: center; }
.endcard-inner h2 { margin: 0 0 6px; }
