/* ============================================================
   Library Assistant — split-screen AI book concierge
   Floating bubble → splits the screen into Chat + Library.
   ============================================================ */

:root {
    --la-width: 420px;
    --la-accent: #2563eb;
    --la-accent-2: #6366f1;
    --la-header-grad: linear-gradient(135deg, #2563eb, #6366f1);
    --la-radius: 16px;
}

/* ===== Floating launcher bubble ===== */
.la-bubble {
    position: fixed;
    bottom: 24px;
    inset-inline-end: 24px;
    width: 104px;
    height: 104px;
    padding: 0;
    border-radius: 50%;
    border: none;
    cursor: pointer;
    z-index: 940;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--la-header-grad);
    box-shadow: 0 10px 28px rgba(37, 99, 235, 0.38), 0 2px 8px rgba(0, 0, 0, 0.14);
    color: #fff;
    transition: transform 0.25s cubic-bezier(0.34, 1.56, 0.64, 1), box-shadow 0.25s, opacity 0.2s;
    animation: la-bubble-in 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.la-bubble:hover {
    transform: scale(1.06) translateY(-2px);
    box-shadow: 0 14px 34px rgba(37, 99, 235, 0.48), 0 3px 10px rgba(0, 0, 0, 0.16);
}

.la-bubble:active { transform: scale(0.97); }

/* --- Rotating "ASK LIBRA" rim --- */
.la-bubble-ring {
    position: absolute;
    inset: 0;
    display: block;
    animation: la-ring-spin 16s linear infinite;
    transform-origin: 50% 50%;
    pointer-events: none;
}
.la-bubble:hover .la-bubble-ring { animation-duration: 7s; }
.la-nudge .la-bubble-ring { animation-duration: 3.2s; }
.la-ring-svg { width: 100%; height: 100%; display: block; }
.la-ring-svg text {
    font-family: inherit;
    font-size: 11.5px;
    font-weight: 800;
    letter-spacing: 5px;
    fill: rgba(255, 255, 255, 0.92);
}
/* Arabic rim: modern Kufi, no letter-spacing (Arabic glyphs must stay joined),
   RTL direction, centered on the arc so it renders on the visible top of the ring. */
.la-ring-svg.la-ring-rtl text {
    font-family: 'Reem Kufi', 'Tahoma', sans-serif;
    font-size: 13px;
    font-weight: 700;
    letter-spacing: 0;
    direction: rtl;
}

/* --- Still, readable mascot in the center --- */
.la-bubble-core {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
}
.la-bubble-core::before {
    content: '';
    position: absolute;
    width: 62px;
    height: 62px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.15);
    box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.18);
}
.la-bubble-core svg { position: relative; z-index: 1; width: 38px; height: 38px; display: block; }

@keyframes la-ring-spin { to { transform: rotate(360deg); } }

/* Subtle attention pulse ring */
.la-bubble::after {
    content: '';
    position: absolute;
    inset: 0;
    border-radius: 50%;
    box-shadow: 0 0 0 0 rgba(37, 99, 235, 0.45);
    animation: la-pulse 2.6s ease-out infinite;
}

@keyframes la-pulse {
    0% { box-shadow: 0 0 0 0 rgba(37, 99, 235, 0.4); }
    70% { box-shadow: 0 0 0 16px rgba(37, 99, 235, 0); }
    100% { box-shadow: 0 0 0 0 rgba(37, 99, 235, 0); }
}

@keyframes la-bubble-in {
    from { opacity: 0; transform: scale(0.4) translateY(20px); }
    to { opacity: 1; transform: scale(1) translateY(0); }
}

/* Hide bubble while the panel is open */
body.la-open .la-bubble { opacity: 0; pointer-events: none; transform: scale(0.5); }

/* ===== First-visit charm: greeting speech bubble ===== */
.la-greet {
    position: fixed;
    z-index: 941;
    bottom: calc(24px + 104px + 14px); /* sit just above the 104px bubble */
    inset-inline-end: 24px;
    max-width: 250px;
    padding: 12px 34px 12px 14px;
    border-radius: 16px;
    background: #fff;
    color: #1f2937;
    font-size: 0.86rem;
    font-weight: 500;
    line-height: 1.4;
    cursor: pointer;
    border: 1px solid rgba(37, 99, 235, 0.12);
    box-shadow: 0 12px 34px rgba(37, 99, 235, 0.18), 0 3px 10px rgba(0, 0, 0, 0.12);
    opacity: 0;
    transform: translateY(12px) scale(0.92);
    transform-origin: bottom right;
    transition: opacity 0.3s ease, transform 0.38s cubic-bezier(0.34, 1.56, 0.64, 1);
}
[dir="rtl"] .la-greet { transform-origin: bottom left; }
.la-greet.show { opacity: 1; transform: translateY(0) scale(1); }

/* Little tail pointing down toward the bubble */
.la-greet::after {
    content: '';
    position: absolute;
    bottom: -7px;
    inset-inline-end: 24px;
    width: 14px;
    height: 14px;
    background: #fff;
    border-right: 1px solid rgba(37, 99, 235, 0.12);
    border-bottom: 1px solid rgba(37, 99, 235, 0.12);
    transform: rotate(45deg);
}

.la-greet-close {
    position: absolute;
    top: 5px;
    inset-inline-end: 7px;
    width: 20px;
    height: 20px;
    border: none;
    background: transparent;
    color: #9ca3af;
    font-size: 1.15rem;
    line-height: 1;
    cursor: pointer;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s, color 0.15s;
}
.la-greet-close:hover { background: rgba(0, 0, 0, 0.06); color: #4b5563; }

[data-theme="dark"] .la-greet {
    background: #1f2430;
    color: #e5e7eb;
    border-color: rgba(255, 255, 255, 0.08);
}
[data-theme="dark"] .la-greet::after {
    background: #1f2430;
    border-color: rgba(255, 255, 255, 0.08);
}
[data-theme="dark"] .la-greet-close:hover { background: rgba(255, 255, 255, 0.1); color: #cbd5e1; }

/* ===== Coexist with the global "Back to top" button =====
   The chat launcher is the anchor FAB; the back-to-top stacks ABOVE it,
   center-aligned, so they never overlap. Scoped to pages with the concierge. */
body.la-active .back-to-top {
    bottom: calc(24px + 104px + 14px); /* above the 104px bubble (bottom:24px) + gap */
    right: 52px;                       /* center-align with the bubble (center ~76px from edge) */
}
[dir="rtl"] body.la-active .back-to-top { right: auto; left: 32px; }

@media (max-width: 860px) {
    body.la-active .back-to-top {
        bottom: calc(18px + 84px + 12px); /* above the 84px mobile bubble (bottom:18px) */
        right: 39px;
    }
    [dir="rtl"] body.la-active .back-to-top { right: auto; left: 23px; }
}

/* ===== Split layout: shrink the library to make room for the chat ===== */
@media (min-width: 861px) {
    body.la-open .library-page {
        margin-inline-end: var(--la-width);
        transition: margin 0.35s cubic-bezier(0.22, 1, 0.36, 1);
    }
    .library-page { transition: margin 0.35s cubic-bezier(0.22, 1, 0.36, 1); }

    /* Slide the page header bar in sync with the library content. Targeting
       .main-nav (the visible sticky bar) is the most reliable — it shrinks from
       the right so it ends at the panel edge, matching the resized content. */
    body.la-active header .main-nav {
        transition: margin 0.35s cubic-bezier(0.22, 1, 0.36, 1),
                    transform 0.35s ease, background-color 0.3s ease, border-color 0.3s ease;
    }
    body.la-open header .main-nav { margin-inline-end: var(--la-width); }
}

/* Safety: a reading-list save button must never render inside the chat header. */
.la-header .rl-save-btn { display: none !important; }

/* ===== Chat panel ===== */
.la-panel {
    position: fixed;
    top: 0;
    inset-inline-end: 0;
    height: 100vh;
    width: var(--la-width);
    max-width: 100vw;
    background: var(--bg-color, #fff);
    border-inline-start: 1px solid var(--border-color, #e5e7eb);
    box-shadow: -4px 0 28px rgba(0, 0, 0, 0.12);
    z-index: 945;
    display: flex;
    flex-direction: column;
    transform: translateX(100%);
    transition: transform 0.38s cubic-bezier(0.22, 1, 0.36, 1);
}
[dir="rtl"] .la-panel { transform: translateX(-100%); }
body.la-open .la-panel { transform: translateX(0); }

[data-theme="dark"] .la-panel {
    background: #14161f;
    border-color: rgba(255, 255, 255, 0.08);
}

/* ===== Header ===== */
.la-header {
    flex-shrink: 0;
    padding: 14px 16px;
    background: var(--la-header-grad);
    color: #fff;
    display: flex;
    align-items: center;
    gap: 12px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.12);
}

.la-header-avatar {
    width: 38px;
    height: 38px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.18);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}
.la-header-avatar svg { width: 22px; height: 22px; }

.la-header-text { flex: 1; min-width: 0; line-height: 1.2; }
.la-header-title { font-weight: 700; font-size: 1rem; display: flex; align-items: center; gap: 6px; }
.la-header-sub { font-size: 0.72rem; opacity: 0.85; }
.la-header-sub::before { content: '●'; color: #4ade80; font-size: 0.7em; margin-inline-end: 4px; vertical-align: middle; }

.la-header-btn {
    width: 36px;
    height: 36px;
    border-radius: 9px;
    border: none;
    background: transparent;
    color: #fff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
    flex-shrink: 0;
}
.la-header-btn:hover { background: rgba(255, 255, 255, 0.18); }
.la-header-btn svg { width: 20px; height: 20px; }

/* ===== Messages area ===== */
.la-messages {
    flex: 1 1 auto;
    min-height: 0; /* critical: lets the flex child shrink so overflow-y can scroll */
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    padding: 18px 16px;
    display: flex;
    flex-direction: column;
    gap: 14px;
    background: var(--bg-secondary, #f7f8fa);
}
/* Children must keep their natural height so the container overflows and SCROLLS,
   instead of flexbox compressing them (which hid the tall book-suggestion cards). */
.la-messages > * { flex-shrink: 0; }
[data-theme="dark"] .la-messages { background: rgba(255, 255, 255, 0.02); }

.la-msg {
    max-width: 88%;
    padding: 11px 15px;
    border-radius: 14px;
    font-size: 0.95rem;
    line-height: 1.55;
    animation: la-msg-in 0.3s ease-out;
    word-wrap: break-word;
}
@keyframes la-msg-in {
    from { opacity: 0; transform: translateY(6px); }
    to { opacity: 1; transform: translateY(0); }
}

.la-msg.bot {
    align-self: flex-start;
    background: var(--bg-color, #fff);
    color: var(--text-color, #1f2937);
    border: 1px solid rgba(0, 0, 0, 0.06);
    border-bottom-left-radius: 5px;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.04);
}
[data-theme="dark"] .la-msg.bot { background: #232633; color: #e5e7eb; border-color: rgba(255, 255, 255, 0.07); }

.la-msg.user {
    align-self: flex-end;
    background: var(--la-header-grad);
    color: #fff;
    border-bottom-right-radius: 5px;
    box-shadow: 0 2px 8px rgba(37, 99, 235, 0.25);
}

/* ===== Inline book suggestions inside chat ===== */
.la-suggest {
    align-self: stretch;
    display: flex;
    gap: 10px;
    overflow-x: auto;
    padding: 4px 2px 8px;
    scroll-snap-type: x mandatory;
    scrollbar-width: thin;
}
.la-suggest-card {
    flex: 0 0 96px;
    width: 96px;
    text-decoration: none;
    color: inherit;
    scroll-snap-align: start;
    border-radius: 10px;
    transition: transform 0.2s;
}
.la-suggest-card:hover { transform: translateY(-3px); }
.la-suggest-cover {
    width: 96px;
    height: 144px;
    border-radius: 10px;
    overflow: hidden;
    background: var(--bg-secondary, #eef0f4);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.14);
}
.la-suggest-cover img { width: 100%; height: 100%; object-fit: cover; display: block; }
.la-suggest-title {
    font-size: 0.72rem;
    font-weight: 600;
    margin-top: 6px;
    line-height: 1.25;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    color: var(--text-color, #1f2937);
}
[data-theme="dark"] .la-suggest-title { color: #e5e7eb; }

/* ===== Typing indicator ===== */
.la-typing { display: inline-flex; gap: 4px; align-items: center; }
.la-typing span {
    width: 7px; height: 7px; border-radius: 50%;
    background: var(--la-accent-2);
    opacity: 0.45;
    animation: la-typing 1s infinite;
}
.la-typing span:nth-child(2) { animation-delay: 0.2s; }
.la-typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes la-typing {
    0%, 100% { transform: translateY(0); opacity: 0.4; }
    50% { transform: translateY(-4px); opacity: 1; }
}

/* ===== Quick-start chips ===== */
.la-chips { display: flex; flex-wrap: wrap; gap: 8px; align-self: flex-start; max-width: 100%; }
.la-chip {
    border: 1px solid rgba(37, 99, 235, 0.3);
    background: rgba(37, 99, 235, 0.06);
    color: var(--la-accent);
    border-radius: 18px;
    padding: 7px 14px;
    font-size: 0.82rem;
    cursor: pointer;
    transition: background 0.2s, transform 0.15s;
    font-family: inherit;
}
.la-chip:hover { background: rgba(37, 99, 235, 0.14); transform: translateY(-1px); }
[data-theme="dark"] .la-chip { color: #93b4ff; border-color: rgba(99, 102, 241, 0.4); background: rgba(99, 102, 241, 0.12); }

/* ===== Sign in / Create account CTA (guests) ===== */
.la-auth {
    align-self: flex-start;
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    margin-top: 2px;
}
.la-auth-btn {
    text-decoration: none;
    border-radius: 12px;
    padding: 10px 18px;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    transition: transform 0.15s, box-shadow 0.2s, background 0.2s;
    border: 1px solid var(--border-color, #d1d5db);
    color: var(--text-color, #1f2937);
    background: var(--bg-color, #fff);
}
.la-auth-btn:hover { transform: translateY(-1px); }
.la-auth-btn.primary {
    border-color: transparent;
    color: #fff;
    background: var(--la-header-grad);
    box-shadow: 0 4px 14px rgba(37, 99, 235, 0.3);
}
[data-theme="dark"] .la-auth-btn { background: #232633; color: #e5e7eb; border-color: rgba(255,255,255,0.12); }

/* ===== Input area ===== */
.la-input-area {
    flex-shrink: 0;
    padding: 12px 14px;
    border-top: 1px solid rgba(0, 0, 0, 0.06);
    background: var(--bg-color, #fff);
    display: flex;
    align-items: flex-end;
    gap: 10px;
}
[data-theme="dark"] .la-input-area { background: #14161f; border-color: rgba(255, 255, 255, 0.07); }

.la-input {
    flex: 1;
    min-width: 0;
    border: 1px solid var(--border-color, #d1d5db);
    border-radius: 14px;
    padding: 11px 14px;
    font-family: inherit;
    font-size: 0.95rem;
    resize: none;
    max-height: 120px;
    height: 44px;
    outline: none;
    background: var(--bg-secondary, #f7f8fa);
    color: var(--text-color, #1f2937);
    transition: border-color 0.2s, box-shadow 0.2s;
    /* Hide the scrollbar (and its spinner-like arrows) — the field auto-grows
       and stays scrollable via wheel/touch. */
    overflow-y: auto;
    scrollbar-width: none;        /* Firefox */
    -ms-overflow-style: none;     /* old Edge/IE */
}
.la-input::-webkit-scrollbar { width: 0; height: 0; display: none; }
.la-input:focus { border-color: var(--la-accent); box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12); }
[data-theme="dark"] .la-input { background: #232633; color: #e5e7eb; border-color: rgba(255, 255, 255, 0.1); }

.la-send {
    width: 44px;
    height: 44px;
    flex-shrink: 0;
    border-radius: 50%;
    border: none;
    background: var(--la-header-grad);
    color: #fff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: opacity 0.2s, transform 0.15s;
}
.la-send:hover:not(:disabled) { transform: scale(1.06); }
.la-send:disabled { opacity: 0.4; cursor: default; }
.la-send svg { width: 20px; height: 20px; }
[dir="rtl"] .la-send svg { transform: scaleX(-1); }

/* ===== "Picked for you" region inside the library ===== */
.la-picks {
    display: none;
    margin: 0 0 8px;
    padding: 18px 20px 6px;
    background: linear-gradient(135deg, rgba(37, 99, 235, 0.05), rgba(99, 102, 241, 0.05));
    border-radius: var(--la-radius);
    animation: la-msg-in 0.4s ease-out;
}
body.la-open .la-picks.has-picks { display: block; }

.la-picks-head {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 700;
    font-size: 1.05rem;
    color: var(--text-color, #1f2937);
    margin-bottom: 14px;
}
.la-picks-head svg { width: 20px; height: 20px; color: var(--la-accent); }
[data-theme="dark"] .la-picks-head { color: #e5e7eb; }

.la-picks-grid {
    display: flex;
    gap: 14px;
    overflow-x: auto;
    overflow-y: hidden;
    padding: 2px 2px 10px;
    scroll-snap-type: x proximity;
    -webkit-overflow-scrolling: touch;
}
/* Compact, fixed-width cards in a horizontal row — same feel as the
   category rows on the library page (not a stretchy wrapping grid). */
.la-picks-grid .lib-card {
    flex: 0 0 150px;
    width: 150px;
    height: auto;            /* override library.css fixed 400px */
    scroll-snap-align: start;
}
/* Constrain the cover too — library.css pins it to 180px, which overflowed the
   150px card and made the covers overlap ("stacked"). Match the grid style. */
.la-picks-grid .lib-card-cover {
    width: 100%;
    height: auto;
    aspect-ratio: 2 / 3;
}
.la-picks-grid::-webkit-scrollbar { height: 6px; }

/* Highlight animation when a fresh pick lands */
.la-picks-grid .lib-card { animation: la-pick-pop 0.45s cubic-bezier(0.34, 1.56, 0.64, 1); }
@keyframes la-pick-pop {
    from { opacity: 0; transform: scale(0.9); }
    to { opacity: 1; transform: scale(1); }
}

/* ===== Mobile: chat becomes a full overlay ===== */
@media (max-width: 860px) {
    .la-panel {
        width: 100vw;
        height: 100dvh;
        border: none;
    }
    body.la-open { overflow: hidden; }
    .la-bubble { bottom: 18px; inset-inline-end: 18px; width: 84px; height: 84px; }
    .la-bubble-core::before { width: 50px; height: 50px; }
    .la-bubble-core svg { width: 32px; height: 32px; }
    .la-ring-svg text { font-size: 11px; letter-spacing: 3.6px; }
    .la-greet { bottom: calc(18px + 84px + 12px); inset-inline-end: 18px; max-width: 220px; }
    .la-greet::after { inset-inline-end: 20px; }
}

/* Respect reduced motion */
@media (prefers-reduced-motion: reduce) {
    .la-bubble, .la-bubble::after, .la-bubble-ring, .la-msg, .la-picks, .la-picks-grid .lib-card { animation: none !important; }
    .la-panel, .library-page, .la-greet { transition: none !important; }
}
