:root {
  color-scheme: light;
  --navy: #5B2D8E;
  --navy-dark: #421f68;
  --blue: #7B52B8;
  --blue-soft: #F0EBF8;
  --orange: #C09060;
  --ink: #1a1025;
  --muted: #7a6b87;
  --line: #E0D5C8;
  --canvas: #FAF7F2;
  --white: #fff;
  --user-bubble: #EDE6F7;
  --shadow: 0 8px 24px rgba(60, 20, 100, .10);
}

* { box-sizing: border-box; }

html { scroll-behavior: smooth; }

body {
  margin: 0;
  height: 100vh;
  overflow: hidden;
  background: var(--canvas);
  color: var(--ink);
  font: 15px/1.55 Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button, textarea { font: inherit; }
button { cursor: pointer; }
button, a, textarea, input { transition: color .18s ease, background-color .18s ease, border-color .18s ease, box-shadow .2s ease, transform .18s ease, opacity .18s ease; }

.login-screen {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  place-items: center;
  padding: 24px;
  background: radial-gradient(circle at 50% 35%, #fff 0, #F5EFF8 55%, #EDE3F4 100%);
  transition: opacity .38s ease, transform .48s cubic-bezier(.22, 1, .36, 1), visibility .48s;
}
.login-card { width: min(430px, 100%); padding: 38px; border: 1px solid var(--line); border-top: 5px solid var(--navy); border-radius: 16px; background: white; box-shadow: 0 24px 70px rgba(60, 20, 100, .14); animation: login-in .55s cubic-bezier(.22, 1, .36, 1) both; }
.login-mark { display: grid; place-items: center; width: 54px; height: 54px; margin-bottom: 22px; border-radius: 12px; border-bottom: 4px solid var(--orange); background: var(--navy); color: white; font: 700 29px Georgia, serif; }
.login-card h2 { margin: 8px 0 5px; color: var(--navy-dark); font-size: 28px; }
.login-card > p:not(.eyebrow):not(.login-error) { margin: 0 0 24px; color: var(--muted); }
.login-card label { display: block; margin-bottom: 6px; color: var(--navy-dark); font-size: 12px; font-weight: 750; }
.passcode-row { display: grid; grid-template-columns: 1fr auto; gap: 8px; }
.passcode-row input { min-width: 0; border: 1px solid var(--line); border-radius: 10px; padding: 12px 13px; outline: none; color: var(--ink); }
.passcode-row input:focus { border-color: var(--blue); box-shadow: 0 0 0 3px rgba(123, 82, 184, .14); }
.passcode-row button { border: 0; border-radius: 999px; padding: 0 20px; background: var(--navy); color: white; font-weight: 750; }
.passcode-row button:hover { transform: translateY(-1px); background: var(--navy-dark); }
.login-error { min-height: 21px; margin: 8px 0 0; color: #bd443d; font-size: 12px; }
.login-card.invalid { animation: shake .32s ease; }
.app-locked .shell { opacity: 0; transform: translateY(26px); }
.authenticated .login-screen { opacity: 0; transform: translateY(-36px); visibility: hidden; pointer-events: none; }
.authenticated .shell { animation: app-enter .55s .12s cubic-bezier(.22, 1, .36, 1) both; }
.login-screen.returning .login-card { animation: login-return .42s cubic-bezier(.22, 1, .36, 1) both; }
.shell { transition: opacity .2s ease, transform .25s cubic-bezier(.22, 1, .36, 1); }
.logging-out .shell { opacity: 0; transform: translateY(-10px) scale(.995); }

.shell {
  width: 100%;
  height: 100vh;
  margin: 0;
  display: flex;
  flex-direction: column;
  background: var(--white);
}

.topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 66px;
  padding: 10px 22px;
}

.topbar { border-bottom: 2px solid #D4B896; }
.topbar-actions { display: flex; align-items: center; gap: 8px; }
.brand { display: flex; gap: 11px; align-items: center; color: inherit; text-decoration: none; }
.brand strong, .brand small { display: block; }
.brand strong { color: var(--navy-dark); font-size: 18px; }
.brand small { color: var(--orange); font-size: 10px; letter-spacing: .09em; text-transform: uppercase; }
.mark { display: grid; place-items: center; width: 42px; height: 42px; color: white; background: var(--navy); font: 700 22px Georgia, serif; border-radius: 10px; border-bottom: 4px solid var(--orange); }
.logout-button { border: 1px solid var(--line); border-radius: 999px; padding: 7px 16px; background: white; color: var(--navy); font-size: 12px; font-weight: 700; }
.logout-button:hover { border-color: var(--blue); background: var(--blue-soft); transform: translateY(-1px); }
.logout-button:disabled { opacity: .5; cursor: wait; transform: none; }
.menu-button, .sidebar-mobile-header, .mobile-logout, .drawer-backdrop { display: none; }

.status { color: var(--muted); font-size: 12px; }
.status span { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 7px; background: #e3a33c; }
.status.ready span { background: #49a76b; box-shadow: 0 0 0 3px #d5f0e2; }
.status.error span { background: #d9534f; }
.header-stats { display: grid; gap: 5px; color: var(--muted); font-size: 9px; text-transform: uppercase; letter-spacing: .05em; }
.header-stats strong { color: var(--navy-dark); }
.header-stats span + span { padding-top: 5px; border-top: 1px solid var(--line); }

.app-layout { flex: 1; min-height: 0; display: grid; grid-template-columns: 250px minmax(0, 1fr); }
.sidebar { min-width: 0; min-height: 0; display: flex; flex-direction: column; padding: 16px 14px; border-right: 1px solid var(--line); background: #F3EEE7; }
.new-chat-button { width: 100%; display: flex; align-items: center; justify-content: center; gap: 7px; border: 0; border-radius: 14px; padding: 10px 12px; background: var(--navy); color: white; font-weight: 750; }
.new-chat-button:hover { transform: translateY(-1px); background: var(--navy-dark); box-shadow: 0 5px 14px rgba(60, 20, 100, .18); }
.new-chat-button:disabled { opacity: .55; cursor: wait; transform: none; }
.chat-log-heading { margin: 22px 8px 8px; color: #9a86a8; font-size: 10px; font-weight: 800; letter-spacing: .09em; text-transform: uppercase; }
.chat-log { min-height: 0; display: flex; flex: 1; flex-direction: column; gap: 4px; overflow-y: auto; }
.chat-log-item { width: 100%; min-width: 0; display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 7px; align-items: center; border: 1px solid transparent; border-radius: 10px; padding: 9px 10px; background: transparent; color: var(--muted); text-align: left; }
.chat-log-item strong { overflow: hidden; color: inherit; font-size: 12px; font-weight: 650; text-overflow: ellipsis; white-space: nowrap; }
.chat-log-item small { font-size: 9px; }
.chat-log-item:hover { color: var(--navy); background: white; border-color: var(--line); transform: translateX(2px); }
.chat-log-item.active { color: var(--navy-dark); background: var(--blue-soft); border-color: #d5c4ea; }
.chat-log-item.active { animation: history-select .25s cubic-bezier(.22, 1, .36, 1); }
.empty-chat-log { margin: 10px 8px; color: #b09fbf; font-size: 11px; }
.sidebar-footer { display: grid; gap: 11px; margin: 16px -2px -2px; padding: 14px 8px 4px; border-top: 1px solid var(--line); }
.sidebar-footer .sync-button { width: 100%; }
.powered-by { color: #b09fbf; font-size: 9px; font-weight: 650; letter-spacing: .07em; text-align: center; text-transform: uppercase; }
.authenticated .powered-by { animation: footer-in .45s .32s ease both; }
.main-panel { min-width: 0; min-height: 0; display: flex; flex-direction: column; }

.hero {
  max-height: 360px;
  overflow: hidden;
  padding: 35px 40px 28px;
  background: linear-gradient(110deg, #fff 46%, #F5EFF8);
  border-bottom: 1px solid var(--line);
  transition: max-height .4s ease, padding .4s ease, opacity .25s ease, transform .4s ease, border-color .3s ease;
}
.chat-started .hero { max-height: 0; padding-top: 0; padding-bottom: 0; opacity: 0; transform: translateY(-20px); border-color: transparent; pointer-events: none; }
.authenticated:not(.chat-started) .hero > * { animation: rise-in .5s cubic-bezier(.22, 1, .36, 1) both; }
.authenticated:not(.chat-started) .hero h1 { animation-delay: .08s; }
.authenticated:not(.chat-started) .hero .intro { animation-delay: .14s; }
.eyebrow { margin: 0; color: var(--orange); letter-spacing: .14em; font-size: 10px; font-weight: 800; }
h1 { margin: 9px 0 12px; color: var(--navy-dark); font-size: clamp(30px, 4vw, 50px); line-height: 1.08; letter-spacing: -.035em; }
h1 em { color: var(--blue); font-style: normal; font-weight: 650; }
.intro { max-width: 660px; margin: 0; color: var(--muted); font-size: 16px; }

.workspace { flex: 1; min-height: 0; display: flex; flex-direction: column; justify-content: center; padding: 18px 32px 12px; overflow: hidden; transition: padding .3s ease; }
.chat-started .workspace { justify-content: flex-start; }

.query-box { display: grid; grid-template-columns: 1fr auto; gap: 10px; padding: 8px; border: 1.5px solid var(--line); background: white; border-radius: 20px; box-shadow: 0 2px 10px rgba(60, 20, 100, .07); animation: rise-in .45s .18s cubic-bezier(.22, 1, .36, 1) both; }
.conversation:not([hidden]) ~ .query-box { margin-top: 0; }
textarea { resize: none; min-height: 52px; max-height: 190px; padding: 14px; color: var(--ink); border: 0; outline: 0; background: transparent; }
textarea::placeholder { color: #b09fbf; }
.query-box:focus-within { border-color: var(--blue); box-shadow: 0 0 0 3px rgba(123, 82, 184, .12); }
.query-box button { align-self: stretch; min-width: 116px; display: flex; align-items: center; justify-content: center; gap: 12px; border: 0; border-radius: 12px; padding: 0 18px; background: var(--navy); color: white; font-weight: 700; }
.query-box button:hover { background: var(--navy-dark); }
.query-box button:disabled { opacity: .55; cursor: wait; }
.input-hint { margin: -3px 2px 5px; text-align: right; color: #b09fbf; font-size: 10px; }

.suggestions { display: flex; flex: 0 0 auto; flex-wrap: wrap; gap: 7px; padding: 10px 0 8px; }
.suggestions button, .sync-button { border: 1px solid var(--line); border-radius: 999px; background: white; color: var(--navy); padding: 7px 14px; }
.suggestions button:hover, .sync-button:hover { color: white; border-color: var(--navy); background: var(--navy); }
.suggestions button:hover { transform: translateY(-2px); box-shadow: 0 4px 10px rgba(60, 20, 100, .12); }
.suggestions button:active { transform: translateY(0) scale(.975); }
.suggestions button.chosen { color: white; border-color: var(--navy); background: var(--navy); animation: suggestion-picked .2s ease both; }
.suggestions button:disabled:not(.chosen) { opacity: .48; cursor: wait; }

.conversation { display: flex; min-height: 0; flex: 1 1 auto; flex-direction: column; gap: 16px; padding: 22px; overflow-y: auto; overscroll-behavior: contain; background: #F5F1EC; border: 1px solid var(--line); border-top: 5px solid var(--navy); scroll-behavior: smooth; }
.conversation[hidden] { display: none; }
.message-row { display: flex; align-items: flex-start; gap: 9px; }
.message-row { animation: message-in .34s cubic-bezier(.22, 1, .36, 1) both; }
.user-row { justify-content: flex-end; }
.assistant-row { justify-content: flex-start; }
.assistant-avatar { flex: 0 0 auto; display: grid; place-items: center; width: 32px; height: 32px; margin-top: 3px; border-radius: 50%; background: var(--navy); color: white; font-weight: 800; border-bottom: 3px solid var(--orange); }
.message { position: relative; padding: 11px 14px; border-radius: 14px; box-shadow: 0 1px 3px rgba(60, 20, 100, .09); }
.user-message { max-width: 72%; background: var(--user-bubble); border-top-right-radius: 4px; }
.assistant-message { width: min(880px, calc(100% - 42px)); background: white; border-top-left-radius: 4px; }
.answer p { margin: 0 0 10px; }
.answer p:last-child { margin-bottom: 0; }
.answer h3, .answer h4, .answer h5 { margin: 15px 0 6px; color: var(--navy-dark); line-height: 1.3; }
.answer ul, .answer ol { margin: 8px 0 12px; padding-left: 22px; }
.answer li { margin: 4px 0; }
.answer code { padding: 2px 5px; border-radius: 6px; background: #F0EBF8; color: #5B2D8E; font-size: .9em; }
.answer blockquote { margin: 10px 0; padding: 8px 12px; border-left: 3px solid var(--blue); background: var(--blue-soft); color: #5a4d65; }
.citation { display: inline-block; margin: 0 2px; padding: 1px 8px; border-radius: 999px; background: var(--blue-soft); color: var(--navy); font-size: 12px; font-weight: 700; text-decoration: none; white-space: nowrap; }
.citation:hover { background: var(--navy); color: white; }
.table-wrap { max-width: 100%; overflow-x: auto; margin: 10px 0; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th { background: var(--navy); color: white; text-align: left; }
th, td { padding: 8px 10px; border: 1px solid var(--line); }

.sources-section { margin-top: 18px; padding-top: 12px; border-top: 1px solid var(--line); }
.source-heading { display: flex; align-items: center; justify-content: space-between; width: 100%; border: 0; background: transparent; padding: 0; cursor: pointer; color: var(--navy); font-size: 12px; font-weight: 800; text-transform: uppercase; letter-spacing: .06em; }
.source-heading:hover { color: var(--blue); }
.source-chevron { font-size: 14px; font-weight: 400; transition: transform .22s cubic-bezier(.22, 1, .36, 1); display: inline-block; }
.sources-section.open .source-chevron { transform: rotate(90deg); }
.source-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 8px; margin-top: 10px; }
.source-grid[hidden] { display: none; }
.source { display: block; min-width: 0; padding: 12px; border: 1px solid var(--line); border-radius: 12px; background: #FDFAF7; color: inherit; text-decoration: none; }
.source { animation: source-in .3s ease both; }
.source:nth-child(2) { animation-delay: .035s; }
.source:nth-child(3) { animation-delay: .07s; }
.source:nth-child(4) { animation-delay: .105s; }
.source:nth-child(5) { animation-delay: .14s; }
.source:nth-child(6) { animation-delay: .175s; }
.source:hover { border-color: var(--blue); background: var(--blue-soft); transform: translateY(-2px); box-shadow: 0 5px 14px rgba(60, 20, 100, .10); }
.source small { display: block; color: var(--orange); margin-bottom: 4px; font-weight: 800; }
.source strong { color: var(--navy-dark); font-size: 13px; }
.source time { display: block; margin-top: 5px; color: var(--blue); font-size: 11px; font-weight: 700; }
.source p { color: var(--muted); font-size: 12px; margin: 6px 0 0; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.error-message { margin: 0; color: #bf3f39; }
.loading { margin: 0; color: var(--muted); }
.loading-label { background: linear-gradient(90deg, var(--muted) 20%, var(--blue) 48%, var(--muted) 76%); background-size: 220% 100%; background-clip: text; color: transparent; animation: reading-shimmer 1.5s ease-in-out infinite; }
.loading-label.stage-change { animation: stage-change .24s ease, reading-shimmer 1.5s ease-in-out infinite; }
.progress-track { height: 5px; margin-top: 13px; overflow: hidden; border-radius: 999px; background: #E8DFF0; }
.progress-fill { display: block; width: 4%; height: 100%; border-radius: inherit; background: linear-gradient(90deg, var(--navy), var(--blue)); box-shadow: 0 0 8px rgba(123, 82, 184, .28); transition: width .22s linear; }
.progress-track.complete .progress-fill { background: #49a76b; transition-duration: .16s; }
.assistant-message.answer-ready .answer { animation: answer-ready .38s cubic-bezier(.22, 1, .36, 1) both; }

.sync-button { color: white; background: var(--navy); border-color: var(--navy); border-radius: 999px; }

@keyframes login-in { from { opacity: 0; transform: translateY(18px) scale(.985); } to { opacity: 1; transform: none; } }
@keyframes login-return { from { opacity: 0; transform: translateY(-14px) scale(.985); } to { opacity: 1; transform: none; } }
@keyframes app-enter { from { opacity: 0; transform: translateY(28px); } to { opacity: 1; transform: none; } }
@keyframes rise-in { from { opacity: 0; transform: translateY(14px); } to { opacity: 1; transform: none; } }
@keyframes message-in { from { opacity: 0; transform: translateY(10px) scale(.995); } to { opacity: 1; transform: none; } }
@keyframes source-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
@keyframes shake { 0%, 100% { transform: translateX(0); } 30% { transform: translateX(-7px); } 65% { transform: translateX(6px); } }
@keyframes suggestion-picked { 50% { transform: translateY(-1px) scale(1.018); box-shadow: 0 0 0 4px rgba(123, 82, 184, .14); } }
@keyframes history-select { from { opacity: .72; transform: translateX(-4px); } to { opacity: 1; transform: none; } }
@keyframes answer-ready { from { opacity: .35; transform: translateY(6px); } to { opacity: 1; transform: none; } }
@keyframes reading-shimmer { from { background-position: 110% 0; } to { background-position: -110% 0; } }
@keyframes stage-change { from { opacity: .25; transform: translateY(3px); } to { opacity: 1; transform: none; } }
@keyframes footer-in { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; animation-delay: 0ms !important; transition-duration: .01ms !important; }
}

@media (max-width: 900px) {
  body { height: 100dvh; min-height: 0; overflow: hidden; }
  .shell { height: 100dvh; min-height: 0; }
  .topbar { min-height: 58px; padding: 7px 12px; }
  .mark { width: 38px; height: 38px; font-size: 20px; }
  .brand { gap: 9px; }
  .brand strong { font-size: 17px; }
  .brand small { font-size: 8px; }
  .desktop-logout { display: none; }
  .menu-button { width: 42px; height: 42px; display: grid; place-content: center; gap: 5px; border: 1px solid var(--line); border-radius: 10px; background: white; }
  .menu-button span { width: 20px; height: 2px; border-radius: 2px; background: var(--navy); transition: transform .25s ease, opacity .18s ease; }
  .drawer-open .menu-button span:nth-child(1) { transform: translateY(7px) rotate(45deg); }
  .drawer-open .menu-button span:nth-child(2) { opacity: 0; }
  .drawer-open .menu-button span:nth-child(3) { transform: translateY(-7px) rotate(-45deg); }
  .app-layout { position: relative; display: block; flex: 1; min-height: 0; }
  .main-panel { height: 100%; }
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 60;
    width: min(86vw, 330px);
    min-height: 0;
    padding: 14px;
    border: 0;
    border-right: 1px solid var(--line);
    background: #F3EEE7;
    box-shadow: 18px 0 45px rgba(60, 20, 100, .16);
    transform: translateX(-105%);
    transition: transform .32s cubic-bezier(.22, 1, .36, 1);
  }
  .drawer-open .sidebar { transform: translateX(0); }
  .sidebar-mobile-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; color: var(--navy-dark); }
  .sidebar-mobile-header strong { font-size: 17px; }
  .sidebar-mobile-header button { width: 38px; height: 38px; border: 0; border-radius: 50%; background: var(--blue-soft); color: var(--navy); font-size: 25px; line-height: 1; }
  .new-chat-button { width: 100%; }
  .chat-log-heading { display: block; }
  .chat-log { flex: 1; margin-top: 0; flex-direction: column; overflow-y: auto; }
  .chat-log-item { flex: none; }
  .sidebar-footer { display: grid; margin-top: 12px; padding-top: 12px; }
  .sidebar-footer .header-stats { display: grid; }
  .sidebar-footer .sync-button { width: 100%; margin: 0; }
  .mobile-logout { display: block; width: 100%; }
  .powered-by { white-space: nowrap; }
  .drawer-backdrop { position: fixed; inset: 0; z-index: 50; display: block; border: 0; background: rgba(30, 10, 50, .36); opacity: 0; visibility: hidden; pointer-events: none; backdrop-filter: blur(2px); transition: opacity .25s ease, visibility .25s; }
  .drawer-open .drawer-backdrop { opacity: 1; visibility: visible; pointer-events: auto; }
  .workspace { height: 100%; min-height: 0; justify-content: flex-end; padding: 8px 10px max(8px, env(safe-area-inset-bottom)); }
  .chat-started .workspace { justify-content: flex-start; }
  .conversation { border-right: 0; border-left: 0; padding: 14px 10px; }
  .suggestions { flex-wrap: nowrap; overflow-x: auto; padding: 8px 0 7px; scrollbar-width: none; }
  .suggestions::-webkit-scrollbar { display: none; }
  .suggestions button { flex: 0 0 auto; white-space: nowrap; }
  .query-box { flex: 0 0 auto; grid-template-columns: minmax(0, 1fr) auto; gap: 4px; padding: 5px; }
  .query-box textarea { min-height: 44px; max-height: 120px; padding: 11px 10px; }
  .query-box button { min-width: 46px; width: 46px; min-height: 44px; padding: 0; }
  .query-box button span:first-child { display: none; }
  .input-hint { display: none; }
  .source-grid { grid-template-columns: 1fr; }
}

@media (max-width: 680px) {
  .shell { width: 100%; margin: 0; border: 0; }
  .sync-button { padding: 6px 9px; font-size: 11px; }
  .hero { padding: 24px 18px 20px; }
  .hero h1 { font-size: clamp(31px, 10vw, 42px); }
  .hero .intro { font-size: 14px; }
  .user-message { max-width: 88%; }
  .assistant-message { width: calc(100% - 41px); }
  .passcode-row { grid-template-columns: 1fr; }
  .passcode-row button { min-height: 44px; }
  .login-card { padding: 28px 22px; }
}
