:root {
  --accent: #2f6fed;
  --accent-ink: #ffffff;
  --bg: #ffffff;
  --side-bg: #f6f7f9;
  --ink: #16181d;
  --muted: #7a808c;
  --line: #e6e8ec;
  --bubble: var(--accent);
  --danger: #e5484d;
  --radius: 20px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--ink);
  -webkit-text-size-adjust: 100%;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); overscroll-behavior: none; }
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; -webkit-tap-highlight-color: transparent; }
.hidden { display: none !important; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
circle[r="1.3"] { fill: currentColor; }

/* login */
.login { position: fixed; inset: 0; display: grid; place-items: center; padding: 24px; background: var(--side-bg); }
.login-card { width: 100%; max-width: 340px; display: flex; flex-direction: column; gap: 12px; text-align: center; }
.login-logo { width: 72px; height: 72px; border-radius: 18px; margin: 0 auto 4px; }
.login-card h1 { font-size: 22px; margin: 0 0 12px; }
.login-card input { font-size: 17px; padding: 14px 16px; border-radius: 14px; border: 1px solid var(--line); background: #fff; outline: none; }
.login-card input:focus { border-color: var(--accent); }
.btn-primary { background: var(--accent); color: var(--accent-ink); padding: 14px; border-radius: 14px; font-size: 17px; font-weight: 600; }
.err { color: var(--danger); font-size: 14px; min-height: 18px; }

/* layout */
.app { display: flex; height: 100dvh; overflow: hidden; }
.sidebar { width: 300px; flex: none; background: var(--side-bg); border-right: 1px solid var(--line); display: flex; flex-direction: column; padding-top: var(--safe-t); }
.chat { flex: 1; min-width: 0; display: flex; flex-direction: column; position: relative; padding-top: var(--safe-t); }

.side-head, .chat-head { height: 56px; flex: none; display: flex; align-items: center; gap: 8px; padding: 0 12px 0 16px; }
.side-title { font-weight: 700; font-size: 18px; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn { width: 40px; height: 40px; display: grid; place-items: center; border-radius: 12px; color: var(--ink); flex: none; }
.icon-btn:hover { background: rgba(0,0,0,.05); }
.icon-btn:disabled { opacity: .35; cursor: default; }

.chat-list { flex: 1; overflow-y: auto; padding: 4px 8px 12px; }
.chat-item { display: flex; align-items: center; gap: 10px; width: 100%; text-align: left; padding: 12px; border-radius: 12px; font-size: 16px; }
.chat-item:hover { background: rgba(0,0,0,.04); }
.chat-item.active { background: #fff; box-shadow: 0 1px 2px rgba(0,0,0,.06); font-weight: 600; }
.chat-item .t { flex: 1; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.chat-item .pin { color: var(--muted); font-size: 12px; }
.chat-item .busy { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); animation: pulse 1.2s infinite; }
.list-sep { height: 1px; background: var(--line); margin: 8px 12px; }
.side-foot { display: flex; justify-content: space-between; align-items: center; padding: 12px 16px calc(12px + var(--safe-b)); color: var(--muted); font-size: 14px; border-top: 1px solid var(--line); }
.link-btn { color: var(--accent); font-size: 14px; }

.chat-head { border-bottom: 1px solid var(--line); position: relative; }
.chat-title { flex: 1; font-weight: 600; font-size: 17px; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.back { visibility: hidden; }
.menu { position: absolute; right: 12px; top: 52px; background: #fff; border: 1px solid var(--line); border-radius: 14px; box-shadow: 0 8px 30px rgba(0,0,0,.12); padding: 6px; z-index: 20; min-width: 200px; }
.menu button { display: block; width: 100%; text-align: left; padding: 12px; border-radius: 10px; font-size: 16px; }
.menu button:hover { background: var(--side-bg); }
.menu .danger { color: var(--danger); }

/* messages */
.messages { flex: 1; overflow-y: auto; padding: 16px 16px 8px; -webkit-overflow-scrolling: touch; }
.msg { max-width: 760px; margin: 0 auto 14px; display: flex; flex-direction: column; }
.msg.user { align-items: flex-end; }
.msg.user .bubble { background: var(--bubble); color: var(--accent-ink); padding: 10px 14px; border-radius: var(--radius); border-bottom-right-radius: 6px; max-width: 85%; white-space: pre-wrap; word-wrap: break-word; font-size: 16px; line-height: 1.4; }
.msg.assistant .md { font-size: 16px; line-height: 1.55; word-wrap: break-word; overflow-wrap: anywhere; }
.md p { margin: 0 0 .7em; } .md p:last-child { margin-bottom: 0; }
.md ul, .md ol { padding-left: 1.3em; margin: .3em 0 .7em; }
.md li { margin: .2em 0; }
.md h1, .md h2, .md h3 { font-size: 1.1em; margin: 1em 0 .4em; }
.md code { background: #f1f3f5; padding: .1em .35em; border-radius: 6px; font-size: .9em; }
.md pre { background: #f6f8fa; padding: 12px; border-radius: 12px; overflow-x: auto; font-size: 13px; }
.md pre code { background: none; padding: 0; }
.md table { border-collapse: collapse; display: block; overflow-x: auto; font-size: 14px; margin: .5em 0; }
.md th, .md td { border: 1px solid var(--line); padding: 6px 10px; text-align: left; }
.md a { color: var(--accent); }
.md img { max-width: 100%; border-radius: 12px; }
.tools { color: var(--muted); font-size: 13px; margin-bottom: 6px; display: flex; align-items: center; gap: 6px; }
.tools svg { width: 14px; height: 14px; }
.files { display: flex; flex-wrap: wrap; gap: 6px; justify-content: flex-end; margin-bottom: 6px; max-width: 85%; }
.file-chip { display: inline-flex; align-items: center; gap: 6px; background: var(--side-bg); border: 1px solid var(--line); border-radius: 12px; padding: 6px 10px; font-size: 13px; color: var(--ink); text-decoration: none; max-width: 220px; }
.file-chip span { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.file-thumb { width: 120px; height: 120px; object-fit: cover; border-radius: 14px; border: 1px solid var(--line); }
.empty { color: var(--muted); text-align: center; margin-top: 30vh; font-size: 15px; padding: 0 24px; }
.card { max-width: 760px; margin: 0 auto 14px; border: 1px solid var(--line); border-radius: 16px; padding: 14px; background: #fff; }
.card .q { font-size: 15px; margin-bottom: 10px; white-space: pre-wrap; }
.card pre { background: #f6f8fa; padding: 10px; border-radius: 10px; font-size: 12px; overflow-x: auto; margin: 0 0 10px; }
.card .acts { display: flex; flex-wrap: wrap; gap: 8px; }
.card .acts button { padding: 9px 14px; border-radius: 12px; background: var(--side-bg); border: 1px solid var(--line); font-size: 15px; }
.card .acts button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.card .acts button.danger { color: var(--danger); }
.typing { display: inline-flex; gap: 4px; padding: 6px 0; }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: #b9bec7; animation: blink 1.2s infinite; }
.typing i:nth-child(2) { animation-delay: .2s; } .typing i:nth-child(3) { animation-delay: .4s; }

/* composer */
.composer-wrap { flex: none; padding: 6px 12px calc(10px + var(--safe-b)); background: var(--bg); }
.status { max-width: 760px; margin: 0 auto 6px; display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 13px; }
.status #statusText { flex: 1; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.stop-btn { border: 1px solid var(--line); border-radius: 10px; padding: 4px 10px; font-size: 13px; color: var(--ink); }
.dot-pulse { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); animation: pulse 1.2s infinite; flex: none; }
.attachments { max-width: 760px; margin: 0 auto; display: flex; gap: 8px; overflow-x: auto; }
.attachments:not(:empty) { padding-bottom: 8px; }
.att { position: relative; flex: none; }
.att img, .att .doc { width: 64px; height: 64px; border-radius: 12px; object-fit: cover; border: 1px solid var(--line); background: var(--side-bg); display: grid; place-items: center; font-size: 11px; color: var(--muted); padding: 4px; text-align: center; overflow: hidden; word-break: break-all; }
.att .x { position: absolute; top: -6px; right: -6px; width: 22px; height: 22px; border-radius: 50%; background: #333; color: #fff; font-size: 13px; line-height: 22px; text-align: center; }
.composer { max-width: 760px; margin: 0 auto; display: flex; align-items: flex-end; gap: 6px; }
.icon-btn.round { border-radius: 50%; width: 40px; height: 40px; margin-bottom: 2px; }
.input-box { flex: 1; position: relative; background: var(--side-bg); border: 1px solid var(--line); border-radius: 22px; min-height: 44px; display: flex; align-items: center; }
.input-box textarea { flex: 1; resize: none; border: 0; outline: none; background: transparent; font: inherit; font-size: 16px; line-height: 1.35; padding: 11px 16px; max-height: 40vh; color: var(--ink); }
.rec-timer { position: absolute; inset: 0; display: flex; align-items: center; gap: 8px; padding: 0 16px; font-size: 16px; color: var(--danger); background: var(--side-bg); border-radius: 22px; font-variant-numeric: tabular-nums; }
.rec-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--danger); animation: pulse 1s infinite; }
#micBtn.recording { background: var(--danger); color: #fff; }
#micBtn.busy { opacity: .5; }
.send { background: var(--accent); color: var(--accent-ink); }
.send:disabled { background: #d5d9e0; color: #fff; opacity: 1; }
.send:hover { background: var(--accent); }

@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: .3; } }
@keyframes blink { 0%,100% { opacity: .3; } 50% { opacity: 1; } }

/* mobile: list ↔ chat */
@media (max-width: 820px) {
  .sidebar { width: 100%; border-right: 0; }
  .chat { position: fixed; inset: 0; transform: translateX(100%); transition: transform .22s ease; background: var(--bg); z-index: 10; }
  body.show-chat .chat { transform: none; }
  .back { visibility: visible; }
  .chat-item { padding: 14px 12px; font-size: 17px; }
  .messages { padding: 12px 12px 6px; }
}
