:root {
  --bg: #f7f7f5; --panel: #ffffff; --side: #efefec; --line: #e2e2dd; --text: #1d1e20; --muted: #6c6d70;
  --accent: #2f5d50; --accent-2: #e6efe9; --user: #2f5d50; --user-text: #ffffff; --bubble: #ffffff;
  --code: #f1f1ee; --danger: #b3261e; --hover: rgba(0,0,0,.05); --shadow: 0 6px 24px rgba(0,0,0,.12);
  --radius: 14px; --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #16171a; --panel: #1e1f23; --side: #121316; --line: #2c2d32; --text: #e9e9e7; --muted: #9a9b9f;
    --accent: #7fc4ac; --accent-2: #20302b; --user: #2c4a41; --user-text: #eef6f2; --bubble: #1e1f23;
    --code: #26272c; --danger: #ff8a80; --hover: rgba(255,255,255,.06); --shadow: 0 6px 24px rgba(0,0,0,.5);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #16171a; --panel: #1e1f23; --side: #121316; --line: #2c2d32; --text: #e9e9e7; --muted: #9a9b9f;
  --accent: #7fc4ac; --accent-2: #20302b; --user: #2c4a41; --user-text: #eef6f2; --bubble: #1e1f23;
  --code: #26272c; --danger: #ff8a80; --hover: rgba(255,255,255,.06); --shadow: 0 6px 24px rgba(0,0,0,.5);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 15px/1.5 var(--font); -webkit-font-smoothing: antialiased; }
[hidden] { display: none !important; }
button { font: inherit; color: inherit; }
input, textarea, select { font: inherit; color: var(--text); }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.muted { color: var(--muted); }
.err { color: var(--danger); min-height: 1.2em; margin: 0; font-size: 14px; }

.btn { border: 1px solid var(--line); background: var(--panel); border-radius: 10px; padding: 8px 14px; cursor: pointer; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--bg); font-weight: 600; }
.btn.danger { color: var(--danger); }
.icon-btn { display: inline-grid; place-items: center; width: 36px; height: 36px; border: 0; background: none; border-radius: 9px; cursor: pointer; color: var(--muted); flex: none; }
.icon-btn:hover { background: var(--hover); color: var(--text); }
.link-btn { border: 0; background: none; color: var(--muted); cursor: pointer; padding: 4px; }
.link-btn:hover { color: var(--text); }

/* login */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 340px; display: grid; gap: 12px; text-align: center; background: var(--panel); padding: 32px 24px; border-radius: 18px; border: 1px solid var(--line); }
.login-card img { margin: 0 auto; }
.login-card h1 { margin: 0; font-size: 22px; }
.login-card p { margin: 0; }
.login-card input { padding: 11px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); }

/* layout */
.app { display: flex; height: 100dvh; }
.side { width: 290px; flex: none; background: var(--side); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.side-head { display: flex; align-items: center; gap: 2px; padding: 12px 10px 6px 14px; padding-top: max(12px, env(safe-area-inset-top)); }
.brand { flex: 1; display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 16px; }
.side-search { padding: 4px 10px 8px; }
.side-search input { width: 100%; padding: 8px 10px; border-radius: 9px; border: 1px solid var(--line); background: var(--panel); }
.list { flex: 1; overflow-y: auto; padding: 0 6px 10px; }
.side-foot { display: flex; justify-content: space-between; align-items: center; padding: 8px 12px; padding-bottom: max(8px, env(safe-area-inset-bottom)); border-top: 1px solid var(--line); font-size: 13px; color: var(--muted); }
.toggle { display: flex; gap: 6px; align-items: center; cursor: pointer; }

.group { margin-top: 10px; }
.group-head { display: flex; align-items: center; gap: 2px; padding: 2px 4px 2px 8px; color: var(--muted); font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.group-head .name { flex: 1; cursor: pointer; display: flex; align-items: center; gap: 6px; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; user-select: none; }
.group-head .chev { width: 14px; height: 14px; transition: transform .15s; flex: none; }
.group.closed .chev { transform: rotate(-90deg); }
.group.closed .items { display: none; }
.group-head .icon-btn { width: 28px; height: 28px; }
.group-head .icon-btn svg { width: 16px; height: 16px; }
.badge { font-size: 10.5px; font-weight: 600; text-transform: none; letter-spacing: 0; padding: 1px 6px; border-radius: 99px; background: var(--accent-2); color: var(--accent); }
.item { display: flex; align-items: center; gap: 8px; padding: 7px 8px 7px 10px; border-radius: 9px; cursor: pointer; min-height: 40px; }
.item:hover { background: var(--hover); }
.item.active { background: var(--panel); box-shadow: 0 0 0 1px var(--line); }
.item .t { flex: 1; min-width: 0; }
.item .t b { display: block; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .t small { display: block; color: var(--muted); font-size: 12.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); flex: none; }
.item .spin { width: 12px; height: 12px; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; flex: none; }
.item.archived { opacity: .55; }
.item .pin { width: 13px; height: 13px; color: var(--muted); flex: none; }
@keyframes spin { to { transform: rotate(360deg); } }

.main { flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--bg); }
.main-head { display: flex; align-items: center; gap: 8px; padding: 10px 14px; padding-top: max(10px, env(safe-area-inset-top)); border-bottom: 1px solid var(--line); background: var(--bg); }
.title-wrap { flex: 1; min-width: 0; }
.chat-title { margin: 0; font-size: 16px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-meta { display: flex; gap: 6px; flex-wrap: wrap; font-size: 12px; color: var(--muted); }
.chat-meta:empty { display: none; }

.msgs { flex: 1; overflow-y: auto; padding: 18px 16px 8px; display: flex; flex-direction: column; gap: 14px; overscroll-behavior: contain; }
.msg { max-width: min(760px, 100%); width: fit-content; }
.msg.user { align-self: flex-end; max-width: min(620px, 88%); }
.msg.user .bubble { background: var(--user); color: var(--user-text); border-radius: 16px 16px 4px 16px; padding: 9px 13px; white-space: pre-wrap; word-wrap: break-word; }
.msg.user .bubble:empty { display: none; }
.msg.assistant { align-self: flex-start; width: 100%; }
.msg.assistant .bubble { padding: 2px 2px; overflow-wrap: anywhere; }
.msg .time { font-size: 11px; color: var(--muted); margin-top: 3px; }
.msg.user .time { text-align: right; }
.bubble > :first-child { margin-top: 0; }
.bubble > :last-child { margin-bottom: 0; }
.bubble p { margin: .5em 0; }
.bubble a { color: var(--accent); }
.msg.user .bubble a { color: inherit; }
.bubble pre { background: var(--code); padding: 10px 12px; border-radius: 10px; overflow-x: auto; font: 13px/1.45 var(--mono); position: relative; }
.bubble code { font-family: var(--mono); font-size: .9em; background: var(--code); padding: 1px 5px; border-radius: 5px; }
.bubble pre code { background: none; padding: 0; }
.bubble table { border-collapse: collapse; display: block; overflow-x: auto; max-width: 100%; font-size: 14px; }
.bubble th, .bubble td { border: 1px solid var(--line); padding: 5px 9px; text-align: left; }
.bubble blockquote { margin: .5em 0; padding-left: 12px; border-left: 3px solid var(--line); color: var(--muted); }
.bubble img { max-width: 100%; border-radius: 10px; }
.copy-btn { position: absolute; top: 6px; right: 6px; font-size: 11px; padding: 2px 7px; border-radius: 6px; border: 1px solid var(--line); background: var(--panel); color: var(--muted); cursor: pointer; opacity: 0; }
.bubble pre:hover .copy-btn, .copy-btn:focus { opacity: 1; }
@media (hover: none) { .copy-btn { opacity: 1; } }
.files { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 6px; }
.msg.user .files { justify-content: flex-end; }
.files img, .files video { max-width: min(360px, 100%); max-height: 360px; border-radius: 12px; display: block; background: var(--code); }
.files audio { width: min(320px, 100%); }
.file-chip { display: inline-flex; align-items: center; gap: 8px; padding: 8px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--panel); color: var(--text); text-decoration: none; font-size: 14px; max-width: 100%; }
.file-chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.typing { align-self: flex-start; display: flex; gap: 4px; padding: 8px 2px; }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite; }
.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; } }

.empty { flex: 1; display: grid; place-content: center; text-align: center; padding: 24px; gap: 8px; max-width: 420px; margin: 0 auto; }
.empty h3 { margin: 0; }
.empty p { margin: 0 0 8px; }

.composer { padding: 8px 12px 12px; padding-bottom: max(12px, env(safe-area-inset-bottom)); }
.composer-row { display: flex; align-items: flex-end; gap: 4px; max-width: 820px; margin: 0 auto; background: var(--panel); border: 1px solid var(--line); border-radius: 18px; padding: 5px; }
.composer textarea { flex: 1; border: 0; outline: 0; resize: none; background: none; padding: 8px 6px; max-height: 40vh; line-height: 1.4; font-size: 16px; }
.send-btn { width: 36px; height: 36px; border-radius: 12px; border: 0; background: var(--accent); color: var(--bg); display: grid; place-items: center; cursor: pointer; flex: none; }
.send-btn.stop { background: var(--danger); }
.send-btn:disabled { opacity: .4; cursor: default; }
#btn-mic.rec { color: var(--danger); background: var(--hover); }
.attach-list { display: flex; gap: 6px; flex-wrap: wrap; max-width: 820px; margin: 0 auto 6px; }
.attach-list:empty { display: none; }
.attach { display: inline-flex; gap: 6px; align-items: center; padding: 4px 6px 4px 10px; background: var(--panel); border: 1px solid var(--line); border-radius: 99px; font-size: 13px; max-width: 220px; }
.attach span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.attach button { border: 0; background: none; cursor: pointer; color: var(--muted); padding: 0 4px; font-size: 15px; }

.menu { position: fixed; z-index: 30; min-width: 190px; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow); padding: 5px; }
.menu button { display: block; width: 100%; text-align: left; border: 0; background: none; padding: 8px 10px; border-radius: 8px; cursor: pointer; }
.menu button:hover { background: var(--hover); }
.menu button.danger { color: var(--danger); }
.menu hr { border: 0; border-top: 1px solid var(--line); margin: 4px 0; }

.dlg { border: 1px solid var(--line); border-radius: 16px; background: var(--panel); color: var(--text); padding: 20px; width: min(480px, calc(100vw - 32px)); box-shadow: var(--shadow); }
.dlg::backdrop { background: rgba(0,0,0,.35); }
.dlg h3 { margin: 0 0 14px; }
.dlg label { display: block; font-size: 13px; font-weight: 600; margin: 12px 0 5px; }
.dlg .hint { font-size: 12.5px; color: var(--muted); font-weight: 400; margin: 4px 0 0; }
.dlg input[type=text], .dlg select, .dlg textarea { width: 100%; padding: 9px 10px; border: 1px solid var(--line); border-radius: 9px; background: var(--bg); }
.dlg textarea { min-height: 110px; resize: vertical; }
.dlg .row { display: flex; gap: 8px; align-items: center; }
.dlg .row select { flex: 1; }
.dlg-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.skill-box { max-height: 180px; overflow-y: auto; border: 1px solid var(--line); border-radius: 9px; padding: 6px 8px; background: var(--bg); }
.skill-box label { display: flex; gap: 8px; align-items: center; font-weight: 400; margin: 3px 0; font-size: 14px; }

.toast { position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 9px 14px; border-radius: 10px; font-size: 14px; z-index: 40; max-width: calc(100vw - 32px); }
.scrim { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 19; }
.only-mobile { display: none; }

@media (max-width: 760px) {
  .only-mobile { display: inline-grid; }
  .side { position: fixed; z-index: 20; inset: 0 auto 0 0; width: min(86vw, 320px); transform: translateX(-100%); transition: transform .2s ease; box-shadow: var(--shadow); }
  .side.open { transform: none; }
  .msgs { padding: 14px 12px 6px; }
  .main-head { padding-left: 6px; }
}
