:root {
  --bg: #faf9f6; --panel: #fff; --fg: #1f1e1b; --muted: #6b6860; --line: #e4e1d9;
  --accent: #c96442; --accent-fg: #fff; --user: #f0ede6; --code: #f4f2ec; --danger: #b3261e;
  --ok: #2e7d32; --warn-bg: #fff4d6; --add: #e6f4ea; --del: #fdecea;
  --radius: 10px; --side: 290px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #1b1a18; --panel: #24231f; --fg: #ece9e1; --muted: #a29e93; --line: #3a3832;
    --user: #2e2c27; --code: #2a2924; --warn-bg: #3d3420; --add: #1e3524; --del: #3d2220; }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--fg); overflow-x: hidden; }
body { font-size: 15px; line-height: 1.5; }
button, input, select, textarea { font: inherit; color: inherit; }
button { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 6px 12px; cursor: pointer; min-height: 36px; }
button:hover:not([disabled]) { border-color: var(--muted); }
button[disabled] { opacity: .5; cursor: default; }
button.primary { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
button.danger { background: var(--danger); color: #fff; border-color: var(--danger); }
button.link { background: none; border: none; padding: 2px 0; color: var(--accent); min-height: 0; text-align: left; }
button.icon { padding: 4px 8px; min-width: 36px; }
input, select, textarea { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 7px 10px; max-width: 100%; }
select { min-height: 36px; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.error { color: var(--danger); }
.warn { background: var(--warn-bg); padding: 6px 10px; border-radius: 8px; margin: 6px 0; }
.check { display: inline-flex; align-items: center; gap: 6px; }

/* вход */
.login { max-width: 340px; margin: 12vh auto; padding: 24px; display: flex; flex-direction: column; gap: 10px; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); }
.login h1 { margin: 0 0 8px; font-size: 22px; }
.login-error { color: var(--danger); min-height: 1.4em; }

/* каркас */
.app { display: grid; grid-template-columns: var(--side) minmax(0, 1fr); height: 100dvh; }
.sidebar { border-right: 1px solid var(--line); background: var(--panel); display: flex; flex-direction: column; gap: 8px; padding: 10px; min-height: 0; overflow: hidden; }
.side-head { display: flex; gap: 6px; }
.side-head select { flex: 1; min-width: 0; }
.close-side, .menu-btn, .backdrop { display: none; }
.thread-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; min-height: 0; }
.thread-list li a { display: block; padding: 7px 8px; border-radius: 8px; text-decoration: none; color: inherit; }
.thread-list li.current a, .thread-list li a:hover { background: var(--user); }
.thread-list .tt { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.thread-list .td { display: block; font-size: 12px; color: var(--muted); }
.thread-list li.muted { padding: 8px; }
.side-foot { display: flex; flex-direction: column; gap: 4px; border-top: 1px solid var(--line); padding-top: 8px; font-size: 14px; }
.whoami { color: var(--muted); }

.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; position: relative; }
.main.drop::after { content: 'Отпустите файлы, чтобы прикрепить'; position: absolute; inset: 8px; border: 2px dashed var(--accent); border-radius: var(--radius); display: grid; place-items: center; background: color-mix(in srgb, var(--bg) 85%, transparent); font-weight: 600; z-index: 5; }
.topbar { display: flex; align-items: center; gap: 8px; padding: 8px 12px; border-bottom: 1px solid var(--line); background: var(--panel); min-width: 0; }
.thread-title { flex: 1; margin: 0; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; cursor: text; min-width: 0; }
.top-actions { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.session-error { background: var(--warn-bg); padding: 8px 12px; border-bottom: 1px solid var(--line); }

.feed { flex: 1; overflow-y: auto; overflow-x: hidden; padding: 12px 16px 24px; min-height: 0; }
.empty { color: var(--muted); text-align: center; padding: 40px 10px; }
.load-more { display: block; margin: 0 auto 12px; }
.task { max-width: 900px; margin: 0 auto 22px; }
.msg { border-radius: var(--radius); padding: 10px 14px; margin: 8px 0; overflow-wrap: anywhere; min-width: 0; }
.msg.user { background: var(--user); margin-left: 8%; }
.msg.assistant.progress { border-left: 3px solid var(--line); border-radius: 0 var(--radius) var(--radius) 0; color: var(--muted); padding: 4px 12px; font-size: 14px; }
.msg.assistant.final { background: var(--panel); border: 1px solid var(--line); }
.stage { font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.meta { font-size: 12px; color: var(--muted); margin-top: 4px; }
.model { font-family: ui-monospace, monospace; font-size: 11.5px; }
.run-status { font-size: 13px; color: var(--muted); display: flex; flex-wrap: wrap; gap: 2px; align-items: center; }
.run-status .error { flex-basis: 100%; }
.spinner { width: 10px; height: 10px; border-radius: 50%; border: 2px solid var(--accent); border-right-color: transparent; display: inline-block; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* markdown */
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md pre { background: var(--code); padding: 10px; border-radius: 8px; overflow-x: auto; max-width: 100%; font-size: 13px; }
.md code { font-family: ui-monospace, "SFMono-Regular", Menlo, monospace; background: var(--code); padding: 1px 4px; border-radius: 4px; font-size: .92em; }
.md pre code { background: none; padding: 0; }
.md blockquote { margin: 6px 0; padding-left: 10px; border-left: 3px solid var(--line); color: var(--muted); }
.md .table-wrap { overflow-x: auto; max-width: 100%; }
.md table { border-collapse: collapse; margin: 6px 0; }
.md th, .md td { border: 1px solid var(--line); padding: 4px 8px; }
.md a { color: var(--accent); }
.code-fold summary { cursor: pointer; color: var(--muted); font-size: 13px; }
.atts { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.att-img img { max-width: 160px; max-height: 120px; border-radius: 6px; display: block; }
.att-file { font-size: 13px; color: var(--accent); }

/* подтверждения */
.approval { border: 1px solid var(--accent); border-radius: var(--radius); padding: 10px 14px; margin: 8px 0; background: var(--panel); }
.approval:not(.pending) { border-color: var(--line); opacity: .85; }
.ap-title { font-weight: 600; }
.ap-context { background: var(--code); padding: 8px; border-radius: 6px; white-space: pre-wrap; overflow-wrap: anywhere; font-size: 13px; }
.ap-actions { display: flex; gap: 8px; margin-top: 8px; flex-wrap: wrap; }
.approval fieldset { border: 1px solid var(--line); border-radius: 8px; margin: 6px 0; display: flex; flex-direction: column; gap: 4px; }
.expires { font-weight: 400; font-size: 12px; color: var(--muted); }

/* ввод */
.typing { font-size: 12px; color: var(--muted); padding: 0 16px; min-height: 18px; }
.composer { border-top: 1px solid var(--line); background: var(--panel); padding: 8px 12px calc(8px + env(safe-area-inset-bottom)); }
.composer textarea { width: 100%; resize: vertical; min-height: 56px; max-height: 40vh; }
.composer-bar { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; margin-top: 6px; }
.composer-bar select { max-width: 200px; }
.spacer { flex: 1; }
.mode-badge { font-size: 12px; color: var(--muted); }
.mode-bypassPermissions { color: var(--danger); font-weight: 600; }
.drafts { display: flex; flex-wrap: wrap; gap: 6px; }
.draft { display: flex; align-items: center; gap: 6px; border: 1px solid var(--line); border-radius: 8px; padding: 3px 6px; font-size: 13px; max-width: 100%; }
.draft img { width: 32px; height: 32px; object-fit: cover; border-radius: 4px; }
.draft.error { border-color: var(--danger); }
.dname { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 180px; }

/* панели */
.panel { position: fixed; top: 0; right: 0; bottom: 0; width: min(720px, 100vw); background: var(--panel); border-left: 1px solid var(--line); z-index: 20; display: flex; flex-direction: column; box-shadow: -8px 0 24px rgba(0,0,0,.12); }
.panel-head { display: flex; align-items: center; justify-content: space-between; padding: 8px 12px; border-bottom: 1px solid var(--line); }
.panel-head h3 { margin: 0; font-size: 16px; }
.panel-body { overflow: auto; padding: 12px; display: flex; flex-direction: column; gap: 8px; }
.kv { display: flex; justify-content: space-between; gap: 12px; border-bottom: 1px dashed var(--line); padding: 3px 0; flex-wrap: wrap; }
.chg-file { border: 1px solid var(--line); border-radius: 8px; padding: 6px 8px; }
.chg-file summary { cursor: pointer; overflow-wrap: anywhere; }
.chg { font-size: 12px; padding: 1px 6px; border-radius: 4px; background: var(--user); }
.chg-added, .chg-untracked { background: var(--add); }
.chg-deleted { background: var(--del); }
.tag { font-size: 11px; color: var(--muted); border: 1px solid var(--line); border-radius: 4px; padding: 0 4px; margin-left: 4px; }
pre.diff { font-size: 12.5px; overflow-x: auto; background: var(--code); padding: 8px; border-radius: 6px; margin: 6px 0 0; }
pre.diff .da { background: var(--add); display: inline-block; min-width: 100%; }
pre.diff .dd { background: var(--del); display: inline-block; min-width: 100%; }
pre.diff .dc { color: var(--accent); }
pre.diff .dh { color: var(--muted); }
.admin-form, .panel-body > div { display: flex; flex-direction: column; gap: 6px; }
.row { display: flex; gap: 6px; flex-wrap: wrap; }
.members { margin: 0; padding-left: 18px; }

.toast { position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%); background: var(--fg); color: var(--bg); padding: 8px 14px; border-radius: 8px; opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 40; max-width: calc(100vw - 24px); }
.toast.show { opacity: 1; }
.toast.error { background: var(--danger); color: #fff; }

/* узкие экраны (до 360 px включительно) */
@media (max-width: 820px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .sidebar { position: fixed; top: 0; bottom: 0; left: 0; width: min(var(--side), 86vw); z-index: 30; transform: translateX(-100%); transition: transform .2s; }
  .app.side-open .sidebar { transform: none; }
  .app.side-open .backdrop { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 25; }
  .close-side, .menu-btn { display: inline-block; }
  .feed { padding: 8px 10px 16px; }
  .msg.user { margin-left: 0; }
  .top-actions button { padding: 4px 8px; font-size: 13px; }
  .composer-bar select { max-width: 46vw; }
}
@media (max-width: 420px) {
  .topbar { flex-wrap: wrap; }
  .thread-title { order: 1; flex-basis: calc(100% - 50px); }
  .top-actions { order: 2; width: 100%; justify-content: flex-start; }
  .mode-badge { flex-basis: 100%; order: 10; }
}
