:root {
  --bg: #ffffff; --bg2: #f4f5f7; --bg3: #e9ebef; --fg: #15171c; --muted: #6b7280;
  --accent: #d97757; --accent-fg: #fff; --bubble-me: #d97757; --bubble-me-fg: #fff;
  --bubble-ai: #f1f2f5; --border: #e3e5ea; --danger: #dc3c3c; --code-bg: #f6f8fa;
  --radius: 18px; --side-w: 290px;
  --safe-b: env(safe-area-inset-bottom, 0px); --safe-t: env(safe-area-inset-top, 0px);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115; --bg2: #161920; --bg3: #20242d; --fg: #e8e9ec; --muted: #8b919c;
    --bubble-ai: #1b1f27; --border: #262a33; --code-bg: #0d1117;
  }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--fg);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; }
body { overflow: hidden; overscroll-behavior: none; }
button { font: inherit; color: inherit; cursor: pointer; }
input, textarea { font: inherit; color: var(--fg); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.error { color: var(--danger); min-height: 1.2em; margin: 8px 0 0; font-size: 14px; }
.screen { position: fixed; inset: 0; }

/* login */
#login { display: flex; align-items: center; justify-content: center; padding: 20px; background: var(--bg2); }
.card { width: 100%; max-width: 360px; background: var(--bg); border: 1px solid var(--border); border-radius: 22px;
  padding: 28px 24px; text-align: center; box-shadow: 0 10px 40px rgba(0,0,0,.06); }
.card .logo { width: 56px; height: 56px; }
.card h1 { margin: 8px 0 20px; font-size: 22px; }
.card input { width: 100%; padding: 13px 14px; margin-bottom: 10px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--bg2); outline: none; }
.card input:focus { border-color: var(--accent); }
#l-code { text-align: center; letter-spacing: 8px; font-size: 22px; }
button.primary { background: var(--accent); color: var(--accent-fg); border: 0; }
.card button.primary { width: 100%; padding: 13px; border-radius: 12px; font-weight: 600; margin: 4px 0 14px; }
.card a { display: inline-block; text-decoration: none; }
.spinner { width: 34px; height: 34px; margin: 6px auto 10px; border: 3px solid var(--bg3); border-top-color: var(--accent);
  border-radius: 50%; animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* layout */
#app { display: flex; }
#sidebar { width: var(--side-w); flex: none; background: var(--bg2); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; padding-top: var(--safe-t); }
.side-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 12px 8px 16px; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; }
.brand img { width: 26px; height: 26px; }
#chat-list { flex: 1; overflow-y: auto; padding: 4px 8px; }
.chat-item { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 12px; cursor: pointer;
  user-select: none; }
.chat-item:hover { background: var(--bg3); }
.chat-item.active { background: var(--bg3); font-weight: 600; }
.chat-item .ico { width: 34px; height: 34px; flex: none; border-radius: 50%; display: grid; place-items: center;
  background: var(--bg); font-size: 17px; }
.chat-item .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-item .busy-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); animation: pulse 1.2s infinite; }
@keyframes pulse { 50% { opacity: .3; } }
.side-foot { padding: 10px 14px calc(10px + var(--safe-b)); display: flex; flex-direction: column; align-items: flex-start; gap: 6px; }
.link { background: none; border: 0; color: var(--muted); padding: 4px 0; }
.link:hover { color: var(--fg); }
.icon-btn { background: none; border: 0; width: 40px; height: 40px; border-radius: 12px; font-size: 19px; flex: none;
  display: grid; place-items: center; }
.icon-btn:hover { background: var(--bg3); }
.icon-btn.round { border-radius: 50%; }
.icon-btn.primary { background: var(--accent); color: #fff; font-size: 17px; }
.icon-btn.danger { background: var(--danger); color: #fff; font-size: 14px; }

#main { flex: 1; min-width: 0; display: flex; flex-direction: column; position: relative; }
#topbar { display: flex; align-items: center; gap: 6px; padding: calc(6px + var(--safe-t)) 8px 6px;
  border-bottom: 1px solid var(--border); background: var(--bg); }
#menu-btn { display: none; }
.title-wrap { flex: 1; min-width: 0; padding-left: 6px; }
#chat-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#chat-status { height: 18px; }
#backdrop { display: none; }

/* messages */
#messages { flex: 1; overflow-y: auto; padding: 16px 14px 8px; -webkit-overflow-scrolling: touch; }
.msg { max-width: 760px; margin: 0 auto 10px; display: flex; }
.msg .bubble { padding: 9px 14px; border-radius: var(--radius); overflow-wrap: anywhere; min-width: 0; }
.msg.user { justify-content: flex-end; }
.msg.user .bubble { background: var(--bubble-me); color: var(--bubble-me-fg); border-bottom-right-radius: 6px;
  max-width: 85%; white-space: pre-wrap; }
.msg.assistant .bubble { background: var(--bubble-ai); border-bottom-left-radius: 6px; max-width: 100%; }
.msg.tool { margin-bottom: 4px; }
.tool-chip { font-size: 13px; color: var(--muted); padding: 3px 10px; border-radius: 10px; background: var(--bg2);
  border: 1px solid var(--border); max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; cursor: pointer; }
.tool-chip.open { white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.error .bubble { background: rgba(220,60,60,.12); color: var(--danger); white-space: pre-wrap; }
.thinking { max-width: 760px; margin: 4px auto 10px; color: var(--muted); font-size: 14px; display: flex; gap: 8px; align-items: center; }
.dots span { display: inline-block; width: 6px; height: 6px; border-radius: 50%; background: var(--muted);
  margin-right: 3px; animation: pulse 1.2s infinite; }
.dots span:nth-child(2) { animation-delay: .2s; } .dots span:nth-child(3) { animation-delay: .4s; }
.day-sep { text-align: center; color: var(--muted); font-size: 12px; margin: 14px 0 10px; }
.empty { text-align: center; color: var(--muted); margin-top: 20vh; }
.attach-thumbs { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.attach-thumbs img { max-width: 220px; max-height: 220px; border-radius: 12px; display: block; cursor: zoom-in; }
.file-pill { display: inline-flex; gap: 6px; align-items: center; padding: 5px 10px; border-radius: 10px;
  background: rgba(255,255,255,.2); font-size: 14px; }

/* markdown */
.md > :first-child { margin-top: 0; } .md > :last-child { margin-bottom: 0; }
.md p { margin: 0 0 .6em; }
.md ul, .md ol { margin: 0 0 .6em; padding-left: 1.4em; }
.md h1, .md h2, .md h3, .md h4 { margin: .8em 0 .4em; line-height: 1.3; font-size: 1.05em; }
.md h1 { font-size: 1.2em; } .md h2 { font-size: 1.12em; }
.md a { color: var(--accent); }
.md blockquote { margin: 0 0 .6em; padding-left: 10px; border-left: 3px solid var(--border); color: var(--muted); }
.md code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .88em;
  background: var(--bg3); padding: 1px 5px; border-radius: 5px; }
.md pre { position: relative; margin: 0 0 .7em; background: var(--code-bg); border: 1px solid var(--border);
  border-radius: 12px; overflow: hidden; }
.md pre code { display: block; padding: 12px 14px; overflow-x: auto; background: none; font-size: 13px; line-height: 1.45; }
.md pre .code-head { display: flex; justify-content: space-between; align-items: center; font-size: 12px;
  color: var(--muted); padding: 5px 8px 5px 14px; border-bottom: 1px solid var(--border); }
.copy-btn { background: none; border: 0; color: var(--muted); font-size: 12px; padding: 2px 6px; border-radius: 6px; }
.copy-btn:hover { background: var(--bg3); color: var(--fg); }
.md table { border-collapse: collapse; margin: 0 0 .7em; font-size: 14px; display: block; overflow-x: auto; }
.md th, .md td { border: 1px solid var(--border); padding: 5px 9px; text-align: left; }
.md th { background: var(--bg3); }
.md hr { border: 0; border-top: 1px solid var(--border); margin: .8em 0; }
.md img { max-width: 100%; }

#to-bottom { position: absolute; right: 16px; bottom: calc(86px + var(--safe-b)); width: 40px; height: 40px;
  border-radius: 50%; border: 1px solid var(--border); background: var(--bg); box-shadow: 0 2px 10px rgba(0,0,0,.12); font-size: 18px; }

/* composer */
#composer { border-top: 1px solid var(--border); padding: 8px 8px calc(8px + var(--safe-b)); background: var(--bg); }
#composer .row { display: flex; align-items: flex-end; gap: 6px; max-width: 800px; margin: 0 auto; }
#input { flex: 1; resize: none; border: 1px solid var(--border); background: var(--bg2); border-radius: 20px;
  padding: 9px 14px; max-height: 40vh; outline: none; line-height: 1.4; min-height: 40px; }
#input:focus { border-color: var(--accent); }
#attachments { display: flex; gap: 6px; flex-wrap: wrap; max-width: 800px; margin: 0 auto 6px; }
#attachments:empty { display: none; }
.att { position: relative; display: flex; align-items: center; gap: 6px; background: var(--bg2); border: 1px solid var(--border);
  border-radius: 10px; padding: 4px 26px 4px 4px; font-size: 13px; max-width: 220px; }
.att img { width: 40px; height: 40px; object-fit: cover; border-radius: 7px; }
.att .fname { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding-left: 4px; }
.att .rm { position: absolute; right: 4px; top: 50%; transform: translateY(-50%); background: none; border: 0; color: var(--muted); }
.att.uploading { opacity: .5; }
#rec-bar { display: flex; align-items: center; gap: 10px; max-width: 800px; margin: 0 auto; padding: 2px 4px; }
#rec-bar .muted { flex: 1; }
.rec-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--danger); animation: pulse 1s infinite; }
#rec-time { font-variant-numeric: tabular-nums; }

#toast { position: fixed; left: 50%; bottom: calc(100px + var(--safe-b)); transform: translateX(-50%); background: var(--fg);
  color: var(--bg); padding: 9px 16px; border-radius: 12px; font-size: 14px; z-index: 50; max-width: 90vw; }

#viewer { position: fixed; inset: 0; background: rgba(0,0,0,.9); z-index: 60; display: grid; place-items: center; }
#viewer img { max-width: 96vw; max-height: 92vh; }

/* mobile */
@media (max-width: 760px) {
  #sidebar { position: fixed; z-index: 30; top: 0; bottom: 0; left: 0; width: 84vw; max-width: 320px;
    transform: translateX(-102%); transition: transform .22s ease; }
  #app.side-open #sidebar { transform: none; }
  #app.side-open #backdrop { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 20; }
  #menu-btn { display: grid; }
  #messages { padding: 12px 10px 6px; }
  .msg.user .bubble { max-width: 88%; }
}
