:root {
  --bg: #f6f7f9;          /* page */
  --panel: #ffffff;       /* cards, chat surface */
  --side: #f0f2f5;        /* sidebar */
  --line: #e3e6ea;        /* borders */
  --fg: #1f2328;          /* text */
  --muted: #6b7280;       /* secondary text */
  --accent: #4f46e5;        /* indigo (replaces the Plex-mimic amber) */
  --accent-strong: #4338ca; /* darker indigo — hovers / links */
  --accent-ink: #ffffff;    /* text on accent */
  --accent-ring: rgba(79,70,229,.20); /* focus ring */
  --user-bubble: #e0e7ff;   /* indigo-tinted user message */
  --asst-bubble: #f0f2f5;   /* neutral assistant message */
  --active: #e0e7ff;        /* active conversation highlight */
  --error: #c0392b;
  --ok: #37b24d;
  --app-h: 100dvh;          /* JS overrides with visualViewport height (mobile keyboard) */
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }

html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;             /* only #messages scrolls — the page itself never moves under the keyboard */
  overscroll-behavior: none;
}

.view { height: var(--app-h); }
[hidden] { display: none !important; }

/* ---------- Login ---------- */
#login { display: flex; align-items: center; justify-content: center; padding: 24px 16px; }
.login-card {
  width: 100%; max-width: 360px; background: var(--panel);
  border: 1px solid var(--line); border-radius: 16px; padding: 32px 28px;
  box-shadow: 0 12px 40px rgba(20, 30, 50, .08); text-align: center;
}
.login-card h1 { margin: 0 0 4px; font-size: 1.8rem; }
.login-card .sub { margin: 0 0 24px; color: var(--muted); }
#login-form { display: flex; flex-direction: column; gap: 12px; }
#login-form input {
  padding: 13px 15px; font-size: 1rem; border: 1px solid var(--line);
  background: #fff; color: var(--fg); border-radius: 10px; outline: none;
}
#login-form input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-ring); }
#login-btn {
  margin-top: 4px; padding: 13px; font-size: 1rem; font-weight: 650;
  border: none; border-radius: 10px; background: var(--accent); color: var(--accent-ink);
  cursor: pointer;
}
#login-btn:hover { filter: brightness(1.05); }
#login-btn:disabled { opacity: .6; cursor: default; }
.error { color: var(--error); margin: 14px 0 0; min-height: 1.2em; font-size: .92rem; }
.login-card .home-link { display: inline-block; margin-top: 18px; color: var(--muted); text-decoration: none; font-size: .9rem; }
.login-card .home-link:hover { color: var(--accent-strong); }

/* ---------- App shell ---------- */
#app { display: flex; overflow: hidden; }

/* Sidebar */
#sidebar {
  width: 270px; flex: none; background: var(--side); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; height: var(--app-h);
}
.side-head { display: flex; gap: 8px; padding: 14px; align-items: center; }
#new-chat {
  flex: 1; padding: 11px 14px; font-size: .95rem; font-weight: 600; text-align: left;
  border: 1px solid var(--line); background: #fff; color: var(--fg);
  border-radius: 10px; cursor: pointer;
}
#new-chat:hover { border-color: var(--accent); color: var(--accent-strong); }

#conv-list { list-style: none; margin: 0; padding: 0 8px; overflow-y: auto; flex: 1; }
#conv-list li {
  padding: 10px 12px; margin: 2px 0; border-radius: 8px; cursor: pointer;
  font-size: .92rem; color: var(--fg); white-space: nowrap; overflow: hidden;
  text-overflow: ellipsis;
}
#conv-list li:hover { background: #e6e9ee; }
#conv-list li.active { background: var(--active); font-weight: 600; }

.side-foot {
  border-top: 1px solid var(--line); padding: 12px 14px;
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
}
#me-name { flex: 1; min-width: 0; font-size: .9rem; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.side-foot .home-link { color: var(--muted); text-decoration: none; font-size: .82rem; flex: none; white-space: nowrap; }
.side-foot .home-link:hover { color: var(--accent-strong); }
#logout {
  border: none; background: none; color: var(--muted); cursor: pointer;
  font-size: .85rem; padding: 4px; flex: none;
}
#logout:hover { color: var(--error); }

/* Chat column */
#chat { flex: 1; display: flex; flex-direction: column; height: var(--app-h); background: var(--panel); min-width: 0; }
#chat-head {
  display: flex; align-items: center; gap: 12px; padding: 14px 18px;
  border-bottom: 1px solid var(--line);
}
#chat-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
#engine-dot { width: 9px; height: 9px; border-radius: 50%; background: #cbd2da; flex: none; }
#engine-dot.up { background: var(--ok); }
#engine-dot.down { background: var(--error); }

.icon-btn {
  border: none; background: none; font-size: 1.35rem; line-height: 1; color: var(--fg);
  cursor: pointer; padding: 2px 6px; flex: none;
}
#side-open { display: none; }
#side-close { display: none; }

/* Messages */
#messages { flex: 1; overflow-y: auto; padding: 22px 18px; display: flex; flex-direction: column; gap: 16px; }
.msg { display: flex; }
.msg .bubble {
  max-width: min(720px, 82%); padding: 12px 15px; border-radius: 14px;
  line-height: 1.5; white-space: pre-wrap; word-wrap: break-word; font-size: .98rem;
}
.msg.user { justify-content: flex-end; }
.msg.user .bubble { background: var(--user-bubble); border-bottom-right-radius: 4px; }
.msg.assistant .bubble { background: var(--asst-bubble); border-bottom-left-radius: 4px; }
.msg.assistant .bubble.err { background: #fdecea; color: var(--error); }

/* Empty-state hint on a fresh chat */
#messages .empty {
  margin: auto; text-align: center; color: var(--muted); max-width: 44ch; line-height: 1.5;
  text-wrap: balance;   /* even 2 lines, no orphaned last word */
}

/* Streaming caret + pre-first-token thinking dots */
.bubble .caret {
  display: inline-block; width: 7px; height: 1.05em; margin-left: 1px; vertical-align: -2px;
  background: var(--accent); animation: blink 1s steps(2) infinite;
}
@keyframes blink { 50% { opacity: 0; } }
.thinking { display: inline-flex; gap: 5px; }
.thinking span {
  width: 7px; height: 7px; border-radius: 50%; background: var(--muted);
  animation: bounce 1s infinite ease-in-out;
}
.thinking span:nth-child(2) { animation-delay: .15s; }
.thinking span:nth-child(3) { animation-delay: .3s; }
@keyframes bounce { 0%,80%,100% { transform: translateY(0); opacity: .5; } 40% { transform: translateY(-6px); opacity: 1; } }

/* Composer */
#composer {
  border-top: 1px solid var(--line); padding: 12px 16px; display: flex; gap: 10px;
  align-items: flex-end;
}
#input {
  flex: 1; resize: none; max-height: 180px; padding: 12px 14px; font: inherit;
  border: 1px solid var(--line); border-radius: 12px; outline: none; background: #fff; color: var(--fg);
  overflow-y: hidden;   /* JS flips to auto only when content exceeds max-height (real multi-line) */
}
#input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-ring); }
/* When the box actually scrolls (multi-line at max height), give it a thin, button-less
   scrollbar. display:none on the buttons removes Windows' up/down arrows that otherwise
   render over the rounded corners; the track margin keeps the thumb clear of the radius,
   and the padding-box border floats the thumb in from the right edge. */
#input::-webkit-scrollbar { width: 12px; }
#input::-webkit-scrollbar-button { display: none; width: 0; height: 0; }
#input::-webkit-scrollbar-track { background: transparent; margin: 12px 0; }
#input::-webkit-scrollbar-thumb {
  background: #c3cad3; border-radius: 8px;
  border: 3px solid #fff; background-clip: padding-box;
}
#send {
  flex: none; padding: 12px 20px; font-weight: 650; border: none; border-radius: 12px;
  background: var(--accent); color: var(--accent-ink); cursor: pointer; min-height: 46px;
}
#send:hover { filter: brightness(1.05); }
#send:disabled { opacity: .55; cursor: default; }

#scrim { display: none; }

/* ---------- Mobile: off-canvas sidebar ---------- */
@media (max-width: 760px) {
  #sidebar {
    position: fixed; z-index: 20; top: 0; left: 0; height: var(--app-h);
    transform: translateX(-100%); transition: transform .22s ease;
    box-shadow: 0 0 40px rgba(0,0,0,.2);
  }
  #app.side-open #sidebar { transform: translateX(0); }
  #scrim { display: none; position: fixed; inset: 0; z-index: 15; background: rgba(0,0,0,.35); }
  #app.side-open #scrim { display: block; }
  #side-open { display: inline-block; }
  #side-close { display: inline-block; margin-left: auto; }
}
