/**
 * Interface Apple Books (iBooks) Minimalista & Responsiva
 * Otimizada especificamente para telas mínimas e monitores secundários
 */

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

html, body {
    width: 100%;
    height: 100%;
    overflow: hidden;
    font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background-color: var(--theme-bg);
    color: var(--theme-text);
    transition: background-color 0.3s cubic-bezier(0.16, 1, 0.3, 1), color 0.3s cubic-bezier(0.16, 1, 0.3, 1);
    user-select: none;
    -webkit-font-smoothing: antialiased;
}

/* Container Geral da Aplicação */
#app {
    width: 100%;
    height: 100%;
    position: relative;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

/* ==========================================================================
   1. TELA DE INÍCIO - ESTANTE BOOKY (Layout Roxo Noturno Fixo & Exclusivo)
   ========================================================================== */

:root {
    --home-bg: #0b0914;
    --home-shell-max: 1140px;
    --home-surface: #130f22;
    --home-surface-subtle: rgba(255, 255, 255, 0.03);
    --home-surface-hover: rgba(255, 255, 255, 0.06);
    --home-border: rgba(255, 255, 255, 0.08);
    --home-border-hover: rgba(168, 85, 247, 0.35);
    --home-primary: #7c3aed;
    --home-primary-hover: #6d28d9;
    --home-accent: #a855f7;
    --home-text: #f5f2fa;
    --home-text-muted: #8f889d;
    --home-text-dim: #686177;
    --home-heading: #ffffff;
    --home-book-shadow: 0 4px 14px -2px rgba(0, 0, 0, 0.55), 0 1px 3px rgba(0, 0, 0, 0.35);
    --home-book-shadow-hover: 0 14px 30px -4px rgba(0, 0, 0, 0.75), 0 2px 8px rgba(0, 0, 0, 0.4);
}

/* Garante fundo obsidian escuro na visualização da estante */
body[data-view="library"],
body:not([data-view="reader"]) {
    background-color: var(--home-bg) !important;
    color: var(--home-text) !important;
}

#library-view {
    position: absolute;
    inset: 0;
    z-index: 10;
    background-color: var(--home-bg);
    color: var(--home-text);
    overflow-y: auto;
    overflow-x: hidden;
    display: flex;
    flex-direction: column;
    padding-top: max(20px, env(safe-area-inset-top, 0px));
    padding-bottom: max(32px, env(safe-area-inset-bottom, 0px));
    padding-left: max(20px, env(safe-area-inset-left, 0px));
    padding-right: max(20px, env(safe-area-inset-right, 0px));
    box-sizing: border-box;
    transition: opacity 0.28s ease, transform 0.28s cubic-bezier(0.16, 1, 0.3, 1);
}

#library-view.hidden {
    opacity: 0;
    transform: scale(0.98);
    pointer-events: none !important;
    display: none !important;
}

.library-shell {
    width: 100%;
    max-width: var(--home-shell-max);
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    flex: 1;
}

/* Cabeçalho Limpo Desktop / Mobile */
.library-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 6px 0 20px 0;
    border-bottom: 1px solid var(--home-border);
    margin-bottom: 24px;
    gap: 16px;
    flex-wrap: nowrap;
}

.library-header-left,
.library-header-right {
    display: flex;
    align-items: center;
    gap: 12px;
    flex-shrink: 0;
}

.library-header-center {
    flex: 1;
    display: flex;
    justify-content: center;
    max-width: 420px;
    margin: 0 12px;
}

.brand-group {
    display: flex;
    align-items: center;
    gap: 10px;
    user-select: none;
}

/* Logo e Branding Booky */
.app-logo-img {
    width: 32px;
    height: 32px;
    border-radius: 8px;
    object-fit: cover;
    border: 1px solid rgba(255, 255, 255, 0.12);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.35);
    flex-shrink: 0;
}

.brand-text {
    display: flex;
    align-items: center;
    gap: 8px;
}

.app-brand-name {
    font-family: -apple-system, BlinkMacSystemFont, "SF Pro Display", "Inter", "Segoe UI", sans-serif;
    font-size: 1.25rem;
    color: var(--home-heading);
    letter-spacing: -0.025em;
    font-weight: 700;
    line-height: 1;
}

.brand-pill {
    font-size: 0.65rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    padding: 2px 7px;
    border-radius: 99px;
    background: rgba(139, 92, 246, 0.12);
    color: #c4b5fd;
    border: 1px solid rgba(139, 92, 246, 0.22);
}

/* Campo de Busca Rápida na Biblioteca */
.library-search-box {
    position: relative;
    display: flex;
    align-items: center;
    width: 100%;
    background: rgba(255, 255, 255, 0.035);
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 10px;
    padding: 0 10px;
    height: 36px;
    transition: all 0.18s ease;
}

.library-search-box:hover {
    background: rgba(255, 255, 255, 0.05);
    border-color: rgba(255, 255, 255, 0.14);
}

.library-search-box:focus-within {
    background: rgba(20, 15, 34, 0.95);
    border-color: rgba(168, 85, 247, 0.5);
    box-shadow: 0 0 0 3px rgba(139, 92, 246, 0.15);
}

.library-search-box .search-icon {
    width: 15px;
    height: 15px;
    color: var(--home-text-dim);
    flex-shrink: 0;
}

.library-search-box input {
    width: 100%;
    background: transparent;
    border: none;
    outline: none;
    font-size: 0.82rem;
    color: var(--home-text);
    padding: 0 8px;
    font-family: inherit;
}

.library-search-box input::placeholder {
    color: var(--home-text-dim);
}

.search-clear-btn {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.08);
    border: none;
    color: var(--home-text-muted);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    flex-shrink: 0;
    transition: all 0.15s ease;
}

.search-clear-btn:hover {
    background: rgba(255, 255, 255, 0.16);
    color: #ffffff;
}

.search-clear-btn [data-lucide] {
    width: 12px;
    height: 12px;
}

/* Regra Global para Ícones Lucide SVG */
[data-lucide],
svg.lucide {
    width: 18px;
    height: 18px;
    stroke-width: 2px;
    stroke: currentColor;
    fill: none;
    display: inline-block;
    vertical-align: middle;
    flex-shrink: 0;
}

/* Botão de Ação Primária da Biblioteca & Ações Gerais */
.btn-library-action,
.btn-apple {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    padding: 0 14px;
    height: 36px;
    border-radius: 9px;
    background: var(--home-primary);
    color: #ffffff;
    border: 1px solid rgba(255, 255, 255, 0.15);
    font-size: 0.8rem;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s ease, transform 0.12s ease, box-shadow 0.15s ease;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.35);
    white-space: nowrap;
    flex-shrink: 0;
}

.btn-library-action:hover,
.btn-apple:hover {
    background: var(--home-primary-hover);
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(124, 58, 237, 0.35);
}

.btn-library-action:active,
.btn-apple:active {
    transform: translateY(0);
}

.btn-library-action [data-lucide],
.btn-apple [data-lucide] {
    width: 15px;
    height: 15px;
}

/* Barra de Subtítulo e Contagem */
.library-subbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 22px;
}

.subbar-left {
    display: flex;
    align-items: center;
    gap: 8px;
}

.subbar-heading {
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--home-text);
    letter-spacing: -0.01em;
}

.subbar-count {
    font-size: 0.7rem;
    font-weight: 500;
    color: var(--home-text-muted);
    background: var(--home-surface-subtle);
    padding: 2px 7px;
    border-radius: 6px;
    border: 1px solid var(--home-border);
    font-variant-numeric: tabular-nums;
}

.subbar-tip {
    font-size: 0.72rem;
    color: var(--home-text-dim);
    display: inline-flex;
    align-items: center;
    gap: 5px;
}

.subbar-tip [data-lucide] {
    width: 13px;
    height: 13px;
}

/* Grade de Livros Estilo Apple Books */
.books-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
    gap: 28px 22px;
    margin-bottom: 32px;
}

/* Card Individual de Livro */
.book-card {
    display: flex;
    flex-direction: column;
    cursor: pointer;
    position: relative;
    user-select: none;
    transition: transform 0.2s cubic-bezier(0.16, 1, 0.3, 1);
    background: transparent;
}

.book-card:hover {
    transform: translateY(-4px);
}

/* Contêiner da Capa com visual de livro físico refinado */
.book-cover-container {
    position: relative;
    width: 100%;
    aspect-ratio: 1 / 1.48;
    border-radius: 7px;
    overflow: hidden;
    background: var(--home-surface);
    border: 1px solid var(--home-border);
    box-shadow: var(--home-book-shadow);
    transition: box-shadow 0.2s ease, border-color 0.2s ease;
}

/* Relevo de lombada físico suave */
.book-cover-container::after {
    content: "";
    position: absolute;
    inset: 0;
    pointer-events: none;
    box-shadow: inset 3px 0 6px -1px rgba(0, 0, 0, 0.45);
    border-radius: 7px;
}

.book-card:hover .book-cover-container {
    box-shadow: var(--home-book-shadow-hover);
    border-color: var(--home-border-hover);
}

.book-card.last-read-card .book-cover-container {
    border-color: rgba(168, 85, 247, 0.45);
}

.book-cover-img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
    border-radius: 6px;
    transition: transform 0.2s ease;
}

.book-card:hover .book-cover-img {
    transform: scale(1.015);
}

/* Badge discreto de progresso no canto do card */
.book-progress-badge {
    position: absolute;
    top: 8px;
    right: 8px;
    background: rgba(11, 9, 20, 0.88);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    border: 1px solid rgba(255, 255, 255, 0.12);
    color: #e9d5ff;
    font-size: 0.65rem;
    font-weight: 600;
    padding: 2px 7px;
    border-radius: 6px;
    z-index: 2;
    display: inline-flex;
    align-items: center;
    gap: 4px;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.4);
}

.book-progress-badge [data-lucide] {
    width: 10px;
    height: 10px;
}

.book-new-badge {
    position: absolute;
    top: 8px;
    right: 8px;
    background: rgba(124, 58, 237, 0.35);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    border: 1px solid rgba(168, 85, 247, 0.3);
    color: #f5f2fa;
    font-size: 0.65rem;
    font-weight: 600;
    padding: 2px 7px;
    border-radius: 6px;
    z-index: 2;
    display: inline-flex;
    align-items: center;
    gap: 4px;
}

.book-new-badge [data-lucide] {
    width: 10px;
    height: 10px;
}

/* Barra de progresso integrada no rodapé da capa */
.book-cover-progress-bar {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 3px;
    background: rgba(0, 0, 0, 0.45);
    z-index: 3;
}

.book-cover-progress-fill {
    height: 100%;
    background: var(--home-accent);
}

/* Botão de Excluir discreto */
.book-delete-btn {
    position: absolute;
    top: 8px;
    left: 8px;
    width: 24px;
    height: 24px;
    border-radius: 6px;
    background: rgba(11, 9, 20, 0.88);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    border: 1px solid rgba(255, 255, 255, 0.12);
    color: #f87171;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    z-index: 4;
    opacity: 0;
    transform: scale(0.9);
    transition: all 0.15s ease;
}

.book-card:hover .book-delete-btn {
    opacity: 1;
    transform: scale(1);
}

.book-delete-btn:hover {
    background: #dc2626;
    color: #ffffff;
    border-color: #ef4444;
}

.book-delete-btn [data-lucide] {
    width: 12px;
    height: 12px;
}

/* Informações do Livro */
.book-card-info {
    margin-top: 10px;
    padding: 0 1px;
}

.book-card-title {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--home-heading);
    line-height: 1.3;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    text-overflow: ellipsis;
    letter-spacing: -0.01em;
}

.book-card-author {
    font-size: 0.72rem;
    color: var(--home-text-muted);
    margin-top: 3px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Faixa Discreta de Upload no Rodapé */
.library-drop-strip {
    margin-top: auto;
    padding: 12px 18px;
    border: 1px dashed var(--home-border);
    border-radius: 10px;
    background: var(--home-surface-subtle);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.18s ease;
}

.library-drop-strip:hover,
.library-drop-strip.dragover {
    background: rgba(124, 58, 237, 0.08);
    border-color: rgba(168, 85, 247, 0.4);
}

.drop-strip-content {
    display: flex;
    align-items: center;
    gap: 8px;
    color: var(--home-text-muted);
    font-size: 0.76rem;
}

.drop-strip-icon {
    width: 16px;
    height: 16px;
    color: var(--home-accent);
}

.drop-strip-text strong {
    color: var(--home-text);
    font-weight: 600;
}

/* Fullscreen Window Drop Overlay */
.window-drop-overlay {
    position: fixed;
    inset: 0;
    background: rgba(9, 7, 18, 0.85);
    backdrop-filter: blur(14px);
    -webkit-backdrop-filter: blur(14px);
    z-index: 999;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s cubic-bezier(0.16, 1, 0.3, 1);
}

.window-drop-overlay.active {
    opacity: 1;
    pointer-events: auto;
}

.window-drop-card {
    padding: 36px 44px;
    border-radius: 18px;
    border: 1.5px dashed rgba(168, 85, 247, 0.55);
    background: rgba(19, 15, 34, 0.95);
    text-align: center;
    box-shadow: 0 20px 48px rgba(0, 0, 0, 0.8);
    display: flex;
    flex-direction: column;
    align-items: center;
    transform: scale(0.96);
    transition: transform 0.2s cubic-bezier(0.16, 1, 0.3, 1);
}

.window-drop-overlay.active .window-drop-card {
    transform: scale(1);
}

.drop-card-icon {
    width: 52px;
    height: 52px;
    border-radius: 14px;
    background: rgba(124, 58, 237, 0.16);
    border: 1px solid rgba(168, 85, 247, 0.3);
    color: #c4b5fd;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 14px;
}

.drop-card-icon [data-lucide] {
    width: 26px;
    height: 26px;
}

.drop-card-title {
    font-size: 1.15rem;
    font-weight: 600;
    color: #ffffff;
}

.drop-card-subtitle {
    font-size: 0.8rem;
    color: var(--home-text-muted);
    margin-top: 4px;
}

/* Estado de Biblioteca Vazia */
.empty-library-container {
    grid-column: 1 / -1;
    text-align: center;
    padding: 60px 20px;
    display: flex;
    flex-direction: column;
    align-items: center;
    max-width: 420px;
    margin: 0 auto;
}

.empty-icon-lucide {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 52px;
    height: 52px;
    border-radius: 14px;
    background: rgba(255, 255, 255, 0.04);
    color: #c4b5fd;
    margin-bottom: 14px;
    border: 1px solid var(--home-border);
}

.empty-icon-lucide [data-lucide] {
    width: 24px;
    height: 24px;
}

.empty-title {
    font-family: inherit;
    font-size: 1.1rem;
    font-weight: 600;
    color: var(--home-heading);
    letter-spacing: -0.01em;
}

.empty-sub {
    font-size: 0.8rem;
    color: var(--home-text-muted);
    margin-top: 6px;
    line-height: 1.5;
}

.btn-subtle {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 6px 14px;
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid var(--home-border);
    color: var(--home-text);
    font-size: 0.78rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.15s ease;
}

.btn-subtle:hover {
    background: rgba(255, 255, 255, 0.1);
}

.popover-footer-shortcuts {
    font-size: 0.68rem;
    color: var(--theme-muted);
    text-align: center;
    margin-top: 4px;
}

#reader-view {
    position: absolute;
    inset: 0;
    z-index: 20;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background-color: var(--theme-bg);
}

#reader-view.hidden {
    display: none !important;
}

/* Header Flutuante Estilo iBooks */
.reader-header {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: calc(48px + env(safe-area-inset-top, 0px));
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding-top: env(safe-area-inset-top, 0px);
    padding-left: max(12px, env(safe-area-inset-left, 0px));
    padding-right: max(12px, env(safe-area-inset-right, 0px));
    background: var(--theme-glass);
    border-bottom: 1px solid var(--theme-glass-border);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    z-index: 200 !important;
    transition: transform 0.28s cubic-bezier(0.16, 1, 0.3, 1), opacity 0.28s ease;
    pointer-events: auto;
}

.reader-header.autohide {
    transform: translateY(-100%);
    opacity: 0;
    pointer-events: none;
}

.reader-header-left,
.reader-header-right {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-shrink: 0;
    z-index: 210;
}

.reader-header-center {
    flex: 1;
    min-width: 0;
    text-align: center;
    padding: 0 10px;
    cursor: pointer;
}

.reader-header-title {
    font-size: 0.82rem;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    color: var(--theme-heading);
}

.reader-header-chapter {
    font-size: 0.68rem;
    color: var(--theme-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Botões do Header */
.nav-btn {
    min-width: 32px;
    height: 32px;
    padding: 0 8px;
    border-radius: 8px;
    background: transparent;
    border: none;
    color: var(--theme-text);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background 0.15s ease, transform 0.1s ease;
    pointer-events: auto !important;
    user-select: none;
    position: relative;
    z-index: 220;
}

.nav-btn .btn-label {
    font-size: 0.78rem;
    font-weight: 500;
    margin-left: 4px;
}

#btn-library {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 5px 12px;
    border-radius: 16px;
    background: rgba(139, 92, 246, 0.14);
    border: 1px solid rgba(139, 92, 246, 0.28);
    color: var(--theme-heading);
    font-weight: 600;
    font-size: 0.8rem;
    transition: all 0.18s ease;
}

#btn-library:hover {
    background: rgba(139, 92, 246, 0.26);
    border-color: rgba(168, 85, 247, 0.5);
    transform: translateX(-2px);
    box-shadow: 0 2px 10px rgba(139, 92, 246, 0.25);
}

.nav-btn:hover {
    background: rgba(120, 105, 87, 0.14);
}

.nav-btn:active {
    transform: scale(0.94);
}

.nav-btn.active {
    color: var(--theme-accent);
}

.nav-btn svg {
    width: 17px;
    height: 17px;
    fill: currentColor;
    pointer-events: none;
}

/* Área de Leitura Principal */
#reader-container {
    flex: 1;
    width: 100%;
    height: 100%;
    position: relative;
    overflow: hidden;
    display: flex;
    justify-content: center;
    align-items: stretch;
}

/* Frame do epub.js ou Leitor Nativo */
#epub-viewer {
    width: 100%;
    height: 100%;
    position: absolute;
    inset: 0;
}

/* Modo Leitor Nativo com Rolagem ou Paginação */
#native-viewer {
    width: 100%;
    height: 100%;
    position: absolute;
    inset: 0;
    overflow-y: auto;
    overflow-x: hidden;
    scroll-behavior: smooth;
    display: flex;
    justify-content: center;
}

.reading-canvas {
    width: 100%;
    max-width: 680px;
    padding-top: calc(58px + env(safe-area-inset-top, 0px));
    padding-bottom: calc(56px + env(safe-area-inset-bottom, 0px));
    padding-left: max(20px, env(safe-area-inset-left, 0px));
    padding-right: max(20px, env(safe-area-inset-right, 0px));
    margin: 0 auto;
    user-select: text;
}

/* Zonas de Toque Desativadas - Cliques tratados diretamente no leitor */
.touch-zone {
    display: none !important;
}

/* Footer / Barra de Progresso Flutuante */
.reader-footer {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: calc(44px + env(safe-area-inset-bottom, 0px));
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding-bottom: env(safe-area-inset-bottom, 0px);
    padding-left: max(14px, env(safe-area-inset-left, 0px));
    padding-right: max(14px, env(safe-area-inset-right, 0px));
    background: var(--theme-glass);
    border-top: 1px solid var(--theme-glass-border);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    z-index: 200 !important;
    transition: transform 0.28s cubic-bezier(0.16, 1, 0.3, 1), opacity 0.28s ease;
    pointer-events: auto;
}

.reader-footer.autohide {
    transform: translateY(100%);
    opacity: 0;
    pointer-events: none;
}

.progress-info {
    font-size: 0.72rem;
    color: var(--theme-muted);
    font-variant-numeric: tabular-nums;
    min-width: 65px;
}

.progress-bar-wrapper {
    flex: 1;
    margin: 0 14px;
    position: relative;
    height: 16px;
    display: flex;
    align-items: center;
    cursor: pointer;
}

.progress-track {
    width: 100%;
    height: 3px;
    background: var(--theme-border);
    border-radius: 2px;
    overflow: hidden;
    position: relative;
}

.progress-fill {
    height: 100%;
    width: 0%;
    background: var(--theme-accent);
    border-radius: 2px;
    transition: width 0.15s ease;
}

.footer-nav-btns {
    display: flex;
    gap: 4px;
}

.footer-btn {
    width: 28px;
    height: 28px;
    border-radius: 6px;
    border: none;
    background: transparent;
    color: var(--theme-text);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
}

.footer-btn:hover {
    background: rgba(120, 105, 87, 0.12);
}

.footer-btn svg {
    width: 14px;
    height: 14px;
    fill: currentColor;
}

/* ==========================================================================
   3. MENUS FLUTUANTES / MODAIS (TOC & APARENCIA "Aa")
   ========================================================================== */

.panel-overlay {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
    z-index: 300 !important;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s ease;
}

.panel-overlay.open {
    opacity: 1;
    pointer-events: auto;
}

/* Drawer de Sumário (TOC) */
.toc-drawer {
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    width: 320px;
    max-width: 85vw;
    background: var(--theme-paper);
    border-right: 1px solid var(--theme-border);
    box-shadow: 4px 0 24px rgba(0, 0, 0, 0.2);
    z-index: 350 !important;
    transform: translateX(-100%);
    transition: transform 0.28s cubic-bezier(0.16, 1, 0.3, 1);
    display: flex;
    flex-direction: column;
}

.toc-drawer.open {
    transform: translateX(0);
}

.toc-header {
    height: 52px;
    padding: 0 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid var(--theme-border);
}

.toc-header h2 {
    font-family: -apple-system, BlinkMacSystemFont, "SF Pro Display", "Inter", sans-serif;
    font-size: 1rem;
    font-weight: 600;
    color: var(--theme-heading);
    letter-spacing: -0.01em;
}

.toc-list {
    flex: 1;
    overflow-y: auto;
    padding: 8px 0;
    list-style: none;
}

.toc-item {
    padding: 10px 16px;
    font-size: 0.85rem;
    color: var(--theme-text);
    cursor: pointer;
    border-bottom: 1px solid rgba(120, 105, 87, 0.08);
    transition: background 0.15s ease, color 0.15s ease;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.toc-item:hover {
    background: rgba(180, 83, 9, 0.08);
    color: var(--theme-accent);
}

.toc-item.active {
    font-weight: 600;
    color: var(--theme-accent);
    background: rgba(180, 83, 9, 0.12);
    border-left: 3px solid var(--theme-accent);
}

/* Popover de Presets & Aparência (Aa) */
.appearance-popover {
    position: absolute;
    top: 54px;
    right: 12px;
    width: 320px;
    max-width: calc(100vw - 20px);
    background: var(--theme-paper);
    border: 1px solid var(--theme-glass-border);
    border-radius: 16px;
    box-shadow: 0 16px 36px rgba(0, 0, 0, 0.4), 0 0 0 1px var(--theme-border);
    padding: 16px;
    z-index: 350 !important;
    display: none;
    flex-direction: column;
    gap: 13px;
    animation: popIn 0.2s cubic-bezier(0.16, 1, 0.3, 1);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
}

.appearance-popover.open {
    display: flex;
}

@keyframes popIn {
    from { opacity: 0; transform: translateY(-8px) scale(0.96); }
    to { opacity: 1; transform: translateY(0) scale(1); }
}

.sheet-drag-handle {
    display: none;
    width: 38px;
    height: 4px;
    background: var(--theme-border);
    border-radius: 999px;
    margin: 0 auto 8px auto;
    opacity: 0.6;
}

.popover-section {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.popover-row-title {
    font-size: 0.7rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--theme-muted);
}

/* 1. Presets / Packs de Leitura em 1 Clique */
.presets-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 6px;
}

.preset-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 5px;
    padding: 9px 4px;
    border-radius: 10px;
    background: rgba(139, 92, 246, 0.08);
    border: 1px solid rgba(139, 92, 246, 0.18);
    color: var(--theme-muted);
    font-size: 0.72rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.18s cubic-bezier(0.16, 1, 0.3, 1);
    user-select: none;
}

.preset-card:hover {
    background: rgba(139, 92, 246, 0.18);
    color: var(--theme-heading);
    transform: translateY(-2px);
    border-color: rgba(139, 92, 246, 0.38);
}

.preset-card.active {
    background: linear-gradient(135deg, rgba(139, 92, 246, 0.32) 0%, rgba(168, 85, 247, 0.2) 100%);
    border-color: var(--theme-accent);
    color: var(--theme-heading);
    font-weight: 700;
    box-shadow: 0 0 14px var(--theme-accent-glow);
}

.preset-card [data-lucide],
.preset-card svg.lucide {
    width: 17px;
    height: 17px;
}

/* Seletor de Temas (Swatches) */
.theme-swatches {
    display: flex;
    gap: 6px;
    justify-content: space-between;
}

.theme-swatch {
    flex: 1;
    height: 30px;
    border-radius: 7px;
    border: 2px solid transparent;
    cursor: pointer;
    position: relative;
    transition: transform 0.15s ease, border-color 0.15s ease, box-shadow 0.15s ease;
}

.theme-swatch:hover {
    transform: scale(1.08);
}

.theme-swatch.active {
    border-color: var(--theme-accent);
    box-shadow: 0 0 0 2px var(--theme-border), 0 0 8px var(--theme-accent-glow);
}

.swatch-booky-dark { background: #130e24; border: 2px solid rgba(139, 92, 246, 0.5); }
.swatch-cyber { background: #080c14; border: 2px solid rgba(6, 182, 212, 0.5); }
.swatch-parchment { background: #f5ecd7; }
.swatch-night { background: #09090b; }
.swatch-sepia { background: #f7efe1; }
.swatch-white { background: #ffffff; border: 1px solid #d1d5db; }
.swatch-valyrian { background: #0d1514; border: 2px solid rgba(16, 185, 129, 0.5); }

/* Ajuste de Tamanho da Fonte (A - A+) */
.font-size-stepper {
    display: flex;
    align-items: center;
    background: rgba(120, 105, 87, 0.1);
    border-radius: 10px;
    padding: 4px;
}

.font-step-btn {
    flex: 1;
    height: 30px;
    border: none;
    background: transparent;
    color: var(--theme-text);
    font-weight: 600;
    cursor: pointer;
    border-radius: 8px;
    transition: background 0.15s ease;
}

.font-step-btn:hover {
    background: rgba(120, 105, 87, 0.15);
}

.font-step-indicator {
    padding: 0 10px;
    font-size: 0.8rem;
    color: var(--theme-muted);
}

/* Seletor de Fontes */
.font-family-select {
    width: 100%;
    padding: 7px 10px;
    border-radius: 8px;
    background: rgba(120, 105, 87, 0.1);
    border: 1px solid var(--theme-border);
    color: var(--theme-text);
    font-size: 0.82rem;
    outline: none;
    cursor: pointer;
}

/* Switches / Toggles */
.toggle-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 0.8rem;
    color: var(--theme-text);
}

.toggle-switch {
    position: relative;
    width: 36px;
    height: 20px;
    cursor: pointer;
}

.toggle-switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.toggle-slider {
    position: absolute;
    inset: 0;
    background: rgba(120, 105, 87, 0.3);
    border-radius: 20px;
    transition: 0.2s cubic-bezier(0.16, 1, 0.3, 1);
}

.toggle-slider::before {
    position: absolute;
    content: "";
    height: 16px;
    width: 16px;
    left: 2px;
    bottom: 2px;
    background: white;
    border-radius: 50%;
    transition: 0.2s cubic-bezier(0.16, 1, 0.3, 1);
}

.toggle-switch input:checked + .toggle-slider {
    background: var(--theme-accent);
}

.toggle-switch input:checked + .toggle-slider::before {
    transform: translateX(16px);
}

/* ==========================================================================
   4. OTIMIZAÇÃO CRÍTICA PARA TELAS MÍNIMAS E MOBILE PWA
   ========================================================================== */

/* Responsividade da Biblioteca em Telas Médias e Mobile */
@media (max-width: 680px) {
    .library-header {
        flex-wrap: wrap;
        gap: 12px;
        padding-bottom: 14px;
        margin-bottom: 18px;
    }

    .library-header-center {
        order: 3;
        max-width: 100%;
        width: 100%;
        margin: 0;
    }

    .library-subbar {
        margin-bottom: 16px;
    }

    .subbar-tip {
        display: none;
    }

    .books-grid {
        grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
        gap: 18px 14px;
    }
}

/* Bottom Sheet no Mobile para o Popover de Aparência */
@media (max-width: 520px) {
    .sheet-drag-handle {
        display: block;
    }

    .appearance-popover {
        position: fixed !important;
        top: auto !important;
        bottom: 0 !important;
        left: 0 !important;
        right: 0 !important;
        width: 100vw !important;
        max-width: 100vw !important;
        border-radius: 22px 22px 0 0 !important;
        border-left: none !important;
        border-right: none !important;
        border-bottom: none !important;
        padding: 12px 18px calc(24px + env(safe-area-inset-bottom, 0px)) 18px !important;
        box-shadow: 0 -12px 48px rgba(0, 0, 0, 0.75) !important;
        max-height: 84vh !important;
        overflow-y: auto !important;
        animation: sheetSlideUp 0.28s cubic-bezier(0.16, 1, 0.3, 1) !important;
        z-index: 400 !important;
    }

    @keyframes sheetSlideUp {
        from { transform: translateY(100%); opacity: 0; }
        to { transform: translateY(0); opacity: 1; }
    }

    .theme-swatch {
        width: 36px;
        height: 36px;
        min-width: 36px;
    }

    .font-step-btn {
        min-height: 42px;
        min-width: 42px;
    }
}

/* Quando a janela tem menos de 480px de largura */
@media (max-width: 480px) {
    .reader-header {
        height: calc(44px + env(safe-area-inset-top, 0px));
        padding-top: env(safe-area-inset-top, 0px);
        padding-left: max(8px, env(safe-area-inset-left, 0px));
        padding-right: max(8px, env(safe-area-inset-right, 0px));
    }

    .reader-footer {
        height: calc(42px + env(safe-area-inset-bottom, 0px));
        padding-bottom: env(safe-area-inset-bottom, 0px);
        padding-left: max(8px, env(safe-area-inset-left, 0px));
        padding-right: max(8px, env(safe-area-inset-right, 0px));
    }

    .nav-btn {
        min-width: 36px;
        height: 36px;
    }

    .nav-btn svg {
        width: 17px;
        height: 17px;
    }

    /* Reduz padding da área de leitura para aproveitar cada pixel */
    .reading-canvas {
        padding-top: calc(50px + env(safe-area-inset-top, 0px));
        padding-bottom: calc(50px + env(safe-area-inset-bottom, 0px));
        padding-left: max(14px, env(safe-area-inset-left, 0px));
        padding-right: max(14px, env(safe-area-inset-right, 0px));
        max-width: 100%;
    }

    .reader-article h1 {
        font-size: 1.45em;
        margin-top: 1.2em;
    }

    .reader-article p {
        text-indent: 1.2em;
        margin-bottom: 0.7em;
    }

    .books-grid {
        grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
        gap: 16px 12px;
    }

    .book-card-title {
        font-size: 0.78rem;
    }

    .book-card-author {
        font-size: 0.68rem;
    }

    .appearance-popover {
        right: 4px;
        width: calc(100vw - 8px);
        max-width: 290px;
    }
}

/* Quando a janela é ultra-pequena (< 340px) */
@media (max-width: 340px) {
    #library-view {
        padding: 14px 10px 24px;
    }

    .library-header {
        margin-bottom: 14px;
        padding-bottom: 10px;
    }

    .app-brand-name {
        font-size: 1.1rem;
    }

    .brand-pill {
        display: none;
    }

    .btn-library-action {
        padding: 0 10px;
        font-size: 0.72rem;
        height: 32px;
    }

    .btn-library-action span {
        display: none;
    }

    .books-grid {
        grid-template-columns: repeat(auto-fill, minmax(90px, 1fr));
        gap: 12px 8px;
        margin-bottom: 18px;
    }

    .book-card-title {
        font-size: 0.74rem;
    }

    .book-card-author {
        font-size: 0.65rem;
    }

    .reader-header-chapter {
        display: none;
    }

    .reader-header-title {
        font-size: 0.75rem;
    }

    .reading-canvas {
        padding: 44px 8px 44px 8px;
    }

    .progress-info {
        font-size: 0.65rem;
        min-width: 45px;
    }

    .footer-nav-btns {
        display: none; /* Usa setas do teclado ou toques laterais */
    }
}

/* Modo Ultra-Compacto (Mini-Mode) Ativado Manualmente */
body.mini-mode .reader-header,
body.mini-mode .reader-footer {
    opacity: 0;
    pointer-events: none;
    transform: translateY(-100%);
}

body.mini-mode .reader-footer {
    transform: translateY(100%);
}

body.mini-mode .reading-canvas {
    padding: 10px 10px 10px 10px;
}

/* Botão Flutuante Discreto de Controle no Mini-Mode */
.mini-mode-fab {
    position: absolute;
    top: 8px;
    right: 8px;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    background: var(--theme-glass);
    border: 1px solid var(--theme-glass-border);
    color: var(--theme-muted);
    display: none;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    z-index: 60;
    opacity: 0.35;
    transition: opacity 0.2s ease, transform 0.2s ease;
}

.mini-mode-fab:hover {
    opacity: 1;
    transform: scale(1.1);
}

body.mini-mode .mini-mode-fab {
    display: flex;
}
