:root {
  --bg: #0e1116;
  --bg2: #161b23;
  --bg3: #1e2530;
  --fg: #e6e9ef;
  --mut: #8b94a3;
  --acc: #4f8cff;
  --acc2: #3a6fd8;
  --usr: #2b4a7a;
  --bdr: #2a3240;
  --ok: #35c26e;
  --bad: #e05252;
  --sb: #12161d;
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--fg);
  font: 15px/1.6 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
}
#icon-defs { display: none; }

.icon {
  width: 18px; height: 18px; flex: 0 0 auto;
  stroke: currentColor; fill: none;
  stroke-width: 2; stroke-linecap: round; stroke-linejoin: round;
}
button { font: inherit; }

#app { display: flex; height: 100%; }

/* ---------- sidebar ---------- */
#sidebar {
  width: 264px; flex: 0 0 264px; background: var(--sb);
  border-right: 1px solid var(--bdr);
  display: flex; flex-direction: column;
  transition: margin-left .2s ease;
}
#sidebar.closed { margin-left: -264px; }
.sb-head { padding: 14px 12px 10px; }
#btn-new {
  width: 100%; display: flex; align-items: center; justify-content: center; gap: 8px;
  background: var(--bg3); color: var(--fg); border: 1px solid var(--bdr);
  border-radius: 10px; padding: 9px 12px; cursor: pointer; font-size: 13px; font-weight: 600;
}
#btn-new:hover { border-color: var(--acc); color: var(--acc); }

#chat-list {
  flex: 1; overflow-y: auto; padding: 4px 8px;
}
.sb-empty { color: var(--mut); font-size: 12.5px; text-align: center; padding: 24px 10px; }
.chat-item {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 10px; border-radius: 8px; cursor: pointer;
  color: var(--mut); border: 1px solid transparent;
  margin-bottom: 2px;
}
.chat-item:hover { background: var(--bg2); color: var(--fg); }
.chat-item.active { background: var(--bg3); color: var(--fg); border-color: var(--bdr); }
.chat-item .icon { width: 15px; height: 15px; }
.chat-item .ci-title {
  flex: 1; min-width: 0; font-size: 13px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chat-item .ci-time { font-size: 10.5px; color: var(--mut); flex: 0 0 auto; }
.chat-item .ci-del {
  flex: 0 0 auto; display: none; background: none; border: none;
  color: var(--mut); cursor: pointer; padding: 2px; border-radius: 4px;
}
.chat-item:hover .ci-del { display: block; }
.chat-item .ci-del:hover { color: var(--bad); }
.chat-item .ci-del .icon { width: 14px; height: 14px; }

.sb-foot {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 14px; border-top: 1px solid var(--bdr); color: var(--mut);
}
.sb-logo {
  flex: 0 0 30px; height: 30px; border-radius: 8px; background: var(--acc);
  color: #fff; font-weight: 700; font-size: 12px;
  display: flex; align-items: center; justify-content: center;
}
.sb-foot b { display: block; color: var(--fg); font-size: 12.5px; }
.sb-foot small { font-size: 10.5px; }

#sb-overlay { display: none; }

/* ---------- main ---------- */
#main { flex: 1; display: flex; flex-direction: column; min-width: 0; }

header {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 18px; background: var(--bg2); border-bottom: 1px solid var(--bdr);
}
#btn-menu {
  background: none; border: none; color: var(--mut); cursor: pointer;
  padding: 8px; border-radius: 8px; display: flex;
}
#btn-menu:hover { background: var(--bg3); color: var(--fg); }
.brand h1 { font-size: 15px; margin: 0; }
.brand p { margin: 2px 0 0; font-size: 12px; color: var(--mut); }
.dot {
  display: inline-block; width: 8px; height: 8px; border-radius: 50%;
  background: var(--mut); margin-right: 6px; vertical-align: middle;
}
.dot.ok { background: var(--ok); }
.dot.bad { background: var(--bad); }

main { flex: 1; overflow-y: auto; padding: 20px 0; }
.msg { max-width: 820px; margin: 0 auto 18px; padding: 0 20px; display: flex; gap: 12px; }
.msg .av {
  flex: 0 0 32px; height: 32px; border-radius: 8px; color: var(--mut);
  display: flex; align-items: center; justify-content: center;
}
.msg .av .icon { width: 18px; height: 18px; }
.msg.user .av { background: var(--usr); color: #cfe0ff; }
.msg.bot .av { background: var(--bg3); border: 1px solid var(--bdr); }
.bubble { min-width: 0; overflow-wrap: break-word; }
.bubble .btext {
  border-radius: 12px; padding: 10px 14px;
  background: var(--bg2); border: 1px solid var(--bdr);
}
.msg.user .bubble .btext { background: var(--usr); border-color: transparent; white-space: pre-wrap; }
.msg.bot.thinking .btext { color: var(--mut); font-style: italic; }

.btext p { margin: 0 0 8px; }
.btext p:last-child { margin-bottom: 0; }
.btext ul, .btext ol { margin: 4px 0 8px; padding-left: 22px; }
.btext h1, .btext h2, .btext h3 { font-size: 15px; margin: 10px 0 6px; }
.btext code {
  background: var(--bg); padding: 1px 5px; border-radius: 4px;
  font-size: 13px; font-family: ui-monospace, Menlo, monospace;
}
.btext pre {
  background: var(--bg); border: 1px solid var(--bdr); border-radius: 8px;
  padding: 10px; overflow-x: auto; position: relative;
}
.btext pre code { background: none; padding: 0; }
.btext table { border-collapse: collapse; margin: 8px 0; font-size: 13px; }
.btext th, .btext td { border: 1px solid var(--bdr); padding: 5px 10px; }
.btext img.manual-img {
  max-width: 100%; border-radius: 8px; border: 1px solid var(--bdr);
  margin: 8px 0; display: block; cursor: zoom-in;
}
.btext a { color: var(--acc); }

.copy-btn {
  position: absolute; top: 6px; right: 6px;
  display: flex; align-items: center; gap: 5px;
  background: var(--bg3); color: var(--mut); border: 1px solid var(--bdr);
  border-radius: 6px; padding: 3px 9px; cursor: pointer;
  font-size: 11px; line-height: 1.4; user-select: none;
  opacity: 0; transition: opacity .15s, color .15s, border-color .15s;
}
.copy-btn .icon { width: 12px; height: 12px; }
.btext pre:hover .copy-btn, .copy-btn:focus-visible { opacity: 1; }
.copy-btn:hover { color: var(--fg); border-color: var(--acc); }
.copy-btn.ok { color: var(--ok); border-color: var(--ok); }
@media (hover: none) { .copy-btn { opacity: 1; } }

#welcome { max-width: 720px; margin: 8vh auto 0; text-align: center; padding: 0 20px; }
.wx {
  width: 56px; height: 56px; margin: 0 auto 12px; border-radius: 14px;
  background: var(--bg3); border: 1px solid var(--bdr); color: var(--acc);
  display: flex; align-items: center; justify-content: center;
}
.wx .icon { width: 28px; height: 28px; }
#welcome h2 { margin: 0 0 6px; font-size: 20px; }
#welcome p { color: var(--mut); margin: 0 0 24px; font-size: 14px; }
#chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
#chips button {
  background: var(--bg2); color: var(--fg); border: 1px solid var(--bdr);
  border-radius: 999px; padding: 8px 14px; cursor: pointer; font-size: 13px;
}
#chips button:hover { border-color: var(--acc); }

footer { padding: 10px 18px 14px; background: var(--bg2); border-top: 1px solid var(--bdr); }
#thumbs { display: flex; gap: 8px; margin-bottom: 8px; }
#thumbs:empty { margin: 0; }
#thumbs .th {
  position: relative; width: 58px; height: 58px; border-radius: 8px;
  border: 1px solid var(--bdr); overflow: hidden;
}
#thumbs .th img { width: 100%; height: 100%; object-fit: cover; }
#thumbs .th button {
  position: absolute; top: 3px; right: 3px; width: 20px; height: 20px;
  border-radius: 5px; border: none; background: rgba(0,0,0,.72); color: #fff;
  cursor: pointer; display: flex; align-items: center; justify-content: center;
}
#thumbs .th button .icon { width: 11px; height: 11px; }
.composer { display: flex; gap: 8px; align-items: flex-end; max-width: 820px; margin: 0 auto; }
#btn-attach, #btn-send {
  flex: 0 0 40px; height: 40px; border-radius: 10px; border: 1px solid var(--bdr);
  background: var(--bg3); color: var(--mut); cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
#btn-attach:hover { color: var(--fg); border-color: var(--acc); }
#btn-send { background: var(--acc); border-color: var(--acc); color: #fff; }
#btn-send:disabled { opacity: .5; cursor: default; }
#btn-send.stop { background: var(--bad); border-color: var(--bad); }
#input {
  flex: 1; resize: none; max-height: 160px; border-radius: 10px;
  border: 1px solid var(--bdr); background: var(--bg3); color: var(--fg);
  padding: 10px 12px; font: inherit; outline: none;
}
#input:focus { border-color: var(--acc); }
.hint { max-width: 820px; margin: 8px auto 0; font-size: 11px; color: var(--mut); text-align: center; }

#lightbox {
  position: fixed; inset: 0; background: rgba(0,0,0,.88); z-index: 99;
  display: none; align-items: center; justify-content: center; cursor: zoom-out;
}
#lightbox:not([hidden]) { display: flex; }
#lightbox img { max-width: 94vw; max-height: 94vh; border-radius: 8px; }

/* ---------- mobile ---------- */
@media (max-width: 768px) {
  #sidebar {
    position: fixed; z-index: 60; top: 0; bottom: 0; left: 0;
    margin-left: -280px; transition: margin-left .2s ease;
  }
  #sidebar.open { margin-left: 0; box-shadow: 0 0 40px rgba(0,0,0,.5); }
  #sidebar.closed { margin-left: -280px; }
  #sb-overlay {
    display: block; position: fixed; inset: 0; z-index: 50;
    background: rgba(0,0,0,.5); opacity: 0; pointer-events: none;
    transition: opacity .2s;
  }
  #sb-overlay.show { opacity: 1; pointer-events: auto; }
  .msg { padding: 0 12px; }
  header { padding: 8px 12px; }
  footer { padding: 8px 12px 12px; }
}
