:root {
    --bg: #f6f7f9;
    --panel: #ffffff;
    --text: #1d2330;
    --muted: #6b7380;
    --border: #dde1e7;
    --user: #2f6fed;
    --user-text: #ffffff;
    --ok: #1f9d55;
    --err: #d64545;
    --active: #e6eefd;
    --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
@media (prefers-color-scheme: dark) {
    :root {
        --bg: #12151b;
        --panel: #1b2029;
        --text: #e3e7ee;
        --muted: #8b93a1;
        --border: #2c3340;
        --user: #3d7bf0;
        --ok: #3fbf74;
        --err: #ef6464;
        --active: #233049;
    }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
    display: flex;
    background: var(--bg); color: var(--text);
    font: 15px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
}
.chat { flex: 1; min-width: 0; display: flex; flex-direction: column; }
header {
    display: flex; align-items: center; gap: 10px;
    padding: 10px 16px; border-bottom: 1px solid var(--border); background: var(--panel);
}
h1 { flex: 1; font-size: 17px; margin: 0; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- sidebar ---------- */
#sidebar {
    width: 250px; flex-shrink: 0; display: flex; flex-direction: column;
    background: var(--panel); border-right: 1px solid var(--border);
    transition: margin-left .15s ease;
}
body.collapsed #sidebar { margin-left: -251px; }
.side-head {
    display: flex; align-items: center; justify-content: space-between;
    padding: 10px 12px; border-bottom: 1px solid var(--border); font-weight: 600;
}
.side-head button { padding: 5px 10px; font-size: 13px; }
#chats { list-style: none; margin: 0; padding: 6px; overflow-y: auto; flex: 1; }
.chat-item {
    display: grid; grid-template-columns: 1fr auto; align-items: center; column-gap: 6px;
    padding: 7px 8px 7px 10px; border-radius: 8px; cursor: pointer; position: relative;
}
.chat-item:hover { background: var(--bg); }
.chat-item.active { background: var(--active); }
.chat-item .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-item .time { grid-column: 1; font-size: 12px; color: var(--muted); }
.chat-item .del { grid-column: 2; grid-row: 1 / span 2; visibility: hidden; }
.chat-item:hover .del, .chat-item.active .del { visibility: visible; }
.chat-item.unread::before {
    content: ""; position: absolute; left: 2px; top: 50%; width: 6px; height: 6px; margin-top: -3px;
    border-radius: 50%; background: var(--user);
}
.chat-item.unread .name { font-weight: 600; }
.chat-item input.rename { padding: 2px 6px; font: inherit; border-radius: 4px; }
h1.editable { cursor: text; border-radius: 4px; }
h1.editable:hover { background: var(--bg); }
header input.rename { flex: 1; padding: 0 6px; font: 600 17px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif; border-radius: 4px; }
input.rename.invalid, input.rename.invalid:focus { outline: 2px solid var(--err); }
button.icon {
    padding: 2px 8px; background: transparent; color: var(--muted); font-weight: normal; font-size: 16px;
}
button.icon:hover { color: var(--text); }
.chat-item .del:hover { color: var(--err); }
.version {
    padding: 6px 12px; border-top: 1px solid var(--border); font: 11px ui-monospace, monospace;
    color: var(--muted); user-select: all;
}
.version:empty { display: none; }
#backdrop { display: none; }
@media (max-width: 700px) {
    #sidebar { position: fixed; z-index: 2; top: 0; bottom: 0; left: 0; box-shadow: 2px 0 12px rgba(0, 0, 0, .25); }
    body.collapsed #sidebar { box-shadow: none; }
    body:not(.collapsed) #backdrop { display: block; position: fixed; inset: 0; z-index: 1; background: rgba(0, 0, 0, .3); }
}
.conn { color: var(--err); font-size: 12px; }
.conn.on { color: var(--ok); }
main {
    flex: 1; overflow-y: auto; padding: 16px;
    display: flex; flex-direction: column; gap: 10px;
}
.msg {
    max-width: min(760px, 92%); padding: 9px 13px; border-radius: 12px;
    white-space: pre-wrap; word-break: break-word;
}
.msg.user {
    align-self: flex-end; background: var(--user); color: var(--user-text);
    font-family: var(--mono); font-size: 14px;
}
.msg.bot { align-self: flex-start; background: var(--panel); border: 1px solid var(--border); }
.msg.bot.done { border-left: 3px solid var(--ok); }
.msg.bot.failed { border-left: 3px solid var(--err); color: var(--err); }
.msg .status { color: var(--muted); font-size: 13px; }
.msg.done .status, .msg.failed .status { display: none; }
.msg .text:empty { display: none; }
.msg .text.pre { font-family: var(--mono); font-size: 13px; white-space: pre; overflow-x: auto; }
progress { width: 260px; max-width: 100%; height: 8px; margin-top: 6px; display: block; accent-color: var(--user); }
.msg.done progress, .msg.failed progress { display: none; }
.msg.bot.link { cursor: pointer; }
.msg.bot.link:hover { border-color: var(--user); }
.msg.bot.link .hint { color: var(--user); font-size: 13px; margin-top: 4px; }
.msg .ts {
    margin: 2px -4px -4px 0; text-align: right; white-space: nowrap;
    font: 11px/1.2 system-ui, -apple-system, "Segoe UI", sans-serif; opacity: .7;
}
.msg.bot .ts { color: var(--muted); opacity: 1; }
.msg .ts, .msg .hint, .msg .status { user-select: none; }   /* a manual selection copies only the content */
.msg .ts button {
    margin-right: 6px; padding: 0 2px; border: 0; background: none; color: inherit; font: inherit;
    cursor: pointer; visibility: hidden;
}
.msg:hover .ts button { visibility: visible; }
@media (hover: none) { .msg .ts button { visibility: visible; } }
.msg .ts .copy:hover, .msg .ts .repeat:hover { color: var(--user); }
.msg.user .ts button:hover { color: inherit; opacity: .6; }
.msg .ts .del { filter: grayscale(1); }
.msg .ts .del:hover { filter: none; }
.msg.bot:not(.done):not(.failed) .ts .del { display: none; }   /* a running job's result can't be deleted */
.question {
    align-self: flex-start; max-width: min(760px, 92%); margin-top: -4px; padding: 7px 11px;
    border: 1px dashed var(--border); border-radius: 10px; font-size: 13px; user-select: none;
}
.question .q-row { display: flex; align-items: center; gap: 6px; margin-top: 6px; }
.question .q-options { display: flex; flex-wrap: wrap; gap: 6px; flex: 1; }
.question button {
    padding: 3px 12px; border: 1px solid var(--user); border-radius: 8px;
    background: var(--user); color: var(--user-text); font: inherit; cursor: pointer;
}
.question button:hover { opacity: .85; }
.question button.dismiss { border-color: var(--border); background: none; color: var(--muted); }
.question button.dismiss:hover { color: inherit; opacity: 1; }
.question.answered { color: var(--muted); }
.question.answered .q-options { font-family: var(--mono); }

/* ---------- full-window viewer for HTML results ---------- */
#viewer[hidden] { display: none; }
#viewer { position: fixed; inset: 0; z-index: 5; display: flex; flex-direction: column; background: var(--bg); }
.viewer-head {
    display: flex; align-items: center; gap: 12px; padding: 6px 12px;
    border-bottom: 1px solid var(--border); background: var(--panel);
}
.viewer-head .caption { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.viewer-head .open-tab { color: var(--muted); font-size: 13px; white-space: nowrap; }
.viewer-head .open-tab:hover { color: var(--text); }
/* ---------- settings page ---------- */
#settings[hidden], #settings [hidden] { display: none; }
#settings { position: fixed; inset: 0; z-index: 6; display: flex; flex-direction: column; background: var(--bg); }
.settings-body {
    flex: 1; overflow-y: auto; padding: 16px;
    display: flex; flex-direction: column; align-items: center; gap: 16px;
}
.card.wide { width: 520px; }
.card .note code { font: 12px var(--mono); word-break: break-all; }
.card label.check { flex-direction: row; align-items: center; gap: 8px; font-size: 15px; color: var(--text); cursor: pointer; }
.card label.check input { margin: 0; }
.card .ok { color: var(--ok); font-size: 14px; }
.card .ok:empty { display: none; }
#viewer iframe { flex: 1; width: 100%; border: 0; }
#form {
    position: relative; display: flex; gap: 8px; padding: 12px 16px;
    border-top: 1px solid var(--border); background: var(--panel);
}
input {
    flex: 1; min-width: 0; padding: 10px 12px; border-radius: 8px;
    border: 1px solid var(--border); background: var(--bg); color: var(--text);
    font: 14px var(--mono);
}
#suggest[hidden] { display: none; }
#suggest {
    position: absolute; left: 16px; right: 16px; bottom: calc(100% - 6px); z-index: 3;
    max-height: 50vh; overflow-y: auto; margin: 0; padding: 4px 0; list-style: none;
    background: var(--panel); border: 1px solid var(--border); border-radius: 8px;
    box-shadow: 0 -4px 16px rgba(0, 0, 0, .12); font: 14px var(--mono);
}
#suggest li { display: flex; gap: 12px; padding: 5px 12px; cursor: pointer; }
#suggest li.selected, #suggest li:hover { background: var(--active); }
#suggest .label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#suggest .kind { color: var(--muted); font: 12px system-ui, -apple-system, "Segoe UI", sans-serif; align-self: center; }
input:focus { outline: 2px solid var(--user); outline-offset: -1px; }
button {
    padding: 0 18px; border: 0; border-radius: 8px;
    background: var(--user); color: var(--user-text); font-weight: 600; cursor: pointer;
}

/* ---------- auth ---------- */
button.link {
    padding: 4px 8px; background: transparent; color: var(--muted); font-weight: normal; font-size: 13px;
    white-space: nowrap;
}
button.link:hover { color: var(--text); }
#auth[hidden], #auth [hidden] { display: none; }
#auth {
    position: fixed; inset: 0; z-index: 10; overflow-y: auto; padding: 16px;
    display: flex; align-items: center; justify-content: center; background: var(--bg);
}
.card {
    width: 340px; max-width: 100%; display: flex; flex-direction: column; gap: 10px;
    padding: 22px; border-radius: 12px; background: var(--panel); border: 1px solid var(--border);
}
.card h2 { margin: 0 0 4px; font-size: 19px; }
.card .note { margin: 0; color: var(--muted); font-size: 14px; }
.card label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.card input { flex: none; font: 15px system-ui, -apple-system, "Segoe UI", sans-serif; }
.card .error { color: var(--err); font-size: 14px; }
.card .error:empty { display: none; }
.card button { padding: 10px 18px; }
.card .buttons { display: flex; justify-content: flex-end; gap: 8px; }
.card .cancel, .card .secondary { background: transparent; color: var(--muted); border: 1px solid var(--border); }
#account-form.forced .optional, #account-form:not(.forced) .forced { display: none; }
