:root {
  --bg: #0f1117; --panel: #1a1d27; --panel-2: #232734;
  --text: #e8eaf0; --muted: #8b90a0; --accent: #6c8cff; --accent-2: #8a6cff;
  --user: #2b3350; --bot: #1f2431; --error: #ff6b6b; --ok: #4ecb8f;
  --border: #2c3140; --radius: 14px;
}
@media (prefers-color-scheme: light) {
  :root { --bg: #f5f6fa; --panel: #fff; --panel-2: #eef0f6; --text: #1a1d27;
          --muted: #6b7080; --user: #dbe4ff; --bot: #eef0f6; --border: #e2e5ee; }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg); color: var(--text);
}
.hidden { display: none !important; }
input, textarea, button { font: inherit; }
input {
  padding: .6rem .8rem; border-radius: 10px; border: 1px solid var(--border);
  background: var(--panel-2); color: var(--text); width: 100%;
}
button { cursor: pointer; border: none; border-radius: 10px; padding: .6rem 1rem;
  background: var(--panel-2); color: var(--text); font-weight: 600; }
button.primary { background: var(--accent); color: #fff; }
button.ghost { background: transparent; color: var(--muted); font-weight: 500; }
button.icon-only { padding: .4rem .6rem; }
button:hover { filter: brightness(1.08); }
.error { color: var(--error); min-height: 1.2em; font-size: .9rem; margin: 0; }
.ok-msg { color: var(--ok); min-height: 1.2em; font-size: .9rem; }

/* Login */
#login-view { height: 100vh; display: flex; align-items: center; justify-content: center; }
.card { background: var(--panel); padding: 2.2rem; border-radius: var(--radius);
  width: min(90vw, 360px); box-shadow: 0 10px 40px rgba(0,0,0,.25);
  display: flex; flex-direction: column; gap: .7rem; text-align: center; }
.card h1 { margin: 0; font-size: 1.6rem; }
.subtitle { margin: 0 0 .8rem; color: var(--muted); }

/* Chat view: sidebar + main */
#chat-view { height: 100vh; display: flex; }
#sidebar { width: 280px; flex-shrink: 0; background: var(--panel);
  border-right: 1px solid var(--border); display: flex; flex-direction: column;
  gap: .6rem; padding: .8rem; }
.side-head { display: flex; }
#new-chat-btn { width: 100%; }
#conv-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: .3rem; }
.conv { padding: .55rem .6rem; border-radius: 10px; cursor: pointer;
  display: flex; align-items: center; gap: .4rem; }
.conv:hover { background: var(--panel-2); }
/* inset shadow (not outline) so the ring isn't clipped by the list's overflow */
.conv.active { background: var(--panel-2); box-shadow: inset 0 0 0 1px var(--accent); }
.conv .txt { flex: 1; min-width: 0; }
.conv .t { font-size: .9rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv .s { font-size: .75rem; color: var(--muted); white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis; }
.conv .del { opacity: 0; color: var(--muted); background: none; padding: .1rem .3rem; }
.conv:hover .del { opacity: 1; }
.side-foot { display: flex; align-items: center; gap: .4rem; border-top: 1px solid var(--border);
  padding-top: .6rem; }
.who { flex: 1; color: var(--muted); font-size: .85rem; white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis; }

#main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
header { display: flex; align-items: center; gap: .6rem; padding: .7rem 1rem;
  background: var(--panel); border-bottom: 1px solid var(--border); }
.conv-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.spacer { flex: 1; }
.sync-state { font-size: .78rem; color: var(--muted); }
main#messages { flex: 1; overflow-y: auto; padding: 1.2rem; display: flex;
  flex-direction: column; gap: .8rem;
  -webkit-user-select: text; user-select: text; cursor: auto; }
.msg { max-width: 75%; padding: .7rem 1rem; border-radius: var(--radius);
  line-height: 1.45; white-space: pre-wrap; word-wrap: break-word;
  -webkit-user-select: text; user-select: text; }  /* selectable + copyable */
.msg.user { align-self: flex-end; background: var(--user); border-bottom-right-radius: 4px; }
.msg.bot { align-self: flex-start; background: var(--bot); border-bottom-left-radius: 4px; }
.msg.tools { align-self: flex-start; color: var(--muted); font-size: .78rem;
  background: none; padding: 0 .4rem; user-select: none; }
.msg.pending { border: 1px solid var(--accent); }
.msg img.msg-thumb { max-width: 220px; max-height: 200px; border-radius: 8px;
  display: block; margin-bottom: .4rem; }

/* Rendered Markdown in bot replies. white-space:normal so block layout (not the
   raw pre-wrap) governs; wide tables/code scroll inside the bubble. */
.msg.md { white-space: normal; }
.msg.md > :first-child { margin-top: 0; }
.msg.md > :last-child { margin-bottom: 0; }
.msg.md p { margin: .5em 0; }
.msg.md h1, .msg.md h2, .msg.md h3, .msg.md h4, .msg.md h5, .msg.md h6 {
  margin: .7em 0 .35em; line-height: 1.25; }
.msg.md h1 { font-size: 1.35em; } .msg.md h2 { font-size: 1.2em; }
.msg.md h3 { font-size: 1.08em; } .msg.md h4 { font-size: 1em; }
.msg.md ul, .msg.md ol { margin: .4em 0; padding-left: 1.4em; }
.msg.md li { margin: .15em 0; }
.msg.md a { color: var(--accent); }
.msg.md code { background: var(--panel-2); padding: .12em .35em; border-radius: 6px;
  font-family: ui-monospace, "Cascadia Code", Consolas, monospace; font-size: .88em; }
.msg.md pre { background: var(--panel-2); border: 1px solid var(--border);
  border-radius: 10px; padding: .7rem .8rem; margin: .5em 0; overflow-x: auto; }
.msg.md pre code { background: none; padding: 0; font-size: .85em; }
.msg.md blockquote { margin: .5em 0; padding: .2em .9em; color: var(--muted);
  border-left: 3px solid var(--border); }
.msg.md hr { border: none; border-top: 1px solid var(--border); margin: .8em 0; }
.msg.md table { border-collapse: collapse; margin: .5em 0; display: block;
  width: max-content; max-width: 100%; overflow-x: auto; }
.msg.md th, .msg.md td { border: 1px solid var(--border); padding: .35rem .6rem;
  text-align: left; }
.msg.md thead th { background: var(--panel-2); font-weight: 600; }
.msg.md tbody tr:nth-child(even) td { background: rgba(127,127,127,.06); }

/* animated typing indicator */
.typing { display: inline-flex; gap: 5px; align-items: center; padding: .35rem 0; }
.typing span { width: 8px; height: 8px; border-radius: 50%; background: var(--muted);
  animation: blink 1.2s infinite both; }
.typing span:nth-child(2) { animation-delay: .2s; }
.typing span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; transform: translateY(0); }
  40% { opacity: 1; transform: translateY(-3px); } }

.toast { position: fixed; left: 50%; bottom: 1.4rem; transform: translateX(-50%) translateY(1rem);
  background: var(--panel); color: var(--text); border: 1px solid var(--border);
  border-radius: 999px; padding: .5rem 1rem; font-size: .9rem; box-shadow: 0 6px 20px rgba(0,0,0,.25);
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 50; }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

footer { display: flex; flex-direction: column; gap: .5rem; padding: .8rem 1rem;
  background: var(--panel); border-top: 1px solid var(--border); }
.composer-row { display: flex; align-items: flex-end; gap: .5rem; }
.attach-preview { display: flex; align-items: center; gap: .5rem; padding: .4rem .6rem;
  background: var(--panel-2); border-radius: 10px; font-size: .85rem; }
.attach-thumb { width: 40px; height: 40px; object-fit: cover; border-radius: 6px; }
#attach-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#composer { flex: 1; resize: none; min-height: 76px; max-height: 200px;
  line-height: 1.4; padding: .55rem .8rem; }
button.icon { width: 44px; height: 44px; padding: 0; border-radius: 12px;
  font-size: 1.2rem; flex-shrink: 0; }
button.icon.send { background: var(--accent); color: #fff; }
button.icon.send.stop { background: var(--error); }
button.icon.recording { background: var(--error); color: #fff; animation: pulse 1s infinite; }
@keyframes pulse { 50% { filter: brightness(1.3); } }

/* Modals — sticky title bar, scrollable body, fixed footer */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.5); display: flex;
  align-items: center; justify-content: center; z-index: 10; }
.modal-card { background: var(--panel); border-radius: var(--radius);
  width: min(94vw, 460px); max-height: 90vh; overflow: hidden;
  display: flex; flex-direction: column;
  box-shadow: 0 16px 48px rgba(0,0,0,.35); }
.modal-header { flex-shrink: 0; display: flex; align-items: center; gap: .6rem;
  padding: .9rem 1.2rem; border-bottom: 1px solid var(--border);
  background: var(--panel-2); border-radius: var(--radius) var(--radius) 0 0; }
.modal-title { font-weight: 700; font-size: 1.05rem; flex: 1; }
.modal-close { font-size: 1rem; }
.modal-body { flex: 1; overflow-y: auto; padding: 1.2rem;
  display: flex; flex-direction: column; gap: .6rem; }
.modal-card label { display: flex; flex-direction: column; gap: .25rem;
  font-size: .82rem; color: var(--muted); }
.modal-card select { padding: .6rem .8rem; border-radius: 10px;
  border: 1px solid var(--border); background: var(--panel-2); color: var(--text); }
.modal-card hr { width: 100%; border: none; border-top: 1px solid var(--border); margin: .2rem 0; }
.modal-actions { flex-shrink: 0; display: flex; align-items: center; gap: .5rem;
  padding: .8rem 1.2rem; border-top: 1px solid var(--border); }
.modal-actions .ok-msg { min-height: 0; margin: 0; }

/* Logs modal — wider card, monospace scrolling tail */
.modal-card.wide { width: min(96vw, 820px); height: min(88vh, 720px); }
.log-level { flex-direction: row !important; align-items: center; gap: .35rem;
  font-size: .78rem; color: var(--muted); }
.log-level select { padding: .25rem .4rem; border-radius: 8px;
  border: 1px solid var(--border); background: var(--panel); color: var(--text); }
.modal-body.log-body { padding: 0; }
.log-view { flex: 1; overflow-y: auto; padding: .7rem .9rem; font-size: .8rem;
  line-height: 1.5; font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
  background: var(--bg); height: 100%; }
.log-line { white-space: pre-wrap; word-break: break-word; padding: .05rem 0;
  border-bottom: 1px solid rgba(127,127,127,.06); }
.log-line .log-t { color: var(--muted); }
.log-line .log-lvl { font-weight: 700; }
.log-line .log-src { color: var(--accent); }
.log-line.lvl-WARNING { background: rgba(255,193,7,.08); }
.log-line.lvl-WARNING .log-lvl { color: #e0a800; }
.log-line.lvl-ERROR, .log-line.lvl-CRITICAL { background: rgba(255,107,107,.10); }
.log-line.lvl-ERROR .log-lvl, .log-line.lvl-CRITICAL .log-lvl { color: var(--error); }
.log-line.lvl-INFO .log-lvl { color: var(--ok); }
.log-line.lvl-DEBUG { opacity: .7; }
.log-empty { color: var(--muted); padding: 1rem; text-align: center; font-family: system-ui; }
.log-status { font-size: .78rem; color: var(--muted); }

/* login "save password" row */
.check-row { display: flex; align-items: center; gap: .5rem; font-size: .85rem;
  color: var(--muted); text-align: left; }
.check-row input { width: auto; }

.mobile-only { display: none; }
@media (max-width: 720px) {
  #sidebar { position: absolute; z-index: 5; height: 100%; box-shadow: 4px 0 20px rgba(0,0,0,.3); }
  #sidebar.collapsed { display: none; }
  .mobile-only { display: inline-flex; }
}
