/* Строгий и лёгкий интерфейс без закруглений: мало рамок, одна заливка на экран (главное действие). */
:root {
  --bg: #ffffff;
  --side: #f7f7f5;
  --soft: #f4f3f0;
  --line: #ebe9e4;
  --line-strong: #d6d4ce;
  --text: #1d1d1b;
  --muted: #76756f;
  --faint: #a3a29c;
  --accent: #b5502b;
  --accent-soft: #fbefe9;
  --on-color: #ffffff;
  --hover: #f2f1ed;
  --sel: #efede8;
  --term-bg: #16171a;
  --term-text: #d6d6d0;
  --term-muted: #7d7e78;
  --term-sys: #e0a36b;
  --term-err: #ef7a6c;
  --ok: #2f7d4f;
  --err: #b3362b;
  --st-new: #8a8984;
  --st-queued: #2462b4;
  --st-in_progress: #7a45b0;
  --st-question: #c2562f;
  --st-testing: #a66a00;
  --st-done: #2f7d4f;
  --shadow: 0 8px 28px rgba(0, 0, 0, .10);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #18191b; --side: #141517; --soft: #202124; --line: #2a2b2f; --line-strong: #3a3b40;
    --text: #e6e5e1; --muted: #9a9993; --faint: #6c6b66; --accent: #e07a4f; --accent-soft: #33251f; --on-color: #141517;
    --hover: #222327; --sel: #26272b; --term-bg: #0e0f11;
    --ok: #5fbf85; --err: #ef7a6c;
    --st-new: #9a9993; --st-queued: #6ea2ea; --st-in_progress: #b48be3; --st-question: #e07a4f; --st-testing: #e0a84a; --st-done: #5fbf85;
    --shadow: 0 8px 28px rgba(0, 0, 0, .45);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #18191b; --side: #141517; --soft: #202124; --line: #2a2b2f; --line-strong: #3a3b40;
  --text: #e6e5e1; --muted: #9a9993; --faint: #6c6b66; --accent: #e07a4f; --accent-soft: #33251f; --on-color: #141517;
  --hover: #222327; --sel: #26272b; --term-bg: #0e0f11;
  --ok: #5fbf85; --err: #ef7a6c;
  --st-new: #9a9993; --st-queued: #6ea2ea; --st-in_progress: #b48be3; --st-question: #e07a4f; --st-testing: #e0a84a; --st-done: #5fbf85;
  --shadow: 0 8px 28px rgba(0, 0, 0, .45);
  color-scheme: dark;
}

* { box-sizing: border-box; border-radius: 0 !important; }
html, body, #app { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 14px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input, textarea, select { font: inherit; color: inherit; }
a { color: var(--accent); }
svg { flex: none; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.muted { color: var(--muted); }
.hidden { display: none !important; }

/* ---------- buttons & fields */
.btn {
  display: inline-flex; align-items: center; gap: 7px; height: 32px; padding: 0 12px;
  border: 1px solid var(--line-strong); background: transparent; cursor: pointer; white-space: nowrap;
  font-weight: 500; transition: background .12s, border-color .12s, color .12s;
}
.btn:hover { background: var(--hover); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-color); }
.btn.primary:hover { filter: brightness(1.07); }
.btn.run { color: var(--ok); border-color: var(--ok); }
.btn.run:hover { background: var(--ok); color: var(--on-color); }
.btn.stop { color: var(--err); border-color: var(--err); }
.btn.stop:hover { background: var(--err); color: var(--on-color); }
.btn.danger { color: var(--err); }
.btn.ghost { border-color: transparent; }
.btn.ghost:hover { background: var(--hover); }
.btn.link { border-color: transparent; color: var(--accent); padding: 0 8px; }
.btn.link:hover { background: var(--accent-soft); }
.btn.icon { width: 32px; padding: 0; justify-content: center; color: var(--muted); }
.btn.icon:hover { color: var(--text); }
.btn.on { background: var(--sel); border-color: var(--line-strong); }
.field {
  width: 100%; height: 36px; padding: 0 10px; border: 1px solid var(--line-strong); background: var(--bg); outline: none;
}
.field:focus { border-color: var(--accent); }
label.lbl { display: block; font-size: 12px; color: var(--muted); margin: 0 0 5px; }
.hint { font-size: 12px; color: var(--faint); }

/* ---------- login */
.login-wrap { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; background: var(--side); }
.login { width: 100%; max-width: 340px; background: var(--bg); border: 1px solid var(--line); padding: 32px 28px 28px; }
.login .mark { width: 22px; height: 22px; background: var(--accent); margin-bottom: 18px; }
.login h1 { margin: 0 0 4px; font-size: 20px; font-weight: 600; }
.login p { margin: 0 0 22px; color: var(--muted); }
.login .row { margin-bottom: 14px; }
.login .btn { width: 100%; justify-content: center; height: 38px; margin-top: 6px; }
.login .err { color: var(--err); min-height: 20px; margin-top: 10px; font-size: 13px; }

/* ---------- shell */
.shell { height: 100%; height: 100dvh; display: grid; grid-template-rows: 44px 1fr; }
.topbar { display: flex; align-items: center; gap: 8px; padding: 0 10px 0 16px; border-bottom: 1px solid var(--line); background: var(--side); }
.brand { display: flex; align-items: center; gap: 9px; font-weight: 600; }
.brand-mark { width: 14px; height: 14px; background: var(--accent); }
.topbar .spacer { flex: 1; }
.auth-badge { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); margin-right: 4px; }
.auth-badge::before { content: ""; width: 7px; height: 7px; background: var(--faint); }
.auth-badge.ok::before { background: var(--ok); }
.auth-badge.bad { color: var(--err); }
.auth-badge.bad::before { background: var(--err); }

.body { display: grid; grid-template-columns: 220px minmax(0, 1fr); min-height: 0; }
.sidebar { background: var(--side); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.side-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 10px 6px 16px; }
.side-head h2 { margin: 0; font-size: 12px; color: var(--muted); font-weight: 500; }
.plist { overflow: auto; flex: 1; padding: 2px 0 12px; }
.pitem { display: flex; align-items: center; gap: 10px; padding: 7px 16px; cursor: pointer; color: var(--muted); }
.pitem:hover { background: var(--hover); color: var(--text); }
.pitem.active { background: var(--sel); color: var(--text); font-weight: 500; }
.pitem .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pitem .cnt { font-size: 12px; color: var(--faint); font-weight: 400; }
.dot { width: 7px; height: 7px; background: var(--line-strong); flex: none; }
.dot.idle { background: var(--ok); }
.dot.running { background: var(--ok); animation: pulse 1.2s infinite; }
.dot.stalled, .dot.error { background: var(--err); }
.dot.unread { background: var(--accent); }
@keyframes pulse { 50% { opacity: .3; } }

.main { display: grid; grid-template-rows: auto 1fr; min-width: 0; min-height: 0; position: relative; }
.pbar { display: flex; align-items: center; gap: 14px; padding: 10px 16px 10px 20px; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.pbar .ptitle { min-width: 0; flex: 1; display: flex; align-items: baseline; gap: 10px; }
.pbar .ptitle h1 { margin: 0; font-size: 16px; font-weight: 600; white-space: nowrap; }
.pbar .ptitle .path { font-size: 12px; color: var(--faint); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.runbox { display: flex; align-items: center; gap: 8px; }
.runstate { display: flex; align-items: center; gap: 7px; font-size: 13px; color: var(--muted); margin-right: 6px; }

.cols { display: grid; grid-template-columns: 300px minmax(0, 1fr); min-height: 0; }
.topic-area { display: grid; grid-template-columns: minmax(0, 1fr); min-height: 0; min-width: 0; }
.tcol { border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.col-head { display: flex; align-items: center; gap: 2px; padding: 8px 8px 8px 12px; border-bottom: 1px solid var(--line); }
.col-head .spacer { flex: 1; }
.tab { height: 28px; padding: 0 8px; border: 0; background: transparent; cursor: pointer; color: var(--muted); font-size: 13px; }
.tab:hover { color: var(--text); }
.tab.on { color: var(--text); font-weight: 600; box-shadow: inset 0 -2px 0 var(--accent); }
.tlist { overflow: auto; flex: 1; }
.titem { padding: 10px 14px 10px 16px; cursor: pointer; border-bottom: 1px solid var(--line); }
.titem:hover { background: var(--hover); }
.titem.active { background: var(--sel); box-shadow: inset 2px 0 0 var(--accent); }
.titem .t1 { display: flex; align-items: center; gap: 8px; }
.titem .t1 .title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.titem.unread .title { font-weight: 600; }
.titem .t2 { display: flex; align-items: center; gap: 8px; margin-top: 3px; font-size: 12px; color: var(--faint); }
.empty { padding: 32px 20px; text-align: center; color: var(--faint); }

.st { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; }
.st::before { content: ""; width: 6px; height: 6px; background: currentColor; }
.s-new { color: var(--st-new); } .s-queued { color: var(--st-queued); } .s-in_progress { color: var(--st-in_progress); }
.s-question { color: var(--st-question); } .s-testing { color: var(--st-testing); } .s-done { color: var(--st-done); }

/* статусы — сразу кнопками */
.stbar { display: flex; flex-wrap: wrap; }
.stbtn {
  height: 28px; padding: 0 10px; border: 1px solid var(--line-strong); margin-left: -1px; background: transparent; cursor: pointer;
  font-size: 12.5px; color: var(--muted); display: inline-flex; align-items: center; gap: 6px; white-space: nowrap;
}
.stbtn:first-child { margin-left: 0; }
.stbtn::before { content: ""; width: 6px; height: 6px; background: var(--c); }
.stbtn:hover { color: var(--text); background: var(--hover); }
.stbtn.on { background: var(--c); border-color: var(--c); color: var(--on-color); position: relative; z-index: 1; font-weight: 600; }
.stbtn.on::before { background: var(--on-color); }

/* ---------- topic */
.topic { display: grid; grid-template-rows: auto auto 1fr; grid-template-columns: minmax(0, 1fr); min-height: 0; min-width: 0; }
.topic-head { padding: 12px 20px 12px 24px; border-bottom: 1px solid var(--line); }
.topic-head .row1 { display: flex; align-items: center; gap: 4px; margin-bottom: 10px; }
.topic-head h2 { margin: 0; font-size: 16px; font-weight: 600; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msgs { overflow: auto; padding: 8px 24px 24px; }
.msg { max-width: 820px; padding: 14px 0 0; }
.msg + .msg { border-top: 1px solid var(--line); margin-top: 14px; }
.msg .meta { display: flex; gap: 10px; align-items: baseline; font-size: 12px; color: var(--faint); margin-bottom: 4px; }
.msg .meta b { color: var(--text); font-weight: 600; font-size: 13px; }
.msg.assistant .meta b { color: var(--accent); }
.content { word-wrap: break-word; overflow-wrap: anywhere; }
.content.plain { white-space: pre-wrap; }
.content p, .content div { margin: 0; }
.content code { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 12.5px; background: var(--soft); padding: 1px 4px; }
.content pre { background: var(--soft); padding: 10px 12px; overflow: auto; margin: 6px 0; white-space: pre; }
.content pre code { background: none; padding: 0; }
.content img { display: block; max-width: min(100%, 560px); max-height: 380px; border: 1px solid var(--line); margin: 8px 0; cursor: zoom-in; background: var(--soft); }
.content a[data-file], .editor a[data-file] {
  display: inline-flex; align-items: center; gap: 6px; padding: 1px 8px; border: 1px solid var(--line-strong);
  color: var(--text); text-decoration: none; font-size: 13px; background: var(--soft); vertical-align: baseline; margin: 1px 0;
}
.content a[data-file]::before, .editor a[data-file]::before { content: ""; width: 9px; height: 11px; flex: none; border: 1.5px solid currentColor; opacity: .5; }
.content a[data-file]::after, .editor a[data-file]::after { content: attr(data-size); color: var(--muted); font-size: 12px; }
.content a[data-file]:hover { border-color: var(--accent); }

/* ---------- editor (contenteditable, как в CRM) */
.composer { border-bottom: 1px solid var(--line); padding: 12px 20px 12px 24px; }
.ebox { border: 1px solid var(--line-strong); background: var(--bg); }
.ebox:focus-within { border-color: var(--accent); }
.ebox.drag { border-color: var(--accent); background: var(--accent-soft); }
.editor { min-height: 64px; max-height: 42vh; overflow: auto; padding: 10px 12px; outline: none; word-wrap: break-word; overflow-wrap: anywhere; }
.editor.tall { min-height: 200px; max-height: 55vh; }
.editor:empty::before { content: attr(data-ph); color: var(--faint); pointer-events: none; }
.editor img { display: block; max-width: min(100%, 480px); max-height: 300px; border: 1px solid var(--line); margin: 6px 0; }
.editor p, .editor div { margin: 0; }
.etools { display: flex; align-items: center; gap: 6px; padding: 6px 6px 6px 8px; border-top: 1px solid var(--line); }
.etools .spacer { flex: 1; }
.uploading { font-size: 12px; color: var(--accent); }

.newtopic { padding: 22px 24px; overflow: auto; }
.newtopic .inner { max-width: 780px; }
.newtopic h2 { margin: 0 0 16px; font-size: 17px; font-weight: 600; }
.newtopic .row { margin-bottom: 14px; }

.placeholder { display: grid; place-items: center; color: var(--faint); padding: 40px; text-align: center; }
.placeholder h3 { color: var(--muted); margin: 0 0 6px; font-weight: 500; font-size: 15px; }

/* ---------- terminal */
.term {
  position: absolute; left: 0; right: 0; bottom: 0; height: min(52vh, 520px); background: var(--term-bg); color: var(--term-text);
  display: grid; grid-template-rows: 36px 1fr; z-index: 5; box-shadow: var(--shadow);
}
.term-head { display: flex; align-items: center; gap: 8px; padding: 0 6px 0 14px; border-bottom: 1px solid #26272c; font-size: 12.5px; color: var(--term-muted); }
.term-head .spacer { flex: 1; }
.term-head .btn { border-color: transparent; color: var(--term-text); height: 26px; font-size: 12px; }
.term-head .btn:hover { background: #26272c; }
.term-body { overflow: auto; padding: 10px 14px 16px; font: 12.5px/1.55 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; white-space: pre-wrap; word-break: break-word; }
.term-line .ts { color: var(--term-muted); margin-right: 10px; }
.term-line.system { color: var(--term-sys); }
.term-line.stderr { color: var(--term-err); }

/* ---------- dialog, viewer, toast */
.modal-bg { position: fixed; inset: 0; background: rgba(0, 0, 0, .35); display: grid; place-items: center; padding: 16px; z-index: 20; }
.modal { background: var(--bg); border: 1px solid var(--line); width: 100%; max-width: 440px; box-shadow: var(--shadow); }
.modal h3 { margin: 0; padding: 16px 20px 0; font-size: 15px; font-weight: 600; }
.modal .mb { padding: 14px 20px 18px; }
.modal .mb .row { margin-bottom: 14px; }
.modal .mb .row:last-child { margin-bottom: 0; }
.modal .mf { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 20px; border-top: 1px solid var(--line); }
.viewer { position: fixed; inset: 0; background: rgba(0, 0, 0, .85); display: grid; place-items: center; z-index: 25; cursor: zoom-out; padding: 20px; }
.viewer img { max-width: 100%; max-height: 100%; }
.toast { position: fixed; right: 16px; bottom: 16px; background: var(--text); color: var(--bg); padding: 10px 14px; z-index: 30; max-width: 380px; box-shadow: var(--shadow); }

.back { display: none; }

/* ---------- телефон: один столбец с «назад», крупные кнопки, поля 16px (иначе iOS приближает) */
@media (max-width: 900px) {
  .body, .cols { grid-template-columns: minmax(0, 1fr); }
  .back { display: inline-flex; }
  .shell[data-view="projects"] .main { display: none; }
  .shell:not([data-view="projects"]) .sidebar { display: none; }
  .shell[data-view="topics"] .topic-area { display: none; }
  .shell[data-view="topic"] .tcol { display: none; }
  .field, .editor { font-size: 16px; }
  .btn { height: 38px; }
  .btn.icon { width: 38px; }
  .pitem { padding: 12px 16px; font-size: 15px; }
  .titem { padding: 12px 16px; }
  .pbar { padding: 8px 12px; gap: 8px; }
  .pbar .ptitle { flex: 1 1 calc(100% - 50px); flex-wrap: wrap; column-gap: 2px; align-items: center; }
  .pbar .ptitle .path { order: 3; flex-basis: 100%; }
  .runbox { width: 100%; gap: 8px; }
  .runbox .runstate { display: none; }
  .runbox .btn { flex: 1; justify-content: center; }
  .topic-head { padding: 10px 12px; }
  .stbar { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; max-width: 100%; }
  .content a[data-file] { max-width: 100%; }
  .stbtn { height: 34px; flex: none; }
  .msgs { padding: 4px 14px 18px; }
  .composer { padding: 10px 12px; }
  .msgs { padding-bottom: calc(18px + env(safe-area-inset-bottom)); }
  .editor { min-height: 48px; }
  .hint.kbd { display: none; }
  .newtopic { padding: 14px 12px; }
  .term { height: 75dvh; }
  .modal-bg { align-items: end; padding: 0; }
  .modal { max-width: none; }
  .auth-badge .long { display: none; }
}
