:root {
  --bg: #ffffff; --side: #f6f7f9; --panel: #ffffff; --line: #e4e7ec; --text: #1d2433; --dim: #6b7385;
  --acc: #2f6fe4; --acc-text: #ffffff; --user: #eef3fd; --code: #0f141c; --err: #c4352b; --think: #f6f7f9;
  --shadow: 0 6px 24px rgba(16, 24, 40, .08);
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #0f1218; --side: #141821; --panel: #181d27; --line: #262d3a; --text: #e6e9ef; --dim: #8d95a6;
    --acc: #4c8bf5; --user: #1c2638; --code: #0b0e13; --err: #f07067; --think: #151a23; --shadow: 0 6px 24px rgba(0, 0, 0, .35); }
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; -webkit-text-size-adjust: 100%; }
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 1px solid var(--line); background: var(--panel); border-radius: 10px; padding: 8px 12px; }
button:hover { border-color: var(--acc); }
button.primary { background: var(--acc); color: var(--acc-text); border-color: var(--acc); }
button.link { background: none; border: 0; padding: 4px 0; color: var(--dim); }
button.link:hover { color: var(--text); }
button.icon { background: none; border: 0; font-size: 20px; padding: 4px 8px; line-height: 1; }
.muted { color: var(--dim); }

/* login */
.login { min-height: 100%; display: flex; align-items: center; justify-content: center; padding: 24px 16px; }
.login-box { width: 100%; max-width: 380px; display: flex; flex-direction: column; gap: 12px; text-align: center; }
.logo { width: 56px; height: 56px; border-radius: 16px; margin: 0 auto 4px; background: var(--acc); color: var(--acc-text); display: flex; align-items: center; justify-content: center; font-weight: 700; letter-spacing: .5px; }
.login-box h1 { margin: 0; font-size: 24px; }
.login-box p { margin: 0; }
.login-box input { width: 100%; padding: 12px 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--panel); font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 14px; }
.login-box input:focus { outline: 2px solid var(--acc); outline-offset: -1px; }
.login-box button { padding: 12px; font-weight: 600; }
.error { color: var(--err); }

/* layout */
.app { display: flex; height: 100%; height: 100dvh; }
.sidebar { width: 270px; flex: none; background: var(--side); border-right: 1px solid var(--line); display: flex; flex-direction: column; padding: 12px; gap: 10px;
  padding-top: max(12px, env(safe-area-inset-top)); padding-bottom: max(12px, env(safe-area-inset-bottom)); }
.wide { width: 100%; }
.chat-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; margin: 0 -4px; padding: 0 4px; }
.chat-item { display: flex; align-items: center; gap: 4px; border-radius: 8px; padding: 7px 8px; cursor: pointer; color: var(--text); }
.chat-item:hover { background: var(--line); }
.chat-item.active { background: var(--user); }
.chat-item span { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.chat-item button { border: 0; background: none; padding: 0 4px; color: var(--dim); visibility: hidden; font-size: 16px; }
.chat-item:hover button, .chat-item.active button { visibility: visible; }
.chat-item button:hover { color: var(--err); }
.side-foot { border-top: 1px solid var(--line); padding-top: 10px; display: flex; flex-direction: column; gap: 6px; font-size: 13px; }
.usage { color: var(--dim); }
.usage .bar { height: 5px; border-radius: 3px; background: var(--line); margin-top: 5px; overflow: hidden; }
.usage .bar i { display: block; height: 100%; background: var(--acc); }
.scrim { position: fixed; inset: 0; background: rgba(0, 0, 0, .4); z-index: 9; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.top { display: flex; align-items: center; gap: 8px; padding: 10px 14px; border-bottom: 1px solid var(--line); padding-top: max(10px, env(safe-area-inset-top)); }
.top .title { flex: 1; min-width: 0; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.top select { border: 1px solid var(--line); background: var(--panel); border-radius: 10px; padding: 6px 8px; }
#menuBtn { display: none; }

.messages { flex: 1; overflow-y: auto; padding: 18px 16px 8px; scroll-behavior: auto; }
.thread { max-width: 780px; margin: 0 auto; display: flex; flex-direction: column; gap: 18px; }
.empty { max-width: 780px; margin: 18vh auto 0; text-align: center; color: var(--dim); }
.empty h2 { color: var(--text); margin: 0 0 6px; font-size: 24px; font-weight: 600; }
.user-wrap { align-self: flex-end; max-width: 85%; display: flex; flex-direction: column; align-items: flex-end; gap: 4px; }
.msg-user { background: var(--user); border-radius: 16px 16px 4px 16px; padding: 9px 14px; white-space: pre-wrap; word-wrap: break-word; overflow-wrap: anywhere; max-width: 100%; }
.user-wrap .files { justify-content: flex-end; }
.user-wrap .actions { opacity: 0; transition: opacity .15s; }
.user-wrap:hover .actions, .user-wrap:focus-within .actions { opacity: .8; }
.edit-box { width: min(640px, 80vw); display: flex; flex-direction: column; gap: 6px; }
.edit-box textarea { width: 100%; min-height: 90px; border: 1px solid var(--acc); border-radius: 12px; padding: 9px 12px; background: var(--panel); resize: vertical; }
.edit-box div { display: flex; gap: 6px; justify-content: flex-end; }
.steps { display: flex; flex-direction: column; gap: 4px; margin-bottom: 8px; font-size: 13.5px; color: var(--dim); }
.step { border: 1px solid var(--line); border-radius: 10px; padding: 0 10px; background: var(--think); }
.step summary { cursor: pointer; padding: 5px 0; list-style: none; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.step summary::-webkit-details-marker { display: none; }
.step.run summary::after { content: ' …'; }
.step.bad summary { color: var(--err); }
.step ol { margin: 0 0 8px; padding-left: 1.3em; }
.step li { margin: 3px 0; overflow-wrap: anywhere; }
.step a, .sources a { color: var(--acc); text-decoration: none; }
.step a:hover, .sources a:hover { text-decoration: underline; }
.step .snip { color: var(--dim); font-size: 12.5px; }
.sources { margin-top: 10px; font-size: 13px; color: var(--dim); }
.sources ol { margin: 4px 0 0; padding-left: 1.4em; }
.sources .dom { color: var(--dim); }
.chat-search { width: 100%; padding: 7px 10px; border-radius: 9px; border: 1px solid var(--line); background: var(--panel); font-size: 14px; }
.chat-item .rename { font-size: 13px; }
.side-links { display: flex; justify-content: space-between; }
.dlg { border: 1px solid var(--line); border-radius: 14px; background: var(--panel); color: var(--text); padding: 18px; width: min(560px, 92vw); }
.dlg::backdrop { background: rgba(0, 0, 0, .45); }
.dlg h2 { margin: 0 0 12px; font-size: 18px; }
.dlg .field { display: flex; flex-direction: column; gap: 6px; font-size: 14px; }
.dlg textarea { width: 100%; border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; background: var(--bg); resize: vertical; }
.dlg .check { display: flex; gap: 8px; align-items: center; margin-top: 12px; font-size: 14px; }
.dlg-acts { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
.toast { position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 8px 14px; border-radius: 10px; font-size: 14px; z-index: 30; max-width: 90vw; }
.msg-bot { min-width: 0; }
.msg-bot .err { color: var(--err); margin: 4px 0; }
.msg-bot .note { color: var(--dim); font-size: 13px; margin-top: 4px; }
.actions { display: flex; gap: 2px; margin-top: 4px; opacity: .75; }
.actions button { border: 0; background: none; padding: 3px 7px; font-size: 13px; color: var(--dim); border-radius: 6px; }
.actions button:hover { background: var(--line); color: var(--text); }
.think { border-left: 3px solid var(--line); background: var(--think); border-radius: 0 8px 8px 0; margin: 0 0 8px; padding: 2px 10px; font-size: 13.5px; color: var(--dim); }
.think summary { cursor: pointer; padding: 4px 0; user-select: none; }
.think .think-body { white-space: pre-wrap; padding-bottom: 6px; max-height: 340px; overflow-y: auto; }
.typing { display: inline-flex; gap: 4px; padding: 6px 0; }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--dim); animation: blink 1.2s infinite ease-in-out; }
.typing i:nth-child(2) { animation-delay: .2s; } .typing i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }

/* markdown */
.md { overflow-wrap: anywhere; }
.md > :first-child { margin-top: 0; } .md > :last-child { margin-bottom: 0; }
.md p, .md ul, .md ol, .md blockquote, .md table { margin: .55em 0; }
.md h1, .md h2, .md h3, .md h4 { margin: 1em 0 .4em; line-height: 1.3; }
.md h1 { font-size: 1.35em; } .md h2 { font-size: 1.2em; } .md h3 { font-size: 1.07em; }
.md ul, .md ol { padding-left: 1.4em; }
.md a { color: var(--acc); }
.md blockquote { border-left: 3px solid var(--line); margin-left: 0; padding-left: 12px; color: var(--dim); }
.md code { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: .9em; background: var(--think); border: 1px solid var(--line); border-radius: 5px; padding: 1px 5px; }
.md pre { position: relative; background: var(--code); color: #e6edf3; border-radius: 10px; padding: 12px 14px; overflow-x: auto; margin: .7em 0; }
.md pre code { background: none; border: 0; padding: 0; font-size: 13px; line-height: 1.5; display: block; color: inherit; }
.md pre .copy { position: absolute; top: 6px; right: 6px; padding: 2px 8px; font-size: 12px; background: #1f2630; color: #c9d1d9; border: 1px solid #30363d; border-radius: 6px; opacity: 0; transition: opacity .15s; }
.md pre:hover .copy, .md pre .copy:focus { opacity: 1; }
.md .tbl { overflow-x: auto; }
.md table { border-collapse: collapse; }
.md th, .md td { border: 1px solid var(--line); padding: 5px 9px; }
.md hr { border: 0; border-top: 1px solid var(--line); }

/* composer */
.composer { padding: 8px 16px 14px; padding-bottom: max(14px, env(safe-area-inset-bottom)); }
.box { max-width: 780px; margin: 0 auto; display: flex; flex-direction: column; gap: 6px; border: 1px solid var(--line); border-radius: 18px; background: var(--panel); padding: 10px 10px 8px 14px; box-shadow: var(--shadow); }
.box:focus-within { border-color: var(--acc); }
.box textarea { width: 100%; border: 0; outline: 0; resize: none; background: none; max-height: 240px; min-height: 26px; padding: 2px 0; line-height: 1.45; }
.tools-row { display: flex; align-items: center; gap: 6px; }
.tools-row .grow { flex: 1; }
.tool { border: 1px solid var(--line); background: none; border-radius: 999px; padding: 4px 11px; font-size: 13px; color: var(--dim); }
.tool:hover { color: var(--text); }
.tool[aria-pressed="true"] { background: var(--user); color: var(--acc); border-color: var(--acc); }
.files { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { display: inline-flex; align-items: center; gap: 6px; max-width: 100%; border: 1px solid var(--line); border-radius: 8px; padding: 3px 8px; font-size: 12.5px; background: var(--side); }
.chip b { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 220px; }
.chip span { color: var(--dim); }
.chip button { border: 0; background: none; padding: 0 2px; color: var(--dim); }
.chip button:hover { color: var(--err); }
.drop { position: absolute; inset: 0; background: rgba(47, 111, 228, .12); border: 2px dashed var(--acc); display: flex; align-items: center; justify-content: center; font-size: 18px; color: var(--acc); z-index: 8; pointer-events: none; }
.main { position: relative; }
.send { width: 38px; height: 38px; flex: none; border-radius: 50%; padding: 0; background: var(--acc); color: var(--acc-text); border: 0; font-size: 18px; font-weight: 700; }
.send:disabled { opacity: .4; cursor: default; }
.send.stop { background: var(--text); color: var(--bg); font-size: 14px; }
.hint { max-width: 780px; margin: 6px auto 0; font-size: 12px; color: var(--dim); text-align: center; }

@media (max-width: 760px) {
  .sidebar { position: fixed; z-index: 10; top: 0; bottom: 0; left: 0; transform: translateX(-105%); transition: transform .2s ease; box-shadow: var(--shadow); }
  .sidebar.open { transform: none; }
  #menuBtn { display: block; }
  .messages { padding: 14px 12px 6px; }
  .composer { padding: 6px 10px 10px; padding-bottom: max(10px, env(safe-area-inset-bottom)); }
  .user-wrap { max-width: 92%; }
  .user-wrap .actions { opacity: .8; }
  .tool { padding: 4px 9px; }
  .hint { display: none; }
  .chat-item button { visibility: visible; }
}

/* language switch */
.lang-row { display: flex; justify-content: flex-end; gap: 4px; }
.lang-btn { border: 1px solid var(--line); background: none; border-radius: 7px; padding: 2px 8px; font-size: 12px; color: var(--dim); }
.lang-btn[aria-pressed="true"] { color: var(--acc); border-color: var(--acc); background: var(--user); }
.side-links { align-items: center; }
.side-links .lang-btn { margin: 0 1px; }
.guide-link { font-size: 12.5px; color: var(--acc); text-decoration: none; }
.guide-link:hover { text-decoration: underline; }
