:root {
  --bg: #f7f7f5; --panel: #ffffff; --side: #efeee9; --text: #1d1d1b; --muted: #74726b;
  --border: #e2e0d8; --accent: #c96442; --accent-2: #b4552f; --user: #ece9e0; --code: #f1efe8;
  --hover: #e5e3db; --danger: #c0392b;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1f1e1c; --panel: #262523; --side: #191817; --text: #ecebe6; --muted: #9b998f;
    --border: #3a3935; --accent: #d97757; --accent-2: #e08a6c; --user: #33322e; --code: #2d2c29;
    --hover: #2e2d2a; --danger: #e5675a;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text);
  font: 15px/1.6 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; overscroll-behavior: none; }
button { font: inherit; color: inherit; cursor: pointer; }
input, textarea { font: inherit; color: inherit; font-size: 16px; }

/* login */
.login { display: grid; place-items: center; height: 100dvh; padding: 16px; }
.login form { background: var(--panel); border: 1px solid var(--border); border-radius: 16px; padding: 28px; width: 100%; max-width: 360px; display: grid; gap: 14px; }
.login h1 { margin: 0; font-size: 22px; }
.login p { margin: 0; color: var(--muted); font-size: 14px; }
.login input { padding: 12px 14px; border-radius: 10px; border: 1px solid var(--border); background: var(--bg); outline: none; }
.login input:focus { border-color: var(--accent); }
.login button { padding: 12px; border: 0; border-radius: 10px; background: var(--accent); color: #fff; font-weight: 600; }
.login .err { color: var(--danger); font-size: 14px; min-height: 1em; }

/* layout */
.app { display: flex; height: 100dvh; }
aside { width: 270px; flex-shrink: 0; background: var(--side); border-right: 1px solid var(--border); display: flex; flex-direction: column; }
aside .top { padding: 12px; display: flex; gap: 8px; }
.new-btn { flex: 1; display: flex; align-items: center; gap: 8px; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--border); background: var(--panel); font-weight: 600; }
.new-btn:hover { border-color: var(--accent); }
.chats { flex: 1; overflow-y: auto; padding: 4px 8px 12px; }
.chat-item { display: flex; align-items: center; gap: 6px; padding: 8px 10px; border-radius: 8px; cursor: pointer; font-size: 14px; }
.chat-item:hover { background: var(--hover); }
.chat-item.active { background: var(--hover); font-weight: 600; }
.chat-item .t { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); animation: pulse 1s infinite alternate; }
.chat-item .act { opacity: 0; border: 0; background: none; padding: 2px 4px; color: var(--muted); border-radius: 4px; }
.chat-item:hover .act { opacity: 1; }
.chat-item .act:hover { color: var(--text); background: var(--border); }
aside .foot { padding: 10px 12px; border-top: 1px solid var(--border); display: flex; justify-content: space-between; color: var(--muted); font-size: 13px; }
aside .foot button { border: 0; background: none; color: var(--muted); }
aside .foot button:hover { color: var(--text); }

main { flex: 1; display: flex; flex-direction: column; min-width: 0; position: relative; }
header { display: none; align-items: center; gap: 10px; padding: 10px 12px; border-bottom: 1px solid var(--border); }
header .title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn { border: 0; background: none; padding: 6px; border-radius: 8px; display: grid; place-items: center; }
.icon-btn:hover { background: var(--hover); }

.messages { flex: 1; overflow-y: auto; padding: 24px 16px 8px; }
.inner { max-width: 780px; margin: 0 auto; }
.empty { height: 100%; display: grid; place-items: center; text-align: center; color: var(--muted); }
.empty h2 { color: var(--text); font-weight: 600; font-size: 26px; margin: 0 0 6px; }

.msg { margin: 0 0 22px; }
.msg.user { display: flex; flex-direction: column; align-items: flex-end; }
.msg.user .bubble { background: var(--user); padding: 10px 14px; border-radius: 16px 16px 4px 16px; max-width: 85%; white-space: pre-wrap; word-wrap: break-word; }
.msg.assistant .bubble { word-wrap: break-word; }
.bubble > :first-child { margin-top: 0; } .bubble > :last-child { margin-bottom: 0; }
.bubble img { max-width: 100%; border-radius: 10px; cursor: zoom-in; }
.bubble pre { background: var(--code); padding: 12px; border-radius: 10px; overflow-x: auto; font-size: 13px; position: relative; }
.bubble code { font-family: ui-monospace, "JetBrains Mono", Menlo, monospace; font-size: .9em; }
.bubble :not(pre) > code { background: var(--code); padding: 1px 5px; border-radius: 5px; }
.bubble table { border-collapse: collapse; display: block; overflow-x: auto; }
.bubble th, .bubble td { border: 1px solid var(--border); padding: 6px 10px; }
.bubble a { color: var(--accent); }
.bubble blockquote { margin: 0; padding-left: 12px; border-left: 3px solid var(--border); color: var(--muted); }
.copy { position: absolute; top: 6px; right: 6px; font-size: 12px; padding: 2px 8px; border-radius: 6px; border: 1px solid var(--border); background: var(--panel); opacity: .7; }
.copy:hover { opacity: 1; }

.tools { display: flex; flex-direction: column; gap: 2px; margin: 0 0 8px; font-size: 13px; color: var(--muted); }
.tools details summary { cursor: pointer; list-style: none; }
.tools details summary::before { content: "▸ "; } .tools details[open] summary::before { content: "▾ "; }
.tools .live { animation: fade 1.2s infinite alternate; }
.typing { display: inline-flex; gap: 4px; padding: 6px 0; }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: pulse .9s infinite alternate; }
.typing i:nth-child(2) { animation-delay: .3s; } .typing i:nth-child(3) { animation-delay: .6s; }
@keyframes pulse { from { opacity: .25; } to { opacity: 1; } }
@keyframes fade { from { opacity: .5; } to { opacity: 1; } }

.files { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.msg.user .files { justify-content: flex-end; }
.file { display: flex; align-items: center; gap: 8px; padding: 8px 12px; border: 1px solid var(--border); border-radius: 10px; background: var(--panel); font-size: 13px; text-decoration: none; color: inherit; max-width: 260px; }
.file:hover { border-color: var(--accent); }
.file .n { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.file .s { color: var(--muted); flex-shrink: 0; }
.thumb { display: block; border-radius: 10px; overflow: hidden; border: 1px solid var(--border); }
.thumb img { display: block; max-width: 240px; max-height: 240px; object-fit: cover; cursor: zoom-in; }
.files audio, .files video { max-width: 100%; border-radius: 10px; }
.meta { font-size: 12px; color: var(--muted); margin-top: 6px; }

/* composer */
.composer { padding: 8px 16px 16px; padding-bottom: max(16px, env(safe-area-inset-bottom)); }
.box { max-width: 780px; margin: 0 auto; background: var(--panel); border: 1px solid var(--border); border-radius: 18px; padding: 8px; box-shadow: 0 2px 12px rgba(0,0,0,.04); }
.box:focus-within { border-color: var(--accent); }
.box.drag { border-style: dashed; border-color: var(--accent); }
.pending { display: flex; flex-wrap: wrap; gap: 6px; padding: 4px 4px 8px; }
.pending:empty { display: none; }
.chip { display: flex; align-items: center; gap: 6px; padding: 4px 6px 4px 4px; border: 1px solid var(--border); border-radius: 8px; font-size: 12px; max-width: 200px; background: var(--bg); }
.chip img { width: 32px; height: 32px; object-fit: cover; border-radius: 5px; }
.chip .n { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chip .x { border: 0; background: none; color: var(--muted); padding: 0 2px; }
.chip.up { opacity: .55; }
.row { display: flex; align-items: flex-end; gap: 6px; }
textarea { flex: 1; resize: none; border: 0; outline: none; background: none; padding: 8px 6px; max-height: 220px; line-height: 1.45; }
.send { width: 38px; height: 38px; border-radius: 12px; border: 0; background: var(--accent); color: #fff; display: grid; place-items: center; flex-shrink: 0; }
.send:disabled { opacity: .35; cursor: default; }
.send.stop { background: var(--text); color: var(--bg); }
.hint { max-width: 780px; margin: 6px auto 0; font-size: 12px; color: var(--muted); text-align: center; }

.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.85); display: grid; place-items: center; z-index: 50; cursor: zoom-out; padding: 16px; }
.lightbox img { max-width: 100%; max-height: 100%; border-radius: 8px; }
.backdrop { display: none; }

@media (max-width: 760px) {
  aside { position: fixed; inset: 0 auto 0 0; z-index: 20; transform: translateX(-100%); transition: transform .22s ease; width: 82%; max-width: 300px; }
  .app.open aside { transform: none; }
  .app.open .backdrop { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 15; }
  header { display: flex; }
  .messages { padding: 16px 16px 8px; }
  .composer { padding: 8px 10px 10px; }
  .chat-item .act { opacity: 1; }
  .msg.user .bubble { max-width: 92%; }
}
