:root {
  --bg: #ffffff;
  --panel: #f6f7f9;
  --line: #e4e7ec;
  --text: #101828;
  --muted: #5d6676;
  --primary: #2459d6;
  --primary-text: #ffffff;
  --user: #eaf0fe;
  --code: #f2f4f7;
  --danger: #b42318;
  --radius: 12px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "Cascadia Code", Menlo, Consolas, monospace;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f172a;
    --panel: #111c33;
    --line: #23304a;
    --text: #e6eaf2;
    --muted: #9aa6bb;
    --primary: #6d9bff;
    --primary-text: #0b1220;
    --user: #1b2a4a;
    --code: #17233d;
    --danger: #ff8a80;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font: 16px/1.55 var(--font); background: var(--bg); color: var(--text); -webkit-font-smoothing: antialiased; }
[hidden] { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

/* buttons */
.btn { font: inherit; font-weight: 600; border-radius: 10px; padding: 0.6em 1.1em; cursor: pointer; border: 1px solid transparent; }
.btn.primary { background: var(--primary); color: var(--primary-text); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; color: var(--text); border-color: var(--line); }
.btn.ghost:hover { background: var(--panel); }
.btn.block { width: 100%; }
.btn:disabled { opacity: 0.6; cursor: default; }
.icon-btn { font: inherit; font-size: 1.2rem; background: none; border: 0; color: var(--text); padding: 0.3em 0.5em; border-radius: 8px; cursor: pointer; }
.chip { font: inherit; font-size: 0.93rem; background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 999px; padding: 0.5em 1em; cursor: pointer; }
.chip:hover { border-color: var(--primary); }
.badge { font-size: 0.75rem; font-weight: 600; background: var(--danger); color: #fff; border-radius: 999px; padding: 0.15em 0.6em; }

/* sign in */
.signin { min-height: 100%; display: grid; place-items: center; padding: 24px; background: var(--panel); }
.signin-card { max-width: 380px; text-align: center; background: var(--bg); border: 1px solid var(--line); border-radius: 18px; padding: 40px 32px; }
.signin-card h1 { margin: 12px 0 4px; font-size: 1.6rem; }
.signin-card p { color: var(--muted); }
.error { color: var(--danger); min-height: 1.5em; }

/* layout */
.app { display: grid; grid-template-columns: 280px 1fr; height: 100%; }
.sidebar { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; gap: 12px; padding: 16px; min-height: 0; }
.sidebar-head { display: flex; justify-content: space-between; align-items: center; }
.brand { display: inline-flex; gap: 8px; align-items: center; font-weight: 700; }
.sidebar nav { flex: 1; overflow-y: auto; min-height: 0; }
.chat-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; }
.chat-list button { width: 100%; text-align: left; font: inherit; font-size: 0.93rem; color: var(--text); background: none; border: 0; border-radius: 8px; padding: 0.55em 0.7em; cursor: pointer; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-list button:hover { background: var(--line); }
.chat-list button.active { background: var(--bg); box-shadow: inset 0 0 0 1px var(--line); font-weight: 600; }
.sidebar-foot { display: grid; gap: 8px; }
.who { font-size: 0.85rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; }

.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; height: 100%; }
.topbar { display: flex; align-items: center; gap: 8px; padding: 12px 20px; border-bottom: 1px solid var(--line); }
.chat-title { font-size: 1rem; margin: 0; flex: 1; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }

.messages { flex: 1; overflow-y: auto; padding: 24px 20px; display: flex; flex-direction: column; gap: 18px; }
.messages > * { width: 100%; max-width: 820px; margin-inline: auto; }
.empty { margin-top: 12vh; text-align: center; }
.empty h2 { font-size: 1.5rem; margin-bottom: 20px; }
.suggestions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }

.msg .body { overflow-wrap: anywhere; }
.msg.user { display: flex; flex-direction: column; align-items: flex-end; }
.msg.user .body { background: var(--user); padding: 0.7em 1em; border-radius: var(--radius) var(--radius) 4px var(--radius); max-width: 85%; white-space: pre-wrap; }
.msg.agent .body > :first-child { margin-top: 0; }
.msg.agent .body > :last-child { margin-bottom: 0; }
.msg.agent h3, .msg.agent h4, .msg.agent h5, .msg.agent h6 { margin: 1.1em 0 0.4em; line-height: 1.3; }
.msg.agent code { font-family: var(--mono); font-size: 0.9em; background: var(--code); padding: 0.1em 0.35em; border-radius: 5px; }
.msg.agent pre { background: var(--code); padding: 12px 14px; border-radius: 10px; overflow-x: auto; }
.msg.agent pre code { background: none; padding: 0; }
.msg.agent a { color: var(--primary); }
.msg.agent .table { overflow-x: auto; }
.msg.agent table { border-collapse: collapse; font-size: 0.93rem; margin: 0.5em 0; }
.msg.agent th, .msg.agent td { border: 1px solid var(--line); padding: 0.4em 0.7em; text-align: left; }
.msg.agent th { background: var(--panel); }
.status { color: var(--muted); font-size: 0.9rem; margin: 0 0 6px; }
.working .status::before { content: ""; display: inline-block; width: 8px; height: 8px; margin-right: 8px; border-radius: 50%; background: var(--primary); animation: pulse 1s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: 0.25; } }
@media (prefers-reduced-motion: reduce) { .working .status::before { animation: none; } }
.note { color: var(--danger); font-size: 0.9rem; margin: 6px 0 0; }
.meta { color: var(--muted); font-size: 0.8rem; margin: 6px 0 0; }
.meta:empty { display: none; }

.composer { display: flex; gap: 8px; align-items: flex-end; width: 100%; max-width: 860px; margin: 0 auto; padding: 12px 20px 4px; }
.composer textarea { flex: 1; font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--line); border-radius: var(--radius); padding: 0.7em 0.9em; resize: none; max-height: 240px; overflow-y: hidden; line-height: 1.5; }
.composer textarea:focus { border-color: var(--primary); outline: none; box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 25%, transparent); }
.hint { text-align: center; color: var(--muted); font-size: 0.78rem; margin: 4px 20px calc(10px + env(safe-area-inset-bottom)); }

.only-mobile { display: none; }
@media (max-width: 800px) {
  .app { grid-template-columns: 1fr; }
  .only-mobile { display: inline-block; }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: min(86vw, 320px); z-index: 10; transform: translateX(-100%); transition: transform 0.2s ease; box-shadow: 0 0 40px rgb(0 0 0 / 0.25); }
  .sidebar-open .sidebar { transform: none; }
  .messages { padding: 16px 14px; }
  .composer { padding: 10px 14px 4px; }
}
