:root {
  --bg: #17153a;
  --bg-2: #201d4d;
  --surface: #2a2660;
  --surface-2: #363175;
  --text: #f6f3ff;
  --muted: #b3acdf;
  --paper: #fff8ec;
  --paper-edge: #f1e3c8;
  --ink: #2c2340;
  --accent: #ffb547;
  --read: #ffcf4a;
  --read-text: #3a2600;
  --listen: #5ee6a8;
  --listen-text: #06311f;
  --radius: 22px;
  --display: "Fredoka", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --story: "Andika", "Fredoka", system-ui, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; }

body {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  background:
    radial-gradient(90% 50% at 10% -10%, rgba(139, 124, 255, .32) 0%, transparent 60%),
    radial-gradient(70% 45% at 100% 0%, rgba(255, 138, 76, .18) 0%, transparent 60%),
    var(--bg);
  background-repeat: no-repeat;
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  line-height: 1.5;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

button { font: inherit; }

/* ---------- Kopfzeile und Sprache ---------- */

.library main, .site-header, .site-footer {
  width: 100%;
  max-width: 1080px;
  margin: 0 auto;
  padding-left: 16px;
  padding-right: 16px;
}

.site-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding-top: 16px;
  padding-bottom: 8px;
}

.brand { display: inline-flex; align-items: center; gap: 10px; color: inherit; text-decoration: none; min-width: 0; }
.brand-mark { width: 34px; height: 34px; flex: none; }
.brand-name { font-family: var(--display); font-weight: 700; font-size: 21px; letter-spacing: .2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.lang-switch { display: inline-flex; padding: 4px; gap: 2px; background: var(--surface); border-radius: 999px; flex: none; }
.lang-switch button {
  border: 0; background: transparent; color: var(--muted);
  font: 700 13px/1 system-ui, sans-serif; letter-spacing: .5px;
  padding: 8px 12px; border-radius: 999px; cursor: pointer;
}
.lang-switch button[aria-pressed="true"] { background: var(--surface-2); color: var(--text); box-shadow: 0 1px 4px rgba(0, 0, 0, .3); }

/* ---------- Startseite ---------- */

.library main { flex: 1; }

.hero { padding: 36px 0 28px; max-width: 640px; }
.hero h1 {
  font-family: var(--display); font-weight: 700;
  font-size: clamp(32px, 7vw, 52px); line-height: 1.08; letter-spacing: -.5px;
  margin: 0 0 14px; text-wrap: balance;
}
.hero p { margin: 0; color: var(--muted); font-size: clamp(16px, 2.4vw, 19px); text-wrap: pretty; }

.section-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 16px; }
.section-head h2 { font-family: var(--display); font-weight: 600; font-size: 22px; margin: 0; }
.count { color: var(--muted); font-size: 14px; }

.shelf {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 22px;
  padding-bottom: 32px;
}

.book {
  display: flex;
  flex-direction: column;
  background: var(--bg-2);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .25);
  border: 1px solid rgba(255, 255, 255, .06);
}

.book-cover {
  position: relative;
  display: block;
  aspect-ratio: 3 / 4;
  background: var(--surface);
  overflow: hidden;
  /* Buchrücken */
  box-shadow: inset 10px 0 0 rgba(0, 0, 0, .18), inset 13px 0 0 rgba(255, 255, 255, .08);
}
.book-cover img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; transition: transform .35s ease; z-index: -0; }
.book-cover::after {
  content: ""; position: absolute; inset: 0;
  box-shadow: inset 10px 0 0 rgba(0, 0, 0, .2), inset 13px 0 0 rgba(255, 255, 255, .1);
  background: linear-gradient(transparent 55%, rgba(10, 8, 30, .78));
  pointer-events: none;
}
.book-cover:hover img { transform: scale(1.04); }
.book-title {
  position: absolute; left: 24px; right: 16px; bottom: 16px; z-index: 1;
  font-family: var(--display); font-weight: 700; font-size: 25px; line-height: 1.12;
  color: #fff; text-shadow: 0 2px 10px rgba(0, 0, 0, .5); text-wrap: balance;
}
.badge {
  position: absolute; top: 12px; right: 12px; z-index: 1;
  background: rgba(23, 21, 58, .85); color: var(--text);
  font-size: 13px; font-weight: 700; padding: 5px 10px; border-radius: 999px;
}
.badge.done { background: var(--listen); color: var(--listen-text); }

.book-body { display: flex; flex-direction: column; gap: 8px; padding: 14px 16px 16px; flex: 1; }
.book-blurb { margin: 0; color: var(--text); font-size: 15px; flex: 1; text-wrap: pretty; }
.book-meta { margin: 0; color: var(--muted); font-size: 13px; }
.book-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 6px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 48px; padding: 10px 16px;
  border: 0; border-radius: 16px; cursor: pointer; text-decoration: none;
  font-family: var(--display); font-weight: 600; font-size: 18px;
  transition: transform .12s ease, filter .12s ease;
  box-shadow: 0 4px 0 rgba(0, 0, 0, .25);
}
.btn:active { transform: translateY(2px); box-shadow: 0 2px 0 rgba(0, 0, 0, .25); }
.btn:hover { filter: brightness(1.06); }
.btn-read { background: var(--read); color: var(--read-text); }
.btn-listen { background: var(--listen); color: var(--listen-text); }
.btn.big { min-height: 58px; font-size: 21px; padding: 12px 22px; border-radius: 18px; }
.btn.pulse { animation: pulse 1.6s ease-in-out infinite; }
@keyframes pulse { 50% { transform: scale(1.06); box-shadow: 0 4px 0 rgba(0, 0, 0, .25), 0 0 0 10px rgba(94, 230, 168, .2); } }

.site-footer {
  display: flex; flex-wrap: wrap; justify-content: space-between; gap: 8px 20px;
  padding-top: 20px; padding-bottom: 28px; color: var(--muted); font-size: 14px;
}
.site-footer a { color: var(--text); text-decoration: none; }

:focus-visible { outline: 3px solid var(--read); outline-offset: 3px; }

/* ---------- Leser ---------- */

.reader { height: 100vh; height: 100dvh; overflow: hidden; }

.reader-bar {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 14px 6px;
}
.reader-title {
  flex: 1; min-width: 0; margin: 0;
  font-family: var(--display); font-weight: 600; font-size: 19px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

.icon-btn {
  display: inline-grid; place-items: center; flex: none;
  width: 46px; height: 46px; border-radius: 14px;
  background: var(--surface); color: var(--text);
}
.icon-btn svg { width: 24px; height: 24px; }

.stage {
  flex: 1; min-height: 0;
  display: grid;
  padding: 8px 14px;
  touch-action: pan-y;
  user-select: none;
  -webkit-user-select: none;
}
.stage > section { min-height: 0; }

.enter-next { animation: enter-next .38s cubic-bezier(.2, .8, .3, 1); }
.enter-prev { animation: enter-prev .38s cubic-bezier(.2, .8, .3, 1); }
@keyframes enter-next { from { opacity: 0; transform: translateX(40px) rotateY(-6deg); } }
@keyframes enter-prev { from { opacity: 0; transform: translateX(-40px) rotateY(6deg); } }

/* Seite: Bild oben, Text darunter – im Querformat nebeneinander.
   Das Bild wird nie beschnitten: es passt sich per Container-Einheiten in den freien Platz ein. */
.page-view {
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  gap: 6px;
  padding: 12px;
  background:
    radial-gradient(120% 80% at 0% 0%, rgba(255, 255, 255, .7), transparent 60%),
    var(--paper);
  border-radius: var(--radius);
  box-shadow: 0 18px 40px rgba(0, 0, 0, .35), inset 0 0 0 1px var(--paper-edge);
  width: 100%;
  max-width: 1280px;
  max-height: 100%;
  margin: 0 auto;
  align-self: center;
}
.page-art { margin: 0; min-height: 0; display: grid; place-items: center; }
.page-art img {
  display: block;
  width: min(100%, 78dvh);
  aspect-ratio: 3 / 2;
  border-radius: 14px;
  background: #e9e4f5;
  box-shadow: 0 6px 18px rgba(60, 40, 0, .18);
}

.paper {
  position: relative;
  color: var(--ink);
  padding: 8px 10px 24px;
  max-height: 46dvh;
  overflow: auto;
  display: flex; align-items: center; justify-content: center;
}
.story-text {
  margin: 0;
  max-width: 34em;
  font-family: var(--story);
  font-size: clamp(21px, 1.6vw + 1.2vh + 6px, 34px);
  line-height: 1.5;
  text-wrap: pretty;
}
.sentence { border-radius: 8px; padding: 1px 3px; margin: 0 -3px; cursor: pointer; transition: background-color .25s ease, color .25s ease; box-decoration-break: clone; -webkit-box-decoration-break: clone; }
.sentence:hover { background: rgba(255, 207, 74, .25); }
.sentence.active { background: var(--read); color: var(--read-text); }
.page-no { position: absolute; right: 6px; bottom: 0; font: 600 14px var(--display); color: #a99b7c; }

@media (min-aspect-ratio: 13/10) and (min-width: 700px) {
  .page-view { grid-template-rows: none; grid-template-columns: minmax(0, 1fr) minmax(260px, 36%); gap: 14px; padding: 16px; height: 100%; }
  .page-art { container-type: size; }
  .page-art img { width: min(100cqw, 150cqh); height: min(66.667cqw, 100cqh); aspect-ratio: auto; }
  .paper { max-height: none; padding: 12px 16px 28px 8px; justify-content: flex-start; }
}

/* Titelseite */
.cover-view {
  display: grid; grid-template-columns: minmax(0, auto) minmax(0, 1fr); gap: 32px;
  align-items: center; align-self: center; justify-self: center;
  width: 100%; max-width: 1000px; max-height: 100%;
}
.cover-art { margin: 0; height: min(70dvh, 620px); aspect-ratio: 3 / 4; border-radius: 10px 22px 22px 10px; overflow: hidden; box-shadow: 0 22px 50px rgba(0, 0, 0, .45), inset 0 0 0 1px rgba(255,255,255,.1); position: relative; }
.cover-art::after { content: ""; position: absolute; inset: 0; box-shadow: inset 12px 0 0 rgba(0, 0, 0, .2), inset 15px 0 0 rgba(255, 255, 255, .1); }
.cover-art img { width: 100%; height: 100%; object-fit: cover; display: block; }
.cover-title { font-family: var(--display); font-weight: 700; font-size: clamp(30px, 4.6vw, 52px); line-height: 1.1; margin: 0 0 12px; text-wrap: balance; }
.cover-title .sentence { cursor: pointer; }
.cover-blurb { color: var(--muted); font-size: clamp(17px, 1.8vw, 20px); margin: 0 0 26px; max-width: 34ch; text-wrap: pretty; }
.cover-actions { display: flex; flex-wrap: wrap; gap: 14px; }

@media (max-width: 699px), (max-aspect-ratio: 5/4) {
  .cover-view { grid-template-columns: none; justify-items: center; text-align: center; gap: 18px; align-content: center; }
  .cover-art { height: auto; width: min(64vw, 38dvh, 360px); }
  .cover-blurb { margin: 0 auto 18px; }
  .cover-actions { justify-content: center; }
  .cover-actions .btn { flex: 1 1 200px; }
}

/* Ende */
.end-view { position: relative; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; gap: 4px; align-self: center; }
.end-cover { width: 120px; aspect-ratio: 3 / 4; object-fit: cover; border-radius: 6px 14px 14px 6px; box-shadow: 0 12px 30px rgba(0, 0, 0, .4); animation: bounce 1s cubic-bezier(.3, 1.6, .5, 1); }
.end-title { font-family: var(--display); font-size: clamp(44px, 8vw, 72px); margin: 10px 0 0; line-height: 1; }
.end-text { color: var(--muted); max-width: 32ch; font-size: 18px; margin: 10px 0 22px; }
@keyframes bounce { from { transform: scale(.4) rotate(-12deg); opacity: 0; } }
.end-stars { position: absolute; inset: -20px 0 auto; height: 100%; pointer-events: none; }
.end-stars i {
  position: absolute; top: 40%; left: 50%; width: 18px; height: 18px;
  background: var(--read);
  clip-path: polygon(50% 0, 61% 35%, 98% 35%, 68% 57%, 79% 91%, 50% 70%, 21% 91%, 32% 57%, 2% 35%, 39% 35%);
  animation: burst 1.4s cubic-bezier(.2, .8, .3, 1) forwards;
  animation-delay: calc(var(--d) * 40ms);
  opacity: 0;
  --a: calc(var(--d) * 30deg);
}
.end-stars i:nth-child(3n) { background: var(--listen); }
.end-stars i:nth-child(3n+1) { background: #ff8fb1; }
@keyframes burst {
  0% { opacity: 1; transform: translate(-50%, -50%) rotate(var(--a)) translateY(0) scale(.4); }
  70% { opacity: 1; }
  100% { opacity: 0; transform: translate(-50%, -50%) rotate(var(--a)) translateY(-220px) scale(1.1); }
}

/* Steuerleiste */
.controls {
  display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 12px;
  padding: 8px 14px 14px;
  max-width: 760px; width: 100%; margin: 0 auto;
}
.controls[hidden] { display: none; }
.controls-mid { display: flex; align-items: center; gap: 14px; justify-content: center; min-width: 0; }
.nav-btn, .play-btn {
  display: inline-grid; place-items: center; border: 0; cursor: pointer; color: inherit;
  transition: transform .12s ease, opacity .2s ease;
}
.nav-btn { width: 60px; height: 60px; border-radius: 20px; background: var(--surface); }
.nav-btn svg { width: 30px; height: 30px; }
.nav-btn:disabled { opacity: .3; cursor: default; }
.nav-btn:not(:disabled):active, .play-btn:active { transform: scale(.94); }
.play-btn { width: 64px; height: 64px; border-radius: 50%; background: var(--listen); color: var(--listen-text); box-shadow: 0 4px 0 rgba(0, 0, 0, .25); flex: none; }
.play-btn svg { width: 32px; height: 32px; }
.play-btn:disabled { opacity: .3; }
.play-btn .i-pause, .playing .play-btn .i-play { display: none; }
.playing .play-btn .i-pause { display: block; }
.playing .play-btn { background: var(--read); color: var(--read-text); }

.dots { display: flex; gap: 6px; flex-wrap: nowrap; justify-content: center; min-width: 0; }
.dots i { width: 9px; height: 9px; border-radius: 50%; background: var(--surface-2); transition: background .3s ease, transform .3s ease; }
.dots i.on { background: var(--accent); }

[data-view="end"] .controls .controls-mid { visibility: hidden; }
.no-audio .play-btn { display: none; }
.no-audio .sentence { cursor: inherit; }
.no-audio .sentence:hover { background: none; }
.book-actions .btn:only-child { grid-column: 1 / -1; }

.error { align-self: center; justify-self: center; font-size: 20px; color: var(--muted); }

@media (max-width: 480px) {
  .reader-title { font-size: 16px; }
  .nav-btn { width: 54px; height: 54px; }
  .stage { padding: 6px 10px; }
  .page-view { padding: 8px; }
  .controls { gap: 8px; }
  .controls-mid { gap: 10px; }
  .dots { gap: 4px; }
  .dots i { width: 7px; height: 7px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition: none !important; }
}
