:root {
  --bg: #f6f5f2; --panel: #ffffff; --panel2: #efede8; --text: #1d1c1a; --muted: #6f6c66;
  --border: #dfdcd5; --accent: #c2603a; --accent-text: #fff; --ok: #2f8a4c; --warn: #b7791f; --bad: #c0392b;
  --user: #e9e4da; --code: #f0eee9; --shadow: 0 1px 2px rgba(0,0,0,.06);
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1b1a18; --panel: #242320; --panel2: #2d2b28; --text: #ebe8e2; --muted: #9c978e;
    --border: #3a3834; --accent: #d97757; --user: #34312c; --code: #2b2926; --shadow: none;
  }
}
:root[data-theme="dark"] {
  --bg: #1b1a18; --panel: #242320; --panel2: #2d2b28; --text: #ebe8e2; --muted: #9c978e;
  --border: #3a3834; --accent: #d97757; --user: #34312c; --code: #2b2926; --shadow: none;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
button { font: inherit; color: inherit; cursor: pointer; }
.btn { border: 1px solid var(--border); background: var(--panel); border-radius: 8px; padding: 6px 12px; }
.btn:hover { background: var(--panel2); }
.btn:disabled { opacity: .45; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.danger { color: var(--bad); }
.btn.icon { padding: 6px 9px; }
.compose .btn { height: 44px; min-width: 44px; display: inline-grid; place-items: center; }
.btn svg { display: block; }

/* вход */
.login { display: grid; place-items: center; height: 100%; padding: 16px; }
.login .card { background: var(--panel); border: 1px solid var(--border); border-radius: 14px; padding: 32px 28px; max-width: 360px; width: 100%; text-align: center; box-shadow: var(--shadow); }
.login h1 { margin: 0 0 6px; font-size: 22px; }
.login p { color: var(--muted); margin: 0 0 22px; }
.login a.btn { display: inline-flex; gap: 10px; align-items: center; text-decoration: none; padding: 10px 18px; }

/* раскладка */
.app { display: grid; grid-template-columns: 260px 1fr; height: 100%; }
.side { background: var(--panel); border-right: 1px solid var(--border); display: flex; flex-direction: column; min-height: 0; }
.side .head { padding: 12px; display: flex; gap: 8px; align-items: center; border-bottom: 1px solid var(--border); }
.side .head b { flex: 1; }
.chats { overflow-y: auto; flex: 1; padding: 6px; }
.chat-item { display: flex; align-items: center; gap: 6px; padding: 8px 10px; border-radius: 8px; cursor: pointer; }
.chat-item:hover { background: var(--panel2); }
.chat-item.active { background: var(--user); }
.chat-item .t { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-item .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ok); flex: none; }
.chat-item .del { opacity: 0; border: 0; background: none; color: var(--muted); padding: 0 4px; }
.chat-item:hover .del { opacity: 1; }
.side .foot { padding: 10px 12px; border-top: 1px solid var(--border); font-size: 13px; color: var(--muted); display: flex; justify-content: space-between; gap: 8px; }
.side .foot a { color: var(--muted); }

.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.bar { display: flex; align-items: center; gap: 8px; padding: 10px 16px; border-bottom: 1px solid var(--border); background: var(--panel); flex-wrap: wrap; }
.bar .menu { display: none; }
.state { display: inline-flex; align-items: center; gap: 6px; font-size: 14px; }
.state i { width: 10px; height: 10px; border-radius: 50%; background: var(--muted); display: inline-block; }
.state.run i { background: var(--ok); }
.state.busy i { background: var(--warn); animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: .35; } }
.spacer { flex: 1; }
.mem { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.mem .track { width: 120px; height: 6px; background: var(--panel2); border-radius: 3px; position: relative; overflow: hidden; }
.mem .fill { position: absolute; inset: 0 auto 0 0; background: var(--ok); border-radius: 3px; }
.mem.warn .fill { background: var(--warn); } .mem.bad .fill { background: var(--bad); } .mem.bad { color: var(--bad); }

.log { flex: 1; overflow-y: auto; padding: 18px 16px 8px; }
.log .inner { max-width: 860px; margin: 0 auto; display: flex; flex-direction: column; gap: 10px; }
.msg { line-height: 1.55; overflow-wrap: anywhere; }
.msg.user { align-self: flex-end; background: var(--user); padding: 8px 14px; border-radius: 14px 14px 4px 14px; max-width: 85%; white-space: pre-wrap; }
.msg.user .files { font-size: 13px; color: var(--muted); margin-top: 4px; }
.msg.assistant p { margin: 0 0 .6em; } .msg.assistant p:last-child { margin-bottom: 0; }
.msg.assistant h1, .msg.assistant h2, .msg.assistant h3, .msg.assistant h4 { margin: .8em 0 .3em; font-size: 1.05em; }
.msg.assistant ul, .msg.assistant ol { margin: .2em 0 .6em; padding-left: 1.4em; }
.msg.assistant table { border-collapse: collapse; margin: .4em 0; font-size: 14px; display: block; overflow-x: auto; }
.msg.assistant th, .msg.assistant td { border: 1px solid var(--border); padding: 4px 8px; text-align: left; }
.msg a { color: var(--accent); }
code { font-family: var(--mono); font-size: .9em; background: var(--code); padding: 1px 4px; border-radius: 4px; }
pre { background: var(--code); padding: 10px 12px; border-radius: 8px; overflow-x: auto; margin: .4em 0 .7em; }
pre code { background: none; padding: 0; font-size: 13px; }
.sys { align-self: center; font-size: 12px; color: var(--muted); }
.result { align-self: flex-start; font-size: 12px; color: var(--muted); }
.result.err { color: var(--bad); }
.tool { border: 1px solid var(--border); border-radius: 8px; background: var(--panel); font-size: 13px; }
.tool summary { padding: 5px 10px; cursor: pointer; list-style: none; display: flex; gap: 8px; align-items: baseline; color: var(--muted); }
.tool summary::-webkit-details-marker { display: none; }
.tool summary b { color: var(--text); font-weight: 600; }
.tool summary .arg { font-family: var(--mono); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.tool summary .st { flex: none; }
.tool.err summary .st { color: var(--bad); }
.tool .body { border-top: 1px solid var(--border); padding: 8px 10px; }
.tool .body pre { margin: 0 0 6px; max-height: 360px; overflow: auto; white-space: pre-wrap; font-size: 12px; }
.tool.sub { margin-left: 18px; }
.cursor::after { content: "▍"; animation: pulse 1s infinite; color: var(--accent); }

.empty { color: var(--muted); text-align: center; margin-top: 18vh; }
.empty h2 { color: var(--text); font-weight: 600; margin-bottom: 6px; }

.compose { padding: 10px 16px 14px; border-top: 1px solid var(--border); background: var(--panel); }
.compose .inner { max-width: 860px; margin: 0 auto; }
.compose .row { display: flex; gap: 8px; align-items: flex-end; }
.compose textarea { flex: 1; resize: none; min-height: 44px; max-height: 40vh; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--border); background: var(--bg); color: var(--text); font: inherit; }
.compose textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.atts { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 6px; }
.att { font-size: 12px; background: var(--panel2); border-radius: 6px; padding: 3px 6px 3px 8px; display: flex; gap: 6px; align-items: center; }
.att img { height: 28px; border-radius: 3px; }
.att button { border: 0; background: none; color: var(--muted); padding: 0 2px; }
.hint { font-size: 12px; color: var(--muted); margin-top: 6px; }
.toast { position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 8px 14px; border-radius: 8px; font-size: 14px; z-index: 50; max-width: calc(100% - 32px); }

@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .side { position: fixed; inset: 0 25% 0 0; z-index: 20; transform: translateX(-100%); transition: transform .2s; visibility: hidden; }
  .app.side-open .side { transform: none; visibility: visible; box-shadow: 0 0 30px rgba(0,0,0,.3); }
  .bar .menu { display: inline-block; }
  .mem .track { width: 60px; }
  .log { padding: 12px; }
  .compose { padding: 8px 12px 12px; }
  .hint { display: none; }
}

/* вкладки */
.tabs { display: flex; gap: 4px; padding: 10px 10px 0; }
.tab { flex: 1; border: 0; background: none; padding: 8px; border-radius: 8px; color: var(--muted); font-weight: 600; }
.tab.active { background: var(--panel2); color: var(--text); }
.side .head .btn { width: 100%; }
.side.files-mode .head, .side.files-mode .chats { display: none; }
.side.files-mode .limits { margin-top: auto; }
select { font: inherit; font-size: 14px; color: var(--text); background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: 5px 6px; max-width: 140px; }

/* лимиты */
.limits { border-top: 1px solid var(--border); padding: 10px 12px; font-size: 12px; color: var(--muted); display: grid; gap: 8px; }
.limits .row { display: grid; gap: 3px; }
.limits .lbl { display: flex; justify-content: space-between; gap: 8px; }
.limits .lbl b { color: var(--text); font-weight: 600; white-space: nowrap; }
.limits .lbl span { white-space: nowrap; }
.limits .track { height: 6px; background: var(--panel2); border-radius: 3px; overflow: hidden; }
.limits .fill { height: 100%; background: var(--ok); border-radius: 3px; }
.limits .warn .fill { background: var(--warn); } .limits .bad .fill { background: var(--bad); }
.limits .bad .lbl b { color: var(--bad); }
.limits .note { font-size: 11px; }

/* файлы */
.files { flex: 1; overflow-y: auto; padding: 14px 16px; }
.files .inner { max-width: 960px; margin: 0 auto; }
.fs-bar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 10px; }
.crumbs { display: flex; flex-wrap: wrap; align-items: center; gap: 2px; font-size: 15px; min-width: 0; }
.crumbs a { color: var(--accent); cursor: pointer; text-decoration: none; padding: 2px 4px; border-radius: 4px; }
.crumbs a:hover { background: var(--panel2); }
.crumbs span { color: var(--muted); }
.fs-list { background: var(--panel); border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.fs-row { display: grid; grid-template-columns: 28px 1fr auto auto auto; gap: 10px; align-items: center; padding: 8px 12px; border-top: 1px solid var(--border); }
.fs-row:first-child { border-top: 0; }
.fs-row:hover { background: var(--panel2); }
.fs-row .ic { color: var(--muted); display: grid; place-items: center; }
.fs-row .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: pointer; }
.fs-row .nm:hover { color: var(--accent); }
.fs-row .sz, .fs-row .dt { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }
.fs-row .acts { display: flex; gap: 2px; }
.fs-row .acts button { border: 0; background: none; color: var(--muted); padding: 4px 6px; border-radius: 6px; font-size: 13px; }
.fs-row .acts button:hover { background: var(--border); color: var(--text); }
.fs-empty { padding: 30px; text-align: center; color: var(--muted); }
.fs-foot { font-size: 12px; color: var(--muted); margin-top: 8px; overflow-wrap: anywhere; }
.fs-progress { height: 4px; background: var(--panel2); border-radius: 2px; overflow: hidden; margin-bottom: 8px; }
.fs-progress span { display: block; height: 100%; width: 0; background: var(--accent); transition: width .2s; }
.files.drop .fs-list { outline: 2px dashed var(--accent); outline-offset: 4px; }
.viewer { position: fixed; inset: 0; background: rgba(0,0,0,.75); z-index: 40; display: grid; place-items: center; padding: 16px; }
.viewer img { max-width: 100%; max-height: 100%; border-radius: 6px; }
.viewer pre { background: var(--panel); color: var(--text); max-width: 100%; max-height: 100%; overflow: auto; padding: 14px; white-space: pre-wrap; }
@media (max-width: 760px) {
  .fs-row { grid-template-columns: 24px 1fr auto; }
  .fs-row .dt { display: none; } .fs-row .sz { display: none; }
  select { max-width: 96px; }
}

.fs-marks { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin-bottom: 10px; }
.bm { border: 1px solid var(--border); background: var(--panel); border-radius: 999px; padding: 3px 11px; font-size: 13px; font-family: var(--mono); }
.bm:hover { background: var(--panel2); }
.chk { font-size: 13px; color: var(--muted); display: inline-flex; gap: 5px; align-items: center; cursor: pointer; }
.crumbs { font-family: var(--mono); font-size: 14px; }
.fs-row.hid .nm { color: var(--muted); }
.editor { width: min(1100px, 100%); height: min(88vh, 100%); background: var(--panel); border-radius: 10px; display: flex; flex-direction: column; overflow: hidden; border: 1px solid var(--border); }
.ed-bar { display: flex; gap: 8px; align-items: center; padding: 8px 10px; border-bottom: 1px solid var(--border); }
.ed-path { flex: 1; font-family: var(--mono); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ed-state { font-size: 12px; color: var(--muted); white-space: nowrap; }
.editor textarea { flex: 1; border: 0; resize: none; padding: 12px; font: 13px/1.5 var(--mono); background: var(--bg); color: var(--text); outline: none; white-space: pre; overflow: auto; tab-size: 4; }
@media (max-width: 760px) { .editor { height: 100%; } .ed-path { font-size: 11px; } }
