:root {
  --bg: #15130f; --panel: #1d1a15; --raise: #27231c; --line: #353026; --text: #ece7dc; --mute: #a39a88;
  --accent: #e08a5b; --accent-ink: #1a120c; --ok: #7fb685; --bad: #e0685b; --warn: #e0b45b; --user: #2f2a21;
  --mono: ui-monospace, "Cascadia Code", Menlo, Consolas, monospace;
  color-scheme: dark;
}
@media (prefers-color-scheme: light) {
  :root { --bg: #f6f3ec; --panel: #efeadf; --raise: #fffdf8; --line: #ddd5c4; --text: #2a251c; --mute: #7a7160;
    --accent: #c4622f; --accent-ink: #fff; --ok: #3d8a4a; --bad: #c0392b; --warn: #a8791a; --user: #e6dfcf; color-scheme: light; }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; overflow: hidden; -webkit-text-size-adjust: 100%; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; border-radius: 10px; padding: 8px 12px; }
button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible, a:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.primary { background: var(--accent); color: var(--accent-ink); font-weight: 600; }
.ghost { color: var(--mute); padding: 6px 8px; font-size: 13px; }
.ghost:hover { color: var(--text); background: var(--raise); }
.danger { color: var(--bad); }
.icon { width: 40px; height: 40px; padding: 0; font-size: 22px; line-height: 1; color: var(--mute); flex: none; }
.icon:hover { background: var(--raise); color: var(--text); }
.row { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.grow { flex: 1; }
.err { color: var(--bad); font-size: 14px; margin: 0; min-height: 1em; }
[hidden] { display: none !important; }

/* kirish */
#auth { height: 100dvh; display: grid; place-items: center; padding: 20px; overflow: auto; }
.card { width: min(380px, 100%); display: grid; gap: 14px; background: var(--panel); border: 1px solid var(--line); border-radius: 18px; padding: 28px 24px; }
.card h1 { margin: 0; font-size: 22px; }
.card div[data-step] { display: grid; gap: 14px; }
label { display: grid; gap: 6px; font-size: 13px; color: var(--mute); }
input, select { background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; font-size: 16px; color: var(--text); width: 100%; }
.secret { font-family: var(--mono); background: var(--bg); border: 1px dashed var(--line); padding: 10px; border-radius: 10px; word-break: break-all; user-select: all; }
.btn { display: inline-block; text-align: center; padding: 8px 12px; border: 1px solid var(--line); border-radius: 10px; color: var(--accent); text-decoration: none; }

/* tuzilma */
#app { display: grid; grid-template-columns: 300px 1fr; height: 100dvh; }
#side { display: flex; flex-direction: column; background: var(--panel); border-right: 1px solid var(--line); min-height: 0; padding-top: env(safe-area-inset-top); }
#side > header { display: flex; align-items: center; gap: 10px; padding: 14px 16px; font-size: 18px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--bad); margin-left: auto; }
.dot.on { background: var(--ok); }
#list { flex: 1; overflow-y: auto; overflow-x: hidden; padding: 0 8px 8px; }
.chatrow > div { min-width: 0; }
.proj { margin-top: 10px; }
.proj > .ph { display: flex; align-items: center; padding: 6px 8px; font-size: 12px; letter-spacing: .06em; text-transform: uppercase; color: var(--mute); }
.proj > .ph span { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.proj > .ph button { padding: 2px 9px; font-size: 18px; color: var(--accent); }
.chatrow { display: grid; grid-template-columns: 10px 1fr; gap: 8px; align-items: center; width: 100%; text-align: left; padding: 9px 10px; border-radius: 10px; }
.chatrow:hover { background: var(--raise); }
.chatrow.cur { background: var(--raise); box-shadow: inset 2px 0 var(--accent); }
.chatrow b { font-weight: 500; display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chatrow small { color: var(--mute); display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 12.5px; }
.chatrow.unread b { font-weight: 700; }
.st { width: 8px; height: 8px; border-radius: 50%; background: transparent; }
.st.running { background: var(--accent); animation: pulse 1.2s infinite; }
.st.waiting { background: var(--warn); }
.st.unread { background: var(--ok); }
@keyframes pulse { 50% { opacity: .3; } }
#side > footer { border-top: 1px solid var(--line); padding: 10px 12px calc(10px + env(safe-area-inset-bottom)); display: grid; gap: 8px; }
.lim { font-size: 12px; color: var(--mute); display: grid; gap: 3px; margin-bottom: 6px; }
.lim .bar { height: 5px; background: var(--line); border-radius: 3px; overflow: hidden; }
.lim .bar i { display: block; height: 100%; background: var(--ok); }
.lim .bar i.mid { background: var(--warn); } .lim .bar i.hi { background: var(--bad); }
.lim .hd { display: flex; justify-content: space-between; }
.wk { font-size: 12px; color: var(--mute); }

#main { min-width: 0; min-height: 0; display: flex; flex-direction: column; }
#empty { margin: auto; text-align: center; color: var(--mute); }
#empty img { opacity: .5; }
#chat { display: flex; flex-direction: column; height: 100%; min-height: 0; }
#chat > header { display: flex; align-items: center; gap: 6px; padding: calc(8px + env(safe-area-inset-top)) 10px 8px; border-bottom: 1px solid var(--line); }
#chat .title { flex: 1; min-width: 0; }
#chat .title strong, #chat .title small { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#chat .title small { color: var(--mute); font-size: 12px; }
#back { display: none; font-size: 30px; }

#msgs { flex: 1; overflow-y: auto; padding: 16px max(14px, calc((100% - 820px) / 2)); overscroll-behavior: contain; }
.m { margin: 0 0 12px; overflow-wrap: anywhere; }
.m.user { margin-left: auto; max-width: 86%; width: fit-content; background: var(--user); padding: 9px 14px; border-radius: 16px 16px 4px 16px; white-space: pre-wrap; }
.m.user .files { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.m.user .files img { max-width: 160px; max-height: 160px; border-radius: 8px; display: block; }
.m.user .files a { font-size: 13px; color: var(--accent); }
.m.text > :first-child { margin-top: 0; } .m.text > :last-child { margin-bottom: 0; }
.m.text p { margin: .5em 0; } .m.text ul, .m.text ol { padding-left: 1.4em; margin: .5em 0; }
.m.text h1, .m.text h2, .m.text h3 { font-size: 1.05em; margin: 1em 0 .4em; }
.m.text a { color: var(--accent); }
.m.text code { font-family: var(--mono); font-size: .88em; background: var(--raise); padding: 1px 5px; border-radius: 5px; }
.m.text pre, .tool pre { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; overflow-x: auto; font: 13px/1.45 var(--mono); margin: .6em 0; white-space: pre; }
.m.text pre code { background: none; padding: 0; font-size: inherit; }
.m.text table { border-collapse: collapse; display: block; overflow-x: auto; margin: .6em 0; font-size: 14px; }
.m.text td, .m.text th { border: 1px solid var(--line); padding: 5px 9px; text-align: left; }
.m.live::after { content: "▍"; color: var(--accent); animation: pulse 1s infinite; }
.tool { margin: 0 0 4px; font-size: 13.5px; color: var(--mute); }
.tool + .m { margin-top: 12px; }
.tool summary { cursor: pointer; list-style: none; display: flex; gap: 8px; align-items: baseline; padding: 3px 0; }
.tool summary::-webkit-details-marker { display: none; }
.tool summary b { font-weight: 600; color: var(--text); flex: none; }
.tool summary span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-family: var(--mono); font-size: 12.5px; }
.tool summary i { font-style: normal; flex: none; width: 1em; }
.tool.ok summary i { color: var(--ok); } .tool.bad summary i { color: var(--bad); }
.tool pre { white-space: pre-wrap; max-height: 320px; overflow-y: auto; }
.perm { border: 1px solid var(--warn); border-radius: 12px; padding: 12px; margin: 0 0 12px; background: var(--panel); }
.perm.danger { border-color: var(--bad); }
.perm h4 { margin: 0 0 6px; font-size: 14px; }
.perm pre { margin: 0 0 10px; font: 13px/1.4 var(--mono); white-space: pre-wrap; max-height: 220px; overflow: auto; }
.perm .row button { border: 1px solid var(--line); }
.perm.done { opacity: .6; border-color: var(--line); }
.meta { font-size: 12px; color: var(--mute); margin: -4px 0 16px; }
.meta.bad { color: var(--bad); white-space: pre-wrap; font-size: 13.5px; }

#composer { border-top: 1px solid var(--line); padding: 8px max(10px, calc((100% - 820px) / 2)) calc(10px + env(safe-area-inset-bottom)); background: var(--bg); }
#chips { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 8px; scrollbar-width: none; }
#chips button { flex: none; border: 1px solid var(--line); border-radius: 999px; padding: 4px 12px; font-size: 13px; color: var(--mute); }
#chips button:hover { color: var(--text); border-color: var(--accent); }
#attach { display: flex; flex-wrap: wrap; gap: 6px; }
#attach:not(:empty) { padding-bottom: 8px; }
#attach span { background: var(--raise); border-radius: 8px; padding: 3px 4px 3px 10px; font-size: 13px; display: flex; align-items: center; gap: 2px; max-width: 100%; }
#attach span button { padding: 0 6px; color: var(--mute); }
.inputrow { display: flex; align-items: flex-end; gap: 6px; background: var(--panel); border: 1px solid var(--line); border-radius: 22px; padding: 4px; }
.inputrow:focus-within { border-color: var(--accent); }
#input { flex: 1; resize: none; border: 0; background: none; outline: none; padding: 8px 4px; max-height: 40dvh; font-size: 16px; line-height: 1.4; }
.send { width: 40px; height: 40px; border-radius: 50%; background: var(--accent); color: var(--accent-ink); font-size: 20px; font-weight: 700; padding: 0; flex: none; }
.send.stop { background: var(--bad); color: #fff; font-size: 14px; }

dialog { border: 1px solid var(--line); border-radius: 18px; background: var(--panel); color: var(--text); padding: 0; width: min(440px, calc(100vw - 24px)); }
dialog::backdrop { background: rgba(0, 0, 0, .55); }
.sheet { display: grid; gap: 14px; padding: 22px; }
.sheet h2 { margin: 0; font-size: 18px; }

@media (max-width: 760px) {
  #app { grid-template-columns: 1fr; }
  #main { display: none; }
  body.open #side { display: none; }
  body.open #main { display: flex; }
  #back { display: block; }
  #side { border-right: 0; }
}
@media (prefers-reduced-motion: reduce) { * { animation: none !important; } }
