:root {
  --bg: #faf9f6; --panel: #f1efe9; --card: #ffffff; --line: #e2dfd6; --text: #22201c; --muted: #6f6b62;
  --accent: #1e5548; --accent-ink: #ffffff; --accent-soft: #e3eee9; --danger: #b3412e; --user: #ebe7dc;
  --code: #f4f2ec; --shadow: 0 6px 24px rgba(0,0,0,.08);
  --side-w: 290px; --files-w: 340px; --max: 780px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1b19; --panel: #232220; --card: #2a2926; --line: #3a3834; --text: #ecebe6; --muted: #a29e94;
    --accent: #5fae98; --accent-ink: #10201b; --accent-soft: #23352f; --danger: #e07a64; --user: #33312d;
    --code: #252421; --shadow: 0 6px 24px rgba(0,0,0,.4);
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 16px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; -webkit-text-size-adjust: 100%; }
[hidden] { display: none !important; }
button, select, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 1px solid transparent; border-radius: 10px; padding: 8px 14px; background: transparent; }
button:disabled { opacity: .5; cursor: default; }
.primary { background: var(--accent); color: var(--accent-ink); }
.danger { background: var(--danger); color: #fff; }
.ghost { border-color: var(--line); background: var(--card); }
.ghost:hover { background: var(--panel); }
.small { padding: 4px 10px; font-size: 14px; }
.icon { width: 40px; height: 40px; padding: 0; display: inline-grid; place-items: center; flex: none; }
.wide { width: 100%; }
select, input, textarea { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; }
input:focus, textarea:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

/* вход */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(360px, 100%); background: var(--card); border: 1px solid var(--line); border-radius: 18px; padding: 28px; display: grid; gap: 14px; box-shadow: var(--shadow); }
.login-card h1 { margin: 0 0 6px; font-size: 22px; text-align: center; }
.login-card label { display: grid; gap: 6px; font-size: 14px; color: var(--muted); }
.login-logo { width: 56px; height: 56px; margin: 0 auto; }
.err { color: var(--danger); min-height: 1.2em; margin: 0; font-size: 14px; }

/* раскладка */
.app { display: flex; height: 100dvh; overflow: hidden; }
.side { width: var(--side-w); flex: none; background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.side-head { padding: 12px; }
.search { margin: 0 12px 8px; }
.sess-list { flex: 1; overflow-y: auto; padding: 0 8px 8px; }
.sess-group { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); padding: 12px 8px 4px; }
.sess { display: flex; align-items: center; gap: 8px; width: 100%; text-align: left; padding: 8px 10px; border-radius: 10px; color: var(--text); }
.sess:hover { background: var(--card); }
.sess.active { background: var(--card); border-color: var(--line); }
.sess-t { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sess-d { font-size: 12px; color: var(--muted); flex: none; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); flex: none; animation: pulse 1.2s infinite; }
.dot.wait { background: #d39b2a; }
@keyframes pulse { 50% { opacity: .3; } }
.side-foot { display: flex; align-items: center; gap: 8px; padding: 10px 12px; border-top: 1px solid var(--line); }
.me { flex: 1; font-weight: 600; overflow: hidden; text-overflow: ellipsis; }

.main { flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.top { display: flex; align-items: center; gap: 8px; padding: 8px 12px; border-bottom: 1px solid var(--line); min-height: 56px; }
#menuBtn { display: none; }
.top-title { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; cursor: text; }
.subtitle { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.msgs { flex: 1; overflow-y: auto; padding: 20px 16px 40px; scroll-behavior: auto; scrollbar-gutter: stable; }
.msgs > * { max-width: var(--max); margin-left: auto; margin-right: auto; }
.empty { text-align: center; color: var(--muted); margin-top: 18vh; }
.empty-logo { width: 64px; opacity: .8; }

.m-user { display: flex; justify-content: flex-end; margin-top: 18px; margin-bottom: 18px; }
/* Ширину ограничивает колонка, а не пузырь: иначе короткий пузырь отъезжает от правого края. */
.m-user > div { max-width: 85%; min-width: 0; display: flex; flex-direction: column; align-items: flex-end; }
.m-user .bubble { background: var(--user); border-radius: 18px 18px 4px 18px; padding: 10px 14px; max-width: 100%; white-space: pre-wrap; overflow-wrap: anywhere; }
.m-user.queued .bubble { opacity: .5; }
.m-user .qlabel { font-size: 11px; color: var(--muted); text-align: right; margin-top: 3px; }
.m-files { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; justify-content: flex-end; }
.m-text { margin: 10px 0; overflow-wrap: anywhere; }
.m-text > :first-child { margin-top: 0; }
.m-text > :last-child { margin-bottom: 0; }
.m-text pre { position: relative; background: var(--code); border: 1px solid var(--line); border-radius: 10px; padding: 12px; overflow-x: auto; font-size: 14px; line-height: 1.45; }
.m-text code { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-size: .9em; }
.m-text :not(pre) > code { background: var(--code); border: 1px solid var(--line); border-radius: 6px; padding: 1px 5px; }
.m-text table { border-collapse: collapse; display: block; overflow-x: auto; margin: 10px 0; }
.m-text th, .m-text td { border: 1px solid var(--line); padding: 6px 10px; text-align: left; vertical-align: top; }
.m-text th { background: var(--panel); }
.m-text blockquote { margin: 8px 0; padding: 2px 14px; border-left: 3px solid var(--line); color: var(--muted); }
.m-text a { color: var(--accent); }
.m-text h1, .m-text h2, .m-text h3, .m-text h4 { line-height: 1.3; margin: 18px 0 8px; }
.m-text h1 { font-size: 1.4em; } .m-text h2 { font-size: 1.2em; } .m-text h3 { font-size: 1.07em; }
.copy { position: absolute; top: 6px; right: 6px; font-size: 12px; padding: 2px 8px; background: var(--card); border: 1px solid var(--line); opacity: .8; }
.pathlink { cursor: pointer; text-decoration: underline dotted; }

.tool { margin: 4px 0; border: 1px solid var(--line); border-radius: 10px; background: var(--card); font-size: 14px; }
.tool > summary { list-style: none; cursor: pointer; padding: 6px 10px; display: flex; gap: 8px; align-items: center; color: var(--muted); }
.tool > summary::-webkit-details-marker { display: none; }
.tool .tn { font-weight: 600; color: var(--text); flex: none; }
.tool .ts { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 13px; }
.tool .st { flex: none; }
.tool.err .st { color: var(--danger); }
.tool pre { margin: 0; padding: 8px 10px; border-top: 1px solid var(--line); white-space: pre-wrap; overflow-wrap: anywhere; max-height: 320px; overflow-y: auto; font-size: 12.5px; background: var(--code); }
.tools-run { margin: 8px 0; }

.chip { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line); background: var(--card); border-radius: 999px; padding: 3px 10px; font-size: 13px; max-width: 100%; }
.chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip button { padding: 0 4px; border: 0; }
.chip a { color: var(--accent); text-decoration: none; }
.file-out { margin: 6px 0; }

.perm { border: 1px solid var(--accent); background: var(--accent-soft); border-radius: 14px; padding: 12px 14px; margin: 10px 0; }
.perm h4 { margin: 0 0 6px; font-size: 15px; }
.perm pre { white-space: pre-wrap; overflow-wrap: anywhere; max-height: 260px; overflow-y: auto; background: var(--card); border-radius: 8px; padding: 8px; font-size: 12.5px; margin: 6px 0; }
.perm .btns { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.perm.done { border-color: var(--line); background: transparent; color: var(--muted); padding: 6px 12px; font-size: 14px; }
.q { margin: 10px 0; }
.q-title { font-weight: 600; margin-bottom: 6px; }
.q-opt { display: block; width: 100%; text-align: left; margin: 4px 0; border-color: var(--line); background: var(--card); }
.q-opt.sel { border-color: var(--accent); outline: 2px solid var(--accent); }
.q-opt small { display: block; color: var(--muted); }
.q-other { width: 100%; margin-top: 4px; }

.note { text-align: center; color: var(--muted); font-size: 13px; margin: 10px auto; }
.m-err { border: 1px solid var(--danger); color: var(--danger); border-radius: 10px; padding: 8px 12px; white-space: pre-wrap; font-size: 14px; margin: 10px auto; }
.meta { color: var(--muted); font-size: 12px; margin: 2px auto 14px; }
.working { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 14px; margin: 12px auto; }
.spinner { width: 14px; height: 14px; border-radius: 50%; border: 2px solid var(--line); border-top-color: var(--accent); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.composer { border-top: 1px solid var(--line); padding: 10px 12px calc(10px + env(safe-area-inset-bottom)); background: var(--bg); }
.composer > * { max-width: var(--max); margin-left: auto; margin-right: auto; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chips:not(:empty) { margin-bottom: 8px; }
.input-row { display: flex; align-items: flex-end; gap: 8px; background: var(--card); border: 1px solid var(--line); border-radius: 18px; padding: 6px; }
.input-row:focus-within { border-color: var(--accent); }
#input { flex: 1; border: 0; resize: none; max-height: 40vh; padding: 8px 6px; background: transparent; outline: none; }
.input-row .icon { border-radius: 12px; }
.opts { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; }
.opts select { font-size: 13px; padding: 4px 8px; flex: 1 1 140px; min-width: 0; }

.files-pane { width: var(--files-w); flex: none; border-left: 1px solid var(--line); background: var(--panel); display: flex; flex-direction: column; min-height: 0; }
.files-head { display: flex; align-items: center; gap: 8px; padding: 8px 12px; border-bottom: 1px solid var(--line); min-height: 56px; }
.files-path { flex: 1; color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.files-list { flex: 1; overflow-y: auto; padding: 6px; }
.f-row { display: flex; align-items: center; gap: 8px; padding: 6px 8px; border-radius: 8px; font-size: 14px; }
.f-row:hover { background: var(--card); }
.f-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: pointer; }
.f-size { color: var(--muted); font-size: 12px; flex: none; }
.f-row a { flex: none; text-decoration: none; color: var(--accent); }

.dlg { border: 1px solid var(--line); border-radius: 16px; background: var(--card); color: var(--text); width: min(420px, calc(100vw - 32px)); padding: 20px; box-shadow: var(--shadow); }
.dlg::backdrop { background: rgba(0,0,0,.35); }
.dlg h2 { margin: 0 0 12px; font-size: 18px; }
.dlg label { display: grid; gap: 6px; margin-bottom: 10px; font-size: 14px; color: var(--muted); }
.dlg-btns { display: flex; justify-content: flex-end; gap: 8px; margin-top: 12px; }
.dlg-col { display: grid; gap: 8px; }
.hint { font-size: 13px; color: var(--muted); margin: 4px 0; }
.dlg-wide { width: min(920px, calc(100vw - 24px)); }
.screen { background: #111; color: #e8e8e8; border-radius: 10px; padding: 10px; font: 12px/1.35 ui-monospace, Menlo, Consolas, monospace; white-space: pre; overflow: auto; max-height: 60vh; margin: 0; }
.keys { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.keys button { min-width: 48px; }
body.simple .tool, body.simple .opts, body.simple .meta, body.simple .perm pre { display: none !important; }
.sess { position: relative; }
.sess-x { display: none; flex: none; border: 0; padding: 0 4px; background: transparent; color: var(--muted); font-size: 15px; line-height: 1; }
.sess:hover .sess-x, .sess.active .sess-x { display: inline-block; }
.sess-x:hover { color: var(--danger); }
@media (pointer: coarse) { .sess-x { display: inline-block; } }
.hidden-link { font-size: 12px; color: var(--muted); padding: 2px 10px 8px; cursor: pointer; text-decoration: underline dotted; }
.usage { position: relative; width: 38px; height: 38px; padding: 0; border: 0; flex: none; }
.usage svg { width: 38px; height: 38px; transform: rotate(-90deg); }
.u-bg, .u-arc { fill: none; stroke-width: 4; }
.u-bg { stroke: var(--line); }
.u-arc { stroke: var(--accent); stroke-linecap: round; stroke-dasharray: 94.25; stroke-dashoffset: 94.25; transition: stroke-dashoffset .4s; }
.usage.warning .u-arc { stroke: #d39b2a; }
.usage.critical .u-arc { stroke: var(--danger); }
.u-pct { position: absolute; inset: 0; display: grid; place-items: center; font-size: 10px; font-weight: 600; }
.u-row { margin: 10px 0; }
.u-row .u-head { display: flex; justify-content: space-between; gap: 8px; font-size: 14px; }
.u-bar { height: 8px; border-radius: 4px; background: var(--line); overflow: hidden; margin: 4px 0 2px; }
.u-bar > i { display: block; height: 100%; background: var(--accent); }
.u-row.warning .u-bar > i { background: #d39b2a; }
.u-row.critical .u-bar > i { background: var(--danger); }
.u-reset { font-size: 12px; color: var(--muted); }
.diff { margin: 0; border-top: 1px solid var(--line); font: 12.5px/1.45 ui-monospace, Menlo, Consolas, monospace; max-height: 340px; overflow: auto; }
.diff div { white-space: pre-wrap; overflow-wrap: anywhere; padding: 0 10px; }
.diff .del { background: color-mix(in srgb, var(--danger) 16%, transparent); }
.diff .add { background: color-mix(in srgb, var(--accent) 18%, transparent); }
.todos { list-style: none; margin: 0; padding: 8px 12px; border-top: 1px solid var(--line); font-size: 14px; }
.todos li { padding: 2px 0; }
.todos li.done { color: var(--muted); text-decoration: line-through; }
.todos li.active { font-weight: 600; }
.sess .tag { font-size: 11px; color: var(--muted); border: 1px solid var(--line); border-radius: 6px; padding: 0 5px; flex: none; }

.drop { position: fixed; inset: 12px; border: 3px dashed var(--accent); border-radius: 20px; background: color-mix(in srgb, var(--bg) 85%, transparent); display: grid; place-items: center; font-size: 22px; z-index: 50; pointer-events: none; }
.toast { position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 8px 16px; border-radius: 10px; z-index: 60; max-width: calc(100vw - 32px); }
.scrim { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 19; }

@media (max-width: 1100px) {
  .files-pane { position: fixed; right: 0; top: 0; bottom: 0; z-index: 20; width: min(var(--files-w), 100vw); box-shadow: var(--shadow); }
}
@media (max-width: 760px) {
  #menuBtn { display: inline-grid; }
  .side { position: fixed; left: 0; top: 0; bottom: 0; z-index: 20; transform: translateX(-100%); transition: transform .2s; width: min(var(--side-w), 85vw); }
  .side.open { transform: none; box-shadow: var(--shadow); }
  .msgs { padding: 14px 16px 30px; }
  .m-user > div { max-width: 92%; }
  .opts select { flex: 1 1 30%; }
}
