/* LawKB Chat — ChatGPT-style dark theme (canvas #212121, sidebar #171717) */
:root {
  --bg: #212121;
  --bg-sidebar: #171717;
  --bg-hover: rgba(255, 255, 255, 0.06);
  --bg-active: rgba(255, 255, 255, 0.1);
  --bg-composer: #2f2f2f;
  --bg-code: #1f1f1f;
  --text: #ececec;
  --text-2: #b4b4b4;
  --text-3: #8e8e8e;
  --border: rgba(255, 255, 255, 0.1);
  --border-strong: rgba(255, 255, 255, 0.2);
  --border-focus: rgba(255, 255, 255, 0.4);
  --bubble-user: #ececec;
  --bubble-user-ink: #171717;
  --accent: #ececec;
  --accent-ink: #171717;
  --green: #10a37f;
  --radius-card: 14px;
  --font: 'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  --mono: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, monospace;
}

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

html, body { height: 100%; overflow: hidden; }

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  letter-spacing: 0.1px;
  -webkit-font-smoothing: antialiased;
}

/* App is a locked absolute container: body can never scroll, so the composer
   dock stays fixed below the scrolling chat area on mobile. */
#app { position: fixed; inset: 0; display: flex; overflow: hidden; }

/* ---------- Sidebar ---------- */
#sidebar {
  width: 286px;
  flex: 0 0 286px;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transition: margin-left 0.22s ease;
}

.side-head { padding: 12px 14px 8px; }

.brand { display: flex; align-items: center; gap: 9px; }
.brand-mark {
  width: 28px; height: 28px; border-radius: 8px;
  background: var(--accent); color: var(--accent-ink);
  display: grid; place-items: center;
}
.brand-name { font-size: 17px; font-weight: 600; color: var(--text); letter-spacing: 0.1px; }
.brand-badge {
  font-size: 10px; font-weight: 600; letter-spacing: 0.6px; text-transform: uppercase;
  background: var(--bg-hover); color: var(--text-3);
  padding: 3px 7px; border-radius: 9999px; border: 1px solid var(--border);
}

.btn-new {
  display: flex; align-items: center; gap: 10px;
  margin: 4px 12px 12px;
  padding: 9px 12px;
  background: transparent; color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  font: inherit; font-size: 13.5px; font-weight: 500; letter-spacing: 0.1px;
  cursor: pointer;
  transition: background 0.12s ease, border-color 0.12s ease;
}
.btn-new:hover { background: var(--bg-hover); }
.btn-new:active { transform: scale(0.99); }

.session-list { flex: 1; overflow-y: auto; padding: 0 8px; }
.session-list::-webkit-scrollbar { width: 6px; }
.session-list::-webkit-scrollbar-thumb { background: rgba(255, 255, 255, 0.15); border-radius: 3px; }

.session-item {
  width: 100%;
  display: flex; align-items: center; gap: 10px;
  padding: 8px 12px;
  border: none; background: transparent;
  border-radius: 8px;
  font: inherit; font-size: 13.5px; color: var(--text-2); letter-spacing: 0.1px;
  text-align: left; cursor: pointer;
  transition: background 0.12s ease;
}
.session-item:hover { background: var(--bg-hover); }
.session-item.active { background: var(--bg-active); color: var(--text); }
.session-item .s-ico { flex: 0 0 auto; color: var(--text-3); display: grid; place-items: center; }
.session-item.active .s-ico { color: var(--text); }
.session-item .s-title { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.session-item .s-time { font-size: 10.5px; color: var(--text-3); flex: 0 0 auto; }

.side-foot {
  padding: 10px 14px;
  border-top: 1px solid var(--border);
  display: flex; flex-direction: column; gap: 8px;
}
.model-pill {
  display: inline-flex; align-items: center; gap: 7px;
  background: var(--bg-hover);
  border: 1px solid var(--border);
  border-radius: 9999px;
  padding: 5px 11px;
  font-size: 12px; color: var(--text-2); letter-spacing: 0.1px;
  align-self: flex-start;
}
.model-pill .dot { width: 7px; height: 7px; border-radius: 50%; background: #565656; }
.model-pill.ok .dot { background: var(--green); box-shadow: 0 0 0 3px rgba(16, 163, 127, 0.2); }
.model-pill.err .dot { background: #e57373; box-shadow: 0 0 0 3px rgba(229, 115, 115, 0.2); }

/* ---------- Main ---------- */
#main { flex: 1; display: flex; flex-direction: column; min-width: 0; position: relative; background: var(--bg); }

#topbar {
  height: 52px; flex: 0 0 52px;
  display: flex; align-items: center; gap: 8px;
  padding: 0 16px;
  background: rgba(33, 33, 33, 0.82);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.topbar-title {
  flex: 1; font-size: 13.5px; font-weight: 500; color: var(--text-2); letter-spacing: 0.1px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.icon-btn, .ghost-btn {
  display: grid; place-items: center;
  width: 34px; height: 34px;
  border: none; background: transparent; border-radius: 9999px;
  color: var(--text-2); cursor: pointer;
  transition: background 0.12s ease, color 0.12s ease;
}
.icon-btn:hover, .ghost-btn:hover { background: var(--bg-hover); color: var(--text); }
#btn-menu { display: none; }

/* ---------- Chat area ---------- */
#chat-area {
  flex: 1; overflow-y: auto;
  overscroll-behavior: contain; /* no rubber-band / body chain scroll on iOS */
}
#chat-area::-webkit-scrollbar { width: 8px; }
#chat-area::-webkit-scrollbar-thumb { background: rgba(255, 255, 255, 0.15); border-radius: 4px; }

/* ---------- Scroll-to-bottom float button ---------- */
.scroll-down {
  position: absolute;
  bottom: 88px;
  right: 18px;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  display: none;
  align-items: center;
  justify-content: center;
  background: var(--panel, #20222b);
  color: var(--text-2);
  border: 1px solid var(--border-strong);
  cursor: pointer;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
  z-index: 30;
  transition: background .15s, color .15s, transform .15s;
}
.scroll-down.show { display: flex; }
.scroll-down:hover { color: var(--accent, #7aa2f7); background: var(--panel-2, #2a2d3a); }
.scroll-down:active { transform: scale(.94); }

/* ---------- Empty state ---------- */
#empty-state {
  height: 100%;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-align: center;
  padding: 0 24px 40px;
  max-width: 680px; margin: 0 auto;
}
.empty-title {
  font-size: 32px; font-weight: 600; line-height: 1.25; letter-spacing: -0.4px;
  margin-bottom: 28px;
  color: var(--text);
}
.suggest { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; width: 100%; }
.suggest-card {
  display: flex; gap: 12px; align-items: flex-start; text-align: left;
  background: transparent;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-card);
  padding: 14px 15px;
  font: inherit;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease;
}
.suggest-card:hover { background: var(--bg-hover); border-color: rgba(255, 255, 255, 0.32); }
.suggest-card .sc-ico { flex: 0 0 auto; color: var(--text-2); margin-top: 1px; }
.suggest-card .sc-text { display: flex; flex-direction: column; min-width: 0; }
.suggest-card .sc-head { font-size: 14px; font-weight: 600; color: var(--text); margin-bottom: 3px; }
.suggest-card .sc-desc { font-size: 12.5px; line-height: 1.5; color: var(--text-3); }
.empty-note { margin-top: 22px; font-size: 12.5px; color: var(--text-3); letter-spacing: 0.1px; }

/* ---------- Messages ---------- */
.thread { max-width: 800px; margin: 0 auto; padding: 30px 24px 160px; }

.msg { margin-bottom: 26px; }

/* User: compact right-aligned pill (like ChatGPT mobile/desktop) */
.msg.user { display: flex; justify-content: flex-end; }
.msg.user .bubble {
  background: var(--bubble-user); color: var(--bubble-user-ink);
  border-radius: 20px;
  padding: 10px 16px;
  max-width: 72%;
  font-size: 15.5px; line-height: 1.55; letter-spacing: 0.1px;
  white-space: pre-wrap; word-break: break-word;
}

/* Assistant: full-width plain text, no bubble (like ChatGPT) */
.msg.ai .bubble { background: none; border: none; box-shadow: none; padding: 0; color: var(--text); }

/* AI content typography (mini-markdown output) */
.ai-content { font-size: 16px; line-height: 1.8; word-break: break-word; }
.ai-content p { margin: 0 0 0.9em; }
.ai-content p:last-child { margin-bottom: 0; }
.ai-content ul, .ai-content ol { margin: 0.3em 0 0.9em 1.35em; padding: 0; }
.ai-content ul { list-style: disc; }
.ai-content ol { list-style: decimal; }
.ai-content li { margin: 0.3em 0; }
.ai-content h1, .ai-content h2, .ai-content h3, .ai-content h4 {
  font-weight: 600; letter-spacing: 0.1px; margin: 1.4em 0 0.5em; line-height: 1.35;
}
.ai-content h1 { font-size: 21px; } .ai-content h2 { font-size: 19px; }
.ai-content h3 { font-size: 17px; } .ai-content h4 { font-size: 16px; }
.ai-content code {
  font-family: var(--mono);
  font-size: 13.5px; background: var(--bg-code); color: var(--text);
  border: 1px solid var(--border);
  border-radius: 5px; padding: 1.5px 5px;
}
.ai-content pre {
  background: var(--bg-code); border: 1px solid var(--border); border-radius: 12px;
  padding: 14px 16px; overflow-x: auto; margin: 0.8em 0 1em;
}
.ai-content pre code { background: none; border: none; padding: 0; font-size: 13px; line-height: 1.6; }
.ai-content strong { font-weight: 600; color: var(--text); }
.ai-content em { font-style: italic; }
.ai-content blockquote {
  border-left: 3px solid var(--border-strong);
  padding: 2px 0 2px 14px;
  margin: 0.6em 0 0.9em;
  color: var(--text-2);
}
.ai-content hr { border: none; border-top: 1px solid var(--border); margin: 1.4em 0; }
.ai-content .cite {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 20px; height: 20px; padding: 0 5px;
  border: 1px solid var(--border-strong); border-radius: 6px;
  font-size: 11px; font-weight: 600; color: var(--text-2);
  margin: 0 3px; cursor: pointer; vertical-align: 1px;
  transition: background 0.12s ease, color 0.12s ease;
}
.ai-content .cite:hover { background: var(--bg-hover); color: var(--text); }

/* ---------- Thinking / shimmer ---------- */
.thinking-wrap { padding: 4px 0; }
.thinking-label { font-size: 13px; color: var(--text-3); letter-spacing: 0.2px; margin-bottom: 14px; }
.shimmer { position: relative; overflow: hidden; }
.shimmer::after {
  content: '';
  position: absolute; inset: 0;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.07), transparent);
  transform: translateX(-100%);
  animation: shine 1.6s infinite;
}
@keyframes shine { 100% { transform: translateX(100%); } }
.shimmer-line { height: 13px; border-radius: 7px; background: #2a2a2a; margin-bottom: 10px; }
.shimmer-line.w90 { width: 90%; } .shimmer-line.w60 { width: 60%; }

/* ---------- Sources (ChatGPT-style numbered rows) ---------- */
.sources { margin-top: 22px; }
.sources-label { font-size: 13px; color: var(--text-3); margin-bottom: 10px; }
.source-item {
  display: flex; gap: 10px; align-items: flex-start;
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  padding: 10px 12px; margin-bottom: 8px;
  background: transparent;
  text-decoration: none; color: inherit; cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
.source-item:hover { background: var(--bg-hover); border-color: rgba(255, 255, 255, 0.25); }
.source-num {
  flex: 0 0 auto;
  min-width: 24px; height: 24px; border-radius: 6px;
  background: var(--bg-active); color: var(--text-2);
  font-size: 12px; font-weight: 600;
  display: grid; place-items: center; margin-top: 1px;
}
.source-body { min-width: 0; }
.source-title { display: block; font-size: 13.5px; line-height: 1.45; color: var(--text); }
.source-sub { display: block; font-size: 11.5px; color: var(--text-3); margin-top: 2px; letter-spacing: 0.1px; }

.msg-meta { margin-top: 10px; font-size: 11.5px; color: var(--text-3); letter-spacing: 0.1px; }

/* ---------- Composer ---------- */
#composer-dock { flex: 0 0 auto; padding: 14px 22px 18px; }
.composer {
  max-width: 800px; margin: 0 auto;
  display: flex; align-items: flex-end; gap: 8px;
  background: var(--bg-composer);
  border: 1px solid var(--border-strong);
  border-radius: 28px;
  padding: 10px 10px 10px 18px;
  box-shadow: 0 2px 20px rgba(0, 0, 0, 0.3);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.composer:focus-within {
  border-color: var(--border-focus);
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.3), 0 2px 20px rgba(0, 0, 0, 0.3);
}
#input {
  flex: 1;
  border: none; outline: none; resize: none;
  background: transparent;
  font: inherit; font-size: 15px; line-height: 1.5; letter-spacing: 0.1px;
  color: var(--text);
  max-height: 160px;
  padding: 6px 0;
}
#input::placeholder { color: var(--text-3); }

.btn-send {
  flex: 0 0 auto;
  width: 38px; height: 38px;
  border-radius: 9999px;
  border: none;
  background: var(--accent); color: var(--accent-ink);
  display: grid; place-items: center;
  cursor: pointer;
  transition: opacity 0.12s ease, transform 0.12s ease, background 0.12s ease, color 0.12s ease;
}
.btn-send:disabled { background: rgba(255, 255, 255, 0.14); color: rgba(255, 255, 255, 0.45); cursor: default; }
.btn-send:not(:disabled):hover { opacity: 0.9; }
.btn-send:not(:disabled):active { transform: scale(0.94); }
.btn-send .ico-stop { display: none; }
.btn-send.stop .ico-send { display: none; }
.btn-send.stop .ico-stop { display: block; }
.btn-send.busy svg.ico-send { animation: sendpulse 1s infinite ease-in-out; }
@keyframes sendpulse { 0%, 100% { transform: scale(1); } 50% { transform: scale(0.82); } }

.composer-note {
  max-width: 800px; margin: 8px auto 0;
  text-align: center;
  font-size: 11.5px; color: var(--text-3); letter-spacing: 0.1px;
}

/* ---------- Responsive ---------- */
@media (max-width: 1024px) {
  #sidebar { position: fixed; z-index: 30; top: 0; bottom: 0; left: 0; width: min(286px, 84vw); flex-basis: min(286px, 84vw); transform: translateX(-105%); box-shadow: none; }
  #sidebar.open { transform: translateX(0); box-shadow: rgba(0, 0, 0, 0.5) 0 12px 40px; }
  #btn-menu { display: grid; }
  .msg.user .bubble { max-width: 82%; }
}

/* ---------- Mobile (<640px) ---------- */
@media (max-width: 640px) {
  #topbar { height: 50px; flex-basis: 50px; padding: 0 8px 0 4px; gap: 2px; }
  .topbar-title { font-size: 12.5px; }
  .icon-btn, .ghost-btn { width: 36px; height: 36px; }

  .thread { padding: 20px 14px 150px; }
  .msg { margin-bottom: 22px; }
  .msg.user .bubble { max-width: 85%; padding: 9px 14px; font-size: 14.5px; line-height: 1.55; border-radius: 18px; }
  .ai-content { font-size: 15px; line-height: 1.75; }
  .ai-content pre { padding: 12px; }
  .msg-meta { font-size: 11px; }

  #composer-dock { padding: 8px 10px calc(10px + env(safe-area-inset-bottom)); }
  .composer { border-radius: 24px; padding: 8px 8px 8px 15px; gap: 6px; }
  #input { font-size: 16px; padding: 5px 0; } /* >=16px: cegah auto-zoom iOS */
  .btn-send { width: 36px; height: 36px; }
  .composer-note { font-size: 11px; padding: 0 12px; }

  #empty-state { padding: 0 14px 24px; }
  .empty-title { font-size: 24px; line-height: 1.3; letter-spacing: -0.3px; margin-bottom: 22px; }
  .suggest { grid-template-columns: 1fr; gap: 9px; }
  .suggest-card { padding: 12px 14px; }
  .suggest-card .sc-head { font-size: 13.5px; }
  .suggest-card .sc-desc { font-size: 12px; }
  .empty-note { margin-top: 18px; font-size: 12px; }

  .btn-new { padding: 9px 12px; font-size: 13px; }
  .session-item { font-size: 13px; }
  .model-pill { font-size: 11px; }
}

/* ---------- PDF side panel (split view) ---------- */
.pdf-panel {
  flex: 0 0 0%;
  width: 0;
  max-width: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg-sidebar);
  border-left: 1px solid var(--border);
  overflow: hidden;
  transition: flex-basis .18s ease, width .18s ease, max-width .18s ease;
}
body.pdf-open .pdf-panel {
  flex-basis: 46%;
  width: 46%;
  max-width: 46%;
  min-width: 0;
}
.pdf-head {
  height: 52px;
  flex: 0 0 52px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 0 10px 0 16px;
  border-bottom: 1px solid var(--border);
}
.pdf-title {
  flex: 1;
  min-width: 0;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-2);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.pdf-actions { display: flex; align-items: center; gap: 2px; }
.pdf-ext {
  width: 32px; height: 32px;
  border-radius: 8px;
  display: grid; place-items: center;
  color: var(--text-3);
  transition: background .12s ease, color .12s ease;
}
.pdf-ext:hover { background: var(--bg-hover); color: var(--text); }
#pdf-frame { flex: 1; border: 0; background: #fff; }

@media (max-width: 900px) {
  .pdf-panel {
    position: absolute;
    top: 0; right: 0; bottom: 0;
    z-index: 40;
    width: 100%;
    max-width: 100%;
    transform: translateX(100%);
    border-left: 0;
    transition: transform .2s ease;
    box-shadow: -12px 0 32px rgba(0, 0, 0, 0.45);
  }
  body.pdf-open .pdf-panel {
    flex-basis: auto;
    width: 100%;
    max-width: 100%;
    transform: translateX(0);
  }
}

/* ---------- per-session delete in sidebar ---------- */
.session-item { position: relative; }
.session-del {
  flex: 0 0 auto;
  width: 22px; height: 22px;
  border-radius: 6px;
  display: grid; place-items: center;
  color: var(--text-3);
  opacity: 0;
  transition: opacity .12s ease, background .12s ease, color .12s ease;
  cursor: pointer;
}
.session-item:hover .session-del,
.session-item.active .session-del,
.session-del:focus-visible { opacity: 1; }
.session-del:hover { background: rgba(255, 90, 90, 0.16); color: #ff8a8a; }
.session-item .s-time { transition: opacity .12s ease; }
