/* =========================================================================
   AI Assistant — "Sara" floating librarian, semantic search & book recs.
   Themed with the site palette (--gold = logo red, --card-bg, --bg, --white).
   ========================================================================= */

:root {
  --ai-red: var(--gold, #d62828);
  --ai-red2: var(--gold2, #b71d1d);
  --ai-panel-bg: var(--bg, #fff);
  --ai-card: var(--card-bg, rgba(255,255,255,0.9));
  --ai-line: var(--border-line, rgba(26,22,20,0.12));
  --ai-text: var(--white, #1a1614);
  --ai-text-dim: var(--white2, #3f3630);
}

/* Dark mode: the themed red reads muddy/pink on the dark background — use a
   brighter, more vivid red so the accent pops. Mirrors the site's theme scoping. */
:root,
[data-theme="dark"] {
  --ai-red: #ff4757;
  --ai-red2: #ff2740;
}
[data-theme="light"] {
  --ai-red: #d62828;
  --ai-red2: #b71d1d;
}
@media (prefers-color-scheme: dark) {
  [data-theme="system"] { --ai-red: #ff4757; --ai-red2: #ff2740; }
}
@media (prefers-color-scheme: light) {
  [data-theme="system"] { --ai-red: #d62828; --ai-red2: #b71d1d; }
}

/* ---- Launcher ---------------------------------------------------------- */
.ai-launcher {
  position: fixed;
  right: 22px; bottom: 22px;
  z-index: 10000;
  display: inline-flex; align-items: center; gap: 10px;
  padding: 13px 20px 13px 16px;
  border: none; cursor: pointer;
  border-radius: 999px;
  color: #fff;
  font-family: Helvetica, Arial, sans-serif;
  font-size: 15px; font-weight: 700; letter-spacing: 0.01em;
  background: linear-gradient(135deg, var(--ai-red), var(--ai-red2));
  box-shadow: 0 10px 30px rgba(214,40,40,0.42), 0 2px 6px rgba(0,0,0,0.18);
  transition: transform .25s cubic-bezier(.2,.9,.3,1), box-shadow .25s ease, opacity .2s ease;
}
.ai-launcher:hover { transform: translateY(-3px) scale(1.02); box-shadow: 0 16px 40px rgba(214,40,40,0.5); }
.ai-launcher:active { transform: translateY(-1px) scale(.99); }
.ai-launcher svg { width: 22px; height: 22px; }
.ai-launcher .ai-spark { animation: ai-twinkle 2.4s ease-in-out infinite; }
.ai-launcher.is-hidden { opacity: 0; pointer-events: none; transform: translateY(12px) scale(.9); }
@keyframes ai-twinkle { 0%,100% { opacity: 1; transform: scale(1); } 50% { opacity: .55; transform: scale(.82) rotate(8deg); } }
@media (max-width: 560px) { .ai-launcher span { display: none; } .ai-launcher { padding: 15px; } }

/* ---- Panel ------------------------------------------------------------- */
.ai-panel {
  position: fixed;
  right: 22px; bottom: 22px;
  z-index: 10001;
  width: 396px; max-width: calc(100vw - 28px);
  height: 616px; max-height: calc(100vh - 90px);
  display: flex; flex-direction: column;
  background: var(--ai-panel-bg);
  border: 1px solid var(--ai-line);
  border-radius: 20px;
  overflow: hidden;
  font-family: Helvetica, Arial, sans-serif;
  box-shadow: 0 30px 80px rgba(0,0,0,0.34), 0 0 0 1px rgba(0,0,0,0.02);
  transform-origin: bottom right;
  animation: ai-pop .32s cubic-bezier(.2,.9,.3,1) both;
}
.ai-panel[hidden] { display: none; }
@keyframes ai-pop { from { opacity: 0; transform: translateY(20px) scale(.94); } to { opacity: 1; transform: none; } }

.ai-head {
  display: flex; align-items: center; gap: 12px;
  padding: 15px 16px;
  color: #fff;
  background: linear-gradient(135deg, var(--ai-red), var(--ai-red2));
}
.ai-head-avatar {
  width: 40px; height: 40px; flex: 0 0 40px;
  border-radius: 12px;
  display: grid; place-items: center;
  background: rgba(255,255,255,0.16);
}
.ai-head-avatar svg { width: 22px; height: 22px; }
.ai-head-txt { line-height: 1.2; }
.ai-head-txt strong { font-size: 16px; font-weight: 800; letter-spacing: .01em; }
.ai-head-txt span { display: flex; align-items: center; gap: 6px; font-size: 12px; opacity: .9; margin-top: 2px; }
.ai-dot { width: 7px; height: 7px; border-radius: 50%; background: #46d17f; box-shadow: 0 0 0 3px rgba(70,209,127,.28); }
.ai-head-new {
  margin-left: auto;
  display: inline-flex; align-items: center; gap: 6px;
  background: rgba(255,255,255,.14); border: none; color: #fff;
  height: 32px; padding: 0 12px; border-radius: 9px; cursor: pointer;
  font-family: inherit; font-size: 12.5px; font-weight: 700; letter-spacing: .01em;
  transition: background .18s ease, transform .18s ease;
}
.ai-head-new:hover { background: rgba(255,255,255,.28); transform: translateY(-1px); }
.ai-head-new svg { width: 15px; height: 15px; }
.ai-head-close {
  margin-left: 8px; background: rgba(255,255,255,.14); border: none; color: #fff;
  width: 32px; height: 32px; border-radius: 9px; cursor: pointer; font-size: 20px; line-height: 1;
  display: grid; place-items: center; transition: background .18s ease;
}
.ai-head-close:hover { background: rgba(255,255,255,.28); }

/* ---- Messages ---------------------------------------------------------- */
.ai-body { flex: 1; overflow-y: auto; padding: 18px 16px 8px; scroll-behavior: smooth; }
.ai-body::-webkit-scrollbar { width: 8px; }
.ai-body::-webkit-scrollbar-thumb { background: var(--ai-line); border-radius: 8px; }

.ai-msg { margin-bottom: 16px; display: flex; flex-direction: column; animation: ai-fade .3s ease both; }
@keyframes ai-fade { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
.ai-msg.user { align-items: flex-end; }
.ai-bubble {
  max-width: 86%;
  padding: 11px 14px; border-radius: 16px;
  font-size: 14.5px; line-height: 1.55; color: var(--ai-text);
  background: var(--ai-card); border: 1px solid var(--ai-line);
  word-wrap: break-word;
}
.ai-msg.user .ai-bubble {
  background: linear-gradient(135deg, var(--ai-red), var(--ai-red2));
  color: #fff; border: none; border-bottom-right-radius: 5px;
}
.ai-msg.bot .ai-bubble { border-bottom-left-radius: 5px; }
.ai-bubble p { margin: 0 0 8px; } .ai-bubble p:last-child { margin-bottom: 0; }
.ai-bubble ul { margin: 6px 0; padding-left: 18px; } .ai-bubble li { margin: 3px 0; }
.ai-bubble strong { font-weight: 700; }
.ai-bubble a { color: var(--ai-red); font-weight: 600; }

.ai-typing { display: inline-flex; gap: 4px; padding: 4px 2px; }
.ai-typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--ai-red); opacity: .5; animation: ai-bounce 1.1s infinite ease-in-out; }
.ai-typing i:nth-child(2) { animation-delay: .15s; } .ai-typing i:nth-child(3) { animation-delay: .3s; }
@keyframes ai-bounce { 0%,80%,100% { transform: scale(.6); opacity: .35; } 40% { transform: scale(1); opacity: 1; } }

/* ---- Book cards (in chat & recs) --------------------------------------- */
.ai-books { display: flex; flex-direction: column; gap: 8px; margin-top: 10px; max-width: 92%; }
.ai-book {
  display: flex; align-items: center; gap: 11px;
  padding: 9px; border-radius: 13px; text-decoration: none;
  background: var(--ai-card); border: 1px solid var(--ai-line);
  transition: transform .18s ease, border-color .18s ease, box-shadow .18s ease;
}
.ai-book:hover { transform: translateX(3px); border-color: var(--ai-red); box-shadow: 0 6px 18px rgba(214,40,40,.14); }
.ai-book-cover {
  width: 42px; height: 56px; flex: 0 0 42px; border-radius: 7px; overflow: hidden;
  display: grid; place-items: center; font-size: 20px; color: #fff;
  background: linear-gradient(150deg, var(--ai-red), var(--ai-red2));
}
.ai-book-cover img { width: 100%; height: 100%; object-fit: cover; }
.ai-book-meta { min-width: 0; }
.ai-book-meta b { display: block; font-size: 13.5px; font-weight: 700; color: var(--ai-text); line-height: 1.3; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ai-book-meta small { display: block; font-size: 12px; color: var(--ai-text-dim); margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ai-book-cat { display: inline-block; margin-top: 4px; font-size: 10.5px; font-weight: 700; letter-spacing: .04em; text-transform: uppercase; color: var(--ai-red); }

/* ---- Export / download panel (chat) ----------------------------------- */
.ai-export { margin-top: 12px; max-width: 92%; padding: 12px; border-radius: 14px; background: var(--ai-card); border: 1px solid var(--ai-line); border-left: 3px solid var(--ai-red); }
.ai-export-note { display: flex; align-items: center; gap: 8px; font-size: 13px; font-weight: 700; color: var(--ai-text); margin-bottom: 10px; }
.ai-export-note svg { width: 17px; height: 17px; flex: 0 0 17px; color: var(--ai-red); }
.ai-export-btns { display: flex; gap: 8px; }
.ai-export-btn { text-decoration: none; padding: 9px 16px; border-radius: 10px; font-size: 13px; font-weight: 700; color: #fff; background: linear-gradient(135deg, var(--ai-red), var(--ai-red2)); transition: transform .18s ease, box-shadow .18s ease; }
.ai-export-btn:hover { transform: translateY(-2px); box-shadow: 0 8px 20px rgba(214,40,40,.24); }
.ai-export-btn.ghost { color: var(--ai-red); background: transparent; border: 1px solid var(--ai-red); }
.ai-export-btn.ghost:hover { background: var(--ai-red); color: #fff; }

/* ---- Suggestion chips -------------------------------------------------- */
.ai-suggest { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 4px; }
.ai-chip {
  padding: 8px 13px; border-radius: 999px; cursor: pointer;
  font-size: 13px; font-weight: 600; color: var(--ai-red);
  background: transparent; border: 1px solid var(--ai-line);
  transition: all .18s ease;
}
.ai-chip:hover { background: var(--ai-red); color: #fff; border-color: var(--ai-red); }

.ai-hello { text-align: center; padding: 6px 4px 14px; }
.ai-hello .ai-hello-icon { width: 54px; height: 54px; margin: 0 auto 10px; border-radius: 16px; display: grid; place-items: center; color: #fff; background: linear-gradient(135deg, var(--ai-red), var(--ai-red2)); }
.ai-hello .ai-hello-icon svg { width: 28px; height: 28px; }
.ai-hello h4 { font-size: 17px; font-weight: 800; color: var(--ai-text); margin: 0 0 4px; }
.ai-hello p { font-size: 13.5px; color: var(--ai-text-dim); margin: 0 0 14px; line-height: 1.5; }

/* ---- Composer ---------------------------------------------------------- */
.ai-foot { padding: 12px; border-top: 1px solid var(--ai-line); background: var(--ai-panel-bg); }
.ai-form { display: flex; gap: 8px; align-items: flex-end; }
.ai-input {
  flex: 1; resize: none; max-height: 108px;
  padding: 11px 14px; border-radius: 14px;
  border: 1px solid var(--ai-line); background: var(--ai-card); color: var(--ai-text);
  font-family: inherit; font-size: 14.5px; line-height: 1.4; outline: none;
  transition: border-color .18s ease;
}
.ai-input:focus { border-color: var(--ai-red); }
.ai-send {
  flex: 0 0 44px; width: 44px; height: 44px; border-radius: 13px; border: none; cursor: pointer;
  display: grid; place-items: center; color: #fff;
  background: linear-gradient(135deg, var(--ai-red), var(--ai-red2));
  transition: transform .18s ease, opacity .18s ease;
}
.ai-send:hover { transform: scale(1.06); } .ai-send:disabled { opacity: .45; cursor: not-allowed; transform: none; }
.ai-send svg { width: 20px; height: 20px; }
.ai-foot-note { text-align: center; font-size: 11px; color: var(--ai-text-dim); margin-top: 8px; opacity: .8; }

/* =========================================================================
   AI semantic search bar (books catalog page)
   ========================================================================= */
.ai-search-wrap { padding: 0 5%; margin: -6px 0 22px; }
.ai-search-box {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 14px 16px; border-radius: 16px;
  background: linear-gradient(135deg, rgba(214,40,40,.07), rgba(183,29,29,.04));
  border: 1px solid var(--ai-line);
}
.ai-search-box .ai-search-icon { width: 34px; height: 34px; flex: 0 0 34px; border-radius: 10px; display: grid; place-items: center; color: #fff; background: linear-gradient(135deg, var(--ai-red), var(--ai-red2)); }
.ai-search-box .ai-search-icon svg { width: 19px; height: 19px; }
.ai-search-input { flex: 1; min-width: 220px; border: none; background: transparent; outline: none; font-family: inherit; font-size: 15px; color: var(--ai-text); }
.ai-search-input::placeholder { color: var(--ai-text-dim); opacity: .8; }
.ai-search-btn {
  border: none; cursor: pointer; padding: 10px 20px; border-radius: 11px;
  font-family: inherit; font-size: 14px; font-weight: 700; color: #fff;
  background: linear-gradient(135deg, var(--ai-red), var(--ai-red2));
  transition: transform .18s ease, opacity .18s ease;
}
.ai-search-btn:hover { transform: translateY(-2px); } .ai-search-btn:disabled { opacity: .5; cursor: wait; }
.ai-search-label { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 700; letter-spacing: .05em; text-transform: uppercase; color: var(--ai-red); width: 100%; }
.ai-search-label svg { width: 15px; height: 15px; flex: 0 0 15px; }
.ai-search-results { padding: 0 5%; margin-bottom: 26px; }
.ai-search-summary { display: flex; align-items: flex-start; gap: 10px; font-size: 15px; color: var(--ai-text); background: var(--ai-card); border: 1px solid var(--ai-line); border-left: 3px solid var(--ai-red); border-radius: 12px; padding: 13px 15px; margin-bottom: 16px; line-height: 1.5; }
.ai-search-summary svg { width: 18px; height: 18px; flex: 0 0 18px; color: var(--ai-red); margin-top: 1px; }
.ai-search-clear { background: none; border: none; color: var(--ai-red); cursor: pointer; font-weight: 700; font-size: 13px; margin-left: auto; white-space: nowrap; }

/* =========================================================================
   "You may also like" — book detail recommendations
   ========================================================================= */
.ai-recs { padding: 40px 5% 10px; }
.ai-recs-head { display: flex; align-items: center; gap: 10px; margin-bottom: 20px; }
.ai-recs-head .ai-recs-badge { display: inline-flex; align-items: center; gap: 7px; font-size: 12px; font-weight: 800; letter-spacing: .05em; text-transform: uppercase; color: var(--ai-red); }
.ai-recs-head .ai-recs-badge svg { width: 15px; height: 15px; flex: 0 0 15px; }
.ai-book-cover svg { width: 20px; height: 20px; }
.ai-rec-cover svg { width: 26px; height: 26px; }
.ai-recs-head h2 { font-size: 26px; font-weight: 800; color: var(--ai-text); margin: 0; }
.ai-recs-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 16px; }
.ai-rec-card {
  display: flex; gap: 13px; padding: 14px; border-radius: 15px; text-decoration: none;
  background: var(--ai-card); border: 1px solid var(--ai-line);
  transition: transform .2s ease, border-color .2s ease, box-shadow .2s ease;
}
.ai-rec-card:hover { transform: translateY(-4px); border-color: var(--ai-red); box-shadow: 0 14px 34px rgba(214,40,40,.16); }
.ai-rec-cover { width: 58px; height: 78px; flex: 0 0 58px; border-radius: 8px; overflow: hidden; display: grid; place-items: center; font-size: 26px; color: #fff; background: linear-gradient(150deg, var(--ai-red), var(--ai-red2)); }
.ai-rec-cover img { width: 100%; height: 100%; object-fit: cover; }
.ai-rec-info { min-width: 0; }
.ai-rec-info .ai-rec-cat { font-size: 10.5px; font-weight: 700; letter-spacing: .04em; text-transform: uppercase; color: var(--ai-red); }
.ai-rec-info b { display: block; font-size: 15px; font-weight: 700; color: var(--ai-text); margin: 3px 0; line-height: 1.3; }
.ai-rec-info small { font-size: 13px; color: var(--ai-text-dim); }
.ai-recs-skeleton { display: inline-flex; gap: 6px; align-items: center; color: var(--ai-text-dim); font-size: 14px; }

/* =========================================================================
   "Continue conversation" resume prompt + persistent-chat launcher
   ========================================================================= */
.ai-launcher--resume { padding-left: 18px; }

.ai-resume {
  position: fixed; right: 22px; bottom: 84px; z-index: 10000;
  max-width: 300px;
  display: flex; align-items: center; gap: 11px;
  padding: 11px 12px; border-radius: 16px; cursor: pointer;
  background: var(--ai-panel-bg); color: var(--ai-text);
  border: 1px solid var(--ai-line);
  box-shadow: 0 18px 44px rgba(0,0,0,0.28), 0 0 0 1px rgba(0,0,0,0.02);
  transform: translateY(16px); opacity: 0; pointer-events: none;
  transition: transform .32s cubic-bezier(.2,.9,.3,1), opacity .28s ease;
  font-family: Helvetica, Arial, sans-serif;
}
.ai-resume.show { transform: none; opacity: 1; pointer-events: auto; }
.ai-resume-ic {
  width: 36px; height: 36px; flex: 0 0 36px; border-radius: 11px;
  display: grid; place-items: center; color: #fff;
  background: linear-gradient(135deg, var(--ai-red), var(--ai-red2));
}
.ai-resume-ic svg { width: 19px; height: 19px; }
.ai-resume-txt { min-width: 0; line-height: 1.3; }
.ai-resume-txt b { display: block; font-size: 13.5px; font-weight: 800; color: var(--ai-text); }
.ai-resume-txt small { display: block; font-size: 12px; color: var(--ai-text-dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ai-resume-x {
  margin-left: 2px; background: none; border: none; color: var(--ai-text-dim);
  font-size: 19px; line-height: 1; cursor: pointer; padding: 2px 5px; border-radius: 7px;
  align-self: flex-start; transition: background .18s ease;
}
.ai-resume-x:hover { background: var(--ai-line); }

/* ---- Mobile: open panel goes full-screen (only after tapping continue) --- */
@media (max-width: 560px) {
  .ai-panel {
    right: 0; left: 0; top: 0; bottom: 0;
    width: 100%; max-width: 100%; height: 100%; max-height: 100%;
    border-radius: 0; border: none;
  }
  .ai-resume { right: 12px; left: 12px; bottom: 80px; max-width: none; }
  .ai-resume-txt small { max-width: none; }
}
