:root {
  --bg: #ffffff;
  --side: #f6f6f4;
  --text: #1d1d1b;
  --muted: #7a7a75;
  --line: #e7e6e1;
  --accent: #d7262b;
  --accent-ink: #ffffff;
  --bubble: #f2f2ef;
  --ok: #1f9d55;
  --err: #c62828;
  --radius: 14px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #171716;
    --side: #1f1f1e;
    --text: #ececea;
    --muted: #9b9b96;
    --line: #2f2f2d;
    --bubble: #262624;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
.hidden { display: none !important; }
.app { height: 100%; }
.center { min-height: 100%; display: flex; align-items: center; justify-content: center; padding: 24px 16px; }
.card {
  width: 100%; max-width: 360px; display: flex; flex-direction: column; gap: 14px;
  padding: 32px 28px; border: 1px solid var(--line); border-radius: 18px; background: var(--bg);
}
.card.wide { max-width: 520px; }
.brand { font-weight: 800; letter-spacing: .04em; font-size: 22px; }
.brand::first-letter { color: var(--accent); }
.small-brand { font-size: 18px; }
h2 { margin: 4px 0 2px; font-size: 20px; font-weight: 600; }
label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: var(--muted); }
input, textarea {
  font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--line); border-radius: 10px; padding: 11px 12px; outline: none;
}
input:focus, textarea:focus { border-color: var(--text); }
button { font: inherit; cursor: pointer; }
button.primary {
  background: var(--text); color: var(--bg); border: 0; border-radius: 10px; padding: 12px; font-weight: 600;
}
button.primary:disabled { opacity: .6; cursor: default; }
button.link { background: none; border: 0; color: var(--muted); padding: 4px 0; text-align: left; font-size: 14px; }
button.link:hover { color: var(--text); }
.err { color: var(--err); font-size: 14px; min-height: 0; }
.err:empty { display: none; }
.muted { color: var(--muted); }
.small { font-size: 13px; word-break: break-all; }
.overlay { position: fixed; inset: 0; background: color-mix(in srgb, var(--bg) 85%, transparent); z-index: 50; }
.card.ok { align-items: center; text-align: center; font-size: 18px; max-width: 280px; }
.okmark { font-size: 40px; color: var(--ok); line-height: 1; }

/* chat layout */
.chat { display: flex; height: 100%; }
.sidebar {
  width: 270px; flex: 0 0 270px; background: var(--side); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; padding: 16px 12px; gap: 12px;
}
.side-top { display: flex; flex-direction: column; gap: 14px; padding: 0 6px; }
.newchat {
  background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; text-align: left;
  color: var(--text);
}
.newchat:hover { border-color: var(--muted); }
.chat-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }
.chat-item {
  display: block; padding: 8px 10px; border-radius: 8px; color: var(--text); text-decoration: none;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 14px;
}
.chat-item:hover { background: color-mix(in srgb, var(--text) 6%, transparent); }
.chat-item.active { background: color-mix(in srgb, var(--text) 10%, transparent); }
.side-bottom { display: flex; flex-direction: column; gap: 4px; padding: 0 6px; }
.status { display: flex; align-items: center; gap: 8px; font-size: 14px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ok); display: inline-block; }
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar { height: 52px; display: flex; align-items: center; gap: 10px; padding: 0 16px; border-bottom: 1px solid var(--line); }
.topbar-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex: 1; }
.icon { display: none; background: none; border: 0; font-size: 20px; color: var(--text); padding: 4px 8px; }
.mobile-only { display: none; }
.messages { flex: 1; overflow-y: auto; padding: 24px 16px 8px; display: flex; flex-direction: column; gap: 18px; }
.messages > * { width: 100%; max-width: 780px; margin: 0 auto; }
.hello { margin-top: 12vh; text-align: center; }
.hello h1 { font-size: 30px; margin: 0 0 4px; font-weight: 600; }
.hello p { color: var(--muted); margin: 0; font-size: 18px; }
.msg { display: flex; }
.msg.user { justify-content: flex-end; }
.bubble { max-width: 100%; overflow-wrap: anywhere; }
.msg.user .bubble { background: var(--bubble); padding: 10px 14px; border-radius: var(--radius); max-width: 85%; white-space: pre-wrap; }
.msg.bot .bubble p { margin: 0 0 10px; }
.msg.bot .bubble p:last-child { margin-bottom: 0; }
.msg.bot .bubble ul, .msg.bot .bubble ol { margin: 0 0 10px; padding-left: 22px; }
.msg.bot .bubble h3, .msg.bot .bubble h4, .msg.bot .bubble h5 { margin: 14px 0 6px; }
.msg.bot code { background: var(--bubble); padding: 1px 5px; border-radius: 5px; font-size: 14px; }
.msg.bot pre { background: var(--bubble); padding: 12px; border-radius: 10px; overflow-x: auto; }
.tbl { overflow-x: auto; margin: 6px 0 12px; }
.tbl table { border-collapse: collapse; font-size: 14px; min-width: 50%; }
.tbl th, .tbl td { border-bottom: 1px solid var(--line); padding: 6px 10px; text-align: left; vertical-align: top; white-space: nowrap; }
.tbl th { font-weight: 600; white-space: nowrap; }
.pending { display: flex; align-items: center; gap: 10px; color: var(--muted); }
.spinner {
  width: 16px; height: 16px; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%;
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.composer {
  width: 100%; max-width: 780px; margin: 0 auto 18px; padding: 0 16px; display: flex; align-items: flex-end; gap: 8px;
}
.composer textarea {
  flex: 1; resize: none; max-height: 200px; border-radius: 16px; padding: 13px 16px; line-height: 1.45;
}
.send {
  width: 46px; height: 46px; border-radius: 50%; border: 0; background: var(--text); color: var(--bg); font-size: 18px;
}
.send:disabled { opacity: .4; }

@media (max-width: 760px) {
  .sidebar { position: fixed; z-index: 40; inset: 0 auto 0 0; transform: translateX(-100%); transition: transform .2s; box-shadow: 0 0 40px rgba(0,0,0,.15); }
  .sidebar.open { transform: none; }
  .icon, .mobile-only { display: inline-flex; }
  .hello { margin-top: 8vh; }
  .composer { margin-bottom: max(12px, env(safe-area-inset-bottom)); padding: 0 12px; }
  .messages { padding: 16px 12px 8px; }
}
