/* ============================================================
   ЧудоСказ — PWA-читалка (T-09)
   Три темы, свайп-страницы, подсветка речи, нижний плеер.
   Никаких внешних шрифтов и CDN — читалка обязана работать офлайн.
   ============================================================ */

*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }

/* Классы вроде .player{display:flex} перебивают браузерное [hidden]{display:none}.
   Без этой строки плеер виден у сказок без озвучки, а кнопка установки — всегда. */
[hidden] { display: none !important; }

:root {
    --brand:      #7C3AED;
    --brand-2:    #EC4899;
    --gold:       #F59E0B;

    --bg:         #141131;
    --bg-2:       #1E1B4B;
    --fg:         #F3F0FF;
    --fg-soft:    #C4B5FD;
    --muted:      #A78BFA;

    --card:       rgba(255,255,255,.055);
    --card-line:  rgba(255,255,255,.12);
    --sheet:      #1B1740;
    --shadow:     0 24px 60px rgba(0,0,0,.45);

    --hl:         rgba(245,158,11,.22);
    --hl-fg:      #FFE9B8;

    --font:       Georgia, 'Iowan Old Style', 'Times New Roman', serif;
    --fs:         1.06rem;
    --lh:         1.85;

    --bar-h:      54px;
    --player-h:   66px;
    --radius:     20px;
}

[data-theme="day"] {
    --bg: #FFFBF5; --bg-2: #FFF6E9; --fg: #241F45; --fg-soft: #5B5480; --muted: #7C6FB0;
    --card: rgba(124,58,237,.05); --card-line: rgba(76,29,149,.14);
    --sheet: #FFFFFF; --shadow: 0 24px 60px rgba(76,29,149,.16);
    --hl: rgba(245,158,11,.3); --hl-fg: #4A2C00;
}

[data-theme="sepia"] {
    --bg: #F3E7D3; --bg-2: #EBDBC1; --fg: #3B2C1A; --fg-soft: #6B563C; --muted: #8A7250;
    --card: rgba(59,44,26,.05); --card-line: rgba(59,44,26,.16);
    --sheet: #FBF2E4; --shadow: 0 24px 60px rgba(59,44,26,.2);
    --hl: rgba(180,120,20,.26); --hl-fg: #3B2C1A;
}

[data-font="sans"] { --font: -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif; }

html, body { height: 100%; }

body {
    background: var(--bg);
    color: var(--fg);
    font-family: var(--font);
    overscroll-behavior: none;
    -webkit-text-size-adjust: 100%;
    -webkit-tap-highlight-color: transparent;
}

/* Звёздное небо — только в ночной теме, чистый CSS ради батареи */
/* overflow:hidden обязателен — слои ниже растянуты за края и иначе
   растягивают документ по горизонтали, ломая ширину страниц */
.sky { position: fixed; inset: 0; overflow: hidden; pointer-events: none; z-index: 0; opacity: 0; transition: opacity .6s; }
[data-theme="night"] .sky { opacity: 1; }
.sky::before, .sky::after {
    content: ''; position: absolute; inset: -50%;
    background-image:
        radial-gradient(1.4px 1.4px at 20% 30%, rgba(214,200,255,.85), transparent),
        radial-gradient(1.2px 1.2px at 70% 15%, rgba(255,235,190,.7), transparent),
        radial-gradient(1.6px 1.6px at 45% 70%, rgba(214,200,255,.7), transparent),
        radial-gradient(1.1px 1.1px at 85% 60%, rgba(236,72,153,.55), transparent),
        radial-gradient(1.3px 1.3px at 12% 82%, rgba(214,200,255,.6), transparent),
        radial-gradient(1px 1px at 60% 45%, rgba(255,255,255,.5), transparent);
    background-size: 260px 260px, 340px 340px, 300px 300px, 420px 420px, 380px 380px, 240px 240px;
    animation: twinkle 7s ease-in-out infinite alternate;
}
.sky::after { animation-delay: -3.5s; opacity: .55; transform: scale(1.3); }
@keyframes twinkle { from { opacity: .35; } to { opacity: .9; } }

/* ============================================================
   Каркас
   ============================================================ */
.reader {
    position: fixed; inset: 0;
    display: flex; flex-direction: column;
    z-index: 1;
}

/* ── Верхняя панель ───────────────────────────────────────── */
.bar {
    height: var(--bar-h);
    flex: 0 0 auto;
    display: flex; align-items: center; gap: 6px;
    padding: 0 6px 0 4px;
    padding-top: env(safe-area-inset-top);
    height: calc(var(--bar-h) + env(safe-area-inset-top));
    background: color-mix(in srgb, var(--bg) 82%, transparent);
    backdrop-filter: blur(14px);
    border-bottom: 1px solid var(--card-line);
    transition: transform .35s ease, opacity .35s ease;
    position: relative; z-index: 30;
}
.bar.is-hidden { transform: translateY(-100%); opacity: 0; pointer-events: none; }

.bar-title {
    flex: 1; min-width: 0;
    font-size: .95rem; font-weight: 700;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.bar-sub { display: block; font-family: system-ui, sans-serif; font-size: .68rem; color: var(--muted); font-weight: 400; }

.icon-btn {
    flex: 0 0 auto;
    width: 44px; height: 44px;
    display: inline-flex; align-items: center; justify-content: center;
    border: 0; border-radius: 14px;
    background: transparent; color: var(--fg);
    font-size: 1.15rem; cursor: pointer;
    transition: background .18s, transform .18s;
}
.icon-btn svg { width: 22px; height: 22px; display: block; }
.icon-btn:hover { background: var(--card); }
.icon-btn:active { transform: scale(.92); }
.icon-btn[aria-pressed="true"] { background: color-mix(in srgb, var(--brand) 30%, transparent); }
.icon-btn:focus-visible, .btn:focus-visible, .chip:focus-visible { outline: 2px solid var(--brand-2); outline-offset: 2px; }

/* Полоса прогресса чтения */
.readbar { position: absolute; left: 0; right: 0; bottom: -1px; height: 2px; background: transparent; }
.readbar i { display: block; height: 100%; width: 0; background: linear-gradient(90deg, var(--brand), var(--brand-2)); transition: width .35s ease; }

/* ── Страницы ─────────────────────────────────────────────── */
.pages {
    flex: 1 1 auto; min-height: 0;
    display: flex;
    overflow-x: auto; overflow-y: hidden;
    scroll-snap-type: x mandatory;
    scroll-behavior: smooth;
    scrollbar-width: none;
    overscroll-behavior-x: contain;
}
.pages::-webkit-scrollbar { display: none; }

.page {
    flex: 0 0 100%; width: 100%; height: 100%;
    scroll-snap-align: center; scroll-snap-stop: always;
    overflow-y: auto; overscroll-behavior-y: contain;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
    padding: 18px 18px 26px;
    display: flex; flex-direction: column; justify-content: center;
}
.page::-webkit-scrollbar { width: 6px; }
.page::-webkit-scrollbar-thumb { background: var(--card-line); border-radius: 3px; }

.page-inner {
    width: 100%; max-width: 1180px; margin: 0 auto;
    display: flex; flex-direction: column; align-items: center; gap: 18px;
}

/* Иллюстрация */
.illo {
    width: 100%; max-width: 460px;
    aspect-ratio: 1 / 1;
    border-radius: var(--radius);
    overflow: hidden;
    background: linear-gradient(135deg, var(--bg-2), color-mix(in srgb, var(--brand) 35%, var(--bg-2)));
    box-shadow: var(--shadow);
    flex: 0 0 auto;
    position: relative;
}
.illo img { width: 100%; height: 100%; object-fit: cover; display: block; will-change: transform; }
.illo.is-empty::after {
    content: '✨'; position: absolute; inset: 0;
    display: flex; align-items: center; justify-content: center; font-size: 3rem; opacity: .5;
}

/* Текст */
.text {
    width: 100%; max-width: 42rem;
    background: var(--card);
    border: 1px solid var(--card-line);
    border-radius: var(--radius);
    padding: 22px 24px;
}
.badge {
    display: block;
    font-family: system-ui, sans-serif; font-size: .68rem; font-weight: 700;
    letter-spacing: .18em; text-transform: uppercase;
    color: var(--muted); margin-bottom: 12px;
}
.body { font-size: var(--fs); line-height: var(--lh); }
.body p + p { margin-top: .9em; }
.body .dialog, .body em { color: var(--gold); font-style: italic; }

/* Подсветка читаемого предложения */
.s { transition: background-color .25s ease, color .25s ease; border-radius: 4px; }
.s.is-live {
    background: var(--hl); color: var(--hl-fg);
    box-shadow: 0 0 0 .16em var(--hl);
    -webkit-box-decoration-break: clone; box-decoration-break: clone;
}

/* ── Обложка и финал ──────────────────────────────────────── */
.cover, .finale { text-align: center; }
.cover .page-inner, .finale .page-inner { gap: 14px; }

.cover-art {
    width: min(300px, 62vw); aspect-ratio: 1/1;
    border-radius: 50%; overflow: hidden;
    border: 3px solid color-mix(in srgb, var(--brand) 55%, transparent);
    box-shadow: 0 0 50px color-mix(in srgb, var(--brand) 40%, transparent), var(--shadow);
    background: var(--bg-2);
    animation: float 5s ease-in-out infinite;
}
.cover-art img { width: 100%; height: 100%; object-fit: cover; }
@keyframes float { 0%,100% { transform: translateY(0); } 50% { transform: translateY(-10px); } }

.title {
    font-size: clamp(1.8rem, 6.5vw, 3.1rem); line-height: 1.12; font-weight: 700;
    background: linear-gradient(135deg, var(--gold), var(--brand-2), var(--muted));
    -webkit-background-clip: text; background-clip: text; -webkit-text-fill-color: transparent;
}
.subtitle { color: var(--fg-soft); font-style: italic; font-size: clamp(.95rem, 3.6vw, 1.15rem); }
.dedication { color: var(--fg-soft); font-style: italic; line-height: 1.7; max-width: 30rem; }
.the-end {
    font-size: clamp(1.7rem, 5.5vw, 2.6rem); font-weight: 700; letter-spacing: .35em;
    background: linear-gradient(135deg, var(--gold), var(--brand-2));
    -webkit-background-clip: text; background-clip: text; -webkit-text-fill-color: transparent;
}
.moral {
    font-family: system-ui, sans-serif; font-size: .92rem; line-height: 1.75;
    color: var(--muted); max-width: 30rem;
}

/* Кнопки */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 8px;
    min-height: 46px; padding: 12px 22px;
    border: 1px solid var(--card-line); border-radius: 999px;
    background: var(--card); color: var(--fg);
    font-family: system-ui, sans-serif; font-size: .92rem; font-weight: 600;
    text-decoration: none; cursor: pointer;
    transition: transform .18s, background .18s, box-shadow .18s;
}
.btn:hover { background: color-mix(in srgb, var(--brand) 18%, var(--card)); }
.btn:active { transform: scale(.97); }
.btn-primary {
    background: linear-gradient(135deg, var(--brand), var(--brand-2));
    border-color: transparent; color: #fff;
    box-shadow: 0 10px 30px color-mix(in srgb, var(--brand) 45%, transparent);
}
.btn-primary:hover { background: linear-gradient(135deg, var(--brand-2), var(--brand)); }
.btn-lg { min-height: 54px; padding: 15px 30px; font-size: 1rem; }
.btn[disabled] { opacity: .55; pointer-events: none; }
.actions { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; margin-top: 6px; }

/* Кольцо прогресса скачивания */
.ring {
    width: 20px; height: 20px; border-radius: 50%;
    background: conic-gradient(var(--gold) calc(var(--p, 0) * 1%), rgba(255,255,255,.18) 0);
    position: relative; flex: 0 0 auto;
}
.ring::after {
    content: ''; position: absolute; inset: 4px; border-radius: 50%; background: var(--sheet);
}

/* Стрелки листания (десктоп) */
.nav-arrow {
    position: absolute; top: 50%; transform: translateY(-50%);
    width: 52px; height: 52px; border-radius: 50%;
    display: none; align-items: center; justify-content: center;
    background: color-mix(in srgb, var(--bg) 70%, transparent);
    border: 1px solid var(--card-line); color: var(--fg);
    backdrop-filter: blur(8px); cursor: pointer; z-index: 20;
    font-size: 1.3rem; transition: background .2s, transform .2s;
}
.nav-arrow:hover { background: color-mix(in srgb, var(--brand) 35%, var(--bg)); }
.nav-arrow[hidden] { display: none !important; }
.nav-prev { left: 14px; } .nav-next { right: 14px; }
@media (hover: hover) and (min-width: 820px) { .nav-arrow { display: flex; } }

.stage { position: relative; flex: 1 1 auto; min-height: 0; display: flex; }

/* ── Нижний плеер ─────────────────────────────────────────── */
.player {
    flex: 0 0 auto;
    min-height: var(--player-h);
    padding: 8px 12px calc(8px + env(safe-area-inset-bottom));
    display: flex; align-items: center; gap: 10px;
    background: color-mix(in srgb, var(--bg) 86%, transparent);
    backdrop-filter: blur(14px);
    border-top: 1px solid var(--card-line);
    position: relative; z-index: 30;
    transition: transform .35s ease, opacity .35s ease;
}
.player.is-hidden { transform: translateY(100%); opacity: 0; pointer-events: none; }

.play-btn {
    width: 48px; height: 48px; border-radius: 50%; flex: 0 0 auto;
    border: 0; cursor: pointer; color: #fff; font-size: 1.1rem;
    background: linear-gradient(135deg, var(--brand), var(--brand-2));
    box-shadow: 0 8px 22px color-mix(in srgb, var(--brand) 45%, transparent);
    display: inline-flex; align-items: center; justify-content: center;
    transition: transform .18s;
}
.play-btn:active { transform: scale(.93); }
.play-btn svg { width: 24px; height: 24px; display: block; }

.track { flex: 1 1 auto; min-width: 0; }
.track-name {
    font-family: system-ui, sans-serif; font-size: .72rem; color: var(--muted);
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-bottom: 5px;
}
.seek {
    height: 16px; display: flex; align-items: center; cursor: pointer; touch-action: none;
}
.seek-line { width: 100%; height: 4px; border-radius: 2px; background: var(--card-line); position: relative; }
.seek-fill { position: absolute; inset: 0 auto 0 0; width: 0; border-radius: 2px; background: linear-gradient(90deg, var(--brand), var(--brand-2)); }
.seek-dot { position: absolute; top: 50%; left: 0; width: 12px; height: 12px; margin: -6px 0 0 -6px; border-radius: 50%; background: #fff; box-shadow: 0 2px 6px rgba(0,0,0,.4); opacity: 0; transition: opacity .2s; }
.seek:hover .seek-dot, .seek.is-active .seek-dot { opacity: 1; }
.time { font-family: system-ui, sans-serif; font-size: .7rem; color: var(--muted); flex: 0 0 auto; font-variant-numeric: tabular-nums; }

.chip {
    flex: 0 0 auto; min-height: 34px; padding: 6px 12px;
    border-radius: 999px; border: 1px solid var(--card-line);
    background: transparent; color: var(--muted); cursor: pointer;
    font-family: system-ui, sans-serif; font-size: .76rem; font-weight: 600;
}
.chip.is-on { background: color-mix(in srgb, var(--brand) 26%, transparent); color: var(--fg); border-color: transparent; }
.chip-hide-sm { display: none; }
@media (min-width: 560px) { .chip-hide-sm { display: inline-flex; align-items: center; } }

/* ── Шторки (настройки, содержание) ───────────────────────── */
.backdrop {
    position: fixed; inset: 0; z-index: 60;
    background: rgba(6,4,24,.55); backdrop-filter: blur(3px);
    opacity: 0; pointer-events: none; transition: opacity .28s;
}
.backdrop.is-open { opacity: 1; pointer-events: auto; }

.sheet {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 70;
    max-height: 82vh; overflow-y: auto;
    background: var(--sheet); color: var(--fg);
    border-radius: 24px 24px 0 0;
    border-top: 1px solid var(--card-line);
    box-shadow: var(--shadow);
    padding: 10px 18px calc(24px + env(safe-area-inset-bottom));
    transform: translateY(101%); transition: transform .32s cubic-bezier(.22,.85,.3,1);
}
.sheet.is-open { transform: translateY(0); }
@media (min-width: 720px) {
    .sheet { left: 50%; right: auto; width: 560px; transform: translate(-50%, 101%); border-radius: 24px 24px 0 0; }
    .sheet.is-open { transform: translate(-50%, 0); }
}
.sheet-grip { width: 42px; height: 4px; border-radius: 2px; background: var(--card-line); margin: 4px auto 14px; }
.sheet h2 { font-size: 1.05rem; margin-bottom: 14px; }
.row { padding: 12px 0; border-bottom: 1px solid var(--card-line); }
.row:last-child { border-bottom: 0; }
.row-label { font-family: system-ui, sans-serif; font-size: .82rem; color: var(--muted); margin-bottom: 9px; display: block; }
.seg { display: flex; gap: 8px; flex-wrap: wrap; }
.seg .chip { min-height: 40px; }
.switch-row { display: flex; align-items: center; justify-content: space-between; gap: 14px; }
.switch-row .row-label { margin: 0; color: var(--fg); font-size: .95rem; }
.switch-hint { display: block; font-size: .74rem; color: var(--muted); margin-top: 3px; }
.switch {
    flex: 0 0 auto; width: 52px; height: 30px; border-radius: 999px;
    border: 1px solid var(--card-line); background: var(--card); cursor: pointer; position: relative;
    transition: background .22s;
}
.switch::after {
    content: ''; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px;
    border-radius: 50%; background: var(--muted); transition: transform .22s, background .22s;
}
.switch[aria-checked="true"] { background: linear-gradient(135deg, var(--brand), var(--brand-2)); border-color: transparent; }
.switch[aria-checked="true"]::after { transform: translateX(22px); background: #fff; }

/* Содержание */
.toc { display: grid; grid-template-columns: repeat(auto-fill, minmax(126px, 1fr)); gap: 12px; }
.toc-item {
    border: 1px solid var(--card-line); background: var(--card);
    border-radius: 14px; overflow: hidden; cursor: pointer; text-align: left;
    color: var(--fg); font-family: system-ui, sans-serif; padding: 0;
    transition: transform .18s, border-color .18s;
}
.toc-item:hover { transform: translateY(-2px); border-color: var(--brand); }
.toc-item.is-current { border-color: var(--brand-2); box-shadow: 0 0 0 1px var(--brand-2) inset; }
.toc-thumb { width: 100%; aspect-ratio: 1/1; object-fit: cover; display: block; background: var(--bg-2); }
.toc-cap { display: block; padding: 8px 10px; font-size: .74rem; line-height: 1.35; }
.toc-num { display: block; font-size: .66rem; color: var(--muted); margin-bottom: 2px; }

/* ── Тост ─────────────────────────────────────────────────── */
.toast {
    position: fixed; left: 50%; bottom: calc(var(--player-h) + 22px + env(safe-area-inset-bottom));
    transform: translate(-50%, 16px); z-index: 90;
    background: var(--sheet); color: var(--fg);
    border: 1px solid var(--card-line); border-radius: 14px;
    padding: 11px 18px; font-family: system-ui, sans-serif; font-size: .84rem;
    box-shadow: var(--shadow); opacity: 0; pointer-events: none;
    transition: opacity .25s, transform .25s; max-width: min(92vw, 460px); text-align: center;
    display: flex; align-items: center; justify-content: center; gap: 12px; flex-wrap: wrap;
}
.toast.is-open { opacity: 1; transform: translate(-50%, 0); pointer-events: auto; }

/* ── Индикатор офлайна ────────────────────────────────────── */
.offline-flag {
    position: fixed; top: calc(var(--bar-h) + env(safe-area-inset-top) + 8px); left: 50%;
    transform: translateX(-50%); z-index: 40;
    background: var(--gold); color: #241900; border-radius: 999px;
    padding: 5px 14px; font-family: system-ui, sans-serif; font-size: .74rem; font-weight: 700;
    box-shadow: var(--shadow);
}
.offline-flag[hidden] { display: none; }

/* ── Мелочи ───────────────────────────────────────────────── */
.hint { font-family: system-ui, sans-serif; font-size: .8rem; color: var(--muted); line-height: 1.6; }
.sr-only {
    position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
    overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* Планшет/десктоп: картинка и текст рядом */
@media (min-width: 900px) {
    .page-inner { flex-direction: row; align-items: center; gap: 34px; }
    .page-inner > .illo { flex: 0 1 46%; max-width: 520px; }
    .page-inner > .text { flex: 1 1 54%; max-height: 82vh; overflow-y: auto; }
    .cover .page-inner, .finale .page-inner { flex-direction: column; }
    :root { --fs: 1.12rem; }
}

/* Низкие экраны в альбомной ориентации — картинка не должна съедать текст */
@media (orientation: landscape) and (max-height: 560px) and (max-width: 899px) {
    .page-inner { flex-direction: row; gap: 16px; align-items: center; }
    .page-inner > .illo { flex: 0 0 40%; max-width: 40%; }
    .page-inner > .text { flex: 1 1 60%; max-height: 78vh; overflow-y: auto; padding: 16px 18px; }
    .cover-art { width: min(190px, 34vw); }
}

@media (prefers-reduced-motion: reduce) {
    * { animation-duration: .001ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
    .pages { scroll-behavior: auto; }
}

/* Страница-заглушка «нет сети» */
.plain {
    position: static; min-height: 100dvh;
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    text-align: center; gap: 16px; padding: 32px 22px;
}
